1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2006, 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 2, 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 COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
25 ------------------------------------------------------------------------------
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Debug; use Debug;
30 with Einfo; use Einfo;
31 with Elists; use Elists;
32 with Errout; use Errout;
33 with Exp_Aggr; use Exp_Aggr;
34 with Exp_Ch7; use Exp_Ch7;
35 with Hostparm; use Hostparm;
36 with Inline; use Inline;
37 with Itypes; use Itypes;
39 with Namet; use Namet;
40 with Nlists; use Nlists;
41 with Nmake; use Nmake;
43 with Restrict; use Restrict;
44 with Rident; use Rident;
46 with Sem_Ch8; use Sem_Ch8;
47 with Sem_Eval; use Sem_Eval;
48 with Sem_Res; use Sem_Res;
49 with Sem_Type; use Sem_Type;
50 with Sem_Util; use Sem_Util;
51 with Snames; use Snames;
52 with Stand; use Stand;
53 with Stringt; use Stringt;
54 with Targparm; use Targparm;
55 with Tbuild; use Tbuild;
56 with Ttypes; use Ttypes;
57 with Uintp; use Uintp;
58 with Urealp; use Urealp;
59 with Validsw; use Validsw;
61 package body Exp_Util is
63 -----------------------
64 -- Local Subprograms --
65 -----------------------
67 function Build_Task_Array_Image
71 Dyn : Boolean := False) return Node_Id;
72 -- Build function to generate the image string for a task that is an
73 -- array component, concatenating the images of each index. To avoid
74 -- storage leaks, the string is built with successive slice assignments.
75 -- The flag Dyn indicates whether this is called for the initialization
76 -- procedure of an array of tasks, or for the name of a dynamically
77 -- created task that is assigned to an indexed component.
79 function Build_Task_Image_Function
83 Res : Entity_Id) return Node_Id;
84 -- Common processing for Task_Array_Image and Task_Record_Image.
85 -- Build function body that computes image.
87 procedure Build_Task_Image_Prefix
94 Decls : in out List_Id;
95 Stats : in out List_Id);
96 -- Common processing for Task_Array_Image and Task_Record_Image.
97 -- Create local variables and assign prefix of name to result string.
99 function Build_Task_Record_Image
102 Dyn : Boolean := False) return Node_Id;
103 -- Build function to generate the image string for a task that is a
104 -- record component. Concatenate name of variable with that of selector.
105 -- The flag Dyn indicates whether this is called for the initialization
106 -- procedure of record with task components, or for a dynamically
107 -- created task that is assigned to a selected component.
109 function Make_CW_Equivalent_Type
111 E : Node_Id) return Entity_Id;
112 -- T is a class-wide type entity, E is the initial expression node that
113 -- constrains T in case such as: " X: T := E" or "new T'(E)"
114 -- This function returns the entity of the Equivalent type and inserts
115 -- on the fly the necessary declaration such as:
117 -- type anon is record
118 -- _parent : Root_Type (T); constrained with E discriminants (if any)
119 -- Extension : String (1 .. expr to match size of E);
122 -- This record is compatible with any object of the class of T thanks
123 -- to the first field and has the same size as E thanks to the second.
125 function Make_Literal_Range
127 Literal_Typ : Entity_Id) return Node_Id;
128 -- Produce a Range node whose bounds are:
129 -- Low_Bound (Literal_Type) ..
130 -- Low_Bound (Literal_Type) + Length (Literal_Typ) - 1
131 -- this is used for expanding declarations like X : String := "sdfgdfg";
133 function New_Class_Wide_Subtype
135 N : Node_Id) return Entity_Id;
136 -- Create an implicit subtype of CW_Typ attached to node N
138 ----------------------
139 -- Adjust_Condition --
140 ----------------------
142 procedure Adjust_Condition (N : Node_Id) is
149 Loc : constant Source_Ptr := Sloc (N);
150 T : constant Entity_Id := Etype (N);
154 -- For now, we simply ignore a call where the argument has no
155 -- type (probably case of unanalyzed condition), or has a type
156 -- that is not Boolean. This is because this is a pretty marginal
157 -- piece of functionality, and violations of these rules are
158 -- likely to be truly marginal (how much code uses Fortran Logical
159 -- as the barrier to a protected entry?) and we do not want to
160 -- blow up existing programs. We can change this to an assertion
161 -- after 3.12a is released ???
163 if No (T) or else not Is_Boolean_Type (T) then
167 -- Apply validity checking if needed
169 if Validity_Checks_On and Validity_Check_Tests then
173 -- Immediate return if standard boolean, the most common case,
174 -- where nothing needs to be done.
176 if Base_Type (T) = Standard_Boolean then
180 -- Case of zero/non-zero semantics or non-standard enumeration
181 -- representation. In each case, we rewrite the node as:
183 -- ityp!(N) /= False'Enum_Rep
185 -- where ityp is an integer type with large enough size to hold
186 -- any value of type T.
188 if Nonzero_Is_True (T) or else Has_Non_Standard_Rep (T) then
189 if Esize (T) <= Esize (Standard_Integer) then
190 Ti := Standard_Integer;
192 Ti := Standard_Long_Long_Integer;
197 Left_Opnd => Unchecked_Convert_To (Ti, N),
199 Make_Attribute_Reference (Loc,
200 Attribute_Name => Name_Enum_Rep,
202 New_Occurrence_Of (First_Literal (T), Loc))));
203 Analyze_And_Resolve (N, Standard_Boolean);
206 Rewrite (N, Convert_To (Standard_Boolean, N));
207 Analyze_And_Resolve (N, Standard_Boolean);
210 end Adjust_Condition;
212 ------------------------
213 -- Adjust_Result_Type --
214 ------------------------
216 procedure Adjust_Result_Type (N : Node_Id; T : Entity_Id) is
218 -- Ignore call if current type is not Standard.Boolean
220 if Etype (N) /= Standard_Boolean then
224 -- If result is already of correct type, nothing to do. Note that
225 -- this will get the most common case where everything has a type
226 -- of Standard.Boolean.
228 if Base_Type (T) = Standard_Boolean then
233 KP : constant Node_Kind := Nkind (Parent (N));
236 -- If result is to be used as a Condition in the syntax, no need
237 -- to convert it back, since if it was changed to Standard.Boolean
238 -- using Adjust_Condition, that is just fine for this usage.
240 if KP in N_Raise_xxx_Error or else KP in N_Has_Condition then
243 -- If result is an operand of another logical operation, no need
244 -- to reset its type, since Standard.Boolean is just fine, and
245 -- such operations always do Adjust_Condition on their operands.
247 elsif KP in N_Op_Boolean
248 or else KP = N_And_Then
249 or else KP = N_Or_Else
250 or else KP = N_Op_Not
254 -- Otherwise we perform a conversion from the current type,
255 -- which must be Standard.Boolean, to the desired type.
259 Rewrite (N, Convert_To (T, N));
260 Analyze_And_Resolve (N, T);
264 end Adjust_Result_Type;
266 --------------------------
267 -- Append_Freeze_Action --
268 --------------------------
270 procedure Append_Freeze_Action (T : Entity_Id; N : Node_Id) is
274 Ensure_Freeze_Node (T);
275 Fnode := Freeze_Node (T);
277 if No (Actions (Fnode)) then
278 Set_Actions (Fnode, New_List);
281 Append (N, Actions (Fnode));
282 end Append_Freeze_Action;
284 ---------------------------
285 -- Append_Freeze_Actions --
286 ---------------------------
288 procedure Append_Freeze_Actions (T : Entity_Id; L : List_Id) is
289 Fnode : constant Node_Id := Freeze_Node (T);
296 if No (Actions (Fnode)) then
297 Set_Actions (Fnode, L);
300 Append_List (L, Actions (Fnode));
304 end Append_Freeze_Actions;
306 ------------------------
307 -- Build_Runtime_Call --
308 ------------------------
310 function Build_Runtime_Call (Loc : Source_Ptr; RE : RE_Id) return Node_Id is
312 -- If entity is not available, we can skip making the call (this avoids
313 -- junk duplicated error messages in a number of cases).
315 if not RTE_Available (RE) then
316 return Make_Null_Statement (Loc);
319 Make_Procedure_Call_Statement (Loc,
320 Name => New_Reference_To (RTE (RE), Loc));
322 end Build_Runtime_Call;
324 ----------------------------
325 -- Build_Task_Array_Image --
326 ----------------------------
328 -- This function generates the body for a function that constructs the
329 -- image string for a task that is an array component. The function is
330 -- local to the init proc for the array type, and is called for each one
331 -- of the components. The constructed image has the form of an indexed
332 -- component, whose prefix is the outer variable of the array type.
333 -- The n-dimensional array type has known indices Index, Index2...
334 -- Id_Ref is an indexed component form created by the enclosing init proc.
335 -- Its successive indices are Val1, Val2,.. which are the loop variables
336 -- in the loops that call the individual task init proc on each component.
338 -- The generated function has the following structure:
340 -- function F return String is
341 -- Pref : string renames Task_Name;
342 -- T1 : String := Index1'Image (Val1);
344 -- Tn : String := indexn'image (Valn);
345 -- Len : Integer := T1'Length + ... + Tn'Length + n + 1;
346 -- -- Len includes commas and the end parentheses.
347 -- Res : String (1..Len);
348 -- Pos : Integer := Pref'Length;
351 -- Res (1 .. Pos) := Pref;
355 -- Res (Pos .. Pos + T1'Length - 1) := T1;
356 -- Pos := Pos + T1'Length;
360 -- Res (Pos .. Pos + Tn'Length - 1) := Tn;
366 -- Needless to say, multidimensional arrays of tasks are rare enough
367 -- that the bulkiness of this code is not really a concern.
369 function Build_Task_Array_Image
373 Dyn : Boolean := False) return Node_Id
375 Dims : constant Nat := Number_Dimensions (A_Type);
376 -- Number of dimensions for array of tasks
378 Temps : array (1 .. Dims) of Entity_Id;
379 -- Array of temporaries to hold string for each index
385 -- Total length of generated name
388 -- Running index for substring assignments
391 -- Name of enclosing variable, prefix of resulting name
394 -- String to hold result
397 -- Value of successive indices
400 -- Expression to compute total size of string
403 -- Entity for name at one index position
405 Decls : List_Id := New_List;
406 Stats : List_Id := New_List;
409 Pref := Make_Defining_Identifier (Loc, New_Internal_Name ('P'));
411 -- For a dynamic task, the name comes from the target variable.
412 -- For a static one it is a formal of the enclosing init proc.
415 Get_Name_String (Chars (Entity (Prefix (Id_Ref))));
417 Make_Object_Declaration (Loc,
418 Defining_Identifier => Pref,
419 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
421 Make_String_Literal (Loc,
422 Strval => String_From_Name_Buffer)));
426 Make_Object_Renaming_Declaration (Loc,
427 Defining_Identifier => Pref,
428 Subtype_Mark => New_Occurrence_Of (Standard_String, Loc),
429 Name => Make_Identifier (Loc, Name_uTask_Name)));
432 Indx := First_Index (A_Type);
433 Val := First (Expressions (Id_Ref));
435 for J in 1 .. Dims loop
436 T := Make_Defining_Identifier (Loc, New_Internal_Name ('T'));
440 Make_Object_Declaration (Loc,
441 Defining_Identifier => T,
442 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
444 Make_Attribute_Reference (Loc,
445 Attribute_Name => Name_Image,
447 New_Occurrence_Of (Etype (Indx), Loc),
448 Expressions => New_List (
449 New_Copy_Tree (Val)))));
455 Sum := Make_Integer_Literal (Loc, Dims + 1);
461 Make_Attribute_Reference (Loc,
462 Attribute_Name => Name_Length,
464 New_Occurrence_Of (Pref, Loc),
465 Expressions => New_List (Make_Integer_Literal (Loc, 1))));
467 for J in 1 .. Dims loop
472 Make_Attribute_Reference (Loc,
473 Attribute_Name => Name_Length,
475 New_Occurrence_Of (Temps (J), Loc),
476 Expressions => New_List (Make_Integer_Literal (Loc, 1))));
479 Build_Task_Image_Prefix (Loc, Len, Res, Pos, Pref, Sum, Decls, Stats);
481 Set_Character_Literal_Name (Char_Code (Character'Pos ('(')));
484 Make_Assignment_Statement (Loc,
485 Name => Make_Indexed_Component (Loc,
486 Prefix => New_Occurrence_Of (Res, Loc),
487 Expressions => New_List (New_Occurrence_Of (Pos, Loc))),
489 Make_Character_Literal (Loc,
491 Char_Literal_Value =>
492 UI_From_Int (Character'Pos ('(')))));
495 Make_Assignment_Statement (Loc,
496 Name => New_Occurrence_Of (Pos, Loc),
499 Left_Opnd => New_Occurrence_Of (Pos, Loc),
500 Right_Opnd => Make_Integer_Literal (Loc, 1))));
502 for J in 1 .. Dims loop
505 Make_Assignment_Statement (Loc,
506 Name => Make_Slice (Loc,
507 Prefix => New_Occurrence_Of (Res, Loc),
510 Low_Bound => New_Occurrence_Of (Pos, Loc),
511 High_Bound => Make_Op_Subtract (Loc,
514 Left_Opnd => New_Occurrence_Of (Pos, Loc),
516 Make_Attribute_Reference (Loc,
517 Attribute_Name => Name_Length,
519 New_Occurrence_Of (Temps (J), Loc),
521 New_List (Make_Integer_Literal (Loc, 1)))),
522 Right_Opnd => Make_Integer_Literal (Loc, 1)))),
524 Expression => New_Occurrence_Of (Temps (J), Loc)));
528 Make_Assignment_Statement (Loc,
529 Name => New_Occurrence_Of (Pos, Loc),
532 Left_Opnd => New_Occurrence_Of (Pos, Loc),
534 Make_Attribute_Reference (Loc,
535 Attribute_Name => Name_Length,
536 Prefix => New_Occurrence_Of (Temps (J), Loc),
538 New_List (Make_Integer_Literal (Loc, 1))))));
540 Set_Character_Literal_Name (Char_Code (Character'Pos (',')));
543 Make_Assignment_Statement (Loc,
544 Name => Make_Indexed_Component (Loc,
545 Prefix => New_Occurrence_Of (Res, Loc),
546 Expressions => New_List (New_Occurrence_Of (Pos, Loc))),
548 Make_Character_Literal (Loc,
550 Char_Literal_Value =>
551 UI_From_Int (Character'Pos (',')))));
554 Make_Assignment_Statement (Loc,
555 Name => New_Occurrence_Of (Pos, Loc),
558 Left_Opnd => New_Occurrence_Of (Pos, Loc),
559 Right_Opnd => Make_Integer_Literal (Loc, 1))));
563 Set_Character_Literal_Name (Char_Code (Character'Pos (')')));
566 Make_Assignment_Statement (Loc,
567 Name => Make_Indexed_Component (Loc,
568 Prefix => New_Occurrence_Of (Res, Loc),
569 Expressions => New_List (New_Occurrence_Of (Len, Loc))),
571 Make_Character_Literal (Loc,
573 Char_Literal_Value =>
574 UI_From_Int (Character'Pos (')')))));
575 return Build_Task_Image_Function (Loc, Decls, Stats, Res);
576 end Build_Task_Array_Image;
578 ----------------------------
579 -- Build_Task_Image_Decls --
580 ----------------------------
582 function Build_Task_Image_Decls
586 In_Init_Proc : Boolean := False) return List_Id
588 Decls : constant List_Id := New_List;
589 T_Id : Entity_Id := Empty;
591 Expr : Node_Id := Empty;
592 Fun : Node_Id := Empty;
593 Is_Dyn : constant Boolean :=
594 Nkind (Parent (Id_Ref)) = N_Assignment_Statement
596 Nkind (Expression (Parent (Id_Ref))) = N_Allocator;
599 -- If Discard_Names or No_Implicit_Heap_Allocations are in effect,
600 -- generate a dummy declaration only.
602 if Restriction_Active (No_Implicit_Heap_Allocations)
603 or else Global_Discard_Names
605 T_Id := Make_Defining_Identifier (Loc, New_Internal_Name ('J'));
610 Make_Object_Declaration (Loc,
611 Defining_Identifier => T_Id,
612 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
614 Make_String_Literal (Loc,
615 Strval => String_From_Name_Buffer)));
618 if Nkind (Id_Ref) = N_Identifier
619 or else Nkind (Id_Ref) = N_Defining_Identifier
621 -- For a simple variable, the image of the task is built from
622 -- the name of the variable. To avoid possible conflict with
623 -- the anonymous type created for a single protected object,
624 -- add a numeric suffix.
627 Make_Defining_Identifier (Loc,
628 New_External_Name (Chars (Id_Ref), 'T', 1));
630 Get_Name_String (Chars (Id_Ref));
633 Make_String_Literal (Loc,
634 Strval => String_From_Name_Buffer);
636 elsif Nkind (Id_Ref) = N_Selected_Component then
638 Make_Defining_Identifier (Loc,
639 New_External_Name (Chars (Selector_Name (Id_Ref)), 'T'));
640 Fun := Build_Task_Record_Image (Loc, Id_Ref, Is_Dyn);
642 elsif Nkind (Id_Ref) = N_Indexed_Component then
644 Make_Defining_Identifier (Loc,
645 New_External_Name (Chars (A_Type), 'N'));
647 Fun := Build_Task_Array_Image (Loc, Id_Ref, A_Type, Is_Dyn);
651 if Present (Fun) then
653 Expr := Make_Function_Call (Loc,
654 Name => New_Occurrence_Of (Defining_Entity (Fun), Loc));
656 if not In_Init_Proc then
657 Set_Uses_Sec_Stack (Defining_Entity (Fun));
661 Decl := Make_Object_Declaration (Loc,
662 Defining_Identifier => T_Id,
663 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
664 Constant_Present => True,
667 Append (Decl, Decls);
669 end Build_Task_Image_Decls;
671 -------------------------------
672 -- Build_Task_Image_Function --
673 -------------------------------
675 function Build_Task_Image_Function
679 Res : Entity_Id) return Node_Id
685 Make_Return_Statement (Loc,
686 Expression => New_Occurrence_Of (Res, Loc)));
688 Spec := Make_Function_Specification (Loc,
689 Defining_Unit_Name =>
690 Make_Defining_Identifier (Loc, New_Internal_Name ('F')),
691 Result_Definition => New_Occurrence_Of (Standard_String, Loc));
693 -- Calls to 'Image use the secondary stack, which must be cleaned
694 -- up after the task name is built.
696 return Make_Subprogram_Body (Loc,
697 Specification => Spec,
698 Declarations => Decls,
699 Handled_Statement_Sequence =>
700 Make_Handled_Sequence_Of_Statements (Loc, Statements => Stats));
701 end Build_Task_Image_Function;
703 -----------------------------
704 -- Build_Task_Image_Prefix --
705 -----------------------------
707 procedure Build_Task_Image_Prefix
714 Decls : in out List_Id;
715 Stats : in out List_Id)
718 Len := Make_Defining_Identifier (Loc, New_Internal_Name ('L'));
721 Make_Object_Declaration (Loc,
722 Defining_Identifier => Len,
723 Object_Definition => New_Occurrence_Of (Standard_Integer, Loc),
726 Res := Make_Defining_Identifier (Loc, New_Internal_Name ('R'));
729 Make_Object_Declaration (Loc,
730 Defining_Identifier => Res,
732 Make_Subtype_Indication (Loc,
733 Subtype_Mark => New_Occurrence_Of (Standard_String, Loc),
735 Make_Index_Or_Discriminant_Constraint (Loc,
739 Low_Bound => Make_Integer_Literal (Loc, 1),
740 High_Bound => New_Occurrence_Of (Len, Loc)))))));
742 Pos := Make_Defining_Identifier (Loc, New_Internal_Name ('P'));
745 Make_Object_Declaration (Loc,
746 Defining_Identifier => Pos,
747 Object_Definition => New_Occurrence_Of (Standard_Integer, Loc)));
749 -- Pos := Prefix'Length;
752 Make_Assignment_Statement (Loc,
753 Name => New_Occurrence_Of (Pos, Loc),
755 Make_Attribute_Reference (Loc,
756 Attribute_Name => Name_Length,
757 Prefix => New_Occurrence_Of (Prefix, Loc),
759 New_List (Make_Integer_Literal (Loc, 1)))));
761 -- Res (1 .. Pos) := Prefix;
764 Make_Assignment_Statement (Loc,
765 Name => Make_Slice (Loc,
766 Prefix => New_Occurrence_Of (Res, Loc),
769 Low_Bound => Make_Integer_Literal (Loc, 1),
770 High_Bound => New_Occurrence_Of (Pos, Loc))),
772 Expression => New_Occurrence_Of (Prefix, Loc)));
775 Make_Assignment_Statement (Loc,
776 Name => New_Occurrence_Of (Pos, Loc),
779 Left_Opnd => New_Occurrence_Of (Pos, Loc),
780 Right_Opnd => Make_Integer_Literal (Loc, 1))));
781 end Build_Task_Image_Prefix;
783 -----------------------------
784 -- Build_Task_Record_Image --
785 -----------------------------
787 function Build_Task_Record_Image
790 Dyn : Boolean := False) return Node_Id
793 -- Total length of generated name
799 -- String to hold result
802 -- Name of enclosing variable, prefix of resulting name
805 -- Expression to compute total size of string
808 -- Entity for selector name
810 Decls : List_Id := New_List;
811 Stats : List_Id := New_List;
814 Pref := Make_Defining_Identifier (Loc, New_Internal_Name ('P'));
816 -- For a dynamic task, the name comes from the target variable.
817 -- For a static one it is a formal of the enclosing init proc.
820 Get_Name_String (Chars (Entity (Prefix (Id_Ref))));
822 Make_Object_Declaration (Loc,
823 Defining_Identifier => Pref,
824 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
826 Make_String_Literal (Loc,
827 Strval => String_From_Name_Buffer)));
831 Make_Object_Renaming_Declaration (Loc,
832 Defining_Identifier => Pref,
833 Subtype_Mark => New_Occurrence_Of (Standard_String, Loc),
834 Name => Make_Identifier (Loc, Name_uTask_Name)));
837 Sel := Make_Defining_Identifier (Loc, New_Internal_Name ('S'));
839 Get_Name_String (Chars (Selector_Name (Id_Ref)));
842 Make_Object_Declaration (Loc,
843 Defining_Identifier => Sel,
844 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
846 Make_String_Literal (Loc,
847 Strval => String_From_Name_Buffer)));
849 Sum := Make_Integer_Literal (Loc, Nat (Name_Len + 1));
855 Make_Attribute_Reference (Loc,
856 Attribute_Name => Name_Length,
858 New_Occurrence_Of (Pref, Loc),
859 Expressions => New_List (Make_Integer_Literal (Loc, 1))));
861 Build_Task_Image_Prefix (Loc, Len, Res, Pos, Pref, Sum, Decls, Stats);
863 Set_Character_Literal_Name (Char_Code (Character'Pos ('.')));
868 Make_Assignment_Statement (Loc,
869 Name => Make_Indexed_Component (Loc,
870 Prefix => New_Occurrence_Of (Res, Loc),
871 Expressions => New_List (New_Occurrence_Of (Pos, Loc))),
873 Make_Character_Literal (Loc,
875 Char_Literal_Value =>
876 UI_From_Int (Character'Pos ('.')))));
879 Make_Assignment_Statement (Loc,
880 Name => New_Occurrence_Of (Pos, Loc),
883 Left_Opnd => New_Occurrence_Of (Pos, Loc),
884 Right_Opnd => Make_Integer_Literal (Loc, 1))));
886 -- Res (Pos .. Len) := Selector;
889 Make_Assignment_Statement (Loc,
890 Name => Make_Slice (Loc,
891 Prefix => New_Occurrence_Of (Res, Loc),
894 Low_Bound => New_Occurrence_Of (Pos, Loc),
895 High_Bound => New_Occurrence_Of (Len, Loc))),
896 Expression => New_Occurrence_Of (Sel, Loc)));
898 return Build_Task_Image_Function (Loc, Decls, Stats, Res);
899 end Build_Task_Record_Image;
901 ----------------------------------
902 -- Component_May_Be_Bit_Aligned --
903 ----------------------------------
905 function Component_May_Be_Bit_Aligned (Comp : Entity_Id) return Boolean is
907 -- If no component clause, then everything is fine, since the
908 -- back end never bit-misaligns by default, even if there is
909 -- a pragma Packed for the record.
911 if No (Component_Clause (Comp)) then
915 -- It is only array and record types that cause trouble
917 if not Is_Record_Type (Etype (Comp))
918 and then not Is_Array_Type (Etype (Comp))
922 -- If we know that we have a small (64 bits or less) record
923 -- or bit-packed array, then everything is fine, since the
924 -- back end can handle these cases correctly.
926 elsif Esize (Comp) <= 64
927 and then (Is_Record_Type (Etype (Comp))
928 or else Is_Bit_Packed_Array (Etype (Comp)))
932 -- Otherwise if the component is not byte aligned, we
933 -- know we have the nasty unaligned case.
935 elsif Normalized_First_Bit (Comp) /= Uint_0
936 or else Esize (Comp) mod System_Storage_Unit /= Uint_0
940 -- If we are large and byte aligned, then OK at this level
945 end Component_May_Be_Bit_Aligned;
947 -------------------------------
948 -- Convert_To_Actual_Subtype --
949 -------------------------------
951 procedure Convert_To_Actual_Subtype (Exp : Entity_Id) is
955 Act_ST := Get_Actual_Subtype (Exp);
957 if Act_ST = Etype (Exp) then
962 Convert_To (Act_ST, Relocate_Node (Exp)));
963 Analyze_And_Resolve (Exp, Act_ST);
965 end Convert_To_Actual_Subtype;
967 -----------------------------------
968 -- Current_Sem_Unit_Declarations --
969 -----------------------------------
971 function Current_Sem_Unit_Declarations return List_Id is
972 U : Node_Id := Unit (Cunit (Current_Sem_Unit));
976 -- If the current unit is a package body, locate the visible
977 -- declarations of the package spec.
979 if Nkind (U) = N_Package_Body then
980 U := Unit (Library_Unit (Cunit (Current_Sem_Unit)));
983 if Nkind (U) = N_Package_Declaration then
984 U := Specification (U);
985 Decls := Visible_Declarations (U);
989 Set_Visible_Declarations (U, Decls);
993 Decls := Declarations (U);
997 Set_Declarations (U, Decls);
1002 end Current_Sem_Unit_Declarations;
1004 -----------------------
1005 -- Duplicate_Subexpr --
1006 -----------------------
1008 function Duplicate_Subexpr
1010 Name_Req : Boolean := False) return Node_Id
1013 Remove_Side_Effects (Exp, Name_Req);
1014 return New_Copy_Tree (Exp);
1015 end Duplicate_Subexpr;
1017 ---------------------------------
1018 -- Duplicate_Subexpr_No_Checks --
1019 ---------------------------------
1021 function Duplicate_Subexpr_No_Checks
1023 Name_Req : Boolean := False) return Node_Id
1028 Remove_Side_Effects (Exp, Name_Req);
1029 New_Exp := New_Copy_Tree (Exp);
1030 Remove_Checks (New_Exp);
1032 end Duplicate_Subexpr_No_Checks;
1034 -----------------------------------
1035 -- Duplicate_Subexpr_Move_Checks --
1036 -----------------------------------
1038 function Duplicate_Subexpr_Move_Checks
1040 Name_Req : Boolean := False) return Node_Id
1045 Remove_Side_Effects (Exp, Name_Req);
1046 New_Exp := New_Copy_Tree (Exp);
1047 Remove_Checks (Exp);
1049 end Duplicate_Subexpr_Move_Checks;
1051 --------------------
1052 -- Ensure_Defined --
1053 --------------------
1055 procedure Ensure_Defined (Typ : Entity_Id; N : Node_Id) is
1060 if Is_Itype (Typ) then
1061 IR := Make_Itype_Reference (Sloc (N));
1062 Set_Itype (IR, Typ);
1064 if not In_Open_Scopes (Scope (Typ))
1065 and then Is_Subprogram (Current_Scope)
1066 and then Scope (Current_Scope) /= Standard_Standard
1068 -- Insert node in front of subprogram, to avoid scope anomalies
1073 and then Nkind (P) /= N_Subprogram_Body
1079 Insert_Action (P, IR);
1081 Insert_Action (N, IR);
1085 Insert_Action (N, IR);
1090 ---------------------
1091 -- Evolve_And_Then --
1092 ---------------------
1094 procedure Evolve_And_Then (Cond : in out Node_Id; Cond1 : Node_Id) is
1100 Make_And_Then (Sloc (Cond1),
1102 Right_Opnd => Cond1);
1104 end Evolve_And_Then;
1106 --------------------
1107 -- Evolve_Or_Else --
1108 --------------------
1110 procedure Evolve_Or_Else (Cond : in out Node_Id; Cond1 : Node_Id) is
1116 Make_Or_Else (Sloc (Cond1),
1118 Right_Opnd => Cond1);
1122 ------------------------------
1123 -- Expand_Subtype_From_Expr --
1124 ------------------------------
1126 -- This function is applicable for both static and dynamic allocation of
1127 -- objects which are constrained by an initial expression. Basically it
1128 -- transforms an unconstrained subtype indication into a constrained one.
1129 -- The expression may also be transformed in certain cases in order to
1130 -- avoid multiple evaluation. In the static allocation case, the general
1135 -- is transformed into
1137 -- Val : Constrained_Subtype_of_T := Maybe_Modified_Expr;
1139 -- Here are the main cases :
1141 -- <if Expr is a Slice>
1142 -- Val : T ([Index_Subtype (Expr)]) := Expr;
1144 -- <elsif Expr is a String Literal>
1145 -- Val : T (T'First .. T'First + Length (string literal) - 1) := Expr;
1147 -- <elsif Expr is Constrained>
1148 -- subtype T is Type_Of_Expr
1151 -- <elsif Expr is an entity_name>
1152 -- Val : T (constraints taken from Expr) := Expr;
1155 -- type Axxx is access all T;
1156 -- Rval : Axxx := Expr'ref;
1157 -- Val : T (constraints taken from Rval) := Rval.all;
1159 -- ??? note: when the Expression is allocated in the secondary stack
1160 -- we could use it directly instead of copying it by declaring
1161 -- Val : T (...) renames Rval.all
1163 procedure Expand_Subtype_From_Expr
1165 Unc_Type : Entity_Id;
1166 Subtype_Indic : Node_Id;
1169 Loc : constant Source_Ptr := Sloc (N);
1170 Exp_Typ : constant Entity_Id := Etype (Exp);
1174 -- In general we cannot build the subtype if expansion is disabled,
1175 -- because internal entities may not have been defined. However, to
1176 -- avoid some cascaded errors, we try to continue when the expression
1177 -- is an array (or string), because it is safe to compute the bounds.
1178 -- It is in fact required to do so even in a generic context, because
1179 -- there may be constants that depend on bounds of string literal.
1181 if not Expander_Active
1182 and then (No (Etype (Exp))
1183 or else Base_Type (Etype (Exp)) /= Standard_String)
1188 if Nkind (Exp) = N_Slice then
1190 Slice_Type : constant Entity_Id := Etype (First_Index (Exp_Typ));
1193 Rewrite (Subtype_Indic,
1194 Make_Subtype_Indication (Loc,
1195 Subtype_Mark => New_Reference_To (Unc_Type, Loc),
1197 Make_Index_Or_Discriminant_Constraint (Loc,
1198 Constraints => New_List
1199 (New_Reference_To (Slice_Type, Loc)))));
1201 -- This subtype indication may be used later for contraint checks
1202 -- we better make sure that if a variable was used as a bound of
1203 -- of the original slice, its value is frozen.
1205 Force_Evaluation (Low_Bound (Scalar_Range (Slice_Type)));
1206 Force_Evaluation (High_Bound (Scalar_Range (Slice_Type)));
1209 elsif Ekind (Exp_Typ) = E_String_Literal_Subtype then
1210 Rewrite (Subtype_Indic,
1211 Make_Subtype_Indication (Loc,
1212 Subtype_Mark => New_Reference_To (Unc_Type, Loc),
1214 Make_Index_Or_Discriminant_Constraint (Loc,
1215 Constraints => New_List (
1216 Make_Literal_Range (Loc,
1217 Literal_Typ => Exp_Typ)))));
1219 elsif Is_Constrained (Exp_Typ)
1220 and then not Is_Class_Wide_Type (Unc_Type)
1222 if Is_Itype (Exp_Typ) then
1224 -- Within an initialization procedure, a selected component
1225 -- denotes a component of the enclosing record, and it appears
1226 -- as an actual in a call to its own initialization procedure.
1227 -- If this component depends on the outer discriminant, we must
1228 -- generate the proper actual subtype for it.
1230 if Nkind (Exp) = N_Selected_Component
1231 and then Within_Init_Proc
1234 Decl : constant Node_Id :=
1235 Build_Actual_Subtype_Of_Component (Exp_Typ, Exp);
1237 if Present (Decl) then
1238 Insert_Action (N, Decl);
1239 T := Defining_Identifier (Decl);
1245 -- No need to generate a new one (new what???)
1253 Make_Defining_Identifier (Loc,
1254 Chars => New_Internal_Name ('T'));
1257 Make_Subtype_Declaration (Loc,
1258 Defining_Identifier => T,
1259 Subtype_Indication => New_Reference_To (Exp_Typ, Loc)));
1261 -- This type is marked as an itype even though it has an
1262 -- explicit declaration because otherwise it can be marked
1263 -- with Is_Generic_Actual_Type and generate spurious errors.
1264 -- (see sem_ch8.Analyze_Package_Renaming and sem_type.covers)
1267 Set_Associated_Node_For_Itype (T, Exp);
1270 Rewrite (Subtype_Indic, New_Reference_To (T, Loc));
1272 -- nothing needs to be done for private types with unknown discriminants
1273 -- if the underlying type is not an unconstrained composite type.
1275 elsif Is_Private_Type (Unc_Type)
1276 and then Has_Unknown_Discriminants (Unc_Type)
1277 and then (not Is_Composite_Type (Underlying_Type (Unc_Type))
1278 or else Is_Constrained (Underlying_Type (Unc_Type)))
1282 -- Nothing to be done for derived types with unknown discriminants if
1283 -- the parent type also has unknown discriminants.
1285 elsif Is_Record_Type (Unc_Type)
1286 and then not Is_Class_Wide_Type (Unc_Type)
1287 and then Has_Unknown_Discriminants (Unc_Type)
1288 and then Has_Unknown_Discriminants (Underlying_Type (Unc_Type))
1292 -- Nothing to be done if the type of the expression is limited, because
1293 -- in this case the expression cannot be copied, and its use can only
1294 -- be by reference and there is no need for the actual subtype.
1296 elsif Is_Limited_Type (Exp_Typ) then
1300 Remove_Side_Effects (Exp);
1301 Rewrite (Subtype_Indic,
1302 Make_Subtype_From_Expr (Exp, Unc_Type));
1304 end Expand_Subtype_From_Expr;
1306 ------------------------
1307 -- Find_Interface_ADT --
1308 ------------------------
1310 function Find_Interface_ADT
1312 Iface : Entity_Id) return Entity_Id
1315 Found : Boolean := False;
1316 Typ : Entity_Id := T;
1318 procedure Find_Secondary_Table (Typ : Entity_Id);
1319 -- Internal subprogram used to recursively climb to the ancestors
1321 --------------------------
1322 -- Find_Secondary_Table --
1323 --------------------------
1325 procedure Find_Secondary_Table (Typ : Entity_Id) is
1330 pragma Assert (Typ /= Iface);
1332 -- Climb to the ancestor (if any) handling synchronized interface
1333 -- derivations and private types
1335 if Is_Concurrent_Record_Type (Typ) then
1337 Iface_List : constant List_Id := Abstract_Interface_List (Typ);
1340 if Is_Non_Empty_List (Iface_List) then
1341 Find_Secondary_Table (Etype (First (Iface_List)));
1345 elsif Present (Full_View (Etype (Typ))) then
1346 if Full_View (Etype (Typ)) /= Typ then
1347 Find_Secondary_Table (Full_View (Etype (Typ)));
1350 elsif Etype (Typ) /= Typ then
1351 Find_Secondary_Table (Etype (Typ));
1354 -- Traverse the list of interfaces implemented by the type
1357 and then Present (Abstract_Interfaces (Typ))
1358 and then not Is_Empty_Elmt_List (Abstract_Interfaces (Typ))
1360 AI_Elmt := First_Elmt (Abstract_Interfaces (Typ));
1361 while Present (AI_Elmt) loop
1362 AI := Node (AI_Elmt);
1364 if AI = Iface or else Is_Ancestor (Iface, AI) then
1370 Next_Elmt (AI_Elmt);
1373 end Find_Secondary_Table;
1375 -- Start of processing for Find_Interface_ADT
1378 pragma Assert (Is_Interface (Iface));
1380 -- Handle private types
1382 if Has_Private_Declaration (Typ)
1383 and then Present (Full_View (Typ))
1385 Typ := Full_View (Typ);
1388 -- Handle access types
1390 if Is_Access_Type (Typ) then
1391 Typ := Directly_Designated_Type (Typ);
1394 -- Handle task and protected types implementing interfaces
1396 if Is_Concurrent_Type (Typ) then
1397 Typ := Corresponding_Record_Type (Typ);
1401 (not Is_Class_Wide_Type (Typ)
1402 and then Ekind (Typ) /= E_Incomplete_Type);
1404 ADT := Next_Elmt (First_Elmt (Access_Disp_Table (Typ)));
1405 pragma Assert (Present (Node (ADT)));
1406 Find_Secondary_Table (Typ);
1407 pragma Assert (Found);
1409 end Find_Interface_ADT;
1411 ------------------------
1412 -- Find_Interface_Tag --
1413 ------------------------
1415 function Find_Interface_Tag
1417 Iface : Entity_Id) return Entity_Id
1420 Found : Boolean := False;
1421 Typ : Entity_Id := T;
1423 Is_Primary_Tag : Boolean := False;
1425 Is_Sync_Typ : Boolean := False;
1426 -- In case of non concurrent-record-types each parent-type has the
1427 -- tags associated with the interface types that are not implemented
1428 -- by the ancestors; concurrent-record-types have their whole list of
1429 -- interface tags (and this case requires some special management).
1431 procedure Find_Tag (Typ : Entity_Id);
1432 -- Internal subprogram used to recursively climb to the ancestors
1438 procedure Find_Tag (Typ : Entity_Id) is
1443 -- Check if the interface is an immediate ancestor of the type and
1444 -- therefore shares the main tag.
1448 Is_Primary_Tag := True;
1451 (Etype (First_Tag_Component (Typ)) = RTE (RE_Tag));
1452 AI_Tag := First_Tag_Component (Typ);
1459 -- Handle synchronized interface derivations
1461 if Is_Concurrent_Record_Type (Typ) then
1463 Iface_List : constant List_Id := Abstract_Interface_List (Typ);
1465 if Is_Non_Empty_List (Iface_List) then
1466 Find_Tag (Etype (First (Iface_List)));
1470 -- Climb to the root type handling private types
1472 elsif Present (Full_View (Etype (Typ))) then
1473 if Full_View (Etype (Typ)) /= Typ then
1474 Find_Tag (Full_View (Etype (Typ)));
1477 elsif Etype (Typ) /= Typ then
1478 Find_Tag (Etype (Typ));
1481 -- Traverse the list of interfaces implemented by the type
1484 and then Present (Abstract_Interfaces (Typ))
1485 and then not (Is_Empty_Elmt_List (Abstract_Interfaces (Typ)))
1487 -- Skip the tag associated with the primary table
1489 if not Is_Sync_Typ then
1491 (Etype (First_Tag_Component (Typ)) = RTE (RE_Tag));
1492 AI_Tag := Next_Tag_Component (First_Tag_Component (Typ));
1493 pragma Assert (Present (AI_Tag));
1496 AI_Elmt := First_Elmt (Abstract_Interfaces (Typ));
1497 while Present (AI_Elmt) loop
1498 AI := Node (AI_Elmt);
1500 if AI = Iface or else Is_Ancestor (Iface, AI) then
1505 AI_Tag := Next_Tag_Component (AI_Tag);
1506 Next_Elmt (AI_Elmt);
1511 -- Start of processing for Find_Interface_Tag
1514 pragma Assert (Is_Interface (Iface));
1516 -- Handle private types
1518 if Has_Private_Declaration (Typ)
1519 and then Present (Full_View (Typ))
1521 Typ := Full_View (Typ);
1524 -- Handle access types
1526 if Is_Access_Type (Typ) then
1527 Typ := Directly_Designated_Type (Typ);
1530 -- Handle task and protected types implementing interfaces
1532 if Is_Concurrent_Type (Typ) then
1533 Typ := Corresponding_Record_Type (Typ);
1536 if Is_Class_Wide_Type (Typ) then
1540 -- Handle entities from the limited view
1542 if Ekind (Typ) = E_Incomplete_Type then
1543 pragma Assert (Present (Non_Limited_View (Typ)));
1544 Typ := Non_Limited_View (Typ);
1547 if not Is_Concurrent_Record_Type (Typ) then
1549 pragma Assert (Found);
1552 -- Concurrent record types
1555 Is_Sync_Typ := True;
1556 AI_Tag := Next_Tag_Component (First_Tag_Component (Typ));
1558 pragma Assert (Found);
1560 if Is_Primary_Tag then
1561 return First_Tag_Component (Typ);
1566 end Find_Interface_Tag;
1568 --------------------
1569 -- Find_Interface --
1570 --------------------
1572 function Find_Interface
1574 Comp : Entity_Id) return Entity_Id
1577 Found : Boolean := False;
1579 Typ : Entity_Id := T;
1581 Is_Sync_Typ : Boolean := False;
1582 -- In case of non concurrent-record-types each parent-type has the
1583 -- tags associated with the interface types that are not implemented
1584 -- by the ancestors; concurrent-record-types have their whole list of
1585 -- interface tags (and this case requires some special management).
1587 procedure Find_Iface (Typ : Entity_Id);
1588 -- Internal subprogram used to recursively climb to the ancestors
1594 procedure Find_Iface (Typ : Entity_Id) is
1598 -- Climb to the root type
1600 -- Handle sychronized interface derivations
1602 if Is_Concurrent_Record_Type (Typ) then
1604 Iface_List : constant List_Id := Abstract_Interface_List (Typ);
1606 if Is_Non_Empty_List (Iface_List) then
1607 Find_Iface (Etype (First (Iface_List)));
1611 -- Handle the common case
1613 elsif Etype (Typ) /= Typ then
1614 pragma Assert (not Present (Full_View (Etype (Typ))));
1615 Find_Iface (Etype (Typ));
1618 -- Traverse the list of interfaces implemented by the type
1621 and then Present (Abstract_Interfaces (Typ))
1622 and then not (Is_Empty_Elmt_List (Abstract_Interfaces (Typ)))
1624 -- Skip the tag associated with the primary table
1626 if not Is_Sync_Typ then
1628 (Etype (First_Tag_Component (Typ)) = RTE (RE_Tag));
1629 AI_Tag := Next_Tag_Component (First_Tag_Component (Typ));
1630 pragma Assert (Present (AI_Tag));
1633 AI_Elmt := First_Elmt (Abstract_Interfaces (Typ));
1634 while Present (AI_Elmt) loop
1635 if AI_Tag = Comp then
1636 Iface := Node (AI_Elmt);
1641 AI_Tag := Next_Tag_Component (AI_Tag);
1642 Next_Elmt (AI_Elmt);
1647 -- Start of processing for Find_Interface
1650 -- Handle private types
1652 if Has_Private_Declaration (Typ)
1653 and then Present (Full_View (Typ))
1655 Typ := Full_View (Typ);
1658 -- Handle access types
1660 if Is_Access_Type (Typ) then
1661 Typ := Directly_Designated_Type (Typ);
1664 -- Handle task and protected types implementing interfaces
1666 if Is_Concurrent_Type (Typ) then
1667 Typ := Corresponding_Record_Type (Typ);
1670 if Is_Class_Wide_Type (Typ) then
1674 -- Handle entities from the limited view
1676 if Ekind (Typ) = E_Incomplete_Type then
1677 pragma Assert (Present (Non_Limited_View (Typ)));
1678 Typ := Non_Limited_View (Typ);
1681 if Is_Concurrent_Record_Type (Typ) then
1682 Is_Sync_Typ := True;
1683 AI_Tag := Next_Tag_Component (First_Tag_Component (Typ));
1687 pragma Assert (Found);
1695 function Find_Prim_Op (T : Entity_Id; Name : Name_Id) return Entity_Id is
1697 Typ : Entity_Id := T;
1701 if Is_Class_Wide_Type (Typ) then
1702 Typ := Root_Type (Typ);
1705 Typ := Underlying_Type (Typ);
1707 -- Loop through primitive operations
1709 Prim := First_Elmt (Primitive_Operations (Typ));
1710 while Present (Prim) loop
1713 -- We can retrieve primitive operations by name if it is an internal
1714 -- name. For equality we must check that both of its operands have
1715 -- the same type, to avoid confusion with user-defined equalities
1716 -- than may have a non-symmetric signature.
1718 exit when Chars (Op) = Name
1721 or else Etype (First_Entity (Op)) = Etype (Last_Entity (Op)));
1724 pragma Assert (Present (Prim));
1734 function Find_Prim_Op
1736 Name : TSS_Name_Type) return Entity_Id
1739 Typ : Entity_Id := T;
1742 if Is_Class_Wide_Type (Typ) then
1743 Typ := Root_Type (Typ);
1746 Typ := Underlying_Type (Typ);
1748 Prim := First_Elmt (Primitive_Operations (Typ));
1749 while not Is_TSS (Node (Prim), Name) loop
1751 pragma Assert (Present (Prim));
1757 ----------------------
1758 -- Force_Evaluation --
1759 ----------------------
1761 procedure Force_Evaluation (Exp : Node_Id; Name_Req : Boolean := False) is
1763 Remove_Side_Effects (Exp, Name_Req, Variable_Ref => True);
1764 end Force_Evaluation;
1766 ------------------------
1767 -- Generate_Poll_Call --
1768 ------------------------
1770 procedure Generate_Poll_Call (N : Node_Id) is
1772 -- No poll call if polling not active
1774 if not Polling_Required then
1777 -- Otherwise generate require poll call
1780 Insert_Before_And_Analyze (N,
1781 Make_Procedure_Call_Statement (Sloc (N),
1782 Name => New_Occurrence_Of (RTE (RE_Poll), Sloc (N))));
1784 end Generate_Poll_Call;
1786 ---------------------------------
1787 -- Get_Current_Value_Condition --
1788 ---------------------------------
1790 -- Note: the implementation of this procedure is very closely tied to the
1791 -- implementation of Set_Current_Value_Condition. In the Get procedure, we
1792 -- interpret Current_Value fields set by the Set procedure, so the two
1793 -- procedures need to be closely coordinated.
1795 procedure Get_Current_Value_Condition
1800 Loc : constant Source_Ptr := Sloc (Var);
1801 Ent : constant Entity_Id := Entity (Var);
1803 procedure Process_Current_Value_Condition
1806 -- N is an expression which holds either True (S = True) or False (S =
1807 -- False) in the condition. This procedure digs out the expression and
1808 -- if it refers to Ent, sets Op and Val appropriately.
1810 -------------------------------------
1811 -- Process_Current_Value_Condition --
1812 -------------------------------------
1814 procedure Process_Current_Value_Condition
1825 -- Deal with NOT operators, inverting sense
1827 while Nkind (Cond) = N_Op_Not loop
1828 Cond := Right_Opnd (Cond);
1832 -- Deal with AND THEN and AND cases
1834 if Nkind (Cond) = N_And_Then
1835 or else Nkind (Cond) = N_Op_And
1837 -- Don't ever try to invert a condition that is of the form
1838 -- of an AND or AND THEN (since we are not doing sufficiently
1839 -- general processing to allow this).
1841 if Sens = False then
1847 -- Recursively process AND and AND THEN branches
1849 Process_Current_Value_Condition (Left_Opnd (Cond), True);
1851 if Op /= N_Empty then
1855 Process_Current_Value_Condition (Right_Opnd (Cond), True);
1858 -- Case of relational operator
1860 elsif Nkind (Cond) in N_Op_Compare then
1863 -- Invert sense of test if inverted test
1865 if Sens = False then
1867 when N_Op_Eq => Op := N_Op_Ne;
1868 when N_Op_Ne => Op := N_Op_Eq;
1869 when N_Op_Lt => Op := N_Op_Ge;
1870 when N_Op_Gt => Op := N_Op_Le;
1871 when N_Op_Le => Op := N_Op_Gt;
1872 when N_Op_Ge => Op := N_Op_Lt;
1873 when others => raise Program_Error;
1877 -- Case of entity op value
1879 if Is_Entity_Name (Left_Opnd (Cond))
1880 and then Ent = Entity (Left_Opnd (Cond))
1881 and then Compile_Time_Known_Value (Right_Opnd (Cond))
1883 Val := Right_Opnd (Cond);
1885 -- Case of value op entity
1887 elsif Is_Entity_Name (Right_Opnd (Cond))
1888 and then Ent = Entity (Right_Opnd (Cond))
1889 and then Compile_Time_Known_Value (Left_Opnd (Cond))
1891 Val := Left_Opnd (Cond);
1893 -- We are effectively swapping operands
1896 when N_Op_Eq => null;
1897 when N_Op_Ne => null;
1898 when N_Op_Lt => Op := N_Op_Gt;
1899 when N_Op_Gt => Op := N_Op_Lt;
1900 when N_Op_Le => Op := N_Op_Ge;
1901 when N_Op_Ge => Op := N_Op_Le;
1902 when others => raise Program_Error;
1911 -- Case of Boolean variable reference, return as though the
1912 -- reference had said var = True.
1915 if Is_Entity_Name (Cond)
1916 and then Ent = Entity (Cond)
1918 Val := New_Occurrence_Of (Standard_True, Sloc (Cond));
1920 if Sens = False then
1927 end Process_Current_Value_Condition;
1929 -- Start of processing for Get_Current_Value_Condition
1935 -- Immediate return, nothing doing, if this is not an object
1937 if Ekind (Ent) not in Object_Kind then
1941 -- Otherwise examine current value
1944 CV : constant Node_Id := Current_Value (Ent);
1949 -- If statement. Condition is known true in THEN section, known False
1950 -- in any ELSIF or ELSE part, and unknown outside the IF statement.
1952 if Nkind (CV) = N_If_Statement then
1954 -- Before start of IF statement
1956 if Loc < Sloc (CV) then
1959 -- After end of IF statement
1961 elsif Loc >= Sloc (CV) + Text_Ptr (UI_To_Int (End_Span (CV))) then
1965 -- At this stage we know that we are within the IF statement, but
1966 -- unfortunately, the tree does not record the SLOC of the ELSE so
1967 -- we cannot use a simple SLOC comparison to distinguish between
1968 -- the then/else statements, so we have to climb the tree.
1975 while Parent (N) /= CV loop
1978 -- If we fall off the top of the tree, then that's odd, but
1979 -- perhaps it could occur in some error situation, and the
1980 -- safest response is simply to assume that the outcome of
1981 -- the condition is unknown. No point in bombing during an
1982 -- attempt to optimize things.
1989 -- Now we have N pointing to a node whose parent is the IF
1990 -- statement in question, so now we can tell if we are within
1991 -- the THEN statements.
1993 if Is_List_Member (N)
1994 and then List_Containing (N) = Then_Statements (CV)
1998 -- If the variable reference does not come from source, we
1999 -- cannot reliably tell whether it appears in the else part.
2000 -- In particular, if if appears in generated code for a node
2001 -- that requires finalization, it may be attached to a list
2002 -- that has not been yet inserted into the code. For now,
2003 -- treat it as unknown.
2005 elsif not Comes_From_Source (N) then
2008 -- Otherwise we must be in ELSIF or ELSE part
2015 -- ELSIF part. Condition is known true within the referenced
2016 -- ELSIF, known False in any subsequent ELSIF or ELSE part, and
2017 -- unknown before the ELSE part or after the IF statement.
2019 elsif Nkind (CV) = N_Elsif_Part then
2022 -- Before start of ELSIF part
2024 if Loc < Sloc (CV) then
2027 -- After end of IF statement
2029 elsif Loc >= Sloc (Stm) +
2030 Text_Ptr (UI_To_Int (End_Span (Stm)))
2035 -- Again we lack the SLOC of the ELSE, so we need to climb the
2036 -- tree to see if we are within the ELSIF part in question.
2043 while Parent (N) /= Stm loop
2046 -- If we fall off the top of the tree, then that's odd, but
2047 -- perhaps it could occur in some error situation, and the
2048 -- safest response is simply to assume that the outcome of
2049 -- the condition is unknown. No point in bombing during an
2050 -- attempt to optimize things.
2057 -- Now we have N pointing to a node whose parent is the IF
2058 -- statement in question, so see if is the ELSIF part we want.
2059 -- the THEN statements.
2064 -- Otherwise we must be in susbequent ELSIF or ELSE part
2071 -- Iteration scheme of while loop. The condition is known to be
2072 -- true within the body of the loop.
2074 elsif Nkind (CV) = N_Iteration_Scheme then
2076 Loop_Stmt : constant Node_Id := Parent (CV);
2079 -- Before start of body of loop
2081 if Loc < Sloc (Loop_Stmt) then
2084 -- After end of LOOP statement
2086 elsif Loc >= Sloc (End_Label (Loop_Stmt)) then
2089 -- We are within the body of the loop
2096 -- All other cases of Current_Value settings
2102 -- If we fall through here, then we have a reportable condition, Sens
2103 -- is True if the condition is true and False if it needs inverting.
2105 Process_Current_Value_Condition (Condition (CV), Sens);
2107 end Get_Current_Value_Condition;
2109 --------------------
2110 -- Homonym_Number --
2111 --------------------
2113 function Homonym_Number (Subp : Entity_Id) return Nat is
2119 Hom := Homonym (Subp);
2120 while Present (Hom) loop
2121 if Scope (Hom) = Scope (Subp) then
2125 Hom := Homonym (Hom);
2131 ------------------------------
2132 -- In_Unconditional_Context --
2133 ------------------------------
2135 function In_Unconditional_Context (Node : Node_Id) return Boolean is
2140 while Present (P) loop
2142 when N_Subprogram_Body =>
2145 when N_If_Statement =>
2148 when N_Loop_Statement =>
2151 when N_Case_Statement =>
2160 end In_Unconditional_Context;
2166 procedure Insert_Action (Assoc_Node : Node_Id; Ins_Action : Node_Id) is
2168 if Present (Ins_Action) then
2169 Insert_Actions (Assoc_Node, New_List (Ins_Action));
2173 -- Version with check(s) suppressed
2175 procedure Insert_Action
2176 (Assoc_Node : Node_Id; Ins_Action : Node_Id; Suppress : Check_Id)
2179 Insert_Actions (Assoc_Node, New_List (Ins_Action), Suppress);
2182 --------------------
2183 -- Insert_Actions --
2184 --------------------
2186 procedure Insert_Actions (Assoc_Node : Node_Id; Ins_Actions : List_Id) is
2190 Wrapped_Node : Node_Id := Empty;
2193 if No (Ins_Actions) or else Is_Empty_List (Ins_Actions) then
2197 -- Ignore insert of actions from inside default expression in the
2198 -- special preliminary analyze mode. Any insertions at this point
2199 -- have no relevance, since we are only doing the analyze to freeze
2200 -- the types of any static expressions. See section "Handling of
2201 -- Default Expressions" in the spec of package Sem for further details.
2203 if In_Default_Expression then
2207 -- If the action derives from stuff inside a record, then the actions
2208 -- are attached to the current scope, to be inserted and analyzed on
2209 -- exit from the scope. The reason for this is that we may also
2210 -- be generating freeze actions at the same time, and they must
2211 -- eventually be elaborated in the correct order.
2213 if Is_Record_Type (Current_Scope)
2214 and then not Is_Frozen (Current_Scope)
2216 if No (Scope_Stack.Table
2217 (Scope_Stack.Last).Pending_Freeze_Actions)
2219 Scope_Stack.Table (Scope_Stack.Last).Pending_Freeze_Actions :=
2224 Scope_Stack.Table (Scope_Stack.Last).Pending_Freeze_Actions);
2230 -- We now intend to climb up the tree to find the right point to
2231 -- insert the actions. We start at Assoc_Node, unless this node is
2232 -- a subexpression in which case we start with its parent. We do this
2233 -- for two reasons. First it speeds things up. Second, if Assoc_Node
2234 -- is itself one of the special nodes like N_And_Then, then we assume
2235 -- that an initial request to insert actions for such a node does not
2236 -- expect the actions to get deposited in the node for later handling
2237 -- when the node is expanded, since clearly the node is being dealt
2238 -- with by the caller. Note that in the subexpression case, N is
2239 -- always the child we came from.
2241 -- N_Raise_xxx_Error is an annoying special case, it is a statement
2242 -- if it has type Standard_Void_Type, and a subexpression otherwise.
2243 -- otherwise. Procedure attribute references are also statements.
2245 if Nkind (Assoc_Node) in N_Subexpr
2246 and then (Nkind (Assoc_Node) in N_Raise_xxx_Error
2247 or else Etype (Assoc_Node) /= Standard_Void_Type)
2248 and then (Nkind (Assoc_Node) /= N_Attribute_Reference
2250 not Is_Procedure_Attribute_Name
2251 (Attribute_Name (Assoc_Node)))
2253 P := Assoc_Node; -- ??? does not agree with above!
2254 N := Parent (Assoc_Node);
2256 -- Non-subexpression case. Note that N is initially Empty in this
2257 -- case (N is only guaranteed Non-Empty in the subexpr case).
2264 -- Capture root of the transient scope
2266 if Scope_Is_Transient then
2267 Wrapped_Node := Node_To_Be_Wrapped;
2271 pragma Assert (Present (P));
2275 -- Case of right operand of AND THEN or OR ELSE. Put the actions
2276 -- in the Actions field of the right operand. They will be moved
2277 -- out further when the AND THEN or OR ELSE operator is expanded.
2278 -- Nothing special needs to be done for the left operand since
2279 -- in that case the actions are executed unconditionally.
2281 when N_And_Then | N_Or_Else =>
2282 if N = Right_Opnd (P) then
2283 if Present (Actions (P)) then
2284 Insert_List_After_And_Analyze
2285 (Last (Actions (P)), Ins_Actions);
2287 Set_Actions (P, Ins_Actions);
2288 Analyze_List (Actions (P));
2294 -- Then or Else operand of conditional expression. Add actions to
2295 -- Then_Actions or Else_Actions field as appropriate. The actions
2296 -- will be moved further out when the conditional is expanded.
2298 when N_Conditional_Expression =>
2300 ThenX : constant Node_Id := Next (First (Expressions (P)));
2301 ElseX : constant Node_Id := Next (ThenX);
2304 -- Actions belong to the then expression, temporarily
2305 -- place them as Then_Actions of the conditional expr.
2306 -- They will be moved to the proper place later when
2307 -- the conditional expression is expanded.
2310 if Present (Then_Actions (P)) then
2311 Insert_List_After_And_Analyze
2312 (Last (Then_Actions (P)), Ins_Actions);
2314 Set_Then_Actions (P, Ins_Actions);
2315 Analyze_List (Then_Actions (P));
2320 -- Actions belong to the else expression, temporarily
2321 -- place them as Else_Actions of the conditional expr.
2322 -- They will be moved to the proper place later when
2323 -- the conditional expression is expanded.
2325 elsif N = ElseX then
2326 if Present (Else_Actions (P)) then
2327 Insert_List_After_And_Analyze
2328 (Last (Else_Actions (P)), Ins_Actions);
2330 Set_Else_Actions (P, Ins_Actions);
2331 Analyze_List (Else_Actions (P));
2336 -- Actions belong to the condition. In this case they are
2337 -- unconditionally executed, and so we can continue the
2338 -- search for the proper insert point.
2345 -- Case of appearing in the condition of a while expression or
2346 -- elsif. We insert the actions into the Condition_Actions field.
2347 -- They will be moved further out when the while loop or elsif
2350 when N_Iteration_Scheme |
2353 if N = Condition (P) then
2354 if Present (Condition_Actions (P)) then
2355 Insert_List_After_And_Analyze
2356 (Last (Condition_Actions (P)), Ins_Actions);
2358 Set_Condition_Actions (P, Ins_Actions);
2360 -- Set the parent of the insert actions explicitly.
2361 -- This is not a syntactic field, but we need the
2362 -- parent field set, in particular so that freeze
2363 -- can understand that it is dealing with condition
2364 -- actions, and properly insert the freezing actions.
2366 Set_Parent (Ins_Actions, P);
2367 Analyze_List (Condition_Actions (P));
2373 -- Statements, declarations, pragmas, representation clauses
2378 N_Procedure_Call_Statement |
2379 N_Statement_Other_Than_Procedure_Call |
2385 -- Representation_Clause
2388 N_Attribute_Definition_Clause |
2389 N_Enumeration_Representation_Clause |
2390 N_Record_Representation_Clause |
2394 N_Abstract_Subprogram_Declaration |
2396 N_Exception_Declaration |
2397 N_Exception_Renaming_Declaration |
2398 N_Formal_Abstract_Subprogram_Declaration |
2399 N_Formal_Concrete_Subprogram_Declaration |
2400 N_Formal_Object_Declaration |
2401 N_Formal_Type_Declaration |
2402 N_Full_Type_Declaration |
2403 N_Function_Instantiation |
2404 N_Generic_Function_Renaming_Declaration |
2405 N_Generic_Package_Declaration |
2406 N_Generic_Package_Renaming_Declaration |
2407 N_Generic_Procedure_Renaming_Declaration |
2408 N_Generic_Subprogram_Declaration |
2409 N_Implicit_Label_Declaration |
2410 N_Incomplete_Type_Declaration |
2411 N_Number_Declaration |
2412 N_Object_Declaration |
2413 N_Object_Renaming_Declaration |
2415 N_Package_Body_Stub |
2416 N_Package_Declaration |
2417 N_Package_Instantiation |
2418 N_Package_Renaming_Declaration |
2419 N_Private_Extension_Declaration |
2420 N_Private_Type_Declaration |
2421 N_Procedure_Instantiation |
2422 N_Protected_Body_Stub |
2423 N_Protected_Type_Declaration |
2424 N_Single_Task_Declaration |
2426 N_Subprogram_Body_Stub |
2427 N_Subprogram_Declaration |
2428 N_Subprogram_Renaming_Declaration |
2429 N_Subtype_Declaration |
2432 N_Task_Type_Declaration |
2434 -- Freeze entity behaves like a declaration or statement
2438 -- Do not insert here if the item is not a list member (this
2439 -- happens for example with a triggering statement, and the
2440 -- proper approach is to insert before the entire select).
2442 if not Is_List_Member (P) then
2445 -- Do not insert if parent of P is an N_Component_Association
2446 -- node (i.e. we are in the context of an N_Aggregate or
2447 -- N_Extension_Aggregate node. In this case we want to insert
2448 -- before the entire aggregate.
2450 elsif Nkind (Parent (P)) = N_Component_Association then
2453 -- Do not insert if the parent of P is either an N_Variant
2454 -- node or an N_Record_Definition node, meaning in either
2455 -- case that P is a member of a component list, and that
2456 -- therefore the actions should be inserted outside the
2457 -- complete record declaration.
2459 elsif Nkind (Parent (P)) = N_Variant
2460 or else Nkind (Parent (P)) = N_Record_Definition
2464 -- Do not insert freeze nodes within the loop generated for
2465 -- an aggregate, because they may be elaborated too late for
2466 -- subsequent use in the back end: within a package spec the
2467 -- loop is part of the elaboration procedure and is only
2468 -- elaborated during the second pass.
2469 -- If the loop comes from source, or the entity is local to
2470 -- the loop itself it must remain within.
2472 elsif Nkind (Parent (P)) = N_Loop_Statement
2473 and then not Comes_From_Source (Parent (P))
2474 and then Nkind (First (Ins_Actions)) = N_Freeze_Entity
2476 Scope (Entity (First (Ins_Actions))) /= Current_Scope
2480 -- Otherwise we can go ahead and do the insertion
2482 elsif P = Wrapped_Node then
2483 Store_Before_Actions_In_Scope (Ins_Actions);
2487 Insert_List_Before_And_Analyze (P, Ins_Actions);
2491 -- A special case, N_Raise_xxx_Error can act either as a
2492 -- statement or a subexpression. We tell the difference
2493 -- by looking at the Etype. It is set to Standard_Void_Type
2494 -- in the statement case.
2497 N_Raise_xxx_Error =>
2498 if Etype (P) = Standard_Void_Type then
2499 if P = Wrapped_Node then
2500 Store_Before_Actions_In_Scope (Ins_Actions);
2502 Insert_List_Before_And_Analyze (P, Ins_Actions);
2507 -- In the subexpression case, keep climbing
2513 -- If a component association appears within a loop created for
2514 -- an array aggregate, attach the actions to the association so
2515 -- they can be subsequently inserted within the loop. For other
2516 -- component associations insert outside of the aggregate. For
2517 -- an association that will generate a loop, its Loop_Actions
2518 -- attribute is already initialized (see exp_aggr.adb).
2520 -- The list of loop_actions can in turn generate additional ones,
2521 -- that are inserted before the associated node. If the associated
2522 -- node is outside the aggregate, the new actions are collected
2523 -- at the end of the loop actions, to respect the order in which
2524 -- they are to be elaborated.
2527 N_Component_Association =>
2528 if Nkind (Parent (P)) = N_Aggregate
2529 and then Present (Loop_Actions (P))
2531 if Is_Empty_List (Loop_Actions (P)) then
2532 Set_Loop_Actions (P, Ins_Actions);
2533 Analyze_List (Ins_Actions);
2540 -- Check whether these actions were generated
2541 -- by a declaration that is part of the loop_
2542 -- actions for the component_association.
2545 while Present (Decl) loop
2546 exit when Parent (Decl) = P
2547 and then Is_List_Member (Decl)
2549 List_Containing (Decl) = Loop_Actions (P);
2550 Decl := Parent (Decl);
2553 if Present (Decl) then
2554 Insert_List_Before_And_Analyze
2555 (Decl, Ins_Actions);
2557 Insert_List_After_And_Analyze
2558 (Last (Loop_Actions (P)), Ins_Actions);
2569 -- Another special case, an attribute denoting a procedure call
2572 N_Attribute_Reference =>
2573 if Is_Procedure_Attribute_Name (Attribute_Name (P)) then
2574 if P = Wrapped_Node then
2575 Store_Before_Actions_In_Scope (Ins_Actions);
2577 Insert_List_Before_And_Analyze (P, Ins_Actions);
2582 -- In the subexpression case, keep climbing
2588 -- For all other node types, keep climbing tree
2592 N_Accept_Alternative |
2593 N_Access_Definition |
2594 N_Access_Function_Definition |
2595 N_Access_Procedure_Definition |
2596 N_Access_To_Object_Definition |
2599 N_Case_Statement_Alternative |
2600 N_Character_Literal |
2601 N_Compilation_Unit |
2602 N_Compilation_Unit_Aux |
2603 N_Component_Clause |
2604 N_Component_Declaration |
2605 N_Component_Definition |
2607 N_Constrained_Array_Definition |
2608 N_Decimal_Fixed_Point_Definition |
2609 N_Defining_Character_Literal |
2610 N_Defining_Identifier |
2611 N_Defining_Operator_Symbol |
2612 N_Defining_Program_Unit_Name |
2613 N_Delay_Alternative |
2614 N_Delta_Constraint |
2615 N_Derived_Type_Definition |
2617 N_Digits_Constraint |
2618 N_Discriminant_Association |
2619 N_Discriminant_Specification |
2621 N_Entry_Body_Formal_Part |
2622 N_Entry_Call_Alternative |
2623 N_Entry_Declaration |
2624 N_Entry_Index_Specification |
2625 N_Enumeration_Type_Definition |
2627 N_Exception_Handler |
2629 N_Explicit_Dereference |
2630 N_Extension_Aggregate |
2631 N_Floating_Point_Definition |
2632 N_Formal_Decimal_Fixed_Point_Definition |
2633 N_Formal_Derived_Type_Definition |
2634 N_Formal_Discrete_Type_Definition |
2635 N_Formal_Floating_Point_Definition |
2636 N_Formal_Modular_Type_Definition |
2637 N_Formal_Ordinary_Fixed_Point_Definition |
2638 N_Formal_Package_Declaration |
2639 N_Formal_Private_Type_Definition |
2640 N_Formal_Signed_Integer_Type_Definition |
2642 N_Function_Specification |
2643 N_Generic_Association |
2644 N_Handled_Sequence_Of_Statements |
2647 N_Index_Or_Discriminant_Constraint |
2648 N_Indexed_Component |
2652 N_Loop_Parameter_Specification |
2654 N_Modular_Type_Definition |
2680 N_Op_Shift_Right_Arithmetic |
2684 N_Ordinary_Fixed_Point_Definition |
2686 N_Package_Specification |
2687 N_Parameter_Association |
2688 N_Parameter_Specification |
2689 N_Pop_Constraint_Error_Label |
2690 N_Pop_Program_Error_Label |
2691 N_Pop_Storage_Error_Label |
2692 N_Pragma_Argument_Association |
2693 N_Procedure_Specification |
2695 N_Protected_Definition |
2696 N_Push_Constraint_Error_Label |
2697 N_Push_Program_Error_Label |
2698 N_Push_Storage_Error_Label |
2699 N_Qualified_Expression |
2701 N_Range_Constraint |
2703 N_Real_Range_Specification |
2704 N_Record_Definition |
2706 N_Selected_Component |
2707 N_Signed_Integer_Type_Definition |
2708 N_Single_Protected_Declaration |
2712 N_Subtype_Indication |
2715 N_Terminate_Alternative |
2716 N_Triggering_Alternative |
2718 N_Unchecked_Expression |
2719 N_Unchecked_Type_Conversion |
2720 N_Unconstrained_Array_Definition |
2723 N_Use_Package_Clause |
2727 N_Validate_Unchecked_Conversion |
2735 -- Make sure that inserted actions stay in the transient scope
2737 if P = Wrapped_Node then
2738 Store_Before_Actions_In_Scope (Ins_Actions);
2742 -- If we fall through above tests, keep climbing tree
2746 if Nkind (Parent (N)) = N_Subunit then
2748 -- This is the proper body corresponding to a stub. Insertion
2749 -- must be done at the point of the stub, which is in the decla-
2750 -- tive part of the parent unit.
2752 P := Corresponding_Stub (Parent (N));
2761 -- Version with check(s) suppressed
2763 procedure Insert_Actions
2764 (Assoc_Node : Node_Id; Ins_Actions : List_Id; Suppress : Check_Id)
2767 if Suppress = All_Checks then
2769 Svg : constant Suppress_Array := Scope_Suppress;
2771 Scope_Suppress := (others => True);
2772 Insert_Actions (Assoc_Node, Ins_Actions);
2773 Scope_Suppress := Svg;
2778 Svg : constant Boolean := Scope_Suppress (Suppress);
2780 Scope_Suppress (Suppress) := True;
2781 Insert_Actions (Assoc_Node, Ins_Actions);
2782 Scope_Suppress (Suppress) := Svg;
2787 --------------------------
2788 -- Insert_Actions_After --
2789 --------------------------
2791 procedure Insert_Actions_After
2792 (Assoc_Node : Node_Id;
2793 Ins_Actions : List_Id)
2796 if Scope_Is_Transient
2797 and then Assoc_Node = Node_To_Be_Wrapped
2799 Store_After_Actions_In_Scope (Ins_Actions);
2801 Insert_List_After_And_Analyze (Assoc_Node, Ins_Actions);
2803 end Insert_Actions_After;
2805 ---------------------------------
2806 -- Insert_Library_Level_Action --
2807 ---------------------------------
2809 procedure Insert_Library_Level_Action (N : Node_Id) is
2810 Aux : constant Node_Id := Aux_Decls_Node (Cunit (Main_Unit));
2813 New_Scope (Cunit_Entity (Main_Unit));
2815 if No (Actions (Aux)) then
2816 Set_Actions (Aux, New_List (N));
2818 Append (N, Actions (Aux));
2823 end Insert_Library_Level_Action;
2825 ----------------------------------
2826 -- Insert_Library_Level_Actions --
2827 ----------------------------------
2829 procedure Insert_Library_Level_Actions (L : List_Id) is
2830 Aux : constant Node_Id := Aux_Decls_Node (Cunit (Main_Unit));
2833 if Is_Non_Empty_List (L) then
2834 New_Scope (Cunit_Entity (Main_Unit));
2836 if No (Actions (Aux)) then
2837 Set_Actions (Aux, L);
2840 Insert_List_After_And_Analyze (Last (Actions (Aux)), L);
2845 end Insert_Library_Level_Actions;
2847 ----------------------
2848 -- Inside_Init_Proc --
2849 ----------------------
2851 function Inside_Init_Proc return Boolean is
2857 and then S /= Standard_Standard
2859 if Is_Init_Proc (S) then
2867 end Inside_Init_Proc;
2869 ----------------------------
2870 -- Is_All_Null_Statements --
2871 ----------------------------
2873 function Is_All_Null_Statements (L : List_Id) return Boolean is
2878 while Present (Stm) loop
2879 if Nkind (Stm) /= N_Null_Statement then
2887 end Is_All_Null_Statements;
2889 -----------------------------------------
2890 -- Is_Predefined_Dispatching_Operation --
2891 -----------------------------------------
2893 function Is_Predefined_Dispatching_Operation (E : Entity_Id) return Boolean
2895 TSS_Name : TSS_Name_Type;
2898 if not Is_Dispatching_Operation (E) then
2902 Get_Name_String (Chars (E));
2904 if Name_Len > TSS_Name_Type'Last then
2905 TSS_Name := TSS_Name_Type (Name_Buffer (Name_Len - TSS_Name'Length + 1
2907 if Chars (E) = Name_uSize
2908 or else Chars (E) = Name_uAlignment
2909 or else TSS_Name = TSS_Stream_Read
2910 or else TSS_Name = TSS_Stream_Write
2911 or else TSS_Name = TSS_Stream_Input
2912 or else TSS_Name = TSS_Stream_Output
2914 (Chars (E) = Name_Op_Eq
2915 and then Etype (First_Entity (E)) = Etype (Last_Entity (E)))
2916 or else Chars (E) = Name_uAssign
2917 or else TSS_Name = TSS_Deep_Adjust
2918 or else TSS_Name = TSS_Deep_Finalize
2919 or else (Ada_Version >= Ada_05
2920 and then (Chars (E) = Name_uDisp_Asynchronous_Select
2921 or else Chars (E) = Name_uDisp_Conditional_Select
2922 or else Chars (E) = Name_uDisp_Get_Prim_Op_Kind
2923 or else Chars (E) = Name_uDisp_Get_Task_Id
2924 or else Chars (E) = Name_uDisp_Timed_Select))
2931 end Is_Predefined_Dispatching_Operation;
2933 ----------------------------------
2934 -- Is_Possibly_Unaligned_Object --
2935 ----------------------------------
2937 function Is_Possibly_Unaligned_Object (N : Node_Id) return Boolean is
2938 T : constant Entity_Id := Etype (N);
2941 -- If renamed object, apply test to underlying object
2943 if Is_Entity_Name (N)
2944 and then Is_Object (Entity (N))
2945 and then Present (Renamed_Object (Entity (N)))
2947 return Is_Possibly_Unaligned_Object (Renamed_Object (Entity (N)));
2950 -- Tagged and controlled types and aliased types are always aligned,
2951 -- as are concurrent types.
2954 or else Has_Controlled_Component (T)
2955 or else Is_Concurrent_Type (T)
2956 or else Is_Tagged_Type (T)
2957 or else Is_Controlled (T)
2962 -- If this is an element of a packed array, may be unaligned
2964 if Is_Ref_To_Bit_Packed_Array (N) then
2968 -- Case of component reference
2970 if Nkind (N) = N_Selected_Component then
2972 P : constant Node_Id := Prefix (N);
2973 C : constant Entity_Id := Entity (Selector_Name (N));
2978 -- If component reference is for an array with non-static bounds,
2979 -- then it is always aligned: we can only process unaligned
2980 -- arrays with static bounds (more accurately bounds known at
2983 if Is_Array_Type (T)
2984 and then not Compile_Time_Known_Bounds (T)
2989 -- If component is aliased, it is definitely properly aligned
2991 if Is_Aliased (C) then
2995 -- If component is for a type implemented as a scalar, and the
2996 -- record is packed, and the component is other than the first
2997 -- component of the record, then the component may be unaligned.
2999 if Is_Packed (Etype (P))
3000 and then Represented_As_Scalar (Etype (C))
3001 and then First_Entity (Scope (C)) /= C
3006 -- Compute maximum possible alignment for T
3008 -- If alignment is known, then that settles things
3010 if Known_Alignment (T) then
3011 M := UI_To_Int (Alignment (T));
3013 -- If alignment is not known, tentatively set max alignment
3016 M := Ttypes.Maximum_Alignment;
3018 -- We can reduce this if the Esize is known since the default
3019 -- alignment will never be more than the smallest power of 2
3020 -- that does not exceed this Esize value.
3022 if Known_Esize (T) then
3023 S := UI_To_Int (Esize (T));
3025 while (M / 2) >= S loop
3031 -- If the component reference is for a record that has a specified
3032 -- alignment, and we either know it is too small, or cannot tell,
3033 -- then the component may be unaligned
3035 if Known_Alignment (Etype (P))
3036 and then Alignment (Etype (P)) < Ttypes.Maximum_Alignment
3037 and then M > Alignment (Etype (P))
3042 -- Case of component clause present which may specify an
3043 -- unaligned position.
3045 if Present (Component_Clause (C)) then
3047 -- Otherwise we can do a test to make sure that the actual
3048 -- start position in the record, and the length, are both
3049 -- consistent with the required alignment. If not, we know
3050 -- that we are unaligned.
3053 Align_In_Bits : constant Nat := M * System_Storage_Unit;
3055 if Component_Bit_Offset (C) mod Align_In_Bits /= 0
3056 or else Esize (C) mod Align_In_Bits /= 0
3063 -- Otherwise, for a component reference, test prefix
3065 return Is_Possibly_Unaligned_Object (P);
3068 -- If not a component reference, must be aligned
3073 end Is_Possibly_Unaligned_Object;
3075 ---------------------------------
3076 -- Is_Possibly_Unaligned_Slice --
3077 ---------------------------------
3079 function Is_Possibly_Unaligned_Slice (N : Node_Id) return Boolean is
3081 -- ??? GCC3 will eventually handle strings with arbitrary alignments,
3082 -- but for now the following check must be disabled.
3084 -- if get_gcc_version >= 3 then
3088 -- For renaming case, go to renamed object
3090 if Is_Entity_Name (N)
3091 and then Is_Object (Entity (N))
3092 and then Present (Renamed_Object (Entity (N)))
3094 return Is_Possibly_Unaligned_Slice (Renamed_Object (Entity (N)));
3097 -- The reference must be a slice
3099 if Nkind (N) /= N_Slice then
3103 -- Always assume the worst for a nested record component with a
3104 -- component clause, which gigi/gcc does not appear to handle well.
3105 -- It is not clear why this special test is needed at all ???
3107 if Nkind (Prefix (N)) = N_Selected_Component
3108 and then Nkind (Prefix (Prefix (N))) = N_Selected_Component
3110 Present (Component_Clause (Entity (Selector_Name (Prefix (N)))))
3115 -- We only need to worry if the target has strict alignment
3117 if not Target_Strict_Alignment then
3121 -- If it is a slice, then look at the array type being sliced
3124 Sarr : constant Node_Id := Prefix (N);
3125 -- Prefix of the slice, i.e. the array being sliced
3127 Styp : constant Entity_Id := Etype (Prefix (N));
3128 -- Type of the array being sliced
3134 -- The problems arise if the array object that is being sliced
3135 -- is a component of a record or array, and we cannot guarantee
3136 -- the alignment of the array within its containing object.
3138 -- To investigate this, we look at successive prefixes to see
3139 -- if we have a worrisome indexed or selected component.
3143 -- Case of array is part of an indexed component reference
3145 if Nkind (Pref) = N_Indexed_Component then
3146 Ptyp := Etype (Prefix (Pref));
3148 -- The only problematic case is when the array is packed,
3149 -- in which case we really know nothing about the alignment
3150 -- of individual components.
3152 if Is_Bit_Packed_Array (Ptyp) then
3156 -- Case of array is part of a selected component reference
3158 elsif Nkind (Pref) = N_Selected_Component then
3159 Ptyp := Etype (Prefix (Pref));
3161 -- We are definitely in trouble if the record in question
3162 -- has an alignment, and either we know this alignment is
3163 -- inconsistent with the alignment of the slice, or we
3164 -- don't know what the alignment of the slice should be.
3166 if Known_Alignment (Ptyp)
3167 and then (Unknown_Alignment (Styp)
3168 or else Alignment (Styp) > Alignment (Ptyp))
3173 -- We are in potential trouble if the record type is packed.
3174 -- We could special case when we know that the array is the
3175 -- first component, but that's not such a simple case ???
3177 if Is_Packed (Ptyp) then
3181 -- We are in trouble if there is a component clause, and
3182 -- either we do not know the alignment of the slice, or
3183 -- the alignment of the slice is inconsistent with the
3184 -- bit position specified by the component clause.
3187 Field : constant Entity_Id := Entity (Selector_Name (Pref));
3189 if Present (Component_Clause (Field))
3191 (Unknown_Alignment (Styp)
3193 (Component_Bit_Offset (Field) mod
3194 (System_Storage_Unit * Alignment (Styp))) /= 0)
3200 -- For cases other than selected or indexed components we
3201 -- know we are OK, since no issues arise over alignment.
3207 -- We processed an indexed component or selected component
3208 -- reference that looked safe, so keep checking prefixes.
3210 Pref := Prefix (Pref);
3213 end Is_Possibly_Unaligned_Slice;
3215 --------------------------------
3216 -- Is_Ref_To_Bit_Packed_Array --
3217 --------------------------------
3219 function Is_Ref_To_Bit_Packed_Array (N : Node_Id) return Boolean is
3224 if Is_Entity_Name (N)
3225 and then Is_Object (Entity (N))
3226 and then Present (Renamed_Object (Entity (N)))
3228 return Is_Ref_To_Bit_Packed_Array (Renamed_Object (Entity (N)));
3231 if Nkind (N) = N_Indexed_Component
3233 Nkind (N) = N_Selected_Component
3235 if Is_Bit_Packed_Array (Etype (Prefix (N))) then
3238 Result := Is_Ref_To_Bit_Packed_Array (Prefix (N));
3241 if Result and then Nkind (N) = N_Indexed_Component then
3242 Expr := First (Expressions (N));
3243 while Present (Expr) loop
3244 Force_Evaluation (Expr);
3254 end Is_Ref_To_Bit_Packed_Array;
3256 --------------------------------
3257 -- Is_Ref_To_Bit_Packed_Slice --
3258 --------------------------------
3260 function Is_Ref_To_Bit_Packed_Slice (N : Node_Id) return Boolean is
3262 if Nkind (N) = N_Type_Conversion then
3263 return Is_Ref_To_Bit_Packed_Slice (Expression (N));
3265 elsif Is_Entity_Name (N)
3266 and then Is_Object (Entity (N))
3267 and then Present (Renamed_Object (Entity (N)))
3269 return Is_Ref_To_Bit_Packed_Slice (Renamed_Object (Entity (N)));
3271 elsif Nkind (N) = N_Slice
3272 and then Is_Bit_Packed_Array (Etype (Prefix (N)))
3276 elsif Nkind (N) = N_Indexed_Component
3278 Nkind (N) = N_Selected_Component
3280 return Is_Ref_To_Bit_Packed_Slice (Prefix (N));
3285 end Is_Ref_To_Bit_Packed_Slice;
3287 -----------------------
3288 -- Is_Renamed_Object --
3289 -----------------------
3291 function Is_Renamed_Object (N : Node_Id) return Boolean is
3292 Pnod : constant Node_Id := Parent (N);
3293 Kind : constant Node_Kind := Nkind (Pnod);
3296 if Kind = N_Object_Renaming_Declaration then
3299 elsif Kind = N_Indexed_Component
3300 or else Kind = N_Selected_Component
3302 return Is_Renamed_Object (Pnod);
3307 end Is_Renamed_Object;
3309 ----------------------------
3310 -- Is_Untagged_Derivation --
3311 ----------------------------
3313 function Is_Untagged_Derivation (T : Entity_Id) return Boolean is
3315 return (not Is_Tagged_Type (T) and then Is_Derived_Type (T))
3317 (Is_Private_Type (T) and then Present (Full_View (T))
3318 and then not Is_Tagged_Type (Full_View (T))
3319 and then Is_Derived_Type (Full_View (T))
3320 and then Etype (Full_View (T)) /= T);
3321 end Is_Untagged_Derivation;
3323 --------------------
3324 -- Kill_Dead_Code --
3325 --------------------
3327 procedure Kill_Dead_Code (N : Node_Id; Warn : Boolean := False) is
3330 Remove_Warning_Messages (N);
3334 ("?this code can never be executed and has been deleted", N);
3337 -- Recurse into block statements and bodies to process declarations
3340 if Nkind (N) = N_Block_Statement
3341 or else Nkind (N) = N_Subprogram_Body
3342 or else Nkind (N) = N_Package_Body
3345 (Declarations (N), False);
3347 (Statements (Handled_Statement_Sequence (N)));
3349 if Nkind (N) = N_Subprogram_Body then
3350 Set_Is_Eliminated (Defining_Entity (N));
3353 elsif Nkind (N) = N_Package_Declaration then
3354 Kill_Dead_Code (Visible_Declarations (Specification (N)));
3355 Kill_Dead_Code (Private_Declarations (Specification (N)));
3358 E : Entity_Id := First_Entity (Defining_Entity (N));
3360 while Present (E) loop
3361 if Ekind (E) = E_Operator then
3362 Set_Is_Eliminated (E);
3369 -- Recurse into composite statement to kill individual statements,
3370 -- in particular instantiations.
3372 elsif Nkind (N) = N_If_Statement then
3373 Kill_Dead_Code (Then_Statements (N));
3374 Kill_Dead_Code (Elsif_Parts (N));
3375 Kill_Dead_Code (Else_Statements (N));
3377 elsif Nkind (N) = N_Loop_Statement then
3378 Kill_Dead_Code (Statements (N));
3380 elsif Nkind (N) = N_Case_Statement then
3384 Alt := First (Alternatives (N));
3385 while Present (Alt) loop
3386 Kill_Dead_Code (Statements (Alt));
3391 elsif Nkind (N) = N_Case_Statement_Alternative then
3392 Kill_Dead_Code (Statements (N));
3394 -- Deal with dead instances caused by deleting instantiations
3396 elsif Nkind (N) in N_Generic_Instantiation then
3397 Remove_Dead_Instance (N);
3404 -- Case where argument is a list of nodes to be killed
3406 procedure Kill_Dead_Code (L : List_Id; Warn : Boolean := False) is
3411 if Is_Non_Empty_List (L) then
3413 N := Remove_Head (L);
3415 Kill_Dead_Code (N, W);
3421 ------------------------
3422 -- Known_Non_Negative --
3423 ------------------------
3425 function Known_Non_Negative (Opnd : Node_Id) return Boolean is
3427 if Is_OK_Static_Expression (Opnd)
3428 and then Expr_Value (Opnd) >= 0
3434 Lo : constant Node_Id := Type_Low_Bound (Etype (Opnd));
3438 Is_OK_Static_Expression (Lo) and then Expr_Value (Lo) >= 0;
3441 end Known_Non_Negative;
3443 --------------------
3444 -- Known_Non_Null --
3445 --------------------
3447 function Known_Non_Null (N : Node_Id) return Boolean is
3449 -- Checks for case where N is an entity reference
3451 if Is_Entity_Name (N) and then Present (Entity (N)) then
3453 E : constant Entity_Id := Entity (N);
3458 -- First check if we are in decisive conditional
3460 Get_Current_Value_Condition (N, Op, Val);
3462 if Nkind (Val) = N_Null then
3463 if Op = N_Op_Eq then
3465 elsif Op = N_Op_Ne then
3470 -- If OK to do replacement, test Is_Known_Non_Null flag
3472 if OK_To_Do_Constant_Replacement (E) then
3473 return Is_Known_Non_Null (E);
3475 -- Otherwise if not safe to do replacement, then say so
3482 -- True if access attribute
3484 elsif Nkind (N) = N_Attribute_Reference
3485 and then (Attribute_Name (N) = Name_Access
3487 Attribute_Name (N) = Name_Unchecked_Access
3489 Attribute_Name (N) = Name_Unrestricted_Access)
3493 -- True if allocator
3495 elsif Nkind (N) = N_Allocator then
3498 -- For a conversion, true if expression is known non-null
3500 elsif Nkind (N) = N_Type_Conversion then
3501 return Known_Non_Null (Expression (N));
3503 -- Above are all cases where the value could be determined to be
3504 -- non-null. In all other cases, we don't know, so return False.
3515 function Known_Null (N : Node_Id) return Boolean is
3517 -- Checks for case where N is an entity reference
3519 if Is_Entity_Name (N) and then Present (Entity (N)) then
3521 E : constant Entity_Id := Entity (N);
3526 -- First check if we are in decisive conditional
3528 Get_Current_Value_Condition (N, Op, Val);
3530 if Nkind (Val) = N_Null then
3531 if Op = N_Op_Eq then
3533 elsif Op = N_Op_Ne then
3538 -- If OK to do replacement, test Is_Known_Null flag
3540 if OK_To_Do_Constant_Replacement (E) then
3541 return Is_Known_Null (E);
3543 -- Otherwise if not safe to do replacement, then say so
3550 -- True if explicit reference to null
3552 elsif Nkind (N) = N_Null then
3555 -- For a conversion, true if expression is known null
3557 elsif Nkind (N) = N_Type_Conversion then
3558 return Known_Null (Expression (N));
3560 -- Above are all cases where the value could be determined to be null.
3561 -- In all other cases, we don't know, so return False.
3568 -----------------------------
3569 -- Make_CW_Equivalent_Type --
3570 -----------------------------
3572 -- Create a record type used as an equivalent of any member
3573 -- of the class which takes its size from exp.
3575 -- Generate the following code:
3577 -- type Equiv_T is record
3578 -- _parent : T (List of discriminant constaints taken from Exp);
3579 -- Ext__50 : Storage_Array (1 .. (Exp'size - Typ'object_size)/8);
3582 -- ??? Note that this type does not guarantee same alignment as all
3585 function Make_CW_Equivalent_Type
3587 E : Node_Id) return Entity_Id
3589 Loc : constant Source_Ptr := Sloc (E);
3590 Root_Typ : constant Entity_Id := Root_Type (T);
3591 List_Def : constant List_Id := Empty_List;
3592 Equiv_Type : Entity_Id;
3593 Range_Type : Entity_Id;
3594 Str_Type : Entity_Id;
3595 Constr_Root : Entity_Id;
3599 if not Has_Discriminants (Root_Typ) then
3600 Constr_Root := Root_Typ;
3603 Make_Defining_Identifier (Loc, New_Internal_Name ('R'));
3605 -- subtype cstr__n is T (List of discr constraints taken from Exp)
3607 Append_To (List_Def,
3608 Make_Subtype_Declaration (Loc,
3609 Defining_Identifier => Constr_Root,
3610 Subtype_Indication =>
3611 Make_Subtype_From_Expr (E, Root_Typ)));
3614 -- subtype rg__xx is Storage_Offset range
3615 -- (Expr'size - typ'size) / Storage_Unit
3617 Range_Type := Make_Defining_Identifier (Loc, New_Internal_Name ('G'));
3620 Make_Op_Subtract (Loc,
3622 Make_Attribute_Reference (Loc,
3624 OK_Convert_To (T, Duplicate_Subexpr_No_Checks (E)),
3625 Attribute_Name => Name_Size),
3627 Make_Attribute_Reference (Loc,
3628 Prefix => New_Reference_To (Constr_Root, Loc),
3629 Attribute_Name => Name_Object_Size));
3631 Set_Paren_Count (Sizexpr, 1);
3633 Append_To (List_Def,
3634 Make_Subtype_Declaration (Loc,
3635 Defining_Identifier => Range_Type,
3636 Subtype_Indication =>
3637 Make_Subtype_Indication (Loc,
3638 Subtype_Mark => New_Reference_To (RTE (RE_Storage_Offset), Loc),
3639 Constraint => Make_Range_Constraint (Loc,
3642 Low_Bound => Make_Integer_Literal (Loc, 1),
3644 Make_Op_Divide (Loc,
3645 Left_Opnd => Sizexpr,
3646 Right_Opnd => Make_Integer_Literal (Loc,
3647 Intval => System_Storage_Unit)))))));
3649 -- subtype str__nn is Storage_Array (rg__x);
3651 Str_Type := Make_Defining_Identifier (Loc, New_Internal_Name ('S'));
3652 Append_To (List_Def,
3653 Make_Subtype_Declaration (Loc,
3654 Defining_Identifier => Str_Type,
3655 Subtype_Indication =>
3656 Make_Subtype_Indication (Loc,
3657 Subtype_Mark => New_Reference_To (RTE (RE_Storage_Array), Loc),
3659 Make_Index_Or_Discriminant_Constraint (Loc,
3661 New_List (New_Reference_To (Range_Type, Loc))))));
3663 -- type Equiv_T is record
3668 Equiv_Type := Make_Defining_Identifier (Loc, New_Internal_Name ('T'));
3670 -- When the target requires front-end layout, it's necessary to allow
3671 -- the equivalent type to be frozen so that layout can occur (when the
3672 -- associated class-wide subtype is frozen, the equivalent type will
3673 -- be frozen, see freeze.adb). For other targets, Gigi wants to have
3674 -- the equivalent type marked as frozen and deals with this type itself.
3675 -- In the Gigi case this will also avoid the generation of an init
3676 -- procedure for the type.
3678 if not Frontend_Layout_On_Target then
3679 Set_Is_Frozen (Equiv_Type);
3682 Set_Ekind (Equiv_Type, E_Record_Type);
3683 Set_Parent_Subtype (Equiv_Type, Constr_Root);
3685 Append_To (List_Def,
3686 Make_Full_Type_Declaration (Loc,
3687 Defining_Identifier => Equiv_Type,
3690 Make_Record_Definition (Loc,
3691 Component_List => Make_Component_List (Loc,
3692 Component_Items => New_List (
3693 Make_Component_Declaration (Loc,
3694 Defining_Identifier =>
3695 Make_Defining_Identifier (Loc, Name_uParent),
3696 Component_Definition =>
3697 Make_Component_Definition (Loc,
3698 Aliased_Present => False,
3699 Subtype_Indication =>
3700 New_Reference_To (Constr_Root, Loc))),
3702 Make_Component_Declaration (Loc,
3703 Defining_Identifier =>
3704 Make_Defining_Identifier (Loc,
3705 Chars => New_Internal_Name ('C')),
3706 Component_Definition =>
3707 Make_Component_Definition (Loc,
3708 Aliased_Present => False,
3709 Subtype_Indication =>
3710 New_Reference_To (Str_Type, Loc)))),
3712 Variant_Part => Empty))));
3714 Insert_Actions (E, List_Def);
3716 end Make_CW_Equivalent_Type;
3718 ------------------------
3719 -- Make_Literal_Range --
3720 ------------------------
3722 function Make_Literal_Range
3724 Literal_Typ : Entity_Id) return Node_Id
3726 Lo : constant Node_Id :=
3727 New_Copy_Tree (String_Literal_Low_Bound (Literal_Typ));
3730 Set_Analyzed (Lo, False);
3737 Make_Op_Subtract (Loc,
3740 Left_Opnd => New_Copy_Tree (Lo),
3742 Make_Integer_Literal (Loc,
3743 String_Literal_Length (Literal_Typ))),
3744 Right_Opnd => Make_Integer_Literal (Loc, 1)));
3745 end Make_Literal_Range;
3747 ----------------------------
3748 -- Make_Subtype_From_Expr --
3749 ----------------------------
3751 -- 1. If Expr is an uncontrained array expression, creates
3752 -- Unc_Type(Expr'first(1)..Expr'Last(1),..., Expr'first(n)..Expr'last(n))
3754 -- 2. If Expr is a unconstrained discriminated type expression, creates
3755 -- Unc_Type(Expr.Discr1, ... , Expr.Discr_n)
3757 -- 3. If Expr is class-wide, creates an implicit class wide subtype
3759 function Make_Subtype_From_Expr
3761 Unc_Typ : Entity_Id) return Node_Id
3763 Loc : constant Source_Ptr := Sloc (E);
3764 List_Constr : constant List_Id := New_List;
3767 Full_Subtyp : Entity_Id;
3768 Priv_Subtyp : Entity_Id;
3773 if Is_Private_Type (Unc_Typ)
3774 and then Has_Unknown_Discriminants (Unc_Typ)
3776 -- Prepare the subtype completion, Go to base type to
3777 -- find underlying type, because the type may be a generic
3778 -- actual or an explicit subtype.
3780 Utyp := Underlying_Type (Base_Type (Unc_Typ));
3781 Full_Subtyp := Make_Defining_Identifier (Loc,
3782 New_Internal_Name ('C'));
3784 Unchecked_Convert_To
3785 (Utyp, Duplicate_Subexpr_No_Checks (E));
3786 Set_Parent (Full_Exp, Parent (E));
3789 Make_Defining_Identifier (Loc, New_Internal_Name ('P'));
3792 Make_Subtype_Declaration (Loc,
3793 Defining_Identifier => Full_Subtyp,
3794 Subtype_Indication => Make_Subtype_From_Expr (Full_Exp, Utyp)));
3796 -- Define the dummy private subtype
3798 Set_Ekind (Priv_Subtyp, Subtype_Kind (Ekind (Unc_Typ)));
3799 Set_Etype (Priv_Subtyp, Base_Type (Unc_Typ));
3800 Set_Scope (Priv_Subtyp, Full_Subtyp);
3801 Set_Is_Constrained (Priv_Subtyp);
3802 Set_Is_Tagged_Type (Priv_Subtyp, Is_Tagged_Type (Unc_Typ));
3803 Set_Is_Itype (Priv_Subtyp);
3804 Set_Associated_Node_For_Itype (Priv_Subtyp, E);
3806 if Is_Tagged_Type (Priv_Subtyp) then
3808 (Base_Type (Priv_Subtyp), Class_Wide_Type (Unc_Typ));
3809 Set_Primitive_Operations (Priv_Subtyp,
3810 Primitive_Operations (Unc_Typ));
3813 Set_Full_View (Priv_Subtyp, Full_Subtyp);
3815 return New_Reference_To (Priv_Subtyp, Loc);
3817 elsif Is_Array_Type (Unc_Typ) then
3818 for J in 1 .. Number_Dimensions (Unc_Typ) loop
3819 Append_To (List_Constr,
3822 Make_Attribute_Reference (Loc,
3823 Prefix => Duplicate_Subexpr_No_Checks (E),
3824 Attribute_Name => Name_First,
3825 Expressions => New_List (
3826 Make_Integer_Literal (Loc, J))),
3829 Make_Attribute_Reference (Loc,
3830 Prefix => Duplicate_Subexpr_No_Checks (E),
3831 Attribute_Name => Name_Last,
3832 Expressions => New_List (
3833 Make_Integer_Literal (Loc, J)))));
3836 elsif Is_Class_Wide_Type (Unc_Typ) then
3838 CW_Subtype : Entity_Id;
3839 EQ_Typ : Entity_Id := Empty;
3842 -- A class-wide equivalent type is not needed when Java_VM
3843 -- because the JVM back end handles the class-wide object
3844 -- initialization itself (and doesn't need or want the
3845 -- additional intermediate type to handle the assignment).
3847 if Expander_Active and then not Java_VM then
3848 EQ_Typ := Make_CW_Equivalent_Type (Unc_Typ, E);
3851 CW_Subtype := New_Class_Wide_Subtype (Unc_Typ, E);
3852 Set_Equivalent_Type (CW_Subtype, EQ_Typ);
3854 if Present (EQ_Typ) then
3855 Set_Is_Class_Wide_Equivalent_Type (EQ_Typ);
3858 Set_Cloned_Subtype (CW_Subtype, Base_Type (Unc_Typ));
3860 return New_Occurrence_Of (CW_Subtype, Loc);
3863 -- Indefinite record type with discriminants
3866 D := First_Discriminant (Unc_Typ);
3867 while Present (D) loop
3868 Append_To (List_Constr,
3869 Make_Selected_Component (Loc,
3870 Prefix => Duplicate_Subexpr_No_Checks (E),
3871 Selector_Name => New_Reference_To (D, Loc)));
3873 Next_Discriminant (D);
3878 Make_Subtype_Indication (Loc,
3879 Subtype_Mark => New_Reference_To (Unc_Typ, Loc),
3881 Make_Index_Or_Discriminant_Constraint (Loc,
3882 Constraints => List_Constr));
3883 end Make_Subtype_From_Expr;
3885 -----------------------------
3886 -- May_Generate_Large_Temp --
3887 -----------------------------
3889 -- At the current time, the only types that we return False for (i.e.
3890 -- where we decide we know they cannot generate large temps) are ones
3891 -- where we know the size is 256 bits or less at compile time, and we
3892 -- are still not doing a thorough job on arrays and records ???
3894 function May_Generate_Large_Temp (Typ : Entity_Id) return Boolean is
3896 if not Size_Known_At_Compile_Time (Typ) then
3899 elsif Esize (Typ) /= 0 and then Esize (Typ) <= 256 then
3902 elsif Is_Array_Type (Typ)
3903 and then Present (Packed_Array_Type (Typ))
3905 return May_Generate_Large_Temp (Packed_Array_Type (Typ));
3907 -- We could do more here to find other small types ???
3912 end May_Generate_Large_Temp;
3914 ----------------------------
3915 -- New_Class_Wide_Subtype --
3916 ----------------------------
3918 function New_Class_Wide_Subtype
3919 (CW_Typ : Entity_Id;
3920 N : Node_Id) return Entity_Id
3922 Res : constant Entity_Id := Create_Itype (E_Void, N);
3923 Res_Name : constant Name_Id := Chars (Res);
3924 Res_Scope : constant Entity_Id := Scope (Res);
3927 Copy_Node (CW_Typ, Res);
3928 Set_Comes_From_Source (Res, False);
3929 Set_Sloc (Res, Sloc (N));
3931 Set_Associated_Node_For_Itype (Res, N);
3932 Set_Is_Public (Res, False); -- By default, may be changed below.
3933 Set_Public_Status (Res);
3934 Set_Chars (Res, Res_Name);
3935 Set_Scope (Res, Res_Scope);
3936 Set_Ekind (Res, E_Class_Wide_Subtype);
3937 Set_Next_Entity (Res, Empty);
3938 Set_Etype (Res, Base_Type (CW_Typ));
3940 -- For targets where front-end layout is required, reset the Is_Frozen
3941 -- status of the subtype to False (it can be implicitly set to true
3942 -- from the copy of the class-wide type). For other targets, Gigi
3943 -- doesn't want the class-wide subtype to go through the freezing
3944 -- process (though it's unclear why that causes problems and it would
3945 -- be nice to allow freezing to occur normally for all targets ???).
3947 if Frontend_Layout_On_Target then
3948 Set_Is_Frozen (Res, False);
3951 Set_Freeze_Node (Res, Empty);
3953 end New_Class_Wide_Subtype;
3955 -----------------------------------
3956 -- OK_To_Do_Constant_Replacement --
3957 -----------------------------------
3959 function OK_To_Do_Constant_Replacement (E : Entity_Id) return Boolean is
3960 ES : constant Entity_Id := Scope (E);
3964 -- Do not replace statically allocated objects, because they may be
3965 -- modified outside the current scope.
3967 if Is_Statically_Allocated (E) then
3970 -- Do not replace aliased or volatile objects, since we don't know what
3971 -- else might change the value.
3973 elsif Is_Aliased (E) or else Treat_As_Volatile (E) then
3976 -- Debug flag -gnatdM disconnects this optimization
3978 elsif Debug_Flag_MM then
3981 -- Otherwise check scopes
3984 CS := Current_Scope;
3987 -- If we are in right scope, replacement is safe
3992 -- Packages do not affect the determination of safety
3994 elsif Ekind (CS) = E_Package then
3995 exit when CS = Standard_Standard;
3998 -- Blocks do not affect the determination of safety
4000 elsif Ekind (CS) = E_Block then
4003 -- Loops do not affect the determination of safety. Note that we
4004 -- kill all current values on entry to a loop, so we are just
4005 -- talking about processing within a loop here.
4007 elsif Ekind (CS) = E_Loop then
4010 -- Otherwise, the reference is dubious, and we cannot be sure that
4011 -- it is safe to do the replacement.
4020 end OK_To_Do_Constant_Replacement;
4022 -------------------------
4023 -- Remove_Side_Effects --
4024 -------------------------
4026 procedure Remove_Side_Effects
4028 Name_Req : Boolean := False;
4029 Variable_Ref : Boolean := False)
4031 Loc : constant Source_Ptr := Sloc (Exp);
4032 Exp_Type : constant Entity_Id := Etype (Exp);
4033 Svg_Suppress : constant Suppress_Array := Scope_Suppress;
4035 Ref_Type : Entity_Id;
4037 Ptr_Typ_Decl : Node_Id;
4041 function Side_Effect_Free (N : Node_Id) return Boolean;
4042 -- Determines if the tree N represents an expression that is known not
4043 -- to have side effects, and for which no processing is required.
4045 function Side_Effect_Free (L : List_Id) return Boolean;
4046 -- Determines if all elements of the list L are side effect free
4048 function Safe_Prefixed_Reference (N : Node_Id) return Boolean;
4049 -- The argument N is a construct where the Prefix is dereferenced if it
4050 -- is an access type and the result is a variable. The call returns True
4051 -- if the construct is side effect free (not considering side effects in
4052 -- other than the prefix which are to be tested by the caller).
4054 function Within_In_Parameter (N : Node_Id) return Boolean;
4055 -- Determines if N is a subcomponent of a composite in-parameter. If so,
4056 -- N is not side-effect free when the actual is global and modifiable
4057 -- indirectly from within a subprogram, because it may be passed by
4058 -- reference. The front-end must be conservative here and assume that
4059 -- this may happen with any array or record type. On the other hand, we
4060 -- cannot create temporaries for all expressions for which this
4061 -- condition is true, for various reasons that might require clearing up
4062 -- ??? For example, descriminant references that appear out of place, or
4063 -- spurious type errors with class-wide expressions. As a result, we
4064 -- limit the transformation to loop bounds, which is so far the only
4065 -- case that requires it.
4067 -----------------------------
4068 -- Safe_Prefixed_Reference --
4069 -----------------------------
4071 function Safe_Prefixed_Reference (N : Node_Id) return Boolean is
4073 -- If prefix is not side effect free, definitely not safe
4075 if not Side_Effect_Free (Prefix (N)) then
4078 -- If the prefix is of an access type that is not access-to-constant,
4079 -- then this construct is a variable reference, which means it is to
4080 -- be considered to have side effects if Variable_Ref is set True
4081 -- Exception is an access to an entity that is a constant or an
4082 -- in-parameter which does not come from source, and is the result
4083 -- of a previous removal of side-effects.
4085 elsif Is_Access_Type (Etype (Prefix (N)))
4086 and then not Is_Access_Constant (Etype (Prefix (N)))
4087 and then Variable_Ref
4089 if not Is_Entity_Name (Prefix (N)) then
4092 return Ekind (Entity (Prefix (N))) = E_Constant
4093 or else Ekind (Entity (Prefix (N))) = E_In_Parameter;
4096 -- The following test is the simplest way of solving a complex
4097 -- problem uncovered by BB08-010: Side effect on loop bound that
4098 -- is a subcomponent of a global variable:
4099 -- If a loop bound is a subcomponent of a global variable, a
4100 -- modification of that variable within the loop may incorrectly
4101 -- affect the execution of the loop.
4104 (Nkind (Parent (Parent (N))) /= N_Loop_Parameter_Specification
4105 or else not Within_In_Parameter (Prefix (N)))
4109 -- All other cases are side effect free
4114 end Safe_Prefixed_Reference;
4116 ----------------------
4117 -- Side_Effect_Free --
4118 ----------------------
4120 function Side_Effect_Free (N : Node_Id) return Boolean is
4122 -- Note on checks that could raise Constraint_Error. Strictly, if
4123 -- we take advantage of 11.6, these checks do not count as side
4124 -- effects. However, we would just as soon consider that they are
4125 -- side effects, since the backend CSE does not work very well on
4126 -- expressions which can raise Constraint_Error. On the other
4127 -- hand, if we do not consider them to be side effect free, then
4128 -- we get some awkward expansions in -gnato mode, resulting in
4129 -- code insertions at a point where we do not have a clear model
4130 -- for performing the insertions. See 4908-002/comment for details.
4132 -- Special handling for entity names
4134 if Is_Entity_Name (N) then
4136 -- If the entity is a constant, it is definitely side effect
4137 -- free. Note that the test of Is_Variable (N) below might
4138 -- be expected to catch this case, but it does not, because
4139 -- this test goes to the original tree, and we may have
4140 -- already rewritten a variable node with a constant as
4141 -- a result of an earlier Force_Evaluation call.
4143 if Ekind (Entity (N)) = E_Constant
4144 or else Ekind (Entity (N)) = E_In_Parameter
4148 -- Functions are not side effect free
4150 elsif Ekind (Entity (N)) = E_Function then
4153 -- Variables are considered to be a side effect if Variable_Ref
4154 -- is set or if we have a volatile variable and Name_Req is off.
4155 -- If Name_Req is True then we can't help returning a name which
4156 -- effectively allows multiple references in any case.
4158 elsif Is_Variable (N) then
4159 return not Variable_Ref
4160 and then (not Treat_As_Volatile (Entity (N))
4163 -- Any other entity (e.g. a subtype name) is definitely side
4170 -- A value known at compile time is always side effect free
4172 elsif Compile_Time_Known_Value (N) then
4176 -- For other than entity names and compile time known values,
4177 -- check the node kind for special processing.
4181 -- An attribute reference is side effect free if its expressions
4182 -- are side effect free and its prefix is side effect free or
4183 -- is an entity reference.
4185 -- Is this right? what about x'first where x is a variable???
4187 when N_Attribute_Reference =>
4188 return Side_Effect_Free (Expressions (N))
4189 and then Attribute_Name (N) /= Name_Input
4190 and then (Is_Entity_Name (Prefix (N))
4191 or else Side_Effect_Free (Prefix (N)));
4193 -- A binary operator is side effect free if and both operands
4194 -- are side effect free. For this purpose binary operators
4195 -- include membership tests and short circuit forms
4201 return Side_Effect_Free (Left_Opnd (N))
4202 and then Side_Effect_Free (Right_Opnd (N));
4204 -- An explicit dereference is side effect free only if it is
4205 -- a side effect free prefixed reference.
4207 when N_Explicit_Dereference =>
4208 return Safe_Prefixed_Reference (N);
4210 -- A call to _rep_to_pos is side effect free, since we generate
4211 -- this pure function call ourselves. Moreover it is critically
4212 -- important to make this exception, since otherwise we can
4213 -- have discriminants in array components which don't look
4214 -- side effect free in the case of an array whose index type
4215 -- is an enumeration type with an enumeration rep clause.
4217 -- All other function calls are not side effect free
4219 when N_Function_Call =>
4220 return Nkind (Name (N)) = N_Identifier
4221 and then Is_TSS (Name (N), TSS_Rep_To_Pos)
4223 Side_Effect_Free (First (Parameter_Associations (N)));
4225 -- An indexed component is side effect free if it is a side
4226 -- effect free prefixed reference and all the indexing
4227 -- expressions are side effect free.
4229 when N_Indexed_Component =>
4230 return Side_Effect_Free (Expressions (N))
4231 and then Safe_Prefixed_Reference (N);
4233 -- A type qualification is side effect free if the expression
4234 -- is side effect free.
4236 when N_Qualified_Expression =>
4237 return Side_Effect_Free (Expression (N));
4239 -- A selected component is side effect free only if it is a
4240 -- side effect free prefixed reference.
4242 when N_Selected_Component =>
4243 return Safe_Prefixed_Reference (N);
4245 -- A range is side effect free if the bounds are side effect free
4248 return Side_Effect_Free (Low_Bound (N))
4249 and then Side_Effect_Free (High_Bound (N));
4251 -- A slice is side effect free if it is a side effect free
4252 -- prefixed reference and the bounds are side effect free.
4255 return Side_Effect_Free (Discrete_Range (N))
4256 and then Safe_Prefixed_Reference (N);
4258 -- A type conversion is side effect free if the expression
4259 -- to be converted is side effect free.
4261 when N_Type_Conversion =>
4262 return Side_Effect_Free (Expression (N));
4264 -- A unary operator is side effect free if the operand
4265 -- is side effect free.
4268 return Side_Effect_Free (Right_Opnd (N));
4270 -- An unchecked type conversion is side effect free only if it
4271 -- is safe and its argument is side effect free.
4273 when N_Unchecked_Type_Conversion =>
4274 return Safe_Unchecked_Type_Conversion (N)
4275 and then Side_Effect_Free (Expression (N));
4277 -- An unchecked expression is side effect free if its expression
4278 -- is side effect free.
4280 when N_Unchecked_Expression =>
4281 return Side_Effect_Free (Expression (N));
4283 -- A literal is side effect free
4285 when N_Character_Literal |
4291 -- We consider that anything else has side effects. This is a bit
4292 -- crude, but we are pretty close for most common cases, and we
4293 -- are certainly correct (i.e. we never return True when the
4294 -- answer should be False).
4299 end Side_Effect_Free;
4301 -- A list is side effect free if all elements of the list are
4302 -- side effect free.
4304 function Side_Effect_Free (L : List_Id) return Boolean is
4308 if L = No_List or else L = Error_List then
4313 while Present (N) loop
4314 if not Side_Effect_Free (N) then
4323 end Side_Effect_Free;
4325 -------------------------
4326 -- Within_In_Parameter --
4327 -------------------------
4329 function Within_In_Parameter (N : Node_Id) return Boolean is
4331 if not Comes_From_Source (N) then
4334 elsif Is_Entity_Name (N) then
4336 Ekind (Entity (N)) = E_In_Parameter;
4338 elsif Nkind (N) = N_Indexed_Component
4339 or else Nkind (N) = N_Selected_Component
4341 return Within_In_Parameter (Prefix (N));
4346 end Within_In_Parameter;
4348 -- Start of processing for Remove_Side_Effects
4351 -- If we are side effect free already or expansion is disabled,
4352 -- there is nothing to do.
4354 if Side_Effect_Free (Exp) or else not Expander_Active then
4358 -- All this must not have any checks
4360 Scope_Suppress := (others => True);
4362 -- If it is a scalar type and we need to capture the value, just
4363 -- make a copy. Likewise for a function call. And if we have a
4364 -- volatile variable and Nam_Req is not set (see comments above
4365 -- for Side_Effect_Free).
4367 if Is_Elementary_Type (Exp_Type)
4368 and then (Variable_Ref
4369 or else Nkind (Exp) = N_Function_Call
4370 or else (not Name_Req
4371 and then Is_Entity_Name (Exp)
4372 and then Treat_As_Volatile (Entity (Exp))))
4375 Def_Id := Make_Defining_Identifier (Loc, New_Internal_Name ('R'));
4376 Set_Etype (Def_Id, Exp_Type);
4377 Res := New_Reference_To (Def_Id, Loc);
4380 Make_Object_Declaration (Loc,
4381 Defining_Identifier => Def_Id,
4382 Object_Definition => New_Reference_To (Exp_Type, Loc),
4383 Constant_Present => True,
4384 Expression => Relocate_Node (Exp));
4386 Set_Assignment_OK (E);
4387 Insert_Action (Exp, E);
4389 -- If the expression has the form v.all then we can just capture
4390 -- the pointer, and then do an explicit dereference on the result.
4392 elsif Nkind (Exp) = N_Explicit_Dereference then
4394 Make_Defining_Identifier (Loc, New_Internal_Name ('R'));
4396 Make_Explicit_Dereference (Loc, New_Reference_To (Def_Id, Loc));
4399 Make_Object_Declaration (Loc,
4400 Defining_Identifier => Def_Id,
4401 Object_Definition =>
4402 New_Reference_To (Etype (Prefix (Exp)), Loc),
4403 Constant_Present => True,
4404 Expression => Relocate_Node (Prefix (Exp))));
4406 -- Similar processing for an unchecked conversion of an expression
4407 -- of the form v.all, where we want the same kind of treatment.
4409 elsif Nkind (Exp) = N_Unchecked_Type_Conversion
4410 and then Nkind (Expression (Exp)) = N_Explicit_Dereference
4412 Remove_Side_Effects (Expression (Exp), Name_Req, Variable_Ref);
4413 Scope_Suppress := Svg_Suppress;
4416 -- If this is a type conversion, leave the type conversion and remove
4417 -- the side effects in the expression. This is important in several
4418 -- circumstances: for change of representations, and also when this
4419 -- is a view conversion to a smaller object, where gigi can end up
4420 -- creating its own temporary of the wrong size.
4422 elsif Nkind (Exp) = N_Type_Conversion then
4423 Remove_Side_Effects (Expression (Exp), Name_Req, Variable_Ref);
4424 Scope_Suppress := Svg_Suppress;
4427 -- If this is an unchecked conversion that Gigi can't handle, make
4428 -- a copy or a use a renaming to capture the value.
4430 elsif Nkind (Exp) = N_Unchecked_Type_Conversion
4431 and then not Safe_Unchecked_Type_Conversion (Exp)
4433 if CW_Or_Controlled_Type (Exp_Type) then
4435 -- Use a renaming to capture the expression, rather than create
4436 -- a controlled temporary.
4438 Def_Id := Make_Defining_Identifier (Loc, New_Internal_Name ('R'));
4439 Res := New_Reference_To (Def_Id, Loc);
4442 Make_Object_Renaming_Declaration (Loc,
4443 Defining_Identifier => Def_Id,
4444 Subtype_Mark => New_Reference_To (Exp_Type, Loc),
4445 Name => Relocate_Node (Exp)));
4448 Def_Id := Make_Defining_Identifier (Loc, New_Internal_Name ('R'));
4449 Set_Etype (Def_Id, Exp_Type);
4450 Res := New_Reference_To (Def_Id, Loc);
4453 Make_Object_Declaration (Loc,
4454 Defining_Identifier => Def_Id,
4455 Object_Definition => New_Reference_To (Exp_Type, Loc),
4456 Constant_Present => not Is_Variable (Exp),
4457 Expression => Relocate_Node (Exp));
4459 Set_Assignment_OK (E);
4460 Insert_Action (Exp, E);
4463 -- For expressions that denote objects, we can use a renaming scheme.
4464 -- We skip using this if we have a volatile variable and we do not
4465 -- have Nam_Req set true (see comments above for Side_Effect_Free).
4467 elsif Is_Object_Reference (Exp)
4468 and then Nkind (Exp) /= N_Function_Call
4470 or else not Is_Entity_Name (Exp)
4471 or else not Treat_As_Volatile (Entity (Exp)))
4473 Def_Id := Make_Defining_Identifier (Loc, New_Internal_Name ('R'));
4475 if Nkind (Exp) = N_Selected_Component
4476 and then Nkind (Prefix (Exp)) = N_Function_Call
4477 and then Is_Array_Type (Exp_Type)
4479 -- Avoid generating a variable-sized temporary, by generating
4480 -- the renaming declaration just for the function call. The
4481 -- transformation could be refined to apply only when the array
4482 -- component is constrained by a discriminant???
4485 Make_Selected_Component (Loc,
4486 Prefix => New_Occurrence_Of (Def_Id, Loc),
4487 Selector_Name => Selector_Name (Exp));
4490 Make_Object_Renaming_Declaration (Loc,
4491 Defining_Identifier => Def_Id,
4493 New_Reference_To (Base_Type (Etype (Prefix (Exp))), Loc),
4494 Name => Relocate_Node (Prefix (Exp))));
4497 Res := New_Reference_To (Def_Id, Loc);
4500 Make_Object_Renaming_Declaration (Loc,
4501 Defining_Identifier => Def_Id,
4502 Subtype_Mark => New_Reference_To (Exp_Type, Loc),
4503 Name => Relocate_Node (Exp)));
4507 -- If this is a packed reference, or a selected component with a
4508 -- non-standard representation, a reference to the temporary will
4509 -- be replaced by a copy of the original expression (see
4510 -- exp_ch2.Expand_Renaming). Otherwise the temporary must be
4511 -- elaborated by gigi, and is of course not to be replaced in-line
4512 -- by the expression it renames, which would defeat the purpose of
4513 -- removing the side-effect.
4515 if (Nkind (Exp) = N_Selected_Component
4516 or else Nkind (Exp) = N_Indexed_Component)
4517 and then Has_Non_Standard_Rep (Etype (Prefix (Exp)))
4521 Set_Is_Renaming_Of_Object (Def_Id, False);
4524 -- Otherwise we generate a reference to the value
4527 Ref_Type := Make_Defining_Identifier (Loc, New_Internal_Name ('A'));
4530 Make_Full_Type_Declaration (Loc,
4531 Defining_Identifier => Ref_Type,
4533 Make_Access_To_Object_Definition (Loc,
4534 All_Present => True,
4535 Subtype_Indication =>
4536 New_Reference_To (Exp_Type, Loc)));
4539 Insert_Action (Exp, Ptr_Typ_Decl);
4541 Def_Id := Make_Defining_Identifier (Loc, New_Internal_Name ('R'));
4542 Set_Etype (Def_Id, Exp_Type);
4545 Make_Explicit_Dereference (Loc,
4546 Prefix => New_Reference_To (Def_Id, Loc));
4548 if Nkind (E) = N_Explicit_Dereference then
4549 New_Exp := Relocate_Node (Prefix (E));
4551 E := Relocate_Node (E);
4552 New_Exp := Make_Reference (Loc, E);
4555 if Is_Delayed_Aggregate (E) then
4557 -- The expansion of nested aggregates is delayed until the
4558 -- enclosing aggregate is expanded. As aggregates are often
4559 -- qualified, the predicate applies to qualified expressions
4560 -- as well, indicating that the enclosing aggregate has not
4561 -- been expanded yet. At this point the aggregate is part of
4562 -- a stand-alone declaration, and must be fully expanded.
4564 if Nkind (E) = N_Qualified_Expression then
4565 Set_Expansion_Delayed (Expression (E), False);
4566 Set_Analyzed (Expression (E), False);
4568 Set_Expansion_Delayed (E, False);
4571 Set_Analyzed (E, False);
4575 Make_Object_Declaration (Loc,
4576 Defining_Identifier => Def_Id,
4577 Object_Definition => New_Reference_To (Ref_Type, Loc),
4578 Expression => New_Exp));
4581 -- Preserve the Assignment_OK flag in all copies, since at least
4582 -- one copy may be used in a context where this flag must be set
4583 -- (otherwise why would the flag be set in the first place).
4585 Set_Assignment_OK (Res, Assignment_OK (Exp));
4587 -- Finally rewrite the original expression and we are done
4590 Analyze_And_Resolve (Exp, Exp_Type);
4591 Scope_Suppress := Svg_Suppress;
4592 end Remove_Side_Effects;
4594 ---------------------------
4595 -- Represented_As_Scalar --
4596 ---------------------------
4598 function Represented_As_Scalar (T : Entity_Id) return Boolean is
4599 UT : constant Entity_Id := Underlying_Type (T);
4601 return Is_Scalar_Type (UT)
4602 or else (Is_Bit_Packed_Array (UT)
4603 and then Is_Scalar_Type (Packed_Array_Type (UT)));
4604 end Represented_As_Scalar;
4606 ------------------------------------
4607 -- Safe_Unchecked_Type_Conversion --
4608 ------------------------------------
4610 -- Note: this function knows quite a bit about the exact requirements
4611 -- of Gigi with respect to unchecked type conversions, and its code
4612 -- must be coordinated with any changes in Gigi in this area.
4614 -- The above requirements should be documented in Sinfo ???
4616 function Safe_Unchecked_Type_Conversion (Exp : Node_Id) return Boolean is
4621 Pexp : constant Node_Id := Parent (Exp);
4624 -- If the expression is the RHS of an assignment or object declaration
4625 -- we are always OK because there will always be a target.
4627 -- Object renaming declarations, (generated for view conversions of
4628 -- actuals in inlined calls), like object declarations, provide an
4629 -- explicit type, and are safe as well.
4631 if (Nkind (Pexp) = N_Assignment_Statement
4632 and then Expression (Pexp) = Exp)
4633 or else Nkind (Pexp) = N_Object_Declaration
4634 or else Nkind (Pexp) = N_Object_Renaming_Declaration
4638 -- If the expression is the prefix of an N_Selected_Component
4639 -- we should also be OK because GCC knows to look inside the
4640 -- conversion except if the type is discriminated. We assume
4641 -- that we are OK anyway if the type is not set yet or if it is
4642 -- controlled since we can't afford to introduce a temporary in
4645 elsif Nkind (Pexp) = N_Selected_Component
4646 and then Prefix (Pexp) = Exp
4648 if No (Etype (Pexp)) then
4652 not Has_Discriminants (Etype (Pexp))
4653 or else Is_Constrained (Etype (Pexp));
4657 -- Set the output type, this comes from Etype if it is set, otherwise
4658 -- we take it from the subtype mark, which we assume was already
4661 if Present (Etype (Exp)) then
4662 Otyp := Etype (Exp);
4664 Otyp := Entity (Subtype_Mark (Exp));
4667 -- The input type always comes from the expression, and we assume
4668 -- this is indeed always analyzed, so we can simply get the Etype.
4670 Ityp := Etype (Expression (Exp));
4672 -- Initialize alignments to unknown so far
4677 -- Replace a concurrent type by its corresponding record type
4678 -- and each type by its underlying type and do the tests on those.
4679 -- The original type may be a private type whose completion is a
4680 -- concurrent type, so find the underlying type first.
4682 if Present (Underlying_Type (Otyp)) then
4683 Otyp := Underlying_Type (Otyp);
4686 if Present (Underlying_Type (Ityp)) then
4687 Ityp := Underlying_Type (Ityp);
4690 if Is_Concurrent_Type (Otyp) then
4691 Otyp := Corresponding_Record_Type (Otyp);
4694 if Is_Concurrent_Type (Ityp) then
4695 Ityp := Corresponding_Record_Type (Ityp);
4698 -- If the base types are the same, we know there is no problem since
4699 -- this conversion will be a noop.
4701 if Implementation_Base_Type (Otyp) = Implementation_Base_Type (Ityp) then
4704 -- Same if this is an upwards conversion of an untagged type, and there
4705 -- are no constraints involved (could be more general???)
4707 elsif Etype (Ityp) = Otyp
4708 and then not Is_Tagged_Type (Ityp)
4709 and then not Has_Discriminants (Ityp)
4710 and then No (First_Rep_Item (Base_Type (Ityp)))
4714 -- If the size of output type is known at compile time, there is
4715 -- never a problem. Note that unconstrained records are considered
4716 -- to be of known size, but we can't consider them that way here,
4717 -- because we are talking about the actual size of the object.
4719 -- We also make sure that in addition to the size being known, we do
4720 -- not have a case which might generate an embarrassingly large temp
4721 -- in stack checking mode.
4723 elsif Size_Known_At_Compile_Time (Otyp)
4725 (not Stack_Checking_Enabled
4726 or else not May_Generate_Large_Temp (Otyp))
4727 and then not (Is_Record_Type (Otyp) and then not Is_Constrained (Otyp))
4731 -- If either type is tagged, then we know the alignment is OK so
4732 -- Gigi will be able to use pointer punning.
4734 elsif Is_Tagged_Type (Otyp) or else Is_Tagged_Type (Ityp) then
4737 -- If either type is a limited record type, we cannot do a copy, so
4738 -- say safe since there's nothing else we can do.
4740 elsif Is_Limited_Record (Otyp) or else Is_Limited_Record (Ityp) then
4743 -- Conversions to and from packed array types are always ignored and
4746 elsif Is_Packed_Array_Type (Otyp)
4747 or else Is_Packed_Array_Type (Ityp)
4752 -- The only other cases known to be safe is if the input type's
4753 -- alignment is known to be at least the maximum alignment for the
4754 -- target or if both alignments are known and the output type's
4755 -- alignment is no stricter than the input's. We can use the alignment
4756 -- of the component type of an array if a type is an unpacked
4759 if Present (Alignment_Clause (Otyp)) then
4760 Oalign := Expr_Value (Expression (Alignment_Clause (Otyp)));
4762 elsif Is_Array_Type (Otyp)
4763 and then Present (Alignment_Clause (Component_Type (Otyp)))
4765 Oalign := Expr_Value (Expression (Alignment_Clause
4766 (Component_Type (Otyp))));
4769 if Present (Alignment_Clause (Ityp)) then
4770 Ialign := Expr_Value (Expression (Alignment_Clause (Ityp)));
4772 elsif Is_Array_Type (Ityp)
4773 and then Present (Alignment_Clause (Component_Type (Ityp)))
4775 Ialign := Expr_Value (Expression (Alignment_Clause
4776 (Component_Type (Ityp))));
4779 if Ialign /= No_Uint and then Ialign > Maximum_Alignment then
4782 elsif Ialign /= No_Uint and then Oalign /= No_Uint
4783 and then Ialign <= Oalign
4787 -- Otherwise, Gigi cannot handle this and we must make a temporary
4792 end Safe_Unchecked_Type_Conversion;
4794 ---------------------------------
4795 -- Set_Current_Value_Condition --
4796 ---------------------------------
4798 -- Note: the implementation of this procedure is very closely tied to the
4799 -- implementation of Get_Current_Value_Condition. Here we set required
4800 -- Current_Value fields, and in Get_Current_Value_Condition, we interpret
4801 -- them, so they must have a consistent view.
4803 procedure Set_Current_Value_Condition (Cnode : Node_Id) is
4805 procedure Set_Entity_Current_Value (N : Node_Id);
4806 -- If N is an entity reference, where the entity is of an appropriate
4807 -- kind, then set the current value of this entity to Cnode, unless
4808 -- there is already a definite value set there.
4810 procedure Set_Expression_Current_Value (N : Node_Id);
4811 -- If N is of an appropriate form, sets an appropriate entry in current
4812 -- value fields of relevant entities. Multiple entities can be affected
4813 -- in the case of an AND or AND THEN.
4815 ------------------------------
4816 -- Set_Entity_Current_Value --
4817 ------------------------------
4819 procedure Set_Entity_Current_Value (N : Node_Id) is
4821 if Is_Entity_Name (N) then
4823 Ent : constant Entity_Id := Entity (N);
4826 -- Don't capture if not safe to do so
4828 if not Safe_To_Capture_Value (N, Ent, Cond => True) then
4832 -- Here we have a case where the Current_Value field may
4833 -- need to be set. We set it if it is not already set to a
4834 -- compile time expression value.
4836 -- Note that this represents a decision that one condition
4837 -- blots out another previous one. That's certainly right
4838 -- if they occur at the same level. If the second one is
4839 -- nested, then the decision is neither right nor wrong (it
4840 -- would be equally OK to leave the outer one in place, or
4841 -- take the new inner one. Really we should record both, but
4842 -- our data structures are not that elaborate.
4844 if Nkind (Current_Value (Ent)) not in N_Subexpr then
4845 Set_Current_Value (Ent, Cnode);
4849 end Set_Entity_Current_Value;
4851 ----------------------------------
4852 -- Set_Expression_Current_Value --
4853 ----------------------------------
4855 procedure Set_Expression_Current_Value (N : Node_Id) is
4861 -- Loop to deal with (ignore for now) any NOT operators present. The
4862 -- presence of NOT operators will be handled properly when we call
4863 -- Get_Current_Value_Condition.
4865 while Nkind (Cond) = N_Op_Not loop
4866 Cond := Right_Opnd (Cond);
4869 -- For an AND or AND THEN, recursively process operands
4871 if Nkind (Cond) = N_Op_And or else Nkind (Cond) = N_And_Then then
4872 Set_Expression_Current_Value (Left_Opnd (Cond));
4873 Set_Expression_Current_Value (Right_Opnd (Cond));
4877 -- Check possible relational operator
4879 if Nkind (Cond) in N_Op_Compare then
4880 if Compile_Time_Known_Value (Right_Opnd (Cond)) then
4881 Set_Entity_Current_Value (Left_Opnd (Cond));
4882 elsif Compile_Time_Known_Value (Left_Opnd (Cond)) then
4883 Set_Entity_Current_Value (Right_Opnd (Cond));
4886 -- Check possible boolean variable reference
4889 Set_Entity_Current_Value (Cond);
4891 end Set_Expression_Current_Value;
4893 -- Start of processing for Set_Current_Value_Condition
4896 Set_Expression_Current_Value (Condition (Cnode));
4897 end Set_Current_Value_Condition;
4899 --------------------------
4900 -- Set_Elaboration_Flag --
4901 --------------------------
4903 procedure Set_Elaboration_Flag (N : Node_Id; Spec_Id : Entity_Id) is
4904 Loc : constant Source_Ptr := Sloc (N);
4905 Ent : constant Entity_Id := Elaboration_Entity (Spec_Id);
4909 if Present (Ent) then
4911 -- Nothing to do if at the compilation unit level, because in this
4912 -- case the flag is set by the binder generated elaboration routine.
4914 if Nkind (Parent (N)) = N_Compilation_Unit then
4917 -- Here we do need to generate an assignment statement
4920 Check_Restriction (No_Elaboration_Code, N);
4922 Make_Assignment_Statement (Loc,
4923 Name => New_Occurrence_Of (Ent, Loc),
4924 Expression => New_Occurrence_Of (Standard_True, Loc));
4926 if Nkind (Parent (N)) = N_Subunit then
4927 Insert_After (Corresponding_Stub (Parent (N)), Asn);
4929 Insert_After (N, Asn);
4934 -- Kill current value indication. This is necessary because
4935 -- the tests of this flag are inserted out of sequence and must
4936 -- not pick up bogus indications of the wrong constant value.
4938 Set_Current_Value (Ent, Empty);
4941 end Set_Elaboration_Flag;
4943 ----------------------------
4944 -- Set_Renamed_Subprogram --
4945 ----------------------------
4947 procedure Set_Renamed_Subprogram (N : Node_Id; E : Entity_Id) is
4949 -- If input node is an identifier, we can just reset it
4951 if Nkind (N) = N_Identifier then
4952 Set_Chars (N, Chars (E));
4955 -- Otherwise we have to do a rewrite, preserving Comes_From_Source
4959 CS : constant Boolean := Comes_From_Source (N);
4961 Rewrite (N, Make_Identifier (Sloc (N), Chars => Chars (E)));
4963 Set_Comes_From_Source (N, CS);
4964 Set_Analyzed (N, True);
4967 end Set_Renamed_Subprogram;
4969 --------------------------
4970 -- Target_Has_Fixed_Ops --
4971 --------------------------
4973 Integer_Sized_Small : Ureal;
4974 -- Set to 2.0 ** -(Integer'Size - 1) the first time that this
4975 -- function is called (we don't want to compute it more than once!)
4977 Long_Integer_Sized_Small : Ureal;
4978 -- Set to 2.0 ** -(Long_Integer'Size - 1) the first time that this
4979 -- functoin is called (we don't want to compute it more than once)
4981 First_Time_For_THFO : Boolean := True;
4982 -- Set to False after first call (if Fractional_Fixed_Ops_On_Target)
4984 function Target_Has_Fixed_Ops
4985 (Left_Typ : Entity_Id;
4986 Right_Typ : Entity_Id;
4987 Result_Typ : Entity_Id) return Boolean
4989 function Is_Fractional_Type (Typ : Entity_Id) return Boolean;
4990 -- Return True if the given type is a fixed-point type with a small
4991 -- value equal to 2 ** (-(T'Object_Size - 1)) and whose values have
4992 -- an absolute value less than 1.0. This is currently limited
4993 -- to fixed-point types that map to Integer or Long_Integer.
4995 ------------------------
4996 -- Is_Fractional_Type --
4997 ------------------------
4999 function Is_Fractional_Type (Typ : Entity_Id) return Boolean is
5001 if Esize (Typ) = Standard_Integer_Size then
5002 return Small_Value (Typ) = Integer_Sized_Small;
5004 elsif Esize (Typ) = Standard_Long_Integer_Size then
5005 return Small_Value (Typ) = Long_Integer_Sized_Small;
5010 end Is_Fractional_Type;
5012 -- Start of processing for Target_Has_Fixed_Ops
5015 -- Return False if Fractional_Fixed_Ops_On_Target is false
5017 if not Fractional_Fixed_Ops_On_Target then
5021 -- Here the target has Fractional_Fixed_Ops, if first time, compute
5022 -- standard constants used by Is_Fractional_Type.
5024 if First_Time_For_THFO then
5025 First_Time_For_THFO := False;
5027 Integer_Sized_Small :=
5030 Den => UI_From_Int (Standard_Integer_Size - 1),
5033 Long_Integer_Sized_Small :=
5036 Den => UI_From_Int (Standard_Long_Integer_Size - 1),
5040 -- Return True if target supports fixed-by-fixed multiply/divide
5041 -- for fractional fixed-point types (see Is_Fractional_Type) and
5042 -- the operand and result types are equivalent fractional types.
5044 return Is_Fractional_Type (Base_Type (Left_Typ))
5045 and then Is_Fractional_Type (Base_Type (Right_Typ))
5046 and then Is_Fractional_Type (Base_Type (Result_Typ))
5047 and then Esize (Left_Typ) = Esize (Right_Typ)
5048 and then Esize (Left_Typ) = Esize (Result_Typ);
5049 end Target_Has_Fixed_Ops;
5051 ------------------------------------------
5052 -- Type_May_Have_Bit_Aligned_Components --
5053 ------------------------------------------
5055 function Type_May_Have_Bit_Aligned_Components
5056 (Typ : Entity_Id) return Boolean
5059 -- Array type, check component type
5061 if Is_Array_Type (Typ) then
5063 Type_May_Have_Bit_Aligned_Components (Component_Type (Typ));
5065 -- Record type, check components
5067 elsif Is_Record_Type (Typ) then
5072 E := First_Component_Or_Discriminant (Typ);
5073 while Present (E) loop
5074 if Component_May_Be_Bit_Aligned (E)
5075 or else Type_May_Have_Bit_Aligned_Components (Etype (E))
5080 Next_Component_Or_Discriminant (E);
5086 -- Type other than array or record is always OK
5091 end Type_May_Have_Bit_Aligned_Components;
5093 ----------------------------
5094 -- Wrap_Cleanup_Procedure --
5095 ----------------------------
5097 procedure Wrap_Cleanup_Procedure (N : Node_Id) is
5098 Loc : constant Source_Ptr := Sloc (N);
5099 Stseq : constant Node_Id := Handled_Statement_Sequence (N);
5100 Stmts : constant List_Id := Statements (Stseq);
5103 if Abort_Allowed then
5104 Prepend_To (Stmts, Build_Runtime_Call (Loc, RE_Abort_Defer));
5105 Append_To (Stmts, Build_Runtime_Call (Loc, RE_Abort_Undefer));
5107 end Wrap_Cleanup_Procedure;