1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
5 -- E X P _ S P A R K --
9 -- Copyright (C) 1992-2020, Free Software Foundation, Inc. --
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. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Atree; use Atree;
27 with Checks; use Checks;
28 with Einfo; use Einfo;
31 with Exp_Ch5; use Exp_Ch5;
32 with Exp_Dbug; use Exp_Dbug;
33 with Exp_Util; use Exp_Util;
34 with Namet; use Namet;
35 with Nlists; use Nlists;
36 with Nmake; use Nmake;
37 with Rtsfind; use Rtsfind;
39 with Sem_Prag; use Sem_Prag;
40 with Sem_Res; use Sem_Res;
41 with Sem_Util; use Sem_Util;
42 with Sinfo; use Sinfo;
43 with Snames; use Snames;
44 with Stand; use Stand;
45 with Tbuild; use Tbuild;
46 with Uintp; use Uintp;
48 package body Exp_SPARK is
50 -----------------------
51 -- Local Subprograms --
52 -----------------------
54 procedure Expand_SPARK_N_Attribute_Reference (N : Node_Id);
55 -- Replace occurrences of System'To_Address by calls to
56 -- System.Storage_Elements.To_Address
58 procedure Expand_SPARK_N_Freeze_Type (E : Entity_Id);
59 -- Build the DIC procedure of a type when needed, if not already done
61 procedure Expand_SPARK_N_Loop_Statement (N : Node_Id);
62 -- Perform loop statement-specific expansion
64 procedure Expand_SPARK_N_Object_Declaration (N : Node_Id);
65 -- Perform object-declaration-specific expansion
67 procedure Expand_SPARK_N_Object_Renaming_Declaration (N : Node_Id);
68 -- Perform name evaluation for a renamed object
70 procedure Expand_SPARK_N_Op_Ne (N : Node_Id);
71 -- Rewrite operator /= based on operator = when defined explicitly
73 procedure Expand_SPARK_N_Selected_Component (N : Node_Id);
74 -- Insert explicit dereference if required
76 procedure Expand_SPARK_N_Slice_Or_Indexed_Component (N : Node_Id);
77 -- Insert explicit dereference if required
83 procedure Expand_SPARK (N : Node_Id) is
87 -- Qualification of entity names in formal verification mode
88 -- is limited to the addition of a suffix for homonyms (see
89 -- Exp_Dbug.Qualify_Entity_Name). We used to qualify entity names
90 -- as full expansion does, but this was removed as this prevents the
91 -- verification back-end from using a short name for debugging and
92 -- user interaction. The verification back-end already takes care
93 -- of qualifying names when needed.
95 when N_Block_Statement
98 | N_Package_Declaration
99 | N_Protected_Type_Declaration
101 | N_Task_Type_Declaration
103 Qualify_Entity_Names (N);
105 -- Replace occurrences of System'To_Address by calls to
106 -- System.Storage_Elements.To_Address.
108 when N_Attribute_Reference =>
109 Expand_SPARK_N_Attribute_Reference (N);
114 Expand_SPARK_Potential_Renaming (N);
116 -- Loop iterations over arrays need to be expanded, to avoid getting
117 -- two names referring to the same object in memory (the array and
118 -- the iterator) in GNATprove, especially since both can be written
119 -- (thus possibly leading to interferences due to aliasing). No such
120 -- problem arises with quantified expressions over arrays, which are
121 -- dealt with specially in GNATprove.
123 when N_Loop_Statement =>
124 Expand_SPARK_N_Loop_Statement (N);
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);
133 Expand_SPARK_N_Op_Ne (N);
135 when N_Freeze_Entity =>
136 if Is_Type (Entity (N)) then
137 Expand_SPARK_N_Freeze_Type (Entity (N));
140 when N_Indexed_Component
143 Expand_SPARK_N_Slice_Or_Indexed_Component (N);
145 when N_Selected_Component =>
146 Expand_SPARK_N_Selected_Component (N);
148 -- In SPARK mode, no other constructs require expansion
155 --------------------------------
156 -- Expand_SPARK_N_Freeze_Type --
157 --------------------------------
159 procedure Expand_SPARK_N_Freeze_Type (E : Entity_Id) is
161 -- When a DIC is inherited by a tagged type, it may need to be
162 -- specialized to the descendant type, hence build a separate DIC
163 -- procedure for it as done during regular expansion for compilation.
165 if Has_DIC (E) and then Is_Tagged_Type (E) then
166 Build_DIC_Procedure_Body (E, For_Freeze => True);
168 end Expand_SPARK_N_Freeze_Type;
170 ----------------------------------------
171 -- Expand_SPARK_N_Attribute_Reference --
172 ----------------------------------------
174 procedure Expand_SPARK_N_Attribute_Reference (N : Node_Id) is
175 Aname : constant Name_Id := Attribute_Name (N);
176 Attr_Id : constant Attribute_Id := Get_Attribute_Id (Aname);
177 Loc : constant Source_Ptr := Sloc (N);
178 Pref : constant Node_Id := Prefix (N);
179 Typ : constant Entity_Id := Etype (N);
183 if Attr_Id = Attribute_To_Address then
185 -- Extract and convert argument to expected type for call
188 Make_Type_Conversion (Loc,
190 New_Occurrence_Of (RTE (RE_Integer_Address), Loc),
191 Expression => Relocate_Node (First (Expressions (N))));
193 -- Replace attribute reference with call
196 Make_Function_Call (Loc,
198 New_Occurrence_Of (RTE (RE_To_Address), Loc),
199 Parameter_Associations => New_List (Expr)));
200 Analyze_And_Resolve (N, Typ);
202 elsif Attr_Id = Attribute_Object_Size
203 or else Attr_Id = Attribute_Size
204 or else Attr_Id = Attribute_Value_Size
205 or else Attr_Id = Attribute_VADS_Size
207 Exp_Attr.Expand_Size_Attribute (N);
209 -- For attributes which return Universal_Integer, introduce a conversion
210 -- to the expected type with the appropriate check flags set.
212 elsif Attr_Id = Attribute_Alignment
213 or else Attr_Id = Attribute_Bit
214 or else Attr_Id = Attribute_Bit_Position
215 or else Attr_Id = Attribute_Descriptor_Size
216 or else Attr_Id = Attribute_First_Bit
217 or else Attr_Id = Attribute_Last_Bit
218 or else Attr_Id = Attribute_Length
219 or else Attr_Id = Attribute_Max_Size_In_Storage_Elements
220 or else Attr_Id = Attribute_Pos
221 or else Attr_Id = Attribute_Position
222 or else Attr_Id = Attribute_Range_Length
223 or else Attr_Id = Attribute_Aft
224 or else Attr_Id = Attribute_Max_Alignment_For_Allocation
226 -- If the expected type is Long_Long_Integer, there will be no check
227 -- flag as the compiler assumes attributes always fit in this type.
228 -- Since in SPARK_Mode we do not take Storage_Error into account, we
229 -- cannot make this assumption and need to produce a check.
230 -- ??? It should be enough to add this check for attributes 'Length
231 -- and 'Range_Length when the type is as big as Long_Long_Integer.
236 if Attr_Id = Attribute_Range_Length then
237 Typ := Etype (Prefix (N));
239 elsif Attr_Id = Attribute_Length then
240 Typ := Get_Index_Subtype (N);
246 Apply_Universal_Integer_Attribute_Checks (N);
249 and then RM_Size (Typ) = RM_Size (Standard_Long_Long_Integer)
251 Set_Do_Overflow_Check (N);
255 elsif Attr_Id = Attribute_Constrained then
257 -- If the prefix is an access to object, the attribute applies to
258 -- the designated object, so rewrite with an explicit dereference.
260 if Is_Access_Type (Etype (Pref))
262 (not Is_Entity_Name (Pref) or else Is_Object (Entity (Pref)))
265 Make_Explicit_Dereference (Loc, Relocate_Node (Pref)));
266 Analyze_And_Resolve (N, Standard_Boolean);
269 -- For attributes First and Last simply reuse the standard expansion
271 elsif Attr_Id = Attribute_First
272 or else Attr_Id = Attribute_Last
274 Exp_Attr.Expand_N_Attribute_Reference (N);
276 end Expand_SPARK_N_Attribute_Reference;
278 -----------------------------------
279 -- Expand_SPARK_N_Loop_Statement --
280 -----------------------------------
282 procedure Expand_SPARK_N_Loop_Statement (N : Node_Id) is
283 Scheme : constant Node_Id := Iteration_Scheme (N);
286 -- Loop iterations over arrays need to be expanded, to avoid getting
287 -- two names referring to the same object in memory (the array and the
288 -- iterator) in GNATprove, especially since both can be written (thus
289 -- possibly leading to interferences due to aliasing). No such problem
290 -- arises with quantified expressions over arrays, which are dealt with
291 -- specially in GNATprove.
294 and then Present (Iterator_Specification (Scheme))
295 and then Is_Iterator_Over_Array (Iterator_Specification (Scheme))
297 Expand_Iterator_Loop_Over_Array (N);
299 end Expand_SPARK_N_Loop_Statement;
301 ---------------------------------------
302 -- Expand_SPARK_N_Object_Declaration --
303 ---------------------------------------
305 procedure Expand_SPARK_N_Object_Declaration (N : Node_Id) is
306 Loc : constant Source_Ptr := Sloc (N);
307 Obj_Id : constant Entity_Id := Defining_Identifier (N);
308 Typ : constant Entity_Id := Etype (Obj_Id);
313 -- If the object declaration denotes a variable without initialization
314 -- whose type is subject to pragma Default_Initial_Condition, create
315 -- and analyze a dummy call to the DIC procedure of the type in order
316 -- to detect potential elaboration issues.
318 if Comes_From_Source (Obj_Id)
319 and then Ekind (Obj_Id) = E_Variable
320 and then Has_DIC (Typ)
321 and then Present (DIC_Procedure (Typ))
322 and then not Has_Init_Expression (N)
324 Call := Build_DIC_Call (Loc, Obj_Id, Typ);
326 -- Partially insert the call into the tree by setting its parent
329 Set_Parent (Call, N);
332 end Expand_SPARK_N_Object_Declaration;
334 ------------------------------------------------
335 -- Expand_SPARK_N_Object_Renaming_Declaration --
336 ------------------------------------------------
338 procedure Expand_SPARK_N_Object_Renaming_Declaration (N : Node_Id) is
339 CFS : constant Boolean := Comes_From_Source (N);
340 Loc : constant Source_Ptr := Sloc (N);
341 Obj_Id : constant Entity_Id := Defining_Entity (N);
342 Nam : constant Node_Id := Name (N);
343 Typ : constant Entity_Id := Etype (Obj_Id);
346 -- Transform a renaming of the form
348 -- Obj_Id : <subtype mark> renames <function call>;
352 -- Obj_Id : constant <subtype mark> := <function call>;
354 -- Invoking Evaluate_Name and ultimately Remove_Side_Effects introduces
355 -- a temporary to capture the function result. Once potential renamings
356 -- are rewritten for SPARK, the temporary may be leaked out into source
357 -- constructs and lead to confusing error diagnostics. Using an object
358 -- declaration prevents this unwanted side effect.
360 if Nkind (Nam) = N_Function_Call then
362 Make_Object_Declaration (Loc,
363 Defining_Identifier => Obj_Id,
364 Constant_Present => True,
365 Object_Definition => New_Occurrence_Of (Typ, Loc),
368 -- Inherit the original Comes_From_Source status of the renaming
370 Set_Comes_From_Source (N, CFS);
372 -- Sever the link to the renamed function result because the entity
373 -- will no longer alias anything.
375 Set_Renamed_Object (Obj_Id, Empty);
377 -- Remove the entity of the renaming declaration from visibility as
378 -- the analysis of the object declaration will reintroduce it again.
380 Remove_Entity_And_Homonym (Obj_Id);
383 -- Otherwise unconditionally remove all side effects from the name
388 end Expand_SPARK_N_Object_Renaming_Declaration;
390 --------------------------
391 -- Expand_SPARK_N_Op_Ne --
392 --------------------------
394 procedure Expand_SPARK_N_Op_Ne (N : Node_Id) is
395 Typ : constant Entity_Id := Etype (Left_Opnd (N));
398 -- Case of elementary type with standard operator
400 if Is_Elementary_Type (Typ)
401 and then Sloc (Entity (N)) = Standard_Location
406 Exp_Ch4.Expand_N_Op_Ne (N);
408 end Expand_SPARK_N_Op_Ne;
410 -------------------------------------
411 -- Expand_SPARK_Potential_Renaming --
412 -------------------------------------
414 procedure Expand_SPARK_Potential_Renaming (N : Node_Id) is
415 function In_Insignificant_Pragma (Nod : Node_Id) return Boolean;
416 -- Determine whether arbitrary node Nod appears within a significant
419 -----------------------------
420 -- In_Insignificant_Pragma --
421 -----------------------------
423 function In_Insignificant_Pragma (Nod : Node_Id) return Boolean is
427 -- Climb the parent chain looking for an enclosing pragma
430 while Present (Par) loop
431 if Nkind (Par) = N_Pragma then
432 return not Pragma_Significant_In_SPARK (Get_Pragma_Id (Par));
434 -- Prevent the search from going too far
436 elsif Is_Body_Or_Package_Declaration (Par) then
444 end In_Insignificant_Pragma;
448 Loc : constant Source_Ptr := Sloc (N);
449 Obj_Id : constant Entity_Id := Entity (N);
450 Typ : constant Entity_Id := Etype (N);
453 -- Start of processing for Expand_SPARK_Potential_Renaming
456 -- Replace a reference to a renaming with the actual renamed object
458 if Ekind (Obj_Id) in Object_Kind then
459 Ren := Renamed_Object (Obj_Id);
461 if Present (Ren) then
463 -- Do not process a reference when it appears within a pragma of
464 -- no significance to SPARK. It is assumed that the replacement
465 -- will violate the semantics of the pragma and cause a spurious
468 if In_Insignificant_Pragma (N) then
471 -- Instantiations and inlining of subprograms employ "prologues"
472 -- which map actual to formal parameters by means of renamings.
473 -- Replace a reference to a formal by the corresponding actual
476 elsif Nkind (Ren) in N_Entity then
477 Rewrite (N, New_Occurrence_Of (Ren, Loc));
479 -- Otherwise the renamed object denotes a name
482 Rewrite (N, New_Copy_Tree (Ren, New_Sloc => Loc));
483 Reset_Analyzed_Flags (N);
486 Analyze_And_Resolve (N, Typ);
489 end Expand_SPARK_Potential_Renaming;
491 ---------------------------------------
492 -- Expand_SPARK_N_Selected_Component --
493 ---------------------------------------
495 procedure Expand_SPARK_N_Selected_Component (N : Node_Id) is
496 Pref : constant Node_Id := Prefix (N);
497 Typ : constant Entity_Id := Underlying_Type (Etype (Pref));
500 if Present (Typ) and then Is_Access_Type (Typ) then
502 -- First set prefix type to proper access type, in case it currently
503 -- has a private (non-access) view of this type.
505 Set_Etype (Pref, Typ);
507 Insert_Explicit_Dereference (Pref);
508 Analyze_And_Resolve (Pref, Designated_Type (Typ));
510 end Expand_SPARK_N_Selected_Component;
512 -----------------------------------------------
513 -- Expand_SPARK_N_Slice_Or_Indexed_Component --
514 -----------------------------------------------
516 procedure Expand_SPARK_N_Slice_Or_Indexed_Component (N : Node_Id) is
517 Pref : constant Node_Id := Prefix (N);
518 Typ : constant Entity_Id := Etype (Pref);
521 if Is_Access_Type (Typ) then
522 Insert_Explicit_Dereference (Pref);
523 Analyze_And_Resolve (Pref, Designated_Type (Typ));
525 end Expand_SPARK_N_Slice_Or_Indexed_Component;