1
----------------------------------------------------------------------
2
-- Rules.Unsafe_Unchecked_Conversion - Package body --
4
-- This software is (c) SAGEM DS and Adalog 2004-2006. The Ada --
5
-- Controller is free software; you can redistribute it and/or --
6
-- modify it under terms of the GNU General Public License as --
7
-- published by the Free Software Foundation; either version 2, or --
8
-- (at your option) any later version. This unit is distributed --
9
-- in the hope that it will be useful, but WITHOUT ANY WARRANTY; --
10
-- without even the implied warranty of MERCHANTABILITY or FITNESS --
11
-- FOR A PARTICULAR PURPOSE. See the GNU General Public License --
12
-- for more details. You should have received a copy of the GNU --
13
-- General Public License distributed with this program; see file --
14
-- COPYING. If not, write to the Free Software Foundation, 59 --
15
-- Temple Place - Suite 330, Boston, MA 02111-1307, USA. --
17
-- As a special exception, if other files instantiate generics --
18
-- from the units of this program, or if you link this unit with --
19
-- other files to produce an executable, this unit does not by --
20
-- itself cause the resulting executable to be covered by the GNU --
21
-- General Public License. This exception does not however --
22
-- invalidate any other reasons why the executable file might be --
23
-- covered by the GNU Public License. --
25
-- This software is distributed in the hope that it will be --
26
-- useful, but WITHOUT ANY WARRANTY; without even the implied --
27
-- warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR --
29
----------------------------------------------------------------------
47
Framework.Rules_Manager,
50
package body Rules.Unsafe_Unchecked_Conversion is
53
Rule_Used : Boolean := False;
55
Context : Framework.Basic_Rule_Context;
64
User_Message ("Rule: " & Rule_Id);
65
User_Message ("Parameter(s): none");
66
User_Message ("Control unsafe usage of Unchecked_Conversion");
73
procedure Add_Use (Label : in Wide_String; Rule_Type : in Rule_Types) is
74
use Framework.Language;
78
Parameter_Error (Rule_Id, "rule already specified");
81
if Parameter_Exists then
82
Parameter_Error (Rule_Id, "no parameter for rule");
85
Context := Basic.New_Context (Rule_Type, Label);
93
procedure Command (Action : Framework.Rules_Manager.Rule_Action) is
94
use Framework.Rules_Manager;
100
Save_Used := Rule_Used;
103
Rule_Used := Save_Used;
108
--------------------------
109
-- Process_Instantation --
110
--------------------------
112
procedure Process_Instantiation (Instantiation : in Asis.Declaration) is
113
use Asis, Asis.Declarations, Asis.Elements, Asis.Expressions;
114
use Framework.Reports, Thick_Queries, Utilities;
116
Source, Target : Asis.Expression;
117
S_Size, T_Size : Integer;
118
Assocs : Asis.Association_List (1..2);
120
Not_Specified : constant Integer := -1;
121
Class_Wide : constant Integer := -2;
123
function Size_Value (Type_Name : Asis.Expression) return Integer is
125
Good_Name : Asis.Expression := Type_Name;
127
if Expression_Kind (Good_Name) = An_Attribute_Reference then
128
case A4G_Bugs.Attribute_Kind (Good_Name) is
129
when A_Base_Attribute =>
130
Good_Name := Prefix (Good_Name);
131
when A_Class_Attribute =>
134
Failure ("unexpected attribute", Good_Name);
139
Reprs : constant Asis.Representation_Clause_List
140
:= Corresponding_Representation_Clauses (Corresponding_Name_Declaration (Good_Name));
142
for R in Reprs'Range loop
143
if Representation_Clause_Kind (Reprs (R)) = An_Attribute_Definition_Clause
144
and then A4G_Bugs.Attribute_Kind (Representation_Clause_Name (Reprs (R))) = A_Size_Attribute
147
Val_Img : constant Wide_String := Static_Expression_Value_Image
148
(Representation_Clause_Expression (Reprs (R)));
151
Uncheckable (Rule_Id,
153
Get_Location (Representation_Clause_Expression (Reprs (R))),
154
"unable to evaluate size clause value");
155
return Not_Specified;
157
return Integer'Wide_Value (Val_Img);
163
-- No size clause found
164
return Not_Specified;
168
Reported : Boolean := False;
170
if not Rule_Used then
173
Rules_Manager.Enter (Rule_Id);
176
Name_Image : constant Wide_String := To_Upper (Full_Name_Image
178
(Generic_Unit_Name (Instantiation))));
180
if Name_Image /= "ADA.UNCHECKED_CONVERSION" and Name_Image /= "UNCHECKED_CONVERSION" then
181
-- In Gnat, Unchecked_Conversion is not a renaming of Ada.Unchecked_Conversion
186
Assocs := Generic_Actual_Part (Instantiation);
187
Source := Actual_Parameter (Assocs (1));
188
if Expression_Kind (Source) = A_Selected_Component then
189
Source := Selector (Source);
191
Target := Actual_Parameter (Assocs (2));
192
if Expression_Kind (Target) = A_Selected_Component then
193
Target := Selector (Target);
196
S_Size := Size_Value (Source);
197
T_Size := Size_Value (Target);
199
if S_Size = Not_Specified then
202
Get_Location (Source),
203
"no size clause given for Source");
206
if T_Size = Not_Specified then
209
Get_Location (Target),
210
"no size clause given for Target");
214
if S_Size = Class_Wide then
217
Get_Location (Source),
218
"class-wide type given for Source");
221
if T_Size = Class_Wide then
224
Get_Location (Target),
225
"class-wide type given for Target");
233
-- Here, S_Size and T_Size are known
234
if S_Size /= T_Size then
237
Get_Location (Source),
238
"Source size (" & Integer_Img (S_Size) & ") /= Target size (" & Integer_Img (T_Size) & ')');
241
end Process_Instantiation;
244
Framework.Rules_Manager.Register_Semantic (Rule_Id,
246
Add_Use => Add_Use'Access,
247
Command => Command'Access);
248
end Rules.Unsafe_Unchecked_Conversion;