1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2021, 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 Casing
; use Casing
;
29 with Checks
; use Checks
;
30 with Debug
; use Debug
;
31 with Einfo
; use Einfo
;
32 with Einfo
.Entities
; use Einfo
.Entities
;
33 with Einfo
.Utils
; use Einfo
.Utils
;
34 with Elists
; use Elists
;
35 with Errout
; use Errout
;
36 with Exp_Aggr
; use Exp_Aggr
;
37 with Exp_Ch6
; use Exp_Ch6
;
38 with Exp_Ch7
; use Exp_Ch7
;
39 with Exp_Ch11
; use Exp_Ch11
;
40 with Freeze
; use Freeze
;
41 with Ghost
; use Ghost
;
42 with Inline
; use Inline
;
43 with Itypes
; use Itypes
;
45 with Nlists
; use Nlists
;
46 with Nmake
; use Nmake
;
48 with Restrict
; use Restrict
;
49 with Rident
; use Rident
;
51 with Sem_Aux
; use Sem_Aux
;
52 with Sem_Ch3
; use Sem_Ch3
;
53 with Sem_Ch6
; use Sem_Ch6
;
54 with Sem_Ch8
; use Sem_Ch8
;
55 with Sem_Ch12
; use Sem_Ch12
;
56 with Sem_Ch13
; use Sem_Ch13
;
57 with Sem_Disp
; use Sem_Disp
;
58 with Sem_Elab
; use Sem_Elab
;
59 with Sem_Eval
; use Sem_Eval
;
60 with Sem_Res
; use Sem_Res
;
61 with Sem_Type
; use Sem_Type
;
62 with Sem_Util
; use Sem_Util
;
63 with Sinfo
.Utils
; use Sinfo
.Utils
;
64 with Snames
; use Snames
;
65 with Stand
; use Stand
;
66 with Stringt
; use Stringt
;
67 with Tbuild
; use Tbuild
;
68 with Ttypes
; use Ttypes
;
69 with Validsw
; use Validsw
;
72 package body Exp_Util
is
74 ---------------------------------------------------------
75 -- Handling of inherited class-wide pre/postconditions --
76 ---------------------------------------------------------
78 -- Following AI12-0113, the expression for a class-wide condition is
79 -- transformed for a subprogram that inherits it, by replacing calls
80 -- to primitive operations of the original controlling type into the
81 -- corresponding overriding operations of the derived type. The following
82 -- hash table manages this mapping, and is expanded on demand whenever
83 -- such inherited expression needs to be constructed.
85 -- The mapping is also used to check whether an inherited operation has
86 -- a condition that depends on overridden operations. For such an
87 -- operation we must create a wrapper that is then treated as a normal
88 -- overriding. In SPARK mode such operations are illegal.
90 -- For a given root type there may be several type extensions with their
91 -- own overriding operations, so at various times a given operation of
92 -- the root will be mapped into different overridings. The root type is
93 -- also mapped into the current type extension to indicate that its
94 -- operations are mapped into the overriding operations of that current
97 -- The contents of the map are as follows:
101 -- Discriminant (Entity_Id) Discriminant (Entity_Id)
102 -- Discriminant (Entity_Id) Non-discriminant name (Entity_Id)
103 -- Discriminant (Entity_Id) Expression (Node_Id)
104 -- Primitive subprogram (Entity_Id) Primitive subprogram (Entity_Id)
105 -- Type (Entity_Id) Type (Entity_Id)
107 Type_Map_Size
: constant := 511;
109 subtype Type_Map_Header
is Integer range 0 .. Type_Map_Size
- 1;
110 function Type_Map_Hash
(Id
: Entity_Id
) return Type_Map_Header
;
112 package Type_Map
is new GNAT
.HTable
.Simple_HTable
113 (Header_Num
=> Type_Map_Header
,
115 Element
=> Node_Or_Entity_Id
,
117 Hash
=> Type_Map_Hash
,
120 -----------------------
121 -- Local Subprograms --
122 -----------------------
124 function Build_Task_Array_Image
128 Dyn
: Boolean := False) return Node_Id
;
129 -- Build function to generate the image string for a task that is an array
130 -- component, concatenating the images of each index. To avoid storage
131 -- leaks, the string is built with successive slice assignments. The flag
132 -- Dyn indicates whether this is called for the initialization procedure of
133 -- an array of tasks, or for the name of a dynamically created task that is
134 -- assigned to an indexed component.
136 function Build_Task_Image_Function
140 Res
: Entity_Id
) return Node_Id
;
141 -- Common processing for Task_Array_Image and Task_Record_Image. Build
142 -- function body that computes image.
144 procedure Build_Task_Image_Prefix
153 -- Common processing for Task_Array_Image and Task_Record_Image. Create
154 -- local variables and assign prefix of name to result string.
156 function Build_Task_Record_Image
159 Dyn
: Boolean := False) return Node_Id
;
160 -- Build function to generate the image string for a task that is a record
161 -- component. Concatenate name of variable with that of selector. The flag
162 -- Dyn indicates whether this is called for the initialization procedure of
163 -- record with task components, or for a dynamically created task that is
164 -- assigned to a selected component.
166 procedure Evaluate_Slice_Bounds
(Slice
: Node_Id
);
167 -- Force evaluation of bounds of a slice, which may be given by a range
168 -- or by a subtype indication with or without a constraint.
170 function Is_Verifiable_DIC_Pragma
(Prag
: Node_Id
) return Boolean;
171 -- Determine whether pragma Default_Initial_Condition denoted by Prag has
172 -- an assertion expression that should be verified at run time.
174 function Is_Uninitialized_Aggregate
176 T
: Entity_Id
) return Boolean;
177 -- Determine whether an array aggregate used in an object declaration
178 -- is uninitialized, when the aggregate is declared with a box and
179 -- the component type has no default value. Such an aggregate can be
180 -- optimized away to prevent the copying of uninitialized data, and
181 -- the bounds of the aggregate can be propagated directly to the
182 -- object declaration.
184 function Make_CW_Equivalent_Type
186 E
: Node_Id
) return Entity_Id
;
187 -- T is a class-wide type entity, E is the initial expression node that
188 -- constrains T in case such as: " X: T := E" or "new T'(E)". This function
189 -- returns the entity of the Equivalent type and inserts on the fly the
190 -- necessary declaration such as:
192 -- type anon is record
193 -- _parent : Root_Type (T); constrained with E discriminants (if any)
194 -- Extension : String (1 .. expr to match size of E);
197 -- This record is compatible with any object of the class of T thanks to
198 -- the first field and has the same size as E thanks to the second.
200 function Make_Literal_Range
202 Literal_Typ
: Entity_Id
) return Node_Id
;
203 -- Produce a Range node whose bounds are:
204 -- Low_Bound (Literal_Type) ..
205 -- Low_Bound (Literal_Type) + (Length (Literal_Typ) - 1)
206 -- this is used for expanding declarations like X : String := "sdfgdfg";
208 -- If the index type of the target array is not integer, we generate:
209 -- Low_Bound (Literal_Type) ..
211 -- (Literal_Type'Pos (Low_Bound (Literal_Type))
212 -- + (Length (Literal_Typ) -1))
214 function Make_Non_Empty_Check
216 N
: Node_Id
) return Node_Id
;
217 -- Produce a boolean expression checking that the unidimensional array
218 -- node N is not empty.
220 function New_Class_Wide_Subtype
222 N
: Node_Id
) return Entity_Id
;
223 -- Create an implicit subtype of CW_Typ attached to node N
225 function Requires_Cleanup_Actions
228 Nested_Constructs
: Boolean) return Boolean;
229 -- Given a list L, determine whether it contains one of the following:
231 -- 1) controlled objects
232 -- 2) library-level tagged types
234 -- Lib_Level is True when the list comes from a construct at the library
235 -- level, and False otherwise. Nested_Constructs is True when any nested
236 -- packages declared in L must be processed, and False otherwise.
238 function Side_Effect_Free_Attribute
(Name
: Name_Id
) return Boolean;
239 -- Return True if the evaluation of the given attribute is considered
240 -- side-effect free, independently of its prefix and expressions.
242 -------------------------------------
243 -- Activate_Atomic_Synchronization --
244 -------------------------------------
246 procedure Activate_Atomic_Synchronization
(N
: Node_Id
) is
250 case Nkind
(Parent
(N
)) is
252 -- Check for cases of appearing in the prefix of a construct where we
253 -- don't need atomic synchronization for this kind of usage.
256 -- Nothing to do if we are the prefix of an attribute, since we
257 -- do not want an atomic sync operation for things like 'Size.
259 N_Attribute_Reference
261 -- The N_Reference node is like an attribute
265 -- Nothing to do for a reference to a component (or components)
266 -- of a composite object. Only reads and updates of the object
267 -- as a whole require atomic synchronization (RM C.6 (15)).
269 | N_Indexed_Component
270 | N_Selected_Component
273 -- For all the above cases, nothing to do if we are the prefix
275 if Prefix
(Parent
(N
)) = N
then
283 -- Nothing to do for the identifier in an object renaming declaration,
284 -- the renaming itself does not need atomic synchronization.
286 if Nkind
(Parent
(N
)) = N_Object_Renaming_Declaration
then
290 -- Go ahead and set the flag
292 Set_Atomic_Sync_Required
(N
);
294 -- Generate info message if requested
296 if Warn_On_Atomic_Synchronization
then
302 | N_Selected_Component
304 Msg_Node
:= Selector_Name
(N
);
306 when N_Explicit_Dereference
307 | N_Indexed_Component
312 pragma Assert
(False);
316 if Present
(Msg_Node
) then
318 ("info: atomic synchronization set for &?.n?", Msg_Node
);
321 ("info: atomic synchronization set?.n?", N
);
324 end Activate_Atomic_Synchronization
;
326 ----------------------
327 -- Adjust_Condition --
328 ----------------------
330 procedure Adjust_Condition
(N
: Node_Id
) is
337 Loc
: constant Source_Ptr
:= Sloc
(N
);
338 T
: constant Entity_Id
:= Etype
(N
);
341 -- Defend against a call where the argument has no type, or has a
342 -- type that is not Boolean. This can occur because of prior errors.
344 if No
(T
) or else not Is_Boolean_Type
(T
) then
348 -- Apply validity checking if needed
350 if Validity_Checks_On
and Validity_Check_Tests
then
354 -- Immediate return if standard boolean, the most common case,
355 -- where nothing needs to be done.
357 if Base_Type
(T
) = Standard_Boolean
then
361 -- Case of zero/nonzero semantics or nonstandard enumeration
362 -- representation. In each case, we rewrite the node as:
364 -- ityp!(N) /= False'Enum_Rep
366 -- where ityp is an integer type with large enough size to hold any
369 if Nonzero_Is_True
(T
) or else Has_Non_Standard_Rep
(T
) then
374 (Integer_Type_For
(Esize
(T
), Uns
=> False), N
),
376 Make_Attribute_Reference
(Loc
,
377 Attribute_Name
=> Name_Enum_Rep
,
379 New_Occurrence_Of
(First_Literal
(T
), Loc
))));
380 Analyze_And_Resolve
(N
, Standard_Boolean
);
383 Rewrite
(N
, Convert_To
(Standard_Boolean
, N
));
384 Analyze_And_Resolve
(N
, Standard_Boolean
);
387 end Adjust_Condition
;
389 ------------------------
390 -- Adjust_Result_Type --
391 ------------------------
393 procedure Adjust_Result_Type
(N
: Node_Id
; T
: Entity_Id
) is
395 -- Ignore call if current type is not Standard.Boolean
397 if Etype
(N
) /= Standard_Boolean
then
401 -- If result is already of correct type, nothing to do. Note that
402 -- this will get the most common case where everything has a type
403 -- of Standard.Boolean.
405 if Base_Type
(T
) = Standard_Boolean
then
410 KP
: constant Node_Kind
:= Nkind
(Parent
(N
));
413 -- If result is to be used as a Condition in the syntax, no need
414 -- to convert it back, since if it was changed to Standard.Boolean
415 -- using Adjust_Condition, that is just fine for this usage.
417 if KP
in N_Raise_xxx_Error
or else KP
in N_Has_Condition
then
420 -- If result is an operand of another logical operation, no need
421 -- to reset its type, since Standard.Boolean is just fine, and
422 -- such operations always do Adjust_Condition on their operands.
424 elsif KP
in N_Op_Boolean
425 or else KP
in N_Short_Circuit
426 or else KP
= N_Op_Not
430 -- Otherwise we perform a conversion from the current type, which
431 -- must be Standard.Boolean, to the desired type. Use the base
432 -- type to prevent spurious constraint checks that are extraneous
433 -- to the transformation. The type and its base have the same
434 -- representation, standard or otherwise.
438 Rewrite
(N
, Convert_To
(Base_Type
(T
), N
));
439 Analyze_And_Resolve
(N
, Base_Type
(T
));
443 end Adjust_Result_Type
;
445 --------------------------
446 -- Append_Freeze_Action --
447 --------------------------
449 procedure Append_Freeze_Action
(T
: Entity_Id
; N
: Node_Id
) is
453 Ensure_Freeze_Node
(T
);
454 Fnode
:= Freeze_Node
(T
);
456 if No
(Actions
(Fnode
)) then
457 Set_Actions
(Fnode
, New_List
(N
));
459 Append
(N
, Actions
(Fnode
));
461 end Append_Freeze_Action
;
463 ---------------------------
464 -- Append_Freeze_Actions --
465 ---------------------------
467 procedure Append_Freeze_Actions
(T
: Entity_Id
; L
: List_Id
) is
475 Ensure_Freeze_Node
(T
);
476 Fnode
:= Freeze_Node
(T
);
478 if No
(Actions
(Fnode
)) then
479 Set_Actions
(Fnode
, L
);
481 Append_List
(L
, Actions
(Fnode
));
483 end Append_Freeze_Actions
;
485 ----------------------------------------
486 -- Attribute_Constrained_Static_Value --
487 ----------------------------------------
489 function Attribute_Constrained_Static_Value
(Pref
: Node_Id
) return Boolean
491 Ptyp
: constant Entity_Id
:= Etype
(Pref
);
492 Formal_Ent
: constant Entity_Id
:= Param_Entity
(Pref
);
494 function Is_Constrained_Aliased_View
(Obj
: Node_Id
) return Boolean;
495 -- Ada 2005 (AI-363): Returns True if the object name Obj denotes a
496 -- view of an aliased object whose subtype is constrained.
498 ---------------------------------
499 -- Is_Constrained_Aliased_View --
500 ---------------------------------
502 function Is_Constrained_Aliased_View
(Obj
: Node_Id
) return Boolean is
506 if Is_Entity_Name
(Obj
) then
509 if Present
(Renamed_Object
(E
)) then
510 return Is_Constrained_Aliased_View
(Renamed_Object
(E
));
512 return Is_Aliased
(E
) and then Is_Constrained
(Etype
(E
));
516 return Is_Aliased_View
(Obj
)
518 (Is_Constrained
(Etype
(Obj
))
520 (Nkind
(Obj
) = N_Explicit_Dereference
522 not Object_Type_Has_Constrained_Partial_View
523 (Typ
=> Base_Type
(Etype
(Obj
)),
524 Scop
=> Current_Scope
)));
526 end Is_Constrained_Aliased_View
;
528 -- Start of processing for Attribute_Constrained_Static_Value
531 -- We are in a case where the attribute is known statically, and
532 -- implicit dereferences have been rewritten.
535 (not (Present
(Formal_Ent
)
536 and then Ekind
(Formal_Ent
) /= E_Constant
537 and then Present
(Extra_Constrained
(Formal_Ent
)))
539 not (Is_Access_Type
(Etype
(Pref
))
540 and then (not Is_Entity_Name
(Pref
)
541 or else Is_Object
(Entity
(Pref
))))
543 not (Nkind
(Pref
) = N_Identifier
544 and then Ekind
(Entity
(Pref
)) = E_Variable
545 and then Present
(Extra_Constrained
(Entity
(Pref
)))));
547 if Is_Entity_Name
(Pref
) then
549 Ent
: constant Entity_Id
:= Entity
(Pref
);
553 -- (RM J.4) obsolescent cases
555 if Is_Type
(Ent
) then
559 if Is_Private_Type
(Ent
) then
560 Res
:= not Has_Discriminants
(Ent
)
561 or else Is_Constrained
(Ent
);
563 -- It not a private type, must be a generic actual type
564 -- that corresponded to a private type. We know that this
565 -- correspondence holds, since otherwise the reference
566 -- within the generic template would have been illegal.
569 if Is_Composite_Type
(Underlying_Type
(Ent
)) then
570 Res
:= Is_Constrained
(Ent
);
578 -- If the prefix is not a variable or is aliased, then
579 -- definitely true; if it's a formal parameter without an
580 -- associated extra formal, then treat it as constrained.
582 -- Ada 2005 (AI-363): An aliased prefix must be known to be
583 -- constrained in order to set the attribute to True.
585 if not Is_Variable
(Pref
)
586 or else Present
(Formal_Ent
)
587 or else (Ada_Version
< Ada_2005
588 and then Is_Aliased_View
(Pref
))
589 or else (Ada_Version
>= Ada_2005
590 and then Is_Constrained_Aliased_View
(Pref
))
594 -- Variable case, look at type to see if it is constrained.
595 -- Note that the one case where this is not accurate (the
596 -- procedure formal case), has been handled above.
598 -- We use the Underlying_Type here (and below) in case the
599 -- type is private without discriminants, but the full type
600 -- has discriminants. This case is illegal, but we generate
601 -- it internally for passing to the Extra_Constrained
605 -- In Ada 2012, test for case of a limited tagged type,
606 -- in which case the attribute is always required to
607 -- return True. The underlying type is tested, to make
608 -- sure we also return True for cases where there is an
609 -- unconstrained object with an untagged limited partial
610 -- view which has defaulted discriminants (such objects
611 -- always produce a False in earlier versions of
612 -- Ada). (Ada 2012: AI05-0214)
615 Is_Constrained
(Underlying_Type
(Etype
(Ent
)))
617 (Ada_Version
>= Ada_2012
618 and then Is_Tagged_Type
(Underlying_Type
(Ptyp
))
619 and then Is_Limited_Type
(Ptyp
));
626 -- Prefix is not an entity name. These are also cases where we can
627 -- always tell at compile time by looking at the form and type of the
628 -- prefix. If an explicit dereference of an object with constrained
629 -- partial view, this is unconstrained (Ada 2005: AI95-0363). If the
630 -- underlying type is a limited tagged type, then Constrained is
631 -- required to always return True (Ada 2012: AI05-0214).
634 return not Is_Variable
(Pref
)
636 (Nkind
(Pref
) = N_Explicit_Dereference
638 not Object_Type_Has_Constrained_Partial_View
639 (Typ
=> Base_Type
(Ptyp
),
640 Scop
=> Current_Scope
))
641 or else Is_Constrained
(Underlying_Type
(Ptyp
))
642 or else (Ada_Version
>= Ada_2012
643 and then Is_Tagged_Type
(Underlying_Type
(Ptyp
))
644 and then Is_Limited_Type
(Ptyp
));
646 end Attribute_Constrained_Static_Value
;
648 ------------------------------------
649 -- Build_Allocate_Deallocate_Proc --
650 ------------------------------------
652 procedure Build_Allocate_Deallocate_Proc
654 Is_Allocate
: Boolean)
656 function Find_Object
(E
: Node_Id
) return Node_Id
;
657 -- Given an arbitrary expression of an allocator, try to find an object
658 -- reference in it, otherwise return the original expression.
660 function Is_Allocate_Deallocate_Proc
(Subp
: Entity_Id
) return Boolean;
661 -- Determine whether subprogram Subp denotes a custom allocate or
668 function Find_Object
(E
: Node_Id
) return Node_Id
is
672 pragma Assert
(Is_Allocate
);
676 if Nkind
(Expr
) = N_Explicit_Dereference
then
677 Expr
:= Prefix
(Expr
);
679 elsif Nkind
(Expr
) = N_Qualified_Expression
then
680 Expr
:= Expression
(Expr
);
682 elsif Nkind
(Expr
) = N_Unchecked_Type_Conversion
then
684 -- When interface class-wide types are involved in allocation,
685 -- the expander introduces several levels of address arithmetic
686 -- to perform dispatch table displacement. In this scenario the
687 -- object appears as:
689 -- Tag_Ptr (Base_Address (<object>'Address))
691 -- Detect this case and utilize the whole expression as the
692 -- "object" since it now points to the proper dispatch table.
694 if Is_RTE
(Etype
(Expr
), RE_Tag_Ptr
) then
697 -- Continue to strip the object
700 Expr
:= Expression
(Expr
);
711 ---------------------------------
712 -- Is_Allocate_Deallocate_Proc --
713 ---------------------------------
715 function Is_Allocate_Deallocate_Proc
(Subp
: Entity_Id
) return Boolean is
717 -- Look for a subprogram body with only one statement which is a
718 -- call to Allocate_Any_Controlled / Deallocate_Any_Controlled.
720 if Ekind
(Subp
) = E_Procedure
721 and then Nkind
(Parent
(Parent
(Subp
))) = N_Subprogram_Body
724 HSS
: constant Node_Id
:=
725 Handled_Statement_Sequence
(Parent
(Parent
(Subp
)));
729 if Present
(Statements
(HSS
))
730 and then Nkind
(First
(Statements
(HSS
))) =
731 N_Procedure_Call_Statement
733 Proc
:= Entity
(Name
(First
(Statements
(HSS
))));
736 Is_RTE
(Proc
, RE_Allocate_Any_Controlled
)
737 or else Is_RTE
(Proc
, RE_Deallocate_Any_Controlled
);
743 end Is_Allocate_Deallocate_Proc
;
747 Desig_Typ
: Entity_Id
;
751 Proc_To_Call
: Node_Id
:= Empty
;
753 Use_Secondary_Stack_Pool
: Boolean;
755 -- Start of processing for Build_Allocate_Deallocate_Proc
758 -- Obtain the attributes of the allocation / deallocation
760 if Nkind
(N
) = N_Free_Statement
then
761 Expr
:= Expression
(N
);
762 Ptr_Typ
:= Base_Type
(Etype
(Expr
));
763 Proc_To_Call
:= Procedure_To_Call
(N
);
766 if Nkind
(N
) = N_Object_Declaration
then
767 Expr
:= Expression
(N
);
772 -- In certain cases an allocator with a qualified expression may
773 -- be relocated and used as the initialization expression of a
777 -- Obj : Ptr_Typ := new Desig_Typ'(...);
780 -- Tmp : Ptr_Typ := new Desig_Typ'(...);
781 -- Obj : Ptr_Typ := Tmp;
783 -- Since the allocator is always marked as analyzed to avoid infinite
784 -- expansion, it will never be processed by this routine given that
785 -- the designated type needs finalization actions. Detect this case
786 -- and complete the expansion of the allocator.
788 if Nkind
(Expr
) = N_Identifier
789 and then Nkind
(Parent
(Entity
(Expr
))) = N_Object_Declaration
790 and then Nkind
(Expression
(Parent
(Entity
(Expr
)))) = N_Allocator
792 Build_Allocate_Deallocate_Proc
(Parent
(Entity
(Expr
)), True);
796 -- The allocator may have been rewritten into something else in which
797 -- case the expansion performed by this routine does not apply.
799 if Nkind
(Expr
) /= N_Allocator
then
803 Ptr_Typ
:= Base_Type
(Etype
(Expr
));
804 Proc_To_Call
:= Procedure_To_Call
(Expr
);
807 Pool_Id
:= Associated_Storage_Pool
(Ptr_Typ
);
808 Desig_Typ
:= Available_View
(Designated_Type
(Ptr_Typ
));
810 -- Handle concurrent types
812 if Is_Concurrent_Type
(Desig_Typ
)
813 and then Present
(Corresponding_Record_Type
(Desig_Typ
))
815 Desig_Typ
:= Corresponding_Record_Type
(Desig_Typ
);
818 Use_Secondary_Stack_Pool
:=
819 Is_RTE
(Pool_Id
, RE_SS_Pool
)
820 or else (Nkind
(Expr
) = N_Allocator
821 and then Is_RTE
(Storage_Pool
(Expr
), RE_SS_Pool
));
823 -- Do not process allocations / deallocations without a pool
828 -- Do not process allocations on / deallocations from the secondary
829 -- stack, except for access types used to implement indirect temps.
831 elsif Use_Secondary_Stack_Pool
832 and then not Old_Attr_Util
.Indirect_Temps
833 .Is_Access_Type_For_Indirect_Temp
(Ptr_Typ
)
837 -- Optimize the case where we are using the default Global_Pool_Object,
838 -- and we don't need the heavy finalization machinery.
840 elsif Is_RTE
(Pool_Id
, RE_Global_Pool_Object
)
841 and then not Needs_Finalization
(Desig_Typ
)
845 -- Do not replicate the machinery if the allocator / free has already
846 -- been expanded and has a custom Allocate / Deallocate.
848 elsif Present
(Proc_To_Call
)
849 and then Is_Allocate_Deallocate_Proc
(Proc_To_Call
)
854 -- Finalization actions are required when the object to be allocated or
855 -- deallocated needs these actions and the associated access type is not
856 -- subject to pragma No_Heap_Finalization.
859 Needs_Finalization
(Desig_Typ
)
860 and then not No_Heap_Finalization
(Ptr_Typ
);
864 -- Do nothing if the access type may never allocate / deallocate
867 if No_Pool_Assigned
(Ptr_Typ
) then
871 -- The allocation / deallocation of a controlled object must be
872 -- chained on / detached from a finalization master.
874 pragma Assert
(Present
(Finalization_Master
(Ptr_Typ
)));
876 -- The only other kind of allocation / deallocation supported by this
877 -- routine is on / from a subpool.
879 elsif Nkind
(Expr
) = N_Allocator
880 and then No
(Subpool_Handle_Name
(Expr
))
886 Loc
: constant Source_Ptr
:= Sloc
(N
);
887 Addr_Id
: constant Entity_Id
:= Make_Temporary
(Loc
, 'A');
888 Alig_Id
: constant Entity_Id
:= Make_Temporary
(Loc
, 'L');
889 Proc_Id
: constant Entity_Id
:= Make_Temporary
(Loc
, 'P');
890 Size_Id
: constant Entity_Id
:= Make_Temporary
(Loc
, 'S');
893 Fin_Addr_Id
: Entity_Id
;
894 Fin_Mas_Act
: Node_Id
;
895 Fin_Mas_Id
: Entity_Id
;
896 Proc_To_Call
: Entity_Id
;
897 Subpool
: Node_Id
:= Empty
;
900 -- Step 1: Construct all the actuals for the call to library routine
901 -- Allocate_Any_Controlled / Deallocate_Any_Controlled.
905 Actuals
:= New_List
(New_Occurrence_Of
(Pool_Id
, Loc
));
911 if Nkind
(Expr
) = N_Allocator
then
912 Subpool
:= Subpool_Handle_Name
(Expr
);
915 -- If a subpool is present it can be an arbitrary name, so make
916 -- the actual by copying the tree.
918 if Present
(Subpool
) then
919 Append_To
(Actuals
, New_Copy_Tree
(Subpool
, New_Sloc
=> Loc
));
921 Append_To
(Actuals
, Make_Null
(Loc
));
924 -- c) Finalization master
927 Fin_Mas_Id
:= Finalization_Master
(Ptr_Typ
);
928 Fin_Mas_Act
:= New_Occurrence_Of
(Fin_Mas_Id
, Loc
);
930 -- Handle the case where the master is actually a pointer to a
931 -- master. This case arises in build-in-place functions.
933 if Is_Access_Type
(Etype
(Fin_Mas_Id
)) then
934 Append_To
(Actuals
, Fin_Mas_Act
);
937 Make_Attribute_Reference
(Loc
,
938 Prefix
=> Fin_Mas_Act
,
939 Attribute_Name
=> Name_Unrestricted_Access
));
942 Append_To
(Actuals
, Make_Null
(Loc
));
945 -- d) Finalize_Address
947 -- Primitive Finalize_Address is never generated in CodePeer mode
948 -- since it contains an Unchecked_Conversion.
950 if Needs_Fin
and then not CodePeer_Mode
then
951 Fin_Addr_Id
:= Finalize_Address
(Desig_Typ
);
952 pragma Assert
(Present
(Fin_Addr_Id
));
955 Make_Attribute_Reference
(Loc
,
956 Prefix
=> New_Occurrence_Of
(Fin_Addr_Id
, Loc
),
957 Attribute_Name
=> Name_Unrestricted_Access
));
959 Append_To
(Actuals
, Make_Null
(Loc
));
967 Append_To
(Actuals
, New_Occurrence_Of
(Addr_Id
, Loc
));
968 Append_To
(Actuals
, New_Occurrence_Of
(Size_Id
, Loc
));
970 if (Is_Allocate
or else not Is_Class_Wide_Type
(Desig_Typ
))
971 and then not Use_Secondary_Stack_Pool
973 Append_To
(Actuals
, New_Occurrence_Of
(Alig_Id
, Loc
));
975 -- For deallocation of class-wide types we obtain the value of
976 -- alignment from the Type Specific Record of the deallocated object.
977 -- This is needed because the frontend expansion of class-wide types
978 -- into equivalent types confuses the back end.
984 -- ... because 'Alignment applied to class-wide types is expanded
985 -- into the code that reads the value of alignment from the TSD
986 -- (see Expand_N_Attribute_Reference)
988 -- In the Use_Secondary_Stack_Pool case, Alig_Id is not
989 -- passed in and therefore must not be referenced.
992 Unchecked_Convert_To
(RTE
(RE_Storage_Offset
),
993 Make_Attribute_Reference
(Loc
,
995 Make_Explicit_Dereference
(Loc
, Relocate_Node
(Expr
)),
996 Attribute_Name
=> Name_Alignment
)));
1002 Is_Controlled
: declare
1003 Flag_Id
: constant Entity_Id
:= Make_Temporary
(Loc
, 'F');
1004 Flag_Expr
: Node_Id
;
1011 Temp
:= Find_Object
(Expression
(Expr
));
1016 -- Processing for allocations where the expression is a subtype
1020 and then Is_Entity_Name
(Temp
)
1021 and then Is_Type
(Entity
(Temp
))
1026 (Needs_Finalization
(Entity
(Temp
))), Loc
);
1028 -- The allocation / deallocation of a class-wide object relies
1029 -- on a runtime check to determine whether the object is truly
1030 -- controlled or not. Depending on this check, the finalization
1031 -- machinery will request or reclaim extra storage reserved for
1034 elsif Is_Class_Wide_Type
(Desig_Typ
) then
1036 -- Detect a special case where interface class-wide types
1037 -- are involved as the object appears as:
1039 -- Tag_Ptr (Base_Address (<object>'Address))
1041 -- The expression already yields the proper tag, generate:
1045 if Is_RTE
(Etype
(Temp
), RE_Tag_Ptr
) then
1047 Make_Explicit_Dereference
(Loc
,
1048 Prefix
=> Relocate_Node
(Temp
));
1050 -- In the default case, obtain the tag of the object about
1051 -- to be allocated / deallocated. Generate:
1055 -- If the object is an unchecked conversion (typically to
1056 -- an access to class-wide type), we must preserve the
1057 -- conversion to ensure that the object is seen as tagged
1058 -- in the code that follows.
1063 if Nkind
(Parent
(Pref
)) = N_Unchecked_Type_Conversion
1065 Pref
:= Parent
(Pref
);
1069 Make_Attribute_Reference
(Loc
,
1070 Prefix
=> Relocate_Node
(Pref
),
1071 Attribute_Name
=> Name_Tag
);
1075 -- Needs_Finalization (<Param>)
1078 Make_Function_Call
(Loc
,
1080 New_Occurrence_Of
(RTE
(RE_Needs_Finalization
), Loc
),
1081 Parameter_Associations
=> New_List
(Param
));
1083 -- Processing for generic actuals
1085 elsif Is_Generic_Actual_Type
(Desig_Typ
) then
1087 New_Occurrence_Of
(Boolean_Literals
1088 (Needs_Finalization
(Base_Type
(Desig_Typ
))), Loc
);
1090 -- The object does not require any specialized checks, it is
1091 -- known to be controlled.
1094 Flag_Expr
:= New_Occurrence_Of
(Standard_True
, Loc
);
1097 -- Create the temporary which represents the finalization state
1098 -- of the expression. Generate:
1100 -- F : constant Boolean := <Flag_Expr>;
1103 Make_Object_Declaration
(Loc
,
1104 Defining_Identifier
=> Flag_Id
,
1105 Constant_Present
=> True,
1106 Object_Definition
=>
1107 New_Occurrence_Of
(Standard_Boolean
, Loc
),
1108 Expression
=> Flag_Expr
));
1110 Append_To
(Actuals
, New_Occurrence_Of
(Flag_Id
, Loc
));
1113 -- The object is not controlled
1116 Append_To
(Actuals
, New_Occurrence_Of
(Standard_False
, Loc
));
1123 New_Occurrence_Of
(Boolean_Literals
(Present
(Subpool
)), Loc
));
1126 -- Step 2: Build a wrapper Allocate / Deallocate which internally
1127 -- calls Allocate_Any_Controlled / Deallocate_Any_Controlled.
1129 -- Select the proper routine to call
1132 Proc_To_Call
:= RTE
(RE_Allocate_Any_Controlled
);
1134 Proc_To_Call
:= RTE
(RE_Deallocate_Any_Controlled
);
1137 -- Create a custom Allocate / Deallocate routine which has identical
1138 -- profile to that of System.Storage_Pools.
1141 -- P : Root_Storage_Pool
1142 function Pool_Param
return Node_Id
is (
1143 Make_Parameter_Specification
(Loc
,
1144 Defining_Identifier
=> Make_Temporary
(Loc
, 'P'),
1146 New_Occurrence_Of
(RTE
(RE_Root_Storage_Pool
), Loc
)));
1148 -- A : [out] Address
1149 function Address_Param
return Node_Id
is (
1150 Make_Parameter_Specification
(Loc
,
1151 Defining_Identifier
=> Addr_Id
,
1152 Out_Present
=> Is_Allocate
,
1154 New_Occurrence_Of
(RTE
(RE_Address
), Loc
)));
1156 -- S : Storage_Count
1157 function Size_Param
return Node_Id
is (
1158 Make_Parameter_Specification
(Loc
,
1159 Defining_Identifier
=> Size_Id
,
1161 New_Occurrence_Of
(RTE
(RE_Storage_Count
), Loc
)));
1163 -- L : Storage_Count
1164 function Alignment_Param
return Node_Id
is (
1165 Make_Parameter_Specification
(Loc
,
1166 Defining_Identifier
=> Alig_Id
,
1168 New_Occurrence_Of
(RTE
(RE_Storage_Count
), Loc
)));
1170 Formal_Params
: List_Id
;
1172 if Use_Secondary_Stack_Pool
then
1173 -- Gigi expects a different profile in the Secondary_Stack_Pool
1174 -- case. There must be no uses of the two missing formals
1175 -- (i.e., Pool_Param and Alignment_Param) in this case.
1176 Formal_Params
:= New_List
(Address_Param
, Size_Param
);
1178 Formal_Params
:= New_List
(
1179 Pool_Param
, Address_Param
, Size_Param
, Alignment_Param
);
1183 Make_Subprogram_Body
(Loc
,
1186 Make_Procedure_Specification
(Loc
,
1187 Defining_Unit_Name
=> Proc_Id
,
1188 Parameter_Specifications
=> Formal_Params
),
1190 Declarations
=> No_List
,
1192 Handled_Statement_Sequence
=>
1193 Make_Handled_Sequence_Of_Statements
(Loc
,
1194 Statements
=> New_List
(
1195 Make_Procedure_Call_Statement
(Loc
,
1197 New_Occurrence_Of
(Proc_To_Call
, Loc
),
1198 Parameter_Associations
=> Actuals
)))),
1199 Suppress
=> All_Checks
);
1202 -- The newly generated Allocate / Deallocate becomes the default
1203 -- procedure to call when the back end processes the allocation /
1207 Set_Procedure_To_Call
(Expr
, Proc_Id
);
1209 Set_Procedure_To_Call
(N
, Proc_Id
);
1212 end Build_Allocate_Deallocate_Proc
;
1214 -------------------------------
1215 -- Build_Abort_Undefer_Block --
1216 -------------------------------
1218 function Build_Abort_Undefer_Block
1221 Context
: Node_Id
) return Node_Id
1223 Exceptions_OK
: constant Boolean :=
1224 not Restriction_Active
(No_Exception_Propagation
);
1232 -- The block should be generated only when undeferring abort in the
1233 -- context of a potential exception.
1235 pragma Assert
(Abort_Allowed
and Exceptions_OK
);
1241 -- Abort_Undefer_Direct;
1244 AUD
:= RTE
(RE_Abort_Undefer_Direct
);
1247 Make_Handled_Sequence_Of_Statements
(Loc
,
1248 Statements
=> Stmts
,
1249 At_End_Proc
=> New_Occurrence_Of
(AUD
, Loc
));
1252 Make_Block_Statement
(Loc
,
1253 Handled_Statement_Sequence
=> HSS
);
1254 Set_Is_Abort_Block
(Blk
);
1256 Add_Block_Identifier
(Blk
, Blk_Id
);
1257 Expand_At_End_Handler
(HSS
, Blk_Id
);
1259 -- Present the Abort_Undefer_Direct function to the back end to inline
1260 -- the call to the routine.
1262 Add_Inlined_Body
(AUD
, Context
);
1265 end Build_Abort_Undefer_Block
;
1267 ---------------------------------
1268 -- Build_Class_Wide_Expression --
1269 ---------------------------------
1271 procedure Build_Class_Wide_Expression
1272 (Pragma_Or_Expr
: Node_Id
;
1274 Par_Subp
: Entity_Id
;
1275 Adjust_Sloc
: Boolean)
1277 function Replace_Entity
(N
: Node_Id
) return Traverse_Result
;
1278 -- Replace reference to formal of inherited operation or to primitive
1279 -- operation of root type, with corresponding entity for derived type,
1280 -- when constructing the class-wide condition of an overriding
1283 --------------------
1284 -- Replace_Entity --
1285 --------------------
1287 function Replace_Entity
(N
: Node_Id
) return Traverse_Result
is
1292 Adjust_Inherited_Pragma_Sloc
(N
);
1295 if Nkind
(N
) in N_Identifier | N_Expanded_Name | N_Operator_Symbol
1296 and then Present
(Entity
(N
))
1298 (Is_Formal
(Entity
(N
)) or else Is_Subprogram
(Entity
(N
)))
1300 (Nkind
(Parent
(N
)) /= N_Attribute_Reference
1301 or else Attribute_Name
(Parent
(N
)) /= Name_Class
)
1303 -- The replacement does not apply to dispatching calls within the
1304 -- condition, but only to calls whose static tag is that of the
1307 if Is_Subprogram
(Entity
(N
))
1308 and then Nkind
(Parent
(N
)) = N_Function_Call
1309 and then Present
(Controlling_Argument
(Parent
(N
)))
1314 -- Determine whether entity has a renaming
1316 New_E
:= Type_Map
.Get
(Entity
(N
));
1318 if Present
(New_E
) then
1319 Rewrite
(N
, New_Occurrence_Of
(New_E
, Sloc
(N
)));
1322 -- Update type of function call node, which should be the same as
1323 -- the function's return type.
1325 if Is_Subprogram
(Entity
(N
))
1326 and then Nkind
(Parent
(N
)) = N_Function_Call
1328 Set_Etype
(Parent
(N
), Etype
(Entity
(N
)));
1331 -- The whole expression will be reanalyzed
1333 elsif Nkind
(N
) in N_Has_Etype
then
1334 Set_Analyzed
(N
, False);
1340 procedure Replace_Condition_Entities
is
1341 new Traverse_Proc
(Replace_Entity
);
1345 Par_Typ
: constant Entity_Id
:= Find_Dispatching_Type
(Par_Subp
);
1346 Subp_Typ
: constant Entity_Id
:= Find_Dispatching_Type
(Subp
);
1348 -- Start of processing for Build_Class_Wide_Expression
1351 pragma Assert
(Par_Typ
/= Subp_Typ
);
1353 Update_Primitives_Mapping
(Par_Subp
, Subp
);
1354 Map_Formals
(Par_Subp
, Subp
);
1355 Replace_Condition_Entities
(Pragma_Or_Expr
);
1356 end Build_Class_Wide_Expression
;
1358 --------------------
1359 -- Build_DIC_Call --
1360 --------------------
1362 function Build_DIC_Call
1365 Typ
: Entity_Id
) return Node_Id
1367 Proc_Id
: constant Entity_Id
:= DIC_Procedure
(Typ
);
1368 Formal_Typ
: constant Entity_Id
:= Etype
(First_Formal
(Proc_Id
));
1371 -- The DIC procedure has a null body if assertions are disabled or
1372 -- Assertion_Policy Ignore is in effect. In that case, it would be
1373 -- nice to generate a null statement instead of a call to the DIC
1374 -- procedure, but doing that seems to interfere with the determination
1375 -- of ECRs (early call regions) in SPARK. ???
1378 Make_Procedure_Call_Statement
(Loc
,
1379 Name
=> New_Occurrence_Of
(Proc_Id
, Loc
),
1380 Parameter_Associations
=> New_List
(
1381 Unchecked_Convert_To
(Formal_Typ
, Obj_Name
)));
1384 ------------------------------
1385 -- Build_DIC_Procedure_Body --
1386 ------------------------------
1388 -- WARNING: This routine manages Ghost regions. Return statements must be
1389 -- replaced by gotos which jump to the end of the routine and restore the
1392 procedure Build_DIC_Procedure_Body
1394 Partial_DIC
: Boolean := False)
1396 Pragmas_Seen
: Elist_Id
:= No_Elist
;
1397 -- This list contains all DIC pragmas processed so far. The list is used
1398 -- to avoid redundant Default_Initial_Condition checks.
1400 procedure Add_DIC_Check
1401 (DIC_Prag
: Node_Id
;
1403 Stmts
: in out List_Id
);
1404 -- Subsidiary to all Add_xxx_DIC routines. Add a runtime check to verify
1405 -- assertion expression DIC_Expr of pragma DIC_Prag. All generated code
1406 -- is added to list Stmts.
1408 procedure Add_Inherited_DIC
1409 (DIC_Prag
: Node_Id
;
1410 Par_Typ
: Entity_Id
;
1411 Deriv_Typ
: Entity_Id
;
1412 Stmts
: in out List_Id
);
1413 -- Add a runtime check to verify the assertion expression of inherited
1414 -- pragma DIC_Prag. Par_Typ is parent type, which is also the owner of
1415 -- the DIC pragma. Deriv_Typ is the derived type inheriting the DIC
1416 -- pragma. All generated code is added to list Stmts.
1418 procedure Add_Inherited_Tagged_DIC
1419 (DIC_Prag
: Node_Id
;
1421 Stmts
: in out List_Id
);
1422 -- Add a runtime check to verify assertion expression DIC_Expr of
1423 -- inherited pragma DIC_Prag. This routine applies class-wide pre-
1424 -- and postcondition-like runtime semantics to the check. Expr is
1425 -- the assertion expression after substitition has been performed
1426 -- (via Replace_References). All generated code is added to list Stmts.
1428 procedure Add_Inherited_DICs
1430 Priv_Typ
: Entity_Id
;
1431 Full_Typ
: Entity_Id
;
1433 Checks
: in out List_Id
);
1434 -- Generate a DIC check for each inherited Default_Initial_Condition
1435 -- coming from all parent types of type T. Priv_Typ and Full_Typ denote
1436 -- the partial and full view of the parent type. Obj_Id denotes the
1437 -- entity of the _object formal parameter of the DIC procedure. All
1438 -- created checks are added to list Checks.
1440 procedure Add_Own_DIC
1441 (DIC_Prag
: Node_Id
;
1442 DIC_Typ
: Entity_Id
;
1444 Stmts
: in out List_Id
);
1445 -- Add a runtime check to verify the assertion expression of pragma
1446 -- DIC_Prag. DIC_Typ is the owner of the DIC pragma. Obj_Id is the
1447 -- object to substitute in the assertion expression for any references
1448 -- to the current instance of the type All generated code is added to
1451 procedure Add_Parent_DICs
1454 Checks
: in out List_Id
);
1455 -- Generate a Default_Initial_Condition check for each inherited DIC
1456 -- aspect coming from all parent types of type T. Obj_Id denotes the
1457 -- entity of the _object formal parameter of the DIC procedure. All
1458 -- created checks are added to list Checks.
1464 procedure Add_DIC_Check
1465 (DIC_Prag
: Node_Id
;
1467 Stmts
: in out List_Id
)
1469 Loc
: constant Source_Ptr
:= Sloc
(DIC_Prag
);
1470 Nam
: constant Name_Id
:= Original_Aspect_Pragma_Name
(DIC_Prag
);
1473 -- The DIC pragma is ignored, nothing left to do
1475 if Is_Ignored
(DIC_Prag
) then
1478 -- Otherwise the DIC expression must be checked at run time.
1481 -- pragma Check (<Nam>, <DIC_Expr>);
1484 Append_New_To
(Stmts
,
1486 Pragma_Identifier
=>
1487 Make_Identifier
(Loc
, Name_Check
),
1489 Pragma_Argument_Associations
=> New_List
(
1490 Make_Pragma_Argument_Association
(Loc
,
1491 Expression
=> Make_Identifier
(Loc
, Nam
)),
1493 Make_Pragma_Argument_Association
(Loc
,
1494 Expression
=> DIC_Expr
))));
1497 -- Add the pragma to the list of processed pragmas
1499 Append_New_Elmt
(DIC_Prag
, Pragmas_Seen
);
1502 -----------------------
1503 -- Add_Inherited_DIC --
1504 -----------------------
1506 procedure Add_Inherited_DIC
1507 (DIC_Prag
: Node_Id
;
1508 Par_Typ
: Entity_Id
;
1509 Deriv_Typ
: Entity_Id
;
1510 Stmts
: in out List_Id
)
1512 Deriv_Proc
: constant Entity_Id
:= DIC_Procedure
(Deriv_Typ
);
1513 Deriv_Obj
: constant Entity_Id
:= First_Entity
(Deriv_Proc
);
1514 Par_Proc
: constant Entity_Id
:= DIC_Procedure
(Par_Typ
);
1515 Par_Obj
: constant Entity_Id
:= First_Entity
(Par_Proc
);
1516 Loc
: constant Source_Ptr
:= Sloc
(DIC_Prag
);
1519 pragma Assert
(Present
(Deriv_Proc
) and then Present
(Par_Proc
));
1521 -- Verify the inherited DIC assertion expression by calling the DIC
1522 -- procedure of the parent type.
1525 -- <Par_Typ>DIC (Par_Typ (_object));
1527 Append_New_To
(Stmts
,
1528 Make_Procedure_Call_Statement
(Loc
,
1529 Name
=> New_Occurrence_Of
(Par_Proc
, Loc
),
1530 Parameter_Associations
=> New_List
(
1532 (Typ
=> Etype
(Par_Obj
),
1533 Expr
=> New_Occurrence_Of
(Deriv_Obj
, Loc
)))));
1534 end Add_Inherited_DIC
;
1536 ------------------------------
1537 -- Add_Inherited_Tagged_DIC --
1538 ------------------------------
1540 procedure Add_Inherited_Tagged_DIC
1541 (DIC_Prag
: Node_Id
;
1543 Stmts
: in out List_Id
)
1546 -- Once the DIC assertion expression is fully processed, add a check
1547 -- to the statements of the DIC procedure.
1550 (DIC_Prag
=> DIC_Prag
,
1553 end Add_Inherited_Tagged_DIC
;
1555 ------------------------
1556 -- Add_Inherited_DICs --
1557 ------------------------
1559 procedure Add_Inherited_DICs
1561 Priv_Typ
: Entity_Id
;
1562 Full_Typ
: Entity_Id
;
1564 Checks
: in out List_Id
)
1566 Deriv_Typ
: Entity_Id
;
1569 Prag_Expr
: Node_Id
;
1570 Prag_Expr_Arg
: Node_Id
;
1572 Prag_Typ_Arg
: Node_Id
;
1574 Par_Proc
: Entity_Id
;
1575 -- The "partial" invariant procedure of Par_Typ
1577 Par_Typ
: Entity_Id
;
1578 -- The suitable view of the parent type used in the substitution of
1582 if not Present
(Priv_Typ
) and then not Present
(Full_Typ
) then
1586 -- When the type inheriting the class-wide invariant is a concurrent
1587 -- type, use the corresponding record type because it contains all
1588 -- primitive operations of the concurrent type and allows for proper
1591 if Is_Concurrent_Type
(T
) then
1592 Deriv_Typ
:= Corresponding_Record_Type
(T
);
1597 pragma Assert
(Present
(Deriv_Typ
));
1599 -- Determine which rep item chain to use. Precedence is given to that
1600 -- of the parent type's partial view since it usually carries all the
1601 -- class-wide invariants.
1603 if Present
(Priv_Typ
) then
1604 Prag
:= First_Rep_Item
(Priv_Typ
);
1606 Prag
:= First_Rep_Item
(Full_Typ
);
1609 while Present
(Prag
) loop
1610 if Nkind
(Prag
) = N_Pragma
1611 and then Pragma_Name
(Prag
) = Name_Default_Initial_Condition
1613 -- Nothing to do if the pragma was already processed
1615 if Contains
(Pragmas_Seen
, Prag
) then
1619 -- Extract arguments of the Default_Initial_Condition pragma
1621 Prag_Expr_Arg
:= First
(Pragma_Argument_Associations
(Prag
));
1622 Prag_Expr
:= Expression_Copy
(Prag_Expr_Arg
);
1624 -- Pick up the implicit second argument of the pragma, which
1625 -- indicates the type that the pragma applies to.
1627 Prag_Typ_Arg
:= Next
(Prag_Expr_Arg
);
1628 if Present
(Prag_Typ_Arg
) then
1629 Prag_Typ
:= Get_Pragma_Arg
(Prag_Typ_Arg
);
1634 -- The pragma applies to the partial view of the parent type
1636 if Present
(Priv_Typ
)
1637 and then Present
(Prag_Typ
)
1638 and then Entity
(Prag_Typ
) = Priv_Typ
1640 Par_Typ
:= Priv_Typ
;
1642 -- The pragma applies to the full view of the parent type
1644 elsif Present
(Full_Typ
)
1645 and then Present
(Prag_Typ
)
1646 and then Entity
(Prag_Typ
) = Full_Typ
1648 Par_Typ
:= Full_Typ
;
1650 -- Otherwise the pragma does not belong to the parent type and
1651 -- should not be considered.
1657 -- Substitute references in the DIC expression that are related
1658 -- to the partial type with corresponding references related to
1659 -- the derived type (call to Replace_References below).
1661 Expr
:= New_Copy_Tree
(Prag_Expr
);
1663 Par_Proc
:= Partial_DIC_Procedure
(Par_Typ
);
1665 -- If there's not a partial DIC procedure (such as when a
1666 -- full type doesn't have its own DIC, but is inherited from
1667 -- a type with DIC), get the full DIC procedure.
1669 if not Present
(Par_Proc
) then
1670 Par_Proc
:= DIC_Procedure
(Par_Typ
);
1676 Deriv_Typ
=> Deriv_Typ
,
1677 Par_Obj
=> First_Formal
(Par_Proc
),
1678 Deriv_Obj
=> Obj_Id
);
1680 -- Why are there different actions depending on whether T is
1681 -- tagged? Can these be unified? ???
1683 if Is_Tagged_Type
(T
) then
1684 Add_Inherited_Tagged_DIC
1693 Deriv_Typ
=> Deriv_Typ
,
1697 -- Leave as soon as we get a DIC pragma, since we'll visit
1698 -- the pragmas of the parents, so will get to any "inherited"
1699 -- pragmas that way.
1704 Next_Rep_Item
(Prag
);
1706 end Add_Inherited_DICs
;
1712 procedure Add_Own_DIC
1713 (DIC_Prag
: Node_Id
;
1714 DIC_Typ
: Entity_Id
;
1716 Stmts
: in out List_Id
)
1718 DIC_Args
: constant List_Id
:=
1719 Pragma_Argument_Associations
(DIC_Prag
);
1720 DIC_Arg
: constant Node_Id
:= First
(DIC_Args
);
1721 DIC_Asp
: constant Node_Id
:= Corresponding_Aspect
(DIC_Prag
);
1722 DIC_Expr
: constant Node_Id
:= Get_Pragma_Arg
(DIC_Arg
);
1726 Typ_Decl
: constant Node_Id
:= Declaration_Node
(DIC_Typ
);
1730 -- Start of processing for Add_Own_DIC
1733 pragma Assert
(Present
(DIC_Expr
));
1734 Expr
:= New_Copy_Tree
(DIC_Expr
);
1736 -- Perform the following substitution:
1738 -- * Replace the current instance of DIC_Typ with a reference to
1739 -- the _object formal parameter of the DIC procedure.
1741 Replace_Type_References
1746 -- Preanalyze the DIC expression to detect errors and at the same
1747 -- time capture the visibility of the proper package part.
1749 Set_Parent
(Expr
, Typ_Decl
);
1750 Preanalyze_Assert_Expression
(Expr
, Any_Boolean
);
1752 -- Save a copy of the expression with all replacements and analysis
1753 -- already taken place in case a derived type inherits the pragma.
1754 -- The copy will be used as the foundation of the derived type's own
1755 -- version of the DIC assertion expression.
1757 if Is_Tagged_Type
(DIC_Typ
) then
1758 Set_Expression_Copy
(DIC_Arg
, New_Copy_Tree
(Expr
));
1761 -- If the pragma comes from an aspect specification, replace the
1762 -- saved expression because all type references must be substituted
1763 -- for the call to Preanalyze_Spec_Expression in Check_Aspect_At_xxx
1766 if Present
(DIC_Asp
) then
1767 Set_Entity
(Identifier
(DIC_Asp
), New_Copy_Tree
(Expr
));
1770 -- Once the DIC assertion expression is fully processed, add a check
1771 -- to the statements of the DIC procedure (unless the type is an
1772 -- abstract type, in which case we don't want the possibility of
1773 -- generating a call to an abstract function of the type; such DIC
1774 -- procedures can never be called in any case, so not generating the
1775 -- check at all is OK).
1777 if not Is_Abstract_Type
(DIC_Typ
) or else GNATprove_Mode
then
1779 (DIC_Prag
=> DIC_Prag
,
1785 ---------------------
1786 -- Add_Parent_DICs --
1787 ---------------------
1789 procedure Add_Parent_DICs
1792 Checks
: in out List_Id
)
1794 Dummy_1
: Entity_Id
;
1795 Dummy_2
: Entity_Id
;
1797 Curr_Typ
: Entity_Id
;
1798 -- The entity of the current type being examined
1800 Full_Typ
: Entity_Id
;
1801 -- The full view of Par_Typ
1803 Par_Typ
: Entity_Id
;
1804 -- The entity of the parent type
1806 Priv_Typ
: Entity_Id
;
1807 -- The partial view of Par_Typ
1810 Par_Prim
: Entity_Id
;
1814 -- Map the overridden primitive to the overriding one; required by
1815 -- Replace_References (called by Add_Inherited_DICs) to handle calls
1816 -- to parent primitives.
1818 Op_Node
:= First_Elmt
(Primitive_Operations
(T
));
1819 while Present
(Op_Node
) loop
1820 Prim
:= Node
(Op_Node
);
1822 if Present
(Overridden_Operation
(Prim
))
1823 and then Comes_From_Source
(Prim
)
1825 Par_Prim
:= Overridden_Operation
(Prim
);
1827 -- Create a mapping of the form:
1828 -- parent type primitive -> derived type primitive
1830 Type_Map
.Set
(Par_Prim
, Prim
);
1833 Next_Elmt
(Op_Node
);
1836 -- Climb the parent type chain
1840 -- Do not consider subtypes, as they inherit the DICs from their
1843 Par_Typ
:= Base_Type
(Etype
(Base_Type
(Curr_Typ
)));
1845 -- Stop the climb once the root of the parent chain is
1848 exit when Curr_Typ
= Par_Typ
;
1850 -- Process the DICs of the parent type
1852 Get_Views
(Par_Typ
, Priv_Typ
, Full_Typ
, Dummy_1
, Dummy_2
);
1854 -- Only try to inherit a DIC pragma from the parent type Par_Typ
1855 -- if it Has_Own_DIC pragma. The loop will proceed up the parent
1856 -- chain to find all types that have their own DIC.
1858 if Has_Own_DIC
(Par_Typ
) then
1861 Priv_Typ
=> Priv_Typ
,
1862 Full_Typ
=> Full_Typ
,
1867 Curr_Typ
:= Par_Typ
;
1869 end Add_Parent_DICs
;
1873 Loc
: constant Source_Ptr
:= Sloc
(Typ
);
1875 Saved_GM
: constant Ghost_Mode_Type
:= Ghost_Mode
;
1876 Saved_IGR
: constant Node_Id
:= Ignored_Ghost_Region
;
1877 -- Save the Ghost-related attributes to restore on exit
1880 DIC_Typ
: Entity_Id
;
1881 Dummy_1
: Entity_Id
;
1882 Dummy_2
: Entity_Id
;
1883 Proc_Body
: Node_Id
;
1884 Proc_Body_Id
: Entity_Id
;
1885 Proc_Decl
: Node_Id
;
1886 Proc_Id
: Entity_Id
;
1887 Stmts
: List_Id
:= No_List
;
1889 CRec_Typ
: Entity_Id
:= Empty
;
1890 -- The corresponding record type of Full_Typ
1892 Full_Typ
: Entity_Id
:= Empty
;
1893 -- The full view of the working type
1895 Obj_Id
: Entity_Id
:= Empty
;
1896 -- The _object formal parameter of the invariant procedure
1898 Part_Proc
: Entity_Id
:= Empty
;
1899 -- The entity of the "partial" invariant procedure
1901 Priv_Typ
: Entity_Id
:= Empty
;
1902 -- The partial view of the working type
1904 Work_Typ
: Entity_Id
;
1907 -- Start of processing for Build_DIC_Procedure_Body
1910 Work_Typ
:= Base_Type
(Typ
);
1912 -- Do not process class-wide types as these are Itypes, but lack a first
1913 -- subtype (see below).
1915 if Is_Class_Wide_Type
(Work_Typ
) then
1918 -- Do not process the underlying full view of a private type. There is
1919 -- no way to get back to the partial view, plus the body will be built
1920 -- by the full view or the base type.
1922 elsif Is_Underlying_Full_View
(Work_Typ
) then
1925 -- Use the first subtype when dealing with various base types
1927 elsif Is_Itype
(Work_Typ
) then
1928 Work_Typ
:= First_Subtype
(Work_Typ
);
1930 -- The input denotes the corresponding record type of a protected or a
1931 -- task type. Work with the concurrent type because the corresponding
1932 -- record type may not be visible to clients of the type.
1934 elsif Ekind
(Work_Typ
) = E_Record_Type
1935 and then Is_Concurrent_Record_Type
(Work_Typ
)
1937 Work_Typ
:= Corresponding_Concurrent_Type
(Work_Typ
);
1940 -- The working type may be subject to pragma Ghost. Set the mode now to
1941 -- ensure that the DIC procedure is properly marked as Ghost.
1943 Set_Ghost_Mode
(Work_Typ
);
1945 -- The working type must be either define a DIC pragma of its own or
1946 -- inherit one from a parent type.
1948 pragma Assert
(Has_DIC
(Work_Typ
));
1950 -- Recover the type which defines the DIC pragma. This is either the
1951 -- working type itself or a parent type when the pragma is inherited.
1953 DIC_Typ
:= Find_DIC_Type
(Work_Typ
);
1954 pragma Assert
(Present
(DIC_Typ
));
1956 DIC_Prag
:= Get_Pragma
(DIC_Typ
, Pragma_Default_Initial_Condition
);
1957 pragma Assert
(Present
(DIC_Prag
));
1959 -- Nothing to do if pragma DIC appears without an argument or its sole
1960 -- argument is "null".
1962 if not Is_Verifiable_DIC_Pragma
(DIC_Prag
) then
1966 -- Obtain both views of the type
1968 Get_Views
(Work_Typ
, Priv_Typ
, Full_Typ
, Dummy_1
, CRec_Typ
);
1970 -- The caller requests a body for the partial DIC procedure
1973 Proc_Id
:= Partial_DIC_Procedure
(Work_Typ
);
1975 -- The "full" DIC procedure body was already created
1977 -- Create a declaration for the "partial" DIC procedure if it
1978 -- is not available.
1980 if No
(Proc_Id
) then
1981 Build_DIC_Procedure_Declaration
1983 Partial_DIC
=> True);
1985 Proc_Id
:= Partial_DIC_Procedure
(Work_Typ
);
1988 -- The caller requests a body for the "full" DIC procedure
1991 Proc_Id
:= DIC_Procedure
(Work_Typ
);
1992 Part_Proc
:= Partial_DIC_Procedure
(Work_Typ
);
1994 -- Create a declaration for the "full" DIC procedure if it is
1997 if No
(Proc_Id
) then
1998 Build_DIC_Procedure_Declaration
(Work_Typ
);
1999 Proc_Id
:= DIC_Procedure
(Work_Typ
);
2003 -- At this point there should be a DIC procedure declaration
2005 pragma Assert
(Present
(Proc_Id
));
2006 Proc_Decl
:= Unit_Declaration_Node
(Proc_Id
);
2008 -- Nothing to do if the DIC procedure already has a body
2010 if Present
(Corresponding_Body
(Proc_Decl
)) then
2014 -- Emulate the environment of the DIC procedure by installing its scope
2015 -- and formal parameters.
2017 Push_Scope
(Proc_Id
);
2018 Install_Formals
(Proc_Id
);
2020 Obj_Id
:= First_Formal
(Proc_Id
);
2021 pragma Assert
(Present
(Obj_Id
));
2023 -- The "partial" DIC procedure verifies the DICs of the partial view
2027 pragma Assert
(Present
(Priv_Typ
));
2029 if Has_Own_DIC
(Work_Typ
) then -- If we're testing this then maybe
2030 Add_Own_DIC
-- we shouldn't be calling Find_DIC_Typ above???
2031 (DIC_Prag
=> DIC_Prag
,
2032 DIC_Typ
=> DIC_Typ
, -- Should this just be Work_Typ???
2037 -- Otherwise, the "full" DIC procedure verifies the DICs inherited from
2038 -- parent types, as well as indirectly verifying the DICs of the partial
2039 -- view by calling the "partial" DIC procedure.
2042 -- Check the DIC of the partial view by calling the "partial" DIC
2043 -- procedure, unless the partial DIC body is empty. Generate:
2045 -- <Work_Typ>Partial_DIC (_object);
2047 if Present
(Part_Proc
) and then not Has_Null_Body
(Part_Proc
) then
2048 Append_New_To
(Stmts
,
2049 Make_Procedure_Call_Statement
(Loc
,
2050 Name
=> New_Occurrence_Of
(Part_Proc
, Loc
),
2051 Parameter_Associations
=> New_List
(
2052 New_Occurrence_Of
(Obj_Id
, Loc
))));
2055 -- Process inherited Default_Initial_Conditions for all parent types
2057 Add_Parent_DICs
(Work_Typ
, Obj_Id
, Stmts
);
2062 -- Produce an empty completing body in the following cases:
2063 -- * Assertions are disabled
2064 -- * The DIC Assertion_Policy is Ignore
2067 Stmts
:= New_List
(Make_Null_Statement
(Loc
));
2071 -- procedure <Work_Typ>DIC (_object : <Work_Typ>) is
2074 -- end <Work_Typ>DIC;
2077 Make_Subprogram_Body
(Loc
,
2079 Copy_Subprogram_Spec
(Parent
(Proc_Id
)),
2080 Declarations
=> Empty_List
,
2081 Handled_Statement_Sequence
=>
2082 Make_Handled_Sequence_Of_Statements
(Loc
,
2083 Statements
=> Stmts
));
2084 Proc_Body_Id
:= Defining_Entity
(Proc_Body
);
2086 -- Perform minor decoration in case the body is not analyzed
2088 Mutate_Ekind
(Proc_Body_Id
, E_Subprogram_Body
);
2089 Set_Etype
(Proc_Body_Id
, Standard_Void_Type
);
2090 Set_Scope
(Proc_Body_Id
, Current_Scope
);
2091 Set_SPARK_Pragma
(Proc_Body_Id
, SPARK_Pragma
(Proc_Id
));
2092 Set_SPARK_Pragma_Inherited
2093 (Proc_Body_Id
, SPARK_Pragma_Inherited
(Proc_Id
));
2095 -- Link both spec and body to avoid generating duplicates
2097 Set_Corresponding_Body
(Proc_Decl
, Proc_Body_Id
);
2098 Set_Corresponding_Spec
(Proc_Body
, Proc_Id
);
2100 -- The body should not be inserted into the tree when the context
2101 -- is a generic unit because it is not part of the template.
2102 -- Note that the body must still be generated in order to resolve the
2103 -- DIC assertion expression.
2105 if Inside_A_Generic
then
2108 -- Semi-insert the body into the tree for GNATprove by setting its
2109 -- Parent field. This allows for proper upstream tree traversals.
2111 elsif GNATprove_Mode
then
2112 Set_Parent
(Proc_Body
, Parent
(Declaration_Node
(Work_Typ
)));
2114 -- Otherwise the body is part of the freezing actions of the working
2118 Append_Freeze_Action
(Work_Typ
, Proc_Body
);
2122 Restore_Ghost_Region
(Saved_GM
, Saved_IGR
);
2123 end Build_DIC_Procedure_Body
;
2125 -------------------------------------
2126 -- Build_DIC_Procedure_Declaration --
2127 -------------------------------------
2129 -- WARNING: This routine manages Ghost regions. Return statements must be
2130 -- replaced by gotos which jump to the end of the routine and restore the
2133 procedure Build_DIC_Procedure_Declaration
2135 Partial_DIC
: Boolean := False)
2137 Loc
: constant Source_Ptr
:= Sloc
(Typ
);
2139 Saved_GM
: constant Ghost_Mode_Type
:= Ghost_Mode
;
2140 Saved_IGR
: constant Node_Id
:= Ignored_Ghost_Region
;
2141 -- Save the Ghost-related attributes to restore on exit
2144 DIC_Typ
: Entity_Id
;
2145 Proc_Decl
: Node_Id
;
2146 Proc_Id
: Entity_Id
;
2150 CRec_Typ
: Entity_Id
;
2151 -- The corresponding record type of Full_Typ
2153 Full_Typ
: Entity_Id
;
2154 -- The full view of working type
2157 -- The _object formal parameter of the DIC procedure
2159 Priv_Typ
: Entity_Id
;
2160 -- The partial view of working type
2162 UFull_Typ
: Entity_Id
;
2163 -- The underlying full view of Full_Typ
2165 Work_Typ
: Entity_Id
;
2169 Work_Typ
:= Base_Type
(Typ
);
2171 -- Do not process class-wide types as these are Itypes, but lack a first
2172 -- subtype (see below).
2174 if Is_Class_Wide_Type
(Work_Typ
) then
2177 -- Do not process the underlying full view of a private type. There is
2178 -- no way to get back to the partial view, plus the body will be built
2179 -- by the full view or the base type.
2181 elsif Is_Underlying_Full_View
(Work_Typ
) then
2184 -- Use the first subtype when dealing with various base types
2186 elsif Is_Itype
(Work_Typ
) then
2187 Work_Typ
:= First_Subtype
(Work_Typ
);
2189 -- The input denotes the corresponding record type of a protected or a
2190 -- task type. Work with the concurrent type because the corresponding
2191 -- record type may not be visible to clients of the type.
2193 elsif Ekind
(Work_Typ
) = E_Record_Type
2194 and then Is_Concurrent_Record_Type
(Work_Typ
)
2196 Work_Typ
:= Corresponding_Concurrent_Type
(Work_Typ
);
2199 -- The working type may be subject to pragma Ghost. Set the mode now to
2200 -- ensure that the DIC procedure is properly marked as Ghost.
2202 Set_Ghost_Mode
(Work_Typ
);
2204 -- The type must be either subject to a DIC pragma or inherit one from a
2207 pragma Assert
(Has_DIC
(Work_Typ
));
2209 -- Recover the type which defines the DIC pragma. This is either the
2210 -- working type itself or a parent type when the pragma is inherited.
2212 DIC_Typ
:= Find_DIC_Type
(Work_Typ
);
2213 pragma Assert
(Present
(DIC_Typ
));
2215 DIC_Prag
:= Get_Pragma
(DIC_Typ
, Pragma_Default_Initial_Condition
);
2216 pragma Assert
(Present
(DIC_Prag
));
2218 -- Nothing to do if pragma DIC appears without an argument or its sole
2219 -- argument is "null".
2221 if not Is_Verifiable_DIC_Pragma
(DIC_Prag
) then
2225 -- Nothing to do if the type already has a "partial" DIC procedure
2228 if Present
(Partial_DIC_Procedure
(Work_Typ
)) then
2232 -- Nothing to do if the type already has a "full" DIC procedure
2234 elsif Present
(DIC_Procedure
(Work_Typ
)) then
2238 -- The caller requests the declaration of the "partial" DIC procedure
2241 Proc_Nam
:= New_External_Name
(Chars
(Work_Typ
), "Partial_DIC");
2243 -- Otherwise the caller requests the declaration of the "full" DIC
2247 Proc_Nam
:= New_External_Name
(Chars
(Work_Typ
), "DIC");
2251 Make_Defining_Identifier
(Loc
, Chars
=> Proc_Nam
);
2253 -- Perform minor decoration in case the declaration is not analyzed
2255 Mutate_Ekind
(Proc_Id
, E_Procedure
);
2256 Set_Etype
(Proc_Id
, Standard_Void_Type
);
2257 Set_Is_DIC_Procedure
(Proc_Id
);
2258 Set_Scope
(Proc_Id
, Current_Scope
);
2259 Set_SPARK_Pragma
(Proc_Id
, SPARK_Mode_Pragma
);
2260 Set_SPARK_Pragma_Inherited
(Proc_Id
);
2262 Set_DIC_Procedure
(Work_Typ
, Proc_Id
);
2264 -- The DIC procedure requires debug info when the assertion expression
2265 -- is subject to Source Coverage Obligations.
2267 if Generate_SCO
then
2268 Set_Debug_Info_Needed
(Proc_Id
);
2271 -- Obtain all views of the input type
2273 Get_Views
(Work_Typ
, Priv_Typ
, Full_Typ
, UFull_Typ
, CRec_Typ
);
2275 -- Associate the DIC procedure and various flags with all views
2277 Propagate_DIC_Attributes
(Priv_Typ
, From_Typ
=> Work_Typ
);
2278 Propagate_DIC_Attributes
(Full_Typ
, From_Typ
=> Work_Typ
);
2279 Propagate_DIC_Attributes
(UFull_Typ
, From_Typ
=> Work_Typ
);
2280 Propagate_DIC_Attributes
(CRec_Typ
, From_Typ
=> Work_Typ
);
2282 -- The declaration of the DIC procedure must be inserted after the
2283 -- declaration of the partial view as this allows for proper external
2286 if Present
(Priv_Typ
) then
2287 Typ_Decl
:= Declaration_Node
(Priv_Typ
);
2289 -- Derived types with the full view as parent do not have a partial
2290 -- view. Insert the DIC procedure after the derived type.
2293 Typ_Decl
:= Declaration_Node
(Full_Typ
);
2296 -- The type should have a declarative node
2298 pragma Assert
(Present
(Typ_Decl
));
2300 -- Create the formal parameter which emulates the variable-like behavior
2301 -- of the type's current instance.
2303 Obj_Id
:= Make_Defining_Identifier
(Loc
, Chars
=> Name_uObject
);
2305 -- Perform minor decoration in case the declaration is not analyzed
2307 Mutate_Ekind
(Obj_Id
, E_In_Parameter
);
2308 Set_Etype
(Obj_Id
, Work_Typ
);
2309 Set_Scope
(Obj_Id
, Proc_Id
);
2311 Set_First_Entity
(Proc_Id
, Obj_Id
);
2312 Set_Last_Entity
(Proc_Id
, Obj_Id
);
2315 -- procedure <Work_Typ>DIC (_object : <Work_Typ>);
2318 Make_Subprogram_Declaration
(Loc
,
2320 Make_Procedure_Specification
(Loc
,
2321 Defining_Unit_Name
=> Proc_Id
,
2322 Parameter_Specifications
=> New_List
(
2323 Make_Parameter_Specification
(Loc
,
2324 Defining_Identifier
=> Obj_Id
,
2326 New_Occurrence_Of
(Work_Typ
, Loc
)))));
2328 -- The declaration should not be inserted into the tree when the context
2329 -- is a generic unit because it is not part of the template.
2331 if Inside_A_Generic
then
2334 -- Semi-insert the declaration into the tree for GNATprove by setting
2335 -- its Parent field. This allows for proper upstream tree traversals.
2337 elsif GNATprove_Mode
then
2338 Set_Parent
(Proc_Decl
, Parent
(Typ_Decl
));
2340 -- Otherwise insert the declaration
2343 Insert_After_And_Analyze
(Typ_Decl
, Proc_Decl
);
2347 Restore_Ghost_Region
(Saved_GM
, Saved_IGR
);
2348 end Build_DIC_Procedure_Declaration
;
2350 ------------------------------------
2351 -- Build_Invariant_Procedure_Body --
2352 ------------------------------------
2354 -- WARNING: This routine manages Ghost regions. Return statements must be
2355 -- replaced by gotos which jump to the end of the routine and restore the
2358 procedure Build_Invariant_Procedure_Body
2360 Partial_Invariant
: Boolean := False)
2362 Loc
: constant Source_Ptr
:= Sloc
(Typ
);
2364 Pragmas_Seen
: Elist_Id
:= No_Elist
;
2365 -- This list contains all invariant pragmas processed so far. The list
2366 -- is used to avoid generating redundant invariant checks.
2368 Produced_Check
: Boolean := False;
2369 -- This flag tracks whether the type has produced at least one invariant
2370 -- check. The flag is used as a sanity check at the end of the routine.
2372 -- NOTE: most of the routines in Build_Invariant_Procedure_Body are
2373 -- intentionally unnested to avoid deep indentation of code.
2375 -- NOTE: all Add_xxx_Invariants routines are reactive. In other words
2376 -- they emit checks, loops (for arrays) and case statements (for record
2377 -- variant parts) only when there are invariants to verify. This keeps
2378 -- the body of the invariant procedure free of useless code.
2380 procedure Add_Array_Component_Invariants
2383 Checks
: in out List_Id
);
2384 -- Generate an invariant check for each component of array type T.
2385 -- Obj_Id denotes the entity of the _object formal parameter of the
2386 -- invariant procedure. All created checks are added to list Checks.
2388 procedure Add_Inherited_Invariants
2390 Priv_Typ
: Entity_Id
;
2391 Full_Typ
: Entity_Id
;
2393 Checks
: in out List_Id
);
2394 -- Generate an invariant check for each inherited class-wide invariant
2395 -- coming from all parent types of type T. Priv_Typ and Full_Typ denote
2396 -- the partial and full view of the parent type. Obj_Id denotes the
2397 -- entity of the _object formal parameter of the invariant procedure.
2398 -- All created checks are added to list Checks.
2400 procedure Add_Interface_Invariants
2403 Checks
: in out List_Id
);
2404 -- Generate an invariant check for each inherited class-wide invariant
2405 -- coming from all interfaces implemented by type T. Obj_Id denotes the
2406 -- entity of the _object formal parameter of the invariant procedure.
2407 -- All created checks are added to list Checks.
2409 procedure Add_Invariant_Check
2412 Checks
: in out List_Id
;
2413 Inherited
: Boolean := False);
2414 -- Subsidiary to all Add_xxx_Invariant routines. Add a runtime check to
2415 -- verify assertion expression Expr of pragma Prag. All generated code
2416 -- is added to list Checks. Flag Inherited should be set when the pragma
2417 -- is inherited from a parent or interface type.
2419 procedure Add_Own_Invariants
2422 Checks
: in out List_Id
;
2423 Priv_Item
: Node_Id
:= Empty
);
2424 -- Generate an invariant check for each invariant found for type T.
2425 -- Obj_Id denotes the entity of the _object formal parameter of the
2426 -- invariant procedure. All created checks are added to list Checks.
2427 -- Priv_Item denotes the first rep item of the private type.
2429 procedure Add_Parent_Invariants
2432 Checks
: in out List_Id
);
2433 -- Generate an invariant check for each inherited class-wide invariant
2434 -- coming from all parent types of type T. Obj_Id denotes the entity of
2435 -- the _object formal parameter of the invariant procedure. All created
2436 -- checks are added to list Checks.
2438 procedure Add_Record_Component_Invariants
2441 Checks
: in out List_Id
);
2442 -- Generate an invariant check for each component of record type T.
2443 -- Obj_Id denotes the entity of the _object formal parameter of the
2444 -- invariant procedure. All created checks are added to list Checks.
2446 ------------------------------------
2447 -- Add_Array_Component_Invariants --
2448 ------------------------------------
2450 procedure Add_Array_Component_Invariants
2453 Checks
: in out List_Id
)
2455 Comp_Typ
: constant Entity_Id
:= Component_Type
(T
);
2456 Dims
: constant Pos
:= Number_Dimensions
(T
);
2458 procedure Process_Array_Component
2460 Comp_Checks
: in out List_Id
);
2461 -- Generate an invariant check for an array component identified by
2462 -- the indices in list Indices. All created checks are added to list
2465 procedure Process_One_Dimension
2468 Dim_Checks
: in out List_Id
);
2469 -- Generate a loop over the Nth dimension Dim of an array type. List
2470 -- Indices contains all array indices for the dimension. All created
2471 -- checks are added to list Dim_Checks.
2473 -----------------------------
2474 -- Process_Array_Component --
2475 -----------------------------
2477 procedure Process_Array_Component
2479 Comp_Checks
: in out List_Id
)
2481 Proc_Id
: Entity_Id
;
2484 if Has_Invariants
(Comp_Typ
) then
2486 -- In GNATprove mode, the component invariants are checked by
2487 -- other means. They should not be added to the array type
2488 -- invariant procedure, so that the procedure can be used to
2489 -- check the array type invariants if any.
2491 if GNATprove_Mode
then
2495 Proc_Id
:= Invariant_Procedure
(Base_Type
(Comp_Typ
));
2497 -- The component type should have an invariant procedure
2498 -- if it has invariants of its own or inherits class-wide
2499 -- invariants from parent or interface types.
2501 pragma Assert
(Present
(Proc_Id
));
2504 -- <Comp_Typ>Invariant (_object (<Indices>));
2506 -- The invariant procedure has a null body if assertions are
2507 -- disabled or Assertion_Policy Ignore is in effect.
2509 if not Has_Null_Body
(Proc_Id
) then
2510 Append_New_To
(Comp_Checks
,
2511 Make_Procedure_Call_Statement
(Loc
,
2513 New_Occurrence_Of
(Proc_Id
, Loc
),
2514 Parameter_Associations
=> New_List
(
2515 Make_Indexed_Component
(Loc
,
2516 Prefix
=> New_Occurrence_Of
(Obj_Id
, Loc
),
2517 Expressions
=> New_Copy_List
(Indices
)))));
2521 Produced_Check
:= True;
2523 end Process_Array_Component
;
2525 ---------------------------
2526 -- Process_One_Dimension --
2527 ---------------------------
2529 procedure Process_One_Dimension
2532 Dim_Checks
: in out List_Id
)
2534 Comp_Checks
: List_Id
:= No_List
;
2538 -- Generate the invariant checks for the array component after all
2539 -- dimensions have produced their respective loops.
2542 Process_Array_Component
2543 (Indices
=> Indices
,
2544 Comp_Checks
=> Dim_Checks
);
2546 -- Otherwise create a loop for the current dimension
2549 -- Create a new loop variable for each dimension
2552 Make_Defining_Identifier
(Loc
,
2553 Chars
=> New_External_Name
('I', Dim
));
2554 Append_To
(Indices
, New_Occurrence_Of
(Index
, Loc
));
2556 Process_One_Dimension
2559 Dim_Checks
=> Comp_Checks
);
2562 -- for I<Dim> in _object'Range (<Dim>) loop
2566 -- Note that the invariant procedure may have a null body if
2567 -- assertions are disabled or Assertion_Policy Ignore is in
2570 if Present
(Comp_Checks
) then
2571 Append_New_To
(Dim_Checks
,
2572 Make_Implicit_Loop_Statement
(T
,
2573 Identifier
=> Empty
,
2575 Make_Iteration_Scheme
(Loc
,
2576 Loop_Parameter_Specification
=>
2577 Make_Loop_Parameter_Specification
(Loc
,
2578 Defining_Identifier
=> Index
,
2579 Discrete_Subtype_Definition
=>
2580 Make_Attribute_Reference
(Loc
,
2582 New_Occurrence_Of
(Obj_Id
, Loc
),
2583 Attribute_Name
=> Name_Range
,
2584 Expressions
=> New_List
(
2585 Make_Integer_Literal
(Loc
, Dim
))))),
2586 Statements
=> Comp_Checks
));
2589 end Process_One_Dimension
;
2591 -- Start of processing for Add_Array_Component_Invariants
2594 Process_One_Dimension
2596 Indices
=> New_List
,
2597 Dim_Checks
=> Checks
);
2598 end Add_Array_Component_Invariants
;
2600 ------------------------------
2601 -- Add_Inherited_Invariants --
2602 ------------------------------
2604 procedure Add_Inherited_Invariants
2606 Priv_Typ
: Entity_Id
;
2607 Full_Typ
: Entity_Id
;
2609 Checks
: in out List_Id
)
2611 Deriv_Typ
: Entity_Id
;
2614 Prag_Expr
: Node_Id
;
2615 Prag_Expr_Arg
: Node_Id
;
2617 Prag_Typ_Arg
: Node_Id
;
2619 Par_Proc
: Entity_Id
;
2620 -- The "partial" invariant procedure of Par_Typ
2622 Par_Typ
: Entity_Id
;
2623 -- The suitable view of the parent type used in the substitution of
2627 if not Present
(Priv_Typ
) and then not Present
(Full_Typ
) then
2631 -- When the type inheriting the class-wide invariant is a concurrent
2632 -- type, use the corresponding record type because it contains all
2633 -- primitive operations of the concurrent type and allows for proper
2636 if Is_Concurrent_Type
(T
) then
2637 Deriv_Typ
:= Corresponding_Record_Type
(T
);
2642 pragma Assert
(Present
(Deriv_Typ
));
2644 -- Determine which rep item chain to use. Precedence is given to that
2645 -- of the parent type's partial view since it usually carries all the
2646 -- class-wide invariants.
2648 if Present
(Priv_Typ
) then
2649 Prag
:= First_Rep_Item
(Priv_Typ
);
2651 Prag
:= First_Rep_Item
(Full_Typ
);
2654 while Present
(Prag
) loop
2655 if Nkind
(Prag
) = N_Pragma
2656 and then Pragma_Name
(Prag
) = Name_Invariant
2658 -- Nothing to do if the pragma was already processed
2660 if Contains
(Pragmas_Seen
, Prag
) then
2663 -- Nothing to do when the caller requests the processing of all
2664 -- inherited class-wide invariants, but the pragma does not
2665 -- fall in this category.
2667 elsif not Class_Present
(Prag
) then
2671 -- Extract the arguments of the invariant pragma
2673 Prag_Typ_Arg
:= First
(Pragma_Argument_Associations
(Prag
));
2674 Prag_Expr_Arg
:= Next
(Prag_Typ_Arg
);
2675 Prag_Expr
:= Expression_Copy
(Prag_Expr_Arg
);
2676 Prag_Typ
:= Get_Pragma_Arg
(Prag_Typ_Arg
);
2678 -- The pragma applies to the partial view of the parent type
2680 if Present
(Priv_Typ
)
2681 and then Entity
(Prag_Typ
) = Priv_Typ
2683 Par_Typ
:= Priv_Typ
;
2685 -- The pragma applies to the full view of the parent type
2687 elsif Present
(Full_Typ
)
2688 and then Entity
(Prag_Typ
) = Full_Typ
2690 Par_Typ
:= Full_Typ
;
2692 -- Otherwise the pragma does not belong to the parent type and
2693 -- should not be considered.
2699 -- Perform the following substitutions:
2701 -- * Replace a reference to the _object parameter of the
2702 -- parent type's partial invariant procedure with a
2703 -- reference to the _object parameter of the derived
2704 -- type's full invariant procedure.
2706 -- * Replace a reference to a discriminant of the parent type
2707 -- with a suitable value from the point of view of the
2710 -- * Replace a call to an overridden parent primitive with a
2711 -- call to the overriding derived type primitive.
2713 -- * Replace a call to an inherited parent primitive with a
2714 -- call to the internally-generated inherited derived type
2717 Expr
:= New_Copy_Tree
(Prag_Expr
);
2719 -- The parent type must have a "partial" invariant procedure
2720 -- because class-wide invariants are captured exclusively by
2723 Par_Proc
:= Partial_Invariant_Procedure
(Par_Typ
);
2724 pragma Assert
(Present
(Par_Proc
));
2729 Deriv_Typ
=> Deriv_Typ
,
2730 Par_Obj
=> First_Formal
(Par_Proc
),
2731 Deriv_Obj
=> Obj_Id
);
2733 Add_Invariant_Check
(Prag
, Expr
, Checks
, Inherited
=> True);
2736 Next_Rep_Item
(Prag
);
2738 end Add_Inherited_Invariants
;
2740 ------------------------------
2741 -- Add_Interface_Invariants --
2742 ------------------------------
2744 procedure Add_Interface_Invariants
2747 Checks
: in out List_Id
)
2749 Iface_Elmt
: Elmt_Id
;
2753 -- Generate an invariant check for each class-wide invariant coming
2754 -- from all interfaces implemented by type T.
2756 if Is_Tagged_Type
(T
) then
2757 Collect_Interfaces
(T
, Ifaces
);
2759 -- Process the class-wide invariants of all implemented interfaces
2761 Iface_Elmt
:= First_Elmt
(Ifaces
);
2762 while Present
(Iface_Elmt
) loop
2764 -- The Full_Typ parameter is intentionally left Empty because
2765 -- interfaces are treated as the partial view of a private type
2766 -- in order to achieve uniformity with the general case.
2768 Add_Inherited_Invariants
2770 Priv_Typ
=> Node
(Iface_Elmt
),
2775 Next_Elmt
(Iface_Elmt
);
2778 end Add_Interface_Invariants
;
2780 -------------------------
2781 -- Add_Invariant_Check --
2782 -------------------------
2784 procedure Add_Invariant_Check
2787 Checks
: in out List_Id
;
2788 Inherited
: Boolean := False)
2790 Args
: constant List_Id
:= Pragma_Argument_Associations
(Prag
);
2791 Nam
: constant Name_Id
:= Original_Aspect_Pragma_Name
(Prag
);
2792 Ploc
: constant Source_Ptr
:= Sloc
(Prag
);
2793 Str_Arg
: constant Node_Id
:= Next
(Next
(First
(Args
)));
2799 -- The invariant is ignored, nothing left to do
2801 if Is_Ignored
(Prag
) then
2804 -- Otherwise the invariant is checked. Build a pragma Check to verify
2805 -- the expression at run time.
2809 Make_Pragma_Argument_Association
(Ploc
,
2810 Expression
=> Make_Identifier
(Ploc
, Nam
)),
2811 Make_Pragma_Argument_Association
(Ploc
,
2812 Expression
=> Expr
));
2814 -- Handle the String argument (if any)
2816 if Present
(Str_Arg
) then
2817 Str
:= Strval
(Get_Pragma_Arg
(Str_Arg
));
2819 -- When inheriting an invariant, modify the message from
2820 -- "failed invariant" to "failed inherited invariant".
2823 String_To_Name_Buffer
(Str
);
2825 if Name_Buffer
(1 .. 16) = "failed invariant" then
2826 Insert_Str_In_Name_Buffer
("inherited ", 8);
2827 Str
:= String_From_Name_Buffer
;
2832 Make_Pragma_Argument_Association
(Ploc
,
2833 Expression
=> Make_String_Literal
(Ploc
, Str
)));
2837 -- pragma Check (<Nam>, <Expr>, <Str>);
2839 Append_New_To
(Checks
,
2841 Chars
=> Name_Check
,
2842 Pragma_Argument_Associations
=> Assoc
));
2845 -- Output an info message when inheriting an invariant and the
2846 -- listing option is enabled.
2848 if Inherited
and Opt
.List_Inherited_Aspects
then
2849 Error_Msg_Sloc
:= Sloc
(Prag
);
2851 ("info: & inherits `Invariant''Class` aspect from #?.l?", Typ
);
2854 -- Add the pragma to the list of processed pragmas
2856 Append_New_Elmt
(Prag
, Pragmas_Seen
);
2857 Produced_Check
:= True;
2858 end Add_Invariant_Check
;
2860 ---------------------------
2861 -- Add_Parent_Invariants --
2862 ---------------------------
2864 procedure Add_Parent_Invariants
2867 Checks
: in out List_Id
)
2869 Dummy_1
: Entity_Id
;
2870 Dummy_2
: Entity_Id
;
2872 Curr_Typ
: Entity_Id
;
2873 -- The entity of the current type being examined
2875 Full_Typ
: Entity_Id
;
2876 -- The full view of Par_Typ
2878 Par_Typ
: Entity_Id
;
2879 -- The entity of the parent type
2881 Priv_Typ
: Entity_Id
;
2882 -- The partial view of Par_Typ
2885 -- Do not process array types because they cannot have true parent
2886 -- types. This also prevents the generation of a duplicate invariant
2887 -- check when the input type is an array base type because its Etype
2888 -- denotes the first subtype, both of which share the same component
2891 if Is_Array_Type
(T
) then
2895 -- Climb the parent type chain
2899 -- Do not consider subtypes as they inherit the invariants
2900 -- from their base types.
2902 Par_Typ
:= Base_Type
(Etype
(Curr_Typ
));
2904 -- Stop the climb once the root of the parent chain is
2907 exit when Curr_Typ
= Par_Typ
;
2909 -- Process the class-wide invariants of the parent type
2911 Get_Views
(Par_Typ
, Priv_Typ
, Full_Typ
, Dummy_1
, Dummy_2
);
2913 -- Process the elements of an array type
2915 if Is_Array_Type
(Full_Typ
) then
2916 Add_Array_Component_Invariants
(Full_Typ
, Obj_Id
, Checks
);
2918 -- Process the components of a record type
2920 elsif Ekind
(Full_Typ
) = E_Record_Type
then
2921 Add_Record_Component_Invariants
(Full_Typ
, Obj_Id
, Checks
);
2924 Add_Inherited_Invariants
2926 Priv_Typ
=> Priv_Typ
,
2927 Full_Typ
=> Full_Typ
,
2931 Curr_Typ
:= Par_Typ
;
2933 end Add_Parent_Invariants
;
2935 ------------------------
2936 -- Add_Own_Invariants --
2937 ------------------------
2939 procedure Add_Own_Invariants
2942 Checks
: in out List_Id
;
2943 Priv_Item
: Node_Id
:= Empty
)
2948 Prag_Expr
: Node_Id
;
2949 Prag_Expr_Arg
: Node_Id
;
2951 Prag_Typ_Arg
: Node_Id
;
2954 if not Present
(T
) then
2958 Prag
:= First_Rep_Item
(T
);
2959 while Present
(Prag
) loop
2960 if Nkind
(Prag
) = N_Pragma
2961 and then Pragma_Name
(Prag
) = Name_Invariant
2963 -- Stop the traversal of the rep item chain once a specific
2964 -- item is encountered.
2966 if Present
(Priv_Item
) and then Prag
= Priv_Item
then
2970 -- Nothing to do if the pragma was already processed
2972 if Contains
(Pragmas_Seen
, Prag
) then
2976 -- Extract the arguments of the invariant pragma
2978 Prag_Typ_Arg
:= First
(Pragma_Argument_Associations
(Prag
));
2979 Prag_Expr_Arg
:= Next
(Prag_Typ_Arg
);
2980 Prag_Expr
:= Get_Pragma_Arg
(Prag_Expr_Arg
);
2981 Prag_Typ
:= Get_Pragma_Arg
(Prag_Typ_Arg
);
2982 Prag_Asp
:= Corresponding_Aspect
(Prag
);
2984 -- Verify the pragma belongs to T, otherwise the pragma applies
2985 -- to a parent type in which case it will be processed later by
2986 -- Add_Parent_Invariants or Add_Interface_Invariants.
2988 if Entity
(Prag_Typ
) /= T
then
2992 Expr
:= New_Copy_Tree
(Prag_Expr
);
2994 -- Substitute all references to type T with references to the
2995 -- _object formal parameter.
2997 Replace_Type_References
(Expr
, T
, Obj_Id
);
2999 -- Preanalyze the invariant expression to detect errors and at
3000 -- the same time capture the visibility of the proper package
3003 Set_Parent
(Expr
, Parent
(Prag_Expr
));
3004 Preanalyze_Assert_Expression
(Expr
, Any_Boolean
);
3006 -- Save a copy of the expression when T is tagged to detect
3007 -- errors and capture the visibility of the proper package part
3008 -- for the generation of inherited type invariants.
3010 if Is_Tagged_Type
(T
) then
3011 Set_Expression_Copy
(Prag_Expr_Arg
, New_Copy_Tree
(Expr
));
3014 -- If the pragma comes from an aspect specification, replace
3015 -- the saved expression because all type references must be
3016 -- substituted for the call to Preanalyze_Spec_Expression in
3017 -- Check_Aspect_At_xxx routines.
3019 if Present
(Prag_Asp
) then
3020 Set_Entity
(Identifier
(Prag_Asp
), New_Copy_Tree
(Expr
));
3023 Add_Invariant_Check
(Prag
, Expr
, Checks
);
3026 Next_Rep_Item
(Prag
);
3028 end Add_Own_Invariants
;
3030 -------------------------------------
3031 -- Add_Record_Component_Invariants --
3032 -------------------------------------
3034 procedure Add_Record_Component_Invariants
3037 Checks
: in out List_Id
)
3039 procedure Process_Component_List
3040 (Comp_List
: Node_Id
;
3041 CL_Checks
: in out List_Id
);
3042 -- Generate invariant checks for all record components found in
3043 -- component list Comp_List, including variant parts. All created
3044 -- checks are added to list CL_Checks.
3046 procedure Process_Record_Component
3047 (Comp_Id
: Entity_Id
;
3048 Comp_Checks
: in out List_Id
);
3049 -- Generate an invariant check for a record component identified by
3050 -- Comp_Id. All created checks are added to list Comp_Checks.
3052 ----------------------------
3053 -- Process_Component_List --
3054 ----------------------------
3056 procedure Process_Component_List
3057 (Comp_List
: Node_Id
;
3058 CL_Checks
: in out List_Id
)
3062 Var_Alts
: List_Id
:= No_List
;
3063 Var_Checks
: List_Id
:= No_List
;
3064 Var_Stmts
: List_Id
;
3066 Produced_Variant_Check
: Boolean := False;
3067 -- This flag tracks whether the component has produced at least
3068 -- one invariant check.
3071 -- Traverse the component items
3073 Comp
:= First
(Component_Items
(Comp_List
));
3074 while Present
(Comp
) loop
3075 if Nkind
(Comp
) = N_Component_Declaration
then
3077 -- Generate the component invariant check
3079 Process_Record_Component
3080 (Comp_Id
=> Defining_Entity
(Comp
),
3081 Comp_Checks
=> CL_Checks
);
3087 -- Traverse the variant part
3089 if Present
(Variant_Part
(Comp_List
)) then
3090 Var
:= First
(Variants
(Variant_Part
(Comp_List
)));
3091 while Present
(Var
) loop
3092 Var_Checks
:= No_List
;
3094 -- Generate invariant checks for all components and variant
3095 -- parts that qualify.
3097 Process_Component_List
3098 (Comp_List
=> Component_List
(Var
),
3099 CL_Checks
=> Var_Checks
);
3101 -- The components of the current variant produced at least
3102 -- one invariant check.
3104 if Present
(Var_Checks
) then
3105 Var_Stmts
:= Var_Checks
;
3106 Produced_Variant_Check
:= True;
3108 -- Otherwise there are either no components with invariants,
3109 -- assertions are disabled, or Assertion_Policy Ignore is in
3113 Var_Stmts
:= New_List
(Make_Null_Statement
(Loc
));
3116 Append_New_To
(Var_Alts
,
3117 Make_Case_Statement_Alternative
(Loc
,
3119 New_Copy_List
(Discrete_Choices
(Var
)),
3120 Statements
=> Var_Stmts
));
3125 -- Create a case statement which verifies the invariant checks
3126 -- of a particular component list depending on the discriminant
3127 -- values only when there is at least one real invariant check.
3129 if Produced_Variant_Check
then
3130 Append_New_To
(CL_Checks
,
3131 Make_Case_Statement
(Loc
,
3133 Make_Selected_Component
(Loc
,
3134 Prefix
=> New_Occurrence_Of
(Obj_Id
, Loc
),
3137 (Entity
(Name
(Variant_Part
(Comp_List
))), Loc
)),
3138 Alternatives
=> Var_Alts
));
3141 end Process_Component_List
;
3143 ------------------------------
3144 -- Process_Record_Component --
3145 ------------------------------
3147 procedure Process_Record_Component
3148 (Comp_Id
: Entity_Id
;
3149 Comp_Checks
: in out List_Id
)
3151 Comp_Typ
: constant Entity_Id
:= Etype
(Comp_Id
);
3152 Proc_Id
: Entity_Id
;
3154 Produced_Component_Check
: Boolean := False;
3155 -- This flag tracks whether the component has produced at least
3156 -- one invariant check.
3159 -- Nothing to do for internal component _parent. Note that it is
3160 -- not desirable to check whether the component comes from source
3161 -- because protected type components are relocated to an internal
3162 -- corresponding record, but still need processing.
3164 if Chars
(Comp_Id
) = Name_uParent
then
3168 -- Verify the invariant of the component. Note that an access
3169 -- type may have an invariant when it acts as the full view of a
3170 -- private type and the invariant appears on the partial view. In
3171 -- this case verify the access value itself.
3173 if Has_Invariants
(Comp_Typ
) then
3175 -- In GNATprove mode, the component invariants are checked by
3176 -- other means. They should not be added to the record type
3177 -- invariant procedure, so that the procedure can be used to
3178 -- check the record type invariants if any.
3180 if GNATprove_Mode
then
3184 Proc_Id
:= Invariant_Procedure
(Base_Type
(Comp_Typ
));
3186 -- The component type should have an invariant procedure
3187 -- if it has invariants of its own or inherits class-wide
3188 -- invariants from parent or interface types.
3190 pragma Assert
(Present
(Proc_Id
));
3193 -- <Comp_Typ>Invariant (T (_object).<Comp_Id>);
3195 -- Note that the invariant procedure may have a null body if
3196 -- assertions are disabled or Assertion_Policy Ignore is in
3199 if not Has_Null_Body
(Proc_Id
) then
3200 Append_New_To
(Comp_Checks
,
3201 Make_Procedure_Call_Statement
(Loc
,
3203 New_Occurrence_Of
(Proc_Id
, Loc
),
3204 Parameter_Associations
=> New_List
(
3205 Make_Selected_Component
(Loc
,
3207 Unchecked_Convert_To
3208 (T
, New_Occurrence_Of
(Obj_Id
, Loc
)),
3210 New_Occurrence_Of
(Comp_Id
, Loc
)))));
3214 Produced_Check
:= True;
3215 Produced_Component_Check
:= True;
3218 if Produced_Component_Check
and then Has_Unchecked_Union
(T
) then
3220 ("invariants cannot be checked on components of "
3221 & "unchecked_union type &??", Comp_Id
, T
);
3223 end Process_Record_Component
;
3230 -- Start of processing for Add_Record_Component_Invariants
3233 -- An untagged derived type inherits the components of its parent
3234 -- type. In order to avoid creating redundant invariant checks, do
3235 -- not process the components now. Instead wait until the ultimate
3236 -- parent of the untagged derivation chain is reached.
3238 if not Is_Untagged_Derivation
(T
) then
3239 Def
:= Type_Definition
(Parent
(T
));
3241 if Nkind
(Def
) = N_Derived_Type_Definition
then
3242 Def
:= Record_Extension_Part
(Def
);
3245 pragma Assert
(Nkind
(Def
) = N_Record_Definition
);
3246 Comps
:= Component_List
(Def
);
3248 if Present
(Comps
) then
3249 Process_Component_List
3250 (Comp_List
=> Comps
,
3251 CL_Checks
=> Checks
);
3254 end Add_Record_Component_Invariants
;
3258 Saved_GM
: constant Ghost_Mode_Type
:= Ghost_Mode
;
3259 Saved_IGR
: constant Node_Id
:= Ignored_Ghost_Region
;
3260 -- Save the Ghost-related attributes to restore on exit
3263 Priv_Item
: Node_Id
;
3264 Proc_Body
: Node_Id
;
3265 Proc_Body_Id
: Entity_Id
;
3266 Proc_Decl
: Node_Id
;
3267 Proc_Id
: Entity_Id
;
3268 Stmts
: List_Id
:= No_List
;
3270 CRec_Typ
: Entity_Id
:= Empty
;
3271 -- The corresponding record type of Full_Typ
3273 Full_Proc
: Entity_Id
:= Empty
;
3274 -- The entity of the "full" invariant procedure
3276 Full_Typ
: Entity_Id
:= Empty
;
3277 -- The full view of the working type
3279 Obj_Id
: Entity_Id
:= Empty
;
3280 -- The _object formal parameter of the invariant procedure
3282 Part_Proc
: Entity_Id
:= Empty
;
3283 -- The entity of the "partial" invariant procedure
3285 Priv_Typ
: Entity_Id
:= Empty
;
3286 -- The partial view of the working type
3288 Work_Typ
: Entity_Id
:= Empty
;
3291 -- Start of processing for Build_Invariant_Procedure_Body
3296 -- Do not process the underlying full view of a private type. There is
3297 -- no way to get back to the partial view, plus the body will be built
3298 -- by the full view or the base type.
3300 if Is_Underlying_Full_View
(Work_Typ
) then
3303 -- The input type denotes the implementation base type of a constrained
3304 -- array type. Work with the first subtype as all invariant pragmas are
3305 -- on its rep item chain.
3307 elsif Ekind
(Work_Typ
) = E_Array_Type
and then Is_Itype
(Work_Typ
) then
3308 Work_Typ
:= First_Subtype
(Work_Typ
);
3310 -- The input type denotes the corresponding record type of a protected
3311 -- or task type. Work with the concurrent type because the corresponding
3312 -- record type may not be visible to clients of the type.
3314 elsif Ekind
(Work_Typ
) = E_Record_Type
3315 and then Is_Concurrent_Record_Type
(Work_Typ
)
3317 Work_Typ
:= Corresponding_Concurrent_Type
(Work_Typ
);
3320 -- The working type may be subject to pragma Ghost. Set the mode now to
3321 -- ensure that the invariant procedure is properly marked as Ghost.
3323 Set_Ghost_Mode
(Work_Typ
);
3325 -- The type must either have invariants of its own, inherit class-wide
3326 -- invariants from parent types or interfaces, or be an array or record
3327 -- type whose components have invariants.
3329 pragma Assert
(Has_Invariants
(Work_Typ
));
3331 -- Interfaces are treated as the partial view of a private type in order
3332 -- to achieve uniformity with the general case.
3334 if Is_Interface
(Work_Typ
) then
3335 Priv_Typ
:= Work_Typ
;
3337 -- Otherwise obtain both views of the type
3340 Get_Views
(Work_Typ
, Priv_Typ
, Full_Typ
, Dummy
, CRec_Typ
);
3343 -- The caller requests a body for the partial invariant procedure
3345 if Partial_Invariant
then
3346 Full_Proc
:= Invariant_Procedure
(Work_Typ
);
3347 Proc_Id
:= Partial_Invariant_Procedure
(Work_Typ
);
3349 -- The "full" invariant procedure body was already created
3351 if Present
(Full_Proc
)
3353 (Corresponding_Body
(Unit_Declaration_Node
(Full_Proc
)))
3355 -- This scenario happens only when the type is an untagged
3356 -- derivation from a private parent and the underlying full
3357 -- view was processed before the partial view.
3360 (Is_Untagged_Private_Derivation
(Priv_Typ
, Full_Typ
));
3362 -- Nothing to do because the processing of the underlying full
3363 -- view already checked the invariants of the partial view.
3368 -- Create a declaration for the "partial" invariant procedure if it
3369 -- is not available.
3371 if No
(Proc_Id
) then
3372 Build_Invariant_Procedure_Declaration
3374 Partial_Invariant
=> True);
3376 Proc_Id
:= Partial_Invariant_Procedure
(Work_Typ
);
3379 -- The caller requests a body for the "full" invariant procedure
3382 Proc_Id
:= Invariant_Procedure
(Work_Typ
);
3383 Part_Proc
:= Partial_Invariant_Procedure
(Work_Typ
);
3385 -- Create a declaration for the "full" invariant procedure if it is
3388 if No
(Proc_Id
) then
3389 Build_Invariant_Procedure_Declaration
(Work_Typ
);
3390 Proc_Id
:= Invariant_Procedure
(Work_Typ
);
3394 -- At this point there should be an invariant procedure declaration
3396 pragma Assert
(Present
(Proc_Id
));
3397 Proc_Decl
:= Unit_Declaration_Node
(Proc_Id
);
3399 -- Nothing to do if the invariant procedure already has a body
3401 if Present
(Corresponding_Body
(Proc_Decl
)) then
3405 -- Emulate the environment of the invariant procedure by installing its
3406 -- scope and formal parameters. Note that this is not needed, but having
3407 -- the scope installed helps with the detection of invariant-related
3410 Push_Scope
(Proc_Id
);
3411 Install_Formals
(Proc_Id
);
3413 Obj_Id
:= First_Formal
(Proc_Id
);
3414 pragma Assert
(Present
(Obj_Id
));
3416 -- The "partial" invariant procedure verifies the invariants of the
3417 -- partial view only.
3419 if Partial_Invariant
then
3420 pragma Assert
(Present
(Priv_Typ
));
3427 -- Otherwise the "full" invariant procedure verifies the invariants of
3428 -- the full view, all array or record components, as well as class-wide
3429 -- invariants inherited from parent types or interfaces. In addition, it
3430 -- indirectly verifies the invariants of the partial view by calling the
3431 -- "partial" invariant procedure.
3434 pragma Assert
(Present
(Full_Typ
));
3436 -- Check the invariants of the partial view by calling the "partial"
3437 -- invariant procedure. Generate:
3439 -- <Work_Typ>Partial_Invariant (_object);
3441 if Present
(Part_Proc
) then
3442 Append_New_To
(Stmts
,
3443 Make_Procedure_Call_Statement
(Loc
,
3444 Name
=> New_Occurrence_Of
(Part_Proc
, Loc
),
3445 Parameter_Associations
=> New_List
(
3446 New_Occurrence_Of
(Obj_Id
, Loc
))));
3448 Produced_Check
:= True;
3453 -- Derived subtypes do not have a partial view
3455 if Present
(Priv_Typ
) then
3457 -- The processing of the "full" invariant procedure intentionally
3458 -- skips the partial view because a) this may result in changes of
3459 -- visibility and b) lead to duplicate checks. However, when the
3460 -- full view is the underlying full view of an untagged derived
3461 -- type whose parent type is private, partial invariants appear on
3462 -- the rep item chain of the partial view only.
3464 -- package Pack_1 is
3465 -- type Root ... is private;
3467 -- <full view of Root>
3471 -- package Pack_2 is
3472 -- type Child is new Pack_1.Root with Type_Invariant => ...;
3473 -- <underlying full view of Child>
3476 -- As a result, the processing of the full view must also consider
3477 -- all invariants of the partial view.
3479 if Is_Untagged_Private_Derivation
(Priv_Typ
, Full_Typ
) then
3482 -- Otherwise the invariants of the partial view are ignored
3485 -- Note that the rep item chain is shared between the partial
3486 -- and full views of a type. To avoid processing the invariants
3487 -- of the partial view, signal the logic to stop when the first
3488 -- rep item of the partial view has been reached.
3490 Priv_Item
:= First_Rep_Item
(Priv_Typ
);
3492 -- Ignore the invariants of the partial view by eliminating the
3499 -- Process the invariants of the full view and in certain cases those
3500 -- of the partial view. This also handles any invariants on array or
3501 -- record components.
3507 Priv_Item
=> Priv_Item
);
3513 Priv_Item
=> Priv_Item
);
3515 -- Process the elements of an array type
3517 if Is_Array_Type
(Full_Typ
) then
3518 Add_Array_Component_Invariants
(Full_Typ
, Obj_Id
, Stmts
);
3520 -- Process the components of a record type
3522 elsif Ekind
(Full_Typ
) = E_Record_Type
then
3523 Add_Record_Component_Invariants
(Full_Typ
, Obj_Id
, Stmts
);
3525 -- Process the components of a corresponding record
3527 elsif Present
(CRec_Typ
) then
3528 Add_Record_Component_Invariants
(CRec_Typ
, Obj_Id
, Stmts
);
3531 -- Process the inherited class-wide invariants of all parent types.
3532 -- This also handles any invariants on record components.
3534 Add_Parent_Invariants
(Full_Typ
, Obj_Id
, Stmts
);
3536 -- Process the inherited class-wide invariants of all implemented
3539 Add_Interface_Invariants
(Full_Typ
, Obj_Id
, Stmts
);
3544 -- At this point there should be at least one invariant check. If this
3545 -- is not the case, then the invariant-related flags were not properly
3546 -- set, or there is a missing invariant procedure on one of the array
3547 -- or record components.
3549 pragma Assert
(Produced_Check
);
3551 -- Account for the case where assertions are disabled or all invariant
3552 -- checks are subject to Assertion_Policy Ignore. Produce a completing
3556 Stmts
:= New_List
(Make_Null_Statement
(Loc
));
3560 -- procedure <Work_Typ>[Partial_]Invariant (_object : <Obj_Typ>) is
3563 -- end <Work_Typ>[Partial_]Invariant;
3566 Make_Subprogram_Body
(Loc
,
3568 Copy_Subprogram_Spec
(Parent
(Proc_Id
)),
3569 Declarations
=> Empty_List
,
3570 Handled_Statement_Sequence
=>
3571 Make_Handled_Sequence_Of_Statements
(Loc
,
3572 Statements
=> Stmts
));
3573 Proc_Body_Id
:= Defining_Entity
(Proc_Body
);
3575 -- Perform minor decoration in case the body is not analyzed
3577 Mutate_Ekind
(Proc_Body_Id
, E_Subprogram_Body
);
3578 Set_Etype
(Proc_Body_Id
, Standard_Void_Type
);
3579 Set_Scope
(Proc_Body_Id
, Current_Scope
);
3581 -- Link both spec and body to avoid generating duplicates
3583 Set_Corresponding_Body
(Proc_Decl
, Proc_Body_Id
);
3584 Set_Corresponding_Spec
(Proc_Body
, Proc_Id
);
3586 -- The body should not be inserted into the tree when the context is
3587 -- a generic unit because it is not part of the template. Note
3588 -- that the body must still be generated in order to resolve the
3591 if Inside_A_Generic
then
3594 -- Semi-insert the body into the tree for GNATprove by setting its
3595 -- Parent field. This allows for proper upstream tree traversals.
3597 elsif GNATprove_Mode
then
3598 Set_Parent
(Proc_Body
, Parent
(Declaration_Node
(Work_Typ
)));
3600 -- Otherwise the body is part of the freezing actions of the type
3603 Append_Freeze_Action
(Work_Typ
, Proc_Body
);
3607 Restore_Ghost_Region
(Saved_GM
, Saved_IGR
);
3608 end Build_Invariant_Procedure_Body
;
3610 -------------------------------------------
3611 -- Build_Invariant_Procedure_Declaration --
3612 -------------------------------------------
3614 -- WARNING: This routine manages Ghost regions. Return statements must be
3615 -- replaced by gotos which jump to the end of the routine and restore the
3618 procedure Build_Invariant_Procedure_Declaration
3620 Partial_Invariant
: Boolean := False)
3622 Loc
: constant Source_Ptr
:= Sloc
(Typ
);
3624 Saved_GM
: constant Ghost_Mode_Type
:= Ghost_Mode
;
3625 Saved_IGR
: constant Node_Id
:= Ignored_Ghost_Region
;
3626 -- Save the Ghost-related attributes to restore on exit
3628 Proc_Decl
: Node_Id
;
3629 Proc_Id
: Entity_Id
;
3633 CRec_Typ
: Entity_Id
;
3634 -- The corresponding record type of Full_Typ
3636 Full_Typ
: Entity_Id
;
3637 -- The full view of working type
3640 -- The _object formal parameter of the invariant procedure
3642 Obj_Typ
: Entity_Id
;
3643 -- The type of the _object formal parameter
3645 Priv_Typ
: Entity_Id
;
3646 -- The partial view of working type
3648 UFull_Typ
: Entity_Id
;
3649 -- The underlying full view of Full_Typ
3651 Work_Typ
: Entity_Id
;
3657 -- The input type denotes the implementation base type of a constrained
3658 -- array type. Work with the first subtype as all invariant pragmas are
3659 -- on its rep item chain.
3661 if Ekind
(Work_Typ
) = E_Array_Type
and then Is_Itype
(Work_Typ
) then
3662 Work_Typ
:= First_Subtype
(Work_Typ
);
3664 -- The input denotes the corresponding record type of a protected or a
3665 -- task type. Work with the concurrent type because the corresponding
3666 -- record type may not be visible to clients of the type.
3668 elsif Ekind
(Work_Typ
) = E_Record_Type
3669 and then Is_Concurrent_Record_Type
(Work_Typ
)
3671 Work_Typ
:= Corresponding_Concurrent_Type
(Work_Typ
);
3674 -- The working type may be subject to pragma Ghost. Set the mode now to
3675 -- ensure that the invariant procedure is properly marked as Ghost.
3677 Set_Ghost_Mode
(Work_Typ
);
3679 -- The type must either have invariants of its own, inherit class-wide
3680 -- invariants from parent or interface types, or be an array or record
3681 -- type whose components have invariants.
3683 pragma Assert
(Has_Invariants
(Work_Typ
));
3685 -- Nothing to do if the type already has a "partial" invariant procedure
3687 if Partial_Invariant
then
3688 if Present
(Partial_Invariant_Procedure
(Work_Typ
)) then
3692 -- Nothing to do if the type already has a "full" invariant procedure
3694 elsif Present
(Invariant_Procedure
(Work_Typ
)) then
3698 -- The caller requests the declaration of the "partial" invariant
3701 if Partial_Invariant
then
3702 Proc_Nam
:= New_External_Name
(Chars
(Work_Typ
), "Partial_Invariant");
3704 -- Otherwise the caller requests the declaration of the "full" invariant
3708 Proc_Nam
:= New_External_Name
(Chars
(Work_Typ
), "Invariant");
3711 Proc_Id
:= Make_Defining_Identifier
(Loc
, Chars
=> Proc_Nam
);
3713 -- Perform minor decoration in case the declaration is not analyzed
3715 Mutate_Ekind
(Proc_Id
, E_Procedure
);
3716 Set_Etype
(Proc_Id
, Standard_Void_Type
);
3717 Set_Scope
(Proc_Id
, Current_Scope
);
3719 if Partial_Invariant
then
3720 Set_Is_Partial_Invariant_Procedure
(Proc_Id
);
3721 Set_Partial_Invariant_Procedure
(Work_Typ
, Proc_Id
);
3723 Set_Is_Invariant_Procedure
(Proc_Id
);
3724 Set_Invariant_Procedure
(Work_Typ
, Proc_Id
);
3727 -- The invariant procedure requires debug info when the invariants are
3728 -- subject to Source Coverage Obligations.
3730 if Generate_SCO
then
3731 Set_Debug_Info_Needed
(Proc_Id
);
3734 -- Obtain all views of the input type
3736 Get_Views
(Work_Typ
, Priv_Typ
, Full_Typ
, UFull_Typ
, CRec_Typ
);
3738 -- Associate the invariant procedure and various flags with all views
3740 Propagate_Invariant_Attributes
(Priv_Typ
, From_Typ
=> Work_Typ
);
3741 Propagate_Invariant_Attributes
(Full_Typ
, From_Typ
=> Work_Typ
);
3742 Propagate_Invariant_Attributes
(UFull_Typ
, From_Typ
=> Work_Typ
);
3743 Propagate_Invariant_Attributes
(CRec_Typ
, From_Typ
=> Work_Typ
);
3745 -- The declaration of the invariant procedure is inserted after the
3746 -- declaration of the partial view as this allows for proper external
3749 if Present
(Priv_Typ
) then
3750 Typ_Decl
:= Declaration_Node
(Priv_Typ
);
3752 -- Anonymous arrays in object declarations have no explicit declaration
3753 -- so use the related object declaration as the insertion point.
3755 elsif Is_Itype
(Work_Typ
) and then Is_Array_Type
(Work_Typ
) then
3756 Typ_Decl
:= Associated_Node_For_Itype
(Work_Typ
);
3758 -- Derived types with the full view as parent do not have a partial
3759 -- view. Insert the invariant procedure after the derived type.
3762 Typ_Decl
:= Declaration_Node
(Full_Typ
);
3765 -- The type should have a declarative node
3767 pragma Assert
(Present
(Typ_Decl
));
3769 -- Create the formal parameter which emulates the variable-like behavior
3770 -- of the current type instance.
3772 Obj_Id
:= Make_Defining_Identifier
(Loc
, Chars
=> Name_uObject
);
3774 -- When generating an invariant procedure declaration for an abstract
3775 -- type (including interfaces), use the class-wide type as the _object
3776 -- type. This has several desirable effects:
3778 -- * The invariant procedure does not become a primitive of the type.
3779 -- This eliminates the need to either special case the treatment of
3780 -- invariant procedures, or to make it a predefined primitive and
3781 -- force every derived type to potentially provide an empty body.
3783 -- * The invariant procedure does not need to be declared as abstract.
3784 -- This allows for a proper body, which in turn avoids redundant
3785 -- processing of the same invariants for types with multiple views.
3787 -- * The class-wide type allows for calls to abstract primitives
3788 -- within a nonabstract subprogram. The calls are treated as
3789 -- dispatching and require additional processing when they are
3790 -- remapped to call primitives of derived types. See routine
3791 -- Replace_References for details.
3793 if Is_Abstract_Type
(Work_Typ
) then
3794 Obj_Typ
:= Class_Wide_Type
(Work_Typ
);
3796 Obj_Typ
:= Work_Typ
;
3799 -- Perform minor decoration in case the declaration is not analyzed
3801 Mutate_Ekind
(Obj_Id
, E_In_Parameter
);
3802 Set_Etype
(Obj_Id
, Obj_Typ
);
3803 Set_Scope
(Obj_Id
, Proc_Id
);
3805 Set_First_Entity
(Proc_Id
, Obj_Id
);
3806 Set_Last_Entity
(Proc_Id
, Obj_Id
);
3809 -- procedure <Work_Typ>[Partial_]Invariant (_object : <Obj_Typ>);
3812 Make_Subprogram_Declaration
(Loc
,
3814 Make_Procedure_Specification
(Loc
,
3815 Defining_Unit_Name
=> Proc_Id
,
3816 Parameter_Specifications
=> New_List
(
3817 Make_Parameter_Specification
(Loc
,
3818 Defining_Identifier
=> Obj_Id
,
3819 Parameter_Type
=> New_Occurrence_Of
(Obj_Typ
, Loc
)))));
3821 -- The declaration should not be inserted into the tree when the context
3822 -- is a generic unit because it is not part of the template.
3824 if Inside_A_Generic
then
3827 -- Semi-insert the declaration into the tree for GNATprove by setting
3828 -- its Parent field. This allows for proper upstream tree traversals.
3830 elsif GNATprove_Mode
then
3831 Set_Parent
(Proc_Decl
, Parent
(Typ_Decl
));
3833 -- Otherwise insert the declaration
3836 pragma Assert
(Present
(Typ_Decl
));
3837 Insert_After_And_Analyze
(Typ_Decl
, Proc_Decl
);
3841 Restore_Ghost_Region
(Saved_GM
, Saved_IGR
);
3842 end Build_Invariant_Procedure_Declaration
;
3844 --------------------------
3845 -- Build_Procedure_Form --
3846 --------------------------
3848 procedure Build_Procedure_Form
(N
: Node_Id
) is
3849 Loc
: constant Source_Ptr
:= Sloc
(N
);
3850 Subp
: constant Entity_Id
:= Defining_Entity
(N
);
3852 Func_Formal
: Entity_Id
;
3853 Proc_Formals
: List_Id
;
3854 Proc_Decl
: Node_Id
;
3857 -- No action needed if this transformation was already done, or in case
3858 -- of subprogram renaming declarations.
3860 if Nkind
(Specification
(N
)) = N_Procedure_Specification
3861 or else Nkind
(N
) = N_Subprogram_Renaming_Declaration
3866 -- Ditto when dealing with an expression function, where both the
3867 -- original expression and the generated declaration end up being
3870 if Rewritten_For_C
(Subp
) then
3874 Proc_Formals
:= New_List
;
3876 -- Create a list of formal parameters with the same types as the
3879 Func_Formal
:= First_Formal
(Subp
);
3880 while Present
(Func_Formal
) loop
3881 Append_To
(Proc_Formals
,
3882 Make_Parameter_Specification
(Loc
,
3883 Defining_Identifier
=>
3884 Make_Defining_Identifier
(Loc
, Chars
(Func_Formal
)),
3886 New_Occurrence_Of
(Etype
(Func_Formal
), Loc
)));
3888 Next_Formal
(Func_Formal
);
3891 -- Add an extra out parameter to carry the function result
3893 Append_To
(Proc_Formals
,
3894 Make_Parameter_Specification
(Loc
,
3895 Defining_Identifier
=>
3896 Make_Defining_Identifier
(Loc
, Name_UP_RESULT
),
3897 Out_Present
=> True,
3898 Parameter_Type
=> New_Occurrence_Of
(Etype
(Subp
), Loc
)));
3900 -- The new procedure declaration is inserted before the function
3901 -- declaration. The processing in Build_Procedure_Body_Form relies on
3902 -- this order. Note that we insert before because in the case of a
3903 -- function body with no separate spec, we do not want to insert the
3904 -- new spec after the body which will later get rewritten.
3907 Make_Subprogram_Declaration
(Loc
,
3909 Make_Procedure_Specification
(Loc
,
3910 Defining_Unit_Name
=>
3911 Make_Defining_Identifier
(Loc
, Chars
(Subp
)),
3912 Parameter_Specifications
=> Proc_Formals
));
3914 Insert_Before_And_Analyze
(Unit_Declaration_Node
(Subp
), Proc_Decl
);
3916 -- Entity of procedure must remain invisible so that it does not
3917 -- overload subsequent references to the original function.
3919 Set_Is_Immediately_Visible
(Defining_Entity
(Proc_Decl
), False);
3921 -- Mark the function as having a procedure form and link the function
3922 -- and its internally built procedure.
3924 Set_Rewritten_For_C
(Subp
);
3925 Set_Corresponding_Procedure
(Subp
, Defining_Entity
(Proc_Decl
));
3926 Set_Corresponding_Function
(Defining_Entity
(Proc_Decl
), Subp
);
3927 end Build_Procedure_Form
;
3929 ------------------------
3930 -- Build_Runtime_Call --
3931 ------------------------
3933 function Build_Runtime_Call
(Loc
: Source_Ptr
; RE
: RE_Id
) return Node_Id
is
3935 -- If entity is not available, we can skip making the call (this avoids
3936 -- junk duplicated error messages in a number of cases).
3938 if not RTE_Available
(RE
) then
3939 return Make_Null_Statement
(Loc
);
3942 Make_Procedure_Call_Statement
(Loc
,
3943 Name
=> New_Occurrence_Of
(RTE
(RE
), Loc
));
3945 end Build_Runtime_Call
;
3947 ------------------------
3948 -- Build_SS_Mark_Call --
3949 ------------------------
3951 function Build_SS_Mark_Call
3953 Mark
: Entity_Id
) return Node_Id
3957 -- Mark : constant Mark_Id := SS_Mark;
3960 Make_Object_Declaration
(Loc
,
3961 Defining_Identifier
=> Mark
,
3962 Constant_Present
=> True,
3963 Object_Definition
=>
3964 New_Occurrence_Of
(RTE
(RE_Mark_Id
), Loc
),
3966 Make_Function_Call
(Loc
,
3967 Name
=> New_Occurrence_Of
(RTE
(RE_SS_Mark
), Loc
)));
3968 end Build_SS_Mark_Call
;
3970 ---------------------------
3971 -- Build_SS_Release_Call --
3972 ---------------------------
3974 function Build_SS_Release_Call
3976 Mark
: Entity_Id
) return Node_Id
3980 -- SS_Release (Mark);
3983 Make_Procedure_Call_Statement
(Loc
,
3985 New_Occurrence_Of
(RTE
(RE_SS_Release
), Loc
),
3986 Parameter_Associations
=> New_List
(
3987 New_Occurrence_Of
(Mark
, Loc
)));
3988 end Build_SS_Release_Call
;
3990 ----------------------------
3991 -- Build_Task_Array_Image --
3992 ----------------------------
3994 -- This function generates the body for a function that constructs the
3995 -- image string for a task that is an array component. The function is
3996 -- local to the init proc for the array type, and is called for each one
3997 -- of the components. The constructed image has the form of an indexed
3998 -- component, whose prefix is the outer variable of the array type.
3999 -- The n-dimensional array type has known indexes Index, Index2...
4001 -- Id_Ref is an indexed component form created by the enclosing init proc.
4002 -- Its successive indexes are Val1, Val2, ... which are the loop variables
4003 -- in the loops that call the individual task init proc on each component.
4005 -- The generated function has the following structure:
4007 -- function F return String is
4008 -- Pref : string renames Task_Name;
4009 -- T1 : String := Index1'Image (Val1);
4011 -- Tn : String := indexn'image (Valn);
4012 -- Len : Integer := T1'Length + ... + Tn'Length + n + 1;
4013 -- -- Len includes commas and the end parentheses.
4014 -- Res : String (1..Len);
4015 -- Pos : Integer := Pref'Length;
4018 -- Res (1 .. Pos) := Pref;
4020 -- Res (Pos) := '(';
4022 -- Res (Pos .. Pos + T1'Length - 1) := T1;
4023 -- Pos := Pos + T1'Length;
4024 -- Res (Pos) := '.';
4027 -- Res (Pos .. Pos + Tn'Length - 1) := Tn;
4028 -- Res (Len) := ')';
4033 -- Needless to say, multidimensional arrays of tasks are rare enough that
4034 -- the bulkiness of this code is not really a concern.
4036 function Build_Task_Array_Image
4040 Dyn
: Boolean := False) return Node_Id
4042 Dims
: constant Nat
:= Number_Dimensions
(A_Type
);
4043 -- Number of dimensions for array of tasks
4045 Temps
: array (1 .. Dims
) of Entity_Id
;
4046 -- Array of temporaries to hold string for each index
4052 -- Total length of generated name
4055 -- Running index for substring assignments
4057 Pref
: constant Entity_Id
:= Make_Temporary
(Loc
, 'P');
4058 -- Name of enclosing variable, prefix of resulting name
4061 -- String to hold result
4064 -- Value of successive indexes
4067 -- Expression to compute total size of string
4070 -- Entity for name at one index position
4072 Decls
: constant List_Id
:= New_List
;
4073 Stats
: constant List_Id
:= New_List
;
4076 -- For a dynamic task, the name comes from the target variable. For a
4077 -- static one it is a formal of the enclosing init proc.
4080 Get_Name_String
(Chars
(Entity
(Prefix
(Id_Ref
))));
4082 Make_Object_Declaration
(Loc
,
4083 Defining_Identifier
=> Pref
,
4084 Object_Definition
=> New_Occurrence_Of
(Standard_String
, Loc
),
4086 Make_String_Literal
(Loc
,
4087 Strval
=> String_From_Name_Buffer
)));
4091 Make_Object_Renaming_Declaration
(Loc
,
4092 Defining_Identifier
=> Pref
,
4093 Subtype_Mark
=> New_Occurrence_Of
(Standard_String
, Loc
),
4094 Name
=> Make_Identifier
(Loc
, Name_uTask_Name
)));
4097 Indx
:= First_Index
(A_Type
);
4098 Val
:= First
(Expressions
(Id_Ref
));
4100 for J
in 1 .. Dims
loop
4101 T
:= Make_Temporary
(Loc
, 'T');
4105 Make_Object_Declaration
(Loc
,
4106 Defining_Identifier
=> T
,
4107 Object_Definition
=> New_Occurrence_Of
(Standard_String
, Loc
),
4109 Make_Attribute_Reference
(Loc
,
4110 Attribute_Name
=> Name_Image
,
4111 Prefix
=> New_Occurrence_Of
(Etype
(Indx
), Loc
),
4112 Expressions
=> New_List
(New_Copy_Tree
(Val
)))));
4118 Sum
:= Make_Integer_Literal
(Loc
, Dims
+ 1);
4124 Make_Attribute_Reference
(Loc
,
4125 Attribute_Name
=> Name_Length
,
4126 Prefix
=> New_Occurrence_Of
(Pref
, Loc
),
4127 Expressions
=> New_List
(Make_Integer_Literal
(Loc
, 1))));
4129 for J
in 1 .. Dims
loop
4134 Make_Attribute_Reference
(Loc
,
4135 Attribute_Name
=> Name_Length
,
4137 New_Occurrence_Of
(Temps
(J
), Loc
),
4138 Expressions
=> New_List
(Make_Integer_Literal
(Loc
, 1))));
4141 Build_Task_Image_Prefix
(Loc
, Len
, Res
, Pos
, Pref
, Sum
, Decls
, Stats
);
4143 Set_Character_Literal_Name
(Char_Code
(Character'Pos ('(')));
4146 Make_Assignment_Statement
(Loc
,
4148 Make_Indexed_Component
(Loc
,
4149 Prefix
=> New_Occurrence_Of
(Res
, Loc
),
4150 Expressions
=> New_List
(New_Occurrence_Of
(Pos
, Loc
))),
4152 Make_Character_Literal
(Loc
,
4154 Char_Literal_Value
=> UI_From_Int
(Character'Pos ('(')))));
4157 Make_Assignment_Statement
(Loc
,
4158 Name
=> New_Occurrence_Of
(Pos
, Loc
),
4161 Left_Opnd
=> New_Occurrence_Of
(Pos
, Loc
),
4162 Right_Opnd
=> Make_Integer_Literal
(Loc
, 1))));
4164 for J
in 1 .. Dims
loop
4167 Make_Assignment_Statement
(Loc
,
4170 Prefix
=> New_Occurrence_Of
(Res
, Loc
),
4173 Low_Bound
=> New_Occurrence_Of
(Pos
, Loc
),
4175 Make_Op_Subtract
(Loc
,
4178 Left_Opnd
=> New_Occurrence_Of
(Pos
, Loc
),
4180 Make_Attribute_Reference
(Loc
,
4181 Attribute_Name
=> Name_Length
,
4183 New_Occurrence_Of
(Temps
(J
), Loc
),
4185 New_List
(Make_Integer_Literal
(Loc
, 1)))),
4186 Right_Opnd
=> Make_Integer_Literal
(Loc
, 1)))),
4188 Expression
=> New_Occurrence_Of
(Temps
(J
), Loc
)));
4192 Make_Assignment_Statement
(Loc
,
4193 Name
=> New_Occurrence_Of
(Pos
, Loc
),
4196 Left_Opnd
=> New_Occurrence_Of
(Pos
, Loc
),
4198 Make_Attribute_Reference
(Loc
,
4199 Attribute_Name
=> Name_Length
,
4200 Prefix
=> New_Occurrence_Of
(Temps
(J
), Loc
),
4202 New_List
(Make_Integer_Literal
(Loc
, 1))))));
4204 Set_Character_Literal_Name
(Char_Code
(Character'Pos (',')));
4207 Make_Assignment_Statement
(Loc
,
4208 Name
=> Make_Indexed_Component
(Loc
,
4209 Prefix
=> New_Occurrence_Of
(Res
, Loc
),
4210 Expressions
=> New_List
(New_Occurrence_Of
(Pos
, Loc
))),
4212 Make_Character_Literal
(Loc
,
4214 Char_Literal_Value
=> UI_From_Int
(Character'Pos (',')))));
4217 Make_Assignment_Statement
(Loc
,
4218 Name
=> New_Occurrence_Of
(Pos
, Loc
),
4221 Left_Opnd
=> New_Occurrence_Of
(Pos
, Loc
),
4222 Right_Opnd
=> Make_Integer_Literal
(Loc
, 1))));
4226 Set_Character_Literal_Name
(Char_Code
(Character'Pos (')')));
4229 Make_Assignment_Statement
(Loc
,
4231 Make_Indexed_Component
(Loc
,
4232 Prefix
=> New_Occurrence_Of
(Res
, Loc
),
4233 Expressions
=> New_List
(New_Occurrence_Of
(Len
, Loc
))),
4235 Make_Character_Literal
(Loc
,
4237 Char_Literal_Value
=> UI_From_Int
(Character'Pos (')')))));
4238 return Build_Task_Image_Function
(Loc
, Decls
, Stats
, Res
);
4239 end Build_Task_Array_Image
;
4241 ----------------------------
4242 -- Build_Task_Image_Decls --
4243 ----------------------------
4245 function Build_Task_Image_Decls
4249 In_Init_Proc
: Boolean := False) return List_Id
4251 Decls
: constant List_Id
:= New_List
;
4252 T_Id
: Entity_Id
:= Empty
;
4254 Expr
: Node_Id
:= Empty
;
4255 Fun
: Node_Id
:= Empty
;
4256 Is_Dyn
: constant Boolean :=
4257 Nkind
(Parent
(Id_Ref
)) = N_Assignment_Statement
4259 Nkind
(Expression
(Parent
(Id_Ref
))) = N_Allocator
;
4262 -- If Discard_Names or No_Implicit_Heap_Allocations are in effect,
4263 -- generate a dummy declaration only.
4265 if Restriction_Active
(No_Implicit_Heap_Allocations
)
4266 or else Global_Discard_Names
4268 T_Id
:= Make_Temporary
(Loc
, 'J');
4273 Make_Object_Declaration
(Loc
,
4274 Defining_Identifier
=> T_Id
,
4275 Object_Definition
=> New_Occurrence_Of
(Standard_String
, Loc
),
4277 Make_String_Literal
(Loc
,
4278 Strval
=> String_From_Name_Buffer
)));
4281 if Nkind
(Id_Ref
) = N_Identifier
4282 or else Nkind
(Id_Ref
) = N_Defining_Identifier
4284 -- For a simple variable, the image of the task is built from
4285 -- the name of the variable. To avoid possible conflict with the
4286 -- anonymous type created for a single protected object, add a
4290 Make_Defining_Identifier
(Loc
,
4291 New_External_Name
(Chars
(Id_Ref
), 'T', 1));
4293 Get_Name_String
(Chars
(Id_Ref
));
4296 Make_String_Literal
(Loc
,
4297 Strval
=> String_From_Name_Buffer
);
4299 elsif Nkind
(Id_Ref
) = N_Selected_Component
then
4301 Make_Defining_Identifier
(Loc
,
4302 New_External_Name
(Chars
(Selector_Name
(Id_Ref
)), 'T'));
4303 Fun
:= Build_Task_Record_Image
(Loc
, Id_Ref
, Is_Dyn
);
4305 elsif Nkind
(Id_Ref
) = N_Indexed_Component
then
4307 Make_Defining_Identifier
(Loc
,
4308 New_External_Name
(Chars
(A_Type
), 'N'));
4310 Fun
:= Build_Task_Array_Image
(Loc
, Id_Ref
, A_Type
, Is_Dyn
);
4314 if Present
(Fun
) then
4315 Append
(Fun
, Decls
);
4316 Expr
:= Make_Function_Call
(Loc
,
4317 Name
=> New_Occurrence_Of
(Defining_Entity
(Fun
), Loc
));
4319 if not In_Init_Proc
then
4320 Set_Uses_Sec_Stack
(Defining_Entity
(Fun
));
4324 Decl
:= Make_Object_Declaration
(Loc
,
4325 Defining_Identifier
=> T_Id
,
4326 Object_Definition
=> New_Occurrence_Of
(Standard_String
, Loc
),
4327 Constant_Present
=> True,
4328 Expression
=> Expr
);
4330 Append
(Decl
, Decls
);
4332 end Build_Task_Image_Decls
;
4334 -------------------------------
4335 -- Build_Task_Image_Function --
4336 -------------------------------
4338 function Build_Task_Image_Function
4342 Res
: Entity_Id
) return Node_Id
4348 Make_Simple_Return_Statement
(Loc
,
4349 Expression
=> New_Occurrence_Of
(Res
, Loc
)));
4351 Spec
:= Make_Function_Specification
(Loc
,
4352 Defining_Unit_Name
=> Make_Temporary
(Loc
, 'F'),
4353 Result_Definition
=> New_Occurrence_Of
(Standard_String
, Loc
));
4355 -- Calls to 'Image use the secondary stack, which must be cleaned up
4356 -- after the task name is built.
4358 return Make_Subprogram_Body
(Loc
,
4359 Specification
=> Spec
,
4360 Declarations
=> Decls
,
4361 Handled_Statement_Sequence
=>
4362 Make_Handled_Sequence_Of_Statements
(Loc
, Statements
=> Stats
));
4363 end Build_Task_Image_Function
;
4365 -----------------------------
4366 -- Build_Task_Image_Prefix --
4367 -----------------------------
4369 procedure Build_Task_Image_Prefix
4371 Len
: out Entity_Id
;
4372 Res
: out Entity_Id
;
4373 Pos
: out Entity_Id
;
4380 Len
:= Make_Temporary
(Loc
, 'L', Sum
);
4383 Make_Object_Declaration
(Loc
,
4384 Defining_Identifier
=> Len
,
4385 Object_Definition
=> New_Occurrence_Of
(Standard_Integer
, Loc
),
4386 Expression
=> Sum
));
4388 Res
:= Make_Temporary
(Loc
, 'R');
4391 Make_Object_Declaration
(Loc
,
4392 Defining_Identifier
=> Res
,
4393 Object_Definition
=>
4394 Make_Subtype_Indication
(Loc
,
4395 Subtype_Mark
=> New_Occurrence_Of
(Standard_String
, Loc
),
4397 Make_Index_Or_Discriminant_Constraint
(Loc
,
4401 Low_Bound
=> Make_Integer_Literal
(Loc
, 1),
4402 High_Bound
=> New_Occurrence_Of
(Len
, Loc
)))))));
4404 -- Indicate that the result is an internal temporary, so it does not
4405 -- receive a bogus initialization when declaration is expanded. This
4406 -- is both efficient, and prevents anomalies in the handling of
4407 -- dynamic objects on the secondary stack.
4409 Set_Is_Internal
(Res
);
4410 Pos
:= Make_Temporary
(Loc
, 'P');
4413 Make_Object_Declaration
(Loc
,
4414 Defining_Identifier
=> Pos
,
4415 Object_Definition
=> New_Occurrence_Of
(Standard_Integer
, Loc
)));
4417 -- Pos := Prefix'Length;
4420 Make_Assignment_Statement
(Loc
,
4421 Name
=> New_Occurrence_Of
(Pos
, Loc
),
4423 Make_Attribute_Reference
(Loc
,
4424 Attribute_Name
=> Name_Length
,
4425 Prefix
=> New_Occurrence_Of
(Prefix
, Loc
),
4426 Expressions
=> New_List
(Make_Integer_Literal
(Loc
, 1)))));
4428 -- Res (1 .. Pos) := Prefix;
4431 Make_Assignment_Statement
(Loc
,
4434 Prefix
=> New_Occurrence_Of
(Res
, Loc
),
4437 Low_Bound
=> Make_Integer_Literal
(Loc
, 1),
4438 High_Bound
=> New_Occurrence_Of
(Pos
, Loc
))),
4440 Expression
=> New_Occurrence_Of
(Prefix
, Loc
)));
4443 Make_Assignment_Statement
(Loc
,
4444 Name
=> New_Occurrence_Of
(Pos
, Loc
),
4447 Left_Opnd
=> New_Occurrence_Of
(Pos
, Loc
),
4448 Right_Opnd
=> Make_Integer_Literal
(Loc
, 1))));
4449 end Build_Task_Image_Prefix
;
4451 -----------------------------
4452 -- Build_Task_Record_Image --
4453 -----------------------------
4455 function Build_Task_Record_Image
4458 Dyn
: Boolean := False) return Node_Id
4461 -- Total length of generated name
4464 -- Index into result
4467 -- String to hold result
4469 Pref
: constant Entity_Id
:= Make_Temporary
(Loc
, 'P');
4470 -- Name of enclosing variable, prefix of resulting name
4473 -- Expression to compute total size of string
4476 -- Entity for selector name
4478 Decls
: constant List_Id
:= New_List
;
4479 Stats
: constant List_Id
:= New_List
;
4482 -- For a dynamic task, the name comes from the target variable. For a
4483 -- static one it is a formal of the enclosing init proc.
4486 Get_Name_String
(Chars
(Entity
(Prefix
(Id_Ref
))));
4488 Make_Object_Declaration
(Loc
,
4489 Defining_Identifier
=> Pref
,
4490 Object_Definition
=> New_Occurrence_Of
(Standard_String
, Loc
),
4492 Make_String_Literal
(Loc
,
4493 Strval
=> String_From_Name_Buffer
)));
4497 Make_Object_Renaming_Declaration
(Loc
,
4498 Defining_Identifier
=> Pref
,
4499 Subtype_Mark
=> New_Occurrence_Of
(Standard_String
, Loc
),
4500 Name
=> Make_Identifier
(Loc
, Name_uTask_Name
)));
4503 Sel
:= Make_Temporary
(Loc
, 'S');
4505 Get_Name_String
(Chars
(Selector_Name
(Id_Ref
)));
4508 Make_Object_Declaration
(Loc
,
4509 Defining_Identifier
=> Sel
,
4510 Object_Definition
=> New_Occurrence_Of
(Standard_String
, Loc
),
4512 Make_String_Literal
(Loc
,
4513 Strval
=> String_From_Name_Buffer
)));
4515 Sum
:= Make_Integer_Literal
(Loc
, Nat
(Name_Len
+ 1));
4521 Make_Attribute_Reference
(Loc
,
4522 Attribute_Name
=> Name_Length
,
4524 New_Occurrence_Of
(Pref
, Loc
),
4525 Expressions
=> New_List
(Make_Integer_Literal
(Loc
, 1))));
4527 Build_Task_Image_Prefix
(Loc
, Len
, Res
, Pos
, Pref
, Sum
, Decls
, Stats
);
4529 Set_Character_Literal_Name
(Char_Code
(Character'Pos ('.')));
4531 -- Res (Pos) := '.';
4534 Make_Assignment_Statement
(Loc
,
4535 Name
=> Make_Indexed_Component
(Loc
,
4536 Prefix
=> New_Occurrence_Of
(Res
, Loc
),
4537 Expressions
=> New_List
(New_Occurrence_Of
(Pos
, Loc
))),
4539 Make_Character_Literal
(Loc
,
4541 Char_Literal_Value
=>
4542 UI_From_Int
(Character'Pos ('.')))));
4545 Make_Assignment_Statement
(Loc
,
4546 Name
=> New_Occurrence_Of
(Pos
, Loc
),
4549 Left_Opnd
=> New_Occurrence_Of
(Pos
, Loc
),
4550 Right_Opnd
=> Make_Integer_Literal
(Loc
, 1))));
4552 -- Res (Pos .. Len) := Selector;
4555 Make_Assignment_Statement
(Loc
,
4556 Name
=> Make_Slice
(Loc
,
4557 Prefix
=> New_Occurrence_Of
(Res
, Loc
),
4560 Low_Bound
=> New_Occurrence_Of
(Pos
, Loc
),
4561 High_Bound
=> New_Occurrence_Of
(Len
, Loc
))),
4562 Expression
=> New_Occurrence_Of
(Sel
, Loc
)));
4564 return Build_Task_Image_Function
(Loc
, Decls
, Stats
, Res
);
4565 end Build_Task_Record_Image
;
4567 ---------------------------------------
4568 -- Build_Transient_Object_Statements --
4569 ---------------------------------------
4571 procedure Build_Transient_Object_Statements
4572 (Obj_Decl
: Node_Id
;
4573 Fin_Call
: out Node_Id
;
4574 Hook_Assign
: out Node_Id
;
4575 Hook_Clear
: out Node_Id
;
4576 Hook_Decl
: out Node_Id
;
4577 Ptr_Decl
: out Node_Id
;
4578 Finalize_Obj
: Boolean := True)
4580 Loc
: constant Source_Ptr
:= Sloc
(Obj_Decl
);
4581 Obj_Id
: constant Entity_Id
:= Defining_Entity
(Obj_Decl
);
4582 Obj_Typ
: constant Entity_Id
:= Base_Type
(Etype
(Obj_Id
));
4584 Desig_Typ
: Entity_Id
;
4585 Hook_Expr
: Node_Id
;
4586 Hook_Id
: Entity_Id
;
4588 Ptr_Typ
: Entity_Id
;
4591 -- Recover the type of the object
4593 Desig_Typ
:= Obj_Typ
;
4595 if Is_Access_Type
(Desig_Typ
) then
4596 Desig_Typ
:= Available_View
(Designated_Type
(Desig_Typ
));
4599 -- Create an access type which provides a reference to the transient
4600 -- object. Generate:
4602 -- type Ptr_Typ is access all Desig_Typ;
4604 Ptr_Typ
:= Make_Temporary
(Loc
, 'A');
4605 Mutate_Ekind
(Ptr_Typ
, E_General_Access_Type
);
4606 Set_Directly_Designated_Type
(Ptr_Typ
, Desig_Typ
);
4609 Make_Full_Type_Declaration
(Loc
,
4610 Defining_Identifier
=> Ptr_Typ
,
4612 Make_Access_To_Object_Definition
(Loc
,
4613 All_Present
=> True,
4614 Subtype_Indication
=> New_Occurrence_Of
(Desig_Typ
, Loc
)));
4616 -- Create a temporary check which acts as a hook to the transient
4617 -- object. Generate:
4619 -- Hook : Ptr_Typ := null;
4621 Hook_Id
:= Make_Temporary
(Loc
, 'T');
4622 Mutate_Ekind
(Hook_Id
, E_Variable
);
4623 Set_Etype
(Hook_Id
, Ptr_Typ
);
4626 Make_Object_Declaration
(Loc
,
4627 Defining_Identifier
=> Hook_Id
,
4628 Object_Definition
=> New_Occurrence_Of
(Ptr_Typ
, Loc
),
4629 Expression
=> Make_Null
(Loc
));
4631 -- Mark the temporary as a hook. This signals the machinery in
4632 -- Build_Finalizer to recognize this special case.
4634 Set_Status_Flag_Or_Transient_Decl
(Hook_Id
, Obj_Decl
);
4636 -- Hook the transient object to the temporary. Generate:
4638 -- Hook := Ptr_Typ (Obj_Id);
4640 -- Hool := Obj_Id'Unrestricted_Access;
4642 if Is_Access_Type
(Obj_Typ
) then
4644 Unchecked_Convert_To
(Ptr_Typ
, New_Occurrence_Of
(Obj_Id
, Loc
));
4647 Make_Attribute_Reference
(Loc
,
4648 Prefix
=> New_Occurrence_Of
(Obj_Id
, Loc
),
4649 Attribute_Name
=> Name_Unrestricted_Access
);
4653 Make_Assignment_Statement
(Loc
,
4654 Name
=> New_Occurrence_Of
(Hook_Id
, Loc
),
4655 Expression
=> Hook_Expr
);
4657 -- Crear the hook prior to finalizing the object. Generate:
4662 Make_Assignment_Statement
(Loc
,
4663 Name
=> New_Occurrence_Of
(Hook_Id
, Loc
),
4664 Expression
=> Make_Null
(Loc
));
4666 -- Finalize the object. Generate:
4668 -- [Deep_]Finalize (Obj_Ref[.all]);
4670 if Finalize_Obj
then
4671 Obj_Ref
:= New_Occurrence_Of
(Obj_Id
, Loc
);
4673 if Is_Access_Type
(Obj_Typ
) then
4674 Obj_Ref
:= Make_Explicit_Dereference
(Loc
, Obj_Ref
);
4675 Set_Etype
(Obj_Ref
, Desig_Typ
);
4680 (Obj_Ref
=> Obj_Ref
,
4683 -- Otherwise finalize the hook. Generate:
4685 -- [Deep_]Finalize (Hook.all);
4691 Make_Explicit_Dereference
(Loc
,
4692 Prefix
=> New_Occurrence_Of
(Hook_Id
, Loc
)),
4695 end Build_Transient_Object_Statements
;
4697 -----------------------------
4698 -- Check_Float_Op_Overflow --
4699 -----------------------------
4701 procedure Check_Float_Op_Overflow
(N
: Node_Id
) is
4703 -- Return if no check needed
4705 if not Is_Floating_Point_Type
(Etype
(N
))
4706 or else not (Do_Overflow_Check
(N
) and then Check_Float_Overflow
)
4708 -- In CodePeer_Mode, rely on the overflow check flag being set instead
4709 -- and do not expand the code for float overflow checking.
4711 or else CodePeer_Mode
4716 -- Otherwise we replace the expression by
4718 -- do Tnn : constant ftype := expression;
4719 -- constraint_error when not Tnn'Valid;
4723 Loc
: constant Source_Ptr
:= Sloc
(N
);
4724 Tnn
: constant Entity_Id
:= Make_Temporary
(Loc
, 'T', N
);
4725 Typ
: constant Entity_Id
:= Etype
(N
);
4728 -- Turn off the Do_Overflow_Check flag, since we are doing that work
4729 -- right here. We also set the node as analyzed to prevent infinite
4730 -- recursion from repeating the operation in the expansion.
4732 Set_Do_Overflow_Check
(N
, False);
4733 Set_Analyzed
(N
, True);
4735 -- Do the rewrite to include the check
4738 Make_Expression_With_Actions
(Loc
,
4739 Actions
=> New_List
(
4740 Make_Object_Declaration
(Loc
,
4741 Defining_Identifier
=> Tnn
,
4742 Object_Definition
=> New_Occurrence_Of
(Typ
, Loc
),
4743 Constant_Present
=> True,
4744 Expression
=> Relocate_Node
(N
)),
4745 Make_Raise_Constraint_Error
(Loc
,
4749 Make_Attribute_Reference
(Loc
,
4750 Prefix
=> New_Occurrence_Of
(Tnn
, Loc
),
4751 Attribute_Name
=> Name_Valid
)),
4752 Reason
=> CE_Overflow_Check_Failed
)),
4753 Expression
=> New_Occurrence_Of
(Tnn
, Loc
)));
4755 Analyze_And_Resolve
(N
, Typ
);
4757 end Check_Float_Op_Overflow
;
4759 ----------------------------------
4760 -- Component_May_Be_Bit_Aligned --
4761 ----------------------------------
4763 function Component_May_Be_Bit_Aligned
(Comp
: Entity_Id
) return Boolean is
4767 -- If no component clause, then everything is fine, since the back end
4768 -- never misaligns from byte boundaries by default, even if there is a
4769 -- pragma Pack for the record.
4771 if No
(Comp
) or else No
(Component_Clause
(Comp
)) then
4775 UT
:= Underlying_Type
(Etype
(Comp
));
4777 -- It is only array and record types that cause trouble
4779 if not Is_Record_Type
(UT
) and then not Is_Array_Type
(UT
) then
4782 -- If we know that we have a small (at most the maximum integer size)
4783 -- record or bit-packed array, then everything is fine, since the back
4784 -- end can handle these cases correctly.
4786 elsif Known_Esize
(Comp
)
4787 and then Esize
(Comp
) <= System_Max_Integer_Size
4788 and then (Is_Record_Type
(UT
) or else Is_Bit_Packed_Array
(UT
))
4792 elsif not Known_Normalized_First_Bit
(Comp
) then
4795 -- Otherwise if the component is not byte aligned, we know we have the
4796 -- nasty unaligned case.
4798 elsif Normalized_First_Bit
(Comp
) /= Uint_0
4799 or else Esize
(Comp
) mod System_Storage_Unit
/= Uint_0
4803 -- If we are large and byte aligned, then OK at this level
4808 end Component_May_Be_Bit_Aligned
;
4810 -------------------------------
4811 -- Convert_To_Actual_Subtype --
4812 -------------------------------
4814 procedure Convert_To_Actual_Subtype
(Exp
: Node_Id
) is
4818 Act_ST
:= Get_Actual_Subtype
(Exp
);
4820 if Act_ST
= Etype
(Exp
) then
4823 Rewrite
(Exp
, Convert_To
(Act_ST
, Relocate_Node
(Exp
)));
4824 Analyze_And_Resolve
(Exp
, Act_ST
);
4826 end Convert_To_Actual_Subtype
;
4828 -----------------------------------
4829 -- Corresponding_Runtime_Package --
4830 -----------------------------------
4832 function Corresponding_Runtime_Package
(Typ
: Entity_Id
) return RTU_Id
is
4833 function Has_One_Entry_And_No_Queue
(T
: Entity_Id
) return Boolean;
4834 -- Return True if protected type T has one entry and the maximum queue
4837 --------------------------------
4838 -- Has_One_Entry_And_No_Queue --
4839 --------------------------------
4841 function Has_One_Entry_And_No_Queue
(T
: Entity_Id
) return Boolean is
4843 Is_First
: Boolean := True;
4846 Item
:= First_Entity
(T
);
4847 while Present
(Item
) loop
4848 if Is_Entry
(Item
) then
4850 -- The protected type has more than one entry
4852 if not Is_First
then
4856 -- The queue length is not one
4858 if not Restriction_Active
(No_Entry_Queue
)
4859 and then Get_Max_Queue_Length
(Item
) /= Uint_1
4871 end Has_One_Entry_And_No_Queue
;
4875 Pkg_Id
: RTU_Id
:= RTU_Null
;
4877 -- Start of processing for Corresponding_Runtime_Package
4880 pragma Assert
(Is_Concurrent_Type
(Typ
));
4882 if Is_Protected_Type
(Typ
) then
4883 if Has_Entries
(Typ
)
4885 -- A protected type without entries that covers an interface and
4886 -- overrides the abstract routines with protected procedures is
4887 -- considered equivalent to a protected type with entries in the
4888 -- context of dispatching select statements. It is sufficient to
4889 -- check for the presence of an interface list in the declaration
4890 -- node to recognize this case.
4892 or else Present
(Interface_List
(Parent
(Typ
)))
4894 -- Protected types with interrupt handlers (when not using a
4895 -- restricted profile) are also considered equivalent to
4896 -- protected types with entries. The types which are used
4897 -- (Static_Interrupt_Protection and Dynamic_Interrupt_Protection)
4898 -- are derived from Protection_Entries.
4900 or else (Has_Attach_Handler
(Typ
) and then not Restricted_Profile
)
4901 or else Has_Interrupt_Handler
(Typ
)
4904 or else Restriction_Active
(No_Select_Statements
) = False
4905 or else not Has_One_Entry_And_No_Queue
(Typ
)
4906 or else (Has_Attach_Handler
(Typ
)
4907 and then not Restricted_Profile
)
4909 Pkg_Id
:= System_Tasking_Protected_Objects_Entries
;
4911 Pkg_Id
:= System_Tasking_Protected_Objects_Single_Entry
;
4915 Pkg_Id
:= System_Tasking_Protected_Objects
;
4920 end Corresponding_Runtime_Package
;
4922 -----------------------------------
4923 -- Current_Sem_Unit_Declarations --
4924 -----------------------------------
4926 function Current_Sem_Unit_Declarations
return List_Id
is
4927 U
: Node_Id
:= Unit
(Cunit
(Current_Sem_Unit
));
4931 -- If the current unit is a package body, locate the visible
4932 -- declarations of the package spec.
4934 if Nkind
(U
) = N_Package_Body
then
4935 U
:= Unit
(Library_Unit
(Cunit
(Current_Sem_Unit
)));
4938 if Nkind
(U
) = N_Package_Declaration
then
4939 U
:= Specification
(U
);
4940 Decls
:= Visible_Declarations
(U
);
4944 Set_Visible_Declarations
(U
, Decls
);
4948 Decls
:= Declarations
(U
);
4952 Set_Declarations
(U
, Decls
);
4957 end Current_Sem_Unit_Declarations
;
4959 -----------------------
4960 -- Duplicate_Subexpr --
4961 -----------------------
4963 function Duplicate_Subexpr
4965 Name_Req
: Boolean := False;
4966 Renaming_Req
: Boolean := False) return Node_Id
4969 Remove_Side_Effects
(Exp
, Name_Req
, Renaming_Req
);
4970 return New_Copy_Tree
(Exp
);
4971 end Duplicate_Subexpr
;
4973 ---------------------------------
4974 -- Duplicate_Subexpr_No_Checks --
4975 ---------------------------------
4977 function Duplicate_Subexpr_No_Checks
4979 Name_Req
: Boolean := False;
4980 Renaming_Req
: Boolean := False;
4981 Related_Id
: Entity_Id
:= Empty
;
4982 Is_Low_Bound
: Boolean := False;
4983 Is_High_Bound
: Boolean := False) return Node_Id
4990 Name_Req
=> Name_Req
,
4991 Renaming_Req
=> Renaming_Req
,
4992 Related_Id
=> Related_Id
,
4993 Is_Low_Bound
=> Is_Low_Bound
,
4994 Is_High_Bound
=> Is_High_Bound
);
4996 New_Exp
:= New_Copy_Tree
(Exp
);
4997 Remove_Checks
(New_Exp
);
4999 end Duplicate_Subexpr_No_Checks
;
5001 -----------------------------------
5002 -- Duplicate_Subexpr_Move_Checks --
5003 -----------------------------------
5005 function Duplicate_Subexpr_Move_Checks
5007 Name_Req
: Boolean := False;
5008 Renaming_Req
: Boolean := False) return Node_Id
5013 Remove_Side_Effects
(Exp
, Name_Req
, Renaming_Req
);
5014 New_Exp
:= New_Copy_Tree
(Exp
);
5015 Remove_Checks
(Exp
);
5017 end Duplicate_Subexpr_Move_Checks
;
5019 -------------------------
5020 -- Enclosing_Init_Proc --
5021 -------------------------
5023 function Enclosing_Init_Proc
return Entity_Id
is
5028 while Present
(S
) and then S
/= Standard_Standard
loop
5029 if Is_Init_Proc
(S
) then
5037 end Enclosing_Init_Proc
;
5039 --------------------
5040 -- Ensure_Defined --
5041 --------------------
5043 procedure Ensure_Defined
(Typ
: Entity_Id
; N
: Node_Id
) is
5047 -- An itype reference must only be created if this is a local itype, so
5048 -- that gigi can elaborate it on the proper objstack.
5050 if Is_Itype
(Typ
) and then Scope
(Typ
) = Current_Scope
then
5051 IR
:= Make_Itype_Reference
(Sloc
(N
));
5052 Set_Itype
(IR
, Typ
);
5053 Insert_Action
(N
, IR
);
5057 --------------------
5058 -- Entry_Names_OK --
5059 --------------------
5061 function Entry_Names_OK
return Boolean is
5064 not Restricted_Profile
5065 and then not Global_Discard_Names
5066 and then not Restriction_Active
(No_Implicit_Heap_Allocations
)
5067 and then not Restriction_Active
(No_Local_Allocators
);
5074 procedure Evaluate_Name
(Nam
: Node_Id
) is
5077 -- For an aggregate, force its evaluation
5080 Force_Evaluation
(Nam
);
5082 -- For an attribute reference or an indexed component, evaluate the
5083 -- prefix, which is itself a name, recursively, and then force the
5084 -- evaluation of all the subscripts (or attribute expressions).
5086 when N_Attribute_Reference
5087 | N_Indexed_Component
5089 Evaluate_Name
(Prefix
(Nam
));
5095 E
:= First
(Expressions
(Nam
));
5096 while Present
(E
) loop
5097 Force_Evaluation
(E
);
5099 if Is_Rewrite_Substitution
(E
) then
5101 (E
, Do_Range_Check
(Original_Node
(E
)));
5108 -- For an explicit dereference, we simply force the evaluation of
5109 -- the name expression. The dereference provides a value that is the
5110 -- address for the renamed object, and it is precisely this value
5111 -- that we want to preserve.
5113 when N_Explicit_Dereference
=>
5114 Force_Evaluation
(Prefix
(Nam
));
5116 -- For a function call, we evaluate the call; same for an operator
5118 when N_Function_Call
5121 Force_Evaluation
(Nam
);
5123 -- For a qualified expression, we evaluate the expression
5125 when N_Qualified_Expression
=>
5126 Evaluate_Name
(Expression
(Nam
));
5128 -- For a selected component, we simply evaluate the prefix
5130 when N_Selected_Component
=>
5131 Evaluate_Name
(Prefix
(Nam
));
5133 -- For a slice, we evaluate the prefix, as for the indexed component
5134 -- case and then, if there is a range present, either directly or as
5135 -- the constraint of a discrete subtype indication, we evaluate the
5136 -- two bounds of this range.
5139 Evaluate_Name
(Prefix
(Nam
));
5140 Evaluate_Slice_Bounds
(Nam
);
5142 -- For a type conversion, the expression of the conversion must be
5143 -- the name of an object, and we simply need to evaluate this name.
5145 when N_Type_Conversion
=>
5146 Evaluate_Name
(Expression
(Nam
));
5148 -- The remaining cases are direct name and character literal. In all
5149 -- these cases, we do nothing, since we want to reevaluate each time
5150 -- the renamed object is used. ??? There are more remaining cases, at
5151 -- least in the GNATprove_Mode, where this routine is called in more
5152 -- contexts than in GNAT.
5159 ---------------------------
5160 -- Evaluate_Slice_Bounds --
5161 ---------------------------
5163 procedure Evaluate_Slice_Bounds
(Slice
: Node_Id
) is
5164 DR
: constant Node_Id
:= Discrete_Range
(Slice
);
5169 if Nkind
(DR
) = N_Range
then
5170 Force_Evaluation
(Low_Bound
(DR
));
5171 Force_Evaluation
(High_Bound
(DR
));
5173 elsif Nkind
(DR
) = N_Subtype_Indication
then
5174 Constr
:= Constraint
(DR
);
5176 if Nkind
(Constr
) = N_Range_Constraint
then
5177 Rexpr
:= Range_Expression
(Constr
);
5179 Force_Evaluation
(Low_Bound
(Rexpr
));
5180 Force_Evaluation
(High_Bound
(Rexpr
));
5183 end Evaluate_Slice_Bounds
;
5185 ---------------------
5186 -- Evolve_And_Then --
5187 ---------------------
5189 procedure Evolve_And_Then
(Cond
: in out Node_Id
; Cond1
: Node_Id
) is
5195 Make_And_Then
(Sloc
(Cond1
),
5197 Right_Opnd
=> Cond1
);
5199 end Evolve_And_Then
;
5201 --------------------
5202 -- Evolve_Or_Else --
5203 --------------------
5205 procedure Evolve_Or_Else
(Cond
: in out Node_Id
; Cond1
: Node_Id
) is
5211 Make_Or_Else
(Sloc
(Cond1
),
5213 Right_Opnd
=> Cond1
);
5217 -------------------------------
5218 -- Expand_Sliding_Conversion --
5219 -------------------------------
5221 procedure Expand_Sliding_Conversion
(N
: Node_Id
; Arr_Typ
: Entity_Id
) is
5223 pragma Assert
(Is_Array_Type
(Arr_Typ
)
5224 and then not Is_Constrained
(Arr_Typ
)
5225 and then Is_Fixed_Lower_Bound_Array_Subtype
(Arr_Typ
));
5227 Constraints
: List_Id
;
5228 Index
: Node_Id
:= First_Index
(Arr_Typ
);
5229 Loc
: constant Source_Ptr
:= Sloc
(N
);
5230 Subt_Decl
: Node_Id
;
5233 Subt_High
: Node_Id
;
5235 Act_Subt
: Entity_Id
;
5236 Act_Index
: Node_Id
;
5239 Adjust_Incr
: Node_Id
;
5240 Dimension
: Int
:= 0;
5241 All_FLBs_Match
: Boolean := True;
5244 -- This procedure is called during semantic analysis, and we only expand
5245 -- a sliding conversion when Expander_Active, to avoid doing it during
5246 -- preanalysis (which can lead to problems with the target subtype not
5247 -- getting properly expanded during later full analysis). Also, sliding
5248 -- should never be needed for string literals, because their bounds are
5249 -- determined directly based on the fixed lower bound of Arr_Typ and
5252 if Expander_Active
and then Nkind
(N
) /= N_String_Literal
then
5253 Constraints
:= New_List
;
5255 Act_Subt
:= Get_Actual_Subtype
(N
);
5256 Act_Index
:= First_Index
(Act_Subt
);
5258 -- Loop over the indexes of the fixed-lower-bound array type or
5259 -- subtype to build up an index constraint for constructing the
5260 -- subtype that will be the target of a conversion of the array
5261 -- object that may need a sliding conversion.
5263 while Present
(Index
) loop
5264 pragma Assert
(Present
(Act_Index
));
5266 Dimension
:= Dimension
+ 1;
5268 Get_Index_Bounds
(Act_Index
, Act_Low
, Act_High
);
5270 -- If Index defines a normal unconstrained range (range <>),
5271 -- then we will simply use the bounds of the actual subtype's
5272 -- corresponding index range.
5274 if not Is_Fixed_Lower_Bound_Index_Subtype
(Etype
(Index
)) then
5275 Subt_Low
:= Act_Low
;
5276 Subt_High
:= Act_High
;
5278 -- Otherwise, a range will be created with a low bound given by
5279 -- the fixed lower bound of the array subtype's index, and with
5280 -- high bound given by (Actual'Length + fixed lower bound - 1).
5283 if Nkind
(Index
) = N_Subtype_Indication
then
5286 (Low_Bound
(Range_Expression
(Constraint
(Index
))));
5288 pragma Assert
(Nkind
(Index
) = N_Range
);
5290 Subt_Low
:= New_Copy_Tree
(Low_Bound
(Index
));
5293 -- If either we have a nonstatic lower bound, or the target and
5294 -- source subtypes are statically known to have unequal lower
5295 -- bounds, then we will need to make a subtype conversion to
5296 -- slide the bounds. However, if all of the indexes' lower
5297 -- bounds are static and known to be equal (the common case),
5298 -- then no conversion will be needed, and we'll end up not
5299 -- creating the subtype or the conversion (though we still
5300 -- build up the index constraint, which will simply be unused).
5302 if not (Compile_Time_Known_Value
(Subt_Low
)
5303 and then Compile_Time_Known_Value
(Act_Low
))
5304 or else Expr_Value
(Subt_Low
) /= Expr_Value
(Act_Low
)
5306 All_FLBs_Match
:= False;
5309 -- Apply 'Pos to lower bound, which may be of an enumeration
5310 -- type, before subtracting.
5313 Make_Op_Subtract
(Loc
,
5314 Make_Attribute_Reference
(Loc
,
5316 New_Occurrence_Of
(Etype
(Act_Index
), Loc
),
5320 New_List
(New_Copy_Tree
(Subt_Low
))),
5321 Make_Integer_Literal
(Loc
, 1));
5323 -- Apply 'Val to the result of adding the increment to the
5324 -- length, to handle indexes of enumeration types.
5327 Make_Attribute_Reference
(Loc
,
5329 New_Occurrence_Of
(Etype
(Act_Index
), Loc
),
5333 New_List
(Make_Op_Add
(Loc
,
5334 Make_Attribute_Reference
(Loc
,
5336 New_Occurrence_Of
(Act_Subt
, Loc
),
5341 (Make_Integer_Literal
5346 Append
(Make_Range
(Loc
, Subt_Low
, Subt_High
), Constraints
);
5352 -- If for each index with a fixed lower bound (FLB), the lower bound
5353 -- of the corresponding index of the actual subtype is statically
5354 -- known be equal to the FLB, then a sliding conversion isn't needed
5355 -- at all, so just return without building a subtype or conversion.
5357 if All_FLBs_Match
then
5361 -- A sliding conversion is needed, so create the target subtype using
5362 -- the index constraint created above, and rewrite the expression
5363 -- as a conversion to that subtype.
5365 Subt
:= Make_Temporary
(Loc
, 'S', Related_Node
=> N
);
5366 Set_Is_Internal
(Subt
);
5369 Make_Subtype_Declaration
(Loc
,
5370 Defining_Identifier
=> Subt
,
5371 Subtype_Indication
=>
5372 Make_Subtype_Indication
(Loc
,
5374 New_Occurrence_Of
(Arr_Typ
, Loc
),
5376 Make_Index_Or_Discriminant_Constraint
(Loc
,
5377 Constraints
=> Constraints
)));
5379 Mark_Rewrite_Insertion
(Subt_Decl
);
5381 -- The actual subtype is an Itype, so we analyze the declaration,
5382 -- but do not attach it to the tree.
5384 Set_Parent
(Subt_Decl
, N
);
5385 Set_Is_Itype
(Subt
);
5386 Analyze
(Subt_Decl
, Suppress
=> All_Checks
);
5387 Set_Associated_Node_For_Itype
(Subt
, N
);
5388 Set_Has_Delayed_Freeze
(Subt
, False);
5390 -- We need to freeze the actual subtype immediately. This is needed
5391 -- because otherwise this Itype will not get frozen at all, and it is
5392 -- always safe to freeze on creation because any associated types
5393 -- must be frozen at this point.
5395 Freeze_Itype
(Subt
, N
);
5398 Make_Type_Conversion
(Loc
,
5400 New_Occurrence_Of
(Subt
, Loc
),
5401 Expression
=> Relocate_Node
(N
)));
5404 end Expand_Sliding_Conversion
;
5406 -----------------------------------------
5407 -- Expand_Static_Predicates_In_Choices --
5408 -----------------------------------------
5410 procedure Expand_Static_Predicates_In_Choices
(N
: Node_Id
) is
5411 pragma Assert
(Nkind
(N
) in N_Case_Statement_Alternative | N_Variant
);
5413 Choices
: List_Id
:= Discrete_Choices
(N
);
5421 -- If this is an "others" alternative, we need to process any static
5422 -- predicates in its Others_Discrete_Choices.
5424 if Nkind
(First
(Choices
)) = N_Others_Choice
then
5425 Choices
:= Others_Discrete_Choices
(First
(Choices
));
5428 Choice
:= First
(Choices
);
5429 while Present
(Choice
) loop
5430 Next_C
:= Next
(Choice
);
5432 -- Check for name of subtype with static predicate
5434 if Is_Entity_Name
(Choice
)
5435 and then Is_Type
(Entity
(Choice
))
5436 and then Has_Predicates
(Entity
(Choice
))
5438 -- Loop through entries in predicate list, converting to choices
5439 -- and inserting in the list before the current choice. Note that
5440 -- if the list is empty, corresponding to a False predicate, then
5441 -- no choices are inserted.
5443 P
:= First
(Static_Discrete_Predicate
(Entity
(Choice
)));
5444 while Present
(P
) loop
5446 -- If low bound and high bounds are equal, copy simple choice
5448 if Expr_Value
(Low_Bound
(P
)) = Expr_Value
(High_Bound
(P
)) then
5449 C
:= New_Copy
(Low_Bound
(P
));
5451 -- Otherwise copy a range
5457 -- Change Sloc to referencing choice (rather than the Sloc of
5458 -- the predicate declaration element itself).
5460 Set_Sloc
(C
, Sloc
(Choice
));
5461 Insert_Before
(Choice
, C
);
5465 -- Delete the predicated entry
5470 -- Move to next choice to check
5475 Set_Has_SP_Choice
(N
, False);
5476 end Expand_Static_Predicates_In_Choices
;
5478 ------------------------------
5479 -- Expand_Subtype_From_Expr --
5480 ------------------------------
5482 -- This function is applicable for both static and dynamic allocation of
5483 -- objects which are constrained by an initial expression. Basically it
5484 -- transforms an unconstrained subtype indication into a constrained one.
5486 -- The expression may also be transformed in certain cases in order to
5487 -- avoid multiple evaluation. In the static allocation case, the general
5492 -- is transformed into
5494 -- Val : Constrained_Subtype_Of_T := Maybe_Modified_Expr;
5496 -- Here are the main cases :
5498 -- <if Expr is a Slice>
5499 -- Val : T ([Index_Subtype (Expr)]) := Expr;
5501 -- <elsif Expr is a String Literal>
5502 -- Val : T (T'First .. T'First + Length (string literal) - 1) := Expr;
5504 -- <elsif Expr is Constrained>
5505 -- subtype T is Type_Of_Expr
5508 -- <elsif Expr is an entity_name>
5509 -- Val : T (constraints taken from Expr) := Expr;
5512 -- type Axxx is access all T;
5513 -- Rval : Axxx := Expr'ref;
5514 -- Val : T (constraints taken from Rval) := Rval.all;
5516 -- ??? note: when the Expression is allocated in the secondary stack
5517 -- we could use it directly instead of copying it by declaring
5518 -- Val : T (...) renames Rval.all
5520 procedure Expand_Subtype_From_Expr
5522 Unc_Type
: Entity_Id
;
5523 Subtype_Indic
: Node_Id
;
5525 Related_Id
: Entity_Id
:= Empty
)
5527 Loc
: constant Source_Ptr
:= Sloc
(N
);
5528 Exp_Typ
: constant Entity_Id
:= Etype
(Exp
);
5532 -- In general we cannot build the subtype if expansion is disabled,
5533 -- because internal entities may not have been defined. However, to
5534 -- avoid some cascaded errors, we try to continue when the expression is
5535 -- an array (or string), because it is safe to compute the bounds. It is
5536 -- in fact required to do so even in a generic context, because there
5537 -- may be constants that depend on the bounds of a string literal, both
5538 -- standard string types and more generally arrays of characters.
5540 -- In GNATprove mode, these extra subtypes are not needed, unless Exp is
5541 -- a static expression. In that case, the subtype will be constrained
5542 -- while the original type might be unconstrained, so expanding the type
5543 -- is necessary both for passing legality checks in GNAT and for precise
5544 -- analysis in GNATprove.
5546 if GNATprove_Mode
and then not Is_Static_Expression
(Exp
) then
5550 if not Expander_Active
5551 and then (No
(Etype
(Exp
)) or else not Is_String_Type
(Etype
(Exp
)))
5556 if Nkind
(Exp
) = N_Slice
then
5558 Slice_Type
: constant Entity_Id
:= Etype
(First_Index
(Exp_Typ
));
5561 Rewrite
(Subtype_Indic
,
5562 Make_Subtype_Indication
(Loc
,
5563 Subtype_Mark
=> New_Occurrence_Of
(Unc_Type
, Loc
),
5565 Make_Index_Or_Discriminant_Constraint
(Loc
,
5566 Constraints
=> New_List
5567 (New_Occurrence_Of
(Slice_Type
, Loc
)))));
5569 -- This subtype indication may be used later for constraint checks
5570 -- we better make sure that if a variable was used as a bound of
5571 -- the original slice, its value is frozen.
5573 Evaluate_Slice_Bounds
(Exp
);
5576 elsif Ekind
(Exp_Typ
) = E_String_Literal_Subtype
then
5577 Rewrite
(Subtype_Indic
,
5578 Make_Subtype_Indication
(Loc
,
5579 Subtype_Mark
=> New_Occurrence_Of
(Unc_Type
, Loc
),
5581 Make_Index_Or_Discriminant_Constraint
(Loc
,
5582 Constraints
=> New_List
(
5583 Make_Literal_Range
(Loc
,
5584 Literal_Typ
=> Exp_Typ
)))));
5586 -- If the type of the expression is an internally generated type it
5587 -- may not be necessary to create a new subtype. However there are two
5588 -- exceptions: references to the current instances, and aliased array
5589 -- object declarations for which the back end has to create a template.
5591 elsif Is_Constrained
(Exp_Typ
)
5592 and then not Is_Class_Wide_Type
(Unc_Type
)
5594 (Nkind
(N
) /= N_Object_Declaration
5595 or else not Is_Entity_Name
(Expression
(N
))
5596 or else not Comes_From_Source
(Entity
(Expression
(N
)))
5597 or else not Is_Array_Type
(Exp_Typ
)
5598 or else not Aliased_Present
(N
))
5600 if Is_Itype
(Exp_Typ
) then
5602 -- Within an initialization procedure, a selected component
5603 -- denotes a component of the enclosing record, and it appears as
5604 -- an actual in a call to its own initialization procedure. If
5605 -- this component depends on the outer discriminant, we must
5606 -- generate the proper actual subtype for it.
5608 if Nkind
(Exp
) = N_Selected_Component
5609 and then Within_Init_Proc
5612 Decl
: constant Node_Id
:=
5613 Build_Actual_Subtype_Of_Component
(Exp_Typ
, Exp
);
5615 if Present
(Decl
) then
5616 Insert_Action
(N
, Decl
);
5617 T
:= Defining_Identifier
(Decl
);
5623 -- No need to generate a new subtype
5630 T
:= Make_Temporary
(Loc
, 'T');
5633 Make_Subtype_Declaration
(Loc
,
5634 Defining_Identifier
=> T
,
5635 Subtype_Indication
=> New_Occurrence_Of
(Exp_Typ
, Loc
)));
5637 -- This type is marked as an itype even though it has an explicit
5638 -- declaration since otherwise Is_Generic_Actual_Type can get
5639 -- set, resulting in the generation of spurious errors. (See
5640 -- sem_ch8.Analyze_Package_Renaming and sem_type.covers)
5643 Set_Associated_Node_For_Itype
(T
, Exp
);
5646 Rewrite
(Subtype_Indic
, New_Occurrence_Of
(T
, Loc
));
5648 -- Nothing needs to be done for private types with unknown discriminants
5649 -- if the underlying type is not an unconstrained composite type or it
5650 -- is an unchecked union.
5652 elsif Is_Private_Type
(Unc_Type
)
5653 and then Has_Unknown_Discriminants
(Unc_Type
)
5654 and then (not Is_Composite_Type
(Underlying_Type
(Unc_Type
))
5655 or else Is_Constrained
(Underlying_Type
(Unc_Type
))
5656 or else Is_Unchecked_Union
(Underlying_Type
(Unc_Type
)))
5660 -- Case of derived type with unknown discriminants where the parent type
5661 -- also has unknown discriminants.
5663 elsif Is_Record_Type
(Unc_Type
)
5664 and then not Is_Class_Wide_Type
(Unc_Type
)
5665 and then Has_Unknown_Discriminants
(Unc_Type
)
5666 and then Has_Unknown_Discriminants
(Underlying_Type
(Unc_Type
))
5668 -- Nothing to be done if no underlying record view available
5670 -- If this is a limited type derived from a type with unknown
5671 -- discriminants, do not expand either, so that subsequent expansion
5672 -- of the call can add build-in-place parameters to call.
5674 if No
(Underlying_Record_View
(Unc_Type
))
5675 or else Is_Limited_Type
(Unc_Type
)
5679 -- Otherwise use the Underlying_Record_View to create the proper
5680 -- constrained subtype for an object of a derived type with unknown
5684 Remove_Side_Effects
(Exp
);
5685 Rewrite
(Subtype_Indic
,
5686 Make_Subtype_From_Expr
(Exp
, Underlying_Record_View
(Unc_Type
)));
5689 -- Renamings of class-wide interface types require no equivalent
5690 -- constrained type declarations because we only need to reference
5691 -- the tag component associated with the interface. The same is
5692 -- presumably true for class-wide types in general, so this test
5693 -- is broadened to include all class-wide renamings, which also
5694 -- avoids cases of unbounded recursion in Remove_Side_Effects.
5695 -- (Is this really correct, or are there some cases of class-wide
5696 -- renamings that require action in this procedure???)
5699 and then Nkind
(N
) = N_Object_Renaming_Declaration
5700 and then Is_Class_Wide_Type
(Unc_Type
)
5704 -- In Ada 95 nothing to be done if the type of the expression is limited
5705 -- because in this case the expression cannot be copied, and its use can
5706 -- only be by reference.
5708 -- In Ada 2005 the context can be an object declaration whose expression
5709 -- is a function that returns in place. If the nominal subtype has
5710 -- unknown discriminants, the call still provides constraints on the
5711 -- object, and we have to create an actual subtype from it.
5713 -- If the type is class-wide, the expression is dynamically tagged and
5714 -- we do not create an actual subtype either. Ditto for an interface.
5715 -- For now this applies only if the type is immutably limited, and the
5716 -- function being called is build-in-place. This will have to be revised
5717 -- when build-in-place functions are generalized to other types.
5719 elsif Is_Limited_View
(Exp_Typ
)
5721 (Is_Class_Wide_Type
(Exp_Typ
)
5722 or else Is_Interface
(Exp_Typ
)
5723 or else not Has_Unknown_Discriminants
(Exp_Typ
)
5724 or else not Is_Composite_Type
(Unc_Type
))
5728 -- For limited objects initialized with build-in-place function calls,
5729 -- nothing to be done; otherwise we prematurely introduce an N_Reference
5730 -- node in the expression initializing the object, which breaks the
5731 -- circuitry that detects and adds the additional arguments to the
5734 elsif Is_Build_In_Place_Function_Call
(Exp
) then
5737 -- If the expression is an uninitialized aggregate, no need to build
5738 -- a subtype from the expression, because this may require the use of
5739 -- dynamic memory to create the object.
5741 elsif Is_Uninitialized_Aggregate
(Exp
, Exp_Typ
) then
5742 Rewrite
(Subtype_Indic
, New_Occurrence_Of
(Etype
(Exp
), Sloc
(N
)));
5743 if Nkind
(N
) = N_Object_Declaration
then
5744 Set_Expression
(N
, Empty
);
5745 Set_No_Initialization
(N
);
5749 Remove_Side_Effects
(Exp
);
5750 Rewrite
(Subtype_Indic
,
5751 Make_Subtype_From_Expr
(Exp
, Unc_Type
, Related_Id
));
5753 end Expand_Subtype_From_Expr
;
5755 ---------------------------------------------
5756 -- Expression_Contains_Primitives_Calls_Of --
5757 ---------------------------------------------
5759 function Expression_Contains_Primitives_Calls_Of
5761 Typ
: Entity_Id
) return Boolean
5763 U_Typ
: constant Entity_Id
:= Unique_Entity
(Typ
);
5765 Calls_OK
: Boolean := False;
5766 -- This flag is set to True when expression Expr contains at least one
5767 -- call to a nondispatching primitive function of Typ.
5769 function Search_Primitive_Calls
(N
: Node_Id
) return Traverse_Result
;
5770 -- Search for nondispatching calls to primitive functions of type Typ
5772 ----------------------------
5773 -- Search_Primitive_Calls --
5774 ----------------------------
5776 function Search_Primitive_Calls
(N
: Node_Id
) return Traverse_Result
is
5777 Disp_Typ
: Entity_Id
;
5781 -- Detect a function call that could denote a nondispatching
5782 -- primitive of the input type.
5784 if Nkind
(N
) = N_Function_Call
5785 and then Is_Entity_Name
(Name
(N
))
5787 Subp
:= Entity
(Name
(N
));
5789 -- Do not consider function calls with a controlling argument, as
5790 -- those are always dispatching calls.
5792 if Is_Dispatching_Operation
(Subp
)
5793 and then No
(Controlling_Argument
(N
))
5795 Disp_Typ
:= Find_Dispatching_Type
(Subp
);
5797 -- To qualify as a suitable primitive, the dispatching type of
5798 -- the function must be the input type.
5800 if Present
(Disp_Typ
)
5801 and then Unique_Entity
(Disp_Typ
) = U_Typ
5805 -- There is no need to continue the traversal, as one such
5814 end Search_Primitive_Calls
;
5816 procedure Search_Calls
is new Traverse_Proc
(Search_Primitive_Calls
);
5818 -- Start of processing for Expression_Contains_Primitives_Calls_Of_Type
5821 Search_Calls
(Expr
);
5823 end Expression_Contains_Primitives_Calls_Of
;
5825 ----------------------
5826 -- Finalize_Address --
5827 ----------------------
5829 function Finalize_Address
(Typ
: Entity_Id
) return Entity_Id
is
5830 Btyp
: constant Entity_Id
:= Base_Type
(Typ
);
5831 Utyp
: Entity_Id
:= Typ
;
5834 -- Handle protected class-wide or task class-wide types
5836 if Is_Class_Wide_Type
(Utyp
) then
5837 if Is_Concurrent_Type
(Root_Type
(Utyp
)) then
5838 Utyp
:= Root_Type
(Utyp
);
5840 elsif Is_Private_Type
(Root_Type
(Utyp
))
5841 and then Present
(Full_View
(Root_Type
(Utyp
)))
5842 and then Is_Concurrent_Type
(Full_View
(Root_Type
(Utyp
)))
5844 Utyp
:= Full_View
(Root_Type
(Utyp
));
5848 -- Handle private types
5850 if Is_Private_Type
(Utyp
) and then Present
(Full_View
(Utyp
)) then
5851 Utyp
:= Full_View
(Utyp
);
5854 -- Handle protected and task types
5856 if Is_Concurrent_Type
(Utyp
)
5857 and then Present
(Corresponding_Record_Type
(Utyp
))
5859 Utyp
:= Corresponding_Record_Type
(Utyp
);
5862 Utyp
:= Underlying_Type
(Base_Type
(Utyp
));
5864 -- Deal with untagged derivation of private views. If the parent is
5865 -- now known to be protected, the finalization routine is the one
5866 -- defined on the corresponding record of the ancestor (corresponding
5867 -- records do not automatically inherit operations, but maybe they
5870 if Is_Untagged_Derivation
(Btyp
) then
5871 if Is_Protected_Type
(Btyp
) then
5872 Utyp
:= Corresponding_Record_Type
(Root_Type
(Btyp
));
5875 Utyp
:= Underlying_Type
(Root_Type
(Btyp
));
5877 if Is_Protected_Type
(Utyp
) then
5878 Utyp
:= Corresponding_Record_Type
(Utyp
);
5883 -- If the underlying_type is a subtype, we are dealing with the
5884 -- completion of a private type. We need to access the base type and
5885 -- generate a conversion to it.
5887 if Utyp
/= Base_Type
(Utyp
) then
5888 pragma Assert
(Is_Private_Type
(Typ
));
5890 Utyp
:= Base_Type
(Utyp
);
5893 -- When dealing with an internally built full view for a type with
5894 -- unknown discriminants, use the original record type.
5896 if Is_Underlying_Record_View
(Utyp
) then
5897 Utyp
:= Etype
(Utyp
);
5900 return TSS
(Utyp
, TSS_Finalize_Address
);
5901 end Finalize_Address
;
5903 ------------------------
5904 -- Find_Interface_ADT --
5905 ------------------------
5907 function Find_Interface_ADT
5909 Iface
: Entity_Id
) return Elmt_Id
5912 Typ
: Entity_Id
:= T
;
5915 pragma Assert
(Is_Interface
(Iface
));
5917 -- Handle private types
5919 if Has_Private_Declaration
(Typ
) and then Present
(Full_View
(Typ
)) then
5920 Typ
:= Full_View
(Typ
);
5923 -- Handle access types
5925 if Is_Access_Type
(Typ
) then
5926 Typ
:= Designated_Type
(Typ
);
5929 -- Handle task and protected types implementing interfaces
5931 if Is_Concurrent_Type
(Typ
) then
5932 Typ
:= Corresponding_Record_Type
(Typ
);
5936 (not Is_Class_Wide_Type
(Typ
)
5937 and then Ekind
(Typ
) /= E_Incomplete_Type
);
5939 if Is_Ancestor
(Iface
, Typ
, Use_Full_View
=> True) then
5940 return First_Elmt
(Access_Disp_Table
(Typ
));
5943 ADT
:= Next_Elmt
(Next_Elmt
(First_Elmt
(Access_Disp_Table
(Typ
))));
5945 and then Present
(Related_Type
(Node
(ADT
)))
5946 and then Related_Type
(Node
(ADT
)) /= Iface
5947 and then not Is_Ancestor
(Iface
, Related_Type
(Node
(ADT
)),
5948 Use_Full_View
=> True)
5953 pragma Assert
(Present
(Related_Type
(Node
(ADT
))));
5956 end Find_Interface_ADT
;
5958 ------------------------
5959 -- Find_Interface_Tag --
5960 ------------------------
5962 function Find_Interface_Tag
5964 Iface
: Entity_Id
) return Entity_Id
5966 AI_Tag
: Entity_Id
:= Empty
;
5967 Found
: Boolean := False;
5968 Typ
: Entity_Id
:= T
;
5970 procedure Find_Tag
(Typ
: Entity_Id
);
5971 -- Internal subprogram used to recursively climb to the ancestors
5977 procedure Find_Tag
(Typ
: Entity_Id
) is
5982 -- This routine does not handle the case in which the interface is an
5983 -- ancestor of Typ. That case is handled by the enclosing subprogram.
5985 pragma Assert
(Typ
/= Iface
);
5987 -- Climb to the root type handling private types
5989 if Present
(Full_View
(Etype
(Typ
))) then
5990 if Full_View
(Etype
(Typ
)) /= Typ
then
5991 Find_Tag
(Full_View
(Etype
(Typ
)));
5994 elsif Etype
(Typ
) /= Typ
then
5995 Find_Tag
(Etype
(Typ
));
5998 -- Traverse the list of interfaces implemented by the type
6001 and then Present
(Interfaces
(Typ
))
6002 and then not (Is_Empty_Elmt_List
(Interfaces
(Typ
)))
6004 -- Skip the tag associated with the primary table
6006 AI_Tag
:= Next_Tag_Component
(First_Tag_Component
(Typ
));
6007 pragma Assert
(Present
(AI_Tag
));
6009 AI_Elmt
:= First_Elmt
(Interfaces
(Typ
));
6010 while Present
(AI_Elmt
) loop
6011 AI
:= Node
(AI_Elmt
);
6014 or else Is_Ancestor
(Iface
, AI
, Use_Full_View
=> True)
6020 AI_Tag
:= Next_Tag_Component
(AI_Tag
);
6021 Next_Elmt
(AI_Elmt
);
6026 -- Start of processing for Find_Interface_Tag
6029 pragma Assert
(Is_Interface
(Iface
));
6031 -- Handle access types
6033 if Is_Access_Type
(Typ
) then
6034 Typ
:= Designated_Type
(Typ
);
6037 -- Handle class-wide types
6039 if Is_Class_Wide_Type
(Typ
) then
6040 Typ
:= Root_Type
(Typ
);
6043 -- Handle private types
6045 if Has_Private_Declaration
(Typ
) and then Present
(Full_View
(Typ
)) then
6046 Typ
:= Full_View
(Typ
);
6049 -- Handle entities from the limited view
6051 if Ekind
(Typ
) = E_Incomplete_Type
then
6052 pragma Assert
(Present
(Non_Limited_View
(Typ
)));
6053 Typ
:= Non_Limited_View
(Typ
);
6056 -- Handle task and protected types implementing interfaces
6058 if Is_Concurrent_Type
(Typ
) then
6059 Typ
:= Corresponding_Record_Type
(Typ
);
6062 -- If the interface is an ancestor of the type, then it shared the
6063 -- primary dispatch table.
6065 if Is_Ancestor
(Iface
, Typ
, Use_Full_View
=> True) then
6066 return First_Tag_Component
(Typ
);
6068 -- Otherwise we need to search for its associated tag component
6074 end Find_Interface_Tag
;
6076 ---------------------------
6077 -- Find_Optional_Prim_Op --
6078 ---------------------------
6080 function Find_Optional_Prim_Op
6081 (T
: Entity_Id
; Name
: Name_Id
) return Entity_Id
6084 Typ
: Entity_Id
:= T
;
6088 if Is_Class_Wide_Type
(Typ
) then
6089 Typ
:= Root_Type
(Typ
);
6092 Typ
:= Underlying_Type
(Typ
);
6094 -- Loop through primitive operations
6096 Prim
:= First_Elmt
(Primitive_Operations
(Typ
));
6097 while Present
(Prim
) loop
6100 -- We can retrieve primitive operations by name if it is an internal
6101 -- name. For equality we must check that both of its operands have
6102 -- the same type, to avoid confusion with user-defined equalities
6103 -- than may have a asymmetric signature.
6105 exit when Chars
(Op
) = Name
6108 or else Etype
(First_Formal
(Op
)) = Etype
(Last_Formal
(Op
)));
6113 return Node
(Prim
); -- Empty if not found
6114 end Find_Optional_Prim_Op
;
6116 ---------------------------
6117 -- Find_Optional_Prim_Op --
6118 ---------------------------
6120 function Find_Optional_Prim_Op
6122 Name
: TSS_Name_Type
) return Entity_Id
6124 Inher_Op
: Entity_Id
:= Empty
;
6125 Own_Op
: Entity_Id
:= Empty
;
6126 Prim_Elmt
: Elmt_Id
;
6127 Prim_Id
: Entity_Id
;
6128 Typ
: Entity_Id
:= T
;
6131 if Is_Class_Wide_Type
(Typ
) then
6132 Typ
:= Root_Type
(Typ
);
6135 Typ
:= Underlying_Type
(Typ
);
6137 -- This search is based on the assertion that the dispatching version
6138 -- of the TSS routine always precedes the real primitive.
6140 Prim_Elmt
:= First_Elmt
(Primitive_Operations
(Typ
));
6141 while Present
(Prim_Elmt
) loop
6142 Prim_Id
:= Node
(Prim_Elmt
);
6144 if Is_TSS
(Prim_Id
, Name
) then
6145 if Present
(Alias
(Prim_Id
)) then
6146 Inher_Op
:= Prim_Id
;
6152 Next_Elmt
(Prim_Elmt
);
6155 if Present
(Own_Op
) then
6157 elsif Present
(Inher_Op
) then
6162 end Find_Optional_Prim_Op
;
6168 function Find_Prim_Op
6169 (T
: Entity_Id
; Name
: Name_Id
) return Entity_Id
6171 Result
: constant Entity_Id
:= Find_Optional_Prim_Op
(T
, Name
);
6174 raise Program_Error
;
6184 function Find_Prim_Op
6186 Name
: TSS_Name_Type
) return Entity_Id
6188 Result
: constant Entity_Id
:= Find_Optional_Prim_Op
(T
, Name
);
6191 raise Program_Error
;
6197 ----------------------------
6198 -- Find_Protection_Object --
6199 ----------------------------
6201 function Find_Protection_Object
(Scop
: Entity_Id
) return Entity_Id
is
6206 while Present
(S
) loop
6207 if Ekind
(S
) in E_Entry | E_Entry_Family | E_Function | E_Procedure
6208 and then Present
(Protection_Object
(S
))
6210 return Protection_Object
(S
);
6216 -- If we do not find a Protection object in the scope chain, then
6217 -- something has gone wrong, most likely the object was never created.
6219 raise Program_Error
;
6220 end Find_Protection_Object
;
6222 --------------------------
6223 -- Find_Protection_Type --
6224 --------------------------
6226 function Find_Protection_Type
(Conc_Typ
: Entity_Id
) return Entity_Id
is
6228 Typ
: Entity_Id
:= Conc_Typ
;
6231 if Is_Concurrent_Type
(Typ
) then
6232 Typ
:= Corresponding_Record_Type
(Typ
);
6235 -- Since restriction violations are not considered serious errors, the
6236 -- expander remains active, but may leave the corresponding record type
6237 -- malformed. In such cases, component _object is not available so do
6240 if not Analyzed
(Typ
) then
6244 Comp
:= First_Component
(Typ
);
6245 while Present
(Comp
) loop
6246 if Chars
(Comp
) = Name_uObject
then
6247 return Base_Type
(Etype
(Comp
));
6250 Next_Component
(Comp
);
6253 -- The corresponding record of a protected type should always have an
6256 raise Program_Error
;
6257 end Find_Protection_Type
;
6259 function Find_Storage_Op
6261 Nam
: Name_Id
) return Entity_Id
6263 use Sem_Util
.Storage_Model_Support
;
6266 if Has_Storage_Model_Type_Aspect
(Typ
) then
6268 SMT_Op
: constant Entity_Id
:=
6269 Get_Storage_Model_Type_Entity
(Typ
, Nam
);
6271 if not Present
(SMT_Op
) then
6272 raise Program_Error
;
6278 -- Otherwise we assume that Typ is a descendant of Root_Storage_Pool
6281 return Find_Prim_Op
(Typ
, Nam
);
6283 end Find_Storage_Op
;
6285 -----------------------
6286 -- Find_Hook_Context --
6287 -----------------------
6289 function Find_Hook_Context
(N
: Node_Id
) return Node_Id
is
6293 Wrapped_Node
: Node_Id
;
6294 -- Note: if we are in a transient scope, we want to reuse it as
6295 -- the context for actions insertion, if possible. But if N is itself
6296 -- part of the stored actions for the current transient scope,
6297 -- then we need to insert at the appropriate (inner) location in
6298 -- the not as an action on Node_To_Be_Wrapped.
6300 In_Cond_Expr
: constant Boolean := Within_Case_Or_If_Expression
(N
);
6303 -- When the node is inside a case/if expression, the lifetime of any
6304 -- temporary controlled object is extended. Find a suitable insertion
6305 -- node by locating the topmost case or if expressions.
6307 if In_Cond_Expr
then
6310 while Present
(Par
) loop
6311 if Nkind
(Original_Node
(Par
)) in
6312 N_Case_Expression | N_If_Expression
6316 -- Prevent the search from going too far
6318 elsif Is_Body_Or_Package_Declaration
(Par
) then
6322 Par
:= Parent
(Par
);
6325 -- The topmost case or if expression is now recovered, but it may
6326 -- still not be the correct place to add generated code. Climb to
6327 -- find a parent that is part of a declarative or statement list,
6328 -- and is not a list of actuals in a call.
6331 while Present
(Par
) loop
6332 if Is_List_Member
(Par
)
6333 and then Nkind
(Par
) not in N_Component_Association
6334 | N_Discriminant_Association
6335 | N_Parameter_Association
6336 | N_Pragma_Argument_Association
6339 | N_Extension_Aggregate
6340 and then Nkind
(Parent
(Par
)) not in N_Function_Call
6341 | N_Procedure_Call_Statement
6342 | N_Entry_Call_Statement
6347 -- Prevent the search from going too far
6349 elsif Is_Body_Or_Package_Declaration
(Par
) then
6353 Par
:= Parent
(Par
);
6360 while Present
(Par
) loop
6362 -- Keep climbing past various operators
6364 if Nkind
(Parent
(Par
)) in N_Op
6365 or else Nkind
(Parent
(Par
)) in N_And_Then | N_Or_Else
6367 Par
:= Parent
(Par
);
6375 -- The node may be located in a pragma in which case return the
6378 -- pragma Precondition (... and then Ctrl_Func_Call ...);
6380 -- Similar case occurs when the node is related to an object
6381 -- declaration or assignment:
6383 -- Obj [: Some_Typ] := ... and then Ctrl_Func_Call ...;
6385 -- Another case to consider is when the node is part of a return
6388 -- return ... and then Ctrl_Func_Call ...;
6390 -- Another case is when the node acts as a formal in a procedure
6393 -- Proc (... and then Ctrl_Func_Call ...);
6395 if Scope_Is_Transient
then
6396 Wrapped_Node
:= Node_To_Be_Wrapped
;
6398 Wrapped_Node
:= Empty
;
6401 while Present
(Par
) loop
6402 if Par
= Wrapped_Node
6403 or else Nkind
(Par
) in N_Assignment_Statement
6404 | N_Object_Declaration
6406 | N_Procedure_Call_Statement
6407 | N_Simple_Return_Statement
6411 -- Prevent the search from going too far
6413 elsif Is_Body_Or_Package_Declaration
(Par
) then
6417 Par
:= Parent
(Par
);
6420 -- Return the topmost short circuit operator
6424 end Find_Hook_Context
;
6426 ------------------------------
6427 -- Following_Address_Clause --
6428 ------------------------------
6430 function Following_Address_Clause
(D
: Node_Id
) return Node_Id
is
6431 Id
: constant Entity_Id
:= Defining_Identifier
(D
);
6435 function Check_Decls
(D
: Node_Id
) return Node_Id
;
6436 -- This internal function differs from the main function in that it
6437 -- gets called to deal with a following package private part, and
6438 -- it checks declarations starting with D (the main function checks
6439 -- declarations following D). If D is Empty, then Empty is returned.
6445 function Check_Decls
(D
: Node_Id
) return Node_Id
is
6450 while Present
(Decl
) loop
6451 if Nkind
(Decl
) = N_At_Clause
6452 and then Chars
(Identifier
(Decl
)) = Chars
(Id
)
6456 elsif Nkind
(Decl
) = N_Attribute_Definition_Clause
6457 and then Chars
(Decl
) = Name_Address
6458 and then Chars
(Name
(Decl
)) = Chars
(Id
)
6466 -- Otherwise not found, return Empty
6471 -- Start of processing for Following_Address_Clause
6474 -- If parser detected no address clause for the identifier in question,
6475 -- then the answer is a quick NO, without the need for a search.
6477 if not Get_Name_Table_Boolean1
(Chars
(Id
)) then
6481 -- Otherwise search current declarative unit
6483 Result
:= Check_Decls
(Next
(D
));
6485 if Present
(Result
) then
6489 -- Check for possible package private part following
6493 if Nkind
(Par
) = N_Package_Specification
6494 and then Visible_Declarations
(Par
) = List_Containing
(D
)
6495 and then Present
(Private_Declarations
(Par
))
6497 -- Private part present, check declarations there
6499 return Check_Decls
(First
(Private_Declarations
(Par
)));
6502 -- No private part, clause not found, return Empty
6506 end Following_Address_Clause
;
6508 ----------------------
6509 -- Force_Evaluation --
6510 ----------------------
6512 procedure Force_Evaluation
6514 Name_Req
: Boolean := False;
6515 Related_Id
: Entity_Id
:= Empty
;
6516 Is_Low_Bound
: Boolean := False;
6517 Is_High_Bound
: Boolean := False;
6518 Discr_Number
: Int
:= 0;
6519 Mode
: Force_Evaluation_Mode
:= Relaxed
)
6524 Name_Req
=> Name_Req
,
6525 Variable_Ref
=> True,
6526 Renaming_Req
=> False,
6527 Related_Id
=> Related_Id
,
6528 Is_Low_Bound
=> Is_Low_Bound
,
6529 Is_High_Bound
=> Is_High_Bound
,
6530 Discr_Number
=> Discr_Number
,
6531 Check_Side_Effects
=>
6532 Is_Static_Expression
(Exp
)
6533 or else Mode
= Relaxed
);
6534 end Force_Evaluation
;
6536 ---------------------------------
6537 -- Fully_Qualified_Name_String --
6538 ---------------------------------
6540 function Fully_Qualified_Name_String
6542 Append_NUL
: Boolean := True) return String_Id
6544 procedure Internal_Full_Qualified_Name
(E
: Entity_Id
);
6545 -- Compute recursively the qualified name without NUL at the end, adding
6546 -- it to the currently started string being generated
6548 ----------------------------------
6549 -- Internal_Full_Qualified_Name --
6550 ----------------------------------
6552 procedure Internal_Full_Qualified_Name
(E
: Entity_Id
) is
6556 -- Deal properly with child units
6558 if Nkind
(E
) = N_Defining_Program_Unit_Name
then
6559 Ent
:= Defining_Identifier
(E
);
6564 -- Compute qualification recursively (only "Standard" has no scope)
6566 if Present
(Scope
(Scope
(Ent
))) then
6567 Internal_Full_Qualified_Name
(Scope
(Ent
));
6568 Store_String_Char
(Get_Char_Code
('.'));
6571 -- Every entity should have a name except some expanded blocks
6572 -- don't bother about those.
6574 if Chars
(Ent
) = No_Name
then
6578 -- Generates the entity name in upper case
6580 Get_Decoded_Name_String
(Chars
(Ent
));
6582 Store_String_Chars
(Name_Buffer
(1 .. Name_Len
));
6584 end Internal_Full_Qualified_Name
;
6586 -- Start of processing for Full_Qualified_Name
6590 Internal_Full_Qualified_Name
(E
);
6593 Store_String_Char
(Get_Char_Code
(ASCII
.NUL
));
6597 end Fully_Qualified_Name_String
;
6599 ---------------------------------
6600 -- Get_Current_Value_Condition --
6601 ---------------------------------
6603 -- Note: the implementation of this procedure is very closely tied to the
6604 -- implementation of Set_Current_Value_Condition. In the Get procedure, we
6605 -- interpret Current_Value fields set by the Set procedure, so the two
6606 -- procedures need to be closely coordinated.
6608 procedure Get_Current_Value_Condition
6613 Loc
: constant Source_Ptr
:= Sloc
(Var
);
6614 Ent
: constant Entity_Id
:= Entity
(Var
);
6616 procedure Process_Current_Value_Condition
(N
: Node_Id
; S
: Boolean);
6617 -- N is an expression which holds either True (S = True) or False (S =
6618 -- False) in the condition. This procedure digs out the expression and
6619 -- if it refers to Ent, sets Op and Val appropriately.
6621 -------------------------------------
6622 -- Process_Current_Value_Condition --
6623 -------------------------------------
6625 procedure Process_Current_Value_Condition
6630 Prev_Cond
: Node_Id
;
6640 -- Deal with NOT operators, inverting sense
6642 while Nkind
(Cond
) = N_Op_Not
loop
6643 Cond
:= Right_Opnd
(Cond
);
6647 -- Deal with conversions, qualifications, and expressions with
6650 while Nkind
(Cond
) in N_Type_Conversion
6651 | N_Qualified_Expression
6652 | N_Expression_With_Actions
6654 Cond
:= Expression
(Cond
);
6657 exit when Cond
= Prev_Cond
;
6660 -- Deal with AND THEN and AND cases
6662 if Nkind
(Cond
) in N_And_Then | N_Op_And
then
6664 -- Don't ever try to invert a condition that is of the form of an
6665 -- AND or AND THEN (since we are not doing sufficiently general
6666 -- processing to allow this).
6668 if Sens
= False then
6674 -- Recursively process AND and AND THEN branches
6676 Process_Current_Value_Condition
(Left_Opnd
(Cond
), True);
6677 pragma Assert
(Op
'Valid);
6679 if Op
/= N_Empty
then
6683 Process_Current_Value_Condition
(Right_Opnd
(Cond
), True);
6686 -- Case of relational operator
6688 elsif Nkind
(Cond
) in N_Op_Compare
then
6691 -- Invert sense of test if inverted test
6693 if Sens
= False then
6695 when N_Op_Eq
=> Op
:= N_Op_Ne
;
6696 when N_Op_Ne
=> Op
:= N_Op_Eq
;
6697 when N_Op_Lt
=> Op
:= N_Op_Ge
;
6698 when N_Op_Gt
=> Op
:= N_Op_Le
;
6699 when N_Op_Le
=> Op
:= N_Op_Gt
;
6700 when N_Op_Ge
=> Op
:= N_Op_Lt
;
6701 when others => raise Program_Error
;
6705 -- Case of entity op value
6707 if Is_Entity_Name
(Left_Opnd
(Cond
))
6708 and then Ent
= Entity
(Left_Opnd
(Cond
))
6709 and then Compile_Time_Known_Value
(Right_Opnd
(Cond
))
6711 Val
:= Right_Opnd
(Cond
);
6713 -- Case of value op entity
6715 elsif Is_Entity_Name
(Right_Opnd
(Cond
))
6716 and then Ent
= Entity
(Right_Opnd
(Cond
))
6717 and then Compile_Time_Known_Value
(Left_Opnd
(Cond
))
6719 Val
:= Left_Opnd
(Cond
);
6721 -- We are effectively swapping operands
6724 when N_Op_Eq
=> null;
6725 when N_Op_Ne
=> null;
6726 when N_Op_Lt
=> Op
:= N_Op_Gt
;
6727 when N_Op_Gt
=> Op
:= N_Op_Lt
;
6728 when N_Op_Le
=> Op
:= N_Op_Ge
;
6729 when N_Op_Ge
=> Op
:= N_Op_Le
;
6730 when others => raise Program_Error
;
6739 elsif Nkind
(Cond
) in N_Type_Conversion
6740 | N_Qualified_Expression
6741 | N_Expression_With_Actions
6743 Cond
:= Expression
(Cond
);
6745 -- Case of Boolean variable reference, return as though the
6746 -- reference had said var = True.
6749 if Is_Entity_Name
(Cond
) and then Ent
= Entity
(Cond
) then
6750 Val
:= New_Occurrence_Of
(Standard_True
, Sloc
(Cond
));
6752 if Sens
= False then
6759 end Process_Current_Value_Condition
;
6761 -- Start of processing for Get_Current_Value_Condition
6767 -- Immediate return, nothing doing, if this is not an object
6769 if not Is_Object
(Ent
) then
6773 -- In GNATprove mode we don't want to use current value optimizer, in
6774 -- particular for loop invariant expressions and other assertions that
6775 -- act as cut points for proof. The optimizer often folds expressions
6776 -- into True/False where they trivially follow from the previous
6777 -- assignments, but this deprives proof from the information needed to
6778 -- discharge checks that are beyond the scope of the value optimizer.
6780 if GNATprove_Mode
then
6784 -- Otherwise examine current value
6787 CV
: constant Node_Id
:= Current_Value
(Ent
);
6792 -- If statement. Condition is known true in THEN section, known False
6793 -- in any ELSIF or ELSE part, and unknown outside the IF statement.
6795 if Nkind
(CV
) = N_If_Statement
then
6797 -- Before start of IF statement
6799 if Loc
< Sloc
(CV
) then
6802 -- After end of IF statement
6804 elsif Loc
>= Sloc
(CV
) + Text_Ptr
(UI_To_Int
(End_Span
(CV
))) then
6808 -- At this stage we know that we are within the IF statement, but
6809 -- unfortunately, the tree does not record the SLOC of the ELSE so
6810 -- we cannot use a simple SLOC comparison to distinguish between
6811 -- the then/else statements, so we have to climb the tree.
6818 while Parent
(N
) /= CV
loop
6821 -- If we fall off the top of the tree, then that's odd, but
6822 -- perhaps it could occur in some error situation, and the
6823 -- safest response is simply to assume that the outcome of
6824 -- the condition is unknown. No point in bombing during an
6825 -- attempt to optimize things.
6832 -- Now we have N pointing to a node whose parent is the IF
6833 -- statement in question, so now we can tell if we are within
6834 -- the THEN statements.
6836 if Is_List_Member
(N
)
6837 and then List_Containing
(N
) = Then_Statements
(CV
)
6841 -- If the variable reference does not come from source, we
6842 -- cannot reliably tell whether it appears in the else part.
6843 -- In particular, if it appears in generated code for a node
6844 -- that requires finalization, it may be attached to a list
6845 -- that has not been yet inserted into the code. For now,
6846 -- treat it as unknown.
6848 elsif not Comes_From_Source
(N
) then
6851 -- Otherwise we must be in ELSIF or ELSE part
6858 -- ELSIF part. Condition is known true within the referenced
6859 -- ELSIF, known False in any subsequent ELSIF or ELSE part,
6860 -- and unknown before the ELSE part or after the IF statement.
6862 elsif Nkind
(CV
) = N_Elsif_Part
then
6864 -- if the Elsif_Part had condition_actions, the elsif has been
6865 -- rewritten as a nested if, and the original elsif_part is
6866 -- detached from the tree, so there is no way to obtain useful
6867 -- information on the current value of the variable.
6868 -- Can this be improved ???
6870 if No
(Parent
(CV
)) then
6876 -- If the tree has been otherwise rewritten there is nothing
6877 -- else to be done either.
6879 if Nkind
(Stm
) /= N_If_Statement
then
6883 -- Before start of ELSIF part
6885 if Loc
< Sloc
(CV
) then
6888 -- After end of IF statement
6890 elsif Loc
>= Sloc
(Stm
) +
6891 Text_Ptr
(UI_To_Int
(End_Span
(Stm
)))
6896 -- Again we lack the SLOC of the ELSE, so we need to climb the
6897 -- tree to see if we are within the ELSIF part in question.
6904 while Parent
(N
) /= Stm
loop
6907 -- If we fall off the top of the tree, then that's odd, but
6908 -- perhaps it could occur in some error situation, and the
6909 -- safest response is simply to assume that the outcome of
6910 -- the condition is unknown. No point in bombing during an
6911 -- attempt to optimize things.
6918 -- Now we have N pointing to a node whose parent is the IF
6919 -- statement in question, so see if is the ELSIF part we want.
6920 -- the THEN statements.
6925 -- Otherwise we must be in subsequent ELSIF or ELSE part
6932 -- Iteration scheme of while loop. The condition is known to be
6933 -- true within the body of the loop.
6935 elsif Nkind
(CV
) = N_Iteration_Scheme
then
6937 Loop_Stmt
: constant Node_Id
:= Parent
(CV
);
6940 -- Before start of body of loop
6942 if Loc
< Sloc
(Loop_Stmt
) then
6945 -- After end of LOOP statement
6947 elsif Loc
>= Sloc
(End_Label
(Loop_Stmt
)) then
6950 -- We are within the body of the loop
6957 -- All other cases of Current_Value settings
6963 -- If we fall through here, then we have a reportable condition, Sens
6964 -- is True if the condition is true and False if it needs inverting.
6966 Process_Current_Value_Condition
(Condition
(CV
), Sens
);
6968 end Get_Current_Value_Condition
;
6970 -----------------------
6971 -- Get_Index_Subtype --
6972 -----------------------
6974 function Get_Index_Subtype
(N
: Node_Id
) return Entity_Id
is
6975 P_Type
: Entity_Id
:= Etype
(Prefix
(N
));
6980 if Is_Access_Type
(P_Type
) then
6981 P_Type
:= Designated_Type
(P_Type
);
6984 if No
(Expressions
(N
)) then
6987 J
:= UI_To_Int
(Expr_Value
(First
(Expressions
(N
))));
6990 Indx
:= First_Index
(P_Type
);
6996 return Etype
(Indx
);
6997 end Get_Index_Subtype
;
6999 -----------------------
7000 -- Get_Mapped_Entity --
7001 -----------------------
7003 function Get_Mapped_Entity
(E
: Entity_Id
) return Entity_Id
is
7005 return Type_Map
.Get
(E
);
7006 end Get_Mapped_Entity
;
7008 ---------------------
7009 -- Get_Stream_Size --
7010 ---------------------
7012 function Get_Stream_Size
(E
: Entity_Id
) return Uint
is
7014 -- If we have a Stream_Size clause for this type use it
7016 if Has_Stream_Size_Clause
(E
) then
7017 return Static_Integer
(Expression
(Stream_Size_Clause
(E
)));
7019 -- Otherwise the Stream_Size is the size of the type
7024 end Get_Stream_Size
;
7026 ---------------------------
7027 -- Has_Access_Constraint --
7028 ---------------------------
7030 function Has_Access_Constraint
(E
: Entity_Id
) return Boolean is
7032 T
: constant Entity_Id
:= Etype
(E
);
7035 if Has_Per_Object_Constraint
(E
) and then Has_Discriminants
(T
) then
7036 Disc
:= First_Discriminant
(T
);
7037 while Present
(Disc
) loop
7038 if Is_Access_Type
(Etype
(Disc
)) then
7042 Next_Discriminant
(Disc
);
7049 end Has_Access_Constraint
;
7051 --------------------
7052 -- Homonym_Number --
7053 --------------------
7055 function Homonym_Number
(Subp
: Entity_Id
) return Pos
is
7056 Hom
: Entity_Id
:= Homonym
(Subp
);
7060 while Present
(Hom
) loop
7061 if Scope
(Hom
) = Scope
(Subp
) then
7065 Hom
:= Homonym
(Hom
);
7071 -----------------------------------
7072 -- In_Library_Level_Package_Body --
7073 -----------------------------------
7075 function In_Library_Level_Package_Body
(Id
: Entity_Id
) return Boolean is
7077 -- First determine whether the entity appears at the library level, then
7078 -- look at the containing unit.
7080 if Is_Library_Level_Entity
(Id
) then
7082 Container
: constant Node_Id
:= Cunit
(Get_Source_Unit
(Id
));
7085 return Nkind
(Unit
(Container
)) = N_Package_Body
;
7090 end In_Library_Level_Package_Body
;
7092 ------------------------------
7093 -- In_Unconditional_Context --
7094 ------------------------------
7096 function In_Unconditional_Context
(Node
: Node_Id
) return Boolean is
7101 while Present
(P
) loop
7103 when N_Subprogram_Body
=> return True;
7104 when N_If_Statement
=> return False;
7105 when N_Loop_Statement
=> return False;
7106 when N_Case_Statement
=> return False;
7107 when others => P
:= Parent
(P
);
7112 end In_Unconditional_Context
;
7118 procedure Insert_Action
7119 (Assoc_Node
: Node_Id
;
7120 Ins_Action
: Node_Id
;
7121 Spec_Expr_OK
: Boolean := False)
7124 if Present
(Ins_Action
) then
7126 (Assoc_Node
=> Assoc_Node
,
7127 Ins_Actions
=> New_List
(Ins_Action
),
7128 Spec_Expr_OK
=> Spec_Expr_OK
);
7132 -- Version with check(s) suppressed
7134 procedure Insert_Action
7135 (Assoc_Node
: Node_Id
;
7136 Ins_Action
: Node_Id
;
7137 Suppress
: Check_Id
;
7138 Spec_Expr_OK
: Boolean := False)
7142 (Assoc_Node
=> Assoc_Node
,
7143 Ins_Actions
=> New_List
(Ins_Action
),
7144 Suppress
=> Suppress
,
7145 Spec_Expr_OK
=> Spec_Expr_OK
);
7148 -------------------------
7149 -- Insert_Action_After --
7150 -------------------------
7152 procedure Insert_Action_After
7153 (Assoc_Node
: Node_Id
;
7154 Ins_Action
: Node_Id
)
7157 Insert_Actions_After
(Assoc_Node
, New_List
(Ins_Action
));
7158 end Insert_Action_After
;
7160 --------------------
7161 -- Insert_Actions --
7162 --------------------
7164 procedure Insert_Actions
7165 (Assoc_Node
: Node_Id
;
7166 Ins_Actions
: List_Id
;
7167 Spec_Expr_OK
: Boolean := False)
7172 Wrapped_Node
: Node_Id
:= Empty
;
7175 if No
(Ins_Actions
) or else Is_Empty_List
(Ins_Actions
) then
7179 -- Insert the action when the context is "Handling of Default and Per-
7180 -- Object Expressions" only when requested by the caller.
7182 if Spec_Expr_OK
then
7185 -- Ignore insert of actions from inside default expression (or other
7186 -- similar "spec expression") in the special spec-expression analyze
7187 -- mode. Any insertions at this point have no relevance, since we are
7188 -- only doing the analyze to freeze the types of any static expressions.
7189 -- See section "Handling of Default and Per-Object Expressions" in the
7190 -- spec of package Sem for further details.
7192 elsif In_Spec_Expression
then
7196 -- If the action derives from stuff inside a record, then the actions
7197 -- are attached to the current scope, to be inserted and analyzed on
7198 -- exit from the scope. The reason for this is that we may also be
7199 -- generating freeze actions at the same time, and they must eventually
7200 -- be elaborated in the correct order.
7202 if Is_Record_Type
(Current_Scope
)
7203 and then not Is_Frozen
(Current_Scope
)
7205 if No
(Scope_Stack
.Table
7206 (Scope_Stack
.Last
).Pending_Freeze_Actions
)
7208 Scope_Stack
.Table
(Scope_Stack
.Last
).Pending_Freeze_Actions
:=
7213 Scope_Stack
.Table
(Scope_Stack
.Last
).Pending_Freeze_Actions
);
7219 -- We now intend to climb up the tree to find the right point to
7220 -- insert the actions. We start at Assoc_Node, unless this node is a
7221 -- subexpression in which case we start with its parent. We do this for
7222 -- two reasons. First it speeds things up. Second, if Assoc_Node is
7223 -- itself one of the special nodes like N_And_Then, then we assume that
7224 -- an initial request to insert actions for such a node does not expect
7225 -- the actions to get deposited in the node for later handling when the
7226 -- node is expanded, since clearly the node is being dealt with by the
7227 -- caller. Note that in the subexpression case, N is always the child we
7230 -- N_Raise_xxx_Error is an annoying special case, it is a statement
7231 -- if it has type Standard_Void_Type, and a subexpression otherwise.
7232 -- Procedure calls, and similarly procedure attribute references, are
7235 if Nkind
(Assoc_Node
) in N_Subexpr
7236 and then (Nkind
(Assoc_Node
) not in N_Raise_xxx_Error
7237 or else Etype
(Assoc_Node
) /= Standard_Void_Type
)
7238 and then Nkind
(Assoc_Node
) /= N_Procedure_Call_Statement
7239 and then (Nkind
(Assoc_Node
) /= N_Attribute_Reference
7240 or else not Is_Procedure_Attribute_Name
7241 (Attribute_Name
(Assoc_Node
)))
7244 P
:= Parent
(Assoc_Node
);
7246 -- Nonsubexpression case. Note that N is initially Empty in this case
7247 -- (N is only guaranteed non-Empty in the subexpr case).
7254 -- Capture root of the transient scope
7256 if Scope_Is_Transient
then
7257 Wrapped_Node
:= Node_To_Be_Wrapped
;
7261 pragma Assert
(Present
(P
));
7263 -- Make sure that inserted actions stay in the transient scope
7265 if Present
(Wrapped_Node
) and then N
= Wrapped_Node
then
7266 Store_Before_Actions_In_Scope
(Ins_Actions
);
7272 -- Case of right operand of AND THEN or OR ELSE. Put the actions
7273 -- in the Actions field of the right operand. They will be moved
7274 -- out further when the AND THEN or OR ELSE operator is expanded.
7275 -- Nothing special needs to be done for the left operand since
7276 -- in that case the actions are executed unconditionally.
7278 when N_Short_Circuit
=>
7279 if N
= Right_Opnd
(P
) then
7281 -- We are now going to either append the actions to the
7282 -- actions field of the short-circuit operation. We will
7283 -- also analyze the actions now.
7285 -- This analysis is really too early, the proper thing would
7286 -- be to just park them there now, and only analyze them if
7287 -- we find we really need them, and to it at the proper
7288 -- final insertion point. However attempting to this proved
7289 -- tricky, so for now we just kill current values before and
7290 -- after the analyze call to make sure we avoid peculiar
7291 -- optimizations from this out of order insertion.
7293 Kill_Current_Values
;
7295 -- If P has already been expanded, we can't park new actions
7296 -- on it, so we need to expand them immediately, introducing
7297 -- an Expression_With_Actions. N can't be an expression
7298 -- with actions, or else then the actions would have been
7299 -- inserted at an inner level.
7301 if Analyzed
(P
) then
7302 pragma Assert
(Nkind
(N
) /= N_Expression_With_Actions
);
7304 Make_Expression_With_Actions
(Sloc
(N
),
7305 Actions
=> Ins_Actions
,
7306 Expression
=> Relocate_Node
(N
)));
7307 Analyze_And_Resolve
(N
);
7309 elsif Present
(Actions
(P
)) then
7310 Insert_List_After_And_Analyze
7311 (Last
(Actions
(P
)), Ins_Actions
);
7313 Set_Actions
(P
, Ins_Actions
);
7314 Analyze_List
(Actions
(P
));
7317 Kill_Current_Values
;
7322 -- Then or Else dependent expression of an if expression. Add
7323 -- actions to Then_Actions or Else_Actions field as appropriate.
7324 -- The actions will be moved further out when the if is expanded.
7326 when N_If_Expression
=>
7328 ThenX
: constant Node_Id
:= Next
(First
(Expressions
(P
)));
7329 ElseX
: constant Node_Id
:= Next
(ThenX
);
7332 -- If the enclosing expression is already analyzed, as
7333 -- is the case for nested elaboration checks, insert the
7334 -- conditional further out.
7336 if Analyzed
(P
) then
7339 -- Actions belong to the then expression, temporarily place
7340 -- them as Then_Actions of the if expression. They will be
7341 -- moved to the proper place later when the if expression is
7344 elsif N
= ThenX
then
7345 if Present
(Then_Actions
(P
)) then
7346 Insert_List_After_And_Analyze
7347 (Last
(Then_Actions
(P
)), Ins_Actions
);
7349 Set_Then_Actions
(P
, Ins_Actions
);
7350 Analyze_List
(Then_Actions
(P
));
7355 -- Else_Actions is treated the same as Then_Actions above
7357 elsif N
= ElseX
then
7358 if Present
(Else_Actions
(P
)) then
7359 Insert_List_After_And_Analyze
7360 (Last
(Else_Actions
(P
)), Ins_Actions
);
7362 Set_Else_Actions
(P
, Ins_Actions
);
7363 Analyze_List
(Else_Actions
(P
));
7368 -- Actions belong to the condition. In this case they are
7369 -- unconditionally executed, and so we can continue the
7370 -- search for the proper insert point.
7377 -- Alternative of case expression, we place the action in the
7378 -- Actions field of the case expression alternative, this will
7379 -- be handled when the case expression is expanded.
7381 when N_Case_Expression_Alternative
=>
7382 if Present
(Actions
(P
)) then
7383 Insert_List_After_And_Analyze
7384 (Last
(Actions
(P
)), Ins_Actions
);
7386 Set_Actions
(P
, Ins_Actions
);
7387 Analyze_List
(Actions
(P
));
7392 -- Case of appearing within an Expressions_With_Actions node. When
7393 -- the new actions come from the expression of the expression with
7394 -- actions, they must be added to the existing actions. The other
7395 -- alternative is when the new actions are related to one of the
7396 -- existing actions of the expression with actions, and should
7397 -- never reach here: if actions are inserted on a statement
7398 -- within the Actions of an expression with actions, or on some
7399 -- subexpression of such a statement, then the outermost proper
7400 -- insertion point is right before the statement, and we should
7401 -- never climb up as far as the N_Expression_With_Actions itself.
7403 when N_Expression_With_Actions
=>
7404 if N
= Expression
(P
) then
7405 if Is_Empty_List
(Actions
(P
)) then
7406 Append_List_To
(Actions
(P
), Ins_Actions
);
7407 Analyze_List
(Actions
(P
));
7409 Insert_List_After_And_Analyze
7410 (Last
(Actions
(P
)), Ins_Actions
);
7416 raise Program_Error
;
7419 -- Case of appearing in the condition of a while expression or
7420 -- elsif. We insert the actions into the Condition_Actions field.
7421 -- They will be moved further out when the while loop or elsif
7425 | N_Iteration_Scheme
7427 if N
= Condition
(P
) then
7428 if Present
(Condition_Actions
(P
)) then
7429 Insert_List_After_And_Analyze
7430 (Last
(Condition_Actions
(P
)), Ins_Actions
);
7432 Set_Condition_Actions
(P
, Ins_Actions
);
7434 -- Set the parent of the insert actions explicitly. This
7435 -- is not a syntactic field, but we need the parent field
7436 -- set, in particular so that freeze can understand that
7437 -- it is dealing with condition actions, and properly
7438 -- insert the freezing actions.
7440 Set_Parent
(Ins_Actions
, P
);
7441 Analyze_List
(Condition_Actions
(P
));
7447 -- Statements, declarations, pragmas, representation clauses
7452 N_Procedure_Call_Statement
7453 | N_Statement_Other_Than_Procedure_Call
7459 -- Representation_Clause
7462 | N_Attribute_Definition_Clause
7463 | N_Enumeration_Representation_Clause
7464 | N_Record_Representation_Clause
7468 | N_Abstract_Subprogram_Declaration
7470 | N_Exception_Declaration
7471 | N_Exception_Renaming_Declaration
7472 | N_Expression_Function
7473 | N_Formal_Abstract_Subprogram_Declaration
7474 | N_Formal_Concrete_Subprogram_Declaration
7475 | N_Formal_Object_Declaration
7476 | N_Formal_Type_Declaration
7477 | N_Full_Type_Declaration
7478 | N_Function_Instantiation
7479 | N_Generic_Function_Renaming_Declaration
7480 | N_Generic_Package_Declaration
7481 | N_Generic_Package_Renaming_Declaration
7482 | N_Generic_Procedure_Renaming_Declaration
7483 | N_Generic_Subprogram_Declaration
7484 | N_Implicit_Label_Declaration
7485 | N_Incomplete_Type_Declaration
7486 | N_Number_Declaration
7487 | N_Object_Declaration
7488 | N_Object_Renaming_Declaration
7490 | N_Package_Body_Stub
7491 | N_Package_Declaration
7492 | N_Package_Instantiation
7493 | N_Package_Renaming_Declaration
7494 | N_Private_Extension_Declaration
7495 | N_Private_Type_Declaration
7496 | N_Procedure_Instantiation
7498 | N_Protected_Body_Stub
7499 | N_Single_Task_Declaration
7501 | N_Subprogram_Body_Stub
7502 | N_Subprogram_Declaration
7503 | N_Subprogram_Renaming_Declaration
7504 | N_Subtype_Declaration
7508 -- Use clauses can appear in lists of declarations
7510 | N_Use_Package_Clause
7513 -- Freeze entity behaves like a declaration or statement
7516 | N_Freeze_Generic_Entity
7518 -- Do not insert here if the item is not a list member (this
7519 -- happens for example with a triggering statement, and the
7520 -- proper approach is to insert before the entire select).
7522 if not Is_List_Member
(P
) then
7525 -- Do not insert if parent of P is an N_Component_Association
7526 -- node (i.e. we are in the context of an N_Aggregate or
7527 -- N_Extension_Aggregate node. In this case we want to insert
7528 -- before the entire aggregate.
7530 elsif Nkind
(Parent
(P
)) = N_Component_Association
then
7533 -- Do not insert if the parent of P is either an N_Variant node
7534 -- or an N_Record_Definition node, meaning in either case that
7535 -- P is a member of a component list, and that therefore the
7536 -- actions should be inserted outside the complete record
7539 elsif Nkind
(Parent
(P
)) in N_Variant | N_Record_Definition
then
7542 -- Do not insert freeze nodes within the loop generated for
7543 -- an aggregate, because they may be elaborated too late for
7544 -- subsequent use in the back end: within a package spec the
7545 -- loop is part of the elaboration procedure and is only
7546 -- elaborated during the second pass.
7548 -- If the loop comes from source, or the entity is local to the
7549 -- loop itself it must remain within.
7551 elsif Nkind
(Parent
(P
)) = N_Loop_Statement
7552 and then not Comes_From_Source
(Parent
(P
))
7553 and then Nkind
(First
(Ins_Actions
)) = N_Freeze_Entity
7555 Scope
(Entity
(First
(Ins_Actions
))) /= Current_Scope
7559 -- Otherwise we can go ahead and do the insertion
7561 elsif P
= Wrapped_Node
then
7562 Store_Before_Actions_In_Scope
(Ins_Actions
);
7566 Insert_List_Before_And_Analyze
(P
, Ins_Actions
);
7570 -- the expansion of Task and protected type declarations can
7571 -- create declarations for temporaries which, like other actions
7572 -- are inserted and analyzed before the current declaraation.
7573 -- However, the current scope is the synchronized type, and
7574 -- for unnesting it is critical that the proper scope for these
7575 -- generated entities be the enclosing one.
7577 when N_Task_Type_Declaration
7578 | N_Protected_Type_Declaration
=>
7580 Push_Scope
(Scope
(Current_Scope
));
7581 Insert_List_Before_And_Analyze
(P
, Ins_Actions
);
7585 -- A special case, N_Raise_xxx_Error can act either as a statement
7586 -- or a subexpression. We tell the difference by looking at the
7587 -- Etype. It is set to Standard_Void_Type in the statement case.
7589 when N_Raise_xxx_Error
=>
7590 if Etype
(P
) = Standard_Void_Type
then
7591 if P
= Wrapped_Node
then
7592 Store_Before_Actions_In_Scope
(Ins_Actions
);
7594 Insert_List_Before_And_Analyze
(P
, Ins_Actions
);
7599 -- In the subexpression case, keep climbing
7605 -- If a component association appears within a loop created for
7606 -- an array aggregate, attach the actions to the association so
7607 -- they can be subsequently inserted within the loop. For other
7608 -- component associations insert outside of the aggregate. For
7609 -- an association that will generate a loop, its Loop_Actions
7610 -- attribute is already initialized (see exp_aggr.adb).
7612 -- The list of Loop_Actions can in turn generate additional ones,
7613 -- that are inserted before the associated node. If the associated
7614 -- node is outside the aggregate, the new actions are collected
7615 -- at the end of the Loop_Actions, to respect the order in which
7616 -- they are to be elaborated.
7618 when N_Component_Association
7619 | N_Iterated_Component_Association
7620 | N_Iterated_Element_Association
7622 if Nkind
(Parent
(P
)) in N_Aggregate | N_Delta_Aggregate
7624 -- We must not climb up out of an N_Iterated_xxx_Association
7625 -- because the actions might contain references to the loop
7626 -- parameter. But it turns out that setting the Loop_Actions
7627 -- attribute in the case of an N_Component_Association
7628 -- when the attribute was not already set can lead to
7629 -- (as yet not understood) bugboxes (gcc failures that are
7630 -- presumably due to malformed trees). So we don't do that.
7632 and then (Nkind
(P
) /= N_Component_Association
7633 or else Present
(Loop_Actions
(P
)))
7635 if Is_Empty_List
(Loop_Actions
(P
)) then
7636 Set_Loop_Actions
(P
, Ins_Actions
);
7637 Analyze_List
(Ins_Actions
);
7643 -- Check whether these actions were generated by a
7644 -- declaration that is part of the Loop_Actions for
7645 -- the component_association.
7648 while Present
(Decl
) loop
7649 exit when Parent
(Decl
) = P
7650 and then Is_List_Member
(Decl
)
7652 List_Containing
(Decl
) = Loop_Actions
(P
);
7653 Decl
:= Parent
(Decl
);
7656 if Present
(Decl
) then
7657 Insert_List_Before_And_Analyze
7658 (Decl
, Ins_Actions
);
7660 Insert_List_After_And_Analyze
7661 (Last
(Loop_Actions
(P
)), Ins_Actions
);
7672 -- Special case: an attribute denoting a procedure call
7674 when N_Attribute_Reference
=>
7675 if Is_Procedure_Attribute_Name
(Attribute_Name
(P
)) then
7676 if P
= Wrapped_Node
then
7677 Store_Before_Actions_In_Scope
(Ins_Actions
);
7679 Insert_List_Before_And_Analyze
(P
, Ins_Actions
);
7684 -- In the subexpression case, keep climbing
7690 -- Special case: a marker
7693 | N_Variable_Reference_Marker
7695 if Is_List_Member
(P
) then
7696 Insert_List_Before_And_Analyze
(P
, Ins_Actions
);
7700 -- A contract node should not belong to the tree
7703 raise Program_Error
;
7705 -- For all other node types, keep climbing tree
7707 when N_Abortable_Part
7708 | N_Accept_Alternative
7709 | N_Access_Definition
7710 | N_Access_Function_Definition
7711 | N_Access_Procedure_Definition
7712 | N_Access_To_Object_Definition
7715 | N_Aspect_Specification
7717 | N_Case_Statement_Alternative
7718 | N_Character_Literal
7719 | N_Compilation_Unit
7720 | N_Compilation_Unit_Aux
7721 | N_Component_Clause
7722 | N_Component_Declaration
7723 | N_Component_Definition
7725 | N_Constrained_Array_Definition
7726 | N_Decimal_Fixed_Point_Definition
7727 | N_Defining_Character_Literal
7728 | N_Defining_Identifier
7729 | N_Defining_Operator_Symbol
7730 | N_Defining_Program_Unit_Name
7731 | N_Delay_Alternative
7733 | N_Delta_Constraint
7734 | N_Derived_Type_Definition
7736 | N_Digits_Constraint
7737 | N_Discriminant_Association
7738 | N_Discriminant_Specification
7740 | N_Entry_Body_Formal_Part
7741 | N_Entry_Call_Alternative
7742 | N_Entry_Declaration
7743 | N_Entry_Index_Specification
7744 | N_Enumeration_Type_Definition
7746 | N_Exception_Handler
7748 | N_Explicit_Dereference
7749 | N_Extension_Aggregate
7750 | N_Floating_Point_Definition
7751 | N_Formal_Decimal_Fixed_Point_Definition
7752 | N_Formal_Derived_Type_Definition
7753 | N_Formal_Discrete_Type_Definition
7754 | N_Formal_Floating_Point_Definition
7755 | N_Formal_Modular_Type_Definition
7756 | N_Formal_Ordinary_Fixed_Point_Definition
7757 | N_Formal_Package_Declaration
7758 | N_Formal_Private_Type_Definition
7759 | N_Formal_Incomplete_Type_Definition
7760 | N_Formal_Signed_Integer_Type_Definition
7762 | N_Function_Specification
7763 | N_Generic_Association
7764 | N_Handled_Sequence_Of_Statements
7767 | N_Index_Or_Discriminant_Constraint
7768 | N_Indexed_Component
7770 | N_Iterator_Specification
7773 | N_Loop_Parameter_Specification
7775 | N_Modular_Type_Definition
7801 | N_Op_Shift_Right_Arithmetic
7805 | N_Ordinary_Fixed_Point_Definition
7807 | N_Package_Specification
7808 | N_Parameter_Association
7809 | N_Parameter_Specification
7810 | N_Pop_Constraint_Error_Label
7811 | N_Pop_Program_Error_Label
7812 | N_Pop_Storage_Error_Label
7813 | N_Pragma_Argument_Association
7814 | N_Procedure_Specification
7815 | N_Protected_Definition
7816 | N_Push_Constraint_Error_Label
7817 | N_Push_Program_Error_Label
7818 | N_Push_Storage_Error_Label
7819 | N_Qualified_Expression
7820 | N_Quantified_Expression
7821 | N_Raise_Expression
7823 | N_Range_Constraint
7825 | N_Real_Range_Specification
7826 | N_Record_Definition
7828 | N_SCIL_Dispatch_Table_Tag_Init
7829 | N_SCIL_Dispatching_Call
7830 | N_SCIL_Membership_Test
7831 | N_Selected_Component
7832 | N_Signed_Integer_Type_Definition
7833 | N_Single_Protected_Declaration
7836 | N_Subtype_Indication
7840 | N_Terminate_Alternative
7841 | N_Triggering_Alternative
7843 | N_Unchecked_Expression
7844 | N_Unchecked_Type_Conversion
7845 | N_Unconstrained_Array_Definition
7850 | N_Validate_Unchecked_Conversion
7856 -- If we fall through above tests, keep climbing tree
7860 if Nkind
(Parent
(N
)) = N_Subunit
then
7862 -- This is the proper body corresponding to a stub. Insertion must
7863 -- be done at the point of the stub, which is in the declarative
7864 -- part of the parent unit.
7866 P
:= Corresponding_Stub
(Parent
(N
));
7874 -- Version with check(s) suppressed
7876 procedure Insert_Actions
7877 (Assoc_Node
: Node_Id
;
7878 Ins_Actions
: List_Id
;
7879 Suppress
: Check_Id
;
7880 Spec_Expr_OK
: Boolean := False)
7883 if Suppress
= All_Checks
then
7885 Sva
: constant Suppress_Array
:= Scope_Suppress
.Suppress
;
7887 Scope_Suppress
.Suppress
:= (others => True);
7888 Insert_Actions
(Assoc_Node
, Ins_Actions
, Spec_Expr_OK
);
7889 Scope_Suppress
.Suppress
:= Sva
;
7894 Svg
: constant Boolean := Scope_Suppress
.Suppress
(Suppress
);
7896 Scope_Suppress
.Suppress
(Suppress
) := True;
7897 Insert_Actions
(Assoc_Node
, Ins_Actions
, Spec_Expr_OK
);
7898 Scope_Suppress
.Suppress
(Suppress
) := Svg
;
7903 --------------------------
7904 -- Insert_Actions_After --
7905 --------------------------
7907 procedure Insert_Actions_After
7908 (Assoc_Node
: Node_Id
;
7909 Ins_Actions
: List_Id
)
7912 if Scope_Is_Transient
and then Assoc_Node
= Node_To_Be_Wrapped
then
7913 Store_After_Actions_In_Scope
(Ins_Actions
);
7915 Insert_List_After_And_Analyze
(Assoc_Node
, Ins_Actions
);
7917 end Insert_Actions_After
;
7919 ------------------------
7920 -- Insert_Declaration --
7921 ------------------------
7923 procedure Insert_Declaration
(N
: Node_Id
; Decl
: Node_Id
) is
7927 pragma Assert
(Nkind
(N
) in N_Subexpr
);
7929 -- Climb until we find a procedure or a package
7933 pragma Assert
(Present
(Parent
(P
)));
7936 if Is_List_Member
(P
) then
7937 exit when Nkind
(Parent
(P
)) in
7938 N_Package_Specification | N_Subprogram_Body
;
7940 -- Special handling for handled sequence of statements, we must
7941 -- insert in the statements not the exception handlers!
7943 if Nkind
(Parent
(P
)) = N_Handled_Sequence_Of_Statements
then
7944 P
:= First
(Statements
(Parent
(P
)));
7950 -- Now do the insertion
7952 Insert_Before
(P
, Decl
);
7954 end Insert_Declaration
;
7956 ---------------------------------
7957 -- Insert_Library_Level_Action --
7958 ---------------------------------
7960 procedure Insert_Library_Level_Action
(N
: Node_Id
) is
7961 Aux
: constant Node_Id
:= Aux_Decls_Node
(Cunit
(Main_Unit
));
7964 Push_Scope
(Cunit_Entity
(Current_Sem_Unit
));
7965 -- And not Main_Unit as previously. If the main unit is a body,
7966 -- the scope needed to analyze the actions is the entity of the
7967 -- corresponding declaration.
7969 if No
(Actions
(Aux
)) then
7970 Set_Actions
(Aux
, New_List
(N
));
7972 Append
(N
, Actions
(Aux
));
7977 end Insert_Library_Level_Action
;
7979 ----------------------------------
7980 -- Insert_Library_Level_Actions --
7981 ----------------------------------
7983 procedure Insert_Library_Level_Actions
(L
: List_Id
) is
7984 Aux
: constant Node_Id
:= Aux_Decls_Node
(Cunit
(Main_Unit
));
7987 if Is_Non_Empty_List
(L
) then
7988 Push_Scope
(Cunit_Entity
(Main_Unit
));
7989 -- ??? should this be Current_Sem_Unit instead of Main_Unit?
7991 if No
(Actions
(Aux
)) then
7992 Set_Actions
(Aux
, L
);
7995 Insert_List_After_And_Analyze
(Last
(Actions
(Aux
)), L
);
8000 end Insert_Library_Level_Actions
;
8002 ----------------------
8003 -- Inside_Init_Proc --
8004 ----------------------
8006 function Inside_Init_Proc
return Boolean is
8008 return Present
(Enclosing_Init_Proc
);
8009 end Inside_Init_Proc
;
8011 ----------------------
8012 -- Integer_Type_For --
8013 ----------------------
8015 function Integer_Type_For
(S
: Uint
; Uns
: Boolean) return Entity_Id
is
8017 pragma Assert
(S
<= System_Max_Integer_Size
);
8019 -- This is the canonical 32-bit type
8021 if S
<= Standard_Integer_Size
then
8023 return Standard_Unsigned
;
8025 return Standard_Integer
;
8028 -- This is the canonical 64-bit type
8030 elsif S
<= Standard_Long_Long_Integer_Size
then
8032 return Standard_Long_Long_Unsigned
;
8034 return Standard_Long_Long_Integer
;
8037 -- This is the canonical 128-bit type
8039 elsif S
<= Standard_Long_Long_Long_Integer_Size
then
8041 return Standard_Long_Long_Long_Unsigned
;
8043 return Standard_Long_Long_Long_Integer
;
8047 raise Program_Error
;
8049 end Integer_Type_For
;
8051 --------------------------------------------------
8052 -- Is_Displacement_Of_Object_Or_Function_Result --
8053 --------------------------------------------------
8055 function Is_Displacement_Of_Object_Or_Function_Result
8056 (Obj_Id
: Entity_Id
) return Boolean
8058 function Is_Controlled_Function_Call
(N
: Node_Id
) return Boolean;
8059 -- Determine whether node N denotes a controlled function call
8061 function Is_Controlled_Indexing
(N
: Node_Id
) return Boolean;
8062 -- Determine whether node N denotes a generalized indexing form which
8063 -- involves a controlled result.
8065 function Is_Displace_Call
(N
: Node_Id
) return Boolean;
8066 -- Determine whether node N denotes a call to Ada.Tags.Displace
8068 function Is_Source_Object
(N
: Node_Id
) return Boolean;
8069 -- Determine whether a particular node denotes a source object
8071 function Strip
(N
: Node_Id
) return Node_Id
;
8072 -- Examine arbitrary node N by stripping various indirections and return
8075 ---------------------------------
8076 -- Is_Controlled_Function_Call --
8077 ---------------------------------
8079 function Is_Controlled_Function_Call
(N
: Node_Id
) return Boolean is
8083 -- When a function call appears in Object.Operation format, the
8084 -- original representation has several possible forms depending on
8085 -- the availability and form of actual parameters:
8087 -- Obj.Func N_Selected_Component
8088 -- Obj.Func (Actual) N_Indexed_Component
8089 -- Obj.Func (Formal => Actual) N_Function_Call, whose Name is an
8090 -- N_Selected_Component
8092 Expr
:= Original_Node
(N
);
8094 if Nkind
(Expr
) = N_Function_Call
then
8095 Expr
:= Name
(Expr
);
8097 -- "Obj.Func (Actual)" case
8099 elsif Nkind
(Expr
) = N_Indexed_Component
then
8100 Expr
:= Prefix
(Expr
);
8102 -- "Obj.Func" or "Obj.Func (Formal => Actual) case
8104 elsif Nkind
(Expr
) = N_Selected_Component
then
8105 Expr
:= Selector_Name
(Expr
);
8113 Nkind
(Expr
) in N_Has_Entity
8114 and then Present
(Entity
(Expr
))
8115 and then Ekind
(Entity
(Expr
)) = E_Function
8116 and then Needs_Finalization
(Etype
(Entity
(Expr
)));
8117 end Is_Controlled_Function_Call
;
8119 ----------------------------
8120 -- Is_Controlled_Indexing --
8121 ----------------------------
8123 function Is_Controlled_Indexing
(N
: Node_Id
) return Boolean is
8124 Expr
: constant Node_Id
:= Original_Node
(N
);
8128 Nkind
(Expr
) = N_Indexed_Component
8129 and then Present
(Generalized_Indexing
(Expr
))
8130 and then Needs_Finalization
(Etype
(Expr
));
8131 end Is_Controlled_Indexing
;
8133 ----------------------
8134 -- Is_Displace_Call --
8135 ----------------------
8137 function Is_Displace_Call
(N
: Node_Id
) return Boolean is
8138 Call
: constant Node_Id
:= Strip
(N
);
8143 and then Nkind
(Call
) = N_Function_Call
8144 and then Nkind
(Name
(Call
)) in N_Has_Entity
8145 and then Is_RTE
(Entity
(Name
(Call
)), RE_Displace
);
8146 end Is_Displace_Call
;
8148 ----------------------
8149 -- Is_Source_Object --
8150 ----------------------
8152 function Is_Source_Object
(N
: Node_Id
) return Boolean is
8153 Obj
: constant Node_Id
:= Strip
(N
);
8158 and then Comes_From_Source
(Obj
)
8159 and then Nkind
(Obj
) in N_Has_Entity
8160 and then Is_Object
(Entity
(Obj
));
8161 end Is_Source_Object
;
8167 function Strip
(N
: Node_Id
) return Node_Id
is
8173 if Nkind
(Result
) = N_Explicit_Dereference
then
8174 Result
:= Prefix
(Result
);
8176 elsif Nkind
(Result
) in
8177 N_Type_Conversion | N_Unchecked_Type_Conversion
8179 Result
:= Expression
(Result
);
8191 Obj_Decl
: constant Node_Id
:= Declaration_Node
(Obj_Id
);
8192 Obj_Typ
: constant Entity_Id
:= Base_Type
(Etype
(Obj_Id
));
8193 Orig_Decl
: constant Node_Id
:= Original_Node
(Obj_Decl
);
8194 Orig_Expr
: Node_Id
;
8196 -- Start of processing for Is_Displacement_Of_Object_Or_Function_Result
8201 -- Obj : CW_Type := Function_Call (...);
8203 -- is rewritten into:
8205 -- Temp : ... := Function_Call (...)'reference;
8206 -- Obj : CW_Type renames (... Ada.Tags.Displace (Temp));
8208 -- where the return type of the function and the class-wide type require
8209 -- dispatch table pointer displacement.
8213 -- Obj : CW_Type := Container (...);
8215 -- is rewritten into:
8217 -- Temp : ... := Function_Call (Container, ...)'reference;
8218 -- Obj : CW_Type renames (... Ada.Tags.Displace (Temp));
8220 -- where the container element type and the class-wide type require
8221 -- dispatch table pointer dispacement.
8225 -- Obj : CW_Type := Src_Obj;
8227 -- is rewritten into:
8229 -- Obj : CW_Type renames (... Ada.Tags.Displace (Src_Obj));
8231 -- where the type of the source object and the class-wide type require
8232 -- dispatch table pointer displacement.
8234 if Nkind
(Obj_Decl
) = N_Object_Renaming_Declaration
8235 and then Is_Class_Wide_Type
(Obj_Typ
)
8236 and then Is_Displace_Call
(Renamed_Object
(Obj_Id
))
8237 and then Nkind
(Orig_Decl
) = N_Object_Declaration
8238 and then Comes_From_Source
(Orig_Decl
)
8240 Orig_Expr
:= Expression
(Orig_Decl
);
8243 Is_Controlled_Function_Call
(Orig_Expr
)
8244 or else Is_Controlled_Indexing
(Orig_Expr
)
8245 or else Is_Source_Object
(Orig_Expr
);
8249 end Is_Displacement_Of_Object_Or_Function_Result
;
8251 ------------------------------
8252 -- Is_Finalizable_Transient --
8253 ------------------------------
8255 function Is_Finalizable_Transient
8257 Rel_Node
: Node_Id
) return Boolean
8259 Obj_Id
: constant Entity_Id
:= Defining_Identifier
(Decl
);
8260 Obj_Typ
: constant Entity_Id
:= Base_Type
(Etype
(Obj_Id
));
8262 function Initialized_By_Access
(Trans_Id
: Entity_Id
) return Boolean;
8263 -- Determine whether transient object Trans_Id is initialized either
8264 -- by a function call which returns an access type or simply renames
8267 function Initialized_By_Aliased_BIP_Func_Call
8268 (Trans_Id
: Entity_Id
) return Boolean;
8269 -- Determine whether transient object Trans_Id is initialized by a
8270 -- build-in-place function call where the BIPalloc parameter is of
8271 -- value 1 and BIPaccess is not null. This case creates an aliasing
8272 -- between the returned value and the value denoted by BIPaccess.
8275 (Trans_Id
: Entity_Id
;
8276 First_Stmt
: Node_Id
) return Boolean;
8277 -- Determine whether transient object Trans_Id has been renamed or
8278 -- aliased through 'reference in the statement list starting from
8281 function Is_Allocated
(Trans_Id
: Entity_Id
) return Boolean;
8282 -- Determine whether transient object Trans_Id is allocated on the heap
8284 function Is_Iterated_Container
8285 (Trans_Id
: Entity_Id
;
8286 First_Stmt
: Node_Id
) return Boolean;
8287 -- Determine whether transient object Trans_Id denotes a container which
8288 -- is in the process of being iterated in the statement list starting
8291 function Is_Part_Of_BIP_Return_Statement
(N
: Node_Id
) return Boolean;
8292 -- Return True if N is directly part of a build-in-place return
8295 ---------------------------
8296 -- Initialized_By_Access --
8297 ---------------------------
8299 function Initialized_By_Access
(Trans_Id
: Entity_Id
) return Boolean is
8300 Expr
: constant Node_Id
:= Expression
(Parent
(Trans_Id
));
8305 and then Nkind
(Expr
) /= N_Reference
8306 and then Is_Access_Type
(Etype
(Expr
));
8307 end Initialized_By_Access
;
8309 ------------------------------------------
8310 -- Initialized_By_Aliased_BIP_Func_Call --
8311 ------------------------------------------
8313 function Initialized_By_Aliased_BIP_Func_Call
8314 (Trans_Id
: Entity_Id
) return Boolean
8316 Call
: Node_Id
:= Expression
(Parent
(Trans_Id
));
8319 -- Build-in-place calls usually appear in 'reference format
8321 if Nkind
(Call
) = N_Reference
then
8322 Call
:= Prefix
(Call
);
8325 Call
:= Unqual_Conv
(Call
);
8327 if Is_Build_In_Place_Function_Call
(Call
) then
8329 Access_Nam
: Name_Id
:= No_Name
;
8330 Access_OK
: Boolean := False;
8332 Alloc_Nam
: Name_Id
:= No_Name
;
8333 Alloc_OK
: Boolean := False;
8335 Func_Id
: Entity_Id
;
8339 -- Examine all parameter associations of the function call
8341 Param
:= First
(Parameter_Associations
(Call
));
8342 while Present
(Param
) loop
8343 if Nkind
(Param
) = N_Parameter_Association
8344 and then Nkind
(Selector_Name
(Param
)) = N_Identifier
8346 Actual
:= Explicit_Actual_Parameter
(Param
);
8347 Formal
:= Selector_Name
(Param
);
8349 -- Construct the names of formals BIPaccess and BIPalloc
8350 -- using the function name retrieved from an arbitrary
8353 if Access_Nam
= No_Name
8354 and then Alloc_Nam
= No_Name
8355 and then Present
(Entity
(Formal
))
8357 Func_Id
:= Scope
(Entity
(Formal
));
8360 New_External_Name
(Chars
(Func_Id
),
8361 BIP_Formal_Suffix
(BIP_Object_Access
));
8364 New_External_Name
(Chars
(Func_Id
),
8365 BIP_Formal_Suffix
(BIP_Alloc_Form
));
8368 -- A match for BIPaccess => Temp has been found
8370 if Chars
(Formal
) = Access_Nam
8371 and then Nkind
(Actual
) /= N_Null
8376 -- A match for BIPalloc => 1 has been found
8378 if Chars
(Formal
) = Alloc_Nam
8379 and then Nkind
(Actual
) = N_Integer_Literal
8380 and then Intval
(Actual
) = Uint_1
8389 return Access_OK
and Alloc_OK
;
8394 end Initialized_By_Aliased_BIP_Func_Call
;
8401 (Trans_Id
: Entity_Id
;
8402 First_Stmt
: Node_Id
) return Boolean
8404 function Find_Renamed_Object
(Ren_Decl
: Node_Id
) return Entity_Id
;
8405 -- Given an object renaming declaration, retrieve the entity of the
8406 -- renamed name. Return Empty if the renamed name is anything other
8407 -- than a variable or a constant.
8409 -------------------------
8410 -- Find_Renamed_Object --
8411 -------------------------
8413 function Find_Renamed_Object
(Ren_Decl
: Node_Id
) return Entity_Id
is
8414 Ren_Obj
: Node_Id
:= Empty
;
8416 function Find_Object
(N
: Node_Id
) return Traverse_Result
;
8417 -- Try to detect an object which is either a constant or a
8424 function Find_Object
(N
: Node_Id
) return Traverse_Result
is
8426 -- Stop the search once a constant or a variable has been
8429 if Nkind
(N
) = N_Identifier
8430 and then Present
(Entity
(N
))
8431 and then Ekind
(Entity
(N
)) in E_Constant | E_Variable
8433 Ren_Obj
:= Entity
(N
);
8440 procedure Search
is new Traverse_Proc
(Find_Object
);
8444 Typ
: constant Entity_Id
:= Etype
(Defining_Identifier
(Ren_Decl
));
8446 -- Start of processing for Find_Renamed_Object
8449 -- Actions related to dispatching calls may appear as renamings of
8450 -- tags. Do not process this type of renaming because it does not
8451 -- use the actual value of the object.
8453 if not Is_RTE
(Typ
, RE_Tag_Ptr
) then
8454 Search
(Name
(Ren_Decl
));
8458 end Find_Renamed_Object
;
8463 Ren_Obj
: Entity_Id
;
8466 -- Start of processing for Is_Aliased
8469 -- A controlled transient object is not considered aliased when it
8470 -- appears inside an expression_with_actions node even when there are
8471 -- explicit aliases of it:
8474 -- Trans_Id : Ctrl_Typ ...; -- transient object
8475 -- Alias : ... := Trans_Id; -- object is aliased
8476 -- Val : constant Boolean :=
8477 -- ... Alias ...; -- aliasing ends
8478 -- <finalize Trans_Id> -- object safe to finalize
8481 -- Expansion ensures that all aliases are encapsulated in the actions
8482 -- list and do not leak to the expression by forcing the evaluation
8483 -- of the expression.
8485 if Nkind
(Rel_Node
) = N_Expression_With_Actions
then
8488 -- Otherwise examine the statements after the controlled transient
8489 -- object and look for various forms of aliasing.
8493 while Present
(Stmt
) loop
8494 if Nkind
(Stmt
) = N_Object_Declaration
then
8495 Expr
:= Expression
(Stmt
);
8497 -- Aliasing of the form:
8498 -- Obj : ... := Trans_Id'reference;
8501 and then Nkind
(Expr
) = N_Reference
8502 and then Nkind
(Prefix
(Expr
)) = N_Identifier
8503 and then Entity
(Prefix
(Expr
)) = Trans_Id
8508 elsif Nkind
(Stmt
) = N_Object_Renaming_Declaration
then
8509 Ren_Obj
:= Find_Renamed_Object
(Stmt
);
8511 -- Aliasing of the form:
8512 -- Obj : ... renames ... Trans_Id ...;
8514 if Present
(Ren_Obj
) and then Ren_Obj
= Trans_Id
then
8530 function Is_Allocated
(Trans_Id
: Entity_Id
) return Boolean is
8531 Expr
: constant Node_Id
:= Expression
(Parent
(Trans_Id
));
8534 Is_Access_Type
(Etype
(Trans_Id
))
8535 and then Present
(Expr
)
8536 and then Nkind
(Expr
) = N_Allocator
;
8539 ---------------------------
8540 -- Is_Iterated_Container --
8541 ---------------------------
8543 function Is_Iterated_Container
8544 (Trans_Id
: Entity_Id
;
8545 First_Stmt
: Node_Id
) return Boolean
8555 -- It is not possible to iterate over containers in non-Ada 2012 code
8557 if Ada_Version
< Ada_2012
then
8561 Typ
:= Etype
(Trans_Id
);
8563 -- Handle access type created for secondary stack use
8565 if Is_Access_Type
(Typ
) then
8566 Typ
:= Designated_Type
(Typ
);
8569 -- Look for aspect Default_Iterator. It may be part of a type
8570 -- declaration for a container, or inherited from a base type
8573 Aspect
:= Find_Value_Of_Aspect
(Typ
, Aspect_Default_Iterator
);
8575 if Present
(Aspect
) then
8576 Iter
:= Entity
(Aspect
);
8578 -- Examine the statements following the container object and
8579 -- look for a call to the default iterate routine where the
8580 -- first parameter is the transient. Such a call appears as:
8582 -- It : Access_To_CW_Iterator :=
8583 -- Iterate (Tran_Id.all, ...)'reference;
8586 while Present
(Stmt
) loop
8588 -- Detect an object declaration which is initialized by a
8589 -- secondary stack function call.
8591 if Nkind
(Stmt
) = N_Object_Declaration
8592 and then Present
(Expression
(Stmt
))
8593 and then Nkind
(Expression
(Stmt
)) = N_Reference
8594 and then Nkind
(Prefix
(Expression
(Stmt
))) = N_Function_Call
8596 Call
:= Prefix
(Expression
(Stmt
));
8598 -- The call must invoke the default iterate routine of
8599 -- the container and the transient object must appear as
8600 -- the first actual parameter. Skip any calls whose names
8601 -- are not entities.
8603 if Is_Entity_Name
(Name
(Call
))
8604 and then Entity
(Name
(Call
)) = Iter
8605 and then Present
(Parameter_Associations
(Call
))
8607 Param
:= First
(Parameter_Associations
(Call
));
8609 if Nkind
(Param
) = N_Explicit_Dereference
8610 and then Entity
(Prefix
(Param
)) = Trans_Id
8622 end Is_Iterated_Container
;
8624 -------------------------------------
8625 -- Is_Part_Of_BIP_Return_Statement --
8626 -------------------------------------
8628 function Is_Part_Of_BIP_Return_Statement
(N
: Node_Id
) return Boolean is
8629 Subp
: constant Entity_Id
:= Current_Subprogram
;
8632 -- First check if N is part of a BIP function
8635 or else not Is_Build_In_Place_Function
(Subp
)
8640 -- Then check whether N is a complete part of a return statement
8641 -- Should we consider other node kinds to go up the tree???
8645 case Nkind
(Context
) is
8646 when N_Expression_With_Actions
=> Context
:= Parent
(Context
);
8647 when N_Simple_Return_Statement
=> return True;
8648 when others => return False;
8651 end Is_Part_Of_BIP_Return_Statement
;
8655 Desig
: Entity_Id
:= Obj_Typ
;
8657 -- Start of processing for Is_Finalizable_Transient
8660 -- Handle access types
8662 if Is_Access_Type
(Desig
) then
8663 Desig
:= Available_View
(Designated_Type
(Desig
));
8667 Ekind
(Obj_Id
) in E_Constant | E_Variable
8668 and then Needs_Finalization
(Desig
)
8669 and then Requires_Transient_Scope
(Desig
)
8670 and then Nkind
(Rel_Node
) /= N_Simple_Return_Statement
8671 and then not Is_Part_Of_BIP_Return_Statement
(Rel_Node
)
8673 -- Do not consider a transient object that was already processed
8675 and then not Is_Finalized_Transient
(Obj_Id
)
8677 -- Do not consider renamed or 'reference-d transient objects because
8678 -- the act of renaming extends the object's lifetime.
8680 and then not Is_Aliased
(Obj_Id
, Decl
)
8682 -- Do not consider transient objects allocated on the heap since
8683 -- they are attached to a finalization master.
8685 and then not Is_Allocated
(Obj_Id
)
8687 -- If the transient object is a pointer, check that it is not
8688 -- initialized by a function that returns a pointer or acts as a
8689 -- renaming of another pointer.
8692 (Is_Access_Type
(Obj_Typ
) and then Initialized_By_Access
(Obj_Id
))
8694 -- Do not consider transient objects which act as indirect aliases
8695 -- of build-in-place function results.
8697 and then not Initialized_By_Aliased_BIP_Func_Call
(Obj_Id
)
8699 -- Do not consider conversions of tags to class-wide types
8701 and then not Is_Tag_To_Class_Wide_Conversion
(Obj_Id
)
8703 -- Do not consider iterators because those are treated as normal
8704 -- controlled objects and are processed by the usual finalization
8705 -- machinery. This avoids the double finalization of an iterator.
8707 and then not Is_Iterator
(Desig
)
8709 -- Do not consider containers in the context of iterator loops. Such
8710 -- transient objects must exist for as long as the loop is around,
8711 -- otherwise any operation carried out by the iterator will fail.
8713 and then not Is_Iterated_Container
(Obj_Id
, Decl
);
8714 end Is_Finalizable_Transient
;
8716 ---------------------------------
8717 -- Is_Fully_Repped_Tagged_Type --
8718 ---------------------------------
8720 function Is_Fully_Repped_Tagged_Type
(T
: Entity_Id
) return Boolean is
8721 U
: constant Entity_Id
:= Underlying_Type
(T
);
8725 if No
(U
) or else not Is_Tagged_Type
(U
) then
8727 elsif Has_Discriminants
(U
) then
8729 elsif not Has_Specified_Layout
(U
) then
8733 -- Here we have a tagged type, see if it has any component (other than
8734 -- tag and parent) with no component_clause. If so, we return False.
8736 Comp
:= First_Component
(U
);
8737 while Present
(Comp
) loop
8738 if not Is_Tag
(Comp
)
8739 and then Chars
(Comp
) /= Name_uParent
8740 and then No
(Component_Clause
(Comp
))
8744 Next_Component
(Comp
);
8748 -- All components have clauses
8751 end Is_Fully_Repped_Tagged_Type
;
8753 ----------------------------------
8754 -- Is_Library_Level_Tagged_Type --
8755 ----------------------------------
8757 function Is_Library_Level_Tagged_Type
(Typ
: Entity_Id
) return Boolean is
8759 return Is_Tagged_Type
(Typ
) and then Is_Library_Level_Entity
(Typ
);
8760 end Is_Library_Level_Tagged_Type
;
8762 --------------------------
8763 -- Is_Non_BIP_Func_Call --
8764 --------------------------
8766 function Is_Non_BIP_Func_Call
(Expr
: Node_Id
) return Boolean is
8768 -- The expected call is of the format
8770 -- Func_Call'reference
8773 Nkind
(Expr
) = N_Reference
8774 and then Nkind
(Prefix
(Expr
)) = N_Function_Call
8775 and then not Is_Build_In_Place_Function_Call
(Prefix
(Expr
));
8776 end Is_Non_BIP_Func_Call
;
8778 ----------------------------------
8779 -- Is_Possibly_Unaligned_Object --
8780 ----------------------------------
8782 function Is_Possibly_Unaligned_Object
(N
: Node_Id
) return Boolean is
8783 T
: constant Entity_Id
:= Etype
(N
);
8786 -- If renamed object, apply test to underlying object
8788 if Is_Entity_Name
(N
)
8789 and then Is_Object
(Entity
(N
))
8790 and then Present
(Renamed_Object
(Entity
(N
)))
8792 return Is_Possibly_Unaligned_Object
(Renamed_Object
(Entity
(N
)));
8795 -- Tagged and controlled types and aliased types are always aligned, as
8796 -- are concurrent types.
8799 or else Has_Controlled_Component
(T
)
8800 or else Is_Concurrent_Type
(T
)
8801 or else Is_Tagged_Type
(T
)
8802 or else Is_Controlled
(T
)
8807 -- If this is an element of a packed array, may be unaligned
8809 if Is_Ref_To_Bit_Packed_Array
(N
) then
8813 -- Case of indexed component reference: test whether prefix is unaligned
8815 if Nkind
(N
) = N_Indexed_Component
then
8816 return Is_Possibly_Unaligned_Object
(Prefix
(N
));
8818 -- Case of selected component reference
8820 elsif Nkind
(N
) = N_Selected_Component
then
8822 P
: constant Node_Id
:= Prefix
(N
);
8823 C
: constant Entity_Id
:= Entity
(Selector_Name
(N
));
8828 -- If component reference is for an array with nonstatic bounds,
8829 -- then it is always aligned: we can only process unaligned arrays
8830 -- with static bounds (more precisely compile time known bounds).
8832 if Is_Array_Type
(T
)
8833 and then not Compile_Time_Known_Bounds
(T
)
8838 -- If component is aliased, it is definitely properly aligned
8840 if Is_Aliased
(C
) then
8844 -- If component is for a type implemented as a scalar, and the
8845 -- record is packed, and the component is other than the first
8846 -- component of the record, then the component may be unaligned.
8848 if Is_Packed
(Etype
(P
))
8849 and then Represented_As_Scalar
(Etype
(C
))
8850 and then First_Entity
(Scope
(C
)) /= C
8855 -- Compute maximum possible alignment for T
8857 -- If alignment is known, then that settles things
8859 if Known_Alignment
(T
) then
8860 M
:= UI_To_Int
(Alignment
(T
));
8862 -- If alignment is not known, tentatively set max alignment
8865 M
:= Ttypes
.Maximum_Alignment
;
8867 -- We can reduce this if the Esize is known since the default
8868 -- alignment will never be more than the smallest power of 2
8869 -- that does not exceed this Esize value.
8871 if Known_Esize
(T
) then
8872 S
:= UI_To_Int
(Esize
(T
));
8874 while (M
/ 2) >= S
loop
8880 -- Case of component clause present which may specify an
8881 -- unaligned position.
8883 if Present
(Component_Clause
(C
)) then
8885 -- Otherwise we can do a test to make sure that the actual
8886 -- start position in the record, and the length, are both
8887 -- consistent with the required alignment. If not, we know
8888 -- that we are unaligned.
8891 Align_In_Bits
: constant Nat
:= M
* System_Storage_Unit
;
8897 -- For a component inherited in a record extension, the
8898 -- clause is inherited but position and size are not set.
8900 if Is_Base_Type
(Etype
(P
))
8901 and then Is_Tagged_Type
(Etype
(P
))
8902 and then Present
(Original_Record_Component
(Comp
))
8904 Comp
:= Original_Record_Component
(Comp
);
8907 if Component_Bit_Offset
(Comp
) mod Align_In_Bits
/= 0
8908 or else Esize
(Comp
) mod Align_In_Bits
/= 0
8915 -- Otherwise, for a component reference, test prefix
8917 return Is_Possibly_Unaligned_Object
(P
);
8920 -- If not a component reference, must be aligned
8925 end Is_Possibly_Unaligned_Object
;
8927 ---------------------------------
8928 -- Is_Possibly_Unaligned_Slice --
8929 ---------------------------------
8931 function Is_Possibly_Unaligned_Slice
(N
: Node_Id
) return Boolean is
8933 -- Go to renamed object
8935 if Is_Entity_Name
(N
)
8936 and then Is_Object
(Entity
(N
))
8937 and then Present
(Renamed_Object
(Entity
(N
)))
8939 return Is_Possibly_Unaligned_Slice
(Renamed_Object
(Entity
(N
)));
8942 -- The reference must be a slice
8944 if Nkind
(N
) /= N_Slice
then
8948 -- If it is a slice, then look at the array type being sliced
8951 Sarr
: constant Node_Id
:= Prefix
(N
);
8952 -- Prefix of the slice, i.e. the array being sliced
8954 Styp
: constant Entity_Id
:= Etype
(Prefix
(N
));
8955 -- Type of the array being sliced
8961 -- The problems arise if the array object that is being sliced
8962 -- is a component of a record or array, and we cannot guarantee
8963 -- the alignment of the array within its containing object.
8965 -- To investigate this, we look at successive prefixes to see
8966 -- if we have a worrisome indexed or selected component.
8970 -- Case of array is part of an indexed component reference
8972 if Nkind
(Pref
) = N_Indexed_Component
then
8973 Ptyp
:= Etype
(Prefix
(Pref
));
8975 -- The only problematic case is when the array is packed, in
8976 -- which case we really know nothing about the alignment of
8977 -- individual components.
8979 if Is_Bit_Packed_Array
(Ptyp
) then
8983 -- Case of array is part of a selected component reference
8985 elsif Nkind
(Pref
) = N_Selected_Component
then
8986 Ptyp
:= Etype
(Prefix
(Pref
));
8988 -- We are definitely in trouble if the record in question
8989 -- has an alignment, and either we know this alignment is
8990 -- inconsistent with the alignment of the slice, or we don't
8991 -- know what the alignment of the slice should be. But this
8992 -- really matters only if the target has strict alignment.
8994 if Target_Strict_Alignment
8995 and then Known_Alignment
(Ptyp
)
8996 and then (not Known_Alignment
(Styp
)
8997 or else Alignment
(Styp
) > Alignment
(Ptyp
))
9002 -- We are in potential trouble if the record type is packed.
9003 -- We could special case when we know that the array is the
9004 -- first component, but that's not such a simple case ???
9006 if Is_Packed
(Ptyp
) then
9010 -- We are in trouble if there is a component clause, and
9011 -- either we do not know the alignment of the slice, or
9012 -- the alignment of the slice is inconsistent with the
9013 -- bit position specified by the component clause.
9016 Field
: constant Entity_Id
:= Entity
(Selector_Name
(Pref
));
9018 if Present
(Component_Clause
(Field
))
9020 (not Known_Alignment
(Styp
)
9022 (Component_Bit_Offset
(Field
) mod
9023 (System_Storage_Unit
* Alignment
(Styp
))) /= 0)
9029 -- For cases other than selected or indexed components we know we
9030 -- are OK, since no issues arise over alignment.
9036 -- We processed an indexed component or selected component
9037 -- reference that looked safe, so keep checking prefixes.
9039 Pref
:= Prefix
(Pref
);
9042 end Is_Possibly_Unaligned_Slice
;
9044 -------------------------------
9045 -- Is_Related_To_Func_Return --
9046 -------------------------------
9048 function Is_Related_To_Func_Return
(Id
: Entity_Id
) return Boolean is
9049 Expr
: constant Node_Id
:= Related_Expression
(Id
);
9051 -- In the case of a function with a class-wide result that returns
9052 -- a call to a function with a specific result, we introduce a
9053 -- type conversion for the return expression. We do not want that
9054 -- type conversion to influence the result of this function.
9058 and then Nkind
(Unqual_Conv
(Expr
)) = N_Explicit_Dereference
9059 and then Nkind
(Parent
(Expr
)) = N_Simple_Return_Statement
;
9060 end Is_Related_To_Func_Return
;
9062 --------------------------------
9063 -- Is_Ref_To_Bit_Packed_Array --
9064 --------------------------------
9066 function Is_Ref_To_Bit_Packed_Array
(N
: Node_Id
) return Boolean is
9071 if Is_Entity_Name
(N
)
9072 and then Is_Object
(Entity
(N
))
9073 and then Present
(Renamed_Object
(Entity
(N
)))
9075 return Is_Ref_To_Bit_Packed_Array
(Renamed_Object
(Entity
(N
)));
9078 if Nkind
(N
) in N_Indexed_Component | N_Selected_Component
then
9079 if Is_Bit_Packed_Array
(Etype
(Prefix
(N
))) then
9082 Result
:= Is_Ref_To_Bit_Packed_Array
(Prefix
(N
));
9085 if Result
and then Nkind
(N
) = N_Indexed_Component
then
9086 Expr
:= First
(Expressions
(N
));
9087 while Present
(Expr
) loop
9088 Force_Evaluation
(Expr
);
9098 end Is_Ref_To_Bit_Packed_Array
;
9100 --------------------------------
9101 -- Is_Ref_To_Bit_Packed_Slice --
9102 --------------------------------
9104 function Is_Ref_To_Bit_Packed_Slice
(N
: Node_Id
) return Boolean is
9106 if Nkind
(N
) = N_Type_Conversion
then
9107 return Is_Ref_To_Bit_Packed_Slice
(Expression
(N
));
9109 elsif Is_Entity_Name
(N
)
9110 and then Is_Object
(Entity
(N
))
9111 and then Present
(Renamed_Object
(Entity
(N
)))
9113 return Is_Ref_To_Bit_Packed_Slice
(Renamed_Object
(Entity
(N
)));
9115 elsif Nkind
(N
) = N_Slice
9116 and then Is_Bit_Packed_Array
(Etype
(Prefix
(N
)))
9120 elsif Nkind
(N
) in N_Indexed_Component | N_Selected_Component
then
9121 return Is_Ref_To_Bit_Packed_Slice
(Prefix
(N
));
9126 end Is_Ref_To_Bit_Packed_Slice
;
9128 -----------------------
9129 -- Is_Renamed_Object --
9130 -----------------------
9132 function Is_Renamed_Object
(N
: Node_Id
) return Boolean is
9133 Pnod
: constant Node_Id
:= Parent
(N
);
9134 Kind
: constant Node_Kind
:= Nkind
(Pnod
);
9136 if Kind
= N_Object_Renaming_Declaration
then
9138 elsif Kind
in N_Indexed_Component | N_Selected_Component
then
9139 return Is_Renamed_Object
(Pnod
);
9143 end Is_Renamed_Object
;
9145 --------------------------------------
9146 -- Is_Secondary_Stack_BIP_Func_Call --
9147 --------------------------------------
9149 function Is_Secondary_Stack_BIP_Func_Call
(Expr
: Node_Id
) return Boolean is
9151 Call
: Node_Id
:= Expr
;
9156 -- Build-in-place calls usually appear in 'reference format. Note that
9157 -- the accessibility check machinery may add an extra 'reference due to
9158 -- side effect removal.
9160 while Nkind
(Call
) = N_Reference
loop
9161 Call
:= Prefix
(Call
);
9164 Call
:= Unqual_Conv
(Call
);
9166 if Is_Build_In_Place_Function_Call
(Call
) then
9168 -- Examine all parameter associations of the function call
9170 Param
:= First
(Parameter_Associations
(Call
));
9171 while Present
(Param
) loop
9172 if Nkind
(Param
) = N_Parameter_Association
then
9173 Formal
:= Selector_Name
(Param
);
9174 Actual
:= Explicit_Actual_Parameter
(Param
);
9176 -- A match for BIPalloc => 2 has been found
9178 if Is_Build_In_Place_Entity
(Formal
)
9179 and then BIP_Suffix_Kind
(Formal
) = BIP_Alloc_Form
9180 and then Nkind
(Actual
) = N_Integer_Literal
9181 and then Intval
(Actual
) = Uint_2
9192 end Is_Secondary_Stack_BIP_Func_Call
;
9194 -------------------------------------
9195 -- Is_Tag_To_Class_Wide_Conversion --
9196 -------------------------------------
9198 function Is_Tag_To_Class_Wide_Conversion
9199 (Obj_Id
: Entity_Id
) return Boolean
9201 Expr
: constant Node_Id
:= Expression
(Parent
(Obj_Id
));
9205 Is_Class_Wide_Type
(Etype
(Obj_Id
))
9206 and then Present
(Expr
)
9207 and then Nkind
(Expr
) = N_Unchecked_Type_Conversion
9208 and then Is_RTE
(Etype
(Expression
(Expr
)), RE_Tag
);
9209 end Is_Tag_To_Class_Wide_Conversion
;
9211 --------------------------------
9212 -- Is_Uninitialized_Aggregate --
9213 --------------------------------
9215 function Is_Uninitialized_Aggregate
9217 T
: Entity_Id
) return Boolean
9220 Comp_Type
: Entity_Id
;
9224 if Nkind
(Exp
) /= N_Aggregate
then
9228 Preanalyze_And_Resolve
(Exp
, T
);
9232 or else Ekind
(Typ
) /= E_Array_Subtype
9233 or else Present
(Expressions
(Exp
))
9234 or else No
(Component_Associations
(Exp
))
9238 Comp_Type
:= Component_Type
(Typ
);
9239 Comp
:= First
(Component_Associations
(Exp
));
9241 if not Box_Present
(Comp
)
9242 or else Present
(Next
(Comp
))
9247 return Is_Scalar_Type
(Comp_Type
)
9248 and then No
(Default_Aspect_Component_Value
(Typ
));
9250 end Is_Uninitialized_Aggregate
;
9252 ----------------------------
9253 -- Is_Untagged_Derivation --
9254 ----------------------------
9256 function Is_Untagged_Derivation
(T
: Entity_Id
) return Boolean is
9258 return (not Is_Tagged_Type
(T
) and then Is_Derived_Type
(T
))
9260 (Is_Private_Type
(T
) and then Present
(Full_View
(T
))
9261 and then not Is_Tagged_Type
(Full_View
(T
))
9262 and then Is_Derived_Type
(Full_View
(T
))
9263 and then Etype
(Full_View
(T
)) /= T
);
9264 end Is_Untagged_Derivation
;
9266 ------------------------------------
9267 -- Is_Untagged_Private_Derivation --
9268 ------------------------------------
9270 function Is_Untagged_Private_Derivation
9271 (Priv_Typ
: Entity_Id
;
9272 Full_Typ
: Entity_Id
) return Boolean
9277 and then Is_Untagged_Derivation
(Priv_Typ
)
9278 and then Is_Private_Type
(Etype
(Priv_Typ
))
9279 and then Present
(Full_Typ
)
9280 and then Is_Itype
(Full_Typ
);
9281 end Is_Untagged_Private_Derivation
;
9283 ------------------------------
9284 -- Is_Verifiable_DIC_Pragma --
9285 ------------------------------
9287 function Is_Verifiable_DIC_Pragma
(Prag
: Node_Id
) return Boolean is
9288 Args
: constant List_Id
:= Pragma_Argument_Associations
(Prag
);
9291 -- To qualify as verifiable, a DIC pragma must have a non-null argument
9296 -- If there are args, but the first arg is Empty, then treat the
9297 -- pragma the same as having no args (there may be a second arg that
9298 -- is an implicitly added type arg, and Empty is a placeholder).
9300 and then Present
(Get_Pragma_Arg
(First
(Args
)))
9302 and then Nkind
(Get_Pragma_Arg
(First
(Args
))) /= N_Null
;
9303 end Is_Verifiable_DIC_Pragma
;
9305 ---------------------------
9306 -- Is_Volatile_Reference --
9307 ---------------------------
9309 function Is_Volatile_Reference
(N
: Node_Id
) return Boolean is
9311 -- Only source references are to be treated as volatile, internally
9312 -- generated stuff cannot have volatile external effects.
9314 if not Comes_From_Source
(N
) then
9317 -- Never true for reference to a type
9319 elsif Is_Entity_Name
(N
) and then Is_Type
(Entity
(N
)) then
9322 -- Never true for a compile time known constant
9324 elsif Compile_Time_Known_Value
(N
) then
9327 -- True if object reference with volatile type
9329 elsif Is_Volatile_Object_Ref
(N
) then
9332 -- True if reference to volatile entity
9334 elsif Is_Entity_Name
(N
) then
9335 return Treat_As_Volatile
(Entity
(N
));
9337 -- True for slice of volatile array
9339 elsif Nkind
(N
) = N_Slice
then
9340 return Is_Volatile_Reference
(Prefix
(N
));
9342 -- True if volatile component
9344 elsif Nkind
(N
) in N_Indexed_Component | N_Selected_Component
then
9345 if (Is_Entity_Name
(Prefix
(N
))
9346 and then Has_Volatile_Components
(Entity
(Prefix
(N
))))
9347 or else (Present
(Etype
(Prefix
(N
)))
9348 and then Has_Volatile_Components
(Etype
(Prefix
(N
))))
9352 return Is_Volatile_Reference
(Prefix
(N
));
9360 end Is_Volatile_Reference
;
9362 --------------------
9363 -- Kill_Dead_Code --
9364 --------------------
9366 procedure Kill_Dead_Code
(N
: Node_Id
; Warn
: Boolean := False) is
9367 W
: Boolean := Warn
;
9368 -- Set False if warnings suppressed
9372 Remove_Warning_Messages
(N
);
9374 -- Update the internal structures of the ABE mechanism in case the
9375 -- dead node is an elaboration scenario.
9377 Kill_Elaboration_Scenario
(N
);
9379 -- Generate warning if appropriate
9383 -- We suppress the warning if this code is under control of an
9384 -- if/case statement and either
9385 -- a) we are in an instance and the condition/selector
9386 -- has a statically known value; or
9387 -- b) the condition/selector is a simple identifier and
9388 -- warnings off is set for this identifier.
9389 -- Dead code is common and reasonable in instances, so we don't
9390 -- want a warning in that case.
9393 C
: Node_Id
:= Empty
;
9395 if Nkind
(Parent
(N
)) = N_If_Statement
then
9396 C
:= Condition
(Parent
(N
));
9397 elsif Nkind
(Parent
(N
)) = N_Case_Statement_Alternative
then
9398 C
:= Expression
(Parent
(Parent
(N
)));
9402 if (In_Instance
and Compile_Time_Known_Value
(C
))
9403 or else (Nkind
(C
) = N_Identifier
9404 and then Present
(Entity
(C
))
9405 and then Has_Warnings_Off
(Entity
(C
)))
9412 -- Generate warning if not suppressed
9416 ("?t?this code can never be executed and has been deleted!",
9421 -- Recurse into block statements and bodies to process declarations
9424 if Nkind
(N
) = N_Block_Statement
9425 or else Nkind
(N
) = N_Subprogram_Body
9426 or else Nkind
(N
) = N_Package_Body
9428 Kill_Dead_Code
(Declarations
(N
), False);
9429 Kill_Dead_Code
(Statements
(Handled_Statement_Sequence
(N
)));
9431 if Nkind
(N
) = N_Subprogram_Body
then
9432 Set_Is_Eliminated
(Defining_Entity
(N
));
9435 elsif Nkind
(N
) = N_Package_Declaration
then
9436 Kill_Dead_Code
(Visible_Declarations
(Specification
(N
)));
9437 Kill_Dead_Code
(Private_Declarations
(Specification
(N
)));
9439 -- ??? After this point, Delete_Tree has been called on all
9440 -- declarations in Specification (N), so references to entities
9441 -- therein look suspicious.
9444 E
: Entity_Id
:= First_Entity
(Defining_Entity
(N
));
9447 while Present
(E
) loop
9448 if Ekind
(E
) = E_Operator
then
9449 Set_Is_Eliminated
(E
);
9456 -- Recurse into composite statement to kill individual statements in
9457 -- particular instantiations.
9459 elsif Nkind
(N
) = N_If_Statement
then
9460 Kill_Dead_Code
(Then_Statements
(N
));
9461 Kill_Dead_Code
(Elsif_Parts
(N
));
9462 Kill_Dead_Code
(Else_Statements
(N
));
9464 elsif Nkind
(N
) = N_Loop_Statement
then
9465 Kill_Dead_Code
(Statements
(N
));
9467 elsif Nkind
(N
) = N_Case_Statement
then
9471 Alt
:= First
(Alternatives
(N
));
9472 while Present
(Alt
) loop
9473 Kill_Dead_Code
(Statements
(Alt
));
9478 elsif Nkind
(N
) = N_Case_Statement_Alternative
then
9479 Kill_Dead_Code
(Statements
(N
));
9481 -- Deal with dead instances caused by deleting instantiations
9483 elsif Nkind
(N
) in N_Generic_Instantiation
then
9484 Remove_Dead_Instance
(N
);
9489 -- Case where argument is a list of nodes to be killed
9491 procedure Kill_Dead_Code
(L
: List_Id
; Warn
: Boolean := False) is
9498 if Is_Non_Empty_List
(L
) then
9500 while Present
(N
) loop
9501 Kill_Dead_Code
(N
, W
);
9508 -----------------------------
9509 -- Make_CW_Equivalent_Type --
9510 -----------------------------
9512 -- Create a record type used as an equivalent of any member of the class
9513 -- which takes its size from exp.
9515 -- Generate the following code:
9517 -- type Equiv_T is record
9518 -- _parent : T (List of discriminant constraints taken from Exp);
9519 -- Ext__50 : Storage_Array (1 .. (Exp'size - Typ'object_size)/8);
9522 -- ??? Note that this type does not guarantee same alignment as all
9525 -- Note: for the freezing circuitry, this looks like a record extension,
9526 -- and so we need to make sure that the scalar storage order is the same
9527 -- as that of the parent type. (This does not change anything for the
9528 -- representation of the extension part.)
9530 function Make_CW_Equivalent_Type
9532 E
: Node_Id
) return Entity_Id
9534 Loc
: constant Source_Ptr
:= Sloc
(E
);
9535 Root_Typ
: constant Entity_Id
:= Root_Type
(T
);
9536 Root_Utyp
: constant Entity_Id
:= Underlying_Type
(Root_Typ
);
9537 List_Def
: constant List_Id
:= Empty_List
;
9538 Comp_List
: constant List_Id
:= New_List
;
9539 Equiv_Type
: Entity_Id
;
9540 Range_Type
: Entity_Id
;
9541 Str_Type
: Entity_Id
;
9542 Constr_Root
: Entity_Id
;
9546 -- If the root type is already constrained, there are no discriminants
9547 -- in the expression.
9549 if not Has_Discriminants
(Root_Typ
)
9550 or else Is_Constrained
(Root_Typ
)
9552 Constr_Root
:= Root_Typ
;
9554 -- At this point in the expansion, nonlimited view of the type
9555 -- must be available, otherwise the error will be reported later.
9557 if From_Limited_With
(Constr_Root
)
9558 and then Present
(Non_Limited_View
(Constr_Root
))
9560 Constr_Root
:= Non_Limited_View
(Constr_Root
);
9564 Constr_Root
:= Make_Temporary
(Loc
, 'R');
9566 -- subtype cstr__n is T (List of discr constraints taken from Exp)
9568 Append_To
(List_Def
,
9569 Make_Subtype_Declaration
(Loc
,
9570 Defining_Identifier
=> Constr_Root
,
9571 Subtype_Indication
=> Make_Subtype_From_Expr
(E
, Root_Typ
)));
9574 -- Generate the range subtype declaration
9576 Range_Type
:= Make_Temporary
(Loc
, 'G');
9578 if not Is_Interface
(Root_Typ
) then
9580 -- subtype rg__xx is
9581 -- Storage_Offset range 1 .. (Expr'size - typ'size) / Storage_Unit
9584 Make_Op_Subtract
(Loc
,
9586 Make_Attribute_Reference
(Loc
,
9588 OK_Convert_To
(T
, Duplicate_Subexpr_No_Checks
(E
)),
9589 Attribute_Name
=> Name_Size
),
9591 Make_Attribute_Reference
(Loc
,
9592 Prefix
=> New_Occurrence_Of
(Constr_Root
, Loc
),
9593 Attribute_Name
=> Name_Object_Size
));
9595 -- subtype rg__xx is
9596 -- Storage_Offset range 1 .. Expr'size / Storage_Unit
9599 Make_Attribute_Reference
(Loc
,
9601 OK_Convert_To
(T
, Duplicate_Subexpr_No_Checks
(E
)),
9602 Attribute_Name
=> Name_Size
);
9605 Set_Paren_Count
(Sizexpr
, 1);
9607 Append_To
(List_Def
,
9608 Make_Subtype_Declaration
(Loc
,
9609 Defining_Identifier
=> Range_Type
,
9610 Subtype_Indication
=>
9611 Make_Subtype_Indication
(Loc
,
9612 Subtype_Mark
=> New_Occurrence_Of
(RTE
(RE_Storage_Offset
), Loc
),
9613 Constraint
=> Make_Range_Constraint
(Loc
,
9616 Low_Bound
=> Make_Integer_Literal
(Loc
, 1),
9618 Make_Op_Divide
(Loc
,
9619 Left_Opnd
=> Sizexpr
,
9620 Right_Opnd
=> Make_Integer_Literal
(Loc
,
9621 Intval
=> System_Storage_Unit
)))))));
9623 -- subtype str__nn is Storage_Array (rg__x);
9625 Str_Type
:= Make_Temporary
(Loc
, 'S');
9626 Append_To
(List_Def
,
9627 Make_Subtype_Declaration
(Loc
,
9628 Defining_Identifier
=> Str_Type
,
9629 Subtype_Indication
=>
9630 Make_Subtype_Indication
(Loc
,
9631 Subtype_Mark
=> New_Occurrence_Of
(RTE
(RE_Storage_Array
), Loc
),
9633 Make_Index_Or_Discriminant_Constraint
(Loc
,
9635 New_List
(New_Occurrence_Of
(Range_Type
, Loc
))))));
9637 -- type Equiv_T is record
9638 -- [ _parent : Tnn; ]
9642 Equiv_Type
:= Make_Temporary
(Loc
, 'T');
9643 Mutate_Ekind
(Equiv_Type
, E_Record_Type
);
9644 Set_Parent_Subtype
(Equiv_Type
, Constr_Root
);
9646 -- Set Is_Class_Wide_Equivalent_Type very early to trigger the special
9647 -- treatment for this type. In particular, even though _parent's type
9648 -- is a controlled type or contains controlled components, we do not
9649 -- want to set Has_Controlled_Component on it to avoid making it gain
9650 -- an unwanted _controller component.
9652 Set_Is_Class_Wide_Equivalent_Type
(Equiv_Type
);
9654 -- A class-wide equivalent type does not require initialization
9656 Set_Suppress_Initialization
(Equiv_Type
);
9658 if not Is_Interface
(Root_Typ
) then
9659 Append_To
(Comp_List
,
9660 Make_Component_Declaration
(Loc
,
9661 Defining_Identifier
=>
9662 Make_Defining_Identifier
(Loc
, Name_uParent
),
9663 Component_Definition
=>
9664 Make_Component_Definition
(Loc
,
9665 Aliased_Present
=> False,
9666 Subtype_Indication
=> New_Occurrence_Of
(Constr_Root
, Loc
))));
9668 Set_Reverse_Storage_Order
9669 (Equiv_Type
, Reverse_Storage_Order
(Base_Type
(Root_Utyp
)));
9670 Set_Reverse_Bit_Order
9671 (Equiv_Type
, Reverse_Bit_Order
(Base_Type
(Root_Utyp
)));
9674 Append_To
(Comp_List
,
9675 Make_Component_Declaration
(Loc
,
9676 Defining_Identifier
=> Make_Temporary
(Loc
, 'C'),
9677 Component_Definition
=>
9678 Make_Component_Definition
(Loc
,
9679 Aliased_Present
=> False,
9680 Subtype_Indication
=> New_Occurrence_Of
(Str_Type
, Loc
))));
9682 Append_To
(List_Def
,
9683 Make_Full_Type_Declaration
(Loc
,
9684 Defining_Identifier
=> Equiv_Type
,
9686 Make_Record_Definition
(Loc
,
9688 Make_Component_List
(Loc
,
9689 Component_Items
=> Comp_List
,
9690 Variant_Part
=> Empty
))));
9692 -- Suppress all checks during the analysis of the expanded code to avoid
9693 -- the generation of spurious warnings under ZFP run-time.
9695 Insert_Actions
(E
, List_Def
, Suppress
=> All_Checks
);
9697 end Make_CW_Equivalent_Type
;
9699 -------------------------
9700 -- Make_Invariant_Call --
9701 -------------------------
9703 function Make_Invariant_Call
(Expr
: Node_Id
) return Node_Id
is
9704 Loc
: constant Source_Ptr
:= Sloc
(Expr
);
9705 Typ
: constant Entity_Id
:= Base_Type
(Etype
(Expr
));
9706 pragma Assert
(Has_Invariants
(Typ
));
9707 Proc_Id
: constant Entity_Id
:= Invariant_Procedure
(Typ
);
9708 pragma Assert
(Present
(Proc_Id
));
9710 -- The invariant procedure has a null body if assertions are disabled or
9711 -- Assertion_Policy Ignore is in effect. In that case, generate a null
9712 -- statement instead of a call to the invariant procedure.
9714 if Has_Null_Body
(Proc_Id
) then
9715 return Make_Null_Statement
(Loc
);
9718 Make_Procedure_Call_Statement
(Loc
,
9719 Name
=> New_Occurrence_Of
(Proc_Id
, Loc
),
9720 Parameter_Associations
=> New_List
(Relocate_Node
(Expr
)));
9722 end Make_Invariant_Call
;
9724 ------------------------
9725 -- Make_Literal_Range --
9726 ------------------------
9728 function Make_Literal_Range
9730 Literal_Typ
: Entity_Id
) return Node_Id
9732 Lo
: constant Node_Id
:=
9733 New_Copy_Tree
(String_Literal_Low_Bound
(Literal_Typ
));
9734 Index
: constant Entity_Id
:= Etype
(Lo
);
9735 Length_Expr
: constant Node_Id
:=
9736 Make_Op_Subtract
(Loc
,
9738 Make_Integer_Literal
(Loc
,
9739 Intval
=> String_Literal_Length
(Literal_Typ
)),
9740 Right_Opnd
=> Make_Integer_Literal
(Loc
, 1));
9745 Set_Analyzed
(Lo
, False);
9747 if Is_Integer_Type
(Index
) then
9750 Left_Opnd
=> New_Copy_Tree
(Lo
),
9751 Right_Opnd
=> Length_Expr
);
9754 Make_Attribute_Reference
(Loc
,
9755 Attribute_Name
=> Name_Val
,
9756 Prefix
=> New_Occurrence_Of
(Index
, Loc
),
9757 Expressions
=> New_List
(
9760 Make_Attribute_Reference
(Loc
,
9761 Attribute_Name
=> Name_Pos
,
9762 Prefix
=> New_Occurrence_Of
(Index
, Loc
),
9763 Expressions
=> New_List
(New_Copy_Tree
(Lo
))),
9764 Right_Opnd
=> Length_Expr
)));
9771 end Make_Literal_Range
;
9773 --------------------------
9774 -- Make_Non_Empty_Check --
9775 --------------------------
9777 function Make_Non_Empty_Check
9779 N
: Node_Id
) return Node_Id
9785 Make_Attribute_Reference
(Loc
,
9786 Attribute_Name
=> Name_Length
,
9787 Prefix
=> Duplicate_Subexpr_No_Checks
(N
, Name_Req
=> True)),
9789 Make_Integer_Literal
(Loc
, 0));
9790 end Make_Non_Empty_Check
;
9792 -------------------------
9793 -- Make_Predicate_Call --
9794 -------------------------
9796 -- WARNING: This routine manages Ghost regions. Return statements must be
9797 -- replaced by gotos which jump to the end of the routine and restore the
9800 function Make_Predicate_Call
9803 Mem
: Boolean := False) return Node_Id
9805 Loc
: constant Source_Ptr
:= Sloc
(Expr
);
9807 Saved_GM
: constant Ghost_Mode_Type
:= Ghost_Mode
;
9808 Saved_IGR
: constant Node_Id
:= Ignored_Ghost_Region
;
9809 -- Save the Ghost-related attributes to restore on exit
9812 Func_Id
: Entity_Id
;
9815 Func_Id
:= Predicate_Function
(Typ
);
9816 pragma Assert
(Present
(Func_Id
));
9818 -- The related type may be subject to pragma Ghost. Set the mode now to
9819 -- ensure that the call is properly marked as Ghost.
9821 Set_Ghost_Mode
(Typ
);
9823 -- Call special membership version if requested and available
9825 if Mem
and then Present
(Predicate_Function_M
(Typ
)) then
9826 Func_Id
:= Predicate_Function_M
(Typ
);
9829 -- Case of calling normal predicate function
9831 -- If the type is tagged, the expression may be class-wide, in which
9832 -- case it has to be converted to its root type, given that the
9833 -- generated predicate function is not dispatching. The conversion is
9834 -- type-safe and does not need validation, which matters when private
9835 -- extensions are involved.
9837 if Is_Tagged_Type
(Typ
) then
9839 Make_Function_Call
(Loc
,
9840 Name
=> New_Occurrence_Of
(Func_Id
, Loc
),
9841 Parameter_Associations
=>
9842 New_List
(OK_Convert_To
(Typ
, Relocate_Node
(Expr
))));
9845 Make_Function_Call
(Loc
,
9846 Name
=> New_Occurrence_Of
(Func_Id
, Loc
),
9847 Parameter_Associations
=> New_List
(Relocate_Node
(Expr
)));
9850 Restore_Ghost_Region
(Saved_GM
, Saved_IGR
);
9853 end Make_Predicate_Call
;
9855 --------------------------
9856 -- Make_Predicate_Check --
9857 --------------------------
9859 function Make_Predicate_Check
9861 Expr
: Node_Id
) return Node_Id
9863 Loc
: constant Source_Ptr
:= Sloc
(Expr
);
9865 procedure Add_Failure_Expression
(Args
: List_Id
);
9866 -- Add the failure expression of pragma Predicate_Failure (if any) to
9869 ----------------------------
9870 -- Add_Failure_Expression --
9871 ----------------------------
9873 procedure Add_Failure_Expression
(Args
: List_Id
) is
9874 function Failure_Expression
return Node_Id
;
9875 pragma Inline
(Failure_Expression
);
9876 -- Find aspect or pragma Predicate_Failure that applies to type Typ
9877 -- and return its expression. Return Empty if no such annotation is
9880 function Is_OK_PF_Aspect
(Asp
: Node_Id
) return Boolean;
9881 pragma Inline
(Is_OK_PF_Aspect
);
9882 -- Determine whether aspect Asp is a suitable Predicate_Failure
9883 -- aspect that applies to type Typ.
9885 function Is_OK_PF_Pragma
(Prag
: Node_Id
) return Boolean;
9886 pragma Inline
(Is_OK_PF_Pragma
);
9887 -- Determine whether pragma Prag is a suitable Predicate_Failure
9888 -- pragma that applies to type Typ.
9890 procedure Replace_Subtype_Reference
(N
: Node_Id
);
9891 -- Replace the current instance of type Typ denoted by N with
9894 ------------------------
9895 -- Failure_Expression --
9896 ------------------------
9898 function Failure_Expression
return Node_Id
is
9902 -- The management of the rep item chain involves "inheritance" of
9903 -- parent type chains. If a parent [sub]type is already subject to
9904 -- pragma Predicate_Failure, then the pragma will also appear in
9905 -- the chain of the child [sub]type, which in turn may possess a
9906 -- pragma of its own. Avoid order-dependent issues by inspecting
9907 -- the rep item chain directly. Note that routine Get_Pragma may
9908 -- return a parent pragma.
9910 Item
:= First_Rep_Item
(Typ
);
9911 while Present
(Item
) loop
9913 -- Predicate_Failure appears as an aspect
9915 if Nkind
(Item
) = N_Aspect_Specification
9916 and then Is_OK_PF_Aspect
(Item
)
9918 return Expression
(Item
);
9920 -- Predicate_Failure appears as a pragma
9922 elsif Nkind
(Item
) = N_Pragma
9923 and then Is_OK_PF_Pragma
(Item
)
9927 (Next
(First
(Pragma_Argument_Associations
(Item
))));
9930 Next_Rep_Item
(Item
);
9934 end Failure_Expression
;
9936 ---------------------
9937 -- Is_OK_PF_Aspect --
9938 ---------------------
9940 function Is_OK_PF_Aspect
(Asp
: Node_Id
) return Boolean is
9942 -- To qualify, the aspect must apply to the type subjected to the
9946 Chars
(Identifier
(Asp
)) = Name_Predicate_Failure
9947 and then Present
(Entity
(Asp
))
9948 and then Entity
(Asp
) = Typ
;
9949 end Is_OK_PF_Aspect
;
9951 ---------------------
9952 -- Is_OK_PF_Pragma --
9953 ---------------------
9955 function Is_OK_PF_Pragma
(Prag
: Node_Id
) return Boolean is
9956 Args
: constant List_Id
:= Pragma_Argument_Associations
(Prag
);
9960 -- Nothing to do when the pragma does not denote Predicate_Failure
9962 if Pragma_Name
(Prag
) /= Name_Predicate_Failure
then
9965 -- Nothing to do when the pragma lacks arguments, in which case it
9968 elsif No
(Args
) or else Is_Empty_List
(Args
) then
9972 Typ_Arg
:= Get_Pragma_Arg
(First
(Args
));
9974 -- To qualify, the local name argument of the pragma must denote
9975 -- the type subjected to the predicate check.
9978 Is_Entity_Name
(Typ_Arg
)
9979 and then Present
(Entity
(Typ_Arg
))
9980 and then Entity
(Typ_Arg
) = Typ
;
9981 end Is_OK_PF_Pragma
;
9983 --------------------------------
9984 -- Replace_Subtype_Reference --
9985 --------------------------------
9987 procedure Replace_Subtype_Reference
(N
: Node_Id
) is
9989 Rewrite
(N
, New_Copy_Tree
(Expr
));
9990 end Replace_Subtype_Reference
;
9992 procedure Replace_Subtype_References
is
9993 new Replace_Type_References_Generic
(Replace_Subtype_Reference
);
9997 PF_Expr
: constant Node_Id
:= Failure_Expression
;
10000 -- Start of processing for Add_Failure_Expression
10003 if Present
(PF_Expr
) then
10005 -- Replace any occurrences of the current instance of the type
10006 -- with the object subjected to the predicate check.
10008 Expr
:= New_Copy_Tree
(PF_Expr
);
10009 Replace_Subtype_References
(Expr
, Typ
);
10011 -- The failure expression appears as the third argument of the
10015 Make_Pragma_Argument_Association
(Loc
,
10016 Expression
=> Expr
));
10018 end Add_Failure_Expression
;
10025 -- Start of processing for Make_Predicate_Check
10028 -- If predicate checks are suppressed, then return a null statement. For
10029 -- this call, we check only the scope setting. If the caller wants to
10030 -- check a specific entity's setting, they must do it manually.
10032 if Predicate_Checks_Suppressed
(Empty
) then
10033 return Make_Null_Statement
(Loc
);
10036 -- Do not generate a check within stream functions and the like.
10038 if not Predicate_Check_In_Scope
(Expr
) then
10039 return Make_Null_Statement
(Loc
);
10042 -- Compute proper name to use, we need to get this right so that the
10043 -- right set of check policies apply to the Check pragma we are making.
10045 if Has_Dynamic_Predicate_Aspect
(Typ
) then
10046 Nam
:= Name_Dynamic_Predicate
;
10047 elsif Has_Static_Predicate_Aspect
(Typ
) then
10048 Nam
:= Name_Static_Predicate
;
10050 Nam
:= Name_Predicate
;
10054 Make_Pragma_Argument_Association
(Loc
,
10055 Expression
=> Make_Identifier
(Loc
, Nam
)),
10056 Make_Pragma_Argument_Association
(Loc
,
10057 Expression
=> Make_Predicate_Call
(Typ
, Expr
)));
10059 -- If the subtype is subject to pragma Predicate_Failure, add the
10060 -- failure expression as an additional parameter.
10062 Add_Failure_Expression
(Args
);
10066 Chars
=> Name_Check
,
10067 Pragma_Argument_Associations
=> Args
);
10068 end Make_Predicate_Check
;
10070 ----------------------------
10071 -- Make_Subtype_From_Expr --
10072 ----------------------------
10074 -- 1. If Expr is an unconstrained array expression, creates
10075 -- Unc_Type(Expr'first(1)..Expr'last(1),..., Expr'first(n)..Expr'last(n))
10077 -- 2. If Expr is a unconstrained discriminated type expression, creates
10078 -- Unc_Type(Expr.Discr1, ... , Expr.Discr_n)
10080 -- 3. If Expr is class-wide, creates an implicit class-wide subtype
10082 function Make_Subtype_From_Expr
10084 Unc_Typ
: Entity_Id
;
10085 Related_Id
: Entity_Id
:= Empty
) return Node_Id
10087 List_Constr
: constant List_Id
:= New_List
;
10088 Loc
: constant Source_Ptr
:= Sloc
(E
);
10090 Full_Exp
: Node_Id
;
10091 Full_Subtyp
: Entity_Id
;
10092 High_Bound
: Entity_Id
;
10093 Index_Typ
: Entity_Id
;
10094 Low_Bound
: Entity_Id
;
10095 Priv_Subtyp
: Entity_Id
;
10099 if Is_Private_Type
(Unc_Typ
)
10100 and then Has_Unknown_Discriminants
(Unc_Typ
)
10102 -- The caller requests a unique external name for both the private
10103 -- and the full subtype.
10105 if Present
(Related_Id
) then
10107 Make_Defining_Identifier
(Loc
,
10108 Chars
=> New_External_Name
(Chars
(Related_Id
), 'C'));
10110 Make_Defining_Identifier
(Loc
,
10111 Chars
=> New_External_Name
(Chars
(Related_Id
), 'P'));
10114 Full_Subtyp
:= Make_Temporary
(Loc
, 'C');
10115 Priv_Subtyp
:= Make_Temporary
(Loc
, 'P');
10118 -- Prepare the subtype completion. Use the base type to find the
10119 -- underlying type because the type may be a generic actual or an
10120 -- explicit subtype.
10122 Utyp
:= Underlying_Type
(Base_Type
(Unc_Typ
));
10125 Unchecked_Convert_To
(Utyp
, Duplicate_Subexpr_No_Checks
(E
));
10126 Set_Parent
(Full_Exp
, Parent
(E
));
10129 Make_Subtype_Declaration
(Loc
,
10130 Defining_Identifier
=> Full_Subtyp
,
10131 Subtype_Indication
=> Make_Subtype_From_Expr
(Full_Exp
, Utyp
)));
10133 -- Define the dummy private subtype
10135 Mutate_Ekind
(Priv_Subtyp
, Subtype_Kind
(Ekind
(Unc_Typ
)));
10136 Set_Etype
(Priv_Subtyp
, Base_Type
(Unc_Typ
));
10137 Set_Scope
(Priv_Subtyp
, Full_Subtyp
);
10138 Set_Is_Constrained
(Priv_Subtyp
);
10139 Set_Is_Tagged_Type
(Priv_Subtyp
, Is_Tagged_Type
(Unc_Typ
));
10140 Set_Is_Itype
(Priv_Subtyp
);
10141 Set_Associated_Node_For_Itype
(Priv_Subtyp
, E
);
10143 if Is_Tagged_Type
(Priv_Subtyp
) then
10144 Set_Class_Wide_Type
10145 (Base_Type
(Priv_Subtyp
), Class_Wide_Type
(Unc_Typ
));
10146 Set_Direct_Primitive_Operations
(Priv_Subtyp
,
10147 Direct_Primitive_Operations
(Unc_Typ
));
10150 Set_Full_View
(Priv_Subtyp
, Full_Subtyp
);
10152 return New_Occurrence_Of
(Priv_Subtyp
, Loc
);
10154 elsif Is_Array_Type
(Unc_Typ
) then
10155 Index_Typ
:= First_Index
(Unc_Typ
);
10156 for J
in 1 .. Number_Dimensions
(Unc_Typ
) loop
10158 -- Capture the bounds of each index constraint in case the context
10159 -- is an object declaration of an unconstrained type initialized
10160 -- by a function call:
10162 -- Obj : Unconstr_Typ := Func_Call;
10164 -- This scenario requires secondary scope management and the index
10165 -- constraint cannot depend on the temporary used to capture the
10166 -- result of the function call.
10169 -- Temp : Unconstr_Typ_Ptr := Func_Call'reference;
10170 -- subtype S is Unconstr_Typ (Temp.all'First .. Temp.all'Last);
10171 -- Obj : S := Temp.all;
10172 -- SS_Release; -- Temp is gone at this point, bounds of S are
10173 -- -- non existent.
10176 -- Low_Bound : constant Base_Type (Index_Typ) := E'First (J);
10178 Low_Bound
:= Make_Temporary
(Loc
, 'B');
10180 Make_Object_Declaration
(Loc
,
10181 Defining_Identifier
=> Low_Bound
,
10182 Object_Definition
=>
10183 New_Occurrence_Of
(Base_Type
(Etype
(Index_Typ
)), Loc
),
10184 Constant_Present
=> True,
10186 Make_Attribute_Reference
(Loc
,
10187 Prefix
=> Duplicate_Subexpr_No_Checks
(E
),
10188 Attribute_Name
=> Name_First
,
10189 Expressions
=> New_List
(
10190 Make_Integer_Literal
(Loc
, J
)))));
10193 -- High_Bound : constant Base_Type (Index_Typ) := E'Last (J);
10195 High_Bound
:= Make_Temporary
(Loc
, 'B');
10197 Make_Object_Declaration
(Loc
,
10198 Defining_Identifier
=> High_Bound
,
10199 Object_Definition
=>
10200 New_Occurrence_Of
(Base_Type
(Etype
(Index_Typ
)), Loc
),
10201 Constant_Present
=> True,
10203 Make_Attribute_Reference
(Loc
,
10204 Prefix
=> Duplicate_Subexpr_No_Checks
(E
),
10205 Attribute_Name
=> Name_Last
,
10206 Expressions
=> New_List
(
10207 Make_Integer_Literal
(Loc
, J
)))));
10209 Append_To
(List_Constr
,
10211 Low_Bound
=> New_Occurrence_Of
(Low_Bound
, Loc
),
10212 High_Bound
=> New_Occurrence_Of
(High_Bound
, Loc
)));
10214 Next_Index
(Index_Typ
);
10217 elsif Is_Class_Wide_Type
(Unc_Typ
) then
10219 CW_Subtype
: Entity_Id
;
10220 EQ_Typ
: Entity_Id
:= Empty
;
10223 -- A class-wide equivalent type is not needed on VM targets
10224 -- because the VM back-ends handle the class-wide object
10225 -- initialization itself (and doesn't need or want the
10226 -- additional intermediate type to handle the assignment).
10228 if Expander_Active
and then Tagged_Type_Expansion
then
10230 -- If this is the class-wide type of a completion that is a
10231 -- record subtype, set the type of the class-wide type to be
10232 -- the full base type, for use in the expanded code for the
10233 -- equivalent type. Should this be done earlier when the
10234 -- completion is analyzed ???
10236 if Is_Private_Type
(Etype
(Unc_Typ
))
10238 Ekind
(Full_View
(Etype
(Unc_Typ
))) = E_Record_Subtype
10240 Set_Etype
(Unc_Typ
, Base_Type
(Full_View
(Etype
(Unc_Typ
))));
10243 EQ_Typ
:= Make_CW_Equivalent_Type
(Unc_Typ
, E
);
10246 CW_Subtype
:= New_Class_Wide_Subtype
(Unc_Typ
, E
);
10247 Set_Equivalent_Type
(CW_Subtype
, EQ_Typ
);
10248 Set_Cloned_Subtype
(CW_Subtype
, Base_Type
(Unc_Typ
));
10250 return New_Occurrence_Of
(CW_Subtype
, Loc
);
10253 -- Indefinite record type with discriminants
10256 D
:= First_Discriminant
(Unc_Typ
);
10257 while Present
(D
) loop
10258 Append_To
(List_Constr
,
10259 Make_Selected_Component
(Loc
,
10260 Prefix
=> Duplicate_Subexpr_No_Checks
(E
),
10261 Selector_Name
=> New_Occurrence_Of
(D
, Loc
)));
10263 Next_Discriminant
(D
);
10268 Make_Subtype_Indication
(Loc
,
10269 Subtype_Mark
=> New_Occurrence_Of
(Unc_Typ
, Loc
),
10271 Make_Index_Or_Discriminant_Constraint
(Loc
,
10272 Constraints
=> List_Constr
));
10273 end Make_Subtype_From_Expr
;
10275 -----------------------------
10276 -- Make_Variant_Comparison --
10277 -----------------------------
10279 function Make_Variant_Comparison
10282 Curr_Val
: Node_Id
;
10283 Old_Val
: Node_Id
) return Node_Id
10286 if Mode
= Name_Increases
then
10287 return Make_Op_Gt
(Loc
, Curr_Val
, Old_Val
);
10288 else pragma Assert
(Mode
= Name_Decreases
);
10289 return Make_Op_Lt
(Loc
, Curr_Val
, Old_Val
);
10291 end Make_Variant_Comparison
;
10297 procedure Map_Formals
10298 (Parent_Subp
: Entity_Id
;
10299 Derived_Subp
: Entity_Id
;
10300 Force_Update
: Boolean := False)
10302 Par_Formal
: Entity_Id
:= First_Formal
(Parent_Subp
);
10303 Subp_Formal
: Entity_Id
:= First_Formal
(Derived_Subp
);
10306 if Force_Update
then
10307 Type_Map
.Set
(Parent_Subp
, Derived_Subp
);
10310 -- At this stage either we are under regular processing and the caller
10311 -- has previously ensured that these primitives are already mapped (by
10312 -- means of calling previously to Update_Primitives_Mapping), or we are
10313 -- processing a late-overriding primitive and Force_Update updated above
10314 -- the mapping of these primitives.
10316 while Present
(Par_Formal
) and then Present
(Subp_Formal
) loop
10317 Type_Map
.Set
(Par_Formal
, Subp_Formal
);
10318 Next_Formal
(Par_Formal
);
10319 Next_Formal
(Subp_Formal
);
10327 procedure Map_Types
(Parent_Type
: Entity_Id
; Derived_Type
: Entity_Id
) is
10329 -- NOTE: Most of the routines in Map_Types are intentionally unnested to
10330 -- avoid deep indentation of code.
10332 -- NOTE: Routines which deal with discriminant mapping operate on the
10333 -- [underlying/record] full view of various types because those views
10334 -- contain all discriminants and stored constraints.
10336 procedure Add_Primitive
(Prim
: Entity_Id
; Par_Typ
: Entity_Id
);
10337 -- Subsidiary to Map_Primitives. Find a primitive in the inheritance or
10338 -- overriding chain starting from Prim whose dispatching type is parent
10339 -- type Par_Typ and add a mapping between the result and primitive Prim.
10341 function Ancestor_Primitive
(Subp
: Entity_Id
) return Entity_Id
;
10342 -- Subsidiary to Map_Primitives. Return the next ancestor primitive in
10343 -- the inheritance or overriding chain of subprogram Subp. Return Empty
10344 -- if no such primitive is available.
10346 function Build_Chain
10347 (Par_Typ
: Entity_Id
;
10348 Deriv_Typ
: Entity_Id
) return Elist_Id
;
10349 -- Subsidiary to Map_Discriminants. Recreate the derivation chain from
10350 -- parent type Par_Typ leading down towards derived type Deriv_Typ. The
10351 -- list has the form:
10355 -- <Ancestor_N> -> <Ancestor_N-1> -> <Ancestor_1> -> Deriv_Typ
10357 -- Note that Par_Typ is not part of the resulting derivation chain
10359 function Discriminated_View
(Typ
: Entity_Id
) return Entity_Id
;
10360 -- Return the view of type Typ which could potentially contains either
10361 -- the discriminants or stored constraints of the type.
10363 function Find_Discriminant_Value
10364 (Discr
: Entity_Id
;
10365 Par_Typ
: Entity_Id
;
10366 Deriv_Typ
: Entity_Id
;
10367 Typ_Elmt
: Elmt_Id
) return Node_Or_Entity_Id
;
10368 -- Subsidiary to Map_Discriminants. Find the value of discriminant Discr
10369 -- in the derivation chain starting from parent type Par_Typ leading to
10370 -- derived type Deriv_Typ. The returned value is one of the following:
10372 -- * An entity which is either a discriminant or a nondiscriminant
10373 -- name, and renames/constraints Discr.
10375 -- * An expression which constraints Discr
10377 -- Typ_Elmt is an element of the derivation chain created by routine
10378 -- Build_Chain and denotes the current ancestor being examined.
10380 procedure Map_Discriminants
10381 (Par_Typ
: Entity_Id
;
10382 Deriv_Typ
: Entity_Id
);
10383 -- Map each discriminant of type Par_Typ to a meaningful constraint
10384 -- from the point of view of type Deriv_Typ.
10386 procedure Map_Primitives
(Par_Typ
: Entity_Id
; Deriv_Typ
: Entity_Id
);
10387 -- Map each primitive of type Par_Typ to a corresponding primitive of
10390 -------------------
10391 -- Add_Primitive --
10392 -------------------
10394 procedure Add_Primitive
(Prim
: Entity_Id
; Par_Typ
: Entity_Id
) is
10395 Par_Prim
: Entity_Id
;
10398 -- Inspect the inheritance chain through the Alias attribute and the
10399 -- overriding chain through the Overridden_Operation looking for an
10400 -- ancestor primitive with the appropriate dispatching type.
10403 while Present
(Par_Prim
) loop
10404 exit when Find_Dispatching_Type
(Par_Prim
) = Par_Typ
;
10405 Par_Prim
:= Ancestor_Primitive
(Par_Prim
);
10408 -- Create a mapping of the form:
10410 -- parent type primitive -> derived type primitive
10412 if Present
(Par_Prim
) then
10413 Type_Map
.Set
(Par_Prim
, Prim
);
10417 ------------------------
10418 -- Ancestor_Primitive --
10419 ------------------------
10421 function Ancestor_Primitive
(Subp
: Entity_Id
) return Entity_Id
is
10422 Inher_Prim
: constant Entity_Id
:= Alias
(Subp
);
10423 Over_Prim
: constant Entity_Id
:= Overridden_Operation
(Subp
);
10426 -- The current subprogram overrides an ancestor primitive
10428 if Present
(Over_Prim
) then
10431 -- The current subprogram is an internally generated alias of an
10432 -- inherited ancestor primitive.
10434 elsif Present
(Inher_Prim
) then
10435 -- It is possible that an internally generated alias could be
10436 -- set to a subprogram which overrides the same aliased primitive,
10437 -- so return Empty in this case.
10439 if Ancestor_Primitive
(Inher_Prim
) = Subp
then
10445 -- Otherwise the current subprogram is the root of the inheritance or
10446 -- overriding chain.
10451 end Ancestor_Primitive
;
10457 function Build_Chain
10458 (Par_Typ
: Entity_Id
;
10459 Deriv_Typ
: Entity_Id
) return Elist_Id
10461 Anc_Typ
: Entity_Id
;
10463 Curr_Typ
: Entity_Id
;
10466 Chain
:= New_Elmt_List
;
10468 -- Add the derived type to the derivation chain
10470 Prepend_Elmt
(Deriv_Typ
, Chain
);
10472 -- Examine all ancestors starting from the derived type climbing
10473 -- towards parent type Par_Typ.
10475 Curr_Typ
:= Deriv_Typ
;
10477 -- Handle the case where the current type is a record which
10478 -- derives from a subtype.
10480 -- subtype Sub_Typ is Par_Typ ...
10481 -- type Deriv_Typ is Sub_Typ ...
10483 if Ekind
(Curr_Typ
) = E_Record_Type
10484 and then Present
(Parent_Subtype
(Curr_Typ
))
10486 Anc_Typ
:= Parent_Subtype
(Curr_Typ
);
10488 -- Handle the case where the current type is a record subtype of
10489 -- another subtype.
10491 -- subtype Sub_Typ1 is Par_Typ ...
10492 -- subtype Sub_Typ2 is Sub_Typ1 ...
10494 elsif Ekind
(Curr_Typ
) = E_Record_Subtype
10495 and then Present
(Cloned_Subtype
(Curr_Typ
))
10497 Anc_Typ
:= Cloned_Subtype
(Curr_Typ
);
10499 -- Otherwise use the direct parent type
10502 Anc_Typ
:= Etype
(Curr_Typ
);
10505 -- Use the first subtype when dealing with itypes
10507 if Is_Itype
(Anc_Typ
) then
10508 Anc_Typ
:= First_Subtype
(Anc_Typ
);
10511 -- Work with the view which contains the discriminants and stored
10514 Anc_Typ
:= Discriminated_View
(Anc_Typ
);
10516 -- Stop the climb when either the parent type has been reached or
10517 -- there are no more ancestors left to examine.
10519 exit when Anc_Typ
= Curr_Typ
or else Anc_Typ
= Par_Typ
;
10521 Prepend_Unique_Elmt
(Anc_Typ
, Chain
);
10522 Curr_Typ
:= Anc_Typ
;
10528 ------------------------
10529 -- Discriminated_View --
10530 ------------------------
10532 function Discriminated_View
(Typ
: Entity_Id
) return Entity_Id
is
10538 -- Use the [underlying] full view when dealing with private types
10539 -- because the view contains all inherited discriminants or stored
10542 if Is_Private_Type
(T
) then
10543 if Present
(Underlying_Full_View
(T
)) then
10544 T
:= Underlying_Full_View
(T
);
10546 elsif Present
(Full_View
(T
)) then
10547 T
:= Full_View
(T
);
10551 -- Use the underlying record view when the type is an extenstion of
10552 -- a parent type with unknown discriminants because the view contains
10553 -- all inherited discriminants or stored constraints.
10555 if Ekind
(T
) = E_Record_Type
10556 and then Present
(Underlying_Record_View
(T
))
10558 T
:= Underlying_Record_View
(T
);
10562 end Discriminated_View
;
10564 -----------------------------
10565 -- Find_Discriminant_Value --
10566 -----------------------------
10568 function Find_Discriminant_Value
10569 (Discr
: Entity_Id
;
10570 Par_Typ
: Entity_Id
;
10571 Deriv_Typ
: Entity_Id
;
10572 Typ_Elmt
: Elmt_Id
) return Node_Or_Entity_Id
10574 Discr_Pos
: constant Uint
:= Discriminant_Number
(Discr
);
10575 Typ
: constant Entity_Id
:= Node
(Typ_Elmt
);
10577 function Find_Constraint_Value
10578 (Constr
: Node_Or_Entity_Id
) return Node_Or_Entity_Id
;
10579 -- Given constraint Constr, find what it denotes. This is either:
10581 -- * An entity which is either a discriminant or a name
10585 ---------------------------
10586 -- Find_Constraint_Value --
10587 ---------------------------
10589 function Find_Constraint_Value
10590 (Constr
: Node_Or_Entity_Id
) return Node_Or_Entity_Id
10593 if Nkind
(Constr
) in N_Entity
then
10595 -- The constraint denotes a discriminant of the curren type
10596 -- which renames the ancestor discriminant:
10599 -- type Typ (D1 : ...; DN : ...) is
10600 -- new Anc (Discr => D1) with ...
10603 if Ekind
(Constr
) = E_Discriminant
then
10605 -- The discriminant belongs to derived type Deriv_Typ. This
10606 -- is the final value for the ancestor discriminant as the
10607 -- derivations chain has been fully exhausted.
10609 if Typ
= Deriv_Typ
then
10612 -- Otherwise the discriminant may be renamed or constrained
10613 -- at a lower level. Continue looking down the derivation
10618 Find_Discriminant_Value
10620 Par_Typ
=> Par_Typ
,
10621 Deriv_Typ
=> Deriv_Typ
,
10622 Typ_Elmt
=> Next_Elmt
(Typ_Elmt
));
10625 -- Otherwise the constraint denotes a reference to some name
10626 -- which results in a Stored discriminant:
10630 -- type Typ (D1 : ...; DN : ...) is
10631 -- new Anc (Discr => Name) with ...
10634 -- Return the name as this is the proper constraint of the
10641 -- The constraint denotes a reference to a name
10643 elsif Is_Entity_Name
(Constr
) then
10644 return Find_Constraint_Value
(Entity
(Constr
));
10646 -- Otherwise the current constraint is an expression which yields
10647 -- a Stored discriminant:
10649 -- type Typ (D1 : ...; DN : ...) is
10650 -- new Anc (Discr => <expression>) with ...
10653 -- Return the expression as this is the proper constraint of the
10659 end Find_Constraint_Value
;
10663 Constrs
: constant Elist_Id
:= Stored_Constraint
(Typ
);
10665 Constr_Elmt
: Elmt_Id
;
10667 Typ_Discr
: Entity_Id
;
10669 -- Start of processing for Find_Discriminant_Value
10672 -- The algorithm for finding the value of a discriminant works as
10673 -- follows. First, it recreates the derivation chain from Par_Typ
10674 -- to Deriv_Typ as a list:
10676 -- Par_Typ (shown for completeness)
10678 -- Ancestor_N <-- head of chain
10682 -- Deriv_Typ <-- tail of chain
10684 -- The algorithm then traces the fate of a parent discriminant down
10685 -- the derivation chain. At each derivation level, the discriminant
10686 -- may be either inherited or constrained.
10688 -- 1) Discriminant is inherited: there are two cases, depending on
10689 -- which type is inheriting.
10691 -- 1.1) Deriv_Typ is inheriting:
10693 -- type Ancestor (D_1 : ...) is tagged ...
10694 -- type Deriv_Typ is new Ancestor ...
10696 -- In this case the inherited discriminant is the final value of
10697 -- the parent discriminant because the end of the derivation chain
10698 -- has been reached.
10700 -- 1.2) Some other type is inheriting:
10702 -- type Ancestor_1 (D_1 : ...) is tagged ...
10703 -- type Ancestor_2 is new Ancestor_1 ...
10705 -- In this case the algorithm continues to trace the fate of the
10706 -- inherited discriminant down the derivation chain because it may
10707 -- be further inherited or constrained.
10709 -- 2) Discriminant is constrained: there are three cases, depending
10710 -- on what the constraint is.
10712 -- 2.1) The constraint is another discriminant (aka renaming):
10714 -- type Ancestor_1 (D_1 : ...) is tagged ...
10715 -- type Ancestor_2 (D_2 : ...) is new Ancestor_1 (D_1 => D_2) ...
10717 -- In this case the constraining discriminant becomes the one to
10718 -- track down the derivation chain. The algorithm already knows
10719 -- that D_2 constrains D_1, therefore if the algorithm finds the
10720 -- value of D_2, then this would also be the value for D_1.
10722 -- 2.2) The constraint is a name (aka Stored):
10725 -- type Ancestor_1 (D_1 : ...) is tagged ...
10726 -- type Ancestor_2 is new Ancestor_1 (D_1 => Name) ...
10728 -- In this case the name is the final value of D_1 because the
10729 -- discriminant cannot be further constrained.
10731 -- 2.3) The constraint is an expression (aka Stored):
10733 -- type Ancestor_1 (D_1 : ...) is tagged ...
10734 -- type Ancestor_2 is new Ancestor_1 (D_1 => 1 + 2) ...
10736 -- Similar to 2.2, the expression is the final value of D_1
10740 -- When a derived type constrains its parent type, all constaints
10741 -- appear in the Stored_Constraint list. Examine the list looking
10742 -- for a positional match.
10744 if Present
(Constrs
) then
10745 Constr_Elmt
:= First_Elmt
(Constrs
);
10746 while Present
(Constr_Elmt
) loop
10748 -- The position of the current constraint matches that of the
10749 -- ancestor discriminant.
10751 if Pos
= Discr_Pos
then
10752 return Find_Constraint_Value
(Node
(Constr_Elmt
));
10755 Next_Elmt
(Constr_Elmt
);
10759 -- Otherwise the derived type does not constraint its parent type in
10760 -- which case it inherits the parent discriminants.
10763 Typ_Discr
:= First_Discriminant
(Typ
);
10764 while Present
(Typ_Discr
) loop
10766 -- The position of the current discriminant matches that of the
10767 -- ancestor discriminant.
10769 if Pos
= Discr_Pos
then
10770 return Find_Constraint_Value
(Typ_Discr
);
10773 Next_Discriminant
(Typ_Discr
);
10778 -- A discriminant must always have a corresponding value. This is
10779 -- either another discriminant, a name, or an expression. If this
10780 -- point is reached, them most likely the derivation chain employs
10781 -- the wrong views of types.
10783 pragma Assert
(False);
10786 end Find_Discriminant_Value
;
10788 -----------------------
10789 -- Map_Discriminants --
10790 -----------------------
10792 procedure Map_Discriminants
10793 (Par_Typ
: Entity_Id
;
10794 Deriv_Typ
: Entity_Id
)
10796 Deriv_Chain
: constant Elist_Id
:= Build_Chain
(Par_Typ
, Deriv_Typ
);
10799 Discr_Val
: Node_Or_Entity_Id
;
10802 -- Examine each discriminant of parent type Par_Typ and find a
10803 -- suitable value for it from the point of view of derived type
10806 if Has_Discriminants
(Par_Typ
) then
10807 Discr
:= First_Discriminant
(Par_Typ
);
10808 while Present
(Discr
) loop
10810 Find_Discriminant_Value
10812 Par_Typ
=> Par_Typ
,
10813 Deriv_Typ
=> Deriv_Typ
,
10814 Typ_Elmt
=> First_Elmt
(Deriv_Chain
));
10816 -- Create a mapping of the form:
10818 -- parent type discriminant -> value
10820 Type_Map
.Set
(Discr
, Discr_Val
);
10822 Next_Discriminant
(Discr
);
10825 end Map_Discriminants
;
10827 --------------------
10828 -- Map_Primitives --
10829 --------------------
10831 procedure Map_Primitives
(Par_Typ
: Entity_Id
; Deriv_Typ
: Entity_Id
) is
10832 Deriv_Prim
: Entity_Id
;
10833 Par_Prim
: Entity_Id
;
10834 Par_Prims
: Elist_Id
;
10835 Prim_Elmt
: Elmt_Id
;
10838 -- Inspect the primitives of the derived type and determine whether
10839 -- they relate to the primitives of the parent type. If there is a
10840 -- meaningful relation, create a mapping of the form:
10842 -- parent type primitive -> derived type primitive
10844 if Present
(Direct_Primitive_Operations
(Deriv_Typ
)) then
10845 Prim_Elmt
:= First_Elmt
(Direct_Primitive_Operations
(Deriv_Typ
));
10846 while Present
(Prim_Elmt
) loop
10847 Deriv_Prim
:= Node
(Prim_Elmt
);
10849 if Is_Subprogram
(Deriv_Prim
)
10850 and then Find_Dispatching_Type
(Deriv_Prim
) = Deriv_Typ
10852 Add_Primitive
(Deriv_Prim
, Par_Typ
);
10855 Next_Elmt
(Prim_Elmt
);
10859 -- If the parent operation is an interface operation, the overriding
10860 -- indicator is not present. Instead, we get from the interface
10861 -- operation the primitive of the current type that implements it.
10863 if Is_Interface
(Par_Typ
) then
10864 Par_Prims
:= Collect_Primitive_Operations
(Par_Typ
);
10866 if Present
(Par_Prims
) then
10867 Prim_Elmt
:= First_Elmt
(Par_Prims
);
10869 while Present
(Prim_Elmt
) loop
10870 Par_Prim
:= Node
(Prim_Elmt
);
10872 Find_Primitive_Covering_Interface
(Deriv_Typ
, Par_Prim
);
10874 if Present
(Deriv_Prim
) then
10875 Type_Map
.Set
(Par_Prim
, Deriv_Prim
);
10878 Next_Elmt
(Prim_Elmt
);
10882 end Map_Primitives
;
10884 -- Start of processing for Map_Types
10887 -- Nothing to do if there are no types to work with
10889 if No
(Parent_Type
) or else No
(Derived_Type
) then
10892 -- Nothing to do if the mapping already exists
10894 elsif Type_Map
.Get
(Parent_Type
) = Derived_Type
then
10897 -- Nothing to do if both types are not tagged. Note that untagged types
10898 -- do not have primitive operations and their discriminants are already
10899 -- handled by gigi.
10901 elsif not Is_Tagged_Type
(Parent_Type
)
10902 or else not Is_Tagged_Type
(Derived_Type
)
10907 -- Create a mapping of the form
10909 -- parent type -> derived type
10911 -- to prevent any subsequent attempts to produce the same relations
10913 Type_Map
.Set
(Parent_Type
, Derived_Type
);
10915 -- Create mappings of the form
10917 -- parent type discriminant -> derived type discriminant
10919 -- parent type discriminant -> constraint
10921 -- Note that mapping of discriminants breaks privacy because it needs to
10922 -- work with those views which contains the discriminants and any stored
10926 (Par_Typ
=> Discriminated_View
(Parent_Type
),
10927 Deriv_Typ
=> Discriminated_View
(Derived_Type
));
10929 -- Create mappings of the form
10931 -- parent type primitive -> derived type primitive
10934 (Par_Typ
=> Parent_Type
,
10935 Deriv_Typ
=> Derived_Type
);
10938 ----------------------------
10939 -- Matching_Standard_Type --
10940 ----------------------------
10942 function Matching_Standard_Type
(Typ
: Entity_Id
) return Entity_Id
is
10943 pragma Assert
(Is_Scalar_Type
(Typ
));
10944 Siz
: constant Uint
:= Esize
(Typ
);
10947 -- Floating-point cases
10949 if Is_Floating_Point_Type
(Typ
) then
10950 if Siz
<= Esize
(Standard_Short_Float
) then
10951 return Standard_Short_Float
;
10952 elsif Siz
<= Esize
(Standard_Float
) then
10953 return Standard_Float
;
10954 elsif Siz
<= Esize
(Standard_Long_Float
) then
10955 return Standard_Long_Float
;
10956 elsif Siz
<= Esize
(Standard_Long_Long_Float
) then
10957 return Standard_Long_Long_Float
;
10959 raise Program_Error
;
10962 -- Integer cases (includes fixed-point types)
10964 -- Unsigned integer cases (includes normal enumeration types)
10967 return Small_Integer_Type_For
(Siz
, Is_Unsigned_Type
(Typ
));
10969 end Matching_Standard_Type
;
10971 -----------------------------
10972 -- May_Generate_Large_Temp --
10973 -----------------------------
10975 -- At the current time, the only types that we return False for (i.e. where
10976 -- we decide we know they cannot generate large temps) are ones where we
10977 -- know the size is 256 bits or less at compile time, and we are still not
10978 -- doing a thorough job on arrays and records.
10980 function May_Generate_Large_Temp
(Typ
: Entity_Id
) return Boolean is
10982 if not Size_Known_At_Compile_Time
(Typ
) then
10986 if Known_Esize
(Typ
) and then Esize
(Typ
) <= 256 then
10990 if Is_Array_Type
(Typ
)
10991 and then Present
(Packed_Array_Impl_Type
(Typ
))
10993 return May_Generate_Large_Temp
(Packed_Array_Impl_Type
(Typ
));
10997 end May_Generate_Large_Temp
;
10999 --------------------------------------------
11000 -- Needs_Conditional_Null_Excluding_Check --
11001 --------------------------------------------
11003 function Needs_Conditional_Null_Excluding_Check
11004 (Typ
: Entity_Id
) return Boolean
11008 Is_Array_Type
(Typ
) and then Can_Never_Be_Null
(Component_Type
(Typ
));
11009 end Needs_Conditional_Null_Excluding_Check
;
11011 ----------------------------
11012 -- Needs_Constant_Address --
11013 ----------------------------
11015 function Needs_Constant_Address
11017 Typ
: Entity_Id
) return Boolean
11020 -- If we have no initialization of any kind, then we don't need to place
11021 -- any restrictions on the address clause, because the object will be
11022 -- elaborated after the address clause is evaluated. This happens if the
11023 -- declaration has no initial expression, or the type has no implicit
11024 -- initialization, or the object is imported.
11026 -- The same holds for all initialized scalar types and all access types.
11027 -- Packed bit array types of size up to the maximum integer size are
11028 -- represented using a modular type with an initialization (to zero) and
11029 -- can be processed like other initialized scalar types.
11031 -- If the type is controlled, code to attach the object to a
11032 -- finalization chain is generated at the point of declaration, and
11033 -- therefore the elaboration of the object cannot be delayed: the
11034 -- address expression must be a constant.
11036 if No
(Expression
(Decl
))
11037 and then not Needs_Finalization
(Typ
)
11039 (not Has_Non_Null_Base_Init_Proc
(Typ
)
11040 or else Is_Imported
(Defining_Identifier
(Decl
)))
11044 elsif (Present
(Expression
(Decl
)) and then Is_Scalar_Type
(Typ
))
11045 or else Is_Access_Type
(Typ
)
11047 (Is_Bit_Packed_Array
(Typ
)
11048 and then Is_Modular_Integer_Type
(Packed_Array_Impl_Type
(Typ
)))
11053 -- Otherwise, we require the address clause to be constant because
11054 -- the call to the initialization procedure (or the attach code) has
11055 -- to happen at the point of the declaration.
11057 -- Actually the IP call has been moved to the freeze actions anyway,
11058 -- so maybe we can relax this restriction???
11062 end Needs_Constant_Address
;
11064 ----------------------------
11065 -- New_Class_Wide_Subtype --
11066 ----------------------------
11068 function New_Class_Wide_Subtype
11069 (CW_Typ
: Entity_Id
;
11070 N
: Node_Id
) return Entity_Id
11072 Res
: constant Entity_Id
:= Create_Itype
(E_Void
, N
);
11074 -- Capture relevant attributes of the class-wide subtype which must be
11075 -- restored after the copy.
11077 Res_Chars
: constant Name_Id
:= Chars
(Res
);
11078 Res_Is_CGE
: constant Boolean := Is_Checked_Ghost_Entity
(Res
);
11079 Res_Is_IGE
: constant Boolean := Is_Ignored_Ghost_Entity
(Res
);
11080 Res_Is_IGN
: constant Boolean := Is_Ignored_Ghost_Node
(Res
);
11081 Res_Scope
: constant Entity_Id
:= Scope
(Res
);
11084 Copy_Node
(CW_Typ
, Res
);
11086 -- Restore the relevant attributes of the class-wide subtype
11088 Set_Chars
(Res
, Res_Chars
);
11089 Set_Is_Checked_Ghost_Entity
(Res
, Res_Is_CGE
);
11090 Set_Is_Ignored_Ghost_Entity
(Res
, Res_Is_IGE
);
11091 Set_Is_Ignored_Ghost_Node
(Res
, Res_Is_IGN
);
11092 Set_Scope
(Res
, Res_Scope
);
11094 -- Decorate the class-wide subtype
11096 Set_Associated_Node_For_Itype
(Res
, N
);
11097 Set_Comes_From_Source
(Res
, False);
11098 Mutate_Ekind
(Res
, E_Class_Wide_Subtype
);
11099 Set_Etype
(Res
, Base_Type
(CW_Typ
));
11100 Set_Freeze_Node
(Res
, Empty
);
11101 Set_Is_Frozen
(Res
, False);
11102 Set_Is_Itype
(Res
);
11103 Set_Is_Public
(Res
, False);
11104 Set_Next_Entity
(Res
, Empty
);
11105 Set_Prev_Entity
(Res
, Empty
);
11106 Set_Sloc
(Res
, Sloc
(N
));
11108 Set_Public_Status
(Res
);
11111 end New_Class_Wide_Subtype
;
11113 -----------------------------------
11114 -- OK_To_Do_Constant_Replacement --
11115 -----------------------------------
11117 function OK_To_Do_Constant_Replacement
(E
: Entity_Id
) return Boolean is
11118 ES
: constant Entity_Id
:= Scope
(E
);
11122 -- Do not replace statically allocated objects, because they may be
11123 -- modified outside the current scope.
11125 if Is_Statically_Allocated
(E
) then
11128 -- Do not replace aliased or volatile objects, since we don't know what
11129 -- else might change the value.
11131 elsif Is_Aliased
(E
) or else Treat_As_Volatile
(E
) then
11134 -- Debug flag -gnatdM disconnects this optimization
11136 elsif Debug_Flag_MM
then
11139 -- Otherwise check scopes
11142 CS
:= Current_Scope
;
11145 -- If we are in right scope, replacement is safe
11150 -- Packages do not affect the determination of safety
11152 elsif Ekind
(CS
) = E_Package
then
11153 exit when CS
= Standard_Standard
;
11156 -- Blocks do not affect the determination of safety
11158 elsif Ekind
(CS
) = E_Block
then
11161 -- Loops do not affect the determination of safety. Note that we
11162 -- kill all current values on entry to a loop, so we are just
11163 -- talking about processing within a loop here.
11165 elsif Ekind
(CS
) = E_Loop
then
11168 -- Otherwise, the reference is dubious, and we cannot be sure that
11169 -- it is safe to do the replacement.
11178 end OK_To_Do_Constant_Replacement
;
11180 ------------------------------------
11181 -- Possible_Bit_Aligned_Component --
11182 ------------------------------------
11184 function Possible_Bit_Aligned_Component
(N
: Node_Id
) return Boolean is
11186 -- Do not process an unanalyzed node because it is not yet decorated and
11187 -- most checks performed below will fail.
11189 if not Analyzed
(N
) then
11193 -- There are never alignment issues in CodePeer mode
11195 if CodePeer_Mode
then
11201 -- Case of indexed component
11203 when N_Indexed_Component
=>
11205 P
: constant Node_Id
:= Prefix
(N
);
11206 Ptyp
: constant Entity_Id
:= Etype
(P
);
11209 -- If we know the component size and it is not larger than the
11210 -- maximum integer size, then we are OK. The back end does the
11211 -- assignment of small misaligned objects correctly.
11213 if Known_Static_Component_Size
(Ptyp
)
11214 and then Component_Size
(Ptyp
) <= System_Max_Integer_Size
11218 -- Otherwise, we need to test the prefix, to see if we are
11219 -- indexing from a possibly unaligned component.
11222 return Possible_Bit_Aligned_Component
(P
);
11226 -- Case of selected component
11228 when N_Selected_Component
=>
11230 P
: constant Node_Id
:= Prefix
(N
);
11231 Comp
: constant Entity_Id
:= Entity
(Selector_Name
(N
));
11234 -- This is the crucial test: if the component itself causes
11235 -- trouble, then we can stop and return True.
11237 if Component_May_Be_Bit_Aligned
(Comp
) then
11240 -- Otherwise, we need to test the prefix, to see if we are
11241 -- selecting from a possibly unaligned component.
11244 return Possible_Bit_Aligned_Component
(P
);
11248 -- For a slice, test the prefix, if that is possibly misaligned,
11249 -- then for sure the slice is.
11252 return Possible_Bit_Aligned_Component
(Prefix
(N
));
11254 -- For an unchecked conversion, check whether the expression may
11257 when N_Unchecked_Type_Conversion
=>
11258 return Possible_Bit_Aligned_Component
(Expression
(N
));
11260 -- If we have none of the above, it means that we have fallen off the
11261 -- top testing prefixes recursively, and we now have a stand alone
11262 -- object, where we don't have a problem, unless this is a renaming,
11263 -- in which case we need to look into the renamed object.
11266 if Is_Entity_Name
(N
)
11267 and then Is_Object
(Entity
(N
))
11268 and then Present
(Renamed_Object
(Entity
(N
)))
11271 Possible_Bit_Aligned_Component
(Renamed_Object
(Entity
(N
)));
11276 end Possible_Bit_Aligned_Component
;
11278 -----------------------------------------------
11279 -- Process_Statements_For_Controlled_Objects --
11280 -----------------------------------------------
11282 procedure Process_Statements_For_Controlled_Objects
(N
: Node_Id
) is
11283 Loc
: constant Source_Ptr
:= Sloc
(N
);
11285 function Are_Wrapped
(L
: List_Id
) return Boolean;
11286 -- Determine whether list L contains only one statement which is a block
11288 function Wrap_Statements_In_Block
11290 Scop
: Entity_Id
:= Current_Scope
) return Node_Id
;
11291 -- Given a list of statements L, wrap it in a block statement and return
11292 -- the generated node. Scop is either the current scope or the scope of
11293 -- the context (if applicable).
11299 function Are_Wrapped
(L
: List_Id
) return Boolean is
11300 Stmt
: constant Node_Id
:= First
(L
);
11304 and then No
(Next
(Stmt
))
11305 and then Nkind
(Stmt
) = N_Block_Statement
;
11308 ------------------------------
11309 -- Wrap_Statements_In_Block --
11310 ------------------------------
11312 function Wrap_Statements_In_Block
11314 Scop
: Entity_Id
:= Current_Scope
) return Node_Id
11316 Block_Id
: Entity_Id
;
11317 Block_Nod
: Node_Id
;
11318 Iter_Loop
: Entity_Id
;
11322 Make_Block_Statement
(Loc
,
11323 Declarations
=> No_List
,
11324 Handled_Statement_Sequence
=>
11325 Make_Handled_Sequence_Of_Statements
(Loc
,
11328 -- Create a label for the block in case the block needs to manage the
11329 -- secondary stack. A label allows for flag Uses_Sec_Stack to be set.
11331 Add_Block_Identifier
(Block_Nod
, Block_Id
);
11333 -- When wrapping the statements of an iterator loop, check whether
11334 -- the loop requires secondary stack management and if so, propagate
11335 -- the appropriate flags to the block. This ensures that the cursor
11336 -- is properly cleaned up at each iteration of the loop.
11338 Iter_Loop
:= Find_Enclosing_Iterator_Loop
(Scop
);
11340 if Present
(Iter_Loop
) then
11341 Set_Uses_Sec_Stack
(Block_Id
, Uses_Sec_Stack
(Iter_Loop
));
11343 -- Secondary stack reclamation is suppressed when the associated
11344 -- iterator loop contains a return statement which uses the stack.
11346 Set_Sec_Stack_Needed_For_Return
11347 (Block_Id
, Sec_Stack_Needed_For_Return
(Iter_Loop
));
11351 end Wrap_Statements_In_Block
;
11357 -- Start of processing for Process_Statements_For_Controlled_Objects
11360 -- Whenever a non-handled statement list is wrapped in a block, the
11361 -- block must be explicitly analyzed to redecorate all entities in the
11362 -- list and ensure that a finalizer is properly built.
11365 when N_Conditional_Entry_Call
11368 | N_Selective_Accept
11370 -- Check the "then statements" for elsif parts and if statements
11372 if Nkind
(N
) in N_Elsif_Part | N_If_Statement
11373 and then not Is_Empty_List
(Then_Statements
(N
))
11374 and then not Are_Wrapped
(Then_Statements
(N
))
11375 and then Requires_Cleanup_Actions
11376 (L
=> Then_Statements
(N
),
11377 Lib_Level
=> False,
11378 Nested_Constructs
=> False)
11380 Block
:= Wrap_Statements_In_Block
(Then_Statements
(N
));
11381 Set_Then_Statements
(N
, New_List
(Block
));
11386 -- Check the "else statements" for conditional entry calls, if
11387 -- statements and selective accepts.
11390 N_Conditional_Entry_Call | N_If_Statement | N_Selective_Accept
11391 and then not Is_Empty_List
(Else_Statements
(N
))
11392 and then not Are_Wrapped
(Else_Statements
(N
))
11393 and then Requires_Cleanup_Actions
11394 (L
=> Else_Statements
(N
),
11395 Lib_Level
=> False,
11396 Nested_Constructs
=> False)
11398 Block
:= Wrap_Statements_In_Block
(Else_Statements
(N
));
11399 Set_Else_Statements
(N
, New_List
(Block
));
11404 when N_Abortable_Part
11405 | N_Accept_Alternative
11406 | N_Case_Statement_Alternative
11407 | N_Delay_Alternative
11408 | N_Entry_Call_Alternative
11409 | N_Exception_Handler
11411 | N_Triggering_Alternative
11413 if not Is_Empty_List
(Statements
(N
))
11414 and then not Are_Wrapped
(Statements
(N
))
11415 and then Requires_Cleanup_Actions
11416 (L
=> Statements
(N
),
11417 Lib_Level
=> False,
11418 Nested_Constructs
=> False)
11420 if Nkind
(N
) = N_Loop_Statement
11421 and then Present
(Identifier
(N
))
11424 Wrap_Statements_In_Block
11425 (L
=> Statements
(N
),
11426 Scop
=> Entity
(Identifier
(N
)));
11428 Block
:= Wrap_Statements_In_Block
(Statements
(N
));
11431 Set_Statements
(N
, New_List
(Block
));
11435 -- Could be e.g. a loop that was transformed into a block or null
11436 -- statement. Do nothing for terminate alternatives.
11438 when N_Block_Statement
11440 | N_Terminate_Alternative
11445 raise Program_Error
;
11447 end Process_Statements_For_Controlled_Objects
;
11453 function Power_Of_Two
(N
: Node_Id
) return Nat
is
11454 Typ
: constant Entity_Id
:= Etype
(N
);
11455 pragma Assert
(Is_Integer_Type
(Typ
));
11457 Siz
: constant Nat
:= UI_To_Int
(Esize
(Typ
));
11461 if not Compile_Time_Known_Value
(N
) then
11465 Val
:= Expr_Value
(N
);
11466 for J
in 1 .. Siz
- 1 loop
11467 if Val
= Uint_2
** J
then
11476 ----------------------
11477 -- Remove_Init_Call --
11478 ----------------------
11480 function Remove_Init_Call
11482 Rep_Clause
: Node_Id
) return Node_Id
11484 Par
: constant Node_Id
:= Parent
(Var
);
11485 Typ
: constant Entity_Id
:= Etype
(Var
);
11487 Init_Proc
: Entity_Id
;
11488 -- Initialization procedure for Typ
11490 function Find_Init_Call_In_List
(From
: Node_Id
) return Node_Id
;
11491 -- Look for init call for Var starting at From and scanning the
11492 -- enclosing list until Rep_Clause or the end of the list is reached.
11494 ----------------------------
11495 -- Find_Init_Call_In_List --
11496 ----------------------------
11498 function Find_Init_Call_In_List
(From
: Node_Id
) return Node_Id
is
11499 Init_Call
: Node_Id
;
11503 while Present
(Init_Call
) and then Init_Call
/= Rep_Clause
loop
11504 if Nkind
(Init_Call
) = N_Procedure_Call_Statement
11505 and then Is_Entity_Name
(Name
(Init_Call
))
11506 and then Entity
(Name
(Init_Call
)) = Init_Proc
11515 end Find_Init_Call_In_List
;
11517 Init_Call
: Node_Id
;
11519 -- Start of processing for Remove_Init_Call
11522 if Present
(Initialization_Statements
(Var
)) then
11523 Init_Call
:= Initialization_Statements
(Var
);
11524 Set_Initialization_Statements
(Var
, Empty
);
11526 elsif not Has_Non_Null_Base_Init_Proc
(Typ
) then
11528 -- No init proc for the type, so obviously no call to be found
11533 -- We might be able to handle other cases below by just properly
11534 -- setting Initialization_Statements at the point where the init proc
11535 -- call is generated???
11537 Init_Proc
:= Base_Init_Proc
(Typ
);
11539 -- First scan the list containing the declaration of Var
11541 Init_Call
:= Find_Init_Call_In_List
(From
=> Next
(Par
));
11543 -- If not found, also look on Var's freeze actions list, if any,
11544 -- since the init call may have been moved there (case of an address
11545 -- clause applying to Var).
11547 if No
(Init_Call
) and then Present
(Freeze_Node
(Var
)) then
11549 Find_Init_Call_In_List
(First
(Actions
(Freeze_Node
(Var
))));
11552 -- If the initialization call has actuals that use the secondary
11553 -- stack, the call may have been wrapped into a temporary block, in
11554 -- which case the block itself has to be removed.
11556 if No
(Init_Call
) and then Nkind
(Next
(Par
)) = N_Block_Statement
then
11558 Blk
: constant Node_Id
:= Next
(Par
);
11561 (Find_Init_Call_In_List
11562 (First
(Statements
(Handled_Statement_Sequence
(Blk
)))))
11570 if Present
(Init_Call
) then
11571 -- If restrictions have forbidden Aborts, the initialization call
11572 -- for objects that require deep initialization has not been wrapped
11573 -- into the following block (see Exp_Ch3, Default_Initialize_Object)
11574 -- so if present remove it as well, and include the IP call in it,
11575 -- in the rare case the caller may need to simply displace the
11576 -- initialization, as is done for a later address specification.
11578 if Nkind
(Next
(Init_Call
)) = N_Block_Statement
11579 and then Is_Initialization_Block
(Next
(Init_Call
))
11582 IP_Call
: constant Node_Id
:= Init_Call
;
11584 Init_Call
:= Next
(IP_Call
);
11587 Statements
(Handled_Statement_Sequence
(Init_Call
)));
11591 Remove
(Init_Call
);
11595 end Remove_Init_Call
;
11597 -------------------------
11598 -- Remove_Side_Effects --
11599 -------------------------
11601 procedure Remove_Side_Effects
11603 Name_Req
: Boolean := False;
11604 Renaming_Req
: Boolean := False;
11605 Variable_Ref
: Boolean := False;
11606 Related_Id
: Entity_Id
:= Empty
;
11607 Is_Low_Bound
: Boolean := False;
11608 Is_High_Bound
: Boolean := False;
11609 Discr_Number
: Int
:= 0;
11610 Check_Side_Effects
: Boolean := True)
11612 function Build_Temporary
11615 Related_Nod
: Node_Id
:= Empty
) return Entity_Id
;
11616 -- Create an external symbol of the form xxx_FIRST/_LAST if Related_Nod
11617 -- is present (xxx is taken from the Chars field of Related_Nod),
11618 -- otherwise it generates an internal temporary. The created temporary
11619 -- entity is marked as internal.
11621 function Possible_Side_Effect_In_SPARK
(Exp
: Node_Id
) return Boolean;
11622 -- Computes whether a side effect is possible in SPARK, which should
11623 -- be handled by removing it from the expression for GNATprove. Note
11624 -- that other side effects related to volatile variables are handled
11627 ---------------------
11628 -- Build_Temporary --
11629 ---------------------
11631 function Build_Temporary
11634 Related_Nod
: Node_Id
:= Empty
) return Entity_Id
11636 Temp_Id
: Entity_Id
;
11637 Temp_Nam
: Name_Id
;
11638 Should_Set_Related_Expression
: Boolean := False;
11641 -- The context requires an external symbol : expression is
11642 -- the bound of an array, or a discriminant value. We create
11643 -- a unique string using the related entity and an appropriate
11644 -- suffix, rather than a numeric serial number (used for internal
11645 -- entities) that may vary depending on compilation options, in
11646 -- particular on the Assertions_Enabled mode. This avoids spurious
11649 if Present
(Related_Id
) then
11650 if Is_Low_Bound
then
11651 Temp_Nam
:= New_External_Name
(Chars
(Related_Id
), "_FIRST");
11653 elsif Is_High_Bound
then
11654 Temp_Nam
:= New_External_Name
(Chars
(Related_Id
), "_LAST");
11657 pragma Assert
(Discr_Number
> 0);
11659 -- We don't have any intelligible way of printing T_DISCR in
11660 -- CodePeer. Thus, set a related expression in this case.
11662 Should_Set_Related_Expression
:= True;
11664 -- Use fully qualified name to avoid ambiguities.
11668 (Get_Qualified_Name
(Related_Id
), "_DISCR", Discr_Number
);
11671 Temp_Id
:= Make_Defining_Identifier
(Loc
, Temp_Nam
);
11673 if Should_Set_Related_Expression
then
11674 Set_Related_Expression
(Temp_Id
, Related_Nod
);
11677 -- Otherwise generate an internal temporary
11680 Temp_Id
:= Make_Temporary
(Loc
, Id
, Related_Nod
);
11683 Set_Is_Internal
(Temp_Id
);
11686 end Build_Temporary
;
11688 -----------------------------------
11689 -- Possible_Side_Effect_In_SPARK --
11690 -----------------------------------
11692 function Possible_Side_Effect_In_SPARK
(Exp
: Node_Id
) return Boolean is
11694 -- Side-effect removal in SPARK should only occur when not inside a
11695 -- generic and not doing a preanalysis, inside an object renaming or
11696 -- a type declaration or a for-loop iteration scheme.
11698 return not Inside_A_Generic
11699 and then Full_Analysis
11700 and then Nkind
(Enclosing_Declaration
(Exp
)) in
11701 N_Component_Declaration
11702 | N_Full_Type_Declaration
11703 | N_Iterator_Specification
11704 | N_Loop_Parameter_Specification
11705 | N_Object_Renaming_Declaration
11706 | N_Subtype_Declaration
;
11707 end Possible_Side_Effect_In_SPARK
;
11711 Loc
: constant Source_Ptr
:= Sloc
(Exp
);
11712 Exp_Type
: constant Entity_Id
:= Etype
(Exp
);
11713 Svg_Suppress
: constant Suppress_Record
:= Scope_Suppress
;
11714 Def_Id
: Entity_Id
;
11717 Ptr_Typ_Decl
: Node_Id
;
11718 Ref_Type
: Entity_Id
;
11721 -- Start of processing for Remove_Side_Effects
11724 -- Handle cases in which there is nothing to do. In GNATprove mode,
11725 -- removal of side effects is useful for the light expansion of
11728 if not Expander_Active
11730 (GNATprove_Mode
and then Possible_Side_Effect_In_SPARK
(Exp
))
11734 -- Cannot generate temporaries if the invocation to remove side effects
11735 -- was issued too early and the type of the expression is not resolved
11736 -- (this happens because routines Duplicate_Subexpr_XX implicitly invoke
11737 -- Remove_Side_Effects).
11739 elsif No
(Exp_Type
)
11740 or else Ekind
(Exp_Type
) = E_Access_Attribute_Type
11744 -- Nothing to do if prior expansion determined that a function call does
11745 -- not require side effect removal.
11747 elsif Nkind
(Exp
) = N_Function_Call
11748 and then No_Side_Effect_Removal
(Exp
)
11752 -- No action needed for side-effect free expressions
11754 elsif Check_Side_Effects
11755 and then Side_Effect_Free
(Exp
, Name_Req
, Variable_Ref
)
11759 -- Generating C code we cannot remove side effect of function returning
11760 -- class-wide types since there is no secondary stack (required to use
11763 elsif Modify_Tree_For_C
11764 and then Nkind
(Exp
) = N_Function_Call
11765 and then Is_Class_Wide_Type
(Etype
(Exp
))
11770 -- The remaining processing is done with all checks suppressed
11772 -- Note: from now on, don't use return statements, instead do a goto
11773 -- Leave, to ensure that we properly restore Scope_Suppress.Suppress.
11775 Scope_Suppress
.Suppress
:= (others => True);
11777 -- If this is a side-effect free attribute reference whose expressions
11778 -- are also side-effect free and whose prefix is not a name, remove the
11779 -- side effects of the prefix. A copy of the prefix is required in this
11780 -- case and it is better not to make an additional one for the attribute
11781 -- itself, because the return type of many of them is universal integer,
11782 -- which is a very large type for a temporary.
11783 -- The prefix of an attribute reference Reduce may be syntactically an
11784 -- aggregate, but will be expanded into a loop, so no need to remove
11787 if Nkind
(Exp
) = N_Attribute_Reference
11788 and then Side_Effect_Free_Attribute
(Attribute_Name
(Exp
))
11789 and then Side_Effect_Free
(Expressions
(Exp
), Name_Req
, Variable_Ref
)
11790 and then (Attribute_Name
(Exp
) /= Name_Reduce
11791 or else Nkind
(Prefix
(Exp
)) /= N_Aggregate
)
11792 and then not Is_Name_Reference
(Prefix
(Exp
))
11794 Remove_Side_Effects
(Prefix
(Exp
), Name_Req
, Variable_Ref
);
11797 -- If this is an elementary or a small not-by-reference record type, and
11798 -- we need to capture the value, just make a constant; this is cheap and
11799 -- objects of both kinds of types can be bit aligned, so it might not be
11800 -- possible to generate a reference to them. Likewise if this is not a
11801 -- name reference, except for a type conversion, because we would enter
11802 -- an infinite recursion with Checks.Apply_Predicate_Check if the target
11803 -- type has predicates (and type conversions need a specific treatment
11804 -- anyway, see below). Also do it if we have a volatile reference and
11805 -- Name_Req is not set (see comments for Side_Effect_Free).
11807 elsif (Is_Elementary_Type
(Exp_Type
)
11808 or else (Is_Record_Type
(Exp_Type
)
11809 and then Known_Static_RM_Size
(Exp_Type
)
11810 and then RM_Size
(Exp_Type
) <= System_Max_Integer_Size
11811 and then not Has_Discriminants
(Exp_Type
)
11812 and then not Is_By_Reference_Type
(Exp_Type
)))
11813 and then (Variable_Ref
11814 or else (not Is_Name_Reference
(Exp
)
11815 and then Nkind
(Exp
) /= N_Type_Conversion
)
11816 or else (not Name_Req
11817 and then Is_Volatile_Reference
(Exp
)))
11819 Def_Id
:= Build_Temporary
(Loc
, 'R', Exp
);
11820 Set_Etype
(Def_Id
, Exp_Type
);
11821 Res
:= New_Occurrence_Of
(Def_Id
, Loc
);
11823 -- If the expression is a packed reference, it must be reanalyzed and
11824 -- expanded, depending on context. This is the case for actuals where
11825 -- a constraint check may capture the actual before expansion of the
11826 -- call is complete.
11828 if Nkind
(Exp
) = N_Indexed_Component
11829 and then Is_Packed
(Etype
(Prefix
(Exp
)))
11831 Set_Analyzed
(Exp
, False);
11832 Set_Analyzed
(Prefix
(Exp
), False);
11836 -- Rnn : Exp_Type renames Expr;
11838 -- In GNATprove mode, we prefer to use renamings for intermediate
11839 -- variables to definition of constants, due to the implicit move
11840 -- operation that such a constant definition causes as part of the
11841 -- support in GNATprove for ownership pointers. Hence, we generate
11842 -- a renaming for a reference to an object of a nonscalar type.
11845 or else (GNATprove_Mode
11846 and then Is_Object_Reference
(Exp
)
11847 and then not Is_Scalar_Type
(Exp_Type
))
11850 Make_Object_Renaming_Declaration
(Loc
,
11851 Defining_Identifier
=> Def_Id
,
11852 Subtype_Mark
=> New_Occurrence_Of
(Exp_Type
, Loc
),
11853 Name
=> Relocate_Node
(Exp
));
11856 -- Rnn : constant Exp_Type := Expr;
11860 Make_Object_Declaration
(Loc
,
11861 Defining_Identifier
=> Def_Id
,
11862 Object_Definition
=> New_Occurrence_Of
(Exp_Type
, Loc
),
11863 Constant_Present
=> True,
11864 Expression
=> Relocate_Node
(Exp
));
11866 Set_Assignment_OK
(E
);
11869 Insert_Action
(Exp
, E
);
11871 -- If the expression has the form v.all then we can just capture the
11872 -- pointer, and then do an explicit dereference on the result, but
11873 -- this is not right if this is a volatile reference.
11875 elsif Nkind
(Exp
) = N_Explicit_Dereference
11876 and then not Is_Volatile_Reference
(Exp
)
11878 Def_Id
:= Build_Temporary
(Loc
, 'R', Exp
);
11880 Make_Explicit_Dereference
(Loc
, New_Occurrence_Of
(Def_Id
, Loc
));
11882 Insert_Action
(Exp
,
11883 Make_Object_Declaration
(Loc
,
11884 Defining_Identifier
=> Def_Id
,
11885 Object_Definition
=>
11886 New_Occurrence_Of
(Etype
(Prefix
(Exp
)), Loc
),
11887 Constant_Present
=> True,
11888 Expression
=> Relocate_Node
(Prefix
(Exp
))));
11890 -- Similar processing for an unchecked conversion of an expression of
11891 -- the form v.all, where we want the same kind of treatment.
11893 elsif Nkind
(Exp
) = N_Unchecked_Type_Conversion
11894 and then Nkind
(Expression
(Exp
)) = N_Explicit_Dereference
11896 Remove_Side_Effects
(Expression
(Exp
), Name_Req
, Variable_Ref
);
11899 -- If this is a type conversion, leave the type conversion and remove
11900 -- side effects in the expression, unless it is of universal integer,
11901 -- which is a very large type for a temporary. This is important in
11902 -- several circumstances: for change of representations and also when
11903 -- this is a view conversion to a smaller object, where gigi can end
11904 -- up creating its own temporary of the wrong size.
11906 elsif Nkind
(Exp
) = N_Type_Conversion
11907 and then Etype
(Expression
(Exp
)) /= Universal_Integer
11909 Remove_Side_Effects
(Expression
(Exp
), Name_Req
, Variable_Ref
);
11911 -- Generating C code the type conversion of an access to constrained
11912 -- array type into an access to unconstrained array type involves
11913 -- initializing a fat pointer and the expression must be free of
11914 -- side effects to safely compute its bounds.
11916 if Modify_Tree_For_C
11917 and then Is_Access_Type
(Etype
(Exp
))
11918 and then Is_Array_Type
(Designated_Type
(Etype
(Exp
)))
11919 and then not Is_Constrained
(Designated_Type
(Etype
(Exp
)))
11921 Def_Id
:= Build_Temporary
(Loc
, 'R', Exp
);
11922 Set_Etype
(Def_Id
, Exp_Type
);
11923 Res
:= New_Occurrence_Of
(Def_Id
, Loc
);
11925 Insert_Action
(Exp
,
11926 Make_Object_Declaration
(Loc
,
11927 Defining_Identifier
=> Def_Id
,
11928 Object_Definition
=> New_Occurrence_Of
(Exp_Type
, Loc
),
11929 Constant_Present
=> True,
11930 Expression
=> Relocate_Node
(Exp
)));
11935 -- If this is an unchecked conversion that Gigi can't handle, make
11936 -- a copy or a use a renaming to capture the value.
11938 elsif Nkind
(Exp
) = N_Unchecked_Type_Conversion
11939 and then not Safe_Unchecked_Type_Conversion
(Exp
)
11941 if CW_Or_Has_Controlled_Part
(Exp_Type
) then
11943 -- Use a renaming to capture the expression, rather than create
11944 -- a controlled temporary.
11946 Def_Id
:= Build_Temporary
(Loc
, 'R', Exp
);
11947 Res
:= New_Occurrence_Of
(Def_Id
, Loc
);
11949 Insert_Action
(Exp
,
11950 Make_Object_Renaming_Declaration
(Loc
,
11951 Defining_Identifier
=> Def_Id
,
11952 Subtype_Mark
=> New_Occurrence_Of
(Exp_Type
, Loc
),
11953 Name
=> Relocate_Node
(Exp
)));
11956 Def_Id
:= Build_Temporary
(Loc
, 'R', Exp
);
11957 Set_Etype
(Def_Id
, Exp_Type
);
11958 Res
:= New_Occurrence_Of
(Def_Id
, Loc
);
11961 Make_Object_Declaration
(Loc
,
11962 Defining_Identifier
=> Def_Id
,
11963 Object_Definition
=> New_Occurrence_Of
(Exp_Type
, Loc
),
11964 Constant_Present
=> not Is_Variable
(Exp
),
11965 Expression
=> Relocate_Node
(Exp
));
11967 Set_Assignment_OK
(E
);
11968 Insert_Action
(Exp
, E
);
11971 -- If this is a packed array component or a selected component with a
11972 -- nonstandard representation, we cannot generate a reference because
11973 -- the component may be unaligned, so we must use a renaming and this
11974 -- renaming is handled by the front end, as the back end may balk at
11975 -- the nonstandard representation (see Evaluation_Required in Exp_Ch8).
11977 elsif Nkind
(Exp
) in N_Indexed_Component | N_Selected_Component
11978 and then Has_Non_Standard_Rep
(Etype
(Prefix
(Exp
)))
11980 Def_Id
:= Build_Temporary
(Loc
, 'R', Exp
);
11981 Res
:= New_Occurrence_Of
(Def_Id
, Loc
);
11983 Insert_Action
(Exp
,
11984 Make_Object_Renaming_Declaration
(Loc
,
11985 Defining_Identifier
=> Def_Id
,
11986 Subtype_Mark
=> New_Occurrence_Of
(Exp_Type
, Loc
),
11987 Name
=> Relocate_Node
(Exp
)));
11989 -- For an expression that denotes a name, we can use a renaming scheme.
11990 -- This is needed for correctness in the case of a volatile object of
11991 -- a nonvolatile type because the Make_Reference call of the "default"
11992 -- approach would generate an illegal access value (an access value
11993 -- cannot designate such an object - see Analyze_Reference).
11995 elsif Is_Name_Reference
(Exp
)
11997 -- We skip using this scheme if we have an object of a volatile
11998 -- type and we do not have Name_Req set true (see comments for
11999 -- Side_Effect_Free).
12001 and then (Name_Req
or else not Treat_As_Volatile
(Exp_Type
))
12003 Def_Id
:= Build_Temporary
(Loc
, 'R', Exp
);
12004 Res
:= New_Occurrence_Of
(Def_Id
, Loc
);
12006 Insert_Action
(Exp
,
12007 Make_Object_Renaming_Declaration
(Loc
,
12008 Defining_Identifier
=> Def_Id
,
12009 Subtype_Mark
=> New_Occurrence_Of
(Exp_Type
, Loc
),
12010 Name
=> Relocate_Node
(Exp
)));
12012 -- Avoid generating a variable-sized temporary, by generating the
12013 -- reference just for the function call. The transformation could be
12014 -- refined to apply only when the array component is constrained by a
12017 elsif Nkind
(Exp
) = N_Selected_Component
12018 and then Nkind
(Prefix
(Exp
)) = N_Function_Call
12019 and then Is_Array_Type
(Exp_Type
)
12021 Remove_Side_Effects
(Prefix
(Exp
), Name_Req
, Variable_Ref
);
12024 -- Otherwise we generate a reference to the expression
12027 -- When generating C code we cannot consider side effect free object
12028 -- declarations that have discriminants and are initialized by means
12029 -- of a function call since on this target there is no secondary
12030 -- stack to store the return value and the expander may generate an
12031 -- extra call to the function to compute the discriminant value. In
12032 -- addition, for targets that have secondary stack, the expansion of
12033 -- functions with side effects involves the generation of an access
12034 -- type to capture the return value stored in the secondary stack;
12035 -- by contrast when generating C code such expansion generates an
12036 -- internal object declaration (no access type involved) which must
12037 -- be identified here to avoid entering into a never-ending loop
12038 -- generating internal object declarations.
12040 if Modify_Tree_For_C
12041 and then Nkind
(Parent
(Exp
)) = N_Object_Declaration
12043 (Nkind
(Exp
) /= N_Function_Call
12044 or else not Has_Discriminants
(Exp_Type
)
12045 or else Is_Internal_Name
12046 (Chars
(Defining_Identifier
(Parent
(Exp
)))))
12051 -- Special processing for function calls that return a limited type.
12052 -- We need to build a declaration that will enable build-in-place
12053 -- expansion of the call. This is not done if the context is already
12054 -- an object declaration, to prevent infinite recursion.
12056 -- This is relevant only in Ada 2005 mode. In Ada 95 programs we have
12057 -- to accommodate functions returning limited objects by reference.
12059 if Ada_Version
>= Ada_2005
12060 and then Nkind
(Exp
) = N_Function_Call
12061 and then Is_Limited_View
(Etype
(Exp
))
12062 and then Nkind
(Parent
(Exp
)) /= N_Object_Declaration
12065 Obj
: constant Entity_Id
:= Make_Temporary
(Loc
, 'F', Exp
);
12070 Make_Object_Declaration
(Loc
,
12071 Defining_Identifier
=> Obj
,
12072 Object_Definition
=> New_Occurrence_Of
(Exp_Type
, Loc
),
12073 Expression
=> Relocate_Node
(Exp
));
12075 Insert_Action
(Exp
, Decl
);
12076 Set_Etype
(Obj
, Exp_Type
);
12077 Rewrite
(Exp
, New_Occurrence_Of
(Obj
, Loc
));
12082 Def_Id
:= Build_Temporary
(Loc
, 'R', Exp
);
12084 -- The regular expansion of functions with side effects involves the
12085 -- generation of an access type to capture the return value found on
12086 -- the secondary stack. Since SPARK (and why) cannot process access
12087 -- types, use a different approach which ignores the secondary stack
12088 -- and "copies" the returned object.
12089 -- When generating C code, no need for a 'reference since the
12090 -- secondary stack is not supported.
12092 if GNATprove_Mode
or Modify_Tree_For_C
then
12093 Res
:= New_Occurrence_Of
(Def_Id
, Loc
);
12094 Ref_Type
:= Exp_Type
;
12096 -- Regular expansion utilizing an access type and 'reference
12100 Make_Explicit_Dereference
(Loc
,
12101 Prefix
=> New_Occurrence_Of
(Def_Id
, Loc
));
12104 -- type Ann is access all <Exp_Type>;
12106 Ref_Type
:= Make_Temporary
(Loc
, 'A');
12109 Make_Full_Type_Declaration
(Loc
,
12110 Defining_Identifier
=> Ref_Type
,
12112 Make_Access_To_Object_Definition
(Loc
,
12113 All_Present
=> True,
12114 Subtype_Indication
=>
12115 New_Occurrence_Of
(Exp_Type
, Loc
)));
12117 Insert_Action
(Exp
, Ptr_Typ_Decl
);
12121 if Nkind
(E
) = N_Explicit_Dereference
then
12122 New_Exp
:= Relocate_Node
(Prefix
(E
));
12125 E
:= Relocate_Node
(E
);
12127 -- Do not generate a 'reference in SPARK mode or C generation
12128 -- since the access type is not created in the first place.
12130 if GNATprove_Mode
or Modify_Tree_For_C
then
12133 -- Otherwise generate reference, marking the value as non-null
12134 -- since we know it cannot be null and we don't want a check.
12137 New_Exp
:= Make_Reference
(Loc
, E
);
12138 Set_Is_Known_Non_Null
(Def_Id
);
12142 if Is_Delayed_Aggregate
(E
) then
12144 -- The expansion of nested aggregates is delayed until the
12145 -- enclosing aggregate is expanded. As aggregates are often
12146 -- qualified, the predicate applies to qualified expressions as
12147 -- well, indicating that the enclosing aggregate has not been
12148 -- expanded yet. At this point the aggregate is part of a
12149 -- stand-alone declaration, and must be fully expanded.
12151 if Nkind
(E
) = N_Qualified_Expression
then
12152 Set_Expansion_Delayed
(Expression
(E
), False);
12153 Set_Analyzed
(Expression
(E
), False);
12155 Set_Expansion_Delayed
(E
, False);
12158 Set_Analyzed
(E
, False);
12161 -- Generating C code of object declarations that have discriminants
12162 -- and are initialized by means of a function call we propagate the
12163 -- discriminants of the parent type to the internally built object.
12164 -- This is needed to avoid generating an extra call to the called
12167 -- For example, if we generate here the following declaration, it
12168 -- will be expanded later adding an extra call to evaluate the value
12169 -- of the discriminant (needed to compute the size of the object).
12171 -- type Rec (D : Integer) is ...
12172 -- Obj : constant Rec := SomeFunc;
12174 if Modify_Tree_For_C
12175 and then Nkind
(Parent
(Exp
)) = N_Object_Declaration
12176 and then Has_Discriminants
(Exp_Type
)
12177 and then Nkind
(Exp
) = N_Function_Call
12179 Insert_Action
(Exp
,
12180 Make_Object_Declaration
(Loc
,
12181 Defining_Identifier
=> Def_Id
,
12182 Object_Definition
=> New_Copy_Tree
12183 (Object_Definition
(Parent
(Exp
))),
12184 Constant_Present
=> True,
12185 Expression
=> New_Exp
));
12187 Insert_Action
(Exp
,
12188 Make_Object_Declaration
(Loc
,
12189 Defining_Identifier
=> Def_Id
,
12190 Object_Definition
=> New_Occurrence_Of
(Ref_Type
, Loc
),
12191 Constant_Present
=> True,
12192 Expression
=> New_Exp
));
12196 -- Preserve the Assignment_OK flag in all copies, since at least one
12197 -- copy may be used in a context where this flag must be set (otherwise
12198 -- why would the flag be set in the first place).
12200 Set_Assignment_OK
(Res
, Assignment_OK
(Exp
));
12202 -- Preserve the Do_Range_Check flag in all copies
12204 Set_Do_Range_Check
(Res
, Do_Range_Check
(Exp
));
12206 -- Finally rewrite the original expression and we are done
12208 Rewrite
(Exp
, Res
);
12209 Analyze_And_Resolve
(Exp
, Exp_Type
);
12212 Scope_Suppress
:= Svg_Suppress
;
12213 end Remove_Side_Effects
;
12215 ------------------------
12216 -- Replace_References --
12217 ------------------------
12219 procedure Replace_References
12221 Par_Typ
: Entity_Id
;
12222 Deriv_Typ
: Entity_Id
;
12223 Par_Obj
: Entity_Id
:= Empty
;
12224 Deriv_Obj
: Entity_Id
:= Empty
)
12226 function Is_Deriv_Obj_Ref
(Ref
: Node_Id
) return Boolean;
12227 -- Determine whether node Ref denotes some component of Deriv_Obj
12229 function Replace_Ref
(Ref
: Node_Id
) return Traverse_Result
;
12230 -- Substitute a reference to an entity with the corresponding value
12231 -- stored in table Type_Map.
12233 function Type_Of_Formal
12235 Actual
: Node_Id
) return Entity_Id
;
12236 -- Find the type of the formal parameter which corresponds to actual
12237 -- parameter Actual in subprogram call Call.
12239 ----------------------
12240 -- Is_Deriv_Obj_Ref --
12241 ----------------------
12243 function Is_Deriv_Obj_Ref
(Ref
: Node_Id
) return Boolean is
12244 Par
: constant Node_Id
:= Parent
(Ref
);
12247 -- Detect the folowing selected component form:
12249 -- Deriv_Obj.(something)
12252 Nkind
(Par
) = N_Selected_Component
12253 and then Is_Entity_Name
(Prefix
(Par
))
12254 and then Entity
(Prefix
(Par
)) = Deriv_Obj
;
12255 end Is_Deriv_Obj_Ref
;
12261 function Replace_Ref
(Ref
: Node_Id
) return Traverse_Result
is
12262 procedure Remove_Controlling_Arguments
(From_Arg
: Node_Id
);
12263 -- Reset the Controlling_Argument of all function calls that
12264 -- encapsulate node From_Arg.
12266 ----------------------------------
12267 -- Remove_Controlling_Arguments --
12268 ----------------------------------
12270 procedure Remove_Controlling_Arguments
(From_Arg
: Node_Id
) is
12275 while Present
(Par
) loop
12276 if Nkind
(Par
) = N_Function_Call
12277 and then Present
(Controlling_Argument
(Par
))
12279 Set_Controlling_Argument
(Par
, Empty
);
12281 -- Prevent the search from going too far
12283 elsif Is_Body_Or_Package_Declaration
(Par
) then
12287 Par
:= Parent
(Par
);
12289 end Remove_Controlling_Arguments
;
12293 Context
: constant Node_Id
:=
12294 (if No
(Ref
) then Empty
else Parent
(Ref
));
12296 Loc
: constant Source_Ptr
:= Sloc
(Ref
);
12297 Ref_Id
: Entity_Id
;
12298 Result
: Traverse_Result
;
12301 -- The new reference which is intended to substitute the old one
12304 -- The reference designated for replacement. In certain cases this
12305 -- may be a node other than Ref.
12307 Val
: Node_Or_Entity_Id
;
12308 -- The corresponding value of Ref from the type map
12310 -- Start of processing for Replace_Ref
12313 -- Assume that the input reference is to be replaced and that the
12314 -- traversal should examine the children of the reference.
12319 -- The input denotes a meaningful reference
12321 if Nkind
(Ref
) in N_Has_Entity
and then Present
(Entity
(Ref
)) then
12322 Ref_Id
:= Entity
(Ref
);
12323 Val
:= Type_Map
.Get
(Ref_Id
);
12325 -- The reference has a corresponding value in the type map, a
12326 -- substitution is possible.
12328 if Present
(Val
) then
12330 -- The reference denotes a discriminant
12332 if Ekind
(Ref_Id
) = E_Discriminant
then
12333 if Nkind
(Val
) in N_Entity
then
12335 -- The value denotes another discriminant. Replace as
12338 -- _object.Discr -> _object.Val
12340 if Ekind
(Val
) = E_Discriminant
then
12341 New_Ref
:= New_Occurrence_Of
(Val
, Loc
);
12343 -- Otherwise the value denotes the entity of a name which
12344 -- constraints the discriminant. Replace as follows:
12346 -- _object.Discr -> Val
12349 pragma Assert
(Is_Deriv_Obj_Ref
(Old_Ref
));
12351 New_Ref
:= New_Occurrence_Of
(Val
, Loc
);
12352 Old_Ref
:= Parent
(Old_Ref
);
12355 -- Otherwise the value denotes an arbitrary expression which
12356 -- constraints the discriminant. Replace as follows:
12358 -- _object.Discr -> Val
12361 pragma Assert
(Is_Deriv_Obj_Ref
(Old_Ref
));
12363 New_Ref
:= New_Copy_Tree
(Val
);
12364 Old_Ref
:= Parent
(Old_Ref
);
12367 -- Otherwise the reference denotes a primitive. Replace as
12370 -- Primitive -> Val
12373 pragma Assert
(Nkind
(Val
) in N_Entity
);
12374 New_Ref
:= New_Occurrence_Of
(Val
, Loc
);
12377 -- The reference mentions the _object parameter of the parent
12378 -- type's DIC or type invariant procedure. Replace as follows:
12380 -- _object -> _object
12382 elsif Present
(Par_Obj
)
12383 and then Present
(Deriv_Obj
)
12384 and then Ref_Id
= Par_Obj
12386 New_Ref
:= New_Occurrence_Of
(Deriv_Obj
, Loc
);
12388 -- The type of the _object parameter is class-wide when the
12389 -- expression comes from an assertion pragma that applies to
12390 -- an abstract parent type or an interface. The class-wide type
12391 -- facilitates the preanalysis of the expression by treating
12392 -- calls to abstract primitives that mention the current
12393 -- instance of the type as dispatching. Once the calls are
12394 -- remapped to invoke overriding or inherited primitives, the
12395 -- calls no longer need to be dispatching. Examine all function
12396 -- calls that encapsulate the _object parameter and reset their
12397 -- Controlling_Argument attribute.
12399 if Is_Class_Wide_Type
(Etype
(Par_Obj
))
12400 and then Is_Abstract_Type
(Root_Type
(Etype
(Par_Obj
)))
12402 Remove_Controlling_Arguments
(Old_Ref
);
12405 -- The reference to _object acts as an actual parameter in a
12406 -- subprogram call which may be invoking a primitive of the
12409 -- Primitive (... _object ...);
12411 -- The parent type primitive may not be overridden nor
12412 -- inherited when it is declared after the derived type
12415 -- type Parent is tagged private;
12416 -- type Child is new Parent with private;
12417 -- procedure Primitive (Obj : Parent);
12419 -- In this scenario the _object parameter is converted to the
12420 -- parent type. Due to complications with partial/full views
12421 -- and view swaps, the parent type is taken from the formal
12422 -- parameter of the subprogram being called.
12424 if Nkind
(Context
) in N_Subprogram_Call
12425 and then No
(Type_Map
.Get
(Entity
(Name
(Context
))))
12428 -- We need to use the Original_Node of the callee, in
12429 -- case it was already modified. Note that we are using
12430 -- Traverse_Proc to walk the tree, and it is defined to
12431 -- walk subtrees in an arbitrary order.
12433 Callee
: constant Entity_Id
:=
12434 Entity
(Original_Node
(Name
(Context
)));
12436 if No
(Type_Map
.Get
(Callee
)) then
12439 (Type_Of_Formal
(Context
, Old_Ref
), New_Ref
);
12441 -- Do not process the generated type conversion
12442 -- because both the parent type and the derived type
12443 -- are in the Type_Map table. This will clobber the
12444 -- type conversion by resetting its subtype mark.
12451 -- Otherwise there is nothing to replace
12457 if Present
(New_Ref
) then
12458 Rewrite
(Old_Ref
, New_Ref
);
12460 -- Update the return type when the context of the reference
12461 -- acts as the name of a function call. Note that the update
12462 -- should not be performed when the reference appears as an
12463 -- actual in the call.
12465 if Nkind
(Context
) = N_Function_Call
12466 and then Name
(Context
) = Old_Ref
12468 Set_Etype
(Context
, Etype
(Val
));
12473 -- Reanalyze the reference due to potential replacements
12475 if Nkind
(Old_Ref
) in N_Has_Etype
then
12476 Set_Analyzed
(Old_Ref
, False);
12482 procedure Replace_Refs
is new Traverse_Proc
(Replace_Ref
);
12484 --------------------
12485 -- Type_Of_Formal --
12486 --------------------
12488 function Type_Of_Formal
12490 Actual
: Node_Id
) return Entity_Id
12496 -- Examine the list of actual and formal parameters in parallel
12498 A
:= First
(Parameter_Associations
(Call
));
12499 F
:= First_Formal
(Entity
(Name
(Call
)));
12500 while Present
(A
) and then Present
(F
) loop
12509 -- The actual parameter must always have a corresponding formal
12511 pragma Assert
(False);
12514 end Type_Of_Formal
;
12516 -- Start of processing for Replace_References
12519 -- Map the attributes of the parent type to the proper corresponding
12520 -- attributes of the derived type.
12523 (Parent_Type
=> Par_Typ
,
12524 Derived_Type
=> Deriv_Typ
);
12526 -- Inspect the input expression and perform substitutions where
12529 Replace_Refs
(Expr
);
12530 end Replace_References
;
12532 -----------------------------
12533 -- Replace_Type_References --
12534 -----------------------------
12536 procedure Replace_Type_References
12539 Obj_Id
: Entity_Id
)
12541 procedure Replace_Type_Ref
(N
: Node_Id
);
12542 -- Substitute a single reference of the current instance of type Typ
12543 -- with a reference to Obj_Id.
12545 ----------------------
12546 -- Replace_Type_Ref --
12547 ----------------------
12549 procedure Replace_Type_Ref
(N
: Node_Id
) is
12551 -- Decorate the reference to Typ even though it may be rewritten
12552 -- further down. This is done so that routines which examine
12553 -- properties of the Original_Node have some semantic information.
12555 if Nkind
(N
) = N_Identifier
then
12556 Set_Entity
(N
, Typ
);
12557 Set_Etype
(N
, Typ
);
12559 elsif Nkind
(N
) = N_Selected_Component
then
12560 Analyze
(Prefix
(N
));
12561 Set_Entity
(Selector_Name
(N
), Typ
);
12562 Set_Etype
(Selector_Name
(N
), Typ
);
12565 -- Perform the following substitution:
12569 Rewrite
(N
, New_Occurrence_Of
(Obj_Id
, Sloc
(N
)));
12570 Set_Comes_From_Source
(N
, True);
12571 end Replace_Type_Ref
;
12573 procedure Replace_Type_Refs
is
12574 new Replace_Type_References_Generic
(Replace_Type_Ref
);
12576 -- Start of processing for Replace_Type_References
12579 Replace_Type_Refs
(Expr
, Typ
);
12580 end Replace_Type_References
;
12582 ---------------------------
12583 -- Represented_As_Scalar --
12584 ---------------------------
12586 function Represented_As_Scalar
(T
: Entity_Id
) return Boolean is
12587 UT
: constant Entity_Id
:= Underlying_Type
(T
);
12589 return Is_Scalar_Type
(UT
)
12590 or else (Is_Bit_Packed_Array
(UT
)
12591 and then Is_Scalar_Type
(Packed_Array_Impl_Type
(UT
)));
12592 end Represented_As_Scalar
;
12594 ------------------------------
12595 -- Requires_Cleanup_Actions --
12596 ------------------------------
12598 function Requires_Cleanup_Actions
12600 Lib_Level
: Boolean) return Boolean
12602 At_Lib_Level
: constant Boolean :=
12604 and then Nkind
(N
) in N_Package_Body | N_Package_Specification
;
12605 -- N is at the library level if the top-most context is a package and
12606 -- the path taken to reach N does not include nonpackage constructs.
12610 when N_Accept_Statement
12611 | N_Block_Statement
12615 | N_Subprogram_Body
12619 Requires_Cleanup_Actions
12620 (L
=> Declarations
(N
),
12621 Lib_Level
=> At_Lib_Level
,
12622 Nested_Constructs
=> True)
12624 (Present
(Handled_Statement_Sequence
(N
))
12626 Requires_Cleanup_Actions
12628 Statements
(Handled_Statement_Sequence
(N
)),
12629 Lib_Level
=> At_Lib_Level
,
12630 Nested_Constructs
=> True));
12632 -- Extended return statements are the same as the above, except that
12633 -- there is no Declarations field. We do not want to clean up the
12634 -- Return_Object_Declarations.
12636 when N_Extended_Return_Statement
=>
12638 Present
(Handled_Statement_Sequence
(N
))
12639 and then Requires_Cleanup_Actions
12641 Statements
(Handled_Statement_Sequence
(N
)),
12642 Lib_Level
=> At_Lib_Level
,
12643 Nested_Constructs
=> True);
12645 when N_Package_Specification
=>
12647 Requires_Cleanup_Actions
12648 (L
=> Visible_Declarations
(N
),
12649 Lib_Level
=> At_Lib_Level
,
12650 Nested_Constructs
=> True)
12652 Requires_Cleanup_Actions
12653 (L
=> Private_Declarations
(N
),
12654 Lib_Level
=> At_Lib_Level
,
12655 Nested_Constructs
=> True);
12658 raise Program_Error
;
12660 end Requires_Cleanup_Actions
;
12662 ------------------------------
12663 -- Requires_Cleanup_Actions --
12664 ------------------------------
12666 function Requires_Cleanup_Actions
12668 Lib_Level
: Boolean;
12669 Nested_Constructs
: Boolean) return Boolean
12673 Obj_Id
: Entity_Id
;
12674 Obj_Typ
: Entity_Id
;
12675 Pack_Id
: Entity_Id
;
12679 if No
(L
) or else Is_Empty_List
(L
) then
12684 while Present
(Decl
) loop
12686 -- Library-level tagged types
12688 if Nkind
(Decl
) = N_Full_Type_Declaration
then
12689 Typ
:= Defining_Identifier
(Decl
);
12691 -- Ignored Ghost types do not need any cleanup actions because
12692 -- they will not appear in the final tree.
12694 if Is_Ignored_Ghost_Entity
(Typ
) then
12697 elsif Is_Tagged_Type
(Typ
)
12698 and then Is_Library_Level_Entity
(Typ
)
12699 and then Convention
(Typ
) = Convention_Ada
12700 and then Present
(Access_Disp_Table
(Typ
))
12701 and then RTE_Available
(RE_Unregister_Tag
)
12702 and then not Is_Abstract_Type
(Typ
)
12703 and then not No_Run_Time_Mode
12708 -- Regular object declarations
12710 elsif Nkind
(Decl
) = N_Object_Declaration
then
12711 Obj_Id
:= Defining_Identifier
(Decl
);
12712 Obj_Typ
:= Base_Type
(Etype
(Obj_Id
));
12713 Expr
:= Expression
(Decl
);
12715 -- Bypass any form of processing for objects which have their
12716 -- finalization disabled. This applies only to objects at the
12719 if Lib_Level
and then Finalize_Storage_Only
(Obj_Typ
) then
12722 -- Finalization of transient objects are treated separately in
12723 -- order to handle sensitive cases. These include:
12725 -- * Aggregate expansion
12726 -- * If, case, and expression with actions expansion
12727 -- * Transient scopes
12729 -- If one of those contexts has marked the transient object as
12730 -- ignored, do not generate finalization actions for it.
12732 elsif Is_Finalized_Transient
(Obj_Id
)
12733 or else Is_Ignored_Transient
(Obj_Id
)
12737 -- Ignored Ghost objects do not need any cleanup actions because
12738 -- they will not appear in the final tree.
12740 elsif Is_Ignored_Ghost_Entity
(Obj_Id
) then
12743 -- The object is of the form:
12744 -- Obj : [constant] Typ [:= Expr];
12746 -- Do not process tag-to-class-wide conversions because they do
12747 -- not yield an object. Do not process the incomplete view of a
12748 -- deferred constant. Note that an object initialized by means
12749 -- of a build-in-place function call may appear as a deferred
12750 -- constant after expansion activities. These kinds of objects
12751 -- must be finalized.
12753 elsif not Is_Imported
(Obj_Id
)
12754 and then Needs_Finalization
(Obj_Typ
)
12755 and then not Is_Tag_To_Class_Wide_Conversion
(Obj_Id
)
12756 and then not (Ekind
(Obj_Id
) = E_Constant
12757 and then not Has_Completion
(Obj_Id
)
12758 and then No
(BIP_Initialization_Call
(Obj_Id
)))
12762 -- The object is of the form:
12763 -- Obj : Access_Typ := Non_BIP_Function_Call'reference;
12765 -- Obj : Access_Typ :=
12766 -- BIP_Function_Call (BIPalloc => 2, ...)'reference;
12768 elsif Is_Access_Type
(Obj_Typ
)
12769 and then Needs_Finalization
12770 (Available_View
(Designated_Type
(Obj_Typ
)))
12771 and then Present
(Expr
)
12773 (Is_Secondary_Stack_BIP_Func_Call
(Expr
)
12775 (Is_Non_BIP_Func_Call
(Expr
)
12776 and then not Is_Related_To_Func_Return
(Obj_Id
)))
12780 -- Processing for "hook" objects generated for transient objects
12781 -- declared inside an Expression_With_Actions.
12783 elsif Is_Access_Type
(Obj_Typ
)
12784 and then Present
(Status_Flag_Or_Transient_Decl
(Obj_Id
))
12785 and then Nkind
(Status_Flag_Or_Transient_Decl
(Obj_Id
)) =
12786 N_Object_Declaration
12790 -- Processing for intermediate results of if expressions where
12791 -- one of the alternatives uses a controlled function call.
12793 elsif Is_Access_Type
(Obj_Typ
)
12794 and then Present
(Status_Flag_Or_Transient_Decl
(Obj_Id
))
12795 and then Nkind
(Status_Flag_Or_Transient_Decl
(Obj_Id
)) =
12796 N_Defining_Identifier
12797 and then Present
(Expr
)
12798 and then Nkind
(Expr
) = N_Null
12802 -- Simple protected objects which use type System.Tasking.
12803 -- Protected_Objects.Protection to manage their locks should be
12804 -- treated as controlled since they require manual cleanup.
12806 elsif Ekind
(Obj_Id
) = E_Variable
12807 and then (Is_Simple_Protected_Type
(Obj_Typ
)
12808 or else Has_Simple_Protected_Object
(Obj_Typ
))
12813 -- Specific cases of object renamings
12815 elsif Nkind
(Decl
) = N_Object_Renaming_Declaration
then
12816 Obj_Id
:= Defining_Identifier
(Decl
);
12817 Obj_Typ
:= Base_Type
(Etype
(Obj_Id
));
12819 -- Bypass any form of processing for objects which have their
12820 -- finalization disabled. This applies only to objects at the
12823 if Lib_Level
and then Finalize_Storage_Only
(Obj_Typ
) then
12826 -- Ignored Ghost object renamings do not need any cleanup actions
12827 -- because they will not appear in the final tree.
12829 elsif Is_Ignored_Ghost_Entity
(Obj_Id
) then
12832 -- Return object of a build-in-place function. This case is
12833 -- recognized and marked by the expansion of an extended return
12834 -- statement (see Expand_N_Extended_Return_Statement).
12836 elsif Needs_Finalization
(Obj_Typ
)
12837 and then Is_Return_Object
(Obj_Id
)
12838 and then Present
(Status_Flag_Or_Transient_Decl
(Obj_Id
))
12842 -- Detect a case where a source object has been initialized by
12843 -- a controlled function call or another object which was later
12844 -- rewritten as a class-wide conversion of Ada.Tags.Displace.
12846 -- Obj1 : CW_Type := Src_Obj;
12847 -- Obj2 : CW_Type := Function_Call (...);
12849 -- Obj1 : CW_Type renames (... Ada.Tags.Displace (Src_Obj));
12850 -- Tmp : ... := Function_Call (...)'reference;
12851 -- Obj2 : CW_Type renames (... Ada.Tags.Displace (Tmp));
12853 elsif Is_Displacement_Of_Object_Or_Function_Result
(Obj_Id
) then
12857 -- Inspect the freeze node of an access-to-controlled type and look
12858 -- for a delayed finalization master. This case arises when the
12859 -- freeze actions are inserted at a later time than the expansion of
12860 -- the context. Since Build_Finalizer is never called on a single
12861 -- construct twice, the master will be ultimately left out and never
12862 -- finalized. This is also needed for freeze actions of designated
12863 -- types themselves, since in some cases the finalization master is
12864 -- associated with a designated type's freeze node rather than that
12865 -- of the access type (see handling for freeze actions in
12866 -- Build_Finalization_Master).
12868 elsif Nkind
(Decl
) = N_Freeze_Entity
12869 and then Present
(Actions
(Decl
))
12871 Typ
:= Entity
(Decl
);
12873 -- Freeze nodes for ignored Ghost types do not need cleanup
12874 -- actions because they will never appear in the final tree.
12876 if Is_Ignored_Ghost_Entity
(Typ
) then
12879 elsif ((Is_Access_Object_Type
(Typ
)
12880 and then Needs_Finalization
12881 (Available_View
(Designated_Type
(Typ
))))
12882 or else (Is_Type
(Typ
) and then Needs_Finalization
(Typ
)))
12883 and then Requires_Cleanup_Actions
12884 (Actions
(Decl
), Lib_Level
, Nested_Constructs
)
12889 -- Nested package declarations
12891 elsif Nested_Constructs
12892 and then Nkind
(Decl
) = N_Package_Declaration
12894 Pack_Id
:= Defining_Entity
(Decl
);
12896 -- Do not inspect an ignored Ghost package because all code found
12897 -- within will not appear in the final tree.
12899 if Is_Ignored_Ghost_Entity
(Pack_Id
) then
12902 elsif Ekind
(Pack_Id
) /= E_Generic_Package
12903 and then Requires_Cleanup_Actions
12904 (Specification
(Decl
), Lib_Level
)
12909 -- Nested package bodies
12911 elsif Nested_Constructs
and then Nkind
(Decl
) = N_Package_Body
then
12913 -- Do not inspect an ignored Ghost package body because all code
12914 -- found within will not appear in the final tree.
12916 if Is_Ignored_Ghost_Entity
(Defining_Entity
(Decl
)) then
12919 elsif Ekind
(Corresponding_Spec
(Decl
)) /= E_Generic_Package
12920 and then Requires_Cleanup_Actions
(Decl
, Lib_Level
)
12925 elsif Nkind
(Decl
) = N_Block_Statement
12928 -- Handle a rare case caused by a controlled transient object
12929 -- created as part of a record init proc. The variable is wrapped
12930 -- in a block, but the block is not associated with a transient
12935 -- Handle the case where the original context has been wrapped in
12936 -- a block to avoid interference between exception handlers and
12937 -- At_End handlers. Treat the block as transparent and process its
12940 or else Is_Finalization_Wrapper
(Decl
))
12942 if Requires_Cleanup_Actions
(Decl
, Lib_Level
) then
12951 end Requires_Cleanup_Actions
;
12953 ------------------------------------
12954 -- Safe_Unchecked_Type_Conversion --
12955 ------------------------------------
12957 -- Note: this function knows quite a bit about the exact requirements of
12958 -- Gigi with respect to unchecked type conversions, and its code must be
12959 -- coordinated with any changes in Gigi in this area.
12961 -- The above requirements should be documented in Sinfo ???
12963 function Safe_Unchecked_Type_Conversion
(Exp
: Node_Id
) return Boolean is
12968 Pexp
: constant Node_Id
:= Parent
(Exp
);
12971 -- If the expression is the RHS of an assignment or object declaration
12972 -- we are always OK because there will always be a target.
12974 -- Object renaming declarations, (generated for view conversions of
12975 -- actuals in inlined calls), like object declarations, provide an
12976 -- explicit type, and are safe as well.
12978 if (Nkind
(Pexp
) = N_Assignment_Statement
12979 and then Expression
(Pexp
) = Exp
)
12980 or else Nkind
(Pexp
)
12981 in N_Object_Declaration | N_Object_Renaming_Declaration
12985 -- If the expression is the prefix of an N_Selected_Component we should
12986 -- also be OK because GCC knows to look inside the conversion except if
12987 -- the type is discriminated. We assume that we are OK anyway if the
12988 -- type is not set yet or if it is controlled since we can't afford to
12989 -- introduce a temporary in this case.
12991 elsif Nkind
(Pexp
) = N_Selected_Component
12992 and then Prefix
(Pexp
) = Exp
12994 return No
(Etype
(Pexp
))
12995 or else not Is_Type
(Etype
(Pexp
))
12996 or else not Has_Discriminants
(Etype
(Pexp
))
12997 or else Is_Constrained
(Etype
(Pexp
));
13000 -- Set the output type, this comes from Etype if it is set, otherwise we
13001 -- take it from the subtype mark, which we assume was already fully
13004 if Present
(Etype
(Exp
)) then
13005 Otyp
:= Etype
(Exp
);
13007 Otyp
:= Entity
(Subtype_Mark
(Exp
));
13010 -- The input type always comes from the expression, and we assume this
13011 -- is indeed always analyzed, so we can simply get the Etype.
13013 Ityp
:= Etype
(Expression
(Exp
));
13015 -- Initialize alignments to unknown so far
13020 -- Replace a concurrent type by its corresponding record type and each
13021 -- type by its underlying type and do the tests on those. The original
13022 -- type may be a private type whose completion is a concurrent type, so
13023 -- find the underlying type first.
13025 if Present
(Underlying_Type
(Otyp
)) then
13026 Otyp
:= Underlying_Type
(Otyp
);
13029 if Present
(Underlying_Type
(Ityp
)) then
13030 Ityp
:= Underlying_Type
(Ityp
);
13033 if Is_Concurrent_Type
(Otyp
) then
13034 Otyp
:= Corresponding_Record_Type
(Otyp
);
13037 if Is_Concurrent_Type
(Ityp
) then
13038 Ityp
:= Corresponding_Record_Type
(Ityp
);
13041 -- If the base types are the same, we know there is no problem since
13042 -- this conversion will be a noop.
13044 if Implementation_Base_Type
(Otyp
) = Implementation_Base_Type
(Ityp
) then
13047 -- Same if this is an upwards conversion of an untagged type, and there
13048 -- are no constraints involved (could be more general???)
13050 elsif Etype
(Ityp
) = Otyp
13051 and then not Is_Tagged_Type
(Ityp
)
13052 and then not Has_Discriminants
(Ityp
)
13053 and then No
(First_Rep_Item
(Base_Type
(Ityp
)))
13057 -- If the expression has an access type (object or subprogram) we assume
13058 -- that the conversion is safe, because the size of the target is safe,
13059 -- even if it is a record (which might be treated as having unknown size
13062 elsif Is_Access_Type
(Ityp
) then
13065 -- If the size of output type is known at compile time, there is never
13066 -- a problem. Note that unconstrained records are considered to be of
13067 -- known size, but we can't consider them that way here, because we are
13068 -- talking about the actual size of the object.
13070 -- We also make sure that in addition to the size being known, we do not
13071 -- have a case which might generate an embarrassingly large temp in
13072 -- stack checking mode.
13074 elsif Size_Known_At_Compile_Time
(Otyp
)
13076 (not Stack_Checking_Enabled
13077 or else not May_Generate_Large_Temp
(Otyp
))
13078 and then not (Is_Record_Type
(Otyp
) and then not Is_Constrained
(Otyp
))
13082 -- If either type is tagged, then we know the alignment is OK so Gigi
13083 -- will be able to use pointer punning.
13085 elsif Is_Tagged_Type
(Otyp
) or else Is_Tagged_Type
(Ityp
) then
13088 -- If either type is a limited record type, we cannot do a copy, so say
13089 -- safe since there's nothing else we can do.
13091 elsif Is_Limited_Record
(Otyp
) or else Is_Limited_Record
(Ityp
) then
13094 -- Conversions to and from packed array types are always ignored and
13097 elsif Is_Packed_Array_Impl_Type
(Otyp
)
13098 or else Is_Packed_Array_Impl_Type
(Ityp
)
13103 -- The only other cases known to be safe is if the input type's
13104 -- alignment is known to be at least the maximum alignment for the
13105 -- target or if both alignments are known and the output type's
13106 -- alignment is no stricter than the input's. We can use the component
13107 -- type alignment for an array if a type is an unpacked array type.
13109 if Present
(Alignment_Clause
(Otyp
)) then
13110 Oalign
:= Expr_Value
(Expression
(Alignment_Clause
(Otyp
)));
13112 elsif Is_Array_Type
(Otyp
)
13113 and then Present
(Alignment_Clause
(Component_Type
(Otyp
)))
13115 Oalign
:= Expr_Value
(Expression
(Alignment_Clause
13116 (Component_Type
(Otyp
))));
13119 if Present
(Alignment_Clause
(Ityp
)) then
13120 Ialign
:= Expr_Value
(Expression
(Alignment_Clause
(Ityp
)));
13122 elsif Is_Array_Type
(Ityp
)
13123 and then Present
(Alignment_Clause
(Component_Type
(Ityp
)))
13125 Ialign
:= Expr_Value
(Expression
(Alignment_Clause
13126 (Component_Type
(Ityp
))));
13129 if Present
(Ialign
) and then Ialign
> Maximum_Alignment
then
13132 elsif Present
(Ialign
)
13133 and then Present
(Oalign
)
13134 and then Ialign
<= Oalign
13138 -- Otherwise, Gigi cannot handle this and we must make a temporary
13143 end Safe_Unchecked_Type_Conversion
;
13145 ---------------------------------
13146 -- Set_Current_Value_Condition --
13147 ---------------------------------
13149 -- Note: the implementation of this procedure is very closely tied to the
13150 -- implementation of Get_Current_Value_Condition. Here we set required
13151 -- Current_Value fields, and in Get_Current_Value_Condition, we interpret
13152 -- them, so they must have a consistent view.
13154 procedure Set_Current_Value_Condition
(Cnode
: Node_Id
) is
13156 procedure Set_Entity_Current_Value
(N
: Node_Id
);
13157 -- If N is an entity reference, where the entity is of an appropriate
13158 -- kind, then set the current value of this entity to Cnode, unless
13159 -- there is already a definite value set there.
13161 procedure Set_Expression_Current_Value
(N
: Node_Id
);
13162 -- If N is of an appropriate form, sets an appropriate entry in current
13163 -- value fields of relevant entities. Multiple entities can be affected
13164 -- in the case of an AND or AND THEN.
13166 ------------------------------
13167 -- Set_Entity_Current_Value --
13168 ------------------------------
13170 procedure Set_Entity_Current_Value
(N
: Node_Id
) is
13172 if Is_Entity_Name
(N
) then
13174 Ent
: constant Entity_Id
:= Entity
(N
);
13177 -- Don't capture if not safe to do so
13179 if not Safe_To_Capture_Value
(N
, Ent
, Cond
=> True) then
13183 -- Here we have a case where the Current_Value field may need
13184 -- to be set. We set it if it is not already set to a compile
13185 -- time expression value.
13187 -- Note that this represents a decision that one condition
13188 -- blots out another previous one. That's certainly right if
13189 -- they occur at the same level. If the second one is nested,
13190 -- then the decision is neither right nor wrong (it would be
13191 -- equally OK to leave the outer one in place, or take the new
13192 -- inner one). Really we should record both, but our data
13193 -- structures are not that elaborate.
13195 if Nkind
(Current_Value
(Ent
)) not in N_Subexpr
then
13196 Set_Current_Value
(Ent
, Cnode
);
13200 end Set_Entity_Current_Value
;
13202 ----------------------------------
13203 -- Set_Expression_Current_Value --
13204 ----------------------------------
13206 procedure Set_Expression_Current_Value
(N
: Node_Id
) is
13212 -- Loop to deal with (ignore for now) any NOT operators present. The
13213 -- presence of NOT operators will be handled properly when we call
13214 -- Get_Current_Value_Condition.
13216 while Nkind
(Cond
) = N_Op_Not
loop
13217 Cond
:= Right_Opnd
(Cond
);
13220 -- For an AND or AND THEN, recursively process operands
13222 if Nkind
(Cond
) = N_Op_And
or else Nkind
(Cond
) = N_And_Then
then
13223 Set_Expression_Current_Value
(Left_Opnd
(Cond
));
13224 Set_Expression_Current_Value
(Right_Opnd
(Cond
));
13228 -- Check possible relational operator
13230 if Nkind
(Cond
) in N_Op_Compare
then
13231 if Compile_Time_Known_Value
(Right_Opnd
(Cond
)) then
13232 Set_Entity_Current_Value
(Left_Opnd
(Cond
));
13233 elsif Compile_Time_Known_Value
(Left_Opnd
(Cond
)) then
13234 Set_Entity_Current_Value
(Right_Opnd
(Cond
));
13237 elsif Nkind
(Cond
) in N_Type_Conversion
13238 | N_Qualified_Expression
13239 | N_Expression_With_Actions
13241 Set_Expression_Current_Value
(Expression
(Cond
));
13243 -- Check possible boolean variable reference
13246 Set_Entity_Current_Value
(Cond
);
13248 end Set_Expression_Current_Value
;
13250 -- Start of processing for Set_Current_Value_Condition
13253 Set_Expression_Current_Value
(Condition
(Cnode
));
13254 end Set_Current_Value_Condition
;
13256 --------------------------
13257 -- Set_Elaboration_Flag --
13258 --------------------------
13260 procedure Set_Elaboration_Flag
(N
: Node_Id
; Spec_Id
: Entity_Id
) is
13261 Loc
: constant Source_Ptr
:= Sloc
(N
);
13262 Ent
: constant Entity_Id
:= Elaboration_Entity
(Spec_Id
);
13266 if Present
(Ent
) then
13268 -- Nothing to do if at the compilation unit level, because in this
13269 -- case the flag is set by the binder generated elaboration routine.
13271 if Nkind
(Parent
(N
)) = N_Compilation_Unit
then
13274 -- Here we do need to generate an assignment statement
13277 Check_Restriction
(No_Elaboration_Code
, N
);
13280 Make_Assignment_Statement
(Loc
,
13281 Name
=> New_Occurrence_Of
(Ent
, Loc
),
13282 Expression
=> Make_Integer_Literal
(Loc
, Uint_1
));
13284 -- Mark the assignment statement as elaboration code. This allows
13285 -- the early call region mechanism (see Sem_Elab) to properly
13286 -- ignore such assignments even though they are nonpreelaborable
13289 Set_Is_Elaboration_Code
(Asn
);
13291 if Nkind
(Parent
(N
)) = N_Subunit
then
13292 Insert_After
(Corresponding_Stub
(Parent
(N
)), Asn
);
13294 Insert_After
(N
, Asn
);
13299 -- Kill current value indication. This is necessary because the
13300 -- tests of this flag are inserted out of sequence and must not
13301 -- pick up bogus indications of the wrong constant value.
13303 Set_Current_Value
(Ent
, Empty
);
13305 -- If the subprogram is in the current declarative part and
13306 -- 'access has been applied to it, generate an elaboration
13307 -- check at the beginning of the declarations of the body.
13309 if Nkind
(N
) = N_Subprogram_Body
13310 and then Address_Taken
(Spec_Id
)
13312 Ekind
(Scope
(Spec_Id
)) in E_Block | E_Procedure | E_Function
13315 Loc
: constant Source_Ptr
:= Sloc
(N
);
13316 Decls
: constant List_Id
:= Declarations
(N
);
13320 -- No need to generate this check if first entry in the
13321 -- declaration list is a raise of Program_Error now.
13324 and then Nkind
(First
(Decls
)) = N_Raise_Program_Error
13329 -- Otherwise generate the check
13332 Make_Raise_Program_Error
(Loc
,
13335 Left_Opnd
=> New_Occurrence_Of
(Ent
, Loc
),
13336 Right_Opnd
=> Make_Integer_Literal
(Loc
, Uint_0
)),
13337 Reason
=> PE_Access_Before_Elaboration
);
13340 Set_Declarations
(N
, New_List
(Chk
));
13342 Prepend
(Chk
, Decls
);
13350 end Set_Elaboration_Flag
;
13352 ----------------------------
13353 -- Set_Renamed_Subprogram --
13354 ----------------------------
13356 procedure Set_Renamed_Subprogram
(N
: Node_Id
; E
: Entity_Id
) is
13358 -- If input node is an identifier, we can just reset it
13360 if Nkind
(N
) = N_Identifier
then
13361 Set_Chars
(N
, Chars
(E
));
13364 -- Otherwise we have to do a rewrite, preserving Comes_From_Source
13368 CS
: constant Boolean := Comes_From_Source
(N
);
13370 Rewrite
(N
, Make_Identifier
(Sloc
(N
), Chars
(E
)));
13372 Set_Comes_From_Source
(N
, CS
);
13373 Set_Analyzed
(N
, True);
13376 end Set_Renamed_Subprogram
;
13378 ----------------------
13379 -- Side_Effect_Free --
13380 ----------------------
13382 function Side_Effect_Free
13384 Name_Req
: Boolean := False;
13385 Variable_Ref
: Boolean := False) return Boolean
13387 Typ
: constant Entity_Id
:= Etype
(N
);
13388 -- Result type of the expression
13390 function Safe_Prefixed_Reference
(N
: Node_Id
) return Boolean;
13391 -- The argument N is a construct where the Prefix is dereferenced if it
13392 -- is an access type and the result is a variable. The call returns True
13393 -- if the construct is side effect free (not considering side effects in
13394 -- other than the prefix which are to be tested by the caller).
13396 function Within_In_Parameter
(N
: Node_Id
) return Boolean;
13397 -- Determines if N is a subcomponent of a composite in-parameter. If so,
13398 -- N is not side-effect free when the actual is global and modifiable
13399 -- indirectly from within a subprogram, because it may be passed by
13400 -- reference. The front-end must be conservative here and assume that
13401 -- this may happen with any array or record type. On the other hand, we
13402 -- cannot create temporaries for all expressions for which this
13403 -- condition is true, for various reasons that might require clearing up
13404 -- ??? For example, discriminant references that appear out of place, or
13405 -- spurious type errors with class-wide expressions. As a result, we
13406 -- limit the transformation to loop bounds, which is so far the only
13407 -- case that requires it.
13409 -----------------------------
13410 -- Safe_Prefixed_Reference --
13411 -----------------------------
13413 function Safe_Prefixed_Reference
(N
: Node_Id
) return Boolean is
13415 -- If prefix is not side effect free, definitely not safe
13417 if not Side_Effect_Free
(Prefix
(N
), Name_Req
, Variable_Ref
) then
13420 -- If the prefix is of an access type that is not access-to-constant,
13421 -- then this construct is a variable reference, which means it is to
13422 -- be considered to have side effects if Variable_Ref is set True.
13424 elsif Is_Access_Type
(Etype
(Prefix
(N
)))
13425 and then not Is_Access_Constant
(Etype
(Prefix
(N
)))
13426 and then Variable_Ref
13428 -- Exception is a prefix that is the result of a previous removal
13429 -- of side effects.
13431 return Is_Entity_Name
(Prefix
(N
))
13432 and then not Comes_From_Source
(Prefix
(N
))
13433 and then Ekind
(Entity
(Prefix
(N
))) = E_Constant
13434 and then Is_Internal_Name
(Chars
(Entity
(Prefix
(N
))));
13436 -- If the prefix is an explicit dereference then this construct is a
13437 -- variable reference, which means it is to be considered to have
13438 -- side effects if Variable_Ref is True.
13440 -- We do NOT exclude dereferences of access-to-constant types because
13441 -- we handle them as constant view of variables.
13443 elsif Nkind
(Prefix
(N
)) = N_Explicit_Dereference
13444 and then Variable_Ref
13448 -- Note: The following test is the simplest way of solving a complex
13449 -- problem uncovered by the following test (Side effect on loop bound
13450 -- that is a subcomponent of a global variable:
13452 -- with Text_Io; use Text_Io;
13453 -- procedure Tloop is
13456 -- V : Natural := 4;
13457 -- S : String (1..5) := (others => 'a');
13464 -- with procedure Action;
13465 -- procedure Loop_G (Arg : X; Msg : String)
13467 -- procedure Loop_G (Arg : X; Msg : String) is
13469 -- Put_Line ("begin loop_g " & Msg & " will loop till: "
13470 -- & Natural'Image (Arg.V));
13471 -- for Index in 1 .. Arg.V loop
13472 -- Text_Io.Put_Line
13473 -- (Natural'Image (Index) & " " & Arg.S (Index));
13474 -- if Index > 2 then
13478 -- Put_Line ("end loop_g " & Msg);
13481 -- procedure Loop1 is new Loop_G (Modi);
13482 -- procedure Modi is
13485 -- Loop1 (X1, "from modi");
13489 -- Loop1 (X1, "initial");
13492 -- The output of the above program should be:
13494 -- begin loop_g initial will loop till: 4
13498 -- begin loop_g from modi will loop till: 1
13500 -- end loop_g from modi
13502 -- begin loop_g from modi will loop till: 1
13504 -- end loop_g from modi
13505 -- end loop_g initial
13507 -- If a loop bound is a subcomponent of a global variable, a
13508 -- modification of that variable within the loop may incorrectly
13509 -- affect the execution of the loop.
13511 elsif Parent_Kind
(Parent
(N
)) = N_Loop_Parameter_Specification
13512 and then Within_In_Parameter
(Prefix
(N
))
13513 and then Variable_Ref
13517 -- All other cases are side effect free
13522 end Safe_Prefixed_Reference
;
13524 -------------------------
13525 -- Within_In_Parameter --
13526 -------------------------
13528 function Within_In_Parameter
(N
: Node_Id
) return Boolean is
13530 if not Comes_From_Source
(N
) then
13533 elsif Is_Entity_Name
(N
) then
13534 return Ekind
(Entity
(N
)) = E_In_Parameter
;
13536 elsif Nkind
(N
) in N_Indexed_Component | N_Selected_Component
then
13537 return Within_In_Parameter
(Prefix
(N
));
13542 end Within_In_Parameter
;
13544 -- Start of processing for Side_Effect_Free
13547 -- If volatile reference, always consider it to have side effects
13549 if Is_Volatile_Reference
(N
) then
13553 -- Note on checks that could raise Constraint_Error. Strictly, if we
13554 -- take advantage of 11.6, these checks do not count as side effects.
13555 -- However, we would prefer to consider that they are side effects,
13556 -- since the back end CSE does not work very well on expressions which
13557 -- can raise Constraint_Error. On the other hand if we don't consider
13558 -- them to be side effect free, then we get some awkward expansions
13559 -- in -gnato mode, resulting in code insertions at a point where we
13560 -- do not have a clear model for performing the insertions.
13562 -- Special handling for entity names
13564 if Is_Entity_Name
(N
) then
13566 -- A type reference is always side effect free
13568 if Is_Type
(Entity
(N
)) then
13571 -- Variables are considered to be a side effect if Variable_Ref
13572 -- is set or if we have a volatile reference and Name_Req is off.
13573 -- If Name_Req is True then we can't help returning a name which
13574 -- effectively allows multiple references in any case.
13576 elsif Is_Variable
(N
, Use_Original_Node
=> False) then
13577 return not Variable_Ref
13578 and then (not Is_Volatile_Reference
(N
) or else Name_Req
);
13580 -- Any other entity (e.g. a subtype name) is definitely side
13587 -- A value known at compile time is always side effect free
13589 elsif Compile_Time_Known_Value
(N
) then
13592 -- A variable renaming is not side-effect free, because the renaming
13593 -- will function like a macro in the front-end in some cases, and an
13594 -- assignment can modify the component designated by N, so we need to
13595 -- create a temporary for it.
13597 -- The guard testing for Entity being present is needed at least in
13598 -- the case of rewritten predicate expressions, and may well also be
13599 -- appropriate elsewhere. Obviously we can't go testing the entity
13600 -- field if it does not exist, so it's reasonable to say that this is
13601 -- not the renaming case if it does not exist.
13603 elsif Is_Entity_Name
(Original_Node
(N
))
13604 and then Present
(Entity
(Original_Node
(N
)))
13605 and then Is_Renaming_Of_Object
(Entity
(Original_Node
(N
)))
13606 and then Ekind
(Entity
(Original_Node
(N
))) /= E_Constant
13609 RO
: constant Node_Id
:=
13610 Renamed_Object
(Entity
(Original_Node
(N
)));
13613 -- If the renamed object is an indexed component, or an
13614 -- explicit dereference, then the designated object could
13615 -- be modified by an assignment.
13617 if Nkind
(RO
) in N_Indexed_Component | N_Explicit_Dereference
then
13620 -- A selected component must have a safe prefix
13622 elsif Nkind
(RO
) = N_Selected_Component
then
13623 return Safe_Prefixed_Reference
(RO
);
13625 -- In all other cases, designated object cannot be changed so
13626 -- we are side effect free.
13633 -- Remove_Side_Effects generates an object renaming declaration to
13634 -- capture the expression of a class-wide expression. In VM targets
13635 -- the frontend performs no expansion for dispatching calls to
13636 -- class- wide types since they are handled by the VM. Hence, we must
13637 -- locate here if this node corresponds to a previous invocation of
13638 -- Remove_Side_Effects to avoid a never ending loop in the frontend.
13640 elsif not Tagged_Type_Expansion
13641 and then not Comes_From_Source
(N
)
13642 and then Nkind
(Parent
(N
)) = N_Object_Renaming_Declaration
13643 and then Is_Class_Wide_Type
(Typ
)
13647 -- Generating C the type conversion of an access to constrained array
13648 -- type into an access to unconstrained array type involves initializing
13649 -- a fat pointer and the expression cannot be assumed to be free of side
13650 -- effects since it must referenced several times to compute its bounds.
13652 elsif Modify_Tree_For_C
13653 and then Nkind
(N
) = N_Type_Conversion
13654 and then Is_Access_Type
(Typ
)
13655 and then Is_Array_Type
(Designated_Type
(Typ
))
13656 and then not Is_Constrained
(Designated_Type
(Typ
))
13661 -- For other than entity names and compile time known values,
13662 -- check the node kind for special processing.
13666 -- An attribute reference is side-effect free if its expressions
13667 -- are side-effect free and its prefix is side-effect free or is
13668 -- an entity reference.
13670 when N_Attribute_Reference
=>
13671 return Side_Effect_Free_Attribute
(Attribute_Name
(N
))
13673 Side_Effect_Free
(Expressions
(N
), Name_Req
, Variable_Ref
)
13675 (Is_Entity_Name
(Prefix
(N
))
13677 Side_Effect_Free
(Prefix
(N
), Name_Req
, Variable_Ref
));
13679 -- A binary operator is side effect free if and both operands are
13680 -- side effect free. For this purpose binary operators include
13681 -- short circuit forms.
13686 return Side_Effect_Free
(Left_Opnd
(N
), Name_Req
, Variable_Ref
)
13688 Side_Effect_Free
(Right_Opnd
(N
), Name_Req
, Variable_Ref
);
13690 -- Membership tests may have either Right_Opnd or Alternatives set
13692 when N_Membership_Test
=>
13693 return Side_Effect_Free
(Left_Opnd
(N
), Name_Req
, Variable_Ref
)
13695 (if Present
(Right_Opnd
(N
))
13696 then Side_Effect_Free
13697 (Right_Opnd
(N
), Name_Req
, Variable_Ref
)
13698 else Side_Effect_Free
13699 (Alternatives
(N
), Name_Req
, Variable_Ref
));
13701 -- An explicit dereference is side effect free only if it is
13702 -- a side effect free prefixed reference.
13704 when N_Explicit_Dereference
=>
13705 return Safe_Prefixed_Reference
(N
);
13707 -- An expression with action is side effect free if its expression
13708 -- is side effect free and it has no actions.
13710 when N_Expression_With_Actions
=>
13712 Is_Empty_List
(Actions
(N
))
13713 and then Side_Effect_Free
13714 (Expression
(N
), Name_Req
, Variable_Ref
);
13716 -- A call to _rep_to_pos is side effect free, since we generate
13717 -- this pure function call ourselves. Moreover it is critically
13718 -- important to make this exception, since otherwise we can have
13719 -- discriminants in array components which don't look side effect
13720 -- free in the case of an array whose index type is an enumeration
13721 -- type with an enumeration rep clause.
13723 -- All other function calls are not side effect free
13725 when N_Function_Call
=>
13727 Nkind
(Name
(N
)) = N_Identifier
13728 and then Is_TSS
(Name
(N
), TSS_Rep_To_Pos
)
13729 and then Side_Effect_Free
13730 (First
(Parameter_Associations
(N
)),
13731 Name_Req
, Variable_Ref
);
13733 -- An IF expression is side effect free if it's of a scalar type, and
13734 -- all its components are all side effect free (conditions and then
13735 -- actions and else actions). We restrict to scalar types, since it
13736 -- is annoying to deal with things like (if A then B else C)'First
13737 -- where the type involved is a string type.
13739 when N_If_Expression
=>
13741 Is_Scalar_Type
(Typ
)
13742 and then Side_Effect_Free
13743 (Expressions
(N
), Name_Req
, Variable_Ref
);
13745 -- An indexed component is side effect free if it is a side
13746 -- effect free prefixed reference and all the indexing
13747 -- expressions are side effect free.
13749 when N_Indexed_Component
=>
13751 Side_Effect_Free
(Expressions
(N
), Name_Req
, Variable_Ref
)
13752 and then Safe_Prefixed_Reference
(N
);
13754 -- A type qualification, type conversion, or unchecked expression is
13755 -- side effect free if the expression is side effect free.
13757 when N_Qualified_Expression
13758 | N_Type_Conversion
13759 | N_Unchecked_Expression
13761 return Side_Effect_Free
(Expression
(N
), Name_Req
, Variable_Ref
);
13763 -- A selected component is side effect free only if it is a side
13764 -- effect free prefixed reference.
13766 when N_Selected_Component
=>
13767 return Safe_Prefixed_Reference
(N
);
13769 -- A range is side effect free if the bounds are side effect free
13772 return Side_Effect_Free
(Low_Bound
(N
), Name_Req
, Variable_Ref
)
13774 Side_Effect_Free
(High_Bound
(N
), Name_Req
, Variable_Ref
);
13776 -- A slice is side effect free if it is a side effect free
13777 -- prefixed reference and the bounds are side effect free.
13781 Side_Effect_Free
(Discrete_Range
(N
), Name_Req
, Variable_Ref
)
13782 and then Safe_Prefixed_Reference
(N
);
13784 -- A unary operator is side effect free if the operand
13785 -- is side effect free.
13788 return Side_Effect_Free
(Right_Opnd
(N
), Name_Req
, Variable_Ref
);
13790 -- An unchecked type conversion is side effect free only if it
13791 -- is safe and its argument is side effect free.
13793 when N_Unchecked_Type_Conversion
=>
13795 Safe_Unchecked_Type_Conversion
(N
)
13796 and then Side_Effect_Free
13797 (Expression
(N
), Name_Req
, Variable_Ref
);
13799 -- A literal is side effect free
13801 when N_Character_Literal
13802 | N_Integer_Literal
13808 -- An aggregate is side effect free if all its values are compile
13811 when N_Aggregate
=>
13812 return Compile_Time_Known_Aggregate
(N
);
13814 -- We consider that anything else has side effects. This is a bit
13815 -- crude, but we are pretty close for most common cases, and we
13816 -- are certainly correct (i.e. we never return True when the
13817 -- answer should be False).
13822 end Side_Effect_Free
;
13824 -- A list is side effect free if all elements of the list are side
13827 function Side_Effect_Free
13829 Name_Req
: Boolean := False;
13830 Variable_Ref
: Boolean := False) return Boolean
13835 if L
= No_List
or else L
= Error_List
then
13840 while Present
(N
) loop
13841 if not Side_Effect_Free
(N
, Name_Req
, Variable_Ref
) then
13850 end Side_Effect_Free
;
13852 --------------------------------
13853 -- Side_Effect_Free_Attribute --
13854 --------------------------------
13856 function Side_Effect_Free_Attribute
(Name
: Name_Id
) return Boolean is
13865 | Name_Wide_Wide_Image
13867 -- CodePeer doesn't want to see replicated copies of 'Image calls
13869 return not CodePeer_Mode
;
13874 end Side_Effect_Free_Attribute
;
13876 ----------------------------------
13877 -- Silly_Boolean_Array_Not_Test --
13878 ----------------------------------
13880 -- This procedure implements an odd and silly test. We explicitly check
13881 -- for the case where the 'First of the component type is equal to the
13882 -- 'Last of this component type, and if this is the case, we make sure
13883 -- that constraint error is raised. The reason is that the NOT is bound
13884 -- to cause CE in this case, and we will not otherwise catch it.
13886 -- No such check is required for AND and OR, since for both these cases
13887 -- False op False = False, and True op True = True. For the XOR case,
13888 -- see Silly_Boolean_Array_Xor_Test.
13890 -- Believe it or not, this was reported as a bug. Note that nearly always,
13891 -- the test will evaluate statically to False, so the code will be
13892 -- statically removed, and no extra overhead caused.
13894 procedure Silly_Boolean_Array_Not_Test
(N
: Node_Id
; T
: Entity_Id
) is
13895 Loc
: constant Source_Ptr
:= Sloc
(N
);
13896 CT
: constant Entity_Id
:= Component_Type
(T
);
13899 -- The check we install is
13901 -- constraint_error when
13902 -- component_type'first = component_type'last
13903 -- and then array_type'Length /= 0)
13905 -- We need the last guard because we don't want to raise CE for empty
13906 -- arrays since no out of range values result. (Empty arrays with a
13907 -- component type of True .. True -- very useful -- even the ACATS
13908 -- does not test that marginal case).
13911 Make_Raise_Constraint_Error
(Loc
,
13913 Make_And_Then
(Loc
,
13917 Make_Attribute_Reference
(Loc
,
13918 Prefix
=> New_Occurrence_Of
(CT
, Loc
),
13919 Attribute_Name
=> Name_First
),
13922 Make_Attribute_Reference
(Loc
,
13923 Prefix
=> New_Occurrence_Of
(CT
, Loc
),
13924 Attribute_Name
=> Name_Last
)),
13926 Right_Opnd
=> Make_Non_Empty_Check
(Loc
, Right_Opnd
(N
))),
13927 Reason
=> CE_Range_Check_Failed
));
13928 end Silly_Boolean_Array_Not_Test
;
13930 ----------------------------------
13931 -- Silly_Boolean_Array_Xor_Test --
13932 ----------------------------------
13934 -- This procedure implements an odd and silly test. We explicitly check
13935 -- for the XOR case where the component type is True .. True, since this
13936 -- will raise constraint error. A special check is required since CE
13937 -- will not be generated otherwise (cf Expand_Packed_Not).
13939 -- No such check is required for AND and OR, since for both these cases
13940 -- False op False = False, and True op True = True, and no check is
13941 -- required for the case of False .. False, since False xor False = False.
13942 -- See also Silly_Boolean_Array_Not_Test
13944 procedure Silly_Boolean_Array_Xor_Test
13949 Loc
: constant Source_Ptr
:= Sloc
(N
);
13950 CT
: constant Entity_Id
:= Component_Type
(T
);
13953 -- The check we install is
13955 -- constraint_error when
13956 -- Boolean (component_type'First)
13957 -- and then Boolean (component_type'Last)
13958 -- and then array_type'Length /= 0)
13960 -- We need the last guard because we don't want to raise CE for empty
13961 -- arrays since no out of range values result (Empty arrays with a
13962 -- component type of True .. True -- very useful -- even the ACATS
13963 -- does not test that marginal case).
13966 Make_Raise_Constraint_Error
(Loc
,
13968 Make_And_Then
(Loc
,
13970 Make_And_Then
(Loc
,
13972 Convert_To
(Standard_Boolean
,
13973 Make_Attribute_Reference
(Loc
,
13974 Prefix
=> New_Occurrence_Of
(CT
, Loc
),
13975 Attribute_Name
=> Name_First
)),
13978 Convert_To
(Standard_Boolean
,
13979 Make_Attribute_Reference
(Loc
,
13980 Prefix
=> New_Occurrence_Of
(CT
, Loc
),
13981 Attribute_Name
=> Name_Last
))),
13983 Right_Opnd
=> Make_Non_Empty_Check
(Loc
, R
)),
13984 Reason
=> CE_Range_Check_Failed
));
13985 end Silly_Boolean_Array_Xor_Test
;
13987 ----------------------------
13988 -- Small_Integer_Type_For --
13989 ----------------------------
13991 function Small_Integer_Type_For
(S
: Uint
; Uns
: Boolean) return Entity_Id
13994 pragma Assert
(S
<= System_Max_Integer_Size
);
13996 if S
<= Standard_Short_Short_Integer_Size
then
13998 return Standard_Short_Short_Unsigned
;
14000 return Standard_Short_Short_Integer
;
14003 elsif S
<= Standard_Short_Integer_Size
then
14005 return Standard_Short_Unsigned
;
14007 return Standard_Short_Integer
;
14010 elsif S
<= Standard_Integer_Size
then
14012 return Standard_Unsigned
;
14014 return Standard_Integer
;
14017 elsif S
<= Standard_Long_Integer_Size
then
14019 return Standard_Long_Unsigned
;
14021 return Standard_Long_Integer
;
14024 elsif S
<= Standard_Long_Long_Integer_Size
then
14026 return Standard_Long_Long_Unsigned
;
14028 return Standard_Long_Long_Integer
;
14031 elsif S
<= Standard_Long_Long_Long_Integer_Size
then
14033 return Standard_Long_Long_Long_Unsigned
;
14035 return Standard_Long_Long_Long_Integer
;
14039 raise Program_Error
;
14041 end Small_Integer_Type_For
;
14043 -------------------
14044 -- Type_Map_Hash --
14045 -------------------
14047 function Type_Map_Hash
(Id
: Entity_Id
) return Type_Map_Header
is
14049 return Type_Map_Header
(Id
mod Type_Map_Size
);
14052 ------------------------------------------
14053 -- Type_May_Have_Bit_Aligned_Components --
14054 ------------------------------------------
14056 function Type_May_Have_Bit_Aligned_Components
14057 (Typ
: Entity_Id
) return Boolean
14060 -- Array type, check component type
14062 if Is_Array_Type
(Typ
) then
14064 Type_May_Have_Bit_Aligned_Components
(Component_Type
(Typ
));
14066 -- Record type, check components
14068 elsif Is_Record_Type
(Typ
) then
14073 E
:= First_Component_Or_Discriminant
(Typ
);
14074 while Present
(E
) loop
14075 -- This is the crucial test: if the component itself causes
14076 -- trouble, then we can stop and return True.
14078 if Component_May_Be_Bit_Aligned
(E
) then
14082 -- Otherwise, we need to test its type, to see if it may
14083 -- itself contain a troublesome component.
14085 if Type_May_Have_Bit_Aligned_Components
(Etype
(E
)) then
14089 Next_Component_Or_Discriminant
(E
);
14095 -- Type other than array or record is always OK
14100 end Type_May_Have_Bit_Aligned_Components
;
14102 -------------------------------
14103 -- Update_Primitives_Mapping --
14104 -------------------------------
14106 procedure Update_Primitives_Mapping
14107 (Inher_Id
: Entity_Id
;
14108 Subp_Id
: Entity_Id
)
14110 Parent_Type
: constant Entity_Id
:= Find_Dispatching_Type
(Inher_Id
);
14111 Derived_Type
: constant Entity_Id
:= Find_Dispatching_Type
(Subp_Id
);
14114 pragma Assert
(Parent_Type
/= Derived_Type
);
14115 Map_Types
(Parent_Type
, Derived_Type
);
14116 end Update_Primitives_Mapping
;
14118 ----------------------------------
14119 -- Within_Case_Or_If_Expression --
14120 ----------------------------------
14122 function Within_Case_Or_If_Expression
(N
: Node_Id
) return Boolean is
14126 -- Locate an enclosing case or if expression. Note that these constructs
14127 -- can be expanded into Expression_With_Actions, hence the test of the
14131 while Present
(Par
) loop
14132 if Nkind
(Original_Node
(Par
)) in N_Case_Expression | N_If_Expression
14136 -- Prevent the search from going too far
14138 elsif Is_Body_Or_Package_Declaration
(Par
) then
14142 Par
:= Parent
(Par
);
14146 end Within_Case_Or_If_Expression
;
14148 ------------------------------
14149 -- Predicate_Check_In_Scope --
14150 ------------------------------
14152 function Predicate_Check_In_Scope
(N
: Node_Id
) return Boolean is
14156 S
:= Current_Scope
;
14157 while Present
(S
) and then not Is_Subprogram
(S
) loop
14161 if Present
(S
) then
14163 -- Predicate checks should only be enabled in init procs for
14164 -- expressions coming from source.
14166 if Is_Init_Proc
(S
) then
14167 return Comes_From_Source
(N
);
14169 elsif Get_TSS_Name
(S
) /= TSS_Null
14170 and then not Is_Predicate_Function
(S
)
14171 and then not Is_Predicate_Function_M
(S
)
14178 end Predicate_Check_In_Scope
;