1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
5 -- E X P _ S P A R K --
9 -- Copyright (C) 1992-2023, 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 Aspects
; use Aspects
;
27 with Atree
; use Atree
;
28 with Checks
; use Checks
;
29 with Einfo
; use Einfo
;
30 with Einfo
.Entities
; use Einfo
.Entities
;
31 with Einfo
.Utils
; use Einfo
.Utils
;
35 with Exp_Ch5
; use Exp_Ch5
;
36 with Exp_Dbug
; use Exp_Dbug
;
37 with Exp_Util
; use Exp_Util
;
38 with Ghost
; use Ghost
;
39 with Namet
; use Namet
;
40 with Nlists
; use Nlists
;
41 with Nmake
; use Nmake
;
43 with Restrict
; use Restrict
;
44 with Rident
; use Rident
;
45 with Rtsfind
; use Rtsfind
;
47 with Sem_Aggr
; use Sem_Aggr
;
48 with Sem_Aux
; use Sem_Aux
;
49 with Sem_Ch7
; use Sem_Ch7
;
50 with Sem_Ch8
; use Sem_Ch8
;
51 with Sem_Ch13
; use Sem_Ch13
;
52 with Sem_Prag
; use Sem_Prag
;
53 with Sem_Res
; use Sem_Res
;
54 with Sem_Util
; use Sem_Util
;
55 with Sinfo
; use Sinfo
;
56 with Sinfo
.Nodes
; use Sinfo
.Nodes
;
57 with Sinfo
.Utils
; use Sinfo
.Utils
;
58 with Snames
; use Snames
;
59 with Stand
; use Stand
;
60 with Tbuild
; use Tbuild
;
61 with Uintp
; use Uintp
;
63 package body Exp_SPARK
is
65 -----------------------
66 -- Local Subprograms --
67 -----------------------
69 procedure Expand_SPARK_N_Aggregate
(N
: Node_Id
);
70 -- Perform specific expansion of container aggregates, to ensure suitable
71 -- checking of expressions.
73 procedure Expand_SPARK_N_Attribute_Reference
(N
: Node_Id
);
74 -- Perform attribute-reference-specific expansion
76 procedure Expand_SPARK_N_Delta_Aggregate
(N
: Node_Id
);
77 -- Perform delta-aggregate-specific expansion
79 procedure Expand_SPARK_N_Freeze_Entity
(N
: Node_Id
);
80 -- Do a minimal expansion of freeze entities required by GNATprove. It is
81 -- a subset of what is done for GNAT in Exp_Ch13.Expand_N_Freeze_Entity.
82 -- Those two routines should be kept in sync.
84 procedure Expand_SPARK_N_Loop_Statement
(N
: Node_Id
);
85 -- Perform loop-statement-specific expansion
87 procedure Expand_SPARK_N_Object_Declaration
(N
: Node_Id
);
88 -- Perform object-declaration-specific expansion
90 procedure Expand_SPARK_N_Object_Renaming_Declaration
(N
: Node_Id
);
91 -- Perform name evaluation for a renamed object
93 procedure Expand_SPARK_N_Op_Ne
(N
: Node_Id
);
94 -- Rewrite operator /= based on operator = when defined explicitly
96 procedure Expand_SPARK_Delta_Or_Update
(Typ
: Entity_Id
; Aggr
: Node_Id
);
97 -- Common expansion for attribute Update and delta aggregates
99 procedure SPARK_Freeze_Type
(N
: Node_Id
);
100 -- Do a minimal type freezing required by GNATprove. It is a subset of what
101 -- is done for GNAT in Exp_Ch3.Freeze_Type. Those two routines should be
104 -- Currently in freezing we build the spec of dispatching equality. This
105 -- spec is needed to properly resolve references to the equality operator.
106 -- The body is not needed, because proof knows how to directly synthesize a
107 -- logical meaning for it. Also, for tagged types with extension the
108 -- expanded body would compare the _parent component, which is
109 -- intentionally not generated in the GNATprove mode.
111 -- We build the DIC and Type_Invariant procedure bodies here as well.
117 procedure Expand_SPARK
(N
: Node_Id
) is
121 -- Qualification of entity names in formal verification mode
122 -- is limited to the addition of a suffix for homonyms (see
123 -- Exp_Dbug.Qualify_Entity_Name). We used to qualify entity names
124 -- as full expansion does, but this was removed as this prevents the
125 -- verification back-end from using a short name for debugging and
126 -- user interaction. The verification back-end already takes care
127 -- of qualifying names when needed.
129 when N_Block_Statement
130 | N_Entry_Declaration
132 | N_Package_Declaration
133 | N_Protected_Type_Declaration
135 | N_Task_Type_Declaration
137 Qualify_Entity_Names
(N
);
139 -- Replace occurrences of System'To_Address by calls to
140 -- System.Storage_Elements.To_Address.
142 when N_Attribute_Reference
=>
143 Expand_SPARK_N_Attribute_Reference
(N
);
145 when N_Delta_Aggregate
=>
146 Expand_SPARK_N_Delta_Aggregate
(N
);
149 Expand_SPARK_N_Aggregate
(N
);
154 Expand_SPARK_Potential_Renaming
(N
);
156 -- Loop iterations over arrays need to be expanded, to avoid getting
157 -- two names referring to the same object in memory (the array and
158 -- the iterator) in GNATprove, especially since both can be written
159 -- (thus possibly leading to interferences due to aliasing). No such
160 -- problem arises with quantified expressions over arrays, which are
161 -- dealt with specially in GNATprove.
163 when N_Loop_Statement
=>
164 Expand_SPARK_N_Loop_Statement
(N
);
166 when N_Object_Declaration
=>
167 Expand_SPARK_N_Object_Declaration
(N
);
169 when N_Object_Renaming_Declaration
=>
170 Expand_SPARK_N_Object_Renaming_Declaration
(N
);
173 Expand_SPARK_N_Op_Ne
(N
);
175 when N_Freeze_Entity
=>
176 -- Currently we only expand type freeze entities, so ignore other
177 -- freeze entites, because it is expensive to create a suitable
178 -- freezing environment.
180 if Is_Type
(Entity
(N
)) then
181 Expand_SPARK_N_Freeze_Entity
(N
);
184 -- In SPARK mode, no other constructs require expansion
191 ----------------------------------
192 -- Expand_SPARK_Delta_Or_Update --
193 ----------------------------------
195 procedure Expand_SPARK_Delta_Or_Update
199 procedure Apply_Range_Checks
(Choice
: Node_Id
);
200 -- Apply range checks on indexes from a deep choice
202 ------------------------
203 -- Apply_Range_Checks --
204 ------------------------
206 procedure Apply_Range_Checks
(Choice
: Node_Id
) is
207 Pref
: Node_Id
:= Choice
;
208 Index
: N_Subexpr_Id
;
211 if Nkind
(Pref
) = N_Indexed_Component
then
212 Index
:= First
(Expressions
(Pref
));
213 Apply_Scalar_Range_Check
(Index
, Etype
(Index
));
215 elsif Is_Array_Type
(Typ
)
216 and then Is_Root_Prefix_Of_Deep_Choice
(Pref
)
219 Apply_Scalar_Range_Check
(Index
, Etype
(Index
));
222 exit when Is_Root_Prefix_Of_Deep_Choice
(Pref
);
224 Pref
:= Prefix
(Pref
);
226 end Apply_Range_Checks
;
232 Comp_Type
: Entity_Id
;
235 Index_Typ
: Entity_Id
;
238 -- Start of processing for Expand_SPARK_Delta_Or_Update
241 -- Apply scalar range checks on the updated components, if needed
243 if Is_Array_Type
(Typ
) then
245 -- Multidimensional arrays
247 if Present
(Next_Index
(First_Index
(Typ
))) then
248 Assoc
:= First
(Component_Associations
(Aggr
));
250 while Present
(Assoc
) loop
251 Expr
:= Expression
(Assoc
);
252 Comp_Type
:= Component_Type
(Typ
);
254 if Is_Scalar_Type
(Comp_Type
) then
255 Apply_Scalar_Range_Check
(Expr
, Comp_Type
);
258 -- The current association contains a sequence of indexes
259 -- denoting an element of a multidimensional array:
261 -- (Index_1, ..., Index_N)
263 Expr
:= First
(Choices
(Assoc
));
265 pragma Assert
(Nkind
(Aggr
) = N_Aggregate
);
267 while Present
(Expr
) loop
268 Index
:= First
(Expressions
(Expr
));
269 Index_Typ
:= First_Index
(Typ
);
271 while Present
(Index_Typ
) loop
272 Apply_Scalar_Range_Check
(Index
, Etype
(Index_Typ
));
274 Next_Index
(Index_Typ
);
283 -- One-dimensional arrays
286 Assoc
:= First
(Component_Associations
(Aggr
));
288 while Present
(Assoc
) loop
289 Expr
:= Expression
(Assoc
);
290 Comp_Type
:= Component_Type
(Typ
);
292 -- Analyze expression of the iterated_component_association
293 -- with its index parameter in scope.
295 if Nkind
(Assoc
) = N_Iterated_Component_Association
then
296 Push_Scope
(Scope
(Defining_Identifier
(Assoc
)));
297 Enter_Name
(Defining_Identifier
(Assoc
));
298 Analyze_And_Resolve
(Expr
, Comp_Type
);
301 if Is_Scalar_Type
(Comp_Type
) then
302 Apply_Scalar_Range_Check
(Expr
, Comp_Type
);
305 -- Restore scope of the iterated_component_association
307 if Nkind
(Assoc
) = N_Iterated_Component_Association
then
311 Index
:= First
(Choice_List
(Assoc
));
312 Index_Typ
:= First_Index
(Typ
);
314 while Present
(Index
) loop
315 -- If the index denotes a range of elements or a constrained
316 -- subtype indication, then their low and high bounds
317 -- already have range checks applied.
319 if Nkind
(Index
) in N_Range | N_Subtype_Indication
then
322 elsif Is_Deep_Choice
(Index
, Typ
) then
323 Apply_Range_Checks
(Index
);
325 -- Otherwise the index denotes a single expression where
326 -- range checks need to be applied or a subtype name
327 -- (without range constraints) where applying checks is
330 -- In delta_aggregate and Update attribute on array the
331 -- others_choice is not allowed.
333 else pragma Assert
(Nkind
(Index
) in N_Subexpr
);
334 Apply_Scalar_Range_Check
(Index
, Etype
(Index_Typ
));
344 else pragma Assert
(Is_Record_Type
(Typ
));
346 -- If the aggregate has multiple component choices, e.g.:
348 -- X'Update (A | B | C => 123)
350 -- then each component might be of a different type and might or
351 -- might not require a range check. We first rewrite associations
352 -- into single-component choices, e.g.:
354 -- X'Update (A => 123, B => 123, C => 123)
356 -- and then apply range checks to individual copies of the
357 -- expressions. We do the same for delta aggregates, accordingly.
359 -- Iterate over associations of the original aggregate
361 Assoc
:= First
(Component_Associations
(Aggr
));
363 -- Rewrite into a new aggregate and decorate
370 (Sloc
=> Sloc
(Aggr
),
371 Component_Associations
=> New_List
));
373 when N_Delta_Aggregate
=>
377 (Sloc
=> Sloc
(Aggr
),
378 Expression
=> Expression
(Aggr
),
379 Component_Associations
=> New_List
));
385 Set_Etype
(Aggr
, Typ
);
387 -- Populate the new aggregate with component associations
389 while Present
(Assoc
) loop
390 Expr
:= Expression
(Assoc
);
391 Comp
:= First
(Choices
(Assoc
));
393 while Present
(Comp
) loop
394 if Is_Deep_Choice
(Comp
, Typ
) then
395 Comp_Type
:= Etype
(Comp
);
397 Comp_Type
:= Etype
(Entity
(Comp
));
401 Make_Component_Association
402 (Sloc
=> Sloc
(Assoc
),
403 Choices
=> New_List
(New_Copy_Tree
(Comp
)),
404 Expression
=> New_Copy_Tree
(Expr
));
406 -- New association must be attached to the aggregate before we
409 Append
(New_Assoc
, Component_Associations
(Aggr
));
411 Analyze_And_Resolve
(Expression
(New_Assoc
), Comp_Type
);
413 if Is_Deep_Choice
(Comp
, Typ
) then
414 Apply_Range_Checks
(First
(Choices
(New_Assoc
)));
417 if Is_Scalar_Type
(Comp_Type
) then
418 Apply_Scalar_Range_Check
419 (Expression
(New_Assoc
), Comp_Type
);
428 end Expand_SPARK_Delta_Or_Update
;
430 ------------------------------
431 -- Expand_SPARK_N_Aggregate --
432 ------------------------------
434 procedure Expand_SPARK_N_Aggregate
(N
: Node_Id
) is
438 procedure Parse_Named_Subp
439 (Subp
: Subprogram_Kind_Id
;
440 Key_Type
: out Type_Kind_Id
;
441 Element_Type
: out Type_Kind_Id
);
442 -- Retrieve key and element types from subprogram for named addition
444 procedure Parse_Unnamed_Subp
445 (Subp
: Subprogram_Kind_Id
;
446 Element_Type
: out Type_Kind_Id
);
447 -- Retrieve element types from subprogram for unnamed addition
449 procedure Wrap_For_Checks
(Expr
: N_Subexpr_Id
; Typ
: Type_Kind_Id
);
450 -- If Expr might require a range check for conversion to type Typ, set
451 -- Do_Range_Check on Expr. In all cases, wrap Expr in a type conversion
452 -- if Typ is not the type of Expr already, for GNATprove to correctly
453 -- identity the target type for the range check and insert any other
456 ----------------------
457 -- Parse_Named_Subp --
458 ----------------------
460 procedure Parse_Named_Subp
461 (Subp
: Subprogram_Kind_Id
;
462 Key_Type
: out Type_Kind_Id
;
463 Element_Type
: out Type_Kind_Id
)
465 Formal
: Entity_Id
:= First_Formal
(Subp
);
467 Next_Formal
(Formal
);
468 Key_Type
:= Etype
(Formal
);
469 Next_Formal
(Formal
);
470 Element_Type
:= Etype
(Formal
);
471 end Parse_Named_Subp
;
473 ------------------------
474 -- Parse_Unnamed_Subp --
475 ------------------------
477 procedure Parse_Unnamed_Subp
478 (Subp
: Subprogram_Kind_Id
;
479 Element_Type
: out Type_Kind_Id
)
481 Formal
: Entity_Id
:= First_Formal
(Subp
);
483 Next_Formal
(Formal
);
484 Element_Type
:= Etype
(Formal
);
485 end Parse_Unnamed_Subp
;
487 ---------------------
488 -- Wrap_For_Checks --
489 ---------------------
491 procedure Wrap_For_Checks
(Expr
: N_Subexpr_Id
; Typ
: Type_Kind_Id
) is
493 if Is_Scalar_Type
(Typ
) then
494 Apply_Scalar_Range_Check
(Expr
, Typ
);
497 Convert_To_And_Rewrite
(Typ
, Expr
);
502 Typ
: constant Entity_Id
:= Etype
(N
);
503 Asp
: constant Node_Id
:= Find_Value_Of_Aspect
(Typ
, Aspect_Aggregate
);
505 Empty_Subp
: Node_Id
:= Empty
;
506 Add_Named_Subp
: Node_Id
:= Empty
;
507 Add_Unnamed_Subp
: Node_Id
:= Empty
;
508 New_Indexed_Subp
: Node_Id
:= Empty
;
509 Assign_Indexed_Subp
: Node_Id
:= Empty
;
510 Key_Type
: Entity_Id
;
511 Element_Type
: Entity_Id
;
513 Assocs
: constant List_Id
:= Component_Associations
(N
);
514 Exprs
: constant List_Id
:= Expressions
(N
);
519 -- Start of processing for Expand_SPARK_N_Aggregate
522 if Is_Container_Aggregate
(N
) then
524 Parse_Aspect_Aggregate
(Asp
,
525 Empty_Subp
, Add_Named_Subp
, Add_Unnamed_Subp
,
526 New_Indexed_Subp
, Assign_Indexed_Subp
);
528 Assoc
:= First
(Assocs
);
529 Expr
:= First
(Exprs
);
531 -- Both lists could be empty as in [] but they can't be both
533 pragma Assert
(not (Present
(Assoc
) and then Present
(Expr
)));
535 -- Deal with cases supported in GNATprove:
536 -- - named container aggregate which is not an indexed aggregate
537 -- - positional container aggregate
540 and then Present
(Add_Named_Subp
)
542 Parse_Named_Subp
(Entity
(Add_Named_Subp
), Key_Type
, Element_Type
);
544 while Present
(Assoc
) loop
545 Choice
:= First
(Choice_List
(Assoc
));
547 while Present
(Choice
) loop
548 Wrap_For_Checks
(Choice
, Key_Type
);
552 Wrap_For_Checks
(Expression
(Assoc
), Element_Type
);
556 elsif Present
(Expr
) then
557 Parse_Unnamed_Subp
(Entity
(Add_Unnamed_Subp
), Element_Type
);
559 while Present
(Expr
) loop
560 Wrap_For_Checks
(Expr
, Element_Type
);
565 end Expand_SPARK_N_Aggregate
;
567 ----------------------------------
568 -- Expand_SPARK_N_Freeze_Entity --
569 ----------------------------------
571 procedure Expand_SPARK_N_Freeze_Entity
(N
: Entity_Id
) is
572 E
: constant Entity_Id
:= Entity
(N
);
576 In_Other_Scope
: Boolean;
577 In_Outer_Scope
: Boolean;
580 -- Here E is a type or a subprogram
582 E_Scope
:= Scope
(E
);
584 -- This is an error protection against previous errors
587 Check_Error_Detected
;
591 -- The entity may be a subtype declared for a constrained record
592 -- component, in which case the relevant scope is the scope of
593 -- the record. This happens for class-wide subtypes created for
594 -- a constrained type extension with inherited discriminants.
597 and then not Is_Concurrent_Type
(E_Scope
)
599 E_Scope
:= Scope
(E_Scope
);
601 -- The entity may be a subtype declared for an iterator
603 elsif Ekind
(E_Scope
) = E_Loop
then
604 E_Scope
:= Scope
(E_Scope
);
607 -- If we are freezing entities defined in protected types, they belong
608 -- in the enclosing scope, given that the original type has been
609 -- expanded away. The same is true for entities in task types, in
610 -- particular the parameter records of entries (Entities in bodies are
611 -- all frozen within the body). If we are in the task body, this is a
612 -- proper scope. If we are within a subprogram body, the proper scope
613 -- is the corresponding spec. This may happen for itypes generated in
614 -- the bodies of protected operations.
616 if Ekind
(E_Scope
) = E_Protected_Type
617 or else (Ekind
(E_Scope
) = E_Task_Type
618 and then not Has_Completion
(E_Scope
))
620 E_Scope
:= Scope
(E_Scope
);
622 elsif Ekind
(E_Scope
) = E_Subprogram_Body
then
623 E_Scope
:= Corresponding_Spec
(Unit_Declaration_Node
(E_Scope
));
626 -- If the scope of the entity is in open scopes, it is the current one
627 -- or an enclosing one, including a loop, a block, or a subprogram.
629 if In_Open_Scopes
(E_Scope
) then
630 In_Other_Scope
:= False;
631 In_Outer_Scope
:= E_Scope
/= Current_Scope
;
633 -- Otherwise it is a local package or a different compilation unit
636 In_Other_Scope
:= True;
637 In_Outer_Scope
:= False;
640 -- If the entity being frozen is defined in a scope that is not
641 -- currently on the scope stack, we must establish the proper
642 -- visibility before freezing the entity and related subprograms.
644 if In_Other_Scope
then
645 Push_Scope
(E_Scope
);
647 -- Finalizers are little odd in terms of freezing. The spec of the
648 -- procedure appears in the declarations while the body appears in
649 -- the statement part of a single construct. Since the finalizer must
650 -- be called by the At_End handler of the construct, the spec is
651 -- manually frozen right after its declaration. The only side effect
652 -- of this action appears in contexts where the construct is not in
653 -- its final resting place. These contexts are:
655 -- * Entry bodies - The declarations and statements are moved to
656 -- the procedure equivalen of the entry.
657 -- * Protected subprograms - The declarations and statements are
658 -- moved to the non-protected version of the subprogram.
659 -- * Task bodies - The declarations and statements are moved to the
660 -- task body procedure.
661 -- * Blocks that will be rewritten as subprograms when unnesting
664 -- Visible declarations do not need to be installed in these three
665 -- cases since it does not make semantic sense to do so. All entities
666 -- referenced by a finalizer are visible and already resolved, plus
667 -- the enclosing scope may not have visible declarations at all.
669 if Ekind
(E
) = E_Procedure
670 and then Is_Finalizer
(E
)
673 or else (Is_Subprogram
(E_Scope
)
674 and then Is_Protected_Type
(Scope
(E_Scope
)))
675 or else Is_Task_Type
(E_Scope
)
676 or else Ekind
(E_Scope
) = E_Block
)
680 Install_Visible_Declarations
(E_Scope
);
683 if Is_Concurrent_Type
(E_Scope
)
684 or else Is_Package_Or_Generic_Package
(E_Scope
)
686 Install_Private_Declarations
(E_Scope
);
689 -- If the entity is in an outer scope, then that scope needs to
690 -- temporarily become the current scope so that operations created
691 -- during type freezing will be declared in the right scope and
692 -- can properly override any corresponding inherited operations.
694 elsif In_Outer_Scope
then
695 Push_Scope
(E_Scope
);
698 -- Remember that we are processing a freezing entity and its freezing
699 -- nodes. This flag (non-zero = set) is used to avoid the need of
700 -- climbing through the tree while processing the freezing actions (ie.
701 -- to avoid generating spurious warnings or to avoid killing constant
702 -- indications while processing the code associated with freezing
703 -- actions). We use a counter to deal with nesting.
705 Inside_Freezing_Actions
:= Inside_Freezing_Actions
+ 1;
707 -- Currently only types require freezing in SPARK
709 SPARK_Freeze_Type
(N
);
711 -- Analyze actions in freeze node, if any
713 Action
:= First
(Actions
(N
));
714 while Present
(Action
) loop
719 -- Pop scope if we installed one for the analysis
721 if In_Other_Scope
then
722 if Ekind
(Current_Scope
) = E_Package
then
723 End_Package_Scope
(E_Scope
);
728 elsif In_Outer_Scope
then
732 -- Restore previous value of the nesting-level counter that records
733 -- whether we are inside a (possibly nested) call to this procedure.
735 Inside_Freezing_Actions
:= Inside_Freezing_Actions
- 1;
736 end Expand_SPARK_N_Freeze_Entity
;
738 ----------------------------------------
739 -- Expand_SPARK_N_Attribute_Reference --
740 ----------------------------------------
742 procedure Expand_SPARK_N_Attribute_Reference
(N
: Node_Id
) is
743 Aname
: constant Name_Id
:= Attribute_Name
(N
);
744 Attr_Id
: constant Attribute_Id
:= Get_Attribute_Id
(Aname
);
745 Loc
: constant Source_Ptr
:= Sloc
(N
);
746 Pref
: constant Node_Id
:= Prefix
(N
);
747 Typ
: constant Entity_Id
:= Etype
(N
);
752 when Attribute_To_Address
=>
754 -- Extract and convert argument to expected type for call
757 Make_Type_Conversion
(Loc
,
759 New_Occurrence_Of
(RTE
(RE_Integer_Address
), Loc
),
760 Expression
=> Relocate_Node
(First
(Expressions
(N
))));
762 -- Replace attribute reference with call
766 Make_Function_Call
(Loc
,
768 New_Occurrence_Of
(RTE
(RE_To_Address
), Loc
),
769 Parameter_Associations
=> New_List
(Expr
)));
770 Analyze_And_Resolve
(N
, Typ
);
772 when Attribute_Object_Size
774 | Attribute_Value_Size
775 | Attribute_VADS_Size
777 Exp_Attr
.Expand_Size_Attribute
(N
);
779 -- For attributes which return Universal_Integer, introduce a
780 -- conversion to the expected type with the appropriate check flags
784 | Attribute_Alignment
786 | Attribute_Bit_Position
787 | Attribute_Descriptor_Size
788 | Attribute_First_Bit
791 | Attribute_Max_Alignment_For_Allocation
792 | Attribute_Max_Size_In_Storage_Elements
795 | Attribute_Range_Length
797 -- If the expected type is Long_Long_Integer, there will be no
798 -- check flag as the compiler assumes attributes always fit in
799 -- this type. Since in SPARK_Mode we do not take Storage_Error
800 -- into account, we cannot make this assumption and need to
801 -- produce a check. ??? It should be enough to add this check for
802 -- attributes 'Length, 'Range_Length and 'Pos when the type is as
803 -- big as Long_Long_Integer.
808 if Attr_Id
in Attribute_Pos | Attribute_Range_Length
then
809 Typ
:= Etype
(Prefix
(N
));
811 elsif Attr_Id
= Attribute_Length
then
812 Typ
:= Get_Index_Subtype
(N
);
818 Apply_Universal_Integer_Attribute_Checks
(N
);
821 and then Known_RM_Size
(Typ
)
822 and then RM_Size
(Typ
) = RM_Size
(Standard_Long_Long_Integer
)
824 -- ??? This should rather be a range check, but this would
825 -- crash GNATprove which somehow recovers the proper kind
827 Set_Do_Overflow_Check
(N
);
831 when Attribute_Constrained
=>
833 -- If the prefix is an access to object, the attribute applies to
834 -- the designated object, so rewrite with an explicit dereference.
836 if Is_Access_Type
(Etype
(Pref
))
838 (not Is_Entity_Name
(Pref
) or else Is_Object
(Entity
(Pref
)))
841 Make_Explicit_Dereference
(Loc
, Relocate_Node
(Pref
)));
842 Analyze_And_Resolve
(N
, Standard_Boolean
);
845 when Attribute_Update
=>
846 Expand_SPARK_Delta_Or_Update
(Typ
, First
(Expressions
(N
)));
851 end Expand_SPARK_N_Attribute_Reference
;
853 ------------------------------------
854 -- Expand_SPARK_N_Delta_Aggregate --
855 ------------------------------------
857 procedure Expand_SPARK_N_Delta_Aggregate
(N
: Node_Id
) is
859 Expand_SPARK_Delta_Or_Update
(Etype
(N
), N
);
860 end Expand_SPARK_N_Delta_Aggregate
;
862 -----------------------------------
863 -- Expand_SPARK_N_Loop_Statement --
864 -----------------------------------
866 procedure Expand_SPARK_N_Loop_Statement
(N
: Node_Id
) is
867 Scheme
: constant Node_Id
:= Iteration_Scheme
(N
);
870 -- Loop iterations over arrays need to be expanded, to avoid getting
871 -- two names referring to the same object in memory (the array and the
872 -- iterator) in GNATprove, especially since both can be written (thus
873 -- possibly leading to interferences due to aliasing). No such problem
874 -- arises with quantified expressions over arrays, which are dealt with
875 -- specially in GNATprove.
878 and then Present
(Iterator_Specification
(Scheme
))
879 and then Is_Iterator_Over_Array
(Iterator_Specification
(Scheme
))
881 Expand_Iterator_Loop_Over_Array
(N
);
883 end Expand_SPARK_N_Loop_Statement
;
885 ---------------------------------------
886 -- Expand_SPARK_N_Object_Declaration --
887 ---------------------------------------
889 procedure Expand_SPARK_N_Object_Declaration
(N
: Node_Id
) is
890 Loc
: constant Source_Ptr
:= Sloc
(N
);
891 Obj_Id
: constant Entity_Id
:= Defining_Identifier
(N
);
892 Typ
: constant Entity_Id
:= Etype
(Obj_Id
);
897 -- If the object declaration denotes a variable without initialization
898 -- whose type is subject to pragma Default_Initial_Condition, create
899 -- and analyze a dummy call to the DIC procedure of the type in order
900 -- to detect potential elaboration issues.
902 if Comes_From_Source
(Obj_Id
)
903 and then Ekind
(Obj_Id
) = E_Variable
904 and then Has_DIC
(Typ
)
905 and then Present
(DIC_Procedure
(Typ
))
906 and then not Has_Init_Expression
(N
)
908 Call
:= Build_DIC_Call
(Loc
, New_Occurrence_Of
(Obj_Id
, Loc
), Typ
);
910 -- Partially insert the call into the tree by setting its parent
913 Set_Parent
(Call
, N
);
916 end Expand_SPARK_N_Object_Declaration
;
918 ------------------------------------------------
919 -- Expand_SPARK_N_Object_Renaming_Declaration --
920 ------------------------------------------------
922 procedure Expand_SPARK_N_Object_Renaming_Declaration
(N
: Node_Id
) is
923 CFS
: constant Boolean := Comes_From_Source
(N
);
924 Loc
: constant Source_Ptr
:= Sloc
(N
);
925 Obj_Id
: constant Entity_Id
:= Defining_Entity
(N
);
926 Nam
: constant Node_Id
:= Name
(N
);
927 Typ
: constant Entity_Id
:= Etype
(Obj_Id
);
930 -- Transform a renaming of the form
932 -- Obj_Id : <subtype mark> renames <function call>;
936 -- Obj_Id : constant <subtype mark> := <function call>;
938 -- Invoking Evaluate_Name and ultimately Remove_Side_Effects introduces
939 -- a temporary to capture the function result. Once potential renamings
940 -- are rewritten for SPARK, the temporary may be leaked out into source
941 -- constructs and lead to confusing error diagnostics. Using an object
942 -- declaration prevents this unwanted side effect.
944 if Nkind
(Nam
) = N_Function_Call
then
946 Make_Object_Declaration
(Loc
,
947 Defining_Identifier
=> Obj_Id
,
948 Constant_Present
=> True,
949 Object_Definition
=> New_Occurrence_Of
(Typ
, Loc
),
952 -- Inherit the original Comes_From_Source status of the renaming
954 Set_Comes_From_Source
(N
, CFS
);
956 -- Sever the link to the renamed function result because the entity
957 -- will no longer alias anything.
959 Set_Renamed_Object
(Obj_Id
, Empty
);
961 -- Remove the entity of the renaming declaration from visibility as
962 -- the analysis of the object declaration will reintroduce it again.
964 Remove_Entity_And_Homonym
(Obj_Id
);
967 -- Otherwise unconditionally remove all side effects from the name
972 end Expand_SPARK_N_Object_Renaming_Declaration
;
974 --------------------------
975 -- Expand_SPARK_N_Op_Ne --
976 --------------------------
978 procedure Expand_SPARK_N_Op_Ne
(N
: Node_Id
) is
979 Typ
: constant Entity_Id
:= Etype
(Left_Opnd
(N
));
982 -- Case of elementary type with standard operator
984 if Is_Elementary_Type
(Typ
)
985 and then Sloc
(Entity
(N
)) = Standard_Location
990 Exp_Ch4
.Expand_N_Op_Ne
(N
);
992 end Expand_SPARK_N_Op_Ne
;
994 -------------------------------------
995 -- Expand_SPARK_Potential_Renaming --
996 -------------------------------------
998 procedure Expand_SPARK_Potential_Renaming
(N
: Node_Id
) is
999 function In_Insignificant_Pragma
(Nod
: Node_Id
) return Boolean;
1000 -- Determine whether arbitrary node Nod appears within a significant
1001 -- pragma for SPARK.
1003 -----------------------------
1004 -- In_Insignificant_Pragma --
1005 -----------------------------
1007 function In_Insignificant_Pragma
(Nod
: Node_Id
) return Boolean is
1011 -- Climb the parent chain looking for an enclosing pragma
1014 while Present
(Par
) loop
1015 if Nkind
(Par
) = N_Pragma
then
1016 return not Pragma_Significant_In_SPARK
(Get_Pragma_Id
(Par
));
1018 -- Prevent the search from going too far
1020 elsif Is_Body_Or_Package_Declaration
(Par
) then
1024 Par
:= Parent
(Par
);
1028 end In_Insignificant_Pragma
;
1032 Loc
: constant Source_Ptr
:= Sloc
(N
);
1033 Obj_Id
: constant Entity_Id
:= Entity
(N
);
1034 Typ
: constant Entity_Id
:= Etype
(N
);
1037 -- Start of processing for Expand_SPARK_Potential_Renaming
1040 -- Replace a reference to a renaming with the actual renamed object.
1041 -- Protect against previous errors leaving no entity in N.
1044 and then Is_Object
(Obj_Id
)
1046 Ren
:= Renamed_Object
(Obj_Id
);
1048 if Present
(Ren
) then
1050 -- Do not process a reference when it appears within a pragma of
1051 -- no significance to SPARK. It is assumed that the replacement
1052 -- will violate the semantics of the pragma and cause a spurious
1055 if In_Insignificant_Pragma
(N
) then
1058 -- Instantiations and inlining of subprograms employ "prologues"
1059 -- which map actual to formal parameters by means of renamings.
1060 -- Replace a reference to a formal by the corresponding actual
1063 elsif Nkind
(Ren
) in N_Entity
then
1064 Rewrite
(N
, New_Occurrence_Of
(Ren
, Loc
));
1066 -- Otherwise the renamed object denotes a name
1069 Rewrite
(N
, New_Copy_Tree
(Ren
, New_Sloc
=> Loc
));
1070 Reset_Analyzed_Flags
(N
);
1073 Analyze_And_Resolve
(N
, Typ
);
1076 end Expand_SPARK_Potential_Renaming
;
1078 -----------------------
1079 -- SPARK_Freeze_Type --
1080 -----------------------
1082 procedure SPARK_Freeze_Type
(N
: Entity_Id
) is
1083 Typ
: constant Entity_Id
:= Entity
(N
);
1085 Renamed_Eq
: Entity_Id
;
1086 -- Defining unit name for the predefined equality function in the case
1087 -- where the type has a primitive operation that is a renaming of
1088 -- predefined equality (but only if there is also an overriding
1089 -- user-defined equality function). Used to pass this entity from
1090 -- Make_Predefined_Primitive_Specs to Predefined_Primitive_Bodies.
1093 Eq_Spec
: Node_Id
:= Empty
;
1094 Predef_List
: List_Id
;
1096 Wrapper_Decl_List
: List_Id
;
1097 Wrapper_Body_List
: List_Id
:= No_List
;
1099 Saved_GM
: constant Ghost_Mode_Type
:= Ghost_Mode
;
1100 Saved_IGR
: constant Node_Id
:= Ignored_Ghost_Region
;
1101 -- Save the Ghost-related attributes to restore on exit
1104 -- The type being frozen may be subject to pragma Ghost. Set the mode
1105 -- now to ensure that any nodes generated during freezing are properly
1108 Set_Ghost_Mode
(Typ
);
1110 -- Generate the [spec and] body of the invariant procedure tasked with
1111 -- the runtime verification of all invariants that pertain to the type.
1112 -- This includes invariants on the partial and full view, inherited
1113 -- class-wide invariants from parent types or interfaces, and invariants
1114 -- on array elements or record components. But skip internal types.
1116 if Is_Itype
(Typ
) then
1119 elsif Is_Interface
(Typ
) then
1121 -- Interfaces are treated as the partial view of a private type in
1122 -- order to achieve uniformity with the general case. As a result, an
1123 -- interface receives only a "partial" invariant procedure which is
1126 if Has_Own_Invariants
(Typ
) then
1127 Build_Invariant_Procedure_Body
1129 Partial_Invariant
=> Is_Interface
(Typ
));
1132 -- Non-interface types
1134 -- Do not generate invariant procedure within other assertion
1135 -- subprograms, which may involve local declarations of local
1136 -- subtypes to which these checks do not apply.
1139 if Has_Invariants
(Typ
) then
1140 if not Predicate_Check_In_Scope
(Typ
)
1141 or else (Ekind
(Current_Scope
) = E_Function
1142 and then Is_Predicate_Function
(Current_Scope
))
1146 Build_Invariant_Procedure_Body
(Typ
);
1150 -- Generate the [spec and] body of the procedure tasked with the
1151 -- run-time verification of pragma Default_Initial_Condition's
1154 if Has_DIC
(Typ
) then
1155 Build_DIC_Procedure_Body
(Typ
);
1159 if Ekind
(Typ
) = E_Record_Type
1160 and then Is_Tagged_Type
(Typ
)
1161 and then not Is_Interface
(Typ
)
1162 and then not Is_Limited_Type
(Typ
)
1164 if Is_CPP_Class
(Root_Type
(Typ
))
1165 and then Convention
(Typ
) = Convention_CPP
1169 -- Do not add the spec of the predefined primitives if we are
1170 -- compiling under restriction No_Dispatching_Calls.
1172 elsif not Restriction_Active
(No_Dispatching_Calls
) then
1173 Set_Is_Frozen
(Typ
, False);
1175 Predef_List
:= New_List
;
1176 Exp_Ch3
.Make_Predefined_Primitive_Eq_Spec
1177 (Typ
, Predef_List
, Renamed_Eq
);
1178 Eq_Spec
:= First
(Predef_List
);
1179 Insert_List_Before_And_Analyze
(N
, Predef_List
);
1181 Set_Is_Frozen
(Typ
);
1183 -- Remove link from the parent list to the spec and body of
1184 -- the dispatching equality, but keep the link in the opposite
1185 -- direction, to allow up-traversal of the AST.
1187 if Present
(Eq_Spec
) then
1188 Decl
:= Parent
(Eq_Spec
);
1190 Set_Parent
(Eq_Spec
, Decl
);
1195 if Ekind
(Typ
) = E_Record_Type
1196 and then Is_Tagged_Type
(Typ
)
1197 and then not Is_CPP_Class
(Typ
)
1199 -- Ada 2005 (AI-391): For a nonabstract null extension, create
1200 -- wrapper functions for each nonoverridden inherited function
1201 -- with a controlling result of the type. The wrapper for such
1202 -- a function returns an extension aggregate that invokes the
1205 if Ada_Version
>= Ada_2005
1206 and then not Is_Abstract_Type
(Typ
)
1207 and then Is_Null_Extension
(Typ
)
1209 Exp_Ch3
.Make_Controlling_Function_Wrappers
1210 (Typ
, Wrapper_Decl_List
, Wrapper_Body_List
);
1211 Insert_List_Before_And_Analyze
(N
, Wrapper_Decl_List
);
1214 -- Ada 2005 (AI-391): If any wrappers were created for nonoverridden
1215 -- inherited functions, then add their bodies to the AST, so they
1216 -- will be processed like ordinary subprogram bodies (even though the
1217 -- compiler adds them into the freezing action).
1219 if not Is_Interface
(Typ
) then
1220 Insert_List_Before_And_Analyze
(N
, Wrapper_Body_List
);
1224 Restore_Ghost_Region
(Saved_GM
, Saved_IGR
);
1225 end SPARK_Freeze_Type
;