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_Ch8; use Sem_Ch8;
40 with Sem_Prag; use Sem_Prag;
41 with Sem_Res; use Sem_Res;
42 with Sem_Util; use Sem_Util;
43 with Sinfo; use Sinfo;
44 with Snames; use Snames;
45 with Stand; use Stand;
46 with Tbuild; use Tbuild;
47 with Uintp; use Uintp;
49 package body Exp_SPARK is
51 -----------------------
52 -- Local Subprograms --
53 -----------------------
55 procedure Expand_SPARK_Array_Aggregate (N : Node_Id; Index : Node_Id);
56 -- Perform array-aggregate-specific expansion of an array sub-aggregate N
57 -- corresponding to the Index of the outer-most aggregate. This routine
58 -- mimics Resolve_Array_Aggregate which only checks the aggregate for being
59 -- well-formed, but doesn't analyze nor apply range checks to
60 -- iterated_component_associations.
62 procedure Expand_SPARK_N_Aggregate (N : Node_Id);
63 -- Perform aggregate-specific expansion
65 procedure Expand_SPARK_N_Attribute_Reference (N : Node_Id);
66 -- Perform attribute-reference-specific expansion
68 procedure Expand_SPARK_N_Delta_Aggregate (N : Node_Id);
69 -- Perform delta-aggregate-specific expansion
71 procedure Expand_SPARK_N_Freeze_Type (E : Entity_Id);
72 -- Build the DIC procedure of a type when needed, if not already done
74 procedure Expand_SPARK_N_Loop_Statement (N : Node_Id);
75 -- Perform loop-statement-specific expansion
77 procedure Expand_SPARK_N_Object_Declaration (N : Node_Id);
78 -- Perform object-declaration-specific expansion
80 procedure Expand_SPARK_N_Object_Renaming_Declaration (N : Node_Id);
81 -- Perform name evaluation for a renamed object
83 procedure Expand_SPARK_N_Op_Ne (N : Node_Id);
84 -- Rewrite operator /= based on operator = when defined explicitly
86 procedure Expand_SPARK_Delta_Or_Update (Typ : Entity_Id; Aggr : Node_Id);
87 -- Common expansion for attribute Update and delta aggregates
93 procedure Expand_SPARK (N : Node_Id) is
97 -- Qualification of entity names in formal verification mode
98 -- is limited to the addition of a suffix for homonyms (see
99 -- Exp_Dbug.Qualify_Entity_Name). We used to qualify entity names
100 -- as full expansion does, but this was removed as this prevents the
101 -- verification back-end from using a short name for debugging and
102 -- user interaction. The verification back-end already takes care
103 -- of qualifying names when needed.
105 when N_Block_Statement
106 | N_Entry_Declaration
108 | N_Package_Declaration
109 | N_Protected_Type_Declaration
111 | N_Task_Type_Declaration
113 Qualify_Entity_Names (N);
116 Expand_SPARK_N_Aggregate (N);
118 -- Replace occurrences of System'To_Address by calls to
119 -- System.Storage_Elements.To_Address.
121 when N_Attribute_Reference =>
122 Expand_SPARK_N_Attribute_Reference (N);
124 when N_Delta_Aggregate =>
125 Expand_SPARK_N_Delta_Aggregate (N);
130 Expand_SPARK_Potential_Renaming (N);
132 -- Loop iterations over arrays need to be expanded, to avoid getting
133 -- two names referring to the same object in memory (the array and
134 -- the iterator) in GNATprove, especially since both can be written
135 -- (thus possibly leading to interferences due to aliasing). No such
136 -- problem arises with quantified expressions over arrays, which are
137 -- dealt with specially in GNATprove.
139 when N_Loop_Statement =>
140 Expand_SPARK_N_Loop_Statement (N);
142 when N_Object_Declaration =>
143 Expand_SPARK_N_Object_Declaration (N);
145 when N_Object_Renaming_Declaration =>
146 Expand_SPARK_N_Object_Renaming_Declaration (N);
149 Expand_SPARK_N_Op_Ne (N);
151 when N_Freeze_Entity =>
152 if Is_Type (Entity (N)) then
153 Expand_SPARK_N_Freeze_Type (Entity (N));
156 -- In SPARK mode, no other constructs require expansion
163 ----------------------------------
164 -- Expand_SPARK_Array_Aggregate --
165 ----------------------------------
167 procedure Expand_SPARK_Array_Aggregate (N : Node_Id; Index : Node_Id) is
169 procedure Expand_Aggr_Expr (Expr : Node_Id);
170 -- If Expr is a subaggregate, then process it recursively; otherwise it
171 -- is an expression for the array components which might not have been
172 -- analyzed and where scalar range checks could be missing.
174 ----------------------
175 -- Expand_Aggr_Expr --
176 ----------------------
178 procedure Expand_Aggr_Expr (Expr : Node_Id) is
179 Nxt_Ind : constant Node_Id := Next_Index (Index);
181 if Present (Nxt_Ind) then
182 Expand_SPARK_Array_Aggregate (Expr, Index => Nxt_Ind);
185 Comp_Type : constant Entity_Id := Component_Type (Etype (N));
187 Analyze_And_Resolve (Expr, Comp_Type);
189 if Is_Scalar_Type (Comp_Type) then
190 Apply_Scalar_Range_Check (Expr, Comp_Type);
194 end Expand_Aggr_Expr;
198 Assoc : Node_Id := First (Component_Associations (N));
200 -- Start of processing for Expand_SPARK_Array_Aggregate
203 while Present (Assoc) loop
204 -- For iterated_component_association we must apply range check to
205 -- discrete choices and re-analyze the expression, because frontend
206 -- only checks its legality and then analyzes the expanded loop code.
208 if Nkind (Assoc) = N_Iterated_Component_Association then
212 -- Analyze discrete choices
214 Choice := First (Discrete_Choices (Assoc));
216 while Present (Choice) loop
218 -- The index denotes a range of elements where range checks
219 -- have been already applied.
221 if Nkind (Choice) in N_Others_Choice
223 | N_Subtype_Indication
227 -- Otherwise the index denotes a single element (or a
228 -- subtype name which doesn't require range checks).
230 else pragma Assert (Nkind (Choice) in N_Subexpr);
231 Apply_Scalar_Range_Check (Choice, Etype (Index));
237 -- Keep processing the expression with index parameter in scope
239 Push_Scope (Scope (Defining_Identifier (Assoc)));
240 Enter_Name (Defining_Identifier (Assoc));
241 Expand_Aggr_Expr (Expression (Assoc));
245 -- For ordinary component associations we recurse into subaggregates,
246 -- because there could be nested iterated_component_association (and
247 -- it is harmless to analyze and apply checks if there is none).
249 else pragma Assert (Nkind (Assoc) = N_Component_Association);
251 Expr : constant Node_Id := Expression (Assoc);
252 pragma Assert (Present (Expr) xor Box_Present (Assoc));
254 if Present (Expr) then
255 Expand_Aggr_Expr (Expr);
262 end Expand_SPARK_Array_Aggregate;
264 ----------------------------------
265 -- Expand_SPARK_Delta_Or_Update --
266 ----------------------------------
268 procedure Expand_SPARK_Delta_Or_Update
275 Comp_Type : Entity_Id;
278 Index_Typ : Entity_Id;
282 -- Apply scalar range checks on the updated components, if needed
284 if Is_Array_Type (Typ) then
286 -- Multidimensional arrays
288 if Present (Next_Index (First_Index (Typ))) then
289 Assoc := First (Component_Associations (Aggr));
291 while Present (Assoc) loop
292 Expr := Expression (Assoc);
293 Comp_Type := Component_Type (Typ);
295 if Is_Scalar_Type (Comp_Type) then
296 Apply_Scalar_Range_Check (Expr, Comp_Type);
299 -- The current association contains a sequence of indexes
300 -- denoting an element of a multidimensional array:
302 -- (Index_1, ..., Index_N)
304 Expr := First (Choices (Assoc));
306 pragma Assert (Nkind (Aggr) = N_Aggregate);
308 while Present (Expr) loop
309 Index := First (Expressions (Expr));
310 Index_Typ := First_Index (Typ);
312 while Present (Index_Typ) loop
313 Apply_Scalar_Range_Check (Index, Etype (Index_Typ));
315 Next_Index (Index_Typ);
324 -- One-dimensional arrays
327 Assoc := First (Component_Associations (Aggr));
329 while Present (Assoc) loop
330 Expr := Expression (Assoc);
331 Comp_Type := Component_Type (Typ);
333 -- Analyze expression of the iterated_component_association
334 -- with its index parameter in scope.
336 if Nkind (Assoc) = N_Iterated_Component_Association then
337 Push_Scope (Scope (Defining_Identifier (Assoc)));
338 Enter_Name (Defining_Identifier (Assoc));
339 Analyze_And_Resolve (Expr, Comp_Type);
342 if Is_Scalar_Type (Comp_Type) then
343 Apply_Scalar_Range_Check (Expr, Comp_Type);
346 -- Restore scope of the iterated_component_association
348 if Nkind (Assoc) = N_Iterated_Component_Association then
352 Index := First (Choice_List (Assoc));
353 Index_Typ := First_Index (Typ);
355 while Present (Index) loop
356 -- If the index denotes a range of elements or a constrained
357 -- subtype indication, then their low and high bounds
358 -- already have range checks applied.
360 if Nkind (Index) in N_Range | N_Subtype_Indication then
363 -- Otherwise the index denotes a single expression where
364 -- range checks need to be applied or a subtype name
365 -- (without range constraints) where applying checks is
368 -- In delta_aggregate and Update attribute on array the
369 -- others_choice is not allowed.
371 else pragma Assert (Nkind (Index) in N_Subexpr);
372 Apply_Scalar_Range_Check (Index, Etype (Index_Typ));
382 else pragma Assert (Is_Record_Type (Typ));
384 -- If the aggregate has multiple component choices, e.g.:
386 -- X'Update (A | B | C => 123)
388 -- then each component might be of a different type and might or
389 -- might not require a range check. We first rewrite associations
390 -- into single-component choices, e.g.:
392 -- X'Update (A => 123, B => 123, C => 123)
394 -- and then apply range checks to individual copies of the
395 -- expressions. We do the same for delta aggregates, accordingly.
397 -- Iterate over associations of the original aggregate
399 Assoc := First (Component_Associations (Aggr));
401 -- Rewrite into a new aggregate and decorate
408 (Sloc => Sloc (Aggr),
409 Component_Associations => New_List));
411 when N_Delta_Aggregate =>
415 (Sloc => Sloc (Aggr),
416 Expression => Expression (Aggr),
417 Component_Associations => New_List));
423 Set_Etype (Aggr, Typ);
425 -- Populate the new aggregate with component associations
427 while Present (Assoc) loop
428 Expr := Expression (Assoc);
429 Comp := First (Choices (Assoc));
431 while Present (Comp) loop
432 Comp_Id := Entity (Comp);
433 Comp_Type := Etype (Comp_Id);
436 Make_Component_Association
437 (Sloc => Sloc (Assoc),
440 (New_Occurrence_Of (Comp_Id, Sloc (Comp))),
441 Expression => New_Copy_Tree (Expr));
443 -- New association must be attached to the aggregate before we
446 Append (New_Assoc, Component_Associations (Aggr));
448 Analyze_And_Resolve (Expression (New_Assoc), Comp_Type);
450 if Is_Scalar_Type (Comp_Type) then
451 Apply_Scalar_Range_Check
452 (Expression (New_Assoc), Comp_Type);
461 end Expand_SPARK_Delta_Or_Update;
463 --------------------------------
464 -- Expand_SPARK_N_Freeze_Type --
465 --------------------------------
467 procedure Expand_SPARK_N_Freeze_Type (E : Entity_Id) is
469 -- When a DIC is inherited by a tagged type, it may need to be
470 -- specialized to the descendant type, hence build a separate DIC
471 -- procedure for it as done during regular expansion for compilation.
473 if Has_DIC (E) and then Is_Tagged_Type (E) then
474 Build_DIC_Procedure_Body (E, For_Freeze => True);
476 end Expand_SPARK_N_Freeze_Type;
478 ------------------------------
479 -- Expand_SPARK_N_Aggregate --
480 ------------------------------
482 procedure Expand_SPARK_N_Aggregate (N : Node_Id) is
483 Aggr_Typ : constant Entity_Id := Etype (N);
485 if Is_Array_Type (Aggr_Typ) then
486 Expand_SPARK_Array_Aggregate (N, Index => First_Index (Aggr_Typ));
488 end Expand_SPARK_N_Aggregate;
490 ----------------------------------------
491 -- Expand_SPARK_N_Attribute_Reference --
492 ----------------------------------------
494 procedure Expand_SPARK_N_Attribute_Reference (N : Node_Id) is
495 Aname : constant Name_Id := Attribute_Name (N);
496 Attr_Id : constant Attribute_Id := Get_Attribute_Id (Aname);
497 Loc : constant Source_Ptr := Sloc (N);
498 Pref : constant Node_Id := Prefix (N);
499 Typ : constant Entity_Id := Etype (N);
503 if Attr_Id = Attribute_To_Address then
505 -- Extract and convert argument to expected type for call
508 Make_Type_Conversion (Loc,
510 New_Occurrence_Of (RTE (RE_Integer_Address), Loc),
511 Expression => Relocate_Node (First (Expressions (N))));
513 -- Replace attribute reference with call
516 Make_Function_Call (Loc,
518 New_Occurrence_Of (RTE (RE_To_Address), Loc),
519 Parameter_Associations => New_List (Expr)));
520 Analyze_And_Resolve (N, Typ);
522 elsif Attr_Id = Attribute_Object_Size
523 or else Attr_Id = Attribute_Size
524 or else Attr_Id = Attribute_Value_Size
525 or else Attr_Id = Attribute_VADS_Size
527 Exp_Attr.Expand_Size_Attribute (N);
529 -- For attributes which return Universal_Integer, introduce a conversion
530 -- to the expected type with the appropriate check flags set.
532 elsif Attr_Id = Attribute_Alignment
533 or else Attr_Id = Attribute_Bit
534 or else Attr_Id = Attribute_Bit_Position
535 or else Attr_Id = Attribute_Descriptor_Size
536 or else Attr_Id = Attribute_First_Bit
537 or else Attr_Id = Attribute_Last_Bit
538 or else Attr_Id = Attribute_Length
539 or else Attr_Id = Attribute_Max_Size_In_Storage_Elements
540 or else Attr_Id = Attribute_Pos
541 or else Attr_Id = Attribute_Position
542 or else Attr_Id = Attribute_Range_Length
543 or else Attr_Id = Attribute_Aft
544 or else Attr_Id = Attribute_Max_Alignment_For_Allocation
546 -- If the expected type is Long_Long_Integer, there will be no check
547 -- flag as the compiler assumes attributes always fit in this type.
548 -- Since in SPARK_Mode we do not take Storage_Error into account, we
549 -- cannot make this assumption and need to produce a check.
550 -- ??? It should be enough to add this check for attributes
551 -- 'Length, 'Range_Length and 'Pos when the type is as big
552 -- as Long_Long_Integer.
557 if Attr_Id = Attribute_Range_Length
558 or else Attr_Id = Attribute_Pos
560 Typ := Etype (Prefix (N));
562 elsif Attr_Id = Attribute_Length then
563 Typ := Get_Index_Subtype (N);
569 Apply_Universal_Integer_Attribute_Checks (N);
572 and then RM_Size (Typ) = RM_Size (Standard_Long_Long_Integer)
574 -- ??? This should rather be a range check, but this would
575 -- crash GNATprove which somehow recovers the proper kind
577 Set_Do_Overflow_Check (N);
581 elsif Attr_Id = Attribute_Constrained then
583 -- If the prefix is an access to object, the attribute applies to
584 -- the designated object, so rewrite with an explicit dereference.
586 if Is_Access_Type (Etype (Pref))
588 (not Is_Entity_Name (Pref) or else Is_Object (Entity (Pref)))
591 Make_Explicit_Dereference (Loc, Relocate_Node (Pref)));
592 Analyze_And_Resolve (N, Standard_Boolean);
595 elsif Attr_Id = Attribute_Update then
596 Expand_SPARK_Delta_Or_Update (Typ, First (Expressions (N)));
598 end Expand_SPARK_N_Attribute_Reference;
600 ------------------------------------
601 -- Expand_SPARK_N_Delta_Aggregate --
602 ------------------------------------
604 procedure Expand_SPARK_N_Delta_Aggregate (N : Node_Id) is
606 Expand_SPARK_Delta_Or_Update (Etype (N), N);
607 end Expand_SPARK_N_Delta_Aggregate;
609 -----------------------------------
610 -- Expand_SPARK_N_Loop_Statement --
611 -----------------------------------
613 procedure Expand_SPARK_N_Loop_Statement (N : Node_Id) is
614 Scheme : constant Node_Id := Iteration_Scheme (N);
617 -- Loop iterations over arrays need to be expanded, to avoid getting
618 -- two names referring to the same object in memory (the array and the
619 -- iterator) in GNATprove, especially since both can be written (thus
620 -- possibly leading to interferences due to aliasing). No such problem
621 -- arises with quantified expressions over arrays, which are dealt with
622 -- specially in GNATprove.
625 and then Present (Iterator_Specification (Scheme))
626 and then Is_Iterator_Over_Array (Iterator_Specification (Scheme))
628 Expand_Iterator_Loop_Over_Array (N);
630 end Expand_SPARK_N_Loop_Statement;
632 ---------------------------------------
633 -- Expand_SPARK_N_Object_Declaration --
634 ---------------------------------------
636 procedure Expand_SPARK_N_Object_Declaration (N : Node_Id) is
637 Loc : constant Source_Ptr := Sloc (N);
638 Obj_Id : constant Entity_Id := Defining_Identifier (N);
639 Typ : constant Entity_Id := Etype (Obj_Id);
644 -- If the object declaration denotes a variable without initialization
645 -- whose type is subject to pragma Default_Initial_Condition, create
646 -- and analyze a dummy call to the DIC procedure of the type in order
647 -- to detect potential elaboration issues.
649 if Comes_From_Source (Obj_Id)
650 and then Ekind (Obj_Id) = E_Variable
651 and then Has_DIC (Typ)
652 and then Present (DIC_Procedure (Typ))
653 and then not Has_Init_Expression (N)
655 Call := Build_DIC_Call (Loc, Obj_Id, Typ);
657 -- Partially insert the call into the tree by setting its parent
660 Set_Parent (Call, N);
663 end Expand_SPARK_N_Object_Declaration;
665 ------------------------------------------------
666 -- Expand_SPARK_N_Object_Renaming_Declaration --
667 ------------------------------------------------
669 procedure Expand_SPARK_N_Object_Renaming_Declaration (N : Node_Id) is
670 CFS : constant Boolean := Comes_From_Source (N);
671 Loc : constant Source_Ptr := Sloc (N);
672 Obj_Id : constant Entity_Id := Defining_Entity (N);
673 Nam : constant Node_Id := Name (N);
674 Typ : constant Entity_Id := Etype (Obj_Id);
677 -- Transform a renaming of the form
679 -- Obj_Id : <subtype mark> renames <function call>;
683 -- Obj_Id : constant <subtype mark> := <function call>;
685 -- Invoking Evaluate_Name and ultimately Remove_Side_Effects introduces
686 -- a temporary to capture the function result. Once potential renamings
687 -- are rewritten for SPARK, the temporary may be leaked out into source
688 -- constructs and lead to confusing error diagnostics. Using an object
689 -- declaration prevents this unwanted side effect.
691 if Nkind (Nam) = N_Function_Call then
693 Make_Object_Declaration (Loc,
694 Defining_Identifier => Obj_Id,
695 Constant_Present => True,
696 Object_Definition => New_Occurrence_Of (Typ, Loc),
699 -- Inherit the original Comes_From_Source status of the renaming
701 Set_Comes_From_Source (N, CFS);
703 -- Sever the link to the renamed function result because the entity
704 -- will no longer alias anything.
706 Set_Renamed_Object (Obj_Id, Empty);
708 -- Remove the entity of the renaming declaration from visibility as
709 -- the analysis of the object declaration will reintroduce it again.
711 Remove_Entity_And_Homonym (Obj_Id);
714 -- Otherwise unconditionally remove all side effects from the name
719 end Expand_SPARK_N_Object_Renaming_Declaration;
721 --------------------------
722 -- Expand_SPARK_N_Op_Ne --
723 --------------------------
725 procedure Expand_SPARK_N_Op_Ne (N : Node_Id) is
726 Typ : constant Entity_Id := Etype (Left_Opnd (N));
729 -- Case of elementary type with standard operator
731 if Is_Elementary_Type (Typ)
732 and then Sloc (Entity (N)) = Standard_Location
737 Exp_Ch4.Expand_N_Op_Ne (N);
739 end Expand_SPARK_N_Op_Ne;
741 -------------------------------------
742 -- Expand_SPARK_Potential_Renaming --
743 -------------------------------------
745 procedure Expand_SPARK_Potential_Renaming (N : Node_Id) is
746 function In_Insignificant_Pragma (Nod : Node_Id) return Boolean;
747 -- Determine whether arbitrary node Nod appears within a significant
750 -----------------------------
751 -- In_Insignificant_Pragma --
752 -----------------------------
754 function In_Insignificant_Pragma (Nod : Node_Id) return Boolean is
758 -- Climb the parent chain looking for an enclosing pragma
761 while Present (Par) loop
762 if Nkind (Par) = N_Pragma then
763 return not Pragma_Significant_In_SPARK (Get_Pragma_Id (Par));
765 -- Prevent the search from going too far
767 elsif Is_Body_Or_Package_Declaration (Par) then
775 end In_Insignificant_Pragma;
779 Loc : constant Source_Ptr := Sloc (N);
780 Obj_Id : constant Entity_Id := Entity (N);
781 Typ : constant Entity_Id := Etype (N);
784 -- Start of processing for Expand_SPARK_Potential_Renaming
787 -- Replace a reference to a renaming with the actual renamed object
789 if Is_Object (Obj_Id) then
790 Ren := Renamed_Object (Obj_Id);
792 if Present (Ren) then
794 -- Do not process a reference when it appears within a pragma of
795 -- no significance to SPARK. It is assumed that the replacement
796 -- will violate the semantics of the pragma and cause a spurious
799 if In_Insignificant_Pragma (N) then
802 -- Instantiations and inlining of subprograms employ "prologues"
803 -- which map actual to formal parameters by means of renamings.
804 -- Replace a reference to a formal by the corresponding actual
807 elsif Nkind (Ren) in N_Entity then
808 Rewrite (N, New_Occurrence_Of (Ren, Loc));
810 -- Otherwise the renamed object denotes a name
813 Rewrite (N, New_Copy_Tree (Ren, New_Sloc => Loc));
814 Reset_Analyzed_Flags (N);
817 Analyze_And_Resolve (N, Typ);
820 end Expand_SPARK_Potential_Renaming;