PR target/81369
[official-gcc.git] / gcc / ada / exp_spark.adb
blob785652e2a43213dbc6f53cc62b95940a653880ac
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-2017, 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 ------------------------------------------------------------------------------
26 with Atree; use Atree;
27 with Checks; use Checks;
28 with Einfo; use Einfo;
29 with Exp_Ch4;
30 with Exp_Ch5; use Exp_Ch5;
31 with Exp_Dbug; use Exp_Dbug;
32 with Exp_Util; use Exp_Util;
33 with Namet; use Namet;
34 with Nlists; use Nlists;
35 with Nmake; use Nmake;
36 with Rtsfind; use Rtsfind;
37 with Sem; use Sem;
38 with Sem_Eval; use Sem_Eval;
39 with Sem_Res; use Sem_Res;
40 with Sem_Util; use Sem_Util;
41 with Sinfo; use Sinfo;
42 with Snames; use Snames;
43 with Stand; use Stand;
44 with Tbuild; use Tbuild;
45 with Uintp; use Uintp;
47 package body Exp_SPARK is
49 -----------------------
50 -- Local Subprograms --
51 -----------------------
53 procedure Expand_SPARK_N_Attribute_Reference (N : Node_Id);
54 -- Replace occurrences of System'To_Address by calls to
55 -- System.Storage_Elements.To_Address
57 procedure Expand_SPARK_Freeze_Type (E : Entity_Id);
58 -- Build the DIC procedure of a type when needed, if not already done
60 procedure Expand_SPARK_N_Object_Declaration (N : Node_Id);
61 -- Perform object-declaration-specific expansion
63 procedure Expand_SPARK_N_Object_Renaming_Declaration (N : Node_Id);
64 -- Perform name evaluation for a renamed object
66 procedure Expand_SPARK_Op_Ne (N : Node_Id);
67 -- Rewrite operator /= based on operator = when defined explicitly
69 ------------------
70 -- Expand_SPARK --
71 ------------------
73 procedure Expand_SPARK (N : Node_Id) is
74 begin
75 case Nkind (N) is
77 -- Qualification of entity names in formal verification mode
78 -- is limited to the addition of a suffix for homonyms (see
79 -- Exp_Dbug.Qualify_Entity_Name). We used to qualify entity names
80 -- as full expansion does, but this was removed as this prevents the
81 -- verification back-end from using a short name for debugging and
82 -- user interaction. The verification back-end already takes care
83 -- of qualifying names when needed.
85 when N_Block_Statement
86 | N_Entry_Declaration
87 | N_Package_Body
88 | N_Package_Declaration
89 | N_Protected_Type_Declaration
90 | N_Subprogram_Body
91 | N_Task_Type_Declaration
93 Qualify_Entity_Names (N);
95 -- Replace occurrences of System'To_Address by calls to
96 -- System.Storage_Elements.To_Address.
98 when N_Attribute_Reference =>
99 Expand_SPARK_N_Attribute_Reference (N);
101 when N_Expanded_Name
102 | N_Identifier
104 Expand_SPARK_Potential_Renaming (N);
106 -- Loop iterations over arrays need to be expanded, to avoid getting
107 -- two names referring to the same object in memory (the array and
108 -- the iterator) in GNATprove, especially since both can be written
109 -- (thus possibly leading to interferences due to aliasing). No such
110 -- problem arises with quantified expressions over arrays, which are
111 -- dealt with specially in GNATprove.
113 when N_Loop_Statement =>
114 declare
115 Scheme : constant Node_Id := Iteration_Scheme (N);
116 begin
117 if Present (Scheme)
118 and then Present (Iterator_Specification (Scheme))
119 and then
120 Is_Iterator_Over_Array (Iterator_Specification (Scheme))
121 then
122 Expand_Iterator_Loop_Over_Array (N);
123 end if;
124 end;
126 when N_Object_Declaration =>
127 Expand_SPARK_N_Object_Declaration (N);
129 when N_Object_Renaming_Declaration =>
130 Expand_SPARK_N_Object_Renaming_Declaration (N);
132 when N_Op_Ne =>
133 Expand_SPARK_Op_Ne (N);
135 when N_Freeze_Entity =>
136 if Is_Type (Entity (N)) then
137 Expand_SPARK_Freeze_Type (Entity (N));
138 end if;
140 -- In SPARK mode, no other constructs require expansion
142 when others =>
143 null;
144 end case;
145 end Expand_SPARK;
147 ----------------------------------------
148 -- Expand_SPARK_N_Attribute_Reference --
149 ----------------------------------------
151 procedure Expand_SPARK_N_Attribute_Reference (N : Node_Id) is
152 Aname : constant Name_Id := Attribute_Name (N);
153 Attr_Id : constant Attribute_Id := Get_Attribute_Id (Aname);
154 Loc : constant Source_Ptr := Sloc (N);
155 Typ : constant Entity_Id := Etype (N);
156 Expr : Node_Id;
158 begin
159 if Attr_Id = Attribute_To_Address then
161 -- Extract and convert argument to expected type for call
163 Expr :=
164 Make_Type_Conversion (Loc,
165 Subtype_Mark =>
166 New_Occurrence_Of (RTE (RE_Integer_Address), Loc),
167 Expression => Relocate_Node (First (Expressions (N))));
169 -- Replace attribute reference with call
171 Rewrite (N,
172 Make_Function_Call (Loc,
173 Name =>
174 New_Occurrence_Of (RTE (RE_To_Address), Loc),
175 Parameter_Associations => New_List (Expr)));
176 Analyze_And_Resolve (N, Typ);
178 -- For attributes which return Universal_Integer, introduce a conversion
179 -- to the expected type with the appropriate check flags set.
181 elsif Attr_Id = Attribute_Alignment
182 or else Attr_Id = Attribute_Bit
183 or else Attr_Id = Attribute_Bit_Position
184 or else Attr_Id = Attribute_Descriptor_Size
185 or else Attr_Id = Attribute_First_Bit
186 or else Attr_Id = Attribute_Last_Bit
187 or else Attr_Id = Attribute_Length
188 or else Attr_Id = Attribute_Max_Size_In_Storage_Elements
189 or else Attr_Id = Attribute_Pos
190 or else Attr_Id = Attribute_Position
191 or else Attr_Id = Attribute_Range_Length
192 or else Attr_Id = Attribute_Object_Size
193 or else Attr_Id = Attribute_Size
194 or else Attr_Id = Attribute_Value_Size
195 or else Attr_Id = Attribute_VADS_Size
196 or else Attr_Id = Attribute_Aft
197 or else Attr_Id = Attribute_Max_Alignment_For_Allocation
198 then
199 -- If the expected type is Long_Long_Integer, there will be no check
200 -- flag as the compiler assumes attributes always fit in this type.
201 -- Since in SPARK_Mode we do not take Storage_Error into account, we
202 -- cannot make this assumption and need to produce a check.
203 -- ??? It should be enough to add this check for attributes 'Length
204 -- and 'Range_Length when the type is as big as Long_Long_Integer.
206 declare
207 Typ : Entity_Id := Empty;
208 begin
209 if Attr_Id = Attribute_Range_Length then
210 Typ := Etype (Prefix (N));
212 elsif Attr_Id = Attribute_Length then
213 Typ := Etype (Prefix (N));
215 declare
216 Indx : Node_Id;
217 J : Int;
219 begin
220 if Is_Access_Type (Typ) then
221 Typ := Designated_Type (Typ);
222 end if;
224 if No (Expressions (N)) then
225 J := 1;
226 else
227 J := UI_To_Int (Expr_Value (First (Expressions (N))));
228 end if;
230 Indx := First_Index (Typ);
231 while J > 1 loop
232 Next_Index (Indx);
233 J := J - 1;
234 end loop;
236 Typ := Etype (Indx);
237 end;
238 end if;
240 Apply_Universal_Integer_Attribute_Checks (N);
242 if Present (Typ)
243 and then RM_Size (Typ) = RM_Size (Standard_Long_Long_Integer)
244 then
245 Set_Do_Overflow_Check (N);
246 end if;
247 end;
248 end if;
249 end Expand_SPARK_N_Attribute_Reference;
251 ------------------------------
252 -- Expand_SPARK_Freeze_Type --
253 ------------------------------
255 procedure Expand_SPARK_Freeze_Type (E : Entity_Id) is
256 begin
257 -- When a DIC is inherited by a tagged type, it may need to be
258 -- specialized to the descendant type, hence build a separate DIC
259 -- procedure for it as done during regular expansion for compilation.
261 if Has_DIC (E) and then Is_Tagged_Type (E) then
262 Build_DIC_Procedure_Body (E, For_Freeze => True);
263 end if;
264 end Expand_SPARK_Freeze_Type;
266 ---------------------------------------
267 -- Expand_SPARK_N_Object_Declaration --
268 ---------------------------------------
270 procedure Expand_SPARK_N_Object_Declaration (N : Node_Id) is
271 Def_Id : constant Entity_Id := Defining_Identifier (N);
272 Loc : constant Source_Ptr := Sloc (N);
273 Typ : constant Entity_Id := Etype (Def_Id);
275 begin
276 -- If the object declaration denotes a variable without initialization
277 -- whose type is subject to pragma Default_Initial_Condition, create
278 -- and analyze a dummy call to the DIC procedure of the type in order
279 -- to detect potential elaboration issues.
281 if Comes_From_Source (Def_Id)
282 and then Has_DIC (Typ)
283 and then Present (DIC_Procedure (Typ))
284 and then not Has_Init_Expression (N)
285 then
286 Analyze (Build_DIC_Call (Loc, Def_Id, Typ));
287 end if;
288 end Expand_SPARK_N_Object_Declaration;
290 ------------------------------------------------
291 -- Expand_SPARK_N_Object_Renaming_Declaration --
292 ------------------------------------------------
294 procedure Expand_SPARK_N_Object_Renaming_Declaration (N : Node_Id) is
295 begin
296 -- Unconditionally remove all side effects from the name
298 Evaluate_Name (Name (N));
299 end Expand_SPARK_N_Object_Renaming_Declaration;
301 ------------------------
302 -- Expand_SPARK_Op_Ne --
303 ------------------------
305 procedure Expand_SPARK_Op_Ne (N : Node_Id) is
306 Typ : constant Entity_Id := Etype (Left_Opnd (N));
308 begin
309 -- Case of elementary type with standard operator
311 if Is_Elementary_Type (Typ)
312 and then Sloc (Entity (N)) = Standard_Location
313 then
314 null;
316 else
317 Exp_Ch4.Expand_N_Op_Ne (N);
318 end if;
319 end Expand_SPARK_Op_Ne;
321 -------------------------------------
322 -- Expand_SPARK_Potential_Renaming --
323 -------------------------------------
325 procedure Expand_SPARK_Potential_Renaming (N : Node_Id) is
326 Loc : constant Source_Ptr := Sloc (N);
327 Ren_Id : constant Entity_Id := Entity (N);
328 Typ : constant Entity_Id := Etype (N);
329 Obj_Id : Node_Id;
331 begin
332 -- Replace a reference to a renaming with the actual renamed object
334 if Ekind (Ren_Id) in Object_Kind then
335 Obj_Id := Renamed_Object (Ren_Id);
337 if Present (Obj_Id) then
339 -- The renamed object is an entity when instantiating generics
340 -- or inlining bodies. In this case the renaming is part of the
341 -- mapping "prologue" which links actuals to formals.
343 if Nkind (Obj_Id) in N_Entity then
344 Rewrite (N, New_Occurrence_Of (Obj_Id, Loc));
346 -- Otherwise the renamed object denotes a name
348 else
349 Rewrite (N, New_Copy_Tree (Obj_Id, New_Sloc => Loc));
350 Reset_Analyzed_Flags (N);
351 end if;
353 Analyze_And_Resolve (N, Typ);
354 end if;
355 end if;
356 end Expand_SPARK_Potential_Renaming;
358 end Exp_SPARK;