sem_ch3.adb, [...]: Minor reformatting.
[gcc.git] / gcc / ada / exp_spark.adb
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- E X P _ S P A R K --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2016, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
25
26 with Atree; use Atree;
27 with Checks; use Checks;
28 with Einfo; use Einfo;
29 with Exp_Ch5; use Exp_Ch5;
30 with Exp_Dbug; use Exp_Dbug;
31 with Exp_Util; use Exp_Util;
32 with Namet; use Namet;
33 with Nlists; use Nlists;
34 with Nmake; use Nmake;
35 with Rtsfind; use Rtsfind;
36 with Sem_Res; use Sem_Res;
37 with Sem_Util; use Sem_Util;
38 with Sinfo; use Sinfo;
39 with Snames; use Snames;
40 with Tbuild; use Tbuild;
41 with Uintp; use Uintp;
42 with Sem_Eval; use Sem_Eval;
43 with Stand; use Stand;
44
45 package body Exp_SPARK is
46
47 -----------------------
48 -- Local Subprograms --
49 -----------------------
50
51 procedure Expand_SPARK_Attribute_Reference (N : Node_Id);
52 -- Replace occurrences of System'To_Address by calls to
53 -- System.Storage_Elements.To_Address
54
55 procedure Expand_SPARK_N_Object_Renaming_Declaration (N : Node_Id);
56 -- Perform name evaluation for a renamed object
57
58 ------------------
59 -- Expand_SPARK --
60 ------------------
61
62 procedure Expand_SPARK (N : Node_Id) is
63 begin
64 case Nkind (N) is
65
66 -- Qualification of entity names in formal verification mode
67 -- is limited to the addition of a suffix for homonyms (see
68 -- Exp_Dbug.Qualify_Entity_Name). We used to qualify entity names
69 -- as full expansion does, but this was removed as this prevents the
70 -- verification back-end from using a short name for debugging and
71 -- user interaction. The verification back-end already takes care
72 -- of qualifying names when needed.
73
74 when N_Block_Statement
75 | N_Entry_Declaration
76 | N_Package_Body
77 | N_Package_Declaration
78 | N_Protected_Type_Declaration
79 | N_Subprogram_Body
80 | N_Task_Type_Declaration
81 =>
82 Qualify_Entity_Names (N);
83
84 when N_Expanded_Name
85 | N_Identifier
86 =>
87 Expand_SPARK_Potential_Renaming (N);
88
89 when N_Object_Renaming_Declaration =>
90 Expand_SPARK_N_Object_Renaming_Declaration (N);
91
92 -- Replace occurrences of System'To_Address by calls to
93 -- System.Storage_Elements.To_Address
94
95 when N_Attribute_Reference =>
96 Expand_SPARK_Attribute_Reference (N);
97
98 -- Loop iterations over arrays need to be expanded, to avoid getting
99 -- two names referring to the same object in memory (the array and
100 -- the iterator) in GNATprove, especially since both can be written
101 -- (thus possibly leading to interferences due to aliasing). No such
102 -- problem arises with quantified expressions over arrays, which are
103 -- dealt with specially in GNATprove.
104
105 when N_Loop_Statement =>
106 declare
107 Scheme : constant Node_Id := Iteration_Scheme (N);
108 begin
109 if Present (Scheme)
110 and then Present (Iterator_Specification (Scheme))
111 and then
112 Is_Iterator_Over_Array (Iterator_Specification (Scheme))
113 then
114 Expand_Iterator_Loop_Over_Array (N);
115 end if;
116 end;
117
118 -- In SPARK mode, no other constructs require expansion
119
120 when others =>
121 null;
122 end case;
123 end Expand_SPARK;
124
125 --------------------------------------
126 -- Expand_SPARK_Attribute_Reference --
127 --------------------------------------
128
129 procedure Expand_SPARK_Attribute_Reference (N : Node_Id) is
130 Aname : constant Name_Id := Attribute_Name (N);
131 Attr_Id : constant Attribute_Id := Get_Attribute_Id (Aname);
132 Loc : constant Source_Ptr := Sloc (N);
133 Typ : constant Entity_Id := Etype (N);
134 Expr : Node_Id;
135
136 begin
137 if Attr_Id = Attribute_To_Address then
138
139 -- Extract and convert argument to expected type for call
140
141 Expr :=
142 Make_Type_Conversion (Loc,
143 Subtype_Mark =>
144 New_Occurrence_Of (RTE (RE_Integer_Address), Loc),
145 Expression => Relocate_Node (First (Expressions (N))));
146
147 -- Replace attribute reference with call
148
149 Rewrite (N,
150 Make_Function_Call (Loc,
151 Name =>
152 New_Occurrence_Of (RTE (RE_To_Address), Loc),
153 Parameter_Associations => New_List (Expr)));
154 Analyze_And_Resolve (N, Typ);
155
156 -- For attributes which return Universal_Integer, introduce a conversion
157 -- to the expected type with the appropriate check flags set.
158
159 elsif Attr_Id = Attribute_Alignment
160 or else Attr_Id = Attribute_Bit
161 or else Attr_Id = Attribute_Bit_Position
162 or else Attr_Id = Attribute_Descriptor_Size
163 or else Attr_Id = Attribute_First_Bit
164 or else Attr_Id = Attribute_Last_Bit
165 or else Attr_Id = Attribute_Length
166 or else Attr_Id = Attribute_Max_Size_In_Storage_Elements
167 or else Attr_Id = Attribute_Pos
168 or else Attr_Id = Attribute_Position
169 or else Attr_Id = Attribute_Range_Length
170 or else Attr_Id = Attribute_Object_Size
171 or else Attr_Id = Attribute_Size
172 or else Attr_Id = Attribute_Value_Size
173 or else Attr_Id = Attribute_VADS_Size
174 or else Attr_Id = Attribute_Aft
175 or else Attr_Id = Attribute_Max_Alignment_For_Allocation
176 then
177 -- If the expected type is Long_Long_Integer, there will be no check
178 -- flag as the compiler assumes attributes always fit in this type.
179 -- Since in SPARK_Mode we do not take Storage_Error into account, we
180 -- cannot make this assumption and need to produce a check.
181 -- ??? It should be enough to add this check for attributes 'Length
182 -- and 'Range_Length when the type is as big as Long_Long_Integer.
183
184 declare
185 Typ : Entity_Id := Empty;
186 begin
187 if Attr_Id = Attribute_Range_Length then
188 Typ := Etype (Prefix (N));
189
190 elsif Attr_Id = Attribute_Length then
191 Typ := Etype (Prefix (N));
192
193 declare
194 Indx : Node_Id;
195 J : Int;
196
197 begin
198 if Is_Access_Type (Typ) then
199 Typ := Designated_Type (Typ);
200 end if;
201
202 if No (Expressions (N)) then
203 J := 1;
204 else
205 J := UI_To_Int (Expr_Value (First (Expressions (N))));
206 end if;
207
208 Indx := First_Index (Typ);
209 while J > 1 loop
210 Next_Index (Indx);
211 J := J - 1;
212 end loop;
213
214 Typ := Etype (Indx);
215 end;
216 end if;
217
218 Apply_Universal_Integer_Attribute_Checks (N);
219
220 if Present (Typ)
221 and then RM_Size (Typ) = RM_Size (Standard_Long_Long_Integer)
222 then
223 Set_Do_Overflow_Check (N);
224 end if;
225 end;
226 end if;
227 end Expand_SPARK_Attribute_Reference;
228
229 ------------------------------------------------
230 -- Expand_SPARK_N_Object_Renaming_Declaration --
231 ------------------------------------------------
232
233 procedure Expand_SPARK_N_Object_Renaming_Declaration (N : Node_Id) is
234 begin
235 -- Unconditionally remove all side effects from the name
236
237 Evaluate_Name (Name (N));
238 end Expand_SPARK_N_Object_Renaming_Declaration;
239
240 -------------------------------------
241 -- Expand_SPARK_Potential_Renaming --
242 -------------------------------------
243
244 procedure Expand_SPARK_Potential_Renaming (N : Node_Id) is
245 Loc : constant Source_Ptr := Sloc (N);
246 Ren_Id : constant Entity_Id := Entity (N);
247 Typ : constant Entity_Id := Etype (N);
248 Obj_Id : Node_Id;
249
250 begin
251 -- Replace a reference to a renaming with the actual renamed object
252
253 if Ekind (Ren_Id) in Object_Kind then
254 Obj_Id := Renamed_Object (Ren_Id);
255
256 if Present (Obj_Id) then
257
258 -- The renamed object is an entity when instantiating generics
259 -- or inlining bodies. In this case the renaming is part of the
260 -- mapping "prologue" which links actuals to formals.
261
262 if Nkind (Obj_Id) in N_Entity then
263 Rewrite (N, New_Occurrence_Of (Obj_Id, Loc));
264
265 -- Otherwise the renamed object denotes a name
266
267 else
268 Rewrite (N, New_Copy_Tree (Obj_Id, New_Sloc => Loc));
269 Reset_Analyzed_Flags (N);
270 end if;
271
272 Analyze_And_Resolve (N, Typ);
273 end if;
274 end if;
275 end Expand_SPARK_Potential_Renaming;
276
277 end Exp_SPARK;