1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2018, 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 Elists
; use Elists
;
33 with Errout
; use Errout
;
34 with Exp_Aggr
; use Exp_Aggr
;
35 with Exp_Ch6
; use Exp_Ch6
;
36 with Exp_Ch7
; use Exp_Ch7
;
37 with Exp_Ch11
; use Exp_Ch11
;
38 with Ghost
; use Ghost
;
39 with Inline
; use Inline
;
40 with Itypes
; use Itypes
;
42 with Nlists
; use Nlists
;
43 with Nmake
; use Nmake
;
45 with Restrict
; use Restrict
;
46 with Rident
; use Rident
;
48 with Sem_Aux
; use Sem_Aux
;
49 with Sem_Ch3
; use Sem_Ch3
;
50 with Sem_Ch6
; use Sem_Ch6
;
51 with Sem_Ch8
; use Sem_Ch8
;
52 with Sem_Ch12
; use Sem_Ch12
;
53 with Sem_Ch13
; use Sem_Ch13
;
54 with Sem_Disp
; use Sem_Disp
;
55 with Sem_Elab
; use Sem_Elab
;
56 with Sem_Eval
; use Sem_Eval
;
57 with Sem_Res
; use Sem_Res
;
58 with Sem_Type
; use Sem_Type
;
59 with Sem_Util
; use Sem_Util
;
60 with Snames
; use Snames
;
61 with Stand
; use Stand
;
62 with Stringt
; use Stringt
;
63 with Targparm
; use Targparm
;
64 with Tbuild
; use Tbuild
;
65 with Ttypes
; use Ttypes
;
66 with Urealp
; use Urealp
;
67 with Validsw
; use Validsw
;
70 package body Exp_Util
is
72 ---------------------------------------------------------
73 -- Handling of inherited class-wide pre/postconditions --
74 ---------------------------------------------------------
76 -- Following AI12-0113, the expression for a class-wide condition is
77 -- transformed for a subprogram that inherits it, by replacing calls
78 -- to primitive operations of the original controlling type into the
79 -- corresponding overriding operations of the derived type. The following
80 -- hash table manages this mapping, and is expanded on demand whenever
81 -- such inherited expression needs to be constructed.
83 -- The mapping is also used to check whether an inherited operation has
84 -- a condition that depends on overridden operations. For such an
85 -- operation we must create a wrapper that is then treated as a normal
86 -- overriding. In SPARK mode such operations are illegal.
88 -- For a given root type there may be several type extensions with their
89 -- own overriding operations, so at various times a given operation of
90 -- the root will be mapped into different overridings. The root type is
91 -- also mapped into the current type extension to indicate that its
92 -- operations are mapped into the overriding operations of that current
95 -- The contents of the map are as follows:
99 -- Discriminant (Entity_Id) Discriminant (Entity_Id)
100 -- Discriminant (Entity_Id) Non-discriminant name (Entity_Id)
101 -- Discriminant (Entity_Id) Expression (Node_Id)
102 -- Primitive subprogram (Entity_Id) Primitive subprogram (Entity_Id)
103 -- Type (Entity_Id) Type (Entity_Id)
105 Type_Map_Size
: constant := 511;
107 subtype Type_Map_Header
is Integer range 0 .. Type_Map_Size
- 1;
108 function Type_Map_Hash
(Id
: Entity_Id
) return Type_Map_Header
;
110 package Type_Map
is new GNAT
.HTable
.Simple_HTable
111 (Header_Num
=> Type_Map_Header
,
113 Element
=> Node_Or_Entity_Id
,
115 Hash
=> Type_Map_Hash
,
118 -----------------------
119 -- Local Subprograms --
120 -----------------------
122 function Build_Task_Array_Image
126 Dyn
: Boolean := False) return Node_Id
;
127 -- Build function to generate the image string for a task that is an array
128 -- component, concatenating the images of each index. To avoid storage
129 -- leaks, the string is built with successive slice assignments. The flag
130 -- Dyn indicates whether this is called for the initialization procedure of
131 -- an array of tasks, or for the name of a dynamically created task that is
132 -- assigned to an indexed component.
134 function Build_Task_Image_Function
138 Res
: Entity_Id
) return Node_Id
;
139 -- Common processing for Task_Array_Image and Task_Record_Image. Build
140 -- function body that computes image.
142 procedure Build_Task_Image_Prefix
151 -- Common processing for Task_Array_Image and Task_Record_Image. Create
152 -- local variables and assign prefix of name to result string.
154 function Build_Task_Record_Image
157 Dyn
: Boolean := False) return Node_Id
;
158 -- Build function to generate the image string for a task that is a record
159 -- component. Concatenate name of variable with that of selector. The flag
160 -- Dyn indicates whether this is called for the initialization procedure of
161 -- record with task components, or for a dynamically created task that is
162 -- assigned to a selected component.
164 procedure Evaluate_Slice_Bounds
(Slice
: Node_Id
);
165 -- Force evaluation of bounds of a slice, which may be given by a range
166 -- or by a subtype indication with or without a constraint.
168 function Is_Verifiable_DIC_Pragma
(Prag
: Node_Id
) return Boolean;
169 -- Determine whether pragma Default_Initial_Condition denoted by Prag has
170 -- an assertion expression that should be verified at run time.
172 function Make_CW_Equivalent_Type
174 E
: Node_Id
) return Entity_Id
;
175 -- T is a class-wide type entity, E is the initial expression node that
176 -- constrains T in case such as: " X: T := E" or "new T'(E)". This function
177 -- returns the entity of the Equivalent type and inserts on the fly the
178 -- necessary declaration such as:
180 -- type anon is record
181 -- _parent : Root_Type (T); constrained with E discriminants (if any)
182 -- Extension : String (1 .. expr to match size of E);
185 -- This record is compatible with any object of the class of T thanks to
186 -- the first field and has the same size as E thanks to the second.
188 function Make_Literal_Range
190 Literal_Typ
: Entity_Id
) return Node_Id
;
191 -- Produce a Range node whose bounds are:
192 -- Low_Bound (Literal_Type) ..
193 -- Low_Bound (Literal_Type) + (Length (Literal_Typ) - 1)
194 -- this is used for expanding declarations like X : String := "sdfgdfg";
196 -- If the index type of the target array is not integer, we generate:
197 -- Low_Bound (Literal_Type) ..
199 -- (Literal_Type'Pos (Low_Bound (Literal_Type))
200 -- + (Length (Literal_Typ) -1))
202 function Make_Non_Empty_Check
204 N
: Node_Id
) return Node_Id
;
205 -- Produce a boolean expression checking that the unidimensional array
206 -- node N is not empty.
208 function New_Class_Wide_Subtype
210 N
: Node_Id
) return Entity_Id
;
211 -- Create an implicit subtype of CW_Typ attached to node N
213 function Requires_Cleanup_Actions
216 Nested_Constructs
: Boolean) return Boolean;
217 -- Given a list L, determine whether it contains one of the following:
219 -- 1) controlled objects
220 -- 2) library-level tagged types
222 -- Lib_Level is True when the list comes from a construct at the library
223 -- level, and False otherwise. Nested_Constructs is True when any nested
224 -- packages declared in L must be processed, and False otherwise.
226 -------------------------------------
227 -- Activate_Atomic_Synchronization --
228 -------------------------------------
230 procedure Activate_Atomic_Synchronization
(N
: Node_Id
) is
234 case Nkind
(Parent
(N
)) is
236 -- Check for cases of appearing in the prefix of a construct where we
237 -- don't need atomic synchronization for this kind of usage.
240 -- Nothing to do if we are the prefix of an attribute, since we
241 -- do not want an atomic sync operation for things like 'Size.
243 N_Attribute_Reference
245 -- The N_Reference node is like an attribute
249 -- Nothing to do for a reference to a component (or components)
250 -- of a composite object. Only reads and updates of the object
251 -- as a whole require atomic synchronization (RM C.6 (15)).
253 | N_Indexed_Component
254 | N_Selected_Component
257 -- For all the above cases, nothing to do if we are the prefix
259 if Prefix
(Parent
(N
)) = N
then
267 -- Nothing to do for the identifier in an object renaming declaration,
268 -- the renaming itself does not need atomic synchronization.
270 if Nkind
(Parent
(N
)) = N_Object_Renaming_Declaration
then
274 -- Go ahead and set the flag
276 Set_Atomic_Sync_Required
(N
);
278 -- Generate info message if requested
280 if Warn_On_Atomic_Synchronization
then
286 | N_Selected_Component
288 Msg_Node
:= Selector_Name
(N
);
290 when N_Explicit_Dereference
291 | N_Indexed_Component
296 pragma Assert
(False);
300 if Present
(Msg_Node
) then
302 ("info: atomic synchronization set for &?N?", Msg_Node
);
305 ("info: atomic synchronization set?N?", N
);
308 end Activate_Atomic_Synchronization
;
310 ----------------------
311 -- Adjust_Condition --
312 ----------------------
314 procedure Adjust_Condition
(N
: Node_Id
) is
321 Loc
: constant Source_Ptr
:= Sloc
(N
);
322 T
: constant Entity_Id
:= Etype
(N
);
326 -- Defend against a call where the argument has no type, or has a
327 -- type that is not Boolean. This can occur because of prior errors.
329 if No
(T
) or else not Is_Boolean_Type
(T
) then
333 -- Apply validity checking if needed
335 if Validity_Checks_On
and Validity_Check_Tests
then
339 -- Immediate return if standard boolean, the most common case,
340 -- where nothing needs to be done.
342 if Base_Type
(T
) = Standard_Boolean
then
346 -- Case of zero/non-zero semantics or non-standard enumeration
347 -- representation. In each case, we rewrite the node as:
349 -- ityp!(N) /= False'Enum_Rep
351 -- where ityp is an integer type with large enough size to hold any
354 if Nonzero_Is_True
(T
) or else Has_Non_Standard_Rep
(T
) then
355 if Esize
(T
) <= Esize
(Standard_Integer
) then
356 Ti
:= Standard_Integer
;
358 Ti
:= Standard_Long_Long_Integer
;
363 Left_Opnd
=> Unchecked_Convert_To
(Ti
, N
),
365 Make_Attribute_Reference
(Loc
,
366 Attribute_Name
=> Name_Enum_Rep
,
368 New_Occurrence_Of
(First_Literal
(T
), Loc
))));
369 Analyze_And_Resolve
(N
, Standard_Boolean
);
372 Rewrite
(N
, Convert_To
(Standard_Boolean
, N
));
373 Analyze_And_Resolve
(N
, Standard_Boolean
);
376 end Adjust_Condition
;
378 ------------------------
379 -- Adjust_Result_Type --
380 ------------------------
382 procedure Adjust_Result_Type
(N
: Node_Id
; T
: Entity_Id
) is
384 -- Ignore call if current type is not Standard.Boolean
386 if Etype
(N
) /= Standard_Boolean
then
390 -- If result is already of correct type, nothing to do. Note that
391 -- this will get the most common case where everything has a type
392 -- of Standard.Boolean.
394 if Base_Type
(T
) = Standard_Boolean
then
399 KP
: constant Node_Kind
:= Nkind
(Parent
(N
));
402 -- If result is to be used as a Condition in the syntax, no need
403 -- to convert it back, since if it was changed to Standard.Boolean
404 -- using Adjust_Condition, that is just fine for this usage.
406 if KP
in N_Raise_xxx_Error
or else KP
in N_Has_Condition
then
409 -- If result is an operand of another logical operation, no need
410 -- to reset its type, since Standard.Boolean is just fine, and
411 -- such operations always do Adjust_Condition on their operands.
413 elsif KP
in N_Op_Boolean
414 or else KP
in N_Short_Circuit
415 or else KP
= N_Op_Not
419 -- Otherwise we perform a conversion from the current type, which
420 -- must be Standard.Boolean, to the desired type. Use the base
421 -- type to prevent spurious constraint checks that are extraneous
422 -- to the transformation. The type and its base have the same
423 -- representation, standard or otherwise.
427 Rewrite
(N
, Convert_To
(Base_Type
(T
), N
));
428 Analyze_And_Resolve
(N
, Base_Type
(T
));
432 end Adjust_Result_Type
;
434 --------------------------
435 -- Append_Freeze_Action --
436 --------------------------
438 procedure Append_Freeze_Action
(T
: Entity_Id
; N
: Node_Id
) is
442 Ensure_Freeze_Node
(T
);
443 Fnode
:= Freeze_Node
(T
);
445 if No
(Actions
(Fnode
)) then
446 Set_Actions
(Fnode
, New_List
(N
));
448 Append
(N
, Actions
(Fnode
));
451 end Append_Freeze_Action
;
453 ---------------------------
454 -- Append_Freeze_Actions --
455 ---------------------------
457 procedure Append_Freeze_Actions
(T
: Entity_Id
; L
: List_Id
) is
465 Ensure_Freeze_Node
(T
);
466 Fnode
:= Freeze_Node
(T
);
468 if No
(Actions
(Fnode
)) then
469 Set_Actions
(Fnode
, L
);
471 Append_List
(L
, Actions
(Fnode
));
473 end Append_Freeze_Actions
;
475 ------------------------------------
476 -- Build_Allocate_Deallocate_Proc --
477 ------------------------------------
479 procedure Build_Allocate_Deallocate_Proc
481 Is_Allocate
: Boolean)
483 function Find_Object
(E
: Node_Id
) return Node_Id
;
484 -- Given an arbitrary expression of an allocator, try to find an object
485 -- reference in it, otherwise return the original expression.
487 function Is_Allocate_Deallocate_Proc
(Subp
: Entity_Id
) return Boolean;
488 -- Determine whether subprogram Subp denotes a custom allocate or
495 function Find_Object
(E
: Node_Id
) return Node_Id
is
499 pragma Assert
(Is_Allocate
);
503 if Nkind
(Expr
) = N_Explicit_Dereference
then
504 Expr
:= Prefix
(Expr
);
506 elsif Nkind
(Expr
) = N_Qualified_Expression
then
507 Expr
:= Expression
(Expr
);
509 elsif Nkind
(Expr
) = N_Unchecked_Type_Conversion
then
511 -- When interface class-wide types are involved in allocation,
512 -- the expander introduces several levels of address arithmetic
513 -- to perform dispatch table displacement. In this scenario the
514 -- object appears as:
516 -- Tag_Ptr (Base_Address (<object>'Address))
518 -- Detect this case and utilize the whole expression as the
519 -- "object" since it now points to the proper dispatch table.
521 if Is_RTE
(Etype
(Expr
), RE_Tag_Ptr
) then
524 -- Continue to strip the object
527 Expr
:= Expression
(Expr
);
538 ---------------------------------
539 -- Is_Allocate_Deallocate_Proc --
540 ---------------------------------
542 function Is_Allocate_Deallocate_Proc
(Subp
: Entity_Id
) return Boolean is
544 -- Look for a subprogram body with only one statement which is a
545 -- call to Allocate_Any_Controlled / Deallocate_Any_Controlled.
547 if Ekind
(Subp
) = E_Procedure
548 and then Nkind
(Parent
(Parent
(Subp
))) = N_Subprogram_Body
551 HSS
: constant Node_Id
:=
552 Handled_Statement_Sequence
(Parent
(Parent
(Subp
)));
556 if Present
(Statements
(HSS
))
557 and then Nkind
(First
(Statements
(HSS
))) =
558 N_Procedure_Call_Statement
560 Proc
:= Entity
(Name
(First
(Statements
(HSS
))));
563 Is_RTE
(Proc
, RE_Allocate_Any_Controlled
)
564 or else Is_RTE
(Proc
, RE_Deallocate_Any_Controlled
);
570 end Is_Allocate_Deallocate_Proc
;
574 Desig_Typ
: Entity_Id
;
578 Proc_To_Call
: Node_Id
:= Empty
;
581 -- Start of processing for Build_Allocate_Deallocate_Proc
584 -- Obtain the attributes of the allocation / deallocation
586 if Nkind
(N
) = N_Free_Statement
then
587 Expr
:= Expression
(N
);
588 Ptr_Typ
:= Base_Type
(Etype
(Expr
));
589 Proc_To_Call
:= Procedure_To_Call
(N
);
592 if Nkind
(N
) = N_Object_Declaration
then
593 Expr
:= Expression
(N
);
598 -- In certain cases an allocator with a qualified expression may
599 -- be relocated and used as the initialization expression of a
603 -- Obj : Ptr_Typ := new Desig_Typ'(...);
606 -- Tmp : Ptr_Typ := new Desig_Typ'(...);
607 -- Obj : Ptr_Typ := Tmp;
609 -- Since the allocator is always marked as analyzed to avoid infinite
610 -- expansion, it will never be processed by this routine given that
611 -- the designated type needs finalization actions. Detect this case
612 -- and complete the expansion of the allocator.
614 if Nkind
(Expr
) = N_Identifier
615 and then Nkind
(Parent
(Entity
(Expr
))) = N_Object_Declaration
616 and then Nkind
(Expression
(Parent
(Entity
(Expr
)))) = N_Allocator
618 Build_Allocate_Deallocate_Proc
(Parent
(Entity
(Expr
)), True);
622 -- The allocator may have been rewritten into something else in which
623 -- case the expansion performed by this routine does not apply.
625 if Nkind
(Expr
) /= N_Allocator
then
629 Ptr_Typ
:= Base_Type
(Etype
(Expr
));
630 Proc_To_Call
:= Procedure_To_Call
(Expr
);
633 Pool_Id
:= Associated_Storage_Pool
(Ptr_Typ
);
634 Desig_Typ
:= Available_View
(Designated_Type
(Ptr_Typ
));
636 -- Handle concurrent types
638 if Is_Concurrent_Type
(Desig_Typ
)
639 and then Present
(Corresponding_Record_Type
(Desig_Typ
))
641 Desig_Typ
:= Corresponding_Record_Type
(Desig_Typ
);
644 -- Do not process allocations / deallocations without a pool
649 -- Do not process allocations on / deallocations from the secondary
652 elsif Is_RTE
(Pool_Id
, RE_SS_Pool
)
653 or else (Nkind
(Expr
) = N_Allocator
654 and then Is_RTE
(Storage_Pool
(Expr
), RE_SS_Pool
))
658 -- Optimize the case where we are using the default Global_Pool_Object,
659 -- and we don't need the heavy finalization machinery.
661 elsif Pool_Id
= RTE
(RE_Global_Pool_Object
)
662 and then not Needs_Finalization
(Desig_Typ
)
666 -- Do not replicate the machinery if the allocator / free has already
667 -- been expanded and has a custom Allocate / Deallocate.
669 elsif Present
(Proc_To_Call
)
670 and then Is_Allocate_Deallocate_Proc
(Proc_To_Call
)
675 -- Finalization actions are required when the object to be allocated or
676 -- deallocated needs these actions and the associated access type is not
677 -- subject to pragma No_Heap_Finalization.
680 Needs_Finalization
(Desig_Typ
)
681 and then not No_Heap_Finalization
(Ptr_Typ
);
685 -- Do nothing if the access type may never allocate / deallocate
688 if No_Pool_Assigned
(Ptr_Typ
) then
692 -- The allocation / deallocation of a controlled object must be
693 -- chained on / detached from a finalization master.
695 pragma Assert
(Present
(Finalization_Master
(Ptr_Typ
)));
697 -- The only other kind of allocation / deallocation supported by this
698 -- routine is on / from a subpool.
700 elsif Nkind
(Expr
) = N_Allocator
701 and then No
(Subpool_Handle_Name
(Expr
))
707 Loc
: constant Source_Ptr
:= Sloc
(N
);
708 Addr_Id
: constant Entity_Id
:= Make_Temporary
(Loc
, 'A');
709 Alig_Id
: constant Entity_Id
:= Make_Temporary
(Loc
, 'L');
710 Proc_Id
: constant Entity_Id
:= Make_Temporary
(Loc
, 'P');
711 Size_Id
: constant Entity_Id
:= Make_Temporary
(Loc
, 'S');
714 Fin_Addr_Id
: Entity_Id
;
715 Fin_Mas_Act
: Node_Id
;
716 Fin_Mas_Id
: Entity_Id
;
717 Proc_To_Call
: Entity_Id
;
718 Subpool
: Node_Id
:= Empty
;
721 -- Step 1: Construct all the actuals for the call to library routine
722 -- Allocate_Any_Controlled / Deallocate_Any_Controlled.
726 Actuals
:= New_List
(New_Occurrence_Of
(Pool_Id
, Loc
));
732 if Nkind
(Expr
) = N_Allocator
then
733 Subpool
:= Subpool_Handle_Name
(Expr
);
736 -- If a subpool is present it can be an arbitrary name, so make
737 -- the actual by copying the tree.
739 if Present
(Subpool
) then
740 Append_To
(Actuals
, New_Copy_Tree
(Subpool
, New_Sloc
=> Loc
));
742 Append_To
(Actuals
, Make_Null
(Loc
));
745 -- c) Finalization master
748 Fin_Mas_Id
:= Finalization_Master
(Ptr_Typ
);
749 Fin_Mas_Act
:= New_Occurrence_Of
(Fin_Mas_Id
, Loc
);
751 -- Handle the case where the master is actually a pointer to a
752 -- master. This case arises in build-in-place functions.
754 if Is_Access_Type
(Etype
(Fin_Mas_Id
)) then
755 Append_To
(Actuals
, Fin_Mas_Act
);
758 Make_Attribute_Reference
(Loc
,
759 Prefix
=> Fin_Mas_Act
,
760 Attribute_Name
=> Name_Unrestricted_Access
));
763 Append_To
(Actuals
, Make_Null
(Loc
));
766 -- d) Finalize_Address
768 -- Primitive Finalize_Address is never generated in CodePeer mode
769 -- since it contains an Unchecked_Conversion.
771 if Needs_Fin
and then not CodePeer_Mode
then
772 Fin_Addr_Id
:= Finalize_Address
(Desig_Typ
);
773 pragma Assert
(Present
(Fin_Addr_Id
));
776 Make_Attribute_Reference
(Loc
,
777 Prefix
=> New_Occurrence_Of
(Fin_Addr_Id
, Loc
),
778 Attribute_Name
=> Name_Unrestricted_Access
));
780 Append_To
(Actuals
, Make_Null
(Loc
));
788 Append_To
(Actuals
, New_Occurrence_Of
(Addr_Id
, Loc
));
789 Append_To
(Actuals
, New_Occurrence_Of
(Size_Id
, Loc
));
791 if Is_Allocate
or else not Is_Class_Wide_Type
(Desig_Typ
) then
792 Append_To
(Actuals
, New_Occurrence_Of
(Alig_Id
, Loc
));
794 -- For deallocation of class-wide types we obtain the value of
795 -- alignment from the Type Specific Record of the deallocated object.
796 -- This is needed because the frontend expansion of class-wide types
797 -- into equivalent types confuses the back end.
803 -- ... because 'Alignment applied to class-wide types is expanded
804 -- into the code that reads the value of alignment from the TSD
805 -- (see Expand_N_Attribute_Reference)
808 Unchecked_Convert_To
(RTE
(RE_Storage_Offset
),
809 Make_Attribute_Reference
(Loc
,
811 Make_Explicit_Dereference
(Loc
, Relocate_Node
(Expr
)),
812 Attribute_Name
=> Name_Alignment
)));
818 Is_Controlled
: declare
819 Flag_Id
: constant Entity_Id
:= Make_Temporary
(Loc
, 'F');
827 Temp
:= Find_Object
(Expression
(Expr
));
832 -- Processing for allocations where the expression is a subtype
836 and then Is_Entity_Name
(Temp
)
837 and then Is_Type
(Entity
(Temp
))
842 (Needs_Finalization
(Entity
(Temp
))), Loc
);
844 -- The allocation / deallocation of a class-wide object relies
845 -- on a runtime check to determine whether the object is truly
846 -- controlled or not. Depending on this check, the finalization
847 -- machinery will request or reclaim extra storage reserved for
850 elsif Is_Class_Wide_Type
(Desig_Typ
) then
852 -- Detect a special case where interface class-wide types
853 -- are involved as the object appears as:
855 -- Tag_Ptr (Base_Address (<object>'Address))
857 -- The expression already yields the proper tag, generate:
861 if Is_RTE
(Etype
(Temp
), RE_Tag_Ptr
) then
863 Make_Explicit_Dereference
(Loc
,
864 Prefix
=> Relocate_Node
(Temp
));
866 -- In the default case, obtain the tag of the object about
867 -- to be allocated / deallocated. Generate:
871 -- If the object is an unchecked conversion (typically to
872 -- an access to class-wide type), we must preserve the
873 -- conversion to ensure that the object is seen as tagged
874 -- in the code that follows.
879 if Nkind
(Parent
(Pref
)) = N_Unchecked_Type_Conversion
881 Pref
:= Parent
(Pref
);
885 Make_Attribute_Reference
(Loc
,
886 Prefix
=> Relocate_Node
(Pref
),
887 Attribute_Name
=> Name_Tag
);
891 -- Needs_Finalization (<Param>)
894 Make_Function_Call
(Loc
,
896 New_Occurrence_Of
(RTE
(RE_Needs_Finalization
), Loc
),
897 Parameter_Associations
=> New_List
(Param
));
899 -- Processing for generic actuals
901 elsif Is_Generic_Actual_Type
(Desig_Typ
) then
903 New_Occurrence_Of
(Boolean_Literals
904 (Needs_Finalization
(Base_Type
(Desig_Typ
))), Loc
);
906 -- The object does not require any specialized checks, it is
907 -- known to be controlled.
910 Flag_Expr
:= New_Occurrence_Of
(Standard_True
, Loc
);
913 -- Create the temporary which represents the finalization state
914 -- of the expression. Generate:
916 -- F : constant Boolean := <Flag_Expr>;
919 Make_Object_Declaration
(Loc
,
920 Defining_Identifier
=> Flag_Id
,
921 Constant_Present
=> True,
923 New_Occurrence_Of
(Standard_Boolean
, Loc
),
924 Expression
=> Flag_Expr
));
926 Append_To
(Actuals
, New_Occurrence_Of
(Flag_Id
, Loc
));
929 -- The object is not controlled
932 Append_To
(Actuals
, New_Occurrence_Of
(Standard_False
, Loc
));
939 New_Occurrence_Of
(Boolean_Literals
(Present
(Subpool
)), Loc
));
942 -- Step 2: Build a wrapper Allocate / Deallocate which internally
943 -- calls Allocate_Any_Controlled / Deallocate_Any_Controlled.
945 -- Select the proper routine to call
948 Proc_To_Call
:= RTE
(RE_Allocate_Any_Controlled
);
950 Proc_To_Call
:= RTE
(RE_Deallocate_Any_Controlled
);
953 -- Create a custom Allocate / Deallocate routine which has identical
954 -- profile to that of System.Storage_Pools.
957 Make_Subprogram_Body
(Loc
,
962 Make_Procedure_Specification
(Loc
,
963 Defining_Unit_Name
=> Proc_Id
,
964 Parameter_Specifications
=> New_List
(
966 -- P : Root_Storage_Pool
968 Make_Parameter_Specification
(Loc
,
969 Defining_Identifier
=> Make_Temporary
(Loc
, 'P'),
971 New_Occurrence_Of
(RTE
(RE_Root_Storage_Pool
), Loc
)),
975 Make_Parameter_Specification
(Loc
,
976 Defining_Identifier
=> Addr_Id
,
977 Out_Present
=> Is_Allocate
,
979 New_Occurrence_Of
(RTE
(RE_Address
), Loc
)),
983 Make_Parameter_Specification
(Loc
,
984 Defining_Identifier
=> Size_Id
,
986 New_Occurrence_Of
(RTE
(RE_Storage_Count
), Loc
)),
990 Make_Parameter_Specification
(Loc
,
991 Defining_Identifier
=> Alig_Id
,
993 New_Occurrence_Of
(RTE
(RE_Storage_Count
), Loc
)))),
995 Declarations
=> No_List
,
997 Handled_Statement_Sequence
=>
998 Make_Handled_Sequence_Of_Statements
(Loc
,
999 Statements
=> New_List
(
1000 Make_Procedure_Call_Statement
(Loc
,
1002 New_Occurrence_Of
(Proc_To_Call
, Loc
),
1003 Parameter_Associations
=> Actuals
)))),
1004 Suppress
=> All_Checks
);
1006 -- The newly generated Allocate / Deallocate becomes the default
1007 -- procedure to call when the back end processes the allocation /
1011 Set_Procedure_To_Call
(Expr
, Proc_Id
);
1013 Set_Procedure_To_Call
(N
, Proc_Id
);
1016 end Build_Allocate_Deallocate_Proc
;
1018 -------------------------------
1019 -- Build_Abort_Undefer_Block --
1020 -------------------------------
1022 function Build_Abort_Undefer_Block
1025 Context
: Node_Id
) return Node_Id
1027 Exceptions_OK
: constant Boolean :=
1028 not Restriction_Active
(No_Exception_Propagation
);
1036 -- The block should be generated only when undeferring abort in the
1037 -- context of a potential exception.
1039 pragma Assert
(Abort_Allowed
and Exceptions_OK
);
1045 -- Abort_Undefer_Direct;
1048 AUD
:= RTE
(RE_Abort_Undefer_Direct
);
1051 Make_Handled_Sequence_Of_Statements
(Loc
,
1052 Statements
=> Stmts
,
1053 At_End_Proc
=> New_Occurrence_Of
(AUD
, Loc
));
1056 Make_Block_Statement
(Loc
,
1057 Handled_Statement_Sequence
=> HSS
);
1058 Set_Is_Abort_Block
(Blk
);
1060 Add_Block_Identifier
(Blk
, Blk_Id
);
1061 Expand_At_End_Handler
(HSS
, Blk_Id
);
1063 -- Present the Abort_Undefer_Direct function to the back end to inline
1064 -- the call to the routine.
1066 Add_Inlined_Body
(AUD
, Context
);
1069 end Build_Abort_Undefer_Block
;
1071 ---------------------------------
1072 -- Build_Class_Wide_Expression --
1073 ---------------------------------
1075 procedure Build_Class_Wide_Expression
1078 Par_Subp
: Entity_Id
;
1079 Adjust_Sloc
: Boolean;
1080 Needs_Wrapper
: out Boolean)
1082 function Replace_Entity
(N
: Node_Id
) return Traverse_Result
;
1083 -- Replace reference to formal of inherited operation or to primitive
1084 -- operation of root type, with corresponding entity for derived type,
1085 -- when constructing the class-wide condition of an overriding
1088 --------------------
1089 -- Replace_Entity --
1090 --------------------
1092 function Replace_Entity
(N
: Node_Id
) return Traverse_Result
is
1097 Adjust_Inherited_Pragma_Sloc
(N
);
1100 if Nkind
(N
) = N_Identifier
1101 and then Present
(Entity
(N
))
1103 (Is_Formal
(Entity
(N
)) or else Is_Subprogram
(Entity
(N
)))
1105 (Nkind
(Parent
(N
)) /= N_Attribute_Reference
1106 or else Attribute_Name
(Parent
(N
)) /= Name_Class
)
1108 -- The replacement does not apply to dispatching calls within the
1109 -- condition, but only to calls whose static tag is that of the
1112 if Is_Subprogram
(Entity
(N
))
1113 and then Nkind
(Parent
(N
)) = N_Function_Call
1114 and then Present
(Controlling_Argument
(Parent
(N
)))
1119 -- Determine whether entity has a renaming
1121 New_E
:= Type_Map
.Get
(Entity
(N
));
1123 if Present
(New_E
) then
1124 Rewrite
(N
, New_Occurrence_Of
(New_E
, Sloc
(N
)));
1126 -- AI12-0166: a precondition for a protected operation
1127 -- cannot include an internal call to a protected function
1128 -- of the type. In the case of an inherited condition for an
1129 -- overriding operation, both the operation and the function
1130 -- are given by primitive wrappers.
1132 if Ekind
(New_E
) = E_Function
1133 and then Is_Primitive_Wrapper
(New_E
)
1134 and then Is_Primitive_Wrapper
(Subp
)
1135 and then Scope
(Subp
) = Scope
(New_E
)
1137 Error_Msg_Node_2
:= Wrapped_Entity
(Subp
);
1139 ("internal call to& cannot appear in inherited "
1140 & "precondition of protected operation&",
1141 N
, Wrapped_Entity
(New_E
));
1144 -- If the entity is an overridden primitive and we are not
1145 -- in GNATprove mode, we must build a wrapper for the current
1146 -- inherited operation. If the reference is the prefix of an
1147 -- attribute such as 'Result (or others ???) there is no need
1148 -- for a wrapper: the condition is just rewritten in terms of
1149 -- the inherited subprogram.
1151 if Is_Subprogram
(New_E
)
1152 and then Nkind
(Parent
(N
)) /= N_Attribute_Reference
1153 and then not GNATprove_Mode
1155 Needs_Wrapper
:= True;
1159 -- Check that there are no calls left to abstract operations if
1160 -- the current subprogram is not abstract.
1162 if Nkind
(Parent
(N
)) = N_Function_Call
1163 and then N
= Name
(Parent
(N
))
1165 if not Is_Abstract_Subprogram
(Subp
)
1166 and then Is_Abstract_Subprogram
(Entity
(N
))
1168 Error_Msg_Sloc
:= Sloc
(Current_Scope
);
1169 Error_Msg_Node_2
:= Subp
;
1170 if Comes_From_Source
(Subp
) then
1172 ("cannot call abstract subprogram & in inherited "
1173 & "condition for&#", Subp
, Entity
(N
));
1176 ("cannot call abstract subprogram & in inherited "
1177 & "condition for inherited&#", Subp
, Entity
(N
));
1180 -- In SPARK mode, reject an inherited condition for an
1181 -- inherited operation if it contains a call to an overriding
1182 -- operation, because this implies that the pre/postconditions
1183 -- of the inherited operation have changed silently.
1185 elsif SPARK_Mode
= On
1186 and then Warn_On_Suspicious_Contract
1187 and then Present
(Alias
(Subp
))
1188 and then Present
(New_E
)
1189 and then Comes_From_Source
(New_E
)
1192 ("cannot modify inherited condition (SPARK RM 6.1.1(1))",
1194 Error_Msg_Sloc
:= Sloc
(New_E
);
1195 Error_Msg_Node_2
:= Subp
;
1197 ("\overriding of&# forces overriding of&",
1198 Parent
(Subp
), New_E
);
1202 -- Update type of function call node, which should be the same as
1203 -- the function's return type.
1205 if Is_Subprogram
(Entity
(N
))
1206 and then Nkind
(Parent
(N
)) = N_Function_Call
1208 Set_Etype
(Parent
(N
), Etype
(Entity
(N
)));
1211 -- The whole expression will be reanalyzed
1213 elsif Nkind
(N
) in N_Has_Etype
then
1214 Set_Analyzed
(N
, False);
1220 procedure Replace_Condition_Entities
is
1221 new Traverse_Proc
(Replace_Entity
);
1225 Par_Formal
: Entity_Id
;
1226 Subp_Formal
: Entity_Id
;
1228 -- Start of processing for Build_Class_Wide_Expression
1231 Needs_Wrapper
:= False;
1233 -- Add mapping from old formals to new formals
1235 Par_Formal
:= First_Formal
(Par_Subp
);
1236 Subp_Formal
:= First_Formal
(Subp
);
1238 while Present
(Par_Formal
) and then Present
(Subp_Formal
) loop
1239 Type_Map
.Set
(Par_Formal
, Subp_Formal
);
1240 Next_Formal
(Par_Formal
);
1241 Next_Formal
(Subp_Formal
);
1244 Replace_Condition_Entities
(Prag
);
1245 end Build_Class_Wide_Expression
;
1247 --------------------
1248 -- Build_DIC_Call --
1249 --------------------
1251 function Build_DIC_Call
1254 Typ
: Entity_Id
) return Node_Id
1256 Proc_Id
: constant Entity_Id
:= DIC_Procedure
(Typ
);
1257 Formal_Typ
: constant Entity_Id
:= Etype
(First_Formal
(Proc_Id
));
1261 Make_Procedure_Call_Statement
(Loc
,
1262 Name
=> New_Occurrence_Of
(Proc_Id
, Loc
),
1263 Parameter_Associations
=> New_List
(
1264 Make_Unchecked_Type_Conversion
(Loc
,
1265 Subtype_Mark
=> New_Occurrence_Of
(Formal_Typ
, Loc
),
1266 Expression
=> New_Occurrence_Of
(Obj_Id
, Loc
))));
1269 ------------------------------
1270 -- Build_DIC_Procedure_Body --
1271 ------------------------------
1273 -- WARNING: This routine manages Ghost regions. Return statements must be
1274 -- replaced by gotos which jump to the end of the routine and restore the
1277 procedure Build_DIC_Procedure_Body
1279 For_Freeze
: Boolean := False)
1281 procedure Add_DIC_Check
1282 (DIC_Prag
: Node_Id
;
1284 Stmts
: in out List_Id
);
1285 -- Subsidiary to all Add_xxx_DIC routines. Add a runtime check to verify
1286 -- assertion expression DIC_Expr of pragma DIC_Prag. All generated code
1287 -- is added to list Stmts.
1289 procedure Add_Inherited_DIC
1290 (DIC_Prag
: Node_Id
;
1291 Par_Typ
: Entity_Id
;
1292 Deriv_Typ
: Entity_Id
;
1293 Stmts
: in out List_Id
);
1294 -- Add a runtime check to verify the assertion expression of inherited
1295 -- pragma DIC_Prag. Par_Typ is parent type, which is also the owner of
1296 -- the DIC pragma. Deriv_Typ is the derived type inheriting the DIC
1297 -- pragma. All generated code is added to list Stmts.
1299 procedure Add_Inherited_Tagged_DIC
1300 (DIC_Prag
: Node_Id
;
1301 Par_Typ
: Entity_Id
;
1302 Deriv_Typ
: Entity_Id
;
1303 Stmts
: in out List_Id
);
1304 -- Add a runtime check to verify assertion expression DIC_Expr of
1305 -- inherited pragma DIC_Prag. This routine applies class-wide pre- and
1306 -- postcondition-like runtime semantics to the check. Par_Typ is the
1307 -- parent type whose DIC pragma is being inherited. Deriv_Typ is the
1308 -- derived type inheriting the DIC pragma. All generated code is added
1311 procedure Add_Own_DIC
1312 (DIC_Prag
: Node_Id
;
1313 DIC_Typ
: Entity_Id
;
1314 Stmts
: in out List_Id
);
1315 -- Add a runtime check to verify the assertion expression of pragma
1316 -- DIC_Prag. DIC_Typ is the owner of the DIC pragma. All generated code
1317 -- is added to list Stmts.
1323 procedure Add_DIC_Check
1324 (DIC_Prag
: Node_Id
;
1326 Stmts
: in out List_Id
)
1328 Loc
: constant Source_Ptr
:= Sloc
(DIC_Prag
);
1329 Nam
: constant Name_Id
:= Original_Aspect_Pragma_Name
(DIC_Prag
);
1332 -- The DIC pragma is ignored, nothing left to do
1334 if Is_Ignored
(DIC_Prag
) then
1337 -- Otherwise the DIC expression must be checked at run time.
1340 -- pragma Check (<Nam>, <DIC_Expr>);
1343 Append_New_To
(Stmts
,
1345 Pragma_Identifier
=>
1346 Make_Identifier
(Loc
, Name_Check
),
1348 Pragma_Argument_Associations
=> New_List
(
1349 Make_Pragma_Argument_Association
(Loc
,
1350 Expression
=> Make_Identifier
(Loc
, Nam
)),
1352 Make_Pragma_Argument_Association
(Loc
,
1353 Expression
=> DIC_Expr
))));
1357 -----------------------
1358 -- Add_Inherited_DIC --
1359 -----------------------
1361 procedure Add_Inherited_DIC
1362 (DIC_Prag
: Node_Id
;
1363 Par_Typ
: Entity_Id
;
1364 Deriv_Typ
: Entity_Id
;
1365 Stmts
: in out List_Id
)
1367 Deriv_Proc
: constant Entity_Id
:= DIC_Procedure
(Deriv_Typ
);
1368 Deriv_Obj
: constant Entity_Id
:= First_Entity
(Deriv_Proc
);
1369 Par_Proc
: constant Entity_Id
:= DIC_Procedure
(Par_Typ
);
1370 Par_Obj
: constant Entity_Id
:= First_Entity
(Par_Proc
);
1371 Loc
: constant Source_Ptr
:= Sloc
(DIC_Prag
);
1374 pragma Assert
(Present
(Deriv_Proc
) and then Present
(Par_Proc
));
1376 -- Verify the inherited DIC assertion expression by calling the DIC
1377 -- procedure of the parent type.
1380 -- <Par_Typ>DIC (Par_Typ (_object));
1382 Append_New_To
(Stmts
,
1383 Make_Procedure_Call_Statement
(Loc
,
1384 Name
=> New_Occurrence_Of
(Par_Proc
, Loc
),
1385 Parameter_Associations
=> New_List
(
1387 (Typ
=> Etype
(Par_Obj
),
1388 Expr
=> New_Occurrence_Of
(Deriv_Obj
, Loc
)))));
1389 end Add_Inherited_DIC
;
1391 ------------------------------
1392 -- Add_Inherited_Tagged_DIC --
1393 ------------------------------
1395 procedure Add_Inherited_Tagged_DIC
1396 (DIC_Prag
: Node_Id
;
1397 Par_Typ
: Entity_Id
;
1398 Deriv_Typ
: Entity_Id
;
1399 Stmts
: in out List_Id
)
1401 Deriv_Proc
: constant Entity_Id
:= DIC_Procedure
(Deriv_Typ
);
1402 DIC_Args
: constant List_Id
:=
1403 Pragma_Argument_Associations
(DIC_Prag
);
1404 DIC_Arg
: constant Node_Id
:= First
(DIC_Args
);
1405 DIC_Expr
: constant Node_Id
:= Expression_Copy
(DIC_Arg
);
1406 Par_Proc
: constant Entity_Id
:= DIC_Procedure
(Par_Typ
);
1411 -- The processing of an inherited DIC assertion expression starts off
1412 -- with a copy of the original parent expression where all references
1413 -- to the parent type have already been replaced with references to
1414 -- the _object formal parameter of the parent type's DIC procedure.
1416 pragma Assert
(Present
(DIC_Expr
));
1417 Expr
:= New_Copy_Tree
(DIC_Expr
);
1419 -- Perform the following substitutions:
1421 -- * Replace a reference to the _object parameter of the parent
1422 -- type's DIC procedure with a reference to the _object parameter
1423 -- of the derived types' DIC procedure.
1425 -- * Replace a reference to a discriminant of the parent type with
1426 -- a suitable value from the point of view of the derived type.
1428 -- * Replace a call to an overridden parent primitive with a call
1429 -- to the overriding derived type primitive.
1431 -- * Replace a call to an inherited parent primitive with a call to
1432 -- the internally-generated inherited derived type primitive.
1434 -- Note that primitives defined in the private part are automatically
1435 -- handled by the overriding/inheritance mechanism and do not require
1436 -- an extra replacement pass.
1438 pragma Assert
(Present
(Deriv_Proc
) and then Present
(Par_Proc
));
1443 Deriv_Typ
=> Deriv_Typ
,
1444 Par_Obj
=> First_Formal
(Par_Proc
),
1445 Deriv_Obj
=> First_Formal
(Deriv_Proc
));
1447 -- Once the DIC assertion expression is fully processed, add a check
1448 -- to the statements of the DIC procedure.
1451 (DIC_Prag
=> DIC_Prag
,
1454 end Add_Inherited_Tagged_DIC
;
1460 procedure Add_Own_DIC
1461 (DIC_Prag
: Node_Id
;
1462 DIC_Typ
: Entity_Id
;
1463 Stmts
: in out List_Id
)
1465 DIC_Args
: constant List_Id
:=
1466 Pragma_Argument_Associations
(DIC_Prag
);
1467 DIC_Arg
: constant Node_Id
:= First
(DIC_Args
);
1468 DIC_Asp
: constant Node_Id
:= Corresponding_Aspect
(DIC_Prag
);
1469 DIC_Expr
: constant Node_Id
:= Get_Pragma_Arg
(DIC_Arg
);
1470 DIC_Proc
: constant Entity_Id
:= DIC_Procedure
(DIC_Typ
);
1471 Obj_Id
: constant Entity_Id
:= First_Formal
(DIC_Proc
);
1473 procedure Preanalyze_Own_DIC_For_ASIS
;
1474 -- Preanalyze the original DIC expression of an aspect or a source
1477 ---------------------------------
1478 -- Preanalyze_Own_DIC_For_ASIS --
1479 ---------------------------------
1481 procedure Preanalyze_Own_DIC_For_ASIS
is
1482 Expr
: Node_Id
:= Empty
;
1485 -- The DIC pragma is a source construct, preanalyze the original
1486 -- expression of the pragma.
1488 if Comes_From_Source
(DIC_Prag
) then
1491 -- Otherwise preanalyze the expression of the corresponding aspect
1493 elsif Present
(DIC_Asp
) then
1494 Expr
:= Expression
(DIC_Asp
);
1497 -- The expression must be subjected to the same substitutions as
1498 -- the copy used in the generation of the runtime check.
1500 if Present
(Expr
) then
1501 Replace_Type_References
1506 Preanalyze_Assert_Expression
(Expr
, Any_Boolean
);
1508 end Preanalyze_Own_DIC_For_ASIS
;
1512 Typ_Decl
: constant Node_Id
:= Declaration_Node
(DIC_Typ
);
1516 -- Start of processing for Add_Own_DIC
1519 pragma Assert
(Present
(DIC_Expr
));
1520 Expr
:= New_Copy_Tree
(DIC_Expr
);
1522 -- Perform the following substitution:
1524 -- * Replace the current instance of DIC_Typ with a reference to
1525 -- the _object formal parameter of the DIC procedure.
1527 Replace_Type_References
1532 -- Preanalyze the DIC expression to detect errors and at the same
1533 -- time capture the visibility of the proper package part.
1535 Set_Parent
(Expr
, Typ_Decl
);
1536 Preanalyze_Assert_Expression
(Expr
, Any_Boolean
);
1538 -- Save a copy of the expression with all replacements and analysis
1539 -- already taken place in case a derived type inherits the pragma.
1540 -- The copy will be used as the foundation of the derived type's own
1541 -- version of the DIC assertion expression.
1543 if Is_Tagged_Type
(DIC_Typ
) then
1544 Set_Expression_Copy
(DIC_Arg
, New_Copy_Tree
(Expr
));
1547 -- If the pragma comes from an aspect specification, replace the
1548 -- saved expression because all type references must be substituted
1549 -- for the call to Preanalyze_Spec_Expression in Check_Aspect_At_xxx
1552 if Present
(DIC_Asp
) then
1553 Set_Entity
(Identifier
(DIC_Asp
), New_Copy_Tree
(Expr
));
1556 -- Preanalyze the original DIC expression for ASIS
1559 Preanalyze_Own_DIC_For_ASIS
;
1562 -- Once the DIC assertion expression is fully processed, add a check
1563 -- to the statements of the DIC procedure.
1566 (DIC_Prag
=> DIC_Prag
,
1573 Loc
: constant Source_Ptr
:= Sloc
(Typ
);
1575 Saved_GM
: constant Ghost_Mode_Type
:= Ghost_Mode
;
1576 Saved_IGR
: constant Node_Id
:= Ignored_Ghost_Region
;
1577 -- Save the Ghost-related attributes to restore on exit
1580 DIC_Typ
: Entity_Id
;
1581 Dummy_1
: Entity_Id
;
1582 Dummy_2
: Entity_Id
;
1583 Proc_Body
: Node_Id
;
1584 Proc_Body_Id
: Entity_Id
;
1585 Proc_Decl
: Node_Id
;
1586 Proc_Id
: Entity_Id
;
1587 Stmts
: List_Id
:= No_List
;
1589 Build_Body
: Boolean := False;
1590 -- Flag set when the type requires a DIC procedure body to be built
1592 Work_Typ
: Entity_Id
;
1595 -- Start of processing for Build_DIC_Procedure_Body
1598 Work_Typ
:= Base_Type
(Typ
);
1600 -- Do not process class-wide types as these are Itypes, but lack a first
1601 -- subtype (see below).
1603 if Is_Class_Wide_Type
(Work_Typ
) then
1606 -- Do not process the underlying full view of a private type. There is
1607 -- no way to get back to the partial view, plus the body will be built
1608 -- by the full view or the base type.
1610 elsif Is_Underlying_Full_View
(Work_Typ
) then
1613 -- Use the first subtype when dealing with various base types
1615 elsif Is_Itype
(Work_Typ
) then
1616 Work_Typ
:= First_Subtype
(Work_Typ
);
1618 -- The input denotes the corresponding record type of a protected or a
1619 -- task type. Work with the concurrent type because the corresponding
1620 -- record type may not be visible to clients of the type.
1622 elsif Ekind
(Work_Typ
) = E_Record_Type
1623 and then Is_Concurrent_Record_Type
(Work_Typ
)
1625 Work_Typ
:= Corresponding_Concurrent_Type
(Work_Typ
);
1628 -- The working type may be subject to pragma Ghost. Set the mode now to
1629 -- ensure that the DIC procedure is properly marked as Ghost.
1631 Set_Ghost_Mode
(Work_Typ
);
1633 -- The working type must be either define a DIC pragma of its own or
1634 -- inherit one from a parent type.
1636 pragma Assert
(Has_DIC
(Work_Typ
));
1638 -- Recover the type which defines the DIC pragma. This is either the
1639 -- working type itself or a parent type when the pragma is inherited.
1641 DIC_Typ
:= Find_DIC_Type
(Work_Typ
);
1642 pragma Assert
(Present
(DIC_Typ
));
1644 DIC_Prag
:= Get_Pragma
(DIC_Typ
, Pragma_Default_Initial_Condition
);
1645 pragma Assert
(Present
(DIC_Prag
));
1647 -- Nothing to do if pragma DIC appears without an argument or its sole
1648 -- argument is "null".
1650 if not Is_Verifiable_DIC_Pragma
(DIC_Prag
) then
1654 -- The working type may lack a DIC procedure declaration. This may be
1655 -- due to several reasons:
1657 -- * The working type's own DIC pragma does not contain a verifiable
1658 -- assertion expression. In this case there is no need to build a
1659 -- DIC procedure because there is nothing to check.
1661 -- * The working type derives from a parent type. In this case a DIC
1662 -- procedure should be built only when the inherited DIC pragma has
1663 -- a verifiable assertion expression.
1665 Proc_Id
:= DIC_Procedure
(Work_Typ
);
1667 -- Build a DIC procedure declaration when the working type derives from
1670 if No
(Proc_Id
) then
1671 Build_DIC_Procedure_Declaration
(Work_Typ
);
1672 Proc_Id
:= DIC_Procedure
(Work_Typ
);
1675 -- At this point there should be a DIC procedure declaration
1677 pragma Assert
(Present
(Proc_Id
));
1678 Proc_Decl
:= Unit_Declaration_Node
(Proc_Id
);
1680 -- Nothing to do if the DIC procedure already has a body
1682 if Present
(Corresponding_Body
(Proc_Decl
)) then
1686 -- Emulate the environment of the DIC procedure by installing its scope
1687 -- and formal parameters.
1689 Push_Scope
(Proc_Id
);
1690 Install_Formals
(Proc_Id
);
1692 -- The working type defines its own DIC pragma. Replace the current
1693 -- instance of the working type with the formal of the DIC procedure.
1694 -- Note that there is no need to consider inherited DIC pragmas from
1695 -- parent types because the working type's DIC pragma "hides" all
1696 -- inherited DIC pragmas.
1698 if Has_Own_DIC
(Work_Typ
) then
1699 pragma Assert
(DIC_Typ
= Work_Typ
);
1702 (DIC_Prag
=> DIC_Prag
,
1708 -- Otherwise the working type inherits a DIC pragma from a parent type.
1709 -- This processing is carried out when the type is frozen because the
1710 -- state of all parent discriminants is known at that point. Note that
1711 -- it is semantically sound to delay the creation of the DIC procedure
1712 -- body till the freeze point. If the type has a DIC pragma of its own,
1713 -- then the DIC procedure body would have already been constructed at
1714 -- the end of the visible declarations and all parent DIC pragmas are
1715 -- effectively "hidden" and irrelevant.
1717 elsif For_Freeze
then
1718 pragma Assert
(Has_Inherited_DIC
(Work_Typ
));
1719 pragma Assert
(DIC_Typ
/= Work_Typ
);
1721 -- The working type is tagged. The verification of the assertion
1722 -- expression is subject to the same semantics as class-wide pre-
1723 -- and postconditions.
1725 if Is_Tagged_Type
(Work_Typ
) then
1726 Add_Inherited_Tagged_DIC
1727 (DIC_Prag
=> DIC_Prag
,
1729 Deriv_Typ
=> Work_Typ
,
1732 -- Otherwise the working type is not tagged. Verify the assertion
1733 -- expression of the inherited DIC pragma by directly calling the
1734 -- DIC procedure of the parent type.
1738 (DIC_Prag
=> DIC_Prag
,
1740 Deriv_Typ
=> Work_Typ
,
1751 -- Produce an empty completing body in the following cases:
1752 -- * Assertions are disabled
1753 -- * The DIC Assertion_Policy is Ignore
1756 Stmts
:= New_List
(Make_Null_Statement
(Loc
));
1760 -- procedure <Work_Typ>DIC (_object : <Work_Typ>) is
1763 -- end <Work_Typ>DIC;
1766 Make_Subprogram_Body
(Loc
,
1768 Copy_Subprogram_Spec
(Parent
(Proc_Id
)),
1769 Declarations
=> Empty_List
,
1770 Handled_Statement_Sequence
=>
1771 Make_Handled_Sequence_Of_Statements
(Loc
,
1772 Statements
=> Stmts
));
1773 Proc_Body_Id
:= Defining_Entity
(Proc_Body
);
1775 -- Perform minor decoration in case the body is not analyzed
1777 Set_Ekind
(Proc_Body_Id
, E_Subprogram_Body
);
1778 Set_Etype
(Proc_Body_Id
, Standard_Void_Type
);
1779 Set_Scope
(Proc_Body_Id
, Current_Scope
);
1780 Set_SPARK_Pragma
(Proc_Body_Id
, SPARK_Pragma
(Proc_Id
));
1781 Set_SPARK_Pragma_Inherited
1782 (Proc_Body_Id
, SPARK_Pragma_Inherited
(Proc_Id
));
1784 -- Link both spec and body to avoid generating duplicates
1786 Set_Corresponding_Body
(Proc_Decl
, Proc_Body_Id
);
1787 Set_Corresponding_Spec
(Proc_Body
, Proc_Id
);
1789 -- The body should not be inserted into the tree when the context
1790 -- is ASIS or a generic unit because it is not part of the template.
1791 -- Note that the body must still be generated in order to resolve the
1792 -- DIC assertion expression.
1794 if ASIS_Mode
or Inside_A_Generic
then
1797 -- Semi-insert the body into the tree for GNATprove by setting its
1798 -- Parent field. This allows for proper upstream tree traversals.
1800 elsif GNATprove_Mode
then
1801 Set_Parent
(Proc_Body
, Parent
(Declaration_Node
(Work_Typ
)));
1803 -- Otherwise the body is part of the freezing actions of the working
1807 Append_Freeze_Action
(Work_Typ
, Proc_Body
);
1812 Restore_Ghost_Region
(Saved_GM
, Saved_IGR
);
1813 end Build_DIC_Procedure_Body
;
1815 -------------------------------------
1816 -- Build_DIC_Procedure_Declaration --
1817 -------------------------------------
1819 -- WARNING: This routine manages Ghost regions. Return statements must be
1820 -- replaced by gotos which jump to the end of the routine and restore the
1823 procedure Build_DIC_Procedure_Declaration
(Typ
: Entity_Id
) is
1824 Loc
: constant Source_Ptr
:= Sloc
(Typ
);
1826 Saved_GM
: constant Ghost_Mode_Type
:= Ghost_Mode
;
1827 Saved_IGR
: constant Node_Id
:= Ignored_Ghost_Region
;
1828 -- Save the Ghost-related attributes to restore on exit
1831 DIC_Typ
: Entity_Id
;
1832 Proc_Decl
: Node_Id
;
1833 Proc_Id
: Entity_Id
;
1836 CRec_Typ
: Entity_Id
;
1837 -- The corresponding record type of Full_Typ
1839 Full_Base
: Entity_Id
;
1840 -- The base type of Full_Typ
1842 Full_Typ
: Entity_Id
;
1843 -- The full view of working type
1846 -- The _object formal parameter of the DIC procedure
1848 Priv_Typ
: Entity_Id
;
1849 -- The partial view of working type
1851 Work_Typ
: Entity_Id
;
1855 Work_Typ
:= Base_Type
(Typ
);
1857 -- Do not process class-wide types as these are Itypes, but lack a first
1858 -- subtype (see below).
1860 if Is_Class_Wide_Type
(Work_Typ
) then
1863 -- Do not process the underlying full view of a private type. There is
1864 -- no way to get back to the partial view, plus the body will be built
1865 -- by the full view or the base type.
1867 elsif Is_Underlying_Full_View
(Work_Typ
) then
1870 -- Use the first subtype when dealing with various base types
1872 elsif Is_Itype
(Work_Typ
) then
1873 Work_Typ
:= First_Subtype
(Work_Typ
);
1875 -- The input denotes the corresponding record type of a protected or a
1876 -- task type. Work with the concurrent type because the corresponding
1877 -- record type may not be visible to clients of the type.
1879 elsif Ekind
(Work_Typ
) = E_Record_Type
1880 and then Is_Concurrent_Record_Type
(Work_Typ
)
1882 Work_Typ
:= Corresponding_Concurrent_Type
(Work_Typ
);
1885 -- The working type may be subject to pragma Ghost. Set the mode now to
1886 -- ensure that the DIC procedure is properly marked as Ghost.
1888 Set_Ghost_Mode
(Work_Typ
);
1890 -- The type must be either subject to a DIC pragma or inherit one from a
1893 pragma Assert
(Has_DIC
(Work_Typ
));
1895 -- Recover the type which defines the DIC pragma. This is either the
1896 -- working type itself or a parent type when the pragma is inherited.
1898 DIC_Typ
:= Find_DIC_Type
(Work_Typ
);
1899 pragma Assert
(Present
(DIC_Typ
));
1901 DIC_Prag
:= Get_Pragma
(DIC_Typ
, Pragma_Default_Initial_Condition
);
1902 pragma Assert
(Present
(DIC_Prag
));
1904 -- Nothing to do if pragma DIC appears without an argument or its sole
1905 -- argument is "null".
1907 if not Is_Verifiable_DIC_Pragma
(DIC_Prag
) then
1910 -- Nothing to do if the type already has a DIC procedure
1912 elsif Present
(DIC_Procedure
(Work_Typ
)) then
1917 Make_Defining_Identifier
(Loc
,
1919 New_External_Name
(Chars
(Work_Typ
), "Default_Initial_Condition"));
1921 -- Perform minor decoration in case the declaration is not analyzed
1923 Set_Ekind
(Proc_Id
, E_Procedure
);
1924 Set_Etype
(Proc_Id
, Standard_Void_Type
);
1925 Set_Is_DIC_Procedure
(Proc_Id
);
1926 Set_Scope
(Proc_Id
, Current_Scope
);
1927 Set_SPARK_Pragma
(Proc_Id
, SPARK_Mode_Pragma
);
1928 Set_SPARK_Pragma_Inherited
(Proc_Id
);
1930 Set_DIC_Procedure
(Work_Typ
, Proc_Id
);
1932 -- The DIC procedure requires debug info when the assertion expression
1933 -- is subject to Source Coverage Obligations.
1935 if Generate_SCO
then
1936 Set_Needs_Debug_Info
(Proc_Id
);
1939 -- Obtain all views of the input type
1941 Get_Views
(Work_Typ
, Priv_Typ
, Full_Typ
, Full_Base
, CRec_Typ
);
1943 -- Associate the DIC procedure and various relevant flags with all views
1945 Propagate_DIC_Attributes
(Priv_Typ
, From_Typ
=> Work_Typ
);
1946 Propagate_DIC_Attributes
(Full_Typ
, From_Typ
=> Work_Typ
);
1947 Propagate_DIC_Attributes
(Full_Base
, From_Typ
=> Work_Typ
);
1948 Propagate_DIC_Attributes
(CRec_Typ
, From_Typ
=> Work_Typ
);
1950 -- The declaration of the DIC procedure must be inserted after the
1951 -- declaration of the partial view as this allows for proper external
1954 if Present
(Priv_Typ
) then
1955 Typ_Decl
:= Declaration_Node
(Priv_Typ
);
1957 -- Derived types with the full view as parent do not have a partial
1958 -- view. Insert the DIC procedure after the derived type.
1961 Typ_Decl
:= Declaration_Node
(Full_Typ
);
1964 -- The type should have a declarative node
1966 pragma Assert
(Present
(Typ_Decl
));
1968 -- Create the formal parameter which emulates the variable-like behavior
1969 -- of the type's current instance.
1971 Obj_Id
:= Make_Defining_Identifier
(Loc
, Chars
=> Name_uObject
);
1973 -- Perform minor decoration in case the declaration is not analyzed
1975 Set_Ekind
(Obj_Id
, E_In_Parameter
);
1976 Set_Etype
(Obj_Id
, Work_Typ
);
1977 Set_Scope
(Obj_Id
, Proc_Id
);
1979 Set_First_Entity
(Proc_Id
, Obj_Id
);
1982 -- procedure <Work_Typ>DIC (_object : <Work_Typ>);
1985 Make_Subprogram_Declaration
(Loc
,
1987 Make_Procedure_Specification
(Loc
,
1988 Defining_Unit_Name
=> Proc_Id
,
1989 Parameter_Specifications
=> New_List
(
1990 Make_Parameter_Specification
(Loc
,
1991 Defining_Identifier
=> Obj_Id
,
1993 New_Occurrence_Of
(Work_Typ
, Loc
)))));
1995 -- The declaration should not be inserted into the tree when the context
1996 -- is ASIS or a generic unit because it is not part of the template.
1998 if ASIS_Mode
or Inside_A_Generic
then
2001 -- Semi-insert the declaration into the tree for GNATprove by setting
2002 -- its Parent field. This allows for proper upstream tree traversals.
2004 elsif GNATprove_Mode
then
2005 Set_Parent
(Proc_Decl
, Parent
(Typ_Decl
));
2007 -- Otherwise insert the declaration
2010 Insert_After_And_Analyze
(Typ_Decl
, Proc_Decl
);
2014 Restore_Ghost_Region
(Saved_GM
, Saved_IGR
);
2015 end Build_DIC_Procedure_Declaration
;
2017 ------------------------------------
2018 -- Build_Invariant_Procedure_Body --
2019 ------------------------------------
2021 -- WARNING: This routine manages Ghost regions. Return statements must be
2022 -- replaced by gotos which jump to the end of the routine and restore the
2025 procedure Build_Invariant_Procedure_Body
2027 Partial_Invariant
: Boolean := False)
2029 Loc
: constant Source_Ptr
:= Sloc
(Typ
);
2031 Pragmas_Seen
: Elist_Id
:= No_Elist
;
2032 -- This list contains all invariant pragmas processed so far. The list
2033 -- is used to avoid generating redundant invariant checks.
2035 Produced_Check
: Boolean := False;
2036 -- This flag tracks whether the type has produced at least one invariant
2037 -- check. The flag is used as a sanity check at the end of the routine.
2039 -- NOTE: most of the routines in Build_Invariant_Procedure_Body are
2040 -- intentionally unnested to avoid deep indentation of code.
2042 -- NOTE: all Add_xxx_Invariants routines are reactive. In other words
2043 -- they emit checks, loops (for arrays) and case statements (for record
2044 -- variant parts) only when there are invariants to verify. This keeps
2045 -- the body of the invariant procedure free of useless code.
2047 procedure Add_Array_Component_Invariants
2050 Checks
: in out List_Id
);
2051 -- Generate an invariant check for each component of array type T.
2052 -- Obj_Id denotes the entity of the _object formal parameter of the
2053 -- invariant procedure. All created checks are added to list Checks.
2055 procedure Add_Inherited_Invariants
2057 Priv_Typ
: Entity_Id
;
2058 Full_Typ
: Entity_Id
;
2060 Checks
: in out List_Id
);
2061 -- Generate an invariant check for each inherited class-wide invariant
2062 -- coming from all parent types of type T. Priv_Typ and Full_Typ denote
2063 -- the partial and full view of the parent type. Obj_Id denotes the
2064 -- entity of the _object formal parameter of the invariant procedure.
2065 -- All created checks are added to list Checks.
2067 procedure Add_Interface_Invariants
2070 Checks
: in out List_Id
);
2071 -- Generate an invariant check for each inherited class-wide invariant
2072 -- coming from all interfaces implemented by type T. Obj_Id denotes the
2073 -- entity of the _object formal parameter of the invariant procedure.
2074 -- All created checks are added to list Checks.
2076 procedure Add_Invariant_Check
2079 Checks
: in out List_Id
;
2080 Inherited
: Boolean := False);
2081 -- Subsidiary to all Add_xxx_Invariant routines. Add a runtime check to
2082 -- verify assertion expression Expr of pragma Prag. All generated code
2083 -- is added to list Checks. Flag Inherited should be set when the pragma
2084 -- is inherited from a parent or interface type.
2086 procedure Add_Own_Invariants
2089 Checks
: in out List_Id
;
2090 Priv_Item
: Node_Id
:= Empty
);
2091 -- Generate an invariant check for each invariant found for type T.
2092 -- Obj_Id denotes the entity of the _object formal parameter of the
2093 -- invariant procedure. All created checks are added to list Checks.
2094 -- Priv_Item denotes the first rep item of the private type.
2096 procedure Add_Parent_Invariants
2099 Checks
: in out List_Id
);
2100 -- Generate an invariant check for each inherited class-wide invariant
2101 -- coming from all parent types of type T. Obj_Id denotes the entity of
2102 -- the _object formal parameter of the invariant procedure. All created
2103 -- checks are added to list Checks.
2105 procedure Add_Record_Component_Invariants
2108 Checks
: in out List_Id
);
2109 -- Generate an invariant check for each component of record type T.
2110 -- Obj_Id denotes the entity of the _object formal parameter of the
2111 -- invariant procedure. All created checks are added to list Checks.
2113 ------------------------------------
2114 -- Add_Array_Component_Invariants --
2115 ------------------------------------
2117 procedure Add_Array_Component_Invariants
2120 Checks
: in out List_Id
)
2122 Comp_Typ
: constant Entity_Id
:= Component_Type
(T
);
2123 Dims
: constant Pos
:= Number_Dimensions
(T
);
2125 procedure Process_Array_Component
2127 Comp_Checks
: in out List_Id
);
2128 -- Generate an invariant check for an array component identified by
2129 -- the indices in list Indices. All created checks are added to list
2132 procedure Process_One_Dimension
2135 Dim_Checks
: in out List_Id
);
2136 -- Generate a loop over the Nth dimension Dim of an array type. List
2137 -- Indices contains all array indices for the dimension. All created
2138 -- checks are added to list Dim_Checks.
2140 -----------------------------
2141 -- Process_Array_Component --
2142 -----------------------------
2144 procedure Process_Array_Component
2146 Comp_Checks
: in out List_Id
)
2148 Proc_Id
: Entity_Id
;
2151 if Has_Invariants
(Comp_Typ
) then
2153 -- In GNATprove mode, the component invariants are checked by
2154 -- other means. They should not be added to the array type
2155 -- invariant procedure, so that the procedure can be used to
2156 -- check the array type invariants if any.
2158 if GNATprove_Mode
then
2162 Proc_Id
:= Invariant_Procedure
(Base_Type
(Comp_Typ
));
2164 -- The component type should have an invariant procedure
2165 -- if it has invariants of its own or inherits class-wide
2166 -- invariants from parent or interface types.
2168 pragma Assert
(Present
(Proc_Id
));
2171 -- <Comp_Typ>Invariant (_object (<Indices>));
2173 -- Note that the invariant procedure may have a null body if
2174 -- assertions are disabled or Assertion_Policy Ignore is in
2177 if not Has_Null_Body
(Proc_Id
) then
2178 Append_New_To
(Comp_Checks
,
2179 Make_Procedure_Call_Statement
(Loc
,
2181 New_Occurrence_Of
(Proc_Id
, Loc
),
2182 Parameter_Associations
=> New_List
(
2183 Make_Indexed_Component
(Loc
,
2184 Prefix
=> New_Occurrence_Of
(Obj_Id
, Loc
),
2185 Expressions
=> New_Copy_List
(Indices
)))));
2189 Produced_Check
:= True;
2191 end Process_Array_Component
;
2193 ---------------------------
2194 -- Process_One_Dimension --
2195 ---------------------------
2197 procedure Process_One_Dimension
2200 Dim_Checks
: in out List_Id
)
2202 Comp_Checks
: List_Id
:= No_List
;
2206 -- Generate the invariant checks for the array component after all
2207 -- dimensions have produced their respective loops.
2210 Process_Array_Component
2211 (Indices
=> Indices
,
2212 Comp_Checks
=> Dim_Checks
);
2214 -- Otherwise create a loop for the current dimension
2217 -- Create a new loop variable for each dimension
2220 Make_Defining_Identifier
(Loc
,
2221 Chars
=> New_External_Name
('I', Dim
));
2222 Append_To
(Indices
, New_Occurrence_Of
(Index
, Loc
));
2224 Process_One_Dimension
2227 Dim_Checks
=> Comp_Checks
);
2230 -- for I<Dim> in _object'Range (<Dim>) loop
2234 -- Note that the invariant procedure may have a null body if
2235 -- assertions are disabled or Assertion_Policy Ignore is in
2238 if Present
(Comp_Checks
) then
2239 Append_New_To
(Dim_Checks
,
2240 Make_Implicit_Loop_Statement
(T
,
2241 Identifier
=> Empty
,
2243 Make_Iteration_Scheme
(Loc
,
2244 Loop_Parameter_Specification
=>
2245 Make_Loop_Parameter_Specification
(Loc
,
2246 Defining_Identifier
=> Index
,
2247 Discrete_Subtype_Definition
=>
2248 Make_Attribute_Reference
(Loc
,
2250 New_Occurrence_Of
(Obj_Id
, Loc
),
2251 Attribute_Name
=> Name_Range
,
2252 Expressions
=> New_List
(
2253 Make_Integer_Literal
(Loc
, Dim
))))),
2254 Statements
=> Comp_Checks
));
2257 end Process_One_Dimension
;
2259 -- Start of processing for Add_Array_Component_Invariants
2262 Process_One_Dimension
2264 Indices
=> New_List
,
2265 Dim_Checks
=> Checks
);
2266 end Add_Array_Component_Invariants
;
2268 ------------------------------
2269 -- Add_Inherited_Invariants --
2270 ------------------------------
2272 procedure Add_Inherited_Invariants
2274 Priv_Typ
: Entity_Id
;
2275 Full_Typ
: Entity_Id
;
2277 Checks
: in out List_Id
)
2279 Deriv_Typ
: Entity_Id
;
2282 Prag_Expr
: Node_Id
;
2283 Prag_Expr_Arg
: Node_Id
;
2285 Prag_Typ_Arg
: Node_Id
;
2287 Par_Proc
: Entity_Id
;
2288 -- The "partial" invariant procedure of Par_Typ
2290 Par_Typ
: Entity_Id
;
2291 -- The suitable view of the parent type used in the substitution of
2295 if not Present
(Priv_Typ
) and then not Present
(Full_Typ
) then
2299 -- When the type inheriting the class-wide invariant is a concurrent
2300 -- type, use the corresponding record type because it contains all
2301 -- primitive operations of the concurrent type and allows for proper
2304 if Is_Concurrent_Type
(T
) then
2305 Deriv_Typ
:= Corresponding_Record_Type
(T
);
2310 pragma Assert
(Present
(Deriv_Typ
));
2312 -- Determine which rep item chain to use. Precedence is given to that
2313 -- of the parent type's partial view since it usually carries all the
2314 -- class-wide invariants.
2316 if Present
(Priv_Typ
) then
2317 Prag
:= First_Rep_Item
(Priv_Typ
);
2319 Prag
:= First_Rep_Item
(Full_Typ
);
2322 while Present
(Prag
) loop
2323 if Nkind
(Prag
) = N_Pragma
2324 and then Pragma_Name
(Prag
) = Name_Invariant
2326 -- Nothing to do if the pragma was already processed
2328 if Contains
(Pragmas_Seen
, Prag
) then
2331 -- Nothing to do when the caller requests the processing of all
2332 -- inherited class-wide invariants, but the pragma does not
2333 -- fall in this category.
2335 elsif not Class_Present
(Prag
) then
2339 -- Extract the arguments of the invariant pragma
2341 Prag_Typ_Arg
:= First
(Pragma_Argument_Associations
(Prag
));
2342 Prag_Expr_Arg
:= Next
(Prag_Typ_Arg
);
2343 Prag_Expr
:= Expression_Copy
(Prag_Expr_Arg
);
2344 Prag_Typ
:= Get_Pragma_Arg
(Prag_Typ_Arg
);
2346 -- The pragma applies to the partial view of the parent type
2348 if Present
(Priv_Typ
)
2349 and then Entity
(Prag_Typ
) = Priv_Typ
2351 Par_Typ
:= Priv_Typ
;
2353 -- The pragma applies to the full view of the parent type
2355 elsif Present
(Full_Typ
)
2356 and then Entity
(Prag_Typ
) = Full_Typ
2358 Par_Typ
:= Full_Typ
;
2360 -- Otherwise the pragma does not belong to the parent type and
2361 -- should not be considered.
2367 -- Perform the following substitutions:
2369 -- * Replace a reference to the _object parameter of the
2370 -- parent type's partial invariant procedure with a
2371 -- reference to the _object parameter of the derived
2372 -- type's full invariant procedure.
2374 -- * Replace a reference to a discriminant of the parent type
2375 -- with a suitable value from the point of view of the
2378 -- * Replace a call to an overridden parent primitive with a
2379 -- call to the overriding derived type primitive.
2381 -- * Replace a call to an inherited parent primitive with a
2382 -- call to the internally-generated inherited derived type
2385 Expr
:= New_Copy_Tree
(Prag_Expr
);
2387 -- The parent type must have a "partial" invariant procedure
2388 -- because class-wide invariants are captured exclusively by
2391 Par_Proc
:= Partial_Invariant_Procedure
(Par_Typ
);
2392 pragma Assert
(Present
(Par_Proc
));
2397 Deriv_Typ
=> Deriv_Typ
,
2398 Par_Obj
=> First_Formal
(Par_Proc
),
2399 Deriv_Obj
=> Obj_Id
);
2401 Add_Invariant_Check
(Prag
, Expr
, Checks
, Inherited
=> True);
2404 Next_Rep_Item
(Prag
);
2406 end Add_Inherited_Invariants
;
2408 ------------------------------
2409 -- Add_Interface_Invariants --
2410 ------------------------------
2412 procedure Add_Interface_Invariants
2415 Checks
: in out List_Id
)
2417 Iface_Elmt
: Elmt_Id
;
2421 -- Generate an invariant check for each class-wide invariant coming
2422 -- from all interfaces implemented by type T.
2424 if Is_Tagged_Type
(T
) then
2425 Collect_Interfaces
(T
, Ifaces
);
2427 -- Process the class-wide invariants of all implemented interfaces
2429 Iface_Elmt
:= First_Elmt
(Ifaces
);
2430 while Present
(Iface_Elmt
) loop
2432 -- The Full_Typ parameter is intentionally left Empty because
2433 -- interfaces are treated as the partial view of a private type
2434 -- in order to achieve uniformity with the general case.
2436 Add_Inherited_Invariants
2438 Priv_Typ
=> Node
(Iface_Elmt
),
2443 Next_Elmt
(Iface_Elmt
);
2446 end Add_Interface_Invariants
;
2448 -------------------------
2449 -- Add_Invariant_Check --
2450 -------------------------
2452 procedure Add_Invariant_Check
2455 Checks
: in out List_Id
;
2456 Inherited
: Boolean := False)
2458 Args
: constant List_Id
:= Pragma_Argument_Associations
(Prag
);
2459 Nam
: constant Name_Id
:= Original_Aspect_Pragma_Name
(Prag
);
2460 Ploc
: constant Source_Ptr
:= Sloc
(Prag
);
2461 Str_Arg
: constant Node_Id
:= Next
(Next
(First
(Args
)));
2467 -- The invariant is ignored, nothing left to do
2469 if Is_Ignored
(Prag
) then
2472 -- Otherwise the invariant is checked. Build a pragma Check to verify
2473 -- the expression at run time.
2477 Make_Pragma_Argument_Association
(Ploc
,
2478 Expression
=> Make_Identifier
(Ploc
, Nam
)),
2479 Make_Pragma_Argument_Association
(Ploc
,
2480 Expression
=> Expr
));
2482 -- Handle the String argument (if any)
2484 if Present
(Str_Arg
) then
2485 Str
:= Strval
(Get_Pragma_Arg
(Str_Arg
));
2487 -- When inheriting an invariant, modify the message from
2488 -- "failed invariant" to "failed inherited invariant".
2491 String_To_Name_Buffer
(Str
);
2493 if Name_Buffer
(1 .. 16) = "failed invariant" then
2494 Insert_Str_In_Name_Buffer
("inherited ", 8);
2495 Str
:= String_From_Name_Buffer
;
2500 Make_Pragma_Argument_Association
(Ploc
,
2501 Expression
=> Make_String_Literal
(Ploc
, Str
)));
2505 -- pragma Check (<Nam>, <Expr>, <Str>);
2507 Append_New_To
(Checks
,
2509 Chars
=> Name_Check
,
2510 Pragma_Argument_Associations
=> Assoc
));
2513 -- Output an info message when inheriting an invariant and the
2514 -- listing option is enabled.
2516 if Inherited
and Opt
.List_Inherited_Aspects
then
2517 Error_Msg_Sloc
:= Sloc
(Prag
);
2519 ("info: & inherits `Invariant''Class` aspect from #?L?", Typ
);
2522 -- Add the pragma to the list of processed pragmas
2524 Append_New_Elmt
(Prag
, Pragmas_Seen
);
2525 Produced_Check
:= True;
2526 end Add_Invariant_Check
;
2528 ---------------------------
2529 -- Add_Parent_Invariants --
2530 ---------------------------
2532 procedure Add_Parent_Invariants
2535 Checks
: in out List_Id
)
2537 Dummy_1
: Entity_Id
;
2538 Dummy_2
: Entity_Id
;
2540 Curr_Typ
: Entity_Id
;
2541 -- The entity of the current type being examined
2543 Full_Typ
: Entity_Id
;
2544 -- The full view of Par_Typ
2546 Par_Typ
: Entity_Id
;
2547 -- The entity of the parent type
2549 Priv_Typ
: Entity_Id
;
2550 -- The partial view of Par_Typ
2553 -- Do not process array types because they cannot have true parent
2554 -- types. This also prevents the generation of a duplicate invariant
2555 -- check when the input type is an array base type because its Etype
2556 -- denotes the first subtype, both of which share the same component
2559 if Is_Array_Type
(T
) then
2563 -- Climb the parent type chain
2567 -- Do not consider subtypes as they inherit the invariants
2568 -- from their base types.
2570 Par_Typ
:= Base_Type
(Etype
(Curr_Typ
));
2572 -- Stop the climb once the root of the parent chain is
2575 exit when Curr_Typ
= Par_Typ
;
2577 -- Process the class-wide invariants of the parent type
2579 Get_Views
(Par_Typ
, Priv_Typ
, Full_Typ
, Dummy_1
, Dummy_2
);
2581 -- Process the elements of an array type
2583 if Is_Array_Type
(Full_Typ
) then
2584 Add_Array_Component_Invariants
(Full_Typ
, Obj_Id
, Checks
);
2586 -- Process the components of a record type
2588 elsif Ekind
(Full_Typ
) = E_Record_Type
then
2589 Add_Record_Component_Invariants
(Full_Typ
, Obj_Id
, Checks
);
2592 Add_Inherited_Invariants
2594 Priv_Typ
=> Priv_Typ
,
2595 Full_Typ
=> Full_Typ
,
2599 Curr_Typ
:= Par_Typ
;
2601 end Add_Parent_Invariants
;
2603 ------------------------
2604 -- Add_Own_Invariants --
2605 ------------------------
2607 procedure Add_Own_Invariants
2610 Checks
: in out List_Id
;
2611 Priv_Item
: Node_Id
:= Empty
)
2613 ASIS_Expr
: Node_Id
;
2617 Prag_Expr
: Node_Id
;
2618 Prag_Expr_Arg
: Node_Id
;
2620 Prag_Typ_Arg
: Node_Id
;
2623 if not Present
(T
) then
2627 Prag
:= First_Rep_Item
(T
);
2628 while Present
(Prag
) loop
2629 if Nkind
(Prag
) = N_Pragma
2630 and then Pragma_Name
(Prag
) = Name_Invariant
2632 -- Stop the traversal of the rep item chain once a specific
2633 -- item is encountered.
2635 if Present
(Priv_Item
) and then Prag
= Priv_Item
then
2639 -- Nothing to do if the pragma was already processed
2641 if Contains
(Pragmas_Seen
, Prag
) then
2645 -- Extract the arguments of the invariant pragma
2647 Prag_Typ_Arg
:= First
(Pragma_Argument_Associations
(Prag
));
2648 Prag_Expr_Arg
:= Next
(Prag_Typ_Arg
);
2649 Prag_Expr
:= Get_Pragma_Arg
(Prag_Expr_Arg
);
2650 Prag_Typ
:= Get_Pragma_Arg
(Prag_Typ_Arg
);
2651 Prag_Asp
:= Corresponding_Aspect
(Prag
);
2653 -- Verify the pragma belongs to T, otherwise the pragma applies
2654 -- to a parent type in which case it will be processed later by
2655 -- Add_Parent_Invariants or Add_Interface_Invariants.
2657 if Entity
(Prag_Typ
) /= T
then
2661 Expr
:= New_Copy_Tree
(Prag_Expr
);
2663 -- Substitute all references to type T with references to the
2664 -- _object formal parameter.
2666 Replace_Type_References
(Expr
, T
, Obj_Id
);
2668 -- Preanalyze the invariant expression to detect errors and at
2669 -- the same time capture the visibility of the proper package
2672 Set_Parent
(Expr
, Parent
(Prag_Expr
));
2673 Preanalyze_Assert_Expression
(Expr
, Any_Boolean
);
2675 -- Save a copy of the expression when T is tagged to detect
2676 -- errors and capture the visibility of the proper package part
2677 -- for the generation of inherited type invariants.
2679 if Is_Tagged_Type
(T
) then
2680 Set_Expression_Copy
(Prag_Expr_Arg
, New_Copy_Tree
(Expr
));
2683 -- If the pragma comes from an aspect specification, replace
2684 -- the saved expression because all type references must be
2685 -- substituted for the call to Preanalyze_Spec_Expression in
2686 -- Check_Aspect_At_xxx routines.
2688 if Present
(Prag_Asp
) then
2689 Set_Entity
(Identifier
(Prag_Asp
), New_Copy_Tree
(Expr
));
2692 -- Analyze the original invariant expression for ASIS
2697 if Comes_From_Source
(Prag
) then
2698 ASIS_Expr
:= Prag_Expr
;
2699 elsif Present
(Prag_Asp
) then
2700 ASIS_Expr
:= Expression
(Prag_Asp
);
2703 if Present
(ASIS_Expr
) then
2704 Replace_Type_References
(ASIS_Expr
, T
, Obj_Id
);
2705 Preanalyze_Assert_Expression
(ASIS_Expr
, Any_Boolean
);
2709 Add_Invariant_Check
(Prag
, Expr
, Checks
);
2712 Next_Rep_Item
(Prag
);
2714 end Add_Own_Invariants
;
2716 -------------------------------------
2717 -- Add_Record_Component_Invariants --
2718 -------------------------------------
2720 procedure Add_Record_Component_Invariants
2723 Checks
: in out List_Id
)
2725 procedure Process_Component_List
2726 (Comp_List
: Node_Id
;
2727 CL_Checks
: in out List_Id
);
2728 -- Generate invariant checks for all record components found in
2729 -- component list Comp_List, including variant parts. All created
2730 -- checks are added to list CL_Checks.
2732 procedure Process_Record_Component
2733 (Comp_Id
: Entity_Id
;
2734 Comp_Checks
: in out List_Id
);
2735 -- Generate an invariant check for a record component identified by
2736 -- Comp_Id. All created checks are added to list Comp_Checks.
2738 ----------------------------
2739 -- Process_Component_List --
2740 ----------------------------
2742 procedure Process_Component_List
2743 (Comp_List
: Node_Id
;
2744 CL_Checks
: in out List_Id
)
2748 Var_Alts
: List_Id
:= No_List
;
2749 Var_Checks
: List_Id
:= No_List
;
2750 Var_Stmts
: List_Id
;
2752 Produced_Variant_Check
: Boolean := False;
2753 -- This flag tracks whether the component has produced at least
2754 -- one invariant check.
2757 -- Traverse the component items
2759 Comp
:= First
(Component_Items
(Comp_List
));
2760 while Present
(Comp
) loop
2761 if Nkind
(Comp
) = N_Component_Declaration
then
2763 -- Generate the component invariant check
2765 Process_Record_Component
2766 (Comp_Id
=> Defining_Entity
(Comp
),
2767 Comp_Checks
=> CL_Checks
);
2773 -- Traverse the variant part
2775 if Present
(Variant_Part
(Comp_List
)) then
2776 Var
:= First
(Variants
(Variant_Part
(Comp_List
)));
2777 while Present
(Var
) loop
2778 Var_Checks
:= No_List
;
2780 -- Generate invariant checks for all components and variant
2781 -- parts that qualify.
2783 Process_Component_List
2784 (Comp_List
=> Component_List
(Var
),
2785 CL_Checks
=> Var_Checks
);
2787 -- The components of the current variant produced at least
2788 -- one invariant check.
2790 if Present
(Var_Checks
) then
2791 Var_Stmts
:= Var_Checks
;
2792 Produced_Variant_Check
:= True;
2794 -- Otherwise there are either no components with invariants,
2795 -- assertions are disabled, or Assertion_Policy Ignore is in
2799 Var_Stmts
:= New_List
(Make_Null_Statement
(Loc
));
2802 Append_New_To
(Var_Alts
,
2803 Make_Case_Statement_Alternative
(Loc
,
2805 New_Copy_List
(Discrete_Choices
(Var
)),
2806 Statements
=> Var_Stmts
));
2811 -- Create a case statement which verifies the invariant checks
2812 -- of a particular component list depending on the discriminant
2813 -- values only when there is at least one real invariant check.
2815 if Produced_Variant_Check
then
2816 Append_New_To
(CL_Checks
,
2817 Make_Case_Statement
(Loc
,
2819 Make_Selected_Component
(Loc
,
2820 Prefix
=> New_Occurrence_Of
(Obj_Id
, Loc
),
2823 (Entity
(Name
(Variant_Part
(Comp_List
))), Loc
)),
2824 Alternatives
=> Var_Alts
));
2827 end Process_Component_List
;
2829 ------------------------------
2830 -- Process_Record_Component --
2831 ------------------------------
2833 procedure Process_Record_Component
2834 (Comp_Id
: Entity_Id
;
2835 Comp_Checks
: in out List_Id
)
2837 Comp_Typ
: constant Entity_Id
:= Etype
(Comp_Id
);
2838 Proc_Id
: Entity_Id
;
2840 Produced_Component_Check
: Boolean := False;
2841 -- This flag tracks whether the component has produced at least
2842 -- one invariant check.
2845 -- Nothing to do for internal component _parent. Note that it is
2846 -- not desirable to check whether the component comes from source
2847 -- because protected type components are relocated to an internal
2848 -- corresponding record, but still need processing.
2850 if Chars
(Comp_Id
) = Name_uParent
then
2854 -- Verify the invariant of the component. Note that an access
2855 -- type may have an invariant when it acts as the full view of a
2856 -- private type and the invariant appears on the partial view. In
2857 -- this case verify the access value itself.
2859 if Has_Invariants
(Comp_Typ
) then
2861 -- In GNATprove mode, the component invariants are checked by
2862 -- other means. They should not be added to the record type
2863 -- invariant procedure, so that the procedure can be used to
2864 -- check the record type invariants if any.
2866 if GNATprove_Mode
then
2870 Proc_Id
:= Invariant_Procedure
(Base_Type
(Comp_Typ
));
2872 -- The component type should have an invariant procedure
2873 -- if it has invariants of its own or inherits class-wide
2874 -- invariants from parent or interface types.
2876 pragma Assert
(Present
(Proc_Id
));
2879 -- <Comp_Typ>Invariant (T (_object).<Comp_Id>);
2881 -- Note that the invariant procedure may have a null body if
2882 -- assertions are disabled or Assertion_Policy Ignore is in
2885 if not Has_Null_Body
(Proc_Id
) then
2886 Append_New_To
(Comp_Checks
,
2887 Make_Procedure_Call_Statement
(Loc
,
2889 New_Occurrence_Of
(Proc_Id
, Loc
),
2890 Parameter_Associations
=> New_List
(
2891 Make_Selected_Component
(Loc
,
2893 Unchecked_Convert_To
2894 (T
, New_Occurrence_Of
(Obj_Id
, Loc
)),
2896 New_Occurrence_Of
(Comp_Id
, Loc
)))));
2900 Produced_Check
:= True;
2901 Produced_Component_Check
:= True;
2904 if Produced_Component_Check
and then Has_Unchecked_Union
(T
) then
2906 ("invariants cannot be checked on components of "
2907 & "unchecked_union type &?", Comp_Id
, T
);
2909 end Process_Record_Component
;
2916 -- Start of processing for Add_Record_Component_Invariants
2919 -- An untagged derived type inherits the components of its parent
2920 -- type. In order to avoid creating redundant invariant checks, do
2921 -- not process the components now. Instead wait until the ultimate
2922 -- parent of the untagged derivation chain is reached.
2924 if not Is_Untagged_Derivation
(T
) then
2925 Def
:= Type_Definition
(Parent
(T
));
2927 if Nkind
(Def
) = N_Derived_Type_Definition
then
2928 Def
:= Record_Extension_Part
(Def
);
2931 pragma Assert
(Nkind
(Def
) = N_Record_Definition
);
2932 Comps
:= Component_List
(Def
);
2934 if Present
(Comps
) then
2935 Process_Component_List
2936 (Comp_List
=> Comps
,
2937 CL_Checks
=> Checks
);
2940 end Add_Record_Component_Invariants
;
2944 Saved_GM
: constant Ghost_Mode_Type
:= Ghost_Mode
;
2945 Saved_IGR
: constant Node_Id
:= Ignored_Ghost_Region
;
2946 -- Save the Ghost-related attributes to restore on exit
2949 Priv_Item
: Node_Id
;
2950 Proc_Body
: Node_Id
;
2951 Proc_Body_Id
: Entity_Id
;
2952 Proc_Decl
: Node_Id
;
2953 Proc_Id
: Entity_Id
;
2954 Stmts
: List_Id
:= No_List
;
2956 CRec_Typ
: Entity_Id
:= Empty
;
2957 -- The corresponding record type of Full_Typ
2959 Full_Proc
: Entity_Id
:= Empty
;
2960 -- The entity of the "full" invariant procedure
2962 Full_Typ
: Entity_Id
:= Empty
;
2963 -- The full view of the working type
2965 Obj_Id
: Entity_Id
:= Empty
;
2966 -- The _object formal parameter of the invariant procedure
2968 Part_Proc
: Entity_Id
:= Empty
;
2969 -- The entity of the "partial" invariant procedure
2971 Priv_Typ
: Entity_Id
:= Empty
;
2972 -- The partial view of the working type
2974 Work_Typ
: Entity_Id
:= Empty
;
2977 -- Start of processing for Build_Invariant_Procedure_Body
2982 -- The input type denotes the implementation base type of a constrained
2983 -- array type. Work with the first subtype as all invariant pragmas are
2984 -- on its rep item chain.
2986 if Ekind
(Work_Typ
) = E_Array_Type
and then Is_Itype
(Work_Typ
) then
2987 Work_Typ
:= First_Subtype
(Work_Typ
);
2989 -- The input type denotes the corresponding record type of a protected
2990 -- or task type. Work with the concurrent type because the corresponding
2991 -- record type may not be visible to clients of the type.
2993 elsif Ekind
(Work_Typ
) = E_Record_Type
2994 and then Is_Concurrent_Record_Type
(Work_Typ
)
2996 Work_Typ
:= Corresponding_Concurrent_Type
(Work_Typ
);
2999 -- The working type may be subject to pragma Ghost. Set the mode now to
3000 -- ensure that the invariant procedure is properly marked as Ghost.
3002 Set_Ghost_Mode
(Work_Typ
);
3004 -- The type must either have invariants of its own, inherit class-wide
3005 -- invariants from parent types or interfaces, or be an array or record
3006 -- type whose components have invariants.
3008 pragma Assert
(Has_Invariants
(Work_Typ
));
3010 -- Interfaces are treated as the partial view of a private type in order
3011 -- to achieve uniformity with the general case.
3013 if Is_Interface
(Work_Typ
) then
3014 Priv_Typ
:= Work_Typ
;
3016 -- Otherwise obtain both views of the type
3019 Get_Views
(Work_Typ
, Priv_Typ
, Full_Typ
, Dummy
, CRec_Typ
);
3022 -- The caller requests a body for the partial invariant procedure
3024 if Partial_Invariant
then
3025 Full_Proc
:= Invariant_Procedure
(Work_Typ
);
3026 Proc_Id
:= Partial_Invariant_Procedure
(Work_Typ
);
3028 -- The "full" invariant procedure body was already created
3030 if Present
(Full_Proc
)
3032 (Corresponding_Body
(Unit_Declaration_Node
(Full_Proc
)))
3034 -- This scenario happens only when the type is an untagged
3035 -- derivation from a private parent and the underlying full
3036 -- view was processed before the partial view.
3039 (Is_Untagged_Private_Derivation
(Priv_Typ
, Full_Typ
));
3041 -- Nothing to do because the processing of the underlying full
3042 -- view already checked the invariants of the partial view.
3047 -- Create a declaration for the "partial" invariant procedure if it
3048 -- is not available.
3050 if No
(Proc_Id
) then
3051 Build_Invariant_Procedure_Declaration
3053 Partial_Invariant
=> True);
3055 Proc_Id
:= Partial_Invariant_Procedure
(Work_Typ
);
3058 -- The caller requests a body for the "full" invariant procedure
3061 Proc_Id
:= Invariant_Procedure
(Work_Typ
);
3062 Part_Proc
:= Partial_Invariant_Procedure
(Work_Typ
);
3064 -- Create a declaration for the "full" invariant procedure if it is
3067 if No
(Proc_Id
) then
3068 Build_Invariant_Procedure_Declaration
(Work_Typ
);
3069 Proc_Id
:= Invariant_Procedure
(Work_Typ
);
3073 -- At this point there should be an invariant procedure declaration
3075 pragma Assert
(Present
(Proc_Id
));
3076 Proc_Decl
:= Unit_Declaration_Node
(Proc_Id
);
3078 -- Nothing to do if the invariant procedure already has a body
3080 if Present
(Corresponding_Body
(Proc_Decl
)) then
3084 -- Emulate the environment of the invariant procedure by installing its
3085 -- scope and formal parameters. Note that this is not needed, but having
3086 -- the scope installed helps with the detection of invariant-related
3089 Push_Scope
(Proc_Id
);
3090 Install_Formals
(Proc_Id
);
3092 Obj_Id
:= First_Formal
(Proc_Id
);
3093 pragma Assert
(Present
(Obj_Id
));
3095 -- The "partial" invariant procedure verifies the invariants of the
3096 -- partial view only.
3098 if Partial_Invariant
then
3099 pragma Assert
(Present
(Priv_Typ
));
3106 -- Otherwise the "full" invariant procedure verifies the invariants of
3107 -- the full view, all array or record components, as well as class-wide
3108 -- invariants inherited from parent types or interfaces. In addition, it
3109 -- indirectly verifies the invariants of the partial view by calling the
3110 -- "partial" invariant procedure.
3113 pragma Assert
(Present
(Full_Typ
));
3115 -- Check the invariants of the partial view by calling the "partial"
3116 -- invariant procedure. Generate:
3118 -- <Work_Typ>Partial_Invariant (_object);
3120 if Present
(Part_Proc
) then
3121 Append_New_To
(Stmts
,
3122 Make_Procedure_Call_Statement
(Loc
,
3123 Name
=> New_Occurrence_Of
(Part_Proc
, Loc
),
3124 Parameter_Associations
=> New_List
(
3125 New_Occurrence_Of
(Obj_Id
, Loc
))));
3127 Produced_Check
:= True;
3132 -- Derived subtypes do not have a partial view
3134 if Present
(Priv_Typ
) then
3136 -- The processing of the "full" invariant procedure intentionally
3137 -- skips the partial view because a) this may result in changes of
3138 -- visibility and b) lead to duplicate checks. However, when the
3139 -- full view is the underlying full view of an untagged derived
3140 -- type whose parent type is private, partial invariants appear on
3141 -- the rep item chain of the partial view only.
3143 -- package Pack_1 is
3144 -- type Root ... is private;
3146 -- <full view of Root>
3150 -- package Pack_2 is
3151 -- type Child is new Pack_1.Root with Type_Invariant => ...;
3152 -- <underlying full view of Child>
3155 -- As a result, the processing of the full view must also consider
3156 -- all invariants of the partial view.
3158 if Is_Untagged_Private_Derivation
(Priv_Typ
, Full_Typ
) then
3161 -- Otherwise the invariants of the partial view are ignored
3164 -- Note that the rep item chain is shared between the partial
3165 -- and full views of a type. To avoid processing the invariants
3166 -- of the partial view, signal the logic to stop when the first
3167 -- rep item of the partial view has been reached.
3169 Priv_Item
:= First_Rep_Item
(Priv_Typ
);
3171 -- Ignore the invariants of the partial view by eliminating the
3178 -- Process the invariants of the full view and in certain cases those
3179 -- of the partial view. This also handles any invariants on array or
3180 -- record components.
3186 Priv_Item
=> Priv_Item
);
3192 Priv_Item
=> Priv_Item
);
3194 -- Process the elements of an array type
3196 if Is_Array_Type
(Full_Typ
) then
3197 Add_Array_Component_Invariants
(Full_Typ
, Obj_Id
, Stmts
);
3199 -- Process the components of a record type
3201 elsif Ekind
(Full_Typ
) = E_Record_Type
then
3202 Add_Record_Component_Invariants
(Full_Typ
, Obj_Id
, Stmts
);
3204 -- Process the components of a corresponding record
3206 elsif Present
(CRec_Typ
) then
3207 Add_Record_Component_Invariants
(CRec_Typ
, Obj_Id
, Stmts
);
3210 -- Process the inherited class-wide invariants of all parent types.
3211 -- This also handles any invariants on record components.
3213 Add_Parent_Invariants
(Full_Typ
, Obj_Id
, Stmts
);
3215 -- Process the inherited class-wide invariants of all implemented
3218 Add_Interface_Invariants
(Full_Typ
, Obj_Id
, Stmts
);
3223 -- At this point there should be at least one invariant check. If this
3224 -- is not the case, then the invariant-related flags were not properly
3225 -- set, or there is a missing invariant procedure on one of the array
3226 -- or record components.
3228 pragma Assert
(Produced_Check
);
3230 -- Account for the case where assertions are disabled or all invariant
3231 -- checks are subject to Assertion_Policy Ignore. Produce a completing
3235 Stmts
:= New_List
(Make_Null_Statement
(Loc
));
3239 -- procedure <Work_Typ>[Partial_]Invariant (_object : <Obj_Typ>) is
3242 -- end <Work_Typ>[Partial_]Invariant;
3245 Make_Subprogram_Body
(Loc
,
3247 Copy_Subprogram_Spec
(Parent
(Proc_Id
)),
3248 Declarations
=> Empty_List
,
3249 Handled_Statement_Sequence
=>
3250 Make_Handled_Sequence_Of_Statements
(Loc
,
3251 Statements
=> Stmts
));
3252 Proc_Body_Id
:= Defining_Entity
(Proc_Body
);
3254 -- Perform minor decoration in case the body is not analyzed
3256 Set_Ekind
(Proc_Body_Id
, E_Subprogram_Body
);
3257 Set_Etype
(Proc_Body_Id
, Standard_Void_Type
);
3258 Set_Scope
(Proc_Body_Id
, Current_Scope
);
3260 -- Link both spec and body to avoid generating duplicates
3262 Set_Corresponding_Body
(Proc_Decl
, Proc_Body_Id
);
3263 Set_Corresponding_Spec
(Proc_Body
, Proc_Id
);
3265 -- The body should not be inserted into the tree when the context is
3266 -- ASIS or a generic unit because it is not part of the template. Note
3267 -- that the body must still be generated in order to resolve the
3270 if ASIS_Mode
or Inside_A_Generic
then
3273 -- Semi-insert the body into the tree for GNATprove by setting its
3274 -- Parent field. This allows for proper upstream tree traversals.
3276 elsif GNATprove_Mode
then
3277 Set_Parent
(Proc_Body
, Parent
(Declaration_Node
(Work_Typ
)));
3279 -- Otherwise the body is part of the freezing actions of the type
3282 Append_Freeze_Action
(Work_Typ
, Proc_Body
);
3286 Restore_Ghost_Region
(Saved_GM
, Saved_IGR
);
3287 end Build_Invariant_Procedure_Body
;
3289 -------------------------------------------
3290 -- Build_Invariant_Procedure_Declaration --
3291 -------------------------------------------
3293 -- WARNING: This routine manages Ghost regions. Return statements must be
3294 -- replaced by gotos which jump to the end of the routine and restore the
3297 procedure Build_Invariant_Procedure_Declaration
3299 Partial_Invariant
: Boolean := False)
3301 Loc
: constant Source_Ptr
:= Sloc
(Typ
);
3303 Saved_GM
: constant Ghost_Mode_Type
:= Ghost_Mode
;
3304 Saved_IGR
: constant Node_Id
:= Ignored_Ghost_Region
;
3305 -- Save the Ghost-related attributes to restore on exit
3307 Proc_Decl
: Node_Id
;
3308 Proc_Id
: Entity_Id
;
3312 CRec_Typ
: Entity_Id
;
3313 -- The corresponding record type of Full_Typ
3315 Full_Base
: Entity_Id
;
3316 -- The base type of Full_Typ
3318 Full_Typ
: Entity_Id
;
3319 -- The full view of working type
3322 -- The _object formal parameter of the invariant procedure
3324 Obj_Typ
: Entity_Id
;
3325 -- The type of the _object formal parameter
3327 Priv_Typ
: Entity_Id
;
3328 -- The partial view of working type
3330 Work_Typ
: Entity_Id
;
3336 -- The input type denotes the implementation base type of a constrained
3337 -- array type. Work with the first subtype as all invariant pragmas are
3338 -- on its rep item chain.
3340 if Ekind
(Work_Typ
) = E_Array_Type
and then Is_Itype
(Work_Typ
) then
3341 Work_Typ
:= First_Subtype
(Work_Typ
);
3343 -- The input denotes the corresponding record type of a protected or a
3344 -- task type. Work with the concurrent type because the corresponding
3345 -- record type may not be visible to clients of the type.
3347 elsif Ekind
(Work_Typ
) = E_Record_Type
3348 and then Is_Concurrent_Record_Type
(Work_Typ
)
3350 Work_Typ
:= Corresponding_Concurrent_Type
(Work_Typ
);
3353 -- The working type may be subject to pragma Ghost. Set the mode now to
3354 -- ensure that the invariant procedure is properly marked as Ghost.
3356 Set_Ghost_Mode
(Work_Typ
);
3358 -- The type must either have invariants of its own, inherit class-wide
3359 -- invariants from parent or interface types, or be an array or record
3360 -- type whose components have invariants.
3362 pragma Assert
(Has_Invariants
(Work_Typ
));
3364 -- Nothing to do if the type already has a "partial" invariant procedure
3366 if Partial_Invariant
then
3367 if Present
(Partial_Invariant_Procedure
(Work_Typ
)) then
3371 -- Nothing to do if the type already has a "full" invariant procedure
3373 elsif Present
(Invariant_Procedure
(Work_Typ
)) then
3377 -- The caller requests the declaration of the "partial" invariant
3380 if Partial_Invariant
then
3381 Proc_Nam
:= New_External_Name
(Chars
(Work_Typ
), "Partial_Invariant");
3383 -- Otherwise the caller requests the declaration of the "full" invariant
3387 Proc_Nam
:= New_External_Name
(Chars
(Work_Typ
), "Invariant");
3390 Proc_Id
:= Make_Defining_Identifier
(Loc
, Chars
=> Proc_Nam
);
3392 -- Perform minor decoration in case the declaration is not analyzed
3394 Set_Ekind
(Proc_Id
, E_Procedure
);
3395 Set_Etype
(Proc_Id
, Standard_Void_Type
);
3396 Set_Scope
(Proc_Id
, Current_Scope
);
3398 if Partial_Invariant
then
3399 Set_Is_Partial_Invariant_Procedure
(Proc_Id
);
3400 Set_Partial_Invariant_Procedure
(Work_Typ
, Proc_Id
);
3402 Set_Is_Invariant_Procedure
(Proc_Id
);
3403 Set_Invariant_Procedure
(Work_Typ
, Proc_Id
);
3406 -- The invariant procedure requires debug info when the invariants are
3407 -- subject to Source Coverage Obligations.
3409 if Generate_SCO
then
3410 Set_Needs_Debug_Info
(Proc_Id
);
3413 -- Obtain all views of the input type
3415 Get_Views
(Work_Typ
, Priv_Typ
, Full_Typ
, Full_Base
, CRec_Typ
);
3417 -- Associate the invariant procedure with all views
3419 Propagate_Invariant_Attributes
(Priv_Typ
, From_Typ
=> Work_Typ
);
3420 Propagate_Invariant_Attributes
(Full_Typ
, From_Typ
=> Work_Typ
);
3421 Propagate_Invariant_Attributes
(Full_Base
, From_Typ
=> Work_Typ
);
3422 Propagate_Invariant_Attributes
(CRec_Typ
, From_Typ
=> Work_Typ
);
3424 -- The declaration of the invariant procedure is inserted after the
3425 -- declaration of the partial view as this allows for proper external
3428 if Present
(Priv_Typ
) then
3429 Typ_Decl
:= Declaration_Node
(Priv_Typ
);
3431 -- Anonymous arrays in object declarations have no explicit declaration
3432 -- so use the related object declaration as the insertion point.
3434 elsif Is_Itype
(Work_Typ
) and then Is_Array_Type
(Work_Typ
) then
3435 Typ_Decl
:= Associated_Node_For_Itype
(Work_Typ
);
3437 -- Derived types with the full view as parent do not have a partial
3438 -- view. Insert the invariant procedure after the derived type.
3441 Typ_Decl
:= Declaration_Node
(Full_Typ
);
3444 -- The type should have a declarative node
3446 pragma Assert
(Present
(Typ_Decl
));
3448 -- Create the formal parameter which emulates the variable-like behavior
3449 -- of the current type instance.
3451 Obj_Id
:= Make_Defining_Identifier
(Loc
, Chars
=> Name_uObject
);
3453 -- When generating an invariant procedure declaration for an abstract
3454 -- type (including interfaces), use the class-wide type as the _object
3455 -- type. This has several desirable effects:
3457 -- * The invariant procedure does not become a primitive of the type.
3458 -- This eliminates the need to either special case the treatment of
3459 -- invariant procedures, or to make it a predefined primitive and
3460 -- force every derived type to potentially provide an empty body.
3462 -- * The invariant procedure does not need to be declared as abstract.
3463 -- This allows for a proper body, which in turn avoids redundant
3464 -- processing of the same invariants for types with multiple views.
3466 -- * The class-wide type allows for calls to abstract primitives
3467 -- within a nonabstract subprogram. The calls are treated as
3468 -- dispatching and require additional processing when they are
3469 -- remapped to call primitives of derived types. See routine
3470 -- Replace_References for details.
3472 if Is_Abstract_Type
(Work_Typ
) then
3473 Obj_Typ
:= Class_Wide_Type
(Work_Typ
);
3475 Obj_Typ
:= Work_Typ
;
3478 -- Perform minor decoration in case the declaration is not analyzed
3480 Set_Ekind
(Obj_Id
, E_In_Parameter
);
3481 Set_Etype
(Obj_Id
, Obj_Typ
);
3482 Set_Scope
(Obj_Id
, Proc_Id
);
3484 Set_First_Entity
(Proc_Id
, Obj_Id
);
3485 Set_Last_Entity
(Proc_Id
, Obj_Id
);
3488 -- procedure <Work_Typ>[Partial_]Invariant (_object : <Obj_Typ>);
3491 Make_Subprogram_Declaration
(Loc
,
3493 Make_Procedure_Specification
(Loc
,
3494 Defining_Unit_Name
=> Proc_Id
,
3495 Parameter_Specifications
=> New_List
(
3496 Make_Parameter_Specification
(Loc
,
3497 Defining_Identifier
=> Obj_Id
,
3498 Parameter_Type
=> New_Occurrence_Of
(Obj_Typ
, Loc
)))));
3500 -- The declaration should not be inserted into the tree when the context
3501 -- is ASIS or a generic unit because it is not part of the template.
3503 if ASIS_Mode
or Inside_A_Generic
then
3506 -- Semi-insert the declaration into the tree for GNATprove by setting
3507 -- its Parent field. This allows for proper upstream tree traversals.
3509 elsif GNATprove_Mode
then
3510 Set_Parent
(Proc_Decl
, Parent
(Typ_Decl
));
3512 -- Otherwise insert the declaration
3515 pragma Assert
(Present
(Typ_Decl
));
3516 Insert_After_And_Analyze
(Typ_Decl
, Proc_Decl
);
3520 Restore_Ghost_Region
(Saved_GM
, Saved_IGR
);
3521 end Build_Invariant_Procedure_Declaration
;
3523 --------------------------
3524 -- Build_Procedure_Form --
3525 --------------------------
3527 procedure Build_Procedure_Form
(N
: Node_Id
) is
3528 Loc
: constant Source_Ptr
:= Sloc
(N
);
3529 Subp
: constant Entity_Id
:= Defining_Entity
(N
);
3531 Func_Formal
: Entity_Id
;
3532 Proc_Formals
: List_Id
;
3533 Proc_Decl
: Node_Id
;
3536 -- No action needed if this transformation was already done, or in case
3537 -- of subprogram renaming declarations.
3539 if Nkind
(Specification
(N
)) = N_Procedure_Specification
3540 or else Nkind
(N
) = N_Subprogram_Renaming_Declaration
3545 -- Ditto when dealing with an expression function, where both the
3546 -- original expression and the generated declaration end up being
3549 if Rewritten_For_C
(Subp
) then
3553 Proc_Formals
:= New_List
;
3555 -- Create a list of formal parameters with the same types as the
3558 Func_Formal
:= First_Formal
(Subp
);
3559 while Present
(Func_Formal
) loop
3560 Append_To
(Proc_Formals
,
3561 Make_Parameter_Specification
(Loc
,
3562 Defining_Identifier
=>
3563 Make_Defining_Identifier
(Loc
, Chars
(Func_Formal
)),
3565 New_Occurrence_Of
(Etype
(Func_Formal
), Loc
)));
3567 Next_Formal
(Func_Formal
);
3570 -- Add an extra out parameter to carry the function result
3573 Name_Buffer
(1 .. Name_Len
) := "RESULT";
3574 Append_To
(Proc_Formals
,
3575 Make_Parameter_Specification
(Loc
,
3576 Defining_Identifier
=>
3577 Make_Defining_Identifier
(Loc
, Chars
=> Name_Find
),
3578 Out_Present
=> True,
3579 Parameter_Type
=> New_Occurrence_Of
(Etype
(Subp
), Loc
)));
3581 -- The new procedure declaration is inserted immediately after the
3582 -- function declaration. The processing in Build_Procedure_Body_Form
3583 -- relies on this order.
3586 Make_Subprogram_Declaration
(Loc
,
3588 Make_Procedure_Specification
(Loc
,
3589 Defining_Unit_Name
=>
3590 Make_Defining_Identifier
(Loc
, Chars
(Subp
)),
3591 Parameter_Specifications
=> Proc_Formals
));
3593 Insert_After_And_Analyze
(Unit_Declaration_Node
(Subp
), Proc_Decl
);
3595 -- Entity of procedure must remain invisible so that it does not
3596 -- overload subsequent references to the original function.
3598 Set_Is_Immediately_Visible
(Defining_Entity
(Proc_Decl
), False);
3600 -- Mark the function as having a procedure form and link the function
3601 -- and its internally built procedure.
3603 Set_Rewritten_For_C
(Subp
);
3604 Set_Corresponding_Procedure
(Subp
, Defining_Entity
(Proc_Decl
));
3605 Set_Corresponding_Function
(Defining_Entity
(Proc_Decl
), Subp
);
3606 end Build_Procedure_Form
;
3608 ------------------------
3609 -- Build_Runtime_Call --
3610 ------------------------
3612 function Build_Runtime_Call
(Loc
: Source_Ptr
; RE
: RE_Id
) return Node_Id
is
3614 -- If entity is not available, we can skip making the call (this avoids
3615 -- junk duplicated error messages in a number of cases).
3617 if not RTE_Available
(RE
) then
3618 return Make_Null_Statement
(Loc
);
3621 Make_Procedure_Call_Statement
(Loc
,
3622 Name
=> New_Occurrence_Of
(RTE
(RE
), Loc
));
3624 end Build_Runtime_Call
;
3626 ------------------------
3627 -- Build_SS_Mark_Call --
3628 ------------------------
3630 function Build_SS_Mark_Call
3632 Mark
: Entity_Id
) return Node_Id
3636 -- Mark : constant Mark_Id := SS_Mark;
3639 Make_Object_Declaration
(Loc
,
3640 Defining_Identifier
=> Mark
,
3641 Constant_Present
=> True,
3642 Object_Definition
=>
3643 New_Occurrence_Of
(RTE
(RE_Mark_Id
), Loc
),
3645 Make_Function_Call
(Loc
,
3646 Name
=> New_Occurrence_Of
(RTE
(RE_SS_Mark
), Loc
)));
3647 end Build_SS_Mark_Call
;
3649 ---------------------------
3650 -- Build_SS_Release_Call --
3651 ---------------------------
3653 function Build_SS_Release_Call
3655 Mark
: Entity_Id
) return Node_Id
3659 -- SS_Release (Mark);
3662 Make_Procedure_Call_Statement
(Loc
,
3664 New_Occurrence_Of
(RTE
(RE_SS_Release
), Loc
),
3665 Parameter_Associations
=> New_List
(
3666 New_Occurrence_Of
(Mark
, Loc
)));
3667 end Build_SS_Release_Call
;
3669 ----------------------------
3670 -- Build_Task_Array_Image --
3671 ----------------------------
3673 -- This function generates the body for a function that constructs the
3674 -- image string for a task that is an array component. The function is
3675 -- local to the init proc for the array type, and is called for each one
3676 -- of the components. The constructed image has the form of an indexed
3677 -- component, whose prefix is the outer variable of the array type.
3678 -- The n-dimensional array type has known indexes Index, Index2...
3680 -- Id_Ref is an indexed component form created by the enclosing init proc.
3681 -- Its successive indexes are Val1, Val2, ... which are the loop variables
3682 -- in the loops that call the individual task init proc on each component.
3684 -- The generated function has the following structure:
3686 -- function F return String is
3687 -- Pref : string renames Task_Name;
3688 -- T1 : String := Index1'Image (Val1);
3690 -- Tn : String := indexn'image (Valn);
3691 -- Len : Integer := T1'Length + ... + Tn'Length + n + 1;
3692 -- -- Len includes commas and the end parentheses.
3693 -- Res : String (1..Len);
3694 -- Pos : Integer := Pref'Length;
3697 -- Res (1 .. Pos) := Pref;
3699 -- Res (Pos) := '(';
3701 -- Res (Pos .. Pos + T1'Length - 1) := T1;
3702 -- Pos := Pos + T1'Length;
3703 -- Res (Pos) := '.';
3706 -- Res (Pos .. Pos + Tn'Length - 1) := Tn;
3707 -- Res (Len) := ')';
3712 -- Needless to say, multidimensional arrays of tasks are rare enough that
3713 -- the bulkiness of this code is not really a concern.
3715 function Build_Task_Array_Image
3719 Dyn
: Boolean := False) return Node_Id
3721 Dims
: constant Nat
:= Number_Dimensions
(A_Type
);
3722 -- Number of dimensions for array of tasks
3724 Temps
: array (1 .. Dims
) of Entity_Id
;
3725 -- Array of temporaries to hold string for each index
3731 -- Total length of generated name
3734 -- Running index for substring assignments
3736 Pref
: constant Entity_Id
:= Make_Temporary
(Loc
, 'P');
3737 -- Name of enclosing variable, prefix of resulting name
3740 -- String to hold result
3743 -- Value of successive indexes
3746 -- Expression to compute total size of string
3749 -- Entity for name at one index position
3751 Decls
: constant List_Id
:= New_List
;
3752 Stats
: constant List_Id
:= New_List
;
3755 -- For a dynamic task, the name comes from the target variable. For a
3756 -- static one it is a formal of the enclosing init proc.
3759 Get_Name_String
(Chars
(Entity
(Prefix
(Id_Ref
))));
3761 Make_Object_Declaration
(Loc
,
3762 Defining_Identifier
=> Pref
,
3763 Object_Definition
=> New_Occurrence_Of
(Standard_String
, Loc
),
3765 Make_String_Literal
(Loc
,
3766 Strval
=> String_From_Name_Buffer
)));
3770 Make_Object_Renaming_Declaration
(Loc
,
3771 Defining_Identifier
=> Pref
,
3772 Subtype_Mark
=> New_Occurrence_Of
(Standard_String
, Loc
),
3773 Name
=> Make_Identifier
(Loc
, Name_uTask_Name
)));
3776 Indx
:= First_Index
(A_Type
);
3777 Val
:= First
(Expressions
(Id_Ref
));
3779 for J
in 1 .. Dims
loop
3780 T
:= Make_Temporary
(Loc
, 'T');
3784 Make_Object_Declaration
(Loc
,
3785 Defining_Identifier
=> T
,
3786 Object_Definition
=> New_Occurrence_Of
(Standard_String
, Loc
),
3788 Make_Attribute_Reference
(Loc
,
3789 Attribute_Name
=> Name_Image
,
3790 Prefix
=> New_Occurrence_Of
(Etype
(Indx
), Loc
),
3791 Expressions
=> New_List
(New_Copy_Tree
(Val
)))));
3797 Sum
:= Make_Integer_Literal
(Loc
, Dims
+ 1);
3803 Make_Attribute_Reference
(Loc
,
3804 Attribute_Name
=> Name_Length
,
3805 Prefix
=> New_Occurrence_Of
(Pref
, Loc
),
3806 Expressions
=> New_List
(Make_Integer_Literal
(Loc
, 1))));
3808 for J
in 1 .. Dims
loop
3813 Make_Attribute_Reference
(Loc
,
3814 Attribute_Name
=> Name_Length
,
3816 New_Occurrence_Of
(Temps
(J
), Loc
),
3817 Expressions
=> New_List
(Make_Integer_Literal
(Loc
, 1))));
3820 Build_Task_Image_Prefix
(Loc
, Len
, Res
, Pos
, Pref
, Sum
, Decls
, Stats
);
3822 Set_Character_Literal_Name
(Char_Code
(Character'Pos ('(')));
3825 Make_Assignment_Statement
(Loc
,
3827 Make_Indexed_Component
(Loc
,
3828 Prefix
=> New_Occurrence_Of
(Res
, Loc
),
3829 Expressions
=> New_List
(New_Occurrence_Of
(Pos
, Loc
))),
3831 Make_Character_Literal
(Loc
,
3833 Char_Literal_Value
=> UI_From_Int
(Character'Pos ('(')))));
3836 Make_Assignment_Statement
(Loc
,
3837 Name
=> New_Occurrence_Of
(Pos
, Loc
),
3840 Left_Opnd
=> New_Occurrence_Of
(Pos
, Loc
),
3841 Right_Opnd
=> Make_Integer_Literal
(Loc
, 1))));
3843 for J
in 1 .. Dims
loop
3846 Make_Assignment_Statement
(Loc
,
3849 Prefix
=> New_Occurrence_Of
(Res
, Loc
),
3852 Low_Bound
=> New_Occurrence_Of
(Pos
, Loc
),
3854 Make_Op_Subtract
(Loc
,
3857 Left_Opnd
=> New_Occurrence_Of
(Pos
, Loc
),
3859 Make_Attribute_Reference
(Loc
,
3860 Attribute_Name
=> Name_Length
,
3862 New_Occurrence_Of
(Temps
(J
), Loc
),
3864 New_List
(Make_Integer_Literal
(Loc
, 1)))),
3865 Right_Opnd
=> Make_Integer_Literal
(Loc
, 1)))),
3867 Expression
=> New_Occurrence_Of
(Temps
(J
), Loc
)));
3871 Make_Assignment_Statement
(Loc
,
3872 Name
=> New_Occurrence_Of
(Pos
, Loc
),
3875 Left_Opnd
=> New_Occurrence_Of
(Pos
, Loc
),
3877 Make_Attribute_Reference
(Loc
,
3878 Attribute_Name
=> Name_Length
,
3879 Prefix
=> New_Occurrence_Of
(Temps
(J
), Loc
),
3881 New_List
(Make_Integer_Literal
(Loc
, 1))))));
3883 Set_Character_Literal_Name
(Char_Code
(Character'Pos (',')));
3886 Make_Assignment_Statement
(Loc
,
3887 Name
=> Make_Indexed_Component
(Loc
,
3888 Prefix
=> New_Occurrence_Of
(Res
, Loc
),
3889 Expressions
=> New_List
(New_Occurrence_Of
(Pos
, Loc
))),
3891 Make_Character_Literal
(Loc
,
3893 Char_Literal_Value
=> UI_From_Int
(Character'Pos (',')))));
3896 Make_Assignment_Statement
(Loc
,
3897 Name
=> New_Occurrence_Of
(Pos
, Loc
),
3900 Left_Opnd
=> New_Occurrence_Of
(Pos
, Loc
),
3901 Right_Opnd
=> Make_Integer_Literal
(Loc
, 1))));
3905 Set_Character_Literal_Name
(Char_Code
(Character'Pos (')')));
3908 Make_Assignment_Statement
(Loc
,
3910 Make_Indexed_Component
(Loc
,
3911 Prefix
=> New_Occurrence_Of
(Res
, Loc
),
3912 Expressions
=> New_List
(New_Occurrence_Of
(Len
, Loc
))),
3914 Make_Character_Literal
(Loc
,
3916 Char_Literal_Value
=> UI_From_Int
(Character'Pos (')')))));
3917 return Build_Task_Image_Function
(Loc
, Decls
, Stats
, Res
);
3918 end Build_Task_Array_Image
;
3920 ----------------------------
3921 -- Build_Task_Image_Decls --
3922 ----------------------------
3924 function Build_Task_Image_Decls
3928 In_Init_Proc
: Boolean := False) return List_Id
3930 Decls
: constant List_Id
:= New_List
;
3931 T_Id
: Entity_Id
:= Empty
;
3933 Expr
: Node_Id
:= Empty
;
3934 Fun
: Node_Id
:= Empty
;
3935 Is_Dyn
: constant Boolean :=
3936 Nkind
(Parent
(Id_Ref
)) = N_Assignment_Statement
3938 Nkind
(Expression
(Parent
(Id_Ref
))) = N_Allocator
;
3941 -- If Discard_Names or No_Implicit_Heap_Allocations are in effect,
3942 -- generate a dummy declaration only.
3944 if Restriction_Active
(No_Implicit_Heap_Allocations
)
3945 or else Global_Discard_Names
3947 T_Id
:= Make_Temporary
(Loc
, 'J');
3952 Make_Object_Declaration
(Loc
,
3953 Defining_Identifier
=> T_Id
,
3954 Object_Definition
=> New_Occurrence_Of
(Standard_String
, Loc
),
3956 Make_String_Literal
(Loc
,
3957 Strval
=> String_From_Name_Buffer
)));
3960 if Nkind
(Id_Ref
) = N_Identifier
3961 or else Nkind
(Id_Ref
) = N_Defining_Identifier
3963 -- For a simple variable, the image of the task is built from
3964 -- the name of the variable. To avoid possible conflict with the
3965 -- anonymous type created for a single protected object, add a
3969 Make_Defining_Identifier
(Loc
,
3970 New_External_Name
(Chars
(Id_Ref
), 'T', 1));
3972 Get_Name_String
(Chars
(Id_Ref
));
3975 Make_String_Literal
(Loc
,
3976 Strval
=> String_From_Name_Buffer
);
3978 elsif Nkind
(Id_Ref
) = N_Selected_Component
then
3980 Make_Defining_Identifier
(Loc
,
3981 New_External_Name
(Chars
(Selector_Name
(Id_Ref
)), 'T'));
3982 Fun
:= Build_Task_Record_Image
(Loc
, Id_Ref
, Is_Dyn
);
3984 elsif Nkind
(Id_Ref
) = N_Indexed_Component
then
3986 Make_Defining_Identifier
(Loc
,
3987 New_External_Name
(Chars
(A_Type
), 'N'));
3989 Fun
:= Build_Task_Array_Image
(Loc
, Id_Ref
, A_Type
, Is_Dyn
);
3993 if Present
(Fun
) then
3994 Append
(Fun
, Decls
);
3995 Expr
:= Make_Function_Call
(Loc
,
3996 Name
=> New_Occurrence_Of
(Defining_Entity
(Fun
), Loc
));
3998 if not In_Init_Proc
then
3999 Set_Uses_Sec_Stack
(Defining_Entity
(Fun
));
4003 Decl
:= Make_Object_Declaration
(Loc
,
4004 Defining_Identifier
=> T_Id
,
4005 Object_Definition
=> New_Occurrence_Of
(Standard_String
, Loc
),
4006 Constant_Present
=> True,
4007 Expression
=> Expr
);
4009 Append
(Decl
, Decls
);
4011 end Build_Task_Image_Decls
;
4013 -------------------------------
4014 -- Build_Task_Image_Function --
4015 -------------------------------
4017 function Build_Task_Image_Function
4021 Res
: Entity_Id
) return Node_Id
4027 Make_Simple_Return_Statement
(Loc
,
4028 Expression
=> New_Occurrence_Of
(Res
, Loc
)));
4030 Spec
:= Make_Function_Specification
(Loc
,
4031 Defining_Unit_Name
=> Make_Temporary
(Loc
, 'F'),
4032 Result_Definition
=> New_Occurrence_Of
(Standard_String
, Loc
));
4034 -- Calls to 'Image use the secondary stack, which must be cleaned up
4035 -- after the task name is built.
4037 return Make_Subprogram_Body
(Loc
,
4038 Specification
=> Spec
,
4039 Declarations
=> Decls
,
4040 Handled_Statement_Sequence
=>
4041 Make_Handled_Sequence_Of_Statements
(Loc
, Statements
=> Stats
));
4042 end Build_Task_Image_Function
;
4044 -----------------------------
4045 -- Build_Task_Image_Prefix --
4046 -----------------------------
4048 procedure Build_Task_Image_Prefix
4050 Len
: out Entity_Id
;
4051 Res
: out Entity_Id
;
4052 Pos
: out Entity_Id
;
4059 Len
:= Make_Temporary
(Loc
, 'L', Sum
);
4062 Make_Object_Declaration
(Loc
,
4063 Defining_Identifier
=> Len
,
4064 Object_Definition
=> New_Occurrence_Of
(Standard_Integer
, Loc
),
4065 Expression
=> Sum
));
4067 Res
:= Make_Temporary
(Loc
, 'R');
4070 Make_Object_Declaration
(Loc
,
4071 Defining_Identifier
=> Res
,
4072 Object_Definition
=>
4073 Make_Subtype_Indication
(Loc
,
4074 Subtype_Mark
=> New_Occurrence_Of
(Standard_String
, Loc
),
4076 Make_Index_Or_Discriminant_Constraint
(Loc
,
4080 Low_Bound
=> Make_Integer_Literal
(Loc
, 1),
4081 High_Bound
=> New_Occurrence_Of
(Len
, Loc
)))))));
4083 -- Indicate that the result is an internal temporary, so it does not
4084 -- receive a bogus initialization when declaration is expanded. This
4085 -- is both efficient, and prevents anomalies in the handling of
4086 -- dynamic objects on the secondary stack.
4088 Set_Is_Internal
(Res
);
4089 Pos
:= Make_Temporary
(Loc
, 'P');
4092 Make_Object_Declaration
(Loc
,
4093 Defining_Identifier
=> Pos
,
4094 Object_Definition
=> New_Occurrence_Of
(Standard_Integer
, Loc
)));
4096 -- Pos := Prefix'Length;
4099 Make_Assignment_Statement
(Loc
,
4100 Name
=> New_Occurrence_Of
(Pos
, Loc
),
4102 Make_Attribute_Reference
(Loc
,
4103 Attribute_Name
=> Name_Length
,
4104 Prefix
=> New_Occurrence_Of
(Prefix
, Loc
),
4105 Expressions
=> New_List
(Make_Integer_Literal
(Loc
, 1)))));
4107 -- Res (1 .. Pos) := Prefix;
4110 Make_Assignment_Statement
(Loc
,
4113 Prefix
=> New_Occurrence_Of
(Res
, Loc
),
4116 Low_Bound
=> Make_Integer_Literal
(Loc
, 1),
4117 High_Bound
=> New_Occurrence_Of
(Pos
, Loc
))),
4119 Expression
=> New_Occurrence_Of
(Prefix
, Loc
)));
4122 Make_Assignment_Statement
(Loc
,
4123 Name
=> New_Occurrence_Of
(Pos
, Loc
),
4126 Left_Opnd
=> New_Occurrence_Of
(Pos
, Loc
),
4127 Right_Opnd
=> Make_Integer_Literal
(Loc
, 1))));
4128 end Build_Task_Image_Prefix
;
4130 -----------------------------
4131 -- Build_Task_Record_Image --
4132 -----------------------------
4134 function Build_Task_Record_Image
4137 Dyn
: Boolean := False) return Node_Id
4140 -- Total length of generated name
4143 -- Index into result
4146 -- String to hold result
4148 Pref
: constant Entity_Id
:= Make_Temporary
(Loc
, 'P');
4149 -- Name of enclosing variable, prefix of resulting name
4152 -- Expression to compute total size of string
4155 -- Entity for selector name
4157 Decls
: constant List_Id
:= New_List
;
4158 Stats
: constant List_Id
:= New_List
;
4161 -- For a dynamic task, the name comes from the target variable. For a
4162 -- static one it is a formal of the enclosing init proc.
4165 Get_Name_String
(Chars
(Entity
(Prefix
(Id_Ref
))));
4167 Make_Object_Declaration
(Loc
,
4168 Defining_Identifier
=> Pref
,
4169 Object_Definition
=> New_Occurrence_Of
(Standard_String
, Loc
),
4171 Make_String_Literal
(Loc
,
4172 Strval
=> String_From_Name_Buffer
)));
4176 Make_Object_Renaming_Declaration
(Loc
,
4177 Defining_Identifier
=> Pref
,
4178 Subtype_Mark
=> New_Occurrence_Of
(Standard_String
, Loc
),
4179 Name
=> Make_Identifier
(Loc
, Name_uTask_Name
)));
4182 Sel
:= Make_Temporary
(Loc
, 'S');
4184 Get_Name_String
(Chars
(Selector_Name
(Id_Ref
)));
4187 Make_Object_Declaration
(Loc
,
4188 Defining_Identifier
=> Sel
,
4189 Object_Definition
=> New_Occurrence_Of
(Standard_String
, Loc
),
4191 Make_String_Literal
(Loc
,
4192 Strval
=> String_From_Name_Buffer
)));
4194 Sum
:= Make_Integer_Literal
(Loc
, Nat
(Name_Len
+ 1));
4200 Make_Attribute_Reference
(Loc
,
4201 Attribute_Name
=> Name_Length
,
4203 New_Occurrence_Of
(Pref
, Loc
),
4204 Expressions
=> New_List
(Make_Integer_Literal
(Loc
, 1))));
4206 Build_Task_Image_Prefix
(Loc
, Len
, Res
, Pos
, Pref
, Sum
, Decls
, Stats
);
4208 Set_Character_Literal_Name
(Char_Code
(Character'Pos ('.')));
4210 -- Res (Pos) := '.';
4213 Make_Assignment_Statement
(Loc
,
4214 Name
=> Make_Indexed_Component
(Loc
,
4215 Prefix
=> New_Occurrence_Of
(Res
, Loc
),
4216 Expressions
=> New_List
(New_Occurrence_Of
(Pos
, Loc
))),
4218 Make_Character_Literal
(Loc
,
4220 Char_Literal_Value
=>
4221 UI_From_Int
(Character'Pos ('.')))));
4224 Make_Assignment_Statement
(Loc
,
4225 Name
=> New_Occurrence_Of
(Pos
, Loc
),
4228 Left_Opnd
=> New_Occurrence_Of
(Pos
, Loc
),
4229 Right_Opnd
=> Make_Integer_Literal
(Loc
, 1))));
4231 -- Res (Pos .. Len) := Selector;
4234 Make_Assignment_Statement
(Loc
,
4235 Name
=> Make_Slice
(Loc
,
4236 Prefix
=> New_Occurrence_Of
(Res
, Loc
),
4239 Low_Bound
=> New_Occurrence_Of
(Pos
, Loc
),
4240 High_Bound
=> New_Occurrence_Of
(Len
, Loc
))),
4241 Expression
=> New_Occurrence_Of
(Sel
, Loc
)));
4243 return Build_Task_Image_Function
(Loc
, Decls
, Stats
, Res
);
4244 end Build_Task_Record_Image
;
4246 ---------------------------------------
4247 -- Build_Transient_Object_Statements --
4248 ---------------------------------------
4250 procedure Build_Transient_Object_Statements
4251 (Obj_Decl
: Node_Id
;
4252 Fin_Call
: out Node_Id
;
4253 Hook_Assign
: out Node_Id
;
4254 Hook_Clear
: out Node_Id
;
4255 Hook_Decl
: out Node_Id
;
4256 Ptr_Decl
: out Node_Id
;
4257 Finalize_Obj
: Boolean := True)
4259 Loc
: constant Source_Ptr
:= Sloc
(Obj_Decl
);
4260 Obj_Id
: constant Entity_Id
:= Defining_Entity
(Obj_Decl
);
4261 Obj_Typ
: constant Entity_Id
:= Base_Type
(Etype
(Obj_Id
));
4263 Desig_Typ
: Entity_Id
;
4264 Hook_Expr
: Node_Id
;
4265 Hook_Id
: Entity_Id
;
4267 Ptr_Typ
: Entity_Id
;
4270 -- Recover the type of the object
4272 Desig_Typ
:= Obj_Typ
;
4274 if Is_Access_Type
(Desig_Typ
) then
4275 Desig_Typ
:= Available_View
(Designated_Type
(Desig_Typ
));
4278 -- Create an access type which provides a reference to the transient
4279 -- object. Generate:
4281 -- type Ptr_Typ is access all Desig_Typ;
4283 Ptr_Typ
:= Make_Temporary
(Loc
, 'A');
4284 Set_Ekind
(Ptr_Typ
, E_General_Access_Type
);
4285 Set_Directly_Designated_Type
(Ptr_Typ
, Desig_Typ
);
4288 Make_Full_Type_Declaration
(Loc
,
4289 Defining_Identifier
=> Ptr_Typ
,
4291 Make_Access_To_Object_Definition
(Loc
,
4292 All_Present
=> True,
4293 Subtype_Indication
=> New_Occurrence_Of
(Desig_Typ
, Loc
)));
4295 -- Create a temporary check which acts as a hook to the transient
4296 -- object. Generate:
4298 -- Hook : Ptr_Typ := null;
4300 Hook_Id
:= Make_Temporary
(Loc
, 'T');
4301 Set_Ekind
(Hook_Id
, E_Variable
);
4302 Set_Etype
(Hook_Id
, Ptr_Typ
);
4305 Make_Object_Declaration
(Loc
,
4306 Defining_Identifier
=> Hook_Id
,
4307 Object_Definition
=> New_Occurrence_Of
(Ptr_Typ
, Loc
),
4308 Expression
=> Make_Null
(Loc
));
4310 -- Mark the temporary as a hook. This signals the machinery in
4311 -- Build_Finalizer to recognize this special case.
4313 Set_Status_Flag_Or_Transient_Decl
(Hook_Id
, Obj_Decl
);
4315 -- Hook the transient object to the temporary. Generate:
4317 -- Hook := Ptr_Typ (Obj_Id);
4319 -- Hool := Obj_Id'Unrestricted_Access;
4321 if Is_Access_Type
(Obj_Typ
) then
4323 Unchecked_Convert_To
(Ptr_Typ
, New_Occurrence_Of
(Obj_Id
, Loc
));
4326 Make_Attribute_Reference
(Loc
,
4327 Prefix
=> New_Occurrence_Of
(Obj_Id
, Loc
),
4328 Attribute_Name
=> Name_Unrestricted_Access
);
4332 Make_Assignment_Statement
(Loc
,
4333 Name
=> New_Occurrence_Of
(Hook_Id
, Loc
),
4334 Expression
=> Hook_Expr
);
4336 -- Crear the hook prior to finalizing the object. Generate:
4341 Make_Assignment_Statement
(Loc
,
4342 Name
=> New_Occurrence_Of
(Hook_Id
, Loc
),
4343 Expression
=> Make_Null
(Loc
));
4345 -- Finalize the object. Generate:
4347 -- [Deep_]Finalize (Obj_Ref[.all]);
4349 if Finalize_Obj
then
4350 Obj_Ref
:= New_Occurrence_Of
(Obj_Id
, Loc
);
4352 if Is_Access_Type
(Obj_Typ
) then
4353 Obj_Ref
:= Make_Explicit_Dereference
(Loc
, Obj_Ref
);
4354 Set_Etype
(Obj_Ref
, Desig_Typ
);
4359 (Obj_Ref
=> Obj_Ref
,
4362 -- Otherwise finalize the hook. Generate:
4364 -- [Deep_]Finalize (Hook.all);
4370 Make_Explicit_Dereference
(Loc
,
4371 Prefix
=> New_Occurrence_Of
(Hook_Id
, Loc
)),
4374 end Build_Transient_Object_Statements
;
4376 -----------------------------
4377 -- Check_Float_Op_Overflow --
4378 -----------------------------
4380 procedure Check_Float_Op_Overflow
(N
: Node_Id
) is
4382 -- Return if no check needed
4384 if not Is_Floating_Point_Type
(Etype
(N
))
4385 or else not (Do_Overflow_Check
(N
) and then Check_Float_Overflow
)
4387 -- In CodePeer_Mode, rely on the overflow check flag being set instead
4388 -- and do not expand the code for float overflow checking.
4390 or else CodePeer_Mode
4395 -- Otherwise we replace the expression by
4397 -- do Tnn : constant ftype := expression;
4398 -- constraint_error when not Tnn'Valid;
4402 Loc
: constant Source_Ptr
:= Sloc
(N
);
4403 Tnn
: constant Entity_Id
:= Make_Temporary
(Loc
, 'T', N
);
4404 Typ
: constant Entity_Id
:= Etype
(N
);
4407 -- Turn off the Do_Overflow_Check flag, since we are doing that work
4408 -- right here. We also set the node as analyzed to prevent infinite
4409 -- recursion from repeating the operation in the expansion.
4411 Set_Do_Overflow_Check
(N
, False);
4412 Set_Analyzed
(N
, True);
4414 -- Do the rewrite to include the check
4417 Make_Expression_With_Actions
(Loc
,
4418 Actions
=> New_List
(
4419 Make_Object_Declaration
(Loc
,
4420 Defining_Identifier
=> Tnn
,
4421 Object_Definition
=> New_Occurrence_Of
(Typ
, Loc
),
4422 Constant_Present
=> True,
4423 Expression
=> Relocate_Node
(N
)),
4424 Make_Raise_Constraint_Error
(Loc
,
4428 Make_Attribute_Reference
(Loc
,
4429 Prefix
=> New_Occurrence_Of
(Tnn
, Loc
),
4430 Attribute_Name
=> Name_Valid
)),
4431 Reason
=> CE_Overflow_Check_Failed
)),
4432 Expression
=> New_Occurrence_Of
(Tnn
, Loc
)));
4434 Analyze_And_Resolve
(N
, Typ
);
4436 end Check_Float_Op_Overflow
;
4438 ----------------------------------
4439 -- Component_May_Be_Bit_Aligned --
4440 ----------------------------------
4442 function Component_May_Be_Bit_Aligned
(Comp
: Entity_Id
) return Boolean is
4446 -- If no component clause, then everything is fine, since the back end
4447 -- never bit-misaligns by default, even if there is a pragma Packed for
4450 if No
(Comp
) or else No
(Component_Clause
(Comp
)) then
4454 UT
:= Underlying_Type
(Etype
(Comp
));
4456 -- It is only array and record types that cause trouble
4458 if not Is_Record_Type
(UT
) and then not Is_Array_Type
(UT
) then
4461 -- If we know that we have a small (64 bits or less) record or small
4462 -- bit-packed array, then everything is fine, since the back end can
4463 -- handle these cases correctly.
4465 elsif Esize
(Comp
) <= 64
4466 and then (Is_Record_Type
(UT
) or else Is_Bit_Packed_Array
(UT
))
4470 -- Otherwise if the component is not byte aligned, we know we have the
4471 -- nasty unaligned case.
4473 elsif Normalized_First_Bit
(Comp
) /= Uint_0
4474 or else Esize
(Comp
) mod System_Storage_Unit
/= Uint_0
4478 -- If we are large and byte aligned, then OK at this level
4483 end Component_May_Be_Bit_Aligned
;
4485 ----------------------------------------
4486 -- Containing_Package_With_Ext_Axioms --
4487 ----------------------------------------
4489 function Containing_Package_With_Ext_Axioms
4490 (E
: Entity_Id
) return Entity_Id
4493 -- E is the package or generic package which is externally axiomatized
4495 if Ekind_In
(E
, E_Generic_Package
, E_Package
)
4496 and then Has_Annotate_Pragma_For_External_Axiomatization
(E
)
4501 -- If E's scope is axiomatized, E is axiomatized
4503 if Present
(Scope
(E
)) then
4505 First_Ax_Parent_Scope
: constant Entity_Id
:=
4506 Containing_Package_With_Ext_Axioms
(Scope
(E
));
4508 if Present
(First_Ax_Parent_Scope
) then
4509 return First_Ax_Parent_Scope
;
4514 -- Otherwise, if E is a package instance, it is axiomatized if the
4515 -- corresponding generic package is axiomatized.
4517 if Ekind
(E
) = E_Package
then
4519 Par
: constant Node_Id
:= Parent
(E
);
4523 if Nkind
(Par
) = N_Defining_Program_Unit_Name
then
4524 Decl
:= Parent
(Par
);
4529 if Present
(Generic_Parent
(Decl
)) then
4531 Containing_Package_With_Ext_Axioms
(Generic_Parent
(Decl
));
4537 end Containing_Package_With_Ext_Axioms
;
4539 -------------------------------
4540 -- Convert_To_Actual_Subtype --
4541 -------------------------------
4543 procedure Convert_To_Actual_Subtype
(Exp
: Entity_Id
) is
4547 Act_ST
:= Get_Actual_Subtype
(Exp
);
4549 if Act_ST
= Etype
(Exp
) then
4552 Rewrite
(Exp
, Convert_To
(Act_ST
, Relocate_Node
(Exp
)));
4553 Analyze_And_Resolve
(Exp
, Act_ST
);
4555 end Convert_To_Actual_Subtype
;
4557 -----------------------------------
4558 -- Corresponding_Runtime_Package --
4559 -----------------------------------
4561 function Corresponding_Runtime_Package
(Typ
: Entity_Id
) return RTU_Id
is
4562 function Has_One_Entry_And_No_Queue
(T
: Entity_Id
) return Boolean;
4563 -- Return True if protected type T has one entry and the maximum queue
4566 --------------------------------
4567 -- Has_One_Entry_And_No_Queue --
4568 --------------------------------
4570 function Has_One_Entry_And_No_Queue
(T
: Entity_Id
) return Boolean is
4572 Is_First
: Boolean := True;
4575 Item
:= First_Entity
(T
);
4576 while Present
(Item
) loop
4577 if Is_Entry
(Item
) then
4579 -- The protected type has more than one entry
4581 if not Is_First
then
4585 -- The queue length is not one
4587 if not Restriction_Active
(No_Entry_Queue
)
4588 and then Get_Max_Queue_Length
(Item
) /= Uint_1
4600 end Has_One_Entry_And_No_Queue
;
4604 Pkg_Id
: RTU_Id
:= RTU_Null
;
4606 -- Start of processing for Corresponding_Runtime_Package
4609 pragma Assert
(Is_Concurrent_Type
(Typ
));
4611 if Ekind
(Typ
) in Protected_Kind
then
4612 if Has_Entries
(Typ
)
4614 -- A protected type without entries that covers an interface and
4615 -- overrides the abstract routines with protected procedures is
4616 -- considered equivalent to a protected type with entries in the
4617 -- context of dispatching select statements. It is sufficient to
4618 -- check for the presence of an interface list in the declaration
4619 -- node to recognize this case.
4621 or else Present
(Interface_List
(Parent
(Typ
)))
4623 -- Protected types with interrupt handlers (when not using a
4624 -- restricted profile) are also considered equivalent to
4625 -- protected types with entries. The types which are used
4626 -- (Static_Interrupt_Protection and Dynamic_Interrupt_Protection)
4627 -- are derived from Protection_Entries.
4629 or else (Has_Attach_Handler
(Typ
) and then not Restricted_Profile
)
4630 or else Has_Interrupt_Handler
(Typ
)
4633 or else Restriction_Active
(No_Select_Statements
) = False
4634 or else not Has_One_Entry_And_No_Queue
(Typ
)
4635 or else (Has_Attach_Handler
(Typ
)
4636 and then not Restricted_Profile
)
4638 Pkg_Id
:= System_Tasking_Protected_Objects_Entries
;
4640 Pkg_Id
:= System_Tasking_Protected_Objects_Single_Entry
;
4644 Pkg_Id
:= System_Tasking_Protected_Objects
;
4649 end Corresponding_Runtime_Package
;
4651 -----------------------------------
4652 -- Current_Sem_Unit_Declarations --
4653 -----------------------------------
4655 function Current_Sem_Unit_Declarations
return List_Id
is
4656 U
: Node_Id
:= Unit
(Cunit
(Current_Sem_Unit
));
4660 -- If the current unit is a package body, locate the visible
4661 -- declarations of the package spec.
4663 if Nkind
(U
) = N_Package_Body
then
4664 U
:= Unit
(Library_Unit
(Cunit
(Current_Sem_Unit
)));
4667 if Nkind
(U
) = N_Package_Declaration
then
4668 U
:= Specification
(U
);
4669 Decls
:= Visible_Declarations
(U
);
4673 Set_Visible_Declarations
(U
, Decls
);
4677 Decls
:= Declarations
(U
);
4681 Set_Declarations
(U
, Decls
);
4686 end Current_Sem_Unit_Declarations
;
4688 -----------------------
4689 -- Duplicate_Subexpr --
4690 -----------------------
4692 function Duplicate_Subexpr
4694 Name_Req
: Boolean := False;
4695 Renaming_Req
: Boolean := False) return Node_Id
4698 Remove_Side_Effects
(Exp
, Name_Req
, Renaming_Req
);
4699 return New_Copy_Tree
(Exp
);
4700 end Duplicate_Subexpr
;
4702 ---------------------------------
4703 -- Duplicate_Subexpr_No_Checks --
4704 ---------------------------------
4706 function Duplicate_Subexpr_No_Checks
4708 Name_Req
: Boolean := False;
4709 Renaming_Req
: Boolean := False;
4710 Related_Id
: Entity_Id
:= Empty
;
4711 Is_Low_Bound
: Boolean := False;
4712 Is_High_Bound
: Boolean := False) return Node_Id
4719 Name_Req
=> Name_Req
,
4720 Renaming_Req
=> Renaming_Req
,
4721 Related_Id
=> Related_Id
,
4722 Is_Low_Bound
=> Is_Low_Bound
,
4723 Is_High_Bound
=> Is_High_Bound
);
4725 New_Exp
:= New_Copy_Tree
(Exp
);
4726 Remove_Checks
(New_Exp
);
4728 end Duplicate_Subexpr_No_Checks
;
4730 -----------------------------------
4731 -- Duplicate_Subexpr_Move_Checks --
4732 -----------------------------------
4734 function Duplicate_Subexpr_Move_Checks
4736 Name_Req
: Boolean := False;
4737 Renaming_Req
: Boolean := False) return Node_Id
4742 Remove_Side_Effects
(Exp
, Name_Req
, Renaming_Req
);
4743 New_Exp
:= New_Copy_Tree
(Exp
);
4744 Remove_Checks
(Exp
);
4746 end Duplicate_Subexpr_Move_Checks
;
4748 -------------------------
4749 -- Enclosing_Init_Proc --
4750 -------------------------
4752 function Enclosing_Init_Proc
return Entity_Id
is
4757 while Present
(S
) and then S
/= Standard_Standard
loop
4758 if Is_Init_Proc
(S
) then
4766 end Enclosing_Init_Proc
;
4768 --------------------
4769 -- Ensure_Defined --
4770 --------------------
4772 procedure Ensure_Defined
(Typ
: Entity_Id
; N
: Node_Id
) is
4776 -- An itype reference must only be created if this is a local itype, so
4777 -- that gigi can elaborate it on the proper objstack.
4779 if Is_Itype
(Typ
) and then Scope
(Typ
) = Current_Scope
then
4780 IR
:= Make_Itype_Reference
(Sloc
(N
));
4781 Set_Itype
(IR
, Typ
);
4782 Insert_Action
(N
, IR
);
4786 --------------------
4787 -- Entry_Names_OK --
4788 --------------------
4790 function Entry_Names_OK
return Boolean is
4793 not Restricted_Profile
4794 and then not Global_Discard_Names
4795 and then not Restriction_Active
(No_Implicit_Heap_Allocations
)
4796 and then not Restriction_Active
(No_Local_Allocators
);
4803 procedure Evaluate_Name
(Nam
: Node_Id
) is
4805 -- For an attribute reference or an indexed component, evaluate the
4806 -- prefix, which is itself a name, recursively, and then force the
4807 -- evaluation of all the subscripts (or attribute expressions).
4810 when N_Attribute_Reference
4811 | N_Indexed_Component
4813 Evaluate_Name
(Prefix
(Nam
));
4819 E
:= First
(Expressions
(Nam
));
4820 while Present
(E
) loop
4821 Force_Evaluation
(E
);
4823 if Is_Rewrite_Substitution
(E
) then
4825 (E
, Do_Range_Check
(Original_Node
(E
)));
4832 -- For an explicit dereference, we simply force the evaluation of
4833 -- the name expression. The dereference provides a value that is the
4834 -- address for the renamed object, and it is precisely this value
4835 -- that we want to preserve.
4837 when N_Explicit_Dereference
=>
4838 Force_Evaluation
(Prefix
(Nam
));
4840 -- For a function call, we evaluate the call
4842 when N_Function_Call
=>
4843 Force_Evaluation
(Nam
);
4845 -- For a qualified expression, we evaluate the underlying object
4846 -- name if any, otherwise we force the evaluation of the underlying
4849 when N_Qualified_Expression
=>
4850 if Is_Object_Reference
(Expression
(Nam
)) then
4851 Evaluate_Name
(Expression
(Nam
));
4853 Force_Evaluation
(Expression
(Nam
));
4856 -- For a selected component, we simply evaluate the prefix
4858 when N_Selected_Component
=>
4859 Evaluate_Name
(Prefix
(Nam
));
4861 -- For a slice, we evaluate the prefix, as for the indexed component
4862 -- case and then, if there is a range present, either directly or as
4863 -- the constraint of a discrete subtype indication, we evaluate the
4864 -- two bounds of this range.
4867 Evaluate_Name
(Prefix
(Nam
));
4868 Evaluate_Slice_Bounds
(Nam
);
4870 -- For a type conversion, the expression of the conversion must be
4871 -- the name of an object, and we simply need to evaluate this name.
4873 when N_Type_Conversion
=>
4874 Evaluate_Name
(Expression
(Nam
));
4876 -- The remaining cases are direct name, operator symbol and character
4877 -- literal. In all these cases, we do nothing, since we want to
4878 -- reevaluate each time the renamed object is used.
4885 ---------------------------
4886 -- Evaluate_Slice_Bounds --
4887 ---------------------------
4889 procedure Evaluate_Slice_Bounds
(Slice
: Node_Id
) is
4890 DR
: constant Node_Id
:= Discrete_Range
(Slice
);
4895 if Nkind
(DR
) = N_Range
then
4896 Force_Evaluation
(Low_Bound
(DR
));
4897 Force_Evaluation
(High_Bound
(DR
));
4899 elsif Nkind
(DR
) = N_Subtype_Indication
then
4900 Constr
:= Constraint
(DR
);
4902 if Nkind
(Constr
) = N_Range_Constraint
then
4903 Rexpr
:= Range_Expression
(Constr
);
4905 Force_Evaluation
(Low_Bound
(Rexpr
));
4906 Force_Evaluation
(High_Bound
(Rexpr
));
4909 end Evaluate_Slice_Bounds
;
4911 ---------------------
4912 -- Evolve_And_Then --
4913 ---------------------
4915 procedure Evolve_And_Then
(Cond
: in out Node_Id
; Cond1
: Node_Id
) is
4921 Make_And_Then
(Sloc
(Cond1
),
4923 Right_Opnd
=> Cond1
);
4925 end Evolve_And_Then
;
4927 --------------------
4928 -- Evolve_Or_Else --
4929 --------------------
4931 procedure Evolve_Or_Else
(Cond
: in out Node_Id
; Cond1
: Node_Id
) is
4937 Make_Or_Else
(Sloc
(Cond1
),
4939 Right_Opnd
=> Cond1
);
4943 -----------------------------------
4944 -- Exceptions_In_Finalization_OK --
4945 -----------------------------------
4947 function Exceptions_In_Finalization_OK
return Boolean is
4950 not (Restriction_Active
(No_Exception_Handlers
) or else
4951 Restriction_Active
(No_Exception_Propagation
) or else
4952 Restriction_Active
(No_Exceptions
));
4953 end Exceptions_In_Finalization_OK
;
4955 -----------------------------------------
4956 -- Expand_Static_Predicates_In_Choices --
4957 -----------------------------------------
4959 procedure Expand_Static_Predicates_In_Choices
(N
: Node_Id
) is
4960 pragma Assert
(Nkind_In
(N
, N_Case_Statement_Alternative
, N_Variant
));
4962 Choices
: constant List_Id
:= Discrete_Choices
(N
);
4970 Choice
:= First
(Choices
);
4971 while Present
(Choice
) loop
4972 Next_C
:= Next
(Choice
);
4974 -- Check for name of subtype with static predicate
4976 if Is_Entity_Name
(Choice
)
4977 and then Is_Type
(Entity
(Choice
))
4978 and then Has_Predicates
(Entity
(Choice
))
4980 -- Loop through entries in predicate list, converting to choices
4981 -- and inserting in the list before the current choice. Note that
4982 -- if the list is empty, corresponding to a False predicate, then
4983 -- no choices are inserted.
4985 P
:= First
(Static_Discrete_Predicate
(Entity
(Choice
)));
4986 while Present
(P
) loop
4988 -- If low bound and high bounds are equal, copy simple choice
4990 if Expr_Value
(Low_Bound
(P
)) = Expr_Value
(High_Bound
(P
)) then
4991 C
:= New_Copy
(Low_Bound
(P
));
4993 -- Otherwise copy a range
4999 -- Change Sloc to referencing choice (rather than the Sloc of
5000 -- the predicate declaration element itself).
5002 Set_Sloc
(C
, Sloc
(Choice
));
5003 Insert_Before
(Choice
, C
);
5007 -- Delete the predicated entry
5012 -- Move to next choice to check
5017 Set_Has_SP_Choice
(N
, False);
5018 end Expand_Static_Predicates_In_Choices
;
5020 ------------------------------
5021 -- Expand_Subtype_From_Expr --
5022 ------------------------------
5024 -- This function is applicable for both static and dynamic allocation of
5025 -- objects which are constrained by an initial expression. Basically it
5026 -- transforms an unconstrained subtype indication into a constrained one.
5028 -- The expression may also be transformed in certain cases in order to
5029 -- avoid multiple evaluation. In the static allocation case, the general
5034 -- is transformed into
5036 -- Val : Constrained_Subtype_Of_T := Maybe_Modified_Expr;
5038 -- Here are the main cases :
5040 -- <if Expr is a Slice>
5041 -- Val : T ([Index_Subtype (Expr)]) := Expr;
5043 -- <elsif Expr is a String Literal>
5044 -- Val : T (T'First .. T'First + Length (string literal) - 1) := Expr;
5046 -- <elsif Expr is Constrained>
5047 -- subtype T is Type_Of_Expr
5050 -- <elsif Expr is an entity_name>
5051 -- Val : T (constraints taken from Expr) := Expr;
5054 -- type Axxx is access all T;
5055 -- Rval : Axxx := Expr'ref;
5056 -- Val : T (constraints taken from Rval) := Rval.all;
5058 -- ??? note: when the Expression is allocated in the secondary stack
5059 -- we could use it directly instead of copying it by declaring
5060 -- Val : T (...) renames Rval.all
5062 procedure Expand_Subtype_From_Expr
5064 Unc_Type
: Entity_Id
;
5065 Subtype_Indic
: Node_Id
;
5067 Related_Id
: Entity_Id
:= Empty
)
5069 Loc
: constant Source_Ptr
:= Sloc
(N
);
5070 Exp_Typ
: constant Entity_Id
:= Etype
(Exp
);
5074 -- In general we cannot build the subtype if expansion is disabled,
5075 -- because internal entities may not have been defined. However, to
5076 -- avoid some cascaded errors, we try to continue when the expression is
5077 -- an array (or string), because it is safe to compute the bounds. It is
5078 -- in fact required to do so even in a generic context, because there
5079 -- may be constants that depend on the bounds of a string literal, both
5080 -- standard string types and more generally arrays of characters.
5082 -- In GNATprove mode, these extra subtypes are not needed
5084 if GNATprove_Mode
then
5088 if not Expander_Active
5089 and then (No
(Etype
(Exp
)) or else not Is_String_Type
(Etype
(Exp
)))
5094 if Nkind
(Exp
) = N_Slice
then
5096 Slice_Type
: constant Entity_Id
:= Etype
(First_Index
(Exp_Typ
));
5099 Rewrite
(Subtype_Indic
,
5100 Make_Subtype_Indication
(Loc
,
5101 Subtype_Mark
=> New_Occurrence_Of
(Unc_Type
, Loc
),
5103 Make_Index_Or_Discriminant_Constraint
(Loc
,
5104 Constraints
=> New_List
5105 (New_Occurrence_Of
(Slice_Type
, Loc
)))));
5107 -- This subtype indication may be used later for constraint checks
5108 -- we better make sure that if a variable was used as a bound of
5109 -- of the original slice, its value is frozen.
5111 Evaluate_Slice_Bounds
(Exp
);
5114 elsif Ekind
(Exp_Typ
) = E_String_Literal_Subtype
then
5115 Rewrite
(Subtype_Indic
,
5116 Make_Subtype_Indication
(Loc
,
5117 Subtype_Mark
=> New_Occurrence_Of
(Unc_Type
, Loc
),
5119 Make_Index_Or_Discriminant_Constraint
(Loc
,
5120 Constraints
=> New_List
(
5121 Make_Literal_Range
(Loc
,
5122 Literal_Typ
=> Exp_Typ
)))));
5124 -- If the type of the expression is an internally generated type it
5125 -- may not be necessary to create a new subtype. However there are two
5126 -- exceptions: references to the current instances, and aliased array
5127 -- object declarations for which the back end has to create a template.
5129 elsif Is_Constrained
(Exp_Typ
)
5130 and then not Is_Class_Wide_Type
(Unc_Type
)
5132 (Nkind
(N
) /= N_Object_Declaration
5133 or else not Is_Entity_Name
(Expression
(N
))
5134 or else not Comes_From_Source
(Entity
(Expression
(N
)))
5135 or else not Is_Array_Type
(Exp_Typ
)
5136 or else not Aliased_Present
(N
))
5138 if Is_Itype
(Exp_Typ
) then
5140 -- Within an initialization procedure, a selected component
5141 -- denotes a component of the enclosing record, and it appears as
5142 -- an actual in a call to its own initialization procedure. If
5143 -- this component depends on the outer discriminant, we must
5144 -- generate the proper actual subtype for it.
5146 if Nkind
(Exp
) = N_Selected_Component
5147 and then Within_Init_Proc
5150 Decl
: constant Node_Id
:=
5151 Build_Actual_Subtype_Of_Component
(Exp_Typ
, Exp
);
5153 if Present
(Decl
) then
5154 Insert_Action
(N
, Decl
);
5155 T
:= Defining_Identifier
(Decl
);
5161 -- No need to generate a new subtype
5168 T
:= Make_Temporary
(Loc
, 'T');
5171 Make_Subtype_Declaration
(Loc
,
5172 Defining_Identifier
=> T
,
5173 Subtype_Indication
=> New_Occurrence_Of
(Exp_Typ
, Loc
)));
5175 -- This type is marked as an itype even though it has an explicit
5176 -- declaration since otherwise Is_Generic_Actual_Type can get
5177 -- set, resulting in the generation of spurious errors. (See
5178 -- sem_ch8.Analyze_Package_Renaming and sem_type.covers)
5181 Set_Associated_Node_For_Itype
(T
, Exp
);
5184 Rewrite
(Subtype_Indic
, New_Occurrence_Of
(T
, Loc
));
5186 -- Nothing needs to be done for private types with unknown discriminants
5187 -- if the underlying type is not an unconstrained composite type or it
5188 -- is an unchecked union.
5190 elsif Is_Private_Type
(Unc_Type
)
5191 and then Has_Unknown_Discriminants
(Unc_Type
)
5192 and then (not Is_Composite_Type
(Underlying_Type
(Unc_Type
))
5193 or else Is_Constrained
(Underlying_Type
(Unc_Type
))
5194 or else Is_Unchecked_Union
(Underlying_Type
(Unc_Type
)))
5198 -- Case of derived type with unknown discriminants where the parent type
5199 -- also has unknown discriminants.
5201 elsif Is_Record_Type
(Unc_Type
)
5202 and then not Is_Class_Wide_Type
(Unc_Type
)
5203 and then Has_Unknown_Discriminants
(Unc_Type
)
5204 and then Has_Unknown_Discriminants
(Underlying_Type
(Unc_Type
))
5206 -- Nothing to be done if no underlying record view available
5208 -- If this is a limited type derived from a type with unknown
5209 -- discriminants, do not expand either, so that subsequent expansion
5210 -- of the call can add build-in-place parameters to call.
5212 if No
(Underlying_Record_View
(Unc_Type
))
5213 or else Is_Limited_Type
(Unc_Type
)
5217 -- Otherwise use the Underlying_Record_View to create the proper
5218 -- constrained subtype for an object of a derived type with unknown
5222 Remove_Side_Effects
(Exp
);
5223 Rewrite
(Subtype_Indic
,
5224 Make_Subtype_From_Expr
(Exp
, Underlying_Record_View
(Unc_Type
)));
5227 -- Renamings of class-wide interface types require no equivalent
5228 -- constrained type declarations because we only need to reference
5229 -- the tag component associated with the interface. The same is
5230 -- presumably true for class-wide types in general, so this test
5231 -- is broadened to include all class-wide renamings, which also
5232 -- avoids cases of unbounded recursion in Remove_Side_Effects.
5233 -- (Is this really correct, or are there some cases of class-wide
5234 -- renamings that require action in this procedure???)
5237 and then Nkind
(N
) = N_Object_Renaming_Declaration
5238 and then Is_Class_Wide_Type
(Unc_Type
)
5242 -- In Ada 95 nothing to be done if the type of the expression is limited
5243 -- because in this case the expression cannot be copied, and its use can
5244 -- only be by reference.
5246 -- In Ada 2005 the context can be an object declaration whose expression
5247 -- is a function that returns in place. If the nominal subtype has
5248 -- unknown discriminants, the call still provides constraints on the
5249 -- object, and we have to create an actual subtype from it.
5251 -- If the type is class-wide, the expression is dynamically tagged and
5252 -- we do not create an actual subtype either. Ditto for an interface.
5253 -- For now this applies only if the type is immutably limited, and the
5254 -- function being called is build-in-place. This will have to be revised
5255 -- when build-in-place functions are generalized to other types.
5257 elsif Is_Limited_View
(Exp_Typ
)
5259 (Is_Class_Wide_Type
(Exp_Typ
)
5260 or else Is_Interface
(Exp_Typ
)
5261 or else not Has_Unknown_Discriminants
(Exp_Typ
)
5262 or else not Is_Composite_Type
(Unc_Type
))
5266 -- For limited objects initialized with build in place function calls,
5267 -- nothing to be done; otherwise we prematurely introduce an N_Reference
5268 -- node in the expression initializing the object, which breaks the
5269 -- circuitry that detects and adds the additional arguments to the
5272 elsif Is_Build_In_Place_Function_Call
(Exp
) then
5276 Remove_Side_Effects
(Exp
);
5277 Rewrite
(Subtype_Indic
,
5278 Make_Subtype_From_Expr
(Exp
, Unc_Type
, Related_Id
));
5280 end Expand_Subtype_From_Expr
;
5282 ---------------------------------------------
5283 -- Expression_Contains_Primitives_Calls_Of --
5284 ---------------------------------------------
5286 function Expression_Contains_Primitives_Calls_Of
5288 Typ
: Entity_Id
) return Boolean
5290 U_Typ
: constant Entity_Id
:= Unique_Entity
(Typ
);
5292 Calls_OK
: Boolean := False;
5293 -- This flag is set to True when expression Expr contains at least one
5294 -- call to a nondispatching primitive function of Typ.
5296 function Search_Primitive_Calls
(N
: Node_Id
) return Traverse_Result
;
5297 -- Search for nondispatching calls to primitive functions of type Typ
5299 ----------------------------
5300 -- Search_Primitive_Calls --
5301 ----------------------------
5303 function Search_Primitive_Calls
(N
: Node_Id
) return Traverse_Result
is
5304 Disp_Typ
: Entity_Id
;
5308 -- Detect a function call that could denote a nondispatching
5309 -- primitive of the input type.
5311 if Nkind
(N
) = N_Function_Call
5312 and then Is_Entity_Name
(Name
(N
))
5314 Subp
:= Entity
(Name
(N
));
5316 -- Do not consider function calls with a controlling argument, as
5317 -- those are always dispatching calls.
5319 if Is_Dispatching_Operation
(Subp
)
5320 and then No
(Controlling_Argument
(N
))
5322 Disp_Typ
:= Find_Dispatching_Type
(Subp
);
5324 -- To qualify as a suitable primitive, the dispatching type of
5325 -- the function must be the input type.
5327 if Present
(Disp_Typ
)
5328 and then Unique_Entity
(Disp_Typ
) = U_Typ
5332 -- There is no need to continue the traversal, as one such
5341 end Search_Primitive_Calls
;
5343 procedure Search_Calls
is new Traverse_Proc
(Search_Primitive_Calls
);
5345 -- Start of processing for Expression_Contains_Primitives_Calls_Of_Type
5348 Search_Calls
(Expr
);
5350 end Expression_Contains_Primitives_Calls_Of
;
5352 ----------------------
5353 -- Finalize_Address --
5354 ----------------------
5356 function Finalize_Address
(Typ
: Entity_Id
) return Entity_Id
is
5357 Utyp
: Entity_Id
:= Typ
;
5360 -- Handle protected class-wide or task class-wide types
5362 if Is_Class_Wide_Type
(Utyp
) then
5363 if Is_Concurrent_Type
(Root_Type
(Utyp
)) then
5364 Utyp
:= Root_Type
(Utyp
);
5366 elsif Is_Private_Type
(Root_Type
(Utyp
))
5367 and then Present
(Full_View
(Root_Type
(Utyp
)))
5368 and then Is_Concurrent_Type
(Full_View
(Root_Type
(Utyp
)))
5370 Utyp
:= Full_View
(Root_Type
(Utyp
));
5374 -- Handle private types
5376 if Is_Private_Type
(Utyp
) and then Present
(Full_View
(Utyp
)) then
5377 Utyp
:= Full_View
(Utyp
);
5380 -- Handle protected and task types
5382 if Is_Concurrent_Type
(Utyp
)
5383 and then Present
(Corresponding_Record_Type
(Utyp
))
5385 Utyp
:= Corresponding_Record_Type
(Utyp
);
5388 Utyp
:= Underlying_Type
(Base_Type
(Utyp
));
5390 -- Deal with untagged derivation of private views. If the parent is
5391 -- now known to be protected, the finalization routine is the one
5392 -- defined on the corresponding record of the ancestor (corresponding
5393 -- records do not automatically inherit operations, but maybe they
5396 if Is_Untagged_Derivation
(Typ
) then
5397 if Is_Protected_Type
(Typ
) then
5398 Utyp
:= Corresponding_Record_Type
(Root_Type
(Base_Type
(Typ
)));
5401 Utyp
:= Underlying_Type
(Root_Type
(Base_Type
(Typ
)));
5403 if Is_Protected_Type
(Utyp
) then
5404 Utyp
:= Corresponding_Record_Type
(Utyp
);
5409 -- If the underlying_type is a subtype, we are dealing with the
5410 -- completion of a private type. We need to access the base type and
5411 -- generate a conversion to it.
5413 if Utyp
/= Base_Type
(Utyp
) then
5414 pragma Assert
(Is_Private_Type
(Typ
));
5416 Utyp
:= Base_Type
(Utyp
);
5419 -- When dealing with an internally built full view for a type with
5420 -- unknown discriminants, use the original record type.
5422 if Is_Underlying_Record_View
(Utyp
) then
5423 Utyp
:= Etype
(Utyp
);
5426 return TSS
(Utyp
, TSS_Finalize_Address
);
5427 end Finalize_Address
;
5429 ------------------------
5430 -- Find_Interface_ADT --
5431 ------------------------
5433 function Find_Interface_ADT
5435 Iface
: Entity_Id
) return Elmt_Id
5438 Typ
: Entity_Id
:= T
;
5441 pragma Assert
(Is_Interface
(Iface
));
5443 -- Handle private types
5445 if Has_Private_Declaration
(Typ
) and then Present
(Full_View
(Typ
)) then
5446 Typ
:= Full_View
(Typ
);
5449 -- Handle access types
5451 if Is_Access_Type
(Typ
) then
5452 Typ
:= Designated_Type
(Typ
);
5455 -- Handle task and protected types implementing interfaces
5457 if Is_Concurrent_Type
(Typ
) then
5458 Typ
:= Corresponding_Record_Type
(Typ
);
5462 (not Is_Class_Wide_Type
(Typ
)
5463 and then Ekind
(Typ
) /= E_Incomplete_Type
);
5465 if Is_Ancestor
(Iface
, Typ
, Use_Full_View
=> True) then
5466 return First_Elmt
(Access_Disp_Table
(Typ
));
5469 ADT
:= Next_Elmt
(Next_Elmt
(First_Elmt
(Access_Disp_Table
(Typ
))));
5471 and then Present
(Related_Type
(Node
(ADT
)))
5472 and then Related_Type
(Node
(ADT
)) /= Iface
5473 and then not Is_Ancestor
(Iface
, Related_Type
(Node
(ADT
)),
5474 Use_Full_View
=> True)
5479 pragma Assert
(Present
(Related_Type
(Node
(ADT
))));
5482 end Find_Interface_ADT
;
5484 ------------------------
5485 -- Find_Interface_Tag --
5486 ------------------------
5488 function Find_Interface_Tag
5490 Iface
: Entity_Id
) return Entity_Id
5492 AI_Tag
: Entity_Id
:= Empty
;
5493 Found
: Boolean := False;
5494 Typ
: Entity_Id
:= T
;
5496 procedure Find_Tag
(Typ
: Entity_Id
);
5497 -- Internal subprogram used to recursively climb to the ancestors
5503 procedure Find_Tag
(Typ
: Entity_Id
) is
5508 -- This routine does not handle the case in which the interface is an
5509 -- ancestor of Typ. That case is handled by the enclosing subprogram.
5511 pragma Assert
(Typ
/= Iface
);
5513 -- Climb to the root type handling private types
5515 if Present
(Full_View
(Etype
(Typ
))) then
5516 if Full_View
(Etype
(Typ
)) /= Typ
then
5517 Find_Tag
(Full_View
(Etype
(Typ
)));
5520 elsif Etype
(Typ
) /= Typ
then
5521 Find_Tag
(Etype
(Typ
));
5524 -- Traverse the list of interfaces implemented by the type
5527 and then Present
(Interfaces
(Typ
))
5528 and then not (Is_Empty_Elmt_List
(Interfaces
(Typ
)))
5530 -- Skip the tag associated with the primary table
5532 pragma Assert
(Etype
(First_Tag_Component
(Typ
)) = RTE
(RE_Tag
));
5533 AI_Tag
:= Next_Tag_Component
(First_Tag_Component
(Typ
));
5534 pragma Assert
(Present
(AI_Tag
));
5536 AI_Elmt
:= First_Elmt
(Interfaces
(Typ
));
5537 while Present
(AI_Elmt
) loop
5538 AI
:= Node
(AI_Elmt
);
5541 or else Is_Ancestor
(Iface
, AI
, Use_Full_View
=> True)
5547 AI_Tag
:= Next_Tag_Component
(AI_Tag
);
5548 Next_Elmt
(AI_Elmt
);
5553 -- Start of processing for Find_Interface_Tag
5556 pragma Assert
(Is_Interface
(Iface
));
5558 -- Handle access types
5560 if Is_Access_Type
(Typ
) then
5561 Typ
:= Designated_Type
(Typ
);
5564 -- Handle class-wide types
5566 if Is_Class_Wide_Type
(Typ
) then
5567 Typ
:= Root_Type
(Typ
);
5570 -- Handle private types
5572 if Has_Private_Declaration
(Typ
) and then Present
(Full_View
(Typ
)) then
5573 Typ
:= Full_View
(Typ
);
5576 -- Handle entities from the limited view
5578 if Ekind
(Typ
) = E_Incomplete_Type
then
5579 pragma Assert
(Present
(Non_Limited_View
(Typ
)));
5580 Typ
:= Non_Limited_View
(Typ
);
5583 -- Handle task and protected types implementing interfaces
5585 if Is_Concurrent_Type
(Typ
) then
5586 Typ
:= Corresponding_Record_Type
(Typ
);
5589 -- If the interface is an ancestor of the type, then it shared the
5590 -- primary dispatch table.
5592 if Is_Ancestor
(Iface
, Typ
, Use_Full_View
=> True) then
5593 pragma Assert
(Etype
(First_Tag_Component
(Typ
)) = RTE
(RE_Tag
));
5594 return First_Tag_Component
(Typ
);
5596 -- Otherwise we need to search for its associated tag component
5600 pragma Assert
(Found
);
5603 end Find_Interface_Tag
;
5605 ---------------------------
5606 -- Find_Optional_Prim_Op --
5607 ---------------------------
5609 function Find_Optional_Prim_Op
5610 (T
: Entity_Id
; Name
: Name_Id
) return Entity_Id
5613 Typ
: Entity_Id
:= T
;
5617 if Is_Class_Wide_Type
(Typ
) then
5618 Typ
:= Root_Type
(Typ
);
5621 Typ
:= Underlying_Type
(Typ
);
5623 -- Loop through primitive operations
5625 Prim
:= First_Elmt
(Primitive_Operations
(Typ
));
5626 while Present
(Prim
) loop
5629 -- We can retrieve primitive operations by name if it is an internal
5630 -- name. For equality we must check that both of its operands have
5631 -- the same type, to avoid confusion with user-defined equalities
5632 -- than may have a non-symmetric signature.
5634 exit when Chars
(Op
) = Name
5637 or else Etype
(First_Formal
(Op
)) = Etype
(Last_Formal
(Op
)));
5642 return Node
(Prim
); -- Empty if not found
5643 end Find_Optional_Prim_Op
;
5645 ---------------------------
5646 -- Find_Optional_Prim_Op --
5647 ---------------------------
5649 function Find_Optional_Prim_Op
5651 Name
: TSS_Name_Type
) return Entity_Id
5653 Inher_Op
: Entity_Id
:= Empty
;
5654 Own_Op
: Entity_Id
:= Empty
;
5655 Prim_Elmt
: Elmt_Id
;
5656 Prim_Id
: Entity_Id
;
5657 Typ
: Entity_Id
:= T
;
5660 if Is_Class_Wide_Type
(Typ
) then
5661 Typ
:= Root_Type
(Typ
);
5664 Typ
:= Underlying_Type
(Typ
);
5666 -- This search is based on the assertion that the dispatching version
5667 -- of the TSS routine always precedes the real primitive.
5669 Prim_Elmt
:= First_Elmt
(Primitive_Operations
(Typ
));
5670 while Present
(Prim_Elmt
) loop
5671 Prim_Id
:= Node
(Prim_Elmt
);
5673 if Is_TSS
(Prim_Id
, Name
) then
5674 if Present
(Alias
(Prim_Id
)) then
5675 Inher_Op
:= Prim_Id
;
5681 Next_Elmt
(Prim_Elmt
);
5684 if Present
(Own_Op
) then
5686 elsif Present
(Inher_Op
) then
5691 end Find_Optional_Prim_Op
;
5697 function Find_Prim_Op
5698 (T
: Entity_Id
; Name
: Name_Id
) return Entity_Id
5700 Result
: constant Entity_Id
:= Find_Optional_Prim_Op
(T
, Name
);
5703 raise Program_Error
;
5713 function Find_Prim_Op
5715 Name
: TSS_Name_Type
) return Entity_Id
5717 Result
: constant Entity_Id
:= Find_Optional_Prim_Op
(T
, Name
);
5720 raise Program_Error
;
5726 ----------------------------
5727 -- Find_Protection_Object --
5728 ----------------------------
5730 function Find_Protection_Object
(Scop
: Entity_Id
) return Entity_Id
is
5735 while Present
(S
) loop
5736 if Ekind_In
(S
, E_Entry
, E_Entry_Family
, E_Function
, E_Procedure
)
5737 and then Present
(Protection_Object
(S
))
5739 return Protection_Object
(S
);
5745 -- If we do not find a Protection object in the scope chain, then
5746 -- something has gone wrong, most likely the object was never created.
5748 raise Program_Error
;
5749 end Find_Protection_Object
;
5751 --------------------------
5752 -- Find_Protection_Type --
5753 --------------------------
5755 function Find_Protection_Type
(Conc_Typ
: Entity_Id
) return Entity_Id
is
5757 Typ
: Entity_Id
:= Conc_Typ
;
5760 if Is_Concurrent_Type
(Typ
) then
5761 Typ
:= Corresponding_Record_Type
(Typ
);
5764 -- Since restriction violations are not considered serious errors, the
5765 -- expander remains active, but may leave the corresponding record type
5766 -- malformed. In such cases, component _object is not available so do
5769 if not Analyzed
(Typ
) then
5773 Comp
:= First_Component
(Typ
);
5774 while Present
(Comp
) loop
5775 if Chars
(Comp
) = Name_uObject
then
5776 return Base_Type
(Etype
(Comp
));
5779 Next_Component
(Comp
);
5782 -- The corresponding record of a protected type should always have an
5785 raise Program_Error
;
5786 end Find_Protection_Type
;
5788 -----------------------
5789 -- Find_Hook_Context --
5790 -----------------------
5792 function Find_Hook_Context
(N
: Node_Id
) return Node_Id
is
5796 Wrapped_Node
: Node_Id
;
5797 -- Note: if we are in a transient scope, we want to reuse it as
5798 -- the context for actions insertion, if possible. But if N is itself
5799 -- part of the stored actions for the current transient scope,
5800 -- then we need to insert at the appropriate (inner) location in
5801 -- the not as an action on Node_To_Be_Wrapped.
5803 In_Cond_Expr
: constant Boolean := Within_Case_Or_If_Expression
(N
);
5806 -- When the node is inside a case/if expression, the lifetime of any
5807 -- temporary controlled object is extended. Find a suitable insertion
5808 -- node by locating the topmost case or if expressions.
5810 if In_Cond_Expr
then
5813 while Present
(Par
) loop
5814 if Nkind_In
(Original_Node
(Par
), N_Case_Expression
,
5819 -- Prevent the search from going too far
5821 elsif Is_Body_Or_Package_Declaration
(Par
) then
5825 Par
:= Parent
(Par
);
5828 -- The topmost case or if expression is now recovered, but it may
5829 -- still not be the correct place to add generated code. Climb to
5830 -- find a parent that is part of a declarative or statement list,
5831 -- and is not a list of actuals in a call.
5834 while Present
(Par
) loop
5835 if Is_List_Member
(Par
)
5836 and then not Nkind_In
(Par
, N_Component_Association
,
5837 N_Discriminant_Association
,
5838 N_Parameter_Association
,
5839 N_Pragma_Argument_Association
)
5840 and then not Nkind_In
(Parent
(Par
), N_Function_Call
,
5841 N_Procedure_Call_Statement
,
5842 N_Entry_Call_Statement
)
5847 -- Prevent the search from going too far
5849 elsif Is_Body_Or_Package_Declaration
(Par
) then
5853 Par
:= Parent
(Par
);
5860 while Present
(Par
) loop
5862 -- Keep climbing past various operators
5864 if Nkind
(Parent
(Par
)) in N_Op
5865 or else Nkind_In
(Parent
(Par
), N_And_Then
, N_Or_Else
)
5867 Par
:= Parent
(Par
);
5875 -- The node may be located in a pragma in which case return the
5878 -- pragma Precondition (... and then Ctrl_Func_Call ...);
5880 -- Similar case occurs when the node is related to an object
5881 -- declaration or assignment:
5883 -- Obj [: Some_Typ] := ... and then Ctrl_Func_Call ...;
5885 -- Another case to consider is when the node is part of a return
5888 -- return ... and then Ctrl_Func_Call ...;
5890 -- Another case is when the node acts as a formal in a procedure
5893 -- Proc (... and then Ctrl_Func_Call ...);
5895 if Scope_Is_Transient
then
5896 Wrapped_Node
:= Node_To_Be_Wrapped
;
5898 Wrapped_Node
:= Empty
;
5901 while Present
(Par
) loop
5902 if Par
= Wrapped_Node
5903 or else Nkind_In
(Par
, N_Assignment_Statement
,
5904 N_Object_Declaration
,
5906 N_Procedure_Call_Statement
,
5907 N_Simple_Return_Statement
)
5911 -- Prevent the search from going too far
5913 elsif Is_Body_Or_Package_Declaration
(Par
) then
5917 Par
:= Parent
(Par
);
5920 -- Return the topmost short circuit operator
5924 end Find_Hook_Context
;
5926 ------------------------------
5927 -- Following_Address_Clause --
5928 ------------------------------
5930 function Following_Address_Clause
(D
: Node_Id
) return Node_Id
is
5931 Id
: constant Entity_Id
:= Defining_Identifier
(D
);
5935 function Check_Decls
(D
: Node_Id
) return Node_Id
;
5936 -- This internal function differs from the main function in that it
5937 -- gets called to deal with a following package private part, and
5938 -- it checks declarations starting with D (the main function checks
5939 -- declarations following D). If D is Empty, then Empty is returned.
5945 function Check_Decls
(D
: Node_Id
) return Node_Id
is
5950 while Present
(Decl
) loop
5951 if Nkind
(Decl
) = N_At_Clause
5952 and then Chars
(Identifier
(Decl
)) = Chars
(Id
)
5956 elsif Nkind
(Decl
) = N_Attribute_Definition_Clause
5957 and then Chars
(Decl
) = Name_Address
5958 and then Chars
(Name
(Decl
)) = Chars
(Id
)
5966 -- Otherwise not found, return Empty
5971 -- Start of processing for Following_Address_Clause
5974 -- If parser detected no address clause for the identifier in question,
5975 -- then the answer is a quick NO, without the need for a search.
5977 if not Get_Name_Table_Boolean1
(Chars
(Id
)) then
5981 -- Otherwise search current declarative unit
5983 Result
:= Check_Decls
(Next
(D
));
5985 if Present
(Result
) then
5989 -- Check for possible package private part following
5993 if Nkind
(Par
) = N_Package_Specification
5994 and then Visible_Declarations
(Par
) = List_Containing
(D
)
5995 and then Present
(Private_Declarations
(Par
))
5997 -- Private part present, check declarations there
5999 return Check_Decls
(First
(Private_Declarations
(Par
)));
6002 -- No private part, clause not found, return Empty
6006 end Following_Address_Clause
;
6008 ----------------------
6009 -- Force_Evaluation --
6010 ----------------------
6012 procedure Force_Evaluation
6014 Name_Req
: Boolean := False;
6015 Related_Id
: Entity_Id
:= Empty
;
6016 Is_Low_Bound
: Boolean := False;
6017 Is_High_Bound
: Boolean := False;
6018 Mode
: Force_Evaluation_Mode
:= Relaxed
)
6023 Name_Req
=> Name_Req
,
6024 Variable_Ref
=> True,
6025 Renaming_Req
=> False,
6026 Related_Id
=> Related_Id
,
6027 Is_Low_Bound
=> Is_Low_Bound
,
6028 Is_High_Bound
=> Is_High_Bound
,
6029 Check_Side_Effects
=>
6030 Is_Static_Expression
(Exp
)
6031 or else Mode
= Relaxed
);
6032 end Force_Evaluation
;
6034 ---------------------------------
6035 -- Fully_Qualified_Name_String --
6036 ---------------------------------
6038 function Fully_Qualified_Name_String
6040 Append_NUL
: Boolean := True) return String_Id
6042 procedure Internal_Full_Qualified_Name
(E
: Entity_Id
);
6043 -- Compute recursively the qualified name without NUL at the end, adding
6044 -- it to the currently started string being generated
6046 ----------------------------------
6047 -- Internal_Full_Qualified_Name --
6048 ----------------------------------
6050 procedure Internal_Full_Qualified_Name
(E
: Entity_Id
) is
6054 -- Deal properly with child units
6056 if Nkind
(E
) = N_Defining_Program_Unit_Name
then
6057 Ent
:= Defining_Identifier
(E
);
6062 -- Compute qualification recursively (only "Standard" has no scope)
6064 if Present
(Scope
(Scope
(Ent
))) then
6065 Internal_Full_Qualified_Name
(Scope
(Ent
));
6066 Store_String_Char
(Get_Char_Code
('.'));
6069 -- Every entity should have a name except some expanded blocks
6070 -- don't bother about those.
6072 if Chars
(Ent
) = No_Name
then
6076 -- Generates the entity name in upper case
6078 Get_Decoded_Name_String
(Chars
(Ent
));
6080 Store_String_Chars
(Name_Buffer
(1 .. Name_Len
));
6082 end Internal_Full_Qualified_Name
;
6084 -- Start of processing for Full_Qualified_Name
6088 Internal_Full_Qualified_Name
(E
);
6091 Store_String_Char
(Get_Char_Code
(ASCII
.NUL
));
6095 end Fully_Qualified_Name_String
;
6097 ------------------------
6098 -- Generate_Poll_Call --
6099 ------------------------
6101 procedure Generate_Poll_Call
(N
: Node_Id
) is
6103 -- No poll call if polling not active
6105 if not Polling_Required
then
6108 -- Otherwise generate require poll call
6111 Insert_Before_And_Analyze
(N
,
6112 Make_Procedure_Call_Statement
(Sloc
(N
),
6113 Name
=> New_Occurrence_Of
(RTE
(RE_Poll
), Sloc
(N
))));
6115 end Generate_Poll_Call
;
6117 ---------------------------------
6118 -- Get_Current_Value_Condition --
6119 ---------------------------------
6121 -- Note: the implementation of this procedure is very closely tied to the
6122 -- implementation of Set_Current_Value_Condition. In the Get procedure, we
6123 -- interpret Current_Value fields set by the Set procedure, so the two
6124 -- procedures need to be closely coordinated.
6126 procedure Get_Current_Value_Condition
6131 Loc
: constant Source_Ptr
:= Sloc
(Var
);
6132 Ent
: constant Entity_Id
:= Entity
(Var
);
6134 procedure Process_Current_Value_Condition
6137 -- N is an expression which holds either True (S = True) or False (S =
6138 -- False) in the condition. This procedure digs out the expression and
6139 -- if it refers to Ent, sets Op and Val appropriately.
6141 -------------------------------------
6142 -- Process_Current_Value_Condition --
6143 -------------------------------------
6145 procedure Process_Current_Value_Condition
6150 Prev_Cond
: Node_Id
;
6160 -- Deal with NOT operators, inverting sense
6162 while Nkind
(Cond
) = N_Op_Not
loop
6163 Cond
:= Right_Opnd
(Cond
);
6167 -- Deal with conversions, qualifications, and expressions with
6170 while Nkind_In
(Cond
,
6172 N_Qualified_Expression
,
6173 N_Expression_With_Actions
)
6175 Cond
:= Expression
(Cond
);
6178 exit when Cond
= Prev_Cond
;
6181 -- Deal with AND THEN and AND cases
6183 if Nkind_In
(Cond
, N_And_Then
, N_Op_And
) then
6185 -- Don't ever try to invert a condition that is of the form of an
6186 -- AND or AND THEN (since we are not doing sufficiently general
6187 -- processing to allow this).
6189 if Sens
= False then
6195 -- Recursively process AND and AND THEN branches
6197 Process_Current_Value_Condition
(Left_Opnd
(Cond
), True);
6199 if Op
/= N_Empty
then
6203 Process_Current_Value_Condition
(Right_Opnd
(Cond
), True);
6206 -- Case of relational operator
6208 elsif Nkind
(Cond
) in N_Op_Compare
then
6211 -- Invert sense of test if inverted test
6213 if Sens
= False then
6215 when N_Op_Eq
=> Op
:= N_Op_Ne
;
6216 when N_Op_Ne
=> Op
:= N_Op_Eq
;
6217 when N_Op_Lt
=> Op
:= N_Op_Ge
;
6218 when N_Op_Gt
=> Op
:= N_Op_Le
;
6219 when N_Op_Le
=> Op
:= N_Op_Gt
;
6220 when N_Op_Ge
=> Op
:= N_Op_Lt
;
6221 when others => raise Program_Error
;
6225 -- Case of entity op value
6227 if Is_Entity_Name
(Left_Opnd
(Cond
))
6228 and then Ent
= Entity
(Left_Opnd
(Cond
))
6229 and then Compile_Time_Known_Value
(Right_Opnd
(Cond
))
6231 Val
:= Right_Opnd
(Cond
);
6233 -- Case of value op entity
6235 elsif Is_Entity_Name
(Right_Opnd
(Cond
))
6236 and then Ent
= Entity
(Right_Opnd
(Cond
))
6237 and then Compile_Time_Known_Value
(Left_Opnd
(Cond
))
6239 Val
:= Left_Opnd
(Cond
);
6241 -- We are effectively swapping operands
6244 when N_Op_Eq
=> null;
6245 when N_Op_Ne
=> null;
6246 when N_Op_Lt
=> Op
:= N_Op_Gt
;
6247 when N_Op_Gt
=> Op
:= N_Op_Lt
;
6248 when N_Op_Le
=> Op
:= N_Op_Ge
;
6249 when N_Op_Ge
=> Op
:= N_Op_Le
;
6250 when others => raise Program_Error
;
6259 elsif Nkind_In
(Cond
,
6261 N_Qualified_Expression
,
6262 N_Expression_With_Actions
)
6264 Cond
:= Expression
(Cond
);
6266 -- Case of Boolean variable reference, return as though the
6267 -- reference had said var = True.
6270 if Is_Entity_Name
(Cond
) and then Ent
= Entity
(Cond
) then
6271 Val
:= New_Occurrence_Of
(Standard_True
, Sloc
(Cond
));
6273 if Sens
= False then
6280 end Process_Current_Value_Condition
;
6282 -- Start of processing for Get_Current_Value_Condition
6288 -- Immediate return, nothing doing, if this is not an object
6290 if Ekind
(Ent
) not in Object_Kind
then
6294 -- Otherwise examine current value
6297 CV
: constant Node_Id
:= Current_Value
(Ent
);
6302 -- If statement. Condition is known true in THEN section, known False
6303 -- in any ELSIF or ELSE part, and unknown outside the IF statement.
6305 if Nkind
(CV
) = N_If_Statement
then
6307 -- Before start of IF statement
6309 if Loc
< Sloc
(CV
) then
6312 -- After end of IF statement
6314 elsif Loc
>= Sloc
(CV
) + Text_Ptr
(UI_To_Int
(End_Span
(CV
))) then
6318 -- At this stage we know that we are within the IF statement, but
6319 -- unfortunately, the tree does not record the SLOC of the ELSE so
6320 -- we cannot use a simple SLOC comparison to distinguish between
6321 -- the then/else statements, so we have to climb the tree.
6328 while Parent
(N
) /= CV
loop
6331 -- If we fall off the top of the tree, then that's odd, but
6332 -- perhaps it could occur in some error situation, and the
6333 -- safest response is simply to assume that the outcome of
6334 -- the condition is unknown. No point in bombing during an
6335 -- attempt to optimize things.
6342 -- Now we have N pointing to a node whose parent is the IF
6343 -- statement in question, so now we can tell if we are within
6344 -- the THEN statements.
6346 if Is_List_Member
(N
)
6347 and then List_Containing
(N
) = Then_Statements
(CV
)
6351 -- If the variable reference does not come from source, we
6352 -- cannot reliably tell whether it appears in the else part.
6353 -- In particular, if it appears in generated code for a node
6354 -- that requires finalization, it may be attached to a list
6355 -- that has not been yet inserted into the code. For now,
6356 -- treat it as unknown.
6358 elsif not Comes_From_Source
(N
) then
6361 -- Otherwise we must be in ELSIF or ELSE part
6368 -- ELSIF part. Condition is known true within the referenced
6369 -- ELSIF, known False in any subsequent ELSIF or ELSE part,
6370 -- and unknown before the ELSE part or after the IF statement.
6372 elsif Nkind
(CV
) = N_Elsif_Part
then
6374 -- if the Elsif_Part had condition_actions, the elsif has been
6375 -- rewritten as a nested if, and the original elsif_part is
6376 -- detached from the tree, so there is no way to obtain useful
6377 -- information on the current value of the variable.
6378 -- Can this be improved ???
6380 if No
(Parent
(CV
)) then
6386 -- If the tree has been otherwise rewritten there is nothing
6387 -- else to be done either.
6389 if Nkind
(Stm
) /= N_If_Statement
then
6393 -- Before start of ELSIF part
6395 if Loc
< Sloc
(CV
) then
6398 -- After end of IF statement
6400 elsif Loc
>= Sloc
(Stm
) +
6401 Text_Ptr
(UI_To_Int
(End_Span
(Stm
)))
6406 -- Again we lack the SLOC of the ELSE, so we need to climb the
6407 -- tree to see if we are within the ELSIF part in question.
6414 while Parent
(N
) /= Stm
loop
6417 -- If we fall off the top of the tree, then that's odd, but
6418 -- perhaps it could occur in some error situation, and the
6419 -- safest response is simply to assume that the outcome of
6420 -- the condition is unknown. No point in bombing during an
6421 -- attempt to optimize things.
6428 -- Now we have N pointing to a node whose parent is the IF
6429 -- statement in question, so see if is the ELSIF part we want.
6430 -- the THEN statements.
6435 -- Otherwise we must be in subsequent ELSIF or ELSE part
6442 -- Iteration scheme of while loop. The condition is known to be
6443 -- true within the body of the loop.
6445 elsif Nkind
(CV
) = N_Iteration_Scheme
then
6447 Loop_Stmt
: constant Node_Id
:= Parent
(CV
);
6450 -- Before start of body of loop
6452 if Loc
< Sloc
(Loop_Stmt
) then
6455 -- After end of LOOP statement
6457 elsif Loc
>= Sloc
(End_Label
(Loop_Stmt
)) then
6460 -- We are within the body of the loop
6467 -- All other cases of Current_Value settings
6473 -- If we fall through here, then we have a reportable condition, Sens
6474 -- is True if the condition is true and False if it needs inverting.
6476 Process_Current_Value_Condition
(Condition
(CV
), Sens
);
6478 end Get_Current_Value_Condition
;
6480 ---------------------
6481 -- Get_Stream_Size --
6482 ---------------------
6484 function Get_Stream_Size
(E
: Entity_Id
) return Uint
is
6486 -- If we have a Stream_Size clause for this type use it
6488 if Has_Stream_Size_Clause
(E
) then
6489 return Static_Integer
(Expression
(Stream_Size_Clause
(E
)));
6491 -- Otherwise the Stream_Size if the size of the type
6496 end Get_Stream_Size
;
6498 ---------------------------
6499 -- Has_Access_Constraint --
6500 ---------------------------
6502 function Has_Access_Constraint
(E
: Entity_Id
) return Boolean is
6504 T
: constant Entity_Id
:= Etype
(E
);
6507 if Has_Per_Object_Constraint
(E
) and then Has_Discriminants
(T
) then
6508 Disc
:= First_Discriminant
(T
);
6509 while Present
(Disc
) loop
6510 if Is_Access_Type
(Etype
(Disc
)) then
6514 Next_Discriminant
(Disc
);
6521 end Has_Access_Constraint
;
6523 -----------------------------------------------------
6524 -- Has_Annotate_Pragma_For_External_Axiomatization --
6525 -----------------------------------------------------
6527 function Has_Annotate_Pragma_For_External_Axiomatization
6528 (E
: Entity_Id
) return Boolean
6530 function Is_Annotate_Pragma_For_External_Axiomatization
6531 (N
: Node_Id
) return Boolean;
6532 -- Returns whether N is
6533 -- pragma Annotate (GNATprove, External_Axiomatization);
6535 ----------------------------------------------------
6536 -- Is_Annotate_Pragma_For_External_Axiomatization --
6537 ----------------------------------------------------
6539 -- The general form of pragma Annotate is
6541 -- pragma Annotate (IDENTIFIER [, IDENTIFIER {, ARG}]);
6542 -- ARG ::= NAME | EXPRESSION
6544 -- The first two arguments are by convention intended to refer to an
6545 -- external tool and a tool-specific function. These arguments are
6548 -- The following is used to annotate a package specification which
6549 -- GNATprove should treat specially, because the axiomatization of
6550 -- this unit is given by the user instead of being automatically
6553 -- pragma Annotate (GNATprove, External_Axiomatization);
6555 function Is_Annotate_Pragma_For_External_Axiomatization
6556 (N
: Node_Id
) return Boolean
6558 Name_GNATprove
: constant String :=
6560 Name_External_Axiomatization
: constant String :=
6561 "external_axiomatization";
6565 if Nkind
(N
) = N_Pragma
6566 and then Get_Pragma_Id
(N
) = Pragma_Annotate
6567 and then List_Length
(Pragma_Argument_Associations
(N
)) = 2
6570 Arg1
: constant Node_Id
:=
6571 First
(Pragma_Argument_Associations
(N
));
6572 Arg2
: constant Node_Id
:= Next
(Arg1
);
6577 -- Fill in Name_Buffer with Name_GNATprove first, and then with
6578 -- Name_External_Axiomatization so that Name_Find returns the
6579 -- corresponding name. This takes care of all possible casings.
6582 Add_Str_To_Name_Buffer
(Name_GNATprove
);
6586 Add_Str_To_Name_Buffer
(Name_External_Axiomatization
);
6589 return Chars
(Get_Pragma_Arg
(Arg1
)) = Nam1
6591 Chars
(Get_Pragma_Arg
(Arg2
)) = Nam2
;
6597 end Is_Annotate_Pragma_For_External_Axiomatization
;
6602 Vis_Decls
: List_Id
;
6605 -- Start of processing for Has_Annotate_Pragma_For_External_Axiomatization
6608 if Nkind
(Parent
(E
)) = N_Defining_Program_Unit_Name
then
6609 Decl
:= Parent
(Parent
(E
));
6614 Vis_Decls
:= Visible_Declarations
(Decl
);
6616 N
:= First
(Vis_Decls
);
6617 while Present
(N
) loop
6619 -- Skip declarations generated by the frontend. Skip all pragmas
6620 -- that are not the desired Annotate pragma. Stop the search on
6621 -- the first non-pragma source declaration.
6623 if Comes_From_Source
(N
) then
6624 if Nkind
(N
) = N_Pragma
then
6625 if Is_Annotate_Pragma_For_External_Axiomatization
(N
) then
6637 end Has_Annotate_Pragma_For_External_Axiomatization
;
6639 --------------------
6640 -- Homonym_Number --
6641 --------------------
6643 function Homonym_Number
(Subp
: Entity_Id
) return Nat
is
6649 Hom
:= Homonym
(Subp
);
6650 while Present
(Hom
) loop
6651 if Scope
(Hom
) = Scope
(Subp
) then
6655 Hom
:= Homonym
(Hom
);
6661 -----------------------------------
6662 -- In_Library_Level_Package_Body --
6663 -----------------------------------
6665 function In_Library_Level_Package_Body
(Id
: Entity_Id
) return Boolean is
6667 -- First determine whether the entity appears at the library level, then
6668 -- look at the containing unit.
6670 if Is_Library_Level_Entity
(Id
) then
6672 Container
: constant Node_Id
:= Cunit
(Get_Source_Unit
(Id
));
6675 return Nkind
(Unit
(Container
)) = N_Package_Body
;
6680 end In_Library_Level_Package_Body
;
6682 ------------------------------
6683 -- In_Unconditional_Context --
6684 ------------------------------
6686 function In_Unconditional_Context
(Node
: Node_Id
) return Boolean is
6691 while Present
(P
) loop
6693 when N_Subprogram_Body
=> return True;
6694 when N_If_Statement
=> return False;
6695 when N_Loop_Statement
=> return False;
6696 when N_Case_Statement
=> return False;
6697 when others => P
:= Parent
(P
);
6702 end In_Unconditional_Context
;
6708 procedure Insert_Action
(Assoc_Node
: Node_Id
; Ins_Action
: Node_Id
) is
6710 if Present
(Ins_Action
) then
6711 Insert_Actions
(Assoc_Node
, New_List
(Ins_Action
));
6715 -- Version with check(s) suppressed
6717 procedure Insert_Action
6718 (Assoc_Node
: Node_Id
; Ins_Action
: Node_Id
; Suppress
: Check_Id
)
6721 Insert_Actions
(Assoc_Node
, New_List
(Ins_Action
), Suppress
);
6724 -------------------------
6725 -- Insert_Action_After --
6726 -------------------------
6728 procedure Insert_Action_After
6729 (Assoc_Node
: Node_Id
;
6730 Ins_Action
: Node_Id
)
6733 Insert_Actions_After
(Assoc_Node
, New_List
(Ins_Action
));
6734 end Insert_Action_After
;
6736 --------------------
6737 -- Insert_Actions --
6738 --------------------
6740 procedure Insert_Actions
(Assoc_Node
: Node_Id
; Ins_Actions
: List_Id
) is
6744 Wrapped_Node
: Node_Id
:= Empty
;
6747 if No
(Ins_Actions
) or else Is_Empty_List
(Ins_Actions
) then
6751 -- Ignore insert of actions from inside default expression (or other
6752 -- similar "spec expression") in the special spec-expression analyze
6753 -- mode. Any insertions at this point have no relevance, since we are
6754 -- only doing the analyze to freeze the types of any static expressions.
6755 -- See section "Handling of Default Expressions" in the spec of package
6756 -- Sem for further details.
6758 if In_Spec_Expression
then
6762 -- If the action derives from stuff inside a record, then the actions
6763 -- are attached to the current scope, to be inserted and analyzed on
6764 -- exit from the scope. The reason for this is that we may also be
6765 -- generating freeze actions at the same time, and they must eventually
6766 -- be elaborated in the correct order.
6768 if Is_Record_Type
(Current_Scope
)
6769 and then not Is_Frozen
(Current_Scope
)
6771 if No
(Scope_Stack
.Table
6772 (Scope_Stack
.Last
).Pending_Freeze_Actions
)
6774 Scope_Stack
.Table
(Scope_Stack
.Last
).Pending_Freeze_Actions
:=
6779 Scope_Stack
.Table
(Scope_Stack
.Last
).Pending_Freeze_Actions
);
6785 -- We now intend to climb up the tree to find the right point to
6786 -- insert the actions. We start at Assoc_Node, unless this node is a
6787 -- subexpression in which case we start with its parent. We do this for
6788 -- two reasons. First it speeds things up. Second, if Assoc_Node is
6789 -- itself one of the special nodes like N_And_Then, then we assume that
6790 -- an initial request to insert actions for such a node does not expect
6791 -- the actions to get deposited in the node for later handling when the
6792 -- node is expanded, since clearly the node is being dealt with by the
6793 -- caller. Note that in the subexpression case, N is always the child we
6796 -- N_Raise_xxx_Error is an annoying special case, it is a statement
6797 -- if it has type Standard_Void_Type, and a subexpression otherwise.
6798 -- Procedure calls, and similarly procedure attribute references, are
6801 if Nkind
(Assoc_Node
) in N_Subexpr
6802 and then (Nkind
(Assoc_Node
) not in N_Raise_xxx_Error
6803 or else Etype
(Assoc_Node
) /= Standard_Void_Type
)
6804 and then Nkind
(Assoc_Node
) /= N_Procedure_Call_Statement
6805 and then (Nkind
(Assoc_Node
) /= N_Attribute_Reference
6806 or else not Is_Procedure_Attribute_Name
6807 (Attribute_Name
(Assoc_Node
)))
6810 P
:= Parent
(Assoc_Node
);
6812 -- Non-subexpression case. Note that N is initially Empty in this case
6813 -- (N is only guaranteed Non-Empty in the subexpr case).
6820 -- Capture root of the transient scope
6822 if Scope_Is_Transient
then
6823 Wrapped_Node
:= Node_To_Be_Wrapped
;
6827 pragma Assert
(Present
(P
));
6829 -- Make sure that inserted actions stay in the transient scope
6831 if Present
(Wrapped_Node
) and then N
= Wrapped_Node
then
6832 Store_Before_Actions_In_Scope
(Ins_Actions
);
6838 -- Case of right operand of AND THEN or OR ELSE. Put the actions
6839 -- in the Actions field of the right operand. They will be moved
6840 -- out further when the AND THEN or OR ELSE operator is expanded.
6841 -- Nothing special needs to be done for the left operand since
6842 -- in that case the actions are executed unconditionally.
6844 when N_Short_Circuit
=>
6845 if N
= Right_Opnd
(P
) then
6847 -- We are now going to either append the actions to the
6848 -- actions field of the short-circuit operation. We will
6849 -- also analyze the actions now.
6851 -- This analysis is really too early, the proper thing would
6852 -- be to just park them there now, and only analyze them if
6853 -- we find we really need them, and to it at the proper
6854 -- final insertion point. However attempting to this proved
6855 -- tricky, so for now we just kill current values before and
6856 -- after the analyze call to make sure we avoid peculiar
6857 -- optimizations from this out of order insertion.
6859 Kill_Current_Values
;
6861 -- If P has already been expanded, we can't park new actions
6862 -- on it, so we need to expand them immediately, introducing
6863 -- an Expression_With_Actions. N can't be an expression
6864 -- with actions, or else then the actions would have been
6865 -- inserted at an inner level.
6867 if Analyzed
(P
) then
6868 pragma Assert
(Nkind
(N
) /= N_Expression_With_Actions
);
6870 Make_Expression_With_Actions
(Sloc
(N
),
6871 Actions
=> Ins_Actions
,
6872 Expression
=> Relocate_Node
(N
)));
6873 Analyze_And_Resolve
(N
);
6875 elsif Present
(Actions
(P
)) then
6876 Insert_List_After_And_Analyze
6877 (Last
(Actions
(P
)), Ins_Actions
);
6879 Set_Actions
(P
, Ins_Actions
);
6880 Analyze_List
(Actions
(P
));
6883 Kill_Current_Values
;
6888 -- Then or Else dependent expression of an if expression. Add
6889 -- actions to Then_Actions or Else_Actions field as appropriate.
6890 -- The actions will be moved further out when the if is expanded.
6892 when N_If_Expression
=>
6894 ThenX
: constant Node_Id
:= Next
(First
(Expressions
(P
)));
6895 ElseX
: constant Node_Id
:= Next
(ThenX
);
6898 -- If the enclosing expression is already analyzed, as
6899 -- is the case for nested elaboration checks, insert the
6900 -- conditional further out.
6902 if Analyzed
(P
) then
6905 -- Actions belong to the then expression, temporarily place
6906 -- them as Then_Actions of the if expression. They will be
6907 -- moved to the proper place later when the if expression
6910 elsif N
= ThenX
then
6911 if Present
(Then_Actions
(P
)) then
6912 Insert_List_After_And_Analyze
6913 (Last
(Then_Actions
(P
)), Ins_Actions
);
6915 Set_Then_Actions
(P
, Ins_Actions
);
6916 Analyze_List
(Then_Actions
(P
));
6921 -- Actions belong to the else expression, temporarily place
6922 -- them as Else_Actions of the if expression. They will be
6923 -- moved to the proper place later when the if expression
6926 elsif N
= ElseX
then
6927 if Present
(Else_Actions
(P
)) then
6928 Insert_List_After_And_Analyze
6929 (Last
(Else_Actions
(P
)), Ins_Actions
);
6931 Set_Else_Actions
(P
, Ins_Actions
);
6932 Analyze_List
(Else_Actions
(P
));
6937 -- Actions belong to the condition. In this case they are
6938 -- unconditionally executed, and so we can continue the
6939 -- search for the proper insert point.
6946 -- Alternative of case expression, we place the action in the
6947 -- Actions field of the case expression alternative, this will
6948 -- be handled when the case expression is expanded.
6950 when N_Case_Expression_Alternative
=>
6951 if Present
(Actions
(P
)) then
6952 Insert_List_After_And_Analyze
6953 (Last
(Actions
(P
)), Ins_Actions
);
6955 Set_Actions
(P
, Ins_Actions
);
6956 Analyze_List
(Actions
(P
));
6961 -- Case of appearing within an Expressions_With_Actions node. When
6962 -- the new actions come from the expression of the expression with
6963 -- actions, they must be added to the existing actions. The other
6964 -- alternative is when the new actions are related to one of the
6965 -- existing actions of the expression with actions, and should
6966 -- never reach here: if actions are inserted on a statement
6967 -- within the Actions of an expression with actions, or on some
6968 -- subexpression of such a statement, then the outermost proper
6969 -- insertion point is right before the statement, and we should
6970 -- never climb up as far as the N_Expression_With_Actions itself.
6972 when N_Expression_With_Actions
=>
6973 if N
= Expression
(P
) then
6974 if Is_Empty_List
(Actions
(P
)) then
6975 Append_List_To
(Actions
(P
), Ins_Actions
);
6976 Analyze_List
(Actions
(P
));
6978 Insert_List_After_And_Analyze
6979 (Last
(Actions
(P
)), Ins_Actions
);
6985 raise Program_Error
;
6988 -- Case of appearing in the condition of a while expression or
6989 -- elsif. We insert the actions into the Condition_Actions field.
6990 -- They will be moved further out when the while loop or elsif
6994 | N_Iteration_Scheme
6996 if N
= Condition
(P
) then
6997 if Present
(Condition_Actions
(P
)) then
6998 Insert_List_After_And_Analyze
6999 (Last
(Condition_Actions
(P
)), Ins_Actions
);
7001 Set_Condition_Actions
(P
, Ins_Actions
);
7003 -- Set the parent of the insert actions explicitly. This
7004 -- is not a syntactic field, but we need the parent field
7005 -- set, in particular so that freeze can understand that
7006 -- it is dealing with condition actions, and properly
7007 -- insert the freezing actions.
7009 Set_Parent
(Ins_Actions
, P
);
7010 Analyze_List
(Condition_Actions
(P
));
7016 -- Statements, declarations, pragmas, representation clauses
7021 N_Procedure_Call_Statement
7022 | N_Statement_Other_Than_Procedure_Call
7028 -- Representation_Clause
7031 | N_Attribute_Definition_Clause
7032 | N_Enumeration_Representation_Clause
7033 | N_Record_Representation_Clause
7037 | N_Abstract_Subprogram_Declaration
7039 | N_Exception_Declaration
7040 | N_Exception_Renaming_Declaration
7041 | N_Expression_Function
7042 | N_Formal_Abstract_Subprogram_Declaration
7043 | N_Formal_Concrete_Subprogram_Declaration
7044 | N_Formal_Object_Declaration
7045 | N_Formal_Type_Declaration
7046 | N_Full_Type_Declaration
7047 | N_Function_Instantiation
7048 | N_Generic_Function_Renaming_Declaration
7049 | N_Generic_Package_Declaration
7050 | N_Generic_Package_Renaming_Declaration
7051 | N_Generic_Procedure_Renaming_Declaration
7052 | N_Generic_Subprogram_Declaration
7053 | N_Implicit_Label_Declaration
7054 | N_Incomplete_Type_Declaration
7055 | N_Number_Declaration
7056 | N_Object_Declaration
7057 | N_Object_Renaming_Declaration
7059 | N_Package_Body_Stub
7060 | N_Package_Declaration
7061 | N_Package_Instantiation
7062 | N_Package_Renaming_Declaration
7063 | N_Private_Extension_Declaration
7064 | N_Private_Type_Declaration
7065 | N_Procedure_Instantiation
7067 | N_Protected_Body_Stub
7068 | N_Protected_Type_Declaration
7069 | N_Single_Task_Declaration
7071 | N_Subprogram_Body_Stub
7072 | N_Subprogram_Declaration
7073 | N_Subprogram_Renaming_Declaration
7074 | N_Subtype_Declaration
7077 | N_Task_Type_Declaration
7079 -- Use clauses can appear in lists of declarations
7081 | N_Use_Package_Clause
7084 -- Freeze entity behaves like a declaration or statement
7087 | N_Freeze_Generic_Entity
7089 -- Do not insert here if the item is not a list member (this
7090 -- happens for example with a triggering statement, and the
7091 -- proper approach is to insert before the entire select).
7093 if not Is_List_Member
(P
) then
7096 -- Do not insert if parent of P is an N_Component_Association
7097 -- node (i.e. we are in the context of an N_Aggregate or
7098 -- N_Extension_Aggregate node. In this case we want to insert
7099 -- before the entire aggregate.
7101 elsif Nkind
(Parent
(P
)) = N_Component_Association
then
7104 -- Do not insert if the parent of P is either an N_Variant node
7105 -- or an N_Record_Definition node, meaning in either case that
7106 -- P is a member of a component list, and that therefore the
7107 -- actions should be inserted outside the complete record
7110 elsif Nkind_In
(Parent
(P
), N_Variant
, N_Record_Definition
) then
7113 -- Do not insert freeze nodes within the loop generated for
7114 -- an aggregate, because they may be elaborated too late for
7115 -- subsequent use in the back end: within a package spec the
7116 -- loop is part of the elaboration procedure and is only
7117 -- elaborated during the second pass.
7119 -- If the loop comes from source, or the entity is local to the
7120 -- loop itself it must remain within.
7122 elsif Nkind
(Parent
(P
)) = N_Loop_Statement
7123 and then not Comes_From_Source
(Parent
(P
))
7124 and then Nkind
(First
(Ins_Actions
)) = N_Freeze_Entity
7126 Scope
(Entity
(First
(Ins_Actions
))) /= Current_Scope
7130 -- Otherwise we can go ahead and do the insertion
7132 elsif P
= Wrapped_Node
then
7133 Store_Before_Actions_In_Scope
(Ins_Actions
);
7137 Insert_List_Before_And_Analyze
(P
, Ins_Actions
);
7141 -- A special case, N_Raise_xxx_Error can act either as a statement
7142 -- or a subexpression. We tell the difference by looking at the
7143 -- Etype. It is set to Standard_Void_Type in the statement case.
7145 when N_Raise_xxx_Error
=>
7146 if Etype
(P
) = Standard_Void_Type
then
7147 if P
= Wrapped_Node
then
7148 Store_Before_Actions_In_Scope
(Ins_Actions
);
7150 Insert_List_Before_And_Analyze
(P
, Ins_Actions
);
7155 -- In the subexpression case, keep climbing
7161 -- If a component association appears within a loop created for
7162 -- an array aggregate, attach the actions to the association so
7163 -- they can be subsequently inserted within the loop. For other
7164 -- component associations insert outside of the aggregate. For
7165 -- an association that will generate a loop, its Loop_Actions
7166 -- attribute is already initialized (see exp_aggr.adb).
7168 -- The list of Loop_Actions can in turn generate additional ones,
7169 -- that are inserted before the associated node. If the associated
7170 -- node is outside the aggregate, the new actions are collected
7171 -- at the end of the Loop_Actions, to respect the order in which
7172 -- they are to be elaborated.
7174 when N_Component_Association
7175 | N_Iterated_Component_Association
7177 if Nkind
(Parent
(P
)) = N_Aggregate
7178 and then Present
(Loop_Actions
(P
))
7180 if Is_Empty_List
(Loop_Actions
(P
)) then
7181 Set_Loop_Actions
(P
, Ins_Actions
);
7182 Analyze_List
(Ins_Actions
);
7188 -- Check whether these actions were generated by a
7189 -- declaration that is part of the Loop_Actions for
7190 -- the component_association.
7193 while Present
(Decl
) loop
7194 exit when Parent
(Decl
) = P
7195 and then Is_List_Member
(Decl
)
7197 List_Containing
(Decl
) = Loop_Actions
(P
);
7198 Decl
:= Parent
(Decl
);
7201 if Present
(Decl
) then
7202 Insert_List_Before_And_Analyze
7203 (Decl
, Ins_Actions
);
7205 Insert_List_After_And_Analyze
7206 (Last
(Loop_Actions
(P
)), Ins_Actions
);
7217 -- Special case: an attribute denoting a procedure call
7219 when N_Attribute_Reference
=>
7220 if Is_Procedure_Attribute_Name
(Attribute_Name
(P
)) then
7221 if P
= Wrapped_Node
then
7222 Store_Before_Actions_In_Scope
(Ins_Actions
);
7224 Insert_List_Before_And_Analyze
(P
, Ins_Actions
);
7229 -- In the subexpression case, keep climbing
7235 -- Special case: a marker
7238 | N_Variable_Reference_Marker
7240 if Is_List_Member
(P
) then
7241 Insert_List_Before_And_Analyze
(P
, Ins_Actions
);
7245 -- A contract node should not belong to the tree
7248 raise Program_Error
;
7250 -- For all other node types, keep climbing tree
7252 when N_Abortable_Part
7253 | N_Accept_Alternative
7254 | N_Access_Definition
7255 | N_Access_Function_Definition
7256 | N_Access_Procedure_Definition
7257 | N_Access_To_Object_Definition
7260 | N_Aspect_Specification
7262 | N_Case_Statement_Alternative
7263 | N_Character_Literal
7264 | N_Compilation_Unit
7265 | N_Compilation_Unit_Aux
7266 | N_Component_Clause
7267 | N_Component_Declaration
7268 | N_Component_Definition
7270 | N_Constrained_Array_Definition
7271 | N_Decimal_Fixed_Point_Definition
7272 | N_Defining_Character_Literal
7273 | N_Defining_Identifier
7274 | N_Defining_Operator_Symbol
7275 | N_Defining_Program_Unit_Name
7276 | N_Delay_Alternative
7278 | N_Delta_Constraint
7279 | N_Derived_Type_Definition
7281 | N_Digits_Constraint
7282 | N_Discriminant_Association
7283 | N_Discriminant_Specification
7285 | N_Entry_Body_Formal_Part
7286 | N_Entry_Call_Alternative
7287 | N_Entry_Declaration
7288 | N_Entry_Index_Specification
7289 | N_Enumeration_Type_Definition
7291 | N_Exception_Handler
7293 | N_Explicit_Dereference
7294 | N_Extension_Aggregate
7295 | N_Floating_Point_Definition
7296 | N_Formal_Decimal_Fixed_Point_Definition
7297 | N_Formal_Derived_Type_Definition
7298 | N_Formal_Discrete_Type_Definition
7299 | N_Formal_Floating_Point_Definition
7300 | N_Formal_Modular_Type_Definition
7301 | N_Formal_Ordinary_Fixed_Point_Definition
7302 | N_Formal_Package_Declaration
7303 | N_Formal_Private_Type_Definition
7304 | N_Formal_Incomplete_Type_Definition
7305 | N_Formal_Signed_Integer_Type_Definition
7307 | N_Function_Specification
7308 | N_Generic_Association
7309 | N_Handled_Sequence_Of_Statements
7312 | N_Index_Or_Discriminant_Constraint
7313 | N_Indexed_Component
7315 | N_Iterator_Specification
7318 | N_Loop_Parameter_Specification
7320 | N_Modular_Type_Definition
7346 | N_Op_Shift_Right_Arithmetic
7350 | N_Ordinary_Fixed_Point_Definition
7352 | N_Package_Specification
7353 | N_Parameter_Association
7354 | N_Parameter_Specification
7355 | N_Pop_Constraint_Error_Label
7356 | N_Pop_Program_Error_Label
7357 | N_Pop_Storage_Error_Label
7358 | N_Pragma_Argument_Association
7359 | N_Procedure_Specification
7360 | N_Protected_Definition
7361 | N_Push_Constraint_Error_Label
7362 | N_Push_Program_Error_Label
7363 | N_Push_Storage_Error_Label
7364 | N_Qualified_Expression
7365 | N_Quantified_Expression
7366 | N_Raise_Expression
7368 | N_Range_Constraint
7370 | N_Real_Range_Specification
7371 | N_Record_Definition
7373 | N_SCIL_Dispatch_Table_Tag_Init
7374 | N_SCIL_Dispatching_Call
7375 | N_SCIL_Membership_Test
7376 | N_Selected_Component
7377 | N_Signed_Integer_Type_Definition
7378 | N_Single_Protected_Declaration
7381 | N_Subtype_Indication
7385 | N_Terminate_Alternative
7386 | N_Triggering_Alternative
7388 | N_Unchecked_Expression
7389 | N_Unchecked_Type_Conversion
7390 | N_Unconstrained_Array_Definition
7395 | N_Validate_Unchecked_Conversion
7401 -- If we fall through above tests, keep climbing tree
7405 if Nkind
(Parent
(N
)) = N_Subunit
then
7407 -- This is the proper body corresponding to a stub. Insertion must
7408 -- be done at the point of the stub, which is in the declarative
7409 -- part of the parent unit.
7411 P
:= Corresponding_Stub
(Parent
(N
));
7419 -- Version with check(s) suppressed
7421 procedure Insert_Actions
7422 (Assoc_Node
: Node_Id
;
7423 Ins_Actions
: List_Id
;
7424 Suppress
: Check_Id
)
7427 if Suppress
= All_Checks
then
7429 Sva
: constant Suppress_Array
:= Scope_Suppress
.Suppress
;
7431 Scope_Suppress
.Suppress
:= (others => True);
7432 Insert_Actions
(Assoc_Node
, Ins_Actions
);
7433 Scope_Suppress
.Suppress
:= Sva
;
7438 Svg
: constant Boolean := Scope_Suppress
.Suppress
(Suppress
);
7440 Scope_Suppress
.Suppress
(Suppress
) := True;
7441 Insert_Actions
(Assoc_Node
, Ins_Actions
);
7442 Scope_Suppress
.Suppress
(Suppress
) := Svg
;
7447 --------------------------
7448 -- Insert_Actions_After --
7449 --------------------------
7451 procedure Insert_Actions_After
7452 (Assoc_Node
: Node_Id
;
7453 Ins_Actions
: List_Id
)
7456 if Scope_Is_Transient
and then Assoc_Node
= Node_To_Be_Wrapped
then
7457 Store_After_Actions_In_Scope
(Ins_Actions
);
7459 Insert_List_After_And_Analyze
(Assoc_Node
, Ins_Actions
);
7461 end Insert_Actions_After
;
7463 ------------------------
7464 -- Insert_Declaration --
7465 ------------------------
7467 procedure Insert_Declaration
(N
: Node_Id
; Decl
: Node_Id
) is
7471 pragma Assert
(Nkind
(N
) in N_Subexpr
);
7473 -- Climb until we find a procedure or a package
7477 pragma Assert
(Present
(Parent
(P
)));
7480 if Is_List_Member
(P
) then
7481 exit when Nkind_In
(Parent
(P
), N_Package_Specification
,
7484 -- Special handling for handled sequence of statements, we must
7485 -- insert in the statements not the exception handlers!
7487 if Nkind
(Parent
(P
)) = N_Handled_Sequence_Of_Statements
then
7488 P
:= First
(Statements
(Parent
(P
)));
7494 -- Now do the insertion
7496 Insert_Before
(P
, Decl
);
7498 end Insert_Declaration
;
7500 ---------------------------------
7501 -- Insert_Library_Level_Action --
7502 ---------------------------------
7504 procedure Insert_Library_Level_Action
(N
: Node_Id
) is
7505 Aux
: constant Node_Id
:= Aux_Decls_Node
(Cunit
(Main_Unit
));
7508 Push_Scope
(Cunit_Entity
(Current_Sem_Unit
));
7509 -- And not Main_Unit as previously. If the main unit is a body,
7510 -- the scope needed to analyze the actions is the entity of the
7511 -- corresponding declaration.
7513 if No
(Actions
(Aux
)) then
7514 Set_Actions
(Aux
, New_List
(N
));
7516 Append
(N
, Actions
(Aux
));
7521 end Insert_Library_Level_Action
;
7523 ----------------------------------
7524 -- Insert_Library_Level_Actions --
7525 ----------------------------------
7527 procedure Insert_Library_Level_Actions
(L
: List_Id
) is
7528 Aux
: constant Node_Id
:= Aux_Decls_Node
(Cunit
(Main_Unit
));
7531 if Is_Non_Empty_List
(L
) then
7532 Push_Scope
(Cunit_Entity
(Main_Unit
));
7533 -- ??? should this be Current_Sem_Unit instead of Main_Unit?
7535 if No
(Actions
(Aux
)) then
7536 Set_Actions
(Aux
, L
);
7539 Insert_List_After_And_Analyze
(Last
(Actions
(Aux
)), L
);
7544 end Insert_Library_Level_Actions
;
7546 ----------------------
7547 -- Inside_Init_Proc --
7548 ----------------------
7550 function Inside_Init_Proc
return Boolean is
7551 Proc
: constant Entity_Id
:= Enclosing_Init_Proc
;
7554 return Proc
/= Empty
;
7555 end Inside_Init_Proc
;
7557 ----------------------------
7558 -- Is_All_Null_Statements --
7559 ----------------------------
7561 function Is_All_Null_Statements
(L
: List_Id
) return Boolean is
7566 while Present
(Stm
) loop
7567 if Nkind
(Stm
) /= N_Null_Statement
then
7575 end Is_All_Null_Statements
;
7577 --------------------------------------------------
7578 -- Is_Displacement_Of_Object_Or_Function_Result --
7579 --------------------------------------------------
7581 function Is_Displacement_Of_Object_Or_Function_Result
7582 (Obj_Id
: Entity_Id
) return Boolean
7584 function Is_Controlled_Function_Call
(N
: Node_Id
) return Boolean;
7585 -- Determine whether node N denotes a controlled function call
7587 function Is_Controlled_Indexing
(N
: Node_Id
) return Boolean;
7588 -- Determine whether node N denotes a generalized indexing form which
7589 -- involves a controlled result.
7591 function Is_Displace_Call
(N
: Node_Id
) return Boolean;
7592 -- Determine whether node N denotes a call to Ada.Tags.Displace
7594 function Is_Source_Object
(N
: Node_Id
) return Boolean;
7595 -- Determine whether a particular node denotes a source object
7597 function Strip
(N
: Node_Id
) return Node_Id
;
7598 -- Examine arbitrary node N by stripping various indirections and return
7601 ---------------------------------
7602 -- Is_Controlled_Function_Call --
7603 ---------------------------------
7605 function Is_Controlled_Function_Call
(N
: Node_Id
) return Boolean is
7609 -- When a function call appears in Object.Operation format, the
7610 -- original representation has several possible forms depending on
7611 -- the availability and form of actual parameters:
7613 -- Obj.Func N_Selected_Component
7614 -- Obj.Func (Actual) N_Indexed_Component
7615 -- Obj.Func (Formal => Actual) N_Function_Call, whose Name is an
7616 -- N_Selected_Component
7618 Expr
:= Original_Node
(N
);
7620 if Nkind
(Expr
) = N_Function_Call
then
7621 Expr
:= Name
(Expr
);
7623 -- "Obj.Func (Actual)" case
7625 elsif Nkind
(Expr
) = N_Indexed_Component
then
7626 Expr
:= Prefix
(Expr
);
7628 -- "Obj.Func" or "Obj.Func (Formal => Actual) case
7630 elsif Nkind
(Expr
) = N_Selected_Component
then
7631 Expr
:= Selector_Name
(Expr
);
7639 Nkind
(Expr
) in N_Has_Entity
7640 and then Present
(Entity
(Expr
))
7641 and then Ekind
(Entity
(Expr
)) = E_Function
7642 and then Needs_Finalization
(Etype
(Entity
(Expr
)));
7643 end Is_Controlled_Function_Call
;
7645 ----------------------------
7646 -- Is_Controlled_Indexing --
7647 ----------------------------
7649 function Is_Controlled_Indexing
(N
: Node_Id
) return Boolean is
7650 Expr
: constant Node_Id
:= Original_Node
(N
);
7654 Nkind
(Expr
) = N_Indexed_Component
7655 and then Present
(Generalized_Indexing
(Expr
))
7656 and then Needs_Finalization
(Etype
(Expr
));
7657 end Is_Controlled_Indexing
;
7659 ----------------------
7660 -- Is_Displace_Call --
7661 ----------------------
7663 function Is_Displace_Call
(N
: Node_Id
) return Boolean is
7664 Call
: constant Node_Id
:= Strip
(N
);
7669 and then Nkind
(Call
) = N_Function_Call
7670 and then Nkind
(Name
(Call
)) in N_Has_Entity
7671 and then Is_RTE
(Entity
(Name
(Call
)), RE_Displace
);
7672 end Is_Displace_Call
;
7674 ----------------------
7675 -- Is_Source_Object --
7676 ----------------------
7678 function Is_Source_Object
(N
: Node_Id
) return Boolean is
7679 Obj
: constant Node_Id
:= Strip
(N
);
7684 and then Comes_From_Source
(Obj
)
7685 and then Nkind
(Obj
) in N_Has_Entity
7686 and then Is_Object
(Entity
(Obj
));
7687 end Is_Source_Object
;
7693 function Strip
(N
: Node_Id
) return Node_Id
is
7699 if Nkind
(Result
) = N_Explicit_Dereference
then
7700 Result
:= Prefix
(Result
);
7702 elsif Nkind_In
(Result
, N_Type_Conversion
,
7703 N_Unchecked_Type_Conversion
)
7705 Result
:= Expression
(Result
);
7717 Obj_Decl
: constant Node_Id
:= Declaration_Node
(Obj_Id
);
7718 Obj_Typ
: constant Entity_Id
:= Base_Type
(Etype
(Obj_Id
));
7719 Orig_Decl
: constant Node_Id
:= Original_Node
(Obj_Decl
);
7720 Orig_Expr
: Node_Id
;
7722 -- Start of processing for Is_Displacement_Of_Object_Or_Function_Result
7727 -- Obj : CW_Type := Function_Call (...);
7729 -- is rewritten into:
7731 -- Temp : ... := Function_Call (...)'reference;
7732 -- Obj : CW_Type renames (... Ada.Tags.Displace (Temp));
7734 -- where the return type of the function and the class-wide type require
7735 -- dispatch table pointer displacement.
7739 -- Obj : CW_Type := Container (...);
7741 -- is rewritten into:
7743 -- Temp : ... := Function_Call (Container, ...)'reference;
7744 -- Obj : CW_Type renames (... Ada.Tags.Displace (Temp));
7746 -- where the container element type and the class-wide type require
7747 -- dispatch table pointer dispacement.
7751 -- Obj : CW_Type := Src_Obj;
7753 -- is rewritten into:
7755 -- Obj : CW_Type renames (... Ada.Tags.Displace (Src_Obj));
7757 -- where the type of the source object and the class-wide type require
7758 -- dispatch table pointer displacement.
7760 if Nkind
(Obj_Decl
) = N_Object_Renaming_Declaration
7761 and then Is_Class_Wide_Type
(Obj_Typ
)
7762 and then Is_Displace_Call
(Renamed_Object
(Obj_Id
))
7763 and then Nkind
(Orig_Decl
) = N_Object_Declaration
7764 and then Comes_From_Source
(Orig_Decl
)
7766 Orig_Expr
:= Expression
(Orig_Decl
);
7769 Is_Controlled_Function_Call
(Orig_Expr
)
7770 or else Is_Controlled_Indexing
(Orig_Expr
)
7771 or else Is_Source_Object
(Orig_Expr
);
7775 end Is_Displacement_Of_Object_Or_Function_Result
;
7777 ------------------------------
7778 -- Is_Finalizable_Transient --
7779 ------------------------------
7781 function Is_Finalizable_Transient
7783 Rel_Node
: Node_Id
) return Boolean
7785 Obj_Id
: constant Entity_Id
:= Defining_Identifier
(Decl
);
7786 Obj_Typ
: constant Entity_Id
:= Base_Type
(Etype
(Obj_Id
));
7788 function Initialized_By_Access
(Trans_Id
: Entity_Id
) return Boolean;
7789 -- Determine whether transient object Trans_Id is initialized either
7790 -- by a function call which returns an access type or simply renames
7793 function Initialized_By_Aliased_BIP_Func_Call
7794 (Trans_Id
: Entity_Id
) return Boolean;
7795 -- Determine whether transient object Trans_Id is initialized by a
7796 -- build-in-place function call where the BIPalloc parameter is of
7797 -- value 1 and BIPaccess is not null. This case creates an aliasing
7798 -- between the returned value and the value denoted by BIPaccess.
7801 (Trans_Id
: Entity_Id
;
7802 First_Stmt
: Node_Id
) return Boolean;
7803 -- Determine whether transient object Trans_Id has been renamed or
7804 -- aliased through 'reference in the statement list starting from
7807 function Is_Allocated
(Trans_Id
: Entity_Id
) return Boolean;
7808 -- Determine whether transient object Trans_Id is allocated on the heap
7810 function Is_Iterated_Container
7811 (Trans_Id
: Entity_Id
;
7812 First_Stmt
: Node_Id
) return Boolean;
7813 -- Determine whether transient object Trans_Id denotes a container which
7814 -- is in the process of being iterated in the statement list starting
7817 ---------------------------
7818 -- Initialized_By_Access --
7819 ---------------------------
7821 function Initialized_By_Access
(Trans_Id
: Entity_Id
) return Boolean is
7822 Expr
: constant Node_Id
:= Expression
(Parent
(Trans_Id
));
7827 and then Nkind
(Expr
) /= N_Reference
7828 and then Is_Access_Type
(Etype
(Expr
));
7829 end Initialized_By_Access
;
7831 ------------------------------------------
7832 -- Initialized_By_Aliased_BIP_Func_Call --
7833 ------------------------------------------
7835 function Initialized_By_Aliased_BIP_Func_Call
7836 (Trans_Id
: Entity_Id
) return Boolean
7838 Call
: Node_Id
:= Expression
(Parent
(Trans_Id
));
7841 -- Build-in-place calls usually appear in 'reference format
7843 if Nkind
(Call
) = N_Reference
then
7844 Call
:= Prefix
(Call
);
7847 Call
:= Unqual_Conv
(Call
);
7849 if Is_Build_In_Place_Function_Call
(Call
) then
7851 Access_Nam
: Name_Id
:= No_Name
;
7852 Access_OK
: Boolean := False;
7854 Alloc_Nam
: Name_Id
:= No_Name
;
7855 Alloc_OK
: Boolean := False;
7857 Func_Id
: Entity_Id
;
7861 -- Examine all parameter associations of the function call
7863 Param
:= First
(Parameter_Associations
(Call
));
7864 while Present
(Param
) loop
7865 if Nkind
(Param
) = N_Parameter_Association
7866 and then Nkind
(Selector_Name
(Param
)) = N_Identifier
7868 Actual
:= Explicit_Actual_Parameter
(Param
);
7869 Formal
:= Selector_Name
(Param
);
7871 -- Construct the names of formals BIPaccess and BIPalloc
7872 -- using the function name retrieved from an arbitrary
7875 if Access_Nam
= No_Name
7876 and then Alloc_Nam
= No_Name
7877 and then Present
(Entity
(Formal
))
7879 Func_Id
:= Scope
(Entity
(Formal
));
7882 New_External_Name
(Chars
(Func_Id
),
7883 BIP_Formal_Suffix
(BIP_Object_Access
));
7886 New_External_Name
(Chars
(Func_Id
),
7887 BIP_Formal_Suffix
(BIP_Alloc_Form
));
7890 -- A match for BIPaccess => Temp has been found
7892 if Chars
(Formal
) = Access_Nam
7893 and then Nkind
(Actual
) /= N_Null
7898 -- A match for BIPalloc => 1 has been found
7900 if Chars
(Formal
) = Alloc_Nam
7901 and then Nkind
(Actual
) = N_Integer_Literal
7902 and then Intval
(Actual
) = Uint_1
7911 return Access_OK
and Alloc_OK
;
7916 end Initialized_By_Aliased_BIP_Func_Call
;
7923 (Trans_Id
: Entity_Id
;
7924 First_Stmt
: Node_Id
) return Boolean
7926 function Find_Renamed_Object
(Ren_Decl
: Node_Id
) return Entity_Id
;
7927 -- Given an object renaming declaration, retrieve the entity of the
7928 -- renamed name. Return Empty if the renamed name is anything other
7929 -- than a variable or a constant.
7931 -------------------------
7932 -- Find_Renamed_Object --
7933 -------------------------
7935 function Find_Renamed_Object
(Ren_Decl
: Node_Id
) return Entity_Id
is
7936 Ren_Obj
: Node_Id
:= Empty
;
7938 function Find_Object
(N
: Node_Id
) return Traverse_Result
;
7939 -- Try to detect an object which is either a constant or a
7946 function Find_Object
(N
: Node_Id
) return Traverse_Result
is
7948 -- Stop the search once a constant or a variable has been
7951 if Nkind
(N
) = N_Identifier
7952 and then Present
(Entity
(N
))
7953 and then Ekind_In
(Entity
(N
), E_Constant
, E_Variable
)
7955 Ren_Obj
:= Entity
(N
);
7962 procedure Search
is new Traverse_Proc
(Find_Object
);
7966 Typ
: constant Entity_Id
:= Etype
(Defining_Identifier
(Ren_Decl
));
7968 -- Start of processing for Find_Renamed_Object
7971 -- Actions related to dispatching calls may appear as renamings of
7972 -- tags. Do not process this type of renaming because it does not
7973 -- use the actual value of the object.
7975 if not Is_RTE
(Typ
, RE_Tag_Ptr
) then
7976 Search
(Name
(Ren_Decl
));
7980 end Find_Renamed_Object
;
7985 Ren_Obj
: Entity_Id
;
7988 -- Start of processing for Is_Aliased
7991 -- A controlled transient object is not considered aliased when it
7992 -- appears inside an expression_with_actions node even when there are
7993 -- explicit aliases of it:
7996 -- Trans_Id : Ctrl_Typ ...; -- transient object
7997 -- Alias : ... := Trans_Id; -- object is aliased
7998 -- Val : constant Boolean :=
7999 -- ... Alias ...; -- aliasing ends
8000 -- <finalize Trans_Id> -- object safe to finalize
8003 -- Expansion ensures that all aliases are encapsulated in the actions
8004 -- list and do not leak to the expression by forcing the evaluation
8005 -- of the expression.
8007 if Nkind
(Rel_Node
) = N_Expression_With_Actions
then
8010 -- Otherwise examine the statements after the controlled transient
8011 -- object and look for various forms of aliasing.
8015 while Present
(Stmt
) loop
8016 if Nkind
(Stmt
) = N_Object_Declaration
then
8017 Expr
:= Expression
(Stmt
);
8019 -- Aliasing of the form:
8020 -- Obj : ... := Trans_Id'reference;
8023 and then Nkind
(Expr
) = N_Reference
8024 and then Nkind
(Prefix
(Expr
)) = N_Identifier
8025 and then Entity
(Prefix
(Expr
)) = Trans_Id
8030 elsif Nkind
(Stmt
) = N_Object_Renaming_Declaration
then
8031 Ren_Obj
:= Find_Renamed_Object
(Stmt
);
8033 -- Aliasing of the form:
8034 -- Obj : ... renames ... Trans_Id ...;
8036 if Present
(Ren_Obj
) and then Ren_Obj
= Trans_Id
then
8052 function Is_Allocated
(Trans_Id
: Entity_Id
) return Boolean is
8053 Expr
: constant Node_Id
:= Expression
(Parent
(Trans_Id
));
8056 Is_Access_Type
(Etype
(Trans_Id
))
8057 and then Present
(Expr
)
8058 and then Nkind
(Expr
) = N_Allocator
;
8061 ---------------------------
8062 -- Is_Iterated_Container --
8063 ---------------------------
8065 function Is_Iterated_Container
8066 (Trans_Id
: Entity_Id
;
8067 First_Stmt
: Node_Id
) return Boolean
8077 -- It is not possible to iterate over containers in non-Ada 2012 code
8079 if Ada_Version
< Ada_2012
then
8083 Typ
:= Etype
(Trans_Id
);
8085 -- Handle access type created for secondary stack use
8087 if Is_Access_Type
(Typ
) then
8088 Typ
:= Designated_Type
(Typ
);
8091 -- Look for aspect Default_Iterator. It may be part of a type
8092 -- declaration for a container, or inherited from a base type
8095 Aspect
:= Find_Value_Of_Aspect
(Typ
, Aspect_Default_Iterator
);
8097 if Present
(Aspect
) then
8098 Iter
:= Entity
(Aspect
);
8100 -- Examine the statements following the container object and
8101 -- look for a call to the default iterate routine where the
8102 -- first parameter is the transient. Such a call appears as:
8104 -- It : Access_To_CW_Iterator :=
8105 -- Iterate (Tran_Id.all, ...)'reference;
8108 while Present
(Stmt
) loop
8110 -- Detect an object declaration which is initialized by a
8111 -- secondary stack function call.
8113 if Nkind
(Stmt
) = N_Object_Declaration
8114 and then Present
(Expression
(Stmt
))
8115 and then Nkind
(Expression
(Stmt
)) = N_Reference
8116 and then Nkind
(Prefix
(Expression
(Stmt
))) = N_Function_Call
8118 Call
:= Prefix
(Expression
(Stmt
));
8120 -- The call must invoke the default iterate routine of
8121 -- the container and the transient object must appear as
8122 -- the first actual parameter. Skip any calls whose names
8123 -- are not entities.
8125 if Is_Entity_Name
(Name
(Call
))
8126 and then Entity
(Name
(Call
)) = Iter
8127 and then Present
(Parameter_Associations
(Call
))
8129 Param
:= First
(Parameter_Associations
(Call
));
8131 if Nkind
(Param
) = N_Explicit_Dereference
8132 and then Entity
(Prefix
(Param
)) = Trans_Id
8144 end Is_Iterated_Container
;
8148 Desig
: Entity_Id
:= Obj_Typ
;
8150 -- Start of processing for Is_Finalizable_Transient
8153 -- Handle access types
8155 if Is_Access_Type
(Desig
) then
8156 Desig
:= Available_View
(Designated_Type
(Desig
));
8160 Ekind_In
(Obj_Id
, E_Constant
, E_Variable
)
8161 and then Needs_Finalization
(Desig
)
8162 and then Requires_Transient_Scope
(Desig
)
8163 and then Nkind
(Rel_Node
) /= N_Simple_Return_Statement
8165 -- Do not consider a transient object that was already processed
8167 and then not Is_Finalized_Transient
(Obj_Id
)
8169 -- Do not consider renamed or 'reference-d transient objects because
8170 -- the act of renaming extends the object's lifetime.
8172 and then not Is_Aliased
(Obj_Id
, Decl
)
8174 -- Do not consider transient objects allocated on the heap since
8175 -- they are attached to a finalization master.
8177 and then not Is_Allocated
(Obj_Id
)
8179 -- If the transient object is a pointer, check that it is not
8180 -- initialized by a function that returns a pointer or acts as a
8181 -- renaming of another pointer.
8184 (not Is_Access_Type
(Obj_Typ
)
8185 or else not Initialized_By_Access
(Obj_Id
))
8187 -- Do not consider transient objects which act as indirect aliases
8188 -- of build-in-place function results.
8190 and then not Initialized_By_Aliased_BIP_Func_Call
(Obj_Id
)
8192 -- Do not consider conversions of tags to class-wide types
8194 and then not Is_Tag_To_Class_Wide_Conversion
(Obj_Id
)
8196 -- Do not consider iterators because those are treated as normal
8197 -- controlled objects and are processed by the usual finalization
8198 -- machinery. This avoids the double finalization of an iterator.
8200 and then not Is_Iterator
(Desig
)
8202 -- Do not consider containers in the context of iterator loops. Such
8203 -- transient objects must exist for as long as the loop is around,
8204 -- otherwise any operation carried out by the iterator will fail.
8206 and then not Is_Iterated_Container
(Obj_Id
, Decl
);
8207 end Is_Finalizable_Transient
;
8209 ---------------------------------
8210 -- Is_Fully_Repped_Tagged_Type --
8211 ---------------------------------
8213 function Is_Fully_Repped_Tagged_Type
(T
: Entity_Id
) return Boolean is
8214 U
: constant Entity_Id
:= Underlying_Type
(T
);
8218 if No
(U
) or else not Is_Tagged_Type
(U
) then
8220 elsif Has_Discriminants
(U
) then
8222 elsif not Has_Specified_Layout
(U
) then
8226 -- Here we have a tagged type, see if it has any unlayed out fields
8227 -- other than a possible tag and parent fields. If so, we return False.
8229 Comp
:= First_Component
(U
);
8230 while Present
(Comp
) loop
8231 if not Is_Tag
(Comp
)
8232 and then Chars
(Comp
) /= Name_uParent
8233 and then No
(Component_Clause
(Comp
))
8237 Next_Component
(Comp
);
8241 -- All components are layed out
8244 end Is_Fully_Repped_Tagged_Type
;
8246 ----------------------------------
8247 -- Is_Library_Level_Tagged_Type --
8248 ----------------------------------
8250 function Is_Library_Level_Tagged_Type
(Typ
: Entity_Id
) return Boolean is
8252 return Is_Tagged_Type
(Typ
) and then Is_Library_Level_Entity
(Typ
);
8253 end Is_Library_Level_Tagged_Type
;
8255 --------------------------
8256 -- Is_Non_BIP_Func_Call --
8257 --------------------------
8259 function Is_Non_BIP_Func_Call
(Expr
: Node_Id
) return Boolean is
8261 -- The expected call is of the format
8263 -- Func_Call'reference
8266 Nkind
(Expr
) = N_Reference
8267 and then Nkind
(Prefix
(Expr
)) = N_Function_Call
8268 and then not Is_Build_In_Place_Function_Call
(Prefix
(Expr
));
8269 end Is_Non_BIP_Func_Call
;
8271 ----------------------------------
8272 -- Is_Possibly_Unaligned_Object --
8273 ----------------------------------
8275 function Is_Possibly_Unaligned_Object
(N
: Node_Id
) return Boolean is
8276 T
: constant Entity_Id
:= Etype
(N
);
8279 -- If renamed object, apply test to underlying object
8281 if Is_Entity_Name
(N
)
8282 and then Is_Object
(Entity
(N
))
8283 and then Present
(Renamed_Object
(Entity
(N
)))
8285 return Is_Possibly_Unaligned_Object
(Renamed_Object
(Entity
(N
)));
8288 -- Tagged and controlled types and aliased types are always aligned, as
8289 -- are concurrent types.
8292 or else Has_Controlled_Component
(T
)
8293 or else Is_Concurrent_Type
(T
)
8294 or else Is_Tagged_Type
(T
)
8295 or else Is_Controlled
(T
)
8300 -- If this is an element of a packed array, may be unaligned
8302 if Is_Ref_To_Bit_Packed_Array
(N
) then
8306 -- Case of indexed component reference: test whether prefix is unaligned
8308 if Nkind
(N
) = N_Indexed_Component
then
8309 return Is_Possibly_Unaligned_Object
(Prefix
(N
));
8311 -- Case of selected component reference
8313 elsif Nkind
(N
) = N_Selected_Component
then
8315 P
: constant Node_Id
:= Prefix
(N
);
8316 C
: constant Entity_Id
:= Entity
(Selector_Name
(N
));
8321 -- If component reference is for an array with non-static bounds,
8322 -- then it is always aligned: we can only process unaligned arrays
8323 -- with static bounds (more precisely compile time known bounds).
8325 if Is_Array_Type
(T
)
8326 and then not Compile_Time_Known_Bounds
(T
)
8331 -- If component is aliased, it is definitely properly aligned
8333 if Is_Aliased
(C
) then
8337 -- If component is for a type implemented as a scalar, and the
8338 -- record is packed, and the component is other than the first
8339 -- component of the record, then the component may be unaligned.
8341 if Is_Packed
(Etype
(P
))
8342 and then Represented_As_Scalar
(Etype
(C
))
8343 and then First_Entity
(Scope
(C
)) /= C
8348 -- Compute maximum possible alignment for T
8350 -- If alignment is known, then that settles things
8352 if Known_Alignment
(T
) then
8353 M
:= UI_To_Int
(Alignment
(T
));
8355 -- If alignment is not known, tentatively set max alignment
8358 M
:= Ttypes
.Maximum_Alignment
;
8360 -- We can reduce this if the Esize is known since the default
8361 -- alignment will never be more than the smallest power of 2
8362 -- that does not exceed this Esize value.
8364 if Known_Esize
(T
) then
8365 S
:= UI_To_Int
(Esize
(T
));
8367 while (M
/ 2) >= S
loop
8373 -- The following code is historical, it used to be present but it
8374 -- is too cautious, because the front-end does not know the proper
8375 -- default alignments for the target. Also, if the alignment is
8376 -- not known, the front end can't know in any case. If a copy is
8377 -- needed, the back-end will take care of it. This whole section
8378 -- including this comment can be removed later ???
8380 -- If the component reference is for a record that has a specified
8381 -- alignment, and we either know it is too small, or cannot tell,
8382 -- then the component may be unaligned.
8384 -- What is the following commented out code ???
8386 -- if Known_Alignment (Etype (P))
8387 -- and then Alignment (Etype (P)) < Ttypes.Maximum_Alignment
8388 -- and then M > Alignment (Etype (P))
8393 -- Case of component clause present which may specify an
8394 -- unaligned position.
8396 if Present
(Component_Clause
(C
)) then
8398 -- Otherwise we can do a test to make sure that the actual
8399 -- start position in the record, and the length, are both
8400 -- consistent with the required alignment. If not, we know
8401 -- that we are unaligned.
8404 Align_In_Bits
: constant Nat
:= M
* System_Storage_Unit
;
8406 if Component_Bit_Offset
(C
) mod Align_In_Bits
/= 0
8407 or else Esize
(C
) mod Align_In_Bits
/= 0
8414 -- Otherwise, for a component reference, test prefix
8416 return Is_Possibly_Unaligned_Object
(P
);
8419 -- If not a component reference, must be aligned
8424 end Is_Possibly_Unaligned_Object
;
8426 ---------------------------------
8427 -- Is_Possibly_Unaligned_Slice --
8428 ---------------------------------
8430 function Is_Possibly_Unaligned_Slice
(N
: Node_Id
) return Boolean is
8432 -- Go to renamed object
8434 if Is_Entity_Name
(N
)
8435 and then Is_Object
(Entity
(N
))
8436 and then Present
(Renamed_Object
(Entity
(N
)))
8438 return Is_Possibly_Unaligned_Slice
(Renamed_Object
(Entity
(N
)));
8441 -- The reference must be a slice
8443 if Nkind
(N
) /= N_Slice
then
8447 -- We only need to worry if the target has strict alignment
8449 if not Target_Strict_Alignment
then
8453 -- If it is a slice, then look at the array type being sliced
8456 Sarr
: constant Node_Id
:= Prefix
(N
);
8457 -- Prefix of the slice, i.e. the array being sliced
8459 Styp
: constant Entity_Id
:= Etype
(Prefix
(N
));
8460 -- Type of the array being sliced
8466 -- The problems arise if the array object that is being sliced
8467 -- is a component of a record or array, and we cannot guarantee
8468 -- the alignment of the array within its containing object.
8470 -- To investigate this, we look at successive prefixes to see
8471 -- if we have a worrisome indexed or selected component.
8475 -- Case of array is part of an indexed component reference
8477 if Nkind
(Pref
) = N_Indexed_Component
then
8478 Ptyp
:= Etype
(Prefix
(Pref
));
8480 -- The only problematic case is when the array is packed, in
8481 -- which case we really know nothing about the alignment of
8482 -- individual components.
8484 if Is_Bit_Packed_Array
(Ptyp
) then
8488 -- Case of array is part of a selected component reference
8490 elsif Nkind
(Pref
) = N_Selected_Component
then
8491 Ptyp
:= Etype
(Prefix
(Pref
));
8493 -- We are definitely in trouble if the record in question
8494 -- has an alignment, and either we know this alignment is
8495 -- inconsistent with the alignment of the slice, or we don't
8496 -- know what the alignment of the slice should be.
8498 if Known_Alignment
(Ptyp
)
8499 and then (Unknown_Alignment
(Styp
)
8500 or else Alignment
(Styp
) > Alignment
(Ptyp
))
8505 -- We are in potential trouble if the record type is packed.
8506 -- We could special case when we know that the array is the
8507 -- first component, but that's not such a simple case ???
8509 if Is_Packed
(Ptyp
) then
8513 -- We are in trouble if there is a component clause, and
8514 -- either we do not know the alignment of the slice, or
8515 -- the alignment of the slice is inconsistent with the
8516 -- bit position specified by the component clause.
8519 Field
: constant Entity_Id
:= Entity
(Selector_Name
(Pref
));
8521 if Present
(Component_Clause
(Field
))
8523 (Unknown_Alignment
(Styp
)
8525 (Component_Bit_Offset
(Field
) mod
8526 (System_Storage_Unit
* Alignment
(Styp
))) /= 0)
8532 -- For cases other than selected or indexed components we know we
8533 -- are OK, since no issues arise over alignment.
8539 -- We processed an indexed component or selected component
8540 -- reference that looked safe, so keep checking prefixes.
8542 Pref
:= Prefix
(Pref
);
8545 end Is_Possibly_Unaligned_Slice
;
8547 -------------------------------
8548 -- Is_Related_To_Func_Return --
8549 -------------------------------
8551 function Is_Related_To_Func_Return
(Id
: Entity_Id
) return Boolean is
8552 Expr
: constant Node_Id
:= Related_Expression
(Id
);
8556 and then Nkind
(Expr
) = N_Explicit_Dereference
8557 and then Nkind
(Parent
(Expr
)) = N_Simple_Return_Statement
;
8558 end Is_Related_To_Func_Return
;
8560 --------------------------------
8561 -- Is_Ref_To_Bit_Packed_Array --
8562 --------------------------------
8564 function Is_Ref_To_Bit_Packed_Array
(N
: Node_Id
) return Boolean is
8569 if Is_Entity_Name
(N
)
8570 and then Is_Object
(Entity
(N
))
8571 and then Present
(Renamed_Object
(Entity
(N
)))
8573 return Is_Ref_To_Bit_Packed_Array
(Renamed_Object
(Entity
(N
)));
8576 if Nkind_In
(N
, N_Indexed_Component
, N_Selected_Component
) then
8577 if Is_Bit_Packed_Array
(Etype
(Prefix
(N
))) then
8580 Result
:= Is_Ref_To_Bit_Packed_Array
(Prefix
(N
));
8583 if Result
and then Nkind
(N
) = N_Indexed_Component
then
8584 Expr
:= First
(Expressions
(N
));
8585 while Present
(Expr
) loop
8586 Force_Evaluation
(Expr
);
8596 end Is_Ref_To_Bit_Packed_Array
;
8598 --------------------------------
8599 -- Is_Ref_To_Bit_Packed_Slice --
8600 --------------------------------
8602 function Is_Ref_To_Bit_Packed_Slice
(N
: Node_Id
) return Boolean is
8604 if Nkind
(N
) = N_Type_Conversion
then
8605 return Is_Ref_To_Bit_Packed_Slice
(Expression
(N
));
8607 elsif Is_Entity_Name
(N
)
8608 and then Is_Object
(Entity
(N
))
8609 and then Present
(Renamed_Object
(Entity
(N
)))
8611 return Is_Ref_To_Bit_Packed_Slice
(Renamed_Object
(Entity
(N
)));
8613 elsif Nkind
(N
) = N_Slice
8614 and then Is_Bit_Packed_Array
(Etype
(Prefix
(N
)))
8618 elsif Nkind_In
(N
, N_Indexed_Component
, N_Selected_Component
) then
8619 return Is_Ref_To_Bit_Packed_Slice
(Prefix
(N
));
8624 end Is_Ref_To_Bit_Packed_Slice
;
8626 -----------------------
8627 -- Is_Renamed_Object --
8628 -----------------------
8630 function Is_Renamed_Object
(N
: Node_Id
) return Boolean is
8631 Pnod
: constant Node_Id
:= Parent
(N
);
8632 Kind
: constant Node_Kind
:= Nkind
(Pnod
);
8634 if Kind
= N_Object_Renaming_Declaration
then
8636 elsif Nkind_In
(Kind
, N_Indexed_Component
, N_Selected_Component
) then
8637 return Is_Renamed_Object
(Pnod
);
8641 end Is_Renamed_Object
;
8643 --------------------------------------
8644 -- Is_Secondary_Stack_BIP_Func_Call --
8645 --------------------------------------
8647 function Is_Secondary_Stack_BIP_Func_Call
(Expr
: Node_Id
) return Boolean is
8648 Alloc_Nam
: Name_Id
:= No_Name
;
8650 Call
: Node_Id
:= Expr
;
8655 -- Build-in-place calls usually appear in 'reference format. Note that
8656 -- the accessibility check machinery may add an extra 'reference due to
8657 -- side effect removal.
8659 while Nkind
(Call
) = N_Reference
loop
8660 Call
:= Prefix
(Call
);
8663 Call
:= Unqual_Conv
(Call
);
8665 if Is_Build_In_Place_Function_Call
(Call
) then
8667 -- Examine all parameter associations of the function call
8669 Param
:= First
(Parameter_Associations
(Call
));
8670 while Present
(Param
) loop
8671 if Nkind
(Param
) = N_Parameter_Association
then
8672 Formal
:= Selector_Name
(Param
);
8673 Actual
:= Explicit_Actual_Parameter
(Param
);
8675 -- Construct the name of formal BIPalloc. It is much easier to
8676 -- extract the name of the function using an arbitrary formal's
8677 -- scope rather than the Name field of Call.
8679 if Alloc_Nam
= No_Name
and then Present
(Entity
(Formal
)) then
8682 (Chars
(Scope
(Entity
(Formal
))),
8683 BIP_Formal_Suffix
(BIP_Alloc_Form
));
8686 -- A match for BIPalloc => 2 has been found
8688 if Chars
(Formal
) = Alloc_Nam
8689 and then Nkind
(Actual
) = N_Integer_Literal
8690 and then Intval
(Actual
) = Uint_2
8701 end Is_Secondary_Stack_BIP_Func_Call
;
8703 -------------------------------------
8704 -- Is_Tag_To_Class_Wide_Conversion --
8705 -------------------------------------
8707 function Is_Tag_To_Class_Wide_Conversion
8708 (Obj_Id
: Entity_Id
) return Boolean
8710 Expr
: constant Node_Id
:= Expression
(Parent
(Obj_Id
));
8714 Is_Class_Wide_Type
(Etype
(Obj_Id
))
8715 and then Present
(Expr
)
8716 and then Nkind
(Expr
) = N_Unchecked_Type_Conversion
8717 and then Etype
(Expression
(Expr
)) = RTE
(RE_Tag
);
8718 end Is_Tag_To_Class_Wide_Conversion
;
8720 ----------------------------
8721 -- Is_Untagged_Derivation --
8722 ----------------------------
8724 function Is_Untagged_Derivation
(T
: Entity_Id
) return Boolean is
8726 return (not Is_Tagged_Type
(T
) and then Is_Derived_Type
(T
))
8728 (Is_Private_Type
(T
) and then Present
(Full_View
(T
))
8729 and then not Is_Tagged_Type
(Full_View
(T
))
8730 and then Is_Derived_Type
(Full_View
(T
))
8731 and then Etype
(Full_View
(T
)) /= T
);
8732 end Is_Untagged_Derivation
;
8734 ------------------------------------
8735 -- Is_Untagged_Private_Derivation --
8736 ------------------------------------
8738 function Is_Untagged_Private_Derivation
8739 (Priv_Typ
: Entity_Id
;
8740 Full_Typ
: Entity_Id
) return Boolean
8745 and then Is_Untagged_Derivation
(Priv_Typ
)
8746 and then Is_Private_Type
(Etype
(Priv_Typ
))
8747 and then Present
(Full_Typ
)
8748 and then Is_Itype
(Full_Typ
);
8749 end Is_Untagged_Private_Derivation
;
8751 ------------------------------
8752 -- Is_Verifiable_DIC_Pragma --
8753 ------------------------------
8755 function Is_Verifiable_DIC_Pragma
(Prag
: Node_Id
) return Boolean is
8756 Args
: constant List_Id
:= Pragma_Argument_Associations
(Prag
);
8759 -- To qualify as verifiable, a DIC pragma must have a non-null argument
8763 and then Nkind
(Get_Pragma_Arg
(First
(Args
))) /= N_Null
;
8764 end Is_Verifiable_DIC_Pragma
;
8766 ---------------------------
8767 -- Is_Volatile_Reference --
8768 ---------------------------
8770 function Is_Volatile_Reference
(N
: Node_Id
) return Boolean is
8772 -- Only source references are to be treated as volatile, internally
8773 -- generated stuff cannot have volatile external effects.
8775 if not Comes_From_Source
(N
) then
8778 -- Never true for reference to a type
8780 elsif Is_Entity_Name
(N
) and then Is_Type
(Entity
(N
)) then
8783 -- Never true for a compile time known constant
8785 elsif Compile_Time_Known_Value
(N
) then
8788 -- True if object reference with volatile type
8790 elsif Is_Volatile_Object
(N
) then
8793 -- True if reference to volatile entity
8795 elsif Is_Entity_Name
(N
) then
8796 return Treat_As_Volatile
(Entity
(N
));
8798 -- True for slice of volatile array
8800 elsif Nkind
(N
) = N_Slice
then
8801 return Is_Volatile_Reference
(Prefix
(N
));
8803 -- True if volatile component
8805 elsif Nkind_In
(N
, N_Indexed_Component
, N_Selected_Component
) then
8806 if (Is_Entity_Name
(Prefix
(N
))
8807 and then Has_Volatile_Components
(Entity
(Prefix
(N
))))
8808 or else (Present
(Etype
(Prefix
(N
)))
8809 and then Has_Volatile_Components
(Etype
(Prefix
(N
))))
8813 return Is_Volatile_Reference
(Prefix
(N
));
8821 end Is_Volatile_Reference
;
8823 --------------------
8824 -- Kill_Dead_Code --
8825 --------------------
8827 procedure Kill_Dead_Code
(N
: Node_Id
; Warn
: Boolean := False) is
8828 W
: Boolean := Warn
;
8829 -- Set False if warnings suppressed
8833 Remove_Warning_Messages
(N
);
8835 -- Update the internal structures of the ABE mechanism in case the
8836 -- dead node is an elaboration scenario.
8838 Kill_Elaboration_Scenario
(N
);
8840 -- Generate warning if appropriate
8844 -- We suppress the warning if this code is under control of an
8845 -- if statement, whose condition is a simple identifier, and
8846 -- either we are in an instance, or warnings off is set for this
8847 -- identifier. The reason for killing it in the instance case is
8848 -- that it is common and reasonable for code to be deleted in
8849 -- instances for various reasons.
8851 -- Could we use Is_Statically_Unevaluated here???
8853 if Nkind
(Parent
(N
)) = N_If_Statement
then
8855 C
: constant Node_Id
:= Condition
(Parent
(N
));
8857 if Nkind
(C
) = N_Identifier
8860 or else (Present
(Entity
(C
))
8861 and then Has_Warnings_Off
(Entity
(C
))))
8868 -- Generate warning if not suppressed
8872 ("?t?this code can never be executed and has been deleted!",
8877 -- Recurse into block statements and bodies to process declarations
8880 if Nkind
(N
) = N_Block_Statement
8881 or else Nkind
(N
) = N_Subprogram_Body
8882 or else Nkind
(N
) = N_Package_Body
8884 Kill_Dead_Code
(Declarations
(N
), False);
8885 Kill_Dead_Code
(Statements
(Handled_Statement_Sequence
(N
)));
8887 if Nkind
(N
) = N_Subprogram_Body
then
8888 Set_Is_Eliminated
(Defining_Entity
(N
));
8891 elsif Nkind
(N
) = N_Package_Declaration
then
8892 Kill_Dead_Code
(Visible_Declarations
(Specification
(N
)));
8893 Kill_Dead_Code
(Private_Declarations
(Specification
(N
)));
8895 -- ??? After this point, Delete_Tree has been called on all
8896 -- declarations in Specification (N), so references to entities
8897 -- therein look suspicious.
8900 E
: Entity_Id
:= First_Entity
(Defining_Entity
(N
));
8903 while Present
(E
) loop
8904 if Ekind
(E
) = E_Operator
then
8905 Set_Is_Eliminated
(E
);
8912 -- Recurse into composite statement to kill individual statements in
8913 -- particular instantiations.
8915 elsif Nkind
(N
) = N_If_Statement
then
8916 Kill_Dead_Code
(Then_Statements
(N
));
8917 Kill_Dead_Code
(Elsif_Parts
(N
));
8918 Kill_Dead_Code
(Else_Statements
(N
));
8920 elsif Nkind
(N
) = N_Loop_Statement
then
8921 Kill_Dead_Code
(Statements
(N
));
8923 elsif Nkind
(N
) = N_Case_Statement
then
8927 Alt
:= First
(Alternatives
(N
));
8928 while Present
(Alt
) loop
8929 Kill_Dead_Code
(Statements
(Alt
));
8934 elsif Nkind
(N
) = N_Case_Statement_Alternative
then
8935 Kill_Dead_Code
(Statements
(N
));
8937 -- Deal with dead instances caused by deleting instantiations
8939 elsif Nkind
(N
) in N_Generic_Instantiation
then
8940 Remove_Dead_Instance
(N
);
8945 -- Case where argument is a list of nodes to be killed
8947 procedure Kill_Dead_Code
(L
: List_Id
; Warn
: Boolean := False) is
8954 if Is_Non_Empty_List
(L
) then
8956 while Present
(N
) loop
8957 Kill_Dead_Code
(N
, W
);
8964 ------------------------
8965 -- Known_Non_Negative --
8966 ------------------------
8968 function Known_Non_Negative
(Opnd
: Node_Id
) return Boolean is
8970 if Is_OK_Static_Expression
(Opnd
) and then Expr_Value
(Opnd
) >= 0 then
8975 Lo
: constant Node_Id
:= Type_Low_Bound
(Etype
(Opnd
));
8978 Is_OK_Static_Expression
(Lo
) and then Expr_Value
(Lo
) >= 0;
8981 end Known_Non_Negative
;
8983 -----------------------------
8984 -- Make_CW_Equivalent_Type --
8985 -----------------------------
8987 -- Create a record type used as an equivalent of any member of the class
8988 -- which takes its size from exp.
8990 -- Generate the following code:
8992 -- type Equiv_T is record
8993 -- _parent : T (List of discriminant constraints taken from Exp);
8994 -- Ext__50 : Storage_Array (1 .. (Exp'size - Typ'object_size)/8);
8997 -- ??? Note that this type does not guarantee same alignment as all
9000 function Make_CW_Equivalent_Type
9002 E
: Node_Id
) return Entity_Id
9004 Loc
: constant Source_Ptr
:= Sloc
(E
);
9005 Root_Typ
: constant Entity_Id
:= Root_Type
(T
);
9006 List_Def
: constant List_Id
:= Empty_List
;
9007 Comp_List
: constant List_Id
:= New_List
;
9008 Equiv_Type
: Entity_Id
;
9009 Range_Type
: Entity_Id
;
9010 Str_Type
: Entity_Id
;
9011 Constr_Root
: Entity_Id
;
9015 -- If the root type is already constrained, there are no discriminants
9016 -- in the expression.
9018 if not Has_Discriminants
(Root_Typ
)
9019 or else Is_Constrained
(Root_Typ
)
9021 Constr_Root
:= Root_Typ
;
9023 -- At this point in the expansion, non-limited view of the type
9024 -- must be available, otherwise the error will be reported later.
9026 if From_Limited_With
(Constr_Root
)
9027 and then Present
(Non_Limited_View
(Constr_Root
))
9029 Constr_Root
:= Non_Limited_View
(Constr_Root
);
9033 Constr_Root
:= Make_Temporary
(Loc
, 'R');
9035 -- subtype cstr__n is T (List of discr constraints taken from Exp)
9037 Append_To
(List_Def
,
9038 Make_Subtype_Declaration
(Loc
,
9039 Defining_Identifier
=> Constr_Root
,
9040 Subtype_Indication
=> Make_Subtype_From_Expr
(E
, Root_Typ
)));
9043 -- Generate the range subtype declaration
9045 Range_Type
:= Make_Temporary
(Loc
, 'G');
9047 if not Is_Interface
(Root_Typ
) then
9049 -- subtype rg__xx is
9050 -- Storage_Offset range 1 .. (Expr'size - typ'size) / Storage_Unit
9053 Make_Op_Subtract
(Loc
,
9055 Make_Attribute_Reference
(Loc
,
9057 OK_Convert_To
(T
, Duplicate_Subexpr_No_Checks
(E
)),
9058 Attribute_Name
=> Name_Size
),
9060 Make_Attribute_Reference
(Loc
,
9061 Prefix
=> New_Occurrence_Of
(Constr_Root
, Loc
),
9062 Attribute_Name
=> Name_Object_Size
));
9064 -- subtype rg__xx is
9065 -- Storage_Offset range 1 .. Expr'size / Storage_Unit
9068 Make_Attribute_Reference
(Loc
,
9070 OK_Convert_To
(T
, Duplicate_Subexpr_No_Checks
(E
)),
9071 Attribute_Name
=> Name_Size
);
9074 Set_Paren_Count
(Sizexpr
, 1);
9076 Append_To
(List_Def
,
9077 Make_Subtype_Declaration
(Loc
,
9078 Defining_Identifier
=> Range_Type
,
9079 Subtype_Indication
=>
9080 Make_Subtype_Indication
(Loc
,
9081 Subtype_Mark
=> New_Occurrence_Of
(RTE
(RE_Storage_Offset
), Loc
),
9082 Constraint
=> Make_Range_Constraint
(Loc
,
9085 Low_Bound
=> Make_Integer_Literal
(Loc
, 1),
9087 Make_Op_Divide
(Loc
,
9088 Left_Opnd
=> Sizexpr
,
9089 Right_Opnd
=> Make_Integer_Literal
(Loc
,
9090 Intval
=> System_Storage_Unit
)))))));
9092 -- subtype str__nn is Storage_Array (rg__x);
9094 Str_Type
:= Make_Temporary
(Loc
, 'S');
9095 Append_To
(List_Def
,
9096 Make_Subtype_Declaration
(Loc
,
9097 Defining_Identifier
=> Str_Type
,
9098 Subtype_Indication
=>
9099 Make_Subtype_Indication
(Loc
,
9100 Subtype_Mark
=> New_Occurrence_Of
(RTE
(RE_Storage_Array
), Loc
),
9102 Make_Index_Or_Discriminant_Constraint
(Loc
,
9104 New_List
(New_Occurrence_Of
(Range_Type
, Loc
))))));
9106 -- type Equiv_T is record
9107 -- [ _parent : Tnn; ]
9111 Equiv_Type
:= Make_Temporary
(Loc
, 'T');
9112 Set_Ekind
(Equiv_Type
, E_Record_Type
);
9113 Set_Parent_Subtype
(Equiv_Type
, Constr_Root
);
9115 -- Set Is_Class_Wide_Equivalent_Type very early to trigger the special
9116 -- treatment for this type. In particular, even though _parent's type
9117 -- is a controlled type or contains controlled components, we do not
9118 -- want to set Has_Controlled_Component on it to avoid making it gain
9119 -- an unwanted _controller component.
9121 Set_Is_Class_Wide_Equivalent_Type
(Equiv_Type
);
9123 -- A class-wide equivalent type does not require initialization
9125 Set_Suppress_Initialization
(Equiv_Type
);
9127 if not Is_Interface
(Root_Typ
) then
9128 Append_To
(Comp_List
,
9129 Make_Component_Declaration
(Loc
,
9130 Defining_Identifier
=>
9131 Make_Defining_Identifier
(Loc
, Name_uParent
),
9132 Component_Definition
=>
9133 Make_Component_Definition
(Loc
,
9134 Aliased_Present
=> False,
9135 Subtype_Indication
=> New_Occurrence_Of
(Constr_Root
, Loc
))));
9138 Append_To
(Comp_List
,
9139 Make_Component_Declaration
(Loc
,
9140 Defining_Identifier
=> Make_Temporary
(Loc
, 'C'),
9141 Component_Definition
=>
9142 Make_Component_Definition
(Loc
,
9143 Aliased_Present
=> False,
9144 Subtype_Indication
=> New_Occurrence_Of
(Str_Type
, Loc
))));
9146 Append_To
(List_Def
,
9147 Make_Full_Type_Declaration
(Loc
,
9148 Defining_Identifier
=> Equiv_Type
,
9150 Make_Record_Definition
(Loc
,
9152 Make_Component_List
(Loc
,
9153 Component_Items
=> Comp_List
,
9154 Variant_Part
=> Empty
))));
9156 -- Suppress all checks during the analysis of the expanded code to avoid
9157 -- the generation of spurious warnings under ZFP run-time.
9159 Insert_Actions
(E
, List_Def
, Suppress
=> All_Checks
);
9161 end Make_CW_Equivalent_Type
;
9163 -------------------------
9164 -- Make_Invariant_Call --
9165 -------------------------
9167 function Make_Invariant_Call
(Expr
: Node_Id
) return Node_Id
is
9168 Loc
: constant Source_Ptr
:= Sloc
(Expr
);
9169 Typ
: constant Entity_Id
:= Base_Type
(Etype
(Expr
));
9171 Proc_Id
: Entity_Id
;
9174 pragma Assert
(Has_Invariants
(Typ
));
9176 Proc_Id
:= Invariant_Procedure
(Typ
);
9177 pragma Assert
(Present
(Proc_Id
));
9180 Make_Procedure_Call_Statement
(Loc
,
9181 Name
=> New_Occurrence_Of
(Proc_Id
, Loc
),
9182 Parameter_Associations
=> New_List
(Relocate_Node
(Expr
)));
9183 end Make_Invariant_Call
;
9185 ------------------------
9186 -- Make_Literal_Range --
9187 ------------------------
9189 function Make_Literal_Range
9191 Literal_Typ
: Entity_Id
) return Node_Id
9193 Lo
: constant Node_Id
:=
9194 New_Copy_Tree
(String_Literal_Low_Bound
(Literal_Typ
));
9195 Index
: constant Entity_Id
:= Etype
(Lo
);
9196 Length_Expr
: constant Node_Id
:=
9197 Make_Op_Subtract
(Loc
,
9199 Make_Integer_Literal
(Loc
,
9200 Intval
=> String_Literal_Length
(Literal_Typ
)),
9201 Right_Opnd
=> Make_Integer_Literal
(Loc
, 1));
9206 Set_Analyzed
(Lo
, False);
9208 if Is_Integer_Type
(Index
) then
9211 Left_Opnd
=> New_Copy_Tree
(Lo
),
9212 Right_Opnd
=> Length_Expr
);
9215 Make_Attribute_Reference
(Loc
,
9216 Attribute_Name
=> Name_Val
,
9217 Prefix
=> New_Occurrence_Of
(Index
, Loc
),
9218 Expressions
=> New_List
(
9221 Make_Attribute_Reference
(Loc
,
9222 Attribute_Name
=> Name_Pos
,
9223 Prefix
=> New_Occurrence_Of
(Index
, Loc
),
9224 Expressions
=> New_List
(New_Copy_Tree
(Lo
))),
9225 Right_Opnd
=> Length_Expr
)));
9232 end Make_Literal_Range
;
9234 --------------------------
9235 -- Make_Non_Empty_Check --
9236 --------------------------
9238 function Make_Non_Empty_Check
9240 N
: Node_Id
) return Node_Id
9246 Make_Attribute_Reference
(Loc
,
9247 Attribute_Name
=> Name_Length
,
9248 Prefix
=> Duplicate_Subexpr_No_Checks
(N
, Name_Req
=> True)),
9250 Make_Integer_Literal
(Loc
, 0));
9251 end Make_Non_Empty_Check
;
9253 -------------------------
9254 -- Make_Predicate_Call --
9255 -------------------------
9257 -- WARNING: This routine manages Ghost regions. Return statements must be
9258 -- replaced by gotos which jump to the end of the routine and restore the
9261 function Make_Predicate_Call
9264 Mem
: Boolean := False) return Node_Id
9266 Loc
: constant Source_Ptr
:= Sloc
(Expr
);
9268 Saved_GM
: constant Ghost_Mode_Type
:= Ghost_Mode
;
9269 Saved_IGR
: constant Node_Id
:= Ignored_Ghost_Region
;
9270 -- Save the Ghost-related attributes to restore on exit
9273 Func_Id
: Entity_Id
;
9276 Func_Id
:= Predicate_Function
(Typ
);
9277 pragma Assert
(Present
(Func_Id
));
9279 -- The related type may be subject to pragma Ghost. Set the mode now to
9280 -- ensure that the call is properly marked as Ghost.
9282 Set_Ghost_Mode
(Typ
);
9284 -- Call special membership version if requested and available
9286 if Mem
and then Present
(Predicate_Function_M
(Typ
)) then
9287 Func_Id
:= Predicate_Function_M
(Typ
);
9290 -- Case of calling normal predicate function
9292 -- If the type is tagged, the expression may be class-wide, in which
9293 -- case it has to be converted to its root type, given that the
9294 -- generated predicate function is not dispatching.
9296 if Is_Tagged_Type
(Typ
) then
9298 Make_Function_Call
(Loc
,
9299 Name
=> New_Occurrence_Of
(Func_Id
, Loc
),
9300 Parameter_Associations
=>
9301 New_List
(Convert_To
(Typ
, Relocate_Node
(Expr
))));
9304 Make_Function_Call
(Loc
,
9305 Name
=> New_Occurrence_Of
(Func_Id
, Loc
),
9306 Parameter_Associations
=> New_List
(Relocate_Node
(Expr
)));
9309 Restore_Ghost_Region
(Saved_GM
, Saved_IGR
);
9312 end Make_Predicate_Call
;
9314 --------------------------
9315 -- Make_Predicate_Check --
9316 --------------------------
9318 function Make_Predicate_Check
9320 Expr
: Node_Id
) return Node_Id
9322 Loc
: constant Source_Ptr
:= Sloc
(Expr
);
9324 procedure Add_Failure_Expression
(Args
: List_Id
);
9325 -- Add the failure expression of pragma Predicate_Failure (if any) to
9328 ----------------------------
9329 -- Add_Failure_Expression --
9330 ----------------------------
9332 procedure Add_Failure_Expression
(Args
: List_Id
) is
9333 function Failure_Expression
return Node_Id
;
9334 pragma Inline
(Failure_Expression
);
9335 -- Find aspect or pragma Predicate_Failure that applies to type Typ
9336 -- and return its expression. Return Empty if no such annotation is
9339 function Is_OK_PF_Aspect
(Asp
: Node_Id
) return Boolean;
9340 pragma Inline
(Is_OK_PF_Aspect
);
9341 -- Determine whether aspect Asp is a suitable Predicate_Failure
9342 -- aspect that applies to type Typ.
9344 function Is_OK_PF_Pragma
(Prag
: Node_Id
) return Boolean;
9345 pragma Inline
(Is_OK_PF_Pragma
);
9346 -- Determine whether pragma Prag is a suitable Predicate_Failure
9347 -- pragma that applies to type Typ.
9349 procedure Replace_Subtype_Reference
(N
: Node_Id
);
9350 -- Replace the current instance of type Typ denoted by N with
9353 ------------------------
9354 -- Failure_Expression --
9355 ------------------------
9357 function Failure_Expression
return Node_Id
is
9361 -- The management of the rep item chain involves "inheritance" of
9362 -- parent type chains. If a parent [sub]type is already subject to
9363 -- pragma Predicate_Failure, then the pragma will also appear in
9364 -- the chain of the child [sub]type, which in turn may possess a
9365 -- pragma of its own. Avoid order-dependent issues by inspecting
9366 -- the rep item chain directly. Note that routine Get_Pragma may
9367 -- return a parent pragma.
9369 Item
:= First_Rep_Item
(Typ
);
9370 while Present
(Item
) loop
9372 -- Predicate_Failure appears as an aspect
9374 if Nkind
(Item
) = N_Aspect_Specification
9375 and then Is_OK_PF_Aspect
(Item
)
9377 return Expression
(Item
);
9379 -- Predicate_Failure appears as a pragma
9381 elsif Nkind
(Item
) = N_Pragma
9382 and then Is_OK_PF_Pragma
(Item
)
9386 (Next
(First
(Pragma_Argument_Associations
(Item
))));
9389 Item
:= Next_Rep_Item
(Item
);
9393 end Failure_Expression
;
9395 ---------------------
9396 -- Is_OK_PF_Aspect --
9397 ---------------------
9399 function Is_OK_PF_Aspect
(Asp
: Node_Id
) return Boolean is
9401 -- To qualify, the aspect must apply to the type subjected to the
9405 Chars
(Identifier
(Asp
)) = Name_Predicate_Failure
9406 and then Present
(Entity
(Asp
))
9407 and then Entity
(Asp
) = Typ
;
9408 end Is_OK_PF_Aspect
;
9410 ---------------------
9411 -- Is_OK_PF_Pragma --
9412 ---------------------
9414 function Is_OK_PF_Pragma
(Prag
: Node_Id
) return Boolean is
9415 Args
: constant List_Id
:= Pragma_Argument_Associations
(Prag
);
9419 -- Nothing to do when the pragma does not denote Predicate_Failure
9421 if Pragma_Name
(Prag
) /= Name_Predicate_Failure
then
9424 -- Nothing to do when the pragma lacks arguments, in which case it
9427 elsif No
(Args
) or else Is_Empty_List
(Args
) then
9431 Typ_Arg
:= Get_Pragma_Arg
(First
(Args
));
9433 -- To qualify, the local name argument of the pragma must denote
9434 -- the type subjected to the predicate check.
9437 Is_Entity_Name
(Typ_Arg
)
9438 and then Present
(Entity
(Typ_Arg
))
9439 and then Entity
(Typ_Arg
) = Typ
;
9440 end Is_OK_PF_Pragma
;
9442 --------------------------------
9443 -- Replace_Subtype_Reference --
9444 --------------------------------
9446 procedure Replace_Subtype_Reference
(N
: Node_Id
) is
9448 Rewrite
(N
, New_Copy_Tree
(Expr
));
9450 -- We want to treat the node as if it comes from source, so that
9451 -- ASIS will not ignore it.
9453 Set_Comes_From_Source
(N
, True);
9454 end Replace_Subtype_Reference
;
9456 procedure Replace_Subtype_References
is
9457 new Replace_Type_References_Generic
(Replace_Subtype_Reference
);
9461 PF_Expr
: constant Node_Id
:= Failure_Expression
;
9464 -- Start of processing for Add_Failure_Expression
9467 if Present
(PF_Expr
) then
9469 -- Replace any occurrences of the current instance of the type
9470 -- with the object subjected to the predicate check.
9472 Expr
:= New_Copy_Tree
(PF_Expr
);
9473 Replace_Subtype_References
(Expr
, Typ
);
9475 -- The failure expression appears as the third argument of the
9479 Make_Pragma_Argument_Association
(Loc
,
9480 Expression
=> Expr
));
9482 end Add_Failure_Expression
;
9489 -- Start of processing for Make_Predicate_Check
9492 -- If predicate checks are suppressed, then return a null statement. For
9493 -- this call, we check only the scope setting. If the caller wants to
9494 -- check a specific entity's setting, they must do it manually.
9496 if Predicate_Checks_Suppressed
(Empty
) then
9497 return Make_Null_Statement
(Loc
);
9500 -- Do not generate a check within an internal subprogram (stream
9501 -- functions and the like, including including predicate functions).
9503 if Within_Internal_Subprogram
then
9504 return Make_Null_Statement
(Loc
);
9507 -- Compute proper name to use, we need to get this right so that the
9508 -- right set of check policies apply to the Check pragma we are making.
9510 if Has_Dynamic_Predicate_Aspect
(Typ
) then
9511 Nam
:= Name_Dynamic_Predicate
;
9512 elsif Has_Static_Predicate_Aspect
(Typ
) then
9513 Nam
:= Name_Static_Predicate
;
9515 Nam
:= Name_Predicate
;
9519 Make_Pragma_Argument_Association
(Loc
,
9520 Expression
=> Make_Identifier
(Loc
, Nam
)),
9521 Make_Pragma_Argument_Association
(Loc
,
9522 Expression
=> Make_Predicate_Call
(Typ
, Expr
)));
9524 -- If the subtype is subject to pragma Predicate_Failure, add the
9525 -- failure expression as an additional parameter.
9527 Add_Failure_Expression
(Args
);
9531 Chars
=> Name_Check
,
9532 Pragma_Argument_Associations
=> Args
);
9533 end Make_Predicate_Check
;
9535 ----------------------------
9536 -- Make_Subtype_From_Expr --
9537 ----------------------------
9539 -- 1. If Expr is an unconstrained array expression, creates
9540 -- Unc_Type(Expr'first(1)..Expr'last(1),..., Expr'first(n)..Expr'last(n))
9542 -- 2. If Expr is a unconstrained discriminated type expression, creates
9543 -- Unc_Type(Expr.Discr1, ... , Expr.Discr_n)
9545 -- 3. If Expr is class-wide, creates an implicit class-wide subtype
9547 function Make_Subtype_From_Expr
9549 Unc_Typ
: Entity_Id
;
9550 Related_Id
: Entity_Id
:= Empty
) return Node_Id
9552 List_Constr
: constant List_Id
:= New_List
;
9553 Loc
: constant Source_Ptr
:= Sloc
(E
);
9556 Full_Subtyp
: Entity_Id
;
9557 High_Bound
: Entity_Id
;
9558 Index_Typ
: Entity_Id
;
9559 Low_Bound
: Entity_Id
;
9560 Priv_Subtyp
: Entity_Id
;
9564 if Is_Private_Type
(Unc_Typ
)
9565 and then Has_Unknown_Discriminants
(Unc_Typ
)
9567 -- The caller requests a unique external name for both the private
9568 -- and the full subtype.
9570 if Present
(Related_Id
) then
9572 Make_Defining_Identifier
(Loc
,
9573 Chars
=> New_External_Name
(Chars
(Related_Id
), 'C'));
9575 Make_Defining_Identifier
(Loc
,
9576 Chars
=> New_External_Name
(Chars
(Related_Id
), 'P'));
9579 Full_Subtyp
:= Make_Temporary
(Loc
, 'C');
9580 Priv_Subtyp
:= Make_Temporary
(Loc
, 'P');
9583 -- Prepare the subtype completion. Use the base type to find the
9584 -- underlying type because the type may be a generic actual or an
9585 -- explicit subtype.
9587 Utyp
:= Underlying_Type
(Base_Type
(Unc_Typ
));
9590 Unchecked_Convert_To
(Utyp
, Duplicate_Subexpr_No_Checks
(E
));
9591 Set_Parent
(Full_Exp
, Parent
(E
));
9594 Make_Subtype_Declaration
(Loc
,
9595 Defining_Identifier
=> Full_Subtyp
,
9596 Subtype_Indication
=> Make_Subtype_From_Expr
(Full_Exp
, Utyp
)));
9598 -- Define the dummy private subtype
9600 Set_Ekind
(Priv_Subtyp
, Subtype_Kind
(Ekind
(Unc_Typ
)));
9601 Set_Etype
(Priv_Subtyp
, Base_Type
(Unc_Typ
));
9602 Set_Scope
(Priv_Subtyp
, Full_Subtyp
);
9603 Set_Is_Constrained
(Priv_Subtyp
);
9604 Set_Is_Tagged_Type
(Priv_Subtyp
, Is_Tagged_Type
(Unc_Typ
));
9605 Set_Is_Itype
(Priv_Subtyp
);
9606 Set_Associated_Node_For_Itype
(Priv_Subtyp
, E
);
9608 if Is_Tagged_Type
(Priv_Subtyp
) then
9610 (Base_Type
(Priv_Subtyp
), Class_Wide_Type
(Unc_Typ
));
9611 Set_Direct_Primitive_Operations
(Priv_Subtyp
,
9612 Direct_Primitive_Operations
(Unc_Typ
));
9615 Set_Full_View
(Priv_Subtyp
, Full_Subtyp
);
9617 return New_Occurrence_Of
(Priv_Subtyp
, Loc
);
9619 elsif Is_Array_Type
(Unc_Typ
) then
9620 Index_Typ
:= First_Index
(Unc_Typ
);
9621 for J
in 1 .. Number_Dimensions
(Unc_Typ
) loop
9623 -- Capture the bounds of each index constraint in case the context
9624 -- is an object declaration of an unconstrained type initialized
9625 -- by a function call:
9627 -- Obj : Unconstr_Typ := Func_Call;
9629 -- This scenario requires secondary scope management and the index
9630 -- constraint cannot depend on the temporary used to capture the
9631 -- result of the function call.
9634 -- Temp : Unconstr_Typ_Ptr := Func_Call'reference;
9635 -- subtype S is Unconstr_Typ (Temp.all'First .. Temp.all'Last);
9636 -- Obj : S := Temp.all;
9637 -- SS_Release; -- Temp is gone at this point, bounds of S are
9641 -- Low_Bound : constant Base_Type (Index_Typ) := E'First (J);
9643 Low_Bound
:= Make_Temporary
(Loc
, 'B');
9645 Make_Object_Declaration
(Loc
,
9646 Defining_Identifier
=> Low_Bound
,
9647 Object_Definition
=>
9648 New_Occurrence_Of
(Base_Type
(Etype
(Index_Typ
)), Loc
),
9649 Constant_Present
=> True,
9651 Make_Attribute_Reference
(Loc
,
9652 Prefix
=> Duplicate_Subexpr_No_Checks
(E
),
9653 Attribute_Name
=> Name_First
,
9654 Expressions
=> New_List
(
9655 Make_Integer_Literal
(Loc
, J
)))));
9658 -- High_Bound : constant Base_Type (Index_Typ) := E'Last (J);
9660 High_Bound
:= Make_Temporary
(Loc
, 'B');
9662 Make_Object_Declaration
(Loc
,
9663 Defining_Identifier
=> High_Bound
,
9664 Object_Definition
=>
9665 New_Occurrence_Of
(Base_Type
(Etype
(Index_Typ
)), Loc
),
9666 Constant_Present
=> True,
9668 Make_Attribute_Reference
(Loc
,
9669 Prefix
=> Duplicate_Subexpr_No_Checks
(E
),
9670 Attribute_Name
=> Name_Last
,
9671 Expressions
=> New_List
(
9672 Make_Integer_Literal
(Loc
, J
)))));
9674 Append_To
(List_Constr
,
9676 Low_Bound
=> New_Occurrence_Of
(Low_Bound
, Loc
),
9677 High_Bound
=> New_Occurrence_Of
(High_Bound
, Loc
)));
9679 Index_Typ
:= Next_Index
(Index_Typ
);
9682 elsif Is_Class_Wide_Type
(Unc_Typ
) then
9684 CW_Subtype
: Entity_Id
;
9685 EQ_Typ
: Entity_Id
:= Empty
;
9688 -- A class-wide equivalent type is not needed on VM targets
9689 -- because the VM back-ends handle the class-wide object
9690 -- initialization itself (and doesn't need or want the
9691 -- additional intermediate type to handle the assignment).
9693 if Expander_Active
and then Tagged_Type_Expansion
then
9695 -- If this is the class-wide type of a completion that is a
9696 -- record subtype, set the type of the class-wide type to be
9697 -- the full base type, for use in the expanded code for the
9698 -- equivalent type. Should this be done earlier when the
9699 -- completion is analyzed ???
9701 if Is_Private_Type
(Etype
(Unc_Typ
))
9703 Ekind
(Full_View
(Etype
(Unc_Typ
))) = E_Record_Subtype
9705 Set_Etype
(Unc_Typ
, Base_Type
(Full_View
(Etype
(Unc_Typ
))));
9708 EQ_Typ
:= Make_CW_Equivalent_Type
(Unc_Typ
, E
);
9711 CW_Subtype
:= New_Class_Wide_Subtype
(Unc_Typ
, E
);
9712 Set_Equivalent_Type
(CW_Subtype
, EQ_Typ
);
9713 Set_Cloned_Subtype
(CW_Subtype
, Base_Type
(Unc_Typ
));
9715 return New_Occurrence_Of
(CW_Subtype
, Loc
);
9718 -- Indefinite record type with discriminants
9721 D
:= First_Discriminant
(Unc_Typ
);
9722 while Present
(D
) loop
9723 Append_To
(List_Constr
,
9724 Make_Selected_Component
(Loc
,
9725 Prefix
=> Duplicate_Subexpr_No_Checks
(E
),
9726 Selector_Name
=> New_Occurrence_Of
(D
, Loc
)));
9728 Next_Discriminant
(D
);
9733 Make_Subtype_Indication
(Loc
,
9734 Subtype_Mark
=> New_Occurrence_Of
(Unc_Typ
, Loc
),
9736 Make_Index_Or_Discriminant_Constraint
(Loc
,
9737 Constraints
=> List_Constr
));
9738 end Make_Subtype_From_Expr
;
9744 procedure Map_Types
(Parent_Type
: Entity_Id
; Derived_Type
: Entity_Id
) is
9746 -- NOTE: Most of the routines in Map_Types are intentionally unnested to
9747 -- avoid deep indentation of code.
9749 -- NOTE: Routines which deal with discriminant mapping operate on the
9750 -- [underlying/record] full view of various types because those views
9751 -- contain all discriminants and stored constraints.
9753 procedure Add_Primitive
(Prim
: Entity_Id
; Par_Typ
: Entity_Id
);
9754 -- Subsidiary to Map_Primitives. Find a primitive in the inheritance or
9755 -- overriding chain starting from Prim whose dispatching type is parent
9756 -- type Par_Typ and add a mapping between the result and primitive Prim.
9758 function Ancestor_Primitive
(Subp
: Entity_Id
) return Entity_Id
;
9759 -- Subsidiary to Map_Primitives. Return the next ancestor primitive in
9760 -- the inheritance or overriding chain of subprogram Subp. Return Empty
9761 -- if no such primitive is available.
9763 function Build_Chain
9764 (Par_Typ
: Entity_Id
;
9765 Deriv_Typ
: Entity_Id
) return Elist_Id
;
9766 -- Subsidiary to Map_Discriminants. Recreate the derivation chain from
9767 -- parent type Par_Typ leading down towards derived type Deriv_Typ. The
9768 -- list has the form:
9772 -- <Ancestor_N> -> <Ancestor_N-1> -> <Ancestor_1> -> Deriv_Typ
9774 -- Note that Par_Typ is not part of the resulting derivation chain
9776 function Discriminated_View
(Typ
: Entity_Id
) return Entity_Id
;
9777 -- Return the view of type Typ which could potentially contains either
9778 -- the discriminants or stored constraints of the type.
9780 function Find_Discriminant_Value
9782 Par_Typ
: Entity_Id
;
9783 Deriv_Typ
: Entity_Id
;
9784 Typ_Elmt
: Elmt_Id
) return Node_Or_Entity_Id
;
9785 -- Subsidiary to Map_Discriminants. Find the value of discriminant Discr
9786 -- in the derivation chain starting from parent type Par_Typ leading to
9787 -- derived type Deriv_Typ. The returned value is one of the following:
9789 -- * An entity which is either a discriminant or a non-discriminant
9790 -- name, and renames/constraints Discr.
9792 -- * An expression which constraints Discr
9794 -- Typ_Elmt is an element of the derivation chain created by routine
9795 -- Build_Chain and denotes the current ancestor being examined.
9797 procedure Map_Discriminants
9798 (Par_Typ
: Entity_Id
;
9799 Deriv_Typ
: Entity_Id
);
9800 -- Map each discriminant of type Par_Typ to a meaningful constraint
9801 -- from the point of view of type Deriv_Typ.
9803 procedure Map_Primitives
(Par_Typ
: Entity_Id
; Deriv_Typ
: Entity_Id
);
9804 -- Map each primitive of type Par_Typ to a corresponding primitive of
9811 procedure Add_Primitive
(Prim
: Entity_Id
; Par_Typ
: Entity_Id
) is
9812 Par_Prim
: Entity_Id
;
9815 -- Inspect the inheritance chain through the Alias attribute and the
9816 -- overriding chain through the Overridden_Operation looking for an
9817 -- ancestor primitive with the appropriate dispatching type.
9820 while Present
(Par_Prim
) loop
9821 exit when Find_Dispatching_Type
(Par_Prim
) = Par_Typ
;
9822 Par_Prim
:= Ancestor_Primitive
(Par_Prim
);
9825 -- Create a mapping of the form:
9827 -- parent type primitive -> derived type primitive
9829 if Present
(Par_Prim
) then
9830 Type_Map
.Set
(Par_Prim
, Prim
);
9834 ------------------------
9835 -- Ancestor_Primitive --
9836 ------------------------
9838 function Ancestor_Primitive
(Subp
: Entity_Id
) return Entity_Id
is
9839 Inher_Prim
: constant Entity_Id
:= Alias
(Subp
);
9840 Over_Prim
: constant Entity_Id
:= Overridden_Operation
(Subp
);
9843 -- The current subprogram overrides an ancestor primitive
9845 if Present
(Over_Prim
) then
9848 -- The current subprogram is an internally generated alias of an
9849 -- inherited ancestor primitive.
9851 elsif Present
(Inher_Prim
) then
9854 -- Otherwise the current subprogram is the root of the inheritance or
9855 -- overriding chain.
9860 end Ancestor_Primitive
;
9866 function Build_Chain
9867 (Par_Typ
: Entity_Id
;
9868 Deriv_Typ
: Entity_Id
) return Elist_Id
9870 Anc_Typ
: Entity_Id
;
9872 Curr_Typ
: Entity_Id
;
9875 Chain
:= New_Elmt_List
;
9877 -- Add the derived type to the derivation chain
9879 Prepend_Elmt
(Deriv_Typ
, Chain
);
9881 -- Examine all ancestors starting from the derived type climbing
9882 -- towards parent type Par_Typ.
9884 Curr_Typ
:= Deriv_Typ
;
9886 -- Handle the case where the current type is a record which
9887 -- derives from a subtype.
9889 -- subtype Sub_Typ is Par_Typ ...
9890 -- type Deriv_Typ is Sub_Typ ...
9892 if Ekind
(Curr_Typ
) = E_Record_Type
9893 and then Present
(Parent_Subtype
(Curr_Typ
))
9895 Anc_Typ
:= Parent_Subtype
(Curr_Typ
);
9897 -- Handle the case where the current type is a record subtype of
9900 -- subtype Sub_Typ1 is Par_Typ ...
9901 -- subtype Sub_Typ2 is Sub_Typ1 ...
9903 elsif Ekind
(Curr_Typ
) = E_Record_Subtype
9904 and then Present
(Cloned_Subtype
(Curr_Typ
))
9906 Anc_Typ
:= Cloned_Subtype
(Curr_Typ
);
9908 -- Otherwise use the direct parent type
9911 Anc_Typ
:= Etype
(Curr_Typ
);
9914 -- Use the first subtype when dealing with itypes
9916 if Is_Itype
(Anc_Typ
) then
9917 Anc_Typ
:= First_Subtype
(Anc_Typ
);
9920 -- Work with the view which contains the discriminants and stored
9923 Anc_Typ
:= Discriminated_View
(Anc_Typ
);
9925 -- Stop the climb when either the parent type has been reached or
9926 -- there are no more ancestors left to examine.
9928 exit when Anc_Typ
= Curr_Typ
or else Anc_Typ
= Par_Typ
;
9930 Prepend_Unique_Elmt
(Anc_Typ
, Chain
);
9931 Curr_Typ
:= Anc_Typ
;
9937 ------------------------
9938 -- Discriminated_View --
9939 ------------------------
9941 function Discriminated_View
(Typ
: Entity_Id
) return Entity_Id
is
9947 -- Use the [underlying] full view when dealing with private types
9948 -- because the view contains all inherited discriminants or stored
9951 if Is_Private_Type
(T
) then
9952 if Present
(Underlying_Full_View
(T
)) then
9953 T
:= Underlying_Full_View
(T
);
9955 elsif Present
(Full_View
(T
)) then
9960 -- Use the underlying record view when the type is an extenstion of
9961 -- a parent type with unknown discriminants because the view contains
9962 -- all inherited discriminants or stored constraints.
9964 if Ekind
(T
) = E_Record_Type
9965 and then Present
(Underlying_Record_View
(T
))
9967 T
:= Underlying_Record_View
(T
);
9971 end Discriminated_View
;
9973 -----------------------------
9974 -- Find_Discriminant_Value --
9975 -----------------------------
9977 function Find_Discriminant_Value
9979 Par_Typ
: Entity_Id
;
9980 Deriv_Typ
: Entity_Id
;
9981 Typ_Elmt
: Elmt_Id
) return Node_Or_Entity_Id
9983 Discr_Pos
: constant Uint
:= Discriminant_Number
(Discr
);
9984 Typ
: constant Entity_Id
:= Node
(Typ_Elmt
);
9986 function Find_Constraint_Value
9987 (Constr
: Node_Or_Entity_Id
) return Node_Or_Entity_Id
;
9988 -- Given constraint Constr, find what it denotes. This is either:
9990 -- * An entity which is either a discriminant or a name
9994 ---------------------------
9995 -- Find_Constraint_Value --
9996 ---------------------------
9998 function Find_Constraint_Value
9999 (Constr
: Node_Or_Entity_Id
) return Node_Or_Entity_Id
10002 if Nkind
(Constr
) in N_Entity
then
10004 -- The constraint denotes a discriminant of the curren type
10005 -- which renames the ancestor discriminant:
10008 -- type Typ (D1 : ...; DN : ...) is
10009 -- new Anc (Discr => D1) with ...
10012 if Ekind
(Constr
) = E_Discriminant
then
10014 -- The discriminant belongs to derived type Deriv_Typ. This
10015 -- is the final value for the ancestor discriminant as the
10016 -- derivations chain has been fully exhausted.
10018 if Typ
= Deriv_Typ
then
10021 -- Otherwise the discriminant may be renamed or constrained
10022 -- at a lower level. Continue looking down the derivation
10027 Find_Discriminant_Value
10029 Par_Typ
=> Par_Typ
,
10030 Deriv_Typ
=> Deriv_Typ
,
10031 Typ_Elmt
=> Next_Elmt
(Typ_Elmt
));
10034 -- Otherwise the constraint denotes a reference to some name
10035 -- which results in a Girder discriminant:
10039 -- type Typ (D1 : ...; DN : ...) is
10040 -- new Anc (Discr => Name) with ...
10043 -- Return the name as this is the proper constraint of the
10050 -- The constraint denotes a reference to a name
10052 elsif Is_Entity_Name
(Constr
) then
10053 return Find_Constraint_Value
(Entity
(Constr
));
10055 -- Otherwise the current constraint is an expression which yields
10056 -- a Girder discriminant:
10058 -- type Typ (D1 : ...; DN : ...) is
10059 -- new Anc (Discr => <expression>) with ...
10062 -- Return the expression as this is the proper constraint of the
10068 end Find_Constraint_Value
;
10072 Constrs
: constant Elist_Id
:= Stored_Constraint
(Typ
);
10074 Constr_Elmt
: Elmt_Id
;
10076 Typ_Discr
: Entity_Id
;
10078 -- Start of processing for Find_Discriminant_Value
10081 -- The algorithm for finding the value of a discriminant works as
10082 -- follows. First, it recreates the derivation chain from Par_Typ
10083 -- to Deriv_Typ as a list:
10085 -- Par_Typ (shown for completeness)
10087 -- Ancestor_N <-- head of chain
10091 -- Deriv_Typ <-- tail of chain
10093 -- The algorithm then traces the fate of a parent discriminant down
10094 -- the derivation chain. At each derivation level, the discriminant
10095 -- may be either inherited or constrained.
10097 -- 1) Discriminant is inherited: there are two cases, depending on
10098 -- which type is inheriting.
10100 -- 1.1) Deriv_Typ is inheriting:
10102 -- type Ancestor (D_1 : ...) is tagged ...
10103 -- type Deriv_Typ is new Ancestor ...
10105 -- In this case the inherited discriminant is the final value of
10106 -- the parent discriminant because the end of the derivation chain
10107 -- has been reached.
10109 -- 1.2) Some other type is inheriting:
10111 -- type Ancestor_1 (D_1 : ...) is tagged ...
10112 -- type Ancestor_2 is new Ancestor_1 ...
10114 -- In this case the algorithm continues to trace the fate of the
10115 -- inherited discriminant down the derivation chain because it may
10116 -- be further inherited or constrained.
10118 -- 2) Discriminant is constrained: there are three cases, depending
10119 -- on what the constraint is.
10121 -- 2.1) The constraint is another discriminant (aka renaming):
10123 -- type Ancestor_1 (D_1 : ...) is tagged ...
10124 -- type Ancestor_2 (D_2 : ...) is new Ancestor_1 (D_1 => D_2) ...
10126 -- In this case the constraining discriminant becomes the one to
10127 -- track down the derivation chain. The algorithm already knows
10128 -- that D_2 constrains D_1, therefore if the algorithm finds the
10129 -- value of D_2, then this would also be the value for D_1.
10131 -- 2.2) The constraint is a name (aka Girder):
10134 -- type Ancestor_1 (D_1 : ...) is tagged ...
10135 -- type Ancestor_2 is new Ancestor_1 (D_1 => Name) ...
10137 -- In this case the name is the final value of D_1 because the
10138 -- discriminant cannot be further constrained.
10140 -- 2.3) The constraint is an expression (aka Girder):
10142 -- type Ancestor_1 (D_1 : ...) is tagged ...
10143 -- type Ancestor_2 is new Ancestor_1 (D_1 => 1 + 2) ...
10145 -- Similar to 2.2, the expression is the final value of D_1
10149 -- When a derived type constrains its parent type, all constaints
10150 -- appear in the Stored_Constraint list. Examine the list looking
10151 -- for a positional match.
10153 if Present
(Constrs
) then
10154 Constr_Elmt
:= First_Elmt
(Constrs
);
10155 while Present
(Constr_Elmt
) loop
10157 -- The position of the current constraint matches that of the
10158 -- ancestor discriminant.
10160 if Pos
= Discr_Pos
then
10161 return Find_Constraint_Value
(Node
(Constr_Elmt
));
10164 Next_Elmt
(Constr_Elmt
);
10168 -- Otherwise the derived type does not constraint its parent type in
10169 -- which case it inherits the parent discriminants.
10172 Typ_Discr
:= First_Discriminant
(Typ
);
10173 while Present
(Typ_Discr
) loop
10175 -- The position of the current discriminant matches that of the
10176 -- ancestor discriminant.
10178 if Pos
= Discr_Pos
then
10179 return Find_Constraint_Value
(Typ_Discr
);
10182 Next_Discriminant
(Typ_Discr
);
10187 -- A discriminant must always have a corresponding value. This is
10188 -- either another discriminant, a name, or an expression. If this
10189 -- point is reached, them most likely the derivation chain employs
10190 -- the wrong views of types.
10192 pragma Assert
(False);
10195 end Find_Discriminant_Value
;
10197 -----------------------
10198 -- Map_Discriminants --
10199 -----------------------
10201 procedure Map_Discriminants
10202 (Par_Typ
: Entity_Id
;
10203 Deriv_Typ
: Entity_Id
)
10205 Deriv_Chain
: constant Elist_Id
:= Build_Chain
(Par_Typ
, Deriv_Typ
);
10208 Discr_Val
: Node_Or_Entity_Id
;
10211 -- Examine each discriminant of parent type Par_Typ and find a
10212 -- suitable value for it from the point of view of derived type
10215 if Has_Discriminants
(Par_Typ
) then
10216 Discr
:= First_Discriminant
(Par_Typ
);
10217 while Present
(Discr
) loop
10219 Find_Discriminant_Value
10221 Par_Typ
=> Par_Typ
,
10222 Deriv_Typ
=> Deriv_Typ
,
10223 Typ_Elmt
=> First_Elmt
(Deriv_Chain
));
10225 -- Create a mapping of the form:
10227 -- parent type discriminant -> value
10229 Type_Map
.Set
(Discr
, Discr_Val
);
10231 Next_Discriminant
(Discr
);
10234 end Map_Discriminants
;
10236 --------------------
10237 -- Map_Primitives --
10238 --------------------
10240 procedure Map_Primitives
(Par_Typ
: Entity_Id
; Deriv_Typ
: Entity_Id
) is
10241 Deriv_Prim
: Entity_Id
;
10242 Par_Prim
: Entity_Id
;
10243 Par_Prims
: Elist_Id
;
10244 Prim_Elmt
: Elmt_Id
;
10247 -- Inspect the primitives of the derived type and determine whether
10248 -- they relate to the primitives of the parent type. If there is a
10249 -- meaningful relation, create a mapping of the form:
10251 -- parent type primitive -> perived type primitive
10253 if Present
(Direct_Primitive_Operations
(Deriv_Typ
)) then
10254 Prim_Elmt
:= First_Elmt
(Direct_Primitive_Operations
(Deriv_Typ
));
10255 while Present
(Prim_Elmt
) loop
10256 Deriv_Prim
:= Node
(Prim_Elmt
);
10258 if Is_Subprogram
(Deriv_Prim
)
10259 and then Find_Dispatching_Type
(Deriv_Prim
) = Deriv_Typ
10261 Add_Primitive
(Deriv_Prim
, Par_Typ
);
10264 Next_Elmt
(Prim_Elmt
);
10268 -- If the parent operation is an interface operation, the overriding
10269 -- indicator is not present. Instead, we get from the interface
10270 -- operation the primitive of the current type that implements it.
10272 if Is_Interface
(Par_Typ
) then
10273 Par_Prims
:= Collect_Primitive_Operations
(Par_Typ
);
10275 if Present
(Par_Prims
) then
10276 Prim_Elmt
:= First_Elmt
(Par_Prims
);
10278 while Present
(Prim_Elmt
) loop
10279 Par_Prim
:= Node
(Prim_Elmt
);
10281 Find_Primitive_Covering_Interface
(Deriv_Typ
, Par_Prim
);
10283 if Present
(Deriv_Prim
) then
10284 Type_Map
.Set
(Par_Prim
, Deriv_Prim
);
10287 Next_Elmt
(Prim_Elmt
);
10291 end Map_Primitives
;
10293 -- Start of processing for Map_Types
10296 -- Nothing to do if there are no types to work with
10298 if No
(Parent_Type
) or else No
(Derived_Type
) then
10301 -- Nothing to do if the mapping already exists
10303 elsif Type_Map
.Get
(Parent_Type
) = Derived_Type
then
10306 -- Nothing to do if both types are not tagged. Note that untagged types
10307 -- do not have primitive operations and their discriminants are already
10308 -- handled by gigi.
10310 elsif not Is_Tagged_Type
(Parent_Type
)
10311 or else not Is_Tagged_Type
(Derived_Type
)
10316 -- Create a mapping of the form
10318 -- parent type -> derived type
10320 -- to prevent any subsequent attempts to produce the same relations
10322 Type_Map
.Set
(Parent_Type
, Derived_Type
);
10324 -- Create mappings of the form
10326 -- parent type discriminant -> derived type discriminant
10328 -- parent type discriminant -> constraint
10330 -- Note that mapping of discriminants breaks privacy because it needs to
10331 -- work with those views which contains the discriminants and any stored
10335 (Par_Typ
=> Discriminated_View
(Parent_Type
),
10336 Deriv_Typ
=> Discriminated_View
(Derived_Type
));
10338 -- Create mappings of the form
10340 -- parent type primitive -> derived type primitive
10343 (Par_Typ
=> Parent_Type
,
10344 Deriv_Typ
=> Derived_Type
);
10347 ----------------------------
10348 -- Matching_Standard_Type --
10349 ----------------------------
10351 function Matching_Standard_Type
(Typ
: Entity_Id
) return Entity_Id
is
10352 pragma Assert
(Is_Scalar_Type
(Typ
));
10353 Siz
: constant Uint
:= Esize
(Typ
);
10356 -- Floating-point cases
10358 if Is_Floating_Point_Type
(Typ
) then
10359 if Siz
<= Esize
(Standard_Short_Float
) then
10360 return Standard_Short_Float
;
10361 elsif Siz
<= Esize
(Standard_Float
) then
10362 return Standard_Float
;
10363 elsif Siz
<= Esize
(Standard_Long_Float
) then
10364 return Standard_Long_Float
;
10365 elsif Siz
<= Esize
(Standard_Long_Long_Float
) then
10366 return Standard_Long_Long_Float
;
10368 raise Program_Error
;
10371 -- Integer cases (includes fixed-point types)
10373 -- Unsigned integer cases (includes normal enumeration types)
10375 elsif Is_Unsigned_Type
(Typ
) then
10376 if Siz
<= Esize
(Standard_Short_Short_Unsigned
) then
10377 return Standard_Short_Short_Unsigned
;
10378 elsif Siz
<= Esize
(Standard_Short_Unsigned
) then
10379 return Standard_Short_Unsigned
;
10380 elsif Siz
<= Esize
(Standard_Unsigned
) then
10381 return Standard_Unsigned
;
10382 elsif Siz
<= Esize
(Standard_Long_Unsigned
) then
10383 return Standard_Long_Unsigned
;
10384 elsif Siz
<= Esize
(Standard_Long_Long_Unsigned
) then
10385 return Standard_Long_Long_Unsigned
;
10387 raise Program_Error
;
10390 -- Signed integer cases
10393 if Siz
<= Esize
(Standard_Short_Short_Integer
) then
10394 return Standard_Short_Short_Integer
;
10395 elsif Siz
<= Esize
(Standard_Short_Integer
) then
10396 return Standard_Short_Integer
;
10397 elsif Siz
<= Esize
(Standard_Integer
) then
10398 return Standard_Integer
;
10399 elsif Siz
<= Esize
(Standard_Long_Integer
) then
10400 return Standard_Long_Integer
;
10401 elsif Siz
<= Esize
(Standard_Long_Long_Integer
) then
10402 return Standard_Long_Long_Integer
;
10404 raise Program_Error
;
10407 end Matching_Standard_Type
;
10409 -----------------------------
10410 -- May_Generate_Large_Temp --
10411 -----------------------------
10413 -- At the current time, the only types that we return False for (i.e. where
10414 -- we decide we know they cannot generate large temps) are ones where we
10415 -- know the size is 256 bits or less at compile time, and we are still not
10416 -- doing a thorough job on arrays and records ???
10418 function May_Generate_Large_Temp
(Typ
: Entity_Id
) return Boolean is
10420 if not Size_Known_At_Compile_Time
(Typ
) then
10423 elsif Esize
(Typ
) /= 0 and then Esize
(Typ
) <= 256 then
10426 elsif Is_Array_Type
(Typ
)
10427 and then Present
(Packed_Array_Impl_Type
(Typ
))
10429 return May_Generate_Large_Temp
(Packed_Array_Impl_Type
(Typ
));
10431 -- We could do more here to find other small types ???
10436 end May_Generate_Large_Temp
;
10438 --------------------------------------------
10439 -- Needs_Conditional_Null_Excluding_Check --
10440 --------------------------------------------
10442 function Needs_Conditional_Null_Excluding_Check
10443 (Typ
: Entity_Id
) return Boolean
10447 Is_Array_Type
(Typ
) and then Can_Never_Be_Null
(Component_Type
(Typ
));
10448 end Needs_Conditional_Null_Excluding_Check
;
10450 ----------------------------
10451 -- Needs_Constant_Address --
10452 ----------------------------
10454 function Needs_Constant_Address
10456 Typ
: Entity_Id
) return Boolean
10459 -- If we have no initialization of any kind, then we don't need to place
10460 -- any restrictions on the address clause, because the object will be
10461 -- elaborated after the address clause is evaluated. This happens if the
10462 -- declaration has no initial expression, or the type has no implicit
10463 -- initialization, or the object is imported.
10465 -- The same holds for all initialized scalar types and all access types.
10466 -- Packed bit arrays of size up to 64 are represented using a modular
10467 -- type with an initialization (to zero) and can be processed like other
10468 -- initialized scalar types.
10470 -- If the type is controlled, code to attach the object to a
10471 -- finalization chain is generated at the point of declaration, and
10472 -- therefore the elaboration of the object cannot be delayed: the
10473 -- address expression must be a constant.
10475 if No
(Expression
(Decl
))
10476 and then not Needs_Finalization
(Typ
)
10478 (not Has_Non_Null_Base_Init_Proc
(Typ
)
10479 or else Is_Imported
(Defining_Identifier
(Decl
)))
10483 elsif (Present
(Expression
(Decl
)) and then Is_Scalar_Type
(Typ
))
10484 or else Is_Access_Type
(Typ
)
10486 (Is_Bit_Packed_Array
(Typ
)
10487 and then Is_Modular_Integer_Type
(Packed_Array_Impl_Type
(Typ
)))
10492 -- Otherwise, we require the address clause to be constant because
10493 -- the call to the initialization procedure (or the attach code) has
10494 -- to happen at the point of the declaration.
10496 -- Actually the IP call has been moved to the freeze actions anyway,
10497 -- so maybe we can relax this restriction???
10501 end Needs_Constant_Address
;
10503 ------------------------
10504 -- Needs_Finalization --
10505 ------------------------
10507 function Needs_Finalization
(Typ
: Entity_Id
) return Boolean is
10508 function Has_Some_Controlled_Component
10509 (Input_Typ
: Entity_Id
) return Boolean;
10510 -- Determine whether type Input_Typ has at least one controlled
10513 -----------------------------------
10514 -- Has_Some_Controlled_Component --
10515 -----------------------------------
10517 function Has_Some_Controlled_Component
10518 (Input_Typ
: Entity_Id
) return Boolean
10523 -- When a type is already frozen and has at least one controlled
10524 -- component, or is manually decorated, it is sufficient to inspect
10525 -- flag Has_Controlled_Component.
10527 if Has_Controlled_Component
(Input_Typ
) then
10530 -- Otherwise inspect the internals of the type
10532 elsif not Is_Frozen
(Input_Typ
) then
10533 if Is_Array_Type
(Input_Typ
) then
10534 return Needs_Finalization
(Component_Type
(Input_Typ
));
10536 elsif Is_Record_Type
(Input_Typ
) then
10537 Comp
:= First_Component
(Input_Typ
);
10538 while Present
(Comp
) loop
10539 if Needs_Finalization
(Etype
(Comp
)) then
10543 Next_Component
(Comp
);
10549 end Has_Some_Controlled_Component
;
10551 -- Start of processing for Needs_Finalization
10554 -- Certain run-time configurations and targets do not provide support
10555 -- for controlled types.
10557 if Restriction_Active
(No_Finalization
) then
10560 -- C++ types are not considered controlled. It is assumed that the non-
10561 -- Ada side will handle their clean up.
10563 elsif Convention
(Typ
) = Convention_CPP
then
10566 -- Class-wide types are treated as controlled because derivations from
10567 -- the root type may introduce controlled components.
10569 elsif Is_Class_Wide_Type
(Typ
) then
10572 -- Concurrent types are controlled as long as their corresponding record
10575 elsif Is_Concurrent_Type
(Typ
)
10576 and then Present
(Corresponding_Record_Type
(Typ
))
10577 and then Needs_Finalization
(Corresponding_Record_Type
(Typ
))
10581 -- Otherwise the type is controlled when it is either derived from type
10582 -- [Limited_]Controlled and not subject to aspect Disable_Controlled, or
10583 -- contains at least one controlled component.
10587 Is_Controlled
(Typ
) or else Has_Some_Controlled_Component
(Typ
);
10589 end Needs_Finalization
;
10591 ----------------------------
10592 -- New_Class_Wide_Subtype --
10593 ----------------------------
10595 function New_Class_Wide_Subtype
10596 (CW_Typ
: Entity_Id
;
10597 N
: Node_Id
) return Entity_Id
10599 Res
: constant Entity_Id
:= Create_Itype
(E_Void
, N
);
10601 -- Capture relevant attributes of the class-wide subtype which must be
10602 -- restored after the copy.
10604 Res_Chars
: constant Name_Id
:= Chars
(Res
);
10605 Res_Is_CGE
: constant Boolean := Is_Checked_Ghost_Entity
(Res
);
10606 Res_Is_IGE
: constant Boolean := Is_Ignored_Ghost_Entity
(Res
);
10607 Res_Is_IGN
: constant Boolean := Is_Ignored_Ghost_Node
(Res
);
10608 Res_Scope
: constant Entity_Id
:= Scope
(Res
);
10611 Copy_Node
(CW_Typ
, Res
);
10613 -- Restore the relevant attributes of the class-wide subtype
10615 Set_Chars
(Res
, Res_Chars
);
10616 Set_Is_Checked_Ghost_Entity
(Res
, Res_Is_CGE
);
10617 Set_Is_Ignored_Ghost_Entity
(Res
, Res_Is_IGE
);
10618 Set_Is_Ignored_Ghost_Node
(Res
, Res_Is_IGN
);
10619 Set_Scope
(Res
, Res_Scope
);
10621 -- Decorate the class-wide subtype
10623 Set_Associated_Node_For_Itype
(Res
, N
);
10624 Set_Comes_From_Source
(Res
, False);
10625 Set_Ekind
(Res
, E_Class_Wide_Subtype
);
10626 Set_Etype
(Res
, Base_Type
(CW_Typ
));
10627 Set_Freeze_Node
(Res
, Empty
);
10628 Set_Is_Frozen
(Res
, False);
10629 Set_Is_Itype
(Res
);
10630 Set_Is_Public
(Res
, False);
10631 Set_Next_Entity
(Res
, Empty
);
10632 Set_Prev_Entity
(Res
, Empty
);
10633 Set_Sloc
(Res
, Sloc
(N
));
10635 Set_Public_Status
(Res
);
10638 end New_Class_Wide_Subtype
;
10640 --------------------------------
10641 -- Non_Limited_Designated_Type --
10642 ---------------------------------
10644 function Non_Limited_Designated_Type
(T
: Entity_Id
) return Entity_Id
is
10645 Desig
: constant Entity_Id
:= Designated_Type
(T
);
10647 if Has_Non_Limited_View
(Desig
) then
10648 return Non_Limited_View
(Desig
);
10652 end Non_Limited_Designated_Type
;
10654 -----------------------------------
10655 -- OK_To_Do_Constant_Replacement --
10656 -----------------------------------
10658 function OK_To_Do_Constant_Replacement
(E
: Entity_Id
) return Boolean is
10659 ES
: constant Entity_Id
:= Scope
(E
);
10663 -- Do not replace statically allocated objects, because they may be
10664 -- modified outside the current scope.
10666 if Is_Statically_Allocated
(E
) then
10669 -- Do not replace aliased or volatile objects, since we don't know what
10670 -- else might change the value.
10672 elsif Is_Aliased
(E
) or else Treat_As_Volatile
(E
) then
10675 -- Debug flag -gnatdM disconnects this optimization
10677 elsif Debug_Flag_MM
then
10680 -- Otherwise check scopes
10683 CS
:= Current_Scope
;
10686 -- If we are in right scope, replacement is safe
10691 -- Packages do not affect the determination of safety
10693 elsif Ekind
(CS
) = E_Package
then
10694 exit when CS
= Standard_Standard
;
10697 -- Blocks do not affect the determination of safety
10699 elsif Ekind
(CS
) = E_Block
then
10702 -- Loops do not affect the determination of safety. Note that we
10703 -- kill all current values on entry to a loop, so we are just
10704 -- talking about processing within a loop here.
10706 elsif Ekind
(CS
) = E_Loop
then
10709 -- Otherwise, the reference is dubious, and we cannot be sure that
10710 -- it is safe to do the replacement.
10719 end OK_To_Do_Constant_Replacement
;
10721 ------------------------------------
10722 -- Possible_Bit_Aligned_Component --
10723 ------------------------------------
10725 function Possible_Bit_Aligned_Component
(N
: Node_Id
) return Boolean is
10727 -- Do not process an unanalyzed node because it is not yet decorated and
10728 -- most checks performed below will fail.
10730 if not Analyzed
(N
) then
10734 -- There are never alignment issues in CodePeer mode
10736 if CodePeer_Mode
then
10742 -- Case of indexed component
10744 when N_Indexed_Component
=>
10746 P
: constant Node_Id
:= Prefix
(N
);
10747 Ptyp
: constant Entity_Id
:= Etype
(P
);
10750 -- If we know the component size and it is less than 64, then
10751 -- we are definitely OK. The back end always does assignment of
10752 -- misaligned small objects correctly.
10754 if Known_Static_Component_Size
(Ptyp
)
10755 and then Component_Size
(Ptyp
) <= 64
10759 -- Otherwise, we need to test the prefix, to see if we are
10760 -- indexing from a possibly unaligned component.
10763 return Possible_Bit_Aligned_Component
(P
);
10767 -- Case of selected component
10769 when N_Selected_Component
=>
10771 P
: constant Node_Id
:= Prefix
(N
);
10772 Comp
: constant Entity_Id
:= Entity
(Selector_Name
(N
));
10775 -- If there is no component clause, then we are in the clear
10776 -- since the back end will never misalign a large component
10777 -- unless it is forced to do so. In the clear means we need
10778 -- only the recursive test on the prefix.
10780 if Component_May_Be_Bit_Aligned
(Comp
) then
10783 return Possible_Bit_Aligned_Component
(P
);
10787 -- For a slice, test the prefix, if that is possibly misaligned,
10788 -- then for sure the slice is.
10791 return Possible_Bit_Aligned_Component
(Prefix
(N
));
10793 -- For an unchecked conversion, check whether the expression may
10796 when N_Unchecked_Type_Conversion
=>
10797 return Possible_Bit_Aligned_Component
(Expression
(N
));
10799 -- If we have none of the above, it means that we have fallen off the
10800 -- top testing prefixes recursively, and we now have a stand alone
10801 -- object, where we don't have a problem, unless this is a renaming,
10802 -- in which case we need to look into the renamed object.
10805 if Is_Entity_Name
(N
)
10806 and then Present
(Renamed_Object
(Entity
(N
)))
10809 Possible_Bit_Aligned_Component
(Renamed_Object
(Entity
(N
)));
10814 end Possible_Bit_Aligned_Component
;
10816 -----------------------------------------------
10817 -- Process_Statements_For_Controlled_Objects --
10818 -----------------------------------------------
10820 procedure Process_Statements_For_Controlled_Objects
(N
: Node_Id
) is
10821 Loc
: constant Source_Ptr
:= Sloc
(N
);
10823 function Are_Wrapped
(L
: List_Id
) return Boolean;
10824 -- Determine whether list L contains only one statement which is a block
10826 function Wrap_Statements_In_Block
10828 Scop
: Entity_Id
:= Current_Scope
) return Node_Id
;
10829 -- Given a list of statements L, wrap it in a block statement and return
10830 -- the generated node. Scop is either the current scope or the scope of
10831 -- the context (if applicable).
10837 function Are_Wrapped
(L
: List_Id
) return Boolean is
10838 Stmt
: constant Node_Id
:= First
(L
);
10842 and then No
(Next
(Stmt
))
10843 and then Nkind
(Stmt
) = N_Block_Statement
;
10846 ------------------------------
10847 -- Wrap_Statements_In_Block --
10848 ------------------------------
10850 function Wrap_Statements_In_Block
10852 Scop
: Entity_Id
:= Current_Scope
) return Node_Id
10854 Block_Id
: Entity_Id
;
10855 Block_Nod
: Node_Id
;
10856 Iter_Loop
: Entity_Id
;
10860 Make_Block_Statement
(Loc
,
10861 Declarations
=> No_List
,
10862 Handled_Statement_Sequence
=>
10863 Make_Handled_Sequence_Of_Statements
(Loc
,
10866 -- Create a label for the block in case the block needs to manage the
10867 -- secondary stack. A label allows for flag Uses_Sec_Stack to be set.
10869 Add_Block_Identifier
(Block_Nod
, Block_Id
);
10871 -- When wrapping the statements of an iterator loop, check whether
10872 -- the loop requires secondary stack management and if so, propagate
10873 -- the appropriate flags to the block. This ensures that the cursor
10874 -- is properly cleaned up at each iteration of the loop.
10876 Iter_Loop
:= Find_Enclosing_Iterator_Loop
(Scop
);
10878 if Present
(Iter_Loop
) then
10879 Set_Uses_Sec_Stack
(Block_Id
, Uses_Sec_Stack
(Iter_Loop
));
10881 -- Secondary stack reclamation is suppressed when the associated
10882 -- iterator loop contains a return statement which uses the stack.
10884 Set_Sec_Stack_Needed_For_Return
10885 (Block_Id
, Sec_Stack_Needed_For_Return
(Iter_Loop
));
10889 end Wrap_Statements_In_Block
;
10895 -- Start of processing for Process_Statements_For_Controlled_Objects
10898 -- Whenever a non-handled statement list is wrapped in a block, the
10899 -- block must be explicitly analyzed to redecorate all entities in the
10900 -- list and ensure that a finalizer is properly built.
10903 when N_Conditional_Entry_Call
10906 | N_Selective_Accept
10908 -- Check the "then statements" for elsif parts and if statements
10910 if Nkind_In
(N
, N_Elsif_Part
, N_If_Statement
)
10911 and then not Is_Empty_List
(Then_Statements
(N
))
10912 and then not Are_Wrapped
(Then_Statements
(N
))
10913 and then Requires_Cleanup_Actions
10914 (L
=> Then_Statements
(N
),
10915 Lib_Level
=> False,
10916 Nested_Constructs
=> False)
10918 Block
:= Wrap_Statements_In_Block
(Then_Statements
(N
));
10919 Set_Then_Statements
(N
, New_List
(Block
));
10924 -- Check the "else statements" for conditional entry calls, if
10925 -- statements and selective accepts.
10927 if Nkind_In
(N
, N_Conditional_Entry_Call
,
10929 N_Selective_Accept
)
10930 and then not Is_Empty_List
(Else_Statements
(N
))
10931 and then not Are_Wrapped
(Else_Statements
(N
))
10932 and then Requires_Cleanup_Actions
10933 (L
=> Else_Statements
(N
),
10934 Lib_Level
=> False,
10935 Nested_Constructs
=> False)
10937 Block
:= Wrap_Statements_In_Block
(Else_Statements
(N
));
10938 Set_Else_Statements
(N
, New_List
(Block
));
10943 when N_Abortable_Part
10944 | N_Accept_Alternative
10945 | N_Case_Statement_Alternative
10946 | N_Delay_Alternative
10947 | N_Entry_Call_Alternative
10948 | N_Exception_Handler
10950 | N_Triggering_Alternative
10952 if not Is_Empty_List
(Statements
(N
))
10953 and then not Are_Wrapped
(Statements
(N
))
10954 and then Requires_Cleanup_Actions
10955 (L
=> Statements
(N
),
10956 Lib_Level
=> False,
10957 Nested_Constructs
=> False)
10959 if Nkind
(N
) = N_Loop_Statement
10960 and then Present
(Identifier
(N
))
10963 Wrap_Statements_In_Block
10964 (L
=> Statements
(N
),
10965 Scop
=> Entity
(Identifier
(N
)));
10967 Block
:= Wrap_Statements_In_Block
(Statements
(N
));
10970 Set_Statements
(N
, New_List
(Block
));
10974 -- Could be e.g. a loop that was transformed into a block or null
10975 -- statement. Do nothing for terminate alternatives.
10977 when N_Block_Statement
10979 | N_Terminate_Alternative
10984 raise Program_Error
;
10986 end Process_Statements_For_Controlled_Objects
;
10992 function Power_Of_Two
(N
: Node_Id
) return Nat
is
10993 Typ
: constant Entity_Id
:= Etype
(N
);
10994 pragma Assert
(Is_Integer_Type
(Typ
));
10996 Siz
: constant Nat
:= UI_To_Int
(Esize
(Typ
));
11000 if not Compile_Time_Known_Value
(N
) then
11004 Val
:= Expr_Value
(N
);
11005 for J
in 1 .. Siz
- 1 loop
11006 if Val
= Uint_2
** J
then
11015 ----------------------
11016 -- Remove_Init_Call --
11017 ----------------------
11019 function Remove_Init_Call
11021 Rep_Clause
: Node_Id
) return Node_Id
11023 Par
: constant Node_Id
:= Parent
(Var
);
11024 Typ
: constant Entity_Id
:= Etype
(Var
);
11026 Init_Proc
: Entity_Id
;
11027 -- Initialization procedure for Typ
11029 function Find_Init_Call_In_List
(From
: Node_Id
) return Node_Id
;
11030 -- Look for init call for Var starting at From and scanning the
11031 -- enclosing list until Rep_Clause or the end of the list is reached.
11033 ----------------------------
11034 -- Find_Init_Call_In_List --
11035 ----------------------------
11037 function Find_Init_Call_In_List
(From
: Node_Id
) return Node_Id
is
11038 Init_Call
: Node_Id
;
11042 while Present
(Init_Call
) and then Init_Call
/= Rep_Clause
loop
11043 if Nkind
(Init_Call
) = N_Procedure_Call_Statement
11044 and then Is_Entity_Name
(Name
(Init_Call
))
11045 and then Entity
(Name
(Init_Call
)) = Init_Proc
11054 end Find_Init_Call_In_List
;
11056 Init_Call
: Node_Id
;
11058 -- Start of processing for Find_Init_Call
11061 if Present
(Initialization_Statements
(Var
)) then
11062 Init_Call
:= Initialization_Statements
(Var
);
11063 Set_Initialization_Statements
(Var
, Empty
);
11065 elsif not Has_Non_Null_Base_Init_Proc
(Typ
) then
11067 -- No init proc for the type, so obviously no call to be found
11072 -- We might be able to handle other cases below by just properly
11073 -- setting Initialization_Statements at the point where the init proc
11074 -- call is generated???
11076 Init_Proc
:= Base_Init_Proc
(Typ
);
11078 -- First scan the list containing the declaration of Var
11080 Init_Call
:= Find_Init_Call_In_List
(From
=> Next
(Par
));
11082 -- If not found, also look on Var's freeze actions list, if any,
11083 -- since the init call may have been moved there (case of an address
11084 -- clause applying to Var).
11086 if No
(Init_Call
) and then Present
(Freeze_Node
(Var
)) then
11088 Find_Init_Call_In_List
(First
(Actions
(Freeze_Node
(Var
))));
11091 -- If the initialization call has actuals that use the secondary
11092 -- stack, the call may have been wrapped into a temporary block, in
11093 -- which case the block itself has to be removed.
11095 if No
(Init_Call
) and then Nkind
(Next
(Par
)) = N_Block_Statement
then
11097 Blk
: constant Node_Id
:= Next
(Par
);
11100 (Find_Init_Call_In_List
11101 (First
(Statements
(Handled_Statement_Sequence
(Blk
)))))
11109 if Present
(Init_Call
) then
11110 Remove
(Init_Call
);
11113 end Remove_Init_Call
;
11115 -------------------------
11116 -- Remove_Side_Effects --
11117 -------------------------
11119 procedure Remove_Side_Effects
11121 Name_Req
: Boolean := False;
11122 Renaming_Req
: Boolean := False;
11123 Variable_Ref
: Boolean := False;
11124 Related_Id
: Entity_Id
:= Empty
;
11125 Is_Low_Bound
: Boolean := False;
11126 Is_High_Bound
: Boolean := False;
11127 Check_Side_Effects
: Boolean := True)
11129 function Build_Temporary
11132 Related_Nod
: Node_Id
:= Empty
) return Entity_Id
;
11133 -- Create an external symbol of the form xxx_FIRST/_LAST if Related_Nod
11134 -- is present (xxx is taken from the Chars field of Related_Nod),
11135 -- otherwise it generates an internal temporary. The created temporary
11136 -- entity is marked as internal.
11138 ---------------------
11139 -- Build_Temporary --
11140 ---------------------
11142 function Build_Temporary
11145 Related_Nod
: Node_Id
:= Empty
) return Entity_Id
11147 Temp_Id
: Entity_Id
;
11148 Temp_Nam
: Name_Id
;
11151 -- The context requires an external symbol
11153 if Present
(Related_Id
) then
11154 if Is_Low_Bound
then
11155 Temp_Nam
:= New_External_Name
(Chars
(Related_Id
), "_FIRST");
11156 else pragma Assert
(Is_High_Bound
);
11157 Temp_Nam
:= New_External_Name
(Chars
(Related_Id
), "_LAST");
11160 Temp_Id
:= Make_Defining_Identifier
(Loc
, Temp_Nam
);
11162 -- Otherwise generate an internal temporary
11165 Temp_Id
:= Make_Temporary
(Loc
, Id
, Related_Nod
);
11168 Set_Is_Internal
(Temp_Id
);
11171 end Build_Temporary
;
11175 Loc
: constant Source_Ptr
:= Sloc
(Exp
);
11176 Exp_Type
: constant Entity_Id
:= Etype
(Exp
);
11177 Svg_Suppress
: constant Suppress_Record
:= Scope_Suppress
;
11178 Def_Id
: Entity_Id
;
11181 Ptr_Typ_Decl
: Node_Id
;
11182 Ref_Type
: Entity_Id
;
11185 -- Start of processing for Remove_Side_Effects
11188 -- Handle cases in which there is nothing to do. In GNATprove mode,
11189 -- removal of side effects is useful for the light expansion of
11190 -- renamings. This removal should only occur when not inside a
11191 -- generic and not doing a preanalysis.
11193 if not Expander_Active
11194 and (Inside_A_Generic
or not Full_Analysis
or not GNATprove_Mode
)
11198 -- Cannot generate temporaries if the invocation to remove side effects
11199 -- was issued too early and the type of the expression is not resolved
11200 -- (this happens because routines Duplicate_Subexpr_XX implicitly invoke
11201 -- Remove_Side_Effects).
11203 elsif No
(Exp_Type
)
11204 or else Ekind
(Exp_Type
) = E_Access_Attribute_Type
11208 -- Nothing to do if prior expansion determined that a function call does
11209 -- not require side effect removal.
11211 elsif Nkind
(Exp
) = N_Function_Call
11212 and then No_Side_Effect_Removal
(Exp
)
11216 -- No action needed for side-effect free expressions
11218 elsif Check_Side_Effects
11219 and then Side_Effect_Free
(Exp
, Name_Req
, Variable_Ref
)
11223 -- Generating C code we cannot remove side effect of function returning
11224 -- class-wide types since there is no secondary stack (required to use
11227 elsif Modify_Tree_For_C
11228 and then Nkind
(Exp
) = N_Function_Call
11229 and then Is_Class_Wide_Type
(Etype
(Exp
))
11234 -- The remaining processing is done with all checks suppressed
11236 -- Note: from now on, don't use return statements, instead do a goto
11237 -- Leave, to ensure that we properly restore Scope_Suppress.Suppress.
11239 Scope_Suppress
.Suppress
:= (others => True);
11241 -- If this is an elementary or a small not-by-reference record type, and
11242 -- we need to capture the value, just make a constant; this is cheap and
11243 -- objects of both kinds of types can be bit aligned, so it might not be
11244 -- possible to generate a reference to them. Likewise if this is not a
11245 -- name reference, except for a type conversion, because we would enter
11246 -- an infinite recursion with Checks.Apply_Predicate_Check if the target
11247 -- type has predicates (and type conversions need a specific treatment
11248 -- anyway, see below). Also do it if we have a volatile reference and
11249 -- Name_Req is not set (see comments for Side_Effect_Free).
11251 if (Is_Elementary_Type
(Exp_Type
)
11252 or else (Is_Record_Type
(Exp_Type
)
11253 and then Known_Static_RM_Size
(Exp_Type
)
11254 and then RM_Size
(Exp_Type
) <= 64
11255 and then not Has_Discriminants
(Exp_Type
)
11256 and then not Is_By_Reference_Type
(Exp_Type
)))
11257 and then (Variable_Ref
11258 or else (not Is_Name_Reference
(Exp
)
11259 and then Nkind
(Exp
) /= N_Type_Conversion
)
11260 or else (not Name_Req
11261 and then Is_Volatile_Reference
(Exp
)))
11263 Def_Id
:= Build_Temporary
(Loc
, 'R', Exp
);
11264 Set_Etype
(Def_Id
, Exp_Type
);
11265 Res
:= New_Occurrence_Of
(Def_Id
, Loc
);
11267 -- If the expression is a packed reference, it must be reanalyzed and
11268 -- expanded, depending on context. This is the case for actuals where
11269 -- a constraint check may capture the actual before expansion of the
11270 -- call is complete.
11272 if Nkind
(Exp
) = N_Indexed_Component
11273 and then Is_Packed
(Etype
(Prefix
(Exp
)))
11275 Set_Analyzed
(Exp
, False);
11276 Set_Analyzed
(Prefix
(Exp
), False);
11280 -- Rnn : Exp_Type renames Expr;
11282 if Renaming_Req
then
11284 Make_Object_Renaming_Declaration
(Loc
,
11285 Defining_Identifier
=> Def_Id
,
11286 Subtype_Mark
=> New_Occurrence_Of
(Exp_Type
, Loc
),
11287 Name
=> Relocate_Node
(Exp
));
11290 -- Rnn : constant Exp_Type := Expr;
11294 Make_Object_Declaration
(Loc
,
11295 Defining_Identifier
=> Def_Id
,
11296 Object_Definition
=> New_Occurrence_Of
(Exp_Type
, Loc
),
11297 Constant_Present
=> True,
11298 Expression
=> Relocate_Node
(Exp
));
11300 Set_Assignment_OK
(E
);
11303 Insert_Action
(Exp
, E
);
11305 -- If the expression has the form v.all then we can just capture the
11306 -- pointer, and then do an explicit dereference on the result, but
11307 -- this is not right if this is a volatile reference.
11309 elsif Nkind
(Exp
) = N_Explicit_Dereference
11310 and then not Is_Volatile_Reference
(Exp
)
11312 Def_Id
:= Build_Temporary
(Loc
, 'R', Exp
);
11314 Make_Explicit_Dereference
(Loc
, New_Occurrence_Of
(Def_Id
, Loc
));
11316 Insert_Action
(Exp
,
11317 Make_Object_Declaration
(Loc
,
11318 Defining_Identifier
=> Def_Id
,
11319 Object_Definition
=>
11320 New_Occurrence_Of
(Etype
(Prefix
(Exp
)), Loc
),
11321 Constant_Present
=> True,
11322 Expression
=> Relocate_Node
(Prefix
(Exp
))));
11324 -- Similar processing for an unchecked conversion of an expression of
11325 -- the form v.all, where we want the same kind of treatment.
11327 elsif Nkind
(Exp
) = N_Unchecked_Type_Conversion
11328 and then Nkind
(Expression
(Exp
)) = N_Explicit_Dereference
11330 Remove_Side_Effects
(Expression
(Exp
), Name_Req
, Variable_Ref
);
11333 -- If this is a type conversion, leave the type conversion and remove
11334 -- the side effects in the expression. This is important in several
11335 -- circumstances: for change of representations, and also when this is a
11336 -- view conversion to a smaller object, where gigi can end up creating
11337 -- its own temporary of the wrong size.
11339 elsif Nkind
(Exp
) = N_Type_Conversion
then
11340 Remove_Side_Effects
(Expression
(Exp
), Name_Req
, Variable_Ref
);
11342 -- Generating C code the type conversion of an access to constrained
11343 -- array type into an access to unconstrained array type involves
11344 -- initializing a fat pointer and the expression must be free of
11345 -- side effects to safely compute its bounds.
11347 if Modify_Tree_For_C
11348 and then Is_Access_Type
(Etype
(Exp
))
11349 and then Is_Array_Type
(Designated_Type
(Etype
(Exp
)))
11350 and then not Is_Constrained
(Designated_Type
(Etype
(Exp
)))
11352 Def_Id
:= Build_Temporary
(Loc
, 'R', Exp
);
11353 Set_Etype
(Def_Id
, Exp_Type
);
11354 Res
:= New_Occurrence_Of
(Def_Id
, Loc
);
11356 Insert_Action
(Exp
,
11357 Make_Object_Declaration
(Loc
,
11358 Defining_Identifier
=> Def_Id
,
11359 Object_Definition
=> New_Occurrence_Of
(Exp_Type
, Loc
),
11360 Constant_Present
=> True,
11361 Expression
=> Relocate_Node
(Exp
)));
11366 -- If this is an unchecked conversion that Gigi can't handle, make
11367 -- a copy or a use a renaming to capture the value.
11369 elsif Nkind
(Exp
) = N_Unchecked_Type_Conversion
11370 and then not Safe_Unchecked_Type_Conversion
(Exp
)
11372 if CW_Or_Has_Controlled_Part
(Exp_Type
) then
11374 -- Use a renaming to capture the expression, rather than create
11375 -- a controlled temporary.
11377 Def_Id
:= Build_Temporary
(Loc
, 'R', Exp
);
11378 Res
:= New_Occurrence_Of
(Def_Id
, Loc
);
11380 Insert_Action
(Exp
,
11381 Make_Object_Renaming_Declaration
(Loc
,
11382 Defining_Identifier
=> Def_Id
,
11383 Subtype_Mark
=> New_Occurrence_Of
(Exp_Type
, Loc
),
11384 Name
=> Relocate_Node
(Exp
)));
11387 Def_Id
:= Build_Temporary
(Loc
, 'R', Exp
);
11388 Set_Etype
(Def_Id
, Exp_Type
);
11389 Res
:= New_Occurrence_Of
(Def_Id
, Loc
);
11392 Make_Object_Declaration
(Loc
,
11393 Defining_Identifier
=> Def_Id
,
11394 Object_Definition
=> New_Occurrence_Of
(Exp_Type
, Loc
),
11395 Constant_Present
=> not Is_Variable
(Exp
),
11396 Expression
=> Relocate_Node
(Exp
));
11398 Set_Assignment_OK
(E
);
11399 Insert_Action
(Exp
, E
);
11402 -- For expressions that denote names, we can use a renaming scheme.
11403 -- This is needed for correctness in the case of a volatile object of
11404 -- a non-volatile type because the Make_Reference call of the "default"
11405 -- approach would generate an illegal access value (an access value
11406 -- cannot designate such an object - see Analyze_Reference).
11408 elsif Is_Name_Reference
(Exp
)
11410 -- We skip using this scheme if we have an object of a volatile
11411 -- type and we do not have Name_Req set true (see comments for
11412 -- Side_Effect_Free).
11414 and then (Name_Req
or else not Treat_As_Volatile
(Exp_Type
))
11416 Def_Id
:= Build_Temporary
(Loc
, 'R', Exp
);
11417 Res
:= New_Occurrence_Of
(Def_Id
, Loc
);
11419 Insert_Action
(Exp
,
11420 Make_Object_Renaming_Declaration
(Loc
,
11421 Defining_Identifier
=> Def_Id
,
11422 Subtype_Mark
=> New_Occurrence_Of
(Exp_Type
, Loc
),
11423 Name
=> Relocate_Node
(Exp
)));
11425 -- If this is a packed reference, or a selected component with
11426 -- a non-standard representation, a reference to the temporary
11427 -- will be replaced by a copy of the original expression (see
11428 -- Exp_Ch2.Expand_Renaming). Otherwise the temporary must be
11429 -- elaborated by gigi, and is of course not to be replaced in-line
11430 -- by the expression it renames, which would defeat the purpose of
11431 -- removing the side effect.
11433 if Nkind_In
(Exp
, N_Selected_Component
, N_Indexed_Component
)
11434 and then Has_Non_Standard_Rep
(Etype
(Prefix
(Exp
)))
11438 Set_Is_Renaming_Of_Object
(Def_Id
, False);
11441 -- Avoid generating a variable-sized temporary, by generating the
11442 -- reference just for the function call. The transformation could be
11443 -- refined to apply only when the array component is constrained by a
11446 elsif Nkind
(Exp
) = N_Selected_Component
11447 and then Nkind
(Prefix
(Exp
)) = N_Function_Call
11448 and then Is_Array_Type
(Exp_Type
)
11450 Remove_Side_Effects
(Prefix
(Exp
), Name_Req
, Variable_Ref
);
11453 -- Otherwise we generate a reference to the expression
11456 -- An expression which is in SPARK mode is considered side effect
11457 -- free if the resulting value is captured by a variable or a
11461 and then Nkind
(Parent
(Exp
)) = N_Object_Declaration
11465 -- When generating C code we cannot consider side effect free object
11466 -- declarations that have discriminants and are initialized by means
11467 -- of a function call since on this target there is no secondary
11468 -- stack to store the return value and the expander may generate an
11469 -- extra call to the function to compute the discriminant value. In
11470 -- addition, for targets that have secondary stack, the expansion of
11471 -- functions with side effects involves the generation of an access
11472 -- type to capture the return value stored in the secondary stack;
11473 -- by contrast when generating C code such expansion generates an
11474 -- internal object declaration (no access type involved) which must
11475 -- be identified here to avoid entering into a never-ending loop
11476 -- generating internal object declarations.
11478 elsif Modify_Tree_For_C
11479 and then Nkind
(Parent
(Exp
)) = N_Object_Declaration
11481 (Nkind
(Exp
) /= N_Function_Call
11482 or else not Has_Discriminants
(Exp_Type
)
11483 or else Is_Internal_Name
11484 (Chars
(Defining_Identifier
(Parent
(Exp
)))))
11489 -- Special processing for function calls that return a limited type.
11490 -- We need to build a declaration that will enable build-in-place
11491 -- expansion of the call. This is not done if the context is already
11492 -- an object declaration, to prevent infinite recursion.
11494 -- This is relevant only in Ada 2005 mode. In Ada 95 programs we have
11495 -- to accommodate functions returning limited objects by reference.
11497 if Ada_Version
>= Ada_2005
11498 and then Nkind
(Exp
) = N_Function_Call
11499 and then Is_Limited_View
(Etype
(Exp
))
11500 and then Nkind
(Parent
(Exp
)) /= N_Object_Declaration
11503 Obj
: constant Entity_Id
:= Make_Temporary
(Loc
, 'F', Exp
);
11508 Make_Object_Declaration
(Loc
,
11509 Defining_Identifier
=> Obj
,
11510 Object_Definition
=> New_Occurrence_Of
(Exp_Type
, Loc
),
11511 Expression
=> Relocate_Node
(Exp
));
11513 Insert_Action
(Exp
, Decl
);
11514 Set_Etype
(Obj
, Exp_Type
);
11515 Rewrite
(Exp
, New_Occurrence_Of
(Obj
, Loc
));
11520 Def_Id
:= Build_Temporary
(Loc
, 'R', Exp
);
11522 -- The regular expansion of functions with side effects involves the
11523 -- generation of an access type to capture the return value found on
11524 -- the secondary stack. Since SPARK (and why) cannot process access
11525 -- types, use a different approach which ignores the secondary stack
11526 -- and "copies" the returned object.
11527 -- When generating C code, no need for a 'reference since the
11528 -- secondary stack is not supported.
11530 if GNATprove_Mode
or Modify_Tree_For_C
then
11531 Res
:= New_Occurrence_Of
(Def_Id
, Loc
);
11532 Ref_Type
:= Exp_Type
;
11534 -- Regular expansion utilizing an access type and 'reference
11538 Make_Explicit_Dereference
(Loc
,
11539 Prefix
=> New_Occurrence_Of
(Def_Id
, Loc
));
11542 -- type Ann is access all <Exp_Type>;
11544 Ref_Type
:= Make_Temporary
(Loc
, 'A');
11547 Make_Full_Type_Declaration
(Loc
,
11548 Defining_Identifier
=> Ref_Type
,
11550 Make_Access_To_Object_Definition
(Loc
,
11551 All_Present
=> True,
11552 Subtype_Indication
=>
11553 New_Occurrence_Of
(Exp_Type
, Loc
)));
11555 Insert_Action
(Exp
, Ptr_Typ_Decl
);
11559 if Nkind
(E
) = N_Explicit_Dereference
then
11560 New_Exp
:= Relocate_Node
(Prefix
(E
));
11563 E
:= Relocate_Node
(E
);
11565 -- Do not generate a 'reference in SPARK mode or C generation
11566 -- since the access type is not created in the first place.
11568 if GNATprove_Mode
or Modify_Tree_For_C
then
11571 -- Otherwise generate reference, marking the value as non-null
11572 -- since we know it cannot be null and we don't want a check.
11575 New_Exp
:= Make_Reference
(Loc
, E
);
11576 Set_Is_Known_Non_Null
(Def_Id
);
11580 if Is_Delayed_Aggregate
(E
) then
11582 -- The expansion of nested aggregates is delayed until the
11583 -- enclosing aggregate is expanded. As aggregates are often
11584 -- qualified, the predicate applies to qualified expressions as
11585 -- well, indicating that the enclosing aggregate has not been
11586 -- expanded yet. At this point the aggregate is part of a
11587 -- stand-alone declaration, and must be fully expanded.
11589 if Nkind
(E
) = N_Qualified_Expression
then
11590 Set_Expansion_Delayed
(Expression
(E
), False);
11591 Set_Analyzed
(Expression
(E
), False);
11593 Set_Expansion_Delayed
(E
, False);
11596 Set_Analyzed
(E
, False);
11599 -- Generating C code of object declarations that have discriminants
11600 -- and are initialized by means of a function call we propagate the
11601 -- discriminants of the parent type to the internally built object.
11602 -- This is needed to avoid generating an extra call to the called
11605 -- For example, if we generate here the following declaration, it
11606 -- will be expanded later adding an extra call to evaluate the value
11607 -- of the discriminant (needed to compute the size of the object).
11609 -- type Rec (D : Integer) is ...
11610 -- Obj : constant Rec := SomeFunc;
11612 if Modify_Tree_For_C
11613 and then Nkind
(Parent
(Exp
)) = N_Object_Declaration
11614 and then Has_Discriminants
(Exp_Type
)
11615 and then Nkind
(Exp
) = N_Function_Call
11617 Insert_Action
(Exp
,
11618 Make_Object_Declaration
(Loc
,
11619 Defining_Identifier
=> Def_Id
,
11620 Object_Definition
=> New_Copy_Tree
11621 (Object_Definition
(Parent
(Exp
))),
11622 Constant_Present
=> True,
11623 Expression
=> New_Exp
));
11625 Insert_Action
(Exp
,
11626 Make_Object_Declaration
(Loc
,
11627 Defining_Identifier
=> Def_Id
,
11628 Object_Definition
=> New_Occurrence_Of
(Ref_Type
, Loc
),
11629 Constant_Present
=> True,
11630 Expression
=> New_Exp
));
11634 -- Preserve the Assignment_OK flag in all copies, since at least one
11635 -- copy may be used in a context where this flag must be set (otherwise
11636 -- why would the flag be set in the first place).
11638 Set_Assignment_OK
(Res
, Assignment_OK
(Exp
));
11640 -- Finally rewrite the original expression and we are done
11642 Rewrite
(Exp
, Res
);
11643 Analyze_And_Resolve
(Exp
, Exp_Type
);
11646 Scope_Suppress
:= Svg_Suppress
;
11647 end Remove_Side_Effects
;
11649 ------------------------
11650 -- Replace_References --
11651 ------------------------
11653 procedure Replace_References
11655 Par_Typ
: Entity_Id
;
11656 Deriv_Typ
: Entity_Id
;
11657 Par_Obj
: Entity_Id
:= Empty
;
11658 Deriv_Obj
: Entity_Id
:= Empty
)
11660 function Is_Deriv_Obj_Ref
(Ref
: Node_Id
) return Boolean;
11661 -- Determine whether node Ref denotes some component of Deriv_Obj
11663 function Replace_Ref
(Ref
: Node_Id
) return Traverse_Result
;
11664 -- Substitute a reference to an entity with the corresponding value
11665 -- stored in table Type_Map.
11667 function Type_Of_Formal
11669 Actual
: Node_Id
) return Entity_Id
;
11670 -- Find the type of the formal parameter which corresponds to actual
11671 -- parameter Actual in subprogram call Call.
11673 ----------------------
11674 -- Is_Deriv_Obj_Ref --
11675 ----------------------
11677 function Is_Deriv_Obj_Ref
(Ref
: Node_Id
) return Boolean is
11678 Par
: constant Node_Id
:= Parent
(Ref
);
11681 -- Detect the folowing selected component form:
11683 -- Deriv_Obj.(something)
11686 Nkind
(Par
) = N_Selected_Component
11687 and then Is_Entity_Name
(Prefix
(Par
))
11688 and then Entity
(Prefix
(Par
)) = Deriv_Obj
;
11689 end Is_Deriv_Obj_Ref
;
11695 function Replace_Ref
(Ref
: Node_Id
) return Traverse_Result
is
11696 procedure Remove_Controlling_Arguments
(From_Arg
: Node_Id
);
11697 -- Reset the Controlling_Argument of all function calls that
11698 -- encapsulate node From_Arg.
11700 ----------------------------------
11701 -- Remove_Controlling_Arguments --
11702 ----------------------------------
11704 procedure Remove_Controlling_Arguments
(From_Arg
: Node_Id
) is
11709 while Present
(Par
) loop
11710 if Nkind
(Par
) = N_Function_Call
11711 and then Present
(Controlling_Argument
(Par
))
11713 Set_Controlling_Argument
(Par
, Empty
);
11715 -- Prevent the search from going too far
11717 elsif Is_Body_Or_Package_Declaration
(Par
) then
11721 Par
:= Parent
(Par
);
11723 end Remove_Controlling_Arguments
;
11727 Context
: constant Node_Id
:= Parent
(Ref
);
11728 Loc
: constant Source_Ptr
:= Sloc
(Ref
);
11729 Ref_Id
: Entity_Id
;
11730 Result
: Traverse_Result
;
11733 -- The new reference which is intended to substitute the old one
11736 -- The reference designated for replacement. In certain cases this
11737 -- may be a node other than Ref.
11739 Val
: Node_Or_Entity_Id
;
11740 -- The corresponding value of Ref from the type map
11742 -- Start of processing for Replace_Ref
11745 -- Assume that the input reference is to be replaced and that the
11746 -- traversal should examine the children of the reference.
11751 -- The input denotes a meaningful reference
11753 if Nkind
(Ref
) in N_Has_Entity
and then Present
(Entity
(Ref
)) then
11754 Ref_Id
:= Entity
(Ref
);
11755 Val
:= Type_Map
.Get
(Ref_Id
);
11757 -- The reference has a corresponding value in the type map, a
11758 -- substitution is possible.
11760 if Present
(Val
) then
11762 -- The reference denotes a discriminant
11764 if Ekind
(Ref_Id
) = E_Discriminant
then
11765 if Nkind
(Val
) in N_Entity
then
11767 -- The value denotes another discriminant. Replace as
11770 -- _object.Discr -> _object.Val
11772 if Ekind
(Val
) = E_Discriminant
then
11773 New_Ref
:= New_Occurrence_Of
(Val
, Loc
);
11775 -- Otherwise the value denotes the entity of a name which
11776 -- constraints the discriminant. Replace as follows:
11778 -- _object.Discr -> Val
11781 pragma Assert
(Is_Deriv_Obj_Ref
(Old_Ref
));
11783 New_Ref
:= New_Occurrence_Of
(Val
, Loc
);
11784 Old_Ref
:= Parent
(Old_Ref
);
11787 -- Otherwise the value denotes an arbitrary expression which
11788 -- constraints the discriminant. Replace as follows:
11790 -- _object.Discr -> Val
11793 pragma Assert
(Is_Deriv_Obj_Ref
(Old_Ref
));
11795 New_Ref
:= New_Copy_Tree
(Val
);
11796 Old_Ref
:= Parent
(Old_Ref
);
11799 -- Otherwise the reference denotes a primitive. Replace as
11802 -- Primitive -> Val
11805 pragma Assert
(Nkind
(Val
) in N_Entity
);
11806 New_Ref
:= New_Occurrence_Of
(Val
, Loc
);
11809 -- The reference mentions the _object parameter of the parent
11810 -- type's DIC or type invariant procedure. Replace as follows:
11812 -- _object -> _object
11814 elsif Present
(Par_Obj
)
11815 and then Present
(Deriv_Obj
)
11816 and then Ref_Id
= Par_Obj
11818 New_Ref
:= New_Occurrence_Of
(Deriv_Obj
, Loc
);
11820 -- The type of the _object parameter is class-wide when the
11821 -- expression comes from an assertion pragma that applies to
11822 -- an abstract parent type or an interface. The class-wide type
11823 -- facilitates the preanalysis of the expression by treating
11824 -- calls to abstract primitives that mention the current
11825 -- instance of the type as dispatching. Once the calls are
11826 -- remapped to invoke overriding or inherited primitives, the
11827 -- calls no longer need to be dispatching. Examine all function
11828 -- calls that encapsulate the _object parameter and reset their
11829 -- Controlling_Argument attribute.
11831 if Is_Class_Wide_Type
(Etype
(Par_Obj
))
11832 and then Is_Abstract_Type
(Root_Type
(Etype
(Par_Obj
)))
11834 Remove_Controlling_Arguments
(Old_Ref
);
11837 -- The reference to _object acts as an actual parameter in a
11838 -- subprogram call which may be invoking a primitive of the
11841 -- Primitive (... _object ...);
11843 -- The parent type primitive may not be overridden nor
11844 -- inherited when it is declared after the derived type
11847 -- type Parent is tagged private;
11848 -- type Child is new Parent with private;
11849 -- procedure Primitive (Obj : Parent);
11851 -- In this scenario the _object parameter is converted to the
11852 -- parent type. Due to complications with partial/full views
11853 -- and view swaps, the parent type is taken from the formal
11854 -- parameter of the subprogram being called.
11856 if Nkind_In
(Context
, N_Function_Call
,
11857 N_Procedure_Call_Statement
)
11858 and then No
(Type_Map
.Get
(Entity
(Name
(Context
))))
11861 Convert_To
(Type_Of_Formal
(Context
, Old_Ref
), New_Ref
);
11863 -- Do not process the generated type conversion because
11864 -- both the parent type and the derived type are in the
11865 -- Type_Map table. This will clobber the type conversion
11866 -- by resetting its subtype mark.
11871 -- Otherwise there is nothing to replace
11877 if Present
(New_Ref
) then
11878 Rewrite
(Old_Ref
, New_Ref
);
11880 -- Update the return type when the context of the reference
11881 -- acts as the name of a function call. Note that the update
11882 -- should not be performed when the reference appears as an
11883 -- actual in the call.
11885 if Nkind
(Context
) = N_Function_Call
11886 and then Name
(Context
) = Old_Ref
11888 Set_Etype
(Context
, Etype
(Val
));
11893 -- Reanalyze the reference due to potential replacements
11895 if Nkind
(Old_Ref
) in N_Has_Etype
then
11896 Set_Analyzed
(Old_Ref
, False);
11902 procedure Replace_Refs
is new Traverse_Proc
(Replace_Ref
);
11904 --------------------
11905 -- Type_Of_Formal --
11906 --------------------
11908 function Type_Of_Formal
11910 Actual
: Node_Id
) return Entity_Id
11916 -- Examine the list of actual and formal parameters in parallel
11918 A
:= First
(Parameter_Associations
(Call
));
11919 F
:= First_Formal
(Entity
(Name
(Call
)));
11920 while Present
(A
) and then Present
(F
) loop
11929 -- The actual parameter must always have a corresponding formal
11931 pragma Assert
(False);
11934 end Type_Of_Formal
;
11936 -- Start of processing for Replace_References
11939 -- Map the attributes of the parent type to the proper corresponding
11940 -- attributes of the derived type.
11943 (Parent_Type
=> Par_Typ
,
11944 Derived_Type
=> Deriv_Typ
);
11946 -- Inspect the input expression and perform substitutions where
11949 Replace_Refs
(Expr
);
11950 end Replace_References
;
11952 -----------------------------
11953 -- Replace_Type_References --
11954 -----------------------------
11956 procedure Replace_Type_References
11959 Obj_Id
: Entity_Id
)
11961 procedure Replace_Type_Ref
(N
: Node_Id
);
11962 -- Substitute a single reference of the current instance of type Typ
11963 -- with a reference to Obj_Id.
11965 ----------------------
11966 -- Replace_Type_Ref --
11967 ----------------------
11969 procedure Replace_Type_Ref
(N
: Node_Id
) is
11971 -- Decorate the reference to Typ even though it may be rewritten
11972 -- further down. This is done for two reasons:
11974 -- * ASIS has all necessary semantic information in the original
11977 -- * Routines which examine properties of the Original_Node have
11978 -- some semantic information.
11980 if Nkind
(N
) = N_Identifier
then
11981 Set_Entity
(N
, Typ
);
11982 Set_Etype
(N
, Typ
);
11984 elsif Nkind
(N
) = N_Selected_Component
then
11985 Analyze
(Prefix
(N
));
11986 Set_Entity
(Selector_Name
(N
), Typ
);
11987 Set_Etype
(Selector_Name
(N
), Typ
);
11990 -- Perform the following substitution:
11994 Rewrite
(N
, New_Occurrence_Of
(Obj_Id
, Sloc
(N
)));
11995 Set_Comes_From_Source
(N
, True);
11996 end Replace_Type_Ref
;
11998 procedure Replace_Type_Refs
is
11999 new Replace_Type_References_Generic
(Replace_Type_Ref
);
12001 -- Start of processing for Replace_Type_References
12004 Replace_Type_Refs
(Expr
, Typ
);
12005 end Replace_Type_References
;
12007 ---------------------------
12008 -- Represented_As_Scalar --
12009 ---------------------------
12011 function Represented_As_Scalar
(T
: Entity_Id
) return Boolean is
12012 UT
: constant Entity_Id
:= Underlying_Type
(T
);
12014 return Is_Scalar_Type
(UT
)
12015 or else (Is_Bit_Packed_Array
(UT
)
12016 and then Is_Scalar_Type
(Packed_Array_Impl_Type
(UT
)));
12017 end Represented_As_Scalar
;
12019 ------------------------------
12020 -- Requires_Cleanup_Actions --
12021 ------------------------------
12023 function Requires_Cleanup_Actions
12025 Lib_Level
: Boolean) return Boolean
12027 At_Lib_Level
: constant Boolean :=
12029 and then Nkind_In
(N
, N_Package_Body
,
12030 N_Package_Specification
);
12031 -- N is at the library level if the top-most context is a package and
12032 -- the path taken to reach N does not inlcude non-package constructs.
12036 when N_Accept_Statement
12037 | N_Block_Statement
12041 | N_Subprogram_Body
12045 Requires_Cleanup_Actions
12046 (L
=> Declarations
(N
),
12047 Lib_Level
=> At_Lib_Level
,
12048 Nested_Constructs
=> True)
12050 (Present
(Handled_Statement_Sequence
(N
))
12052 Requires_Cleanup_Actions
12054 Statements
(Handled_Statement_Sequence
(N
)),
12055 Lib_Level
=> At_Lib_Level
,
12056 Nested_Constructs
=> True));
12058 -- Extended return statements are the same as the above, except that
12059 -- there is no Declarations field. We do not want to clean up the
12060 -- Return_Object_Declarations.
12062 when N_Extended_Return_Statement
=>
12064 Present
(Handled_Statement_Sequence
(N
))
12065 and then Requires_Cleanup_Actions
12067 Statements
(Handled_Statement_Sequence
(N
)),
12068 Lib_Level
=> At_Lib_Level
,
12069 Nested_Constructs
=> True);
12071 when N_Package_Specification
=>
12073 Requires_Cleanup_Actions
12074 (L
=> Visible_Declarations
(N
),
12075 Lib_Level
=> At_Lib_Level
,
12076 Nested_Constructs
=> True)
12078 Requires_Cleanup_Actions
12079 (L
=> Private_Declarations
(N
),
12080 Lib_Level
=> At_Lib_Level
,
12081 Nested_Constructs
=> True);
12084 raise Program_Error
;
12086 end Requires_Cleanup_Actions
;
12088 ------------------------------
12089 -- Requires_Cleanup_Actions --
12090 ------------------------------
12092 function Requires_Cleanup_Actions
12094 Lib_Level
: Boolean;
12095 Nested_Constructs
: Boolean) return Boolean
12099 Obj_Id
: Entity_Id
;
12100 Obj_Typ
: Entity_Id
;
12101 Pack_Id
: Entity_Id
;
12106 or else Is_Empty_List
(L
)
12112 while Present
(Decl
) loop
12114 -- Library-level tagged types
12116 if Nkind
(Decl
) = N_Full_Type_Declaration
then
12117 Typ
:= Defining_Identifier
(Decl
);
12119 -- Ignored Ghost types do not need any cleanup actions because
12120 -- they will not appear in the final tree.
12122 if Is_Ignored_Ghost_Entity
(Typ
) then
12125 elsif Is_Tagged_Type
(Typ
)
12126 and then Is_Library_Level_Entity
(Typ
)
12127 and then Convention
(Typ
) = Convention_Ada
12128 and then Present
(Access_Disp_Table
(Typ
))
12129 and then RTE_Available
(RE_Unregister_Tag
)
12130 and then not Is_Abstract_Type
(Typ
)
12131 and then not No_Run_Time_Mode
12136 -- Regular object declarations
12138 elsif Nkind
(Decl
) = N_Object_Declaration
then
12139 Obj_Id
:= Defining_Identifier
(Decl
);
12140 Obj_Typ
:= Base_Type
(Etype
(Obj_Id
));
12141 Expr
:= Expression
(Decl
);
12143 -- Bypass any form of processing for objects which have their
12144 -- finalization disabled. This applies only to objects at the
12147 if Lib_Level
and then Finalize_Storage_Only
(Obj_Typ
) then
12150 -- Finalization of transient objects are treated separately in
12151 -- order to handle sensitive cases. These include:
12153 -- * Aggregate expansion
12154 -- * If, case, and expression with actions expansion
12155 -- * Transient scopes
12157 -- If one of those contexts has marked the transient object as
12158 -- ignored, do not generate finalization actions for it.
12160 elsif Is_Finalized_Transient
(Obj_Id
)
12161 or else Is_Ignored_Transient
(Obj_Id
)
12165 -- Ignored Ghost objects do not need any cleanup actions because
12166 -- they will not appear in the final tree.
12168 elsif Is_Ignored_Ghost_Entity
(Obj_Id
) then
12171 -- The object is of the form:
12172 -- Obj : [constant] Typ [:= Expr];
12174 -- Do not process tag-to-class-wide conversions because they do
12175 -- not yield an object. Do not process the incomplete view of a
12176 -- deferred constant. Note that an object initialized by means
12177 -- of a build-in-place function call may appear as a deferred
12178 -- constant after expansion activities. These kinds of objects
12179 -- must be finalized.
12181 elsif not Is_Imported
(Obj_Id
)
12182 and then Needs_Finalization
(Obj_Typ
)
12183 and then not Is_Tag_To_Class_Wide_Conversion
(Obj_Id
)
12184 and then not (Ekind
(Obj_Id
) = E_Constant
12185 and then not Has_Completion
(Obj_Id
)
12186 and then No
(BIP_Initialization_Call
(Obj_Id
)))
12190 -- The object is of the form:
12191 -- Obj : Access_Typ := Non_BIP_Function_Call'reference;
12193 -- Obj : Access_Typ :=
12194 -- BIP_Function_Call (BIPalloc => 2, ...)'reference;
12196 elsif Is_Access_Type
(Obj_Typ
)
12197 and then Needs_Finalization
12198 (Available_View
(Designated_Type
(Obj_Typ
)))
12199 and then Present
(Expr
)
12201 (Is_Secondary_Stack_BIP_Func_Call
(Expr
)
12203 (Is_Non_BIP_Func_Call
(Expr
)
12204 and then not Is_Related_To_Func_Return
(Obj_Id
)))
12208 -- Processing for "hook" objects generated for transient objects
12209 -- declared inside an Expression_With_Actions.
12211 elsif Is_Access_Type
(Obj_Typ
)
12212 and then Present
(Status_Flag_Or_Transient_Decl
(Obj_Id
))
12213 and then Nkind
(Status_Flag_Or_Transient_Decl
(Obj_Id
)) =
12214 N_Object_Declaration
12218 -- Processing for intermediate results of if expressions where
12219 -- one of the alternatives uses a controlled function call.
12221 elsif Is_Access_Type
(Obj_Typ
)
12222 and then Present
(Status_Flag_Or_Transient_Decl
(Obj_Id
))
12223 and then Nkind
(Status_Flag_Or_Transient_Decl
(Obj_Id
)) =
12224 N_Defining_Identifier
12225 and then Present
(Expr
)
12226 and then Nkind
(Expr
) = N_Null
12230 -- Simple protected objects which use type System.Tasking.
12231 -- Protected_Objects.Protection to manage their locks should be
12232 -- treated as controlled since they require manual cleanup.
12234 elsif Ekind
(Obj_Id
) = E_Variable
12235 and then (Is_Simple_Protected_Type
(Obj_Typ
)
12236 or else Has_Simple_Protected_Object
(Obj_Typ
))
12241 -- Specific cases of object renamings
12243 elsif Nkind
(Decl
) = N_Object_Renaming_Declaration
then
12244 Obj_Id
:= Defining_Identifier
(Decl
);
12245 Obj_Typ
:= Base_Type
(Etype
(Obj_Id
));
12247 -- Bypass any form of processing for objects which have their
12248 -- finalization disabled. This applies only to objects at the
12251 if Lib_Level
and then Finalize_Storage_Only
(Obj_Typ
) then
12254 -- Ignored Ghost object renamings do not need any cleanup actions
12255 -- because they will not appear in the final tree.
12257 elsif Is_Ignored_Ghost_Entity
(Obj_Id
) then
12260 -- Return object of a build-in-place function. This case is
12261 -- recognized and marked by the expansion of an extended return
12262 -- statement (see Expand_N_Extended_Return_Statement).
12264 elsif Needs_Finalization
(Obj_Typ
)
12265 and then Is_Return_Object
(Obj_Id
)
12266 and then Present
(Status_Flag_Or_Transient_Decl
(Obj_Id
))
12270 -- Detect a case where a source object has been initialized by
12271 -- a controlled function call or another object which was later
12272 -- rewritten as a class-wide conversion of Ada.Tags.Displace.
12274 -- Obj1 : CW_Type := Src_Obj;
12275 -- Obj2 : CW_Type := Function_Call (...);
12277 -- Obj1 : CW_Type renames (... Ada.Tags.Displace (Src_Obj));
12278 -- Tmp : ... := Function_Call (...)'reference;
12279 -- Obj2 : CW_Type renames (... Ada.Tags.Displace (Tmp));
12281 elsif Is_Displacement_Of_Object_Or_Function_Result
(Obj_Id
) then
12285 -- Inspect the freeze node of an access-to-controlled type and look
12286 -- for a delayed finalization master. This case arises when the
12287 -- freeze actions are inserted at a later time than the expansion of
12288 -- the context. Since Build_Finalizer is never called on a single
12289 -- construct twice, the master will be ultimately left out and never
12290 -- finalized. This is also needed for freeze actions of designated
12291 -- types themselves, since in some cases the finalization master is
12292 -- associated with a designated type's freeze node rather than that
12293 -- of the access type (see handling for freeze actions in
12294 -- Build_Finalization_Master).
12296 elsif Nkind
(Decl
) = N_Freeze_Entity
12297 and then Present
(Actions
(Decl
))
12299 Typ
:= Entity
(Decl
);
12301 -- Freeze nodes for ignored Ghost types do not need cleanup
12302 -- actions because they will never appear in the final tree.
12304 if Is_Ignored_Ghost_Entity
(Typ
) then
12307 elsif ((Is_Access_Type
(Typ
)
12308 and then not Is_Access_Subprogram_Type
(Typ
)
12309 and then Needs_Finalization
12310 (Available_View
(Designated_Type
(Typ
))))
12311 or else (Is_Type
(Typ
) and then Needs_Finalization
(Typ
)))
12312 and then Requires_Cleanup_Actions
12313 (Actions
(Decl
), Lib_Level
, Nested_Constructs
)
12318 -- Nested package declarations
12320 elsif Nested_Constructs
12321 and then Nkind
(Decl
) = N_Package_Declaration
12323 Pack_Id
:= Defining_Entity
(Decl
);
12325 -- Do not inspect an ignored Ghost package because all code found
12326 -- within will not appear in the final tree.
12328 if Is_Ignored_Ghost_Entity
(Pack_Id
) then
12331 elsif Ekind
(Pack_Id
) /= E_Generic_Package
12332 and then Requires_Cleanup_Actions
12333 (Specification
(Decl
), Lib_Level
)
12338 -- Nested package bodies
12340 elsif Nested_Constructs
and then Nkind
(Decl
) = N_Package_Body
then
12342 -- Do not inspect an ignored Ghost package body because all code
12343 -- found within will not appear in the final tree.
12345 if Is_Ignored_Ghost_Entity
(Defining_Entity
(Decl
)) then
12348 elsif Ekind
(Corresponding_Spec
(Decl
)) /= E_Generic_Package
12349 and then Requires_Cleanup_Actions
(Decl
, Lib_Level
)
12354 elsif Nkind
(Decl
) = N_Block_Statement
12357 -- Handle a rare case caused by a controlled transient object
12358 -- created as part of a record init proc. The variable is wrapped
12359 -- in a block, but the block is not associated with a transient
12364 -- Handle the case where the original context has been wrapped in
12365 -- a block to avoid interference between exception handlers and
12366 -- At_End handlers. Treat the block as transparent and process its
12369 or else Is_Finalization_Wrapper
(Decl
))
12371 if Requires_Cleanup_Actions
(Decl
, Lib_Level
) then
12380 end Requires_Cleanup_Actions
;
12382 ------------------------------------
12383 -- Safe_Unchecked_Type_Conversion --
12384 ------------------------------------
12386 -- Note: this function knows quite a bit about the exact requirements of
12387 -- Gigi with respect to unchecked type conversions, and its code must be
12388 -- coordinated with any changes in Gigi in this area.
12390 -- The above requirements should be documented in Sinfo ???
12392 function Safe_Unchecked_Type_Conversion
(Exp
: Node_Id
) return Boolean is
12397 Pexp
: constant Node_Id
:= Parent
(Exp
);
12400 -- If the expression is the RHS of an assignment or object declaration
12401 -- we are always OK because there will always be a target.
12403 -- Object renaming declarations, (generated for view conversions of
12404 -- actuals in inlined calls), like object declarations, provide an
12405 -- explicit type, and are safe as well.
12407 if (Nkind
(Pexp
) = N_Assignment_Statement
12408 and then Expression
(Pexp
) = Exp
)
12409 or else Nkind_In
(Pexp
, N_Object_Declaration
,
12410 N_Object_Renaming_Declaration
)
12414 -- If the expression is the prefix of an N_Selected_Component we should
12415 -- also be OK because GCC knows to look inside the conversion except if
12416 -- the type is discriminated. We assume that we are OK anyway if the
12417 -- type is not set yet or if it is controlled since we can't afford to
12418 -- introduce a temporary in this case.
12420 elsif Nkind
(Pexp
) = N_Selected_Component
12421 and then Prefix
(Pexp
) = Exp
12423 if No
(Etype
(Pexp
)) then
12427 not Has_Discriminants
(Etype
(Pexp
))
12428 or else Is_Constrained
(Etype
(Pexp
));
12432 -- Set the output type, this comes from Etype if it is set, otherwise we
12433 -- take it from the subtype mark, which we assume was already fully
12436 if Present
(Etype
(Exp
)) then
12437 Otyp
:= Etype
(Exp
);
12439 Otyp
:= Entity
(Subtype_Mark
(Exp
));
12442 -- The input type always comes from the expression, and we assume this
12443 -- is indeed always analyzed, so we can simply get the Etype.
12445 Ityp
:= Etype
(Expression
(Exp
));
12447 -- Initialize alignments to unknown so far
12452 -- Replace a concurrent type by its corresponding record type and each
12453 -- type by its underlying type and do the tests on those. The original
12454 -- type may be a private type whose completion is a concurrent type, so
12455 -- find the underlying type first.
12457 if Present
(Underlying_Type
(Otyp
)) then
12458 Otyp
:= Underlying_Type
(Otyp
);
12461 if Present
(Underlying_Type
(Ityp
)) then
12462 Ityp
:= Underlying_Type
(Ityp
);
12465 if Is_Concurrent_Type
(Otyp
) then
12466 Otyp
:= Corresponding_Record_Type
(Otyp
);
12469 if Is_Concurrent_Type
(Ityp
) then
12470 Ityp
:= Corresponding_Record_Type
(Ityp
);
12473 -- If the base types are the same, we know there is no problem since
12474 -- this conversion will be a noop.
12476 if Implementation_Base_Type
(Otyp
) = Implementation_Base_Type
(Ityp
) then
12479 -- Same if this is an upwards conversion of an untagged type, and there
12480 -- are no constraints involved (could be more general???)
12482 elsif Etype
(Ityp
) = Otyp
12483 and then not Is_Tagged_Type
(Ityp
)
12484 and then not Has_Discriminants
(Ityp
)
12485 and then No
(First_Rep_Item
(Base_Type
(Ityp
)))
12489 -- If the expression has an access type (object or subprogram) we assume
12490 -- that the conversion is safe, because the size of the target is safe,
12491 -- even if it is a record (which might be treated as having unknown size
12494 elsif Is_Access_Type
(Ityp
) then
12497 -- If the size of output type is known at compile time, there is never
12498 -- a problem. Note that unconstrained records are considered to be of
12499 -- known size, but we can't consider them that way here, because we are
12500 -- talking about the actual size of the object.
12502 -- We also make sure that in addition to the size being known, we do not
12503 -- have a case which might generate an embarrassingly large temp in
12504 -- stack checking mode.
12506 elsif Size_Known_At_Compile_Time
(Otyp
)
12508 (not Stack_Checking_Enabled
12509 or else not May_Generate_Large_Temp
(Otyp
))
12510 and then not (Is_Record_Type
(Otyp
) and then not Is_Constrained
(Otyp
))
12514 -- If either type is tagged, then we know the alignment is OK so Gigi
12515 -- will be able to use pointer punning.
12517 elsif Is_Tagged_Type
(Otyp
) or else Is_Tagged_Type
(Ityp
) then
12520 -- If either type is a limited record type, we cannot do a copy, so say
12521 -- safe since there's nothing else we can do.
12523 elsif Is_Limited_Record
(Otyp
) or else Is_Limited_Record
(Ityp
) then
12526 -- Conversions to and from packed array types are always ignored and
12529 elsif Is_Packed_Array_Impl_Type
(Otyp
)
12530 or else Is_Packed_Array_Impl_Type
(Ityp
)
12535 -- The only other cases known to be safe is if the input type's
12536 -- alignment is known to be at least the maximum alignment for the
12537 -- target or if both alignments are known and the output type's
12538 -- alignment is no stricter than the input's. We can use the component
12539 -- type alignment for an array if a type is an unpacked array type.
12541 if Present
(Alignment_Clause
(Otyp
)) then
12542 Oalign
:= Expr_Value
(Expression
(Alignment_Clause
(Otyp
)));
12544 elsif Is_Array_Type
(Otyp
)
12545 and then Present
(Alignment_Clause
(Component_Type
(Otyp
)))
12547 Oalign
:= Expr_Value
(Expression
(Alignment_Clause
12548 (Component_Type
(Otyp
))));
12551 if Present
(Alignment_Clause
(Ityp
)) then
12552 Ialign
:= Expr_Value
(Expression
(Alignment_Clause
(Ityp
)));
12554 elsif Is_Array_Type
(Ityp
)
12555 and then Present
(Alignment_Clause
(Component_Type
(Ityp
)))
12557 Ialign
:= Expr_Value
(Expression
(Alignment_Clause
12558 (Component_Type
(Ityp
))));
12561 if Ialign
/= No_Uint
and then Ialign
> Maximum_Alignment
then
12564 elsif Ialign
/= No_Uint
12565 and then Oalign
/= No_Uint
12566 and then Ialign
<= Oalign
12570 -- Otherwise, Gigi cannot handle this and we must make a temporary
12575 end Safe_Unchecked_Type_Conversion
;
12577 ---------------------------------
12578 -- Set_Current_Value_Condition --
12579 ---------------------------------
12581 -- Note: the implementation of this procedure is very closely tied to the
12582 -- implementation of Get_Current_Value_Condition. Here we set required
12583 -- Current_Value fields, and in Get_Current_Value_Condition, we interpret
12584 -- them, so they must have a consistent view.
12586 procedure Set_Current_Value_Condition
(Cnode
: Node_Id
) is
12588 procedure Set_Entity_Current_Value
(N
: Node_Id
);
12589 -- If N is an entity reference, where the entity is of an appropriate
12590 -- kind, then set the current value of this entity to Cnode, unless
12591 -- there is already a definite value set there.
12593 procedure Set_Expression_Current_Value
(N
: Node_Id
);
12594 -- If N is of an appropriate form, sets an appropriate entry in current
12595 -- value fields of relevant entities. Multiple entities can be affected
12596 -- in the case of an AND or AND THEN.
12598 ------------------------------
12599 -- Set_Entity_Current_Value --
12600 ------------------------------
12602 procedure Set_Entity_Current_Value
(N
: Node_Id
) is
12604 if Is_Entity_Name
(N
) then
12606 Ent
: constant Entity_Id
:= Entity
(N
);
12609 -- Don't capture if not safe to do so
12611 if not Safe_To_Capture_Value
(N
, Ent
, Cond
=> True) then
12615 -- Here we have a case where the Current_Value field may need
12616 -- to be set. We set it if it is not already set to a compile
12617 -- time expression value.
12619 -- Note that this represents a decision that one condition
12620 -- blots out another previous one. That's certainly right if
12621 -- they occur at the same level. If the second one is nested,
12622 -- then the decision is neither right nor wrong (it would be
12623 -- equally OK to leave the outer one in place, or take the new
12624 -- inner one. Really we should record both, but our data
12625 -- structures are not that elaborate.
12627 if Nkind
(Current_Value
(Ent
)) not in N_Subexpr
then
12628 Set_Current_Value
(Ent
, Cnode
);
12632 end Set_Entity_Current_Value
;
12634 ----------------------------------
12635 -- Set_Expression_Current_Value --
12636 ----------------------------------
12638 procedure Set_Expression_Current_Value
(N
: Node_Id
) is
12644 -- Loop to deal with (ignore for now) any NOT operators present. The
12645 -- presence of NOT operators will be handled properly when we call
12646 -- Get_Current_Value_Condition.
12648 while Nkind
(Cond
) = N_Op_Not
loop
12649 Cond
:= Right_Opnd
(Cond
);
12652 -- For an AND or AND THEN, recursively process operands
12654 if Nkind
(Cond
) = N_Op_And
or else Nkind
(Cond
) = N_And_Then
then
12655 Set_Expression_Current_Value
(Left_Opnd
(Cond
));
12656 Set_Expression_Current_Value
(Right_Opnd
(Cond
));
12660 -- Check possible relational operator
12662 if Nkind
(Cond
) in N_Op_Compare
then
12663 if Compile_Time_Known_Value
(Right_Opnd
(Cond
)) then
12664 Set_Entity_Current_Value
(Left_Opnd
(Cond
));
12665 elsif Compile_Time_Known_Value
(Left_Opnd
(Cond
)) then
12666 Set_Entity_Current_Value
(Right_Opnd
(Cond
));
12669 elsif Nkind_In
(Cond
,
12671 N_Qualified_Expression
,
12672 N_Expression_With_Actions
)
12674 Set_Expression_Current_Value
(Expression
(Cond
));
12676 -- Check possible boolean variable reference
12679 Set_Entity_Current_Value
(Cond
);
12681 end Set_Expression_Current_Value
;
12683 -- Start of processing for Set_Current_Value_Condition
12686 Set_Expression_Current_Value
(Condition
(Cnode
));
12687 end Set_Current_Value_Condition
;
12689 --------------------------
12690 -- Set_Elaboration_Flag --
12691 --------------------------
12693 procedure Set_Elaboration_Flag
(N
: Node_Id
; Spec_Id
: Entity_Id
) is
12694 Loc
: constant Source_Ptr
:= Sloc
(N
);
12695 Ent
: constant Entity_Id
:= Elaboration_Entity
(Spec_Id
);
12699 if Present
(Ent
) then
12701 -- Nothing to do if at the compilation unit level, because in this
12702 -- case the flag is set by the binder generated elaboration routine.
12704 if Nkind
(Parent
(N
)) = N_Compilation_Unit
then
12707 -- Here we do need to generate an assignment statement
12710 Check_Restriction
(No_Elaboration_Code
, N
);
12713 Make_Assignment_Statement
(Loc
,
12714 Name
=> New_Occurrence_Of
(Ent
, Loc
),
12715 Expression
=> Make_Integer_Literal
(Loc
, Uint_1
));
12717 -- Mark the assignment statement as elaboration code. This allows
12718 -- the early call region mechanism (see Sem_Elab) to properly
12719 -- ignore such assignments even though they are non-preelaborable
12722 Set_Is_Elaboration_Code
(Asn
);
12724 if Nkind
(Parent
(N
)) = N_Subunit
then
12725 Insert_After
(Corresponding_Stub
(Parent
(N
)), Asn
);
12727 Insert_After
(N
, Asn
);
12732 -- Kill current value indication. This is necessary because the
12733 -- tests of this flag are inserted out of sequence and must not
12734 -- pick up bogus indications of the wrong constant value.
12736 Set_Current_Value
(Ent
, Empty
);
12738 -- If the subprogram is in the current declarative part and
12739 -- 'access has been applied to it, generate an elaboration
12740 -- check at the beginning of the declarations of the body.
12742 if Nkind
(N
) = N_Subprogram_Body
12743 and then Address_Taken
(Spec_Id
)
12745 Ekind_In
(Scope
(Spec_Id
), E_Block
, E_Procedure
, E_Function
)
12748 Loc
: constant Source_Ptr
:= Sloc
(N
);
12749 Decls
: constant List_Id
:= Declarations
(N
);
12753 -- No need to generate this check if first entry in the
12754 -- declaration list is a raise of Program_Error now.
12757 and then Nkind
(First
(Decls
)) = N_Raise_Program_Error
12762 -- Otherwise generate the check
12765 Make_Raise_Program_Error
(Loc
,
12768 Left_Opnd
=> New_Occurrence_Of
(Ent
, Loc
),
12769 Right_Opnd
=> Make_Integer_Literal
(Loc
, Uint_0
)),
12770 Reason
=> PE_Access_Before_Elaboration
);
12773 Set_Declarations
(N
, New_List
(Chk
));
12775 Prepend
(Chk
, Decls
);
12783 end Set_Elaboration_Flag
;
12785 ----------------------------
12786 -- Set_Renamed_Subprogram --
12787 ----------------------------
12789 procedure Set_Renamed_Subprogram
(N
: Node_Id
; E
: Entity_Id
) is
12791 -- If input node is an identifier, we can just reset it
12793 if Nkind
(N
) = N_Identifier
then
12794 Set_Chars
(N
, Chars
(E
));
12797 -- Otherwise we have to do a rewrite, preserving Comes_From_Source
12801 CS
: constant Boolean := Comes_From_Source
(N
);
12803 Rewrite
(N
, Make_Identifier
(Sloc
(N
), Chars
(E
)));
12805 Set_Comes_From_Source
(N
, CS
);
12806 Set_Analyzed
(N
, True);
12809 end Set_Renamed_Subprogram
;
12811 ----------------------
12812 -- Side_Effect_Free --
12813 ----------------------
12815 function Side_Effect_Free
12817 Name_Req
: Boolean := False;
12818 Variable_Ref
: Boolean := False) return Boolean
12820 Typ
: constant Entity_Id
:= Etype
(N
);
12821 -- Result type of the expression
12823 function Safe_Prefixed_Reference
(N
: Node_Id
) return Boolean;
12824 -- The argument N is a construct where the Prefix is dereferenced if it
12825 -- is an access type and the result is a variable. The call returns True
12826 -- if the construct is side effect free (not considering side effects in
12827 -- other than the prefix which are to be tested by the caller).
12829 function Within_In_Parameter
(N
: Node_Id
) return Boolean;
12830 -- Determines if N is a subcomponent of a composite in-parameter. If so,
12831 -- N is not side-effect free when the actual is global and modifiable
12832 -- indirectly from within a subprogram, because it may be passed by
12833 -- reference. The front-end must be conservative here and assume that
12834 -- this may happen with any array or record type. On the other hand, we
12835 -- cannot create temporaries for all expressions for which this
12836 -- condition is true, for various reasons that might require clearing up
12837 -- ??? For example, discriminant references that appear out of place, or
12838 -- spurious type errors with class-wide expressions. As a result, we
12839 -- limit the transformation to loop bounds, which is so far the only
12840 -- case that requires it.
12842 -----------------------------
12843 -- Safe_Prefixed_Reference --
12844 -----------------------------
12846 function Safe_Prefixed_Reference
(N
: Node_Id
) return Boolean is
12848 -- If prefix is not side effect free, definitely not safe
12850 if not Side_Effect_Free
(Prefix
(N
), Name_Req
, Variable_Ref
) then
12853 -- If the prefix is of an access type that is not access-to-constant,
12854 -- then this construct is a variable reference, which means it is to
12855 -- be considered to have side effects if Variable_Ref is set True.
12857 elsif Is_Access_Type
(Etype
(Prefix
(N
)))
12858 and then not Is_Access_Constant
(Etype
(Prefix
(N
)))
12859 and then Variable_Ref
12861 -- Exception is a prefix that is the result of a previous removal
12862 -- of side effects.
12864 return Is_Entity_Name
(Prefix
(N
))
12865 and then not Comes_From_Source
(Prefix
(N
))
12866 and then Ekind
(Entity
(Prefix
(N
))) = E_Constant
12867 and then Is_Internal_Name
(Chars
(Entity
(Prefix
(N
))));
12869 -- If the prefix is an explicit dereference then this construct is a
12870 -- variable reference, which means it is to be considered to have
12871 -- side effects if Variable_Ref is True.
12873 -- We do NOT exclude dereferences of access-to-constant types because
12874 -- we handle them as constant view of variables.
12876 elsif Nkind
(Prefix
(N
)) = N_Explicit_Dereference
12877 and then Variable_Ref
12881 -- Note: The following test is the simplest way of solving a complex
12882 -- problem uncovered by the following test (Side effect on loop bound
12883 -- that is a subcomponent of a global variable:
12885 -- with Text_Io; use Text_Io;
12886 -- procedure Tloop is
12889 -- V : Natural := 4;
12890 -- S : String (1..5) := (others => 'a');
12897 -- with procedure Action;
12898 -- procedure Loop_G (Arg : X; Msg : String)
12900 -- procedure Loop_G (Arg : X; Msg : String) is
12902 -- Put_Line ("begin loop_g " & Msg & " will loop till: "
12903 -- & Natural'Image (Arg.V));
12904 -- for Index in 1 .. Arg.V loop
12905 -- Text_Io.Put_Line
12906 -- (Natural'Image (Index) & " " & Arg.S (Index));
12907 -- if Index > 2 then
12911 -- Put_Line ("end loop_g " & Msg);
12914 -- procedure Loop1 is new Loop_G (Modi);
12915 -- procedure Modi is
12918 -- Loop1 (X1, "from modi");
12922 -- Loop1 (X1, "initial");
12925 -- The output of the above program should be:
12927 -- begin loop_g initial will loop till: 4
12931 -- begin loop_g from modi will loop till: 1
12933 -- end loop_g from modi
12935 -- begin loop_g from modi will loop till: 1
12937 -- end loop_g from modi
12938 -- end loop_g initial
12940 -- If a loop bound is a subcomponent of a global variable, a
12941 -- modification of that variable within the loop may incorrectly
12942 -- affect the execution of the loop.
12944 elsif Nkind
(Parent
(Parent
(N
))) = N_Loop_Parameter_Specification
12945 and then Within_In_Parameter
(Prefix
(N
))
12946 and then Variable_Ref
12950 -- All other cases are side effect free
12955 end Safe_Prefixed_Reference
;
12957 -------------------------
12958 -- Within_In_Parameter --
12959 -------------------------
12961 function Within_In_Parameter
(N
: Node_Id
) return Boolean is
12963 if not Comes_From_Source
(N
) then
12966 elsif Is_Entity_Name
(N
) then
12967 return Ekind
(Entity
(N
)) = E_In_Parameter
;
12969 elsif Nkind_In
(N
, N_Indexed_Component
, N_Selected_Component
) then
12970 return Within_In_Parameter
(Prefix
(N
));
12975 end Within_In_Parameter
;
12977 -- Start of processing for Side_Effect_Free
12980 -- If volatile reference, always consider it to have side effects
12982 if Is_Volatile_Reference
(N
) then
12986 -- Note on checks that could raise Constraint_Error. Strictly, if we
12987 -- take advantage of 11.6, these checks do not count as side effects.
12988 -- However, we would prefer to consider that they are side effects,
12989 -- since the back end CSE does not work very well on expressions which
12990 -- can raise Constraint_Error. On the other hand if we don't consider
12991 -- them to be side effect free, then we get some awkward expansions
12992 -- in -gnato mode, resulting in code insertions at a point where we
12993 -- do not have a clear model for performing the insertions.
12995 -- Special handling for entity names
12997 if Is_Entity_Name
(N
) then
12999 -- A type reference is always side effect free
13001 if Is_Type
(Entity
(N
)) then
13004 -- Variables are considered to be a side effect if Variable_Ref
13005 -- is set or if we have a volatile reference and Name_Req is off.
13006 -- If Name_Req is True then we can't help returning a name which
13007 -- effectively allows multiple references in any case.
13009 elsif Is_Variable
(N
, Use_Original_Node
=> False) then
13010 return not Variable_Ref
13011 and then (not Is_Volatile_Reference
(N
) or else Name_Req
);
13013 -- Any other entity (e.g. a subtype name) is definitely side
13020 -- A value known at compile time is always side effect free
13022 elsif Compile_Time_Known_Value
(N
) then
13025 -- A variable renaming is not side-effect free, because the renaming
13026 -- will function like a macro in the front-end in some cases, and an
13027 -- assignment can modify the component designated by N, so we need to
13028 -- create a temporary for it.
13030 -- The guard testing for Entity being present is needed at least in
13031 -- the case of rewritten predicate expressions, and may well also be
13032 -- appropriate elsewhere. Obviously we can't go testing the entity
13033 -- field if it does not exist, so it's reasonable to say that this is
13034 -- not the renaming case if it does not exist.
13036 elsif Is_Entity_Name
(Original_Node
(N
))
13037 and then Present
(Entity
(Original_Node
(N
)))
13038 and then Is_Renaming_Of_Object
(Entity
(Original_Node
(N
)))
13039 and then Ekind
(Entity
(Original_Node
(N
))) /= E_Constant
13042 RO
: constant Node_Id
:=
13043 Renamed_Object
(Entity
(Original_Node
(N
)));
13046 -- If the renamed object is an indexed component, or an
13047 -- explicit dereference, then the designated object could
13048 -- be modified by an assignment.
13050 if Nkind_In
(RO
, N_Indexed_Component
,
13051 N_Explicit_Dereference
)
13055 -- A selected component must have a safe prefix
13057 elsif Nkind
(RO
) = N_Selected_Component
then
13058 return Safe_Prefixed_Reference
(RO
);
13060 -- In all other cases, designated object cannot be changed so
13061 -- we are side effect free.
13068 -- Remove_Side_Effects generates an object renaming declaration to
13069 -- capture the expression of a class-wide expression. In VM targets
13070 -- the frontend performs no expansion for dispatching calls to
13071 -- class- wide types since they are handled by the VM. Hence, we must
13072 -- locate here if this node corresponds to a previous invocation of
13073 -- Remove_Side_Effects to avoid a never ending loop in the frontend.
13075 elsif not Tagged_Type_Expansion
13076 and then not Comes_From_Source
(N
)
13077 and then Nkind
(Parent
(N
)) = N_Object_Renaming_Declaration
13078 and then Is_Class_Wide_Type
(Typ
)
13082 -- Generating C the type conversion of an access to constrained array
13083 -- type into an access to unconstrained array type involves initializing
13084 -- a fat pointer and the expression cannot be assumed to be free of side
13085 -- effects since it must referenced several times to compute its bounds.
13087 elsif Modify_Tree_For_C
13088 and then Nkind
(N
) = N_Type_Conversion
13089 and then Is_Access_Type
(Typ
)
13090 and then Is_Array_Type
(Designated_Type
(Typ
))
13091 and then not Is_Constrained
(Designated_Type
(Typ
))
13096 -- For other than entity names and compile time known values,
13097 -- check the node kind for special processing.
13101 -- An attribute reference is side effect free if its expressions
13102 -- are side effect free and its prefix is side effect free or
13103 -- is an entity reference.
13105 -- Is this right? what about x'first where x is a variable???
13107 when N_Attribute_Reference
=>
13108 Attribute_Reference
: declare
13110 function Side_Effect_Free_Attribute
13111 (Attribute_Name
: Name_Id
) return Boolean;
13112 -- Returns True if evaluation of the given attribute is
13113 -- considered side-effect free (independent of prefix and
13116 --------------------------------
13117 -- Side_Effect_Free_Attribute --
13118 --------------------------------
13120 function Side_Effect_Free_Attribute
13121 (Attribute_Name
: Name_Id
) return Boolean
13124 case Attribute_Name
is
13131 | Name_Wide_Wide_Image
13133 -- CodePeer doesn't want to see replicated copies of
13136 return not CodePeer_Mode
;
13141 end Side_Effect_Free_Attribute
;
13143 -- Start of processing for Attribute_Reference
13147 Side_Effect_Free
(Expressions
(N
), Name_Req
, Variable_Ref
)
13148 and then Side_Effect_Free_Attribute
(Attribute_Name
(N
))
13149 and then (Is_Entity_Name
(Prefix
(N
))
13150 or else Side_Effect_Free
13151 (Prefix
(N
), Name_Req
, Variable_Ref
));
13152 end Attribute_Reference
;
13154 -- A binary operator is side effect free if and both operands are
13155 -- side effect free. For this purpose binary operators include
13156 -- membership tests and short circuit forms.
13159 | N_Membership_Test
13162 return Side_Effect_Free
(Left_Opnd
(N
), Name_Req
, Variable_Ref
)
13164 Side_Effect_Free
(Right_Opnd
(N
), Name_Req
, Variable_Ref
);
13166 -- An explicit dereference is side effect free only if it is
13167 -- a side effect free prefixed reference.
13169 when N_Explicit_Dereference
=>
13170 return Safe_Prefixed_Reference
(N
);
13172 -- An expression with action is side effect free if its expression
13173 -- is side effect free and it has no actions.
13175 when N_Expression_With_Actions
=>
13177 Is_Empty_List
(Actions
(N
))
13178 and then Side_Effect_Free
13179 (Expression
(N
), Name_Req
, Variable_Ref
);
13181 -- A call to _rep_to_pos is side effect free, since we generate
13182 -- this pure function call ourselves. Moreover it is critically
13183 -- important to make this exception, since otherwise we can have
13184 -- discriminants in array components which don't look side effect
13185 -- free in the case of an array whose index type is an enumeration
13186 -- type with an enumeration rep clause.
13188 -- All other function calls are not side effect free
13190 when N_Function_Call
=>
13192 Nkind
(Name
(N
)) = N_Identifier
13193 and then Is_TSS
(Name
(N
), TSS_Rep_To_Pos
)
13194 and then Side_Effect_Free
13195 (First
(Parameter_Associations
(N
)),
13196 Name_Req
, Variable_Ref
);
13198 -- An IF expression is side effect free if it's of a scalar type, and
13199 -- all its components are all side effect free (conditions and then
13200 -- actions and else actions). We restrict to scalar types, since it
13201 -- is annoying to deal with things like (if A then B else C)'First
13202 -- where the type involved is a string type.
13204 when N_If_Expression
=>
13206 Is_Scalar_Type
(Typ
)
13207 and then Side_Effect_Free
13208 (Expressions
(N
), Name_Req
, Variable_Ref
);
13210 -- An indexed component is side effect free if it is a side
13211 -- effect free prefixed reference and all the indexing
13212 -- expressions are side effect free.
13214 when N_Indexed_Component
=>
13216 Side_Effect_Free
(Expressions
(N
), Name_Req
, Variable_Ref
)
13217 and then Safe_Prefixed_Reference
(N
);
13219 -- A type qualification, type conversion, or unchecked expression is
13220 -- side effect free if the expression is side effect free.
13222 when N_Qualified_Expression
13223 | N_Type_Conversion
13224 | N_Unchecked_Expression
13226 return Side_Effect_Free
(Expression
(N
), Name_Req
, Variable_Ref
);
13228 -- A selected component is side effect free only if it is a side
13229 -- effect free prefixed reference.
13231 when N_Selected_Component
=>
13232 return Safe_Prefixed_Reference
(N
);
13234 -- A range is side effect free if the bounds are side effect free
13237 return Side_Effect_Free
(Low_Bound
(N
), Name_Req
, Variable_Ref
)
13239 Side_Effect_Free
(High_Bound
(N
), Name_Req
, Variable_Ref
);
13241 -- A slice is side effect free if it is a side effect free
13242 -- prefixed reference and the bounds are side effect free.
13246 Side_Effect_Free
(Discrete_Range
(N
), Name_Req
, Variable_Ref
)
13247 and then Safe_Prefixed_Reference
(N
);
13249 -- A unary operator is side effect free if the operand
13250 -- is side effect free.
13253 return Side_Effect_Free
(Right_Opnd
(N
), Name_Req
, Variable_Ref
);
13255 -- An unchecked type conversion is side effect free only if it
13256 -- is safe and its argument is side effect free.
13258 when N_Unchecked_Type_Conversion
=>
13260 Safe_Unchecked_Type_Conversion
(N
)
13261 and then Side_Effect_Free
13262 (Expression
(N
), Name_Req
, Variable_Ref
);
13264 -- A literal is side effect free
13266 when N_Character_Literal
13267 | N_Integer_Literal
13273 -- We consider that anything else has side effects. This is a bit
13274 -- crude, but we are pretty close for most common cases, and we
13275 -- are certainly correct (i.e. we never return True when the
13276 -- answer should be False).
13281 end Side_Effect_Free
;
13283 -- A list is side effect free if all elements of the list are side
13286 function Side_Effect_Free
13288 Name_Req
: Boolean := False;
13289 Variable_Ref
: Boolean := False) return Boolean
13294 if L
= No_List
or else L
= Error_List
then
13299 while Present
(N
) loop
13300 if not Side_Effect_Free
(N
, Name_Req
, Variable_Ref
) then
13309 end Side_Effect_Free
;
13311 ----------------------------------
13312 -- Silly_Boolean_Array_Not_Test --
13313 ----------------------------------
13315 -- This procedure implements an odd and silly test. We explicitly check
13316 -- for the case where the 'First of the component type is equal to the
13317 -- 'Last of this component type, and if this is the case, we make sure
13318 -- that constraint error is raised. The reason is that the NOT is bound
13319 -- to cause CE in this case, and we will not otherwise catch it.
13321 -- No such check is required for AND and OR, since for both these cases
13322 -- False op False = False, and True op True = True. For the XOR case,
13323 -- see Silly_Boolean_Array_Xor_Test.
13325 -- Believe it or not, this was reported as a bug. Note that nearly always,
13326 -- the test will evaluate statically to False, so the code will be
13327 -- statically removed, and no extra overhead caused.
13329 procedure Silly_Boolean_Array_Not_Test
(N
: Node_Id
; T
: Entity_Id
) is
13330 Loc
: constant Source_Ptr
:= Sloc
(N
);
13331 CT
: constant Entity_Id
:= Component_Type
(T
);
13334 -- The check we install is
13336 -- constraint_error when
13337 -- component_type'first = component_type'last
13338 -- and then array_type'Length /= 0)
13340 -- We need the last guard because we don't want to raise CE for empty
13341 -- arrays since no out of range values result. (Empty arrays with a
13342 -- component type of True .. True -- very useful -- even the ACATS
13343 -- does not test that marginal case).
13346 Make_Raise_Constraint_Error
(Loc
,
13348 Make_And_Then
(Loc
,
13352 Make_Attribute_Reference
(Loc
,
13353 Prefix
=> New_Occurrence_Of
(CT
, Loc
),
13354 Attribute_Name
=> Name_First
),
13357 Make_Attribute_Reference
(Loc
,
13358 Prefix
=> New_Occurrence_Of
(CT
, Loc
),
13359 Attribute_Name
=> Name_Last
)),
13361 Right_Opnd
=> Make_Non_Empty_Check
(Loc
, Right_Opnd
(N
))),
13362 Reason
=> CE_Range_Check_Failed
));
13363 end Silly_Boolean_Array_Not_Test
;
13365 ----------------------------------
13366 -- Silly_Boolean_Array_Xor_Test --
13367 ----------------------------------
13369 -- This procedure implements an odd and silly test. We explicitly check
13370 -- for the XOR case where the component type is True .. True, since this
13371 -- will raise constraint error. A special check is required since CE
13372 -- will not be generated otherwise (cf Expand_Packed_Not).
13374 -- No such check is required for AND and OR, since for both these cases
13375 -- False op False = False, and True op True = True, and no check is
13376 -- required for the case of False .. False, since False xor False = False.
13377 -- See also Silly_Boolean_Array_Not_Test
13379 procedure Silly_Boolean_Array_Xor_Test
(N
: Node_Id
; T
: Entity_Id
) is
13380 Loc
: constant Source_Ptr
:= Sloc
(N
);
13381 CT
: constant Entity_Id
:= Component_Type
(T
);
13384 -- The check we install is
13386 -- constraint_error when
13387 -- Boolean (component_type'First)
13388 -- and then Boolean (component_type'Last)
13389 -- and then array_type'Length /= 0)
13391 -- We need the last guard because we don't want to raise CE for empty
13392 -- arrays since no out of range values result (Empty arrays with a
13393 -- component type of True .. True -- very useful -- even the ACATS
13394 -- does not test that marginal case).
13397 Make_Raise_Constraint_Error
(Loc
,
13399 Make_And_Then
(Loc
,
13401 Make_And_Then
(Loc
,
13403 Convert_To
(Standard_Boolean
,
13404 Make_Attribute_Reference
(Loc
,
13405 Prefix
=> New_Occurrence_Of
(CT
, Loc
),
13406 Attribute_Name
=> Name_First
)),
13409 Convert_To
(Standard_Boolean
,
13410 Make_Attribute_Reference
(Loc
,
13411 Prefix
=> New_Occurrence_Of
(CT
, Loc
),
13412 Attribute_Name
=> Name_Last
))),
13414 Right_Opnd
=> Make_Non_Empty_Check
(Loc
, Right_Opnd
(N
))),
13415 Reason
=> CE_Range_Check_Failed
));
13416 end Silly_Boolean_Array_Xor_Test
;
13418 --------------------------
13419 -- Target_Has_Fixed_Ops --
13420 --------------------------
13422 Integer_Sized_Small
: Ureal
;
13423 -- Set to 2.0 ** -(Integer'Size - 1) the first time that this function is
13424 -- called (we don't want to compute it more than once).
13426 Long_Integer_Sized_Small
: Ureal
;
13427 -- Set to 2.0 ** -(Long_Integer'Size - 1) the first time that this function
13428 -- is called (we don't want to compute it more than once)
13430 First_Time_For_THFO
: Boolean := True;
13431 -- Set to False after first call (if Fractional_Fixed_Ops_On_Target)
13433 function Target_Has_Fixed_Ops
13434 (Left_Typ
: Entity_Id
;
13435 Right_Typ
: Entity_Id
;
13436 Result_Typ
: Entity_Id
) return Boolean
13438 function Is_Fractional_Type
(Typ
: Entity_Id
) return Boolean;
13439 -- Return True if the given type is a fixed-point type with a small
13440 -- value equal to 2 ** (-(T'Object_Size - 1)) and whose values have
13441 -- an absolute value less than 1.0. This is currently limited to
13442 -- fixed-point types that map to Integer or Long_Integer.
13444 ------------------------
13445 -- Is_Fractional_Type --
13446 ------------------------
13448 function Is_Fractional_Type
(Typ
: Entity_Id
) return Boolean is
13450 if Esize
(Typ
) = Standard_Integer_Size
then
13451 return Small_Value
(Typ
) = Integer_Sized_Small
;
13453 elsif Esize
(Typ
) = Standard_Long_Integer_Size
then
13454 return Small_Value
(Typ
) = Long_Integer_Sized_Small
;
13459 end Is_Fractional_Type
;
13461 -- Start of processing for Target_Has_Fixed_Ops
13464 -- Return False if Fractional_Fixed_Ops_On_Target is false
13466 if not Fractional_Fixed_Ops_On_Target
then
13470 -- Here the target has Fractional_Fixed_Ops, if first time, compute
13471 -- standard constants used by Is_Fractional_Type.
13473 if First_Time_For_THFO
then
13474 First_Time_For_THFO
:= False;
13476 Integer_Sized_Small
:=
13479 Den
=> UI_From_Int
(Standard_Integer_Size
- 1),
13482 Long_Integer_Sized_Small
:=
13485 Den
=> UI_From_Int
(Standard_Long_Integer_Size
- 1),
13489 -- Return True if target supports fixed-by-fixed multiply/divide for
13490 -- fractional fixed-point types (see Is_Fractional_Type) and the operand
13491 -- and result types are equivalent fractional types.
13493 return Is_Fractional_Type
(Base_Type
(Left_Typ
))
13494 and then Is_Fractional_Type
(Base_Type
(Right_Typ
))
13495 and then Is_Fractional_Type
(Base_Type
(Result_Typ
))
13496 and then Esize
(Left_Typ
) = Esize
(Right_Typ
)
13497 and then Esize
(Left_Typ
) = Esize
(Result_Typ
);
13498 end Target_Has_Fixed_Ops
;
13500 -------------------
13501 -- Type_Map_Hash --
13502 -------------------
13504 function Type_Map_Hash
(Id
: Entity_Id
) return Type_Map_Header
is
13506 return Type_Map_Header
(Id
mod Type_Map_Size
);
13509 ------------------------------------------
13510 -- Type_May_Have_Bit_Aligned_Components --
13511 ------------------------------------------
13513 function Type_May_Have_Bit_Aligned_Components
13514 (Typ
: Entity_Id
) return Boolean
13517 -- Array type, check component type
13519 if Is_Array_Type
(Typ
) then
13521 Type_May_Have_Bit_Aligned_Components
(Component_Type
(Typ
));
13523 -- Record type, check components
13525 elsif Is_Record_Type
(Typ
) then
13530 E
:= First_Component_Or_Discriminant
(Typ
);
13531 while Present
(E
) loop
13532 if Component_May_Be_Bit_Aligned
(E
)
13533 or else Type_May_Have_Bit_Aligned_Components
(Etype
(E
))
13538 Next_Component_Or_Discriminant
(E
);
13544 -- Type other than array or record is always OK
13549 end Type_May_Have_Bit_Aligned_Components
;
13551 -------------------------------
13552 -- Update_Primitives_Mapping --
13553 -------------------------------
13555 procedure Update_Primitives_Mapping
13556 (Inher_Id
: Entity_Id
;
13557 Subp_Id
: Entity_Id
)
13561 (Parent_Type
=> Find_Dispatching_Type
(Inher_Id
),
13562 Derived_Type
=> Find_Dispatching_Type
(Subp_Id
));
13563 end Update_Primitives_Mapping
;
13565 ----------------------------------
13566 -- Within_Case_Or_If_Expression --
13567 ----------------------------------
13569 function Within_Case_Or_If_Expression
(N
: Node_Id
) return Boolean is
13573 -- Locate an enclosing case or if expression. Note that these constructs
13574 -- can be expanded into Expression_With_Actions, hence the test of the
13578 while Present
(Par
) loop
13579 if Nkind_In
(Original_Node
(Par
), N_Case_Expression
,
13584 -- Prevent the search from going too far
13586 elsif Is_Body_Or_Package_Declaration
(Par
) then
13590 Par
:= Parent
(Par
);
13594 end Within_Case_Or_If_Expression
;
13596 --------------------------------
13597 -- Within_Internal_Subprogram --
13598 --------------------------------
13600 function Within_Internal_Subprogram
return Boolean is
13604 S
:= Current_Scope
;
13605 while Present
(S
) and then not Is_Subprogram
(S
) loop
13610 and then Get_TSS_Name
(S
) /= TSS_Null
13611 and then not Is_Predicate_Function
(S
)
13612 and then not Is_Predicate_Function_M
(S
);
13613 end Within_Internal_Subprogram
;