1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2017, 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 -- Certain run-time configurations and targets do not provide support
686 -- for controlled types.
688 if Restriction_Active
(No_Finalization
) then
691 -- Do nothing if the access type may never allocate / deallocate
694 elsif No_Pool_Assigned
(Ptr_Typ
) then
698 -- The allocation / deallocation of a controlled object must be
699 -- chained on / detached from a finalization master.
701 pragma Assert
(Present
(Finalization_Master
(Ptr_Typ
)));
703 -- The only other kind of allocation / deallocation supported by this
704 -- routine is on / from a subpool.
706 elsif Nkind
(Expr
) = N_Allocator
707 and then No
(Subpool_Handle_Name
(Expr
))
713 Loc
: constant Source_Ptr
:= Sloc
(N
);
714 Addr_Id
: constant Entity_Id
:= Make_Temporary
(Loc
, 'A');
715 Alig_Id
: constant Entity_Id
:= Make_Temporary
(Loc
, 'L');
716 Proc_Id
: constant Entity_Id
:= Make_Temporary
(Loc
, 'P');
717 Size_Id
: constant Entity_Id
:= Make_Temporary
(Loc
, 'S');
720 Fin_Addr_Id
: Entity_Id
;
721 Fin_Mas_Act
: Node_Id
;
722 Fin_Mas_Id
: Entity_Id
;
723 Proc_To_Call
: Entity_Id
;
724 Subpool
: Node_Id
:= Empty
;
727 -- Step 1: Construct all the actuals for the call to library routine
728 -- Allocate_Any_Controlled / Deallocate_Any_Controlled.
732 Actuals
:= New_List
(New_Occurrence_Of
(Pool_Id
, Loc
));
738 if Nkind
(Expr
) = N_Allocator
then
739 Subpool
:= Subpool_Handle_Name
(Expr
);
742 -- If a subpool is present it can be an arbitrary name, so make
743 -- the actual by copying the tree.
745 if Present
(Subpool
) then
746 Append_To
(Actuals
, New_Copy_Tree
(Subpool
, New_Sloc
=> Loc
));
748 Append_To
(Actuals
, Make_Null
(Loc
));
751 -- c) Finalization master
754 Fin_Mas_Id
:= Finalization_Master
(Ptr_Typ
);
755 Fin_Mas_Act
:= New_Occurrence_Of
(Fin_Mas_Id
, Loc
);
757 -- Handle the case where the master is actually a pointer to a
758 -- master. This case arises in build-in-place functions.
760 if Is_Access_Type
(Etype
(Fin_Mas_Id
)) then
761 Append_To
(Actuals
, Fin_Mas_Act
);
764 Make_Attribute_Reference
(Loc
,
765 Prefix
=> Fin_Mas_Act
,
766 Attribute_Name
=> Name_Unrestricted_Access
));
769 Append_To
(Actuals
, Make_Null
(Loc
));
772 -- d) Finalize_Address
774 -- Primitive Finalize_Address is never generated in CodePeer mode
775 -- since it contains an Unchecked_Conversion.
777 if Needs_Fin
and then not CodePeer_Mode
then
778 Fin_Addr_Id
:= Finalize_Address
(Desig_Typ
);
779 pragma Assert
(Present
(Fin_Addr_Id
));
782 Make_Attribute_Reference
(Loc
,
783 Prefix
=> New_Occurrence_Of
(Fin_Addr_Id
, Loc
),
784 Attribute_Name
=> Name_Unrestricted_Access
));
786 Append_To
(Actuals
, Make_Null
(Loc
));
794 Append_To
(Actuals
, New_Occurrence_Of
(Addr_Id
, Loc
));
795 Append_To
(Actuals
, New_Occurrence_Of
(Size_Id
, Loc
));
797 if Is_Allocate
or else not Is_Class_Wide_Type
(Desig_Typ
) then
798 Append_To
(Actuals
, New_Occurrence_Of
(Alig_Id
, Loc
));
800 -- For deallocation of class-wide types we obtain the value of
801 -- alignment from the Type Specific Record of the deallocated object.
802 -- This is needed because the frontend expansion of class-wide types
803 -- into equivalent types confuses the back end.
809 -- ... because 'Alignment applied to class-wide types is expanded
810 -- into the code that reads the value of alignment from the TSD
811 -- (see Expand_N_Attribute_Reference)
814 Unchecked_Convert_To
(RTE
(RE_Storage_Offset
),
815 Make_Attribute_Reference
(Loc
,
817 Make_Explicit_Dereference
(Loc
, Relocate_Node
(Expr
)),
818 Attribute_Name
=> Name_Alignment
)));
824 Is_Controlled
: declare
825 Flag_Id
: constant Entity_Id
:= Make_Temporary
(Loc
, 'F');
833 Temp
:= Find_Object
(Expression
(Expr
));
838 -- Processing for allocations where the expression is a subtype
842 and then Is_Entity_Name
(Temp
)
843 and then Is_Type
(Entity
(Temp
))
848 (Needs_Finalization
(Entity
(Temp
))), Loc
);
850 -- The allocation / deallocation of a class-wide object relies
851 -- on a runtime check to determine whether the object is truly
852 -- controlled or not. Depending on this check, the finalization
853 -- machinery will request or reclaim extra storage reserved for
856 elsif Is_Class_Wide_Type
(Desig_Typ
) then
858 -- Detect a special case where interface class-wide types
859 -- are involved as the object appears as:
861 -- Tag_Ptr (Base_Address (<object>'Address))
863 -- The expression already yields the proper tag, generate:
867 if Is_RTE
(Etype
(Temp
), RE_Tag_Ptr
) then
869 Make_Explicit_Dereference
(Loc
,
870 Prefix
=> Relocate_Node
(Temp
));
872 -- In the default case, obtain the tag of the object about
873 -- to be allocated / deallocated. Generate:
877 -- If the object is an unchecked conversion (typically to
878 -- an access to class-wide type), we must preserve the
879 -- conversion to ensure that the object is seen as tagged
880 -- in the code that follows.
885 if Nkind
(Parent
(Pref
)) = N_Unchecked_Type_Conversion
887 Pref
:= Parent
(Pref
);
891 Make_Attribute_Reference
(Loc
,
892 Prefix
=> Relocate_Node
(Pref
),
893 Attribute_Name
=> Name_Tag
);
897 -- Needs_Finalization (<Param>)
900 Make_Function_Call
(Loc
,
902 New_Occurrence_Of
(RTE
(RE_Needs_Finalization
), Loc
),
903 Parameter_Associations
=> New_List
(Param
));
905 -- Processing for generic actuals
907 elsif Is_Generic_Actual_Type
(Desig_Typ
) then
909 New_Occurrence_Of
(Boolean_Literals
910 (Needs_Finalization
(Base_Type
(Desig_Typ
))), Loc
);
912 -- The object does not require any specialized checks, it is
913 -- known to be controlled.
916 Flag_Expr
:= New_Occurrence_Of
(Standard_True
, Loc
);
919 -- Create the temporary which represents the finalization state
920 -- of the expression. Generate:
922 -- F : constant Boolean := <Flag_Expr>;
925 Make_Object_Declaration
(Loc
,
926 Defining_Identifier
=> Flag_Id
,
927 Constant_Present
=> True,
929 New_Occurrence_Of
(Standard_Boolean
, Loc
),
930 Expression
=> Flag_Expr
));
932 Append_To
(Actuals
, New_Occurrence_Of
(Flag_Id
, Loc
));
935 -- The object is not controlled
938 Append_To
(Actuals
, New_Occurrence_Of
(Standard_False
, Loc
));
945 New_Occurrence_Of
(Boolean_Literals
(Present
(Subpool
)), Loc
));
948 -- Step 2: Build a wrapper Allocate / Deallocate which internally
949 -- calls Allocate_Any_Controlled / Deallocate_Any_Controlled.
951 -- Select the proper routine to call
954 Proc_To_Call
:= RTE
(RE_Allocate_Any_Controlled
);
956 Proc_To_Call
:= RTE
(RE_Deallocate_Any_Controlled
);
959 -- Create a custom Allocate / Deallocate routine which has identical
960 -- profile to that of System.Storage_Pools.
963 Make_Subprogram_Body
(Loc
,
968 Make_Procedure_Specification
(Loc
,
969 Defining_Unit_Name
=> Proc_Id
,
970 Parameter_Specifications
=> New_List
(
972 -- P : Root_Storage_Pool
974 Make_Parameter_Specification
(Loc
,
975 Defining_Identifier
=> Make_Temporary
(Loc
, 'P'),
977 New_Occurrence_Of
(RTE
(RE_Root_Storage_Pool
), Loc
)),
981 Make_Parameter_Specification
(Loc
,
982 Defining_Identifier
=> Addr_Id
,
983 Out_Present
=> Is_Allocate
,
985 New_Occurrence_Of
(RTE
(RE_Address
), Loc
)),
989 Make_Parameter_Specification
(Loc
,
990 Defining_Identifier
=> Size_Id
,
992 New_Occurrence_Of
(RTE
(RE_Storage_Count
), Loc
)),
996 Make_Parameter_Specification
(Loc
,
997 Defining_Identifier
=> Alig_Id
,
999 New_Occurrence_Of
(RTE
(RE_Storage_Count
), Loc
)))),
1001 Declarations
=> No_List
,
1003 Handled_Statement_Sequence
=>
1004 Make_Handled_Sequence_Of_Statements
(Loc
,
1005 Statements
=> New_List
(
1006 Make_Procedure_Call_Statement
(Loc
,
1008 New_Occurrence_Of
(Proc_To_Call
, Loc
),
1009 Parameter_Associations
=> Actuals
)))),
1010 Suppress
=> All_Checks
);
1012 -- The newly generated Allocate / Deallocate becomes the default
1013 -- procedure to call when the back end processes the allocation /
1017 Set_Procedure_To_Call
(Expr
, Proc_Id
);
1019 Set_Procedure_To_Call
(N
, Proc_Id
);
1022 end Build_Allocate_Deallocate_Proc
;
1024 -------------------------------
1025 -- Build_Abort_Undefer_Block --
1026 -------------------------------
1028 function Build_Abort_Undefer_Block
1031 Context
: Node_Id
) return Node_Id
1033 Exceptions_OK
: constant Boolean :=
1034 not Restriction_Active
(No_Exception_Propagation
);
1042 -- The block should be generated only when undeferring abort in the
1043 -- context of a potential exception.
1045 pragma Assert
(Abort_Allowed
and Exceptions_OK
);
1051 -- Abort_Undefer_Direct;
1054 AUD
:= RTE
(RE_Abort_Undefer_Direct
);
1057 Make_Handled_Sequence_Of_Statements
(Loc
,
1058 Statements
=> Stmts
,
1059 At_End_Proc
=> New_Occurrence_Of
(AUD
, Loc
));
1062 Make_Block_Statement
(Loc
,
1063 Handled_Statement_Sequence
=> HSS
);
1064 Set_Is_Abort_Block
(Blk
);
1066 Add_Block_Identifier
(Blk
, Blk_Id
);
1067 Expand_At_End_Handler
(HSS
, Blk_Id
);
1069 -- Present the Abort_Undefer_Direct function to the back end to inline
1070 -- the call to the routine.
1072 Add_Inlined_Body
(AUD
, Context
);
1075 end Build_Abort_Undefer_Block
;
1077 ---------------------------------
1078 -- Build_Class_Wide_Expression --
1079 ---------------------------------
1081 procedure Build_Class_Wide_Expression
1084 Par_Subp
: Entity_Id
;
1085 Adjust_Sloc
: Boolean;
1086 Needs_Wrapper
: out Boolean)
1088 function Replace_Entity
(N
: Node_Id
) return Traverse_Result
;
1089 -- Replace reference to formal of inherited operation or to primitive
1090 -- operation of root type, with corresponding entity for derived type,
1091 -- when constructing the class-wide condition of an overriding
1094 --------------------
1095 -- Replace_Entity --
1096 --------------------
1098 function Replace_Entity
(N
: Node_Id
) return Traverse_Result
is
1103 Adjust_Inherited_Pragma_Sloc
(N
);
1106 if Nkind
(N
) = N_Identifier
1107 and then Present
(Entity
(N
))
1109 (Is_Formal
(Entity
(N
)) or else Is_Subprogram
(Entity
(N
)))
1111 (Nkind
(Parent
(N
)) /= N_Attribute_Reference
1112 or else Attribute_Name
(Parent
(N
)) /= Name_Class
)
1114 -- The replacement does not apply to dispatching calls within the
1115 -- condition, but only to calls whose static tag is that of the
1118 if Is_Subprogram
(Entity
(N
))
1119 and then Nkind
(Parent
(N
)) = N_Function_Call
1120 and then Present
(Controlling_Argument
(Parent
(N
)))
1125 -- Determine whether entity has a renaming
1127 New_E
:= Type_Map
.Get
(Entity
(N
));
1129 if Present
(New_E
) then
1130 Rewrite
(N
, New_Occurrence_Of
(New_E
, Sloc
(N
)));
1132 -- If the entity is an overridden primitive and we are not
1133 -- in GNATprove mode, we must build a wrapper for the current
1134 -- inherited operation. If the reference is the prefix of an
1135 -- attribute such as 'Result (or others ???) there is no need
1136 -- for a wrapper: the condition is just rewritten in terms of
1137 -- the inherited subprogram.
1139 if Is_Subprogram
(New_E
)
1140 and then Nkind
(Parent
(N
)) /= N_Attribute_Reference
1141 and then not GNATprove_Mode
1143 Needs_Wrapper
:= True;
1147 -- Check that there are no calls left to abstract operations if
1148 -- the current subprogram is not abstract.
1150 if Nkind
(Parent
(N
)) = N_Function_Call
1151 and then N
= Name
(Parent
(N
))
1153 if not Is_Abstract_Subprogram
(Subp
)
1154 and then Is_Abstract_Subprogram
(Entity
(N
))
1156 Error_Msg_Sloc
:= Sloc
(Current_Scope
);
1157 Error_Msg_Node_2
:= Subp
;
1158 if Comes_From_Source
(Subp
) then
1160 ("cannot call abstract subprogram & in inherited "
1161 & "condition for&#", Subp
, Entity
(N
));
1164 ("cannot call abstract subprogram & in inherited "
1165 & "condition for inherited&#", Subp
, Entity
(N
));
1168 -- In SPARK mode, reject an inherited condition for an
1169 -- inherited operation if it contains a call to an overriding
1170 -- operation, because this implies that the pre/postconditions
1171 -- of the inherited operation have changed silently.
1173 elsif SPARK_Mode
= On
1174 and then Warn_On_Suspicious_Contract
1175 and then Present
(Alias
(Subp
))
1176 and then Present
(New_E
)
1177 and then Comes_From_Source
(New_E
)
1180 ("cannot modify inherited condition (SPARK RM 6.1.1(1))",
1182 Error_Msg_Sloc
:= Sloc
(New_E
);
1183 Error_Msg_Node_2
:= Subp
;
1185 ("\overriding of&# forces overriding of&",
1186 Parent
(Subp
), New_E
);
1190 -- Update type of function call node, which should be the same as
1191 -- the function's return type.
1193 if Is_Subprogram
(Entity
(N
))
1194 and then Nkind
(Parent
(N
)) = N_Function_Call
1196 Set_Etype
(Parent
(N
), Etype
(Entity
(N
)));
1199 -- The whole expression will be reanalyzed
1201 elsif Nkind
(N
) in N_Has_Etype
then
1202 Set_Analyzed
(N
, False);
1208 procedure Replace_Condition_Entities
is
1209 new Traverse_Proc
(Replace_Entity
);
1213 Par_Formal
: Entity_Id
;
1214 Subp_Formal
: Entity_Id
;
1216 -- Start of processing for Build_Class_Wide_Expression
1219 Needs_Wrapper
:= False;
1221 -- Add mapping from old formals to new formals
1223 Par_Formal
:= First_Formal
(Par_Subp
);
1224 Subp_Formal
:= First_Formal
(Subp
);
1226 while Present
(Par_Formal
) and then Present
(Subp_Formal
) loop
1227 Type_Map
.Set
(Par_Formal
, Subp_Formal
);
1228 Next_Formal
(Par_Formal
);
1229 Next_Formal
(Subp_Formal
);
1232 Replace_Condition_Entities
(Prag
);
1233 end Build_Class_Wide_Expression
;
1235 --------------------
1236 -- Build_DIC_Call --
1237 --------------------
1239 function Build_DIC_Call
1242 Typ
: Entity_Id
) return Node_Id
1244 Proc_Id
: constant Entity_Id
:= DIC_Procedure
(Typ
);
1245 Formal_Typ
: constant Entity_Id
:= Etype
(First_Formal
(Proc_Id
));
1249 Make_Procedure_Call_Statement
(Loc
,
1250 Name
=> New_Occurrence_Of
(Proc_Id
, Loc
),
1251 Parameter_Associations
=> New_List
(
1252 Make_Unchecked_Type_Conversion
(Loc
,
1253 Subtype_Mark
=> New_Occurrence_Of
(Formal_Typ
, Loc
),
1254 Expression
=> New_Occurrence_Of
(Obj_Id
, Loc
))));
1257 ------------------------------
1258 -- Build_DIC_Procedure_Body --
1259 ------------------------------
1261 -- WARNING: This routine manages Ghost regions. Return statements must be
1262 -- replaced by gotos which jump to the end of the routine and restore the
1265 procedure Build_DIC_Procedure_Body
1267 For_Freeze
: Boolean := False)
1269 procedure Add_DIC_Check
1270 (DIC_Prag
: Node_Id
;
1272 Stmts
: in out List_Id
);
1273 -- Subsidiary to all Add_xxx_DIC routines. Add a runtime check to verify
1274 -- assertion expression DIC_Expr of pragma DIC_Prag. All generated code
1275 -- is added to list Stmts.
1277 procedure Add_Inherited_DIC
1278 (DIC_Prag
: Node_Id
;
1279 Par_Typ
: Entity_Id
;
1280 Deriv_Typ
: Entity_Id
;
1281 Stmts
: in out List_Id
);
1282 -- Add a runtime check to verify the assertion expression of inherited
1283 -- pragma DIC_Prag. Par_Typ is parent type, which is also the owner of
1284 -- the DIC pragma. Deriv_Typ is the derived type inheriting the DIC
1285 -- pragma. All generated code is added to list Stmts.
1287 procedure Add_Inherited_Tagged_DIC
1288 (DIC_Prag
: Node_Id
;
1289 Par_Typ
: Entity_Id
;
1290 Deriv_Typ
: Entity_Id
;
1291 Stmts
: in out List_Id
);
1292 -- Add a runtime check to verify assertion expression DIC_Expr of
1293 -- inherited pragma DIC_Prag. This routine applies class-wide pre- and
1294 -- postcondition-like runtime semantics to the check. Par_Typ is the
1295 -- parent type whose DIC pragma is being inherited. Deriv_Typ is the
1296 -- derived type inheriting the DIC pragma. All generated code is added
1299 procedure Add_Own_DIC
1300 (DIC_Prag
: Node_Id
;
1301 DIC_Typ
: Entity_Id
;
1302 Stmts
: in out List_Id
);
1303 -- Add a runtime check to verify the assertion expression of pragma
1304 -- DIC_Prag. DIC_Typ is the owner of the DIC pragma. All generated code
1305 -- is added to list Stmts.
1311 procedure Add_DIC_Check
1312 (DIC_Prag
: Node_Id
;
1314 Stmts
: in out List_Id
)
1316 Loc
: constant Source_Ptr
:= Sloc
(DIC_Prag
);
1317 Nam
: constant Name_Id
:= Original_Aspect_Pragma_Name
(DIC_Prag
);
1320 -- The DIC pragma is ignored, nothing left to do
1322 if Is_Ignored
(DIC_Prag
) then
1325 -- Otherwise the DIC expression must be checked at run time.
1328 -- pragma Check (<Nam>, <DIC_Expr>);
1331 Append_New_To
(Stmts
,
1333 Pragma_Identifier
=>
1334 Make_Identifier
(Loc
, Name_Check
),
1336 Pragma_Argument_Associations
=> New_List
(
1337 Make_Pragma_Argument_Association
(Loc
,
1338 Expression
=> Make_Identifier
(Loc
, Nam
)),
1340 Make_Pragma_Argument_Association
(Loc
,
1341 Expression
=> DIC_Expr
))));
1345 -----------------------
1346 -- Add_Inherited_DIC --
1347 -----------------------
1349 procedure Add_Inherited_DIC
1350 (DIC_Prag
: Node_Id
;
1351 Par_Typ
: Entity_Id
;
1352 Deriv_Typ
: Entity_Id
;
1353 Stmts
: in out List_Id
)
1355 Deriv_Proc
: constant Entity_Id
:= DIC_Procedure
(Deriv_Typ
);
1356 Deriv_Obj
: constant Entity_Id
:= First_Entity
(Deriv_Proc
);
1357 Par_Proc
: constant Entity_Id
:= DIC_Procedure
(Par_Typ
);
1358 Par_Obj
: constant Entity_Id
:= First_Entity
(Par_Proc
);
1359 Loc
: constant Source_Ptr
:= Sloc
(DIC_Prag
);
1362 pragma Assert
(Present
(Deriv_Proc
) and then Present
(Par_Proc
));
1364 -- Verify the inherited DIC assertion expression by calling the DIC
1365 -- procedure of the parent type.
1368 -- <Par_Typ>DIC (Par_Typ (_object));
1370 Append_New_To
(Stmts
,
1371 Make_Procedure_Call_Statement
(Loc
,
1372 Name
=> New_Occurrence_Of
(Par_Proc
, Loc
),
1373 Parameter_Associations
=> New_List
(
1375 (Typ
=> Etype
(Par_Obj
),
1376 Expr
=> New_Occurrence_Of
(Deriv_Obj
, Loc
)))));
1377 end Add_Inherited_DIC
;
1379 ------------------------------
1380 -- Add_Inherited_Tagged_DIC --
1381 ------------------------------
1383 procedure Add_Inherited_Tagged_DIC
1384 (DIC_Prag
: Node_Id
;
1385 Par_Typ
: Entity_Id
;
1386 Deriv_Typ
: Entity_Id
;
1387 Stmts
: in out List_Id
)
1389 Deriv_Proc
: constant Entity_Id
:= DIC_Procedure
(Deriv_Typ
);
1390 DIC_Args
: constant List_Id
:=
1391 Pragma_Argument_Associations
(DIC_Prag
);
1392 DIC_Arg
: constant Node_Id
:= First
(DIC_Args
);
1393 DIC_Expr
: constant Node_Id
:= Expression_Copy
(DIC_Arg
);
1394 Par_Proc
: constant Entity_Id
:= DIC_Procedure
(Par_Typ
);
1399 -- The processing of an inherited DIC assertion expression starts off
1400 -- with a copy of the original parent expression where all references
1401 -- to the parent type have already been replaced with references to
1402 -- the _object formal parameter of the parent type's DIC procedure.
1404 pragma Assert
(Present
(DIC_Expr
));
1405 Expr
:= New_Copy_Tree
(DIC_Expr
);
1407 -- Perform the following substitutions:
1409 -- * Replace a reference to the _object parameter of the parent
1410 -- type's DIC procedure with a reference to the _object parameter
1411 -- of the derived types' DIC procedure.
1413 -- * Replace a reference to a discriminant of the parent type with
1414 -- a suitable value from the point of view of the derived type.
1416 -- * Replace a call to an overridden parent primitive with a call
1417 -- to the overriding derived type primitive.
1419 -- * Replace a call to an inherited parent primitive with a call to
1420 -- the internally-generated inherited derived type primitive.
1422 -- Note that primitives defined in the private part are automatically
1423 -- handled by the overriding/inheritance mechanism and do not require
1424 -- an extra replacement pass.
1426 pragma Assert
(Present
(Deriv_Proc
) and then Present
(Par_Proc
));
1431 Deriv_Typ
=> Deriv_Typ
,
1432 Par_Obj
=> First_Formal
(Par_Proc
),
1433 Deriv_Obj
=> First_Formal
(Deriv_Proc
));
1435 -- Once the DIC assertion expression is fully processed, add a check
1436 -- to the statements of the DIC procedure.
1439 (DIC_Prag
=> DIC_Prag
,
1442 end Add_Inherited_Tagged_DIC
;
1448 procedure Add_Own_DIC
1449 (DIC_Prag
: Node_Id
;
1450 DIC_Typ
: Entity_Id
;
1451 Stmts
: in out List_Id
)
1453 DIC_Args
: constant List_Id
:=
1454 Pragma_Argument_Associations
(DIC_Prag
);
1455 DIC_Arg
: constant Node_Id
:= First
(DIC_Args
);
1456 DIC_Asp
: constant Node_Id
:= Corresponding_Aspect
(DIC_Prag
);
1457 DIC_Expr
: constant Node_Id
:= Get_Pragma_Arg
(DIC_Arg
);
1458 DIC_Proc
: constant Entity_Id
:= DIC_Procedure
(DIC_Typ
);
1459 Obj_Id
: constant Entity_Id
:= First_Formal
(DIC_Proc
);
1461 procedure Preanalyze_Own_DIC_For_ASIS
;
1462 -- Preanalyze the original DIC expression of an aspect or a source
1465 ---------------------------------
1466 -- Preanalyze_Own_DIC_For_ASIS --
1467 ---------------------------------
1469 procedure Preanalyze_Own_DIC_For_ASIS
is
1470 Expr
: Node_Id
:= Empty
;
1473 -- The DIC pragma is a source construct, preanalyze the original
1474 -- expression of the pragma.
1476 if Comes_From_Source
(DIC_Prag
) then
1479 -- Otherwise preanalyze the expression of the corresponding aspect
1481 elsif Present
(DIC_Asp
) then
1482 Expr
:= Expression
(DIC_Asp
);
1485 -- The expression must be subjected to the same substitutions as
1486 -- the copy used in the generation of the runtime check.
1488 if Present
(Expr
) then
1489 Replace_Type_References
1494 Preanalyze_Assert_Expression
(Expr
, Any_Boolean
);
1496 end Preanalyze_Own_DIC_For_ASIS
;
1500 Typ_Decl
: constant Node_Id
:= Declaration_Node
(DIC_Typ
);
1504 -- Start of processing for Add_Own_DIC
1507 pragma Assert
(Present
(DIC_Expr
));
1508 Expr
:= New_Copy_Tree
(DIC_Expr
);
1510 -- Perform the following substitution:
1512 -- * Replace the current instance of DIC_Typ with a reference to
1513 -- the _object formal parameter of the DIC procedure.
1515 Replace_Type_References
1520 -- Preanalyze the DIC expression to detect errors and at the same
1521 -- time capture the visibility of the proper package part.
1523 Set_Parent
(Expr
, Typ_Decl
);
1524 Preanalyze_Assert_Expression
(Expr
, Any_Boolean
);
1526 -- Save a copy of the expression with all replacements and analysis
1527 -- already taken place in case a derived type inherits the pragma.
1528 -- The copy will be used as the foundation of the derived type's own
1529 -- version of the DIC assertion expression.
1531 if Is_Tagged_Type
(DIC_Typ
) then
1532 Set_Expression_Copy
(DIC_Arg
, New_Copy_Tree
(Expr
));
1535 -- If the pragma comes from an aspect specification, replace the
1536 -- saved expression because all type references must be substituted
1537 -- for the call to Preanalyze_Spec_Expression in Check_Aspect_At_xxx
1540 if Present
(DIC_Asp
) then
1541 Set_Entity
(Identifier
(DIC_Asp
), New_Copy_Tree
(Expr
));
1544 -- Preanalyze the original DIC expression for ASIS
1547 Preanalyze_Own_DIC_For_ASIS
;
1550 -- Once the DIC assertion expression is fully processed, add a check
1551 -- to the statements of the DIC procedure.
1554 (DIC_Prag
=> DIC_Prag
,
1561 Loc
: constant Source_Ptr
:= Sloc
(Typ
);
1563 Saved_GM
: constant Ghost_Mode_Type
:= Ghost_Mode
;
1564 -- Save the Ghost mode to restore on exit
1567 DIC_Typ
: Entity_Id
;
1568 Dummy_1
: Entity_Id
;
1569 Dummy_2
: Entity_Id
;
1570 Proc_Body
: Node_Id
;
1571 Proc_Body_Id
: Entity_Id
;
1572 Proc_Decl
: Node_Id
;
1573 Proc_Id
: Entity_Id
;
1574 Stmts
: List_Id
:= No_List
;
1576 Build_Body
: Boolean := False;
1577 -- Flag set when the type requires a DIC procedure body to be built
1579 Work_Typ
: Entity_Id
;
1582 -- Start of processing for Build_DIC_Procedure_Body
1585 Work_Typ
:= Base_Type
(Typ
);
1587 -- Do not process class-wide types as these are Itypes, but lack a first
1588 -- subtype (see below).
1590 if Is_Class_Wide_Type
(Work_Typ
) then
1593 -- Do not process the underlying full view of a private type. There is
1594 -- no way to get back to the partial view, plus the body will be built
1595 -- by the full view or the base type.
1597 elsif Is_Underlying_Full_View
(Work_Typ
) then
1600 -- Use the first subtype when dealing with various base types
1602 elsif Is_Itype
(Work_Typ
) then
1603 Work_Typ
:= First_Subtype
(Work_Typ
);
1605 -- The input denotes the corresponding record type of a protected or a
1606 -- task type. Work with the concurrent type because the corresponding
1607 -- record type may not be visible to clients of the type.
1609 elsif Ekind
(Work_Typ
) = E_Record_Type
1610 and then Is_Concurrent_Record_Type
(Work_Typ
)
1612 Work_Typ
:= Corresponding_Concurrent_Type
(Work_Typ
);
1615 -- The working type may be subject to pragma Ghost. Set the mode now to
1616 -- ensure that the DIC procedure is properly marked as Ghost.
1618 Set_Ghost_Mode
(Work_Typ
);
1620 -- The working type must be either define a DIC pragma of its own or
1621 -- inherit one from a parent type.
1623 pragma Assert
(Has_DIC
(Work_Typ
));
1625 -- Recover the type which defines the DIC pragma. This is either the
1626 -- working type itself or a parent type when the pragma is inherited.
1628 DIC_Typ
:= Find_DIC_Type
(Work_Typ
);
1629 pragma Assert
(Present
(DIC_Typ
));
1631 DIC_Prag
:= Get_Pragma
(DIC_Typ
, Pragma_Default_Initial_Condition
);
1632 pragma Assert
(Present
(DIC_Prag
));
1634 -- Nothing to do if pragma DIC appears without an argument or its sole
1635 -- argument is "null".
1637 if not Is_Verifiable_DIC_Pragma
(DIC_Prag
) then
1641 -- The working type may lack a DIC procedure declaration. This may be
1642 -- due to several reasons:
1644 -- * The working type's own DIC pragma does not contain a verifiable
1645 -- assertion expression. In this case there is no need to build a
1646 -- DIC procedure because there is nothing to check.
1648 -- * The working type derives from a parent type. In this case a DIC
1649 -- procedure should be built only when the inherited DIC pragma has
1650 -- a verifiable assertion expression.
1652 Proc_Id
:= DIC_Procedure
(Work_Typ
);
1654 -- Build a DIC procedure declaration when the working type derives from
1657 if No
(Proc_Id
) then
1658 Build_DIC_Procedure_Declaration
(Work_Typ
);
1659 Proc_Id
:= DIC_Procedure
(Work_Typ
);
1662 -- At this point there should be a DIC procedure declaration
1664 pragma Assert
(Present
(Proc_Id
));
1665 Proc_Decl
:= Unit_Declaration_Node
(Proc_Id
);
1667 -- Nothing to do if the DIC procedure already has a body
1669 if Present
(Corresponding_Body
(Proc_Decl
)) then
1673 -- Emulate the environment of the DIC procedure by installing its scope
1674 -- and formal parameters.
1676 Push_Scope
(Proc_Id
);
1677 Install_Formals
(Proc_Id
);
1679 -- The working type defines its own DIC pragma. Replace the current
1680 -- instance of the working type with the formal of the DIC procedure.
1681 -- Note that there is no need to consider inherited DIC pragmas from
1682 -- parent types because the working type's DIC pragma "hides" all
1683 -- inherited DIC pragmas.
1685 if Has_Own_DIC
(Work_Typ
) then
1686 pragma Assert
(DIC_Typ
= Work_Typ
);
1689 (DIC_Prag
=> DIC_Prag
,
1695 -- Otherwise the working type inherits a DIC pragma from a parent type.
1696 -- This processing is carried out when the type is frozen because the
1697 -- state of all parent discriminants is known at that point. Note that
1698 -- it is semantically sound to delay the creation of the DIC procedure
1699 -- body till the freeze point. If the type has a DIC pragma of its own,
1700 -- then the DIC procedure body would have already been constructed at
1701 -- the end of the visible declarations and all parent DIC pragmas are
1702 -- effectively "hidden" and irrelevant.
1704 elsif For_Freeze
then
1705 pragma Assert
(Has_Inherited_DIC
(Work_Typ
));
1706 pragma Assert
(DIC_Typ
/= Work_Typ
);
1708 -- The working type is tagged. The verification of the assertion
1709 -- expression is subject to the same semantics as class-wide pre-
1710 -- and postconditions.
1712 if Is_Tagged_Type
(Work_Typ
) then
1713 Add_Inherited_Tagged_DIC
1714 (DIC_Prag
=> DIC_Prag
,
1716 Deriv_Typ
=> Work_Typ
,
1719 -- Otherwise the working type is not tagged. Verify the assertion
1720 -- expression of the inherited DIC pragma by directly calling the
1721 -- DIC procedure of the parent type.
1725 (DIC_Prag
=> DIC_Prag
,
1727 Deriv_Typ
=> Work_Typ
,
1738 -- Produce an empty completing body in the following cases:
1739 -- * Assertions are disabled
1740 -- * The DIC Assertion_Policy is Ignore
1743 Stmts
:= New_List
(Make_Null_Statement
(Loc
));
1747 -- procedure <Work_Typ>DIC (_object : <Work_Typ>) is
1750 -- end <Work_Typ>DIC;
1753 Make_Subprogram_Body
(Loc
,
1755 Copy_Subprogram_Spec
(Parent
(Proc_Id
)),
1756 Declarations
=> Empty_List
,
1757 Handled_Statement_Sequence
=>
1758 Make_Handled_Sequence_Of_Statements
(Loc
,
1759 Statements
=> Stmts
));
1760 Proc_Body_Id
:= Defining_Entity
(Proc_Body
);
1762 -- Perform minor decoration in case the body is not analyzed
1764 Set_Ekind
(Proc_Body_Id
, E_Subprogram_Body
);
1765 Set_Etype
(Proc_Body_Id
, Standard_Void_Type
);
1766 Set_Scope
(Proc_Body_Id
, Current_Scope
);
1767 Set_SPARK_Pragma
(Proc_Body_Id
, SPARK_Pragma
(Proc_Id
));
1768 Set_SPARK_Pragma_Inherited
1769 (Proc_Body_Id
, SPARK_Pragma_Inherited
(Proc_Id
));
1771 -- Link both spec and body to avoid generating duplicates
1773 Set_Corresponding_Body
(Proc_Decl
, Proc_Body_Id
);
1774 Set_Corresponding_Spec
(Proc_Body
, Proc_Id
);
1776 -- The body should not be inserted into the tree when the context
1777 -- is ASIS or a generic unit because it is not part of the template.
1778 -- Note that the body must still be generated in order to resolve the
1779 -- DIC assertion expression.
1781 if ASIS_Mode
or Inside_A_Generic
then
1784 -- Semi-insert the body into the tree for GNATprove by setting its
1785 -- Parent field. This allows for proper upstream tree traversals.
1787 elsif GNATprove_Mode
then
1788 Set_Parent
(Proc_Body
, Parent
(Declaration_Node
(Work_Typ
)));
1790 -- Otherwise the body is part of the freezing actions of the working
1794 Append_Freeze_Action
(Work_Typ
, Proc_Body
);
1799 Restore_Ghost_Mode
(Saved_GM
);
1800 end Build_DIC_Procedure_Body
;
1802 -------------------------------------
1803 -- Build_DIC_Procedure_Declaration --
1804 -------------------------------------
1806 -- WARNING: This routine manages Ghost regions. Return statements must be
1807 -- replaced by gotos which jump to the end of the routine and restore the
1810 procedure Build_DIC_Procedure_Declaration
(Typ
: Entity_Id
) is
1811 Loc
: constant Source_Ptr
:= Sloc
(Typ
);
1813 Saved_GM
: constant Ghost_Mode_Type
:= Ghost_Mode
;
1814 -- Save the Ghost mode to restore on exit
1817 DIC_Typ
: Entity_Id
;
1818 Proc_Decl
: Node_Id
;
1819 Proc_Id
: Entity_Id
;
1822 CRec_Typ
: Entity_Id
;
1823 -- The corresponding record type of Full_Typ
1825 Full_Base
: Entity_Id
;
1826 -- The base type of Full_Typ
1828 Full_Typ
: Entity_Id
;
1829 -- The full view of working type
1832 -- The _object formal parameter of the DIC procedure
1834 Priv_Typ
: Entity_Id
;
1835 -- The partial view of working type
1837 Work_Typ
: Entity_Id
;
1841 Work_Typ
:= Base_Type
(Typ
);
1843 -- Do not process class-wide types as these are Itypes, but lack a first
1844 -- subtype (see below).
1846 if Is_Class_Wide_Type
(Work_Typ
) then
1849 -- Do not process the underlying full view of a private type. There is
1850 -- no way to get back to the partial view, plus the body will be built
1851 -- by the full view or the base type.
1853 elsif Is_Underlying_Full_View
(Work_Typ
) then
1856 -- Use the first subtype when dealing with various base types
1858 elsif Is_Itype
(Work_Typ
) then
1859 Work_Typ
:= First_Subtype
(Work_Typ
);
1861 -- The input denotes the corresponding record type of a protected or a
1862 -- task type. Work with the concurrent type because the corresponding
1863 -- record type may not be visible to clients of the type.
1865 elsif Ekind
(Work_Typ
) = E_Record_Type
1866 and then Is_Concurrent_Record_Type
(Work_Typ
)
1868 Work_Typ
:= Corresponding_Concurrent_Type
(Work_Typ
);
1871 -- The working type may be subject to pragma Ghost. Set the mode now to
1872 -- ensure that the DIC procedure is properly marked as Ghost.
1874 Set_Ghost_Mode
(Work_Typ
);
1876 -- The type must be either subject to a DIC pragma or inherit one from a
1879 pragma Assert
(Has_DIC
(Work_Typ
));
1881 -- Recover the type which defines the DIC pragma. This is either the
1882 -- working type itself or a parent type when the pragma is inherited.
1884 DIC_Typ
:= Find_DIC_Type
(Work_Typ
);
1885 pragma Assert
(Present
(DIC_Typ
));
1887 DIC_Prag
:= Get_Pragma
(DIC_Typ
, Pragma_Default_Initial_Condition
);
1888 pragma Assert
(Present
(DIC_Prag
));
1890 -- Nothing to do if pragma DIC appears without an argument or its sole
1891 -- argument is "null".
1893 if not Is_Verifiable_DIC_Pragma
(DIC_Prag
) then
1896 -- Nothing to do if the type already has a DIC procedure
1898 elsif Present
(DIC_Procedure
(Work_Typ
)) then
1903 Make_Defining_Identifier
(Loc
,
1905 New_External_Name
(Chars
(Work_Typ
), "Default_Initial_Condition"));
1907 -- Perform minor decoration in case the declaration is not analyzed
1909 Set_Ekind
(Proc_Id
, E_Procedure
);
1910 Set_Etype
(Proc_Id
, Standard_Void_Type
);
1911 Set_Is_DIC_Procedure
(Proc_Id
);
1912 Set_Scope
(Proc_Id
, Current_Scope
);
1913 Set_SPARK_Pragma
(Proc_Id
, SPARK_Mode_Pragma
);
1914 Set_SPARK_Pragma_Inherited
(Proc_Id
);
1916 Set_DIC_Procedure
(Work_Typ
, Proc_Id
);
1918 -- The DIC procedure requires debug info when the assertion expression
1919 -- is subject to Source Coverage Obligations.
1921 if Generate_SCO
then
1922 Set_Needs_Debug_Info
(Proc_Id
);
1925 -- Obtain all views of the input type
1927 Get_Views
(Work_Typ
, Priv_Typ
, Full_Typ
, Full_Base
, CRec_Typ
);
1929 -- Associate the DIC procedure and various relevant flags with all views
1931 Propagate_DIC_Attributes
(Priv_Typ
, From_Typ
=> Work_Typ
);
1932 Propagate_DIC_Attributes
(Full_Typ
, From_Typ
=> Work_Typ
);
1933 Propagate_DIC_Attributes
(Full_Base
, From_Typ
=> Work_Typ
);
1934 Propagate_DIC_Attributes
(CRec_Typ
, From_Typ
=> Work_Typ
);
1936 -- The declaration of the DIC procedure must be inserted after the
1937 -- declaration of the partial view as this allows for proper external
1940 if Present
(Priv_Typ
) then
1941 Typ_Decl
:= Declaration_Node
(Priv_Typ
);
1943 -- Derived types with the full view as parent do not have a partial
1944 -- view. Insert the DIC procedure after the derived type.
1947 Typ_Decl
:= Declaration_Node
(Full_Typ
);
1950 -- The type should have a declarative node
1952 pragma Assert
(Present
(Typ_Decl
));
1954 -- Create the formal parameter which emulates the variable-like behavior
1955 -- of the type's current instance.
1957 Obj_Id
:= Make_Defining_Identifier
(Loc
, Chars
=> Name_uObject
);
1959 -- Perform minor decoration in case the declaration is not analyzed
1961 Set_Ekind
(Obj_Id
, E_In_Parameter
);
1962 Set_Etype
(Obj_Id
, Work_Typ
);
1963 Set_Scope
(Obj_Id
, Proc_Id
);
1965 Set_First_Entity
(Proc_Id
, Obj_Id
);
1968 -- procedure <Work_Typ>DIC (_object : <Work_Typ>);
1971 Make_Subprogram_Declaration
(Loc
,
1973 Make_Procedure_Specification
(Loc
,
1974 Defining_Unit_Name
=> Proc_Id
,
1975 Parameter_Specifications
=> New_List
(
1976 Make_Parameter_Specification
(Loc
,
1977 Defining_Identifier
=> Obj_Id
,
1979 New_Occurrence_Of
(Work_Typ
, Loc
)))));
1981 -- The declaration should not be inserted into the tree when the context
1982 -- is ASIS or a generic unit because it is not part of the template.
1984 if ASIS_Mode
or Inside_A_Generic
then
1987 -- Semi-insert the declaration into the tree for GNATprove by setting
1988 -- its Parent field. This allows for proper upstream tree traversals.
1990 elsif GNATprove_Mode
then
1991 Set_Parent
(Proc_Decl
, Parent
(Typ_Decl
));
1993 -- Otherwise insert the declaration
1996 Insert_After_And_Analyze
(Typ_Decl
, Proc_Decl
);
2000 Restore_Ghost_Mode
(Saved_GM
);
2001 end Build_DIC_Procedure_Declaration
;
2003 ------------------------------------
2004 -- Build_Invariant_Procedure_Body --
2005 ------------------------------------
2007 -- WARNING: This routine manages Ghost regions. Return statements must be
2008 -- replaced by gotos which jump to the end of the routine and restore the
2011 procedure Build_Invariant_Procedure_Body
2013 Partial_Invariant
: Boolean := False)
2015 Loc
: constant Source_Ptr
:= Sloc
(Typ
);
2017 Pragmas_Seen
: Elist_Id
:= No_Elist
;
2018 -- This list contains all invariant pragmas processed so far. The list
2019 -- is used to avoid generating redundant invariant checks.
2021 Produced_Check
: Boolean := False;
2022 -- This flag tracks whether the type has produced at least one invariant
2023 -- check. The flag is used as a sanity check at the end of the routine.
2025 -- NOTE: most of the routines in Build_Invariant_Procedure_Body are
2026 -- intentionally unnested to avoid deep indentation of code.
2028 -- NOTE: all Add_xxx_Invariants routines are reactive. In other words
2029 -- they emit checks, loops (for arrays) and case statements (for record
2030 -- variant parts) only when there are invariants to verify. This keeps
2031 -- the body of the invariant procedure free of useless code.
2033 procedure Add_Array_Component_Invariants
2036 Checks
: in out List_Id
);
2037 -- Generate an invariant check for each component of array type T.
2038 -- Obj_Id denotes the entity of the _object formal parameter of the
2039 -- invariant procedure. All created checks are added to list Checks.
2041 procedure Add_Inherited_Invariants
2043 Priv_Typ
: Entity_Id
;
2044 Full_Typ
: Entity_Id
;
2046 Checks
: in out List_Id
);
2047 -- Generate an invariant check for each inherited class-wide invariant
2048 -- coming from all parent types of type T. Priv_Typ and Full_Typ denote
2049 -- the partial and full view of the parent type. Obj_Id denotes the
2050 -- entity of the _object formal parameter of the invariant procedure.
2051 -- All created checks are added to list Checks.
2053 procedure Add_Interface_Invariants
2056 Checks
: in out List_Id
);
2057 -- Generate an invariant check for each inherited class-wide invariant
2058 -- coming from all interfaces implemented by type T. Obj_Id denotes the
2059 -- entity of the _object formal parameter of the invariant procedure.
2060 -- All created checks are added to list Checks.
2062 procedure Add_Invariant_Check
2065 Checks
: in out List_Id
;
2066 Inherited
: Boolean := False);
2067 -- Subsidiary to all Add_xxx_Invariant routines. Add a runtime check to
2068 -- verify assertion expression Expr of pragma Prag. All generated code
2069 -- is added to list Checks. Flag Inherited should be set when the pragma
2070 -- is inherited from a parent or interface type.
2072 procedure Add_Own_Invariants
2075 Checks
: in out List_Id
;
2076 Priv_Item
: Node_Id
:= Empty
);
2077 -- Generate an invariant check for each invariant found for type T.
2078 -- Obj_Id denotes the entity of the _object formal parameter of the
2079 -- invariant procedure. All created checks are added to list Checks.
2080 -- Priv_Item denotes the first rep item of the private type.
2082 procedure Add_Parent_Invariants
2085 Checks
: in out List_Id
);
2086 -- Generate an invariant check for each inherited class-wide invariant
2087 -- coming from all parent types of type T. Obj_Id denotes the entity of
2088 -- the _object formal parameter of the invariant procedure. All created
2089 -- checks are added to list Checks.
2091 procedure Add_Record_Component_Invariants
2094 Checks
: in out List_Id
);
2095 -- Generate an invariant check for each component of record type T.
2096 -- Obj_Id denotes the entity of the _object formal parameter of the
2097 -- invariant procedure. All created checks are added to list Checks.
2099 ------------------------------------
2100 -- Add_Array_Component_Invariants --
2101 ------------------------------------
2103 procedure Add_Array_Component_Invariants
2106 Checks
: in out List_Id
)
2108 Comp_Typ
: constant Entity_Id
:= Component_Type
(T
);
2109 Dims
: constant Pos
:= Number_Dimensions
(T
);
2111 procedure Process_Array_Component
2113 Comp_Checks
: in out List_Id
);
2114 -- Generate an invariant check for an array component identified by
2115 -- the indices in list Indices. All created checks are added to list
2118 procedure Process_One_Dimension
2121 Dim_Checks
: in out List_Id
);
2122 -- Generate a loop over the Nth dimension Dim of an array type. List
2123 -- Indices contains all array indices for the dimension. All created
2124 -- checks are added to list Dim_Checks.
2126 -----------------------------
2127 -- Process_Array_Component --
2128 -----------------------------
2130 procedure Process_Array_Component
2132 Comp_Checks
: in out List_Id
)
2134 Proc_Id
: Entity_Id
;
2137 if Has_Invariants
(Comp_Typ
) then
2139 -- In GNATprove mode, the component invariants are checked by
2140 -- other means. They should not be added to the array type
2141 -- invariant procedure, so that the procedure can be used to
2142 -- check the array type invariants if any.
2144 if GNATprove_Mode
then
2148 Proc_Id
:= Invariant_Procedure
(Base_Type
(Comp_Typ
));
2150 -- The component type should have an invariant procedure
2151 -- if it has invariants of its own or inherits class-wide
2152 -- invariants from parent or interface types.
2154 pragma Assert
(Present
(Proc_Id
));
2157 -- <Comp_Typ>Invariant (_object (<Indices>));
2159 -- Note that the invariant procedure may have a null body if
2160 -- assertions are disabled or Assertion_Policy Ignore is in
2163 if not Has_Null_Body
(Proc_Id
) then
2164 Append_New_To
(Comp_Checks
,
2165 Make_Procedure_Call_Statement
(Loc
,
2167 New_Occurrence_Of
(Proc_Id
, Loc
),
2168 Parameter_Associations
=> New_List
(
2169 Make_Indexed_Component
(Loc
,
2170 Prefix
=> New_Occurrence_Of
(Obj_Id
, Loc
),
2171 Expressions
=> New_Copy_List
(Indices
)))));
2175 Produced_Check
:= True;
2177 end Process_Array_Component
;
2179 ---------------------------
2180 -- Process_One_Dimension --
2181 ---------------------------
2183 procedure Process_One_Dimension
2186 Dim_Checks
: in out List_Id
)
2188 Comp_Checks
: List_Id
:= No_List
;
2192 -- Generate the invariant checks for the array component after all
2193 -- dimensions have produced their respective loops.
2196 Process_Array_Component
2197 (Indices
=> Indices
,
2198 Comp_Checks
=> Dim_Checks
);
2200 -- Otherwise create a loop for the current dimension
2203 -- Create a new loop variable for each dimension
2206 Make_Defining_Identifier
(Loc
,
2207 Chars
=> New_External_Name
('I', Dim
));
2208 Append_To
(Indices
, New_Occurrence_Of
(Index
, Loc
));
2210 Process_One_Dimension
2213 Dim_Checks
=> Comp_Checks
);
2216 -- for I<Dim> in _object'Range (<Dim>) loop
2220 -- Note that the invariant procedure may have a null body if
2221 -- assertions are disabled or Assertion_Policy Ignore is in
2224 if Present
(Comp_Checks
) then
2225 Append_New_To
(Dim_Checks
,
2226 Make_Implicit_Loop_Statement
(T
,
2227 Identifier
=> Empty
,
2229 Make_Iteration_Scheme
(Loc
,
2230 Loop_Parameter_Specification
=>
2231 Make_Loop_Parameter_Specification
(Loc
,
2232 Defining_Identifier
=> Index
,
2233 Discrete_Subtype_Definition
=>
2234 Make_Attribute_Reference
(Loc
,
2236 New_Occurrence_Of
(Obj_Id
, Loc
),
2237 Attribute_Name
=> Name_Range
,
2238 Expressions
=> New_List
(
2239 Make_Integer_Literal
(Loc
, Dim
))))),
2240 Statements
=> Comp_Checks
));
2243 end Process_One_Dimension
;
2245 -- Start of processing for Add_Array_Component_Invariants
2248 Process_One_Dimension
2250 Indices
=> New_List
,
2251 Dim_Checks
=> Checks
);
2252 end Add_Array_Component_Invariants
;
2254 ------------------------------
2255 -- Add_Inherited_Invariants --
2256 ------------------------------
2258 procedure Add_Inherited_Invariants
2260 Priv_Typ
: Entity_Id
;
2261 Full_Typ
: Entity_Id
;
2263 Checks
: in out List_Id
)
2265 Deriv_Typ
: Entity_Id
;
2268 Prag_Expr
: Node_Id
;
2269 Prag_Expr_Arg
: Node_Id
;
2271 Prag_Typ_Arg
: Node_Id
;
2273 Par_Proc
: Entity_Id
;
2274 -- The "partial" invariant procedure of Par_Typ
2276 Par_Typ
: Entity_Id
;
2277 -- The suitable view of the parent type used in the substitution of
2281 if not Present
(Priv_Typ
) and then not Present
(Full_Typ
) then
2285 -- When the type inheriting the class-wide invariant is a concurrent
2286 -- type, use the corresponding record type because it contains all
2287 -- primitive operations of the concurrent type and allows for proper
2290 if Is_Concurrent_Type
(T
) then
2291 Deriv_Typ
:= Corresponding_Record_Type
(T
);
2296 pragma Assert
(Present
(Deriv_Typ
));
2298 -- Determine which rep item chain to use. Precedence is given to that
2299 -- of the parent type's partial view since it usually carries all the
2300 -- class-wide invariants.
2302 if Present
(Priv_Typ
) then
2303 Prag
:= First_Rep_Item
(Priv_Typ
);
2305 Prag
:= First_Rep_Item
(Full_Typ
);
2308 while Present
(Prag
) loop
2309 if Nkind
(Prag
) = N_Pragma
2310 and then Pragma_Name
(Prag
) = Name_Invariant
2312 -- Nothing to do if the pragma was already processed
2314 if Contains
(Pragmas_Seen
, Prag
) then
2317 -- Nothing to do when the caller requests the processing of all
2318 -- inherited class-wide invariants, but the pragma does not
2319 -- fall in this category.
2321 elsif not Class_Present
(Prag
) then
2325 -- Extract the arguments of the invariant pragma
2327 Prag_Typ_Arg
:= First
(Pragma_Argument_Associations
(Prag
));
2328 Prag_Expr_Arg
:= Next
(Prag_Typ_Arg
);
2329 Prag_Expr
:= Expression_Copy
(Prag_Expr_Arg
);
2330 Prag_Typ
:= Get_Pragma_Arg
(Prag_Typ_Arg
);
2332 -- The pragma applies to the partial view of the parent type
2334 if Present
(Priv_Typ
)
2335 and then Entity
(Prag_Typ
) = Priv_Typ
2337 Par_Typ
:= Priv_Typ
;
2339 -- The pragma applies to the full view of the parent type
2341 elsif Present
(Full_Typ
)
2342 and then Entity
(Prag_Typ
) = Full_Typ
2344 Par_Typ
:= Full_Typ
;
2346 -- Otherwise the pragma does not belong to the parent type and
2347 -- should not be considered.
2353 -- Perform the following substitutions:
2355 -- * Replace a reference to the _object parameter of the
2356 -- parent type's partial invariant procedure with a
2357 -- reference to the _object parameter of the derived
2358 -- type's full invariant procedure.
2360 -- * Replace a reference to a discriminant of the parent type
2361 -- with a suitable value from the point of view of the
2364 -- * Replace a call to an overridden parent primitive with a
2365 -- call to the overriding derived type primitive.
2367 -- * Replace a call to an inherited parent primitive with a
2368 -- call to the internally-generated inherited derived type
2371 Expr
:= New_Copy_Tree
(Prag_Expr
);
2373 -- The parent type must have a "partial" invariant procedure
2374 -- because class-wide invariants are captured exclusively by
2377 Par_Proc
:= Partial_Invariant_Procedure
(Par_Typ
);
2378 pragma Assert
(Present
(Par_Proc
));
2383 Deriv_Typ
=> Deriv_Typ
,
2384 Par_Obj
=> First_Formal
(Par_Proc
),
2385 Deriv_Obj
=> Obj_Id
);
2387 Add_Invariant_Check
(Prag
, Expr
, Checks
, Inherited
=> True);
2390 Next_Rep_Item
(Prag
);
2392 end Add_Inherited_Invariants
;
2394 ------------------------------
2395 -- Add_Interface_Invariants --
2396 ------------------------------
2398 procedure Add_Interface_Invariants
2401 Checks
: in out List_Id
)
2403 Iface_Elmt
: Elmt_Id
;
2407 -- Generate an invariant check for each class-wide invariant coming
2408 -- from all interfaces implemented by type T.
2410 if Is_Tagged_Type
(T
) then
2411 Collect_Interfaces
(T
, Ifaces
);
2413 -- Process the class-wide invariants of all implemented interfaces
2415 Iface_Elmt
:= First_Elmt
(Ifaces
);
2416 while Present
(Iface_Elmt
) loop
2418 -- The Full_Typ parameter is intentionally left Empty because
2419 -- interfaces are treated as the partial view of a private type
2420 -- in order to achieve uniformity with the general case.
2422 Add_Inherited_Invariants
2424 Priv_Typ
=> Node
(Iface_Elmt
),
2429 Next_Elmt
(Iface_Elmt
);
2432 end Add_Interface_Invariants
;
2434 -------------------------
2435 -- Add_Invariant_Check --
2436 -------------------------
2438 procedure Add_Invariant_Check
2441 Checks
: in out List_Id
;
2442 Inherited
: Boolean := False)
2444 Args
: constant List_Id
:= Pragma_Argument_Associations
(Prag
);
2445 Nam
: constant Name_Id
:= Original_Aspect_Pragma_Name
(Prag
);
2446 Ploc
: constant Source_Ptr
:= Sloc
(Prag
);
2447 Str_Arg
: constant Node_Id
:= Next
(Next
(First
(Args
)));
2453 -- The invariant is ignored, nothing left to do
2455 if Is_Ignored
(Prag
) then
2458 -- Otherwise the invariant is checked. Build a pragma Check to verify
2459 -- the expression at run time.
2463 Make_Pragma_Argument_Association
(Ploc
,
2464 Expression
=> Make_Identifier
(Ploc
, Nam
)),
2465 Make_Pragma_Argument_Association
(Ploc
,
2466 Expression
=> Expr
));
2468 -- Handle the String argument (if any)
2470 if Present
(Str_Arg
) then
2471 Str
:= Strval
(Get_Pragma_Arg
(Str_Arg
));
2473 -- When inheriting an invariant, modify the message from
2474 -- "failed invariant" to "failed inherited invariant".
2477 String_To_Name_Buffer
(Str
);
2479 if Name_Buffer
(1 .. 16) = "failed invariant" then
2480 Insert_Str_In_Name_Buffer
("inherited ", 8);
2481 Str
:= String_From_Name_Buffer
;
2486 Make_Pragma_Argument_Association
(Ploc
,
2487 Expression
=> Make_String_Literal
(Ploc
, Str
)));
2491 -- pragma Check (<Nam>, <Expr>, <Str>);
2493 Append_New_To
(Checks
,
2495 Chars
=> Name_Check
,
2496 Pragma_Argument_Associations
=> Assoc
));
2499 -- Output an info message when inheriting an invariant and the
2500 -- listing option is enabled.
2502 if Inherited
and Opt
.List_Inherited_Aspects
then
2503 Error_Msg_Sloc
:= Sloc
(Prag
);
2505 ("info: & inherits `Invariant''Class` aspect from #?L?", Typ
);
2508 -- Add the pragma to the list of processed pragmas
2510 Append_New_Elmt
(Prag
, Pragmas_Seen
);
2511 Produced_Check
:= True;
2512 end Add_Invariant_Check
;
2514 ---------------------------
2515 -- Add_Parent_Invariants --
2516 ---------------------------
2518 procedure Add_Parent_Invariants
2521 Checks
: in out List_Id
)
2523 Dummy_1
: Entity_Id
;
2524 Dummy_2
: Entity_Id
;
2526 Curr_Typ
: Entity_Id
;
2527 -- The entity of the current type being examined
2529 Full_Typ
: Entity_Id
;
2530 -- The full view of Par_Typ
2532 Par_Typ
: Entity_Id
;
2533 -- The entity of the parent type
2535 Priv_Typ
: Entity_Id
;
2536 -- The partial view of Par_Typ
2539 -- Do not process array types because they cannot have true parent
2540 -- types. This also prevents the generation of a duplicate invariant
2541 -- check when the input type is an array base type because its Etype
2542 -- denotes the first subtype, both of which share the same component
2545 if Is_Array_Type
(T
) then
2549 -- Climb the parent type chain
2553 -- Do not consider subtypes as they inherit the invariants
2554 -- from their base types.
2556 Par_Typ
:= Base_Type
(Etype
(Curr_Typ
));
2558 -- Stop the climb once the root of the parent chain is
2561 exit when Curr_Typ
= Par_Typ
;
2563 -- Process the class-wide invariants of the parent type
2565 Get_Views
(Par_Typ
, Priv_Typ
, Full_Typ
, Dummy_1
, Dummy_2
);
2567 -- Process the elements of an array type
2569 if Is_Array_Type
(Full_Typ
) then
2570 Add_Array_Component_Invariants
(Full_Typ
, Obj_Id
, Checks
);
2572 -- Process the components of a record type
2574 elsif Ekind
(Full_Typ
) = E_Record_Type
then
2575 Add_Record_Component_Invariants
(Full_Typ
, Obj_Id
, Checks
);
2578 Add_Inherited_Invariants
2580 Priv_Typ
=> Priv_Typ
,
2581 Full_Typ
=> Full_Typ
,
2585 Curr_Typ
:= Par_Typ
;
2587 end Add_Parent_Invariants
;
2589 ------------------------
2590 -- Add_Own_Invariants --
2591 ------------------------
2593 procedure Add_Own_Invariants
2596 Checks
: in out List_Id
;
2597 Priv_Item
: Node_Id
:= Empty
)
2599 ASIS_Expr
: Node_Id
;
2603 Prag_Expr
: Node_Id
;
2604 Prag_Expr_Arg
: Node_Id
;
2606 Prag_Typ_Arg
: Node_Id
;
2609 if not Present
(T
) then
2613 Prag
:= First_Rep_Item
(T
);
2614 while Present
(Prag
) loop
2615 if Nkind
(Prag
) = N_Pragma
2616 and then Pragma_Name
(Prag
) = Name_Invariant
2618 -- Stop the traversal of the rep item chain once a specific
2619 -- item is encountered.
2621 if Present
(Priv_Item
) and then Prag
= Priv_Item
then
2625 -- Nothing to do if the pragma was already processed
2627 if Contains
(Pragmas_Seen
, Prag
) then
2631 -- Extract the arguments of the invariant pragma
2633 Prag_Typ_Arg
:= First
(Pragma_Argument_Associations
(Prag
));
2634 Prag_Expr_Arg
:= Next
(Prag_Typ_Arg
);
2635 Prag_Expr
:= Get_Pragma_Arg
(Prag_Expr_Arg
);
2636 Prag_Typ
:= Get_Pragma_Arg
(Prag_Typ_Arg
);
2637 Prag_Asp
:= Corresponding_Aspect
(Prag
);
2639 -- Verify the pragma belongs to T, otherwise the pragma applies
2640 -- to a parent type in which case it will be processed later by
2641 -- Add_Parent_Invariants or Add_Interface_Invariants.
2643 if Entity
(Prag_Typ
) /= T
then
2647 Expr
:= New_Copy_Tree
(Prag_Expr
);
2649 -- Substitute all references to type T with references to the
2650 -- _object formal parameter.
2652 Replace_Type_References
(Expr
, T
, Obj_Id
);
2654 -- Preanalyze the invariant expression to detect errors and at
2655 -- the same time capture the visibility of the proper package
2658 Set_Parent
(Expr
, Parent
(Prag_Expr
));
2659 Preanalyze_Assert_Expression
(Expr
, Any_Boolean
);
2661 -- Save a copy of the expression when T is tagged to detect
2662 -- errors and capture the visibility of the proper package part
2663 -- for the generation of inherited type invariants.
2665 if Is_Tagged_Type
(T
) then
2666 Set_Expression_Copy
(Prag_Expr_Arg
, New_Copy_Tree
(Expr
));
2669 -- If the pragma comes from an aspect specification, replace
2670 -- the saved expression because all type references must be
2671 -- substituted for the call to Preanalyze_Spec_Expression in
2672 -- Check_Aspect_At_xxx routines.
2674 if Present
(Prag_Asp
) then
2675 Set_Entity
(Identifier
(Prag_Asp
), New_Copy_Tree
(Expr
));
2678 -- Analyze the original invariant expression for ASIS
2683 if Comes_From_Source
(Prag
) then
2684 ASIS_Expr
:= Prag_Expr
;
2685 elsif Present
(Prag_Asp
) then
2686 ASIS_Expr
:= Expression
(Prag_Asp
);
2689 if Present
(ASIS_Expr
) then
2690 Replace_Type_References
(ASIS_Expr
, T
, Obj_Id
);
2691 Preanalyze_Assert_Expression
(ASIS_Expr
, Any_Boolean
);
2695 Add_Invariant_Check
(Prag
, Expr
, Checks
);
2698 Next_Rep_Item
(Prag
);
2700 end Add_Own_Invariants
;
2702 -------------------------------------
2703 -- Add_Record_Component_Invariants --
2704 -------------------------------------
2706 procedure Add_Record_Component_Invariants
2709 Checks
: in out List_Id
)
2711 procedure Process_Component_List
2712 (Comp_List
: Node_Id
;
2713 CL_Checks
: in out List_Id
);
2714 -- Generate invariant checks for all record components found in
2715 -- component list Comp_List, including variant parts. All created
2716 -- checks are added to list CL_Checks.
2718 procedure Process_Record_Component
2719 (Comp_Id
: Entity_Id
;
2720 Comp_Checks
: in out List_Id
);
2721 -- Generate an invariant check for a record component identified by
2722 -- Comp_Id. All created checks are added to list Comp_Checks.
2724 ----------------------------
2725 -- Process_Component_List --
2726 ----------------------------
2728 procedure Process_Component_List
2729 (Comp_List
: Node_Id
;
2730 CL_Checks
: in out List_Id
)
2734 Var_Alts
: List_Id
:= No_List
;
2735 Var_Checks
: List_Id
:= No_List
;
2736 Var_Stmts
: List_Id
;
2738 Produced_Variant_Check
: Boolean := False;
2739 -- This flag tracks whether the component has produced at least
2740 -- one invariant check.
2743 -- Traverse the component items
2745 Comp
:= First
(Component_Items
(Comp_List
));
2746 while Present
(Comp
) loop
2747 if Nkind
(Comp
) = N_Component_Declaration
then
2749 -- Generate the component invariant check
2751 Process_Record_Component
2752 (Comp_Id
=> Defining_Entity
(Comp
),
2753 Comp_Checks
=> CL_Checks
);
2759 -- Traverse the variant part
2761 if Present
(Variant_Part
(Comp_List
)) then
2762 Var
:= First
(Variants
(Variant_Part
(Comp_List
)));
2763 while Present
(Var
) loop
2764 Var_Checks
:= No_List
;
2766 -- Generate invariant checks for all components and variant
2767 -- parts that qualify.
2769 Process_Component_List
2770 (Comp_List
=> Component_List
(Var
),
2771 CL_Checks
=> Var_Checks
);
2773 -- The components of the current variant produced at least
2774 -- one invariant check.
2776 if Present
(Var_Checks
) then
2777 Var_Stmts
:= Var_Checks
;
2778 Produced_Variant_Check
:= True;
2780 -- Otherwise there are either no components with invariants,
2781 -- assertions are disabled, or Assertion_Policy Ignore is in
2785 Var_Stmts
:= New_List
(Make_Null_Statement
(Loc
));
2788 Append_New_To
(Var_Alts
,
2789 Make_Case_Statement_Alternative
(Loc
,
2791 New_Copy_List
(Discrete_Choices
(Var
)),
2792 Statements
=> Var_Stmts
));
2797 -- Create a case statement which verifies the invariant checks
2798 -- of a particular component list depending on the discriminant
2799 -- values only when there is at least one real invariant check.
2801 if Produced_Variant_Check
then
2802 Append_New_To
(CL_Checks
,
2803 Make_Case_Statement
(Loc
,
2805 Make_Selected_Component
(Loc
,
2806 Prefix
=> New_Occurrence_Of
(Obj_Id
, Loc
),
2809 (Entity
(Name
(Variant_Part
(Comp_List
))), Loc
)),
2810 Alternatives
=> Var_Alts
));
2813 end Process_Component_List
;
2815 ------------------------------
2816 -- Process_Record_Component --
2817 ------------------------------
2819 procedure Process_Record_Component
2820 (Comp_Id
: Entity_Id
;
2821 Comp_Checks
: in out List_Id
)
2823 Comp_Typ
: constant Entity_Id
:= Etype
(Comp_Id
);
2824 Proc_Id
: Entity_Id
;
2826 Produced_Component_Check
: Boolean := False;
2827 -- This flag tracks whether the component has produced at least
2828 -- one invariant check.
2831 -- Nothing to do for internal component _parent. Note that it is
2832 -- not desirable to check whether the component comes from source
2833 -- because protected type components are relocated to an internal
2834 -- corresponding record, but still need processing.
2836 if Chars
(Comp_Id
) = Name_uParent
then
2840 -- Verify the invariant of the component. Note that an access
2841 -- type may have an invariant when it acts as the full view of a
2842 -- private type and the invariant appears on the partial view. In
2843 -- this case verify the access value itself.
2845 if Has_Invariants
(Comp_Typ
) then
2847 -- In GNATprove mode, the component invariants are checked by
2848 -- other means. They should not be added to the record type
2849 -- invariant procedure, so that the procedure can be used to
2850 -- check the record type invariants if any.
2852 if GNATprove_Mode
then
2856 Proc_Id
:= Invariant_Procedure
(Base_Type
(Comp_Typ
));
2858 -- The component type should have an invariant procedure
2859 -- if it has invariants of its own or inherits class-wide
2860 -- invariants from parent or interface types.
2862 pragma Assert
(Present
(Proc_Id
));
2865 -- <Comp_Typ>Invariant (T (_object).<Comp_Id>);
2867 -- Note that the invariant procedure may have a null body if
2868 -- assertions are disabled or Assertion_Policy Ignore is in
2871 if not Has_Null_Body
(Proc_Id
) then
2872 Append_New_To
(Comp_Checks
,
2873 Make_Procedure_Call_Statement
(Loc
,
2875 New_Occurrence_Of
(Proc_Id
, Loc
),
2876 Parameter_Associations
=> New_List
(
2877 Make_Selected_Component
(Loc
,
2879 Unchecked_Convert_To
2880 (T
, New_Occurrence_Of
(Obj_Id
, Loc
)),
2882 New_Occurrence_Of
(Comp_Id
, Loc
)))));
2886 Produced_Check
:= True;
2887 Produced_Component_Check
:= True;
2890 if Produced_Component_Check
and then Has_Unchecked_Union
(T
) then
2892 ("invariants cannot be checked on components of "
2893 & "unchecked_union type &?", Comp_Id
, T
);
2895 end Process_Record_Component
;
2902 -- Start of processing for Add_Record_Component_Invariants
2905 -- An untagged derived type inherits the components of its parent
2906 -- type. In order to avoid creating redundant invariant checks, do
2907 -- not process the components now. Instead wait until the ultimate
2908 -- parent of the untagged derivation chain is reached.
2910 if not Is_Untagged_Derivation
(T
) then
2911 Def
:= Type_Definition
(Parent
(T
));
2913 if Nkind
(Def
) = N_Derived_Type_Definition
then
2914 Def
:= Record_Extension_Part
(Def
);
2917 pragma Assert
(Nkind
(Def
) = N_Record_Definition
);
2918 Comps
:= Component_List
(Def
);
2920 if Present
(Comps
) then
2921 Process_Component_List
2922 (Comp_List
=> Comps
,
2923 CL_Checks
=> Checks
);
2926 end Add_Record_Component_Invariants
;
2930 Saved_GM
: constant Ghost_Mode_Type
:= Ghost_Mode
;
2931 -- Save the Ghost mode to restore on exit
2934 Priv_Item
: Node_Id
;
2935 Proc_Body
: Node_Id
;
2936 Proc_Body_Id
: Entity_Id
;
2937 Proc_Decl
: Node_Id
;
2938 Proc_Id
: Entity_Id
;
2939 Stmts
: List_Id
:= No_List
;
2941 CRec_Typ
: Entity_Id
:= Empty
;
2942 -- The corresponding record type of Full_Typ
2944 Full_Proc
: Entity_Id
:= Empty
;
2945 -- The entity of the "full" invariant procedure
2947 Full_Typ
: Entity_Id
:= Empty
;
2948 -- The full view of the working type
2950 Obj_Id
: Entity_Id
:= Empty
;
2951 -- The _object formal parameter of the invariant procedure
2953 Part_Proc
: Entity_Id
:= Empty
;
2954 -- The entity of the "partial" invariant procedure
2956 Priv_Typ
: Entity_Id
:= Empty
;
2957 -- The partial view of the working type
2959 Work_Typ
: Entity_Id
:= Empty
;
2962 -- Start of processing for Build_Invariant_Procedure_Body
2967 -- The input type denotes the implementation base type of a constrained
2968 -- array type. Work with the first subtype as all invariant pragmas are
2969 -- on its rep item chain.
2971 if Ekind
(Work_Typ
) = E_Array_Type
and then Is_Itype
(Work_Typ
) then
2972 Work_Typ
:= First_Subtype
(Work_Typ
);
2974 -- The input type denotes the corresponding record type of a protected
2975 -- or task type. Work with the concurrent type because the corresponding
2976 -- record type may not be visible to clients of the type.
2978 elsif Ekind
(Work_Typ
) = E_Record_Type
2979 and then Is_Concurrent_Record_Type
(Work_Typ
)
2981 Work_Typ
:= Corresponding_Concurrent_Type
(Work_Typ
);
2984 -- The working type may be subject to pragma Ghost. Set the mode now to
2985 -- ensure that the invariant procedure is properly marked as Ghost.
2987 Set_Ghost_Mode
(Work_Typ
);
2989 -- The type must either have invariants of its own, inherit class-wide
2990 -- invariants from parent types or interfaces, or be an array or record
2991 -- type whose components have invariants.
2993 pragma Assert
(Has_Invariants
(Work_Typ
));
2995 -- Interfaces are treated as the partial view of a private type in order
2996 -- to achieve uniformity with the general case.
2998 if Is_Interface
(Work_Typ
) then
2999 Priv_Typ
:= Work_Typ
;
3001 -- Otherwise obtain both views of the type
3004 Get_Views
(Work_Typ
, Priv_Typ
, Full_Typ
, Dummy
, CRec_Typ
);
3007 -- The caller requests a body for the partial invariant procedure
3009 if Partial_Invariant
then
3010 Full_Proc
:= Invariant_Procedure
(Work_Typ
);
3011 Proc_Id
:= Partial_Invariant_Procedure
(Work_Typ
);
3013 -- The "full" invariant procedure body was already created
3015 if Present
(Full_Proc
)
3017 (Corresponding_Body
(Unit_Declaration_Node
(Full_Proc
)))
3019 -- This scenario happens only when the type is an untagged
3020 -- derivation from a private parent and the underlying full
3021 -- view was processed before the partial view.
3024 (Is_Untagged_Private_Derivation
(Priv_Typ
, Full_Typ
));
3026 -- Nothing to do because the processing of the underlying full
3027 -- view already checked the invariants of the partial view.
3032 -- Create a declaration for the "partial" invariant procedure if it
3033 -- is not available.
3035 if No
(Proc_Id
) then
3036 Build_Invariant_Procedure_Declaration
3038 Partial_Invariant
=> True);
3040 Proc_Id
:= Partial_Invariant_Procedure
(Work_Typ
);
3043 -- The caller requests a body for the "full" invariant procedure
3046 Proc_Id
:= Invariant_Procedure
(Work_Typ
);
3047 Part_Proc
:= Partial_Invariant_Procedure
(Work_Typ
);
3049 -- Create a declaration for the "full" invariant procedure if it is
3052 if No
(Proc_Id
) then
3053 Build_Invariant_Procedure_Declaration
(Work_Typ
);
3054 Proc_Id
:= Invariant_Procedure
(Work_Typ
);
3058 -- At this point there should be an invariant procedure declaration
3060 pragma Assert
(Present
(Proc_Id
));
3061 Proc_Decl
:= Unit_Declaration_Node
(Proc_Id
);
3063 -- Nothing to do if the invariant procedure already has a body
3065 if Present
(Corresponding_Body
(Proc_Decl
)) then
3069 -- Emulate the environment of the invariant procedure by installing its
3070 -- scope and formal parameters. Note that this is not needed, but having
3071 -- the scope installed helps with the detection of invariant-related
3074 Push_Scope
(Proc_Id
);
3075 Install_Formals
(Proc_Id
);
3077 Obj_Id
:= First_Formal
(Proc_Id
);
3078 pragma Assert
(Present
(Obj_Id
));
3080 -- The "partial" invariant procedure verifies the invariants of the
3081 -- partial view only.
3083 if Partial_Invariant
then
3084 pragma Assert
(Present
(Priv_Typ
));
3091 -- Otherwise the "full" invariant procedure verifies the invariants of
3092 -- the full view, all array or record components, as well as class-wide
3093 -- invariants inherited from parent types or interfaces. In addition, it
3094 -- indirectly verifies the invariants of the partial view by calling the
3095 -- "partial" invariant procedure.
3098 pragma Assert
(Present
(Full_Typ
));
3100 -- Check the invariants of the partial view by calling the "partial"
3101 -- invariant procedure. Generate:
3103 -- <Work_Typ>Partial_Invariant (_object);
3105 if Present
(Part_Proc
) then
3106 Append_New_To
(Stmts
,
3107 Make_Procedure_Call_Statement
(Loc
,
3108 Name
=> New_Occurrence_Of
(Part_Proc
, Loc
),
3109 Parameter_Associations
=> New_List
(
3110 New_Occurrence_Of
(Obj_Id
, Loc
))));
3112 Produced_Check
:= True;
3117 -- Derived subtypes do not have a partial view
3119 if Present
(Priv_Typ
) then
3121 -- The processing of the "full" invariant procedure intentionally
3122 -- skips the partial view because a) this may result in changes of
3123 -- visibility and b) lead to duplicate checks. However, when the
3124 -- full view is the underlying full view of an untagged derived
3125 -- type whose parent type is private, partial invariants appear on
3126 -- the rep item chain of the partial view only.
3128 -- package Pack_1 is
3129 -- type Root ... is private;
3131 -- <full view of Root>
3135 -- package Pack_2 is
3136 -- type Child is new Pack_1.Root with Type_Invariant => ...;
3137 -- <underlying full view of Child>
3140 -- As a result, the processing of the full view must also consider
3141 -- all invariants of the partial view.
3143 if Is_Untagged_Private_Derivation
(Priv_Typ
, Full_Typ
) then
3146 -- Otherwise the invariants of the partial view are ignored
3149 -- Note that the rep item chain is shared between the partial
3150 -- and full views of a type. To avoid processing the invariants
3151 -- of the partial view, signal the logic to stop when the first
3152 -- rep item of the partial view has been reached.
3154 Priv_Item
:= First_Rep_Item
(Priv_Typ
);
3156 -- Ignore the invariants of the partial view by eliminating the
3163 -- Process the invariants of the full view and in certain cases those
3164 -- of the partial view. This also handles any invariants on array or
3165 -- record components.
3171 Priv_Item
=> Priv_Item
);
3177 Priv_Item
=> Priv_Item
);
3179 -- Process the elements of an array type
3181 if Is_Array_Type
(Full_Typ
) then
3182 Add_Array_Component_Invariants
(Full_Typ
, Obj_Id
, Stmts
);
3184 -- Process the components of a record type
3186 elsif Ekind
(Full_Typ
) = E_Record_Type
then
3187 Add_Record_Component_Invariants
(Full_Typ
, Obj_Id
, Stmts
);
3189 -- Process the components of a corresponding record
3191 elsif Present
(CRec_Typ
) then
3192 Add_Record_Component_Invariants
(CRec_Typ
, Obj_Id
, Stmts
);
3195 -- Process the inherited class-wide invariants of all parent types.
3196 -- This also handles any invariants on record components.
3198 Add_Parent_Invariants
(Full_Typ
, Obj_Id
, Stmts
);
3200 -- Process the inherited class-wide invariants of all implemented
3203 Add_Interface_Invariants
(Full_Typ
, Obj_Id
, Stmts
);
3208 -- At this point there should be at least one invariant check. If this
3209 -- is not the case, then the invariant-related flags were not properly
3210 -- set, or there is a missing invariant procedure on one of the array
3211 -- or record components.
3213 pragma Assert
(Produced_Check
);
3215 -- Account for the case where assertions are disabled or all invariant
3216 -- checks are subject to Assertion_Policy Ignore. Produce a completing
3220 Stmts
:= New_List
(Make_Null_Statement
(Loc
));
3224 -- procedure <Work_Typ>[Partial_]Invariant (_object : <Obj_Typ>) is
3227 -- end <Work_Typ>[Partial_]Invariant;
3230 Make_Subprogram_Body
(Loc
,
3232 Copy_Subprogram_Spec
(Parent
(Proc_Id
)),
3233 Declarations
=> Empty_List
,
3234 Handled_Statement_Sequence
=>
3235 Make_Handled_Sequence_Of_Statements
(Loc
,
3236 Statements
=> Stmts
));
3237 Proc_Body_Id
:= Defining_Entity
(Proc_Body
);
3239 -- Perform minor decoration in case the body is not analyzed
3241 Set_Ekind
(Proc_Body_Id
, E_Subprogram_Body
);
3242 Set_Etype
(Proc_Body_Id
, Standard_Void_Type
);
3243 Set_Scope
(Proc_Body_Id
, Current_Scope
);
3245 -- Link both spec and body to avoid generating duplicates
3247 Set_Corresponding_Body
(Proc_Decl
, Proc_Body_Id
);
3248 Set_Corresponding_Spec
(Proc_Body
, Proc_Id
);
3250 -- The body should not be inserted into the tree when the context is
3251 -- ASIS or a generic unit because it is not part of the template. Note
3252 -- that the body must still be generated in order to resolve the
3255 if ASIS_Mode
or Inside_A_Generic
then
3258 -- Semi-insert the body into the tree for GNATprove by setting its
3259 -- Parent field. This allows for proper upstream tree traversals.
3261 elsif GNATprove_Mode
then
3262 Set_Parent
(Proc_Body
, Parent
(Declaration_Node
(Work_Typ
)));
3264 -- Otherwise the body is part of the freezing actions of the type
3267 Append_Freeze_Action
(Work_Typ
, Proc_Body
);
3271 Restore_Ghost_Mode
(Saved_GM
);
3272 end Build_Invariant_Procedure_Body
;
3274 -------------------------------------------
3275 -- Build_Invariant_Procedure_Declaration --
3276 -------------------------------------------
3278 -- WARNING: This routine manages Ghost regions. Return statements must be
3279 -- replaced by gotos which jump to the end of the routine and restore the
3282 procedure Build_Invariant_Procedure_Declaration
3284 Partial_Invariant
: Boolean := False)
3286 Loc
: constant Source_Ptr
:= Sloc
(Typ
);
3288 Saved_GM
: constant Ghost_Mode_Type
:= Ghost_Mode
;
3289 -- Save the Ghost mode to restore on exit
3291 Proc_Decl
: Node_Id
;
3292 Proc_Id
: Entity_Id
;
3296 CRec_Typ
: Entity_Id
;
3297 -- The corresponding record type of Full_Typ
3299 Full_Base
: Entity_Id
;
3300 -- The base type of Full_Typ
3302 Full_Typ
: Entity_Id
;
3303 -- The full view of working type
3306 -- The _object formal parameter of the invariant procedure
3308 Obj_Typ
: Entity_Id
;
3309 -- The type of the _object formal parameter
3311 Priv_Typ
: Entity_Id
;
3312 -- The partial view of working type
3314 Work_Typ
: Entity_Id
;
3320 -- The input type denotes the implementation base type of a constrained
3321 -- array type. Work with the first subtype as all invariant pragmas are
3322 -- on its rep item chain.
3324 if Ekind
(Work_Typ
) = E_Array_Type
and then Is_Itype
(Work_Typ
) then
3325 Work_Typ
:= First_Subtype
(Work_Typ
);
3327 -- The input denotes the corresponding record type of a protected or a
3328 -- task type. Work with the concurrent type because the corresponding
3329 -- record type may not be visible to clients of the type.
3331 elsif Ekind
(Work_Typ
) = E_Record_Type
3332 and then Is_Concurrent_Record_Type
(Work_Typ
)
3334 Work_Typ
:= Corresponding_Concurrent_Type
(Work_Typ
);
3337 -- The working type may be subject to pragma Ghost. Set the mode now to
3338 -- ensure that the invariant procedure is properly marked as Ghost.
3340 Set_Ghost_Mode
(Work_Typ
);
3342 -- The type must either have invariants of its own, inherit class-wide
3343 -- invariants from parent or interface types, or be an array or record
3344 -- type whose components have invariants.
3346 pragma Assert
(Has_Invariants
(Work_Typ
));
3348 -- Nothing to do if the type already has a "partial" invariant procedure
3350 if Partial_Invariant
then
3351 if Present
(Partial_Invariant_Procedure
(Work_Typ
)) then
3355 -- Nothing to do if the type already has a "full" invariant procedure
3357 elsif Present
(Invariant_Procedure
(Work_Typ
)) then
3361 -- The caller requests the declaration of the "partial" invariant
3364 if Partial_Invariant
then
3365 Proc_Nam
:= New_External_Name
(Chars
(Work_Typ
), "Partial_Invariant");
3367 -- Otherwise the caller requests the declaration of the "full" invariant
3371 Proc_Nam
:= New_External_Name
(Chars
(Work_Typ
), "Invariant");
3374 Proc_Id
:= Make_Defining_Identifier
(Loc
, Chars
=> Proc_Nam
);
3376 -- Perform minor decoration in case the declaration is not analyzed
3378 Set_Ekind
(Proc_Id
, E_Procedure
);
3379 Set_Etype
(Proc_Id
, Standard_Void_Type
);
3380 Set_Scope
(Proc_Id
, Current_Scope
);
3382 if Partial_Invariant
then
3383 Set_Is_Partial_Invariant_Procedure
(Proc_Id
);
3384 Set_Partial_Invariant_Procedure
(Work_Typ
, Proc_Id
);
3386 Set_Is_Invariant_Procedure
(Proc_Id
);
3387 Set_Invariant_Procedure
(Work_Typ
, Proc_Id
);
3390 -- The invariant procedure requires debug info when the invariants are
3391 -- subject to Source Coverage Obligations.
3393 if Generate_SCO
then
3394 Set_Needs_Debug_Info
(Proc_Id
);
3397 -- Obtain all views of the input type
3399 Get_Views
(Work_Typ
, Priv_Typ
, Full_Typ
, Full_Base
, CRec_Typ
);
3401 -- Associate the invariant procedure with all views
3403 Propagate_Invariant_Attributes
(Priv_Typ
, From_Typ
=> Work_Typ
);
3404 Propagate_Invariant_Attributes
(Full_Typ
, From_Typ
=> Work_Typ
);
3405 Propagate_Invariant_Attributes
(Full_Base
, From_Typ
=> Work_Typ
);
3406 Propagate_Invariant_Attributes
(CRec_Typ
, From_Typ
=> Work_Typ
);
3408 -- The declaration of the invariant procedure is inserted after the
3409 -- declaration of the partial view as this allows for proper external
3412 if Present
(Priv_Typ
) then
3413 Typ_Decl
:= Declaration_Node
(Priv_Typ
);
3415 -- Anonymous arrays in object declarations have no explicit declaration
3416 -- so use the related object declaration as the insertion point.
3418 elsif Is_Itype
(Work_Typ
) and then Is_Array_Type
(Work_Typ
) then
3419 Typ_Decl
:= Associated_Node_For_Itype
(Work_Typ
);
3421 -- Derived types with the full view as parent do not have a partial
3422 -- view. Insert the invariant procedure after the derived type.
3425 Typ_Decl
:= Declaration_Node
(Full_Typ
);
3428 -- The type should have a declarative node
3430 pragma Assert
(Present
(Typ_Decl
));
3432 -- Create the formal parameter which emulates the variable-like behavior
3433 -- of the current type instance.
3435 Obj_Id
:= Make_Defining_Identifier
(Loc
, Chars
=> Name_uObject
);
3437 -- When generating an invariant procedure declaration for an abstract
3438 -- type (including interfaces), use the class-wide type as the _object
3439 -- type. This has several desirable effects:
3441 -- * The invariant procedure does not become a primitive of the type.
3442 -- This eliminates the need to either special case the treatment of
3443 -- invariant procedures, or to make it a predefined primitive and
3444 -- force every derived type to potentially provide an empty body.
3446 -- * The invariant procedure does not need to be declared as abstract.
3447 -- This allows for a proper body, which in turn avoids redundant
3448 -- processing of the same invariants for types with multiple views.
3450 -- * The class-wide type allows for calls to abstract primitives
3451 -- within a nonabstract subprogram. The calls are treated as
3452 -- dispatching and require additional processing when they are
3453 -- remapped to call primitives of derived types. See routine
3454 -- Replace_References for details.
3456 if Is_Abstract_Type
(Work_Typ
) then
3457 Obj_Typ
:= Class_Wide_Type
(Work_Typ
);
3459 Obj_Typ
:= Work_Typ
;
3462 -- Perform minor decoration in case the declaration is not analyzed
3464 Set_Ekind
(Obj_Id
, E_In_Parameter
);
3465 Set_Etype
(Obj_Id
, Obj_Typ
);
3466 Set_Scope
(Obj_Id
, Proc_Id
);
3468 Set_First_Entity
(Proc_Id
, Obj_Id
);
3471 -- procedure <Work_Typ>[Partial_]Invariant (_object : <Obj_Typ>);
3474 Make_Subprogram_Declaration
(Loc
,
3476 Make_Procedure_Specification
(Loc
,
3477 Defining_Unit_Name
=> Proc_Id
,
3478 Parameter_Specifications
=> New_List
(
3479 Make_Parameter_Specification
(Loc
,
3480 Defining_Identifier
=> Obj_Id
,
3481 Parameter_Type
=> New_Occurrence_Of
(Obj_Typ
, Loc
)))));
3483 -- The declaration should not be inserted into the tree when the context
3484 -- is ASIS or a generic unit because it is not part of the template.
3486 if ASIS_Mode
or Inside_A_Generic
then
3489 -- Semi-insert the declaration into the tree for GNATprove by setting
3490 -- its Parent field. This allows for proper upstream tree traversals.
3492 elsif GNATprove_Mode
then
3493 Set_Parent
(Proc_Decl
, Parent
(Typ_Decl
));
3495 -- Otherwise insert the declaration
3498 pragma Assert
(Present
(Typ_Decl
));
3499 Insert_After_And_Analyze
(Typ_Decl
, Proc_Decl
);
3503 Restore_Ghost_Mode
(Saved_GM
);
3504 end Build_Invariant_Procedure_Declaration
;
3506 --------------------------
3507 -- Build_Procedure_Form --
3508 --------------------------
3510 procedure Build_Procedure_Form
(N
: Node_Id
) is
3511 Loc
: constant Source_Ptr
:= Sloc
(N
);
3512 Subp
: constant Entity_Id
:= Defining_Entity
(N
);
3514 Func_Formal
: Entity_Id
;
3515 Proc_Formals
: List_Id
;
3516 Proc_Decl
: Node_Id
;
3519 -- No action needed if this transformation was already done, or in case
3520 -- of subprogram renaming declarations.
3522 if Nkind
(Specification
(N
)) = N_Procedure_Specification
3523 or else Nkind
(N
) = N_Subprogram_Renaming_Declaration
3528 -- Ditto when dealing with an expression function, where both the
3529 -- original expression and the generated declaration end up being
3532 if Rewritten_For_C
(Subp
) then
3536 Proc_Formals
:= New_List
;
3538 -- Create a list of formal parameters with the same types as the
3541 Func_Formal
:= First_Formal
(Subp
);
3542 while Present
(Func_Formal
) loop
3543 Append_To
(Proc_Formals
,
3544 Make_Parameter_Specification
(Loc
,
3545 Defining_Identifier
=>
3546 Make_Defining_Identifier
(Loc
, Chars
(Func_Formal
)),
3548 New_Occurrence_Of
(Etype
(Func_Formal
), Loc
)));
3550 Next_Formal
(Func_Formal
);
3553 -- Add an extra out parameter to carry the function result
3556 Name_Buffer
(1 .. Name_Len
) := "RESULT";
3557 Append_To
(Proc_Formals
,
3558 Make_Parameter_Specification
(Loc
,
3559 Defining_Identifier
=>
3560 Make_Defining_Identifier
(Loc
, Chars
=> Name_Find
),
3561 Out_Present
=> True,
3562 Parameter_Type
=> New_Occurrence_Of
(Etype
(Subp
), Loc
)));
3564 -- The new procedure declaration is inserted immediately after the
3565 -- function declaration. The processing in Build_Procedure_Body_Form
3566 -- relies on this order.
3569 Make_Subprogram_Declaration
(Loc
,
3571 Make_Procedure_Specification
(Loc
,
3572 Defining_Unit_Name
=>
3573 Make_Defining_Identifier
(Loc
, Chars
(Subp
)),
3574 Parameter_Specifications
=> Proc_Formals
));
3576 Insert_After_And_Analyze
(Unit_Declaration_Node
(Subp
), Proc_Decl
);
3578 -- Entity of procedure must remain invisible so that it does not
3579 -- overload subsequent references to the original function.
3581 Set_Is_Immediately_Visible
(Defining_Entity
(Proc_Decl
), False);
3583 -- Mark the function as having a procedure form and link the function
3584 -- and its internally built procedure.
3586 Set_Rewritten_For_C
(Subp
);
3587 Set_Corresponding_Procedure
(Subp
, Defining_Entity
(Proc_Decl
));
3588 Set_Corresponding_Function
(Defining_Entity
(Proc_Decl
), Subp
);
3589 end Build_Procedure_Form
;
3591 ------------------------
3592 -- Build_Runtime_Call --
3593 ------------------------
3595 function Build_Runtime_Call
(Loc
: Source_Ptr
; RE
: RE_Id
) return Node_Id
is
3597 -- If entity is not available, we can skip making the call (this avoids
3598 -- junk duplicated error messages in a number of cases).
3600 if not RTE_Available
(RE
) then
3601 return Make_Null_Statement
(Loc
);
3604 Make_Procedure_Call_Statement
(Loc
,
3605 Name
=> New_Occurrence_Of
(RTE
(RE
), Loc
));
3607 end Build_Runtime_Call
;
3609 ------------------------
3610 -- Build_SS_Mark_Call --
3611 ------------------------
3613 function Build_SS_Mark_Call
3615 Mark
: Entity_Id
) return Node_Id
3619 -- Mark : constant Mark_Id := SS_Mark;
3622 Make_Object_Declaration
(Loc
,
3623 Defining_Identifier
=> Mark
,
3624 Constant_Present
=> True,
3625 Object_Definition
=>
3626 New_Occurrence_Of
(RTE
(RE_Mark_Id
), Loc
),
3628 Make_Function_Call
(Loc
,
3629 Name
=> New_Occurrence_Of
(RTE
(RE_SS_Mark
), Loc
)));
3630 end Build_SS_Mark_Call
;
3632 ---------------------------
3633 -- Build_SS_Release_Call --
3634 ---------------------------
3636 function Build_SS_Release_Call
3638 Mark
: Entity_Id
) return Node_Id
3642 -- SS_Release (Mark);
3645 Make_Procedure_Call_Statement
(Loc
,
3647 New_Occurrence_Of
(RTE
(RE_SS_Release
), Loc
),
3648 Parameter_Associations
=> New_List
(
3649 New_Occurrence_Of
(Mark
, Loc
)));
3650 end Build_SS_Release_Call
;
3652 ----------------------------
3653 -- Build_Task_Array_Image --
3654 ----------------------------
3656 -- This function generates the body for a function that constructs the
3657 -- image string for a task that is an array component. The function is
3658 -- local to the init proc for the array type, and is called for each one
3659 -- of the components. The constructed image has the form of an indexed
3660 -- component, whose prefix is the outer variable of the array type.
3661 -- The n-dimensional array type has known indexes Index, Index2...
3663 -- Id_Ref is an indexed component form created by the enclosing init proc.
3664 -- Its successive indexes are Val1, Val2, ... which are the loop variables
3665 -- in the loops that call the individual task init proc on each component.
3667 -- The generated function has the following structure:
3669 -- function F return String is
3670 -- Pref : string renames Task_Name;
3671 -- T1 : String := Index1'Image (Val1);
3673 -- Tn : String := indexn'image (Valn);
3674 -- Len : Integer := T1'Length + ... + Tn'Length + n + 1;
3675 -- -- Len includes commas and the end parentheses.
3676 -- Res : String (1..Len);
3677 -- Pos : Integer := Pref'Length;
3680 -- Res (1 .. Pos) := Pref;
3682 -- Res (Pos) := '(';
3684 -- Res (Pos .. Pos + T1'Length - 1) := T1;
3685 -- Pos := Pos + T1'Length;
3686 -- Res (Pos) := '.';
3689 -- Res (Pos .. Pos + Tn'Length - 1) := Tn;
3690 -- Res (Len) := ')';
3695 -- Needless to say, multidimensional arrays of tasks are rare enough that
3696 -- the bulkiness of this code is not really a concern.
3698 function Build_Task_Array_Image
3702 Dyn
: Boolean := False) return Node_Id
3704 Dims
: constant Nat
:= Number_Dimensions
(A_Type
);
3705 -- Number of dimensions for array of tasks
3707 Temps
: array (1 .. Dims
) of Entity_Id
;
3708 -- Array of temporaries to hold string for each index
3714 -- Total length of generated name
3717 -- Running index for substring assignments
3719 Pref
: constant Entity_Id
:= Make_Temporary
(Loc
, 'P');
3720 -- Name of enclosing variable, prefix of resulting name
3723 -- String to hold result
3726 -- Value of successive indexes
3729 -- Expression to compute total size of string
3732 -- Entity for name at one index position
3734 Decls
: constant List_Id
:= New_List
;
3735 Stats
: constant List_Id
:= New_List
;
3738 -- For a dynamic task, the name comes from the target variable. For a
3739 -- static one it is a formal of the enclosing init proc.
3742 Get_Name_String
(Chars
(Entity
(Prefix
(Id_Ref
))));
3744 Make_Object_Declaration
(Loc
,
3745 Defining_Identifier
=> Pref
,
3746 Object_Definition
=> New_Occurrence_Of
(Standard_String
, Loc
),
3748 Make_String_Literal
(Loc
,
3749 Strval
=> String_From_Name_Buffer
)));
3753 Make_Object_Renaming_Declaration
(Loc
,
3754 Defining_Identifier
=> Pref
,
3755 Subtype_Mark
=> New_Occurrence_Of
(Standard_String
, Loc
),
3756 Name
=> Make_Identifier
(Loc
, Name_uTask_Name
)));
3759 Indx
:= First_Index
(A_Type
);
3760 Val
:= First
(Expressions
(Id_Ref
));
3762 for J
in 1 .. Dims
loop
3763 T
:= Make_Temporary
(Loc
, 'T');
3767 Make_Object_Declaration
(Loc
,
3768 Defining_Identifier
=> T
,
3769 Object_Definition
=> New_Occurrence_Of
(Standard_String
, Loc
),
3771 Make_Attribute_Reference
(Loc
,
3772 Attribute_Name
=> Name_Image
,
3773 Prefix
=> New_Occurrence_Of
(Etype
(Indx
), Loc
),
3774 Expressions
=> New_List
(New_Copy_Tree
(Val
)))));
3780 Sum
:= Make_Integer_Literal
(Loc
, Dims
+ 1);
3786 Make_Attribute_Reference
(Loc
,
3787 Attribute_Name
=> Name_Length
,
3788 Prefix
=> New_Occurrence_Of
(Pref
, Loc
),
3789 Expressions
=> New_List
(Make_Integer_Literal
(Loc
, 1))));
3791 for J
in 1 .. Dims
loop
3796 Make_Attribute_Reference
(Loc
,
3797 Attribute_Name
=> Name_Length
,
3799 New_Occurrence_Of
(Temps
(J
), Loc
),
3800 Expressions
=> New_List
(Make_Integer_Literal
(Loc
, 1))));
3803 Build_Task_Image_Prefix
(Loc
, Len
, Res
, Pos
, Pref
, Sum
, Decls
, Stats
);
3805 Set_Character_Literal_Name
(Char_Code
(Character'Pos ('(')));
3808 Make_Assignment_Statement
(Loc
,
3810 Make_Indexed_Component
(Loc
,
3811 Prefix
=> New_Occurrence_Of
(Res
, Loc
),
3812 Expressions
=> New_List
(New_Occurrence_Of
(Pos
, Loc
))),
3814 Make_Character_Literal
(Loc
,
3816 Char_Literal_Value
=> UI_From_Int
(Character'Pos ('(')))));
3819 Make_Assignment_Statement
(Loc
,
3820 Name
=> New_Occurrence_Of
(Pos
, Loc
),
3823 Left_Opnd
=> New_Occurrence_Of
(Pos
, Loc
),
3824 Right_Opnd
=> Make_Integer_Literal
(Loc
, 1))));
3826 for J
in 1 .. Dims
loop
3829 Make_Assignment_Statement
(Loc
,
3832 Prefix
=> New_Occurrence_Of
(Res
, Loc
),
3835 Low_Bound
=> New_Occurrence_Of
(Pos
, Loc
),
3837 Make_Op_Subtract
(Loc
,
3840 Left_Opnd
=> New_Occurrence_Of
(Pos
, Loc
),
3842 Make_Attribute_Reference
(Loc
,
3843 Attribute_Name
=> Name_Length
,
3845 New_Occurrence_Of
(Temps
(J
), Loc
),
3847 New_List
(Make_Integer_Literal
(Loc
, 1)))),
3848 Right_Opnd
=> Make_Integer_Literal
(Loc
, 1)))),
3850 Expression
=> New_Occurrence_Of
(Temps
(J
), Loc
)));
3854 Make_Assignment_Statement
(Loc
,
3855 Name
=> New_Occurrence_Of
(Pos
, Loc
),
3858 Left_Opnd
=> New_Occurrence_Of
(Pos
, Loc
),
3860 Make_Attribute_Reference
(Loc
,
3861 Attribute_Name
=> Name_Length
,
3862 Prefix
=> New_Occurrence_Of
(Temps
(J
), Loc
),
3864 New_List
(Make_Integer_Literal
(Loc
, 1))))));
3866 Set_Character_Literal_Name
(Char_Code
(Character'Pos (',')));
3869 Make_Assignment_Statement
(Loc
,
3870 Name
=> Make_Indexed_Component
(Loc
,
3871 Prefix
=> New_Occurrence_Of
(Res
, Loc
),
3872 Expressions
=> New_List
(New_Occurrence_Of
(Pos
, Loc
))),
3874 Make_Character_Literal
(Loc
,
3876 Char_Literal_Value
=> UI_From_Int
(Character'Pos (',')))));
3879 Make_Assignment_Statement
(Loc
,
3880 Name
=> New_Occurrence_Of
(Pos
, Loc
),
3883 Left_Opnd
=> New_Occurrence_Of
(Pos
, Loc
),
3884 Right_Opnd
=> Make_Integer_Literal
(Loc
, 1))));
3888 Set_Character_Literal_Name
(Char_Code
(Character'Pos (')')));
3891 Make_Assignment_Statement
(Loc
,
3893 Make_Indexed_Component
(Loc
,
3894 Prefix
=> New_Occurrence_Of
(Res
, Loc
),
3895 Expressions
=> New_List
(New_Occurrence_Of
(Len
, Loc
))),
3897 Make_Character_Literal
(Loc
,
3899 Char_Literal_Value
=> UI_From_Int
(Character'Pos (')')))));
3900 return Build_Task_Image_Function
(Loc
, Decls
, Stats
, Res
);
3901 end Build_Task_Array_Image
;
3903 ----------------------------
3904 -- Build_Task_Image_Decls --
3905 ----------------------------
3907 function Build_Task_Image_Decls
3911 In_Init_Proc
: Boolean := False) return List_Id
3913 Decls
: constant List_Id
:= New_List
;
3914 T_Id
: Entity_Id
:= Empty
;
3916 Expr
: Node_Id
:= Empty
;
3917 Fun
: Node_Id
:= Empty
;
3918 Is_Dyn
: constant Boolean :=
3919 Nkind
(Parent
(Id_Ref
)) = N_Assignment_Statement
3921 Nkind
(Expression
(Parent
(Id_Ref
))) = N_Allocator
;
3924 -- If Discard_Names or No_Implicit_Heap_Allocations are in effect,
3925 -- generate a dummy declaration only.
3927 if Restriction_Active
(No_Implicit_Heap_Allocations
)
3928 or else Global_Discard_Names
3930 T_Id
:= Make_Temporary
(Loc
, 'J');
3935 Make_Object_Declaration
(Loc
,
3936 Defining_Identifier
=> T_Id
,
3937 Object_Definition
=> New_Occurrence_Of
(Standard_String
, Loc
),
3939 Make_String_Literal
(Loc
,
3940 Strval
=> String_From_Name_Buffer
)));
3943 if Nkind
(Id_Ref
) = N_Identifier
3944 or else Nkind
(Id_Ref
) = N_Defining_Identifier
3946 -- For a simple variable, the image of the task is built from
3947 -- the name of the variable. To avoid possible conflict with the
3948 -- anonymous type created for a single protected object, add a
3952 Make_Defining_Identifier
(Loc
,
3953 New_External_Name
(Chars
(Id_Ref
), 'T', 1));
3955 Get_Name_String
(Chars
(Id_Ref
));
3958 Make_String_Literal
(Loc
,
3959 Strval
=> String_From_Name_Buffer
);
3961 elsif Nkind
(Id_Ref
) = N_Selected_Component
then
3963 Make_Defining_Identifier
(Loc
,
3964 New_External_Name
(Chars
(Selector_Name
(Id_Ref
)), 'T'));
3965 Fun
:= Build_Task_Record_Image
(Loc
, Id_Ref
, Is_Dyn
);
3967 elsif Nkind
(Id_Ref
) = N_Indexed_Component
then
3969 Make_Defining_Identifier
(Loc
,
3970 New_External_Name
(Chars
(A_Type
), 'N'));
3972 Fun
:= Build_Task_Array_Image
(Loc
, Id_Ref
, A_Type
, Is_Dyn
);
3976 if Present
(Fun
) then
3977 Append
(Fun
, Decls
);
3978 Expr
:= Make_Function_Call
(Loc
,
3979 Name
=> New_Occurrence_Of
(Defining_Entity
(Fun
), Loc
));
3981 if not In_Init_Proc
then
3982 Set_Uses_Sec_Stack
(Defining_Entity
(Fun
));
3986 Decl
:= Make_Object_Declaration
(Loc
,
3987 Defining_Identifier
=> T_Id
,
3988 Object_Definition
=> New_Occurrence_Of
(Standard_String
, Loc
),
3989 Constant_Present
=> True,
3990 Expression
=> Expr
);
3992 Append
(Decl
, Decls
);
3994 end Build_Task_Image_Decls
;
3996 -------------------------------
3997 -- Build_Task_Image_Function --
3998 -------------------------------
4000 function Build_Task_Image_Function
4004 Res
: Entity_Id
) return Node_Id
4010 Make_Simple_Return_Statement
(Loc
,
4011 Expression
=> New_Occurrence_Of
(Res
, Loc
)));
4013 Spec
:= Make_Function_Specification
(Loc
,
4014 Defining_Unit_Name
=> Make_Temporary
(Loc
, 'F'),
4015 Result_Definition
=> New_Occurrence_Of
(Standard_String
, Loc
));
4017 -- Calls to 'Image use the secondary stack, which must be cleaned up
4018 -- after the task name is built.
4020 return Make_Subprogram_Body
(Loc
,
4021 Specification
=> Spec
,
4022 Declarations
=> Decls
,
4023 Handled_Statement_Sequence
=>
4024 Make_Handled_Sequence_Of_Statements
(Loc
, Statements
=> Stats
));
4025 end Build_Task_Image_Function
;
4027 -----------------------------
4028 -- Build_Task_Image_Prefix --
4029 -----------------------------
4031 procedure Build_Task_Image_Prefix
4033 Len
: out Entity_Id
;
4034 Res
: out Entity_Id
;
4035 Pos
: out Entity_Id
;
4042 Len
:= Make_Temporary
(Loc
, 'L', Sum
);
4045 Make_Object_Declaration
(Loc
,
4046 Defining_Identifier
=> Len
,
4047 Object_Definition
=> New_Occurrence_Of
(Standard_Integer
, Loc
),
4048 Expression
=> Sum
));
4050 Res
:= Make_Temporary
(Loc
, 'R');
4053 Make_Object_Declaration
(Loc
,
4054 Defining_Identifier
=> Res
,
4055 Object_Definition
=>
4056 Make_Subtype_Indication
(Loc
,
4057 Subtype_Mark
=> New_Occurrence_Of
(Standard_String
, Loc
),
4059 Make_Index_Or_Discriminant_Constraint
(Loc
,
4063 Low_Bound
=> Make_Integer_Literal
(Loc
, 1),
4064 High_Bound
=> New_Occurrence_Of
(Len
, Loc
)))))));
4066 -- Indicate that the result is an internal temporary, so it does not
4067 -- receive a bogus initialization when declaration is expanded. This
4068 -- is both efficient, and prevents anomalies in the handling of
4069 -- dynamic objects on the secondary stack.
4071 Set_Is_Internal
(Res
);
4072 Pos
:= Make_Temporary
(Loc
, 'P');
4075 Make_Object_Declaration
(Loc
,
4076 Defining_Identifier
=> Pos
,
4077 Object_Definition
=> New_Occurrence_Of
(Standard_Integer
, Loc
)));
4079 -- Pos := Prefix'Length;
4082 Make_Assignment_Statement
(Loc
,
4083 Name
=> New_Occurrence_Of
(Pos
, Loc
),
4085 Make_Attribute_Reference
(Loc
,
4086 Attribute_Name
=> Name_Length
,
4087 Prefix
=> New_Occurrence_Of
(Prefix
, Loc
),
4088 Expressions
=> New_List
(Make_Integer_Literal
(Loc
, 1)))));
4090 -- Res (1 .. Pos) := Prefix;
4093 Make_Assignment_Statement
(Loc
,
4096 Prefix
=> New_Occurrence_Of
(Res
, Loc
),
4099 Low_Bound
=> Make_Integer_Literal
(Loc
, 1),
4100 High_Bound
=> New_Occurrence_Of
(Pos
, Loc
))),
4102 Expression
=> New_Occurrence_Of
(Prefix
, Loc
)));
4105 Make_Assignment_Statement
(Loc
,
4106 Name
=> New_Occurrence_Of
(Pos
, Loc
),
4109 Left_Opnd
=> New_Occurrence_Of
(Pos
, Loc
),
4110 Right_Opnd
=> Make_Integer_Literal
(Loc
, 1))));
4111 end Build_Task_Image_Prefix
;
4113 -----------------------------
4114 -- Build_Task_Record_Image --
4115 -----------------------------
4117 function Build_Task_Record_Image
4120 Dyn
: Boolean := False) return Node_Id
4123 -- Total length of generated name
4126 -- Index into result
4129 -- String to hold result
4131 Pref
: constant Entity_Id
:= Make_Temporary
(Loc
, 'P');
4132 -- Name of enclosing variable, prefix of resulting name
4135 -- Expression to compute total size of string
4138 -- Entity for selector name
4140 Decls
: constant List_Id
:= New_List
;
4141 Stats
: constant List_Id
:= New_List
;
4144 -- For a dynamic task, the name comes from the target variable. For a
4145 -- static one it is a formal of the enclosing init proc.
4148 Get_Name_String
(Chars
(Entity
(Prefix
(Id_Ref
))));
4150 Make_Object_Declaration
(Loc
,
4151 Defining_Identifier
=> Pref
,
4152 Object_Definition
=> New_Occurrence_Of
(Standard_String
, Loc
),
4154 Make_String_Literal
(Loc
,
4155 Strval
=> String_From_Name_Buffer
)));
4159 Make_Object_Renaming_Declaration
(Loc
,
4160 Defining_Identifier
=> Pref
,
4161 Subtype_Mark
=> New_Occurrence_Of
(Standard_String
, Loc
),
4162 Name
=> Make_Identifier
(Loc
, Name_uTask_Name
)));
4165 Sel
:= Make_Temporary
(Loc
, 'S');
4167 Get_Name_String
(Chars
(Selector_Name
(Id_Ref
)));
4170 Make_Object_Declaration
(Loc
,
4171 Defining_Identifier
=> Sel
,
4172 Object_Definition
=> New_Occurrence_Of
(Standard_String
, Loc
),
4174 Make_String_Literal
(Loc
,
4175 Strval
=> String_From_Name_Buffer
)));
4177 Sum
:= Make_Integer_Literal
(Loc
, Nat
(Name_Len
+ 1));
4183 Make_Attribute_Reference
(Loc
,
4184 Attribute_Name
=> Name_Length
,
4186 New_Occurrence_Of
(Pref
, Loc
),
4187 Expressions
=> New_List
(Make_Integer_Literal
(Loc
, 1))));
4189 Build_Task_Image_Prefix
(Loc
, Len
, Res
, Pos
, Pref
, Sum
, Decls
, Stats
);
4191 Set_Character_Literal_Name
(Char_Code
(Character'Pos ('.')));
4193 -- Res (Pos) := '.';
4196 Make_Assignment_Statement
(Loc
,
4197 Name
=> Make_Indexed_Component
(Loc
,
4198 Prefix
=> New_Occurrence_Of
(Res
, Loc
),
4199 Expressions
=> New_List
(New_Occurrence_Of
(Pos
, Loc
))),
4201 Make_Character_Literal
(Loc
,
4203 Char_Literal_Value
=>
4204 UI_From_Int
(Character'Pos ('.')))));
4207 Make_Assignment_Statement
(Loc
,
4208 Name
=> New_Occurrence_Of
(Pos
, Loc
),
4211 Left_Opnd
=> New_Occurrence_Of
(Pos
, Loc
),
4212 Right_Opnd
=> Make_Integer_Literal
(Loc
, 1))));
4214 -- Res (Pos .. Len) := Selector;
4217 Make_Assignment_Statement
(Loc
,
4218 Name
=> Make_Slice
(Loc
,
4219 Prefix
=> New_Occurrence_Of
(Res
, Loc
),
4222 Low_Bound
=> New_Occurrence_Of
(Pos
, Loc
),
4223 High_Bound
=> New_Occurrence_Of
(Len
, Loc
))),
4224 Expression
=> New_Occurrence_Of
(Sel
, Loc
)));
4226 return Build_Task_Image_Function
(Loc
, Decls
, Stats
, Res
);
4227 end Build_Task_Record_Image
;
4229 ---------------------------------------
4230 -- Build_Transient_Object_Statements --
4231 ---------------------------------------
4233 procedure Build_Transient_Object_Statements
4234 (Obj_Decl
: Node_Id
;
4235 Fin_Call
: out Node_Id
;
4236 Hook_Assign
: out Node_Id
;
4237 Hook_Clear
: out Node_Id
;
4238 Hook_Decl
: out Node_Id
;
4239 Ptr_Decl
: out Node_Id
;
4240 Finalize_Obj
: Boolean := True)
4242 Loc
: constant Source_Ptr
:= Sloc
(Obj_Decl
);
4243 Obj_Id
: constant Entity_Id
:= Defining_Entity
(Obj_Decl
);
4244 Obj_Typ
: constant Entity_Id
:= Base_Type
(Etype
(Obj_Id
));
4246 Desig_Typ
: Entity_Id
;
4247 Hook_Expr
: Node_Id
;
4248 Hook_Id
: Entity_Id
;
4250 Ptr_Typ
: Entity_Id
;
4253 -- Recover the type of the object
4255 Desig_Typ
:= Obj_Typ
;
4257 if Is_Access_Type
(Desig_Typ
) then
4258 Desig_Typ
:= Available_View
(Designated_Type
(Desig_Typ
));
4261 -- Create an access type which provides a reference to the transient
4262 -- object. Generate:
4264 -- type Ptr_Typ is access all Desig_Typ;
4266 Ptr_Typ
:= Make_Temporary
(Loc
, 'A');
4267 Set_Ekind
(Ptr_Typ
, E_General_Access_Type
);
4268 Set_Directly_Designated_Type
(Ptr_Typ
, Desig_Typ
);
4271 Make_Full_Type_Declaration
(Loc
,
4272 Defining_Identifier
=> Ptr_Typ
,
4274 Make_Access_To_Object_Definition
(Loc
,
4275 All_Present
=> True,
4276 Subtype_Indication
=> New_Occurrence_Of
(Desig_Typ
, Loc
)));
4278 -- Create a temporary check which acts as a hook to the transient
4279 -- object. Generate:
4281 -- Hook : Ptr_Typ := null;
4283 Hook_Id
:= Make_Temporary
(Loc
, 'T');
4284 Set_Ekind
(Hook_Id
, E_Variable
);
4285 Set_Etype
(Hook_Id
, Ptr_Typ
);
4288 Make_Object_Declaration
(Loc
,
4289 Defining_Identifier
=> Hook_Id
,
4290 Object_Definition
=> New_Occurrence_Of
(Ptr_Typ
, Loc
),
4291 Expression
=> Make_Null
(Loc
));
4293 -- Mark the temporary as a hook. This signals the machinery in
4294 -- Build_Finalizer to recognize this special case.
4296 Set_Status_Flag_Or_Transient_Decl
(Hook_Id
, Obj_Decl
);
4298 -- Hook the transient object to the temporary. Generate:
4300 -- Hook := Ptr_Typ (Obj_Id);
4302 -- Hool := Obj_Id'Unrestricted_Access;
4304 if Is_Access_Type
(Obj_Typ
) then
4306 Unchecked_Convert_To
(Ptr_Typ
, New_Occurrence_Of
(Obj_Id
, Loc
));
4309 Make_Attribute_Reference
(Loc
,
4310 Prefix
=> New_Occurrence_Of
(Obj_Id
, Loc
),
4311 Attribute_Name
=> Name_Unrestricted_Access
);
4315 Make_Assignment_Statement
(Loc
,
4316 Name
=> New_Occurrence_Of
(Hook_Id
, Loc
),
4317 Expression
=> Hook_Expr
);
4319 -- Crear the hook prior to finalizing the object. Generate:
4324 Make_Assignment_Statement
(Loc
,
4325 Name
=> New_Occurrence_Of
(Hook_Id
, Loc
),
4326 Expression
=> Make_Null
(Loc
));
4328 -- Finalize the object. Generate:
4330 -- [Deep_]Finalize (Obj_Ref[.all]);
4332 if Finalize_Obj
then
4333 Obj_Ref
:= New_Occurrence_Of
(Obj_Id
, Loc
);
4335 if Is_Access_Type
(Obj_Typ
) then
4336 Obj_Ref
:= Make_Explicit_Dereference
(Loc
, Obj_Ref
);
4337 Set_Etype
(Obj_Ref
, Desig_Typ
);
4342 (Obj_Ref
=> Obj_Ref
,
4345 -- Otherwise finalize the hook. Generate:
4347 -- [Deep_]Finalize (Hook.all);
4353 Make_Explicit_Dereference
(Loc
,
4354 Prefix
=> New_Occurrence_Of
(Hook_Id
, Loc
)),
4357 end Build_Transient_Object_Statements
;
4359 -----------------------------
4360 -- Check_Float_Op_Overflow --
4361 -----------------------------
4363 procedure Check_Float_Op_Overflow
(N
: Node_Id
) is
4365 -- Return if no check needed
4367 if not Is_Floating_Point_Type
(Etype
(N
))
4368 or else not (Do_Overflow_Check
(N
) and then Check_Float_Overflow
)
4370 -- In CodePeer_Mode, rely on the overflow check flag being set instead
4371 -- and do not expand the code for float overflow checking.
4373 or else CodePeer_Mode
4378 -- Otherwise we replace the expression by
4380 -- do Tnn : constant ftype := expression;
4381 -- constraint_error when not Tnn'Valid;
4385 Loc
: constant Source_Ptr
:= Sloc
(N
);
4386 Tnn
: constant Entity_Id
:= Make_Temporary
(Loc
, 'T', N
);
4387 Typ
: constant Entity_Id
:= Etype
(N
);
4390 -- Turn off the Do_Overflow_Check flag, since we are doing that work
4391 -- right here. We also set the node as analyzed to prevent infinite
4392 -- recursion from repeating the operation in the expansion.
4394 Set_Do_Overflow_Check
(N
, False);
4395 Set_Analyzed
(N
, True);
4397 -- Do the rewrite to include the check
4400 Make_Expression_With_Actions
(Loc
,
4401 Actions
=> New_List
(
4402 Make_Object_Declaration
(Loc
,
4403 Defining_Identifier
=> Tnn
,
4404 Object_Definition
=> New_Occurrence_Of
(Typ
, Loc
),
4405 Constant_Present
=> True,
4406 Expression
=> Relocate_Node
(N
)),
4407 Make_Raise_Constraint_Error
(Loc
,
4411 Make_Attribute_Reference
(Loc
,
4412 Prefix
=> New_Occurrence_Of
(Tnn
, Loc
),
4413 Attribute_Name
=> Name_Valid
)),
4414 Reason
=> CE_Overflow_Check_Failed
)),
4415 Expression
=> New_Occurrence_Of
(Tnn
, Loc
)));
4417 Analyze_And_Resolve
(N
, Typ
);
4419 end Check_Float_Op_Overflow
;
4421 ----------------------------------
4422 -- Component_May_Be_Bit_Aligned --
4423 ----------------------------------
4425 function Component_May_Be_Bit_Aligned
(Comp
: Entity_Id
) return Boolean is
4429 -- If no component clause, then everything is fine, since the back end
4430 -- never bit-misaligns by default, even if there is a pragma Packed for
4433 if No
(Comp
) or else No
(Component_Clause
(Comp
)) then
4437 UT
:= Underlying_Type
(Etype
(Comp
));
4439 -- It is only array and record types that cause trouble
4441 if not Is_Record_Type
(UT
) and then not Is_Array_Type
(UT
) then
4444 -- If we know that we have a small (64 bits or less) record or small
4445 -- bit-packed array, then everything is fine, since the back end can
4446 -- handle these cases correctly.
4448 elsif Esize
(Comp
) <= 64
4449 and then (Is_Record_Type
(UT
) or else Is_Bit_Packed_Array
(UT
))
4453 -- Otherwise if the component is not byte aligned, we know we have the
4454 -- nasty unaligned case.
4456 elsif Normalized_First_Bit
(Comp
) /= Uint_0
4457 or else Esize
(Comp
) mod System_Storage_Unit
/= Uint_0
4461 -- If we are large and byte aligned, then OK at this level
4466 end Component_May_Be_Bit_Aligned
;
4468 ----------------------------------------
4469 -- Containing_Package_With_Ext_Axioms --
4470 ----------------------------------------
4472 function Containing_Package_With_Ext_Axioms
4473 (E
: Entity_Id
) return Entity_Id
4476 -- E is the package or generic package which is externally axiomatized
4478 if Ekind_In
(E
, E_Generic_Package
, E_Package
)
4479 and then Has_Annotate_Pragma_For_External_Axiomatization
(E
)
4484 -- If E's scope is axiomatized, E is axiomatized
4486 if Present
(Scope
(E
)) then
4488 First_Ax_Parent_Scope
: constant Entity_Id
:=
4489 Containing_Package_With_Ext_Axioms
(Scope
(E
));
4491 if Present
(First_Ax_Parent_Scope
) then
4492 return First_Ax_Parent_Scope
;
4497 -- Otherwise, if E is a package instance, it is axiomatized if the
4498 -- corresponding generic package is axiomatized.
4500 if Ekind
(E
) = E_Package
then
4502 Par
: constant Node_Id
:= Parent
(E
);
4506 if Nkind
(Par
) = N_Defining_Program_Unit_Name
then
4507 Decl
:= Parent
(Par
);
4512 if Present
(Generic_Parent
(Decl
)) then
4514 Containing_Package_With_Ext_Axioms
(Generic_Parent
(Decl
));
4520 end Containing_Package_With_Ext_Axioms
;
4522 -------------------------------
4523 -- Convert_To_Actual_Subtype --
4524 -------------------------------
4526 procedure Convert_To_Actual_Subtype
(Exp
: Entity_Id
) is
4530 Act_ST
:= Get_Actual_Subtype
(Exp
);
4532 if Act_ST
= Etype
(Exp
) then
4535 Rewrite
(Exp
, Convert_To
(Act_ST
, Relocate_Node
(Exp
)));
4536 Analyze_And_Resolve
(Exp
, Act_ST
);
4538 end Convert_To_Actual_Subtype
;
4540 -----------------------------------
4541 -- Corresponding_Runtime_Package --
4542 -----------------------------------
4544 function Corresponding_Runtime_Package
(Typ
: Entity_Id
) return RTU_Id
is
4545 function Has_One_Entry_And_No_Queue
(T
: Entity_Id
) return Boolean;
4546 -- Return True if protected type T has one entry and the maximum queue
4549 --------------------------------
4550 -- Has_One_Entry_And_No_Queue --
4551 --------------------------------
4553 function Has_One_Entry_And_No_Queue
(T
: Entity_Id
) return Boolean is
4555 Is_First
: Boolean := True;
4558 Item
:= First_Entity
(T
);
4559 while Present
(Item
) loop
4560 if Is_Entry
(Item
) then
4562 -- The protected type has more than one entry
4564 if not Is_First
then
4568 -- The queue length is not one
4570 if not Restriction_Active
(No_Entry_Queue
)
4571 and then Get_Max_Queue_Length
(Item
) /= Uint_1
4583 end Has_One_Entry_And_No_Queue
;
4587 Pkg_Id
: RTU_Id
:= RTU_Null
;
4589 -- Start of processing for Corresponding_Runtime_Package
4592 pragma Assert
(Is_Concurrent_Type
(Typ
));
4594 if Ekind
(Typ
) in Protected_Kind
then
4595 if Has_Entries
(Typ
)
4597 -- A protected type without entries that covers an interface and
4598 -- overrides the abstract routines with protected procedures is
4599 -- considered equivalent to a protected type with entries in the
4600 -- context of dispatching select statements. It is sufficient to
4601 -- check for the presence of an interface list in the declaration
4602 -- node to recognize this case.
4604 or else Present
(Interface_List
(Parent
(Typ
)))
4606 -- Protected types with interrupt handlers (when not using a
4607 -- restricted profile) are also considered equivalent to
4608 -- protected types with entries. The types which are used
4609 -- (Static_Interrupt_Protection and Dynamic_Interrupt_Protection)
4610 -- are derived from Protection_Entries.
4612 or else (Has_Attach_Handler
(Typ
) and then not Restricted_Profile
)
4613 or else Has_Interrupt_Handler
(Typ
)
4616 or else Restriction_Active
(No_Select_Statements
) = False
4617 or else not Has_One_Entry_And_No_Queue
(Typ
)
4618 or else (Has_Attach_Handler
(Typ
)
4619 and then not Restricted_Profile
)
4621 Pkg_Id
:= System_Tasking_Protected_Objects_Entries
;
4623 Pkg_Id
:= System_Tasking_Protected_Objects_Single_Entry
;
4627 Pkg_Id
:= System_Tasking_Protected_Objects
;
4632 end Corresponding_Runtime_Package
;
4634 -----------------------------------
4635 -- Current_Sem_Unit_Declarations --
4636 -----------------------------------
4638 function Current_Sem_Unit_Declarations
return List_Id
is
4639 U
: Node_Id
:= Unit
(Cunit
(Current_Sem_Unit
));
4643 -- If the current unit is a package body, locate the visible
4644 -- declarations of the package spec.
4646 if Nkind
(U
) = N_Package_Body
then
4647 U
:= Unit
(Library_Unit
(Cunit
(Current_Sem_Unit
)));
4650 if Nkind
(U
) = N_Package_Declaration
then
4651 U
:= Specification
(U
);
4652 Decls
:= Visible_Declarations
(U
);
4656 Set_Visible_Declarations
(U
, Decls
);
4660 Decls
:= Declarations
(U
);
4664 Set_Declarations
(U
, Decls
);
4669 end Current_Sem_Unit_Declarations
;
4671 -----------------------
4672 -- Duplicate_Subexpr --
4673 -----------------------
4675 function Duplicate_Subexpr
4677 Name_Req
: Boolean := False;
4678 Renaming_Req
: Boolean := False) return Node_Id
4681 Remove_Side_Effects
(Exp
, Name_Req
, Renaming_Req
);
4682 return New_Copy_Tree
(Exp
);
4683 end Duplicate_Subexpr
;
4685 ---------------------------------
4686 -- Duplicate_Subexpr_No_Checks --
4687 ---------------------------------
4689 function Duplicate_Subexpr_No_Checks
4691 Name_Req
: Boolean := False;
4692 Renaming_Req
: Boolean := False;
4693 Related_Id
: Entity_Id
:= Empty
;
4694 Is_Low_Bound
: Boolean := False;
4695 Is_High_Bound
: Boolean := False) return Node_Id
4702 Name_Req
=> Name_Req
,
4703 Renaming_Req
=> Renaming_Req
,
4704 Related_Id
=> Related_Id
,
4705 Is_Low_Bound
=> Is_Low_Bound
,
4706 Is_High_Bound
=> Is_High_Bound
);
4708 New_Exp
:= New_Copy_Tree
(Exp
);
4709 Remove_Checks
(New_Exp
);
4711 end Duplicate_Subexpr_No_Checks
;
4713 -----------------------------------
4714 -- Duplicate_Subexpr_Move_Checks --
4715 -----------------------------------
4717 function Duplicate_Subexpr_Move_Checks
4719 Name_Req
: Boolean := False;
4720 Renaming_Req
: Boolean := False) return Node_Id
4725 Remove_Side_Effects
(Exp
, Name_Req
, Renaming_Req
);
4726 New_Exp
:= New_Copy_Tree
(Exp
);
4727 Remove_Checks
(Exp
);
4729 end Duplicate_Subexpr_Move_Checks
;
4731 --------------------
4732 -- Ensure_Defined --
4733 --------------------
4735 procedure Ensure_Defined
(Typ
: Entity_Id
; N
: Node_Id
) is
4739 -- An itype reference must only be created if this is a local itype, so
4740 -- that gigi can elaborate it on the proper objstack.
4742 if Is_Itype
(Typ
) and then Scope
(Typ
) = Current_Scope
then
4743 IR
:= Make_Itype_Reference
(Sloc
(N
));
4744 Set_Itype
(IR
, Typ
);
4745 Insert_Action
(N
, IR
);
4749 --------------------
4750 -- Entry_Names_OK --
4751 --------------------
4753 function Entry_Names_OK
return Boolean is
4756 not Restricted_Profile
4757 and then not Global_Discard_Names
4758 and then not Restriction_Active
(No_Implicit_Heap_Allocations
)
4759 and then not Restriction_Active
(No_Local_Allocators
);
4766 procedure Evaluate_Name
(Nam
: Node_Id
) is
4768 -- For an attribute reference or an indexed component, evaluate the
4769 -- prefix, which is itself a name, recursively, and then force the
4770 -- evaluation of all the subscripts (or attribute expressions).
4773 when N_Attribute_Reference
4774 | N_Indexed_Component
4776 Evaluate_Name
(Prefix
(Nam
));
4782 E
:= First
(Expressions
(Nam
));
4783 while Present
(E
) loop
4784 Force_Evaluation
(E
);
4786 if Original_Node
(E
) /= E
then
4788 (E
, Do_Range_Check
(Original_Node
(E
)));
4795 -- For an explicit dereference, we simply force the evaluation of
4796 -- the name expression. The dereference provides a value that is the
4797 -- address for the renamed object, and it is precisely this value
4798 -- that we want to preserve.
4800 when N_Explicit_Dereference
=>
4801 Force_Evaluation
(Prefix
(Nam
));
4803 -- For a function call, we evaluate the call
4805 when N_Function_Call
=>
4806 Force_Evaluation
(Nam
);
4808 -- For a qualified expression, we evaluate the underlying object
4809 -- name if any, otherwise we force the evaluation of the underlying
4812 when N_Qualified_Expression
=>
4813 if Is_Object_Reference
(Expression
(Nam
)) then
4814 Evaluate_Name
(Expression
(Nam
));
4816 Force_Evaluation
(Expression
(Nam
));
4819 -- For a selected component, we simply evaluate the prefix
4821 when N_Selected_Component
=>
4822 Evaluate_Name
(Prefix
(Nam
));
4824 -- For a slice, we evaluate the prefix, as for the indexed component
4825 -- case and then, if there is a range present, either directly or as
4826 -- the constraint of a discrete subtype indication, we evaluate the
4827 -- two bounds of this range.
4830 Evaluate_Name
(Prefix
(Nam
));
4831 Evaluate_Slice_Bounds
(Nam
);
4833 -- For a type conversion, the expression of the conversion must be
4834 -- the name of an object, and we simply need to evaluate this name.
4836 when N_Type_Conversion
=>
4837 Evaluate_Name
(Expression
(Nam
));
4839 -- The remaining cases are direct name, operator symbol and character
4840 -- literal. In all these cases, we do nothing, since we want to
4841 -- reevaluate each time the renamed object is used.
4848 ---------------------------
4849 -- Evaluate_Slice_Bounds --
4850 ---------------------------
4852 procedure Evaluate_Slice_Bounds
(Slice
: Node_Id
) is
4853 DR
: constant Node_Id
:= Discrete_Range
(Slice
);
4858 if Nkind
(DR
) = N_Range
then
4859 Force_Evaluation
(Low_Bound
(DR
));
4860 Force_Evaluation
(High_Bound
(DR
));
4862 elsif Nkind
(DR
) = N_Subtype_Indication
then
4863 Constr
:= Constraint
(DR
);
4865 if Nkind
(Constr
) = N_Range_Constraint
then
4866 Rexpr
:= Range_Expression
(Constr
);
4868 Force_Evaluation
(Low_Bound
(Rexpr
));
4869 Force_Evaluation
(High_Bound
(Rexpr
));
4872 end Evaluate_Slice_Bounds
;
4874 ---------------------
4875 -- Evolve_And_Then --
4876 ---------------------
4878 procedure Evolve_And_Then
(Cond
: in out Node_Id
; Cond1
: Node_Id
) is
4884 Make_And_Then
(Sloc
(Cond1
),
4886 Right_Opnd
=> Cond1
);
4888 end Evolve_And_Then
;
4890 --------------------
4891 -- Evolve_Or_Else --
4892 --------------------
4894 procedure Evolve_Or_Else
(Cond
: in out Node_Id
; Cond1
: Node_Id
) is
4900 Make_Or_Else
(Sloc
(Cond1
),
4902 Right_Opnd
=> Cond1
);
4906 -----------------------------------
4907 -- Exceptions_In_Finalization_OK --
4908 -----------------------------------
4910 function Exceptions_In_Finalization_OK
return Boolean is
4913 not (Restriction_Active
(No_Exception_Handlers
) or else
4914 Restriction_Active
(No_Exception_Propagation
) or else
4915 Restriction_Active
(No_Exceptions
));
4916 end Exceptions_In_Finalization_OK
;
4918 -----------------------------------------
4919 -- Expand_Static_Predicates_In_Choices --
4920 -----------------------------------------
4922 procedure Expand_Static_Predicates_In_Choices
(N
: Node_Id
) is
4923 pragma Assert
(Nkind_In
(N
, N_Case_Statement_Alternative
, N_Variant
));
4925 Choices
: constant List_Id
:= Discrete_Choices
(N
);
4933 Choice
:= First
(Choices
);
4934 while Present
(Choice
) loop
4935 Next_C
:= Next
(Choice
);
4937 -- Check for name of subtype with static predicate
4939 if Is_Entity_Name
(Choice
)
4940 and then Is_Type
(Entity
(Choice
))
4941 and then Has_Predicates
(Entity
(Choice
))
4943 -- Loop through entries in predicate list, converting to choices
4944 -- and inserting in the list before the current choice. Note that
4945 -- if the list is empty, corresponding to a False predicate, then
4946 -- no choices are inserted.
4948 P
:= First
(Static_Discrete_Predicate
(Entity
(Choice
)));
4949 while Present
(P
) loop
4951 -- If low bound and high bounds are equal, copy simple choice
4953 if Expr_Value
(Low_Bound
(P
)) = Expr_Value
(High_Bound
(P
)) then
4954 C
:= New_Copy
(Low_Bound
(P
));
4956 -- Otherwise copy a range
4962 -- Change Sloc to referencing choice (rather than the Sloc of
4963 -- the predicate declaration element itself).
4965 Set_Sloc
(C
, Sloc
(Choice
));
4966 Insert_Before
(Choice
, C
);
4970 -- Delete the predicated entry
4975 -- Move to next choice to check
4979 end Expand_Static_Predicates_In_Choices
;
4981 ------------------------------
4982 -- Expand_Subtype_From_Expr --
4983 ------------------------------
4985 -- This function is applicable for both static and dynamic allocation of
4986 -- objects which are constrained by an initial expression. Basically it
4987 -- transforms an unconstrained subtype indication into a constrained one.
4989 -- The expression may also be transformed in certain cases in order to
4990 -- avoid multiple evaluation. In the static allocation case, the general
4995 -- is transformed into
4997 -- Val : Constrained_Subtype_Of_T := Maybe_Modified_Expr;
4999 -- Here are the main cases :
5001 -- <if Expr is a Slice>
5002 -- Val : T ([Index_Subtype (Expr)]) := Expr;
5004 -- <elsif Expr is a String Literal>
5005 -- Val : T (T'First .. T'First + Length (string literal) - 1) := Expr;
5007 -- <elsif Expr is Constrained>
5008 -- subtype T is Type_Of_Expr
5011 -- <elsif Expr is an entity_name>
5012 -- Val : T (constraints taken from Expr) := Expr;
5015 -- type Axxx is access all T;
5016 -- Rval : Axxx := Expr'ref;
5017 -- Val : T (constraints taken from Rval) := Rval.all;
5019 -- ??? note: when the Expression is allocated in the secondary stack
5020 -- we could use it directly instead of copying it by declaring
5021 -- Val : T (...) renames Rval.all
5023 procedure Expand_Subtype_From_Expr
5025 Unc_Type
: Entity_Id
;
5026 Subtype_Indic
: Node_Id
;
5028 Related_Id
: Entity_Id
:= Empty
)
5030 Loc
: constant Source_Ptr
:= Sloc
(N
);
5031 Exp_Typ
: constant Entity_Id
:= Etype
(Exp
);
5035 -- In general we cannot build the subtype if expansion is disabled,
5036 -- because internal entities may not have been defined. However, to
5037 -- avoid some cascaded errors, we try to continue when the expression is
5038 -- an array (or string), because it is safe to compute the bounds. It is
5039 -- in fact required to do so even in a generic context, because there
5040 -- may be constants that depend on the bounds of a string literal, both
5041 -- standard string types and more generally arrays of characters.
5043 -- In GNATprove mode, these extra subtypes are not needed
5045 if GNATprove_Mode
then
5049 if not Expander_Active
5050 and then (No
(Etype
(Exp
)) or else not Is_String_Type
(Etype
(Exp
)))
5055 if Nkind
(Exp
) = N_Slice
then
5057 Slice_Type
: constant Entity_Id
:= Etype
(First_Index
(Exp_Typ
));
5060 Rewrite
(Subtype_Indic
,
5061 Make_Subtype_Indication
(Loc
,
5062 Subtype_Mark
=> New_Occurrence_Of
(Unc_Type
, Loc
),
5064 Make_Index_Or_Discriminant_Constraint
(Loc
,
5065 Constraints
=> New_List
5066 (New_Occurrence_Of
(Slice_Type
, Loc
)))));
5068 -- This subtype indication may be used later for constraint checks
5069 -- we better make sure that if a variable was used as a bound of
5070 -- of the original slice, its value is frozen.
5072 Evaluate_Slice_Bounds
(Exp
);
5075 elsif Ekind
(Exp_Typ
) = E_String_Literal_Subtype
then
5076 Rewrite
(Subtype_Indic
,
5077 Make_Subtype_Indication
(Loc
,
5078 Subtype_Mark
=> New_Occurrence_Of
(Unc_Type
, Loc
),
5080 Make_Index_Or_Discriminant_Constraint
(Loc
,
5081 Constraints
=> New_List
(
5082 Make_Literal_Range
(Loc
,
5083 Literal_Typ
=> Exp_Typ
)))));
5085 -- If the type of the expression is an internally generated type it
5086 -- may not be necessary to create a new subtype. However there are two
5087 -- exceptions: references to the current instances, and aliased array
5088 -- object declarations for which the back end has to create a template.
5090 elsif Is_Constrained
(Exp_Typ
)
5091 and then not Is_Class_Wide_Type
(Unc_Type
)
5093 (Nkind
(N
) /= N_Object_Declaration
5094 or else not Is_Entity_Name
(Expression
(N
))
5095 or else not Comes_From_Source
(Entity
(Expression
(N
)))
5096 or else not Is_Array_Type
(Exp_Typ
)
5097 or else not Aliased_Present
(N
))
5099 if Is_Itype
(Exp_Typ
) then
5101 -- Within an initialization procedure, a selected component
5102 -- denotes a component of the enclosing record, and it appears as
5103 -- an actual in a call to its own initialization procedure. If
5104 -- this component depends on the outer discriminant, we must
5105 -- generate the proper actual subtype for it.
5107 if Nkind
(Exp
) = N_Selected_Component
5108 and then Within_Init_Proc
5111 Decl
: constant Node_Id
:=
5112 Build_Actual_Subtype_Of_Component
(Exp_Typ
, Exp
);
5114 if Present
(Decl
) then
5115 Insert_Action
(N
, Decl
);
5116 T
:= Defining_Identifier
(Decl
);
5122 -- No need to generate a new subtype
5129 T
:= Make_Temporary
(Loc
, 'T');
5132 Make_Subtype_Declaration
(Loc
,
5133 Defining_Identifier
=> T
,
5134 Subtype_Indication
=> New_Occurrence_Of
(Exp_Typ
, Loc
)));
5136 -- This type is marked as an itype even though it has an explicit
5137 -- declaration since otherwise Is_Generic_Actual_Type can get
5138 -- set, resulting in the generation of spurious errors. (See
5139 -- sem_ch8.Analyze_Package_Renaming and sem_type.covers)
5142 Set_Associated_Node_For_Itype
(T
, Exp
);
5145 Rewrite
(Subtype_Indic
, New_Occurrence_Of
(T
, Loc
));
5147 -- Nothing needs to be done for private types with unknown discriminants
5148 -- if the underlying type is not an unconstrained composite type or it
5149 -- is an unchecked union.
5151 elsif Is_Private_Type
(Unc_Type
)
5152 and then Has_Unknown_Discriminants
(Unc_Type
)
5153 and then (not Is_Composite_Type
(Underlying_Type
(Unc_Type
))
5154 or else Is_Constrained
(Underlying_Type
(Unc_Type
))
5155 or else Is_Unchecked_Union
(Underlying_Type
(Unc_Type
)))
5159 -- Case of derived type with unknown discriminants where the parent type
5160 -- also has unknown discriminants.
5162 elsif Is_Record_Type
(Unc_Type
)
5163 and then not Is_Class_Wide_Type
(Unc_Type
)
5164 and then Has_Unknown_Discriminants
(Unc_Type
)
5165 and then Has_Unknown_Discriminants
(Underlying_Type
(Unc_Type
))
5167 -- Nothing to be done if no underlying record view available
5169 -- If this is a limited type derived from a type with unknown
5170 -- discriminants, do not expand either, so that subsequent expansion
5171 -- of the call can add build-in-place parameters to call.
5173 if No
(Underlying_Record_View
(Unc_Type
))
5174 or else Is_Limited_Type
(Unc_Type
)
5178 -- Otherwise use the Underlying_Record_View to create the proper
5179 -- constrained subtype for an object of a derived type with unknown
5183 Remove_Side_Effects
(Exp
);
5184 Rewrite
(Subtype_Indic
,
5185 Make_Subtype_From_Expr
(Exp
, Underlying_Record_View
(Unc_Type
)));
5188 -- Renamings of class-wide interface types require no equivalent
5189 -- constrained type declarations because we only need to reference
5190 -- the tag component associated with the interface. The same is
5191 -- presumably true for class-wide types in general, so this test
5192 -- is broadened to include all class-wide renamings, which also
5193 -- avoids cases of unbounded recursion in Remove_Side_Effects.
5194 -- (Is this really correct, or are there some cases of class-wide
5195 -- renamings that require action in this procedure???)
5198 and then Nkind
(N
) = N_Object_Renaming_Declaration
5199 and then Is_Class_Wide_Type
(Unc_Type
)
5203 -- In Ada 95 nothing to be done if the type of the expression is limited
5204 -- because in this case the expression cannot be copied, and its use can
5205 -- only be by reference.
5207 -- In Ada 2005 the context can be an object declaration whose expression
5208 -- is a function that returns in place. If the nominal subtype has
5209 -- unknown discriminants, the call still provides constraints on the
5210 -- object, and we have to create an actual subtype from it.
5212 -- If the type is class-wide, the expression is dynamically tagged and
5213 -- we do not create an actual subtype either. Ditto for an interface.
5214 -- For now this applies only if the type is immutably limited, and the
5215 -- function being called is build-in-place. This will have to be revised
5216 -- when build-in-place functions are generalized to other types.
5218 elsif Is_Limited_View
(Exp_Typ
)
5220 (Is_Class_Wide_Type
(Exp_Typ
)
5221 or else Is_Interface
(Exp_Typ
)
5222 or else not Has_Unknown_Discriminants
(Exp_Typ
)
5223 or else not Is_Composite_Type
(Unc_Type
))
5227 -- For limited objects initialized with build in place function calls,
5228 -- nothing to be done; otherwise we prematurely introduce an N_Reference
5229 -- node in the expression initializing the object, which breaks the
5230 -- circuitry that detects and adds the additional arguments to the
5233 elsif Is_Build_In_Place_Function_Call
(Exp
) then
5237 Remove_Side_Effects
(Exp
);
5238 Rewrite
(Subtype_Indic
,
5239 Make_Subtype_From_Expr
(Exp
, Unc_Type
, Related_Id
));
5241 end Expand_Subtype_From_Expr
;
5243 ---------------------------------------------
5244 -- Expression_Contains_Primitives_Calls_Of --
5245 ---------------------------------------------
5247 function Expression_Contains_Primitives_Calls_Of
5249 Typ
: Entity_Id
) return Boolean
5251 U_Typ
: constant Entity_Id
:= Unique_Entity
(Typ
);
5253 Calls_OK
: Boolean := False;
5254 -- This flag is set to True when expression Expr contains at least one
5255 -- call to a nondispatching primitive function of Typ.
5257 function Search_Primitive_Calls
(N
: Node_Id
) return Traverse_Result
;
5258 -- Search for nondispatching calls to primitive functions of type Typ
5260 ----------------------------
5261 -- Search_Primitive_Calls --
5262 ----------------------------
5264 function Search_Primitive_Calls
(N
: Node_Id
) return Traverse_Result
is
5265 Disp_Typ
: Entity_Id
;
5269 -- Detect a function call that could denote a nondispatching
5270 -- primitive of the input type.
5272 if Nkind
(N
) = N_Function_Call
5273 and then Is_Entity_Name
(Name
(N
))
5275 Subp
:= Entity
(Name
(N
));
5277 -- Do not consider function calls with a controlling argument, as
5278 -- those are always dispatching calls.
5280 if Is_Dispatching_Operation
(Subp
)
5281 and then No
(Controlling_Argument
(N
))
5283 Disp_Typ
:= Find_Dispatching_Type
(Subp
);
5285 -- To qualify as a suitable primitive, the dispatching type of
5286 -- the function must be the input type.
5288 if Present
(Disp_Typ
)
5289 and then Unique_Entity
(Disp_Typ
) = U_Typ
5293 -- There is no need to continue the traversal, as one such
5302 end Search_Primitive_Calls
;
5304 procedure Search_Calls
is new Traverse_Proc
(Search_Primitive_Calls
);
5306 -- Start of processing for Expression_Contains_Primitives_Calls_Of_Type
5309 Search_Calls
(Expr
);
5311 end Expression_Contains_Primitives_Calls_Of
;
5313 ----------------------
5314 -- Finalize_Address --
5315 ----------------------
5317 function Finalize_Address
(Typ
: Entity_Id
) return Entity_Id
is
5318 Utyp
: Entity_Id
:= Typ
;
5321 -- Handle protected class-wide or task class-wide types
5323 if Is_Class_Wide_Type
(Utyp
) then
5324 if Is_Concurrent_Type
(Root_Type
(Utyp
)) then
5325 Utyp
:= Root_Type
(Utyp
);
5327 elsif Is_Private_Type
(Root_Type
(Utyp
))
5328 and then Present
(Full_View
(Root_Type
(Utyp
)))
5329 and then Is_Concurrent_Type
(Full_View
(Root_Type
(Utyp
)))
5331 Utyp
:= Full_View
(Root_Type
(Utyp
));
5335 -- Handle private types
5337 if Is_Private_Type
(Utyp
) and then Present
(Full_View
(Utyp
)) then
5338 Utyp
:= Full_View
(Utyp
);
5341 -- Handle protected and task types
5343 if Is_Concurrent_Type
(Utyp
)
5344 and then Present
(Corresponding_Record_Type
(Utyp
))
5346 Utyp
:= Corresponding_Record_Type
(Utyp
);
5349 Utyp
:= Underlying_Type
(Base_Type
(Utyp
));
5351 -- Deal with untagged derivation of private views. If the parent is
5352 -- now known to be protected, the finalization routine is the one
5353 -- defined on the corresponding record of the ancestor (corresponding
5354 -- records do not automatically inherit operations, but maybe they
5357 if Is_Untagged_Derivation
(Typ
) then
5358 if Is_Protected_Type
(Typ
) then
5359 Utyp
:= Corresponding_Record_Type
(Root_Type
(Base_Type
(Typ
)));
5362 Utyp
:= Underlying_Type
(Root_Type
(Base_Type
(Typ
)));
5364 if Is_Protected_Type
(Utyp
) then
5365 Utyp
:= Corresponding_Record_Type
(Utyp
);
5370 -- If the underlying_type is a subtype, we are dealing with the
5371 -- completion of a private type. We need to access the base type and
5372 -- generate a conversion to it.
5374 if Utyp
/= Base_Type
(Utyp
) then
5375 pragma Assert
(Is_Private_Type
(Typ
));
5377 Utyp
:= Base_Type
(Utyp
);
5380 -- When dealing with an internally built full view for a type with
5381 -- unknown discriminants, use the original record type.
5383 if Is_Underlying_Record_View
(Utyp
) then
5384 Utyp
:= Etype
(Utyp
);
5387 return TSS
(Utyp
, TSS_Finalize_Address
);
5388 end Finalize_Address
;
5390 ------------------------
5391 -- Find_Interface_ADT --
5392 ------------------------
5394 function Find_Interface_ADT
5396 Iface
: Entity_Id
) return Elmt_Id
5399 Typ
: Entity_Id
:= T
;
5402 pragma Assert
(Is_Interface
(Iface
));
5404 -- Handle private types
5406 if Has_Private_Declaration
(Typ
) and then Present
(Full_View
(Typ
)) then
5407 Typ
:= Full_View
(Typ
);
5410 -- Handle access types
5412 if Is_Access_Type
(Typ
) then
5413 Typ
:= Designated_Type
(Typ
);
5416 -- Handle task and protected types implementing interfaces
5418 if Is_Concurrent_Type
(Typ
) then
5419 Typ
:= Corresponding_Record_Type
(Typ
);
5423 (not Is_Class_Wide_Type
(Typ
)
5424 and then Ekind
(Typ
) /= E_Incomplete_Type
);
5426 if Is_Ancestor
(Iface
, Typ
, Use_Full_View
=> True) then
5427 return First_Elmt
(Access_Disp_Table
(Typ
));
5430 ADT
:= Next_Elmt
(Next_Elmt
(First_Elmt
(Access_Disp_Table
(Typ
))));
5432 and then Present
(Related_Type
(Node
(ADT
)))
5433 and then Related_Type
(Node
(ADT
)) /= Iface
5434 and then not Is_Ancestor
(Iface
, Related_Type
(Node
(ADT
)),
5435 Use_Full_View
=> True)
5440 pragma Assert
(Present
(Related_Type
(Node
(ADT
))));
5443 end Find_Interface_ADT
;
5445 ------------------------
5446 -- Find_Interface_Tag --
5447 ------------------------
5449 function Find_Interface_Tag
5451 Iface
: Entity_Id
) return Entity_Id
5453 AI_Tag
: Entity_Id
:= Empty
;
5454 Found
: Boolean := False;
5455 Typ
: Entity_Id
:= T
;
5457 procedure Find_Tag
(Typ
: Entity_Id
);
5458 -- Internal subprogram used to recursively climb to the ancestors
5464 procedure Find_Tag
(Typ
: Entity_Id
) is
5469 -- This routine does not handle the case in which the interface is an
5470 -- ancestor of Typ. That case is handled by the enclosing subprogram.
5472 pragma Assert
(Typ
/= Iface
);
5474 -- Climb to the root type handling private types
5476 if Present
(Full_View
(Etype
(Typ
))) then
5477 if Full_View
(Etype
(Typ
)) /= Typ
then
5478 Find_Tag
(Full_View
(Etype
(Typ
)));
5481 elsif Etype
(Typ
) /= Typ
then
5482 Find_Tag
(Etype
(Typ
));
5485 -- Traverse the list of interfaces implemented by the type
5488 and then Present
(Interfaces
(Typ
))
5489 and then not (Is_Empty_Elmt_List
(Interfaces
(Typ
)))
5491 -- Skip the tag associated with the primary table
5493 pragma Assert
(Etype
(First_Tag_Component
(Typ
)) = RTE
(RE_Tag
));
5494 AI_Tag
:= Next_Tag_Component
(First_Tag_Component
(Typ
));
5495 pragma Assert
(Present
(AI_Tag
));
5497 AI_Elmt
:= First_Elmt
(Interfaces
(Typ
));
5498 while Present
(AI_Elmt
) loop
5499 AI
:= Node
(AI_Elmt
);
5502 or else Is_Ancestor
(Iface
, AI
, Use_Full_View
=> True)
5508 AI_Tag
:= Next_Tag_Component
(AI_Tag
);
5509 Next_Elmt
(AI_Elmt
);
5514 -- Start of processing for Find_Interface_Tag
5517 pragma Assert
(Is_Interface
(Iface
));
5519 -- Handle access types
5521 if Is_Access_Type
(Typ
) then
5522 Typ
:= Designated_Type
(Typ
);
5525 -- Handle class-wide types
5527 if Is_Class_Wide_Type
(Typ
) then
5528 Typ
:= Root_Type
(Typ
);
5531 -- Handle private types
5533 if Has_Private_Declaration
(Typ
) and then Present
(Full_View
(Typ
)) then
5534 Typ
:= Full_View
(Typ
);
5537 -- Handle entities from the limited view
5539 if Ekind
(Typ
) = E_Incomplete_Type
then
5540 pragma Assert
(Present
(Non_Limited_View
(Typ
)));
5541 Typ
:= Non_Limited_View
(Typ
);
5544 -- Handle task and protected types implementing interfaces
5546 if Is_Concurrent_Type
(Typ
) then
5547 Typ
:= Corresponding_Record_Type
(Typ
);
5550 -- If the interface is an ancestor of the type, then it shared the
5551 -- primary dispatch table.
5553 if Is_Ancestor
(Iface
, Typ
, Use_Full_View
=> True) then
5554 pragma Assert
(Etype
(First_Tag_Component
(Typ
)) = RTE
(RE_Tag
));
5555 return First_Tag_Component
(Typ
);
5557 -- Otherwise we need to search for its associated tag component
5561 pragma Assert
(Found
);
5564 end Find_Interface_Tag
;
5566 ---------------------------
5567 -- Find_Optional_Prim_Op --
5568 ---------------------------
5570 function Find_Optional_Prim_Op
5571 (T
: Entity_Id
; Name
: Name_Id
) return Entity_Id
5574 Typ
: Entity_Id
:= T
;
5578 if Is_Class_Wide_Type
(Typ
) then
5579 Typ
:= Root_Type
(Typ
);
5582 Typ
:= Underlying_Type
(Typ
);
5584 -- Loop through primitive operations
5586 Prim
:= First_Elmt
(Primitive_Operations
(Typ
));
5587 while Present
(Prim
) loop
5590 -- We can retrieve primitive operations by name if it is an internal
5591 -- name. For equality we must check that both of its operands have
5592 -- the same type, to avoid confusion with user-defined equalities
5593 -- than may have a non-symmetric signature.
5595 exit when Chars
(Op
) = Name
5598 or else Etype
(First_Formal
(Op
)) = Etype
(Last_Formal
(Op
)));
5603 return Node
(Prim
); -- Empty if not found
5604 end Find_Optional_Prim_Op
;
5606 ---------------------------
5607 -- Find_Optional_Prim_Op --
5608 ---------------------------
5610 function Find_Optional_Prim_Op
5612 Name
: TSS_Name_Type
) return Entity_Id
5614 Inher_Op
: Entity_Id
:= Empty
;
5615 Own_Op
: Entity_Id
:= Empty
;
5616 Prim_Elmt
: Elmt_Id
;
5617 Prim_Id
: Entity_Id
;
5618 Typ
: Entity_Id
:= T
;
5621 if Is_Class_Wide_Type
(Typ
) then
5622 Typ
:= Root_Type
(Typ
);
5625 Typ
:= Underlying_Type
(Typ
);
5627 -- This search is based on the assertion that the dispatching version
5628 -- of the TSS routine always precedes the real primitive.
5630 Prim_Elmt
:= First_Elmt
(Primitive_Operations
(Typ
));
5631 while Present
(Prim_Elmt
) loop
5632 Prim_Id
:= Node
(Prim_Elmt
);
5634 if Is_TSS
(Prim_Id
, Name
) then
5635 if Present
(Alias
(Prim_Id
)) then
5636 Inher_Op
:= Prim_Id
;
5642 Next_Elmt
(Prim_Elmt
);
5645 if Present
(Own_Op
) then
5647 elsif Present
(Inher_Op
) then
5652 end Find_Optional_Prim_Op
;
5658 function Find_Prim_Op
5659 (T
: Entity_Id
; Name
: Name_Id
) return Entity_Id
5661 Result
: constant Entity_Id
:= Find_Optional_Prim_Op
(T
, Name
);
5664 raise Program_Error
;
5674 function Find_Prim_Op
5676 Name
: TSS_Name_Type
) return Entity_Id
5678 Result
: constant Entity_Id
:= Find_Optional_Prim_Op
(T
, Name
);
5681 raise Program_Error
;
5687 ----------------------------
5688 -- Find_Protection_Object --
5689 ----------------------------
5691 function Find_Protection_Object
(Scop
: Entity_Id
) return Entity_Id
is
5696 while Present
(S
) loop
5697 if Ekind_In
(S
, E_Entry
, E_Entry_Family
, E_Function
, E_Procedure
)
5698 and then Present
(Protection_Object
(S
))
5700 return Protection_Object
(S
);
5706 -- If we do not find a Protection object in the scope chain, then
5707 -- something has gone wrong, most likely the object was never created.
5709 raise Program_Error
;
5710 end Find_Protection_Object
;
5712 --------------------------
5713 -- Find_Protection_Type --
5714 --------------------------
5716 function Find_Protection_Type
(Conc_Typ
: Entity_Id
) return Entity_Id
is
5718 Typ
: Entity_Id
:= Conc_Typ
;
5721 if Is_Concurrent_Type
(Typ
) then
5722 Typ
:= Corresponding_Record_Type
(Typ
);
5725 -- Since restriction violations are not considered serious errors, the
5726 -- expander remains active, but may leave the corresponding record type
5727 -- malformed. In such cases, component _object is not available so do
5730 if not Analyzed
(Typ
) then
5734 Comp
:= First_Component
(Typ
);
5735 while Present
(Comp
) loop
5736 if Chars
(Comp
) = Name_uObject
then
5737 return Base_Type
(Etype
(Comp
));
5740 Next_Component
(Comp
);
5743 -- The corresponding record of a protected type should always have an
5746 raise Program_Error
;
5747 end Find_Protection_Type
;
5749 -----------------------
5750 -- Find_Hook_Context --
5751 -----------------------
5753 function Find_Hook_Context
(N
: Node_Id
) return Node_Id
is
5757 Wrapped_Node
: Node_Id
;
5758 -- Note: if we are in a transient scope, we want to reuse it as
5759 -- the context for actions insertion, if possible. But if N is itself
5760 -- part of the stored actions for the current transient scope,
5761 -- then we need to insert at the appropriate (inner) location in
5762 -- the not as an action on Node_To_Be_Wrapped.
5764 In_Cond_Expr
: constant Boolean := Within_Case_Or_If_Expression
(N
);
5767 -- When the node is inside a case/if expression, the lifetime of any
5768 -- temporary controlled object is extended. Find a suitable insertion
5769 -- node by locating the topmost case or if expressions.
5771 if In_Cond_Expr
then
5774 while Present
(Par
) loop
5775 if Nkind_In
(Original_Node
(Par
), N_Case_Expression
,
5780 -- Prevent the search from going too far
5782 elsif Is_Body_Or_Package_Declaration
(Par
) then
5786 Par
:= Parent
(Par
);
5789 -- The topmost case or if expression is now recovered, but it may
5790 -- still not be the correct place to add generated code. Climb to
5791 -- find a parent that is part of a declarative or statement list,
5792 -- and is not a list of actuals in a call.
5795 while Present
(Par
) loop
5796 if Is_List_Member
(Par
)
5797 and then not Nkind_In
(Par
, N_Component_Association
,
5798 N_Discriminant_Association
,
5799 N_Parameter_Association
,
5800 N_Pragma_Argument_Association
)
5801 and then not Nkind_In
(Parent
(Par
), N_Function_Call
,
5802 N_Procedure_Call_Statement
,
5803 N_Entry_Call_Statement
)
5808 -- Prevent the search from going too far
5810 elsif Is_Body_Or_Package_Declaration
(Par
) then
5814 Par
:= Parent
(Par
);
5821 while Present
(Par
) loop
5823 -- Keep climbing past various operators
5825 if Nkind
(Parent
(Par
)) in N_Op
5826 or else Nkind_In
(Parent
(Par
), N_And_Then
, N_Or_Else
)
5828 Par
:= Parent
(Par
);
5836 -- The node may be located in a pragma in which case return the
5839 -- pragma Precondition (... and then Ctrl_Func_Call ...);
5841 -- Similar case occurs when the node is related to an object
5842 -- declaration or assignment:
5844 -- Obj [: Some_Typ] := ... and then Ctrl_Func_Call ...;
5846 -- Another case to consider is when the node is part of a return
5849 -- return ... and then Ctrl_Func_Call ...;
5851 -- Another case is when the node acts as a formal in a procedure
5854 -- Proc (... and then Ctrl_Func_Call ...);
5856 if Scope_Is_Transient
then
5857 Wrapped_Node
:= Node_To_Be_Wrapped
;
5859 Wrapped_Node
:= Empty
;
5862 while Present
(Par
) loop
5863 if Par
= Wrapped_Node
5864 or else Nkind_In
(Par
, N_Assignment_Statement
,
5865 N_Object_Declaration
,
5867 N_Procedure_Call_Statement
,
5868 N_Simple_Return_Statement
)
5872 -- Prevent the search from going too far
5874 elsif Is_Body_Or_Package_Declaration
(Par
) then
5878 Par
:= Parent
(Par
);
5881 -- Return the topmost short circuit operator
5885 end Find_Hook_Context
;
5887 ------------------------------
5888 -- Following_Address_Clause --
5889 ------------------------------
5891 function Following_Address_Clause
(D
: Node_Id
) return Node_Id
is
5892 Id
: constant Entity_Id
:= Defining_Identifier
(D
);
5896 function Check_Decls
(D
: Node_Id
) return Node_Id
;
5897 -- This internal function differs from the main function in that it
5898 -- gets called to deal with a following package private part, and
5899 -- it checks declarations starting with D (the main function checks
5900 -- declarations following D). If D is Empty, then Empty is returned.
5906 function Check_Decls
(D
: Node_Id
) return Node_Id
is
5911 while Present
(Decl
) loop
5912 if Nkind
(Decl
) = N_At_Clause
5913 and then Chars
(Identifier
(Decl
)) = Chars
(Id
)
5917 elsif Nkind
(Decl
) = N_Attribute_Definition_Clause
5918 and then Chars
(Decl
) = Name_Address
5919 and then Chars
(Name
(Decl
)) = Chars
(Id
)
5927 -- Otherwise not found, return Empty
5932 -- Start of processing for Following_Address_Clause
5935 -- If parser detected no address clause for the identifier in question,
5936 -- then the answer is a quick NO, without the need for a search.
5938 if not Get_Name_Table_Boolean1
(Chars
(Id
)) then
5942 -- Otherwise search current declarative unit
5944 Result
:= Check_Decls
(Next
(D
));
5946 if Present
(Result
) then
5950 -- Check for possible package private part following
5954 if Nkind
(Par
) = N_Package_Specification
5955 and then Visible_Declarations
(Par
) = List_Containing
(D
)
5956 and then Present
(Private_Declarations
(Par
))
5958 -- Private part present, check declarations there
5960 return Check_Decls
(First
(Private_Declarations
(Par
)));
5963 -- No private part, clause not found, return Empty
5967 end Following_Address_Clause
;
5969 ----------------------
5970 -- Force_Evaluation --
5971 ----------------------
5973 procedure Force_Evaluation
5975 Name_Req
: Boolean := False;
5976 Related_Id
: Entity_Id
:= Empty
;
5977 Is_Low_Bound
: Boolean := False;
5978 Is_High_Bound
: Boolean := False;
5979 Mode
: Force_Evaluation_Mode
:= Relaxed
)
5984 Name_Req
=> Name_Req
,
5985 Variable_Ref
=> True,
5986 Renaming_Req
=> False,
5987 Related_Id
=> Related_Id
,
5988 Is_Low_Bound
=> Is_Low_Bound
,
5989 Is_High_Bound
=> Is_High_Bound
,
5990 Check_Side_Effects
=>
5991 Is_Static_Expression
(Exp
)
5992 or else Mode
= Relaxed
);
5993 end Force_Evaluation
;
5995 ---------------------------------
5996 -- Fully_Qualified_Name_String --
5997 ---------------------------------
5999 function Fully_Qualified_Name_String
6001 Append_NUL
: Boolean := True) return String_Id
6003 procedure Internal_Full_Qualified_Name
(E
: Entity_Id
);
6004 -- Compute recursively the qualified name without NUL at the end, adding
6005 -- it to the currently started string being generated
6007 ----------------------------------
6008 -- Internal_Full_Qualified_Name --
6009 ----------------------------------
6011 procedure Internal_Full_Qualified_Name
(E
: Entity_Id
) is
6015 -- Deal properly with child units
6017 if Nkind
(E
) = N_Defining_Program_Unit_Name
then
6018 Ent
:= Defining_Identifier
(E
);
6023 -- Compute qualification recursively (only "Standard" has no scope)
6025 if Present
(Scope
(Scope
(Ent
))) then
6026 Internal_Full_Qualified_Name
(Scope
(Ent
));
6027 Store_String_Char
(Get_Char_Code
('.'));
6030 -- Every entity should have a name except some expanded blocks
6031 -- don't bother about those.
6033 if Chars
(Ent
) = No_Name
then
6037 -- Generates the entity name in upper case
6039 Get_Decoded_Name_String
(Chars
(Ent
));
6041 Store_String_Chars
(Name_Buffer
(1 .. Name_Len
));
6043 end Internal_Full_Qualified_Name
;
6045 -- Start of processing for Full_Qualified_Name
6049 Internal_Full_Qualified_Name
(E
);
6052 Store_String_Char
(Get_Char_Code
(ASCII
.NUL
));
6056 end Fully_Qualified_Name_String
;
6058 ------------------------
6059 -- Generate_Poll_Call --
6060 ------------------------
6062 procedure Generate_Poll_Call
(N
: Node_Id
) is
6064 -- No poll call if polling not active
6066 if not Polling_Required
then
6069 -- Otherwise generate require poll call
6072 Insert_Before_And_Analyze
(N
,
6073 Make_Procedure_Call_Statement
(Sloc
(N
),
6074 Name
=> New_Occurrence_Of
(RTE
(RE_Poll
), Sloc
(N
))));
6076 end Generate_Poll_Call
;
6078 ---------------------------------
6079 -- Get_Current_Value_Condition --
6080 ---------------------------------
6082 -- Note: the implementation of this procedure is very closely tied to the
6083 -- implementation of Set_Current_Value_Condition. In the Get procedure, we
6084 -- interpret Current_Value fields set by the Set procedure, so the two
6085 -- procedures need to be closely coordinated.
6087 procedure Get_Current_Value_Condition
6092 Loc
: constant Source_Ptr
:= Sloc
(Var
);
6093 Ent
: constant Entity_Id
:= Entity
(Var
);
6095 procedure Process_Current_Value_Condition
6098 -- N is an expression which holds either True (S = True) or False (S =
6099 -- False) in the condition. This procedure digs out the expression and
6100 -- if it refers to Ent, sets Op and Val appropriately.
6102 -------------------------------------
6103 -- Process_Current_Value_Condition --
6104 -------------------------------------
6106 procedure Process_Current_Value_Condition
6111 Prev_Cond
: Node_Id
;
6121 -- Deal with NOT operators, inverting sense
6123 while Nkind
(Cond
) = N_Op_Not
loop
6124 Cond
:= Right_Opnd
(Cond
);
6128 -- Deal with conversions, qualifications, and expressions with
6131 while Nkind_In
(Cond
,
6133 N_Qualified_Expression
,
6134 N_Expression_With_Actions
)
6136 Cond
:= Expression
(Cond
);
6139 exit when Cond
= Prev_Cond
;
6142 -- Deal with AND THEN and AND cases
6144 if Nkind_In
(Cond
, N_And_Then
, N_Op_And
) then
6146 -- Don't ever try to invert a condition that is of the form of an
6147 -- AND or AND THEN (since we are not doing sufficiently general
6148 -- processing to allow this).
6150 if Sens
= False then
6156 -- Recursively process AND and AND THEN branches
6158 Process_Current_Value_Condition
(Left_Opnd
(Cond
), True);
6160 if Op
/= N_Empty
then
6164 Process_Current_Value_Condition
(Right_Opnd
(Cond
), True);
6167 -- Case of relational operator
6169 elsif Nkind
(Cond
) in N_Op_Compare
then
6172 -- Invert sense of test if inverted test
6174 if Sens
= False then
6176 when N_Op_Eq
=> Op
:= N_Op_Ne
;
6177 when N_Op_Ne
=> Op
:= N_Op_Eq
;
6178 when N_Op_Lt
=> Op
:= N_Op_Ge
;
6179 when N_Op_Gt
=> Op
:= N_Op_Le
;
6180 when N_Op_Le
=> Op
:= N_Op_Gt
;
6181 when N_Op_Ge
=> Op
:= N_Op_Lt
;
6182 when others => raise Program_Error
;
6186 -- Case of entity op value
6188 if Is_Entity_Name
(Left_Opnd
(Cond
))
6189 and then Ent
= Entity
(Left_Opnd
(Cond
))
6190 and then Compile_Time_Known_Value
(Right_Opnd
(Cond
))
6192 Val
:= Right_Opnd
(Cond
);
6194 -- Case of value op entity
6196 elsif Is_Entity_Name
(Right_Opnd
(Cond
))
6197 and then Ent
= Entity
(Right_Opnd
(Cond
))
6198 and then Compile_Time_Known_Value
(Left_Opnd
(Cond
))
6200 Val
:= Left_Opnd
(Cond
);
6202 -- We are effectively swapping operands
6205 when N_Op_Eq
=> null;
6206 when N_Op_Ne
=> null;
6207 when N_Op_Lt
=> Op
:= N_Op_Gt
;
6208 when N_Op_Gt
=> Op
:= N_Op_Lt
;
6209 when N_Op_Le
=> Op
:= N_Op_Ge
;
6210 when N_Op_Ge
=> Op
:= N_Op_Le
;
6211 when others => raise Program_Error
;
6220 elsif Nkind_In
(Cond
,
6222 N_Qualified_Expression
,
6223 N_Expression_With_Actions
)
6225 Cond
:= Expression
(Cond
);
6227 -- Case of Boolean variable reference, return as though the
6228 -- reference had said var = True.
6231 if Is_Entity_Name
(Cond
) and then Ent
= Entity
(Cond
) then
6232 Val
:= New_Occurrence_Of
(Standard_True
, Sloc
(Cond
));
6234 if Sens
= False then
6241 end Process_Current_Value_Condition
;
6243 -- Start of processing for Get_Current_Value_Condition
6249 -- Immediate return, nothing doing, if this is not an object
6251 if Ekind
(Ent
) not in Object_Kind
then
6255 -- Otherwise examine current value
6258 CV
: constant Node_Id
:= Current_Value
(Ent
);
6263 -- If statement. Condition is known true in THEN section, known False
6264 -- in any ELSIF or ELSE part, and unknown outside the IF statement.
6266 if Nkind
(CV
) = N_If_Statement
then
6268 -- Before start of IF statement
6270 if Loc
< Sloc
(CV
) then
6273 -- After end of IF statement
6275 elsif Loc
>= Sloc
(CV
) + Text_Ptr
(UI_To_Int
(End_Span
(CV
))) then
6279 -- At this stage we know that we are within the IF statement, but
6280 -- unfortunately, the tree does not record the SLOC of the ELSE so
6281 -- we cannot use a simple SLOC comparison to distinguish between
6282 -- the then/else statements, so we have to climb the tree.
6289 while Parent
(N
) /= CV
loop
6292 -- If we fall off the top of the tree, then that's odd, but
6293 -- perhaps it could occur in some error situation, and the
6294 -- safest response is simply to assume that the outcome of
6295 -- the condition is unknown. No point in bombing during an
6296 -- attempt to optimize things.
6303 -- Now we have N pointing to a node whose parent is the IF
6304 -- statement in question, so now we can tell if we are within
6305 -- the THEN statements.
6307 if Is_List_Member
(N
)
6308 and then List_Containing
(N
) = Then_Statements
(CV
)
6312 -- If the variable reference does not come from source, we
6313 -- cannot reliably tell whether it appears in the else part.
6314 -- In particular, if it appears in generated code for a node
6315 -- that requires finalization, it may be attached to a list
6316 -- that has not been yet inserted into the code. For now,
6317 -- treat it as unknown.
6319 elsif not Comes_From_Source
(N
) then
6322 -- Otherwise we must be in ELSIF or ELSE part
6329 -- ELSIF part. Condition is known true within the referenced
6330 -- ELSIF, known False in any subsequent ELSIF or ELSE part,
6331 -- and unknown before the ELSE part or after the IF statement.
6333 elsif Nkind
(CV
) = N_Elsif_Part
then
6335 -- if the Elsif_Part had condition_actions, the elsif has been
6336 -- rewritten as a nested if, and the original elsif_part is
6337 -- detached from the tree, so there is no way to obtain useful
6338 -- information on the current value of the variable.
6339 -- Can this be improved ???
6341 if No
(Parent
(CV
)) then
6347 -- If the tree has been otherwise rewritten there is nothing
6348 -- else to be done either.
6350 if Nkind
(Stm
) /= N_If_Statement
then
6354 -- Before start of ELSIF part
6356 if Loc
< Sloc
(CV
) then
6359 -- After end of IF statement
6361 elsif Loc
>= Sloc
(Stm
) +
6362 Text_Ptr
(UI_To_Int
(End_Span
(Stm
)))
6367 -- Again we lack the SLOC of the ELSE, so we need to climb the
6368 -- tree to see if we are within the ELSIF part in question.
6375 while Parent
(N
) /= Stm
loop
6378 -- If we fall off the top of the tree, then that's odd, but
6379 -- perhaps it could occur in some error situation, and the
6380 -- safest response is simply to assume that the outcome of
6381 -- the condition is unknown. No point in bombing during an
6382 -- attempt to optimize things.
6389 -- Now we have N pointing to a node whose parent is the IF
6390 -- statement in question, so see if is the ELSIF part we want.
6391 -- the THEN statements.
6396 -- Otherwise we must be in subsequent ELSIF or ELSE part
6403 -- Iteration scheme of while loop. The condition is known to be
6404 -- true within the body of the loop.
6406 elsif Nkind
(CV
) = N_Iteration_Scheme
then
6408 Loop_Stmt
: constant Node_Id
:= Parent
(CV
);
6411 -- Before start of body of loop
6413 if Loc
< Sloc
(Loop_Stmt
) then
6416 -- After end of LOOP statement
6418 elsif Loc
>= Sloc
(End_Label
(Loop_Stmt
)) then
6421 -- We are within the body of the loop
6428 -- All other cases of Current_Value settings
6434 -- If we fall through here, then we have a reportable condition, Sens
6435 -- is True if the condition is true and False if it needs inverting.
6437 Process_Current_Value_Condition
(Condition
(CV
), Sens
);
6439 end Get_Current_Value_Condition
;
6441 ---------------------
6442 -- Get_Stream_Size --
6443 ---------------------
6445 function Get_Stream_Size
(E
: Entity_Id
) return Uint
is
6447 -- If we have a Stream_Size clause for this type use it
6449 if Has_Stream_Size_Clause
(E
) then
6450 return Static_Integer
(Expression
(Stream_Size_Clause
(E
)));
6452 -- Otherwise the Stream_Size if the size of the type
6457 end Get_Stream_Size
;
6459 ---------------------------
6460 -- Has_Access_Constraint --
6461 ---------------------------
6463 function Has_Access_Constraint
(E
: Entity_Id
) return Boolean is
6465 T
: constant Entity_Id
:= Etype
(E
);
6468 if Has_Per_Object_Constraint
(E
) and then Has_Discriminants
(T
) then
6469 Disc
:= First_Discriminant
(T
);
6470 while Present
(Disc
) loop
6471 if Is_Access_Type
(Etype
(Disc
)) then
6475 Next_Discriminant
(Disc
);
6482 end Has_Access_Constraint
;
6484 -----------------------------------------------------
6485 -- Has_Annotate_Pragma_For_External_Axiomatization --
6486 -----------------------------------------------------
6488 function Has_Annotate_Pragma_For_External_Axiomatization
6489 (E
: Entity_Id
) return Boolean
6491 function Is_Annotate_Pragma_For_External_Axiomatization
6492 (N
: Node_Id
) return Boolean;
6493 -- Returns whether N is
6494 -- pragma Annotate (GNATprove, External_Axiomatization);
6496 ----------------------------------------------------
6497 -- Is_Annotate_Pragma_For_External_Axiomatization --
6498 ----------------------------------------------------
6500 -- The general form of pragma Annotate is
6502 -- pragma Annotate (IDENTIFIER [, IDENTIFIER {, ARG}]);
6503 -- ARG ::= NAME | EXPRESSION
6505 -- The first two arguments are by convention intended to refer to an
6506 -- external tool and a tool-specific function. These arguments are
6509 -- The following is used to annotate a package specification which
6510 -- GNATprove should treat specially, because the axiomatization of
6511 -- this unit is given by the user instead of being automatically
6514 -- pragma Annotate (GNATprove, External_Axiomatization);
6516 function Is_Annotate_Pragma_For_External_Axiomatization
6517 (N
: Node_Id
) return Boolean
6519 Name_GNATprove
: constant String :=
6521 Name_External_Axiomatization
: constant String :=
6522 "external_axiomatization";
6526 if Nkind
(N
) = N_Pragma
6527 and then Get_Pragma_Id
(N
) = Pragma_Annotate
6528 and then List_Length
(Pragma_Argument_Associations
(N
)) = 2
6531 Arg1
: constant Node_Id
:=
6532 First
(Pragma_Argument_Associations
(N
));
6533 Arg2
: constant Node_Id
:= Next
(Arg1
);
6538 -- Fill in Name_Buffer with Name_GNATprove first, and then with
6539 -- Name_External_Axiomatization so that Name_Find returns the
6540 -- corresponding name. This takes care of all possible casings.
6543 Add_Str_To_Name_Buffer
(Name_GNATprove
);
6547 Add_Str_To_Name_Buffer
(Name_External_Axiomatization
);
6550 return Chars
(Get_Pragma_Arg
(Arg1
)) = Nam1
6552 Chars
(Get_Pragma_Arg
(Arg2
)) = Nam2
;
6558 end Is_Annotate_Pragma_For_External_Axiomatization
;
6563 Vis_Decls
: List_Id
;
6566 -- Start of processing for Has_Annotate_Pragma_For_External_Axiomatization
6569 if Nkind
(Parent
(E
)) = N_Defining_Program_Unit_Name
then
6570 Decl
:= Parent
(Parent
(E
));
6575 Vis_Decls
:= Visible_Declarations
(Decl
);
6577 N
:= First
(Vis_Decls
);
6578 while Present
(N
) loop
6580 -- Skip declarations generated by the frontend. Skip all pragmas
6581 -- that are not the desired Annotate pragma. Stop the search on
6582 -- the first non-pragma source declaration.
6584 if Comes_From_Source
(N
) then
6585 if Nkind
(N
) = N_Pragma
then
6586 if Is_Annotate_Pragma_For_External_Axiomatization
(N
) then
6598 end Has_Annotate_Pragma_For_External_Axiomatization
;
6600 --------------------
6601 -- Homonym_Number --
6602 --------------------
6604 function Homonym_Number
(Subp
: Entity_Id
) return Nat
is
6610 Hom
:= Homonym
(Subp
);
6611 while Present
(Hom
) loop
6612 if Scope
(Hom
) = Scope
(Subp
) then
6616 Hom
:= Homonym
(Hom
);
6622 -----------------------------------
6623 -- In_Library_Level_Package_Body --
6624 -----------------------------------
6626 function In_Library_Level_Package_Body
(Id
: Entity_Id
) return Boolean is
6628 -- First determine whether the entity appears at the library level, then
6629 -- look at the containing unit.
6631 if Is_Library_Level_Entity
(Id
) then
6633 Container
: constant Node_Id
:= Cunit
(Get_Source_Unit
(Id
));
6636 return Nkind
(Unit
(Container
)) = N_Package_Body
;
6641 end In_Library_Level_Package_Body
;
6643 ------------------------------
6644 -- In_Unconditional_Context --
6645 ------------------------------
6647 function In_Unconditional_Context
(Node
: Node_Id
) return Boolean is
6652 while Present
(P
) loop
6654 when N_Subprogram_Body
=> return True;
6655 when N_If_Statement
=> return False;
6656 when N_Loop_Statement
=> return False;
6657 when N_Case_Statement
=> return False;
6658 when others => P
:= Parent
(P
);
6663 end In_Unconditional_Context
;
6669 procedure Insert_Action
(Assoc_Node
: Node_Id
; Ins_Action
: Node_Id
) is
6671 if Present
(Ins_Action
) then
6672 Insert_Actions
(Assoc_Node
, New_List
(Ins_Action
));
6676 -- Version with check(s) suppressed
6678 procedure Insert_Action
6679 (Assoc_Node
: Node_Id
; Ins_Action
: Node_Id
; Suppress
: Check_Id
)
6682 Insert_Actions
(Assoc_Node
, New_List
(Ins_Action
), Suppress
);
6685 -------------------------
6686 -- Insert_Action_After --
6687 -------------------------
6689 procedure Insert_Action_After
6690 (Assoc_Node
: Node_Id
;
6691 Ins_Action
: Node_Id
)
6694 Insert_Actions_After
(Assoc_Node
, New_List
(Ins_Action
));
6695 end Insert_Action_After
;
6697 --------------------
6698 -- Insert_Actions --
6699 --------------------
6701 procedure Insert_Actions
(Assoc_Node
: Node_Id
; Ins_Actions
: List_Id
) is
6705 Wrapped_Node
: Node_Id
:= Empty
;
6708 if No
(Ins_Actions
) or else Is_Empty_List
(Ins_Actions
) then
6712 -- Ignore insert of actions from inside default expression (or other
6713 -- similar "spec expression") in the special spec-expression analyze
6714 -- mode. Any insertions at this point have no relevance, since we are
6715 -- only doing the analyze to freeze the types of any static expressions.
6716 -- See section "Handling of Default Expressions" in the spec of package
6717 -- Sem for further details.
6719 if In_Spec_Expression
then
6723 -- If the action derives from stuff inside a record, then the actions
6724 -- are attached to the current scope, to be inserted and analyzed on
6725 -- exit from the scope. The reason for this is that we may also be
6726 -- generating freeze actions at the same time, and they must eventually
6727 -- be elaborated in the correct order.
6729 if Is_Record_Type
(Current_Scope
)
6730 and then not Is_Frozen
(Current_Scope
)
6732 if No
(Scope_Stack
.Table
6733 (Scope_Stack
.Last
).Pending_Freeze_Actions
)
6735 Scope_Stack
.Table
(Scope_Stack
.Last
).Pending_Freeze_Actions
:=
6740 Scope_Stack
.Table
(Scope_Stack
.Last
).Pending_Freeze_Actions
);
6746 -- We now intend to climb up the tree to find the right point to
6747 -- insert the actions. We start at Assoc_Node, unless this node is a
6748 -- subexpression in which case we start with its parent. We do this for
6749 -- two reasons. First it speeds things up. Second, if Assoc_Node is
6750 -- itself one of the special nodes like N_And_Then, then we assume that
6751 -- an initial request to insert actions for such a node does not expect
6752 -- the actions to get deposited in the node for later handling when the
6753 -- node is expanded, since clearly the node is being dealt with by the
6754 -- caller. Note that in the subexpression case, N is always the child we
6757 -- N_Raise_xxx_Error is an annoying special case, it is a statement
6758 -- if it has type Standard_Void_Type, and a subexpression otherwise.
6759 -- Procedure calls, and similarly procedure attribute references, are
6762 if Nkind
(Assoc_Node
) in N_Subexpr
6763 and then (Nkind
(Assoc_Node
) not in N_Raise_xxx_Error
6764 or else Etype
(Assoc_Node
) /= Standard_Void_Type
)
6765 and then Nkind
(Assoc_Node
) /= N_Procedure_Call_Statement
6766 and then (Nkind
(Assoc_Node
) /= N_Attribute_Reference
6767 or else not Is_Procedure_Attribute_Name
6768 (Attribute_Name
(Assoc_Node
)))
6771 P
:= Parent
(Assoc_Node
);
6773 -- Non-subexpression case. Note that N is initially Empty in this case
6774 -- (N is only guaranteed Non-Empty in the subexpr case).
6781 -- Capture root of the transient scope
6783 if Scope_Is_Transient
then
6784 Wrapped_Node
:= Node_To_Be_Wrapped
;
6788 pragma Assert
(Present
(P
));
6790 -- Make sure that inserted actions stay in the transient scope
6792 if Present
(Wrapped_Node
) and then N
= Wrapped_Node
then
6793 Store_Before_Actions_In_Scope
(Ins_Actions
);
6799 -- Case of right operand of AND THEN or OR ELSE. Put the actions
6800 -- in the Actions field of the right operand. They will be moved
6801 -- out further when the AND THEN or OR ELSE operator is expanded.
6802 -- Nothing special needs to be done for the left operand since
6803 -- in that case the actions are executed unconditionally.
6805 when N_Short_Circuit
=>
6806 if N
= Right_Opnd
(P
) then
6808 -- We are now going to either append the actions to the
6809 -- actions field of the short-circuit operation. We will
6810 -- also analyze the actions now.
6812 -- This analysis is really too early, the proper thing would
6813 -- be to just park them there now, and only analyze them if
6814 -- we find we really need them, and to it at the proper
6815 -- final insertion point. However attempting to this proved
6816 -- tricky, so for now we just kill current values before and
6817 -- after the analyze call to make sure we avoid peculiar
6818 -- optimizations from this out of order insertion.
6820 Kill_Current_Values
;
6822 -- If P has already been expanded, we can't park new actions
6823 -- on it, so we need to expand them immediately, introducing
6824 -- an Expression_With_Actions. N can't be an expression
6825 -- with actions, or else then the actions would have been
6826 -- inserted at an inner level.
6828 if Analyzed
(P
) then
6829 pragma Assert
(Nkind
(N
) /= N_Expression_With_Actions
);
6831 Make_Expression_With_Actions
(Sloc
(N
),
6832 Actions
=> Ins_Actions
,
6833 Expression
=> Relocate_Node
(N
)));
6834 Analyze_And_Resolve
(N
);
6836 elsif Present
(Actions
(P
)) then
6837 Insert_List_After_And_Analyze
6838 (Last
(Actions
(P
)), Ins_Actions
);
6840 Set_Actions
(P
, Ins_Actions
);
6841 Analyze_List
(Actions
(P
));
6844 Kill_Current_Values
;
6849 -- Then or Else dependent expression of an if expression. Add
6850 -- actions to Then_Actions or Else_Actions field as appropriate.
6851 -- The actions will be moved further out when the if is expanded.
6853 when N_If_Expression
=>
6855 ThenX
: constant Node_Id
:= Next
(First
(Expressions
(P
)));
6856 ElseX
: constant Node_Id
:= Next
(ThenX
);
6859 -- If the enclosing expression is already analyzed, as
6860 -- is the case for nested elaboration checks, insert the
6861 -- conditional further out.
6863 if Analyzed
(P
) then
6866 -- Actions belong to the then expression, temporarily place
6867 -- them as Then_Actions of the if expression. They will be
6868 -- moved to the proper place later when the if expression
6871 elsif N
= ThenX
then
6872 if Present
(Then_Actions
(P
)) then
6873 Insert_List_After_And_Analyze
6874 (Last
(Then_Actions
(P
)), Ins_Actions
);
6876 Set_Then_Actions
(P
, Ins_Actions
);
6877 Analyze_List
(Then_Actions
(P
));
6882 -- Actions belong to the else expression, temporarily place
6883 -- them as Else_Actions of the if expression. They will be
6884 -- moved to the proper place later when the if expression
6887 elsif N
= ElseX
then
6888 if Present
(Else_Actions
(P
)) then
6889 Insert_List_After_And_Analyze
6890 (Last
(Else_Actions
(P
)), Ins_Actions
);
6892 Set_Else_Actions
(P
, Ins_Actions
);
6893 Analyze_List
(Else_Actions
(P
));
6898 -- Actions belong to the condition. In this case they are
6899 -- unconditionally executed, and so we can continue the
6900 -- search for the proper insert point.
6907 -- Alternative of case expression, we place the action in the
6908 -- Actions field of the case expression alternative, this will
6909 -- be handled when the case expression is expanded.
6911 when N_Case_Expression_Alternative
=>
6912 if Present
(Actions
(P
)) then
6913 Insert_List_After_And_Analyze
6914 (Last
(Actions
(P
)), Ins_Actions
);
6916 Set_Actions
(P
, Ins_Actions
);
6917 Analyze_List
(Actions
(P
));
6922 -- Case of appearing within an Expressions_With_Actions node. When
6923 -- the new actions come from the expression of the expression with
6924 -- actions, they must be added to the existing actions. The other
6925 -- alternative is when the new actions are related to one of the
6926 -- existing actions of the expression with actions, and should
6927 -- never reach here: if actions are inserted on a statement
6928 -- within the Actions of an expression with actions, or on some
6929 -- subexpression of such a statement, then the outermost proper
6930 -- insertion point is right before the statement, and we should
6931 -- never climb up as far as the N_Expression_With_Actions itself.
6933 when N_Expression_With_Actions
=>
6934 if N
= Expression
(P
) then
6935 if Is_Empty_List
(Actions
(P
)) then
6936 Append_List_To
(Actions
(P
), Ins_Actions
);
6937 Analyze_List
(Actions
(P
));
6939 Insert_List_After_And_Analyze
6940 (Last
(Actions
(P
)), Ins_Actions
);
6946 raise Program_Error
;
6949 -- Case of appearing in the condition of a while expression or
6950 -- elsif. We insert the actions into the Condition_Actions field.
6951 -- They will be moved further out when the while loop or elsif
6955 | N_Iteration_Scheme
6957 if N
= Condition
(P
) then
6958 if Present
(Condition_Actions
(P
)) then
6959 Insert_List_After_And_Analyze
6960 (Last
(Condition_Actions
(P
)), Ins_Actions
);
6962 Set_Condition_Actions
(P
, Ins_Actions
);
6964 -- Set the parent of the insert actions explicitly. This
6965 -- is not a syntactic field, but we need the parent field
6966 -- set, in particular so that freeze can understand that
6967 -- it is dealing with condition actions, and properly
6968 -- insert the freezing actions.
6970 Set_Parent
(Ins_Actions
, P
);
6971 Analyze_List
(Condition_Actions
(P
));
6977 -- Statements, declarations, pragmas, representation clauses
6982 N_Procedure_Call_Statement
6983 | N_Statement_Other_Than_Procedure_Call
6989 -- Representation_Clause
6992 | N_Attribute_Definition_Clause
6993 | N_Enumeration_Representation_Clause
6994 | N_Record_Representation_Clause
6998 | N_Abstract_Subprogram_Declaration
7000 | N_Exception_Declaration
7001 | N_Exception_Renaming_Declaration
7002 | N_Expression_Function
7003 | N_Formal_Abstract_Subprogram_Declaration
7004 | N_Formal_Concrete_Subprogram_Declaration
7005 | N_Formal_Object_Declaration
7006 | N_Formal_Type_Declaration
7007 | N_Full_Type_Declaration
7008 | N_Function_Instantiation
7009 | N_Generic_Function_Renaming_Declaration
7010 | N_Generic_Package_Declaration
7011 | N_Generic_Package_Renaming_Declaration
7012 | N_Generic_Procedure_Renaming_Declaration
7013 | N_Generic_Subprogram_Declaration
7014 | N_Implicit_Label_Declaration
7015 | N_Incomplete_Type_Declaration
7016 | N_Number_Declaration
7017 | N_Object_Declaration
7018 | N_Object_Renaming_Declaration
7020 | N_Package_Body_Stub
7021 | N_Package_Declaration
7022 | N_Package_Instantiation
7023 | N_Package_Renaming_Declaration
7024 | N_Private_Extension_Declaration
7025 | N_Private_Type_Declaration
7026 | N_Procedure_Instantiation
7028 | N_Protected_Body_Stub
7029 | N_Protected_Type_Declaration
7030 | N_Single_Task_Declaration
7032 | N_Subprogram_Body_Stub
7033 | N_Subprogram_Declaration
7034 | N_Subprogram_Renaming_Declaration
7035 | N_Subtype_Declaration
7038 | N_Task_Type_Declaration
7040 -- Use clauses can appear in lists of declarations
7042 | N_Use_Package_Clause
7045 -- Freeze entity behaves like a declaration or statement
7048 | N_Freeze_Generic_Entity
7050 -- Do not insert here if the item is not a list member (this
7051 -- happens for example with a triggering statement, and the
7052 -- proper approach is to insert before the entire select).
7054 if not Is_List_Member
(P
) then
7057 -- Do not insert if parent of P is an N_Component_Association
7058 -- node (i.e. we are in the context of an N_Aggregate or
7059 -- N_Extension_Aggregate node. In this case we want to insert
7060 -- before the entire aggregate.
7062 elsif Nkind
(Parent
(P
)) = N_Component_Association
then
7065 -- Do not insert if the parent of P is either an N_Variant node
7066 -- or an N_Record_Definition node, meaning in either case that
7067 -- P is a member of a component list, and that therefore the
7068 -- actions should be inserted outside the complete record
7071 elsif Nkind_In
(Parent
(P
), N_Variant
, N_Record_Definition
) then
7074 -- Do not insert freeze nodes within the loop generated for
7075 -- an aggregate, because they may be elaborated too late for
7076 -- subsequent use in the back end: within a package spec the
7077 -- loop is part of the elaboration procedure and is only
7078 -- elaborated during the second pass.
7080 -- If the loop comes from source, or the entity is local to the
7081 -- loop itself it must remain within.
7083 elsif Nkind
(Parent
(P
)) = N_Loop_Statement
7084 and then not Comes_From_Source
(Parent
(P
))
7085 and then Nkind
(First
(Ins_Actions
)) = N_Freeze_Entity
7087 Scope
(Entity
(First
(Ins_Actions
))) /= Current_Scope
7091 -- Otherwise we can go ahead and do the insertion
7093 elsif P
= Wrapped_Node
then
7094 Store_Before_Actions_In_Scope
(Ins_Actions
);
7098 Insert_List_Before_And_Analyze
(P
, Ins_Actions
);
7102 -- A special case, N_Raise_xxx_Error can act either as a statement
7103 -- or a subexpression. We tell the difference by looking at the
7104 -- Etype. It is set to Standard_Void_Type in the statement case.
7106 when N_Raise_xxx_Error
=>
7107 if Etype
(P
) = Standard_Void_Type
then
7108 if P
= Wrapped_Node
then
7109 Store_Before_Actions_In_Scope
(Ins_Actions
);
7111 Insert_List_Before_And_Analyze
(P
, Ins_Actions
);
7116 -- In the subexpression case, keep climbing
7122 -- If a component association appears within a loop created for
7123 -- an array aggregate, attach the actions to the association so
7124 -- they can be subsequently inserted within the loop. For other
7125 -- component associations insert outside of the aggregate. For
7126 -- an association that will generate a loop, its Loop_Actions
7127 -- attribute is already initialized (see exp_aggr.adb).
7129 -- The list of Loop_Actions can in turn generate additional ones,
7130 -- that are inserted before the associated node. If the associated
7131 -- node is outside the aggregate, the new actions are collected
7132 -- at the end of the Loop_Actions, to respect the order in which
7133 -- they are to be elaborated.
7135 when N_Component_Association
7136 | N_Iterated_Component_Association
7138 if Nkind
(Parent
(P
)) = N_Aggregate
7139 and then Present
(Loop_Actions
(P
))
7141 if Is_Empty_List
(Loop_Actions
(P
)) then
7142 Set_Loop_Actions
(P
, Ins_Actions
);
7143 Analyze_List
(Ins_Actions
);
7149 -- Check whether these actions were generated by a
7150 -- declaration that is part of the Loop_Actions for
7151 -- the component_association.
7154 while Present
(Decl
) loop
7155 exit when Parent
(Decl
) = P
7156 and then Is_List_Member
(Decl
)
7158 List_Containing
(Decl
) = Loop_Actions
(P
);
7159 Decl
:= Parent
(Decl
);
7162 if Present
(Decl
) then
7163 Insert_List_Before_And_Analyze
7164 (Decl
, Ins_Actions
);
7166 Insert_List_After_And_Analyze
7167 (Last
(Loop_Actions
(P
)), Ins_Actions
);
7178 -- Special case: an attribute denoting a procedure call
7180 when N_Attribute_Reference
=>
7181 if Is_Procedure_Attribute_Name
(Attribute_Name
(P
)) then
7182 if P
= Wrapped_Node
then
7183 Store_Before_Actions_In_Scope
(Ins_Actions
);
7185 Insert_List_Before_And_Analyze
(P
, Ins_Actions
);
7190 -- In the subexpression case, keep climbing
7196 -- Special case: a marker
7199 | N_Variable_Reference_Marker
7201 if Is_List_Member
(P
) then
7202 Insert_List_Before_And_Analyze
(P
, Ins_Actions
);
7206 -- A contract node should not belong to the tree
7209 raise Program_Error
;
7211 -- For all other node types, keep climbing tree
7213 when N_Abortable_Part
7214 | N_Accept_Alternative
7215 | N_Access_Definition
7216 | N_Access_Function_Definition
7217 | N_Access_Procedure_Definition
7218 | N_Access_To_Object_Definition
7221 | N_Aspect_Specification
7223 | N_Case_Statement_Alternative
7224 | N_Character_Literal
7225 | N_Compilation_Unit
7226 | N_Compilation_Unit_Aux
7227 | N_Component_Clause
7228 | N_Component_Declaration
7229 | N_Component_Definition
7231 | N_Constrained_Array_Definition
7232 | N_Decimal_Fixed_Point_Definition
7233 | N_Defining_Character_Literal
7234 | N_Defining_Identifier
7235 | N_Defining_Operator_Symbol
7236 | N_Defining_Program_Unit_Name
7237 | N_Delay_Alternative
7239 | N_Delta_Constraint
7240 | N_Derived_Type_Definition
7242 | N_Digits_Constraint
7243 | N_Discriminant_Association
7244 | N_Discriminant_Specification
7246 | N_Entry_Body_Formal_Part
7247 | N_Entry_Call_Alternative
7248 | N_Entry_Declaration
7249 | N_Entry_Index_Specification
7250 | N_Enumeration_Type_Definition
7252 | N_Exception_Handler
7254 | N_Explicit_Dereference
7255 | N_Extension_Aggregate
7256 | N_Floating_Point_Definition
7257 | N_Formal_Decimal_Fixed_Point_Definition
7258 | N_Formal_Derived_Type_Definition
7259 | N_Formal_Discrete_Type_Definition
7260 | N_Formal_Floating_Point_Definition
7261 | N_Formal_Modular_Type_Definition
7262 | N_Formal_Ordinary_Fixed_Point_Definition
7263 | N_Formal_Package_Declaration
7264 | N_Formal_Private_Type_Definition
7265 | N_Formal_Incomplete_Type_Definition
7266 | N_Formal_Signed_Integer_Type_Definition
7268 | N_Function_Specification
7269 | N_Generic_Association
7270 | N_Handled_Sequence_Of_Statements
7273 | N_Index_Or_Discriminant_Constraint
7274 | N_Indexed_Component
7276 | N_Iterator_Specification
7279 | N_Loop_Parameter_Specification
7281 | N_Modular_Type_Definition
7307 | N_Op_Shift_Right_Arithmetic
7311 | N_Ordinary_Fixed_Point_Definition
7313 | N_Package_Specification
7314 | N_Parameter_Association
7315 | N_Parameter_Specification
7316 | N_Pop_Constraint_Error_Label
7317 | N_Pop_Program_Error_Label
7318 | N_Pop_Storage_Error_Label
7319 | N_Pragma_Argument_Association
7320 | N_Procedure_Specification
7321 | N_Protected_Definition
7322 | N_Push_Constraint_Error_Label
7323 | N_Push_Program_Error_Label
7324 | N_Push_Storage_Error_Label
7325 | N_Qualified_Expression
7326 | N_Quantified_Expression
7327 | N_Raise_Expression
7329 | N_Range_Constraint
7331 | N_Real_Range_Specification
7332 | N_Record_Definition
7333 | N_Reduction_Expression
7334 | N_Reduction_Expression_Parameter
7336 | N_SCIL_Dispatch_Table_Tag_Init
7337 | N_SCIL_Dispatching_Call
7338 | N_SCIL_Membership_Test
7339 | N_Selected_Component
7340 | N_Signed_Integer_Type_Definition
7341 | N_Single_Protected_Declaration
7344 | N_Subtype_Indication
7348 | N_Terminate_Alternative
7349 | N_Triggering_Alternative
7351 | N_Unchecked_Expression
7352 | N_Unchecked_Type_Conversion
7353 | N_Unconstrained_Array_Definition
7358 | N_Validate_Unchecked_Conversion
7364 -- If we fall through above tests, keep climbing tree
7368 if Nkind
(Parent
(N
)) = N_Subunit
then
7370 -- This is the proper body corresponding to a stub. Insertion must
7371 -- be done at the point of the stub, which is in the declarative
7372 -- part of the parent unit.
7374 P
:= Corresponding_Stub
(Parent
(N
));
7382 -- Version with check(s) suppressed
7384 procedure Insert_Actions
7385 (Assoc_Node
: Node_Id
;
7386 Ins_Actions
: List_Id
;
7387 Suppress
: Check_Id
)
7390 if Suppress
= All_Checks
then
7392 Sva
: constant Suppress_Array
:= Scope_Suppress
.Suppress
;
7394 Scope_Suppress
.Suppress
:= (others => True);
7395 Insert_Actions
(Assoc_Node
, Ins_Actions
);
7396 Scope_Suppress
.Suppress
:= Sva
;
7401 Svg
: constant Boolean := Scope_Suppress
.Suppress
(Suppress
);
7403 Scope_Suppress
.Suppress
(Suppress
) := True;
7404 Insert_Actions
(Assoc_Node
, Ins_Actions
);
7405 Scope_Suppress
.Suppress
(Suppress
) := Svg
;
7410 --------------------------
7411 -- Insert_Actions_After --
7412 --------------------------
7414 procedure Insert_Actions_After
7415 (Assoc_Node
: Node_Id
;
7416 Ins_Actions
: List_Id
)
7419 if Scope_Is_Transient
and then Assoc_Node
= Node_To_Be_Wrapped
then
7420 Store_After_Actions_In_Scope
(Ins_Actions
);
7422 Insert_List_After_And_Analyze
(Assoc_Node
, Ins_Actions
);
7424 end Insert_Actions_After
;
7426 ------------------------
7427 -- Insert_Declaration --
7428 ------------------------
7430 procedure Insert_Declaration
(N
: Node_Id
; Decl
: Node_Id
) is
7434 pragma Assert
(Nkind
(N
) in N_Subexpr
);
7436 -- Climb until we find a procedure or a package
7440 pragma Assert
(Present
(Parent
(P
)));
7443 if Is_List_Member
(P
) then
7444 exit when Nkind_In
(Parent
(P
), N_Package_Specification
,
7447 -- Special handling for handled sequence of statements, we must
7448 -- insert in the statements not the exception handlers!
7450 if Nkind
(Parent
(P
)) = N_Handled_Sequence_Of_Statements
then
7451 P
:= First
(Statements
(Parent
(P
)));
7457 -- Now do the insertion
7459 Insert_Before
(P
, Decl
);
7461 end Insert_Declaration
;
7463 ---------------------------------
7464 -- Insert_Library_Level_Action --
7465 ---------------------------------
7467 procedure Insert_Library_Level_Action
(N
: Node_Id
) is
7468 Aux
: constant Node_Id
:= Aux_Decls_Node
(Cunit
(Main_Unit
));
7471 Push_Scope
(Cunit_Entity
(Current_Sem_Unit
));
7472 -- And not Main_Unit as previously. If the main unit is a body,
7473 -- the scope needed to analyze the actions is the entity of the
7474 -- corresponding declaration.
7476 if No
(Actions
(Aux
)) then
7477 Set_Actions
(Aux
, New_List
(N
));
7479 Append
(N
, Actions
(Aux
));
7484 end Insert_Library_Level_Action
;
7486 ----------------------------------
7487 -- Insert_Library_Level_Actions --
7488 ----------------------------------
7490 procedure Insert_Library_Level_Actions
(L
: List_Id
) is
7491 Aux
: constant Node_Id
:= Aux_Decls_Node
(Cunit
(Main_Unit
));
7494 if Is_Non_Empty_List
(L
) then
7495 Push_Scope
(Cunit_Entity
(Main_Unit
));
7496 -- ??? should this be Current_Sem_Unit instead of Main_Unit?
7498 if No
(Actions
(Aux
)) then
7499 Set_Actions
(Aux
, L
);
7502 Insert_List_After_And_Analyze
(Last
(Actions
(Aux
)), L
);
7507 end Insert_Library_Level_Actions
;
7509 ----------------------
7510 -- Inside_Init_Proc --
7511 ----------------------
7513 function Inside_Init_Proc
return Boolean is
7518 while Present
(S
) and then S
/= Standard_Standard
loop
7519 if Is_Init_Proc
(S
) then
7527 end Inside_Init_Proc
;
7529 ----------------------------
7530 -- Is_All_Null_Statements --
7531 ----------------------------
7533 function Is_All_Null_Statements
(L
: List_Id
) return Boolean is
7538 while Present
(Stm
) loop
7539 if Nkind
(Stm
) /= N_Null_Statement
then
7547 end Is_All_Null_Statements
;
7549 --------------------------------------------------
7550 -- Is_Displacement_Of_Object_Or_Function_Result --
7551 --------------------------------------------------
7553 function Is_Displacement_Of_Object_Or_Function_Result
7554 (Obj_Id
: Entity_Id
) return Boolean
7556 function Is_Controlled_Function_Call
(N
: Node_Id
) return Boolean;
7557 -- Determine whether node N denotes a controlled function call
7559 function Is_Controlled_Indexing
(N
: Node_Id
) return Boolean;
7560 -- Determine whether node N denotes a generalized indexing form which
7561 -- involves a controlled result.
7563 function Is_Displace_Call
(N
: Node_Id
) return Boolean;
7564 -- Determine whether node N denotes a call to Ada.Tags.Displace
7566 function Is_Source_Object
(N
: Node_Id
) return Boolean;
7567 -- Determine whether a particular node denotes a source object
7569 function Strip
(N
: Node_Id
) return Node_Id
;
7570 -- Examine arbitrary node N by stripping various indirections and return
7573 ---------------------------------
7574 -- Is_Controlled_Function_Call --
7575 ---------------------------------
7577 function Is_Controlled_Function_Call
(N
: Node_Id
) return Boolean is
7581 -- When a function call appears in Object.Operation format, the
7582 -- original representation has several possible forms depending on
7583 -- the availability and form of actual parameters:
7585 -- Obj.Func N_Selected_Component
7586 -- Obj.Func (Actual) N_Indexed_Component
7587 -- Obj.Func (Formal => Actual) N_Function_Call, whose Name is an
7588 -- N_Selected_Component
7590 Expr
:= Original_Node
(N
);
7592 if Nkind
(Expr
) = N_Function_Call
then
7593 Expr
:= Name
(Expr
);
7595 -- "Obj.Func (Actual)" case
7597 elsif Nkind
(Expr
) = N_Indexed_Component
then
7598 Expr
:= Prefix
(Expr
);
7600 -- "Obj.Func" or "Obj.Func (Formal => Actual) case
7602 elsif Nkind
(Expr
) = N_Selected_Component
then
7603 Expr
:= Selector_Name
(Expr
);
7611 Nkind
(Expr
) in N_Has_Entity
7612 and then Present
(Entity
(Expr
))
7613 and then Ekind
(Entity
(Expr
)) = E_Function
7614 and then Needs_Finalization
(Etype
(Entity
(Expr
)));
7615 end Is_Controlled_Function_Call
;
7617 ----------------------------
7618 -- Is_Controlled_Indexing --
7619 ----------------------------
7621 function Is_Controlled_Indexing
(N
: Node_Id
) return Boolean is
7622 Expr
: constant Node_Id
:= Original_Node
(N
);
7626 Nkind
(Expr
) = N_Indexed_Component
7627 and then Present
(Generalized_Indexing
(Expr
))
7628 and then Needs_Finalization
(Etype
(Expr
));
7629 end Is_Controlled_Indexing
;
7631 ----------------------
7632 -- Is_Displace_Call --
7633 ----------------------
7635 function Is_Displace_Call
(N
: Node_Id
) return Boolean is
7636 Call
: constant Node_Id
:= Strip
(N
);
7641 and then Nkind
(Call
) = N_Function_Call
7642 and then Nkind
(Name
(Call
)) in N_Has_Entity
7643 and then Is_RTE
(Entity
(Name
(Call
)), RE_Displace
);
7644 end Is_Displace_Call
;
7646 ----------------------
7647 -- Is_Source_Object --
7648 ----------------------
7650 function Is_Source_Object
(N
: Node_Id
) return Boolean is
7651 Obj
: constant Node_Id
:= Strip
(N
);
7656 and then Comes_From_Source
(Obj
)
7657 and then Nkind
(Obj
) in N_Has_Entity
7658 and then Is_Object
(Entity
(Obj
));
7659 end Is_Source_Object
;
7665 function Strip
(N
: Node_Id
) return Node_Id
is
7671 if Nkind
(Result
) = N_Explicit_Dereference
then
7672 Result
:= Prefix
(Result
);
7674 elsif Nkind_In
(Result
, N_Type_Conversion
,
7675 N_Unchecked_Type_Conversion
)
7677 Result
:= Expression
(Result
);
7689 Obj_Decl
: constant Node_Id
:= Declaration_Node
(Obj_Id
);
7690 Obj_Typ
: constant Entity_Id
:= Base_Type
(Etype
(Obj_Id
));
7691 Orig_Decl
: constant Node_Id
:= Original_Node
(Obj_Decl
);
7692 Orig_Expr
: Node_Id
;
7694 -- Start of processing for Is_Displacement_Of_Object_Or_Function_Result
7699 -- Obj : CW_Type := Function_Call (...);
7701 -- is rewritten into:
7703 -- Temp : ... := Function_Call (...)'reference;
7704 -- Obj : CW_Type renames (... Ada.Tags.Displace (Temp));
7706 -- where the return type of the function and the class-wide type require
7707 -- dispatch table pointer displacement.
7711 -- Obj : CW_Type := Container (...);
7713 -- is rewritten into:
7715 -- Temp : ... := Function_Call (Container, ...)'reference;
7716 -- Obj : CW_Type renames (... Ada.Tags.Displace (Temp));
7718 -- where the container element type and the class-wide type require
7719 -- dispatch table pointer dispacement.
7723 -- Obj : CW_Type := Src_Obj;
7725 -- is rewritten into:
7727 -- Obj : CW_Type renames (... Ada.Tags.Displace (Src_Obj));
7729 -- where the type of the source object and the class-wide type require
7730 -- dispatch table pointer displacement.
7732 if Nkind
(Obj_Decl
) = N_Object_Renaming_Declaration
7733 and then Is_Class_Wide_Type
(Obj_Typ
)
7734 and then Is_Displace_Call
(Renamed_Object
(Obj_Id
))
7735 and then Nkind
(Orig_Decl
) = N_Object_Declaration
7736 and then Comes_From_Source
(Orig_Decl
)
7738 Orig_Expr
:= Expression
(Orig_Decl
);
7741 Is_Controlled_Function_Call
(Orig_Expr
)
7742 or else Is_Controlled_Indexing
(Orig_Expr
)
7743 or else Is_Source_Object
(Orig_Expr
);
7747 end Is_Displacement_Of_Object_Or_Function_Result
;
7749 ------------------------------
7750 -- Is_Finalizable_Transient --
7751 ------------------------------
7753 function Is_Finalizable_Transient
7755 Rel_Node
: Node_Id
) return Boolean
7757 Obj_Id
: constant Entity_Id
:= Defining_Identifier
(Decl
);
7758 Obj_Typ
: constant Entity_Id
:= Base_Type
(Etype
(Obj_Id
));
7760 function Initialized_By_Access
(Trans_Id
: Entity_Id
) return Boolean;
7761 -- Determine whether transient object Trans_Id is initialized either
7762 -- by a function call which returns an access type or simply renames
7765 function Initialized_By_Aliased_BIP_Func_Call
7766 (Trans_Id
: Entity_Id
) return Boolean;
7767 -- Determine whether transient object Trans_Id is initialized by a
7768 -- build-in-place function call where the BIPalloc parameter is of
7769 -- value 1 and BIPaccess is not null. This case creates an aliasing
7770 -- between the returned value and the value denoted by BIPaccess.
7773 (Trans_Id
: Entity_Id
;
7774 First_Stmt
: Node_Id
) return Boolean;
7775 -- Determine whether transient object Trans_Id has been renamed or
7776 -- aliased through 'reference in the statement list starting from
7779 function Is_Allocated
(Trans_Id
: Entity_Id
) return Boolean;
7780 -- Determine whether transient object Trans_Id is allocated on the heap
7782 function Is_Iterated_Container
7783 (Trans_Id
: Entity_Id
;
7784 First_Stmt
: Node_Id
) return Boolean;
7785 -- Determine whether transient object Trans_Id denotes a container which
7786 -- is in the process of being iterated in the statement list starting
7789 ---------------------------
7790 -- Initialized_By_Access --
7791 ---------------------------
7793 function Initialized_By_Access
(Trans_Id
: Entity_Id
) return Boolean is
7794 Expr
: constant Node_Id
:= Expression
(Parent
(Trans_Id
));
7799 and then Nkind
(Expr
) /= N_Reference
7800 and then Is_Access_Type
(Etype
(Expr
));
7801 end Initialized_By_Access
;
7803 ------------------------------------------
7804 -- Initialized_By_Aliased_BIP_Func_Call --
7805 ------------------------------------------
7807 function Initialized_By_Aliased_BIP_Func_Call
7808 (Trans_Id
: Entity_Id
) return Boolean
7810 Call
: Node_Id
:= Expression
(Parent
(Trans_Id
));
7813 -- Build-in-place calls usually appear in 'reference format
7815 if Nkind
(Call
) = N_Reference
then
7816 Call
:= Prefix
(Call
);
7819 Call
:= Unqual_Conv
(Call
);
7821 if Is_Build_In_Place_Function_Call
(Call
) then
7823 Access_Nam
: Name_Id
:= No_Name
;
7824 Access_OK
: Boolean := False;
7826 Alloc_Nam
: Name_Id
:= No_Name
;
7827 Alloc_OK
: Boolean := False;
7829 Func_Id
: Entity_Id
;
7833 -- Examine all parameter associations of the function call
7835 Param
:= First
(Parameter_Associations
(Call
));
7836 while Present
(Param
) loop
7837 if Nkind
(Param
) = N_Parameter_Association
7838 and then Nkind
(Selector_Name
(Param
)) = N_Identifier
7840 Actual
:= Explicit_Actual_Parameter
(Param
);
7841 Formal
:= Selector_Name
(Param
);
7843 -- Construct the names of formals BIPaccess and BIPalloc
7844 -- using the function name retrieved from an arbitrary
7847 if Access_Nam
= No_Name
7848 and then Alloc_Nam
= No_Name
7849 and then Present
(Entity
(Formal
))
7851 Func_Id
:= Scope
(Entity
(Formal
));
7854 New_External_Name
(Chars
(Func_Id
),
7855 BIP_Formal_Suffix
(BIP_Object_Access
));
7858 New_External_Name
(Chars
(Func_Id
),
7859 BIP_Formal_Suffix
(BIP_Alloc_Form
));
7862 -- A match for BIPaccess => Temp has been found
7864 if Chars
(Formal
) = Access_Nam
7865 and then Nkind
(Actual
) /= N_Null
7870 -- A match for BIPalloc => 1 has been found
7872 if Chars
(Formal
) = Alloc_Nam
7873 and then Nkind
(Actual
) = N_Integer_Literal
7874 and then Intval
(Actual
) = Uint_1
7883 return Access_OK
and Alloc_OK
;
7888 end Initialized_By_Aliased_BIP_Func_Call
;
7895 (Trans_Id
: Entity_Id
;
7896 First_Stmt
: Node_Id
) return Boolean
7898 function Find_Renamed_Object
(Ren_Decl
: Node_Id
) return Entity_Id
;
7899 -- Given an object renaming declaration, retrieve the entity of the
7900 -- renamed name. Return Empty if the renamed name is anything other
7901 -- than a variable or a constant.
7903 -------------------------
7904 -- Find_Renamed_Object --
7905 -------------------------
7907 function Find_Renamed_Object
(Ren_Decl
: Node_Id
) return Entity_Id
is
7908 Ren_Obj
: Node_Id
:= Empty
;
7910 function Find_Object
(N
: Node_Id
) return Traverse_Result
;
7911 -- Try to detect an object which is either a constant or a
7918 function Find_Object
(N
: Node_Id
) return Traverse_Result
is
7920 -- Stop the search once a constant or a variable has been
7923 if Nkind
(N
) = N_Identifier
7924 and then Present
(Entity
(N
))
7925 and then Ekind_In
(Entity
(N
), E_Constant
, E_Variable
)
7927 Ren_Obj
:= Entity
(N
);
7934 procedure Search
is new Traverse_Proc
(Find_Object
);
7938 Typ
: constant Entity_Id
:= Etype
(Defining_Identifier
(Ren_Decl
));
7940 -- Start of processing for Find_Renamed_Object
7943 -- Actions related to dispatching calls may appear as renamings of
7944 -- tags. Do not process this type of renaming because it does not
7945 -- use the actual value of the object.
7947 if not Is_RTE
(Typ
, RE_Tag_Ptr
) then
7948 Search
(Name
(Ren_Decl
));
7952 end Find_Renamed_Object
;
7957 Ren_Obj
: Entity_Id
;
7960 -- Start of processing for Is_Aliased
7963 -- A controlled transient object is not considered aliased when it
7964 -- appears inside an expression_with_actions node even when there are
7965 -- explicit aliases of it:
7968 -- Trans_Id : Ctrl_Typ ...; -- transient object
7969 -- Alias : ... := Trans_Id; -- object is aliased
7970 -- Val : constant Boolean :=
7971 -- ... Alias ...; -- aliasing ends
7972 -- <finalize Trans_Id> -- object safe to finalize
7975 -- Expansion ensures that all aliases are encapsulated in the actions
7976 -- list and do not leak to the expression by forcing the evaluation
7977 -- of the expression.
7979 if Nkind
(Rel_Node
) = N_Expression_With_Actions
then
7982 -- Otherwise examine the statements after the controlled transient
7983 -- object and look for various forms of aliasing.
7987 while Present
(Stmt
) loop
7988 if Nkind
(Stmt
) = N_Object_Declaration
then
7989 Expr
:= Expression
(Stmt
);
7991 -- Aliasing of the form:
7992 -- Obj : ... := Trans_Id'reference;
7995 and then Nkind
(Expr
) = N_Reference
7996 and then Nkind
(Prefix
(Expr
)) = N_Identifier
7997 and then Entity
(Prefix
(Expr
)) = Trans_Id
8002 elsif Nkind
(Stmt
) = N_Object_Renaming_Declaration
then
8003 Ren_Obj
:= Find_Renamed_Object
(Stmt
);
8005 -- Aliasing of the form:
8006 -- Obj : ... renames ... Trans_Id ...;
8008 if Present
(Ren_Obj
) and then Ren_Obj
= Trans_Id
then
8024 function Is_Allocated
(Trans_Id
: Entity_Id
) return Boolean is
8025 Expr
: constant Node_Id
:= Expression
(Parent
(Trans_Id
));
8028 Is_Access_Type
(Etype
(Trans_Id
))
8029 and then Present
(Expr
)
8030 and then Nkind
(Expr
) = N_Allocator
;
8033 ---------------------------
8034 -- Is_Iterated_Container --
8035 ---------------------------
8037 function Is_Iterated_Container
8038 (Trans_Id
: Entity_Id
;
8039 First_Stmt
: Node_Id
) return Boolean
8049 -- It is not possible to iterate over containers in non-Ada 2012 code
8051 if Ada_Version
< Ada_2012
then
8055 Typ
:= Etype
(Trans_Id
);
8057 -- Handle access type created for secondary stack use
8059 if Is_Access_Type
(Typ
) then
8060 Typ
:= Designated_Type
(Typ
);
8063 -- Look for aspect Default_Iterator. It may be part of a type
8064 -- declaration for a container, or inherited from a base type
8067 Aspect
:= Find_Value_Of_Aspect
(Typ
, Aspect_Default_Iterator
);
8069 if Present
(Aspect
) then
8070 Iter
:= Entity
(Aspect
);
8072 -- Examine the statements following the container object and
8073 -- look for a call to the default iterate routine where the
8074 -- first parameter is the transient. Such a call appears as:
8076 -- It : Access_To_CW_Iterator :=
8077 -- Iterate (Tran_Id.all, ...)'reference;
8080 while Present
(Stmt
) loop
8082 -- Detect an object declaration which is initialized by a
8083 -- secondary stack function call.
8085 if Nkind
(Stmt
) = N_Object_Declaration
8086 and then Present
(Expression
(Stmt
))
8087 and then Nkind
(Expression
(Stmt
)) = N_Reference
8088 and then Nkind
(Prefix
(Expression
(Stmt
))) = N_Function_Call
8090 Call
:= Prefix
(Expression
(Stmt
));
8092 -- The call must invoke the default iterate routine of
8093 -- the container and the transient object must appear as
8094 -- the first actual parameter. Skip any calls whose names
8095 -- are not entities.
8097 if Is_Entity_Name
(Name
(Call
))
8098 and then Entity
(Name
(Call
)) = Iter
8099 and then Present
(Parameter_Associations
(Call
))
8101 Param
:= First
(Parameter_Associations
(Call
));
8103 if Nkind
(Param
) = N_Explicit_Dereference
8104 and then Entity
(Prefix
(Param
)) = Trans_Id
8116 end Is_Iterated_Container
;
8120 Desig
: Entity_Id
:= Obj_Typ
;
8122 -- Start of processing for Is_Finalizable_Transient
8125 -- Handle access types
8127 if Is_Access_Type
(Desig
) then
8128 Desig
:= Available_View
(Designated_Type
(Desig
));
8132 Ekind_In
(Obj_Id
, E_Constant
, E_Variable
)
8133 and then Needs_Finalization
(Desig
)
8134 and then Requires_Transient_Scope
(Desig
)
8135 and then Nkind
(Rel_Node
) /= N_Simple_Return_Statement
8137 -- Do not consider a transient object that was already processed
8139 and then not Is_Finalized_Transient
(Obj_Id
)
8141 -- Do not consider renamed or 'reference-d transient objects because
8142 -- the act of renaming extends the object's lifetime.
8144 and then not Is_Aliased
(Obj_Id
, Decl
)
8146 -- Do not consider transient objects allocated on the heap since
8147 -- they are attached to a finalization master.
8149 and then not Is_Allocated
(Obj_Id
)
8151 -- If the transient object is a pointer, check that it is not
8152 -- initialized by a function that returns a pointer or acts as a
8153 -- renaming of another pointer.
8156 (not Is_Access_Type
(Obj_Typ
)
8157 or else not Initialized_By_Access
(Obj_Id
))
8159 -- Do not consider transient objects which act as indirect aliases
8160 -- of build-in-place function results.
8162 and then not Initialized_By_Aliased_BIP_Func_Call
(Obj_Id
)
8164 -- Do not consider conversions of tags to class-wide types
8166 and then not Is_Tag_To_Class_Wide_Conversion
(Obj_Id
)
8168 -- Do not consider iterators because those are treated as normal
8169 -- controlled objects and are processed by the usual finalization
8170 -- machinery. This avoids the double finalization of an iterator.
8172 and then not Is_Iterator
(Desig
)
8174 -- Do not consider containers in the context of iterator loops. Such
8175 -- transient objects must exist for as long as the loop is around,
8176 -- otherwise any operation carried out by the iterator will fail.
8178 and then not Is_Iterated_Container
(Obj_Id
, Decl
);
8179 end Is_Finalizable_Transient
;
8181 ---------------------------------
8182 -- Is_Fully_Repped_Tagged_Type --
8183 ---------------------------------
8185 function Is_Fully_Repped_Tagged_Type
(T
: Entity_Id
) return Boolean is
8186 U
: constant Entity_Id
:= Underlying_Type
(T
);
8190 if No
(U
) or else not Is_Tagged_Type
(U
) then
8192 elsif Has_Discriminants
(U
) then
8194 elsif not Has_Specified_Layout
(U
) then
8198 -- Here we have a tagged type, see if it has any unlayed out fields
8199 -- other than a possible tag and parent fields. If so, we return False.
8201 Comp
:= First_Component
(U
);
8202 while Present
(Comp
) loop
8203 if not Is_Tag
(Comp
)
8204 and then Chars
(Comp
) /= Name_uParent
8205 and then No
(Component_Clause
(Comp
))
8209 Next_Component
(Comp
);
8213 -- All components are layed out
8216 end Is_Fully_Repped_Tagged_Type
;
8218 ----------------------------------
8219 -- Is_Library_Level_Tagged_Type --
8220 ----------------------------------
8222 function Is_Library_Level_Tagged_Type
(Typ
: Entity_Id
) return Boolean is
8224 return Is_Tagged_Type
(Typ
) and then Is_Library_Level_Entity
(Typ
);
8225 end Is_Library_Level_Tagged_Type
;
8227 --------------------------
8228 -- Is_Non_BIP_Func_Call --
8229 --------------------------
8231 function Is_Non_BIP_Func_Call
(Expr
: Node_Id
) return Boolean is
8233 -- The expected call is of the format
8235 -- Func_Call'reference
8238 Nkind
(Expr
) = N_Reference
8239 and then Nkind
(Prefix
(Expr
)) = N_Function_Call
8240 and then not Is_Build_In_Place_Function_Call
(Prefix
(Expr
));
8241 end Is_Non_BIP_Func_Call
;
8243 ----------------------------------
8244 -- Is_Possibly_Unaligned_Object --
8245 ----------------------------------
8247 function Is_Possibly_Unaligned_Object
(N
: Node_Id
) return Boolean is
8248 T
: constant Entity_Id
:= Etype
(N
);
8251 -- If renamed object, apply test to underlying object
8253 if Is_Entity_Name
(N
)
8254 and then Is_Object
(Entity
(N
))
8255 and then Present
(Renamed_Object
(Entity
(N
)))
8257 return Is_Possibly_Unaligned_Object
(Renamed_Object
(Entity
(N
)));
8260 -- Tagged and controlled types and aliased types are always aligned, as
8261 -- are concurrent types.
8264 or else Has_Controlled_Component
(T
)
8265 or else Is_Concurrent_Type
(T
)
8266 or else Is_Tagged_Type
(T
)
8267 or else Is_Controlled
(T
)
8272 -- If this is an element of a packed array, may be unaligned
8274 if Is_Ref_To_Bit_Packed_Array
(N
) then
8278 -- Case of indexed component reference: test whether prefix is unaligned
8280 if Nkind
(N
) = N_Indexed_Component
then
8281 return Is_Possibly_Unaligned_Object
(Prefix
(N
));
8283 -- Case of selected component reference
8285 elsif Nkind
(N
) = N_Selected_Component
then
8287 P
: constant Node_Id
:= Prefix
(N
);
8288 C
: constant Entity_Id
:= Entity
(Selector_Name
(N
));
8293 -- If component reference is for an array with non-static bounds,
8294 -- then it is always aligned: we can only process unaligned arrays
8295 -- with static bounds (more precisely compile time known bounds).
8297 if Is_Array_Type
(T
)
8298 and then not Compile_Time_Known_Bounds
(T
)
8303 -- If component is aliased, it is definitely properly aligned
8305 if Is_Aliased
(C
) then
8309 -- If component is for a type implemented as a scalar, and the
8310 -- record is packed, and the component is other than the first
8311 -- component of the record, then the component may be unaligned.
8313 if Is_Packed
(Etype
(P
))
8314 and then Represented_As_Scalar
(Etype
(C
))
8315 and then First_Entity
(Scope
(C
)) /= C
8320 -- Compute maximum possible alignment for T
8322 -- If alignment is known, then that settles things
8324 if Known_Alignment
(T
) then
8325 M
:= UI_To_Int
(Alignment
(T
));
8327 -- If alignment is not known, tentatively set max alignment
8330 M
:= Ttypes
.Maximum_Alignment
;
8332 -- We can reduce this if the Esize is known since the default
8333 -- alignment will never be more than the smallest power of 2
8334 -- that does not exceed this Esize value.
8336 if Known_Esize
(T
) then
8337 S
:= UI_To_Int
(Esize
(T
));
8339 while (M
/ 2) >= S
loop
8345 -- The following code is historical, it used to be present but it
8346 -- is too cautious, because the front-end does not know the proper
8347 -- default alignments for the target. Also, if the alignment is
8348 -- not known, the front end can't know in any case. If a copy is
8349 -- needed, the back-end will take care of it. This whole section
8350 -- including this comment can be removed later ???
8352 -- If the component reference is for a record that has a specified
8353 -- alignment, and we either know it is too small, or cannot tell,
8354 -- then the component may be unaligned.
8356 -- What is the following commented out code ???
8358 -- if Known_Alignment (Etype (P))
8359 -- and then Alignment (Etype (P)) < Ttypes.Maximum_Alignment
8360 -- and then M > Alignment (Etype (P))
8365 -- Case of component clause present which may specify an
8366 -- unaligned position.
8368 if Present
(Component_Clause
(C
)) then
8370 -- Otherwise we can do a test to make sure that the actual
8371 -- start position in the record, and the length, are both
8372 -- consistent with the required alignment. If not, we know
8373 -- that we are unaligned.
8376 Align_In_Bits
: constant Nat
:= M
* System_Storage_Unit
;
8378 if Component_Bit_Offset
(C
) mod Align_In_Bits
/= 0
8379 or else Esize
(C
) mod Align_In_Bits
/= 0
8386 -- Otherwise, for a component reference, test prefix
8388 return Is_Possibly_Unaligned_Object
(P
);
8391 -- If not a component reference, must be aligned
8396 end Is_Possibly_Unaligned_Object
;
8398 ---------------------------------
8399 -- Is_Possibly_Unaligned_Slice --
8400 ---------------------------------
8402 function Is_Possibly_Unaligned_Slice
(N
: Node_Id
) return Boolean is
8404 -- Go to renamed object
8406 if Is_Entity_Name
(N
)
8407 and then Is_Object
(Entity
(N
))
8408 and then Present
(Renamed_Object
(Entity
(N
)))
8410 return Is_Possibly_Unaligned_Slice
(Renamed_Object
(Entity
(N
)));
8413 -- The reference must be a slice
8415 if Nkind
(N
) /= N_Slice
then
8419 -- We only need to worry if the target has strict alignment
8421 if not Target_Strict_Alignment
then
8425 -- If it is a slice, then look at the array type being sliced
8428 Sarr
: constant Node_Id
:= Prefix
(N
);
8429 -- Prefix of the slice, i.e. the array being sliced
8431 Styp
: constant Entity_Id
:= Etype
(Prefix
(N
));
8432 -- Type of the array being sliced
8438 -- The problems arise if the array object that is being sliced
8439 -- is a component of a record or array, and we cannot guarantee
8440 -- the alignment of the array within its containing object.
8442 -- To investigate this, we look at successive prefixes to see
8443 -- if we have a worrisome indexed or selected component.
8447 -- Case of array is part of an indexed component reference
8449 if Nkind
(Pref
) = N_Indexed_Component
then
8450 Ptyp
:= Etype
(Prefix
(Pref
));
8452 -- The only problematic case is when the array is packed, in
8453 -- which case we really know nothing about the alignment of
8454 -- individual components.
8456 if Is_Bit_Packed_Array
(Ptyp
) then
8460 -- Case of array is part of a selected component reference
8462 elsif Nkind
(Pref
) = N_Selected_Component
then
8463 Ptyp
:= Etype
(Prefix
(Pref
));
8465 -- We are definitely in trouble if the record in question
8466 -- has an alignment, and either we know this alignment is
8467 -- inconsistent with the alignment of the slice, or we don't
8468 -- know what the alignment of the slice should be.
8470 if Known_Alignment
(Ptyp
)
8471 and then (Unknown_Alignment
(Styp
)
8472 or else Alignment
(Styp
) > Alignment
(Ptyp
))
8477 -- We are in potential trouble if the record type is packed.
8478 -- We could special case when we know that the array is the
8479 -- first component, but that's not such a simple case ???
8481 if Is_Packed
(Ptyp
) then
8485 -- We are in trouble if there is a component clause, and
8486 -- either we do not know the alignment of the slice, or
8487 -- the alignment of the slice is inconsistent with the
8488 -- bit position specified by the component clause.
8491 Field
: constant Entity_Id
:= Entity
(Selector_Name
(Pref
));
8493 if Present
(Component_Clause
(Field
))
8495 (Unknown_Alignment
(Styp
)
8497 (Component_Bit_Offset
(Field
) mod
8498 (System_Storage_Unit
* Alignment
(Styp
))) /= 0)
8504 -- For cases other than selected or indexed components we know we
8505 -- are OK, since no issues arise over alignment.
8511 -- We processed an indexed component or selected component
8512 -- reference that looked safe, so keep checking prefixes.
8514 Pref
:= Prefix
(Pref
);
8517 end Is_Possibly_Unaligned_Slice
;
8519 -------------------------------
8520 -- Is_Related_To_Func_Return --
8521 -------------------------------
8523 function Is_Related_To_Func_Return
(Id
: Entity_Id
) return Boolean is
8524 Expr
: constant Node_Id
:= Related_Expression
(Id
);
8528 and then Nkind
(Expr
) = N_Explicit_Dereference
8529 and then Nkind
(Parent
(Expr
)) = N_Simple_Return_Statement
;
8530 end Is_Related_To_Func_Return
;
8532 --------------------------------
8533 -- Is_Ref_To_Bit_Packed_Array --
8534 --------------------------------
8536 function Is_Ref_To_Bit_Packed_Array
(N
: Node_Id
) return Boolean is
8541 if Is_Entity_Name
(N
)
8542 and then Is_Object
(Entity
(N
))
8543 and then Present
(Renamed_Object
(Entity
(N
)))
8545 return Is_Ref_To_Bit_Packed_Array
(Renamed_Object
(Entity
(N
)));
8548 if Nkind_In
(N
, N_Indexed_Component
, N_Selected_Component
) then
8549 if Is_Bit_Packed_Array
(Etype
(Prefix
(N
))) then
8552 Result
:= Is_Ref_To_Bit_Packed_Array
(Prefix
(N
));
8555 if Result
and then Nkind
(N
) = N_Indexed_Component
then
8556 Expr
:= First
(Expressions
(N
));
8557 while Present
(Expr
) loop
8558 Force_Evaluation
(Expr
);
8568 end Is_Ref_To_Bit_Packed_Array
;
8570 --------------------------------
8571 -- Is_Ref_To_Bit_Packed_Slice --
8572 --------------------------------
8574 function Is_Ref_To_Bit_Packed_Slice
(N
: Node_Id
) return Boolean is
8576 if Nkind
(N
) = N_Type_Conversion
then
8577 return Is_Ref_To_Bit_Packed_Slice
(Expression
(N
));
8579 elsif Is_Entity_Name
(N
)
8580 and then Is_Object
(Entity
(N
))
8581 and then Present
(Renamed_Object
(Entity
(N
)))
8583 return Is_Ref_To_Bit_Packed_Slice
(Renamed_Object
(Entity
(N
)));
8585 elsif Nkind
(N
) = N_Slice
8586 and then Is_Bit_Packed_Array
(Etype
(Prefix
(N
)))
8590 elsif Nkind_In
(N
, N_Indexed_Component
, N_Selected_Component
) then
8591 return Is_Ref_To_Bit_Packed_Slice
(Prefix
(N
));
8596 end Is_Ref_To_Bit_Packed_Slice
;
8598 -----------------------
8599 -- Is_Renamed_Object --
8600 -----------------------
8602 function Is_Renamed_Object
(N
: Node_Id
) return Boolean is
8603 Pnod
: constant Node_Id
:= Parent
(N
);
8604 Kind
: constant Node_Kind
:= Nkind
(Pnod
);
8606 if Kind
= N_Object_Renaming_Declaration
then
8608 elsif Nkind_In
(Kind
, N_Indexed_Component
, N_Selected_Component
) then
8609 return Is_Renamed_Object
(Pnod
);
8613 end Is_Renamed_Object
;
8615 --------------------------------------
8616 -- Is_Secondary_Stack_BIP_Func_Call --
8617 --------------------------------------
8619 function Is_Secondary_Stack_BIP_Func_Call
(Expr
: Node_Id
) return Boolean is
8620 Alloc_Nam
: Name_Id
:= No_Name
;
8622 Call
: Node_Id
:= Expr
;
8627 -- Build-in-place calls usually appear in 'reference format. Note that
8628 -- the accessibility check machinery may add an extra 'reference due to
8629 -- side effect removal.
8631 while Nkind
(Call
) = N_Reference
loop
8632 Call
:= Prefix
(Call
);
8635 Call
:= Unqual_Conv
(Call
);
8637 if Is_Build_In_Place_Function_Call
(Call
) then
8639 -- Examine all parameter associations of the function call
8641 Param
:= First
(Parameter_Associations
(Call
));
8642 while Present
(Param
) loop
8643 if Nkind
(Param
) = N_Parameter_Association
then
8644 Formal
:= Selector_Name
(Param
);
8645 Actual
:= Explicit_Actual_Parameter
(Param
);
8647 -- Construct the name of formal BIPalloc. It is much easier to
8648 -- extract the name of the function using an arbitrary formal's
8649 -- scope rather than the Name field of Call.
8651 if Alloc_Nam
= No_Name
and then Present
(Entity
(Formal
)) then
8654 (Chars
(Scope
(Entity
(Formal
))),
8655 BIP_Formal_Suffix
(BIP_Alloc_Form
));
8658 -- A match for BIPalloc => 2 has been found
8660 if Chars
(Formal
) = Alloc_Nam
8661 and then Nkind
(Actual
) = N_Integer_Literal
8662 and then Intval
(Actual
) = Uint_2
8673 end Is_Secondary_Stack_BIP_Func_Call
;
8675 -------------------------------------
8676 -- Is_Tag_To_Class_Wide_Conversion --
8677 -------------------------------------
8679 function Is_Tag_To_Class_Wide_Conversion
8680 (Obj_Id
: Entity_Id
) return Boolean
8682 Expr
: constant Node_Id
:= Expression
(Parent
(Obj_Id
));
8686 Is_Class_Wide_Type
(Etype
(Obj_Id
))
8687 and then Present
(Expr
)
8688 and then Nkind
(Expr
) = N_Unchecked_Type_Conversion
8689 and then Etype
(Expression
(Expr
)) = RTE
(RE_Tag
);
8690 end Is_Tag_To_Class_Wide_Conversion
;
8692 ----------------------------
8693 -- Is_Untagged_Derivation --
8694 ----------------------------
8696 function Is_Untagged_Derivation
(T
: Entity_Id
) return Boolean is
8698 return (not Is_Tagged_Type
(T
) and then Is_Derived_Type
(T
))
8700 (Is_Private_Type
(T
) and then Present
(Full_View
(T
))
8701 and then not Is_Tagged_Type
(Full_View
(T
))
8702 and then Is_Derived_Type
(Full_View
(T
))
8703 and then Etype
(Full_View
(T
)) /= T
);
8704 end Is_Untagged_Derivation
;
8706 ------------------------------------
8707 -- Is_Untagged_Private_Derivation --
8708 ------------------------------------
8710 function Is_Untagged_Private_Derivation
8711 (Priv_Typ
: Entity_Id
;
8712 Full_Typ
: Entity_Id
) return Boolean
8717 and then Is_Untagged_Derivation
(Priv_Typ
)
8718 and then Is_Private_Type
(Etype
(Priv_Typ
))
8719 and then Present
(Full_Typ
)
8720 and then Is_Itype
(Full_Typ
);
8721 end Is_Untagged_Private_Derivation
;
8723 ------------------------------
8724 -- Is_Verifiable_DIC_Pragma --
8725 ------------------------------
8727 function Is_Verifiable_DIC_Pragma
(Prag
: Node_Id
) return Boolean is
8728 Args
: constant List_Id
:= Pragma_Argument_Associations
(Prag
);
8731 -- To qualify as verifiable, a DIC pragma must have a non-null argument
8735 and then Nkind
(Get_Pragma_Arg
(First
(Args
))) /= N_Null
;
8736 end Is_Verifiable_DIC_Pragma
;
8738 ---------------------------
8739 -- Is_Volatile_Reference --
8740 ---------------------------
8742 function Is_Volatile_Reference
(N
: Node_Id
) return Boolean is
8744 -- Only source references are to be treated as volatile, internally
8745 -- generated stuff cannot have volatile external effects.
8747 if not Comes_From_Source
(N
) then
8750 -- Never true for reference to a type
8752 elsif Is_Entity_Name
(N
) and then Is_Type
(Entity
(N
)) then
8755 -- Never true for a compile time known constant
8757 elsif Compile_Time_Known_Value
(N
) then
8760 -- True if object reference with volatile type
8762 elsif Is_Volatile_Object
(N
) then
8765 -- True if reference to volatile entity
8767 elsif Is_Entity_Name
(N
) then
8768 return Treat_As_Volatile
(Entity
(N
));
8770 -- True for slice of volatile array
8772 elsif Nkind
(N
) = N_Slice
then
8773 return Is_Volatile_Reference
(Prefix
(N
));
8775 -- True if volatile component
8777 elsif Nkind_In
(N
, N_Indexed_Component
, N_Selected_Component
) then
8778 if (Is_Entity_Name
(Prefix
(N
))
8779 and then Has_Volatile_Components
(Entity
(Prefix
(N
))))
8780 or else (Present
(Etype
(Prefix
(N
)))
8781 and then Has_Volatile_Components
(Etype
(Prefix
(N
))))
8785 return Is_Volatile_Reference
(Prefix
(N
));
8793 end Is_Volatile_Reference
;
8795 --------------------
8796 -- Kill_Dead_Code --
8797 --------------------
8799 procedure Kill_Dead_Code
(N
: Node_Id
; Warn
: Boolean := False) is
8800 W
: Boolean := Warn
;
8801 -- Set False if warnings suppressed
8805 Remove_Warning_Messages
(N
);
8807 -- Update the internal structures of the ABE mechanism in case the
8808 -- dead node is an elaboration scenario.
8810 Kill_Elaboration_Scenario
(N
);
8812 -- Generate warning if appropriate
8816 -- We suppress the warning if this code is under control of an
8817 -- if statement, whose condition is a simple identifier, and
8818 -- either we are in an instance, or warnings off is set for this
8819 -- identifier. The reason for killing it in the instance case is
8820 -- that it is common and reasonable for code to be deleted in
8821 -- instances for various reasons.
8823 -- Could we use Is_Statically_Unevaluated here???
8825 if Nkind
(Parent
(N
)) = N_If_Statement
then
8827 C
: constant Node_Id
:= Condition
(Parent
(N
));
8829 if Nkind
(C
) = N_Identifier
8832 or else (Present
(Entity
(C
))
8833 and then Has_Warnings_Off
(Entity
(C
))))
8840 -- Generate warning if not suppressed
8844 ("?t?this code can never be executed and has been deleted!",
8849 -- Recurse into block statements and bodies to process declarations
8852 if Nkind
(N
) = N_Block_Statement
8853 or else Nkind
(N
) = N_Subprogram_Body
8854 or else Nkind
(N
) = N_Package_Body
8856 Kill_Dead_Code
(Declarations
(N
), False);
8857 Kill_Dead_Code
(Statements
(Handled_Statement_Sequence
(N
)));
8859 if Nkind
(N
) = N_Subprogram_Body
then
8860 Set_Is_Eliminated
(Defining_Entity
(N
));
8863 elsif Nkind
(N
) = N_Package_Declaration
then
8864 Kill_Dead_Code
(Visible_Declarations
(Specification
(N
)));
8865 Kill_Dead_Code
(Private_Declarations
(Specification
(N
)));
8867 -- ??? After this point, Delete_Tree has been called on all
8868 -- declarations in Specification (N), so references to entities
8869 -- therein look suspicious.
8872 E
: Entity_Id
:= First_Entity
(Defining_Entity
(N
));
8875 while Present
(E
) loop
8876 if Ekind
(E
) = E_Operator
then
8877 Set_Is_Eliminated
(E
);
8884 -- Recurse into composite statement to kill individual statements in
8885 -- particular instantiations.
8887 elsif Nkind
(N
) = N_If_Statement
then
8888 Kill_Dead_Code
(Then_Statements
(N
));
8889 Kill_Dead_Code
(Elsif_Parts
(N
));
8890 Kill_Dead_Code
(Else_Statements
(N
));
8892 elsif Nkind
(N
) = N_Loop_Statement
then
8893 Kill_Dead_Code
(Statements
(N
));
8895 elsif Nkind
(N
) = N_Case_Statement
then
8899 Alt
:= First
(Alternatives
(N
));
8900 while Present
(Alt
) loop
8901 Kill_Dead_Code
(Statements
(Alt
));
8906 elsif Nkind
(N
) = N_Case_Statement_Alternative
then
8907 Kill_Dead_Code
(Statements
(N
));
8909 -- Deal with dead instances caused by deleting instantiations
8911 elsif Nkind
(N
) in N_Generic_Instantiation
then
8912 Remove_Dead_Instance
(N
);
8917 -- Case where argument is a list of nodes to be killed
8919 procedure Kill_Dead_Code
(L
: List_Id
; Warn
: Boolean := False) is
8926 if Is_Non_Empty_List
(L
) then
8928 while Present
(N
) loop
8929 Kill_Dead_Code
(N
, W
);
8936 ------------------------
8937 -- Known_Non_Negative --
8938 ------------------------
8940 function Known_Non_Negative
(Opnd
: Node_Id
) return Boolean is
8942 if Is_OK_Static_Expression
(Opnd
) and then Expr_Value
(Opnd
) >= 0 then
8947 Lo
: constant Node_Id
:= Type_Low_Bound
(Etype
(Opnd
));
8950 Is_OK_Static_Expression
(Lo
) and then Expr_Value
(Lo
) >= 0;
8953 end Known_Non_Negative
;
8955 -----------------------------
8956 -- Make_CW_Equivalent_Type --
8957 -----------------------------
8959 -- Create a record type used as an equivalent of any member of the class
8960 -- which takes its size from exp.
8962 -- Generate the following code:
8964 -- type Equiv_T is record
8965 -- _parent : T (List of discriminant constraints taken from Exp);
8966 -- Ext__50 : Storage_Array (1 .. (Exp'size - Typ'object_size)/8);
8969 -- ??? Note that this type does not guarantee same alignment as all
8972 function Make_CW_Equivalent_Type
8974 E
: Node_Id
) return Entity_Id
8976 Loc
: constant Source_Ptr
:= Sloc
(E
);
8977 Root_Typ
: constant Entity_Id
:= Root_Type
(T
);
8978 List_Def
: constant List_Id
:= Empty_List
;
8979 Comp_List
: constant List_Id
:= New_List
;
8980 Equiv_Type
: Entity_Id
;
8981 Range_Type
: Entity_Id
;
8982 Str_Type
: Entity_Id
;
8983 Constr_Root
: Entity_Id
;
8987 -- If the root type is already constrained, there are no discriminants
8988 -- in the expression.
8990 if not Has_Discriminants
(Root_Typ
)
8991 or else Is_Constrained
(Root_Typ
)
8993 Constr_Root
:= Root_Typ
;
8995 -- At this point in the expansion, non-limited view of the type
8996 -- must be available, otherwise the error will be reported later.
8998 if From_Limited_With
(Constr_Root
)
8999 and then Present
(Non_Limited_View
(Constr_Root
))
9001 Constr_Root
:= Non_Limited_View
(Constr_Root
);
9005 Constr_Root
:= Make_Temporary
(Loc
, 'R');
9007 -- subtype cstr__n is T (List of discr constraints taken from Exp)
9009 Append_To
(List_Def
,
9010 Make_Subtype_Declaration
(Loc
,
9011 Defining_Identifier
=> Constr_Root
,
9012 Subtype_Indication
=> Make_Subtype_From_Expr
(E
, Root_Typ
)));
9015 -- Generate the range subtype declaration
9017 Range_Type
:= Make_Temporary
(Loc
, 'G');
9019 if not Is_Interface
(Root_Typ
) then
9021 -- subtype rg__xx is
9022 -- Storage_Offset range 1 .. (Expr'size - typ'size) / Storage_Unit
9025 Make_Op_Subtract
(Loc
,
9027 Make_Attribute_Reference
(Loc
,
9029 OK_Convert_To
(T
, Duplicate_Subexpr_No_Checks
(E
)),
9030 Attribute_Name
=> Name_Size
),
9032 Make_Attribute_Reference
(Loc
,
9033 Prefix
=> New_Occurrence_Of
(Constr_Root
, Loc
),
9034 Attribute_Name
=> Name_Object_Size
));
9036 -- subtype rg__xx is
9037 -- Storage_Offset range 1 .. Expr'size / Storage_Unit
9040 Make_Attribute_Reference
(Loc
,
9042 OK_Convert_To
(T
, Duplicate_Subexpr_No_Checks
(E
)),
9043 Attribute_Name
=> Name_Size
);
9046 Set_Paren_Count
(Sizexpr
, 1);
9048 Append_To
(List_Def
,
9049 Make_Subtype_Declaration
(Loc
,
9050 Defining_Identifier
=> Range_Type
,
9051 Subtype_Indication
=>
9052 Make_Subtype_Indication
(Loc
,
9053 Subtype_Mark
=> New_Occurrence_Of
(RTE
(RE_Storage_Offset
), Loc
),
9054 Constraint
=> Make_Range_Constraint
(Loc
,
9057 Low_Bound
=> Make_Integer_Literal
(Loc
, 1),
9059 Make_Op_Divide
(Loc
,
9060 Left_Opnd
=> Sizexpr
,
9061 Right_Opnd
=> Make_Integer_Literal
(Loc
,
9062 Intval
=> System_Storage_Unit
)))))));
9064 -- subtype str__nn is Storage_Array (rg__x);
9066 Str_Type
:= Make_Temporary
(Loc
, 'S');
9067 Append_To
(List_Def
,
9068 Make_Subtype_Declaration
(Loc
,
9069 Defining_Identifier
=> Str_Type
,
9070 Subtype_Indication
=>
9071 Make_Subtype_Indication
(Loc
,
9072 Subtype_Mark
=> New_Occurrence_Of
(RTE
(RE_Storage_Array
), Loc
),
9074 Make_Index_Or_Discriminant_Constraint
(Loc
,
9076 New_List
(New_Occurrence_Of
(Range_Type
, Loc
))))));
9078 -- type Equiv_T is record
9079 -- [ _parent : Tnn; ]
9083 Equiv_Type
:= Make_Temporary
(Loc
, 'T');
9084 Set_Ekind
(Equiv_Type
, E_Record_Type
);
9085 Set_Parent_Subtype
(Equiv_Type
, Constr_Root
);
9087 -- Set Is_Class_Wide_Equivalent_Type very early to trigger the special
9088 -- treatment for this type. In particular, even though _parent's type
9089 -- is a controlled type or contains controlled components, we do not
9090 -- want to set Has_Controlled_Component on it to avoid making it gain
9091 -- an unwanted _controller component.
9093 Set_Is_Class_Wide_Equivalent_Type
(Equiv_Type
);
9095 -- A class-wide equivalent type does not require initialization
9097 Set_Suppress_Initialization
(Equiv_Type
);
9099 if not Is_Interface
(Root_Typ
) then
9100 Append_To
(Comp_List
,
9101 Make_Component_Declaration
(Loc
,
9102 Defining_Identifier
=>
9103 Make_Defining_Identifier
(Loc
, Name_uParent
),
9104 Component_Definition
=>
9105 Make_Component_Definition
(Loc
,
9106 Aliased_Present
=> False,
9107 Subtype_Indication
=> New_Occurrence_Of
(Constr_Root
, Loc
))));
9110 Append_To
(Comp_List
,
9111 Make_Component_Declaration
(Loc
,
9112 Defining_Identifier
=> Make_Temporary
(Loc
, 'C'),
9113 Component_Definition
=>
9114 Make_Component_Definition
(Loc
,
9115 Aliased_Present
=> False,
9116 Subtype_Indication
=> New_Occurrence_Of
(Str_Type
, Loc
))));
9118 Append_To
(List_Def
,
9119 Make_Full_Type_Declaration
(Loc
,
9120 Defining_Identifier
=> Equiv_Type
,
9122 Make_Record_Definition
(Loc
,
9124 Make_Component_List
(Loc
,
9125 Component_Items
=> Comp_List
,
9126 Variant_Part
=> Empty
))));
9128 -- Suppress all checks during the analysis of the expanded code to avoid
9129 -- the generation of spurious warnings under ZFP run-time.
9131 Insert_Actions
(E
, List_Def
, Suppress
=> All_Checks
);
9133 end Make_CW_Equivalent_Type
;
9135 -------------------------
9136 -- Make_Invariant_Call --
9137 -------------------------
9139 function Make_Invariant_Call
(Expr
: Node_Id
) return Node_Id
is
9140 Loc
: constant Source_Ptr
:= Sloc
(Expr
);
9141 Typ
: constant Entity_Id
:= Base_Type
(Etype
(Expr
));
9143 Proc_Id
: Entity_Id
;
9146 pragma Assert
(Has_Invariants
(Typ
));
9148 Proc_Id
:= Invariant_Procedure
(Typ
);
9149 pragma Assert
(Present
(Proc_Id
));
9152 Make_Procedure_Call_Statement
(Loc
,
9153 Name
=> New_Occurrence_Of
(Proc_Id
, Loc
),
9154 Parameter_Associations
=> New_List
(Relocate_Node
(Expr
)));
9155 end Make_Invariant_Call
;
9157 ------------------------
9158 -- Make_Literal_Range --
9159 ------------------------
9161 function Make_Literal_Range
9163 Literal_Typ
: Entity_Id
) return Node_Id
9165 Lo
: constant Node_Id
:=
9166 New_Copy_Tree
(String_Literal_Low_Bound
(Literal_Typ
));
9167 Index
: constant Entity_Id
:= Etype
(Lo
);
9168 Length_Expr
: constant Node_Id
:=
9169 Make_Op_Subtract
(Loc
,
9171 Make_Integer_Literal
(Loc
,
9172 Intval
=> String_Literal_Length
(Literal_Typ
)),
9173 Right_Opnd
=> Make_Integer_Literal
(Loc
, 1));
9178 Set_Analyzed
(Lo
, False);
9180 if Is_Integer_Type
(Index
) then
9183 Left_Opnd
=> New_Copy_Tree
(Lo
),
9184 Right_Opnd
=> Length_Expr
);
9187 Make_Attribute_Reference
(Loc
,
9188 Attribute_Name
=> Name_Val
,
9189 Prefix
=> New_Occurrence_Of
(Index
, Loc
),
9190 Expressions
=> New_List
(
9193 Make_Attribute_Reference
(Loc
,
9194 Attribute_Name
=> Name_Pos
,
9195 Prefix
=> New_Occurrence_Of
(Index
, Loc
),
9196 Expressions
=> New_List
(New_Copy_Tree
(Lo
))),
9197 Right_Opnd
=> Length_Expr
)));
9204 end Make_Literal_Range
;
9206 --------------------------
9207 -- Make_Non_Empty_Check --
9208 --------------------------
9210 function Make_Non_Empty_Check
9212 N
: Node_Id
) return Node_Id
9218 Make_Attribute_Reference
(Loc
,
9219 Attribute_Name
=> Name_Length
,
9220 Prefix
=> Duplicate_Subexpr_No_Checks
(N
, Name_Req
=> True)),
9222 Make_Integer_Literal
(Loc
, 0));
9223 end Make_Non_Empty_Check
;
9225 -------------------------
9226 -- Make_Predicate_Call --
9227 -------------------------
9229 -- WARNING: This routine manages Ghost regions. Return statements must be
9230 -- replaced by gotos which jump to the end of the routine and restore the
9233 function Make_Predicate_Call
9236 Mem
: Boolean := False) return Node_Id
9238 Loc
: constant Source_Ptr
:= Sloc
(Expr
);
9240 Saved_GM
: constant Ghost_Mode_Type
:= Ghost_Mode
;
9241 -- Save the Ghost mode to restore on exit
9244 Func_Id
: Entity_Id
;
9247 pragma Assert
(Present
(Predicate_Function
(Typ
)));
9249 -- The related type may be subject to pragma Ghost. Set the mode now to
9250 -- ensure that the call is properly marked as Ghost.
9252 Set_Ghost_Mode
(Typ
);
9254 -- Call special membership version if requested and available
9256 if Mem
and then Present
(Predicate_Function_M
(Typ
)) then
9257 Func_Id
:= Predicate_Function_M
(Typ
);
9259 Func_Id
:= Predicate_Function
(Typ
);
9262 -- Case of calling normal predicate function
9264 -- If the type is tagged, the expression may be class-wide, in which
9265 -- case it has to be converted to its root type, given that the
9266 -- generated predicate function is not dispatching.
9268 if Is_Tagged_Type
(Typ
) then
9270 Make_Function_Call
(Loc
,
9271 Name
=> New_Occurrence_Of
(Func_Id
, Loc
),
9272 Parameter_Associations
=>
9273 New_List
(Convert_To
(Typ
, Relocate_Node
(Expr
))));
9276 Make_Function_Call
(Loc
,
9277 Name
=> New_Occurrence_Of
(Func_Id
, Loc
),
9278 Parameter_Associations
=> New_List
(Relocate_Node
(Expr
)));
9281 Restore_Ghost_Mode
(Saved_GM
);
9284 end Make_Predicate_Call
;
9286 --------------------------
9287 -- Make_Predicate_Check --
9288 --------------------------
9290 function Make_Predicate_Check
9292 Expr
: Node_Id
) return Node_Id
9294 procedure Replace_Subtype_Reference
(N
: Node_Id
);
9295 -- Replace current occurrences of the subtype to which a dynamic
9296 -- predicate applies, by the expression that triggers a predicate
9297 -- check. This is needed for aspect Predicate_Failure, for which
9298 -- we do not generate a wrapper procedure, but simply modify the
9299 -- expression for the pragma of the predicate check.
9301 --------------------------------
9302 -- Replace_Subtype_Reference --
9303 --------------------------------
9305 procedure Replace_Subtype_Reference
(N
: Node_Id
) is
9307 Rewrite
(N
, New_Copy_Tree
(Expr
));
9309 -- We want to treat the node as if it comes from source, so
9310 -- that ASIS will not ignore it.
9312 Set_Comes_From_Source
(N
, True);
9313 end Replace_Subtype_Reference
;
9315 procedure Replace_Subtype_References
is
9316 new Replace_Type_References_Generic
(Replace_Subtype_Reference
);
9320 Loc
: constant Source_Ptr
:= Sloc
(Expr
);
9322 Fail_Expr
: Node_Id
;
9325 -- Start of processing for Make_Predicate_Check
9328 -- If predicate checks are suppressed, then return a null statement. For
9329 -- this call, we check only the scope setting. If the caller wants to
9330 -- check a specific entity's setting, they must do it manually.
9332 if Predicate_Checks_Suppressed
(Empty
) then
9333 return Make_Null_Statement
(Loc
);
9336 -- Do not generate a check within an internal subprogram (stream
9337 -- functions and the like, including including predicate functions).
9339 if Within_Internal_Subprogram
then
9340 return Make_Null_Statement
(Loc
);
9343 -- Compute proper name to use, we need to get this right so that the
9344 -- right set of check policies apply to the Check pragma we are making.
9346 if Has_Dynamic_Predicate_Aspect
(Typ
) then
9347 Nam
:= Name_Dynamic_Predicate
;
9348 elsif Has_Static_Predicate_Aspect
(Typ
) then
9349 Nam
:= Name_Static_Predicate
;
9351 Nam
:= Name_Predicate
;
9354 Arg_List
:= New_List
(
9355 Make_Pragma_Argument_Association
(Loc
,
9356 Expression
=> Make_Identifier
(Loc
, Nam
)),
9357 Make_Pragma_Argument_Association
(Loc
,
9358 Expression
=> Make_Predicate_Call
(Typ
, Expr
)));
9360 -- If subtype has Predicate_Failure defined, add the correponding
9361 -- expression as an additional pragma parameter, after replacing
9362 -- current instances with the expression being checked.
9364 if Has_Aspect
(Typ
, Aspect_Predicate_Failure
) then
9367 (Expression
(Find_Aspect
(Typ
, Aspect_Predicate_Failure
)));
9368 Replace_Subtype_References
(Fail_Expr
, Typ
);
9370 Append_To
(Arg_List
,
9371 Make_Pragma_Argument_Association
(Loc
,
9372 Expression
=> Fail_Expr
));
9377 Chars
=> Name_Check
,
9378 Pragma_Argument_Associations
=> Arg_List
);
9379 end Make_Predicate_Check
;
9381 ----------------------------
9382 -- Make_Subtype_From_Expr --
9383 ----------------------------
9385 -- 1. If Expr is an unconstrained array expression, creates
9386 -- Unc_Type(Expr'first(1)..Expr'last(1),..., Expr'first(n)..Expr'last(n))
9388 -- 2. If Expr is a unconstrained discriminated type expression, creates
9389 -- Unc_Type(Expr.Discr1, ... , Expr.Discr_n)
9391 -- 3. If Expr is class-wide, creates an implicit class-wide subtype
9393 function Make_Subtype_From_Expr
9395 Unc_Typ
: Entity_Id
;
9396 Related_Id
: Entity_Id
:= Empty
) return Node_Id
9398 List_Constr
: constant List_Id
:= New_List
;
9399 Loc
: constant Source_Ptr
:= Sloc
(E
);
9402 Full_Subtyp
: Entity_Id
;
9403 High_Bound
: Entity_Id
;
9404 Index_Typ
: Entity_Id
;
9405 Low_Bound
: Entity_Id
;
9406 Priv_Subtyp
: Entity_Id
;
9410 if Is_Private_Type
(Unc_Typ
)
9411 and then Has_Unknown_Discriminants
(Unc_Typ
)
9413 -- The caller requests a unique external name for both the private
9414 -- and the full subtype.
9416 if Present
(Related_Id
) then
9418 Make_Defining_Identifier
(Loc
,
9419 Chars
=> New_External_Name
(Chars
(Related_Id
), 'C'));
9421 Make_Defining_Identifier
(Loc
,
9422 Chars
=> New_External_Name
(Chars
(Related_Id
), 'P'));
9425 Full_Subtyp
:= Make_Temporary
(Loc
, 'C');
9426 Priv_Subtyp
:= Make_Temporary
(Loc
, 'P');
9429 -- Prepare the subtype completion. Use the base type to find the
9430 -- underlying type because the type may be a generic actual or an
9431 -- explicit subtype.
9433 Utyp
:= Underlying_Type
(Base_Type
(Unc_Typ
));
9436 Unchecked_Convert_To
(Utyp
, Duplicate_Subexpr_No_Checks
(E
));
9437 Set_Parent
(Full_Exp
, Parent
(E
));
9440 Make_Subtype_Declaration
(Loc
,
9441 Defining_Identifier
=> Full_Subtyp
,
9442 Subtype_Indication
=> Make_Subtype_From_Expr
(Full_Exp
, Utyp
)));
9444 -- Define the dummy private subtype
9446 Set_Ekind
(Priv_Subtyp
, Subtype_Kind
(Ekind
(Unc_Typ
)));
9447 Set_Etype
(Priv_Subtyp
, Base_Type
(Unc_Typ
));
9448 Set_Scope
(Priv_Subtyp
, Full_Subtyp
);
9449 Set_Is_Constrained
(Priv_Subtyp
);
9450 Set_Is_Tagged_Type
(Priv_Subtyp
, Is_Tagged_Type
(Unc_Typ
));
9451 Set_Is_Itype
(Priv_Subtyp
);
9452 Set_Associated_Node_For_Itype
(Priv_Subtyp
, E
);
9454 if Is_Tagged_Type
(Priv_Subtyp
) then
9456 (Base_Type
(Priv_Subtyp
), Class_Wide_Type
(Unc_Typ
));
9457 Set_Direct_Primitive_Operations
(Priv_Subtyp
,
9458 Direct_Primitive_Operations
(Unc_Typ
));
9461 Set_Full_View
(Priv_Subtyp
, Full_Subtyp
);
9463 return New_Occurrence_Of
(Priv_Subtyp
, Loc
);
9465 elsif Is_Array_Type
(Unc_Typ
) then
9466 Index_Typ
:= First_Index
(Unc_Typ
);
9467 for J
in 1 .. Number_Dimensions
(Unc_Typ
) loop
9469 -- Capture the bounds of each index constraint in case the context
9470 -- is an object declaration of an unconstrained type initialized
9471 -- by a function call:
9473 -- Obj : Unconstr_Typ := Func_Call;
9475 -- This scenario requires secondary scope management and the index
9476 -- constraint cannot depend on the temporary used to capture the
9477 -- result of the function call.
9480 -- Temp : Unconstr_Typ_Ptr := Func_Call'reference;
9481 -- subtype S is Unconstr_Typ (Temp.all'First .. Temp.all'Last);
9482 -- Obj : S := Temp.all;
9483 -- SS_Release; -- Temp is gone at this point, bounds of S are
9487 -- Low_Bound : constant Base_Type (Index_Typ) := E'First (J);
9489 Low_Bound
:= Make_Temporary
(Loc
, 'B');
9491 Make_Object_Declaration
(Loc
,
9492 Defining_Identifier
=> Low_Bound
,
9493 Object_Definition
=>
9494 New_Occurrence_Of
(Base_Type
(Etype
(Index_Typ
)), Loc
),
9495 Constant_Present
=> True,
9497 Make_Attribute_Reference
(Loc
,
9498 Prefix
=> Duplicate_Subexpr_No_Checks
(E
),
9499 Attribute_Name
=> Name_First
,
9500 Expressions
=> New_List
(
9501 Make_Integer_Literal
(Loc
, J
)))));
9504 -- High_Bound : constant Base_Type (Index_Typ) := E'Last (J);
9506 High_Bound
:= Make_Temporary
(Loc
, 'B');
9508 Make_Object_Declaration
(Loc
,
9509 Defining_Identifier
=> High_Bound
,
9510 Object_Definition
=>
9511 New_Occurrence_Of
(Base_Type
(Etype
(Index_Typ
)), Loc
),
9512 Constant_Present
=> True,
9514 Make_Attribute_Reference
(Loc
,
9515 Prefix
=> Duplicate_Subexpr_No_Checks
(E
),
9516 Attribute_Name
=> Name_Last
,
9517 Expressions
=> New_List
(
9518 Make_Integer_Literal
(Loc
, J
)))));
9520 Append_To
(List_Constr
,
9522 Low_Bound
=> New_Occurrence_Of
(Low_Bound
, Loc
),
9523 High_Bound
=> New_Occurrence_Of
(High_Bound
, Loc
)));
9525 Index_Typ
:= Next_Index
(Index_Typ
);
9528 elsif Is_Class_Wide_Type
(Unc_Typ
) then
9530 CW_Subtype
: Entity_Id
;
9531 EQ_Typ
: Entity_Id
:= Empty
;
9534 -- A class-wide equivalent type is not needed on VM targets
9535 -- because the VM back-ends handle the class-wide object
9536 -- initialization itself (and doesn't need or want the
9537 -- additional intermediate type to handle the assignment).
9539 if Expander_Active
and then Tagged_Type_Expansion
then
9541 -- If this is the class-wide type of a completion that is a
9542 -- record subtype, set the type of the class-wide type to be
9543 -- the full base type, for use in the expanded code for the
9544 -- equivalent type. Should this be done earlier when the
9545 -- completion is analyzed ???
9547 if Is_Private_Type
(Etype
(Unc_Typ
))
9549 Ekind
(Full_View
(Etype
(Unc_Typ
))) = E_Record_Subtype
9551 Set_Etype
(Unc_Typ
, Base_Type
(Full_View
(Etype
(Unc_Typ
))));
9554 EQ_Typ
:= Make_CW_Equivalent_Type
(Unc_Typ
, E
);
9557 CW_Subtype
:= New_Class_Wide_Subtype
(Unc_Typ
, E
);
9558 Set_Equivalent_Type
(CW_Subtype
, EQ_Typ
);
9559 Set_Cloned_Subtype
(CW_Subtype
, Base_Type
(Unc_Typ
));
9561 return New_Occurrence_Of
(CW_Subtype
, Loc
);
9564 -- Indefinite record type with discriminants
9567 D
:= First_Discriminant
(Unc_Typ
);
9568 while Present
(D
) loop
9569 Append_To
(List_Constr
,
9570 Make_Selected_Component
(Loc
,
9571 Prefix
=> Duplicate_Subexpr_No_Checks
(E
),
9572 Selector_Name
=> New_Occurrence_Of
(D
, Loc
)));
9574 Next_Discriminant
(D
);
9579 Make_Subtype_Indication
(Loc
,
9580 Subtype_Mark
=> New_Occurrence_Of
(Unc_Typ
, Loc
),
9582 Make_Index_Or_Discriminant_Constraint
(Loc
,
9583 Constraints
=> List_Constr
));
9584 end Make_Subtype_From_Expr
;
9590 procedure Map_Types
(Parent_Type
: Entity_Id
; Derived_Type
: Entity_Id
) is
9592 -- NOTE: Most of the routines in Map_Types are intentionally unnested to
9593 -- avoid deep indentation of code.
9595 -- NOTE: Routines which deal with discriminant mapping operate on the
9596 -- [underlying/record] full view of various types because those views
9597 -- contain all discriminants and stored constraints.
9599 procedure Add_Primitive
(Prim
: Entity_Id
; Par_Typ
: Entity_Id
);
9600 -- Subsidiary to Map_Primitives. Find a primitive in the inheritance or
9601 -- overriding chain starting from Prim whose dispatching type is parent
9602 -- type Par_Typ and add a mapping between the result and primitive Prim.
9604 function Ancestor_Primitive
(Subp
: Entity_Id
) return Entity_Id
;
9605 -- Subsidiary to Map_Primitives. Return the next ancestor primitive in
9606 -- the inheritance or overriding chain of subprogram Subp. Return Empty
9607 -- if no such primitive is available.
9609 function Build_Chain
9610 (Par_Typ
: Entity_Id
;
9611 Deriv_Typ
: Entity_Id
) return Elist_Id
;
9612 -- Subsidiary to Map_Discriminants. Recreate the derivation chain from
9613 -- parent type Par_Typ leading down towards derived type Deriv_Typ. The
9614 -- list has the form:
9618 -- <Ancestor_N> -> <Ancestor_N-1> -> <Ancestor_1> -> Deriv_Typ
9620 -- Note that Par_Typ is not part of the resulting derivation chain
9622 function Discriminated_View
(Typ
: Entity_Id
) return Entity_Id
;
9623 -- Return the view of type Typ which could potentially contains either
9624 -- the discriminants or stored constraints of the type.
9626 function Find_Discriminant_Value
9628 Par_Typ
: Entity_Id
;
9629 Deriv_Typ
: Entity_Id
;
9630 Typ_Elmt
: Elmt_Id
) return Node_Or_Entity_Id
;
9631 -- Subsidiary to Map_Discriminants. Find the value of discriminant Discr
9632 -- in the derivation chain starting from parent type Par_Typ leading to
9633 -- derived type Deriv_Typ. The returned value is one of the following:
9635 -- * An entity which is either a discriminant or a non-discriminant
9636 -- name, and renames/constraints Discr.
9638 -- * An expression which constraints Discr
9640 -- Typ_Elmt is an element of the derivation chain created by routine
9641 -- Build_Chain and denotes the current ancestor being examined.
9643 procedure Map_Discriminants
9644 (Par_Typ
: Entity_Id
;
9645 Deriv_Typ
: Entity_Id
);
9646 -- Map each discriminant of type Par_Typ to a meaningful constraint
9647 -- from the point of view of type Deriv_Typ.
9649 procedure Map_Primitives
(Par_Typ
: Entity_Id
; Deriv_Typ
: Entity_Id
);
9650 -- Map each primitive of type Par_Typ to a corresponding primitive of
9657 procedure Add_Primitive
(Prim
: Entity_Id
; Par_Typ
: Entity_Id
) is
9658 Par_Prim
: Entity_Id
;
9661 -- Inspect the inheritance chain through the Alias attribute and the
9662 -- overriding chain through the Overridden_Operation looking for an
9663 -- ancestor primitive with the appropriate dispatching type.
9666 while Present
(Par_Prim
) loop
9667 exit when Find_Dispatching_Type
(Par_Prim
) = Par_Typ
;
9668 Par_Prim
:= Ancestor_Primitive
(Par_Prim
);
9671 -- Create a mapping of the form:
9673 -- parent type primitive -> derived type primitive
9675 if Present
(Par_Prim
) then
9676 Type_Map
.Set
(Par_Prim
, Prim
);
9680 ------------------------
9681 -- Ancestor_Primitive --
9682 ------------------------
9684 function Ancestor_Primitive
(Subp
: Entity_Id
) return Entity_Id
is
9685 Inher_Prim
: constant Entity_Id
:= Alias
(Subp
);
9686 Over_Prim
: constant Entity_Id
:= Overridden_Operation
(Subp
);
9689 -- The current subprogram overrides an ancestor primitive
9691 if Present
(Over_Prim
) then
9694 -- The current subprogram is an internally generated alias of an
9695 -- inherited ancestor primitive.
9697 elsif Present
(Inher_Prim
) then
9700 -- Otherwise the current subprogram is the root of the inheritance or
9701 -- overriding chain.
9706 end Ancestor_Primitive
;
9712 function Build_Chain
9713 (Par_Typ
: Entity_Id
;
9714 Deriv_Typ
: Entity_Id
) return Elist_Id
9716 Anc_Typ
: Entity_Id
;
9718 Curr_Typ
: Entity_Id
;
9721 Chain
:= New_Elmt_List
;
9723 -- Add the derived type to the derivation chain
9725 Prepend_Elmt
(Deriv_Typ
, Chain
);
9727 -- Examine all ancestors starting from the derived type climbing
9728 -- towards parent type Par_Typ.
9730 Curr_Typ
:= Deriv_Typ
;
9732 -- Handle the case where the current type is a record which
9733 -- derives from a subtype.
9735 -- subtype Sub_Typ is Par_Typ ...
9736 -- type Deriv_Typ is Sub_Typ ...
9738 if Ekind
(Curr_Typ
) = E_Record_Type
9739 and then Present
(Parent_Subtype
(Curr_Typ
))
9741 Anc_Typ
:= Parent_Subtype
(Curr_Typ
);
9743 -- Handle the case where the current type is a record subtype of
9746 -- subtype Sub_Typ1 is Par_Typ ...
9747 -- subtype Sub_Typ2 is Sub_Typ1 ...
9749 elsif Ekind
(Curr_Typ
) = E_Record_Subtype
9750 and then Present
(Cloned_Subtype
(Curr_Typ
))
9752 Anc_Typ
:= Cloned_Subtype
(Curr_Typ
);
9754 -- Otherwise use the direct parent type
9757 Anc_Typ
:= Etype
(Curr_Typ
);
9760 -- Use the first subtype when dealing with itypes
9762 if Is_Itype
(Anc_Typ
) then
9763 Anc_Typ
:= First_Subtype
(Anc_Typ
);
9766 -- Work with the view which contains the discriminants and stored
9769 Anc_Typ
:= Discriminated_View
(Anc_Typ
);
9771 -- Stop the climb when either the parent type has been reached or
9772 -- there are no more ancestors left to examine.
9774 exit when Anc_Typ
= Curr_Typ
or else Anc_Typ
= Par_Typ
;
9776 Prepend_Unique_Elmt
(Anc_Typ
, Chain
);
9777 Curr_Typ
:= Anc_Typ
;
9783 ------------------------
9784 -- Discriminated_View --
9785 ------------------------
9787 function Discriminated_View
(Typ
: Entity_Id
) return Entity_Id
is
9793 -- Use the [underlying] full view when dealing with private types
9794 -- because the view contains all inherited discriminants or stored
9797 if Is_Private_Type
(T
) then
9798 if Present
(Underlying_Full_View
(T
)) then
9799 T
:= Underlying_Full_View
(T
);
9801 elsif Present
(Full_View
(T
)) then
9806 -- Use the underlying record view when the type is an extenstion of
9807 -- a parent type with unknown discriminants because the view contains
9808 -- all inherited discriminants or stored constraints.
9810 if Ekind
(T
) = E_Record_Type
9811 and then Present
(Underlying_Record_View
(T
))
9813 T
:= Underlying_Record_View
(T
);
9817 end Discriminated_View
;
9819 -----------------------------
9820 -- Find_Discriminant_Value --
9821 -----------------------------
9823 function Find_Discriminant_Value
9825 Par_Typ
: Entity_Id
;
9826 Deriv_Typ
: Entity_Id
;
9827 Typ_Elmt
: Elmt_Id
) return Node_Or_Entity_Id
9829 Discr_Pos
: constant Uint
:= Discriminant_Number
(Discr
);
9830 Typ
: constant Entity_Id
:= Node
(Typ_Elmt
);
9832 function Find_Constraint_Value
9833 (Constr
: Node_Or_Entity_Id
) return Node_Or_Entity_Id
;
9834 -- Given constraint Constr, find what it denotes. This is either:
9836 -- * An entity which is either a discriminant or a name
9840 ---------------------------
9841 -- Find_Constraint_Value --
9842 ---------------------------
9844 function Find_Constraint_Value
9845 (Constr
: Node_Or_Entity_Id
) return Node_Or_Entity_Id
9848 if Nkind
(Constr
) in N_Entity
then
9850 -- The constraint denotes a discriminant of the curren type
9851 -- which renames the ancestor discriminant:
9854 -- type Typ (D1 : ...; DN : ...) is
9855 -- new Anc (Discr => D1) with ...
9858 if Ekind
(Constr
) = E_Discriminant
then
9860 -- The discriminant belongs to derived type Deriv_Typ. This
9861 -- is the final value for the ancestor discriminant as the
9862 -- derivations chain has been fully exhausted.
9864 if Typ
= Deriv_Typ
then
9867 -- Otherwise the discriminant may be renamed or constrained
9868 -- at a lower level. Continue looking down the derivation
9873 Find_Discriminant_Value
9876 Deriv_Typ
=> Deriv_Typ
,
9877 Typ_Elmt
=> Next_Elmt
(Typ_Elmt
));
9880 -- Otherwise the constraint denotes a reference to some name
9881 -- which results in a Girder discriminant:
9885 -- type Typ (D1 : ...; DN : ...) is
9886 -- new Anc (Discr => Name) with ...
9889 -- Return the name as this is the proper constraint of the
9896 -- The constraint denotes a reference to a name
9898 elsif Is_Entity_Name
(Constr
) then
9899 return Find_Constraint_Value
(Entity
(Constr
));
9901 -- Otherwise the current constraint is an expression which yields
9902 -- a Girder discriminant:
9904 -- type Typ (D1 : ...; DN : ...) is
9905 -- new Anc (Discr => <expression>) with ...
9908 -- Return the expression as this is the proper constraint of the
9914 end Find_Constraint_Value
;
9918 Constrs
: constant Elist_Id
:= Stored_Constraint
(Typ
);
9920 Constr_Elmt
: Elmt_Id
;
9922 Typ_Discr
: Entity_Id
;
9924 -- Start of processing for Find_Discriminant_Value
9927 -- The algorithm for finding the value of a discriminant works as
9928 -- follows. First, it recreates the derivation chain from Par_Typ
9929 -- to Deriv_Typ as a list:
9931 -- Par_Typ (shown for completeness)
9933 -- Ancestor_N <-- head of chain
9937 -- Deriv_Typ <-- tail of chain
9939 -- The algorithm then traces the fate of a parent discriminant down
9940 -- the derivation chain. At each derivation level, the discriminant
9941 -- may be either inherited or constrained.
9943 -- 1) Discriminant is inherited: there are two cases, depending on
9944 -- which type is inheriting.
9946 -- 1.1) Deriv_Typ is inheriting:
9948 -- type Ancestor (D_1 : ...) is tagged ...
9949 -- type Deriv_Typ is new Ancestor ...
9951 -- In this case the inherited discriminant is the final value of
9952 -- the parent discriminant because the end of the derivation chain
9953 -- has been reached.
9955 -- 1.2) Some other type is inheriting:
9957 -- type Ancestor_1 (D_1 : ...) is tagged ...
9958 -- type Ancestor_2 is new Ancestor_1 ...
9960 -- In this case the algorithm continues to trace the fate of the
9961 -- inherited discriminant down the derivation chain because it may
9962 -- be further inherited or constrained.
9964 -- 2) Discriminant is constrained: there are three cases, depending
9965 -- on what the constraint is.
9967 -- 2.1) The constraint is another discriminant (aka renaming):
9969 -- type Ancestor_1 (D_1 : ...) is tagged ...
9970 -- type Ancestor_2 (D_2 : ...) is new Ancestor_1 (D_1 => D_2) ...
9972 -- In this case the constraining discriminant becomes the one to
9973 -- track down the derivation chain. The algorithm already knows
9974 -- that D_2 constrains D_1, therefore if the algorithm finds the
9975 -- value of D_2, then this would also be the value for D_1.
9977 -- 2.2) The constraint is a name (aka Girder):
9980 -- type Ancestor_1 (D_1 : ...) is tagged ...
9981 -- type Ancestor_2 is new Ancestor_1 (D_1 => Name) ...
9983 -- In this case the name is the final value of D_1 because the
9984 -- discriminant cannot be further constrained.
9986 -- 2.3) The constraint is an expression (aka Girder):
9988 -- type Ancestor_1 (D_1 : ...) is tagged ...
9989 -- type Ancestor_2 is new Ancestor_1 (D_1 => 1 + 2) ...
9991 -- Similar to 2.2, the expression is the final value of D_1
9995 -- When a derived type constrains its parent type, all constaints
9996 -- appear in the Stored_Constraint list. Examine the list looking
9997 -- for a positional match.
9999 if Present
(Constrs
) then
10000 Constr_Elmt
:= First_Elmt
(Constrs
);
10001 while Present
(Constr_Elmt
) loop
10003 -- The position of the current constraint matches that of the
10004 -- ancestor discriminant.
10006 if Pos
= Discr_Pos
then
10007 return Find_Constraint_Value
(Node
(Constr_Elmt
));
10010 Next_Elmt
(Constr_Elmt
);
10014 -- Otherwise the derived type does not constraint its parent type in
10015 -- which case it inherits the parent discriminants.
10018 Typ_Discr
:= First_Discriminant
(Typ
);
10019 while Present
(Typ_Discr
) loop
10021 -- The position of the current discriminant matches that of the
10022 -- ancestor discriminant.
10024 if Pos
= Discr_Pos
then
10025 return Find_Constraint_Value
(Typ_Discr
);
10028 Next_Discriminant
(Typ_Discr
);
10033 -- A discriminant must always have a corresponding value. This is
10034 -- either another discriminant, a name, or an expression. If this
10035 -- point is reached, them most likely the derivation chain employs
10036 -- the wrong views of types.
10038 pragma Assert
(False);
10041 end Find_Discriminant_Value
;
10043 -----------------------
10044 -- Map_Discriminants --
10045 -----------------------
10047 procedure Map_Discriminants
10048 (Par_Typ
: Entity_Id
;
10049 Deriv_Typ
: Entity_Id
)
10051 Deriv_Chain
: constant Elist_Id
:= Build_Chain
(Par_Typ
, Deriv_Typ
);
10054 Discr_Val
: Node_Or_Entity_Id
;
10057 -- Examine each discriminant of parent type Par_Typ and find a
10058 -- suitable value for it from the point of view of derived type
10061 if Has_Discriminants
(Par_Typ
) then
10062 Discr
:= First_Discriminant
(Par_Typ
);
10063 while Present
(Discr
) loop
10065 Find_Discriminant_Value
10067 Par_Typ
=> Par_Typ
,
10068 Deriv_Typ
=> Deriv_Typ
,
10069 Typ_Elmt
=> First_Elmt
(Deriv_Chain
));
10071 -- Create a mapping of the form:
10073 -- parent type discriminant -> value
10075 Type_Map
.Set
(Discr
, Discr_Val
);
10077 Next_Discriminant
(Discr
);
10080 end Map_Discriminants
;
10082 --------------------
10083 -- Map_Primitives --
10084 --------------------
10086 procedure Map_Primitives
(Par_Typ
: Entity_Id
; Deriv_Typ
: Entity_Id
) is
10087 Deriv_Prim
: Entity_Id
;
10088 Par_Prim
: Entity_Id
;
10089 Par_Prims
: Elist_Id
;
10090 Prim_Elmt
: Elmt_Id
;
10093 -- Inspect the primitives of the derived type and determine whether
10094 -- they relate to the primitives of the parent type. If there is a
10095 -- meaningful relation, create a mapping of the form:
10097 -- parent type primitive -> perived type primitive
10099 if Present
(Direct_Primitive_Operations
(Deriv_Typ
)) then
10100 Prim_Elmt
:= First_Elmt
(Direct_Primitive_Operations
(Deriv_Typ
));
10101 while Present
(Prim_Elmt
) loop
10102 Deriv_Prim
:= Node
(Prim_Elmt
);
10104 if Is_Subprogram
(Deriv_Prim
)
10105 and then Find_Dispatching_Type
(Deriv_Prim
) = Deriv_Typ
10107 Add_Primitive
(Deriv_Prim
, Par_Typ
);
10110 Next_Elmt
(Prim_Elmt
);
10114 -- If the parent operation is an interface operation, the overriding
10115 -- indicator is not present. Instead, we get from the interface
10116 -- operation the primitive of the current type that implements it.
10118 if Is_Interface
(Par_Typ
) then
10119 Par_Prims
:= Collect_Primitive_Operations
(Par_Typ
);
10121 if Present
(Par_Prims
) then
10122 Prim_Elmt
:= First_Elmt
(Par_Prims
);
10124 while Present
(Prim_Elmt
) loop
10125 Par_Prim
:= Node
(Prim_Elmt
);
10127 Find_Primitive_Covering_Interface
(Deriv_Typ
, Par_Prim
);
10129 if Present
(Deriv_Prim
) then
10130 Type_Map
.Set
(Par_Prim
, Deriv_Prim
);
10133 Next_Elmt
(Prim_Elmt
);
10137 end Map_Primitives
;
10139 -- Start of processing for Map_Types
10142 -- Nothing to do if there are no types to work with
10144 if No
(Parent_Type
) or else No
(Derived_Type
) then
10147 -- Nothing to do if the mapping already exists
10149 elsif Type_Map
.Get
(Parent_Type
) = Derived_Type
then
10152 -- Nothing to do if both types are not tagged. Note that untagged types
10153 -- do not have primitive operations and their discriminants are already
10154 -- handled by gigi.
10156 elsif not Is_Tagged_Type
(Parent_Type
)
10157 or else not Is_Tagged_Type
(Derived_Type
)
10162 -- Create a mapping of the form
10164 -- parent type -> derived type
10166 -- to prevent any subsequent attempts to produce the same relations
10168 Type_Map
.Set
(Parent_Type
, Derived_Type
);
10170 -- Create mappings of the form
10172 -- parent type discriminant -> derived type discriminant
10174 -- parent type discriminant -> constraint
10176 -- Note that mapping of discriminants breaks privacy because it needs to
10177 -- work with those views which contains the discriminants and any stored
10181 (Par_Typ
=> Discriminated_View
(Parent_Type
),
10182 Deriv_Typ
=> Discriminated_View
(Derived_Type
));
10184 -- Create mappings of the form
10186 -- parent type primitive -> derived type primitive
10189 (Par_Typ
=> Parent_Type
,
10190 Deriv_Typ
=> Derived_Type
);
10193 ----------------------------
10194 -- Matching_Standard_Type --
10195 ----------------------------
10197 function Matching_Standard_Type
(Typ
: Entity_Id
) return Entity_Id
is
10198 pragma Assert
(Is_Scalar_Type
(Typ
));
10199 Siz
: constant Uint
:= Esize
(Typ
);
10202 -- Floating-point cases
10204 if Is_Floating_Point_Type
(Typ
) then
10205 if Siz
<= Esize
(Standard_Short_Float
) then
10206 return Standard_Short_Float
;
10207 elsif Siz
<= Esize
(Standard_Float
) then
10208 return Standard_Float
;
10209 elsif Siz
<= Esize
(Standard_Long_Float
) then
10210 return Standard_Long_Float
;
10211 elsif Siz
<= Esize
(Standard_Long_Long_Float
) then
10212 return Standard_Long_Long_Float
;
10214 raise Program_Error
;
10217 -- Integer cases (includes fixed-point types)
10219 -- Unsigned integer cases (includes normal enumeration types)
10221 elsif Is_Unsigned_Type
(Typ
) then
10222 if Siz
<= Esize
(Standard_Short_Short_Unsigned
) then
10223 return Standard_Short_Short_Unsigned
;
10224 elsif Siz
<= Esize
(Standard_Short_Unsigned
) then
10225 return Standard_Short_Unsigned
;
10226 elsif Siz
<= Esize
(Standard_Unsigned
) then
10227 return Standard_Unsigned
;
10228 elsif Siz
<= Esize
(Standard_Long_Unsigned
) then
10229 return Standard_Long_Unsigned
;
10230 elsif Siz
<= Esize
(Standard_Long_Long_Unsigned
) then
10231 return Standard_Long_Long_Unsigned
;
10233 raise Program_Error
;
10236 -- Signed integer cases
10239 if Siz
<= Esize
(Standard_Short_Short_Integer
) then
10240 return Standard_Short_Short_Integer
;
10241 elsif Siz
<= Esize
(Standard_Short_Integer
) then
10242 return Standard_Short_Integer
;
10243 elsif Siz
<= Esize
(Standard_Integer
) then
10244 return Standard_Integer
;
10245 elsif Siz
<= Esize
(Standard_Long_Integer
) then
10246 return Standard_Long_Integer
;
10247 elsif Siz
<= Esize
(Standard_Long_Long_Integer
) then
10248 return Standard_Long_Long_Integer
;
10250 raise Program_Error
;
10253 end Matching_Standard_Type
;
10255 -----------------------------
10256 -- May_Generate_Large_Temp --
10257 -----------------------------
10259 -- At the current time, the only types that we return False for (i.e. where
10260 -- we decide we know they cannot generate large temps) are ones where we
10261 -- know the size is 256 bits or less at compile time, and we are still not
10262 -- doing a thorough job on arrays and records ???
10264 function May_Generate_Large_Temp
(Typ
: Entity_Id
) return Boolean is
10266 if not Size_Known_At_Compile_Time
(Typ
) then
10269 elsif Esize
(Typ
) /= 0 and then Esize
(Typ
) <= 256 then
10272 elsif Is_Array_Type
(Typ
)
10273 and then Present
(Packed_Array_Impl_Type
(Typ
))
10275 return May_Generate_Large_Temp
(Packed_Array_Impl_Type
(Typ
));
10277 -- We could do more here to find other small types ???
10282 end May_Generate_Large_Temp
;
10284 ------------------------
10285 -- Needs_Finalization --
10286 ------------------------
10288 function Needs_Finalization
(Typ
: Entity_Id
) return Boolean is
10289 function Has_Some_Controlled_Component
10290 (Input_Typ
: Entity_Id
) return Boolean;
10291 -- Determine whether type Input_Typ has at least one controlled
10294 -----------------------------------
10295 -- Has_Some_Controlled_Component --
10296 -----------------------------------
10298 function Has_Some_Controlled_Component
10299 (Input_Typ
: Entity_Id
) return Boolean
10304 -- When a type is already frozen and has at least one controlled
10305 -- component, or is manually decorated, it is sufficient to inspect
10306 -- flag Has_Controlled_Component.
10308 if Has_Controlled_Component
(Input_Typ
) then
10311 -- Otherwise inspect the internals of the type
10313 elsif not Is_Frozen
(Input_Typ
) then
10314 if Is_Array_Type
(Input_Typ
) then
10315 return Needs_Finalization
(Component_Type
(Input_Typ
));
10317 elsif Is_Record_Type
(Input_Typ
) then
10318 Comp
:= First_Component
(Input_Typ
);
10319 while Present
(Comp
) loop
10320 if Needs_Finalization
(Etype
(Comp
)) then
10324 Next_Component
(Comp
);
10330 end Has_Some_Controlled_Component
;
10332 -- Start of processing for Needs_Finalization
10335 -- Certain run-time configurations and targets do not provide support
10336 -- for controlled types.
10338 if Restriction_Active
(No_Finalization
) then
10341 -- C++ types are not considered controlled. It is assumed that the non-
10342 -- Ada side will handle their clean up.
10344 elsif Convention
(Typ
) = Convention_CPP
then
10347 -- Class-wide types are treated as controlled because derivations from
10348 -- the root type may introduce controlled components.
10350 elsif Is_Class_Wide_Type
(Typ
) then
10353 -- Concurrent types are controlled as long as their corresponding record
10356 elsif Is_Concurrent_Type
(Typ
)
10357 and then Present
(Corresponding_Record_Type
(Typ
))
10358 and then Needs_Finalization
(Corresponding_Record_Type
(Typ
))
10362 -- Otherwise the type is controlled when it is either derived from type
10363 -- [Limited_]Controlled and not subject to aspect Disable_Controlled, or
10364 -- contains at least one controlled component.
10368 Is_Controlled
(Typ
) or else Has_Some_Controlled_Component
(Typ
);
10370 end Needs_Finalization
;
10372 ----------------------------
10373 -- Needs_Constant_Address --
10374 ----------------------------
10376 function Needs_Constant_Address
10378 Typ
: Entity_Id
) return Boolean
10381 -- If we have no initialization of any kind, then we don't need to place
10382 -- any restrictions on the address clause, because the object will be
10383 -- elaborated after the address clause is evaluated. This happens if the
10384 -- declaration has no initial expression, or the type has no implicit
10385 -- initialization, or the object is imported.
10387 -- The same holds for all initialized scalar types and all access types.
10388 -- Packed bit arrays of size up to 64 are represented using a modular
10389 -- type with an initialization (to zero) and can be processed like other
10390 -- initialized scalar types.
10392 -- If the type is controlled, code to attach the object to a
10393 -- finalization chain is generated at the point of declaration, and
10394 -- therefore the elaboration of the object cannot be delayed: the
10395 -- address expression must be a constant.
10397 if No
(Expression
(Decl
))
10398 and then not Needs_Finalization
(Typ
)
10400 (not Has_Non_Null_Base_Init_Proc
(Typ
)
10401 or else Is_Imported
(Defining_Identifier
(Decl
)))
10405 elsif (Present
(Expression
(Decl
)) and then Is_Scalar_Type
(Typ
))
10406 or else Is_Access_Type
(Typ
)
10408 (Is_Bit_Packed_Array
(Typ
)
10409 and then Is_Modular_Integer_Type
(Packed_Array_Impl_Type
(Typ
)))
10415 -- Otherwise, we require the address clause to be constant because
10416 -- the call to the initialization procedure (or the attach code) has
10417 -- to happen at the point of the declaration.
10419 -- Actually the IP call has been moved to the freeze actions anyway,
10420 -- so maybe we can relax this restriction???
10424 end Needs_Constant_Address
;
10426 ----------------------------
10427 -- New_Class_Wide_Subtype --
10428 ----------------------------
10430 function New_Class_Wide_Subtype
10431 (CW_Typ
: Entity_Id
;
10432 N
: Node_Id
) return Entity_Id
10434 Res
: constant Entity_Id
:= Create_Itype
(E_Void
, N
);
10435 Res_Name
: constant Name_Id
:= Chars
(Res
);
10436 Res_Scope
: constant Entity_Id
:= Scope
(Res
);
10439 Copy_Node
(CW_Typ
, Res
);
10440 Set_Comes_From_Source
(Res
, False);
10441 Set_Sloc
(Res
, Sloc
(N
));
10442 Set_Is_Itype
(Res
);
10443 Set_Associated_Node_For_Itype
(Res
, N
);
10444 Set_Is_Public
(Res
, False); -- By default, may be changed below.
10445 Set_Public_Status
(Res
);
10446 Set_Chars
(Res
, Res_Name
);
10447 Set_Scope
(Res
, Res_Scope
);
10448 Set_Ekind
(Res
, E_Class_Wide_Subtype
);
10449 Set_Next_Entity
(Res
, Empty
);
10450 Set_Etype
(Res
, Base_Type
(CW_Typ
));
10451 Set_Is_Frozen
(Res
, False);
10452 Set_Freeze_Node
(Res
, Empty
);
10454 end New_Class_Wide_Subtype
;
10456 --------------------------------
10457 -- Non_Limited_Designated_Type --
10458 ---------------------------------
10460 function Non_Limited_Designated_Type
(T
: Entity_Id
) return Entity_Id
is
10461 Desig
: constant Entity_Id
:= Designated_Type
(T
);
10463 if Has_Non_Limited_View
(Desig
) then
10464 return Non_Limited_View
(Desig
);
10468 end Non_Limited_Designated_Type
;
10470 -----------------------------------
10471 -- OK_To_Do_Constant_Replacement --
10472 -----------------------------------
10474 function OK_To_Do_Constant_Replacement
(E
: Entity_Id
) return Boolean is
10475 ES
: constant Entity_Id
:= Scope
(E
);
10479 -- Do not replace statically allocated objects, because they may be
10480 -- modified outside the current scope.
10482 if Is_Statically_Allocated
(E
) then
10485 -- Do not replace aliased or volatile objects, since we don't know what
10486 -- else might change the value.
10488 elsif Is_Aliased
(E
) or else Treat_As_Volatile
(E
) then
10491 -- Debug flag -gnatdM disconnects this optimization
10493 elsif Debug_Flag_MM
then
10496 -- Otherwise check scopes
10499 CS
:= Current_Scope
;
10502 -- If we are in right scope, replacement is safe
10507 -- Packages do not affect the determination of safety
10509 elsif Ekind
(CS
) = E_Package
then
10510 exit when CS
= Standard_Standard
;
10513 -- Blocks do not affect the determination of safety
10515 elsif Ekind
(CS
) = E_Block
then
10518 -- Loops do not affect the determination of safety. Note that we
10519 -- kill all current values on entry to a loop, so we are just
10520 -- talking about processing within a loop here.
10522 elsif Ekind
(CS
) = E_Loop
then
10525 -- Otherwise, the reference is dubious, and we cannot be sure that
10526 -- it is safe to do the replacement.
10535 end OK_To_Do_Constant_Replacement
;
10537 ------------------------------------
10538 -- Possible_Bit_Aligned_Component --
10539 ------------------------------------
10541 function Possible_Bit_Aligned_Component
(N
: Node_Id
) return Boolean is
10543 -- Do not process an unanalyzed node because it is not yet decorated and
10544 -- most checks performed below will fail.
10546 if not Analyzed
(N
) then
10552 -- Case of indexed component
10554 when N_Indexed_Component
=>
10556 P
: constant Node_Id
:= Prefix
(N
);
10557 Ptyp
: constant Entity_Id
:= Etype
(P
);
10560 -- If we know the component size and it is less than 64, then
10561 -- we are definitely OK. The back end always does assignment of
10562 -- misaligned small objects correctly.
10564 if Known_Static_Component_Size
(Ptyp
)
10565 and then Component_Size
(Ptyp
) <= 64
10569 -- Otherwise, we need to test the prefix, to see if we are
10570 -- indexing from a possibly unaligned component.
10573 return Possible_Bit_Aligned_Component
(P
);
10577 -- Case of selected component
10579 when N_Selected_Component
=>
10581 P
: constant Node_Id
:= Prefix
(N
);
10582 Comp
: constant Entity_Id
:= Entity
(Selector_Name
(N
));
10585 -- If there is no component clause, then we are in the clear
10586 -- since the back end will never misalign a large component
10587 -- unless it is forced to do so. In the clear means we need
10588 -- only the recursive test on the prefix.
10590 if Component_May_Be_Bit_Aligned
(Comp
) then
10593 return Possible_Bit_Aligned_Component
(P
);
10597 -- For a slice, test the prefix, if that is possibly misaligned,
10598 -- then for sure the slice is.
10601 return Possible_Bit_Aligned_Component
(Prefix
(N
));
10603 -- For an unchecked conversion, check whether the expression may
10606 when N_Unchecked_Type_Conversion
=>
10607 return Possible_Bit_Aligned_Component
(Expression
(N
));
10609 -- If we have none of the above, it means that we have fallen off the
10610 -- top testing prefixes recursively, and we now have a stand alone
10611 -- object, where we don't have a problem, unless this is a renaming,
10612 -- in which case we need to look into the renamed object.
10615 if Is_Entity_Name
(N
)
10616 and then Present
(Renamed_Object
(Entity
(N
)))
10619 Possible_Bit_Aligned_Component
(Renamed_Object
(Entity
(N
)));
10624 end Possible_Bit_Aligned_Component
;
10626 -----------------------------------------------
10627 -- Process_Statements_For_Controlled_Objects --
10628 -----------------------------------------------
10630 procedure Process_Statements_For_Controlled_Objects
(N
: Node_Id
) is
10631 Loc
: constant Source_Ptr
:= Sloc
(N
);
10633 function Are_Wrapped
(L
: List_Id
) return Boolean;
10634 -- Determine whether list L contains only one statement which is a block
10636 function Wrap_Statements_In_Block
10638 Scop
: Entity_Id
:= Current_Scope
) return Node_Id
;
10639 -- Given a list of statements L, wrap it in a block statement and return
10640 -- the generated node. Scop is either the current scope or the scope of
10641 -- the context (if applicable).
10647 function Are_Wrapped
(L
: List_Id
) return Boolean is
10648 Stmt
: constant Node_Id
:= First
(L
);
10652 and then No
(Next
(Stmt
))
10653 and then Nkind
(Stmt
) = N_Block_Statement
;
10656 ------------------------------
10657 -- Wrap_Statements_In_Block --
10658 ------------------------------
10660 function Wrap_Statements_In_Block
10662 Scop
: Entity_Id
:= Current_Scope
) return Node_Id
10664 Block_Id
: Entity_Id
;
10665 Block_Nod
: Node_Id
;
10666 Iter_Loop
: Entity_Id
;
10670 Make_Block_Statement
(Loc
,
10671 Declarations
=> No_List
,
10672 Handled_Statement_Sequence
=>
10673 Make_Handled_Sequence_Of_Statements
(Loc
,
10676 -- Create a label for the block in case the block needs to manage the
10677 -- secondary stack. A label allows for flag Uses_Sec_Stack to be set.
10679 Add_Block_Identifier
(Block_Nod
, Block_Id
);
10681 -- When wrapping the statements of an iterator loop, check whether
10682 -- the loop requires secondary stack management and if so, propagate
10683 -- the appropriate flags to the block. This ensures that the cursor
10684 -- is properly cleaned up at each iteration of the loop.
10686 Iter_Loop
:= Find_Enclosing_Iterator_Loop
(Scop
);
10688 if Present
(Iter_Loop
) then
10689 Set_Uses_Sec_Stack
(Block_Id
, Uses_Sec_Stack
(Iter_Loop
));
10691 -- Secondary stack reclamation is suppressed when the associated
10692 -- iterator loop contains a return statement which uses the stack.
10694 Set_Sec_Stack_Needed_For_Return
10695 (Block_Id
, Sec_Stack_Needed_For_Return
(Iter_Loop
));
10699 end Wrap_Statements_In_Block
;
10705 -- Start of processing for Process_Statements_For_Controlled_Objects
10708 -- Whenever a non-handled statement list is wrapped in a block, the
10709 -- block must be explicitly analyzed to redecorate all entities in the
10710 -- list and ensure that a finalizer is properly built.
10713 when N_Conditional_Entry_Call
10716 | N_Selective_Accept
10718 -- Check the "then statements" for elsif parts and if statements
10720 if Nkind_In
(N
, N_Elsif_Part
, N_If_Statement
)
10721 and then not Is_Empty_List
(Then_Statements
(N
))
10722 and then not Are_Wrapped
(Then_Statements
(N
))
10723 and then Requires_Cleanup_Actions
10724 (L
=> Then_Statements
(N
),
10725 Lib_Level
=> False,
10726 Nested_Constructs
=> False)
10728 Block
:= Wrap_Statements_In_Block
(Then_Statements
(N
));
10729 Set_Then_Statements
(N
, New_List
(Block
));
10734 -- Check the "else statements" for conditional entry calls, if
10735 -- statements and selective accepts.
10737 if Nkind_In
(N
, N_Conditional_Entry_Call
,
10739 N_Selective_Accept
)
10740 and then not Is_Empty_List
(Else_Statements
(N
))
10741 and then not Are_Wrapped
(Else_Statements
(N
))
10742 and then Requires_Cleanup_Actions
10743 (L
=> Else_Statements
(N
),
10744 Lib_Level
=> False,
10745 Nested_Constructs
=> False)
10747 Block
:= Wrap_Statements_In_Block
(Else_Statements
(N
));
10748 Set_Else_Statements
(N
, New_List
(Block
));
10753 when N_Abortable_Part
10754 | N_Accept_Alternative
10755 | N_Case_Statement_Alternative
10756 | N_Delay_Alternative
10757 | N_Entry_Call_Alternative
10758 | N_Exception_Handler
10760 | N_Triggering_Alternative
10762 if not Is_Empty_List
(Statements
(N
))
10763 and then not Are_Wrapped
(Statements
(N
))
10764 and then Requires_Cleanup_Actions
10765 (L
=> Statements
(N
),
10766 Lib_Level
=> False,
10767 Nested_Constructs
=> False)
10769 if Nkind
(N
) = N_Loop_Statement
10770 and then Present
(Identifier
(N
))
10773 Wrap_Statements_In_Block
10774 (L
=> Statements
(N
),
10775 Scop
=> Entity
(Identifier
(N
)));
10777 Block
:= Wrap_Statements_In_Block
(Statements
(N
));
10780 Set_Statements
(N
, New_List
(Block
));
10784 -- Could be e.g. a loop that was transformed into a block or null
10785 -- statement. Do nothing for terminate alternatives.
10787 when N_Block_Statement
10789 | N_Terminate_Alternative
10794 raise Program_Error
;
10796 end Process_Statements_For_Controlled_Objects
;
10802 function Power_Of_Two
(N
: Node_Id
) return Nat
is
10803 Typ
: constant Entity_Id
:= Etype
(N
);
10804 pragma Assert
(Is_Integer_Type
(Typ
));
10806 Siz
: constant Nat
:= UI_To_Int
(Esize
(Typ
));
10810 if not Compile_Time_Known_Value
(N
) then
10814 Val
:= Expr_Value
(N
);
10815 for J
in 1 .. Siz
- 1 loop
10816 if Val
= Uint_2
** J
then
10825 ----------------------
10826 -- Remove_Init_Call --
10827 ----------------------
10829 function Remove_Init_Call
10831 Rep_Clause
: Node_Id
) return Node_Id
10833 Par
: constant Node_Id
:= Parent
(Var
);
10834 Typ
: constant Entity_Id
:= Etype
(Var
);
10836 Init_Proc
: Entity_Id
;
10837 -- Initialization procedure for Typ
10839 function Find_Init_Call_In_List
(From
: Node_Id
) return Node_Id
;
10840 -- Look for init call for Var starting at From and scanning the
10841 -- enclosing list until Rep_Clause or the end of the list is reached.
10843 ----------------------------
10844 -- Find_Init_Call_In_List --
10845 ----------------------------
10847 function Find_Init_Call_In_List
(From
: Node_Id
) return Node_Id
is
10848 Init_Call
: Node_Id
;
10852 while Present
(Init_Call
) and then Init_Call
/= Rep_Clause
loop
10853 if Nkind
(Init_Call
) = N_Procedure_Call_Statement
10854 and then Is_Entity_Name
(Name
(Init_Call
))
10855 and then Entity
(Name
(Init_Call
)) = Init_Proc
10864 end Find_Init_Call_In_List
;
10866 Init_Call
: Node_Id
;
10868 -- Start of processing for Find_Init_Call
10871 if Present
(Initialization_Statements
(Var
)) then
10872 Init_Call
:= Initialization_Statements
(Var
);
10873 Set_Initialization_Statements
(Var
, Empty
);
10875 elsif not Has_Non_Null_Base_Init_Proc
(Typ
) then
10877 -- No init proc for the type, so obviously no call to be found
10882 -- We might be able to handle other cases below by just properly
10883 -- setting Initialization_Statements at the point where the init proc
10884 -- call is generated???
10886 Init_Proc
:= Base_Init_Proc
(Typ
);
10888 -- First scan the list containing the declaration of Var
10890 Init_Call
:= Find_Init_Call_In_List
(From
=> Next
(Par
));
10892 -- If not found, also look on Var's freeze actions list, if any,
10893 -- since the init call may have been moved there (case of an address
10894 -- clause applying to Var).
10896 if No
(Init_Call
) and then Present
(Freeze_Node
(Var
)) then
10898 Find_Init_Call_In_List
(First
(Actions
(Freeze_Node
(Var
))));
10901 -- If the initialization call has actuals that use the secondary
10902 -- stack, the call may have been wrapped into a temporary block, in
10903 -- which case the block itself has to be removed.
10905 if No
(Init_Call
) and then Nkind
(Next
(Par
)) = N_Block_Statement
then
10907 Blk
: constant Node_Id
:= Next
(Par
);
10910 (Find_Init_Call_In_List
10911 (First
(Statements
(Handled_Statement_Sequence
(Blk
)))))
10919 if Present
(Init_Call
) then
10920 Remove
(Init_Call
);
10923 end Remove_Init_Call
;
10925 -------------------------
10926 -- Remove_Side_Effects --
10927 -------------------------
10929 procedure Remove_Side_Effects
10931 Name_Req
: Boolean := False;
10932 Renaming_Req
: Boolean := False;
10933 Variable_Ref
: Boolean := False;
10934 Related_Id
: Entity_Id
:= Empty
;
10935 Is_Low_Bound
: Boolean := False;
10936 Is_High_Bound
: Boolean := False;
10937 Check_Side_Effects
: Boolean := True)
10939 function Build_Temporary
10942 Related_Nod
: Node_Id
:= Empty
) return Entity_Id
;
10943 -- Create an external symbol of the form xxx_FIRST/_LAST if Related_Nod
10944 -- is present (xxx is taken from the Chars field of Related_Nod),
10945 -- otherwise it generates an internal temporary. The created temporary
10946 -- entity is marked as internal.
10948 ---------------------
10949 -- Build_Temporary --
10950 ---------------------
10952 function Build_Temporary
10955 Related_Nod
: Node_Id
:= Empty
) return Entity_Id
10957 Temp_Id
: Entity_Id
;
10958 Temp_Nam
: Name_Id
;
10961 -- The context requires an external symbol
10963 if Present
(Related_Id
) then
10964 if Is_Low_Bound
then
10965 Temp_Nam
:= New_External_Name
(Chars
(Related_Id
), "_FIRST");
10966 else pragma Assert
(Is_High_Bound
);
10967 Temp_Nam
:= New_External_Name
(Chars
(Related_Id
), "_LAST");
10970 Temp_Id
:= Make_Defining_Identifier
(Loc
, Temp_Nam
);
10972 -- Otherwise generate an internal temporary
10975 Temp_Id
:= Make_Temporary
(Loc
, Id
, Related_Nod
);
10978 Set_Is_Internal
(Temp_Id
);
10981 end Build_Temporary
;
10985 Loc
: constant Source_Ptr
:= Sloc
(Exp
);
10986 Exp_Type
: constant Entity_Id
:= Etype
(Exp
);
10987 Svg_Suppress
: constant Suppress_Record
:= Scope_Suppress
;
10988 Def_Id
: Entity_Id
;
10991 Ptr_Typ_Decl
: Node_Id
;
10992 Ref_Type
: Entity_Id
;
10995 -- Start of processing for Remove_Side_Effects
10998 -- Handle cases in which there is nothing to do. In GNATprove mode,
10999 -- removal of side effects is useful for the light expansion of
11000 -- renamings. This removal should only occur when not inside a
11001 -- generic and not doing a pre-analysis.
11003 if not Expander_Active
11004 and (Inside_A_Generic
or not Full_Analysis
or not GNATprove_Mode
)
11008 -- Cannot generate temporaries if the invocation to remove side effects
11009 -- was issued too early and the type of the expression is not resolved
11010 -- (this happens because routines Duplicate_Subexpr_XX implicitly invoke
11011 -- Remove_Side_Effects).
11013 elsif No
(Exp_Type
)
11014 or else Ekind
(Exp_Type
) = E_Access_Attribute_Type
11018 -- Nothing to do if prior expansion determined that a function call does
11019 -- not require side effect removal.
11021 elsif Nkind
(Exp
) = N_Function_Call
11022 and then No_Side_Effect_Removal
(Exp
)
11026 -- No action needed for side-effect free expressions
11028 elsif Check_Side_Effects
11029 and then Side_Effect_Free
(Exp
, Name_Req
, Variable_Ref
)
11034 -- The remaining processing is done with all checks suppressed
11036 -- Note: from now on, don't use return statements, instead do a goto
11037 -- Leave, to ensure that we properly restore Scope_Suppress.Suppress.
11039 Scope_Suppress
.Suppress
:= (others => True);
11041 -- If this is an elementary or a small not-by-reference record type, and
11042 -- we need to capture the value, just make a constant; this is cheap and
11043 -- objects of both kinds of types can be bit aligned, so it might not be
11044 -- possible to generate a reference to them. Likewise if this is not a
11045 -- name reference, except for a type conversion, because we would enter
11046 -- an infinite recursion with Checks.Apply_Predicate_Check if the target
11047 -- type has predicates (and type conversions need a specific treatment
11048 -- anyway, see below). Also do it if we have a volatile reference and
11049 -- Name_Req is not set (see comments for Side_Effect_Free).
11051 if (Is_Elementary_Type
(Exp_Type
)
11052 or else (Is_Record_Type
(Exp_Type
)
11053 and then Known_Static_RM_Size
(Exp_Type
)
11054 and then RM_Size
(Exp_Type
) <= 64
11055 and then not Has_Discriminants
(Exp_Type
)
11056 and then not Is_By_Reference_Type
(Exp_Type
)))
11057 and then (Variable_Ref
11058 or else (not Is_Name_Reference
(Exp
)
11059 and then Nkind
(Exp
) /= N_Type_Conversion
)
11060 or else (not Name_Req
11061 and then Is_Volatile_Reference
(Exp
)))
11063 Def_Id
:= Build_Temporary
(Loc
, 'R', Exp
);
11064 Set_Etype
(Def_Id
, Exp_Type
);
11065 Res
:= New_Occurrence_Of
(Def_Id
, Loc
);
11067 -- If the expression is a packed reference, it must be reanalyzed and
11068 -- expanded, depending on context. This is the case for actuals where
11069 -- a constraint check may capture the actual before expansion of the
11070 -- call is complete.
11072 if Nkind
(Exp
) = N_Indexed_Component
11073 and then Is_Packed
(Etype
(Prefix
(Exp
)))
11075 Set_Analyzed
(Exp
, False);
11076 Set_Analyzed
(Prefix
(Exp
), False);
11080 -- Rnn : Exp_Type renames Expr;
11082 if Renaming_Req
then
11084 Make_Object_Renaming_Declaration
(Loc
,
11085 Defining_Identifier
=> Def_Id
,
11086 Subtype_Mark
=> New_Occurrence_Of
(Exp_Type
, Loc
),
11087 Name
=> Relocate_Node
(Exp
));
11090 -- Rnn : constant Exp_Type := Expr;
11094 Make_Object_Declaration
(Loc
,
11095 Defining_Identifier
=> Def_Id
,
11096 Object_Definition
=> New_Occurrence_Of
(Exp_Type
, Loc
),
11097 Constant_Present
=> True,
11098 Expression
=> Relocate_Node
(Exp
));
11100 Set_Assignment_OK
(E
);
11103 Insert_Action
(Exp
, E
);
11105 -- If the expression has the form v.all then we can just capture the
11106 -- pointer, and then do an explicit dereference on the result, but
11107 -- this is not right if this is a volatile reference.
11109 elsif Nkind
(Exp
) = N_Explicit_Dereference
11110 and then not Is_Volatile_Reference
(Exp
)
11112 Def_Id
:= Build_Temporary
(Loc
, 'R', Exp
);
11114 Make_Explicit_Dereference
(Loc
, New_Occurrence_Of
(Def_Id
, Loc
));
11116 Insert_Action
(Exp
,
11117 Make_Object_Declaration
(Loc
,
11118 Defining_Identifier
=> Def_Id
,
11119 Object_Definition
=>
11120 New_Occurrence_Of
(Etype
(Prefix
(Exp
)), Loc
),
11121 Constant_Present
=> True,
11122 Expression
=> Relocate_Node
(Prefix
(Exp
))));
11124 -- Similar processing for an unchecked conversion of an expression of
11125 -- the form v.all, where we want the same kind of treatment.
11127 elsif Nkind
(Exp
) = N_Unchecked_Type_Conversion
11128 and then Nkind
(Expression
(Exp
)) = N_Explicit_Dereference
11130 Remove_Side_Effects
(Expression
(Exp
), Name_Req
, Variable_Ref
);
11133 -- If this is a type conversion, leave the type conversion and remove
11134 -- the side effects in the expression. This is important in several
11135 -- circumstances: for change of representations, and also when this is a
11136 -- view conversion to a smaller object, where gigi can end up creating
11137 -- its own temporary of the wrong size.
11139 elsif Nkind
(Exp
) = N_Type_Conversion
then
11140 Remove_Side_Effects
(Expression
(Exp
), Name_Req
, Variable_Ref
);
11142 -- Generating C code the type conversion of an access to constrained
11143 -- array type into an access to unconstrained array type involves
11144 -- initializing a fat pointer and the expression must be free of
11145 -- side effects to safely compute its bounds.
11147 if Modify_Tree_For_C
11148 and then Is_Access_Type
(Etype
(Exp
))
11149 and then Is_Array_Type
(Designated_Type
(Etype
(Exp
)))
11150 and then not Is_Constrained
(Designated_Type
(Etype
(Exp
)))
11152 Def_Id
:= Build_Temporary
(Loc
, 'R', Exp
);
11153 Set_Etype
(Def_Id
, Exp_Type
);
11154 Res
:= New_Occurrence_Of
(Def_Id
, Loc
);
11156 Insert_Action
(Exp
,
11157 Make_Object_Declaration
(Loc
,
11158 Defining_Identifier
=> Def_Id
,
11159 Object_Definition
=> New_Occurrence_Of
(Exp_Type
, Loc
),
11160 Constant_Present
=> True,
11161 Expression
=> Relocate_Node
(Exp
)));
11166 -- If this is an unchecked conversion that Gigi can't handle, make
11167 -- a copy or a use a renaming to capture the value.
11169 elsif Nkind
(Exp
) = N_Unchecked_Type_Conversion
11170 and then not Safe_Unchecked_Type_Conversion
(Exp
)
11172 if CW_Or_Has_Controlled_Part
(Exp_Type
) then
11174 -- Use a renaming to capture the expression, rather than create
11175 -- a controlled temporary.
11177 Def_Id
:= Build_Temporary
(Loc
, 'R', Exp
);
11178 Res
:= New_Occurrence_Of
(Def_Id
, Loc
);
11180 Insert_Action
(Exp
,
11181 Make_Object_Renaming_Declaration
(Loc
,
11182 Defining_Identifier
=> Def_Id
,
11183 Subtype_Mark
=> New_Occurrence_Of
(Exp_Type
, Loc
),
11184 Name
=> Relocate_Node
(Exp
)));
11187 Def_Id
:= Build_Temporary
(Loc
, 'R', Exp
);
11188 Set_Etype
(Def_Id
, Exp_Type
);
11189 Res
:= New_Occurrence_Of
(Def_Id
, Loc
);
11192 Make_Object_Declaration
(Loc
,
11193 Defining_Identifier
=> Def_Id
,
11194 Object_Definition
=> New_Occurrence_Of
(Exp_Type
, Loc
),
11195 Constant_Present
=> not Is_Variable
(Exp
),
11196 Expression
=> Relocate_Node
(Exp
));
11198 Set_Assignment_OK
(E
);
11199 Insert_Action
(Exp
, E
);
11202 -- For expressions that denote names, we can use a renaming scheme.
11203 -- This is needed for correctness in the case of a volatile object of
11204 -- a non-volatile type because the Make_Reference call of the "default"
11205 -- approach would generate an illegal access value (an access value
11206 -- cannot designate such an object - see Analyze_Reference).
11208 elsif Is_Name_Reference
(Exp
)
11210 -- We skip using this scheme if we have an object of a volatile
11211 -- type and we do not have Name_Req set true (see comments for
11212 -- Side_Effect_Free).
11214 and then (Name_Req
or else not Treat_As_Volatile
(Exp_Type
))
11216 Def_Id
:= Build_Temporary
(Loc
, 'R', Exp
);
11217 Res
:= New_Occurrence_Of
(Def_Id
, Loc
);
11219 Insert_Action
(Exp
,
11220 Make_Object_Renaming_Declaration
(Loc
,
11221 Defining_Identifier
=> Def_Id
,
11222 Subtype_Mark
=> New_Occurrence_Of
(Exp_Type
, Loc
),
11223 Name
=> Relocate_Node
(Exp
)));
11225 -- If this is a packed reference, or a selected component with
11226 -- a non-standard representation, a reference to the temporary
11227 -- will be replaced by a copy of the original expression (see
11228 -- Exp_Ch2.Expand_Renaming). Otherwise the temporary must be
11229 -- elaborated by gigi, and is of course not to be replaced in-line
11230 -- by the expression it renames, which would defeat the purpose of
11231 -- removing the side effect.
11233 if Nkind_In
(Exp
, N_Selected_Component
, N_Indexed_Component
)
11234 and then Has_Non_Standard_Rep
(Etype
(Prefix
(Exp
)))
11238 Set_Is_Renaming_Of_Object
(Def_Id
, False);
11241 -- Avoid generating a variable-sized temporary, by generating the
11242 -- reference just for the function call. The transformation could be
11243 -- refined to apply only when the array component is constrained by a
11246 elsif Nkind
(Exp
) = N_Selected_Component
11247 and then Nkind
(Prefix
(Exp
)) = N_Function_Call
11248 and then Is_Array_Type
(Exp_Type
)
11250 Remove_Side_Effects
(Prefix
(Exp
), Name_Req
, Variable_Ref
);
11253 -- Otherwise we generate a reference to the expression
11256 -- An expression which is in SPARK mode is considered side effect
11257 -- free if the resulting value is captured by a variable or a
11261 and then Nkind
(Parent
(Exp
)) = N_Object_Declaration
11265 -- When generating C code we cannot consider side effect free object
11266 -- declarations that have discriminants and are initialized by means
11267 -- of a function call since on this target there is no secondary
11268 -- stack to store the return value and the expander may generate an
11269 -- extra call to the function to compute the discriminant value. In
11270 -- addition, for targets that have secondary stack, the expansion of
11271 -- functions with side effects involves the generation of an access
11272 -- type to capture the return value stored in the secondary stack;
11273 -- by contrast when generating C code such expansion generates an
11274 -- internal object declaration (no access type involved) which must
11275 -- be identified here to avoid entering into a never-ending loop
11276 -- generating internal object declarations.
11278 elsif Modify_Tree_For_C
11279 and then Nkind
(Parent
(Exp
)) = N_Object_Declaration
11281 (Nkind
(Exp
) /= N_Function_Call
11282 or else not Has_Discriminants
(Exp_Type
)
11283 or else Is_Internal_Name
11284 (Chars
(Defining_Identifier
(Parent
(Exp
)))))
11289 -- Special processing for function calls that return a limited type.
11290 -- We need to build a declaration that will enable build-in-place
11291 -- expansion of the call. This is not done if the context is already
11292 -- an object declaration, to prevent infinite recursion.
11294 -- This is relevant only in Ada 2005 mode. In Ada 95 programs we have
11295 -- to accommodate functions returning limited objects by reference.
11297 if Ada_Version
>= Ada_2005
11298 and then Nkind
(Exp
) = N_Function_Call
11299 and then Is_Limited_View
(Etype
(Exp
))
11300 and then Nkind
(Parent
(Exp
)) /= N_Object_Declaration
11303 Obj
: constant Entity_Id
:= Make_Temporary
(Loc
, 'F', Exp
);
11308 Make_Object_Declaration
(Loc
,
11309 Defining_Identifier
=> Obj
,
11310 Object_Definition
=> New_Occurrence_Of
(Exp_Type
, Loc
),
11311 Expression
=> Relocate_Node
(Exp
));
11313 Insert_Action
(Exp
, Decl
);
11314 Set_Etype
(Obj
, Exp_Type
);
11315 Rewrite
(Exp
, New_Occurrence_Of
(Obj
, Loc
));
11320 Def_Id
:= Build_Temporary
(Loc
, 'R', Exp
);
11322 -- The regular expansion of functions with side effects involves the
11323 -- generation of an access type to capture the return value found on
11324 -- the secondary stack. Since SPARK (and why) cannot process access
11325 -- types, use a different approach which ignores the secondary stack
11326 -- and "copies" the returned object.
11327 -- When generating C code, no need for a 'reference since the
11328 -- secondary stack is not supported.
11330 if GNATprove_Mode
or Modify_Tree_For_C
then
11331 Res
:= New_Occurrence_Of
(Def_Id
, Loc
);
11332 Ref_Type
:= Exp_Type
;
11334 -- Regular expansion utilizing an access type and 'reference
11338 Make_Explicit_Dereference
(Loc
,
11339 Prefix
=> New_Occurrence_Of
(Def_Id
, Loc
));
11342 -- type Ann is access all <Exp_Type>;
11344 Ref_Type
:= Make_Temporary
(Loc
, 'A');
11347 Make_Full_Type_Declaration
(Loc
,
11348 Defining_Identifier
=> Ref_Type
,
11350 Make_Access_To_Object_Definition
(Loc
,
11351 All_Present
=> True,
11352 Subtype_Indication
=>
11353 New_Occurrence_Of
(Exp_Type
, Loc
)));
11355 Insert_Action
(Exp
, Ptr_Typ_Decl
);
11359 if Nkind
(E
) = N_Explicit_Dereference
then
11360 New_Exp
:= Relocate_Node
(Prefix
(E
));
11363 E
:= Relocate_Node
(E
);
11365 -- Do not generate a 'reference in SPARK mode or C generation
11366 -- since the access type is not created in the first place.
11368 if GNATprove_Mode
or Modify_Tree_For_C
then
11371 -- Otherwise generate reference, marking the value as non-null
11372 -- since we know it cannot be null and we don't want a check.
11375 New_Exp
:= Make_Reference
(Loc
, E
);
11376 Set_Is_Known_Non_Null
(Def_Id
);
11380 if Is_Delayed_Aggregate
(E
) then
11382 -- The expansion of nested aggregates is delayed until the
11383 -- enclosing aggregate is expanded. As aggregates are often
11384 -- qualified, the predicate applies to qualified expressions as
11385 -- well, indicating that the enclosing aggregate has not been
11386 -- expanded yet. At this point the aggregate is part of a
11387 -- stand-alone declaration, and must be fully expanded.
11389 if Nkind
(E
) = N_Qualified_Expression
then
11390 Set_Expansion_Delayed
(Expression
(E
), False);
11391 Set_Analyzed
(Expression
(E
), False);
11393 Set_Expansion_Delayed
(E
, False);
11396 Set_Analyzed
(E
, False);
11399 -- Generating C code of object declarations that have discriminants
11400 -- and are initialized by means of a function call we propagate the
11401 -- discriminants of the parent type to the internally built object.
11402 -- This is needed to avoid generating an extra call to the called
11405 -- For example, if we generate here the following declaration, it
11406 -- will be expanded later adding an extra call to evaluate the value
11407 -- of the discriminant (needed to compute the size of the object).
11409 -- type Rec (D : Integer) is ...
11410 -- Obj : constant Rec := SomeFunc;
11412 if Modify_Tree_For_C
11413 and then Nkind
(Parent
(Exp
)) = N_Object_Declaration
11414 and then Has_Discriminants
(Exp_Type
)
11415 and then Nkind
(Exp
) = N_Function_Call
11417 Insert_Action
(Exp
,
11418 Make_Object_Declaration
(Loc
,
11419 Defining_Identifier
=> Def_Id
,
11420 Object_Definition
=> New_Copy_Tree
11421 (Object_Definition
(Parent
(Exp
))),
11422 Constant_Present
=> True,
11423 Expression
=> New_Exp
));
11425 Insert_Action
(Exp
,
11426 Make_Object_Declaration
(Loc
,
11427 Defining_Identifier
=> Def_Id
,
11428 Object_Definition
=> New_Occurrence_Of
(Ref_Type
, Loc
),
11429 Constant_Present
=> True,
11430 Expression
=> New_Exp
));
11434 -- Preserve the Assignment_OK flag in all copies, since at least one
11435 -- copy may be used in a context where this flag must be set (otherwise
11436 -- why would the flag be set in the first place).
11438 Set_Assignment_OK
(Res
, Assignment_OK
(Exp
));
11440 -- Finally rewrite the original expression and we are done
11442 Rewrite
(Exp
, Res
);
11443 Analyze_And_Resolve
(Exp
, Exp_Type
);
11446 Scope_Suppress
:= Svg_Suppress
;
11447 end Remove_Side_Effects
;
11449 ------------------------
11450 -- Replace_References --
11451 ------------------------
11453 procedure Replace_References
11455 Par_Typ
: Entity_Id
;
11456 Deriv_Typ
: Entity_Id
;
11457 Par_Obj
: Entity_Id
:= Empty
;
11458 Deriv_Obj
: Entity_Id
:= Empty
)
11460 function Is_Deriv_Obj_Ref
(Ref
: Node_Id
) return Boolean;
11461 -- Determine whether node Ref denotes some component of Deriv_Obj
11463 function Replace_Ref
(Ref
: Node_Id
) return Traverse_Result
;
11464 -- Substitute a reference to an entity with the corresponding value
11465 -- stored in table Type_Map.
11467 function Type_Of_Formal
11469 Actual
: Node_Id
) return Entity_Id
;
11470 -- Find the type of the formal parameter which corresponds to actual
11471 -- parameter Actual in subprogram call Call.
11473 ----------------------
11474 -- Is_Deriv_Obj_Ref --
11475 ----------------------
11477 function Is_Deriv_Obj_Ref
(Ref
: Node_Id
) return Boolean is
11478 Par
: constant Node_Id
:= Parent
(Ref
);
11481 -- Detect the folowing selected component form:
11483 -- Deriv_Obj.(something)
11486 Nkind
(Par
) = N_Selected_Component
11487 and then Is_Entity_Name
(Prefix
(Par
))
11488 and then Entity
(Prefix
(Par
)) = Deriv_Obj
;
11489 end Is_Deriv_Obj_Ref
;
11495 function Replace_Ref
(Ref
: Node_Id
) return Traverse_Result
is
11496 procedure Remove_Controlling_Arguments
(From_Arg
: Node_Id
);
11497 -- Reset the Controlling_Argument of all function calls that
11498 -- encapsulate node From_Arg.
11500 ----------------------------------
11501 -- Remove_Controlling_Arguments --
11502 ----------------------------------
11504 procedure Remove_Controlling_Arguments
(From_Arg
: Node_Id
) is
11509 while Present
(Par
) loop
11510 if Nkind
(Par
) = N_Function_Call
11511 and then Present
(Controlling_Argument
(Par
))
11513 Set_Controlling_Argument
(Par
, Empty
);
11515 -- Prevent the search from going too far
11517 elsif Is_Body_Or_Package_Declaration
(Par
) then
11521 Par
:= Parent
(Par
);
11523 end Remove_Controlling_Arguments
;
11527 Context
: constant Node_Id
:= Parent
(Ref
);
11528 Loc
: constant Source_Ptr
:= Sloc
(Ref
);
11529 Ref_Id
: Entity_Id
;
11530 Result
: Traverse_Result
;
11533 -- The new reference which is intended to substitute the old one
11536 -- The reference designated for replacement. In certain cases this
11537 -- may be a node other than Ref.
11539 Val
: Node_Or_Entity_Id
;
11540 -- The corresponding value of Ref from the type map
11542 -- Start of processing for Replace_Ref
11545 -- Assume that the input reference is to be replaced and that the
11546 -- traversal should examine the children of the reference.
11551 -- The input denotes a meaningful reference
11553 if Nkind
(Ref
) in N_Has_Entity
and then Present
(Entity
(Ref
)) then
11554 Ref_Id
:= Entity
(Ref
);
11555 Val
:= Type_Map
.Get
(Ref_Id
);
11557 -- The reference has a corresponding value in the type map, a
11558 -- substitution is possible.
11560 if Present
(Val
) then
11562 -- The reference denotes a discriminant
11564 if Ekind
(Ref_Id
) = E_Discriminant
then
11565 if Nkind
(Val
) in N_Entity
then
11567 -- The value denotes another discriminant. Replace as
11570 -- _object.Discr -> _object.Val
11572 if Ekind
(Val
) = E_Discriminant
then
11573 New_Ref
:= New_Occurrence_Of
(Val
, Loc
);
11575 -- Otherwise the value denotes the entity of a name which
11576 -- constraints the discriminant. Replace as follows:
11578 -- _object.Discr -> Val
11581 pragma Assert
(Is_Deriv_Obj_Ref
(Old_Ref
));
11583 New_Ref
:= New_Occurrence_Of
(Val
, Loc
);
11584 Old_Ref
:= Parent
(Old_Ref
);
11587 -- Otherwise the value denotes an arbitrary expression which
11588 -- constraints the discriminant. Replace as follows:
11590 -- _object.Discr -> Val
11593 pragma Assert
(Is_Deriv_Obj_Ref
(Old_Ref
));
11595 New_Ref
:= New_Copy_Tree
(Val
);
11596 Old_Ref
:= Parent
(Old_Ref
);
11599 -- Otherwise the reference denotes a primitive. Replace as
11602 -- Primitive -> Val
11605 pragma Assert
(Nkind
(Val
) in N_Entity
);
11606 New_Ref
:= New_Occurrence_Of
(Val
, Loc
);
11609 -- The reference mentions the _object parameter of the parent
11610 -- type's DIC or type invariant procedure. Replace as follows:
11612 -- _object -> _object
11614 elsif Present
(Par_Obj
)
11615 and then Present
(Deriv_Obj
)
11616 and then Ref_Id
= Par_Obj
11618 New_Ref
:= New_Occurrence_Of
(Deriv_Obj
, Loc
);
11620 -- The type of the _object parameter is class-wide when the
11621 -- expression comes from an assertion pragma that applies to
11622 -- an abstract parent type or an interface. The class-wide type
11623 -- facilitates the preanalysis of the expression by treating
11624 -- calls to abstract primitives that mention the current
11625 -- instance of the type as dispatching. Once the calls are
11626 -- remapped to invoke overriding or inherited primitives, the
11627 -- calls no longer need to be dispatching. Examine all function
11628 -- calls that encapsulate the _object parameter and reset their
11629 -- Controlling_Argument attribute.
11631 if Is_Class_Wide_Type
(Etype
(Par_Obj
))
11632 and then Is_Abstract_Type
(Root_Type
(Etype
(Par_Obj
)))
11634 Remove_Controlling_Arguments
(Old_Ref
);
11637 -- The reference to _object acts as an actual parameter in a
11638 -- subprogram call which may be invoking a primitive of the
11641 -- Primitive (... _object ...);
11643 -- The parent type primitive may not be overridden nor
11644 -- inherited when it is declared after the derived type
11647 -- type Parent is tagged private;
11648 -- type Child is new Parent with private;
11649 -- procedure Primitive (Obj : Parent);
11651 -- In this scenario the _object parameter is converted to the
11652 -- parent type. Due to complications with partial/full views
11653 -- and view swaps, the parent type is taken from the formal
11654 -- parameter of the subprogram being called.
11656 if Nkind_In
(Context
, N_Function_Call
,
11657 N_Procedure_Call_Statement
)
11658 and then No
(Type_Map
.Get
(Entity
(Name
(Context
))))
11661 Convert_To
(Type_Of_Formal
(Context
, Old_Ref
), New_Ref
);
11663 -- Do not process the generated type conversion because
11664 -- both the parent type and the derived type are in the
11665 -- Type_Map table. This will clobber the type conversion
11666 -- by resetting its subtype mark.
11671 -- Otherwise there is nothing to replace
11677 if Present
(New_Ref
) then
11678 Rewrite
(Old_Ref
, New_Ref
);
11680 -- Update the return type when the context of the reference
11681 -- acts as the name of a function call. Note that the update
11682 -- should not be performed when the reference appears as an
11683 -- actual in the call.
11685 if Nkind
(Context
) = N_Function_Call
11686 and then Name
(Context
) = Old_Ref
11688 Set_Etype
(Context
, Etype
(Val
));
11693 -- Reanalyze the reference due to potential replacements
11695 if Nkind
(Old_Ref
) in N_Has_Etype
then
11696 Set_Analyzed
(Old_Ref
, False);
11702 procedure Replace_Refs
is new Traverse_Proc
(Replace_Ref
);
11704 --------------------
11705 -- Type_Of_Formal --
11706 --------------------
11708 function Type_Of_Formal
11710 Actual
: Node_Id
) return Entity_Id
11716 -- Examine the list of actual and formal parameters in parallel
11718 A
:= First
(Parameter_Associations
(Call
));
11719 F
:= First_Formal
(Entity
(Name
(Call
)));
11720 while Present
(A
) and then Present
(F
) loop
11729 -- The actual parameter must always have a corresponding formal
11731 pragma Assert
(False);
11734 end Type_Of_Formal
;
11736 -- Start of processing for Replace_References
11739 -- Map the attributes of the parent type to the proper corresponding
11740 -- attributes of the derived type.
11743 (Parent_Type
=> Par_Typ
,
11744 Derived_Type
=> Deriv_Typ
);
11746 -- Inspect the input expression and perform substitutions where
11749 Replace_Refs
(Expr
);
11750 end Replace_References
;
11752 -----------------------------
11753 -- Replace_Type_References --
11754 -----------------------------
11756 procedure Replace_Type_References
11759 Obj_Id
: Entity_Id
)
11761 procedure Replace_Type_Ref
(N
: Node_Id
);
11762 -- Substitute a single reference of the current instance of type Typ
11763 -- with a reference to Obj_Id.
11765 ----------------------
11766 -- Replace_Type_Ref --
11767 ----------------------
11769 procedure Replace_Type_Ref
(N
: Node_Id
) is
11771 -- Decorate the reference to Typ even though it may be rewritten
11772 -- further down. This is done for two reasons:
11774 -- * ASIS has all necessary semantic information in the original
11777 -- * Routines which examine properties of the Original_Node have
11778 -- some semantic information.
11780 if Nkind
(N
) = N_Identifier
then
11781 Set_Entity
(N
, Typ
);
11782 Set_Etype
(N
, Typ
);
11784 elsif Nkind
(N
) = N_Selected_Component
then
11785 Analyze
(Prefix
(N
));
11786 Set_Entity
(Selector_Name
(N
), Typ
);
11787 Set_Etype
(Selector_Name
(N
), Typ
);
11790 -- Perform the following substitution:
11794 Rewrite
(N
, New_Occurrence_Of
(Obj_Id
, Sloc
(N
)));
11795 Set_Comes_From_Source
(N
, True);
11796 end Replace_Type_Ref
;
11798 procedure Replace_Type_Refs
is
11799 new Replace_Type_References_Generic
(Replace_Type_Ref
);
11801 -- Start of processing for Replace_Type_References
11804 Replace_Type_Refs
(Expr
, Typ
);
11805 end Replace_Type_References
;
11807 ---------------------------
11808 -- Represented_As_Scalar --
11809 ---------------------------
11811 function Represented_As_Scalar
(T
: Entity_Id
) return Boolean is
11812 UT
: constant Entity_Id
:= Underlying_Type
(T
);
11814 return Is_Scalar_Type
(UT
)
11815 or else (Is_Bit_Packed_Array
(UT
)
11816 and then Is_Scalar_Type
(Packed_Array_Impl_Type
(UT
)));
11817 end Represented_As_Scalar
;
11819 ------------------------------
11820 -- Requires_Cleanup_Actions --
11821 ------------------------------
11823 function Requires_Cleanup_Actions
11825 Lib_Level
: Boolean) return Boolean
11827 At_Lib_Level
: constant Boolean :=
11829 and then Nkind_In
(N
, N_Package_Body
,
11830 N_Package_Specification
);
11831 -- N is at the library level if the top-most context is a package and
11832 -- the path taken to reach N does not inlcude non-package constructs.
11836 when N_Accept_Statement
11837 | N_Block_Statement
11841 | N_Subprogram_Body
11845 Requires_Cleanup_Actions
11846 (L
=> Declarations
(N
),
11847 Lib_Level
=> At_Lib_Level
,
11848 Nested_Constructs
=> True)
11850 (Present
(Handled_Statement_Sequence
(N
))
11852 Requires_Cleanup_Actions
11854 Statements
(Handled_Statement_Sequence
(N
)),
11855 Lib_Level
=> At_Lib_Level
,
11856 Nested_Constructs
=> True));
11858 -- Extended return statements are the same as the above, except that
11859 -- there is no Declarations field. We do not want to clean up the
11860 -- Return_Object_Declarations.
11862 when N_Extended_Return_Statement
=>
11864 Present
(Handled_Statement_Sequence
(N
))
11865 and then Requires_Cleanup_Actions
11867 Statements
(Handled_Statement_Sequence
(N
)),
11868 Lib_Level
=> At_Lib_Level
,
11869 Nested_Constructs
=> True);
11871 when N_Package_Specification
=>
11873 Requires_Cleanup_Actions
11874 (L
=> Visible_Declarations
(N
),
11875 Lib_Level
=> At_Lib_Level
,
11876 Nested_Constructs
=> True)
11878 Requires_Cleanup_Actions
11879 (L
=> Private_Declarations
(N
),
11880 Lib_Level
=> At_Lib_Level
,
11881 Nested_Constructs
=> True);
11884 raise Program_Error
;
11886 end Requires_Cleanup_Actions
;
11888 ------------------------------
11889 -- Requires_Cleanup_Actions --
11890 ------------------------------
11892 function Requires_Cleanup_Actions
11894 Lib_Level
: Boolean;
11895 Nested_Constructs
: Boolean) return Boolean
11899 Obj_Id
: Entity_Id
;
11900 Obj_Typ
: Entity_Id
;
11901 Pack_Id
: Entity_Id
;
11906 or else Is_Empty_List
(L
)
11912 while Present
(Decl
) loop
11914 -- Library-level tagged types
11916 if Nkind
(Decl
) = N_Full_Type_Declaration
then
11917 Typ
:= Defining_Identifier
(Decl
);
11919 -- Ignored Ghost types do not need any cleanup actions because
11920 -- they will not appear in the final tree.
11922 if Is_Ignored_Ghost_Entity
(Typ
) then
11925 elsif Is_Tagged_Type
(Typ
)
11926 and then Is_Library_Level_Entity
(Typ
)
11927 and then Convention
(Typ
) = Convention_Ada
11928 and then Present
(Access_Disp_Table
(Typ
))
11929 and then RTE_Available
(RE_Unregister_Tag
)
11930 and then not Is_Abstract_Type
(Typ
)
11931 and then not No_Run_Time_Mode
11936 -- Regular object declarations
11938 elsif Nkind
(Decl
) = N_Object_Declaration
then
11939 Obj_Id
:= Defining_Identifier
(Decl
);
11940 Obj_Typ
:= Base_Type
(Etype
(Obj_Id
));
11941 Expr
:= Expression
(Decl
);
11943 -- Bypass any form of processing for objects which have their
11944 -- finalization disabled. This applies only to objects at the
11947 if Lib_Level
and then Finalize_Storage_Only
(Obj_Typ
) then
11950 -- Finalization of transient objects are treated separately in
11951 -- order to handle sensitive cases. These include:
11953 -- * Aggregate expansion
11954 -- * If, case, and expression with actions expansion
11955 -- * Transient scopes
11957 -- If one of those contexts has marked the transient object as
11958 -- ignored, do not generate finalization actions for it.
11960 elsif Is_Finalized_Transient
(Obj_Id
)
11961 or else Is_Ignored_Transient
(Obj_Id
)
11965 -- Ignored Ghost objects do not need any cleanup actions because
11966 -- they will not appear in the final tree.
11968 elsif Is_Ignored_Ghost_Entity
(Obj_Id
) then
11971 -- The object is of the form:
11972 -- Obj : [constant] Typ [:= Expr];
11974 -- Do not process tag-to-class-wide conversions because they do
11975 -- not yield an object. Do not process the incomplete view of a
11976 -- deferred constant. Note that an object initialized by means
11977 -- of a build-in-place function call may appear as a deferred
11978 -- constant after expansion activities. These kinds of objects
11979 -- must be finalized.
11981 elsif not Is_Imported
(Obj_Id
)
11982 and then Needs_Finalization
(Obj_Typ
)
11983 and then not Is_Tag_To_Class_Wide_Conversion
(Obj_Id
)
11984 and then not (Ekind
(Obj_Id
) = E_Constant
11985 and then not Has_Completion
(Obj_Id
)
11986 and then No
(BIP_Initialization_Call
(Obj_Id
)))
11990 -- The object is of the form:
11991 -- Obj : Access_Typ := Non_BIP_Function_Call'reference;
11993 -- Obj : Access_Typ :=
11994 -- BIP_Function_Call (BIPalloc => 2, ...)'reference;
11996 elsif Is_Access_Type
(Obj_Typ
)
11997 and then Needs_Finalization
11998 (Available_View
(Designated_Type
(Obj_Typ
)))
11999 and then Present
(Expr
)
12001 (Is_Secondary_Stack_BIP_Func_Call
(Expr
)
12003 (Is_Non_BIP_Func_Call
(Expr
)
12004 and then not Is_Related_To_Func_Return
(Obj_Id
)))
12008 -- Processing for "hook" objects generated for transient objects
12009 -- declared inside an Expression_With_Actions.
12011 elsif Is_Access_Type
(Obj_Typ
)
12012 and then Present
(Status_Flag_Or_Transient_Decl
(Obj_Id
))
12013 and then Nkind
(Status_Flag_Or_Transient_Decl
(Obj_Id
)) =
12014 N_Object_Declaration
12018 -- Processing for intermediate results of if expressions where
12019 -- one of the alternatives uses a controlled function call.
12021 elsif Is_Access_Type
(Obj_Typ
)
12022 and then Present
(Status_Flag_Or_Transient_Decl
(Obj_Id
))
12023 and then Nkind
(Status_Flag_Or_Transient_Decl
(Obj_Id
)) =
12024 N_Defining_Identifier
12025 and then Present
(Expr
)
12026 and then Nkind
(Expr
) = N_Null
12030 -- Simple protected objects which use type System.Tasking.
12031 -- Protected_Objects.Protection to manage their locks should be
12032 -- treated as controlled since they require manual cleanup.
12034 elsif Ekind
(Obj_Id
) = E_Variable
12035 and then (Is_Simple_Protected_Type
(Obj_Typ
)
12036 or else Has_Simple_Protected_Object
(Obj_Typ
))
12041 -- Specific cases of object renamings
12043 elsif Nkind
(Decl
) = N_Object_Renaming_Declaration
then
12044 Obj_Id
:= Defining_Identifier
(Decl
);
12045 Obj_Typ
:= Base_Type
(Etype
(Obj_Id
));
12047 -- Bypass any form of processing for objects which have their
12048 -- finalization disabled. This applies only to objects at the
12051 if Lib_Level
and then Finalize_Storage_Only
(Obj_Typ
) then
12054 -- Ignored Ghost object renamings do not need any cleanup actions
12055 -- because they will not appear in the final tree.
12057 elsif Is_Ignored_Ghost_Entity
(Obj_Id
) then
12060 -- Return object of a build-in-place function. This case is
12061 -- recognized and marked by the expansion of an extended return
12062 -- statement (see Expand_N_Extended_Return_Statement).
12064 elsif Needs_Finalization
(Obj_Typ
)
12065 and then Is_Return_Object
(Obj_Id
)
12066 and then Present
(Status_Flag_Or_Transient_Decl
(Obj_Id
))
12070 -- Detect a case where a source object has been initialized by
12071 -- a controlled function call or another object which was later
12072 -- rewritten as a class-wide conversion of Ada.Tags.Displace.
12074 -- Obj1 : CW_Type := Src_Obj;
12075 -- Obj2 : CW_Type := Function_Call (...);
12077 -- Obj1 : CW_Type renames (... Ada.Tags.Displace (Src_Obj));
12078 -- Tmp : ... := Function_Call (...)'reference;
12079 -- Obj2 : CW_Type renames (... Ada.Tags.Displace (Tmp));
12081 elsif Is_Displacement_Of_Object_Or_Function_Result
(Obj_Id
) then
12085 -- Inspect the freeze node of an access-to-controlled type and look
12086 -- for a delayed finalization master. This case arises when the
12087 -- freeze actions are inserted at a later time than the expansion of
12088 -- the context. Since Build_Finalizer is never called on a single
12089 -- construct twice, the master will be ultimately left out and never
12090 -- finalized. This is also needed for freeze actions of designated
12091 -- types themselves, since in some cases the finalization master is
12092 -- associated with a designated type's freeze node rather than that
12093 -- of the access type (see handling for freeze actions in
12094 -- Build_Finalization_Master).
12096 elsif Nkind
(Decl
) = N_Freeze_Entity
12097 and then Present
(Actions
(Decl
))
12099 Typ
:= Entity
(Decl
);
12101 -- Freeze nodes for ignored Ghost types do not need cleanup
12102 -- actions because they will never appear in the final tree.
12104 if Is_Ignored_Ghost_Entity
(Typ
) then
12107 elsif ((Is_Access_Type
(Typ
)
12108 and then not Is_Access_Subprogram_Type
(Typ
)
12109 and then Needs_Finalization
12110 (Available_View
(Designated_Type
(Typ
))))
12111 or else (Is_Type
(Typ
) and then Needs_Finalization
(Typ
)))
12112 and then Requires_Cleanup_Actions
12113 (Actions
(Decl
), Lib_Level
, Nested_Constructs
)
12118 -- Nested package declarations
12120 elsif Nested_Constructs
12121 and then Nkind
(Decl
) = N_Package_Declaration
12123 Pack_Id
:= Defining_Entity
(Decl
);
12125 -- Do not inspect an ignored Ghost package because all code found
12126 -- within will not appear in the final tree.
12128 if Is_Ignored_Ghost_Entity
(Pack_Id
) then
12131 elsif Ekind
(Pack_Id
) /= E_Generic_Package
12132 and then Requires_Cleanup_Actions
12133 (Specification
(Decl
), Lib_Level
)
12138 -- Nested package bodies
12140 elsif Nested_Constructs
and then Nkind
(Decl
) = N_Package_Body
then
12142 -- Do not inspect an ignored Ghost package body because all code
12143 -- found within will not appear in the final tree.
12145 if Is_Ignored_Ghost_Entity
(Defining_Entity
(Decl
)) then
12148 elsif Ekind
(Corresponding_Spec
(Decl
)) /= E_Generic_Package
12149 and then Requires_Cleanup_Actions
(Decl
, Lib_Level
)
12154 elsif Nkind
(Decl
) = N_Block_Statement
12157 -- Handle a rare case caused by a controlled transient object
12158 -- created as part of a record init proc. The variable is wrapped
12159 -- in a block, but the block is not associated with a transient
12164 -- Handle the case where the original context has been wrapped in
12165 -- a block to avoid interference between exception handlers and
12166 -- At_End handlers. Treat the block as transparent and process its
12169 or else Is_Finalization_Wrapper
(Decl
))
12171 if Requires_Cleanup_Actions
(Decl
, Lib_Level
) then
12180 end Requires_Cleanup_Actions
;
12182 ------------------------------------
12183 -- Safe_Unchecked_Type_Conversion --
12184 ------------------------------------
12186 -- Note: this function knows quite a bit about the exact requirements of
12187 -- Gigi with respect to unchecked type conversions, and its code must be
12188 -- coordinated with any changes in Gigi in this area.
12190 -- The above requirements should be documented in Sinfo ???
12192 function Safe_Unchecked_Type_Conversion
(Exp
: Node_Id
) return Boolean is
12197 Pexp
: constant Node_Id
:= Parent
(Exp
);
12200 -- If the expression is the RHS of an assignment or object declaration
12201 -- we are always OK because there will always be a target.
12203 -- Object renaming declarations, (generated for view conversions of
12204 -- actuals in inlined calls), like object declarations, provide an
12205 -- explicit type, and are safe as well.
12207 if (Nkind
(Pexp
) = N_Assignment_Statement
12208 and then Expression
(Pexp
) = Exp
)
12209 or else Nkind_In
(Pexp
, N_Object_Declaration
,
12210 N_Object_Renaming_Declaration
)
12214 -- If the expression is the prefix of an N_Selected_Component we should
12215 -- also be OK because GCC knows to look inside the conversion except if
12216 -- the type is discriminated. We assume that we are OK anyway if the
12217 -- type is not set yet or if it is controlled since we can't afford to
12218 -- introduce a temporary in this case.
12220 elsif Nkind
(Pexp
) = N_Selected_Component
12221 and then Prefix
(Pexp
) = Exp
12223 if No
(Etype
(Pexp
)) then
12227 not Has_Discriminants
(Etype
(Pexp
))
12228 or else Is_Constrained
(Etype
(Pexp
));
12232 -- Set the output type, this comes from Etype if it is set, otherwise we
12233 -- take it from the subtype mark, which we assume was already fully
12236 if Present
(Etype
(Exp
)) then
12237 Otyp
:= Etype
(Exp
);
12239 Otyp
:= Entity
(Subtype_Mark
(Exp
));
12242 -- The input type always comes from the expression, and we assume this
12243 -- is indeed always analyzed, so we can simply get the Etype.
12245 Ityp
:= Etype
(Expression
(Exp
));
12247 -- Initialize alignments to unknown so far
12252 -- Replace a concurrent type by its corresponding record type and each
12253 -- type by its underlying type and do the tests on those. The original
12254 -- type may be a private type whose completion is a concurrent type, so
12255 -- find the underlying type first.
12257 if Present
(Underlying_Type
(Otyp
)) then
12258 Otyp
:= Underlying_Type
(Otyp
);
12261 if Present
(Underlying_Type
(Ityp
)) then
12262 Ityp
:= Underlying_Type
(Ityp
);
12265 if Is_Concurrent_Type
(Otyp
) then
12266 Otyp
:= Corresponding_Record_Type
(Otyp
);
12269 if Is_Concurrent_Type
(Ityp
) then
12270 Ityp
:= Corresponding_Record_Type
(Ityp
);
12273 -- If the base types are the same, we know there is no problem since
12274 -- this conversion will be a noop.
12276 if Implementation_Base_Type
(Otyp
) = Implementation_Base_Type
(Ityp
) then
12279 -- Same if this is an upwards conversion of an untagged type, and there
12280 -- are no constraints involved (could be more general???)
12282 elsif Etype
(Ityp
) = Otyp
12283 and then not Is_Tagged_Type
(Ityp
)
12284 and then not Has_Discriminants
(Ityp
)
12285 and then No
(First_Rep_Item
(Base_Type
(Ityp
)))
12289 -- If the expression has an access type (object or subprogram) we assume
12290 -- that the conversion is safe, because the size of the target is safe,
12291 -- even if it is a record (which might be treated as having unknown size
12294 elsif Is_Access_Type
(Ityp
) then
12297 -- If the size of output type is known at compile time, there is never
12298 -- a problem. Note that unconstrained records are considered to be of
12299 -- known size, but we can't consider them that way here, because we are
12300 -- talking about the actual size of the object.
12302 -- We also make sure that in addition to the size being known, we do not
12303 -- have a case which might generate an embarrassingly large temp in
12304 -- stack checking mode.
12306 elsif Size_Known_At_Compile_Time
(Otyp
)
12308 (not Stack_Checking_Enabled
12309 or else not May_Generate_Large_Temp
(Otyp
))
12310 and then not (Is_Record_Type
(Otyp
) and then not Is_Constrained
(Otyp
))
12314 -- If either type is tagged, then we know the alignment is OK so Gigi
12315 -- will be able to use pointer punning.
12317 elsif Is_Tagged_Type
(Otyp
) or else Is_Tagged_Type
(Ityp
) then
12320 -- If either type is a limited record type, we cannot do a copy, so say
12321 -- safe since there's nothing else we can do.
12323 elsif Is_Limited_Record
(Otyp
) or else Is_Limited_Record
(Ityp
) then
12326 -- Conversions to and from packed array types are always ignored and
12329 elsif Is_Packed_Array_Impl_Type
(Otyp
)
12330 or else Is_Packed_Array_Impl_Type
(Ityp
)
12335 -- The only other cases known to be safe is if the input type's
12336 -- alignment is known to be at least the maximum alignment for the
12337 -- target or if both alignments are known and the output type's
12338 -- alignment is no stricter than the input's. We can use the component
12339 -- type alignment for an array if a type is an unpacked array type.
12341 if Present
(Alignment_Clause
(Otyp
)) then
12342 Oalign
:= Expr_Value
(Expression
(Alignment_Clause
(Otyp
)));
12344 elsif Is_Array_Type
(Otyp
)
12345 and then Present
(Alignment_Clause
(Component_Type
(Otyp
)))
12347 Oalign
:= Expr_Value
(Expression
(Alignment_Clause
12348 (Component_Type
(Otyp
))));
12351 if Present
(Alignment_Clause
(Ityp
)) then
12352 Ialign
:= Expr_Value
(Expression
(Alignment_Clause
(Ityp
)));
12354 elsif Is_Array_Type
(Ityp
)
12355 and then Present
(Alignment_Clause
(Component_Type
(Ityp
)))
12357 Ialign
:= Expr_Value
(Expression
(Alignment_Clause
12358 (Component_Type
(Ityp
))));
12361 if Ialign
/= No_Uint
and then Ialign
> Maximum_Alignment
then
12364 elsif Ialign
/= No_Uint
12365 and then Oalign
/= No_Uint
12366 and then Ialign
<= Oalign
12370 -- Otherwise, Gigi cannot handle this and we must make a temporary
12375 end Safe_Unchecked_Type_Conversion
;
12377 ---------------------------------
12378 -- Set_Current_Value_Condition --
12379 ---------------------------------
12381 -- Note: the implementation of this procedure is very closely tied to the
12382 -- implementation of Get_Current_Value_Condition. Here we set required
12383 -- Current_Value fields, and in Get_Current_Value_Condition, we interpret
12384 -- them, so they must have a consistent view.
12386 procedure Set_Current_Value_Condition
(Cnode
: Node_Id
) is
12388 procedure Set_Entity_Current_Value
(N
: Node_Id
);
12389 -- If N is an entity reference, where the entity is of an appropriate
12390 -- kind, then set the current value of this entity to Cnode, unless
12391 -- there is already a definite value set there.
12393 procedure Set_Expression_Current_Value
(N
: Node_Id
);
12394 -- If N is of an appropriate form, sets an appropriate entry in current
12395 -- value fields of relevant entities. Multiple entities can be affected
12396 -- in the case of an AND or AND THEN.
12398 ------------------------------
12399 -- Set_Entity_Current_Value --
12400 ------------------------------
12402 procedure Set_Entity_Current_Value
(N
: Node_Id
) is
12404 if Is_Entity_Name
(N
) then
12406 Ent
: constant Entity_Id
:= Entity
(N
);
12409 -- Don't capture if not safe to do so
12411 if not Safe_To_Capture_Value
(N
, Ent
, Cond
=> True) then
12415 -- Here we have a case where the Current_Value field may need
12416 -- to be set. We set it if it is not already set to a compile
12417 -- time expression value.
12419 -- Note that this represents a decision that one condition
12420 -- blots out another previous one. That's certainly right if
12421 -- they occur at the same level. If the second one is nested,
12422 -- then the decision is neither right nor wrong (it would be
12423 -- equally OK to leave the outer one in place, or take the new
12424 -- inner one. Really we should record both, but our data
12425 -- structures are not that elaborate.
12427 if Nkind
(Current_Value
(Ent
)) not in N_Subexpr
then
12428 Set_Current_Value
(Ent
, Cnode
);
12432 end Set_Entity_Current_Value
;
12434 ----------------------------------
12435 -- Set_Expression_Current_Value --
12436 ----------------------------------
12438 procedure Set_Expression_Current_Value
(N
: Node_Id
) is
12444 -- Loop to deal with (ignore for now) any NOT operators present. The
12445 -- presence of NOT operators will be handled properly when we call
12446 -- Get_Current_Value_Condition.
12448 while Nkind
(Cond
) = N_Op_Not
loop
12449 Cond
:= Right_Opnd
(Cond
);
12452 -- For an AND or AND THEN, recursively process operands
12454 if Nkind
(Cond
) = N_Op_And
or else Nkind
(Cond
) = N_And_Then
then
12455 Set_Expression_Current_Value
(Left_Opnd
(Cond
));
12456 Set_Expression_Current_Value
(Right_Opnd
(Cond
));
12460 -- Check possible relational operator
12462 if Nkind
(Cond
) in N_Op_Compare
then
12463 if Compile_Time_Known_Value
(Right_Opnd
(Cond
)) then
12464 Set_Entity_Current_Value
(Left_Opnd
(Cond
));
12465 elsif Compile_Time_Known_Value
(Left_Opnd
(Cond
)) then
12466 Set_Entity_Current_Value
(Right_Opnd
(Cond
));
12469 elsif Nkind_In
(Cond
,
12471 N_Qualified_Expression
,
12472 N_Expression_With_Actions
)
12474 Set_Expression_Current_Value
(Expression
(Cond
));
12476 -- Check possible boolean variable reference
12479 Set_Entity_Current_Value
(Cond
);
12481 end Set_Expression_Current_Value
;
12483 -- Start of processing for Set_Current_Value_Condition
12486 Set_Expression_Current_Value
(Condition
(Cnode
));
12487 end Set_Current_Value_Condition
;
12489 --------------------------
12490 -- Set_Elaboration_Flag --
12491 --------------------------
12493 procedure Set_Elaboration_Flag
(N
: Node_Id
; Spec_Id
: Entity_Id
) is
12494 Loc
: constant Source_Ptr
:= Sloc
(N
);
12495 Ent
: constant Entity_Id
:= Elaboration_Entity
(Spec_Id
);
12499 if Present
(Ent
) then
12501 -- Nothing to do if at the compilation unit level, because in this
12502 -- case the flag is set by the binder generated elaboration routine.
12504 if Nkind
(Parent
(N
)) = N_Compilation_Unit
then
12507 -- Here we do need to generate an assignment statement
12510 Check_Restriction
(No_Elaboration_Code
, N
);
12513 Make_Assignment_Statement
(Loc
,
12514 Name
=> New_Occurrence_Of
(Ent
, Loc
),
12515 Expression
=> Make_Integer_Literal
(Loc
, Uint_1
));
12517 -- Mark the assignment statement as elaboration code. This allows
12518 -- the early call region mechanism (see Sem_Elab) to properly
12519 -- ignore such assignments even though they are non-preelaborable
12522 Set_Is_Elaboration_Code
(Asn
);
12524 if Nkind
(Parent
(N
)) = N_Subunit
then
12525 Insert_After
(Corresponding_Stub
(Parent
(N
)), Asn
);
12527 Insert_After
(N
, Asn
);
12532 -- Kill current value indication. This is necessary because the
12533 -- tests of this flag are inserted out of sequence and must not
12534 -- pick up bogus indications of the wrong constant value.
12536 Set_Current_Value
(Ent
, Empty
);
12538 -- If the subprogram is in the current declarative part and
12539 -- 'access has been applied to it, generate an elaboration
12540 -- check at the beginning of the declarations of the body.
12542 if Nkind
(N
) = N_Subprogram_Body
12543 and then Address_Taken
(Spec_Id
)
12545 Ekind_In
(Scope
(Spec_Id
), E_Block
, E_Procedure
, E_Function
)
12548 Loc
: constant Source_Ptr
:= Sloc
(N
);
12549 Decls
: constant List_Id
:= Declarations
(N
);
12553 -- No need to generate this check if first entry in the
12554 -- declaration list is a raise of Program_Error now.
12557 and then Nkind
(First
(Decls
)) = N_Raise_Program_Error
12562 -- Otherwise generate the check
12565 Make_Raise_Program_Error
(Loc
,
12568 Left_Opnd
=> New_Occurrence_Of
(Ent
, Loc
),
12569 Right_Opnd
=> Make_Integer_Literal
(Loc
, Uint_0
)),
12570 Reason
=> PE_Access_Before_Elaboration
);
12573 Set_Declarations
(N
, New_List
(Chk
));
12575 Prepend
(Chk
, Decls
);
12583 end Set_Elaboration_Flag
;
12585 ----------------------------
12586 -- Set_Renamed_Subprogram --
12587 ----------------------------
12589 procedure Set_Renamed_Subprogram
(N
: Node_Id
; E
: Entity_Id
) is
12591 -- If input node is an identifier, we can just reset it
12593 if Nkind
(N
) = N_Identifier
then
12594 Set_Chars
(N
, Chars
(E
));
12597 -- Otherwise we have to do a rewrite, preserving Comes_From_Source
12601 CS
: constant Boolean := Comes_From_Source
(N
);
12603 Rewrite
(N
, Make_Identifier
(Sloc
(N
), Chars
(E
)));
12605 Set_Comes_From_Source
(N
, CS
);
12606 Set_Analyzed
(N
, True);
12609 end Set_Renamed_Subprogram
;
12611 ----------------------
12612 -- Side_Effect_Free --
12613 ----------------------
12615 function Side_Effect_Free
12617 Name_Req
: Boolean := False;
12618 Variable_Ref
: Boolean := False) return Boolean
12620 Typ
: constant Entity_Id
:= Etype
(N
);
12621 -- Result type of the expression
12623 function Safe_Prefixed_Reference
(N
: Node_Id
) return Boolean;
12624 -- The argument N is a construct where the Prefix is dereferenced if it
12625 -- is an access type and the result is a variable. The call returns True
12626 -- if the construct is side effect free (not considering side effects in
12627 -- other than the prefix which are to be tested by the caller).
12629 function Within_In_Parameter
(N
: Node_Id
) return Boolean;
12630 -- Determines if N is a subcomponent of a composite in-parameter. If so,
12631 -- N is not side-effect free when the actual is global and modifiable
12632 -- indirectly from within a subprogram, because it may be passed by
12633 -- reference. The front-end must be conservative here and assume that
12634 -- this may happen with any array or record type. On the other hand, we
12635 -- cannot create temporaries for all expressions for which this
12636 -- condition is true, for various reasons that might require clearing up
12637 -- ??? For example, discriminant references that appear out of place, or
12638 -- spurious type errors with class-wide expressions. As a result, we
12639 -- limit the transformation to loop bounds, which is so far the only
12640 -- case that requires it.
12642 -----------------------------
12643 -- Safe_Prefixed_Reference --
12644 -----------------------------
12646 function Safe_Prefixed_Reference
(N
: Node_Id
) return Boolean is
12648 -- If prefix is not side effect free, definitely not safe
12650 if not Side_Effect_Free
(Prefix
(N
), Name_Req
, Variable_Ref
) then
12653 -- If the prefix is of an access type that is not access-to-constant,
12654 -- then this construct is a variable reference, which means it is to
12655 -- be considered to have side effects if Variable_Ref is set True.
12657 elsif Is_Access_Type
(Etype
(Prefix
(N
)))
12658 and then not Is_Access_Constant
(Etype
(Prefix
(N
)))
12659 and then Variable_Ref
12661 -- Exception is a prefix that is the result of a previous removal
12662 -- of side effects.
12664 return Is_Entity_Name
(Prefix
(N
))
12665 and then not Comes_From_Source
(Prefix
(N
))
12666 and then Ekind
(Entity
(Prefix
(N
))) = E_Constant
12667 and then Is_Internal_Name
(Chars
(Entity
(Prefix
(N
))));
12669 -- If the prefix is an explicit dereference then this construct is a
12670 -- variable reference, which means it is to be considered to have
12671 -- side effects if Variable_Ref is True.
12673 -- We do NOT exclude dereferences of access-to-constant types because
12674 -- we handle them as constant view of variables.
12676 elsif Nkind
(Prefix
(N
)) = N_Explicit_Dereference
12677 and then Variable_Ref
12681 -- Note: The following test is the simplest way of solving a complex
12682 -- problem uncovered by the following test (Side effect on loop bound
12683 -- that is a subcomponent of a global variable:
12685 -- with Text_Io; use Text_Io;
12686 -- procedure Tloop is
12689 -- V : Natural := 4;
12690 -- S : String (1..5) := (others => 'a');
12697 -- with procedure Action;
12698 -- procedure Loop_G (Arg : X; Msg : String)
12700 -- procedure Loop_G (Arg : X; Msg : String) is
12702 -- Put_Line ("begin loop_g " & Msg & " will loop till: "
12703 -- & Natural'Image (Arg.V));
12704 -- for Index in 1 .. Arg.V loop
12705 -- Text_Io.Put_Line
12706 -- (Natural'Image (Index) & " " & Arg.S (Index));
12707 -- if Index > 2 then
12711 -- Put_Line ("end loop_g " & Msg);
12714 -- procedure Loop1 is new Loop_G (Modi);
12715 -- procedure Modi is
12718 -- Loop1 (X1, "from modi");
12722 -- Loop1 (X1, "initial");
12725 -- The output of the above program should be:
12727 -- begin loop_g initial will loop till: 4
12731 -- begin loop_g from modi will loop till: 1
12733 -- end loop_g from modi
12735 -- begin loop_g from modi will loop till: 1
12737 -- end loop_g from modi
12738 -- end loop_g initial
12740 -- If a loop bound is a subcomponent of a global variable, a
12741 -- modification of that variable within the loop may incorrectly
12742 -- affect the execution of the loop.
12744 elsif Nkind
(Parent
(Parent
(N
))) = N_Loop_Parameter_Specification
12745 and then Within_In_Parameter
(Prefix
(N
))
12746 and then Variable_Ref
12750 -- All other cases are side effect free
12755 end Safe_Prefixed_Reference
;
12757 -------------------------
12758 -- Within_In_Parameter --
12759 -------------------------
12761 function Within_In_Parameter
(N
: Node_Id
) return Boolean is
12763 if not Comes_From_Source
(N
) then
12766 elsif Is_Entity_Name
(N
) then
12767 return Ekind
(Entity
(N
)) = E_In_Parameter
;
12769 elsif Nkind_In
(N
, N_Indexed_Component
, N_Selected_Component
) then
12770 return Within_In_Parameter
(Prefix
(N
));
12775 end Within_In_Parameter
;
12777 -- Start of processing for Side_Effect_Free
12780 -- If volatile reference, always consider it to have side effects
12782 if Is_Volatile_Reference
(N
) then
12786 -- Note on checks that could raise Constraint_Error. Strictly, if we
12787 -- take advantage of 11.6, these checks do not count as side effects.
12788 -- However, we would prefer to consider that they are side effects,
12789 -- since the back end CSE does not work very well on expressions which
12790 -- can raise Constraint_Error. On the other hand if we don't consider
12791 -- them to be side effect free, then we get some awkward expansions
12792 -- in -gnato mode, resulting in code insertions at a point where we
12793 -- do not have a clear model for performing the insertions.
12795 -- Special handling for entity names
12797 if Is_Entity_Name
(N
) then
12799 -- A type reference is always side effect free
12801 if Is_Type
(Entity
(N
)) then
12804 -- Variables are considered to be a side effect if Variable_Ref
12805 -- is set or if we have a volatile reference and Name_Req is off.
12806 -- If Name_Req is True then we can't help returning a name which
12807 -- effectively allows multiple references in any case.
12809 elsif Is_Variable
(N
, Use_Original_Node
=> False) then
12810 return not Variable_Ref
12811 and then (not Is_Volatile_Reference
(N
) or else Name_Req
);
12813 -- Any other entity (e.g. a subtype name) is definitely side
12820 -- A value known at compile time is always side effect free
12822 elsif Compile_Time_Known_Value
(N
) then
12825 -- A variable renaming is not side-effect free, because the renaming
12826 -- will function like a macro in the front-end in some cases, and an
12827 -- assignment can modify the component designated by N, so we need to
12828 -- create a temporary for it.
12830 -- The guard testing for Entity being present is needed at least in
12831 -- the case of rewritten predicate expressions, and may well also be
12832 -- appropriate elsewhere. Obviously we can't go testing the entity
12833 -- field if it does not exist, so it's reasonable to say that this is
12834 -- not the renaming case if it does not exist.
12836 elsif Is_Entity_Name
(Original_Node
(N
))
12837 and then Present
(Entity
(Original_Node
(N
)))
12838 and then Is_Renaming_Of_Object
(Entity
(Original_Node
(N
)))
12839 and then Ekind
(Entity
(Original_Node
(N
))) /= E_Constant
12842 RO
: constant Node_Id
:=
12843 Renamed_Object
(Entity
(Original_Node
(N
)));
12846 -- If the renamed object is an indexed component, or an
12847 -- explicit dereference, then the designated object could
12848 -- be modified by an assignment.
12850 if Nkind_In
(RO
, N_Indexed_Component
,
12851 N_Explicit_Dereference
)
12855 -- A selected component must have a safe prefix
12857 elsif Nkind
(RO
) = N_Selected_Component
then
12858 return Safe_Prefixed_Reference
(RO
);
12860 -- In all other cases, designated object cannot be changed so
12861 -- we are side effect free.
12868 -- Remove_Side_Effects generates an object renaming declaration to
12869 -- capture the expression of a class-wide expression. In VM targets
12870 -- the frontend performs no expansion for dispatching calls to
12871 -- class- wide types since they are handled by the VM. Hence, we must
12872 -- locate here if this node corresponds to a previous invocation of
12873 -- Remove_Side_Effects to avoid a never ending loop in the frontend.
12875 elsif not Tagged_Type_Expansion
12876 and then not Comes_From_Source
(N
)
12877 and then Nkind
(Parent
(N
)) = N_Object_Renaming_Declaration
12878 and then Is_Class_Wide_Type
(Typ
)
12882 -- Generating C the type conversion of an access to constrained array
12883 -- type into an access to unconstrained array type involves initializing
12884 -- a fat pointer and the expression cannot be assumed to be free of side
12885 -- effects since it must referenced several times to compute its bounds.
12887 elsif Modify_Tree_For_C
12888 and then Nkind
(N
) = N_Type_Conversion
12889 and then Is_Access_Type
(Typ
)
12890 and then Is_Array_Type
(Designated_Type
(Typ
))
12891 and then not Is_Constrained
(Designated_Type
(Typ
))
12896 -- For other than entity names and compile time known values,
12897 -- check the node kind for special processing.
12901 -- An attribute reference is side effect free if its expressions
12902 -- are side effect free and its prefix is side effect free or
12903 -- is an entity reference.
12905 -- Is this right? what about x'first where x is a variable???
12907 when N_Attribute_Reference
=>
12908 Attribute_Reference
: declare
12910 function Side_Effect_Free_Attribute
12911 (Attribute_Name
: Name_Id
) return Boolean;
12912 -- Returns True if evaluation of the given attribute is
12913 -- considered side-effect free (independent of prefix and
12916 --------------------------------
12917 -- Side_Effect_Free_Attribute --
12918 --------------------------------
12920 function Side_Effect_Free_Attribute
12921 (Attribute_Name
: Name_Id
) return Boolean
12924 case Attribute_Name
is
12931 | Name_Wide_Wide_Image
12933 -- CodePeer doesn't want to see replicated copies of
12936 return not CodePeer_Mode
;
12941 end Side_Effect_Free_Attribute
;
12943 -- Start of processing for Attribute_Reference
12947 Side_Effect_Free
(Expressions
(N
), Name_Req
, Variable_Ref
)
12948 and then Side_Effect_Free_Attribute
(Attribute_Name
(N
))
12949 and then (Is_Entity_Name
(Prefix
(N
))
12950 or else Side_Effect_Free
12951 (Prefix
(N
), Name_Req
, Variable_Ref
));
12952 end Attribute_Reference
;
12954 -- A binary operator is side effect free if and both operands are
12955 -- side effect free. For this purpose binary operators include
12956 -- membership tests and short circuit forms.
12959 | N_Membership_Test
12962 return Side_Effect_Free
(Left_Opnd
(N
), Name_Req
, Variable_Ref
)
12964 Side_Effect_Free
(Right_Opnd
(N
), Name_Req
, Variable_Ref
);
12966 -- An explicit dereference is side effect free only if it is
12967 -- a side effect free prefixed reference.
12969 when N_Explicit_Dereference
=>
12970 return Safe_Prefixed_Reference
(N
);
12972 -- An expression with action is side effect free if its expression
12973 -- is side effect free and it has no actions.
12975 when N_Expression_With_Actions
=>
12977 Is_Empty_List
(Actions
(N
))
12978 and then Side_Effect_Free
12979 (Expression
(N
), Name_Req
, Variable_Ref
);
12981 -- A call to _rep_to_pos is side effect free, since we generate
12982 -- this pure function call ourselves. Moreover it is critically
12983 -- important to make this exception, since otherwise we can have
12984 -- discriminants in array components which don't look side effect
12985 -- free in the case of an array whose index type is an enumeration
12986 -- type with an enumeration rep clause.
12988 -- All other function calls are not side effect free
12990 when N_Function_Call
=>
12992 Nkind
(Name
(N
)) = N_Identifier
12993 and then Is_TSS
(Name
(N
), TSS_Rep_To_Pos
)
12994 and then Side_Effect_Free
12995 (First
(Parameter_Associations
(N
)),
12996 Name_Req
, Variable_Ref
);
12998 -- An IF expression is side effect free if it's of a scalar type, and
12999 -- all its components are all side effect free (conditions and then
13000 -- actions and else actions). We restrict to scalar types, since it
13001 -- is annoying to deal with things like (if A then B else C)'First
13002 -- where the type involved is a string type.
13004 when N_If_Expression
=>
13006 Is_Scalar_Type
(Typ
)
13007 and then Side_Effect_Free
13008 (Expressions
(N
), Name_Req
, Variable_Ref
);
13010 -- An indexed component is side effect free if it is a side
13011 -- effect free prefixed reference and all the indexing
13012 -- expressions are side effect free.
13014 when N_Indexed_Component
=>
13016 Side_Effect_Free
(Expressions
(N
), Name_Req
, Variable_Ref
)
13017 and then Safe_Prefixed_Reference
(N
);
13019 -- A type qualification, type conversion, or unchecked expression is
13020 -- side effect free if the expression is side effect free.
13022 when N_Qualified_Expression
13023 | N_Type_Conversion
13024 | N_Unchecked_Expression
13026 return Side_Effect_Free
(Expression
(N
), Name_Req
, Variable_Ref
);
13028 -- A selected component is side effect free only if it is a side
13029 -- effect free prefixed reference.
13031 when N_Selected_Component
=>
13032 return Safe_Prefixed_Reference
(N
);
13034 -- A range is side effect free if the bounds are side effect free
13037 return Side_Effect_Free
(Low_Bound
(N
), Name_Req
, Variable_Ref
)
13039 Side_Effect_Free
(High_Bound
(N
), Name_Req
, Variable_Ref
);
13041 -- A slice is side effect free if it is a side effect free
13042 -- prefixed reference and the bounds are side effect free.
13046 Side_Effect_Free
(Discrete_Range
(N
), Name_Req
, Variable_Ref
)
13047 and then Safe_Prefixed_Reference
(N
);
13049 -- A unary operator is side effect free if the operand
13050 -- is side effect free.
13053 return Side_Effect_Free
(Right_Opnd
(N
), Name_Req
, Variable_Ref
);
13055 -- An unchecked type conversion is side effect free only if it
13056 -- is safe and its argument is side effect free.
13058 when N_Unchecked_Type_Conversion
=>
13060 Safe_Unchecked_Type_Conversion
(N
)
13061 and then Side_Effect_Free
13062 (Expression
(N
), Name_Req
, Variable_Ref
);
13064 -- A literal is side effect free
13066 when N_Character_Literal
13067 | N_Integer_Literal
13073 -- We consider that anything else has side effects. This is a bit
13074 -- crude, but we are pretty close for most common cases, and we
13075 -- are certainly correct (i.e. we never return True when the
13076 -- answer should be False).
13081 end Side_Effect_Free
;
13083 -- A list is side effect free if all elements of the list are side
13086 function Side_Effect_Free
13088 Name_Req
: Boolean := False;
13089 Variable_Ref
: Boolean := False) return Boolean
13094 if L
= No_List
or else L
= Error_List
then
13099 while Present
(N
) loop
13100 if not Side_Effect_Free
(N
, Name_Req
, Variable_Ref
) then
13109 end Side_Effect_Free
;
13111 ----------------------------------
13112 -- Silly_Boolean_Array_Not_Test --
13113 ----------------------------------
13115 -- This procedure implements an odd and silly test. We explicitly check
13116 -- for the case where the 'First of the component type is equal to the
13117 -- 'Last of this component type, and if this is the case, we make sure
13118 -- that constraint error is raised. The reason is that the NOT is bound
13119 -- to cause CE in this case, and we will not otherwise catch it.
13121 -- No such check is required for AND and OR, since for both these cases
13122 -- False op False = False, and True op True = True. For the XOR case,
13123 -- see Silly_Boolean_Array_Xor_Test.
13125 -- Believe it or not, this was reported as a bug. Note that nearly always,
13126 -- the test will evaluate statically to False, so the code will be
13127 -- statically removed, and no extra overhead caused.
13129 procedure Silly_Boolean_Array_Not_Test
(N
: Node_Id
; T
: Entity_Id
) is
13130 Loc
: constant Source_Ptr
:= Sloc
(N
);
13131 CT
: constant Entity_Id
:= Component_Type
(T
);
13134 -- The check we install is
13136 -- constraint_error when
13137 -- component_type'first = component_type'last
13138 -- and then array_type'Length /= 0)
13140 -- We need the last guard because we don't want to raise CE for empty
13141 -- arrays since no out of range values result. (Empty arrays with a
13142 -- component type of True .. True -- very useful -- even the ACATS
13143 -- does not test that marginal case).
13146 Make_Raise_Constraint_Error
(Loc
,
13148 Make_And_Then
(Loc
,
13152 Make_Attribute_Reference
(Loc
,
13153 Prefix
=> New_Occurrence_Of
(CT
, Loc
),
13154 Attribute_Name
=> Name_First
),
13157 Make_Attribute_Reference
(Loc
,
13158 Prefix
=> New_Occurrence_Of
(CT
, Loc
),
13159 Attribute_Name
=> Name_Last
)),
13161 Right_Opnd
=> Make_Non_Empty_Check
(Loc
, Right_Opnd
(N
))),
13162 Reason
=> CE_Range_Check_Failed
));
13163 end Silly_Boolean_Array_Not_Test
;
13165 ----------------------------------
13166 -- Silly_Boolean_Array_Xor_Test --
13167 ----------------------------------
13169 -- This procedure implements an odd and silly test. We explicitly check
13170 -- for the XOR case where the component type is True .. True, since this
13171 -- will raise constraint error. A special check is required since CE
13172 -- will not be generated otherwise (cf Expand_Packed_Not).
13174 -- No such check is required for AND and OR, since for both these cases
13175 -- False op False = False, and True op True = True, and no check is
13176 -- required for the case of False .. False, since False xor False = False.
13177 -- See also Silly_Boolean_Array_Not_Test
13179 procedure Silly_Boolean_Array_Xor_Test
(N
: Node_Id
; T
: Entity_Id
) is
13180 Loc
: constant Source_Ptr
:= Sloc
(N
);
13181 CT
: constant Entity_Id
:= Component_Type
(T
);
13184 -- The check we install is
13186 -- constraint_error when
13187 -- Boolean (component_type'First)
13188 -- and then Boolean (component_type'Last)
13189 -- and then array_type'Length /= 0)
13191 -- We need the last guard because we don't want to raise CE for empty
13192 -- arrays since no out of range values result (Empty arrays with a
13193 -- component type of True .. True -- very useful -- even the ACATS
13194 -- does not test that marginal case).
13197 Make_Raise_Constraint_Error
(Loc
,
13199 Make_And_Then
(Loc
,
13201 Make_And_Then
(Loc
,
13203 Convert_To
(Standard_Boolean
,
13204 Make_Attribute_Reference
(Loc
,
13205 Prefix
=> New_Occurrence_Of
(CT
, Loc
),
13206 Attribute_Name
=> Name_First
)),
13209 Convert_To
(Standard_Boolean
,
13210 Make_Attribute_Reference
(Loc
,
13211 Prefix
=> New_Occurrence_Of
(CT
, Loc
),
13212 Attribute_Name
=> Name_Last
))),
13214 Right_Opnd
=> Make_Non_Empty_Check
(Loc
, Right_Opnd
(N
))),
13215 Reason
=> CE_Range_Check_Failed
));
13216 end Silly_Boolean_Array_Xor_Test
;
13218 --------------------------
13219 -- Target_Has_Fixed_Ops --
13220 --------------------------
13222 Integer_Sized_Small
: Ureal
;
13223 -- Set to 2.0 ** -(Integer'Size - 1) the first time that this function is
13224 -- called (we don't want to compute it more than once).
13226 Long_Integer_Sized_Small
: Ureal
;
13227 -- Set to 2.0 ** -(Long_Integer'Size - 1) the first time that this function
13228 -- is called (we don't want to compute it more than once)
13230 First_Time_For_THFO
: Boolean := True;
13231 -- Set to False after first call (if Fractional_Fixed_Ops_On_Target)
13233 function Target_Has_Fixed_Ops
13234 (Left_Typ
: Entity_Id
;
13235 Right_Typ
: Entity_Id
;
13236 Result_Typ
: Entity_Id
) return Boolean
13238 function Is_Fractional_Type
(Typ
: Entity_Id
) return Boolean;
13239 -- Return True if the given type is a fixed-point type with a small
13240 -- value equal to 2 ** (-(T'Object_Size - 1)) and whose values have
13241 -- an absolute value less than 1.0. This is currently limited to
13242 -- fixed-point types that map to Integer or Long_Integer.
13244 ------------------------
13245 -- Is_Fractional_Type --
13246 ------------------------
13248 function Is_Fractional_Type
(Typ
: Entity_Id
) return Boolean is
13250 if Esize
(Typ
) = Standard_Integer_Size
then
13251 return Small_Value
(Typ
) = Integer_Sized_Small
;
13253 elsif Esize
(Typ
) = Standard_Long_Integer_Size
then
13254 return Small_Value
(Typ
) = Long_Integer_Sized_Small
;
13259 end Is_Fractional_Type
;
13261 -- Start of processing for Target_Has_Fixed_Ops
13264 -- Return False if Fractional_Fixed_Ops_On_Target is false
13266 if not Fractional_Fixed_Ops_On_Target
then
13270 -- Here the target has Fractional_Fixed_Ops, if first time, compute
13271 -- standard constants used by Is_Fractional_Type.
13273 if First_Time_For_THFO
then
13274 First_Time_For_THFO
:= False;
13276 Integer_Sized_Small
:=
13279 Den
=> UI_From_Int
(Standard_Integer_Size
- 1),
13282 Long_Integer_Sized_Small
:=
13285 Den
=> UI_From_Int
(Standard_Long_Integer_Size
- 1),
13289 -- Return True if target supports fixed-by-fixed multiply/divide for
13290 -- fractional fixed-point types (see Is_Fractional_Type) and the operand
13291 -- and result types are equivalent fractional types.
13293 return Is_Fractional_Type
(Base_Type
(Left_Typ
))
13294 and then Is_Fractional_Type
(Base_Type
(Right_Typ
))
13295 and then Is_Fractional_Type
(Base_Type
(Result_Typ
))
13296 and then Esize
(Left_Typ
) = Esize
(Right_Typ
)
13297 and then Esize
(Left_Typ
) = Esize
(Result_Typ
);
13298 end Target_Has_Fixed_Ops
;
13300 -------------------
13301 -- Type_Map_Hash --
13302 -------------------
13304 function Type_Map_Hash
(Id
: Entity_Id
) return Type_Map_Header
is
13306 return Type_Map_Header
(Id
mod Type_Map_Size
);
13309 ------------------------------------------
13310 -- Type_May_Have_Bit_Aligned_Components --
13311 ------------------------------------------
13313 function Type_May_Have_Bit_Aligned_Components
13314 (Typ
: Entity_Id
) return Boolean
13317 -- Array type, check component type
13319 if Is_Array_Type
(Typ
) then
13321 Type_May_Have_Bit_Aligned_Components
(Component_Type
(Typ
));
13323 -- Record type, check components
13325 elsif Is_Record_Type
(Typ
) then
13330 E
:= First_Component_Or_Discriminant
(Typ
);
13331 while Present
(E
) loop
13332 if Component_May_Be_Bit_Aligned
(E
)
13333 or else Type_May_Have_Bit_Aligned_Components
(Etype
(E
))
13338 Next_Component_Or_Discriminant
(E
);
13344 -- Type other than array or record is always OK
13349 end Type_May_Have_Bit_Aligned_Components
;
13351 -------------------------------
13352 -- Update_Primitives_Mapping --
13353 -------------------------------
13355 procedure Update_Primitives_Mapping
13356 (Inher_Id
: Entity_Id
;
13357 Subp_Id
: Entity_Id
)
13361 (Parent_Type
=> Find_Dispatching_Type
(Inher_Id
),
13362 Derived_Type
=> Find_Dispatching_Type
(Subp_Id
));
13363 end Update_Primitives_Mapping
;
13365 ----------------------------------
13366 -- Within_Case_Or_If_Expression --
13367 ----------------------------------
13369 function Within_Case_Or_If_Expression
(N
: Node_Id
) return Boolean is
13373 -- Locate an enclosing case or if expression. Note that these constructs
13374 -- can be expanded into Expression_With_Actions, hence the test of the
13378 while Present
(Par
) loop
13379 if Nkind_In
(Original_Node
(Par
), N_Case_Expression
,
13384 -- Prevent the search from going too far
13386 elsif Is_Body_Or_Package_Declaration
(Par
) then
13390 Par
:= Parent
(Par
);
13394 end Within_Case_Or_If_Expression
;
13396 --------------------------------
13397 -- Within_Internal_Subprogram --
13398 --------------------------------
13400 function Within_Internal_Subprogram
return Boolean is
13404 S
:= Current_Scope
;
13405 while Present
(S
) and then not Is_Subprogram
(S
) loop
13410 and then Get_TSS_Name
(S
) /= TSS_Null
13411 and then not Is_Predicate_Function
(S
)
13412 and then not Is_Predicate_Function_M
(S
);
13413 end Within_Internal_Subprogram
;