1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2012, 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 Inline; use Inline;
38 with Itypes; use Itypes;
40 with Nlists; use Nlists;
41 with Nmake; use Nmake;
43 with Restrict; use Restrict;
44 with Rident; use Rident;
46 with Sem_Aux; use Sem_Aux;
47 with Sem_Ch8; use Sem_Ch8;
48 with Sem_Eval; use Sem_Eval;
49 with Sem_Prag; use Sem_Prag;
50 with Sem_Res; use Sem_Res;
51 with Sem_Type; use Sem_Type;
52 with Sem_Util; use Sem_Util;
53 with Snames; use Snames;
54 with Stand; use Stand;
55 with Stringt; use Stringt;
56 with Targparm; use Targparm;
57 with Tbuild; use Tbuild;
58 with Ttypes; use Ttypes;
59 with Urealp; use Urealp;
60 with Validsw; use Validsw;
62 package body Exp_Util is
64 -----------------------
65 -- Local Subprograms --
66 -----------------------
68 function Build_Task_Array_Image
72 Dyn : Boolean := False) return Node_Id;
73 -- Build function to generate the image string for a task that is an array
74 -- component, concatenating the images of each index. To avoid storage
75 -- leaks, the string is built with successive slice assignments. The flag
76 -- Dyn indicates whether this is called for the initialization procedure of
77 -- an array of tasks, or for the name of a dynamically created task that is
78 -- assigned to an indexed component.
80 function Build_Task_Image_Function
84 Res : Entity_Id) return Node_Id;
85 -- Common processing for Task_Array_Image and Task_Record_Image. Build
86 -- function body that computes image.
88 procedure Build_Task_Image_Prefix
97 -- Common processing for Task_Array_Image and Task_Record_Image. Create
98 -- local variables and assign prefix of name to result string.
100 function Build_Task_Record_Image
103 Dyn : Boolean := False) return Node_Id;
104 -- Build function to generate the image string for a task that is a record
105 -- component. Concatenate name of variable with that of selector. The flag
106 -- Dyn indicates whether this is called for the initialization procedure of
107 -- record with task components, or for a dynamically created task that is
108 -- assigned to a selected component.
110 function Make_CW_Equivalent_Type
112 E : Node_Id) return Entity_Id;
113 -- T is a class-wide type entity, E is the initial expression node that
114 -- constrains T in case such as: " X: T := E" or "new T'(E)". This function
115 -- returns the entity of the Equivalent type and inserts on the fly the
116 -- necessary declaration such as:
118 -- type anon is record
119 -- _parent : Root_Type (T); constrained with E discriminants (if any)
120 -- Extension : String (1 .. expr to match size of E);
123 -- This record is compatible with any object of the class of T thanks to
124 -- the first field and has the same size as E thanks to the second.
126 function Make_Literal_Range
128 Literal_Typ : Entity_Id) return Node_Id;
129 -- Produce a Range node whose bounds are:
130 -- Low_Bound (Literal_Type) ..
131 -- Low_Bound (Literal_Type) + (Length (Literal_Typ) - 1)
132 -- this is used for expanding declarations like X : String := "sdfgdfg";
134 -- If the index type of the target array is not integer, we generate:
135 -- Low_Bound (Literal_Type) ..
137 -- (Literal_Type'Pos (Low_Bound (Literal_Type))
138 -- + (Length (Literal_Typ) -1))
140 function Make_Non_Empty_Check
142 N : Node_Id) return Node_Id;
143 -- Produce a boolean expression checking that the unidimensional array
144 -- node N is not empty.
146 function New_Class_Wide_Subtype
148 N : Node_Id) return Entity_Id;
149 -- Create an implicit subtype of CW_Typ attached to node N
151 function Requires_Cleanup_Actions
153 For_Package : Boolean;
154 Nested_Constructs : Boolean) return Boolean;
155 -- Given a list L, determine whether it contains one of the following:
157 -- 1) controlled objects
158 -- 2) library-level tagged types
160 -- Flag For_Package should be set when the list comes from a package spec
161 -- or body. Flag Nested_Constructs should be set when any nested packages
162 -- declared in L must be processed.
164 -------------------------------------
165 -- Activate_Atomic_Synchronization --
166 -------------------------------------
168 procedure Activate_Atomic_Synchronization (N : Node_Id) is
172 case Nkind (Parent (N)) is
174 -- Check for cases of appearing in the prefix of a construct where
175 -- we don't need atomic synchronization for this kind of usage.
178 -- Nothing to do if we are the prefix of an attribute, since we
179 -- do not want an atomic sync operation for things like 'Size.
181 N_Attribute_Reference |
183 -- The N_Reference node is like an attribute
187 -- Nothing to do for a reference to a component (or components)
188 -- of a composite object. Only reads and updates of the object
189 -- as a whole require atomic synchronization (RM C.6 (15)).
191 N_Indexed_Component |
192 N_Selected_Component |
195 -- For all the above cases, nothing to do if we are the prefix
197 if Prefix (Parent (N)) = N then
204 -- Go ahead and set the flag
206 Set_Atomic_Sync_Required (N);
208 -- Generate info message if requested
210 if Warn_On_Atomic_Synchronization then
215 when N_Selected_Component | N_Expanded_Name =>
216 Msg_Node := Selector_Name (N);
218 when N_Explicit_Dereference | N_Indexed_Component =>
222 pragma Assert (False);
226 if Present (Msg_Node) then
227 Error_Msg_N ("?info: atomic synchronization set for &", Msg_Node);
229 Error_Msg_N ("?info: atomic synchronization set", N);
232 end Activate_Atomic_Synchronization;
234 ----------------------
235 -- Adjust_Condition --
236 ----------------------
238 procedure Adjust_Condition (N : Node_Id) is
245 Loc : constant Source_Ptr := Sloc (N);
246 T : constant Entity_Id := Etype (N);
250 -- Defend against a call where the argument has no type, or has a
251 -- type that is not Boolean. This can occur because of prior errors.
253 if No (T) or else not Is_Boolean_Type (T) then
257 -- Apply validity checking if needed
259 if Validity_Checks_On and Validity_Check_Tests then
263 -- Immediate return if standard boolean, the most common case,
264 -- where nothing needs to be done.
266 if Base_Type (T) = Standard_Boolean then
270 -- Case of zero/non-zero semantics or non-standard enumeration
271 -- representation. In each case, we rewrite the node as:
273 -- ityp!(N) /= False'Enum_Rep
275 -- where ityp is an integer type with large enough size to hold any
278 if Nonzero_Is_True (T) or else Has_Non_Standard_Rep (T) then
279 if Esize (T) <= Esize (Standard_Integer) then
280 Ti := Standard_Integer;
282 Ti := Standard_Long_Long_Integer;
287 Left_Opnd => Unchecked_Convert_To (Ti, N),
289 Make_Attribute_Reference (Loc,
290 Attribute_Name => Name_Enum_Rep,
292 New_Occurrence_Of (First_Literal (T), Loc))));
293 Analyze_And_Resolve (N, Standard_Boolean);
296 Rewrite (N, Convert_To (Standard_Boolean, N));
297 Analyze_And_Resolve (N, Standard_Boolean);
300 end Adjust_Condition;
302 ------------------------
303 -- Adjust_Result_Type --
304 ------------------------
306 procedure Adjust_Result_Type (N : Node_Id; T : Entity_Id) is
308 -- Ignore call if current type is not Standard.Boolean
310 if Etype (N) /= Standard_Boolean then
314 -- If result is already of correct type, nothing to do. Note that
315 -- this will get the most common case where everything has a type
316 -- of Standard.Boolean.
318 if Base_Type (T) = Standard_Boolean then
323 KP : constant Node_Kind := Nkind (Parent (N));
326 -- If result is to be used as a Condition in the syntax, no need
327 -- to convert it back, since if it was changed to Standard.Boolean
328 -- using Adjust_Condition, that is just fine for this usage.
330 if KP in N_Raise_xxx_Error or else KP in N_Has_Condition then
333 -- If result is an operand of another logical operation, no need
334 -- to reset its type, since Standard.Boolean is just fine, and
335 -- such operations always do Adjust_Condition on their operands.
337 elsif KP in N_Op_Boolean
338 or else KP in N_Short_Circuit
339 or else KP = N_Op_Not
343 -- Otherwise we perform a conversion from the current type, which
344 -- must be Standard.Boolean, to the desired type.
348 Rewrite (N, Convert_To (T, N));
349 Analyze_And_Resolve (N, T);
353 end Adjust_Result_Type;
355 --------------------------
356 -- Append_Freeze_Action --
357 --------------------------
359 procedure Append_Freeze_Action (T : Entity_Id; N : Node_Id) is
363 Ensure_Freeze_Node (T);
364 Fnode := Freeze_Node (T);
366 if No (Actions (Fnode)) then
367 Set_Actions (Fnode, New_List);
370 Append (N, Actions (Fnode));
371 end Append_Freeze_Action;
373 ---------------------------
374 -- Append_Freeze_Actions --
375 ---------------------------
377 procedure Append_Freeze_Actions (T : Entity_Id; L : List_Id) is
378 Fnode : constant Node_Id := Freeze_Node (T);
385 if No (Actions (Fnode)) then
386 Set_Actions (Fnode, L);
388 Append_List (L, Actions (Fnode));
391 end Append_Freeze_Actions;
393 ------------------------------------
394 -- Build_Allocate_Deallocate_Proc --
395 ------------------------------------
397 procedure Build_Allocate_Deallocate_Proc
399 Is_Allocate : Boolean)
401 Desig_Typ : Entity_Id;
404 Proc_To_Call : Node_Id := Empty;
407 function Find_Finalize_Address (Typ : Entity_Id) return Entity_Id;
408 -- Locate TSS primitive Finalize_Address in type Typ
410 function Find_Object (E : Node_Id) return Node_Id;
411 -- Given an arbitrary expression of an allocator, try to find an object
412 -- reference in it, otherwise return the original expression.
414 function Is_Allocate_Deallocate_Proc (Subp : Entity_Id) return Boolean;
415 -- Determine whether subprogram Subp denotes a custom allocate or
418 ---------------------------
419 -- Find_Finalize_Address --
420 ---------------------------
422 function Find_Finalize_Address (Typ : Entity_Id) return Entity_Id is
423 Utyp : Entity_Id := Typ;
426 -- Handle protected class-wide or task class-wide types
428 if Is_Class_Wide_Type (Utyp) then
429 if Is_Concurrent_Type (Root_Type (Utyp)) then
430 Utyp := Root_Type (Utyp);
432 elsif Is_Private_Type (Root_Type (Utyp))
433 and then Present (Full_View (Root_Type (Utyp)))
434 and then Is_Concurrent_Type (Full_View (Root_Type (Utyp)))
436 Utyp := Full_View (Root_Type (Utyp));
440 -- Handle private types
442 if Is_Private_Type (Utyp)
443 and then Present (Full_View (Utyp))
445 Utyp := Full_View (Utyp);
448 -- Handle protected and task types
450 if Is_Concurrent_Type (Utyp)
451 and then Present (Corresponding_Record_Type (Utyp))
453 Utyp := Corresponding_Record_Type (Utyp);
456 Utyp := Underlying_Type (Base_Type (Utyp));
458 -- Deal with non-tagged derivation of private views. If the parent is
459 -- now known to be protected, the finalization routine is the one
460 -- defined on the corresponding record of the ancestor (corresponding
461 -- records do not automatically inherit operations, but maybe they
464 if Is_Untagged_Derivation (Typ) then
465 if Is_Protected_Type (Typ) then
466 Utyp := Corresponding_Record_Type (Root_Type (Base_Type (Typ)));
468 Utyp := Underlying_Type (Root_Type (Base_Type (Typ)));
470 if Is_Protected_Type (Utyp) then
471 Utyp := Corresponding_Record_Type (Utyp);
476 -- If the underlying_type is a subtype, we are dealing with the
477 -- completion of a private type. We need to access the base type and
478 -- generate a conversion to it.
480 if Utyp /= Base_Type (Utyp) then
481 pragma Assert (Is_Private_Type (Typ));
483 Utyp := Base_Type (Utyp);
486 -- When dealing with an internally built full view for a type with
487 -- unknown discriminants, use the original record type.
489 if Is_Underlying_Record_View (Utyp) then
490 Utyp := Etype (Utyp);
493 return TSS (Utyp, TSS_Finalize_Address);
494 end Find_Finalize_Address;
500 function Find_Object (E : Node_Id) return Node_Id is
504 pragma Assert (Is_Allocate);
508 if Nkind_In (Expr, N_Qualified_Expression,
509 N_Unchecked_Type_Conversion)
511 Expr := Expression (Expr);
513 elsif Nkind (Expr) = N_Explicit_Dereference then
514 Expr := Prefix (Expr);
524 ---------------------------------
525 -- Is_Allocate_Deallocate_Proc --
526 ---------------------------------
528 function Is_Allocate_Deallocate_Proc (Subp : Entity_Id) return Boolean is
530 -- Look for a subprogram body with only one statement which is a
531 -- call to Allocate_Any_Controlled / Deallocate_Any_Controlled.
533 if Ekind (Subp) = E_Procedure
534 and then Nkind (Parent (Parent (Subp))) = N_Subprogram_Body
537 HSS : constant Node_Id :=
538 Handled_Statement_Sequence (Parent (Parent (Subp)));
542 if Present (Statements (HSS))
543 and then Nkind (First (Statements (HSS))) =
544 N_Procedure_Call_Statement
546 Proc := Entity (Name (First (Statements (HSS))));
549 Is_RTE (Proc, RE_Allocate_Any_Controlled)
550 or else Is_RTE (Proc, RE_Deallocate_Any_Controlled);
556 end Is_Allocate_Deallocate_Proc;
558 -- Start of processing for Build_Allocate_Deallocate_Proc
561 -- Do not perform this expansion in Alfa mode because it is not
568 -- Obtain the attributes of the allocation / deallocation
570 if Nkind (N) = N_Free_Statement then
571 Expr := Expression (N);
572 Ptr_Typ := Base_Type (Etype (Expr));
573 Proc_To_Call := Procedure_To_Call (N);
576 if Nkind (N) = N_Object_Declaration then
577 Expr := Expression (N);
582 -- In certain cases an allocator with a qualified expression may
583 -- be relocated and used as the initialization expression of a
587 -- Obj : Ptr_Typ := new Desig_Typ'(...);
590 -- Tmp : Ptr_Typ := new Desig_Typ'(...);
591 -- Obj : Ptr_Typ := Tmp;
593 -- Since the allocator is always marked as analyzed to avoid infinite
594 -- expansion, it will never be processed by this routine given that
595 -- the designated type needs finalization actions. Detect this case
596 -- and complete the expansion of the allocator.
598 if Nkind (Expr) = N_Identifier
599 and then Nkind (Parent (Entity (Expr))) = N_Object_Declaration
600 and then Nkind (Expression (Parent (Entity (Expr)))) = N_Allocator
602 Build_Allocate_Deallocate_Proc (Parent (Entity (Expr)), True);
606 -- The allocator may have been rewritten into something else in which
607 -- case the expansion performed by this routine does not apply.
609 if Nkind (Expr) /= N_Allocator then
613 Ptr_Typ := Base_Type (Etype (Expr));
614 Proc_To_Call := Procedure_To_Call (Expr);
617 Pool_Id := Associated_Storage_Pool (Ptr_Typ);
618 Desig_Typ := Available_View (Designated_Type (Ptr_Typ));
620 -- Handle concurrent types
622 if Is_Concurrent_Type (Desig_Typ)
623 and then Present (Corresponding_Record_Type (Desig_Typ))
625 Desig_Typ := Corresponding_Record_Type (Desig_Typ);
628 -- Do not process allocations / deallocations without a pool
633 -- Do not process allocations on / deallocations from the secondary
636 elsif Is_RTE (Pool_Id, RE_SS_Pool) then
639 -- Do not replicate the machinery if the allocator / free has already
640 -- been expanded and has a custom Allocate / Deallocate.
642 elsif Present (Proc_To_Call)
643 and then Is_Allocate_Deallocate_Proc (Proc_To_Call)
648 if Needs_Finalization (Desig_Typ) then
650 -- Certain run-time configurations and targets do not provide support
651 -- for controlled types.
653 if Restriction_Active (No_Finalization) then
656 -- Do nothing if the access type may never allocate / deallocate
659 elsif No_Pool_Assigned (Ptr_Typ) then
662 -- Access-to-controlled types are not supported on .NET/JVM since
663 -- these targets cannot support pools and address arithmetic.
665 elsif VM_Target /= No_VM then
669 -- The allocation / deallocation of a controlled object must be
670 -- chained on / detached from a finalization master.
672 pragma Assert (Present (Finalization_Master (Ptr_Typ)));
674 -- The only other kind of allocation / deallocation supported by this
675 -- routine is on / from a subpool.
677 elsif Nkind (Expr) = N_Allocator
678 and then No (Subpool_Handle_Name (Expr))
684 Loc : constant Source_Ptr := Sloc (N);
685 Addr_Id : constant Entity_Id := Make_Temporary (Loc, 'A');
686 Alig_Id : constant Entity_Id := Make_Temporary (Loc, 'L');
687 Proc_Id : constant Entity_Id := Make_Temporary (Loc, 'P');
688 Size_Id : constant Entity_Id := Make_Temporary (Loc, 'S');
691 Fin_Addr_Id : Entity_Id;
692 Fin_Mas_Act : Node_Id;
693 Fin_Mas_Id : Entity_Id;
694 Proc_To_Call : Entity_Id;
695 Subpool : Node_Id := Empty;
698 -- Step 1: Construct all the actuals for the call to library routine
699 -- Allocate_Any_Controlled / Deallocate_Any_Controlled.
703 Actuals := New_List (New_Reference_To (Pool_Id, Loc));
709 if Nkind (Expr) = N_Allocator then
710 Subpool := Subpool_Handle_Name (Expr);
713 if Present (Subpool) then
714 Append_To (Actuals, New_Reference_To (Entity (Subpool), Loc));
716 Append_To (Actuals, Make_Null (Loc));
719 -- c) Finalization master
721 if Needs_Finalization (Desig_Typ) then
722 Fin_Mas_Id := Finalization_Master (Ptr_Typ);
723 Fin_Mas_Act := New_Reference_To (Fin_Mas_Id, Loc);
725 -- Handle the case where the master is actually a pointer to a
726 -- master. This case arises in build-in-place functions.
728 if Is_Access_Type (Etype (Fin_Mas_Id)) then
729 Append_To (Actuals, Fin_Mas_Act);
732 Make_Attribute_Reference (Loc,
733 Prefix => Fin_Mas_Act,
734 Attribute_Name => Name_Unrestricted_Access));
737 Append_To (Actuals, Make_Null (Loc));
740 -- d) Finalize_Address
742 -- Primitive Finalize_Address is never generated in CodePeer mode
743 -- since it contains an Unchecked_Conversion.
745 if Needs_Finalization (Desig_Typ)
746 and then not CodePeer_Mode
748 Fin_Addr_Id := Find_Finalize_Address (Desig_Typ);
749 pragma Assert (Present (Fin_Addr_Id));
752 Make_Attribute_Reference (Loc,
753 Prefix => New_Reference_To (Fin_Addr_Id, Loc),
754 Attribute_Name => Name_Unrestricted_Access));
756 Append_To (Actuals, Make_Null (Loc));
764 Append_To (Actuals, New_Reference_To (Addr_Id, Loc));
765 Append_To (Actuals, New_Reference_To (Size_Id, Loc));
767 if Is_Allocate or else not Is_Class_Wide_Type (Desig_Typ) then
768 Append_To (Actuals, New_Reference_To (Alig_Id, Loc));
770 -- For deallocation of class wide types we obtain the value of
771 -- alignment from the Type Specific Record of the deallocated object.
772 -- This is needed because the frontend expansion of class-wide types
773 -- into equivalent types confuses the backend.
779 -- ... because 'Alignment applied to class-wide types is expanded
780 -- into the code that reads the value of alignment from the TSD
781 -- (see Expand_N_Attribute_Reference)
784 Unchecked_Convert_To (RTE (RE_Storage_Offset),
785 Make_Attribute_Reference (Loc,
787 Make_Explicit_Dereference (Loc, Relocate_Node (Expr)),
788 Attribute_Name => Name_Alignment)));
793 -- Generate a run-time check to determine whether a class-wide object
794 -- is truly controlled.
796 if Needs_Finalization (Desig_Typ) then
797 if Is_Class_Wide_Type (Desig_Typ)
798 or else Is_Generic_Actual_Type (Desig_Typ)
801 Flag_Id : constant Entity_Id := Make_Temporary (Loc, 'F');
808 Temp := Find_Object (Expression (Expr));
813 -- Processing for generic actuals
815 if Is_Generic_Actual_Type (Desig_Typ) then
817 New_Reference_To (Boolean_Literals
818 (Needs_Finalization (Base_Type (Desig_Typ))), Loc);
820 -- Processing for subtype indications
822 elsif Nkind (Temp) in N_Has_Entity
823 and then Is_Type (Entity (Temp))
826 New_Reference_To (Boolean_Literals
827 (Needs_Finalization (Entity (Temp))), Loc);
829 -- Generate a runtime check to test the controlled state of
830 -- an object for the purposes of allocation / deallocation.
833 -- The following case arises when allocating through an
834 -- interface class-wide type, generate:
838 if Is_RTE (Etype (Temp), RE_Tag_Ptr) then
840 Make_Explicit_Dereference (Loc,
842 Relocate_Node (Temp));
849 Make_Attribute_Reference (Loc,
851 Relocate_Node (Temp),
852 Attribute_Name => Name_Tag);
856 -- Needs_Finalization (<Param>)
859 Make_Function_Call (Loc,
861 New_Reference_To (RTE (RE_Needs_Finalization), Loc),
862 Parameter_Associations => New_List (Param));
865 -- Create the temporary which represents the finalization
866 -- state of the expression. Generate:
868 -- F : constant Boolean := <Flag_Expr>;
871 Make_Object_Declaration (Loc,
872 Defining_Identifier => Flag_Id,
873 Constant_Present => True,
875 New_Reference_To (Standard_Boolean, Loc),
876 Expression => Flag_Expr));
878 -- The flag acts as the last actual
880 Append_To (Actuals, New_Reference_To (Flag_Id, Loc));
883 -- The object is statically known to be controlled
886 Append_To (Actuals, New_Reference_To (Standard_True, Loc));
890 Append_To (Actuals, New_Reference_To (Standard_False, Loc));
897 New_Reference_To (Boolean_Literals (Present (Subpool)), Loc));
900 -- Step 2: Build a wrapper Allocate / Deallocate which internally
901 -- calls Allocate_Any_Controlled / Deallocate_Any_Controlled.
903 -- Select the proper routine to call
906 Proc_To_Call := RTE (RE_Allocate_Any_Controlled);
908 Proc_To_Call := RTE (RE_Deallocate_Any_Controlled);
911 -- Create a custom Allocate / Deallocate routine which has identical
912 -- profile to that of System.Storage_Pools.
915 Make_Subprogram_Body (Loc,
920 Make_Procedure_Specification (Loc,
921 Defining_Unit_Name => Proc_Id,
922 Parameter_Specifications => New_List (
924 -- P : Root_Storage_Pool
926 Make_Parameter_Specification (Loc,
927 Defining_Identifier => Make_Temporary (Loc, 'P'),
929 New_Reference_To (RTE (RE_Root_Storage_Pool), Loc)),
933 Make_Parameter_Specification (Loc,
934 Defining_Identifier => Addr_Id,
935 Out_Present => Is_Allocate,
937 New_Reference_To (RTE (RE_Address), Loc)),
941 Make_Parameter_Specification (Loc,
942 Defining_Identifier => Size_Id,
944 New_Reference_To (RTE (RE_Storage_Count), Loc)),
948 Make_Parameter_Specification (Loc,
949 Defining_Identifier => Alig_Id,
951 New_Reference_To (RTE (RE_Storage_Count), Loc)))),
953 Declarations => No_List,
955 Handled_Statement_Sequence =>
956 Make_Handled_Sequence_Of_Statements (Loc,
957 Statements => New_List (
958 Make_Procedure_Call_Statement (Loc,
959 Name => New_Reference_To (Proc_To_Call, Loc),
960 Parameter_Associations => Actuals)))));
962 -- The newly generated Allocate / Deallocate becomes the default
963 -- procedure to call when the back end processes the allocation /
967 Set_Procedure_To_Call (Expr, Proc_Id);
969 Set_Procedure_To_Call (N, Proc_Id);
972 end Build_Allocate_Deallocate_Proc;
974 ------------------------
975 -- Build_Runtime_Call --
976 ------------------------
978 function Build_Runtime_Call (Loc : Source_Ptr; RE : RE_Id) return Node_Id is
980 -- If entity is not available, we can skip making the call (this avoids
981 -- junk duplicated error messages in a number of cases).
983 if not RTE_Available (RE) then
984 return Make_Null_Statement (Loc);
987 Make_Procedure_Call_Statement (Loc,
988 Name => New_Reference_To (RTE (RE), Loc));
990 end Build_Runtime_Call;
992 ----------------------------
993 -- Build_Task_Array_Image --
994 ----------------------------
996 -- This function generates the body for a function that constructs the
997 -- image string for a task that is an array component. The function is
998 -- local to the init proc for the array type, and is called for each one
999 -- of the components. The constructed image has the form of an indexed
1000 -- component, whose prefix is the outer variable of the array type.
1001 -- The n-dimensional array type has known indexes Index, Index2...
1003 -- Id_Ref is an indexed component form created by the enclosing init proc.
1004 -- Its successive indexes are Val1, Val2, ... which are the loop variables
1005 -- in the loops that call the individual task init proc on each component.
1007 -- The generated function has the following structure:
1009 -- function F return String is
1010 -- Pref : string renames Task_Name;
1011 -- T1 : String := Index1'Image (Val1);
1013 -- Tn : String := indexn'image (Valn);
1014 -- Len : Integer := T1'Length + ... + Tn'Length + n + 1;
1015 -- -- Len includes commas and the end parentheses.
1016 -- Res : String (1..Len);
1017 -- Pos : Integer := Pref'Length;
1020 -- Res (1 .. Pos) := Pref;
1022 -- Res (Pos) := '(';
1024 -- Res (Pos .. Pos + T1'Length - 1) := T1;
1025 -- Pos := Pos + T1'Length;
1026 -- Res (Pos) := '.';
1029 -- Res (Pos .. Pos + Tn'Length - 1) := Tn;
1030 -- Res (Len) := ')';
1035 -- Needless to say, multidimensional arrays of tasks are rare enough that
1036 -- the bulkiness of this code is not really a concern.
1038 function Build_Task_Array_Image
1042 Dyn : Boolean := False) return Node_Id
1044 Dims : constant Nat := Number_Dimensions (A_Type);
1045 -- Number of dimensions for array of tasks
1047 Temps : array (1 .. Dims) of Entity_Id;
1048 -- Array of temporaries to hold string for each index
1054 -- Total length of generated name
1057 -- Running index for substring assignments
1059 Pref : constant Entity_Id := Make_Temporary (Loc, 'P');
1060 -- Name of enclosing variable, prefix of resulting name
1063 -- String to hold result
1066 -- Value of successive indexes
1069 -- Expression to compute total size of string
1072 -- Entity for name at one index position
1074 Decls : constant List_Id := New_List;
1075 Stats : constant List_Id := New_List;
1078 -- For a dynamic task, the name comes from the target variable. For a
1079 -- static one it is a formal of the enclosing init proc.
1082 Get_Name_String (Chars (Entity (Prefix (Id_Ref))));
1084 Make_Object_Declaration (Loc,
1085 Defining_Identifier => Pref,
1086 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
1088 Make_String_Literal (Loc,
1089 Strval => String_From_Name_Buffer)));
1093 Make_Object_Renaming_Declaration (Loc,
1094 Defining_Identifier => Pref,
1095 Subtype_Mark => New_Occurrence_Of (Standard_String, Loc),
1096 Name => Make_Identifier (Loc, Name_uTask_Name)));
1099 Indx := First_Index (A_Type);
1100 Val := First (Expressions (Id_Ref));
1102 for J in 1 .. Dims loop
1103 T := Make_Temporary (Loc, 'T');
1107 Make_Object_Declaration (Loc,
1108 Defining_Identifier => T,
1109 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
1111 Make_Attribute_Reference (Loc,
1112 Attribute_Name => Name_Image,
1113 Prefix => New_Occurrence_Of (Etype (Indx), Loc),
1114 Expressions => New_List (New_Copy_Tree (Val)))));
1120 Sum := Make_Integer_Literal (Loc, Dims + 1);
1126 Make_Attribute_Reference (Loc,
1127 Attribute_Name => Name_Length,
1129 New_Occurrence_Of (Pref, Loc),
1130 Expressions => New_List (Make_Integer_Literal (Loc, 1))));
1132 for J in 1 .. Dims loop
1137 Make_Attribute_Reference (Loc,
1138 Attribute_Name => Name_Length,
1140 New_Occurrence_Of (Temps (J), Loc),
1141 Expressions => New_List (Make_Integer_Literal (Loc, 1))));
1144 Build_Task_Image_Prefix (Loc, Len, Res, Pos, Pref, Sum, Decls, Stats);
1146 Set_Character_Literal_Name (Char_Code (Character'Pos ('(')));
1149 Make_Assignment_Statement (Loc,
1150 Name => Make_Indexed_Component (Loc,
1151 Prefix => New_Occurrence_Of (Res, Loc),
1152 Expressions => New_List (New_Occurrence_Of (Pos, Loc))),
1154 Make_Character_Literal (Loc,
1156 Char_Literal_Value =>
1157 UI_From_Int (Character'Pos ('(')))));
1160 Make_Assignment_Statement (Loc,
1161 Name => New_Occurrence_Of (Pos, Loc),
1164 Left_Opnd => New_Occurrence_Of (Pos, Loc),
1165 Right_Opnd => Make_Integer_Literal (Loc, 1))));
1167 for J in 1 .. Dims loop
1170 Make_Assignment_Statement (Loc,
1171 Name => Make_Slice (Loc,
1172 Prefix => New_Occurrence_Of (Res, Loc),
1175 Low_Bound => New_Occurrence_Of (Pos, Loc),
1176 High_Bound => Make_Op_Subtract (Loc,
1179 Left_Opnd => New_Occurrence_Of (Pos, Loc),
1181 Make_Attribute_Reference (Loc,
1182 Attribute_Name => Name_Length,
1184 New_Occurrence_Of (Temps (J), Loc),
1186 New_List (Make_Integer_Literal (Loc, 1)))),
1187 Right_Opnd => Make_Integer_Literal (Loc, 1)))),
1189 Expression => New_Occurrence_Of (Temps (J), Loc)));
1193 Make_Assignment_Statement (Loc,
1194 Name => New_Occurrence_Of (Pos, Loc),
1197 Left_Opnd => New_Occurrence_Of (Pos, Loc),
1199 Make_Attribute_Reference (Loc,
1200 Attribute_Name => Name_Length,
1201 Prefix => New_Occurrence_Of (Temps (J), Loc),
1203 New_List (Make_Integer_Literal (Loc, 1))))));
1205 Set_Character_Literal_Name (Char_Code (Character'Pos (',')));
1208 Make_Assignment_Statement (Loc,
1209 Name => Make_Indexed_Component (Loc,
1210 Prefix => New_Occurrence_Of (Res, Loc),
1211 Expressions => New_List (New_Occurrence_Of (Pos, Loc))),
1213 Make_Character_Literal (Loc,
1215 Char_Literal_Value =>
1216 UI_From_Int (Character'Pos (',')))));
1219 Make_Assignment_Statement (Loc,
1220 Name => New_Occurrence_Of (Pos, Loc),
1223 Left_Opnd => New_Occurrence_Of (Pos, Loc),
1224 Right_Opnd => Make_Integer_Literal (Loc, 1))));
1228 Set_Character_Literal_Name (Char_Code (Character'Pos (')')));
1231 Make_Assignment_Statement (Loc,
1232 Name => Make_Indexed_Component (Loc,
1233 Prefix => New_Occurrence_Of (Res, Loc),
1234 Expressions => New_List (New_Occurrence_Of (Len, Loc))),
1236 Make_Character_Literal (Loc,
1238 Char_Literal_Value =>
1239 UI_From_Int (Character'Pos (')')))));
1240 return Build_Task_Image_Function (Loc, Decls, Stats, Res);
1241 end Build_Task_Array_Image;
1243 ----------------------------
1244 -- Build_Task_Image_Decls --
1245 ----------------------------
1247 function Build_Task_Image_Decls
1251 In_Init_Proc : Boolean := False) return List_Id
1253 Decls : constant List_Id := New_List;
1254 T_Id : Entity_Id := Empty;
1256 Expr : Node_Id := Empty;
1257 Fun : Node_Id := Empty;
1258 Is_Dyn : constant Boolean :=
1259 Nkind (Parent (Id_Ref)) = N_Assignment_Statement
1261 Nkind (Expression (Parent (Id_Ref))) = N_Allocator;
1264 -- If Discard_Names or No_Implicit_Heap_Allocations are in effect,
1265 -- generate a dummy declaration only.
1267 if Restriction_Active (No_Implicit_Heap_Allocations)
1268 or else Global_Discard_Names
1270 T_Id := Make_Temporary (Loc, 'J');
1275 Make_Object_Declaration (Loc,
1276 Defining_Identifier => T_Id,
1277 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
1279 Make_String_Literal (Loc,
1280 Strval => String_From_Name_Buffer)));
1283 if Nkind (Id_Ref) = N_Identifier
1284 or else Nkind (Id_Ref) = N_Defining_Identifier
1286 -- For a simple variable, the image of the task is built from
1287 -- the name of the variable. To avoid possible conflict with the
1288 -- anonymous type created for a single protected object, add a
1292 Make_Defining_Identifier (Loc,
1293 New_External_Name (Chars (Id_Ref), 'T', 1));
1295 Get_Name_String (Chars (Id_Ref));
1298 Make_String_Literal (Loc,
1299 Strval => String_From_Name_Buffer);
1301 elsif Nkind (Id_Ref) = N_Selected_Component then
1303 Make_Defining_Identifier (Loc,
1304 New_External_Name (Chars (Selector_Name (Id_Ref)), 'T'));
1305 Fun := Build_Task_Record_Image (Loc, Id_Ref, Is_Dyn);
1307 elsif Nkind (Id_Ref) = N_Indexed_Component then
1309 Make_Defining_Identifier (Loc,
1310 New_External_Name (Chars (A_Type), 'N'));
1312 Fun := Build_Task_Array_Image (Loc, Id_Ref, A_Type, Is_Dyn);
1316 if Present (Fun) then
1317 Append (Fun, Decls);
1318 Expr := Make_Function_Call (Loc,
1319 Name => New_Occurrence_Of (Defining_Entity (Fun), Loc));
1321 if not In_Init_Proc and then VM_Target = No_VM then
1322 Set_Uses_Sec_Stack (Defining_Entity (Fun));
1326 Decl := Make_Object_Declaration (Loc,
1327 Defining_Identifier => T_Id,
1328 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
1329 Constant_Present => True,
1330 Expression => Expr);
1332 Append (Decl, Decls);
1334 end Build_Task_Image_Decls;
1336 -------------------------------
1337 -- Build_Task_Image_Function --
1338 -------------------------------
1340 function Build_Task_Image_Function
1344 Res : Entity_Id) return Node_Id
1350 Make_Simple_Return_Statement (Loc,
1351 Expression => New_Occurrence_Of (Res, Loc)));
1353 Spec := Make_Function_Specification (Loc,
1354 Defining_Unit_Name => Make_Temporary (Loc, 'F'),
1355 Result_Definition => New_Occurrence_Of (Standard_String, Loc));
1357 -- Calls to 'Image use the secondary stack, which must be cleaned up
1358 -- after the task name is built.
1360 return Make_Subprogram_Body (Loc,
1361 Specification => Spec,
1362 Declarations => Decls,
1363 Handled_Statement_Sequence =>
1364 Make_Handled_Sequence_Of_Statements (Loc, Statements => Stats));
1365 end Build_Task_Image_Function;
1367 -----------------------------
1368 -- Build_Task_Image_Prefix --
1369 -----------------------------
1371 procedure Build_Task_Image_Prefix
1373 Len : out Entity_Id;
1374 Res : out Entity_Id;
1375 Pos : out Entity_Id;
1382 Len := Make_Temporary (Loc, 'L', Sum);
1385 Make_Object_Declaration (Loc,
1386 Defining_Identifier => Len,
1387 Object_Definition => New_Occurrence_Of (Standard_Integer, Loc),
1388 Expression => Sum));
1390 Res := Make_Temporary (Loc, 'R');
1393 Make_Object_Declaration (Loc,
1394 Defining_Identifier => Res,
1395 Object_Definition =>
1396 Make_Subtype_Indication (Loc,
1397 Subtype_Mark => New_Occurrence_Of (Standard_String, Loc),
1399 Make_Index_Or_Discriminant_Constraint (Loc,
1403 Low_Bound => Make_Integer_Literal (Loc, 1),
1404 High_Bound => New_Occurrence_Of (Len, Loc)))))));
1406 Pos := Make_Temporary (Loc, 'P');
1409 Make_Object_Declaration (Loc,
1410 Defining_Identifier => Pos,
1411 Object_Definition => New_Occurrence_Of (Standard_Integer, Loc)));
1413 -- Pos := Prefix'Length;
1416 Make_Assignment_Statement (Loc,
1417 Name => New_Occurrence_Of (Pos, Loc),
1419 Make_Attribute_Reference (Loc,
1420 Attribute_Name => Name_Length,
1421 Prefix => New_Occurrence_Of (Prefix, Loc),
1422 Expressions => New_List (Make_Integer_Literal (Loc, 1)))));
1424 -- Res (1 .. Pos) := Prefix;
1427 Make_Assignment_Statement (Loc,
1430 Prefix => New_Occurrence_Of (Res, Loc),
1433 Low_Bound => Make_Integer_Literal (Loc, 1),
1434 High_Bound => New_Occurrence_Of (Pos, Loc))),
1436 Expression => New_Occurrence_Of (Prefix, Loc)));
1439 Make_Assignment_Statement (Loc,
1440 Name => New_Occurrence_Of (Pos, Loc),
1443 Left_Opnd => New_Occurrence_Of (Pos, Loc),
1444 Right_Opnd => Make_Integer_Literal (Loc, 1))));
1445 end Build_Task_Image_Prefix;
1447 -----------------------------
1448 -- Build_Task_Record_Image --
1449 -----------------------------
1451 function Build_Task_Record_Image
1454 Dyn : Boolean := False) return Node_Id
1457 -- Total length of generated name
1460 -- Index into result
1463 -- String to hold result
1465 Pref : constant Entity_Id := Make_Temporary (Loc, 'P');
1466 -- Name of enclosing variable, prefix of resulting name
1469 -- Expression to compute total size of string
1472 -- Entity for selector name
1474 Decls : constant List_Id := New_List;
1475 Stats : constant List_Id := New_List;
1478 -- For a dynamic task, the name comes from the target variable. For a
1479 -- static one it is a formal of the enclosing init proc.
1482 Get_Name_String (Chars (Entity (Prefix (Id_Ref))));
1484 Make_Object_Declaration (Loc,
1485 Defining_Identifier => Pref,
1486 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
1488 Make_String_Literal (Loc,
1489 Strval => String_From_Name_Buffer)));
1493 Make_Object_Renaming_Declaration (Loc,
1494 Defining_Identifier => Pref,
1495 Subtype_Mark => New_Occurrence_Of (Standard_String, Loc),
1496 Name => Make_Identifier (Loc, Name_uTask_Name)));
1499 Sel := Make_Temporary (Loc, 'S');
1501 Get_Name_String (Chars (Selector_Name (Id_Ref)));
1504 Make_Object_Declaration (Loc,
1505 Defining_Identifier => Sel,
1506 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
1508 Make_String_Literal (Loc,
1509 Strval => String_From_Name_Buffer)));
1511 Sum := Make_Integer_Literal (Loc, Nat (Name_Len + 1));
1517 Make_Attribute_Reference (Loc,
1518 Attribute_Name => Name_Length,
1520 New_Occurrence_Of (Pref, Loc),
1521 Expressions => New_List (Make_Integer_Literal (Loc, 1))));
1523 Build_Task_Image_Prefix (Loc, Len, Res, Pos, Pref, Sum, Decls, Stats);
1525 Set_Character_Literal_Name (Char_Code (Character'Pos ('.')));
1527 -- Res (Pos) := '.';
1530 Make_Assignment_Statement (Loc,
1531 Name => Make_Indexed_Component (Loc,
1532 Prefix => New_Occurrence_Of (Res, Loc),
1533 Expressions => New_List (New_Occurrence_Of (Pos, Loc))),
1535 Make_Character_Literal (Loc,
1537 Char_Literal_Value =>
1538 UI_From_Int (Character'Pos ('.')))));
1541 Make_Assignment_Statement (Loc,
1542 Name => New_Occurrence_Of (Pos, Loc),
1545 Left_Opnd => New_Occurrence_Of (Pos, Loc),
1546 Right_Opnd => Make_Integer_Literal (Loc, 1))));
1548 -- Res (Pos .. Len) := Selector;
1551 Make_Assignment_Statement (Loc,
1552 Name => Make_Slice (Loc,
1553 Prefix => New_Occurrence_Of (Res, Loc),
1556 Low_Bound => New_Occurrence_Of (Pos, Loc),
1557 High_Bound => New_Occurrence_Of (Len, Loc))),
1558 Expression => New_Occurrence_Of (Sel, Loc)));
1560 return Build_Task_Image_Function (Loc, Decls, Stats, Res);
1561 end Build_Task_Record_Image;
1563 ----------------------------------
1564 -- Component_May_Be_Bit_Aligned --
1565 ----------------------------------
1567 function Component_May_Be_Bit_Aligned (Comp : Entity_Id) return Boolean is
1571 -- If no component clause, then everything is fine, since the back end
1572 -- never bit-misaligns by default, even if there is a pragma Packed for
1575 if No (Comp) or else No (Component_Clause (Comp)) then
1579 UT := Underlying_Type (Etype (Comp));
1581 -- It is only array and record types that cause trouble
1583 if not Is_Record_Type (UT)
1584 and then not Is_Array_Type (UT)
1588 -- If we know that we have a small (64 bits or less) record or small
1589 -- bit-packed array, then everything is fine, since the back end can
1590 -- handle these cases correctly.
1592 elsif Esize (Comp) <= 64
1593 and then (Is_Record_Type (UT)
1594 or else Is_Bit_Packed_Array (UT))
1598 -- Otherwise if the component is not byte aligned, we know we have the
1599 -- nasty unaligned case.
1601 elsif Normalized_First_Bit (Comp) /= Uint_0
1602 or else Esize (Comp) mod System_Storage_Unit /= Uint_0
1606 -- If we are large and byte aligned, then OK at this level
1611 end Component_May_Be_Bit_Aligned;
1613 -----------------------------------
1614 -- Corresponding_Runtime_Package --
1615 -----------------------------------
1617 function Corresponding_Runtime_Package (Typ : Entity_Id) return RTU_Id is
1618 Pkg_Id : RTU_Id := RTU_Null;
1621 pragma Assert (Is_Concurrent_Type (Typ));
1623 if Ekind (Typ) in Protected_Kind then
1624 if Has_Entries (Typ)
1626 -- A protected type without entries that covers an interface and
1627 -- overrides the abstract routines with protected procedures is
1628 -- considered equivalent to a protected type with entries in the
1629 -- context of dispatching select statements. It is sufficient to
1630 -- check for the presence of an interface list in the declaration
1631 -- node to recognize this case.
1633 or else Present (Interface_List (Parent (Typ)))
1635 (((Has_Attach_Handler (Typ) and then not Restricted_Profile)
1636 or else Has_Interrupt_Handler (Typ))
1637 and then not Restriction_Active (No_Dynamic_Attachment))
1640 or else Restriction_Active (No_Entry_Queue) = False
1641 or else Number_Entries (Typ) > 1
1642 or else (Has_Attach_Handler (Typ)
1643 and then not Restricted_Profile)
1645 Pkg_Id := System_Tasking_Protected_Objects_Entries;
1647 Pkg_Id := System_Tasking_Protected_Objects_Single_Entry;
1651 Pkg_Id := System_Tasking_Protected_Objects;
1656 end Corresponding_Runtime_Package;
1658 -------------------------------
1659 -- Convert_To_Actual_Subtype --
1660 -------------------------------
1662 procedure Convert_To_Actual_Subtype (Exp : Entity_Id) is
1666 Act_ST := Get_Actual_Subtype (Exp);
1668 if Act_ST = Etype (Exp) then
1671 Rewrite (Exp, Convert_To (Act_ST, Relocate_Node (Exp)));
1672 Analyze_And_Resolve (Exp, Act_ST);
1674 end Convert_To_Actual_Subtype;
1676 -----------------------------------
1677 -- Current_Sem_Unit_Declarations --
1678 -----------------------------------
1680 function Current_Sem_Unit_Declarations return List_Id is
1681 U : Node_Id := Unit (Cunit (Current_Sem_Unit));
1685 -- If the current unit is a package body, locate the visible
1686 -- declarations of the package spec.
1688 if Nkind (U) = N_Package_Body then
1689 U := Unit (Library_Unit (Cunit (Current_Sem_Unit)));
1692 if Nkind (U) = N_Package_Declaration then
1693 U := Specification (U);
1694 Decls := Visible_Declarations (U);
1698 Set_Visible_Declarations (U, Decls);
1702 Decls := Declarations (U);
1706 Set_Declarations (U, Decls);
1711 end Current_Sem_Unit_Declarations;
1713 -----------------------
1714 -- Duplicate_Subexpr --
1715 -----------------------
1717 function Duplicate_Subexpr
1719 Name_Req : Boolean := False) return Node_Id
1722 Remove_Side_Effects (Exp, Name_Req);
1723 return New_Copy_Tree (Exp);
1724 end Duplicate_Subexpr;
1726 ---------------------------------
1727 -- Duplicate_Subexpr_No_Checks --
1728 ---------------------------------
1730 function Duplicate_Subexpr_No_Checks
1732 Name_Req : Boolean := False) return Node_Id
1737 Remove_Side_Effects (Exp, Name_Req);
1738 New_Exp := New_Copy_Tree (Exp);
1739 Remove_Checks (New_Exp);
1741 end Duplicate_Subexpr_No_Checks;
1743 -----------------------------------
1744 -- Duplicate_Subexpr_Move_Checks --
1745 -----------------------------------
1747 function Duplicate_Subexpr_Move_Checks
1749 Name_Req : Boolean := False) return Node_Id
1753 Remove_Side_Effects (Exp, Name_Req);
1754 New_Exp := New_Copy_Tree (Exp);
1755 Remove_Checks (Exp);
1757 end Duplicate_Subexpr_Move_Checks;
1759 --------------------
1760 -- Ensure_Defined --
1761 --------------------
1763 procedure Ensure_Defined (Typ : Entity_Id; N : Node_Id) is
1767 -- An itype reference must only be created if this is a local itype, so
1768 -- that gigi can elaborate it on the proper objstack.
1771 and then Scope (Typ) = Current_Scope
1773 IR := Make_Itype_Reference (Sloc (N));
1774 Set_Itype (IR, Typ);
1775 Insert_Action (N, IR);
1779 --------------------
1780 -- Entry_Names_OK --
1781 --------------------
1783 function Entry_Names_OK return Boolean is
1786 not Restricted_Profile
1787 and then not Global_Discard_Names
1788 and then not Restriction_Active (No_Implicit_Heap_Allocations)
1789 and then not Restriction_Active (No_Local_Allocators);
1796 procedure Evaluate_Name (Nam : Node_Id) is
1797 K : constant Node_Kind := Nkind (Nam);
1800 -- For an explicit dereference, we simply force the evaluation of the
1801 -- name expression. The dereference provides a value that is the address
1802 -- for the renamed object, and it is precisely this value that we want
1805 if K = N_Explicit_Dereference then
1806 Force_Evaluation (Prefix (Nam));
1808 -- For a selected component, we simply evaluate the prefix
1810 elsif K = N_Selected_Component then
1811 Evaluate_Name (Prefix (Nam));
1813 -- For an indexed component, or an attribute reference, we evaluate the
1814 -- prefix, which is itself a name, recursively, and then force the
1815 -- evaluation of all the subscripts (or attribute expressions).
1817 elsif Nkind_In (K, N_Indexed_Component, N_Attribute_Reference) then
1818 Evaluate_Name (Prefix (Nam));
1824 E := First (Expressions (Nam));
1825 while Present (E) loop
1826 Force_Evaluation (E);
1828 if Original_Node (E) /= E then
1829 Set_Do_Range_Check (E, Do_Range_Check (Original_Node (E)));
1836 -- For a slice, we evaluate the prefix, as for the indexed component
1837 -- case and then, if there is a range present, either directly or as the
1838 -- constraint of a discrete subtype indication, we evaluate the two
1839 -- bounds of this range.
1841 elsif K = N_Slice then
1842 Evaluate_Name (Prefix (Nam));
1845 DR : constant Node_Id := Discrete_Range (Nam);
1850 if Nkind (DR) = N_Range then
1851 Force_Evaluation (Low_Bound (DR));
1852 Force_Evaluation (High_Bound (DR));
1854 elsif Nkind (DR) = N_Subtype_Indication then
1855 Constr := Constraint (DR);
1857 if Nkind (Constr) = N_Range_Constraint then
1858 Rexpr := Range_Expression (Constr);
1860 Force_Evaluation (Low_Bound (Rexpr));
1861 Force_Evaluation (High_Bound (Rexpr));
1866 -- For a type conversion, the expression of the conversion must be the
1867 -- name of an object, and we simply need to evaluate this name.
1869 elsif K = N_Type_Conversion then
1870 Evaluate_Name (Expression (Nam));
1872 -- For a function call, we evaluate the call
1874 elsif K = N_Function_Call then
1875 Force_Evaluation (Nam);
1877 -- The remaining cases are direct name, operator symbol and character
1878 -- literal. In all these cases, we do nothing, since we want to
1879 -- reevaluate each time the renamed object is used.
1886 ---------------------
1887 -- Evolve_And_Then --
1888 ---------------------
1890 procedure Evolve_And_Then (Cond : in out Node_Id; Cond1 : Node_Id) is
1896 Make_And_Then (Sloc (Cond1),
1898 Right_Opnd => Cond1);
1900 end Evolve_And_Then;
1902 --------------------
1903 -- Evolve_Or_Else --
1904 --------------------
1906 procedure Evolve_Or_Else (Cond : in out Node_Id; Cond1 : Node_Id) is
1912 Make_Or_Else (Sloc (Cond1),
1914 Right_Opnd => Cond1);
1918 ------------------------------
1919 -- Expand_Subtype_From_Expr --
1920 ------------------------------
1922 -- This function is applicable for both static and dynamic allocation of
1923 -- objects which are constrained by an initial expression. Basically it
1924 -- transforms an unconstrained subtype indication into a constrained one.
1926 -- The expression may also be transformed in certain cases in order to
1927 -- avoid multiple evaluation. In the static allocation case, the general
1932 -- is transformed into
1934 -- Val : Constrained_Subtype_of_T := Maybe_Modified_Expr;
1936 -- Here are the main cases :
1938 -- <if Expr is a Slice>
1939 -- Val : T ([Index_Subtype (Expr)]) := Expr;
1941 -- <elsif Expr is a String Literal>
1942 -- Val : T (T'First .. T'First + Length (string literal) - 1) := Expr;
1944 -- <elsif Expr is Constrained>
1945 -- subtype T is Type_Of_Expr
1948 -- <elsif Expr is an entity_name>
1949 -- Val : T (constraints taken from Expr) := Expr;
1952 -- type Axxx is access all T;
1953 -- Rval : Axxx := Expr'ref;
1954 -- Val : T (constraints taken from Rval) := Rval.all;
1956 -- ??? note: when the Expression is allocated in the secondary stack
1957 -- we could use it directly instead of copying it by declaring
1958 -- Val : T (...) renames Rval.all
1960 procedure Expand_Subtype_From_Expr
1962 Unc_Type : Entity_Id;
1963 Subtype_Indic : Node_Id;
1966 Loc : constant Source_Ptr := Sloc (N);
1967 Exp_Typ : constant Entity_Id := Etype (Exp);
1971 -- In general we cannot build the subtype if expansion is disabled,
1972 -- because internal entities may not have been defined. However, to
1973 -- avoid some cascaded errors, we try to continue when the expression is
1974 -- an array (or string), because it is safe to compute the bounds. It is
1975 -- in fact required to do so even in a generic context, because there
1976 -- may be constants that depend on the bounds of a string literal, both
1977 -- standard string types and more generally arrays of characters.
1979 if not Expander_Active
1980 and then (No (Etype (Exp))
1981 or else not Is_String_Type (Etype (Exp)))
1986 if Nkind (Exp) = N_Slice then
1988 Slice_Type : constant Entity_Id := Etype (First_Index (Exp_Typ));
1991 Rewrite (Subtype_Indic,
1992 Make_Subtype_Indication (Loc,
1993 Subtype_Mark => New_Reference_To (Unc_Type, Loc),
1995 Make_Index_Or_Discriminant_Constraint (Loc,
1996 Constraints => New_List
1997 (New_Reference_To (Slice_Type, Loc)))));
1999 -- This subtype indication may be used later for constraint checks
2000 -- we better make sure that if a variable was used as a bound of
2001 -- of the original slice, its value is frozen.
2003 Force_Evaluation (Low_Bound (Scalar_Range (Slice_Type)));
2004 Force_Evaluation (High_Bound (Scalar_Range (Slice_Type)));
2007 elsif Ekind (Exp_Typ) = E_String_Literal_Subtype then
2008 Rewrite (Subtype_Indic,
2009 Make_Subtype_Indication (Loc,
2010 Subtype_Mark => New_Reference_To (Unc_Type, Loc),
2012 Make_Index_Or_Discriminant_Constraint (Loc,
2013 Constraints => New_List (
2014 Make_Literal_Range (Loc,
2015 Literal_Typ => Exp_Typ)))));
2017 elsif Is_Constrained (Exp_Typ)
2018 and then not Is_Class_Wide_Type (Unc_Type)
2020 if Is_Itype (Exp_Typ) then
2022 -- Within an initialization procedure, a selected component
2023 -- denotes a component of the enclosing record, and it appears as
2024 -- an actual in a call to its own initialization procedure. If
2025 -- this component depends on the outer discriminant, we must
2026 -- generate the proper actual subtype for it.
2028 if Nkind (Exp) = N_Selected_Component
2029 and then Within_Init_Proc
2032 Decl : constant Node_Id :=
2033 Build_Actual_Subtype_Of_Component (Exp_Typ, Exp);
2035 if Present (Decl) then
2036 Insert_Action (N, Decl);
2037 T := Defining_Identifier (Decl);
2043 -- No need to generate a new one (new what???)
2050 T := Make_Temporary (Loc, 'T');
2053 Make_Subtype_Declaration (Loc,
2054 Defining_Identifier => T,
2055 Subtype_Indication => New_Reference_To (Exp_Typ, Loc)));
2057 -- This type is marked as an itype even though it has an explicit
2058 -- declaration since otherwise Is_Generic_Actual_Type can get
2059 -- set, resulting in the generation of spurious errors. (See
2060 -- sem_ch8.Analyze_Package_Renaming and sem_type.covers)
2063 Set_Associated_Node_For_Itype (T, Exp);
2066 Rewrite (Subtype_Indic, New_Reference_To (T, Loc));
2068 -- Nothing needs to be done for private types with unknown discriminants
2069 -- if the underlying type is not an unconstrained composite type or it
2070 -- is an unchecked union.
2072 elsif Is_Private_Type (Unc_Type)
2073 and then Has_Unknown_Discriminants (Unc_Type)
2074 and then (not Is_Composite_Type (Underlying_Type (Unc_Type))
2075 or else Is_Constrained (Underlying_Type (Unc_Type))
2076 or else Is_Unchecked_Union (Underlying_Type (Unc_Type)))
2080 -- Case of derived type with unknown discriminants where the parent type
2081 -- also has unknown discriminants.
2083 elsif Is_Record_Type (Unc_Type)
2084 and then not Is_Class_Wide_Type (Unc_Type)
2085 and then Has_Unknown_Discriminants (Unc_Type)
2086 and then Has_Unknown_Discriminants (Underlying_Type (Unc_Type))
2088 -- Nothing to be done if no underlying record view available
2090 if No (Underlying_Record_View (Unc_Type)) then
2093 -- Otherwise use the Underlying_Record_View to create the proper
2094 -- constrained subtype for an object of a derived type with unknown
2098 Remove_Side_Effects (Exp);
2099 Rewrite (Subtype_Indic,
2100 Make_Subtype_From_Expr (Exp, Underlying_Record_View (Unc_Type)));
2103 -- Renamings of class-wide interface types require no equivalent
2104 -- constrained type declarations because we only need to reference
2105 -- the tag component associated with the interface. The same is
2106 -- presumably true for class-wide types in general, so this test
2107 -- is broadened to include all class-wide renamings, which also
2108 -- avoids cases of unbounded recursion in Remove_Side_Effects.
2109 -- (Is this really correct, or are there some cases of class-wide
2110 -- renamings that require action in this procedure???)
2113 and then Nkind (N) = N_Object_Renaming_Declaration
2114 and then Is_Class_Wide_Type (Unc_Type)
2118 -- In Ada 95 nothing to be done if the type of the expression is limited
2119 -- because in this case the expression cannot be copied, and its use can
2120 -- only be by reference.
2122 -- In Ada 2005 the context can be an object declaration whose expression
2123 -- is a function that returns in place. If the nominal subtype has
2124 -- unknown discriminants, the call still provides constraints on the
2125 -- object, and we have to create an actual subtype from it.
2127 -- If the type is class-wide, the expression is dynamically tagged and
2128 -- we do not create an actual subtype either. Ditto for an interface.
2129 -- For now this applies only if the type is immutably limited, and the
2130 -- function being called is build-in-place. This will have to be revised
2131 -- when build-in-place functions are generalized to other types.
2133 elsif Is_Immutably_Limited_Type (Exp_Typ)
2135 (Is_Class_Wide_Type (Exp_Typ)
2136 or else Is_Interface (Exp_Typ)
2137 or else not Has_Unknown_Discriminants (Exp_Typ)
2138 or else not Is_Composite_Type (Unc_Type))
2142 -- For limited objects initialized with build in place function calls,
2143 -- nothing to be done; otherwise we prematurely introduce an N_Reference
2144 -- node in the expression initializing the object, which breaks the
2145 -- circuitry that detects and adds the additional arguments to the
2148 elsif Is_Build_In_Place_Function_Call (Exp) then
2152 Remove_Side_Effects (Exp);
2153 Rewrite (Subtype_Indic,
2154 Make_Subtype_From_Expr (Exp, Unc_Type));
2156 end Expand_Subtype_From_Expr;
2158 --------------------
2159 -- Find_Init_Call --
2160 --------------------
2162 function Find_Init_Call
2164 Rep_Clause : Node_Id) return Node_Id
2166 Typ : constant Entity_Id := Etype (Var);
2168 Init_Proc : Entity_Id;
2169 -- Initialization procedure for Typ
2171 function Find_Init_Call_In_List (From : Node_Id) return Node_Id;
2172 -- Look for init call for Var starting at From and scanning the
2173 -- enclosing list until Rep_Clause or the end of the list is reached.
2175 ----------------------------
2176 -- Find_Init_Call_In_List --
2177 ----------------------------
2179 function Find_Init_Call_In_List (From : Node_Id) return Node_Id is
2180 Init_Call : Node_Id;
2184 while Present (Init_Call) and then Init_Call /= Rep_Clause loop
2185 if Nkind (Init_Call) = N_Procedure_Call_Statement
2186 and then Is_Entity_Name (Name (Init_Call))
2187 and then Entity (Name (Init_Call)) = Init_Proc
2196 end Find_Init_Call_In_List;
2198 Init_Call : Node_Id;
2200 -- Start of processing for Find_Init_Call
2203 if not Has_Non_Null_Base_Init_Proc (Typ) then
2204 -- No init proc for the type, so obviously no call to be found
2209 Init_Proc := Base_Init_Proc (Typ);
2211 -- First scan the list containing the declaration of Var
2213 Init_Call := Find_Init_Call_In_List (From => Next (Parent (Var)));
2215 -- If not found, also look on Var's freeze actions list, if any, since
2216 -- the init call may have been moved there (case of an address clause
2217 -- applying to Var).
2219 if No (Init_Call) and then Present (Freeze_Node (Var)) then
2221 Find_Init_Call_In_List (First (Actions (Freeze_Node (Var))));
2227 ------------------------
2228 -- Find_Interface_ADT --
2229 ------------------------
2231 function Find_Interface_ADT
2233 Iface : Entity_Id) return Elmt_Id
2236 Typ : Entity_Id := T;
2239 pragma Assert (Is_Interface (Iface));
2241 -- Handle private types
2243 if Has_Private_Declaration (Typ)
2244 and then Present (Full_View (Typ))
2246 Typ := Full_View (Typ);
2249 -- Handle access types
2251 if Is_Access_Type (Typ) then
2252 Typ := Designated_Type (Typ);
2255 -- Handle task and protected types implementing interfaces
2257 if Is_Concurrent_Type (Typ) then
2258 Typ := Corresponding_Record_Type (Typ);
2262 (not Is_Class_Wide_Type (Typ)
2263 and then Ekind (Typ) /= E_Incomplete_Type);
2265 if Is_Ancestor (Iface, Typ, Use_Full_View => True) then
2266 return First_Elmt (Access_Disp_Table (Typ));
2270 Next_Elmt (Next_Elmt (First_Elmt (Access_Disp_Table (Typ))));
2272 and then Present (Related_Type (Node (ADT)))
2273 and then Related_Type (Node (ADT)) /= Iface
2274 and then not Is_Ancestor (Iface, Related_Type (Node (ADT)),
2275 Use_Full_View => True)
2280 pragma Assert (Present (Related_Type (Node (ADT))));
2283 end Find_Interface_ADT;
2285 ------------------------
2286 -- Find_Interface_Tag --
2287 ------------------------
2289 function Find_Interface_Tag
2291 Iface : Entity_Id) return Entity_Id
2294 Found : Boolean := False;
2295 Typ : Entity_Id := T;
2297 procedure Find_Tag (Typ : Entity_Id);
2298 -- Internal subprogram used to recursively climb to the ancestors
2304 procedure Find_Tag (Typ : Entity_Id) is
2309 -- This routine does not handle the case in which the interface is an
2310 -- ancestor of Typ. That case is handled by the enclosing subprogram.
2312 pragma Assert (Typ /= Iface);
2314 -- Climb to the root type handling private types
2316 if Present (Full_View (Etype (Typ))) then
2317 if Full_View (Etype (Typ)) /= Typ then
2318 Find_Tag (Full_View (Etype (Typ)));
2321 elsif Etype (Typ) /= Typ then
2322 Find_Tag (Etype (Typ));
2325 -- Traverse the list of interfaces implemented by the type
2328 and then Present (Interfaces (Typ))
2329 and then not (Is_Empty_Elmt_List (Interfaces (Typ)))
2331 -- Skip the tag associated with the primary table
2333 pragma Assert (Etype (First_Tag_Component (Typ)) = RTE (RE_Tag));
2334 AI_Tag := Next_Tag_Component (First_Tag_Component (Typ));
2335 pragma Assert (Present (AI_Tag));
2337 AI_Elmt := First_Elmt (Interfaces (Typ));
2338 while Present (AI_Elmt) loop
2339 AI := Node (AI_Elmt);
2342 or else Is_Ancestor (Iface, AI, Use_Full_View => True)
2348 AI_Tag := Next_Tag_Component (AI_Tag);
2349 Next_Elmt (AI_Elmt);
2354 -- Start of processing for Find_Interface_Tag
2357 pragma Assert (Is_Interface (Iface));
2359 -- Handle access types
2361 if Is_Access_Type (Typ) then
2362 Typ := Designated_Type (Typ);
2365 -- Handle class-wide types
2367 if Is_Class_Wide_Type (Typ) then
2368 Typ := Root_Type (Typ);
2371 -- Handle private types
2373 if Has_Private_Declaration (Typ)
2374 and then Present (Full_View (Typ))
2376 Typ := Full_View (Typ);
2379 -- Handle entities from the limited view
2381 if Ekind (Typ) = E_Incomplete_Type then
2382 pragma Assert (Present (Non_Limited_View (Typ)));
2383 Typ := Non_Limited_View (Typ);
2386 -- Handle task and protected types implementing interfaces
2388 if Is_Concurrent_Type (Typ) then
2389 Typ := Corresponding_Record_Type (Typ);
2392 -- If the interface is an ancestor of the type, then it shared the
2393 -- primary dispatch table.
2395 if Is_Ancestor (Iface, Typ, Use_Full_View => True) then
2396 pragma Assert (Etype (First_Tag_Component (Typ)) = RTE (RE_Tag));
2397 return First_Tag_Component (Typ);
2399 -- Otherwise we need to search for its associated tag component
2403 pragma Assert (Found);
2406 end Find_Interface_Tag;
2412 function Find_Prim_Op (T : Entity_Id; Name : Name_Id) return Entity_Id is
2414 Typ : Entity_Id := T;
2418 if Is_Class_Wide_Type (Typ) then
2419 Typ := Root_Type (Typ);
2422 Typ := Underlying_Type (Typ);
2424 -- Loop through primitive operations
2426 Prim := First_Elmt (Primitive_Operations (Typ));
2427 while Present (Prim) loop
2430 -- We can retrieve primitive operations by name if it is an internal
2431 -- name. For equality we must check that both of its operands have
2432 -- the same type, to avoid confusion with user-defined equalities
2433 -- than may have a non-symmetric signature.
2435 exit when Chars (Op) = Name
2438 or else Etype (First_Formal (Op)) = Etype (Last_Formal (Op)));
2442 -- Raise Program_Error if no primitive found
2445 raise Program_Error;
2456 function Find_Prim_Op
2458 Name : TSS_Name_Type) return Entity_Id
2460 Inher_Op : Entity_Id := Empty;
2461 Own_Op : Entity_Id := Empty;
2462 Prim_Elmt : Elmt_Id;
2463 Prim_Id : Entity_Id;
2464 Typ : Entity_Id := T;
2467 if Is_Class_Wide_Type (Typ) then
2468 Typ := Root_Type (Typ);
2471 Typ := Underlying_Type (Typ);
2473 -- This search is based on the assertion that the dispatching version
2474 -- of the TSS routine always precedes the real primitive.
2476 Prim_Elmt := First_Elmt (Primitive_Operations (Typ));
2477 while Present (Prim_Elmt) loop
2478 Prim_Id := Node (Prim_Elmt);
2480 if Is_TSS (Prim_Id, Name) then
2481 if Present (Alias (Prim_Id)) then
2482 Inher_Op := Prim_Id;
2488 Next_Elmt (Prim_Elmt);
2491 if Present (Own_Op) then
2493 elsif Present (Inher_Op) then
2496 raise Program_Error;
2500 ----------------------------
2501 -- Find_Protection_Object --
2502 ----------------------------
2504 function Find_Protection_Object (Scop : Entity_Id) return Entity_Id is
2509 while Present (S) loop
2510 if (Ekind (S) = E_Entry
2511 or else Ekind (S) = E_Entry_Family
2512 or else Ekind (S) = E_Function
2513 or else Ekind (S) = E_Procedure)
2514 and then Present (Protection_Object (S))
2516 return Protection_Object (S);
2522 -- If we do not find a Protection object in the scope chain, then
2523 -- something has gone wrong, most likely the object was never created.
2525 raise Program_Error;
2526 end Find_Protection_Object;
2528 --------------------------
2529 -- Find_Protection_Type --
2530 --------------------------
2532 function Find_Protection_Type (Conc_Typ : Entity_Id) return Entity_Id is
2534 Typ : Entity_Id := Conc_Typ;
2537 if Is_Concurrent_Type (Typ) then
2538 Typ := Corresponding_Record_Type (Typ);
2541 -- Since restriction violations are not considered serious errors, the
2542 -- expander remains active, but may leave the corresponding record type
2543 -- malformed. In such cases, component _object is not available so do
2546 if not Analyzed (Typ) then
2550 Comp := First_Component (Typ);
2551 while Present (Comp) loop
2552 if Chars (Comp) = Name_uObject then
2553 return Base_Type (Etype (Comp));
2556 Next_Component (Comp);
2559 -- The corresponding record of a protected type should always have an
2562 raise Program_Error;
2563 end Find_Protection_Type;
2565 ----------------------
2566 -- Force_Evaluation --
2567 ----------------------
2569 procedure Force_Evaluation (Exp : Node_Id; Name_Req : Boolean := False) is
2571 Remove_Side_Effects (Exp, Name_Req, Variable_Ref => True);
2572 end Force_Evaluation;
2574 ---------------------------------
2575 -- Fully_Qualified_Name_String --
2576 ---------------------------------
2578 function Fully_Qualified_Name_String (E : Entity_Id) return String_Id is
2579 procedure Internal_Full_Qualified_Name (E : Entity_Id);
2580 -- Compute recursively the qualified name without NUL at the end, adding
2581 -- it to the currently started string being generated
2583 ----------------------------------
2584 -- Internal_Full_Qualified_Name --
2585 ----------------------------------
2587 procedure Internal_Full_Qualified_Name (E : Entity_Id) is
2591 -- Deal properly with child units
2593 if Nkind (E) = N_Defining_Program_Unit_Name then
2594 Ent := Defining_Identifier (E);
2599 -- Compute qualification recursively (only "Standard" has no scope)
2601 if Present (Scope (Scope (Ent))) then
2602 Internal_Full_Qualified_Name (Scope (Ent));
2603 Store_String_Char (Get_Char_Code ('.'));
2606 -- Every entity should have a name except some expanded blocks
2607 -- don't bother about those.
2609 if Chars (Ent) = No_Name then
2613 -- Generates the entity name in upper case
2615 Get_Decoded_Name_String (Chars (Ent));
2617 Store_String_Chars (Name_Buffer (1 .. Name_Len));
2619 end Internal_Full_Qualified_Name;
2621 -- Start of processing for Full_Qualified_Name
2625 Internal_Full_Qualified_Name (E);
2626 Store_String_Char (Get_Char_Code (ASCII.NUL));
2628 end Fully_Qualified_Name_String;
2630 ------------------------
2631 -- Generate_Poll_Call --
2632 ------------------------
2634 procedure Generate_Poll_Call (N : Node_Id) is
2636 -- No poll call if polling not active
2638 if not Polling_Required then
2641 -- Otherwise generate require poll call
2644 Insert_Before_And_Analyze (N,
2645 Make_Procedure_Call_Statement (Sloc (N),
2646 Name => New_Occurrence_Of (RTE (RE_Poll), Sloc (N))));
2648 end Generate_Poll_Call;
2650 ---------------------------------
2651 -- Get_Current_Value_Condition --
2652 ---------------------------------
2654 -- Note: the implementation of this procedure is very closely tied to the
2655 -- implementation of Set_Current_Value_Condition. In the Get procedure, we
2656 -- interpret Current_Value fields set by the Set procedure, so the two
2657 -- procedures need to be closely coordinated.
2659 procedure Get_Current_Value_Condition
2664 Loc : constant Source_Ptr := Sloc (Var);
2665 Ent : constant Entity_Id := Entity (Var);
2667 procedure Process_Current_Value_Condition
2670 -- N is an expression which holds either True (S = True) or False (S =
2671 -- False) in the condition. This procedure digs out the expression and
2672 -- if it refers to Ent, sets Op and Val appropriately.
2674 -------------------------------------
2675 -- Process_Current_Value_Condition --
2676 -------------------------------------
2678 procedure Process_Current_Value_Condition
2689 -- Deal with NOT operators, inverting sense
2691 while Nkind (Cond) = N_Op_Not loop
2692 Cond := Right_Opnd (Cond);
2696 -- Deal with AND THEN and AND cases
2698 if Nkind (Cond) = N_And_Then
2699 or else Nkind (Cond) = N_Op_And
2701 -- Don't ever try to invert a condition that is of the form of an
2702 -- AND or AND THEN (since we are not doing sufficiently general
2703 -- processing to allow this).
2705 if Sens = False then
2711 -- Recursively process AND and AND THEN branches
2713 Process_Current_Value_Condition (Left_Opnd (Cond), True);
2715 if Op /= N_Empty then
2719 Process_Current_Value_Condition (Right_Opnd (Cond), True);
2722 -- Case of relational operator
2724 elsif Nkind (Cond) in N_Op_Compare then
2727 -- Invert sense of test if inverted test
2729 if Sens = False then
2731 when N_Op_Eq => Op := N_Op_Ne;
2732 when N_Op_Ne => Op := N_Op_Eq;
2733 when N_Op_Lt => Op := N_Op_Ge;
2734 when N_Op_Gt => Op := N_Op_Le;
2735 when N_Op_Le => Op := N_Op_Gt;
2736 when N_Op_Ge => Op := N_Op_Lt;
2737 when others => raise Program_Error;
2741 -- Case of entity op value
2743 if Is_Entity_Name (Left_Opnd (Cond))
2744 and then Ent = Entity (Left_Opnd (Cond))
2745 and then Compile_Time_Known_Value (Right_Opnd (Cond))
2747 Val := Right_Opnd (Cond);
2749 -- Case of value op entity
2751 elsif Is_Entity_Name (Right_Opnd (Cond))
2752 and then Ent = Entity (Right_Opnd (Cond))
2753 and then Compile_Time_Known_Value (Left_Opnd (Cond))
2755 Val := Left_Opnd (Cond);
2757 -- We are effectively swapping operands
2760 when N_Op_Eq => null;
2761 when N_Op_Ne => null;
2762 when N_Op_Lt => Op := N_Op_Gt;
2763 when N_Op_Gt => Op := N_Op_Lt;
2764 when N_Op_Le => Op := N_Op_Ge;
2765 when N_Op_Ge => Op := N_Op_Le;
2766 when others => raise Program_Error;
2775 -- Case of Boolean variable reference, return as though the
2776 -- reference had said var = True.
2779 if Is_Entity_Name (Cond)
2780 and then Ent = Entity (Cond)
2782 Val := New_Occurrence_Of (Standard_True, Sloc (Cond));
2784 if Sens = False then
2791 end Process_Current_Value_Condition;
2793 -- Start of processing for Get_Current_Value_Condition
2799 -- Immediate return, nothing doing, if this is not an object
2801 if Ekind (Ent) not in Object_Kind then
2805 -- Otherwise examine current value
2808 CV : constant Node_Id := Current_Value (Ent);
2813 -- If statement. Condition is known true in THEN section, known False
2814 -- in any ELSIF or ELSE part, and unknown outside the IF statement.
2816 if Nkind (CV) = N_If_Statement then
2818 -- Before start of IF statement
2820 if Loc < Sloc (CV) then
2823 -- After end of IF statement
2825 elsif Loc >= Sloc (CV) + Text_Ptr (UI_To_Int (End_Span (CV))) then
2829 -- At this stage we know that we are within the IF statement, but
2830 -- unfortunately, the tree does not record the SLOC of the ELSE so
2831 -- we cannot use a simple SLOC comparison to distinguish between
2832 -- the then/else statements, so we have to climb the tree.
2839 while Parent (N) /= CV loop
2842 -- If we fall off the top of the tree, then that's odd, but
2843 -- perhaps it could occur in some error situation, and the
2844 -- safest response is simply to assume that the outcome of
2845 -- the condition is unknown. No point in bombing during an
2846 -- attempt to optimize things.
2853 -- Now we have N pointing to a node whose parent is the IF
2854 -- statement in question, so now we can tell if we are within
2855 -- the THEN statements.
2857 if Is_List_Member (N)
2858 and then List_Containing (N) = Then_Statements (CV)
2862 -- If the variable reference does not come from source, we
2863 -- cannot reliably tell whether it appears in the else part.
2864 -- In particular, if it appears in generated code for a node
2865 -- that requires finalization, it may be attached to a list
2866 -- that has not been yet inserted into the code. For now,
2867 -- treat it as unknown.
2869 elsif not Comes_From_Source (N) then
2872 -- Otherwise we must be in ELSIF or ELSE part
2879 -- ELSIF part. Condition is known true within the referenced
2880 -- ELSIF, known False in any subsequent ELSIF or ELSE part,
2881 -- and unknown before the ELSE part or after the IF statement.
2883 elsif Nkind (CV) = N_Elsif_Part then
2885 -- if the Elsif_Part had condition_actions, the elsif has been
2886 -- rewritten as a nested if, and the original elsif_part is
2887 -- detached from the tree, so there is no way to obtain useful
2888 -- information on the current value of the variable.
2889 -- Can this be improved ???
2891 if No (Parent (CV)) then
2897 -- Before start of ELSIF part
2899 if Loc < Sloc (CV) then
2902 -- After end of IF statement
2904 elsif Loc >= Sloc (Stm) +
2905 Text_Ptr (UI_To_Int (End_Span (Stm)))
2910 -- Again we lack the SLOC of the ELSE, so we need to climb the
2911 -- tree to see if we are within the ELSIF part in question.
2918 while Parent (N) /= Stm loop
2921 -- If we fall off the top of the tree, then that's odd, but
2922 -- perhaps it could occur in some error situation, and the
2923 -- safest response is simply to assume that the outcome of
2924 -- the condition is unknown. No point in bombing during an
2925 -- attempt to optimize things.
2932 -- Now we have N pointing to a node whose parent is the IF
2933 -- statement in question, so see if is the ELSIF part we want.
2934 -- the THEN statements.
2939 -- Otherwise we must be in subsequent ELSIF or ELSE part
2946 -- Iteration scheme of while loop. The condition is known to be
2947 -- true within the body of the loop.
2949 elsif Nkind (CV) = N_Iteration_Scheme then
2951 Loop_Stmt : constant Node_Id := Parent (CV);
2954 -- Before start of body of loop
2956 if Loc < Sloc (Loop_Stmt) then
2959 -- After end of LOOP statement
2961 elsif Loc >= Sloc (End_Label (Loop_Stmt)) then
2964 -- We are within the body of the loop
2971 -- All other cases of Current_Value settings
2977 -- If we fall through here, then we have a reportable condition, Sens
2978 -- is True if the condition is true and False if it needs inverting.
2980 Process_Current_Value_Condition (Condition (CV), Sens);
2982 end Get_Current_Value_Condition;
2984 ---------------------
2985 -- Get_Stream_Size --
2986 ---------------------
2988 function Get_Stream_Size (E : Entity_Id) return Uint is
2990 -- If we have a Stream_Size clause for this type use it
2992 if Has_Stream_Size_Clause (E) then
2993 return Static_Integer (Expression (Stream_Size_Clause (E)));
2995 -- Otherwise the Stream_Size if the size of the type
3000 end Get_Stream_Size;
3002 ---------------------------
3003 -- Has_Access_Constraint --
3004 ---------------------------
3006 function Has_Access_Constraint (E : Entity_Id) return Boolean is
3008 T : constant Entity_Id := Etype (E);
3011 if Has_Per_Object_Constraint (E)
3012 and then Has_Discriminants (T)
3014 Disc := First_Discriminant (T);
3015 while Present (Disc) loop
3016 if Is_Access_Type (Etype (Disc)) then
3020 Next_Discriminant (Disc);
3027 end Has_Access_Constraint;
3029 ----------------------------------
3030 -- Has_Following_Address_Clause --
3031 ----------------------------------
3033 -- Should this function check the private part in a package ???
3035 function Has_Following_Address_Clause (D : Node_Id) return Boolean is
3036 Id : constant Entity_Id := Defining_Identifier (D);
3041 while Present (Decl) loop
3042 if Nkind (Decl) = N_At_Clause
3043 and then Chars (Identifier (Decl)) = Chars (Id)
3047 elsif Nkind (Decl) = N_Attribute_Definition_Clause
3048 and then Chars (Decl) = Name_Address
3049 and then Chars (Name (Decl)) = Chars (Id)
3058 end Has_Following_Address_Clause;
3060 --------------------
3061 -- Homonym_Number --
3062 --------------------
3064 function Homonym_Number (Subp : Entity_Id) return Nat is
3070 Hom := Homonym (Subp);
3071 while Present (Hom) loop
3072 if Scope (Hom) = Scope (Subp) then
3076 Hom := Homonym (Hom);
3082 -----------------------------------
3083 -- In_Library_Level_Package_Body --
3084 -----------------------------------
3086 function In_Library_Level_Package_Body (Id : Entity_Id) return Boolean is
3088 -- First determine whether the entity appears at the library level, then
3089 -- look at the containing unit.
3091 if Is_Library_Level_Entity (Id) then
3093 Container : constant Node_Id := Cunit (Get_Source_Unit (Id));
3096 return Nkind (Unit (Container)) = N_Package_Body;
3101 end In_Library_Level_Package_Body;
3103 ------------------------------
3104 -- In_Unconditional_Context --
3105 ------------------------------
3107 function In_Unconditional_Context (Node : Node_Id) return Boolean is
3112 while Present (P) loop
3114 when N_Subprogram_Body =>
3117 when N_If_Statement =>
3120 when N_Loop_Statement =>
3123 when N_Case_Statement =>
3132 end In_Unconditional_Context;
3138 procedure Insert_Action (Assoc_Node : Node_Id; Ins_Action : Node_Id) is
3140 if Present (Ins_Action) then
3141 Insert_Actions (Assoc_Node, New_List (Ins_Action));
3145 -- Version with check(s) suppressed
3147 procedure Insert_Action
3148 (Assoc_Node : Node_Id; Ins_Action : Node_Id; Suppress : Check_Id)
3151 Insert_Actions (Assoc_Node, New_List (Ins_Action), Suppress);
3154 -------------------------
3155 -- Insert_Action_After --
3156 -------------------------
3158 procedure Insert_Action_After
3159 (Assoc_Node : Node_Id;
3160 Ins_Action : Node_Id)
3163 Insert_Actions_After (Assoc_Node, New_List (Ins_Action));
3164 end Insert_Action_After;
3166 --------------------
3167 -- Insert_Actions --
3168 --------------------
3170 procedure Insert_Actions (Assoc_Node : Node_Id; Ins_Actions : List_Id) is
3174 Wrapped_Node : Node_Id := Empty;
3177 if No (Ins_Actions) or else Is_Empty_List (Ins_Actions) then
3181 -- Ignore insert of actions from inside default expression (or other
3182 -- similar "spec expression") in the special spec-expression analyze
3183 -- mode. Any insertions at this point have no relevance, since we are
3184 -- only doing the analyze to freeze the types of any static expressions.
3185 -- See section "Handling of Default Expressions" in the spec of package
3186 -- Sem for further details.
3188 if In_Spec_Expression then
3192 -- If the action derives from stuff inside a record, then the actions
3193 -- are attached to the current scope, to be inserted and analyzed on
3194 -- exit from the scope. The reason for this is that we may also be
3195 -- generating freeze actions at the same time, and they must eventually
3196 -- be elaborated in the correct order.
3198 if Is_Record_Type (Current_Scope)
3199 and then not Is_Frozen (Current_Scope)
3201 if No (Scope_Stack.Table
3202 (Scope_Stack.Last).Pending_Freeze_Actions)
3204 Scope_Stack.Table (Scope_Stack.Last).Pending_Freeze_Actions :=
3209 Scope_Stack.Table (Scope_Stack.Last).Pending_Freeze_Actions);
3215 -- We now intend to climb up the tree to find the right point to
3216 -- insert the actions. We start at Assoc_Node, unless this node is a
3217 -- subexpression in which case we start with its parent. We do this for
3218 -- two reasons. First it speeds things up. Second, if Assoc_Node is
3219 -- itself one of the special nodes like N_And_Then, then we assume that
3220 -- an initial request to insert actions for such a node does not expect
3221 -- the actions to get deposited in the node for later handling when the
3222 -- node is expanded, since clearly the node is being dealt with by the
3223 -- caller. Note that in the subexpression case, N is always the child we
3226 -- N_Raise_xxx_Error is an annoying special case, it is a statement if
3227 -- it has type Standard_Void_Type, and a subexpression otherwise.
3228 -- otherwise. Procedure attribute references are also statements.
3230 if Nkind (Assoc_Node) in N_Subexpr
3231 and then (Nkind (Assoc_Node) in N_Raise_xxx_Error
3232 or else Etype (Assoc_Node) /= Standard_Void_Type)
3233 and then (Nkind (Assoc_Node) /= N_Attribute_Reference
3235 not Is_Procedure_Attribute_Name
3236 (Attribute_Name (Assoc_Node)))
3238 P := Assoc_Node; -- ??? does not agree with above!
3239 N := Parent (Assoc_Node);
3241 -- Non-subexpression case. Note that N is initially Empty in this case
3242 -- (N is only guaranteed Non-Empty in the subexpr case).
3249 -- Capture root of the transient scope
3251 if Scope_Is_Transient then
3252 Wrapped_Node := Node_To_Be_Wrapped;
3256 pragma Assert (Present (P));
3260 -- Case of right operand of AND THEN or OR ELSE. Put the actions
3261 -- in the Actions field of the right operand. They will be moved
3262 -- out further when the AND THEN or OR ELSE operator is expanded.
3263 -- Nothing special needs to be done for the left operand since
3264 -- in that case the actions are executed unconditionally.
3266 when N_Short_Circuit =>
3267 if N = Right_Opnd (P) then
3269 -- We are now going to either append the actions to the
3270 -- actions field of the short-circuit operation. We will
3271 -- also analyze the actions now.
3273 -- This analysis is really too early, the proper thing would
3274 -- be to just park them there now, and only analyze them if
3275 -- we find we really need them, and to it at the proper
3276 -- final insertion point. However attempting to this proved
3277 -- tricky, so for now we just kill current values before and
3278 -- after the analyze call to make sure we avoid peculiar
3279 -- optimizations from this out of order insertion.
3281 Kill_Current_Values;
3283 if Present (Actions (P)) then
3284 Insert_List_After_And_Analyze
3285 (Last (Actions (P)), Ins_Actions);
3287 Set_Actions (P, Ins_Actions);
3288 Analyze_List (Actions (P));
3291 Kill_Current_Values;
3296 -- Then or Else operand of conditional expression. Add actions to
3297 -- Then_Actions or Else_Actions field as appropriate. The actions
3298 -- will be moved further out when the conditional is expanded.
3300 when N_Conditional_Expression =>
3302 ThenX : constant Node_Id := Next (First (Expressions (P)));
3303 ElseX : constant Node_Id := Next (ThenX);
3306 -- If the enclosing expression is already analyzed, as
3307 -- is the case for nested elaboration checks, insert the
3308 -- conditional further out.
3310 if Analyzed (P) then
3313 -- Actions belong to the then expression, temporarily place
3314 -- them as Then_Actions of the conditional expr. They will
3315 -- be moved to the proper place later when the conditional
3316 -- expression is expanded.
3318 elsif N = ThenX then
3319 if Present (Then_Actions (P)) then
3320 Insert_List_After_And_Analyze
3321 (Last (Then_Actions (P)), Ins_Actions);
3323 Set_Then_Actions (P, Ins_Actions);
3324 Analyze_List (Then_Actions (P));
3329 -- Actions belong to the else expression, temporarily
3330 -- place them as Else_Actions of the conditional expr.
3331 -- They will be moved to the proper place later when
3332 -- the conditional expression is expanded.
3334 elsif N = ElseX then
3335 if Present (Else_Actions (P)) then
3336 Insert_List_After_And_Analyze
3337 (Last (Else_Actions (P)), Ins_Actions);
3339 Set_Else_Actions (P, Ins_Actions);
3340 Analyze_List (Else_Actions (P));
3345 -- Actions belong to the condition. In this case they are
3346 -- unconditionally executed, and so we can continue the
3347 -- search for the proper insert point.
3354 -- Alternative of case expression, we place the action in the
3355 -- Actions field of the case expression alternative, this will
3356 -- be handled when the case expression is expanded.
3358 when N_Case_Expression_Alternative =>
3359 if Present (Actions (P)) then
3360 Insert_List_After_And_Analyze
3361 (Last (Actions (P)), Ins_Actions);
3363 Set_Actions (P, Ins_Actions);
3364 Analyze_List (Actions (P));
3369 -- Case of appearing within an Expressions_With_Actions node. We
3370 -- prepend the actions to the list of actions already there, if
3371 -- the node has not been analyzed yet. Otherwise find insertion
3372 -- location further up the tree.
3374 when N_Expression_With_Actions =>
3375 if not Analyzed (P) then
3376 Prepend_List (Ins_Actions, Actions (P));
3380 -- Case of appearing in the condition of a while expression or
3381 -- elsif. We insert the actions into the Condition_Actions field.
3382 -- They will be moved further out when the while loop or elsif
3385 when N_Iteration_Scheme |
3388 if N = Condition (P) then
3389 if Present (Condition_Actions (P)) then
3390 Insert_List_After_And_Analyze
3391 (Last (Condition_Actions (P)), Ins_Actions);
3393 Set_Condition_Actions (P, Ins_Actions);
3395 -- Set the parent of the insert actions explicitly. This
3396 -- is not a syntactic field, but we need the parent field
3397 -- set, in particular so that freeze can understand that
3398 -- it is dealing with condition actions, and properly
3399 -- insert the freezing actions.
3401 Set_Parent (Ins_Actions, P);
3402 Analyze_List (Condition_Actions (P));
3408 -- Statements, declarations, pragmas, representation clauses
3413 N_Procedure_Call_Statement |
3414 N_Statement_Other_Than_Procedure_Call |
3420 -- Representation_Clause
3423 N_Attribute_Definition_Clause |
3424 N_Enumeration_Representation_Clause |
3425 N_Record_Representation_Clause |
3429 N_Abstract_Subprogram_Declaration |
3431 N_Exception_Declaration |
3432 N_Exception_Renaming_Declaration |
3433 N_Expression_Function |
3434 N_Formal_Abstract_Subprogram_Declaration |
3435 N_Formal_Concrete_Subprogram_Declaration |
3436 N_Formal_Object_Declaration |
3437 N_Formal_Type_Declaration |
3438 N_Full_Type_Declaration |
3439 N_Function_Instantiation |
3440 N_Generic_Function_Renaming_Declaration |
3441 N_Generic_Package_Declaration |
3442 N_Generic_Package_Renaming_Declaration |
3443 N_Generic_Procedure_Renaming_Declaration |
3444 N_Generic_Subprogram_Declaration |
3445 N_Implicit_Label_Declaration |
3446 N_Incomplete_Type_Declaration |
3447 N_Number_Declaration |
3448 N_Object_Declaration |
3449 N_Object_Renaming_Declaration |
3451 N_Package_Body_Stub |
3452 N_Package_Declaration |
3453 N_Package_Instantiation |
3454 N_Package_Renaming_Declaration |
3455 N_Private_Extension_Declaration |
3456 N_Private_Type_Declaration |
3457 N_Procedure_Instantiation |
3459 N_Protected_Body_Stub |
3460 N_Protected_Type_Declaration |
3461 N_Single_Task_Declaration |
3463 N_Subprogram_Body_Stub |
3464 N_Subprogram_Declaration |
3465 N_Subprogram_Renaming_Declaration |
3466 N_Subtype_Declaration |
3469 N_Task_Type_Declaration |
3471 -- Use clauses can appear in lists of declarations
3473 N_Use_Package_Clause |
3476 -- Freeze entity behaves like a declaration or statement
3480 -- Do not insert here if the item is not a list member (this
3481 -- happens for example with a triggering statement, and the
3482 -- proper approach is to insert before the entire select).
3484 if not Is_List_Member (P) then
3487 -- Do not insert if parent of P is an N_Component_Association
3488 -- node (i.e. we are in the context of an N_Aggregate or
3489 -- N_Extension_Aggregate node. In this case we want to insert
3490 -- before the entire aggregate.
3492 elsif Nkind (Parent (P)) = N_Component_Association then
3495 -- Do not insert if the parent of P is either an N_Variant node
3496 -- or an N_Record_Definition node, meaning in either case that
3497 -- P is a member of a component list, and that therefore the
3498 -- actions should be inserted outside the complete record
3501 elsif Nkind (Parent (P)) = N_Variant
3502 or else Nkind (Parent (P)) = N_Record_Definition
3506 -- Do not insert freeze nodes within the loop generated for
3507 -- an aggregate, because they may be elaborated too late for
3508 -- subsequent use in the back end: within a package spec the
3509 -- loop is part of the elaboration procedure and is only
3510 -- elaborated during the second pass.
3512 -- If the loop comes from source, or the entity is local to the
3513 -- loop itself it must remain within.
3515 elsif Nkind (Parent (P)) = N_Loop_Statement
3516 and then not Comes_From_Source (Parent (P))
3517 and then Nkind (First (Ins_Actions)) = N_Freeze_Entity
3519 Scope (Entity (First (Ins_Actions))) /= Current_Scope
3523 -- Otherwise we can go ahead and do the insertion
3525 elsif P = Wrapped_Node then
3526 Store_Before_Actions_In_Scope (Ins_Actions);
3530 Insert_List_Before_And_Analyze (P, Ins_Actions);
3534 -- A special case, N_Raise_xxx_Error can act either as a statement
3535 -- or a subexpression. We tell the difference by looking at the
3536 -- Etype. It is set to Standard_Void_Type in the statement case.
3539 N_Raise_xxx_Error =>
3540 if Etype (P) = Standard_Void_Type then
3541 if P = Wrapped_Node then
3542 Store_Before_Actions_In_Scope (Ins_Actions);
3544 Insert_List_Before_And_Analyze (P, Ins_Actions);
3549 -- In the subexpression case, keep climbing
3555 -- If a component association appears within a loop created for
3556 -- an array aggregate, attach the actions to the association so
3557 -- they can be subsequently inserted within the loop. For other
3558 -- component associations insert outside of the aggregate. For
3559 -- an association that will generate a loop, its Loop_Actions
3560 -- attribute is already initialized (see exp_aggr.adb).
3562 -- The list of loop_actions can in turn generate additional ones,
3563 -- that are inserted before the associated node. If the associated
3564 -- node is outside the aggregate, the new actions are collected
3565 -- at the end of the loop actions, to respect the order in which
3566 -- they are to be elaborated.
3569 N_Component_Association =>
3570 if Nkind (Parent (P)) = N_Aggregate
3571 and then Present (Loop_Actions (P))
3573 if Is_Empty_List (Loop_Actions (P)) then
3574 Set_Loop_Actions (P, Ins_Actions);
3575 Analyze_List (Ins_Actions);
3582 -- Check whether these actions were generated by a
3583 -- declaration that is part of the loop_ actions
3584 -- for the component_association.
3587 while Present (Decl) loop
3588 exit when Parent (Decl) = P
3589 and then Is_List_Member (Decl)
3591 List_Containing (Decl) = Loop_Actions (P);
3592 Decl := Parent (Decl);
3595 if Present (Decl) then
3596 Insert_List_Before_And_Analyze
3597 (Decl, Ins_Actions);
3599 Insert_List_After_And_Analyze
3600 (Last (Loop_Actions (P)), Ins_Actions);
3611 -- Another special case, an attribute denoting a procedure call
3614 N_Attribute_Reference =>
3615 if Is_Procedure_Attribute_Name (Attribute_Name (P)) then
3616 if P = Wrapped_Node then
3617 Store_Before_Actions_In_Scope (Ins_Actions);
3619 Insert_List_Before_And_Analyze (P, Ins_Actions);
3624 -- In the subexpression case, keep climbing
3630 -- A contract node should not belong to the tree
3633 raise Program_Error;
3635 -- For all other node types, keep climbing tree
3639 N_Accept_Alternative |
3640 N_Access_Definition |
3641 N_Access_Function_Definition |
3642 N_Access_Procedure_Definition |
3643 N_Access_To_Object_Definition |
3646 N_Aspect_Specification |
3648 N_Case_Statement_Alternative |
3649 N_Character_Literal |
3650 N_Compilation_Unit |
3651 N_Compilation_Unit_Aux |
3652 N_Component_Clause |
3653 N_Component_Declaration |
3654 N_Component_Definition |
3656 N_Constrained_Array_Definition |
3657 N_Decimal_Fixed_Point_Definition |
3658 N_Defining_Character_Literal |
3659 N_Defining_Identifier |
3660 N_Defining_Operator_Symbol |
3661 N_Defining_Program_Unit_Name |
3662 N_Delay_Alternative |
3663 N_Delta_Constraint |
3664 N_Derived_Type_Definition |
3666 N_Digits_Constraint |
3667 N_Discriminant_Association |
3668 N_Discriminant_Specification |
3670 N_Entry_Body_Formal_Part |
3671 N_Entry_Call_Alternative |
3672 N_Entry_Declaration |
3673 N_Entry_Index_Specification |
3674 N_Enumeration_Type_Definition |
3676 N_Exception_Handler |
3678 N_Explicit_Dereference |
3679 N_Extension_Aggregate |
3680 N_Floating_Point_Definition |
3681 N_Formal_Decimal_Fixed_Point_Definition |
3682 N_Formal_Derived_Type_Definition |
3683 N_Formal_Discrete_Type_Definition |
3684 N_Formal_Floating_Point_Definition |
3685 N_Formal_Modular_Type_Definition |
3686 N_Formal_Ordinary_Fixed_Point_Definition |
3687 N_Formal_Package_Declaration |
3688 N_Formal_Private_Type_Definition |
3689 N_Formal_Incomplete_Type_Definition |
3690 N_Formal_Signed_Integer_Type_Definition |
3692 N_Function_Specification |
3693 N_Generic_Association |
3694 N_Handled_Sequence_Of_Statements |
3697 N_Index_Or_Discriminant_Constraint |
3698 N_Indexed_Component |
3700 N_Iterator_Specification |
3703 N_Loop_Parameter_Specification |
3705 N_Modular_Type_Definition |
3731 N_Op_Shift_Right_Arithmetic |
3735 N_Ordinary_Fixed_Point_Definition |
3737 N_Package_Specification |
3738 N_Parameter_Association |
3739 N_Parameter_Specification |
3740 N_Pop_Constraint_Error_Label |
3741 N_Pop_Program_Error_Label |
3742 N_Pop_Storage_Error_Label |
3743 N_Pragma_Argument_Association |
3744 N_Procedure_Specification |
3745 N_Protected_Definition |
3746 N_Push_Constraint_Error_Label |
3747 N_Push_Program_Error_Label |
3748 N_Push_Storage_Error_Label |
3749 N_Qualified_Expression |
3750 N_Quantified_Expression |
3752 N_Range_Constraint |
3754 N_Real_Range_Specification |
3755 N_Record_Definition |
3757 N_SCIL_Dispatch_Table_Tag_Init |
3758 N_SCIL_Dispatching_Call |
3759 N_SCIL_Membership_Test |
3760 N_Selected_Component |
3761 N_Signed_Integer_Type_Definition |
3762 N_Single_Protected_Declaration |
3766 N_Subtype_Indication |
3769 N_Terminate_Alternative |
3770 N_Triggering_Alternative |
3772 N_Unchecked_Expression |
3773 N_Unchecked_Type_Conversion |
3774 N_Unconstrained_Array_Definition |
3779 N_Validate_Unchecked_Conversion |
3786 -- Make sure that inserted actions stay in the transient scope
3788 if P = Wrapped_Node then
3789 Store_Before_Actions_In_Scope (Ins_Actions);
3793 -- If we fall through above tests, keep climbing tree
3797 if Nkind (Parent (N)) = N_Subunit then
3799 -- This is the proper body corresponding to a stub. Insertion must
3800 -- be done at the point of the stub, which is in the declarative
3801 -- part of the parent unit.
3803 P := Corresponding_Stub (Parent (N));
3811 -- Version with check(s) suppressed
3813 procedure Insert_Actions
3814 (Assoc_Node : Node_Id;
3815 Ins_Actions : List_Id;
3816 Suppress : Check_Id)
3819 if Suppress = All_Checks then
3821 Svg : constant Suppress_Array := Scope_Suppress;
3823 Scope_Suppress := (others => True);
3824 Insert_Actions (Assoc_Node, Ins_Actions);
3825 Scope_Suppress := Svg;
3830 Svg : constant Boolean := Scope_Suppress (Suppress);
3832 Scope_Suppress (Suppress) := True;
3833 Insert_Actions (Assoc_Node, Ins_Actions);
3834 Scope_Suppress (Suppress) := Svg;
3839 --------------------------
3840 -- Insert_Actions_After --
3841 --------------------------
3843 procedure Insert_Actions_After
3844 (Assoc_Node : Node_Id;
3845 Ins_Actions : List_Id)
3848 if Scope_Is_Transient
3849 and then Assoc_Node = Node_To_Be_Wrapped
3851 Store_After_Actions_In_Scope (Ins_Actions);
3853 Insert_List_After_And_Analyze (Assoc_Node, Ins_Actions);
3855 end Insert_Actions_After;
3857 ---------------------------------
3858 -- Insert_Library_Level_Action --
3859 ---------------------------------
3861 procedure Insert_Library_Level_Action (N : Node_Id) is
3862 Aux : constant Node_Id := Aux_Decls_Node (Cunit (Main_Unit));
3865 Push_Scope (Cunit_Entity (Main_Unit));
3866 -- ??? should this be Current_Sem_Unit instead of Main_Unit?
3868 if No (Actions (Aux)) then
3869 Set_Actions (Aux, New_List (N));
3871 Append (N, Actions (Aux));
3876 end Insert_Library_Level_Action;
3878 ----------------------------------
3879 -- Insert_Library_Level_Actions --
3880 ----------------------------------
3882 procedure Insert_Library_Level_Actions (L : List_Id) is
3883 Aux : constant Node_Id := Aux_Decls_Node (Cunit (Main_Unit));
3886 if Is_Non_Empty_List (L) then
3887 Push_Scope (Cunit_Entity (Main_Unit));
3888 -- ??? should this be Current_Sem_Unit instead of Main_Unit?
3890 if No (Actions (Aux)) then
3891 Set_Actions (Aux, L);
3894 Insert_List_After_And_Analyze (Last (Actions (Aux)), L);
3899 end Insert_Library_Level_Actions;
3901 ----------------------
3902 -- Inside_Init_Proc --
3903 ----------------------
3905 function Inside_Init_Proc return Boolean is
3911 and then S /= Standard_Standard
3913 if Is_Init_Proc (S) then
3921 end Inside_Init_Proc;
3923 ----------------------------
3924 -- Is_All_Null_Statements --
3925 ----------------------------
3927 function Is_All_Null_Statements (L : List_Id) return Boolean is
3932 while Present (Stm) loop
3933 if Nkind (Stm) /= N_Null_Statement then
3941 end Is_All_Null_Statements;
3943 ---------------------------------------------
3944 -- Is_Displacement_Of_Ctrl_Function_Result --
3945 ---------------------------------------------
3947 function Is_Displacement_Of_Ctrl_Function_Result
3948 (Obj_Id : Entity_Id) return Boolean
3950 function Initialized_By_Ctrl_Function (N : Node_Id) return Boolean;
3951 -- Determine whether object declaration N is initialized by a controlled
3954 function Is_Displace_Call (N : Node_Id) return Boolean;
3955 -- Determine whether a particular node is a call to Ada.Tags.Displace.
3956 -- The call might be nested within other actions such as conversions.
3958 ----------------------------------
3959 -- Initialized_By_Ctrl_Function --
3960 ----------------------------------
3962 function Initialized_By_Ctrl_Function (N : Node_Id) return Boolean is
3963 Expr : constant Node_Id := Original_Node (Expression (N));
3966 Nkind (Expr) = N_Function_Call
3967 and then Needs_Finalization (Etype (Expr));
3968 end Initialized_By_Ctrl_Function;
3970 ----------------------
3971 -- Is_Displace_Call --
3972 ----------------------
3974 function Is_Displace_Call (N : Node_Id) return Boolean is
3975 Call : Node_Id := N;
3978 -- Strip various actions which may precede a call to Displace
3981 if Nkind (Call) = N_Explicit_Dereference then
3982 Call := Prefix (Call);
3984 elsif Nkind_In (Call, N_Type_Conversion,
3985 N_Unchecked_Type_Conversion)
3987 Call := Expression (Call);
3995 Nkind (Call) = N_Function_Call
3996 and then Is_RTE (Entity (Name (Call)), RE_Displace);
3997 end Is_Displace_Call;
4001 Decl : constant Node_Id := Parent (Obj_Id);
4002 Obj_Typ : constant Entity_Id := Base_Type (Etype (Obj_Id));
4003 Orig_Decl : constant Node_Id := Original_Node (Decl);
4005 -- Start of processing for Is_Displacement_Of_Ctrl_Function_Result
4008 -- Detect the following case:
4010 -- Obj : Class_Wide_Type := Function_Call (...);
4012 -- which is rewritten into:
4014 -- Temp : ... := Function_Call (...)'reference;
4015 -- Obj : Class_Wide_Type renames (... Ada.Tags.Displace (Temp));
4017 -- when the return type of the function and the class-wide type require
4018 -- dispatch table pointer displacement.
4021 Nkind (Decl) = N_Object_Renaming_Declaration
4022 and then Nkind (Orig_Decl) = N_Object_Declaration
4023 and then Comes_From_Source (Orig_Decl)
4024 and then Initialized_By_Ctrl_Function (Orig_Decl)
4025 and then Is_Class_Wide_Type (Obj_Typ)
4026 and then Is_Displace_Call (Renamed_Object (Obj_Id));
4027 end Is_Displacement_Of_Ctrl_Function_Result;
4029 ------------------------------
4030 -- Is_Finalizable_Transient --
4031 ------------------------------
4033 function Is_Finalizable_Transient
4035 Rel_Node : Node_Id) return Boolean
4037 Obj_Id : constant Entity_Id := Defining_Identifier (Decl);
4038 Obj_Typ : constant Entity_Id := Base_Type (Etype (Obj_Id));
4039 Desig : Entity_Id := Obj_Typ;
4041 function Initialized_By_Access (Trans_Id : Entity_Id) return Boolean;
4042 -- Determine whether transient object Trans_Id is initialized either
4043 -- by a function call which returns an access type or simply renames
4046 function Initialized_By_Aliased_BIP_Func_Call
4047 (Trans_Id : Entity_Id) return Boolean;
4048 -- Determine whether transient object Trans_Id is initialized by a
4049 -- build-in-place function call where the BIPalloc parameter is of
4050 -- value 1 and BIPaccess is not null. This case creates an aliasing
4051 -- between the returned value and the value denoted by BIPaccess.
4054 (Trans_Id : Entity_Id;
4055 First_Stmt : Node_Id) return Boolean;
4056 -- Determine whether transient object Trans_Id has been renamed or
4057 -- aliased through 'reference in the statement list starting from
4060 function Is_Allocated (Trans_Id : Entity_Id) return Boolean;
4061 -- Determine whether transient object Trans_Id is allocated on the heap
4063 function Is_Iterated_Container
4064 (Trans_Id : Entity_Id;
4065 First_Stmt : Node_Id) return Boolean;
4066 -- Determine whether transient object Trans_Id denotes a container which
4067 -- is in the process of being iterated in the statement list starting
4070 ---------------------------
4071 -- Initialized_By_Access --
4072 ---------------------------
4074 function Initialized_By_Access (Trans_Id : Entity_Id) return Boolean is
4075 Expr : constant Node_Id := Expression (Parent (Trans_Id));
4080 and then Nkind (Expr) /= N_Reference
4081 and then Is_Access_Type (Etype (Expr));
4082 end Initialized_By_Access;
4084 ------------------------------------------
4085 -- Initialized_By_Aliased_BIP_Func_Call --
4086 ------------------------------------------
4088 function Initialized_By_Aliased_BIP_Func_Call
4089 (Trans_Id : Entity_Id) return Boolean
4091 Call : Node_Id := Expression (Parent (Trans_Id));
4094 -- Build-in-place calls usually appear in 'reference format
4096 if Nkind (Call) = N_Reference then
4097 Call := Prefix (Call);
4100 if Is_Build_In_Place_Function_Call (Call) then
4102 Access_Nam : Name_Id := No_Name;
4103 Access_OK : Boolean := False;
4105 Alloc_Nam : Name_Id := No_Name;
4106 Alloc_OK : Boolean := False;
4108 Func_Id : Entity_Id;
4112 -- Examine all parameter associations of the function call
4114 Param := First (Parameter_Associations (Call));
4115 while Present (Param) loop
4116 if Nkind (Param) = N_Parameter_Association
4117 and then Nkind (Selector_Name (Param)) = N_Identifier
4119 Actual := Explicit_Actual_Parameter (Param);
4120 Formal := Selector_Name (Param);
4122 -- Construct the names of formals BIPaccess and BIPalloc
4123 -- using the function name retrieved from an arbitrary
4126 if Access_Nam = No_Name
4127 and then Alloc_Nam = No_Name
4128 and then Present (Entity (Formal))
4130 Func_Id := Scope (Entity (Formal));
4133 New_External_Name (Chars (Func_Id),
4134 BIP_Formal_Suffix (BIP_Object_Access));
4137 New_External_Name (Chars (Func_Id),
4138 BIP_Formal_Suffix (BIP_Alloc_Form));
4141 -- A match for BIPaccess => Temp has been found
4143 if Chars (Formal) = Access_Nam
4144 and then Nkind (Actual) /= N_Null
4149 -- A match for BIPalloc => 1 has been found
4151 if Chars (Formal) = Alloc_Nam
4152 and then Nkind (Actual) = N_Integer_Literal
4153 and then Intval (Actual) = Uint_1
4162 return Access_OK and then Alloc_OK;
4167 end Initialized_By_Aliased_BIP_Func_Call;
4174 (Trans_Id : Entity_Id;
4175 First_Stmt : Node_Id) return Boolean
4177 function Find_Renamed_Object (Ren_Decl : Node_Id) return Entity_Id;
4178 -- Given an object renaming declaration, retrieve the entity of the
4179 -- renamed name. Return Empty if the renamed name is anything other
4180 -- than a variable or a constant.
4182 -------------------------
4183 -- Find_Renamed_Object --
4184 -------------------------
4186 function Find_Renamed_Object (Ren_Decl : Node_Id) return Entity_Id is
4187 Ren_Obj : Node_Id := Empty;
4189 function Find_Object (N : Node_Id) return Traverse_Result;
4190 -- Try to detect an object which is either a constant or a
4197 function Find_Object (N : Node_Id) return Traverse_Result is
4199 -- Stop the search once a constant or a variable has been
4202 if Nkind (N) = N_Identifier
4203 and then Present (Entity (N))
4204 and then Ekind_In (Entity (N), E_Constant, E_Variable)
4206 Ren_Obj := Entity (N);
4213 procedure Search is new Traverse_Proc (Find_Object);
4217 Typ : constant Entity_Id := Etype (Defining_Identifier (Ren_Decl));
4219 -- Start of processing for Find_Renamed_Object
4222 -- Actions related to dispatching calls may appear as renamings of
4223 -- tags. Do not process this type of renaming because it does not
4224 -- use the actual value of the object.
4226 if not Is_RTE (Typ, RE_Tag_Ptr) then
4227 Search (Name (Ren_Decl));
4231 end Find_Renamed_Object;
4236 Ren_Obj : Entity_Id;
4239 -- Start of processing for Is_Aliased
4243 while Present (Stmt) loop
4244 if Nkind (Stmt) = N_Object_Declaration then
4245 Expr := Expression (Stmt);
4248 and then Nkind (Expr) = N_Reference
4249 and then Nkind (Prefix (Expr)) = N_Identifier
4250 and then Entity (Prefix (Expr)) = Trans_Id
4255 elsif Nkind (Stmt) = N_Object_Renaming_Declaration then
4256 Ren_Obj := Find_Renamed_Object (Stmt);
4258 if Present (Ren_Obj)
4259 and then Ren_Obj = Trans_Id
4275 function Is_Allocated (Trans_Id : Entity_Id) return Boolean is
4276 Expr : constant Node_Id := Expression (Parent (Trans_Id));
4279 Is_Access_Type (Etype (Trans_Id))
4280 and then Present (Expr)
4281 and then Nkind (Expr) = N_Allocator;
4284 ---------------------------
4285 -- Is_Iterated_Container --
4286 ---------------------------
4288 function Is_Iterated_Container
4289 (Trans_Id : Entity_Id;
4290 First_Stmt : Node_Id) return Boolean
4300 -- It is not possible to iterate over containers in non-Ada 2012 code
4302 if Ada_Version < Ada_2012 then
4306 Typ := Etype (Trans_Id);
4308 -- Handle access type created for secondary stack use
4310 if Is_Access_Type (Typ) then
4311 Typ := Designated_Type (Typ);
4314 -- Look for aspect Default_Iterator
4316 if Has_Aspects (Parent (Typ)) then
4317 Aspect := Find_Aspect (Typ, Aspect_Default_Iterator);
4319 if Present (Aspect) then
4320 Iter := Entity (Aspect);
4322 -- Examine the statements following the container object and
4323 -- look for a call to the default iterate routine where the
4324 -- first parameter is the transient. Such a call appears as:
4326 -- It : Access_To_CW_Iterator :=
4327 -- Iterate (Tran_Id.all, ...)'reference;
4330 while Present (Stmt) loop
4332 -- Detect an object declaration which is initialized by a
4333 -- secondary stack function call.
4335 if Nkind (Stmt) = N_Object_Declaration
4336 and then Present (Expression (Stmt))
4337 and then Nkind (Expression (Stmt)) = N_Reference
4338 and then Nkind (Prefix (Expression (Stmt))) =
4341 Call := Prefix (Expression (Stmt));
4343 -- The call must invoke the default iterate routine of
4344 -- the container and the transient object must appear as
4345 -- the first actual parameter. Skip any calls whose names
4346 -- are not entities.
4348 if Is_Entity_Name (Name (Call))
4349 and then Entity (Name (Call)) = Iter
4350 and then Present (Parameter_Associations (Call))
4352 Param := First (Parameter_Associations (Call));
4354 if Nkind (Param) = N_Explicit_Dereference
4355 and then Entity (Prefix (Param)) = Trans_Id
4368 end Is_Iterated_Container;
4370 -- Start of processing for Is_Finalizable_Transient
4373 -- Handle access types
4375 if Is_Access_Type (Desig) then
4376 Desig := Available_View (Designated_Type (Desig));
4380 Ekind_In (Obj_Id, E_Constant, E_Variable)
4381 and then Needs_Finalization (Desig)
4382 and then Requires_Transient_Scope (Desig)
4383 and then Nkind (Rel_Node) /= N_Simple_Return_Statement
4385 -- Do not consider renamed or 'reference-d transient objects because
4386 -- the act of renaming extends the object's lifetime.
4388 and then not Is_Aliased (Obj_Id, Decl)
4390 -- Do not consider transient objects allocated on the heap since
4391 -- they are attached to a finalization master.
4393 and then not Is_Allocated (Obj_Id)
4395 -- If the transient object is a pointer, check that it is not
4396 -- initialized by a function which returns a pointer or acts as a
4397 -- renaming of another pointer.
4400 (not Is_Access_Type (Obj_Typ)
4401 or else not Initialized_By_Access (Obj_Id))
4403 -- Do not consider transient objects which act as indirect aliases
4404 -- of build-in-place function results.
4406 and then not Initialized_By_Aliased_BIP_Func_Call (Obj_Id)
4408 -- Do not consider conversions of tags to class-wide types
4410 and then not Is_Tag_To_Class_Wide_Conversion (Obj_Id)
4412 -- Do not consider containers in the context of iterator loops. Such
4413 -- transient objects must exist for as long as the loop is around,
4414 -- otherwise any operation carried out by the iterator will fail.
4416 and then not Is_Iterated_Container (Obj_Id, Decl);
4417 end Is_Finalizable_Transient;
4419 ---------------------------------
4420 -- Is_Fully_Repped_Tagged_Type --
4421 ---------------------------------
4423 function Is_Fully_Repped_Tagged_Type (T : Entity_Id) return Boolean is
4424 U : constant Entity_Id := Underlying_Type (T);
4428 if No (U) or else not Is_Tagged_Type (U) then
4430 elsif Has_Discriminants (U) then
4432 elsif not Has_Specified_Layout (U) then
4436 -- Here we have a tagged type, see if it has any unlayed out fields
4437 -- other than a possible tag and parent fields. If so, we return False.
4439 Comp := First_Component (U);
4440 while Present (Comp) loop
4441 if not Is_Tag (Comp)
4442 and then Chars (Comp) /= Name_uParent
4443 and then No (Component_Clause (Comp))
4447 Next_Component (Comp);
4451 -- All components are layed out
4454 end Is_Fully_Repped_Tagged_Type;
4456 ----------------------------------
4457 -- Is_Library_Level_Tagged_Type --
4458 ----------------------------------
4460 function Is_Library_Level_Tagged_Type (Typ : Entity_Id) return Boolean is
4462 return Is_Tagged_Type (Typ)
4463 and then Is_Library_Level_Entity (Typ);
4464 end Is_Library_Level_Tagged_Type;
4466 ----------------------------------
4467 -- Is_Null_Access_BIP_Func_Call --
4468 ----------------------------------
4470 function Is_Null_Access_BIP_Func_Call (Expr : Node_Id) return Boolean is
4471 Call : Node_Id := Expr;
4474 -- Build-in-place calls usually appear in 'reference format
4476 if Nkind (Call) = N_Reference then
4477 Call := Prefix (Call);
4480 if Nkind_In (Call, N_Qualified_Expression,
4481 N_Unchecked_Type_Conversion)
4483 Call := Expression (Call);
4486 if Is_Build_In_Place_Function_Call (Call) then
4488 Access_Nam : Name_Id := No_Name;
4494 -- Examine all parameter associations of the function call
4496 Param := First (Parameter_Associations (Call));
4497 while Present (Param) loop
4498 if Nkind (Param) = N_Parameter_Association
4499 and then Nkind (Selector_Name (Param)) = N_Identifier
4501 Formal := Selector_Name (Param);
4502 Actual := Explicit_Actual_Parameter (Param);
4504 -- Construct the name of formal BIPaccess. It is much easier
4505 -- to extract the name of the function using an arbitrary
4506 -- formal's scope rather than the Name field of Call.
4508 if Access_Nam = No_Name
4509 and then Present (Entity (Formal))
4513 (Chars (Scope (Entity (Formal))),
4514 BIP_Formal_Suffix (BIP_Object_Access));
4517 -- A match for BIPaccess => null has been found
4519 if Chars (Formal) = Access_Nam
4520 and then Nkind (Actual) = N_Null
4532 end Is_Null_Access_BIP_Func_Call;
4534 --------------------------
4535 -- Is_Non_BIP_Func_Call --
4536 --------------------------
4538 function Is_Non_BIP_Func_Call (Expr : Node_Id) return Boolean is
4540 -- The expected call is of the format
4542 -- Func_Call'reference
4545 Nkind (Expr) = N_Reference
4546 and then Nkind (Prefix (Expr)) = N_Function_Call
4547 and then not Is_Build_In_Place_Function_Call (Prefix (Expr));
4548 end Is_Non_BIP_Func_Call;
4550 ----------------------------------
4551 -- Is_Possibly_Unaligned_Object --
4552 ----------------------------------
4554 function Is_Possibly_Unaligned_Object (N : Node_Id) return Boolean is
4555 T : constant Entity_Id := Etype (N);
4558 -- If renamed object, apply test to underlying object
4560 if Is_Entity_Name (N)
4561 and then Is_Object (Entity (N))
4562 and then Present (Renamed_Object (Entity (N)))
4564 return Is_Possibly_Unaligned_Object (Renamed_Object (Entity (N)));
4567 -- Tagged and controlled types and aliased types are always aligned, as
4568 -- are concurrent types.
4571 or else Has_Controlled_Component (T)
4572 or else Is_Concurrent_Type (T)
4573 or else Is_Tagged_Type (T)
4574 or else Is_Controlled (T)
4579 -- If this is an element of a packed array, may be unaligned
4581 if Is_Ref_To_Bit_Packed_Array (N) then
4585 -- Case of indexed component reference: test whether prefix is unaligned
4587 if Nkind (N) = N_Indexed_Component then
4588 return Is_Possibly_Unaligned_Object (Prefix (N));
4590 -- Case of selected component reference
4592 elsif Nkind (N) = N_Selected_Component then
4594 P : constant Node_Id := Prefix (N);
4595 C : constant Entity_Id := Entity (Selector_Name (N));
4600 -- If component reference is for an array with non-static bounds,
4601 -- then it is always aligned: we can only process unaligned arrays
4602 -- with static bounds (more precisely compile time known bounds).
4604 if Is_Array_Type (T)
4605 and then not Compile_Time_Known_Bounds (T)
4610 -- If component is aliased, it is definitely properly aligned
4612 if Is_Aliased (C) then
4616 -- If component is for a type implemented as a scalar, and the
4617 -- record is packed, and the component is other than the first
4618 -- component of the record, then the component may be unaligned.
4620 if Is_Packed (Etype (P))
4621 and then Represented_As_Scalar (Etype (C))
4622 and then First_Entity (Scope (C)) /= C
4627 -- Compute maximum possible alignment for T
4629 -- If alignment is known, then that settles things
4631 if Known_Alignment (T) then
4632 M := UI_To_Int (Alignment (T));
4634 -- If alignment is not known, tentatively set max alignment
4637 M := Ttypes.Maximum_Alignment;
4639 -- We can reduce this if the Esize is known since the default
4640 -- alignment will never be more than the smallest power of 2
4641 -- that does not exceed this Esize value.
4643 if Known_Esize (T) then
4644 S := UI_To_Int (Esize (T));
4646 while (M / 2) >= S loop
4652 -- The following code is historical, it used to be present but it
4653 -- is too cautious, because the front-end does not know the proper
4654 -- default alignments for the target. Also, if the alignment is
4655 -- not known, the front end can't know in any case! If a copy is
4656 -- needed, the back-end will take care of it. This whole section
4657 -- including this comment can be removed later ???
4659 -- If the component reference is for a record that has a specified
4660 -- alignment, and we either know it is too small, or cannot tell,
4661 -- then the component may be unaligned.
4663 -- What is the following commented out code ???
4665 -- if Known_Alignment (Etype (P))
4666 -- and then Alignment (Etype (P)) < Ttypes.Maximum_Alignment
4667 -- and then M > Alignment (Etype (P))
4672 -- Case of component clause present which may specify an
4673 -- unaligned position.
4675 if Present (Component_Clause (C)) then
4677 -- Otherwise we can do a test to make sure that the actual
4678 -- start position in the record, and the length, are both
4679 -- consistent with the required alignment. If not, we know
4680 -- that we are unaligned.
4683 Align_In_Bits : constant Nat := M * System_Storage_Unit;
4685 if Component_Bit_Offset (C) mod Align_In_Bits /= 0
4686 or else Esize (C) mod Align_In_Bits /= 0
4693 -- Otherwise, for a component reference, test prefix
4695 return Is_Possibly_Unaligned_Object (P);
4698 -- If not a component reference, must be aligned
4703 end Is_Possibly_Unaligned_Object;
4705 ---------------------------------
4706 -- Is_Possibly_Unaligned_Slice --
4707 ---------------------------------
4709 function Is_Possibly_Unaligned_Slice (N : Node_Id) return Boolean is
4711 -- Go to renamed object
4713 if Is_Entity_Name (N)
4714 and then Is_Object (Entity (N))
4715 and then Present (Renamed_Object (Entity (N)))
4717 return Is_Possibly_Unaligned_Slice (Renamed_Object (Entity (N)));
4720 -- The reference must be a slice
4722 if Nkind (N) /= N_Slice then
4726 -- Always assume the worst for a nested record component with a
4727 -- component clause, which gigi/gcc does not appear to handle well.
4728 -- It is not clear why this special test is needed at all ???
4730 if Nkind (Prefix (N)) = N_Selected_Component
4731 and then Nkind (Prefix (Prefix (N))) = N_Selected_Component
4733 Present (Component_Clause (Entity (Selector_Name (Prefix (N)))))
4738 -- We only need to worry if the target has strict alignment
4740 if not Target_Strict_Alignment then
4744 -- If it is a slice, then look at the array type being sliced
4747 Sarr : constant Node_Id := Prefix (N);
4748 -- Prefix of the slice, i.e. the array being sliced
4750 Styp : constant Entity_Id := Etype (Prefix (N));
4751 -- Type of the array being sliced
4757 -- The problems arise if the array object that is being sliced
4758 -- is a component of a record or array, and we cannot guarantee
4759 -- the alignment of the array within its containing object.
4761 -- To investigate this, we look at successive prefixes to see
4762 -- if we have a worrisome indexed or selected component.
4766 -- Case of array is part of an indexed component reference
4768 if Nkind (Pref) = N_Indexed_Component then
4769 Ptyp := Etype (Prefix (Pref));
4771 -- The only problematic case is when the array is packed, in
4772 -- which case we really know nothing about the alignment of
4773 -- individual components.
4775 if Is_Bit_Packed_Array (Ptyp) then
4779 -- Case of array is part of a selected component reference
4781 elsif Nkind (Pref) = N_Selected_Component then
4782 Ptyp := Etype (Prefix (Pref));
4784 -- We are definitely in trouble if the record in question
4785 -- has an alignment, and either we know this alignment is
4786 -- inconsistent with the alignment of the slice, or we don't
4787 -- know what the alignment of the slice should be.
4789 if Known_Alignment (Ptyp)
4790 and then (Unknown_Alignment (Styp)
4791 or else Alignment (Styp) > Alignment (Ptyp))
4796 -- We are in potential trouble if the record type is packed.
4797 -- We could special case when we know that the array is the
4798 -- first component, but that's not such a simple case ???
4800 if Is_Packed (Ptyp) then
4804 -- We are in trouble if there is a component clause, and
4805 -- either we do not know the alignment of the slice, or
4806 -- the alignment of the slice is inconsistent with the
4807 -- bit position specified by the component clause.
4810 Field : constant Entity_Id := Entity (Selector_Name (Pref));
4812 if Present (Component_Clause (Field))
4814 (Unknown_Alignment (Styp)
4816 (Component_Bit_Offset (Field) mod
4817 (System_Storage_Unit * Alignment (Styp))) /= 0)
4823 -- For cases other than selected or indexed components we know we
4824 -- are OK, since no issues arise over alignment.
4830 -- We processed an indexed component or selected component
4831 -- reference that looked safe, so keep checking prefixes.
4833 Pref := Prefix (Pref);
4836 end Is_Possibly_Unaligned_Slice;
4838 -------------------------------
4839 -- Is_Related_To_Func_Return --
4840 -------------------------------
4842 function Is_Related_To_Func_Return (Id : Entity_Id) return Boolean is
4843 Expr : constant Node_Id := Related_Expression (Id);
4847 and then Nkind (Expr) = N_Explicit_Dereference
4848 and then Nkind (Parent (Expr)) = N_Simple_Return_Statement;
4849 end Is_Related_To_Func_Return;
4851 --------------------------------
4852 -- Is_Ref_To_Bit_Packed_Array --
4853 --------------------------------
4855 function Is_Ref_To_Bit_Packed_Array (N : Node_Id) return Boolean is
4860 if Is_Entity_Name (N)
4861 and then Is_Object (Entity (N))
4862 and then Present (Renamed_Object (Entity (N)))
4864 return Is_Ref_To_Bit_Packed_Array (Renamed_Object (Entity (N)));
4867 if Nkind (N) = N_Indexed_Component
4869 Nkind (N) = N_Selected_Component
4871 if Is_Bit_Packed_Array (Etype (Prefix (N))) then
4874 Result := Is_Ref_To_Bit_Packed_Array (Prefix (N));
4877 if Result and then Nkind (N) = N_Indexed_Component then
4878 Expr := First (Expressions (N));
4879 while Present (Expr) loop
4880 Force_Evaluation (Expr);
4890 end Is_Ref_To_Bit_Packed_Array;
4892 --------------------------------
4893 -- Is_Ref_To_Bit_Packed_Slice --
4894 --------------------------------
4896 function Is_Ref_To_Bit_Packed_Slice (N : Node_Id) return Boolean is
4898 if Nkind (N) = N_Type_Conversion then
4899 return Is_Ref_To_Bit_Packed_Slice (Expression (N));
4901 elsif Is_Entity_Name (N)
4902 and then Is_Object (Entity (N))
4903 and then Present (Renamed_Object (Entity (N)))
4905 return Is_Ref_To_Bit_Packed_Slice (Renamed_Object (Entity (N)));
4907 elsif Nkind (N) = N_Slice
4908 and then Is_Bit_Packed_Array (Etype (Prefix (N)))
4912 elsif Nkind (N) = N_Indexed_Component
4914 Nkind (N) = N_Selected_Component
4916 return Is_Ref_To_Bit_Packed_Slice (Prefix (N));
4921 end Is_Ref_To_Bit_Packed_Slice;
4923 -----------------------
4924 -- Is_Renamed_Object --
4925 -----------------------
4927 function Is_Renamed_Object (N : Node_Id) return Boolean is
4928 Pnod : constant Node_Id := Parent (N);
4929 Kind : constant Node_Kind := Nkind (Pnod);
4931 if Kind = N_Object_Renaming_Declaration then
4933 elsif Nkind_In (Kind, N_Indexed_Component, N_Selected_Component) then
4934 return Is_Renamed_Object (Pnod);
4938 end Is_Renamed_Object;
4940 -------------------------------------
4941 -- Is_Tag_To_Class_Wide_Conversion --
4942 -------------------------------------
4944 function Is_Tag_To_Class_Wide_Conversion
4945 (Obj_Id : Entity_Id) return Boolean
4947 Expr : constant Node_Id := Expression (Parent (Obj_Id));
4951 Is_Class_Wide_Type (Etype (Obj_Id))
4952 and then Present (Expr)
4953 and then Nkind (Expr) = N_Unchecked_Type_Conversion
4954 and then Etype (Expression (Expr)) = RTE (RE_Tag);
4955 end Is_Tag_To_Class_Wide_Conversion;
4957 ----------------------------
4958 -- Is_Untagged_Derivation --
4959 ----------------------------
4961 function Is_Untagged_Derivation (T : Entity_Id) return Boolean is
4963 return (not Is_Tagged_Type (T) and then Is_Derived_Type (T))
4965 (Is_Private_Type (T) and then Present (Full_View (T))
4966 and then not Is_Tagged_Type (Full_View (T))
4967 and then Is_Derived_Type (Full_View (T))
4968 and then Etype (Full_View (T)) /= T);
4969 end Is_Untagged_Derivation;
4971 ---------------------------
4972 -- Is_Volatile_Reference --
4973 ---------------------------
4975 function Is_Volatile_Reference (N : Node_Id) return Boolean is
4977 if Nkind (N) in N_Has_Etype
4978 and then Present (Etype (N))
4979 and then Treat_As_Volatile (Etype (N))
4983 elsif Is_Entity_Name (N) then
4984 return Treat_As_Volatile (Entity (N));
4986 elsif Nkind (N) = N_Slice then
4987 return Is_Volatile_Reference (Prefix (N));
4989 elsif Nkind_In (N, N_Indexed_Component, N_Selected_Component) then
4990 if (Is_Entity_Name (Prefix (N))
4991 and then Has_Volatile_Components (Entity (Prefix (N))))
4992 or else (Present (Etype (Prefix (N)))
4993 and then Has_Volatile_Components (Etype (Prefix (N))))
4997 return Is_Volatile_Reference (Prefix (N));
5003 end Is_Volatile_Reference;
5005 --------------------------
5006 -- Is_VM_By_Copy_Actual --
5007 --------------------------
5009 function Is_VM_By_Copy_Actual (N : Node_Id) return Boolean is
5011 return VM_Target /= No_VM
5012 and then (Nkind (N) = N_Slice
5014 (Nkind (N) = N_Identifier
5015 and then Present (Renamed_Object (Entity (N)))
5016 and then Nkind (Renamed_Object (Entity (N)))
5018 end Is_VM_By_Copy_Actual;
5020 --------------------
5021 -- Kill_Dead_Code --
5022 --------------------
5024 procedure Kill_Dead_Code (N : Node_Id; Warn : Boolean := False) is
5025 W : Boolean := Warn;
5026 -- Set False if warnings suppressed
5030 Remove_Warning_Messages (N);
5032 -- Generate warning if appropriate
5036 -- We suppress the warning if this code is under control of an
5037 -- if statement, whose condition is a simple identifier, and
5038 -- either we are in an instance, or warnings off is set for this
5039 -- identifier. The reason for killing it in the instance case is
5040 -- that it is common and reasonable for code to be deleted in
5041 -- instances for various reasons.
5043 if Nkind (Parent (N)) = N_If_Statement then
5045 C : constant Node_Id := Condition (Parent (N));
5047 if Nkind (C) = N_Identifier
5050 or else (Present (Entity (C))
5051 and then Has_Warnings_Off (Entity (C))))
5058 -- Generate warning if not suppressed
5062 ("?this code can never be executed and has been deleted!", N);
5066 -- Recurse into block statements and bodies to process declarations
5069 if Nkind (N) = N_Block_Statement
5070 or else Nkind (N) = N_Subprogram_Body
5071 or else Nkind (N) = N_Package_Body
5073 Kill_Dead_Code (Declarations (N), False);
5074 Kill_Dead_Code (Statements (Handled_Statement_Sequence (N)));
5076 if Nkind (N) = N_Subprogram_Body then
5077 Set_Is_Eliminated (Defining_Entity (N));
5080 elsif Nkind (N) = N_Package_Declaration then
5081 Kill_Dead_Code (Visible_Declarations (Specification (N)));
5082 Kill_Dead_Code (Private_Declarations (Specification (N)));
5084 -- ??? After this point, Delete_Tree has been called on all
5085 -- declarations in Specification (N), so references to entities
5086 -- therein look suspicious.
5089 E : Entity_Id := First_Entity (Defining_Entity (N));
5091 while Present (E) loop
5092 if Ekind (E) = E_Operator then
5093 Set_Is_Eliminated (E);
5100 -- Recurse into composite statement to kill individual statements in
5101 -- particular instantiations.
5103 elsif Nkind (N) = N_If_Statement then
5104 Kill_Dead_Code (Then_Statements (N));
5105 Kill_Dead_Code (Elsif_Parts (N));
5106 Kill_Dead_Code (Else_Statements (N));
5108 elsif Nkind (N) = N_Loop_Statement then
5109 Kill_Dead_Code (Statements (N));
5111 elsif Nkind (N) = N_Case_Statement then
5115 Alt := First (Alternatives (N));
5116 while Present (Alt) loop
5117 Kill_Dead_Code (Statements (Alt));
5122 elsif Nkind (N) = N_Case_Statement_Alternative then
5123 Kill_Dead_Code (Statements (N));
5125 -- Deal with dead instances caused by deleting instantiations
5127 elsif Nkind (N) in N_Generic_Instantiation then
5128 Remove_Dead_Instance (N);
5133 -- Case where argument is a list of nodes to be killed
5135 procedure Kill_Dead_Code (L : List_Id; Warn : Boolean := False) is
5140 if Is_Non_Empty_List (L) then
5142 while Present (N) loop
5143 Kill_Dead_Code (N, W);
5150 ------------------------
5151 -- Known_Non_Negative --
5152 ------------------------
5154 function Known_Non_Negative (Opnd : Node_Id) return Boolean is
5156 if Is_OK_Static_Expression (Opnd)
5157 and then Expr_Value (Opnd) >= 0
5163 Lo : constant Node_Id := Type_Low_Bound (Etype (Opnd));
5167 Is_OK_Static_Expression (Lo) and then Expr_Value (Lo) >= 0;
5170 end Known_Non_Negative;
5172 --------------------
5173 -- Known_Non_Null --
5174 --------------------
5176 function Known_Non_Null (N : Node_Id) return Boolean is
5178 -- Checks for case where N is an entity reference
5180 if Is_Entity_Name (N) and then Present (Entity (N)) then
5182 E : constant Entity_Id := Entity (N);
5187 -- First check if we are in decisive conditional
5189 Get_Current_Value_Condition (N, Op, Val);
5191 if Known_Null (Val) then
5192 if Op = N_Op_Eq then
5194 elsif Op = N_Op_Ne then
5199 -- If OK to do replacement, test Is_Known_Non_Null flag
5201 if OK_To_Do_Constant_Replacement (E) then
5202 return Is_Known_Non_Null (E);
5204 -- Otherwise if not safe to do replacement, then say so
5211 -- True if access attribute
5213 elsif Nkind (N) = N_Attribute_Reference
5214 and then (Attribute_Name (N) = Name_Access
5216 Attribute_Name (N) = Name_Unchecked_Access
5218 Attribute_Name (N) = Name_Unrestricted_Access)
5222 -- True if allocator
5224 elsif Nkind (N) = N_Allocator then
5227 -- For a conversion, true if expression is known non-null
5229 elsif Nkind (N) = N_Type_Conversion then
5230 return Known_Non_Null (Expression (N));
5232 -- Above are all cases where the value could be determined to be
5233 -- non-null. In all other cases, we don't know, so return False.
5244 function Known_Null (N : Node_Id) return Boolean is
5246 -- Checks for case where N is an entity reference
5248 if Is_Entity_Name (N) and then Present (Entity (N)) then
5250 E : constant Entity_Id := Entity (N);
5255 -- Constant null value is for sure null
5257 if Ekind (E) = E_Constant
5258 and then Known_Null (Constant_Value (E))
5263 -- First check if we are in decisive conditional
5265 Get_Current_Value_Condition (N, Op, Val);
5267 if Known_Null (Val) then
5268 if Op = N_Op_Eq then
5270 elsif Op = N_Op_Ne then
5275 -- If OK to do replacement, test Is_Known_Null flag
5277 if OK_To_Do_Constant_Replacement (E) then
5278 return Is_Known_Null (E);
5280 -- Otherwise if not safe to do replacement, then say so
5287 -- True if explicit reference to null
5289 elsif Nkind (N) = N_Null then
5292 -- For a conversion, true if expression is known null
5294 elsif Nkind (N) = N_Type_Conversion then
5295 return Known_Null (Expression (N));
5297 -- Above are all cases where the value could be determined to be null.
5298 -- In all other cases, we don't know, so return False.
5305 -----------------------------
5306 -- Make_CW_Equivalent_Type --
5307 -----------------------------
5309 -- Create a record type used as an equivalent of any member of the class
5310 -- which takes its size from exp.
5312 -- Generate the following code:
5314 -- type Equiv_T is record
5315 -- _parent : T (List of discriminant constraints taken from Exp);
5316 -- Ext__50 : Storage_Array (1 .. (Exp'size - Typ'object_size)/8);
5319 -- ??? Note that this type does not guarantee same alignment as all
5322 function Make_CW_Equivalent_Type
5324 E : Node_Id) return Entity_Id
5326 Loc : constant Source_Ptr := Sloc (E);
5327 Root_Typ : constant Entity_Id := Root_Type (T);
5328 List_Def : constant List_Id := Empty_List;
5329 Comp_List : constant List_Id := New_List;
5330 Equiv_Type : Entity_Id;
5331 Range_Type : Entity_Id;
5332 Str_Type : Entity_Id;
5333 Constr_Root : Entity_Id;
5337 -- If the root type is already constrained, there are no discriminants
5338 -- in the expression.
5340 if not Has_Discriminants (Root_Typ)
5341 or else Is_Constrained (Root_Typ)
5343 Constr_Root := Root_Typ;
5345 Constr_Root := Make_Temporary (Loc, 'R');
5347 -- subtype cstr__n is T (List of discr constraints taken from Exp)
5349 Append_To (List_Def,
5350 Make_Subtype_Declaration (Loc,
5351 Defining_Identifier => Constr_Root,
5352 Subtype_Indication => Make_Subtype_From_Expr (E, Root_Typ)));
5355 -- Generate the range subtype declaration
5357 Range_Type := Make_Temporary (Loc, 'G');
5359 if not Is_Interface (Root_Typ) then
5361 -- subtype rg__xx is
5362 -- Storage_Offset range 1 .. (Expr'size - typ'size) / Storage_Unit
5365 Make_Op_Subtract (Loc,
5367 Make_Attribute_Reference (Loc,
5369 OK_Convert_To (T, Duplicate_Subexpr_No_Checks (E)),
5370 Attribute_Name => Name_Size),
5372 Make_Attribute_Reference (Loc,
5373 Prefix => New_Reference_To (Constr_Root, Loc),
5374 Attribute_Name => Name_Object_Size));
5376 -- subtype rg__xx is
5377 -- Storage_Offset range 1 .. Expr'size / Storage_Unit
5380 Make_Attribute_Reference (Loc,
5382 OK_Convert_To (T, Duplicate_Subexpr_No_Checks (E)),
5383 Attribute_Name => Name_Size);
5386 Set_Paren_Count (Sizexpr, 1);
5388 Append_To (List_Def,
5389 Make_Subtype_Declaration (Loc,
5390 Defining_Identifier => Range_Type,
5391 Subtype_Indication =>
5392 Make_Subtype_Indication (Loc,
5393 Subtype_Mark => New_Reference_To (RTE (RE_Storage_Offset), Loc),
5394 Constraint => Make_Range_Constraint (Loc,
5397 Low_Bound => Make_Integer_Literal (Loc, 1),
5399 Make_Op_Divide (Loc,
5400 Left_Opnd => Sizexpr,
5401 Right_Opnd => Make_Integer_Literal (Loc,
5402 Intval => System_Storage_Unit)))))));
5404 -- subtype str__nn is Storage_Array (rg__x);
5406 Str_Type := Make_Temporary (Loc, 'S');
5407 Append_To (List_Def,
5408 Make_Subtype_Declaration (Loc,
5409 Defining_Identifier => Str_Type,
5410 Subtype_Indication =>
5411 Make_Subtype_Indication (Loc,
5412 Subtype_Mark => New_Reference_To (RTE (RE_Storage_Array), Loc),
5414 Make_Index_Or_Discriminant_Constraint (Loc,
5416 New_List (New_Reference_To (Range_Type, Loc))))));
5418 -- type Equiv_T is record
5419 -- [ _parent : Tnn; ]
5423 Equiv_Type := Make_Temporary (Loc, 'T');
5424 Set_Ekind (Equiv_Type, E_Record_Type);
5425 Set_Parent_Subtype (Equiv_Type, Constr_Root);
5427 -- Set Is_Class_Wide_Equivalent_Type very early to trigger the special
5428 -- treatment for this type. In particular, even though _parent's type
5429 -- is a controlled type or contains controlled components, we do not
5430 -- want to set Has_Controlled_Component on it to avoid making it gain
5431 -- an unwanted _controller component.
5433 Set_Is_Class_Wide_Equivalent_Type (Equiv_Type);
5435 if not Is_Interface (Root_Typ) then
5436 Append_To (Comp_List,
5437 Make_Component_Declaration (Loc,
5438 Defining_Identifier =>
5439 Make_Defining_Identifier (Loc, Name_uParent),
5440 Component_Definition =>
5441 Make_Component_Definition (Loc,
5442 Aliased_Present => False,
5443 Subtype_Indication => New_Reference_To (Constr_Root, Loc))));
5446 Append_To (Comp_List,
5447 Make_Component_Declaration (Loc,
5448 Defining_Identifier => Make_Temporary (Loc, 'C'),
5449 Component_Definition =>
5450 Make_Component_Definition (Loc,
5451 Aliased_Present => False,
5452 Subtype_Indication => New_Reference_To (Str_Type, Loc))));
5454 Append_To (List_Def,
5455 Make_Full_Type_Declaration (Loc,
5456 Defining_Identifier => Equiv_Type,
5458 Make_Record_Definition (Loc,
5460 Make_Component_List (Loc,
5461 Component_Items => Comp_List,
5462 Variant_Part => Empty))));
5464 -- Suppress all checks during the analysis of the expanded code to avoid
5465 -- the generation of spurious warnings under ZFP run-time.
5467 Insert_Actions (E, List_Def, Suppress => All_Checks);
5469 end Make_CW_Equivalent_Type;
5471 -------------------------
5472 -- Make_Invariant_Call --
5473 -------------------------
5475 function Make_Invariant_Call (Expr : Node_Id) return Node_Id is
5476 Loc : constant Source_Ptr := Sloc (Expr);
5477 Typ : constant Entity_Id := Etype (Expr);
5481 (Has_Invariants (Typ) and then Present (Invariant_Procedure (Typ)));
5483 if Check_Enabled (Name_Invariant)
5485 Check_Enabled (Name_Assertion)
5488 Make_Procedure_Call_Statement (Loc,
5490 New_Occurrence_Of (Invariant_Procedure (Typ), Loc),
5491 Parameter_Associations => New_List (Relocate_Node (Expr)));
5495 Make_Null_Statement (Loc);
5497 end Make_Invariant_Call;
5499 ------------------------
5500 -- Make_Literal_Range --
5501 ------------------------
5503 function Make_Literal_Range
5505 Literal_Typ : Entity_Id) return Node_Id
5507 Lo : constant Node_Id :=
5508 New_Copy_Tree (String_Literal_Low_Bound (Literal_Typ));
5509 Index : constant Entity_Id := Etype (Lo);
5512 Length_Expr : constant Node_Id :=
5513 Make_Op_Subtract (Loc,
5515 Make_Integer_Literal (Loc,
5516 Intval => String_Literal_Length (Literal_Typ)),
5518 Make_Integer_Literal (Loc, 1));
5521 Set_Analyzed (Lo, False);
5523 if Is_Integer_Type (Index) then
5526 Left_Opnd => New_Copy_Tree (Lo),
5527 Right_Opnd => Length_Expr);
5530 Make_Attribute_Reference (Loc,
5531 Attribute_Name => Name_Val,
5532 Prefix => New_Occurrence_Of (Index, Loc),
5533 Expressions => New_List (
5536 Make_Attribute_Reference (Loc,
5537 Attribute_Name => Name_Pos,
5538 Prefix => New_Occurrence_Of (Index, Loc),
5539 Expressions => New_List (New_Copy_Tree (Lo))),
5540 Right_Opnd => Length_Expr)));
5547 end Make_Literal_Range;
5549 --------------------------
5550 -- Make_Non_Empty_Check --
5551 --------------------------
5553 function Make_Non_Empty_Check
5555 N : Node_Id) return Node_Id
5561 Make_Attribute_Reference (Loc,
5562 Attribute_Name => Name_Length,
5563 Prefix => Duplicate_Subexpr_No_Checks (N, Name_Req => True)),
5565 Make_Integer_Literal (Loc, 0));
5566 end Make_Non_Empty_Check;
5568 -------------------------
5569 -- Make_Predicate_Call --
5570 -------------------------
5572 function Make_Predicate_Call
5574 Expr : Node_Id) return Node_Id
5576 Loc : constant Source_Ptr := Sloc (Expr);
5579 pragma Assert (Present (Predicate_Function (Typ)));
5582 Make_Function_Call (Loc,
5584 New_Occurrence_Of (Predicate_Function (Typ), Loc),
5585 Parameter_Associations => New_List (Relocate_Node (Expr)));
5586 end Make_Predicate_Call;
5588 --------------------------
5589 -- Make_Predicate_Check --
5590 --------------------------
5592 function Make_Predicate_Check
5594 Expr : Node_Id) return Node_Id
5596 Loc : constant Source_Ptr := Sloc (Expr);
5601 Pragma_Identifier => Make_Identifier (Loc, Name_Check),
5602 Pragma_Argument_Associations => New_List (
5603 Make_Pragma_Argument_Association (Loc,
5604 Expression => Make_Identifier (Loc, Name_Predicate)),
5605 Make_Pragma_Argument_Association (Loc,
5606 Expression => Make_Predicate_Call (Typ, Expr))));
5607 end Make_Predicate_Check;
5609 ----------------------------
5610 -- Make_Subtype_From_Expr --
5611 ----------------------------
5613 -- 1. If Expr is an unconstrained array expression, creates
5614 -- Unc_Type(Expr'first(1)..Expr'last(1),..., Expr'first(n)..Expr'last(n))
5616 -- 2. If Expr is a unconstrained discriminated type expression, creates
5617 -- Unc_Type(Expr.Discr1, ... , Expr.Discr_n)
5619 -- 3. If Expr is class-wide, creates an implicit class wide subtype
5621 function Make_Subtype_From_Expr
5623 Unc_Typ : Entity_Id) return Node_Id
5625 Loc : constant Source_Ptr := Sloc (E);
5626 List_Constr : constant List_Id := New_List;
5629 Full_Subtyp : Entity_Id;
5630 Priv_Subtyp : Entity_Id;
5635 if Is_Private_Type (Unc_Typ)
5636 and then Has_Unknown_Discriminants (Unc_Typ)
5638 -- Prepare the subtype completion, Go to base type to
5639 -- find underlying type, because the type may be a generic
5640 -- actual or an explicit subtype.
5642 Utyp := Underlying_Type (Base_Type (Unc_Typ));
5643 Full_Subtyp := Make_Temporary (Loc, 'C');
5645 Unchecked_Convert_To (Utyp, Duplicate_Subexpr_No_Checks (E));
5646 Set_Parent (Full_Exp, Parent (E));
5648 Priv_Subtyp := Make_Temporary (Loc, 'P');
5651 Make_Subtype_Declaration (Loc,
5652 Defining_Identifier => Full_Subtyp,
5653 Subtype_Indication => Make_Subtype_From_Expr (Full_Exp, Utyp)));
5655 -- Define the dummy private subtype
5657 Set_Ekind (Priv_Subtyp, Subtype_Kind (Ekind (Unc_Typ)));
5658 Set_Etype (Priv_Subtyp, Base_Type (Unc_Typ));
5659 Set_Scope (Priv_Subtyp, Full_Subtyp);
5660 Set_Is_Constrained (Priv_Subtyp);
5661 Set_Is_Tagged_Type (Priv_Subtyp, Is_Tagged_Type (Unc_Typ));
5662 Set_Is_Itype (Priv_Subtyp);
5663 Set_Associated_Node_For_Itype (Priv_Subtyp, E);
5665 if Is_Tagged_Type (Priv_Subtyp) then
5667 (Base_Type (Priv_Subtyp), Class_Wide_Type (Unc_Typ));
5668 Set_Direct_Primitive_Operations (Priv_Subtyp,
5669 Direct_Primitive_Operations (Unc_Typ));
5672 Set_Full_View (Priv_Subtyp, Full_Subtyp);
5674 return New_Reference_To (Priv_Subtyp, Loc);
5676 elsif Is_Array_Type (Unc_Typ) then
5677 for J in 1 .. Number_Dimensions (Unc_Typ) loop
5678 Append_To (List_Constr,
5681 Make_Attribute_Reference (Loc,
5682 Prefix => Duplicate_Subexpr_No_Checks (E),
5683 Attribute_Name => Name_First,
5684 Expressions => New_List (
5685 Make_Integer_Literal (Loc, J))),
5688 Make_Attribute_Reference (Loc,
5689 Prefix => Duplicate_Subexpr_No_Checks (E),
5690 Attribute_Name => Name_Last,
5691 Expressions => New_List (
5692 Make_Integer_Literal (Loc, J)))));
5695 elsif Is_Class_Wide_Type (Unc_Typ) then
5697 CW_Subtype : Entity_Id;
5698 EQ_Typ : Entity_Id := Empty;
5701 -- A class-wide equivalent type is not needed when VM_Target
5702 -- because the VM back-ends handle the class-wide object
5703 -- initialization itself (and doesn't need or want the
5704 -- additional intermediate type to handle the assignment).
5706 if Expander_Active and then Tagged_Type_Expansion then
5708 -- If this is the class_wide type of a completion that is a
5709 -- record subtype, set the type of the class_wide type to be
5710 -- the full base type, for use in the expanded code for the
5711 -- equivalent type. Should this be done earlier when the
5712 -- completion is analyzed ???
5714 if Is_Private_Type (Etype (Unc_Typ))
5716 Ekind (Full_View (Etype (Unc_Typ))) = E_Record_Subtype
5718 Set_Etype (Unc_Typ, Base_Type (Full_View (Etype (Unc_Typ))));
5721 EQ_Typ := Make_CW_Equivalent_Type (Unc_Typ, E);
5724 CW_Subtype := New_Class_Wide_Subtype (Unc_Typ, E);
5725 Set_Equivalent_Type (CW_Subtype, EQ_Typ);
5726 Set_Cloned_Subtype (CW_Subtype, Base_Type (Unc_Typ));
5728 return New_Occurrence_Of (CW_Subtype, Loc);
5731 -- Indefinite record type with discriminants
5734 D := First_Discriminant (Unc_Typ);
5735 while Present (D) loop
5736 Append_To (List_Constr,
5737 Make_Selected_Component (Loc,
5738 Prefix => Duplicate_Subexpr_No_Checks (E),
5739 Selector_Name => New_Reference_To (D, Loc)));
5741 Next_Discriminant (D);
5746 Make_Subtype_Indication (Loc,
5747 Subtype_Mark => New_Reference_To (Unc_Typ, Loc),
5749 Make_Index_Or_Discriminant_Constraint (Loc,
5750 Constraints => List_Constr));
5751 end Make_Subtype_From_Expr;
5753 -----------------------------
5754 -- May_Generate_Large_Temp --
5755 -----------------------------
5757 -- At the current time, the only types that we return False for (i.e. where
5758 -- we decide we know they cannot generate large temps) are ones where we
5759 -- know the size is 256 bits or less at compile time, and we are still not
5760 -- doing a thorough job on arrays and records ???
5762 function May_Generate_Large_Temp (Typ : Entity_Id) return Boolean is
5764 if not Size_Known_At_Compile_Time (Typ) then
5767 elsif Esize (Typ) /= 0 and then Esize (Typ) <= 256 then
5770 elsif Is_Array_Type (Typ)
5771 and then Present (Packed_Array_Type (Typ))
5773 return May_Generate_Large_Temp (Packed_Array_Type (Typ));
5775 -- We could do more here to find other small types ???
5780 end May_Generate_Large_Temp;
5782 ------------------------
5783 -- Needs_Finalization --
5784 ------------------------
5786 function Needs_Finalization (T : Entity_Id) return Boolean is
5787 function Has_Some_Controlled_Component (Rec : Entity_Id) return Boolean;
5788 -- If type is not frozen yet, check explicitly among its components,
5789 -- because the Has_Controlled_Component flag is not necessarily set.
5791 -----------------------------------
5792 -- Has_Some_Controlled_Component --
5793 -----------------------------------
5795 function Has_Some_Controlled_Component
5796 (Rec : Entity_Id) return Boolean
5801 if Has_Controlled_Component (Rec) then
5804 elsif not Is_Frozen (Rec) then
5805 if Is_Record_Type (Rec) then
5806 Comp := First_Entity (Rec);
5808 while Present (Comp) loop
5809 if not Is_Type (Comp)
5810 and then Needs_Finalization (Etype (Comp))
5820 elsif Is_Array_Type (Rec) then
5821 return Needs_Finalization (Component_Type (Rec));
5824 return Has_Controlled_Component (Rec);
5829 end Has_Some_Controlled_Component;
5831 -- Start of processing for Needs_Finalization
5834 -- Certain run-time configurations and targets do not provide support
5835 -- for controlled types.
5837 if Restriction_Active (No_Finalization) then
5840 -- C, C++, CIL and Java types are not considered controlled. It is
5841 -- assumed that the non-Ada side will handle their clean up.
5843 elsif Convention (T) = Convention_C
5844 or else Convention (T) = Convention_CIL
5845 or else Convention (T) = Convention_CPP
5846 or else Convention (T) = Convention_Java
5851 -- Class-wide types are treated as controlled because derivations
5852 -- from the root type can introduce controlled components.
5855 Is_Class_Wide_Type (T)
5856 or else Is_Controlled (T)
5857 or else Has_Controlled_Component (T)
5858 or else Has_Some_Controlled_Component (T)
5860 (Is_Concurrent_Type (T)
5861 and then Present (Corresponding_Record_Type (T))
5862 and then Needs_Finalization (Corresponding_Record_Type (T)));
5864 end Needs_Finalization;
5866 ----------------------------
5867 -- Needs_Constant_Address --
5868 ----------------------------
5870 function Needs_Constant_Address
5872 Typ : Entity_Id) return Boolean
5876 -- If we have no initialization of any kind, then we don't need to place
5877 -- any restrictions on the address clause, because the object will be
5878 -- elaborated after the address clause is evaluated. This happens if the
5879 -- declaration has no initial expression, or the type has no implicit
5880 -- initialization, or the object is imported.
5882 -- The same holds for all initialized scalar types and all access types.
5883 -- Packed bit arrays of size up to 64 are represented using a modular
5884 -- type with an initialization (to zero) and can be processed like other
5885 -- initialized scalar types.
5887 -- If the type is controlled, code to attach the object to a
5888 -- finalization chain is generated at the point of declaration, and
5889 -- therefore the elaboration of the object cannot be delayed: the
5890 -- address expression must be a constant.
5892 if No (Expression (Decl))
5893 and then not Needs_Finalization (Typ)
5895 (not Has_Non_Null_Base_Init_Proc (Typ)
5896 or else Is_Imported (Defining_Identifier (Decl)))
5900 elsif (Present (Expression (Decl)) and then Is_Scalar_Type (Typ))
5901 or else Is_Access_Type (Typ)
5903 (Is_Bit_Packed_Array (Typ)
5904 and then Is_Modular_Integer_Type (Packed_Array_Type (Typ)))
5910 -- Otherwise, we require the address clause to be constant because
5911 -- the call to the initialization procedure (or the attach code) has
5912 -- to happen at the point of the declaration.
5914 -- Actually the IP call has been moved to the freeze actions anyway,
5915 -- so maybe we can relax this restriction???
5919 end Needs_Constant_Address;
5921 ----------------------------
5922 -- New_Class_Wide_Subtype --
5923 ----------------------------
5925 function New_Class_Wide_Subtype
5926 (CW_Typ : Entity_Id;
5927 N : Node_Id) return Entity_Id
5929 Res : constant Entity_Id := Create_Itype (E_Void, N);
5930 Res_Name : constant Name_Id := Chars (Res);
5931 Res_Scope : constant Entity_Id := Scope (Res);
5934 Copy_Node (CW_Typ, Res);
5935 Set_Comes_From_Source (Res, False);
5936 Set_Sloc (Res, Sloc (N));
5938 Set_Associated_Node_For_Itype (Res, N);
5939 Set_Is_Public (Res, False); -- By default, may be changed below.
5940 Set_Public_Status (Res);
5941 Set_Chars (Res, Res_Name);
5942 Set_Scope (Res, Res_Scope);
5943 Set_Ekind (Res, E_Class_Wide_Subtype);
5944 Set_Next_Entity (Res, Empty);
5945 Set_Etype (Res, Base_Type (CW_Typ));
5946 Set_Is_Frozen (Res, False);
5947 Set_Freeze_Node (Res, Empty);
5949 end New_Class_Wide_Subtype;
5951 --------------------------------
5952 -- Non_Limited_Designated_Type --
5953 ---------------------------------
5955 function Non_Limited_Designated_Type (T : Entity_Id) return Entity_Id is
5956 Desig : constant Entity_Id := Designated_Type (T);
5958 if Ekind (Desig) = E_Incomplete_Type
5959 and then Present (Non_Limited_View (Desig))
5961 return Non_Limited_View (Desig);
5965 end Non_Limited_Designated_Type;
5967 -----------------------------------
5968 -- OK_To_Do_Constant_Replacement --
5969 -----------------------------------
5971 function OK_To_Do_Constant_Replacement (E : Entity_Id) return Boolean is
5972 ES : constant Entity_Id := Scope (E);
5976 -- Do not replace statically allocated objects, because they may be
5977 -- modified outside the current scope.
5979 if Is_Statically_Allocated (E) then
5982 -- Do not replace aliased or volatile objects, since we don't know what
5983 -- else might change the value.
5985 elsif Is_Aliased (E) or else Treat_As_Volatile (E) then
5988 -- Debug flag -gnatdM disconnects this optimization
5990 elsif Debug_Flag_MM then
5993 -- Otherwise check scopes
5996 CS := Current_Scope;
5999 -- If we are in right scope, replacement is safe
6004 -- Packages do not affect the determination of safety
6006 elsif Ekind (CS) = E_Package then
6007 exit when CS = Standard_Standard;
6010 -- Blocks do not affect the determination of safety
6012 elsif Ekind (CS) = E_Block then
6015 -- Loops do not affect the determination of safety. Note that we
6016 -- kill all current values on entry to a loop, so we are just
6017 -- talking about processing within a loop here.
6019 elsif Ekind (CS) = E_Loop then
6022 -- Otherwise, the reference is dubious, and we cannot be sure that
6023 -- it is safe to do the replacement.
6032 end OK_To_Do_Constant_Replacement;
6034 ------------------------------------
6035 -- Possible_Bit_Aligned_Component --
6036 ------------------------------------
6038 function Possible_Bit_Aligned_Component (N : Node_Id) return Boolean is
6042 -- Case of indexed component
6044 when N_Indexed_Component =>
6046 P : constant Node_Id := Prefix (N);
6047 Ptyp : constant Entity_Id := Etype (P);
6050 -- If we know the component size and it is less than 64, then
6051 -- we are definitely OK. The back end always does assignment of
6052 -- misaligned small objects correctly.
6054 if Known_Static_Component_Size (Ptyp)
6055 and then Component_Size (Ptyp) <= 64
6059 -- Otherwise, we need to test the prefix, to see if we are
6060 -- indexing from a possibly unaligned component.
6063 return Possible_Bit_Aligned_Component (P);
6067 -- Case of selected component
6069 when N_Selected_Component =>
6071 P : constant Node_Id := Prefix (N);
6072 Comp : constant Entity_Id := Entity (Selector_Name (N));
6075 -- If there is no component clause, then we are in the clear
6076 -- since the back end will never misalign a large component
6077 -- unless it is forced to do so. In the clear means we need
6078 -- only the recursive test on the prefix.
6080 if Component_May_Be_Bit_Aligned (Comp) then
6083 return Possible_Bit_Aligned_Component (P);
6087 -- For a slice, test the prefix, if that is possibly misaligned,
6088 -- then for sure the slice is!
6091 return Possible_Bit_Aligned_Component (Prefix (N));
6093 -- For an unchecked conversion, check whether the expression may
6096 when N_Unchecked_Type_Conversion =>
6097 return Possible_Bit_Aligned_Component (Expression (N));
6099 -- If we have none of the above, it means that we have fallen off the
6100 -- top testing prefixes recursively, and we now have a stand alone
6101 -- object, where we don't have a problem.
6107 end Possible_Bit_Aligned_Component;
6109 -----------------------------------------------
6110 -- Process_Statements_For_Controlled_Objects --
6111 -----------------------------------------------
6113 procedure Process_Statements_For_Controlled_Objects (N : Node_Id) is
6114 Loc : constant Source_Ptr := Sloc (N);
6116 function Are_Wrapped (L : List_Id) return Boolean;
6117 -- Determine whether list L contains only one statement which is a block
6119 function Wrap_Statements_In_Block (L : List_Id) return Node_Id;
6120 -- Given a list of statements L, wrap it in a block statement and return
6121 -- the generated node.
6127 function Are_Wrapped (L : List_Id) return Boolean is
6128 Stmt : constant Node_Id := First (L);
6132 and then No (Next (Stmt))
6133 and then Nkind (Stmt) = N_Block_Statement;
6136 ------------------------------
6137 -- Wrap_Statements_In_Block --
6138 ------------------------------
6140 function Wrap_Statements_In_Block (L : List_Id) return Node_Id is
6143 Make_Block_Statement (Loc,
6144 Declarations => No_List,
6145 Handled_Statement_Sequence =>
6146 Make_Handled_Sequence_Of_Statements (Loc,
6148 end Wrap_Statements_In_Block;
6154 -- Start of processing for Process_Statements_For_Controlled_Objects
6157 -- Whenever a non-handled statement list is wrapped in a block, the
6158 -- block must be explicitly analyzed to redecorate all entities in the
6159 -- list and ensure that a finalizer is properly built.
6164 N_Conditional_Entry_Call |
6165 N_Selective_Accept =>
6167 -- Check the "then statements" for elsif parts and if statements
6169 if Nkind_In (N, N_Elsif_Part, N_If_Statement)
6170 and then not Is_Empty_List (Then_Statements (N))
6171 and then not Are_Wrapped (Then_Statements (N))
6172 and then Requires_Cleanup_Actions
6173 (Then_Statements (N), False, False)
6175 Block := Wrap_Statements_In_Block (Then_Statements (N));
6176 Set_Then_Statements (N, New_List (Block));
6181 -- Check the "else statements" for conditional entry calls, if
6182 -- statements and selective accepts.
6184 if Nkind_In (N, N_Conditional_Entry_Call,
6187 and then not Is_Empty_List (Else_Statements (N))
6188 and then not Are_Wrapped (Else_Statements (N))
6189 and then Requires_Cleanup_Actions
6190 (Else_Statements (N), False, False)
6192 Block := Wrap_Statements_In_Block (Else_Statements (N));
6193 Set_Else_Statements (N, New_List (Block));
6198 when N_Abortable_Part |
6199 N_Accept_Alternative |
6200 N_Case_Statement_Alternative |
6201 N_Delay_Alternative |
6202 N_Entry_Call_Alternative |
6203 N_Exception_Handler |
6205 N_Triggering_Alternative =>
6207 if not Is_Empty_List (Statements (N))
6208 and then not Are_Wrapped (Statements (N))
6209 and then Requires_Cleanup_Actions (Statements (N), False, False)
6211 Block := Wrap_Statements_In_Block (Statements (N));
6212 Set_Statements (N, New_List (Block));
6220 end Process_Statements_For_Controlled_Objects;
6222 -------------------------
6223 -- Remove_Side_Effects --
6224 -------------------------
6226 procedure Remove_Side_Effects
6228 Name_Req : Boolean := False;
6229 Variable_Ref : Boolean := False)
6231 Loc : constant Source_Ptr := Sloc (Exp);
6232 Exp_Type : constant Entity_Id := Etype (Exp);
6233 Svg_Suppress : constant Suppress_Array := Scope_Suppress;
6237 Ptr_Typ_Decl : Node_Id;
6238 Ref_Type : Entity_Id;
6241 function Side_Effect_Free (N : Node_Id) return Boolean;
6242 -- Determines if the tree N represents an expression that is known not
6243 -- to have side effects, and for which no processing is required.
6245 function Side_Effect_Free (L : List_Id) return Boolean;
6246 -- Determines if all elements of the list L are side effect free
6248 function Safe_Prefixed_Reference (N : Node_Id) return Boolean;
6249 -- The argument N is a construct where the Prefix is dereferenced if it
6250 -- is an access type and the result is a variable. The call returns True
6251 -- if the construct is side effect free (not considering side effects in
6252 -- other than the prefix which are to be tested by the caller).
6254 function Within_In_Parameter (N : Node_Id) return Boolean;
6255 -- Determines if N is a subcomponent of a composite in-parameter. If so,
6256 -- N is not side-effect free when the actual is global and modifiable
6257 -- indirectly from within a subprogram, because it may be passed by
6258 -- reference. The front-end must be conservative here and assume that
6259 -- this may happen with any array or record type. On the other hand, we
6260 -- cannot create temporaries for all expressions for which this
6261 -- condition is true, for various reasons that might require clearing up
6262 -- ??? For example, discriminant references that appear out of place, or
6263 -- spurious type errors with class-wide expressions. As a result, we
6264 -- limit the transformation to loop bounds, which is so far the only
6265 -- case that requires it.
6267 -----------------------------
6268 -- Safe_Prefixed_Reference --
6269 -----------------------------
6271 function Safe_Prefixed_Reference (N : Node_Id) return Boolean is
6273 -- If prefix is not side effect free, definitely not safe
6275 if not Side_Effect_Free (Prefix (N)) then
6278 -- If the prefix is of an access type that is not access-to-constant,
6279 -- then this construct is a variable reference, which means it is to
6280 -- be considered to have side effects if Variable_Ref is set True.
6282 elsif Is_Access_Type (Etype (Prefix (N)))
6283 and then not Is_Access_Constant (Etype (Prefix (N)))
6284 and then Variable_Ref
6286 -- Exception is a prefix that is the result of a previous removal
6289 return Is_Entity_Name (Prefix (N))
6290 and then not Comes_From_Source (Prefix (N))
6291 and then Ekind (Entity (Prefix (N))) = E_Constant
6292 and then Is_Internal_Name (Chars (Entity (Prefix (N))));
6294 -- If the prefix is an explicit dereference then this construct is a
6295 -- variable reference, which means it is to be considered to have
6296 -- side effects if Variable_Ref is True.
6298 -- We do NOT exclude dereferences of access-to-constant types because
6299 -- we handle them as constant view of variables.
6301 elsif Nkind (Prefix (N)) = N_Explicit_Dereference
6302 and then Variable_Ref
6306 -- Note: The following test is the simplest way of solving a complex
6307 -- problem uncovered by the following test (Side effect on loop bound
6308 -- that is a subcomponent of a global variable:
6310 -- with Text_Io; use Text_Io;
6311 -- procedure Tloop is
6314 -- V : Natural := 4;
6315 -- S : String (1..5) := (others => 'a');
6322 -- with procedure Action;
6323 -- procedure Loop_G (Arg : X; Msg : String)
6325 -- procedure Loop_G (Arg : X; Msg : String) is
6327 -- Put_Line ("begin loop_g " & Msg & " will loop till: "
6328 -- & Natural'Image (Arg.V));
6329 -- for Index in 1 .. Arg.V loop
6331 -- (Natural'Image (Index) & " " & Arg.S (Index));
6332 -- if Index > 2 then
6336 -- Put_Line ("end loop_g " & Msg);
6339 -- procedure Loop1 is new Loop_G (Modi);
6340 -- procedure Modi is
6343 -- Loop1 (X1, "from modi");
6347 -- Loop1 (X1, "initial");
6350 -- The output of the above program should be:
6352 -- begin loop_g initial will loop till: 4
6356 -- begin loop_g from modi will loop till: 1
6358 -- end loop_g from modi
6360 -- begin loop_g from modi will loop till: 1
6362 -- end loop_g from modi
6363 -- end loop_g initial
6365 -- If a loop bound is a subcomponent of a global variable, a
6366 -- modification of that variable within the loop may incorrectly
6367 -- affect the execution of the loop.
6369 elsif Nkind (Parent (Parent (N))) = N_Loop_Parameter_Specification
6370 and then Within_In_Parameter (Prefix (N))
6371 and then Variable_Ref
6375 -- All other cases are side effect free
6380 end Safe_Prefixed_Reference;
6382 ----------------------
6383 -- Side_Effect_Free --
6384 ----------------------
6386 function Side_Effect_Free (N : Node_Id) return Boolean is
6388 -- Note on checks that could raise Constraint_Error. Strictly, if we
6389 -- take advantage of 11.6, these checks do not count as side effects.
6390 -- However, we would prefer to consider that they are side effects,
6391 -- since the backend CSE does not work very well on expressions which
6392 -- can raise Constraint_Error. On the other hand if we don't consider
6393 -- them to be side effect free, then we get some awkward expansions
6394 -- in -gnato mode, resulting in code insertions at a point where we
6395 -- do not have a clear model for performing the insertions.
6397 -- Special handling for entity names
6399 if Is_Entity_Name (N) then
6401 -- Variables are considered to be a side effect if Variable_Ref
6402 -- is set or if we have a volatile reference and Name_Req is off.
6403 -- If Name_Req is True then we can't help returning a name which
6404 -- effectively allows multiple references in any case.
6406 if Is_Variable (N, Use_Original_Node => False) then
6407 return not Variable_Ref
6408 and then (not Is_Volatile_Reference (N) or else Name_Req);
6410 -- Any other entity (e.g. a subtype name) is definitely side
6417 -- A value known at compile time is always side effect free
6419 elsif Compile_Time_Known_Value (N) then
6422 -- A variable renaming is not side-effect free, because the renaming
6423 -- will function like a macro in the front-end in some cases, and an
6424 -- assignment can modify the component designated by N, so we need to
6425 -- create a temporary for it.
6427 -- The guard testing for Entity being present is needed at least in
6428 -- the case of rewritten predicate expressions, and may well also be
6429 -- appropriate elsewhere. Obviously we can't go testing the entity
6430 -- field if it does not exist, so it's reasonable to say that this is
6431 -- not the renaming case if it does not exist.
6433 elsif Is_Entity_Name (Original_Node (N))
6434 and then Present (Entity (Original_Node (N)))
6435 and then Is_Renaming_Of_Object (Entity (Original_Node (N)))
6436 and then Ekind (Entity (Original_Node (N))) /= E_Constant
6440 -- Remove_Side_Effects generates an object renaming declaration to
6441 -- capture the expression of a class-wide expression. In VM targets
6442 -- the frontend performs no expansion for dispatching calls to
6443 -- class- wide types since they are handled by the VM. Hence, we must
6444 -- locate here if this node corresponds to a previous invocation of
6445 -- Remove_Side_Effects to avoid a never ending loop in the frontend.
6447 elsif VM_Target /= No_VM
6448 and then not Comes_From_Source (N)
6449 and then Nkind (Parent (N)) = N_Object_Renaming_Declaration
6450 and then Is_Class_Wide_Type (Etype (N))
6455 -- For other than entity names and compile time known values,
6456 -- check the node kind for special processing.
6460 -- An attribute reference is side effect free if its expressions
6461 -- are side effect free and its prefix is side effect free or
6462 -- is an entity reference.
6464 -- Is this right? what about x'first where x is a variable???
6466 when N_Attribute_Reference =>
6467 return Side_Effect_Free (Expressions (N))
6468 and then Attribute_Name (N) /= Name_Input
6469 and then (Is_Entity_Name (Prefix (N))
6470 or else Side_Effect_Free (Prefix (N)));
6472 -- A binary operator is side effect free if and both operands are
6473 -- side effect free. For this purpose binary operators include
6474 -- membership tests and short circuit forms.
6476 when N_Binary_Op | N_Membership_Test | N_Short_Circuit =>
6477 return Side_Effect_Free (Left_Opnd (N))
6479 Side_Effect_Free (Right_Opnd (N));
6481 -- An explicit dereference is side effect free only if it is
6482 -- a side effect free prefixed reference.
6484 when N_Explicit_Dereference =>
6485 return Safe_Prefixed_Reference (N);
6487 -- A call to _rep_to_pos is side effect free, since we generate
6488 -- this pure function call ourselves. Moreover it is critically
6489 -- important to make this exception, since otherwise we can have
6490 -- discriminants in array components which don't look side effect
6491 -- free in the case of an array whose index type is an enumeration
6492 -- type with an enumeration rep clause.
6494 -- All other function calls are not side effect free
6496 when N_Function_Call =>
6497 return Nkind (Name (N)) = N_Identifier
6498 and then Is_TSS (Name (N), TSS_Rep_To_Pos)
6500 Side_Effect_Free (First (Parameter_Associations (N)));
6502 -- An indexed component is side effect free if it is a side
6503 -- effect free prefixed reference and all the indexing
6504 -- expressions are side effect free.
6506 when N_Indexed_Component =>
6507 return Side_Effect_Free (Expressions (N))
6508 and then Safe_Prefixed_Reference (N);
6510 -- A type qualification is side effect free if the expression
6511 -- is side effect free.
6513 when N_Qualified_Expression =>
6514 return Side_Effect_Free (Expression (N));
6516 -- A selected component is side effect free only if it is a side
6517 -- effect free prefixed reference. If it designates a component
6518 -- with a rep. clause it must be treated has having a potential
6519 -- side effect, because it may be modified through a renaming, and
6520 -- a subsequent use of the renaming as a macro will yield the
6521 -- wrong value. This complex interaction between renaming and
6522 -- removing side effects is a reminder that the latter has become
6523 -- a headache to maintain, and that it should be removed in favor
6524 -- of the gcc mechanism to capture values ???
6526 when N_Selected_Component =>
6527 if Nkind (Parent (N)) = N_Explicit_Dereference
6528 and then Has_Non_Standard_Rep (Designated_Type (Etype (N)))
6532 return Safe_Prefixed_Reference (N);
6535 -- A range is side effect free if the bounds are side effect free
6538 return Side_Effect_Free (Low_Bound (N))
6539 and then Side_Effect_Free (High_Bound (N));
6541 -- A slice is side effect free if it is a side effect free
6542 -- prefixed reference and the bounds are side effect free.
6545 return Side_Effect_Free (Discrete_Range (N))
6546 and then Safe_Prefixed_Reference (N);
6548 -- A type conversion is side effect free if the expression to be
6549 -- converted is side effect free.
6551 when N_Type_Conversion =>
6552 return Side_Effect_Free (Expression (N));
6554 -- A unary operator is side effect free if the operand
6555 -- is side effect free.
6558 return Side_Effect_Free (Right_Opnd (N));
6560 -- An unchecked type conversion is side effect free only if it
6561 -- is safe and its argument is side effect free.
6563 when N_Unchecked_Type_Conversion =>
6564 return Safe_Unchecked_Type_Conversion (N)
6565 and then Side_Effect_Free (Expression (N));
6567 -- An unchecked expression is side effect free if its expression
6568 -- is side effect free.
6570 when N_Unchecked_Expression =>
6571 return Side_Effect_Free (Expression (N));
6573 -- A literal is side effect free
6575 when N_Character_Literal |
6581 -- We consider that anything else has side effects. This is a bit
6582 -- crude, but we are pretty close for most common cases, and we
6583 -- are certainly correct (i.e. we never return True when the
6584 -- answer should be False).
6589 end Side_Effect_Free;
6591 -- A list is side effect free if all elements of the list are side
6594 function Side_Effect_Free (L : List_Id) return Boolean is
6598 if L = No_List or else L = Error_List then
6603 while Present (N) loop
6604 if not Side_Effect_Free (N) then
6613 end Side_Effect_Free;
6615 -------------------------
6616 -- Within_In_Parameter --
6617 -------------------------
6619 function Within_In_Parameter (N : Node_Id) return Boolean is
6621 if not Comes_From_Source (N) then
6624 elsif Is_Entity_Name (N) then
6625 return Ekind (Entity (N)) = E_In_Parameter;
6627 elsif Nkind (N) = N_Indexed_Component
6628 or else Nkind (N) = N_Selected_Component
6630 return Within_In_Parameter (Prefix (N));
6635 end Within_In_Parameter;
6637 -- Start of processing for Remove_Side_Effects
6640 -- Handle cases in which there is nothing to do
6642 if not Expander_Active then
6646 -- Cannot generate temporaries if the invocation to remove side effects
6647 -- was issued too early and the type of the expression is not resolved
6648 -- (this happens because routines Duplicate_Subexpr_XX implicitly invoke
6649 -- Remove_Side_Effects).
6652 or else Ekind (Exp_Type) = E_Access_Attribute_Type
6656 -- No action needed for side-effect free expressions
6658 elsif Side_Effect_Free (Exp) then
6662 -- All this must not have any checks
6664 Scope_Suppress := (others => True);
6666 -- If it is a scalar type and we need to capture the value, just make
6667 -- a copy. Likewise for a function call, an attribute reference, an
6668 -- allocator, or an operator. And if we have a volatile reference and
6669 -- Name_Req is not set (see comments above for Side_Effect_Free).
6671 if Is_Elementary_Type (Exp_Type)
6672 and then (Variable_Ref
6673 or else Nkind (Exp) = N_Function_Call
6674 or else Nkind (Exp) = N_Attribute_Reference
6675 or else Nkind (Exp) = N_Allocator
6676 or else Nkind (Exp) in N_Op
6677 or else (not Name_Req and then Is_Volatile_Reference (Exp)))
6679 Def_Id := Make_Temporary (Loc, 'R', Exp);
6680 Set_Etype (Def_Id, Exp_Type);
6681 Res := New_Reference_To (Def_Id, Loc);
6683 -- If the expression is a packed reference, it must be reanalyzed and
6684 -- expanded, depending on context. This is the case for actuals where
6685 -- a constraint check may capture the actual before expansion of the
6686 -- call is complete.
6688 if Nkind (Exp) = N_Indexed_Component
6689 and then Is_Packed (Etype (Prefix (Exp)))
6691 Set_Analyzed (Exp, False);
6692 Set_Analyzed (Prefix (Exp), False);
6696 Make_Object_Declaration (Loc,
6697 Defining_Identifier => Def_Id,
6698 Object_Definition => New_Reference_To (Exp_Type, Loc),
6699 Constant_Present => True,
6700 Expression => Relocate_Node (Exp));
6702 Set_Assignment_OK (E);
6703 Insert_Action (Exp, E);
6705 -- If the expression has the form v.all then we can just capture the
6706 -- pointer, and then do an explicit dereference on the result.
6708 elsif Nkind (Exp) = N_Explicit_Dereference then
6709 Def_Id := Make_Temporary (Loc, 'R', Exp);
6711 Make_Explicit_Dereference (Loc, New_Reference_To (Def_Id, Loc));
6714 Make_Object_Declaration (Loc,
6715 Defining_Identifier => Def_Id,
6716 Object_Definition =>
6717 New_Reference_To (Etype (Prefix (Exp)), Loc),
6718 Constant_Present => True,
6719 Expression => Relocate_Node (Prefix (Exp))));
6721 -- Similar processing for an unchecked conversion of an expression of
6722 -- the form v.all, where we want the same kind of treatment.
6724 elsif Nkind (Exp) = N_Unchecked_Type_Conversion
6725 and then Nkind (Expression (Exp)) = N_Explicit_Dereference
6727 Remove_Side_Effects (Expression (Exp), Name_Req, Variable_Ref);
6728 Scope_Suppress := Svg_Suppress;
6731 -- If this is a type conversion, leave the type conversion and remove
6732 -- the side effects in the expression. This is important in several
6733 -- circumstances: for change of representations, and also when this is a
6734 -- view conversion to a smaller object, where gigi can end up creating
6735 -- its own temporary of the wrong size.
6737 elsif Nkind (Exp) = N_Type_Conversion then
6738 Remove_Side_Effects (Expression (Exp), Name_Req, Variable_Ref);
6739 Scope_Suppress := Svg_Suppress;
6742 -- If this is an unchecked conversion that Gigi can't handle, make
6743 -- a copy or a use a renaming to capture the value.
6745 elsif Nkind (Exp) = N_Unchecked_Type_Conversion
6746 and then not Safe_Unchecked_Type_Conversion (Exp)
6748 if CW_Or_Has_Controlled_Part (Exp_Type) then
6750 -- Use a renaming to capture the expression, rather than create
6751 -- a controlled temporary.
6753 Def_Id := Make_Temporary (Loc, 'R', Exp);
6754 Res := New_Reference_To (Def_Id, Loc);
6757 Make_Object_Renaming_Declaration (Loc,
6758 Defining_Identifier => Def_Id,
6759 Subtype_Mark => New_Reference_To (Exp_Type, Loc),
6760 Name => Relocate_Node (Exp)));
6763 Def_Id := Make_Temporary (Loc, 'R', Exp);
6764 Set_Etype (Def_Id, Exp_Type);
6765 Res := New_Reference_To (Def_Id, Loc);
6768 Make_Object_Declaration (Loc,
6769 Defining_Identifier => Def_Id,
6770 Object_Definition => New_Reference_To (Exp_Type, Loc),
6771 Constant_Present => not Is_Variable (Exp),
6772 Expression => Relocate_Node (Exp));
6774 Set_Assignment_OK (E);
6775 Insert_Action (Exp, E);
6778 -- For expressions that denote objects, we can use a renaming scheme.
6779 -- This is needed for correctness in the case of a volatile object of a
6780 -- non-volatile type because the Make_Reference call of the "default"
6781 -- approach would generate an illegal access value (an access value
6782 -- cannot designate such an object - see Analyze_Reference). We skip
6783 -- using this scheme if we have an object of a volatile type and we do
6784 -- not have Name_Req set true (see comments above for Side_Effect_Free).
6786 elsif Is_Object_Reference (Exp)
6787 and then Nkind (Exp) /= N_Function_Call
6788 and then (Name_Req or else not Treat_As_Volatile (Exp_Type))
6790 Def_Id := Make_Temporary (Loc, 'R', Exp);
6792 if Nkind (Exp) = N_Selected_Component
6793 and then Nkind (Prefix (Exp)) = N_Function_Call
6794 and then Is_Array_Type (Exp_Type)
6796 -- Avoid generating a variable-sized temporary, by generating
6797 -- the renaming declaration just for the function call. The
6798 -- transformation could be refined to apply only when the array
6799 -- component is constrained by a discriminant???
6802 Make_Selected_Component (Loc,
6803 Prefix => New_Occurrence_Of (Def_Id, Loc),
6804 Selector_Name => Selector_Name (Exp));
6807 Make_Object_Renaming_Declaration (Loc,
6808 Defining_Identifier => Def_Id,
6810 New_Reference_To (Base_Type (Etype (Prefix (Exp))), Loc),
6811 Name => Relocate_Node (Prefix (Exp))));
6814 Res := New_Reference_To (Def_Id, Loc);
6817 Make_Object_Renaming_Declaration (Loc,
6818 Defining_Identifier => Def_Id,
6819 Subtype_Mark => New_Reference_To (Exp_Type, Loc),
6820 Name => Relocate_Node (Exp)));
6823 -- If this is a packed reference, or a selected component with
6824 -- a non-standard representation, a reference to the temporary
6825 -- will be replaced by a copy of the original expression (see
6826 -- Exp_Ch2.Expand_Renaming). Otherwise the temporary must be
6827 -- elaborated by gigi, and is of course not to be replaced in-line
6828 -- by the expression it renames, which would defeat the purpose of
6829 -- removing the side-effect.
6831 if (Nkind (Exp) = N_Selected_Component
6832 or else Nkind (Exp) = N_Indexed_Component)
6833 and then Has_Non_Standard_Rep (Etype (Prefix (Exp)))
6837 Set_Is_Renaming_Of_Object (Def_Id, False);
6840 -- Otherwise we generate a reference to the value
6843 -- An expression which is in Alfa mode is considered side effect free
6844 -- if the resulting value is captured by a variable or a constant.
6847 and then Nkind (Parent (Exp)) = N_Object_Declaration
6852 -- Special processing for function calls that return a limited type.
6853 -- We need to build a declaration that will enable build-in-place
6854 -- expansion of the call. This is not done if the context is already
6855 -- an object declaration, to prevent infinite recursion.
6857 -- This is relevant only in Ada 2005 mode. In Ada 95 programs we have
6858 -- to accommodate functions returning limited objects by reference.
6860 if Ada_Version >= Ada_2005
6861 and then Nkind (Exp) = N_Function_Call
6862 and then Is_Immutably_Limited_Type (Etype (Exp))
6863 and then Nkind (Parent (Exp)) /= N_Object_Declaration
6866 Obj : constant Entity_Id := Make_Temporary (Loc, 'F', Exp);
6871 Make_Object_Declaration (Loc,
6872 Defining_Identifier => Obj,
6873 Object_Definition => New_Occurrence_Of (Exp_Type, Loc),
6874 Expression => Relocate_Node (Exp));
6876 Insert_Action (Exp, Decl);
6877 Set_Etype (Obj, Exp_Type);
6878 Rewrite (Exp, New_Occurrence_Of (Obj, Loc));
6883 Def_Id := Make_Temporary (Loc, 'R', Exp);
6884 Set_Etype (Def_Id, Exp_Type);
6886 -- The regular expansion of functions with side effects involves the
6887 -- generation of an access type to capture the return value found on
6888 -- the secondary stack. Since Alfa (and why) cannot process access
6889 -- types, use a different approach which ignores the secondary stack
6890 -- and "copies" the returned object.
6893 Res := New_Reference_To (Def_Id, Loc);
6894 Ref_Type := Exp_Type;
6896 -- Regular expansion utilizing an access type and 'reference
6900 Make_Explicit_Dereference (Loc,
6901 Prefix => New_Reference_To (Def_Id, Loc));
6904 -- type Ann is access all <Exp_Type>;
6906 Ref_Type := Make_Temporary (Loc, 'A');
6909 Make_Full_Type_Declaration (Loc,
6910 Defining_Identifier => Ref_Type,
6912 Make_Access_To_Object_Definition (Loc,
6913 All_Present => True,
6914 Subtype_Indication =>
6915 New_Reference_To (Exp_Type, Loc)));
6917 Insert_Action (Exp, Ptr_Typ_Decl);
6921 if Nkind (E) = N_Explicit_Dereference then
6922 New_Exp := Relocate_Node (Prefix (E));
6924 E := Relocate_Node (E);
6926 -- Do not generate a 'reference in Alfa mode since the access type
6927 -- is not created in the first place.
6932 -- Otherwise generate reference, marking the value as non-null
6933 -- since we know it cannot be null and we don't want a check.
6936 New_Exp := Make_Reference (Loc, E);
6937 Set_Is_Known_Non_Null (Def_Id);
6941 if Is_Delayed_Aggregate (E) then
6943 -- The expansion of nested aggregates is delayed until the
6944 -- enclosing aggregate is expanded. As aggregates are often
6945 -- qualified, the predicate applies to qualified expressions as
6946 -- well, indicating that the enclosing aggregate has not been
6947 -- expanded yet. At this point the aggregate is part of a
6948 -- stand-alone declaration, and must be fully expanded.
6950 if Nkind (E) = N_Qualified_Expression then
6951 Set_Expansion_Delayed (Expression (E), False);
6952 Set_Analyzed (Expression (E), False);
6954 Set_Expansion_Delayed (E, False);
6957 Set_Analyzed (E, False);
6961 Make_Object_Declaration (Loc,
6962 Defining_Identifier => Def_Id,
6963 Object_Definition => New_Reference_To (Ref_Type, Loc),
6964 Constant_Present => True,
6965 Expression => New_Exp));
6968 -- Preserve the Assignment_OK flag in all copies, since at least one
6969 -- copy may be used in a context where this flag must be set (otherwise
6970 -- why would the flag be set in the first place).
6972 Set_Assignment_OK (Res, Assignment_OK (Exp));
6974 -- Finally rewrite the original expression and we are done
6977 Analyze_And_Resolve (Exp, Exp_Type);
6978 Scope_Suppress := Svg_Suppress;
6979 end Remove_Side_Effects;
6981 ---------------------------
6982 -- Represented_As_Scalar --
6983 ---------------------------
6985 function Represented_As_Scalar (T : Entity_Id) return Boolean is
6986 UT : constant Entity_Id := Underlying_Type (T);
6988 return Is_Scalar_Type (UT)
6989 or else (Is_Bit_Packed_Array (UT)
6990 and then Is_Scalar_Type (Packed_Array_Type (UT)));
6991 end Represented_As_Scalar;
6993 ------------------------------
6994 -- Requires_Cleanup_Actions --
6995 ------------------------------
6997 function Requires_Cleanup_Actions (N : Node_Id) return Boolean is
6998 For_Pkg : constant Boolean :=
6999 Nkind_In (N, N_Package_Body, N_Package_Specification);
7003 when N_Accept_Statement |
7011 Requires_Cleanup_Actions (Declarations (N), For_Pkg, True)
7013 (Present (Handled_Statement_Sequence (N))
7015 Requires_Cleanup_Actions (Statements
7016 (Handled_Statement_Sequence (N)), For_Pkg, True));
7018 when N_Package_Specification =>
7020 Requires_Cleanup_Actions
7021 (Visible_Declarations (N), For_Pkg, True)
7023 Requires_Cleanup_Actions
7024 (Private_Declarations (N), For_Pkg, True);
7029 end Requires_Cleanup_Actions;
7031 ------------------------------
7032 -- Requires_Cleanup_Actions --
7033 ------------------------------
7035 function Requires_Cleanup_Actions
7037 For_Package : Boolean;
7038 Nested_Constructs : Boolean) return Boolean
7043 Obj_Typ : Entity_Id;
7044 Pack_Id : Entity_Id;
7049 or else Is_Empty_List (L)
7055 while Present (Decl) loop
7057 -- Library-level tagged types
7059 if Nkind (Decl) = N_Full_Type_Declaration then
7060 Typ := Defining_Identifier (Decl);
7062 if Is_Tagged_Type (Typ)
7063 and then Is_Library_Level_Entity (Typ)
7064 and then Convention (Typ) = Convention_Ada
7065 and then Present (Access_Disp_Table (Typ))
7066 and then RTE_Available (RE_Unregister_Tag)
7067 and then not No_Run_Time_Mode
7068 and then not Is_Abstract_Type (Typ)
7073 -- Regular object declarations
7075 elsif Nkind (Decl) = N_Object_Declaration then
7076 Obj_Id := Defining_Identifier (Decl);
7077 Obj_Typ := Base_Type (Etype (Obj_Id));
7078 Expr := Expression (Decl);
7080 -- Bypass any form of processing for objects which have their
7081 -- finalization disabled. This applies only to objects at the
7085 and then Finalize_Storage_Only (Obj_Typ)
7089 -- Transient variables are treated separately in order to minimize
7090 -- the size of the generated code. See Exp_Ch7.Process_Transient_
7093 elsif Is_Processed_Transient (Obj_Id) then
7096 -- The object is of the form:
7097 -- Obj : Typ [:= Expr];
7099 -- Do not process the incomplete view of a deferred constant. Do
7100 -- not consider tag-to-class-wide conversions.
7102 elsif not Is_Imported (Obj_Id)
7103 and then Needs_Finalization (Obj_Typ)
7104 and then not (Ekind (Obj_Id) = E_Constant
7105 and then not Has_Completion (Obj_Id))
7106 and then not Is_Tag_To_Class_Wide_Conversion (Obj_Id)
7110 -- The object is of the form:
7111 -- Obj : Access_Typ := Non_BIP_Function_Call'reference;
7113 -- Obj : Access_Typ :=
7114 -- BIP_Function_Call
7115 -- (..., BIPaccess => null, ...)'reference;
7117 elsif Is_Access_Type (Obj_Typ)
7118 and then Needs_Finalization
7119 (Available_View (Designated_Type (Obj_Typ)))
7120 and then Present (Expr)
7122 (Is_Null_Access_BIP_Func_Call (Expr)
7124 (Is_Non_BIP_Func_Call (Expr)
7125 and then not Is_Related_To_Func_Return (Obj_Id)))
7129 -- Processing for "hook" objects generated for controlled
7130 -- transients declared inside an Expression_With_Actions.
7132 elsif Is_Access_Type (Obj_Typ)
7133 and then Present (Return_Flag_Or_Transient_Decl (Obj_Id))
7134 and then Nkind (Return_Flag_Or_Transient_Decl (Obj_Id)) =
7135 N_Object_Declaration
7136 and then Is_Finalizable_Transient
7137 (Return_Flag_Or_Transient_Decl (Obj_Id), Decl)
7141 -- Simple protected objects which use type System.Tasking.
7142 -- Protected_Objects.Protection to manage their locks should be
7143 -- treated as controlled since they require manual cleanup.
7145 elsif Ekind (Obj_Id) = E_Variable
7147 (Is_Simple_Protected_Type (Obj_Typ)
7148 or else Has_Simple_Protected_Object (Obj_Typ))
7153 -- Specific cases of object renamings
7155 elsif Nkind (Decl) = N_Object_Renaming_Declaration then
7156 Obj_Id := Defining_Identifier (Decl);
7157 Obj_Typ := Base_Type (Etype (Obj_Id));
7159 -- Bypass any form of processing for objects which have their
7160 -- finalization disabled. This applies only to objects at the
7164 and then Finalize_Storage_Only (Obj_Typ)
7168 -- Return object of a build-in-place function. This case is
7169 -- recognized and marked by the expansion of an extended return
7170 -- statement (see Expand_N_Extended_Return_Statement).
7172 elsif Needs_Finalization (Obj_Typ)
7173 and then Is_Return_Object (Obj_Id)
7174 and then Present (Return_Flag_Or_Transient_Decl (Obj_Id))
7178 -- Detect a case where a source object has been initialized by a
7179 -- controlled function call which was later rewritten as a class-
7180 -- wide conversion of Ada.Tags.Displace.
7182 -- Obj : Class_Wide_Type := Function_Call (...);
7184 -- Temp : ... := Function_Call (...)'reference;
7185 -- Obj : Class_Wide_Type renames
7186 -- (... Ada.Tags.Displace (Temp));
7188 elsif Is_Displacement_Of_Ctrl_Function_Result (Obj_Id) then
7192 -- Inspect the freeze node of an access-to-controlled type and look
7193 -- for a delayed finalization master. This case arises when the
7194 -- freeze actions are inserted at a later time than the expansion of
7195 -- the context. Since Build_Finalizer is never called on a single
7196 -- construct twice, the master will be ultimately left out and never
7197 -- finalized. This is also needed for freeze actions of designated
7198 -- types themselves, since in some cases the finalization master is
7199 -- associated with a designated type's freeze node rather than that
7200 -- of the access type (see handling for freeze actions in
7201 -- Build_Finalization_Master).
7203 elsif Nkind (Decl) = N_Freeze_Entity
7204 and then Present (Actions (Decl))
7206 Typ := Entity (Decl);
7208 if ((Is_Access_Type (Typ)
7209 and then not Is_Access_Subprogram_Type (Typ)
7210 and then Needs_Finalization
7211 (Available_View (Designated_Type (Typ))))
7214 and then Needs_Finalization (Typ)))
7215 and then Requires_Cleanup_Actions
7216 (Actions (Decl), For_Package, Nested_Constructs)
7221 -- Nested package declarations
7223 elsif Nested_Constructs
7224 and then Nkind (Decl) = N_Package_Declaration
7226 Pack_Id := Defining_Unit_Name (Specification (Decl));
7228 if Nkind (Pack_Id) = N_Defining_Program_Unit_Name then
7229 Pack_Id := Defining_Identifier (Pack_Id);
7232 if Ekind (Pack_Id) /= E_Generic_Package
7233 and then Requires_Cleanup_Actions (Specification (Decl))
7238 -- Nested package bodies
7240 elsif Nested_Constructs
7241 and then Nkind (Decl) = N_Package_Body
7243 Pack_Id := Corresponding_Spec (Decl);
7245 if Ekind (Pack_Id) /= E_Generic_Package
7246 and then Requires_Cleanup_Actions (Decl)
7256 end Requires_Cleanup_Actions;
7258 ------------------------------------
7259 -- Safe_Unchecked_Type_Conversion --
7260 ------------------------------------
7262 -- Note: this function knows quite a bit about the exact requirements of
7263 -- Gigi with respect to unchecked type conversions, and its code must be
7264 -- coordinated with any changes in Gigi in this area.
7266 -- The above requirements should be documented in Sinfo ???
7268 function Safe_Unchecked_Type_Conversion (Exp : Node_Id) return Boolean is
7273 Pexp : constant Node_Id := Parent (Exp);
7276 -- If the expression is the RHS of an assignment or object declaration
7277 -- we are always OK because there will always be a target.
7279 -- Object renaming declarations, (generated for view conversions of
7280 -- actuals in inlined calls), like object declarations, provide an
7281 -- explicit type, and are safe as well.
7283 if (Nkind (Pexp) = N_Assignment_Statement
7284 and then Expression (Pexp) = Exp)
7285 or else Nkind (Pexp) = N_Object_Declaration
7286 or else Nkind (Pexp) = N_Object_Renaming_Declaration
7290 -- If the expression is the prefix of an N_Selected_Component we should
7291 -- also be OK because GCC knows to look inside the conversion except if
7292 -- the type is discriminated. We assume that we are OK anyway if the
7293 -- type is not set yet or if it is controlled since we can't afford to
7294 -- introduce a temporary in this case.
7296 elsif Nkind (Pexp) = N_Selected_Component
7297 and then Prefix (Pexp) = Exp
7299 if No (Etype (Pexp)) then
7303 not Has_Discriminants (Etype (Pexp))
7304 or else Is_Constrained (Etype (Pexp));
7308 -- Set the output type, this comes from Etype if it is set, otherwise we
7309 -- take it from the subtype mark, which we assume was already fully
7312 if Present (Etype (Exp)) then
7313 Otyp := Etype (Exp);
7315 Otyp := Entity (Subtype_Mark (Exp));
7318 -- The input type always comes from the expression, and we assume
7319 -- this is indeed always analyzed, so we can simply get the Etype.
7321 Ityp := Etype (Expression (Exp));
7323 -- Initialize alignments to unknown so far
7328 -- Replace a concurrent type by its corresponding record type and each
7329 -- type by its underlying type and do the tests on those. The original
7330 -- type may be a private type whose completion is a concurrent type, so
7331 -- find the underlying type first.
7333 if Present (Underlying_Type (Otyp)) then
7334 Otyp := Underlying_Type (Otyp);
7337 if Present (Underlying_Type (Ityp)) then
7338 Ityp := Underlying_Type (Ityp);
7341 if Is_Concurrent_Type (Otyp) then
7342 Otyp := Corresponding_Record_Type (Otyp);
7345 if Is_Concurrent_Type (Ityp) then
7346 Ityp := Corresponding_Record_Type (Ityp);
7349 -- If the base types are the same, we know there is no problem since
7350 -- this conversion will be a noop.
7352 if Implementation_Base_Type (Otyp) = Implementation_Base_Type (Ityp) then
7355 -- Same if this is an upwards conversion of an untagged type, and there
7356 -- are no constraints involved (could be more general???)
7358 elsif Etype (Ityp) = Otyp
7359 and then not Is_Tagged_Type (Ityp)
7360 and then not Has_Discriminants (Ityp)
7361 and then No (First_Rep_Item (Base_Type (Ityp)))
7365 -- If the expression has an access type (object or subprogram) we assume
7366 -- that the conversion is safe, because the size of the target is safe,
7367 -- even if it is a record (which might be treated as having unknown size
7370 elsif Is_Access_Type (Ityp) then
7373 -- If the size of output type is known at compile time, there is never
7374 -- a problem. Note that unconstrained records are considered to be of
7375 -- known size, but we can't consider them that way here, because we are
7376 -- talking about the actual size of the object.
7378 -- We also make sure that in addition to the size being known, we do not
7379 -- have a case which might generate an embarrassingly large temp in
7380 -- stack checking mode.
7382 elsif Size_Known_At_Compile_Time (Otyp)
7384 (not Stack_Checking_Enabled
7385 or else not May_Generate_Large_Temp (Otyp))
7386 and then not (Is_Record_Type (Otyp) and then not Is_Constrained (Otyp))
7390 -- If either type is tagged, then we know the alignment is OK so
7391 -- Gigi will be able to use pointer punning.
7393 elsif Is_Tagged_Type (Otyp) or else Is_Tagged_Type (Ityp) then
7396 -- If either type is a limited record type, we cannot do a copy, so say
7397 -- safe since there's nothing else we can do.
7399 elsif Is_Limited_Record (Otyp) or else Is_Limited_Record (Ityp) then
7402 -- Conversions to and from packed array types are always ignored and
7405 elsif Is_Packed_Array_Type (Otyp)
7406 or else Is_Packed_Array_Type (Ityp)
7411 -- The only other cases known to be safe is if the input type's
7412 -- alignment is known to be at least the maximum alignment for the
7413 -- target or if both alignments are known and the output type's
7414 -- alignment is no stricter than the input's. We can use the component
7415 -- type alignement for an array if a type is an unpacked array type.
7417 if Present (Alignment_Clause (Otyp)) then
7418 Oalign := Expr_Value (Expression (Alignment_Clause (Otyp)));
7420 elsif Is_Array_Type (Otyp)
7421 and then Present (Alignment_Clause (Component_Type (Otyp)))
7423 Oalign := Expr_Value (Expression (Alignment_Clause
7424 (Component_Type (Otyp))));
7427 if Present (Alignment_Clause (Ityp)) then
7428 Ialign := Expr_Value (Expression (Alignment_Clause (Ityp)));
7430 elsif Is_Array_Type (Ityp)
7431 and then Present (Alignment_Clause (Component_Type (Ityp)))
7433 Ialign := Expr_Value (Expression (Alignment_Clause
7434 (Component_Type (Ityp))));
7437 if Ialign /= No_Uint and then Ialign > Maximum_Alignment then
7440 elsif Ialign /= No_Uint and then Oalign /= No_Uint
7441 and then Ialign <= Oalign
7445 -- Otherwise, Gigi cannot handle this and we must make a temporary
7450 end Safe_Unchecked_Type_Conversion;
7452 ---------------------------------
7453 -- Set_Current_Value_Condition --
7454 ---------------------------------
7456 -- Note: the implementation of this procedure is very closely tied to the
7457 -- implementation of Get_Current_Value_Condition. Here we set required
7458 -- Current_Value fields, and in Get_Current_Value_Condition, we interpret
7459 -- them, so they must have a consistent view.
7461 procedure Set_Current_Value_Condition (Cnode : Node_Id) is
7463 procedure Set_Entity_Current_Value (N : Node_Id);
7464 -- If N is an entity reference, where the entity is of an appropriate
7465 -- kind, then set the current value of this entity to Cnode, unless
7466 -- there is already a definite value set there.
7468 procedure Set_Expression_Current_Value (N : Node_Id);
7469 -- If N is of an appropriate form, sets an appropriate entry in current
7470 -- value fields of relevant entities. Multiple entities can be affected
7471 -- in the case of an AND or AND THEN.
7473 ------------------------------
7474 -- Set_Entity_Current_Value --
7475 ------------------------------
7477 procedure Set_Entity_Current_Value (N : Node_Id) is
7479 if Is_Entity_Name (N) then
7481 Ent : constant Entity_Id := Entity (N);
7484 -- Don't capture if not safe to do so
7486 if not Safe_To_Capture_Value (N, Ent, Cond => True) then
7490 -- Here we have a case where the Current_Value field may need
7491 -- to be set. We set it if it is not already set to a compile
7492 -- time expression value.
7494 -- Note that this represents a decision that one condition
7495 -- blots out another previous one. That's certainly right if
7496 -- they occur at the same level. If the second one is nested,
7497 -- then the decision is neither right nor wrong (it would be
7498 -- equally OK to leave the outer one in place, or take the new
7499 -- inner one. Really we should record both, but our data
7500 -- structures are not that elaborate.
7502 if Nkind (Current_Value (Ent)) not in N_Subexpr then
7503 Set_Current_Value (Ent, Cnode);
7507 end Set_Entity_Current_Value;
7509 ----------------------------------
7510 -- Set_Expression_Current_Value --
7511 ----------------------------------
7513 procedure Set_Expression_Current_Value (N : Node_Id) is
7519 -- Loop to deal with (ignore for now) any NOT operators present. The
7520 -- presence of NOT operators will be handled properly when we call
7521 -- Get_Current_Value_Condition.
7523 while Nkind (Cond) = N_Op_Not loop
7524 Cond := Right_Opnd (Cond);
7527 -- For an AND or AND THEN, recursively process operands
7529 if Nkind (Cond) = N_Op_And or else Nkind (Cond) = N_And_Then then
7530 Set_Expression_Current_Value (Left_Opnd (Cond));
7531 Set_Expression_Current_Value (Right_Opnd (Cond));
7535 -- Check possible relational operator
7537 if Nkind (Cond) in N_Op_Compare then
7538 if Compile_Time_Known_Value (Right_Opnd (Cond)) then
7539 Set_Entity_Current_Value (Left_Opnd (Cond));
7540 elsif Compile_Time_Known_Value (Left_Opnd (Cond)) then
7541 Set_Entity_Current_Value (Right_Opnd (Cond));
7544 -- Check possible boolean variable reference
7547 Set_Entity_Current_Value (Cond);
7549 end Set_Expression_Current_Value;
7551 -- Start of processing for Set_Current_Value_Condition
7554 Set_Expression_Current_Value (Condition (Cnode));
7555 end Set_Current_Value_Condition;
7557 --------------------------
7558 -- Set_Elaboration_Flag --
7559 --------------------------
7561 procedure Set_Elaboration_Flag (N : Node_Id; Spec_Id : Entity_Id) is
7562 Loc : constant Source_Ptr := Sloc (N);
7563 Ent : constant Entity_Id := Elaboration_Entity (Spec_Id);
7567 if Present (Ent) then
7569 -- Nothing to do if at the compilation unit level, because in this
7570 -- case the flag is set by the binder generated elaboration routine.
7572 if Nkind (Parent (N)) = N_Compilation_Unit then
7575 -- Here we do need to generate an assignment statement
7578 Check_Restriction (No_Elaboration_Code, N);
7580 Make_Assignment_Statement (Loc,
7581 Name => New_Occurrence_Of (Ent, Loc),
7582 Expression => Make_Integer_Literal (Loc, Uint_1));
7584 if Nkind (Parent (N)) = N_Subunit then
7585 Insert_After (Corresponding_Stub (Parent (N)), Asn);
7587 Insert_After (N, Asn);
7592 -- Kill current value indication. This is necessary because the
7593 -- tests of this flag are inserted out of sequence and must not
7594 -- pick up bogus indications of the wrong constant value.
7596 Set_Current_Value (Ent, Empty);
7599 end Set_Elaboration_Flag;
7601 ----------------------------
7602 -- Set_Renamed_Subprogram --
7603 ----------------------------
7605 procedure Set_Renamed_Subprogram (N : Node_Id; E : Entity_Id) is
7607 -- If input node is an identifier, we can just reset it
7609 if Nkind (N) = N_Identifier then
7610 Set_Chars (N, Chars (E));
7613 -- Otherwise we have to do a rewrite, preserving Comes_From_Source
7617 CS : constant Boolean := Comes_From_Source (N);
7619 Rewrite (N, Make_Identifier (Sloc (N), Chars (E)));
7621 Set_Comes_From_Source (N, CS);
7622 Set_Analyzed (N, True);
7625 end Set_Renamed_Subprogram;
7627 ----------------------------------
7628 -- Silly_Boolean_Array_Not_Test --
7629 ----------------------------------
7631 -- This procedure implements an odd and silly test. We explicitly check
7632 -- for the case where the 'First of the component type is equal to the
7633 -- 'Last of this component type, and if this is the case, we make sure
7634 -- that constraint error is raised. The reason is that the NOT is bound
7635 -- to cause CE in this case, and we will not otherwise catch it.
7637 -- No such check is required for AND and OR, since for both these cases
7638 -- False op False = False, and True op True = True. For the XOR case,
7639 -- see Silly_Boolean_Array_Xor_Test.
7641 -- Believe it or not, this was reported as a bug. Note that nearly always,
7642 -- the test will evaluate statically to False, so the code will be
7643 -- statically removed, and no extra overhead caused.
7645 procedure Silly_Boolean_Array_Not_Test (N : Node_Id; T : Entity_Id) is
7646 Loc : constant Source_Ptr := Sloc (N);
7647 CT : constant Entity_Id := Component_Type (T);
7650 -- The check we install is
7652 -- constraint_error when
7653 -- component_type'first = component_type'last
7654 -- and then array_type'Length /= 0)
7656 -- We need the last guard because we don't want to raise CE for empty
7657 -- arrays since no out of range values result. (Empty arrays with a
7658 -- component type of True .. True -- very useful -- even the ACATS
7659 -- does not test that marginal case!)
7662 Make_Raise_Constraint_Error (Loc,
7668 Make_Attribute_Reference (Loc,
7669 Prefix => New_Occurrence_Of (CT, Loc),
7670 Attribute_Name => Name_First),
7673 Make_Attribute_Reference (Loc,
7674 Prefix => New_Occurrence_Of (CT, Loc),
7675 Attribute_Name => Name_Last)),
7677 Right_Opnd => Make_Non_Empty_Check (Loc, Right_Opnd (N))),
7678 Reason => CE_Range_Check_Failed));
7679 end Silly_Boolean_Array_Not_Test;
7681 ----------------------------------
7682 -- Silly_Boolean_Array_Xor_Test --
7683 ----------------------------------
7685 -- This procedure implements an odd and silly test. We explicitly check
7686 -- for the XOR case where the component type is True .. True, since this
7687 -- will raise constraint error. A special check is required since CE
7688 -- will not be generated otherwise (cf Expand_Packed_Not).
7690 -- No such check is required for AND and OR, since for both these cases
7691 -- False op False = False, and True op True = True, and no check is
7692 -- required for the case of False .. False, since False xor False = False.
7693 -- See also Silly_Boolean_Array_Not_Test
7695 procedure Silly_Boolean_Array_Xor_Test (N : Node_Id; T : Entity_Id) is
7696 Loc : constant Source_Ptr := Sloc (N);
7697 CT : constant Entity_Id := Component_Type (T);
7700 -- The check we install is
7702 -- constraint_error when
7703 -- Boolean (component_type'First)
7704 -- and then Boolean (component_type'Last)
7705 -- and then array_type'Length /= 0)
7707 -- We need the last guard because we don't want to raise CE for empty
7708 -- arrays since no out of range values result (Empty arrays with a
7709 -- component type of True .. True -- very useful -- even the ACATS
7710 -- does not test that marginal case!).
7713 Make_Raise_Constraint_Error (Loc,
7719 Convert_To (Standard_Boolean,
7720 Make_Attribute_Reference (Loc,
7721 Prefix => New_Occurrence_Of (CT, Loc),
7722 Attribute_Name => Name_First)),
7725 Convert_To (Standard_Boolean,
7726 Make_Attribute_Reference (Loc,
7727 Prefix => New_Occurrence_Of (CT, Loc),
7728 Attribute_Name => Name_Last))),
7730 Right_Opnd => Make_Non_Empty_Check (Loc, Right_Opnd (N))),
7731 Reason => CE_Range_Check_Failed));
7732 end Silly_Boolean_Array_Xor_Test;
7734 --------------------------
7735 -- Target_Has_Fixed_Ops --
7736 --------------------------
7738 Integer_Sized_Small : Ureal;
7739 -- Set to 2.0 ** -(Integer'Size - 1) the first time that this function is
7740 -- called (we don't want to compute it more than once!)
7742 Long_Integer_Sized_Small : Ureal;
7743 -- Set to 2.0 ** -(Long_Integer'Size - 1) the first time that this function
7744 -- is called (we don't want to compute it more than once)
7746 First_Time_For_THFO : Boolean := True;
7747 -- Set to False after first call (if Fractional_Fixed_Ops_On_Target)
7749 function Target_Has_Fixed_Ops
7750 (Left_Typ : Entity_Id;
7751 Right_Typ : Entity_Id;
7752 Result_Typ : Entity_Id) return Boolean
7754 function Is_Fractional_Type (Typ : Entity_Id) return Boolean;
7755 -- Return True if the given type is a fixed-point type with a small
7756 -- value equal to 2 ** (-(T'Object_Size - 1)) and whose values have
7757 -- an absolute value less than 1.0. This is currently limited to
7758 -- fixed-point types that map to Integer or Long_Integer.
7760 ------------------------
7761 -- Is_Fractional_Type --
7762 ------------------------
7764 function Is_Fractional_Type (Typ : Entity_Id) return Boolean is
7766 if Esize (Typ) = Standard_Integer_Size then
7767 return Small_Value (Typ) = Integer_Sized_Small;
7769 elsif Esize (Typ) = Standard_Long_Integer_Size then
7770 return Small_Value (Typ) = Long_Integer_Sized_Small;
7775 end Is_Fractional_Type;
7777 -- Start of processing for Target_Has_Fixed_Ops
7780 -- Return False if Fractional_Fixed_Ops_On_Target is false
7782 if not Fractional_Fixed_Ops_On_Target then
7786 -- Here the target has Fractional_Fixed_Ops, if first time, compute
7787 -- standard constants used by Is_Fractional_Type.
7789 if First_Time_For_THFO then
7790 First_Time_For_THFO := False;
7792 Integer_Sized_Small :=
7795 Den => UI_From_Int (Standard_Integer_Size - 1),
7798 Long_Integer_Sized_Small :=
7801 Den => UI_From_Int (Standard_Long_Integer_Size - 1),
7805 -- Return True if target supports fixed-by-fixed multiply/divide for
7806 -- fractional fixed-point types (see Is_Fractional_Type) and the operand
7807 -- and result types are equivalent fractional types.
7809 return Is_Fractional_Type (Base_Type (Left_Typ))
7810 and then Is_Fractional_Type (Base_Type (Right_Typ))
7811 and then Is_Fractional_Type (Base_Type (Result_Typ))
7812 and then Esize (Left_Typ) = Esize (Right_Typ)
7813 and then Esize (Left_Typ) = Esize (Result_Typ);
7814 end Target_Has_Fixed_Ops;
7816 ------------------------------------------
7817 -- Type_May_Have_Bit_Aligned_Components --
7818 ------------------------------------------
7820 function Type_May_Have_Bit_Aligned_Components
7821 (Typ : Entity_Id) return Boolean
7824 -- Array type, check component type
7826 if Is_Array_Type (Typ) then
7828 Type_May_Have_Bit_Aligned_Components (Component_Type (Typ));
7830 -- Record type, check components
7832 elsif Is_Record_Type (Typ) then
7837 E := First_Component_Or_Discriminant (Typ);
7838 while Present (E) loop
7839 if Component_May_Be_Bit_Aligned (E)
7840 or else Type_May_Have_Bit_Aligned_Components (Etype (E))
7845 Next_Component_Or_Discriminant (E);
7851 -- Type other than array or record is always OK
7856 end Type_May_Have_Bit_Aligned_Components;
7858 ----------------------------
7859 -- Wrap_Cleanup_Procedure --
7860 ----------------------------
7862 procedure Wrap_Cleanup_Procedure (N : Node_Id) is
7863 Loc : constant Source_Ptr := Sloc (N);
7864 Stseq : constant Node_Id := Handled_Statement_Sequence (N);
7865 Stmts : constant List_Id := Statements (Stseq);
7868 if Abort_Allowed then
7869 Prepend_To (Stmts, Build_Runtime_Call (Loc, RE_Abort_Defer));
7870 Append_To (Stmts, Build_Runtime_Call (Loc, RE_Abort_Undefer));
7872 end Wrap_Cleanup_Procedure;