1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2010, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Atree; use Atree;
27 with Casing; use Casing;
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_Ch6; use Exp_Ch6;
35 with Exp_Ch7; use Exp_Ch7;
36 with Inline; use Inline;
37 with Itypes; use Itypes;
39 with Nlists; use Nlists;
40 with Nmake; use Nmake;
42 with Restrict; use Restrict;
43 with Rident; use Rident;
45 with Sem_Aux; use Sem_Aux;
46 with Sem_Ch8; use Sem_Ch8;
47 with Sem_Eval; use Sem_Eval;
48 with Sem_Prag; use Sem_Prag;
49 with Sem_Res; use Sem_Res;
50 with Sem_Type; use Sem_Type;
51 with Sem_Util; use Sem_Util;
52 with Snames; use Snames;
53 with Stand; use Stand;
54 with Stringt; use Stringt;
55 with Targparm; use Targparm;
56 with Tbuild; use Tbuild;
57 with Ttypes; use Ttypes;
58 with Uintp; use Uintp;
59 with Urealp; use Urealp;
60 with Validsw; use Validsw;
62 package body Exp_Util is
64 -----------------------
65 -- Local Subprograms --
66 -----------------------
68 function Build_Task_Array_Image
72 Dyn : Boolean := False) return Node_Id;
73 -- Build function to generate the image string for a task that is an
74 -- array component, concatenating the images of each index. To avoid
75 -- storage leaks, the string is built with successive slice assignments.
76 -- The flag Dyn indicates whether this is called for the initialization
77 -- procedure of an array of tasks, or for the name of a dynamically
78 -- created task that is assigned to an indexed component.
80 function Build_Task_Image_Function
84 Res : Entity_Id) return Node_Id;
85 -- Common processing for Task_Array_Image and Task_Record_Image.
86 -- Build function body that computes image.
88 procedure Build_Task_Image_Prefix
97 -- Common processing for Task_Array_Image and Task_Record_Image.
98 -- Create local variables and assign prefix of name to result string.
100 function Build_Task_Record_Image
103 Dyn : Boolean := False) return Node_Id;
104 -- Build function to generate the image string for a task that is a
105 -- record component. Concatenate name of variable with that of selector.
106 -- The flag Dyn indicates whether this is called for the initialization
107 -- procedure of record with task components, or for a dynamically
108 -- created task that is assigned to a selected component.
110 function Make_CW_Equivalent_Type
112 E : Node_Id) return Entity_Id;
113 -- T is a class-wide type entity, E is the initial expression node that
114 -- constrains T in case such as: " X: T := E" or "new T'(E)"
115 -- This function returns the entity of the Equivalent type and inserts
116 -- on the fly the necessary declaration such as:
118 -- type anon is record
119 -- _parent : Root_Type (T); constrained with E discriminants (if any)
120 -- Extension : String (1 .. expr to match size of E);
123 -- This record is compatible with any object of the class of T thanks
124 -- to the first field and has the same size as E thanks to the second.
126 function Make_Literal_Range
128 Literal_Typ : Entity_Id) return Node_Id;
129 -- Produce a Range node whose bounds are:
130 -- Low_Bound (Literal_Type) ..
131 -- Low_Bound (Literal_Type) + (Length (Literal_Typ) - 1)
132 -- this is used for expanding declarations like X : String := "sdfgdfg";
134 -- If the index type of the target array is not integer, we generate:
135 -- Low_Bound (Literal_Type) ..
137 -- (Literal_Type'Pos (Low_Bound (Literal_Type))
138 -- + (Length (Literal_Typ) -1))
140 function Make_Non_Empty_Check
142 N : Node_Id) return Node_Id;
143 -- Produce a boolean expression checking that the unidimensional array
144 -- node N is not empty.
146 function New_Class_Wide_Subtype
148 N : Node_Id) return Entity_Id;
149 -- Create an implicit subtype of CW_Typ attached to node N
151 ----------------------
152 -- Adjust_Condition --
153 ----------------------
155 procedure Adjust_Condition (N : Node_Id) is
162 Loc : constant Source_Ptr := Sloc (N);
163 T : constant Entity_Id := Etype (N);
167 -- For now, we simply ignore a call where the argument has no
168 -- type (probably case of unanalyzed condition), or has a type
169 -- that is not Boolean. This is because this is a pretty marginal
170 -- piece of functionality, and violations of these rules are
171 -- likely to be truly marginal (how much code uses Fortran Logical
172 -- as the barrier to a protected entry?) and we do not want to
173 -- blow up existing programs. We can change this to an assertion
174 -- after 3.12a is released ???
176 if No (T) or else not Is_Boolean_Type (T) then
180 -- Apply validity checking if needed
182 if Validity_Checks_On and Validity_Check_Tests then
186 -- Immediate return if standard boolean, the most common case,
187 -- where nothing needs to be done.
189 if Base_Type (T) = Standard_Boolean then
193 -- Case of zero/non-zero semantics or non-standard enumeration
194 -- representation. In each case, we rewrite the node as:
196 -- ityp!(N) /= False'Enum_Rep
198 -- where ityp is an integer type with large enough size to hold
199 -- any value of type T.
201 if Nonzero_Is_True (T) or else Has_Non_Standard_Rep (T) then
202 if Esize (T) <= Esize (Standard_Integer) then
203 Ti := Standard_Integer;
205 Ti := Standard_Long_Long_Integer;
210 Left_Opnd => Unchecked_Convert_To (Ti, N),
212 Make_Attribute_Reference (Loc,
213 Attribute_Name => Name_Enum_Rep,
215 New_Occurrence_Of (First_Literal (T), Loc))));
216 Analyze_And_Resolve (N, Standard_Boolean);
219 Rewrite (N, Convert_To (Standard_Boolean, N));
220 Analyze_And_Resolve (N, Standard_Boolean);
223 end Adjust_Condition;
225 ------------------------
226 -- Adjust_Result_Type --
227 ------------------------
229 procedure Adjust_Result_Type (N : Node_Id; T : Entity_Id) is
231 -- Ignore call if current type is not Standard.Boolean
233 if Etype (N) /= Standard_Boolean then
237 -- If result is already of correct type, nothing to do. Note that
238 -- this will get the most common case where everything has a type
239 -- of Standard.Boolean.
241 if Base_Type (T) = Standard_Boolean then
246 KP : constant Node_Kind := Nkind (Parent (N));
249 -- If result is to be used as a Condition in the syntax, no need
250 -- to convert it back, since if it was changed to Standard.Boolean
251 -- using Adjust_Condition, that is just fine for this usage.
253 if KP in N_Raise_xxx_Error or else KP in N_Has_Condition then
256 -- If result is an operand of another logical operation, no need
257 -- to reset its type, since Standard.Boolean is just fine, and
258 -- such operations always do Adjust_Condition on their operands.
260 elsif KP in N_Op_Boolean
261 or else KP in N_Short_Circuit
262 or else KP = N_Op_Not
266 -- Otherwise we perform a conversion from the current type,
267 -- which must be Standard.Boolean, to the desired type.
271 Rewrite (N, Convert_To (T, N));
272 Analyze_And_Resolve (N, T);
276 end Adjust_Result_Type;
278 --------------------------
279 -- Append_Freeze_Action --
280 --------------------------
282 procedure Append_Freeze_Action (T : Entity_Id; N : Node_Id) is
286 Ensure_Freeze_Node (T);
287 Fnode := Freeze_Node (T);
289 if No (Actions (Fnode)) then
290 Set_Actions (Fnode, New_List);
293 Append (N, Actions (Fnode));
294 end Append_Freeze_Action;
296 ---------------------------
297 -- Append_Freeze_Actions --
298 ---------------------------
300 procedure Append_Freeze_Actions (T : Entity_Id; L : List_Id) is
301 Fnode : constant Node_Id := Freeze_Node (T);
308 if No (Actions (Fnode)) then
309 Set_Actions (Fnode, L);
311 Append_List (L, Actions (Fnode));
314 end Append_Freeze_Actions;
316 ------------------------
317 -- Build_Runtime_Call --
318 ------------------------
320 function Build_Runtime_Call (Loc : Source_Ptr; RE : RE_Id) return Node_Id is
322 -- If entity is not available, we can skip making the call (this avoids
323 -- junk duplicated error messages in a number of cases).
325 if not RTE_Available (RE) then
326 return Make_Null_Statement (Loc);
329 Make_Procedure_Call_Statement (Loc,
330 Name => New_Reference_To (RTE (RE), Loc));
332 end Build_Runtime_Call;
334 ----------------------------
335 -- Build_Task_Array_Image --
336 ----------------------------
338 -- This function generates the body for a function that constructs the
339 -- image string for a task that is an array component. The function is
340 -- local to the init proc for the array type, and is called for each one
341 -- of the components. The constructed image has the form of an indexed
342 -- component, whose prefix is the outer variable of the array type.
343 -- The n-dimensional array type has known indexes Index, Index2...
344 -- Id_Ref is an indexed component form created by the enclosing init proc.
345 -- Its successive indexes are Val1, Val2, ... which are the loop variables
346 -- in the loops that call the individual task init proc on each component.
348 -- The generated function has the following structure:
350 -- function F return String is
351 -- Pref : string renames Task_Name;
352 -- T1 : String := Index1'Image (Val1);
354 -- Tn : String := indexn'image (Valn);
355 -- Len : Integer := T1'Length + ... + Tn'Length + n + 1;
356 -- -- Len includes commas and the end parentheses.
357 -- Res : String (1..Len);
358 -- Pos : Integer := Pref'Length;
361 -- Res (1 .. Pos) := Pref;
365 -- Res (Pos .. Pos + T1'Length - 1) := T1;
366 -- Pos := Pos + T1'Length;
370 -- Res (Pos .. Pos + Tn'Length - 1) := Tn;
376 -- Needless to say, multidimensional arrays of tasks are rare enough
377 -- that the bulkiness of this code is not really a concern.
379 function Build_Task_Array_Image
383 Dyn : Boolean := False) return Node_Id
385 Dims : constant Nat := Number_Dimensions (A_Type);
386 -- Number of dimensions for array of tasks
388 Temps : array (1 .. Dims) of Entity_Id;
389 -- Array of temporaries to hold string for each index
395 -- Total length of generated name
398 -- Running index for substring assignments
400 Pref : constant Entity_Id := Make_Temporary (Loc, 'P');
401 -- Name of enclosing variable, prefix of resulting name
404 -- String to hold result
407 -- Value of successive indexes
410 -- Expression to compute total size of string
413 -- Entity for name at one index position
415 Decls : constant List_Id := New_List;
416 Stats : constant List_Id := New_List;
419 -- For a dynamic task, the name comes from the target variable.
420 -- For a static one it is a formal of the enclosing init proc.
423 Get_Name_String (Chars (Entity (Prefix (Id_Ref))));
425 Make_Object_Declaration (Loc,
426 Defining_Identifier => Pref,
427 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
429 Make_String_Literal (Loc,
430 Strval => String_From_Name_Buffer)));
434 Make_Object_Renaming_Declaration (Loc,
435 Defining_Identifier => Pref,
436 Subtype_Mark => New_Occurrence_Of (Standard_String, Loc),
437 Name => Make_Identifier (Loc, Name_uTask_Name)));
440 Indx := First_Index (A_Type);
441 Val := First (Expressions (Id_Ref));
443 for J in 1 .. Dims loop
444 T := Make_Temporary (Loc, 'T');
448 Make_Object_Declaration (Loc,
449 Defining_Identifier => T,
450 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
452 Make_Attribute_Reference (Loc,
453 Attribute_Name => Name_Image,
454 Prefix => New_Occurrence_Of (Etype (Indx), Loc),
455 Expressions => New_List (New_Copy_Tree (Val)))));
461 Sum := Make_Integer_Literal (Loc, Dims + 1);
467 Make_Attribute_Reference (Loc,
468 Attribute_Name => Name_Length,
470 New_Occurrence_Of (Pref, Loc),
471 Expressions => New_List (Make_Integer_Literal (Loc, 1))));
473 for J in 1 .. Dims loop
478 Make_Attribute_Reference (Loc,
479 Attribute_Name => Name_Length,
481 New_Occurrence_Of (Temps (J), Loc),
482 Expressions => New_List (Make_Integer_Literal (Loc, 1))));
485 Build_Task_Image_Prefix (Loc, Len, Res, Pos, Pref, Sum, Decls, Stats);
487 Set_Character_Literal_Name (Char_Code (Character'Pos ('(')));
490 Make_Assignment_Statement (Loc,
491 Name => Make_Indexed_Component (Loc,
492 Prefix => New_Occurrence_Of (Res, Loc),
493 Expressions => New_List (New_Occurrence_Of (Pos, Loc))),
495 Make_Character_Literal (Loc,
497 Char_Literal_Value =>
498 UI_From_Int (Character'Pos ('(')))));
501 Make_Assignment_Statement (Loc,
502 Name => New_Occurrence_Of (Pos, Loc),
505 Left_Opnd => New_Occurrence_Of (Pos, Loc),
506 Right_Opnd => Make_Integer_Literal (Loc, 1))));
508 for J in 1 .. Dims loop
511 Make_Assignment_Statement (Loc,
512 Name => Make_Slice (Loc,
513 Prefix => New_Occurrence_Of (Res, Loc),
516 Low_Bound => New_Occurrence_Of (Pos, Loc),
517 High_Bound => Make_Op_Subtract (Loc,
520 Left_Opnd => New_Occurrence_Of (Pos, Loc),
522 Make_Attribute_Reference (Loc,
523 Attribute_Name => Name_Length,
525 New_Occurrence_Of (Temps (J), Loc),
527 New_List (Make_Integer_Literal (Loc, 1)))),
528 Right_Opnd => Make_Integer_Literal (Loc, 1)))),
530 Expression => New_Occurrence_Of (Temps (J), Loc)));
534 Make_Assignment_Statement (Loc,
535 Name => New_Occurrence_Of (Pos, Loc),
538 Left_Opnd => New_Occurrence_Of (Pos, Loc),
540 Make_Attribute_Reference (Loc,
541 Attribute_Name => Name_Length,
542 Prefix => New_Occurrence_Of (Temps (J), Loc),
544 New_List (Make_Integer_Literal (Loc, 1))))));
546 Set_Character_Literal_Name (Char_Code (Character'Pos (',')));
549 Make_Assignment_Statement (Loc,
550 Name => Make_Indexed_Component (Loc,
551 Prefix => New_Occurrence_Of (Res, Loc),
552 Expressions => New_List (New_Occurrence_Of (Pos, Loc))),
554 Make_Character_Literal (Loc,
556 Char_Literal_Value =>
557 UI_From_Int (Character'Pos (',')))));
560 Make_Assignment_Statement (Loc,
561 Name => New_Occurrence_Of (Pos, Loc),
564 Left_Opnd => New_Occurrence_Of (Pos, Loc),
565 Right_Opnd => Make_Integer_Literal (Loc, 1))));
569 Set_Character_Literal_Name (Char_Code (Character'Pos (')')));
572 Make_Assignment_Statement (Loc,
573 Name => Make_Indexed_Component (Loc,
574 Prefix => New_Occurrence_Of (Res, Loc),
575 Expressions => New_List (New_Occurrence_Of (Len, Loc))),
577 Make_Character_Literal (Loc,
579 Char_Literal_Value =>
580 UI_From_Int (Character'Pos (')')))));
581 return Build_Task_Image_Function (Loc, Decls, Stats, Res);
582 end Build_Task_Array_Image;
584 ----------------------------
585 -- Build_Task_Image_Decls --
586 ----------------------------
588 function Build_Task_Image_Decls
592 In_Init_Proc : Boolean := False) return List_Id
594 Decls : constant List_Id := New_List;
595 T_Id : Entity_Id := Empty;
597 Expr : Node_Id := Empty;
598 Fun : Node_Id := Empty;
599 Is_Dyn : constant Boolean :=
600 Nkind (Parent (Id_Ref)) = N_Assignment_Statement
602 Nkind (Expression (Parent (Id_Ref))) = N_Allocator;
605 -- If Discard_Names or No_Implicit_Heap_Allocations are in effect,
606 -- generate a dummy declaration only.
608 if Restriction_Active (No_Implicit_Heap_Allocations)
609 or else Global_Discard_Names
611 T_Id := Make_Temporary (Loc, 'J');
616 Make_Object_Declaration (Loc,
617 Defining_Identifier => T_Id,
618 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
620 Make_String_Literal (Loc,
621 Strval => String_From_Name_Buffer)));
624 if Nkind (Id_Ref) = N_Identifier
625 or else Nkind (Id_Ref) = N_Defining_Identifier
627 -- For a simple variable, the image of the task is built from
628 -- the name of the variable. To avoid possible conflict with
629 -- the anonymous type created for a single protected object,
630 -- add a numeric suffix.
633 Make_Defining_Identifier (Loc,
634 New_External_Name (Chars (Id_Ref), 'T', 1));
636 Get_Name_String (Chars (Id_Ref));
639 Make_String_Literal (Loc,
640 Strval => String_From_Name_Buffer);
642 elsif Nkind (Id_Ref) = N_Selected_Component then
644 Make_Defining_Identifier (Loc,
645 New_External_Name (Chars (Selector_Name (Id_Ref)), 'T'));
646 Fun := Build_Task_Record_Image (Loc, Id_Ref, Is_Dyn);
648 elsif Nkind (Id_Ref) = N_Indexed_Component then
650 Make_Defining_Identifier (Loc,
651 New_External_Name (Chars (A_Type), 'N'));
653 Fun := Build_Task_Array_Image (Loc, Id_Ref, A_Type, Is_Dyn);
657 if Present (Fun) then
659 Expr := Make_Function_Call (Loc,
660 Name => New_Occurrence_Of (Defining_Entity (Fun), Loc));
662 if not In_Init_Proc and then VM_Target = No_VM then
663 Set_Uses_Sec_Stack (Defining_Entity (Fun));
667 Decl := Make_Object_Declaration (Loc,
668 Defining_Identifier => T_Id,
669 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
670 Constant_Present => True,
673 Append (Decl, Decls);
675 end Build_Task_Image_Decls;
677 -------------------------------
678 -- Build_Task_Image_Function --
679 -------------------------------
681 function Build_Task_Image_Function
685 Res : Entity_Id) return Node_Id
691 Make_Simple_Return_Statement (Loc,
692 Expression => New_Occurrence_Of (Res, Loc)));
694 Spec := Make_Function_Specification (Loc,
695 Defining_Unit_Name => Make_Temporary (Loc, 'F'),
696 Result_Definition => New_Occurrence_Of (Standard_String, Loc));
698 -- Calls to 'Image use the secondary stack, which must be cleaned
699 -- up after the task name is built.
701 return Make_Subprogram_Body (Loc,
702 Specification => Spec,
703 Declarations => Decls,
704 Handled_Statement_Sequence =>
705 Make_Handled_Sequence_Of_Statements (Loc, Statements => Stats));
706 end Build_Task_Image_Function;
708 -----------------------------
709 -- Build_Task_Image_Prefix --
710 -----------------------------
712 procedure Build_Task_Image_Prefix
723 Len := Make_Temporary (Loc, 'L', Sum);
726 Make_Object_Declaration (Loc,
727 Defining_Identifier => Len,
728 Object_Definition => New_Occurrence_Of (Standard_Integer, Loc),
731 Res := Make_Temporary (Loc, 'R');
734 Make_Object_Declaration (Loc,
735 Defining_Identifier => Res,
737 Make_Subtype_Indication (Loc,
738 Subtype_Mark => New_Occurrence_Of (Standard_String, Loc),
740 Make_Index_Or_Discriminant_Constraint (Loc,
744 Low_Bound => Make_Integer_Literal (Loc, 1),
745 High_Bound => New_Occurrence_Of (Len, Loc)))))));
747 Pos := Make_Temporary (Loc, 'P');
750 Make_Object_Declaration (Loc,
751 Defining_Identifier => Pos,
752 Object_Definition => New_Occurrence_Of (Standard_Integer, Loc)));
754 -- Pos := Prefix'Length;
757 Make_Assignment_Statement (Loc,
758 Name => New_Occurrence_Of (Pos, Loc),
760 Make_Attribute_Reference (Loc,
761 Attribute_Name => Name_Length,
762 Prefix => New_Occurrence_Of (Prefix, Loc),
763 Expressions => New_List (Make_Integer_Literal (Loc, 1)))));
765 -- Res (1 .. Pos) := Prefix;
768 Make_Assignment_Statement (Loc,
771 Prefix => New_Occurrence_Of (Res, Loc),
774 Low_Bound => Make_Integer_Literal (Loc, 1),
775 High_Bound => New_Occurrence_Of (Pos, Loc))),
777 Expression => New_Occurrence_Of (Prefix, Loc)));
780 Make_Assignment_Statement (Loc,
781 Name => New_Occurrence_Of (Pos, Loc),
784 Left_Opnd => New_Occurrence_Of (Pos, Loc),
785 Right_Opnd => Make_Integer_Literal (Loc, 1))));
786 end Build_Task_Image_Prefix;
788 -----------------------------
789 -- Build_Task_Record_Image --
790 -----------------------------
792 function Build_Task_Record_Image
795 Dyn : Boolean := False) return Node_Id
798 -- Total length of generated name
804 -- String to hold result
806 Pref : constant Entity_Id := Make_Temporary (Loc, 'P');
807 -- Name of enclosing variable, prefix of resulting name
810 -- Expression to compute total size of string
813 -- Entity for selector name
815 Decls : constant List_Id := New_List;
816 Stats : constant List_Id := New_List;
819 -- For a dynamic task, the name comes from the target variable. For a
820 -- static one it is a formal of the enclosing init proc.
823 Get_Name_String (Chars (Entity (Prefix (Id_Ref))));
825 Make_Object_Declaration (Loc,
826 Defining_Identifier => Pref,
827 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
829 Make_String_Literal (Loc,
830 Strval => String_From_Name_Buffer)));
834 Make_Object_Renaming_Declaration (Loc,
835 Defining_Identifier => Pref,
836 Subtype_Mark => New_Occurrence_Of (Standard_String, Loc),
837 Name => Make_Identifier (Loc, Name_uTask_Name)));
840 Sel := Make_Temporary (Loc, 'S');
842 Get_Name_String (Chars (Selector_Name (Id_Ref)));
845 Make_Object_Declaration (Loc,
846 Defining_Identifier => Sel,
847 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
849 Make_String_Literal (Loc,
850 Strval => String_From_Name_Buffer)));
852 Sum := Make_Integer_Literal (Loc, Nat (Name_Len + 1));
858 Make_Attribute_Reference (Loc,
859 Attribute_Name => Name_Length,
861 New_Occurrence_Of (Pref, Loc),
862 Expressions => New_List (Make_Integer_Literal (Loc, 1))));
864 Build_Task_Image_Prefix (Loc, Len, Res, Pos, Pref, Sum, Decls, Stats);
866 Set_Character_Literal_Name (Char_Code (Character'Pos ('.')));
871 Make_Assignment_Statement (Loc,
872 Name => Make_Indexed_Component (Loc,
873 Prefix => New_Occurrence_Of (Res, Loc),
874 Expressions => New_List (New_Occurrence_Of (Pos, Loc))),
876 Make_Character_Literal (Loc,
878 Char_Literal_Value =>
879 UI_From_Int (Character'Pos ('.')))));
882 Make_Assignment_Statement (Loc,
883 Name => New_Occurrence_Of (Pos, Loc),
886 Left_Opnd => New_Occurrence_Of (Pos, Loc),
887 Right_Opnd => Make_Integer_Literal (Loc, 1))));
889 -- Res (Pos .. Len) := Selector;
892 Make_Assignment_Statement (Loc,
893 Name => Make_Slice (Loc,
894 Prefix => New_Occurrence_Of (Res, Loc),
897 Low_Bound => New_Occurrence_Of (Pos, Loc),
898 High_Bound => New_Occurrence_Of (Len, Loc))),
899 Expression => New_Occurrence_Of (Sel, Loc)));
901 return Build_Task_Image_Function (Loc, Decls, Stats, Res);
902 end Build_Task_Record_Image;
904 ----------------------------------
905 -- Component_May_Be_Bit_Aligned --
906 ----------------------------------
908 function Component_May_Be_Bit_Aligned (Comp : Entity_Id) return Boolean is
912 -- If no component clause, then everything is fine, since the back end
913 -- never bit-misaligns by default, even if there is a pragma Packed for
916 if No (Comp) or else No (Component_Clause (Comp)) then
920 UT := Underlying_Type (Etype (Comp));
922 -- It is only array and record types that cause trouble
924 if not Is_Record_Type (UT)
925 and then not Is_Array_Type (UT)
929 -- If we know that we have a small (64 bits or less) record or small
930 -- bit-packed array, then everything is fine, since the back end can
931 -- handle these cases correctly.
933 elsif Esize (Comp) <= 64
934 and then (Is_Record_Type (UT)
935 or else Is_Bit_Packed_Array (UT))
939 -- Otherwise if the component is not byte aligned, we know we have the
940 -- nasty unaligned case.
942 elsif Normalized_First_Bit (Comp) /= Uint_0
943 or else Esize (Comp) mod System_Storage_Unit /= Uint_0
947 -- If we are large and byte aligned, then OK at this level
952 end Component_May_Be_Bit_Aligned;
954 -----------------------------------
955 -- Corresponding_Runtime_Package --
956 -----------------------------------
958 function Corresponding_Runtime_Package (Typ : Entity_Id) return RTU_Id is
959 Pkg_Id : RTU_Id := RTU_Null;
962 pragma Assert (Is_Concurrent_Type (Typ));
964 if Ekind (Typ) in Protected_Kind then
966 or else Has_Interrupt_Handler (Typ)
967 or else (Has_Attach_Handler (Typ)
968 and then not Restricted_Profile)
970 -- A protected type without entries that covers an interface and
971 -- overrides the abstract routines with protected procedures is
972 -- considered equivalent to a protected type with entries in the
973 -- context of dispatching select statements. It is sufficient to
974 -- check for the presence of an interface list in the declaration
975 -- node to recognize this case.
977 or else Present (Interface_List (Parent (Typ)))
980 or else Restriction_Active (No_Entry_Queue) = False
981 or else Number_Entries (Typ) > 1
982 or else (Has_Attach_Handler (Typ)
983 and then not Restricted_Profile)
985 Pkg_Id := System_Tasking_Protected_Objects_Entries;
987 Pkg_Id := System_Tasking_Protected_Objects_Single_Entry;
991 Pkg_Id := System_Tasking_Protected_Objects;
996 end Corresponding_Runtime_Package;
998 -------------------------------
999 -- Convert_To_Actual_Subtype --
1000 -------------------------------
1002 procedure Convert_To_Actual_Subtype (Exp : Entity_Id) is
1006 Act_ST := Get_Actual_Subtype (Exp);
1008 if Act_ST = Etype (Exp) then
1013 Convert_To (Act_ST, Relocate_Node (Exp)));
1014 Analyze_And_Resolve (Exp, Act_ST);
1016 end Convert_To_Actual_Subtype;
1018 -----------------------------------
1019 -- Current_Sem_Unit_Declarations --
1020 -----------------------------------
1022 function Current_Sem_Unit_Declarations return List_Id is
1023 U : Node_Id := Unit (Cunit (Current_Sem_Unit));
1027 -- If the current unit is a package body, locate the visible
1028 -- declarations of the package spec.
1030 if Nkind (U) = N_Package_Body then
1031 U := Unit (Library_Unit (Cunit (Current_Sem_Unit)));
1034 if Nkind (U) = N_Package_Declaration then
1035 U := Specification (U);
1036 Decls := Visible_Declarations (U);
1040 Set_Visible_Declarations (U, Decls);
1044 Decls := Declarations (U);
1048 Set_Declarations (U, Decls);
1053 end Current_Sem_Unit_Declarations;
1055 -----------------------
1056 -- Duplicate_Subexpr --
1057 -----------------------
1059 function Duplicate_Subexpr
1061 Name_Req : Boolean := False) return Node_Id
1064 Remove_Side_Effects (Exp, Name_Req);
1065 return New_Copy_Tree (Exp);
1066 end Duplicate_Subexpr;
1068 ---------------------------------
1069 -- Duplicate_Subexpr_No_Checks --
1070 ---------------------------------
1072 function Duplicate_Subexpr_No_Checks
1074 Name_Req : Boolean := False) return Node_Id
1079 Remove_Side_Effects (Exp, Name_Req);
1080 New_Exp := New_Copy_Tree (Exp);
1081 Remove_Checks (New_Exp);
1083 end Duplicate_Subexpr_No_Checks;
1085 -----------------------------------
1086 -- Duplicate_Subexpr_Move_Checks --
1087 -----------------------------------
1089 function Duplicate_Subexpr_Move_Checks
1091 Name_Req : Boolean := False) return Node_Id
1096 Remove_Side_Effects (Exp, Name_Req);
1097 New_Exp := New_Copy_Tree (Exp);
1098 Remove_Checks (Exp);
1100 end Duplicate_Subexpr_Move_Checks;
1102 --------------------
1103 -- Ensure_Defined --
1104 --------------------
1106 procedure Ensure_Defined (Typ : Entity_Id; N : Node_Id) is
1110 -- An itype reference must only be created if this is a local itype, so
1111 -- that gigi can elaborate it on the proper objstack.
1114 and then Scope (Typ) = Current_Scope
1116 IR := Make_Itype_Reference (Sloc (N));
1117 Set_Itype (IR, Typ);
1118 Insert_Action (N, IR);
1122 --------------------
1123 -- Entry_Names_OK --
1124 --------------------
1126 function Entry_Names_OK return Boolean is
1129 not Restricted_Profile
1130 and then not Global_Discard_Names
1131 and then not Restriction_Active (No_Implicit_Heap_Allocations)
1132 and then not Restriction_Active (No_Local_Allocators);
1135 ---------------------
1136 -- Evolve_And_Then --
1137 ---------------------
1139 procedure Evolve_And_Then (Cond : in out Node_Id; Cond1 : Node_Id) is
1145 Make_And_Then (Sloc (Cond1),
1147 Right_Opnd => Cond1);
1149 end Evolve_And_Then;
1151 --------------------
1152 -- Evolve_Or_Else --
1153 --------------------
1155 procedure Evolve_Or_Else (Cond : in out Node_Id; Cond1 : Node_Id) is
1161 Make_Or_Else (Sloc (Cond1),
1163 Right_Opnd => Cond1);
1167 ------------------------------
1168 -- Expand_Subtype_From_Expr --
1169 ------------------------------
1171 -- This function is applicable for both static and dynamic allocation of
1172 -- objects which are constrained by an initial expression. Basically it
1173 -- transforms an unconstrained subtype indication into a constrained one.
1174 -- The expression may also be transformed in certain cases in order to
1175 -- avoid multiple evaluation. In the static allocation case, the general
1180 -- is transformed into
1182 -- Val : Constrained_Subtype_of_T := Maybe_Modified_Expr;
1184 -- Here are the main cases :
1186 -- <if Expr is a Slice>
1187 -- Val : T ([Index_Subtype (Expr)]) := Expr;
1189 -- <elsif Expr is a String Literal>
1190 -- Val : T (T'First .. T'First + Length (string literal) - 1) := Expr;
1192 -- <elsif Expr is Constrained>
1193 -- subtype T is Type_Of_Expr
1196 -- <elsif Expr is an entity_name>
1197 -- Val : T (constraints taken from Expr) := Expr;
1200 -- type Axxx is access all T;
1201 -- Rval : Axxx := Expr'ref;
1202 -- Val : T (constraints taken from Rval) := Rval.all;
1204 -- ??? note: when the Expression is allocated in the secondary stack
1205 -- we could use it directly instead of copying it by declaring
1206 -- Val : T (...) renames Rval.all
1208 procedure Expand_Subtype_From_Expr
1210 Unc_Type : Entity_Id;
1211 Subtype_Indic : Node_Id;
1214 Loc : constant Source_Ptr := Sloc (N);
1215 Exp_Typ : constant Entity_Id := Etype (Exp);
1219 -- In general we cannot build the subtype if expansion is disabled,
1220 -- because internal entities may not have been defined. However, to
1221 -- avoid some cascaded errors, we try to continue when the expression is
1222 -- an array (or string), because it is safe to compute the bounds. It is
1223 -- in fact required to do so even in a generic context, because there
1224 -- may be constants that depend on the bounds of a string literal, both
1225 -- standard string types and more generally arrays of characters.
1227 if not Expander_Active
1228 and then (No (Etype (Exp))
1229 or else not Is_String_Type (Etype (Exp)))
1234 if Nkind (Exp) = N_Slice then
1236 Slice_Type : constant Entity_Id := Etype (First_Index (Exp_Typ));
1239 Rewrite (Subtype_Indic,
1240 Make_Subtype_Indication (Loc,
1241 Subtype_Mark => New_Reference_To (Unc_Type, Loc),
1243 Make_Index_Or_Discriminant_Constraint (Loc,
1244 Constraints => New_List
1245 (New_Reference_To (Slice_Type, Loc)))));
1247 -- This subtype indication may be used later for constraint checks
1248 -- we better make sure that if a variable was used as a bound of
1249 -- of the original slice, its value is frozen.
1251 Force_Evaluation (Low_Bound (Scalar_Range (Slice_Type)));
1252 Force_Evaluation (High_Bound (Scalar_Range (Slice_Type)));
1255 elsif Ekind (Exp_Typ) = E_String_Literal_Subtype then
1256 Rewrite (Subtype_Indic,
1257 Make_Subtype_Indication (Loc,
1258 Subtype_Mark => New_Reference_To (Unc_Type, Loc),
1260 Make_Index_Or_Discriminant_Constraint (Loc,
1261 Constraints => New_List (
1262 Make_Literal_Range (Loc,
1263 Literal_Typ => Exp_Typ)))));
1265 elsif Is_Constrained (Exp_Typ)
1266 and then not Is_Class_Wide_Type (Unc_Type)
1268 if Is_Itype (Exp_Typ) then
1270 -- Within an initialization procedure, a selected component
1271 -- denotes a component of the enclosing record, and it appears
1272 -- as an actual in a call to its own initialization procedure.
1273 -- If this component depends on the outer discriminant, we must
1274 -- generate the proper actual subtype for it.
1276 if Nkind (Exp) = N_Selected_Component
1277 and then Within_Init_Proc
1280 Decl : constant Node_Id :=
1281 Build_Actual_Subtype_Of_Component (Exp_Typ, Exp);
1283 if Present (Decl) then
1284 Insert_Action (N, Decl);
1285 T := Defining_Identifier (Decl);
1291 -- No need to generate a new one (new what???)
1298 T := Make_Temporary (Loc, 'T');
1301 Make_Subtype_Declaration (Loc,
1302 Defining_Identifier => T,
1303 Subtype_Indication => New_Reference_To (Exp_Typ, Loc)));
1305 -- This type is marked as an itype even though it has an
1306 -- explicit declaration because otherwise it can be marked
1307 -- with Is_Generic_Actual_Type and generate spurious errors.
1308 -- (see sem_ch8.Analyze_Package_Renaming and sem_type.covers)
1311 Set_Associated_Node_For_Itype (T, Exp);
1314 Rewrite (Subtype_Indic, New_Reference_To (T, Loc));
1316 -- Nothing needs to be done for private types with unknown discriminants
1317 -- if the underlying type is not an unconstrained composite type or it
1318 -- is an unchecked union.
1320 elsif Is_Private_Type (Unc_Type)
1321 and then Has_Unknown_Discriminants (Unc_Type)
1322 and then (not Is_Composite_Type (Underlying_Type (Unc_Type))
1323 or else Is_Constrained (Underlying_Type (Unc_Type))
1324 or else Is_Unchecked_Union (Underlying_Type (Unc_Type)))
1328 -- Case of derived type with unknown discriminants where the parent type
1329 -- also has unknown discriminants.
1331 elsif Is_Record_Type (Unc_Type)
1332 and then not Is_Class_Wide_Type (Unc_Type)
1333 and then Has_Unknown_Discriminants (Unc_Type)
1334 and then Has_Unknown_Discriminants (Underlying_Type (Unc_Type))
1336 -- Nothing to be done if no underlying record view available
1338 if No (Underlying_Record_View (Unc_Type)) then
1341 -- Otherwise use the Underlying_Record_View to create the proper
1342 -- constrained subtype for an object of a derived type with unknown
1346 Remove_Side_Effects (Exp);
1347 Rewrite (Subtype_Indic,
1348 Make_Subtype_From_Expr (Exp, Underlying_Record_View (Unc_Type)));
1351 -- Renamings of class-wide interface types require no equivalent
1352 -- constrained type declarations because we only need to reference
1353 -- the tag component associated with the interface.
1356 and then Nkind (N) = N_Object_Renaming_Declaration
1357 and then Is_Interface (Unc_Type)
1359 pragma Assert (Is_Class_Wide_Type (Unc_Type));
1362 -- In Ada95 nothing to be done if the type of the expression is limited,
1363 -- because in this case the expression cannot be copied, and its use can
1364 -- only be by reference.
1366 -- In Ada2005, the context can be an object declaration whose expression
1367 -- is a function that returns in place. If the nominal subtype has
1368 -- unknown discriminants, the call still provides constraints on the
1369 -- object, and we have to create an actual subtype from it.
1371 -- If the type is class-wide, the expression is dynamically tagged and
1372 -- we do not create an actual subtype either. Ditto for an interface.
1374 elsif Is_Limited_Type (Exp_Typ)
1376 (Is_Class_Wide_Type (Exp_Typ)
1377 or else Is_Interface (Exp_Typ)
1378 or else not Has_Unknown_Discriminants (Exp_Typ)
1379 or else not Is_Composite_Type (Unc_Type))
1383 -- For limited objects initialized with build in place function calls,
1384 -- nothing to be done; otherwise we prematurely introduce an N_Reference
1385 -- node in the expression initializing the object, which breaks the
1386 -- circuitry that detects and adds the additional arguments to the
1389 elsif Is_Build_In_Place_Function_Call (Exp) then
1393 Remove_Side_Effects (Exp);
1394 Rewrite (Subtype_Indic,
1395 Make_Subtype_From_Expr (Exp, Unc_Type));
1397 end Expand_Subtype_From_Expr;
1399 --------------------
1400 -- Find_Init_Call --
1401 --------------------
1403 function Find_Init_Call
1405 Rep_Clause : Node_Id) return Node_Id
1407 Typ : constant Entity_Id := Etype (Var);
1409 Init_Proc : Entity_Id;
1410 -- Initialization procedure for Typ
1412 function Find_Init_Call_In_List (From : Node_Id) return Node_Id;
1413 -- Look for init call for Var starting at From and scanning the
1414 -- enclosing list until Rep_Clause or the end of the list is reached.
1416 ----------------------------
1417 -- Find_Init_Call_In_List --
1418 ----------------------------
1420 function Find_Init_Call_In_List (From : Node_Id) return Node_Id is
1421 Init_Call : Node_Id;
1425 while Present (Init_Call) and then Init_Call /= Rep_Clause loop
1426 if Nkind (Init_Call) = N_Procedure_Call_Statement
1427 and then Is_Entity_Name (Name (Init_Call))
1428 and then Entity (Name (Init_Call)) = Init_Proc
1436 end Find_Init_Call_In_List;
1438 Init_Call : Node_Id;
1440 -- Start of processing for Find_Init_Call
1443 if not Has_Non_Null_Base_Init_Proc (Typ) then
1444 -- No init proc for the type, so obviously no call to be found
1449 Init_Proc := Base_Init_Proc (Typ);
1451 -- First scan the list containing the declaration of Var
1453 Init_Call := Find_Init_Call_In_List (From => Next (Parent (Var)));
1455 -- If not found, also look on Var's freeze actions list, if any, since
1456 -- the init call may have been moved there (case of an address clause
1457 -- applying to Var).
1459 if No (Init_Call) and then Present (Freeze_Node (Var)) then
1460 Init_Call := Find_Init_Call_In_List
1461 (First (Actions (Freeze_Node (Var))));
1467 ------------------------
1468 -- Find_Interface_ADT --
1469 ------------------------
1471 function Find_Interface_ADT
1473 Iface : Entity_Id) return Elmt_Id
1476 Typ : Entity_Id := T;
1479 pragma Assert (Is_Interface (Iface));
1481 -- Handle private types
1483 if Has_Private_Declaration (Typ)
1484 and then Present (Full_View (Typ))
1486 Typ := Full_View (Typ);
1489 -- Handle access types
1491 if Is_Access_Type (Typ) then
1492 Typ := Designated_Type (Typ);
1495 -- Handle task and protected types implementing interfaces
1497 if Is_Concurrent_Type (Typ) then
1498 Typ := Corresponding_Record_Type (Typ);
1502 (not Is_Class_Wide_Type (Typ)
1503 and then Ekind (Typ) /= E_Incomplete_Type);
1505 if Is_Ancestor (Iface, Typ) then
1506 return First_Elmt (Access_Disp_Table (Typ));
1510 Next_Elmt (Next_Elmt (First_Elmt (Access_Disp_Table (Typ))));
1512 and then Present (Related_Type (Node (ADT)))
1513 and then Related_Type (Node (ADT)) /= Iface
1514 and then not Is_Ancestor (Iface, Related_Type (Node (ADT)))
1519 pragma Assert (Present (Related_Type (Node (ADT))));
1522 end Find_Interface_ADT;
1524 ------------------------
1525 -- Find_Interface_Tag --
1526 ------------------------
1528 function Find_Interface_Tag
1530 Iface : Entity_Id) return Entity_Id
1533 Found : Boolean := False;
1534 Typ : Entity_Id := T;
1536 procedure Find_Tag (Typ : Entity_Id);
1537 -- Internal subprogram used to recursively climb to the ancestors
1543 procedure Find_Tag (Typ : Entity_Id) is
1548 -- This routine does not handle the case in which the interface is an
1549 -- ancestor of Typ. That case is handled by the enclosing subprogram.
1551 pragma Assert (Typ /= Iface);
1553 -- Climb to the root type handling private types
1555 if Present (Full_View (Etype (Typ))) then
1556 if Full_View (Etype (Typ)) /= Typ then
1557 Find_Tag (Full_View (Etype (Typ)));
1560 elsif Etype (Typ) /= Typ then
1561 Find_Tag (Etype (Typ));
1564 -- Traverse the list of interfaces implemented by the type
1567 and then Present (Interfaces (Typ))
1568 and then not (Is_Empty_Elmt_List (Interfaces (Typ)))
1570 -- Skip the tag associated with the primary table
1572 pragma Assert (Etype (First_Tag_Component (Typ)) = RTE (RE_Tag));
1573 AI_Tag := Next_Tag_Component (First_Tag_Component (Typ));
1574 pragma Assert (Present (AI_Tag));
1576 AI_Elmt := First_Elmt (Interfaces (Typ));
1577 while Present (AI_Elmt) loop
1578 AI := Node (AI_Elmt);
1580 if AI = Iface or else Is_Ancestor (Iface, AI) then
1585 AI_Tag := Next_Tag_Component (AI_Tag);
1586 Next_Elmt (AI_Elmt);
1591 -- Start of processing for Find_Interface_Tag
1594 pragma Assert (Is_Interface (Iface));
1596 -- Handle access types
1598 if Is_Access_Type (Typ) then
1599 Typ := Designated_Type (Typ);
1602 -- Handle class-wide types
1604 if Is_Class_Wide_Type (Typ) then
1605 Typ := Root_Type (Typ);
1608 -- Handle private types
1610 if Has_Private_Declaration (Typ)
1611 and then Present (Full_View (Typ))
1613 Typ := Full_View (Typ);
1616 -- Handle entities from the limited view
1618 if Ekind (Typ) = E_Incomplete_Type then
1619 pragma Assert (Present (Non_Limited_View (Typ)));
1620 Typ := Non_Limited_View (Typ);
1623 -- Handle task and protected types implementing interfaces
1625 if Is_Concurrent_Type (Typ) then
1626 Typ := Corresponding_Record_Type (Typ);
1629 -- If the interface is an ancestor of the type, then it shared the
1630 -- primary dispatch table.
1632 if Is_Ancestor (Iface, Typ) then
1633 pragma Assert (Etype (First_Tag_Component (Typ)) = RTE (RE_Tag));
1634 return First_Tag_Component (Typ);
1636 -- Otherwise we need to search for its associated tag component
1640 pragma Assert (Found);
1643 end Find_Interface_Tag;
1649 function Find_Prim_Op (T : Entity_Id; Name : Name_Id) return Entity_Id is
1651 Typ : Entity_Id := T;
1655 if Is_Class_Wide_Type (Typ) then
1656 Typ := Root_Type (Typ);
1659 Typ := Underlying_Type (Typ);
1661 -- Loop through primitive operations
1663 Prim := First_Elmt (Primitive_Operations (Typ));
1664 while Present (Prim) loop
1667 -- We can retrieve primitive operations by name if it is an internal
1668 -- name. For equality we must check that both of its operands have
1669 -- the same type, to avoid confusion with user-defined equalities
1670 -- than may have a non-symmetric signature.
1672 exit when Chars (Op) = Name
1675 or else Etype (First_Formal (Op)) = Etype (Last_Formal (Op)));
1679 -- Raise Program_Error if no primitive found
1682 raise Program_Error;
1693 function Find_Prim_Op
1695 Name : TSS_Name_Type) return Entity_Id
1698 Typ : Entity_Id := T;
1701 if Is_Class_Wide_Type (Typ) then
1702 Typ := Root_Type (Typ);
1705 Typ := Underlying_Type (Typ);
1707 Prim := First_Elmt (Primitive_Operations (Typ));
1708 while not Is_TSS (Node (Prim), Name) loop
1711 -- Raise program error if no primitive found
1714 raise Program_Error;
1721 ----------------------------
1722 -- Find_Protection_Object --
1723 ----------------------------
1725 function Find_Protection_Object (Scop : Entity_Id) return Entity_Id is
1730 while Present (S) loop
1731 if (Ekind (S) = E_Entry
1732 or else Ekind (S) = E_Entry_Family
1733 or else Ekind (S) = E_Function
1734 or else Ekind (S) = E_Procedure)
1735 and then Present (Protection_Object (S))
1737 return Protection_Object (S);
1743 -- If we do not find a Protection object in the scope chain, then
1744 -- something has gone wrong, most likely the object was never created.
1746 raise Program_Error;
1747 end Find_Protection_Object;
1749 ----------------------
1750 -- Force_Evaluation --
1751 ----------------------
1753 procedure Force_Evaluation (Exp : Node_Id; Name_Req : Boolean := False) is
1755 Remove_Side_Effects (Exp, Name_Req, Variable_Ref => True);
1756 end Force_Evaluation;
1758 ---------------------------------
1759 -- Fully_Qualified_Name_String --
1760 ---------------------------------
1762 function Fully_Qualified_Name_String (E : Entity_Id) return String_Id is
1763 procedure Internal_Full_Qualified_Name (E : Entity_Id);
1764 -- Compute recursively the qualified name without NUL at the end, adding
1765 -- it to the currently started string being generated
1767 ----------------------------------
1768 -- Internal_Full_Qualified_Name --
1769 ----------------------------------
1771 procedure Internal_Full_Qualified_Name (E : Entity_Id) is
1775 -- Deal properly with child units
1777 if Nkind (E) = N_Defining_Program_Unit_Name then
1778 Ent := Defining_Identifier (E);
1783 -- Compute qualification recursively (only "Standard" has no scope)
1785 if Present (Scope (Scope (Ent))) then
1786 Internal_Full_Qualified_Name (Scope (Ent));
1787 Store_String_Char (Get_Char_Code ('.'));
1790 -- Every entity should have a name except some expanded blocks
1791 -- don't bother about those.
1793 if Chars (Ent) = No_Name then
1797 -- Generates the entity name in upper case
1799 Get_Decoded_Name_String (Chars (Ent));
1801 Store_String_Chars (Name_Buffer (1 .. Name_Len));
1803 end Internal_Full_Qualified_Name;
1805 -- Start of processing for Full_Qualified_Name
1809 Internal_Full_Qualified_Name (E);
1810 Store_String_Char (Get_Char_Code (ASCII.NUL));
1812 end Fully_Qualified_Name_String;
1814 ------------------------
1815 -- Generate_Poll_Call --
1816 ------------------------
1818 procedure Generate_Poll_Call (N : Node_Id) is
1820 -- No poll call if polling not active
1822 if not Polling_Required then
1825 -- Otherwise generate require poll call
1828 Insert_Before_And_Analyze (N,
1829 Make_Procedure_Call_Statement (Sloc (N),
1830 Name => New_Occurrence_Of (RTE (RE_Poll), Sloc (N))));
1832 end Generate_Poll_Call;
1834 ---------------------------------
1835 -- Get_Current_Value_Condition --
1836 ---------------------------------
1838 -- Note: the implementation of this procedure is very closely tied to the
1839 -- implementation of Set_Current_Value_Condition. In the Get procedure, we
1840 -- interpret Current_Value fields set by the Set procedure, so the two
1841 -- procedures need to be closely coordinated.
1843 procedure Get_Current_Value_Condition
1848 Loc : constant Source_Ptr := Sloc (Var);
1849 Ent : constant Entity_Id := Entity (Var);
1851 procedure Process_Current_Value_Condition
1854 -- N is an expression which holds either True (S = True) or False (S =
1855 -- False) in the condition. This procedure digs out the expression and
1856 -- if it refers to Ent, sets Op and Val appropriately.
1858 -------------------------------------
1859 -- Process_Current_Value_Condition --
1860 -------------------------------------
1862 procedure Process_Current_Value_Condition
1873 -- Deal with NOT operators, inverting sense
1875 while Nkind (Cond) = N_Op_Not loop
1876 Cond := Right_Opnd (Cond);
1880 -- Deal with AND THEN and AND cases
1882 if Nkind (Cond) = N_And_Then
1883 or else Nkind (Cond) = N_Op_And
1885 -- Don't ever try to invert a condition that is of the form of an
1886 -- AND or AND THEN (since we are not doing sufficiently general
1887 -- processing to allow this).
1889 if Sens = False then
1895 -- Recursively process AND and AND THEN branches
1897 Process_Current_Value_Condition (Left_Opnd (Cond), True);
1899 if Op /= N_Empty then
1903 Process_Current_Value_Condition (Right_Opnd (Cond), True);
1906 -- Case of relational operator
1908 elsif Nkind (Cond) in N_Op_Compare then
1911 -- Invert sense of test if inverted test
1913 if Sens = False then
1915 when N_Op_Eq => Op := N_Op_Ne;
1916 when N_Op_Ne => Op := N_Op_Eq;
1917 when N_Op_Lt => Op := N_Op_Ge;
1918 when N_Op_Gt => Op := N_Op_Le;
1919 when N_Op_Le => Op := N_Op_Gt;
1920 when N_Op_Ge => Op := N_Op_Lt;
1921 when others => raise Program_Error;
1925 -- Case of entity op value
1927 if Is_Entity_Name (Left_Opnd (Cond))
1928 and then Ent = Entity (Left_Opnd (Cond))
1929 and then Compile_Time_Known_Value (Right_Opnd (Cond))
1931 Val := Right_Opnd (Cond);
1933 -- Case of value op entity
1935 elsif Is_Entity_Name (Right_Opnd (Cond))
1936 and then Ent = Entity (Right_Opnd (Cond))
1937 and then Compile_Time_Known_Value (Left_Opnd (Cond))
1939 Val := Left_Opnd (Cond);
1941 -- We are effectively swapping operands
1944 when N_Op_Eq => null;
1945 when N_Op_Ne => null;
1946 when N_Op_Lt => Op := N_Op_Gt;
1947 when N_Op_Gt => Op := N_Op_Lt;
1948 when N_Op_Le => Op := N_Op_Ge;
1949 when N_Op_Ge => Op := N_Op_Le;
1950 when others => raise Program_Error;
1959 -- Case of Boolean variable reference, return as though the
1960 -- reference had said var = True.
1963 if Is_Entity_Name (Cond)
1964 and then Ent = Entity (Cond)
1966 Val := New_Occurrence_Of (Standard_True, Sloc (Cond));
1968 if Sens = False then
1975 end Process_Current_Value_Condition;
1977 -- Start of processing for Get_Current_Value_Condition
1983 -- Immediate return, nothing doing, if this is not an object
1985 if Ekind (Ent) not in Object_Kind then
1989 -- Otherwise examine current value
1992 CV : constant Node_Id := Current_Value (Ent);
1997 -- If statement. Condition is known true in THEN section, known False
1998 -- in any ELSIF or ELSE part, and unknown outside the IF statement.
2000 if Nkind (CV) = N_If_Statement then
2002 -- Before start of IF statement
2004 if Loc < Sloc (CV) then
2007 -- After end of IF statement
2009 elsif Loc >= Sloc (CV) + Text_Ptr (UI_To_Int (End_Span (CV))) then
2013 -- At this stage we know that we are within the IF statement, but
2014 -- unfortunately, the tree does not record the SLOC of the ELSE so
2015 -- we cannot use a simple SLOC comparison to distinguish between
2016 -- the then/else statements, so we have to climb the tree.
2023 while Parent (N) /= CV loop
2026 -- If we fall off the top of the tree, then that's odd, but
2027 -- perhaps it could occur in some error situation, and the
2028 -- safest response is simply to assume that the outcome of
2029 -- the condition is unknown. No point in bombing during an
2030 -- attempt to optimize things.
2037 -- Now we have N pointing to a node whose parent is the IF
2038 -- statement in question, so now we can tell if we are within
2039 -- the THEN statements.
2041 if Is_List_Member (N)
2042 and then List_Containing (N) = Then_Statements (CV)
2046 -- If the variable reference does not come from source, we
2047 -- cannot reliably tell whether it appears in the else part.
2048 -- In particular, if it appears in generated code for a node
2049 -- that requires finalization, it may be attached to a list
2050 -- that has not been yet inserted into the code. For now,
2051 -- treat it as unknown.
2053 elsif not Comes_From_Source (N) then
2056 -- Otherwise we must be in ELSIF or ELSE part
2063 -- ELSIF part. Condition is known true within the referenced
2064 -- ELSIF, known False in any subsequent ELSIF or ELSE part,
2065 -- and unknown before the ELSE part or after the IF statement.
2067 elsif Nkind (CV) = N_Elsif_Part then
2069 -- if the Elsif_Part had condition_actions, the elsif has been
2070 -- rewritten as a nested if, and the original elsif_part is
2071 -- detached from the tree, so there is no way to obtain useful
2072 -- information on the current value of the variable.
2073 -- Can this be improved ???
2075 if No (Parent (CV)) then
2081 -- Before start of ELSIF part
2083 if Loc < Sloc (CV) then
2086 -- After end of IF statement
2088 elsif Loc >= Sloc (Stm) +
2089 Text_Ptr (UI_To_Int (End_Span (Stm)))
2094 -- Again we lack the SLOC of the ELSE, so we need to climb the
2095 -- tree to see if we are within the ELSIF part in question.
2102 while Parent (N) /= Stm loop
2105 -- If we fall off the top of the tree, then that's odd, but
2106 -- perhaps it could occur in some error situation, and the
2107 -- safest response is simply to assume that the outcome of
2108 -- the condition is unknown. No point in bombing during an
2109 -- attempt to optimize things.
2116 -- Now we have N pointing to a node whose parent is the IF
2117 -- statement in question, so see if is the ELSIF part we want.
2118 -- the THEN statements.
2123 -- Otherwise we must be in subsequent ELSIF or ELSE part
2130 -- Iteration scheme of while loop. The condition is known to be
2131 -- true within the body of the loop.
2133 elsif Nkind (CV) = N_Iteration_Scheme then
2135 Loop_Stmt : constant Node_Id := Parent (CV);
2138 -- Before start of body of loop
2140 if Loc < Sloc (Loop_Stmt) then
2143 -- After end of LOOP statement
2145 elsif Loc >= Sloc (End_Label (Loop_Stmt)) then
2148 -- We are within the body of the loop
2155 -- All other cases of Current_Value settings
2161 -- If we fall through here, then we have a reportable condition, Sens
2162 -- is True if the condition is true and False if it needs inverting.
2164 Process_Current_Value_Condition (Condition (CV), Sens);
2166 end Get_Current_Value_Condition;
2168 ---------------------------------
2169 -- Has_Controlled_Coextensions --
2170 ---------------------------------
2172 function Has_Controlled_Coextensions (Typ : Entity_Id) return Boolean is
2177 -- Only consider record types
2179 if not Ekind_In (Typ, E_Record_Type, E_Record_Subtype) then
2183 if Has_Discriminants (Typ) then
2184 Discr := First_Discriminant (Typ);
2185 while Present (Discr) loop
2186 D_Typ := Etype (Discr);
2188 if Ekind (D_Typ) = E_Anonymous_Access_Type
2190 (Is_Controlled (Designated_Type (D_Typ))
2192 Is_Concurrent_Type (Designated_Type (D_Typ)))
2197 Next_Discriminant (Discr);
2202 end Has_Controlled_Coextensions;
2204 ------------------------
2205 -- Has_Address_Clause --
2206 ------------------------
2208 -- Should this function check the private part in a package ???
2210 function Has_Following_Address_Clause (D : Node_Id) return Boolean is
2211 Id : constant Entity_Id := Defining_Identifier (D);
2216 while Present (Decl) loop
2217 if Nkind (Decl) = N_At_Clause
2218 and then Chars (Identifier (Decl)) = Chars (Id)
2222 elsif Nkind (Decl) = N_Attribute_Definition_Clause
2223 and then Chars (Decl) = Name_Address
2224 and then Chars (Name (Decl)) = Chars (Id)
2233 end Has_Following_Address_Clause;
2235 --------------------
2236 -- Homonym_Number --
2237 --------------------
2239 function Homonym_Number (Subp : Entity_Id) return Nat is
2245 Hom := Homonym (Subp);
2246 while Present (Hom) loop
2247 if Scope (Hom) = Scope (Subp) then
2251 Hom := Homonym (Hom);
2257 ------------------------------
2258 -- In_Unconditional_Context --
2259 ------------------------------
2261 function In_Unconditional_Context (Node : Node_Id) return Boolean is
2266 while Present (P) loop
2268 when N_Subprogram_Body =>
2271 when N_If_Statement =>
2274 when N_Loop_Statement =>
2277 when N_Case_Statement =>
2286 end In_Unconditional_Context;
2292 procedure Insert_Action (Assoc_Node : Node_Id; Ins_Action : Node_Id) is
2294 if Present (Ins_Action) then
2295 Insert_Actions (Assoc_Node, New_List (Ins_Action));
2299 -- Version with check(s) suppressed
2301 procedure Insert_Action
2302 (Assoc_Node : Node_Id; Ins_Action : Node_Id; Suppress : Check_Id)
2305 Insert_Actions (Assoc_Node, New_List (Ins_Action), Suppress);
2308 --------------------
2309 -- Insert_Actions --
2310 --------------------
2312 procedure Insert_Actions (Assoc_Node : Node_Id; Ins_Actions : List_Id) is
2316 Wrapped_Node : Node_Id := Empty;
2319 if No (Ins_Actions) or else Is_Empty_List (Ins_Actions) then
2323 -- Ignore insert of actions from inside default expression (or other
2324 -- similar "spec expression") in the special spec-expression analyze
2325 -- mode. Any insertions at this point have no relevance, since we are
2326 -- only doing the analyze to freeze the types of any static expressions.
2327 -- See section "Handling of Default Expressions" in the spec of package
2328 -- Sem for further details.
2330 if In_Spec_Expression then
2334 -- If the action derives from stuff inside a record, then the actions
2335 -- are attached to the current scope, to be inserted and analyzed on
2336 -- exit from the scope. The reason for this is that we may also
2337 -- be generating freeze actions at the same time, and they must
2338 -- eventually be elaborated in the correct order.
2340 if Is_Record_Type (Current_Scope)
2341 and then not Is_Frozen (Current_Scope)
2343 if No (Scope_Stack.Table
2344 (Scope_Stack.Last).Pending_Freeze_Actions)
2346 Scope_Stack.Table (Scope_Stack.Last).Pending_Freeze_Actions :=
2351 Scope_Stack.Table (Scope_Stack.Last).Pending_Freeze_Actions);
2357 -- We now intend to climb up the tree to find the right point to
2358 -- insert the actions. We start at Assoc_Node, unless this node is
2359 -- a subexpression in which case we start with its parent. We do this
2360 -- for two reasons. First it speeds things up. Second, if Assoc_Node
2361 -- is itself one of the special nodes like N_And_Then, then we assume
2362 -- that an initial request to insert actions for such a node does not
2363 -- expect the actions to get deposited in the node for later handling
2364 -- when the node is expanded, since clearly the node is being dealt
2365 -- with by the caller. Note that in the subexpression case, N is
2366 -- always the child we came from.
2368 -- N_Raise_xxx_Error is an annoying special case, it is a statement
2369 -- if it has type Standard_Void_Type, and a subexpression otherwise.
2370 -- otherwise. Procedure attribute references are also statements.
2372 if Nkind (Assoc_Node) in N_Subexpr
2373 and then (Nkind (Assoc_Node) in N_Raise_xxx_Error
2374 or else Etype (Assoc_Node) /= Standard_Void_Type)
2375 and then (Nkind (Assoc_Node) /= N_Attribute_Reference
2377 not Is_Procedure_Attribute_Name
2378 (Attribute_Name (Assoc_Node)))
2380 P := Assoc_Node; -- ??? does not agree with above!
2381 N := Parent (Assoc_Node);
2383 -- Non-subexpression case. Note that N is initially Empty in this
2384 -- case (N is only guaranteed Non-Empty in the subexpr case).
2391 -- Capture root of the transient scope
2393 if Scope_Is_Transient then
2394 Wrapped_Node := Node_To_Be_Wrapped;
2398 pragma Assert (Present (P));
2402 -- Case of right operand of AND THEN or OR ELSE. Put the actions
2403 -- in the Actions field of the right operand. They will be moved
2404 -- out further when the AND THEN or OR ELSE operator is expanded.
2405 -- Nothing special needs to be done for the left operand since
2406 -- in that case the actions are executed unconditionally.
2408 when N_Short_Circuit =>
2409 if N = Right_Opnd (P) then
2411 -- We are now going to either append the actions to the
2412 -- actions field of the short-circuit operation. We will
2413 -- also analyze the actions now.
2415 -- This analysis is really too early, the proper thing would
2416 -- be to just park them there now, and only analyze them if
2417 -- we find we really need them, and to it at the proper
2418 -- final insertion point. However attempting to this proved
2419 -- tricky, so for now we just kill current values before and
2420 -- after the analyze call to make sure we avoid peculiar
2421 -- optimizations from this out of order insertion.
2423 Kill_Current_Values;
2425 if Present (Actions (P)) then
2426 Insert_List_After_And_Analyze
2427 (Last (Actions (P)), Ins_Actions);
2429 Set_Actions (P, Ins_Actions);
2430 Analyze_List (Actions (P));
2433 Kill_Current_Values;
2438 -- Then or Else operand of conditional expression. Add actions to
2439 -- Then_Actions or Else_Actions field as appropriate. The actions
2440 -- will be moved further out when the conditional is expanded.
2442 when N_Conditional_Expression =>
2444 ThenX : constant Node_Id := Next (First (Expressions (P)));
2445 ElseX : constant Node_Id := Next (ThenX);
2448 -- If the enclosing expression is already analyzed, as
2449 -- is the case for nested elaboration checks, insert the
2450 -- conditional further out.
2452 if Analyzed (P) then
2455 -- Actions belong to the then expression, temporarily place
2456 -- them as Then_Actions of the conditional expr. They will
2457 -- be moved to the proper place later when the conditional
2458 -- expression is expanded.
2460 elsif N = ThenX then
2461 if Present (Then_Actions (P)) then
2462 Insert_List_After_And_Analyze
2463 (Last (Then_Actions (P)), Ins_Actions);
2465 Set_Then_Actions (P, Ins_Actions);
2466 Analyze_List (Then_Actions (P));
2471 -- Actions belong to the else expression, temporarily
2472 -- place them as Else_Actions of the conditional expr.
2473 -- They will be moved to the proper place later when
2474 -- the conditional expression is expanded.
2476 elsif N = ElseX then
2477 if Present (Else_Actions (P)) then
2478 Insert_List_After_And_Analyze
2479 (Last (Else_Actions (P)), Ins_Actions);
2481 Set_Else_Actions (P, Ins_Actions);
2482 Analyze_List (Else_Actions (P));
2487 -- Actions belong to the condition. In this case they are
2488 -- unconditionally executed, and so we can continue the
2489 -- search for the proper insert point.
2496 -- Alternative of case expression, we place the action in the
2497 -- Actions field of the case expression alternative, this will
2498 -- be handled when the case expression is expanded.
2500 when N_Case_Expression_Alternative =>
2501 if Present (Actions (P)) then
2502 Insert_List_After_And_Analyze
2503 (Last (Actions (P)), Ins_Actions);
2505 Set_Actions (P, Ins_Actions);
2506 Analyze_List (Then_Actions (P));
2511 -- Case of appearing within an Expressions_With_Actions node. We
2512 -- prepend the actions to the list of actions already there, if
2513 -- the node has not been analyzed yet. Otherwise find insertion
2514 -- location further up the tree.
2516 when N_Expression_With_Actions =>
2517 if not Analyzed (P) then
2518 Prepend_List (Ins_Actions, Actions (P));
2522 -- Case of appearing in the condition of a while expression or
2523 -- elsif. We insert the actions into the Condition_Actions field.
2524 -- They will be moved further out when the while loop or elsif
2527 when N_Iteration_Scheme |
2530 if N = Condition (P) then
2531 if Present (Condition_Actions (P)) then
2532 Insert_List_After_And_Analyze
2533 (Last (Condition_Actions (P)), Ins_Actions);
2535 Set_Condition_Actions (P, Ins_Actions);
2537 -- Set the parent of the insert actions explicitly. This
2538 -- is not a syntactic field, but we need the parent field
2539 -- set, in particular so that freeze can understand that
2540 -- it is dealing with condition actions, and properly
2541 -- insert the freezing actions.
2543 Set_Parent (Ins_Actions, P);
2544 Analyze_List (Condition_Actions (P));
2550 -- Statements, declarations, pragmas, representation clauses
2555 N_Procedure_Call_Statement |
2556 N_Statement_Other_Than_Procedure_Call |
2562 -- Representation_Clause
2565 N_Attribute_Definition_Clause |
2566 N_Enumeration_Representation_Clause |
2567 N_Record_Representation_Clause |
2571 N_Abstract_Subprogram_Declaration |
2573 N_Exception_Declaration |
2574 N_Exception_Renaming_Declaration |
2575 N_Formal_Abstract_Subprogram_Declaration |
2576 N_Formal_Concrete_Subprogram_Declaration |
2577 N_Formal_Object_Declaration |
2578 N_Formal_Type_Declaration |
2579 N_Full_Type_Declaration |
2580 N_Function_Instantiation |
2581 N_Generic_Function_Renaming_Declaration |
2582 N_Generic_Package_Declaration |
2583 N_Generic_Package_Renaming_Declaration |
2584 N_Generic_Procedure_Renaming_Declaration |
2585 N_Generic_Subprogram_Declaration |
2586 N_Implicit_Label_Declaration |
2587 N_Incomplete_Type_Declaration |
2588 N_Number_Declaration |
2589 N_Object_Declaration |
2590 N_Object_Renaming_Declaration |
2592 N_Package_Body_Stub |
2593 N_Package_Declaration |
2594 N_Package_Instantiation |
2595 N_Package_Renaming_Declaration |
2596 N_Parameterized_Expression |
2597 N_Private_Extension_Declaration |
2598 N_Private_Type_Declaration |
2599 N_Procedure_Instantiation |
2601 N_Protected_Body_Stub |
2602 N_Protected_Type_Declaration |
2603 N_Single_Task_Declaration |
2605 N_Subprogram_Body_Stub |
2606 N_Subprogram_Declaration |
2607 N_Subprogram_Renaming_Declaration |
2608 N_Subtype_Declaration |
2611 N_Task_Type_Declaration |
2613 -- Freeze entity behaves like a declaration or statement
2617 -- Do not insert here if the item is not a list member (this
2618 -- happens for example with a triggering statement, and the
2619 -- proper approach is to insert before the entire select).
2621 if not Is_List_Member (P) then
2624 -- Do not insert if parent of P is an N_Component_Association
2625 -- node (i.e. we are in the context of an N_Aggregate or
2626 -- N_Extension_Aggregate node. In this case we want to insert
2627 -- before the entire aggregate.
2629 elsif Nkind (Parent (P)) = N_Component_Association then
2632 -- Do not insert if the parent of P is either an N_Variant
2633 -- node or an N_Record_Definition node, meaning in either
2634 -- case that P is a member of a component list, and that
2635 -- therefore the actions should be inserted outside the
2636 -- complete record declaration.
2638 elsif Nkind (Parent (P)) = N_Variant
2639 or else Nkind (Parent (P)) = N_Record_Definition
2643 -- Do not insert freeze nodes within the loop generated for
2644 -- an aggregate, because they may be elaborated too late for
2645 -- subsequent use in the back end: within a package spec the
2646 -- loop is part of the elaboration procedure and is only
2647 -- elaborated during the second pass.
2649 -- If the loop comes from source, or the entity is local to
2650 -- the loop itself it must remain within.
2652 elsif Nkind (Parent (P)) = N_Loop_Statement
2653 and then not Comes_From_Source (Parent (P))
2654 and then Nkind (First (Ins_Actions)) = N_Freeze_Entity
2656 Scope (Entity (First (Ins_Actions))) /= Current_Scope
2660 -- Otherwise we can go ahead and do the insertion
2662 elsif P = Wrapped_Node then
2663 Store_Before_Actions_In_Scope (Ins_Actions);
2667 Insert_List_Before_And_Analyze (P, Ins_Actions);
2671 -- A special case, N_Raise_xxx_Error can act either as a statement
2672 -- or a subexpression. We tell the difference by looking at the
2673 -- Etype. It is set to Standard_Void_Type in the statement case.
2676 N_Raise_xxx_Error =>
2677 if Etype (P) = Standard_Void_Type then
2678 if P = Wrapped_Node then
2679 Store_Before_Actions_In_Scope (Ins_Actions);
2681 Insert_List_Before_And_Analyze (P, Ins_Actions);
2686 -- In the subexpression case, keep climbing
2692 -- If a component association appears within a loop created for
2693 -- an array aggregate, attach the actions to the association so
2694 -- they can be subsequently inserted within the loop. For other
2695 -- component associations insert outside of the aggregate. For
2696 -- an association that will generate a loop, its Loop_Actions
2697 -- attribute is already initialized (see exp_aggr.adb).
2699 -- The list of loop_actions can in turn generate additional ones,
2700 -- that are inserted before the associated node. If the associated
2701 -- node is outside the aggregate, the new actions are collected
2702 -- at the end of the loop actions, to respect the order in which
2703 -- they are to be elaborated.
2706 N_Component_Association =>
2707 if Nkind (Parent (P)) = N_Aggregate
2708 and then Present (Loop_Actions (P))
2710 if Is_Empty_List (Loop_Actions (P)) then
2711 Set_Loop_Actions (P, Ins_Actions);
2712 Analyze_List (Ins_Actions);
2719 -- Check whether these actions were generated by a
2720 -- declaration that is part of the loop_ actions
2721 -- for the component_association.
2724 while Present (Decl) loop
2725 exit when Parent (Decl) = P
2726 and then Is_List_Member (Decl)
2728 List_Containing (Decl) = Loop_Actions (P);
2729 Decl := Parent (Decl);
2732 if Present (Decl) then
2733 Insert_List_Before_And_Analyze
2734 (Decl, Ins_Actions);
2736 Insert_List_After_And_Analyze
2737 (Last (Loop_Actions (P)), Ins_Actions);
2748 -- Another special case, an attribute denoting a procedure call
2751 N_Attribute_Reference =>
2752 if Is_Procedure_Attribute_Name (Attribute_Name (P)) then
2753 if P = Wrapped_Node then
2754 Store_Before_Actions_In_Scope (Ins_Actions);
2756 Insert_List_Before_And_Analyze (P, Ins_Actions);
2761 -- In the subexpression case, keep climbing
2767 -- For all other node types, keep climbing tree
2771 N_Accept_Alternative |
2772 N_Access_Definition |
2773 N_Access_Function_Definition |
2774 N_Access_Procedure_Definition |
2775 N_Access_To_Object_Definition |
2778 N_Aspect_Specification |
2780 N_Case_Statement_Alternative |
2781 N_Character_Literal |
2782 N_Compilation_Unit |
2783 N_Compilation_Unit_Aux |
2784 N_Component_Clause |
2785 N_Component_Declaration |
2786 N_Component_Definition |
2788 N_Constrained_Array_Definition |
2789 N_Decimal_Fixed_Point_Definition |
2790 N_Defining_Character_Literal |
2791 N_Defining_Identifier |
2792 N_Defining_Operator_Symbol |
2793 N_Defining_Program_Unit_Name |
2794 N_Delay_Alternative |
2795 N_Delta_Constraint |
2796 N_Derived_Type_Definition |
2798 N_Digits_Constraint |
2799 N_Discriminant_Association |
2800 N_Discriminant_Specification |
2802 N_Entry_Body_Formal_Part |
2803 N_Entry_Call_Alternative |
2804 N_Entry_Declaration |
2805 N_Entry_Index_Specification |
2806 N_Enumeration_Type_Definition |
2808 N_Exception_Handler |
2810 N_Explicit_Dereference |
2811 N_Extension_Aggregate |
2812 N_Floating_Point_Definition |
2813 N_Formal_Decimal_Fixed_Point_Definition |
2814 N_Formal_Derived_Type_Definition |
2815 N_Formal_Discrete_Type_Definition |
2816 N_Formal_Floating_Point_Definition |
2817 N_Formal_Modular_Type_Definition |
2818 N_Formal_Ordinary_Fixed_Point_Definition |
2819 N_Formal_Package_Declaration |
2820 N_Formal_Private_Type_Definition |
2821 N_Formal_Signed_Integer_Type_Definition |
2823 N_Function_Specification |
2824 N_Generic_Association |
2825 N_Handled_Sequence_Of_Statements |
2828 N_Index_Or_Discriminant_Constraint |
2829 N_Indexed_Component |
2831 N_Iterator_Specification |
2834 N_Loop_Parameter_Specification |
2836 N_Modular_Type_Definition |
2862 N_Op_Shift_Right_Arithmetic |
2866 N_Ordinary_Fixed_Point_Definition |
2868 N_Package_Specification |
2869 N_Parameter_Association |
2870 N_Parameter_Specification |
2871 N_Pop_Constraint_Error_Label |
2872 N_Pop_Program_Error_Label |
2873 N_Pop_Storage_Error_Label |
2874 N_Pragma_Argument_Association |
2875 N_Procedure_Specification |
2876 N_Protected_Definition |
2877 N_Push_Constraint_Error_Label |
2878 N_Push_Program_Error_Label |
2879 N_Push_Storage_Error_Label |
2880 N_Qualified_Expression |
2881 N_Quantified_Expression |
2883 N_Range_Constraint |
2885 N_Real_Range_Specification |
2886 N_Record_Definition |
2888 N_SCIL_Dispatch_Table_Tag_Init |
2889 N_SCIL_Dispatching_Call |
2890 N_SCIL_Membership_Test |
2891 N_Selected_Component |
2892 N_Signed_Integer_Type_Definition |
2893 N_Single_Protected_Declaration |
2897 N_Subtype_Indication |
2900 N_Terminate_Alternative |
2901 N_Triggering_Alternative |
2903 N_Unchecked_Expression |
2904 N_Unchecked_Type_Conversion |
2905 N_Unconstrained_Array_Definition |
2908 N_Use_Package_Clause |
2912 N_Validate_Unchecked_Conversion |
2919 -- Make sure that inserted actions stay in the transient scope
2921 if P = Wrapped_Node then
2922 Store_Before_Actions_In_Scope (Ins_Actions);
2926 -- If we fall through above tests, keep climbing tree
2930 if Nkind (Parent (N)) = N_Subunit then
2932 -- This is the proper body corresponding to a stub. Insertion must
2933 -- be done at the point of the stub, which is in the declarative
2934 -- part of the parent unit.
2936 P := Corresponding_Stub (Parent (N));
2944 -- Version with check(s) suppressed
2946 procedure Insert_Actions
2947 (Assoc_Node : Node_Id;
2948 Ins_Actions : List_Id;
2949 Suppress : Check_Id)
2952 if Suppress = All_Checks then
2954 Svg : constant Suppress_Array := Scope_Suppress;
2956 Scope_Suppress := (others => True);
2957 Insert_Actions (Assoc_Node, Ins_Actions);
2958 Scope_Suppress := Svg;
2963 Svg : constant Boolean := Scope_Suppress (Suppress);
2965 Scope_Suppress (Suppress) := True;
2966 Insert_Actions (Assoc_Node, Ins_Actions);
2967 Scope_Suppress (Suppress) := Svg;
2972 --------------------------
2973 -- Insert_Actions_After --
2974 --------------------------
2976 procedure Insert_Actions_After
2977 (Assoc_Node : Node_Id;
2978 Ins_Actions : List_Id)
2981 if Scope_Is_Transient
2982 and then Assoc_Node = Node_To_Be_Wrapped
2984 Store_After_Actions_In_Scope (Ins_Actions);
2986 Insert_List_After_And_Analyze (Assoc_Node, Ins_Actions);
2988 end Insert_Actions_After;
2990 ---------------------------------
2991 -- Insert_Library_Level_Action --
2992 ---------------------------------
2994 procedure Insert_Library_Level_Action (N : Node_Id) is
2995 Aux : constant Node_Id := Aux_Decls_Node (Cunit (Main_Unit));
2998 Push_Scope (Cunit_Entity (Main_Unit));
2999 -- ??? should this be Current_Sem_Unit instead of Main_Unit?
3001 if No (Actions (Aux)) then
3002 Set_Actions (Aux, New_List (N));
3004 Append (N, Actions (Aux));
3009 end Insert_Library_Level_Action;
3011 ----------------------------------
3012 -- Insert_Library_Level_Actions --
3013 ----------------------------------
3015 procedure Insert_Library_Level_Actions (L : List_Id) is
3016 Aux : constant Node_Id := Aux_Decls_Node (Cunit (Main_Unit));
3019 if Is_Non_Empty_List (L) then
3020 Push_Scope (Cunit_Entity (Main_Unit));
3021 -- ??? should this be Current_Sem_Unit instead of Main_Unit?
3023 if No (Actions (Aux)) then
3024 Set_Actions (Aux, L);
3027 Insert_List_After_And_Analyze (Last (Actions (Aux)), L);
3032 end Insert_Library_Level_Actions;
3034 ----------------------
3035 -- Inside_Init_Proc --
3036 ----------------------
3038 function Inside_Init_Proc return Boolean is
3044 and then S /= Standard_Standard
3046 if Is_Init_Proc (S) then
3054 end Inside_Init_Proc;
3056 ----------------------------
3057 -- Is_All_Null_Statements --
3058 ----------------------------
3060 function Is_All_Null_Statements (L : List_Id) return Boolean is
3065 while Present (Stm) loop
3066 if Nkind (Stm) /= N_Null_Statement then
3074 end Is_All_Null_Statements;
3076 ---------------------------------
3077 -- Is_Fully_Repped_Tagged_Type --
3078 ---------------------------------
3080 function Is_Fully_Repped_Tagged_Type (T : Entity_Id) return Boolean is
3081 U : constant Entity_Id := Underlying_Type (T);
3085 if No (U) or else not Is_Tagged_Type (U) then
3087 elsif Has_Discriminants (U) then
3089 elsif not Has_Specified_Layout (U) then
3093 -- Here we have a tagged type, see if it has any unlayed out fields
3094 -- other than a possible tag and parent fields. If so, we return False.
3096 Comp := First_Component (U);
3097 while Present (Comp) loop
3098 if not Is_Tag (Comp)
3099 and then Chars (Comp) /= Name_uParent
3100 and then No (Component_Clause (Comp))
3104 Next_Component (Comp);
3108 -- All components are layed out
3111 end Is_Fully_Repped_Tagged_Type;
3113 ----------------------------------
3114 -- Is_Library_Level_Tagged_Type --
3115 ----------------------------------
3117 function Is_Library_Level_Tagged_Type (Typ : Entity_Id) return Boolean is
3119 return Is_Tagged_Type (Typ)
3120 and then Is_Library_Level_Entity (Typ);
3121 end Is_Library_Level_Tagged_Type;
3123 ----------------------------------
3124 -- Is_Possibly_Unaligned_Object --
3125 ----------------------------------
3127 function Is_Possibly_Unaligned_Object (N : Node_Id) return Boolean is
3128 T : constant Entity_Id := Etype (N);
3131 -- If renamed object, apply test to underlying object
3133 if Is_Entity_Name (N)
3134 and then Is_Object (Entity (N))
3135 and then Present (Renamed_Object (Entity (N)))
3137 return Is_Possibly_Unaligned_Object (Renamed_Object (Entity (N)));
3140 -- Tagged and controlled types and aliased types are always aligned,
3141 -- as are concurrent types.
3144 or else Has_Controlled_Component (T)
3145 or else Is_Concurrent_Type (T)
3146 or else Is_Tagged_Type (T)
3147 or else Is_Controlled (T)
3152 -- If this is an element of a packed array, may be unaligned
3154 if Is_Ref_To_Bit_Packed_Array (N) then
3158 -- Case of component reference
3160 if Nkind (N) = N_Selected_Component then
3162 P : constant Node_Id := Prefix (N);
3163 C : constant Entity_Id := Entity (Selector_Name (N));
3168 -- If component reference is for an array with non-static bounds,
3169 -- then it is always aligned: we can only process unaligned
3170 -- arrays with static bounds (more accurately bounds known at
3173 if Is_Array_Type (T)
3174 and then not Compile_Time_Known_Bounds (T)
3179 -- If component is aliased, it is definitely properly aligned
3181 if Is_Aliased (C) then
3185 -- If component is for a type implemented as a scalar, and the
3186 -- record is packed, and the component is other than the first
3187 -- component of the record, then the component may be unaligned.
3189 if Is_Packed (Etype (P))
3190 and then Represented_As_Scalar (Etype (C))
3191 and then First_Entity (Scope (C)) /= C
3196 -- Compute maximum possible alignment for T
3198 -- If alignment is known, then that settles things
3200 if Known_Alignment (T) then
3201 M := UI_To_Int (Alignment (T));
3203 -- If alignment is not known, tentatively set max alignment
3206 M := Ttypes.Maximum_Alignment;
3208 -- We can reduce this if the Esize is known since the default
3209 -- alignment will never be more than the smallest power of 2
3210 -- that does not exceed this Esize value.
3212 if Known_Esize (T) then
3213 S := UI_To_Int (Esize (T));
3215 while (M / 2) >= S loop
3221 -- The following code is historical, it used to be present but it
3222 -- is too cautious, because the front-end does not know the proper
3223 -- default alignments for the target. Also, if the alignment is
3224 -- not known, the front end can't know in any case! If a copy is
3225 -- needed, the back-end will take care of it. This whole section
3226 -- including this comment can be removed later ???
3228 -- If the component reference is for a record that has a specified
3229 -- alignment, and we either know it is too small, or cannot tell,
3230 -- then the component may be unaligned.
3232 -- if Known_Alignment (Etype (P))
3233 -- and then Alignment (Etype (P)) < Ttypes.Maximum_Alignment
3234 -- and then M > Alignment (Etype (P))
3239 -- Case of component clause present which may specify an
3240 -- unaligned position.
3242 if Present (Component_Clause (C)) then
3244 -- Otherwise we can do a test to make sure that the actual
3245 -- start position in the record, and the length, are both
3246 -- consistent with the required alignment. If not, we know
3247 -- that we are unaligned.
3250 Align_In_Bits : constant Nat := M * System_Storage_Unit;
3252 if Component_Bit_Offset (C) mod Align_In_Bits /= 0
3253 or else Esize (C) mod Align_In_Bits /= 0
3260 -- Otherwise, for a component reference, test prefix
3262 return Is_Possibly_Unaligned_Object (P);
3265 -- If not a component reference, must be aligned
3270 end Is_Possibly_Unaligned_Object;
3272 ---------------------------------
3273 -- Is_Possibly_Unaligned_Slice --
3274 ---------------------------------
3276 function Is_Possibly_Unaligned_Slice (N : Node_Id) return Boolean is
3278 -- Go to renamed object
3280 if Is_Entity_Name (N)
3281 and then Is_Object (Entity (N))
3282 and then Present (Renamed_Object (Entity (N)))
3284 return Is_Possibly_Unaligned_Slice (Renamed_Object (Entity (N)));
3287 -- The reference must be a slice
3289 if Nkind (N) /= N_Slice then
3293 -- Always assume the worst for a nested record component with a
3294 -- component clause, which gigi/gcc does not appear to handle well.
3295 -- It is not clear why this special test is needed at all ???
3297 if Nkind (Prefix (N)) = N_Selected_Component
3298 and then Nkind (Prefix (Prefix (N))) = N_Selected_Component
3300 Present (Component_Clause (Entity (Selector_Name (Prefix (N)))))
3305 -- We only need to worry if the target has strict alignment
3307 if not Target_Strict_Alignment then
3311 -- If it is a slice, then look at the array type being sliced
3314 Sarr : constant Node_Id := Prefix (N);
3315 -- Prefix of the slice, i.e. the array being sliced
3317 Styp : constant Entity_Id := Etype (Prefix (N));
3318 -- Type of the array being sliced
3324 -- The problems arise if the array object that is being sliced
3325 -- is a component of a record or array, and we cannot guarantee
3326 -- the alignment of the array within its containing object.
3328 -- To investigate this, we look at successive prefixes to see
3329 -- if we have a worrisome indexed or selected component.
3333 -- Case of array is part of an indexed component reference
3335 if Nkind (Pref) = N_Indexed_Component then
3336 Ptyp := Etype (Prefix (Pref));
3338 -- The only problematic case is when the array is packed,
3339 -- in which case we really know nothing about the alignment
3340 -- of individual components.
3342 if Is_Bit_Packed_Array (Ptyp) then
3346 -- Case of array is part of a selected component reference
3348 elsif Nkind (Pref) = N_Selected_Component then
3349 Ptyp := Etype (Prefix (Pref));
3351 -- We are definitely in trouble if the record in question
3352 -- has an alignment, and either we know this alignment is
3353 -- inconsistent with the alignment of the slice, or we
3354 -- don't know what the alignment of the slice should be.
3356 if Known_Alignment (Ptyp)
3357 and then (Unknown_Alignment (Styp)
3358 or else Alignment (Styp) > Alignment (Ptyp))
3363 -- We are in potential trouble if the record type is packed.
3364 -- We could special case when we know that the array is the
3365 -- first component, but that's not such a simple case ???
3367 if Is_Packed (Ptyp) then
3371 -- We are in trouble if there is a component clause, and
3372 -- either we do not know the alignment of the slice, or
3373 -- the alignment of the slice is inconsistent with the
3374 -- bit position specified by the component clause.
3377 Field : constant Entity_Id := Entity (Selector_Name (Pref));
3379 if Present (Component_Clause (Field))
3381 (Unknown_Alignment (Styp)
3383 (Component_Bit_Offset (Field) mod
3384 (System_Storage_Unit * Alignment (Styp))) /= 0)
3390 -- For cases other than selected or indexed components we
3391 -- know we are OK, since no issues arise over alignment.
3397 -- We processed an indexed component or selected component
3398 -- reference that looked safe, so keep checking prefixes.
3400 Pref := Prefix (Pref);
3403 end Is_Possibly_Unaligned_Slice;
3405 --------------------------------
3406 -- Is_Ref_To_Bit_Packed_Array --
3407 --------------------------------
3409 function Is_Ref_To_Bit_Packed_Array (N : Node_Id) return Boolean is
3414 if Is_Entity_Name (N)
3415 and then Is_Object (Entity (N))
3416 and then Present (Renamed_Object (Entity (N)))
3418 return Is_Ref_To_Bit_Packed_Array (Renamed_Object (Entity (N)));
3421 if Nkind (N) = N_Indexed_Component
3423 Nkind (N) = N_Selected_Component
3425 if Is_Bit_Packed_Array (Etype (Prefix (N))) then
3428 Result := Is_Ref_To_Bit_Packed_Array (Prefix (N));
3431 if Result and then Nkind (N) = N_Indexed_Component then
3432 Expr := First (Expressions (N));
3433 while Present (Expr) loop
3434 Force_Evaluation (Expr);
3444 end Is_Ref_To_Bit_Packed_Array;
3446 --------------------------------
3447 -- Is_Ref_To_Bit_Packed_Slice --
3448 --------------------------------
3450 function Is_Ref_To_Bit_Packed_Slice (N : Node_Id) return Boolean is
3452 if Nkind (N) = N_Type_Conversion then
3453 return Is_Ref_To_Bit_Packed_Slice (Expression (N));
3455 elsif Is_Entity_Name (N)
3456 and then Is_Object (Entity (N))
3457 and then Present (Renamed_Object (Entity (N)))
3459 return Is_Ref_To_Bit_Packed_Slice (Renamed_Object (Entity (N)));
3461 elsif Nkind (N) = N_Slice
3462 and then Is_Bit_Packed_Array (Etype (Prefix (N)))
3466 elsif Nkind (N) = N_Indexed_Component
3468 Nkind (N) = N_Selected_Component
3470 return Is_Ref_To_Bit_Packed_Slice (Prefix (N));
3475 end Is_Ref_To_Bit_Packed_Slice;
3477 -----------------------
3478 -- Is_Renamed_Object --
3479 -----------------------
3481 function Is_Renamed_Object (N : Node_Id) return Boolean is
3482 Pnod : constant Node_Id := Parent (N);
3483 Kind : constant Node_Kind := Nkind (Pnod);
3485 if Kind = N_Object_Renaming_Declaration then
3487 elsif Nkind_In (Kind, N_Indexed_Component, N_Selected_Component) then
3488 return Is_Renamed_Object (Pnod);
3492 end Is_Renamed_Object;
3494 ----------------------------
3495 -- Is_Untagged_Derivation --
3496 ----------------------------
3498 function Is_Untagged_Derivation (T : Entity_Id) return Boolean is
3500 return (not Is_Tagged_Type (T) and then Is_Derived_Type (T))
3502 (Is_Private_Type (T) and then Present (Full_View (T))
3503 and then not Is_Tagged_Type (Full_View (T))
3504 and then Is_Derived_Type (Full_View (T))
3505 and then Etype (Full_View (T)) /= T);
3506 end Is_Untagged_Derivation;
3508 ---------------------------
3509 -- Is_Volatile_Reference --
3510 ---------------------------
3512 function Is_Volatile_Reference (N : Node_Id) return Boolean is
3514 if Nkind (N) in N_Has_Etype
3515 and then Present (Etype (N))
3516 and then Treat_As_Volatile (Etype (N))
3520 elsif Is_Entity_Name (N) then
3521 return Treat_As_Volatile (Entity (N));
3523 elsif Nkind (N) = N_Slice then
3524 return Is_Volatile_Reference (Prefix (N));
3526 elsif Nkind_In (N, N_Indexed_Component, N_Selected_Component) then
3527 if (Is_Entity_Name (Prefix (N))
3528 and then Has_Volatile_Components (Entity (Prefix (N))))
3529 or else (Present (Etype (Prefix (N)))
3530 and then Has_Volatile_Components (Etype (Prefix (N))))
3534 return Is_Volatile_Reference (Prefix (N));
3540 end Is_Volatile_Reference;
3542 --------------------
3543 -- Kill_Dead_Code --
3544 --------------------
3546 procedure Kill_Dead_Code (N : Node_Id; Warn : Boolean := False) is
3547 W : Boolean := Warn;
3548 -- Set False if warnings suppressed
3552 Remove_Warning_Messages (N);
3554 -- Generate warning if appropriate
3558 -- We suppress the warning if this code is under control of an
3559 -- if statement, whose condition is a simple identifier, and
3560 -- either we are in an instance, or warnings off is set for this
3561 -- identifier. The reason for killing it in the instance case is
3562 -- that it is common and reasonable for code to be deleted in
3563 -- instances for various reasons.
3565 if Nkind (Parent (N)) = N_If_Statement then
3567 C : constant Node_Id := Condition (Parent (N));
3569 if Nkind (C) = N_Identifier
3572 or else (Present (Entity (C))
3573 and then Has_Warnings_Off (Entity (C))))
3580 -- Generate warning if not suppressed
3584 ("?this code can never be executed and has been deleted!", N);
3588 -- Recurse into block statements and bodies to process declarations
3591 if Nkind (N) = N_Block_Statement
3592 or else Nkind (N) = N_Subprogram_Body
3593 or else Nkind (N) = N_Package_Body
3595 Kill_Dead_Code (Declarations (N), False);
3596 Kill_Dead_Code (Statements (Handled_Statement_Sequence (N)));
3598 if Nkind (N) = N_Subprogram_Body then
3599 Set_Is_Eliminated (Defining_Entity (N));
3602 elsif Nkind (N) = N_Package_Declaration then
3603 Kill_Dead_Code (Visible_Declarations (Specification (N)));
3604 Kill_Dead_Code (Private_Declarations (Specification (N)));
3606 -- ??? After this point, Delete_Tree has been called on all
3607 -- declarations in Specification (N), so references to
3608 -- entities therein look suspicious.
3611 E : Entity_Id := First_Entity (Defining_Entity (N));
3613 while Present (E) loop
3614 if Ekind (E) = E_Operator then
3615 Set_Is_Eliminated (E);
3622 -- Recurse into composite statement to kill individual statements,
3623 -- in particular instantiations.
3625 elsif Nkind (N) = N_If_Statement then
3626 Kill_Dead_Code (Then_Statements (N));
3627 Kill_Dead_Code (Elsif_Parts (N));
3628 Kill_Dead_Code (Else_Statements (N));
3630 elsif Nkind (N) = N_Loop_Statement then
3631 Kill_Dead_Code (Statements (N));
3633 elsif Nkind (N) = N_Case_Statement then
3637 Alt := First (Alternatives (N));
3638 while Present (Alt) loop
3639 Kill_Dead_Code (Statements (Alt));
3644 elsif Nkind (N) = N_Case_Statement_Alternative then
3645 Kill_Dead_Code (Statements (N));
3647 -- Deal with dead instances caused by deleting instantiations
3649 elsif Nkind (N) in N_Generic_Instantiation then
3650 Remove_Dead_Instance (N);
3655 -- Case where argument is a list of nodes to be killed
3657 procedure Kill_Dead_Code (L : List_Id; Warn : Boolean := False) is
3662 if Is_Non_Empty_List (L) then
3664 while Present (N) loop
3665 Kill_Dead_Code (N, W);
3672 ------------------------
3673 -- Known_Non_Negative --
3674 ------------------------
3676 function Known_Non_Negative (Opnd : Node_Id) return Boolean is
3678 if Is_OK_Static_Expression (Opnd)
3679 and then Expr_Value (Opnd) >= 0
3685 Lo : constant Node_Id := Type_Low_Bound (Etype (Opnd));
3689 Is_OK_Static_Expression (Lo) and then Expr_Value (Lo) >= 0;
3692 end Known_Non_Negative;
3694 --------------------
3695 -- Known_Non_Null --
3696 --------------------
3698 function Known_Non_Null (N : Node_Id) return Boolean is
3700 -- Checks for case where N is an entity reference
3702 if Is_Entity_Name (N) and then Present (Entity (N)) then
3704 E : constant Entity_Id := Entity (N);
3709 -- First check if we are in decisive conditional
3711 Get_Current_Value_Condition (N, Op, Val);
3713 if Known_Null (Val) then
3714 if Op = N_Op_Eq then
3716 elsif Op = N_Op_Ne then
3721 -- If OK to do replacement, test Is_Known_Non_Null flag
3723 if OK_To_Do_Constant_Replacement (E) then
3724 return Is_Known_Non_Null (E);
3726 -- Otherwise if not safe to do replacement, then say so
3733 -- True if access attribute
3735 elsif Nkind (N) = N_Attribute_Reference
3736 and then (Attribute_Name (N) = Name_Access
3738 Attribute_Name (N) = Name_Unchecked_Access
3740 Attribute_Name (N) = Name_Unrestricted_Access)
3744 -- True if allocator
3746 elsif Nkind (N) = N_Allocator then
3749 -- For a conversion, true if expression is known non-null
3751 elsif Nkind (N) = N_Type_Conversion then
3752 return Known_Non_Null (Expression (N));
3754 -- Above are all cases where the value could be determined to be
3755 -- non-null. In all other cases, we don't know, so return False.
3766 function Known_Null (N : Node_Id) return Boolean is
3768 -- Checks for case where N is an entity reference
3770 if Is_Entity_Name (N) and then Present (Entity (N)) then
3772 E : constant Entity_Id := Entity (N);
3777 -- Constant null value is for sure null
3779 if Ekind (E) = E_Constant
3780 and then Known_Null (Constant_Value (E))
3785 -- First check if we are in decisive conditional
3787 Get_Current_Value_Condition (N, Op, Val);
3789 if Known_Null (Val) then
3790 if Op = N_Op_Eq then
3792 elsif Op = N_Op_Ne then
3797 -- If OK to do replacement, test Is_Known_Null flag
3799 if OK_To_Do_Constant_Replacement (E) then
3800 return Is_Known_Null (E);
3802 -- Otherwise if not safe to do replacement, then say so
3809 -- True if explicit reference to null
3811 elsif Nkind (N) = N_Null then
3814 -- For a conversion, true if expression is known null
3816 elsif Nkind (N) = N_Type_Conversion then
3817 return Known_Null (Expression (N));
3819 -- Above are all cases where the value could be determined to be null.
3820 -- In all other cases, we don't know, so return False.
3827 -----------------------------
3828 -- Make_CW_Equivalent_Type --
3829 -----------------------------
3831 -- Create a record type used as an equivalent of any member of the class
3832 -- which takes its size from exp.
3834 -- Generate the following code:
3836 -- type Equiv_T is record
3837 -- _parent : T (List of discriminant constraints taken from Exp);
3838 -- Ext__50 : Storage_Array (1 .. (Exp'size - Typ'object_size)/8);
3841 -- ??? Note that this type does not guarantee same alignment as all
3844 function Make_CW_Equivalent_Type
3846 E : Node_Id) return Entity_Id
3848 Loc : constant Source_Ptr := Sloc (E);
3849 Root_Typ : constant Entity_Id := Root_Type (T);
3850 List_Def : constant List_Id := Empty_List;
3851 Comp_List : constant List_Id := New_List;
3852 Equiv_Type : Entity_Id;
3853 Range_Type : Entity_Id;
3854 Str_Type : Entity_Id;
3855 Constr_Root : Entity_Id;
3859 -- If the root type is already constrained, there are no discriminants
3860 -- in the expression.
3862 if not Has_Discriminants (Root_Typ)
3863 or else Is_Constrained (Root_Typ)
3865 Constr_Root := Root_Typ;
3867 Constr_Root := Make_Temporary (Loc, 'R');
3869 -- subtype cstr__n is T (List of discr constraints taken from Exp)
3871 Append_To (List_Def,
3872 Make_Subtype_Declaration (Loc,
3873 Defining_Identifier => Constr_Root,
3874 Subtype_Indication => Make_Subtype_From_Expr (E, Root_Typ)));
3877 -- Generate the range subtype declaration
3879 Range_Type := Make_Temporary (Loc, 'G');
3881 if not Is_Interface (Root_Typ) then
3883 -- subtype rg__xx is
3884 -- Storage_Offset range 1 .. (Expr'size - typ'size) / Storage_Unit
3887 Make_Op_Subtract (Loc,
3889 Make_Attribute_Reference (Loc,
3891 OK_Convert_To (T, Duplicate_Subexpr_No_Checks (E)),
3892 Attribute_Name => Name_Size),
3894 Make_Attribute_Reference (Loc,
3895 Prefix => New_Reference_To (Constr_Root, Loc),
3896 Attribute_Name => Name_Object_Size));
3898 -- subtype rg__xx is
3899 -- Storage_Offset range 1 .. Expr'size / Storage_Unit
3902 Make_Attribute_Reference (Loc,
3904 OK_Convert_To (T, Duplicate_Subexpr_No_Checks (E)),
3905 Attribute_Name => Name_Size);
3908 Set_Paren_Count (Sizexpr, 1);
3910 Append_To (List_Def,
3911 Make_Subtype_Declaration (Loc,
3912 Defining_Identifier => Range_Type,
3913 Subtype_Indication =>
3914 Make_Subtype_Indication (Loc,
3915 Subtype_Mark => New_Reference_To (RTE (RE_Storage_Offset), Loc),
3916 Constraint => Make_Range_Constraint (Loc,
3919 Low_Bound => Make_Integer_Literal (Loc, 1),
3921 Make_Op_Divide (Loc,
3922 Left_Opnd => Sizexpr,
3923 Right_Opnd => Make_Integer_Literal (Loc,
3924 Intval => System_Storage_Unit)))))));
3926 -- subtype str__nn is Storage_Array (rg__x);
3928 Str_Type := Make_Temporary (Loc, 'S');
3929 Append_To (List_Def,
3930 Make_Subtype_Declaration (Loc,
3931 Defining_Identifier => Str_Type,
3932 Subtype_Indication =>
3933 Make_Subtype_Indication (Loc,
3934 Subtype_Mark => New_Reference_To (RTE (RE_Storage_Array), Loc),
3936 Make_Index_Or_Discriminant_Constraint (Loc,
3938 New_List (New_Reference_To (Range_Type, Loc))))));
3940 -- type Equiv_T is record
3941 -- [ _parent : Tnn; ]
3945 Equiv_Type := Make_Temporary (Loc, 'T');
3946 Set_Ekind (Equiv_Type, E_Record_Type);
3947 Set_Parent_Subtype (Equiv_Type, Constr_Root);
3949 -- Set Is_Class_Wide_Equivalent_Type very early to trigger the special
3950 -- treatment for this type. In particular, even though _parent's type
3951 -- is a controlled type or contains controlled components, we do not
3952 -- want to set Has_Controlled_Component on it to avoid making it gain
3953 -- an unwanted _controller component.
3955 Set_Is_Class_Wide_Equivalent_Type (Equiv_Type);
3957 if not Is_Interface (Root_Typ) then
3958 Append_To (Comp_List,
3959 Make_Component_Declaration (Loc,
3960 Defining_Identifier =>
3961 Make_Defining_Identifier (Loc, Name_uParent),
3962 Component_Definition =>
3963 Make_Component_Definition (Loc,
3964 Aliased_Present => False,
3965 Subtype_Indication => New_Reference_To (Constr_Root, Loc))));
3968 Append_To (Comp_List,
3969 Make_Component_Declaration (Loc,
3970 Defining_Identifier => Make_Temporary (Loc, 'C'),
3971 Component_Definition =>
3972 Make_Component_Definition (Loc,
3973 Aliased_Present => False,
3974 Subtype_Indication => New_Reference_To (Str_Type, Loc))));
3976 Append_To (List_Def,
3977 Make_Full_Type_Declaration (Loc,
3978 Defining_Identifier => Equiv_Type,
3980 Make_Record_Definition (Loc,
3982 Make_Component_List (Loc,
3983 Component_Items => Comp_List,
3984 Variant_Part => Empty))));
3986 -- Suppress all checks during the analysis of the expanded code
3987 -- to avoid the generation of spurious warnings under ZFP run-time.
3989 Insert_Actions (E, List_Def, Suppress => All_Checks);
3991 end Make_CW_Equivalent_Type;
3993 -------------------------
3994 -- Make_Invariant_Call --
3995 -------------------------
3997 function Make_Invariant_Call (Expr : Node_Id) return Node_Id is
3998 Loc : constant Source_Ptr := Sloc (Expr);
3999 Typ : constant Entity_Id := Etype (Expr);
4003 (Has_Invariants (Typ) and then Present (Invariant_Procedure (Typ)));
4005 if Check_Enabled (Name_Invariant)
4007 Check_Enabled (Name_Assertion)
4010 Make_Procedure_Call_Statement (Loc,
4012 New_Occurrence_Of (Invariant_Procedure (Typ), Loc),
4013 Parameter_Associations => New_List (Relocate_Node (Expr)));
4017 Make_Null_Statement (Loc);
4019 end Make_Invariant_Call;
4021 ------------------------
4022 -- Make_Literal_Range --
4023 ------------------------
4025 function Make_Literal_Range
4027 Literal_Typ : Entity_Id) return Node_Id
4029 Lo : constant Node_Id :=
4030 New_Copy_Tree (String_Literal_Low_Bound (Literal_Typ));
4031 Index : constant Entity_Id := Etype (Lo);
4034 Length_Expr : constant Node_Id :=
4035 Make_Op_Subtract (Loc,
4037 Make_Integer_Literal (Loc,
4038 Intval => String_Literal_Length (Literal_Typ)),
4040 Make_Integer_Literal (Loc, 1));
4043 Set_Analyzed (Lo, False);
4045 if Is_Integer_Type (Index) then
4048 Left_Opnd => New_Copy_Tree (Lo),
4049 Right_Opnd => Length_Expr);
4052 Make_Attribute_Reference (Loc,
4053 Attribute_Name => Name_Val,
4054 Prefix => New_Occurrence_Of (Index, Loc),
4055 Expressions => New_List (
4058 Make_Attribute_Reference (Loc,
4059 Attribute_Name => Name_Pos,
4060 Prefix => New_Occurrence_Of (Index, Loc),
4061 Expressions => New_List (New_Copy_Tree (Lo))),
4062 Right_Opnd => Length_Expr)));
4069 end Make_Literal_Range;
4071 --------------------------
4072 -- Make_Non_Empty_Check --
4073 --------------------------
4075 function Make_Non_Empty_Check
4077 N : Node_Id) return Node_Id
4083 Make_Attribute_Reference (Loc,
4084 Attribute_Name => Name_Length,
4085 Prefix => Duplicate_Subexpr_No_Checks (N, Name_Req => True)),
4087 Make_Integer_Literal (Loc, 0));
4088 end Make_Non_Empty_Check;
4090 -------------------------
4091 -- Make_Predicate_Call --
4092 -------------------------
4094 function Make_Predicate_Call
4096 Expr : Node_Id) return Node_Id
4098 Loc : constant Source_Ptr := Sloc (Expr);
4101 pragma Assert (Present (Predicate_Function (Typ)));
4104 Make_Function_Call (Loc,
4106 New_Occurrence_Of (Predicate_Function (Typ), Loc),
4107 Parameter_Associations => New_List (Relocate_Node (Expr)));
4108 end Make_Predicate_Call;
4110 --------------------------
4111 -- Make_Predicate_Check --
4112 --------------------------
4114 function Make_Predicate_Check
4116 Expr : Node_Id) return Node_Id
4118 Loc : constant Source_Ptr := Sloc (Expr);
4123 Pragma_Identifier =>
4124 Make_Identifier (Loc,
4126 Pragma_Argument_Associations => New_List (
4127 Make_Pragma_Argument_Association (Loc,
4129 Make_Identifier (Loc,
4130 Chars => Name_Predicate)),
4131 Make_Pragma_Argument_Association (Loc,
4132 Expression => Make_Predicate_Call (Typ, Expr))));
4133 end Make_Predicate_Check;
4135 ----------------------------
4136 -- Make_Subtype_From_Expr --
4137 ----------------------------
4139 -- 1. If Expr is an unconstrained array expression, creates
4140 -- Unc_Type(Expr'first(1)..Expr'last(1),..., Expr'first(n)..Expr'last(n))
4142 -- 2. If Expr is a unconstrained discriminated type expression, creates
4143 -- Unc_Type(Expr.Discr1, ... , Expr.Discr_n)
4145 -- 3. If Expr is class-wide, creates an implicit class wide subtype
4147 function Make_Subtype_From_Expr
4149 Unc_Typ : Entity_Id) return Node_Id
4151 Loc : constant Source_Ptr := Sloc (E);
4152 List_Constr : constant List_Id := New_List;
4155 Full_Subtyp : Entity_Id;
4156 Priv_Subtyp : Entity_Id;
4161 if Is_Private_Type (Unc_Typ)
4162 and then Has_Unknown_Discriminants (Unc_Typ)
4164 -- Prepare the subtype completion, Go to base type to
4165 -- find underlying type, because the type may be a generic
4166 -- actual or an explicit subtype.
4168 Utyp := Underlying_Type (Base_Type (Unc_Typ));
4169 Full_Subtyp := Make_Temporary (Loc, 'C');
4171 Unchecked_Convert_To (Utyp, Duplicate_Subexpr_No_Checks (E));
4172 Set_Parent (Full_Exp, Parent (E));
4174 Priv_Subtyp := Make_Temporary (Loc, 'P');
4177 Make_Subtype_Declaration (Loc,
4178 Defining_Identifier => Full_Subtyp,
4179 Subtype_Indication => Make_Subtype_From_Expr (Full_Exp, Utyp)));
4181 -- Define the dummy private subtype
4183 Set_Ekind (Priv_Subtyp, Subtype_Kind (Ekind (Unc_Typ)));
4184 Set_Etype (Priv_Subtyp, Base_Type (Unc_Typ));
4185 Set_Scope (Priv_Subtyp, Full_Subtyp);
4186 Set_Is_Constrained (Priv_Subtyp);
4187 Set_Is_Tagged_Type (Priv_Subtyp, Is_Tagged_Type (Unc_Typ));
4188 Set_Is_Itype (Priv_Subtyp);
4189 Set_Associated_Node_For_Itype (Priv_Subtyp, E);
4191 if Is_Tagged_Type (Priv_Subtyp) then
4193 (Base_Type (Priv_Subtyp), Class_Wide_Type (Unc_Typ));
4194 Set_Direct_Primitive_Operations (Priv_Subtyp,
4195 Direct_Primitive_Operations (Unc_Typ));
4198 Set_Full_View (Priv_Subtyp, Full_Subtyp);
4200 return New_Reference_To (Priv_Subtyp, Loc);
4202 elsif Is_Array_Type (Unc_Typ) then
4203 for J in 1 .. Number_Dimensions (Unc_Typ) loop
4204 Append_To (List_Constr,
4207 Make_Attribute_Reference (Loc,
4208 Prefix => Duplicate_Subexpr_No_Checks (E),
4209 Attribute_Name => Name_First,
4210 Expressions => New_List (
4211 Make_Integer_Literal (Loc, J))),
4214 Make_Attribute_Reference (Loc,
4215 Prefix => Duplicate_Subexpr_No_Checks (E),
4216 Attribute_Name => Name_Last,
4217 Expressions => New_List (
4218 Make_Integer_Literal (Loc, J)))));
4221 elsif Is_Class_Wide_Type (Unc_Typ) then
4223 CW_Subtype : Entity_Id;
4224 EQ_Typ : Entity_Id := Empty;
4227 -- A class-wide equivalent type is not needed when VM_Target
4228 -- because the VM back-ends handle the class-wide object
4229 -- initialization itself (and doesn't need or want the
4230 -- additional intermediate type to handle the assignment).
4232 if Expander_Active and then Tagged_Type_Expansion then
4234 -- If this is the class_wide type of a completion that is
4235 -- a record subtype, set the type of the class_wide type
4236 -- to be the full base type, for use in the expanded code
4237 -- for the equivalent type. Should this be done earlier when
4238 -- the completion is analyzed ???
4240 if Is_Private_Type (Etype (Unc_Typ))
4242 Ekind (Full_View (Etype (Unc_Typ))) = E_Record_Subtype
4244 Set_Etype (Unc_Typ, Base_Type (Full_View (Etype (Unc_Typ))));
4247 EQ_Typ := Make_CW_Equivalent_Type (Unc_Typ, E);
4250 CW_Subtype := New_Class_Wide_Subtype (Unc_Typ, E);
4251 Set_Equivalent_Type (CW_Subtype, EQ_Typ);
4252 Set_Cloned_Subtype (CW_Subtype, Base_Type (Unc_Typ));
4254 return New_Occurrence_Of (CW_Subtype, Loc);
4257 -- Indefinite record type with discriminants
4260 D := First_Discriminant (Unc_Typ);
4261 while Present (D) loop
4262 Append_To (List_Constr,
4263 Make_Selected_Component (Loc,
4264 Prefix => Duplicate_Subexpr_No_Checks (E),
4265 Selector_Name => New_Reference_To (D, Loc)));
4267 Next_Discriminant (D);
4272 Make_Subtype_Indication (Loc,
4273 Subtype_Mark => New_Reference_To (Unc_Typ, Loc),
4275 Make_Index_Or_Discriminant_Constraint (Loc,
4276 Constraints => List_Constr));
4277 end Make_Subtype_From_Expr;
4279 -----------------------------
4280 -- May_Generate_Large_Temp --
4281 -----------------------------
4283 -- At the current time, the only types that we return False for (i.e.
4284 -- where we decide we know they cannot generate large temps) are ones
4285 -- where we know the size is 256 bits or less at compile time, and we
4286 -- are still not doing a thorough job on arrays and records ???
4288 function May_Generate_Large_Temp (Typ : Entity_Id) return Boolean is
4290 if not Size_Known_At_Compile_Time (Typ) then
4293 elsif Esize (Typ) /= 0 and then Esize (Typ) <= 256 then
4296 elsif Is_Array_Type (Typ)
4297 and then Present (Packed_Array_Type (Typ))
4299 return May_Generate_Large_Temp (Packed_Array_Type (Typ));
4301 -- We could do more here to find other small types ???
4306 end May_Generate_Large_Temp;
4308 ----------------------------
4309 -- Needs_Constant_Address --
4310 ----------------------------
4312 function Needs_Constant_Address
4314 Typ : Entity_Id) return Boolean
4318 -- If we have no initialization of any kind, then we don't need to
4319 -- place any restrictions on the address clause, because the object
4320 -- will be elaborated after the address clause is evaluated. This
4321 -- happens if the declaration has no initial expression, or the type
4322 -- has no implicit initialization, or the object is imported.
4324 -- The same holds for all initialized scalar types and all access
4325 -- types. Packed bit arrays of size up to 64 are represented using a
4326 -- modular type with an initialization (to zero) and can be processed
4327 -- like other initialized scalar types.
4329 -- If the type is controlled, code to attach the object to a
4330 -- finalization chain is generated at the point of declaration,
4331 -- and therefore the elaboration of the object cannot be delayed:
4332 -- the address expression must be a constant.
4334 if No (Expression (Decl))
4335 and then not Needs_Finalization (Typ)
4337 (not Has_Non_Null_Base_Init_Proc (Typ)
4338 or else Is_Imported (Defining_Identifier (Decl)))
4342 elsif (Present (Expression (Decl)) and then Is_Scalar_Type (Typ))
4343 or else Is_Access_Type (Typ)
4345 (Is_Bit_Packed_Array (Typ)
4346 and then Is_Modular_Integer_Type (Packed_Array_Type (Typ)))
4352 -- Otherwise, we require the address clause to be constant because
4353 -- the call to the initialization procedure (or the attach code) has
4354 -- to happen at the point of the declaration.
4356 -- Actually the IP call has been moved to the freeze actions
4357 -- anyway, so maybe we can relax this restriction???
4361 end Needs_Constant_Address;
4363 ----------------------------
4364 -- New_Class_Wide_Subtype --
4365 ----------------------------
4367 function New_Class_Wide_Subtype
4368 (CW_Typ : Entity_Id;
4369 N : Node_Id) return Entity_Id
4371 Res : constant Entity_Id := Create_Itype (E_Void, N);
4372 Res_Name : constant Name_Id := Chars (Res);
4373 Res_Scope : constant Entity_Id := Scope (Res);
4376 Copy_Node (CW_Typ, Res);
4377 Set_Comes_From_Source (Res, False);
4378 Set_Sloc (Res, Sloc (N));
4380 Set_Associated_Node_For_Itype (Res, N);
4381 Set_Is_Public (Res, False); -- By default, may be changed below.
4382 Set_Public_Status (Res);
4383 Set_Chars (Res, Res_Name);
4384 Set_Scope (Res, Res_Scope);
4385 Set_Ekind (Res, E_Class_Wide_Subtype);
4386 Set_Next_Entity (Res, Empty);
4387 Set_Etype (Res, Base_Type (CW_Typ));
4388 Set_Is_Frozen (Res, False);
4389 Set_Freeze_Node (Res, Empty);
4391 end New_Class_Wide_Subtype;
4393 --------------------------------
4394 -- Non_Limited_Designated_Type --
4395 ---------------------------------
4397 function Non_Limited_Designated_Type (T : Entity_Id) return Entity_Id is
4398 Desig : constant Entity_Id := Designated_Type (T);
4400 if Ekind (Desig) = E_Incomplete_Type
4401 and then Present (Non_Limited_View (Desig))
4403 return Non_Limited_View (Desig);
4407 end Non_Limited_Designated_Type;
4409 -----------------------------------
4410 -- OK_To_Do_Constant_Replacement --
4411 -----------------------------------
4413 function OK_To_Do_Constant_Replacement (E : Entity_Id) return Boolean is
4414 ES : constant Entity_Id := Scope (E);
4418 -- Do not replace statically allocated objects, because they may be
4419 -- modified outside the current scope.
4421 if Is_Statically_Allocated (E) then
4424 -- Do not replace aliased or volatile objects, since we don't know what
4425 -- else might change the value.
4427 elsif Is_Aliased (E) or else Treat_As_Volatile (E) then
4430 -- Debug flag -gnatdM disconnects this optimization
4432 elsif Debug_Flag_MM then
4435 -- Otherwise check scopes
4438 CS := Current_Scope;
4441 -- If we are in right scope, replacement is safe
4446 -- Packages do not affect the determination of safety
4448 elsif Ekind (CS) = E_Package then
4449 exit when CS = Standard_Standard;
4452 -- Blocks do not affect the determination of safety
4454 elsif Ekind (CS) = E_Block then
4457 -- Loops do not affect the determination of safety. Note that we
4458 -- kill all current values on entry to a loop, so we are just
4459 -- talking about processing within a loop here.
4461 elsif Ekind (CS) = E_Loop then
4464 -- Otherwise, the reference is dubious, and we cannot be sure that
4465 -- it is safe to do the replacement.
4474 end OK_To_Do_Constant_Replacement;
4476 ------------------------------------
4477 -- Possible_Bit_Aligned_Component --
4478 ------------------------------------
4480 function Possible_Bit_Aligned_Component (N : Node_Id) return Boolean is
4484 -- Case of indexed component
4486 when N_Indexed_Component =>
4488 P : constant Node_Id := Prefix (N);
4489 Ptyp : constant Entity_Id := Etype (P);
4492 -- If we know the component size and it is less than 64, then
4493 -- we are definitely OK. The back end always does assignment of
4494 -- misaligned small objects correctly.
4496 if Known_Static_Component_Size (Ptyp)
4497 and then Component_Size (Ptyp) <= 64
4501 -- Otherwise, we need to test the prefix, to see if we are
4502 -- indexing from a possibly unaligned component.
4505 return Possible_Bit_Aligned_Component (P);
4509 -- Case of selected component
4511 when N_Selected_Component =>
4513 P : constant Node_Id := Prefix (N);
4514 Comp : constant Entity_Id := Entity (Selector_Name (N));
4517 -- If there is no component clause, then we are in the clear
4518 -- since the back end will never misalign a large component
4519 -- unless it is forced to do so. In the clear means we need
4520 -- only the recursive test on the prefix.
4522 if Component_May_Be_Bit_Aligned (Comp) then
4525 return Possible_Bit_Aligned_Component (P);
4529 -- For a slice, test the prefix, if that is possibly misaligned,
4530 -- then for sure the slice is!
4533 return Possible_Bit_Aligned_Component (Prefix (N));
4535 -- If we have none of the above, it means that we have fallen off the
4536 -- top testing prefixes recursively, and we now have a stand alone
4537 -- object, where we don't have a problem.
4543 end Possible_Bit_Aligned_Component;
4545 -------------------------
4546 -- Remove_Side_Effects --
4547 -------------------------
4549 procedure Remove_Side_Effects
4551 Name_Req : Boolean := False;
4552 Variable_Ref : Boolean := False)
4554 Loc : constant Source_Ptr := Sloc (Exp);
4555 Exp_Type : constant Entity_Id := Etype (Exp);
4556 Svg_Suppress : constant Suppress_Array := Scope_Suppress;
4558 Ref_Type : Entity_Id;
4560 Ptr_Typ_Decl : Node_Id;
4564 function Side_Effect_Free (N : Node_Id) return Boolean;
4565 -- Determines if the tree N represents an expression that is known not
4566 -- to have side effects, and for which no processing is required.
4568 function Side_Effect_Free (L : List_Id) return Boolean;
4569 -- Determines if all elements of the list L are side effect free
4571 function Safe_Prefixed_Reference (N : Node_Id) return Boolean;
4572 -- The argument N is a construct where the Prefix is dereferenced if it
4573 -- is an access type and the result is a variable. The call returns True
4574 -- if the construct is side effect free (not considering side effects in
4575 -- other than the prefix which are to be tested by the caller).
4577 function Within_In_Parameter (N : Node_Id) return Boolean;
4578 -- Determines if N is a subcomponent of a composite in-parameter. If so,
4579 -- N is not side-effect free when the actual is global and modifiable
4580 -- indirectly from within a subprogram, because it may be passed by
4581 -- reference. The front-end must be conservative here and assume that
4582 -- this may happen with any array or record type. On the other hand, we
4583 -- cannot create temporaries for all expressions for which this
4584 -- condition is true, for various reasons that might require clearing up
4585 -- ??? For example, discriminant references that appear out of place, or
4586 -- spurious type errors with class-wide expressions. As a result, we
4587 -- limit the transformation to loop bounds, which is so far the only
4588 -- case that requires it.
4590 -----------------------------
4591 -- Safe_Prefixed_Reference --
4592 -----------------------------
4594 function Safe_Prefixed_Reference (N : Node_Id) return Boolean is
4596 -- If prefix is not side effect free, definitely not safe
4598 if not Side_Effect_Free (Prefix (N)) then
4601 -- If the prefix is of an access type that is not access-to-constant,
4602 -- then this construct is a variable reference, which means it is to
4603 -- be considered to have side effects if Variable_Ref is set True
4604 -- Exception is an access to an entity that is a constant or an
4605 -- in-parameter which does not come from source, and is the result
4606 -- of a previous removal of side-effects.
4608 elsif Is_Access_Type (Etype (Prefix (N)))
4609 and then not Is_Access_Constant (Etype (Prefix (N)))
4610 and then Variable_Ref
4612 if not Is_Entity_Name (Prefix (N)) then
4615 return Ekind (Entity (Prefix (N))) = E_Constant
4616 or else Ekind (Entity (Prefix (N))) = E_In_Parameter;
4619 -- If the prefix is an explicit dereference then this construct is a
4620 -- variable reference, which means it is to be considered to have
4621 -- side effects if Variable_Ref is True.
4623 -- We do NOT exclude dereferences of access-to-constant types because
4624 -- we handle them as constant view of variables.
4626 -- Exception is an access to an entity that is a constant or an
4629 elsif Nkind (Prefix (N)) = N_Explicit_Dereference
4630 and then Variable_Ref
4633 DDT : constant Entity_Id :=
4634 Designated_Type (Etype (Prefix (Prefix (N))));
4636 return Ekind_In (DDT, E_Constant, E_In_Parameter);
4639 -- The following test is the simplest way of solving a complex
4640 -- problem uncovered by BB08-010: Side effect on loop bound that
4641 -- is a subcomponent of a global variable:
4642 -- If a loop bound is a subcomponent of a global variable, a
4643 -- modification of that variable within the loop may incorrectly
4644 -- affect the execution of the loop.
4647 (Nkind (Parent (Parent (N))) /= N_Loop_Parameter_Specification
4648 or else not Within_In_Parameter (Prefix (N)))
4652 -- All other cases are side effect free
4657 end Safe_Prefixed_Reference;
4659 ----------------------
4660 -- Side_Effect_Free --
4661 ----------------------
4663 function Side_Effect_Free (N : Node_Id) return Boolean is
4665 -- Note on checks that could raise Constraint_Error. Strictly, if we
4666 -- take advantage of 11.6, these checks do not count as side effects.
4667 -- However, we would prefer to consider that they are side effects,
4668 -- since the backend CSE does not work very well on expressions which
4669 -- can raise Constraint_Error. On the other hand if we don't consider
4670 -- them to be side effect free, then we get some awkward expansions
4671 -- in -gnato mode, resulting in code insertions at a point where we
4672 -- do not have a clear model for performing the insertions.
4674 -- Special handling for entity names
4676 if Is_Entity_Name (N) then
4678 -- If the entity is a constant, it is definitely side effect
4679 -- free. Note that the test of Is_Variable (N) below might
4680 -- be expected to catch this case, but it does not, because
4681 -- this test goes to the original tree, and we may have
4682 -- already rewritten a variable node with a constant as
4683 -- a result of an earlier Force_Evaluation call.
4685 if Ekind_In (Entity (N), E_Constant, E_In_Parameter) then
4688 -- Functions are not side effect free
4690 elsif Ekind (Entity (N)) = E_Function then
4693 -- Variables are considered to be a side effect if Variable_Ref
4694 -- is set or if we have a volatile reference and Name_Req is off.
4695 -- If Name_Req is True then we can't help returning a name which
4696 -- effectively allows multiple references in any case.
4698 elsif Is_Variable (N) then
4699 return not Variable_Ref
4700 and then (not Is_Volatile_Reference (N) or else Name_Req);
4702 -- Any other entity (e.g. a subtype name) is definitely side
4709 -- A value known at compile time is always side effect free
4711 elsif Compile_Time_Known_Value (N) then
4714 -- A variable renaming is not side-effect free, because the
4715 -- renaming will function like a macro in the front-end in
4716 -- some cases, and an assignment can modify the component
4717 -- designated by N, so we need to create a temporary for it.
4719 elsif Is_Entity_Name (Original_Node (N))
4720 and then Is_Renaming_Of_Object (Entity (Original_Node (N)))
4721 and then Ekind (Entity (Original_Node (N))) /= E_Constant
4725 -- Remove_Side_Effects generates an object renaming declaration to
4726 -- capture the expression of a class-wide expression. In VM targets
4727 -- the frontend performs no expansion for dispatching calls to
4728 -- class-wide types since they are handled by the VM. Hence, we must
4729 -- locate here if this node corresponds to a previous invocation of
4730 -- Remove_Side_Effects to avoid a never ending loop in the frontend.
4732 elsif VM_Target /= No_VM
4733 and then not Comes_From_Source (N)
4734 and then Nkind (Parent (N)) = N_Object_Renaming_Declaration
4735 and then Is_Class_Wide_Type (Etype (N))
4740 -- For other than entity names and compile time known values,
4741 -- check the node kind for special processing.
4745 -- An attribute reference is side effect free if its expressions
4746 -- are side effect free and its prefix is side effect free or
4747 -- is an entity reference.
4749 -- Is this right? what about x'first where x is a variable???
4751 when N_Attribute_Reference =>
4752 return Side_Effect_Free (Expressions (N))
4753 and then Attribute_Name (N) /= Name_Input
4754 and then (Is_Entity_Name (Prefix (N))
4755 or else Side_Effect_Free (Prefix (N)));
4757 -- A binary operator is side effect free if and both operands
4758 -- are side effect free. For this purpose binary operators
4759 -- include membership tests and short circuit forms
4761 when N_Binary_Op | N_Membership_Test | N_Short_Circuit =>
4762 return Side_Effect_Free (Left_Opnd (N))
4764 Side_Effect_Free (Right_Opnd (N));
4766 -- An explicit dereference is side effect free only if it is
4767 -- a side effect free prefixed reference.
4769 when N_Explicit_Dereference =>
4770 return Safe_Prefixed_Reference (N);
4772 -- A call to _rep_to_pos is side effect free, since we generate
4773 -- this pure function call ourselves. Moreover it is critically
4774 -- important to make this exception, since otherwise we can
4775 -- have discriminants in array components which don't look
4776 -- side effect free in the case of an array whose index type
4777 -- is an enumeration type with an enumeration rep clause.
4779 -- All other function calls are not side effect free
4781 when N_Function_Call =>
4782 return Nkind (Name (N)) = N_Identifier
4783 and then Is_TSS (Name (N), TSS_Rep_To_Pos)
4785 Side_Effect_Free (First (Parameter_Associations (N)));
4787 -- An indexed component is side effect free if it is a side
4788 -- effect free prefixed reference and all the indexing
4789 -- expressions are side effect free.
4791 when N_Indexed_Component =>
4792 return Side_Effect_Free (Expressions (N))
4793 and then Safe_Prefixed_Reference (N);
4795 -- A type qualification is side effect free if the expression
4796 -- is side effect free.
4798 when N_Qualified_Expression =>
4799 return Side_Effect_Free (Expression (N));
4801 -- A selected component is side effect free only if it is a
4802 -- side effect free prefixed reference. If it designates a
4803 -- component with a rep. clause it must be treated has having
4804 -- a potential side effect, because it may be modified through
4805 -- a renaming, and a subsequent use of the renaming as a macro
4806 -- will yield the wrong value. This complex interaction between
4807 -- renaming and removing side effects is a reminder that the
4808 -- latter has become a headache to maintain, and that it should
4809 -- be removed in favor of the gcc mechanism to capture values ???
4811 when N_Selected_Component =>
4812 if Nkind (Parent (N)) = N_Explicit_Dereference
4813 and then Has_Non_Standard_Rep (Designated_Type (Etype (N)))
4817 return Safe_Prefixed_Reference (N);
4820 -- A range is side effect free if the bounds are side effect free
4823 return Side_Effect_Free (Low_Bound (N))
4824 and then Side_Effect_Free (High_Bound (N));
4826 -- A slice is side effect free if it is a side effect free
4827 -- prefixed reference and the bounds are side effect free.
4830 return Side_Effect_Free (Discrete_Range (N))
4831 and then Safe_Prefixed_Reference (N);
4833 -- A type conversion is side effect free if the expression to be
4834 -- converted is side effect free.
4836 when N_Type_Conversion =>
4837 return Side_Effect_Free (Expression (N));
4839 -- A unary operator is side effect free if the operand
4840 -- is side effect free.
4843 return Side_Effect_Free (Right_Opnd (N));
4845 -- An unchecked type conversion is side effect free only if it
4846 -- is safe and its argument is side effect free.
4848 when N_Unchecked_Type_Conversion =>
4849 return Safe_Unchecked_Type_Conversion (N)
4850 and then Side_Effect_Free (Expression (N));
4852 -- An unchecked expression is side effect free if its expression
4853 -- is side effect free.
4855 when N_Unchecked_Expression =>
4856 return Side_Effect_Free (Expression (N));
4858 -- A literal is side effect free
4860 when N_Character_Literal |
4866 -- We consider that anything else has side effects. This is a bit
4867 -- crude, but we are pretty close for most common cases, and we
4868 -- are certainly correct (i.e. we never return True when the
4869 -- answer should be False).
4874 end Side_Effect_Free;
4876 -- A list is side effect free if all elements of the list are
4877 -- side effect free.
4879 function Side_Effect_Free (L : List_Id) return Boolean is
4883 if L = No_List or else L = Error_List then
4888 while Present (N) loop
4889 if not Side_Effect_Free (N) then
4898 end Side_Effect_Free;
4900 -------------------------
4901 -- Within_In_Parameter --
4902 -------------------------
4904 function Within_In_Parameter (N : Node_Id) return Boolean is
4906 if not Comes_From_Source (N) then
4909 elsif Is_Entity_Name (N) then
4910 return Ekind (Entity (N)) = E_In_Parameter;
4912 elsif Nkind (N) = N_Indexed_Component
4913 or else Nkind (N) = N_Selected_Component
4915 return Within_In_Parameter (Prefix (N));
4920 end Within_In_Parameter;
4922 -- Start of processing for Remove_Side_Effects
4925 -- If we are side effect free already or expansion is disabled,
4926 -- there is nothing to do.
4928 if Side_Effect_Free (Exp) or else not Expander_Active then
4932 -- All this must not have any checks
4934 Scope_Suppress := (others => True);
4936 -- If it is a scalar type and we need to capture the value, just make
4937 -- a copy. Likewise for a function call, an attribute reference, an
4938 -- allocator, or an operator. And if we have a volatile reference and
4939 -- Name_Req is not set (see comments above for Side_Effect_Free).
4941 if Is_Elementary_Type (Exp_Type)
4942 and then (Variable_Ref
4943 or else Nkind (Exp) = N_Function_Call
4944 or else Nkind (Exp) = N_Attribute_Reference
4945 or else Nkind (Exp) = N_Allocator
4946 or else Nkind (Exp) in N_Op
4947 or else (not Name_Req and then Is_Volatile_Reference (Exp)))
4949 Def_Id := Make_Temporary (Loc, 'R', Exp);
4950 Set_Etype (Def_Id, Exp_Type);
4951 Res := New_Reference_To (Def_Id, Loc);
4953 -- If the expression is a packed reference, it must be reanalyzed
4954 -- and expanded, depending on context. This is the case for actuals
4955 -- where a constraint check may capture the actual before expansion
4956 -- of the call is complete.
4958 if Nkind (Exp) = N_Indexed_Component
4959 and then Is_Packed (Etype (Prefix (Exp)))
4961 Set_Analyzed (Exp, False);
4962 Set_Analyzed (Prefix (Exp), False);
4966 Make_Object_Declaration (Loc,
4967 Defining_Identifier => Def_Id,
4968 Object_Definition => New_Reference_To (Exp_Type, Loc),
4969 Constant_Present => True,
4970 Expression => Relocate_Node (Exp));
4972 Set_Assignment_OK (E);
4973 Insert_Action (Exp, E);
4975 -- If the expression has the form v.all then we can just capture
4976 -- the pointer, and then do an explicit dereference on the result.
4978 elsif Nkind (Exp) = N_Explicit_Dereference then
4979 Def_Id := Make_Temporary (Loc, 'R', Exp);
4981 Make_Explicit_Dereference (Loc, New_Reference_To (Def_Id, Loc));
4984 Make_Object_Declaration (Loc,
4985 Defining_Identifier => Def_Id,
4986 Object_Definition =>
4987 New_Reference_To (Etype (Prefix (Exp)), Loc),
4988 Constant_Present => True,
4989 Expression => Relocate_Node (Prefix (Exp))));
4991 -- Similar processing for an unchecked conversion of an expression
4992 -- of the form v.all, where we want the same kind of treatment.
4994 elsif Nkind (Exp) = N_Unchecked_Type_Conversion
4995 and then Nkind (Expression (Exp)) = N_Explicit_Dereference
4997 Remove_Side_Effects (Expression (Exp), Name_Req, Variable_Ref);
4998 Scope_Suppress := Svg_Suppress;
5001 -- If this is a type conversion, leave the type conversion and remove
5002 -- the side effects in the expression. This is important in several
5003 -- circumstances: for change of representations, and also when this is
5004 -- a view conversion to a smaller object, where gigi can end up creating
5005 -- its own temporary of the wrong size.
5007 elsif Nkind (Exp) = N_Type_Conversion then
5008 Remove_Side_Effects (Expression (Exp), Name_Req, Variable_Ref);
5009 Scope_Suppress := Svg_Suppress;
5012 -- If this is an unchecked conversion that Gigi can't handle, make
5013 -- a copy or a use a renaming to capture the value.
5015 elsif Nkind (Exp) = N_Unchecked_Type_Conversion
5016 and then not Safe_Unchecked_Type_Conversion (Exp)
5018 if CW_Or_Has_Controlled_Part (Exp_Type) then
5020 -- Use a renaming to capture the expression, rather than create
5021 -- a controlled temporary.
5023 Def_Id := Make_Temporary (Loc, 'R', Exp);
5024 Res := New_Reference_To (Def_Id, Loc);
5027 Make_Object_Renaming_Declaration (Loc,
5028 Defining_Identifier => Def_Id,
5029 Subtype_Mark => New_Reference_To (Exp_Type, Loc),
5030 Name => Relocate_Node (Exp)));
5033 Def_Id := Make_Temporary (Loc, 'R', Exp);
5034 Set_Etype (Def_Id, Exp_Type);
5035 Res := New_Reference_To (Def_Id, Loc);
5038 Make_Object_Declaration (Loc,
5039 Defining_Identifier => Def_Id,
5040 Object_Definition => New_Reference_To (Exp_Type, Loc),
5041 Constant_Present => not Is_Variable (Exp),
5042 Expression => Relocate_Node (Exp));
5044 Set_Assignment_OK (E);
5045 Insert_Action (Exp, E);
5048 -- For expressions that denote objects, we can use a renaming scheme.
5049 -- This is needed for correctness in the case of a volatile object
5050 -- of a non-volatile type because the Make_Reference call of the
5051 -- "default" approach would generate an illegal access value (an access
5052 -- value cannot designate such an object - see Analyze_Reference).
5053 -- We skip using this scheme if we have an object of a volatile type
5054 -- and we do not have Name_Req set true (see comments above for
5055 -- Side_Effect_Free).
5057 elsif Is_Object_Reference (Exp)
5058 and then Nkind (Exp) /= N_Function_Call
5059 and then (Name_Req or else not Treat_As_Volatile (Exp_Type))
5061 Def_Id := Make_Temporary (Loc, 'R', Exp);
5063 if Nkind (Exp) = N_Selected_Component
5064 and then Nkind (Prefix (Exp)) = N_Function_Call
5065 and then Is_Array_Type (Exp_Type)
5067 -- Avoid generating a variable-sized temporary, by generating
5068 -- the renaming declaration just for the function call. The
5069 -- transformation could be refined to apply only when the array
5070 -- component is constrained by a discriminant???
5073 Make_Selected_Component (Loc,
5074 Prefix => New_Occurrence_Of (Def_Id, Loc),
5075 Selector_Name => Selector_Name (Exp));
5078 Make_Object_Renaming_Declaration (Loc,
5079 Defining_Identifier => Def_Id,
5081 New_Reference_To (Base_Type (Etype (Prefix (Exp))), Loc),
5082 Name => Relocate_Node (Prefix (Exp))));
5085 Res := New_Reference_To (Def_Id, Loc);
5088 Make_Object_Renaming_Declaration (Loc,
5089 Defining_Identifier => Def_Id,
5090 Subtype_Mark => New_Reference_To (Exp_Type, Loc),
5091 Name => Relocate_Node (Exp)));
5094 -- If this is a packed reference, or a selected component with a
5095 -- non-standard representation, a reference to the temporary will
5096 -- be replaced by a copy of the original expression (see
5097 -- Exp_Ch2.Expand_Renaming). Otherwise the temporary must be
5098 -- elaborated by gigi, and is of course not to be replaced in-line
5099 -- by the expression it renames, which would defeat the purpose of
5100 -- removing the side-effect.
5102 if (Nkind (Exp) = N_Selected_Component
5103 or else Nkind (Exp) = N_Indexed_Component)
5104 and then Has_Non_Standard_Rep (Etype (Prefix (Exp)))
5108 Set_Is_Renaming_Of_Object (Def_Id, False);
5111 -- Otherwise we generate a reference to the value
5114 -- Special processing for function calls that return a limited type.
5115 -- We need to build a declaration that will enable build-in-place
5116 -- expansion of the call. This is not done if the context is already
5117 -- an object declaration, to prevent infinite recursion.
5119 -- This is relevant only in Ada 2005 mode. In Ada 95 programs we have
5120 -- to accommodate functions returning limited objects by reference.
5122 if Nkind (Exp) = N_Function_Call
5123 and then Is_Immutably_Limited_Type (Etype (Exp))
5124 and then Nkind (Parent (Exp)) /= N_Object_Declaration
5125 and then Ada_Version >= Ada_2005
5128 Obj : constant Entity_Id := Make_Temporary (Loc, 'F', Exp);
5133 Make_Object_Declaration (Loc,
5134 Defining_Identifier => Obj,
5135 Object_Definition => New_Occurrence_Of (Exp_Type, Loc),
5136 Expression => Relocate_Node (Exp));
5138 Insert_Action (Exp, Decl);
5139 Set_Etype (Obj, Exp_Type);
5140 Rewrite (Exp, New_Occurrence_Of (Obj, Loc));
5145 Ref_Type := Make_Temporary (Loc, 'A');
5148 Make_Full_Type_Declaration (Loc,
5149 Defining_Identifier => Ref_Type,
5151 Make_Access_To_Object_Definition (Loc,
5152 All_Present => True,
5153 Subtype_Indication =>
5154 New_Reference_To (Exp_Type, Loc)));
5157 Insert_Action (Exp, Ptr_Typ_Decl);
5159 Def_Id := Make_Temporary (Loc, 'R', Exp);
5160 Set_Etype (Def_Id, Exp_Type);
5163 Make_Explicit_Dereference (Loc,
5164 Prefix => New_Reference_To (Def_Id, Loc));
5166 if Nkind (E) = N_Explicit_Dereference then
5167 New_Exp := Relocate_Node (Prefix (E));
5169 E := Relocate_Node (E);
5170 New_Exp := Make_Reference (Loc, E);
5173 if Is_Delayed_Aggregate (E) then
5175 -- The expansion of nested aggregates is delayed until the
5176 -- enclosing aggregate is expanded. As aggregates are often
5177 -- qualified, the predicate applies to qualified expressions
5178 -- as well, indicating that the enclosing aggregate has not
5179 -- been expanded yet. At this point the aggregate is part of
5180 -- a stand-alone declaration, and must be fully expanded.
5182 if Nkind (E) = N_Qualified_Expression then
5183 Set_Expansion_Delayed (Expression (E), False);
5184 Set_Analyzed (Expression (E), False);
5186 Set_Expansion_Delayed (E, False);
5189 Set_Analyzed (E, False);
5193 Make_Object_Declaration (Loc,
5194 Defining_Identifier => Def_Id,
5195 Object_Definition => New_Reference_To (Ref_Type, Loc),
5196 Constant_Present => True,
5197 Expression => New_Exp));
5200 -- Preserve the Assignment_OK flag in all copies, since at least
5201 -- one copy may be used in a context where this flag must be set
5202 -- (otherwise why would the flag be set in the first place).
5204 Set_Assignment_OK (Res, Assignment_OK (Exp));
5206 -- Finally rewrite the original expression and we are done
5209 Analyze_And_Resolve (Exp, Exp_Type);
5210 Scope_Suppress := Svg_Suppress;
5211 end Remove_Side_Effects;
5213 ---------------------------
5214 -- Represented_As_Scalar --
5215 ---------------------------
5217 function Represented_As_Scalar (T : Entity_Id) return Boolean is
5218 UT : constant Entity_Id := Underlying_Type (T);
5220 return Is_Scalar_Type (UT)
5221 or else (Is_Bit_Packed_Array (UT)
5222 and then Is_Scalar_Type (Packed_Array_Type (UT)));
5223 end Represented_As_Scalar;
5225 ------------------------------------
5226 -- Safe_Unchecked_Type_Conversion --
5227 ------------------------------------
5229 -- Note: this function knows quite a bit about the exact requirements
5230 -- of Gigi with respect to unchecked type conversions, and its code
5231 -- must be coordinated with any changes in Gigi in this area.
5233 -- The above requirements should be documented in Sinfo ???
5235 function Safe_Unchecked_Type_Conversion (Exp : Node_Id) return Boolean is
5240 Pexp : constant Node_Id := Parent (Exp);
5243 -- If the expression is the RHS of an assignment or object declaration
5244 -- we are always OK because there will always be a target.
5246 -- Object renaming declarations, (generated for view conversions of
5247 -- actuals in inlined calls), like object declarations, provide an
5248 -- explicit type, and are safe as well.
5250 if (Nkind (Pexp) = N_Assignment_Statement
5251 and then Expression (Pexp) = Exp)
5252 or else Nkind (Pexp) = N_Object_Declaration
5253 or else Nkind (Pexp) = N_Object_Renaming_Declaration
5257 -- If the expression is the prefix of an N_Selected_Component
5258 -- we should also be OK because GCC knows to look inside the
5259 -- conversion except if the type is discriminated. We assume
5260 -- that we are OK anyway if the type is not set yet or if it is
5261 -- controlled since we can't afford to introduce a temporary in
5264 elsif Nkind (Pexp) = N_Selected_Component
5265 and then Prefix (Pexp) = Exp
5267 if No (Etype (Pexp)) then
5271 not Has_Discriminants (Etype (Pexp))
5272 or else Is_Constrained (Etype (Pexp));
5276 -- Set the output type, this comes from Etype if it is set, otherwise
5277 -- we take it from the subtype mark, which we assume was already
5280 if Present (Etype (Exp)) then
5281 Otyp := Etype (Exp);
5283 Otyp := Entity (Subtype_Mark (Exp));
5286 -- The input type always comes from the expression, and we assume
5287 -- this is indeed always analyzed, so we can simply get the Etype.
5289 Ityp := Etype (Expression (Exp));
5291 -- Initialize alignments to unknown so far
5296 -- Replace a concurrent type by its corresponding record type
5297 -- and each type by its underlying type and do the tests on those.
5298 -- The original type may be a private type whose completion is a
5299 -- concurrent type, so find the underlying type first.
5301 if Present (Underlying_Type (Otyp)) then
5302 Otyp := Underlying_Type (Otyp);
5305 if Present (Underlying_Type (Ityp)) then
5306 Ityp := Underlying_Type (Ityp);
5309 if Is_Concurrent_Type (Otyp) then
5310 Otyp := Corresponding_Record_Type (Otyp);
5313 if Is_Concurrent_Type (Ityp) then
5314 Ityp := Corresponding_Record_Type (Ityp);
5317 -- If the base types are the same, we know there is no problem since
5318 -- this conversion will be a noop.
5320 if Implementation_Base_Type (Otyp) = Implementation_Base_Type (Ityp) then
5323 -- Same if this is an upwards conversion of an untagged type, and there
5324 -- are no constraints involved (could be more general???)
5326 elsif Etype (Ityp) = Otyp
5327 and then not Is_Tagged_Type (Ityp)
5328 and then not Has_Discriminants (Ityp)
5329 and then No (First_Rep_Item (Base_Type (Ityp)))
5333 -- If the expression has an access type (object or subprogram) we
5334 -- assume that the conversion is safe, because the size of the target
5335 -- is safe, even if it is a record (which might be treated as having
5336 -- unknown size at this point).
5338 elsif Is_Access_Type (Ityp) then
5341 -- If the size of output type is known at compile time, there is
5342 -- never a problem. Note that unconstrained records are considered
5343 -- to be of known size, but we can't consider them that way here,
5344 -- because we are talking about the actual size of the object.
5346 -- We also make sure that in addition to the size being known, we do
5347 -- not have a case which might generate an embarrassingly large temp
5348 -- in stack checking mode.
5350 elsif Size_Known_At_Compile_Time (Otyp)
5352 (not Stack_Checking_Enabled
5353 or else not May_Generate_Large_Temp (Otyp))
5354 and then not (Is_Record_Type (Otyp) and then not Is_Constrained (Otyp))
5358 -- If either type is tagged, then we know the alignment is OK so
5359 -- Gigi will be able to use pointer punning.
5361 elsif Is_Tagged_Type (Otyp) or else Is_Tagged_Type (Ityp) then
5364 -- If either type is a limited record type, we cannot do a copy, so
5365 -- say safe since there's nothing else we can do.
5367 elsif Is_Limited_Record (Otyp) or else Is_Limited_Record (Ityp) then
5370 -- Conversions to and from packed array types are always ignored and
5373 elsif Is_Packed_Array_Type (Otyp)
5374 or else Is_Packed_Array_Type (Ityp)
5379 -- The only other cases known to be safe is if the input type's
5380 -- alignment is known to be at least the maximum alignment for the
5381 -- target or if both alignments are known and the output type's
5382 -- alignment is no stricter than the input's. We can use the alignment
5383 -- of the component type of an array if a type is an unpacked
5386 if Present (Alignment_Clause (Otyp)) then
5387 Oalign := Expr_Value (Expression (Alignment_Clause (Otyp)));
5389 elsif Is_Array_Type (Otyp)
5390 and then Present (Alignment_Clause (Component_Type (Otyp)))
5392 Oalign := Expr_Value (Expression (Alignment_Clause
5393 (Component_Type (Otyp))));
5396 if Present (Alignment_Clause (Ityp)) then
5397 Ialign := Expr_Value (Expression (Alignment_Clause (Ityp)));
5399 elsif Is_Array_Type (Ityp)
5400 and then Present (Alignment_Clause (Component_Type (Ityp)))
5402 Ialign := Expr_Value (Expression (Alignment_Clause
5403 (Component_Type (Ityp))));
5406 if Ialign /= No_Uint and then Ialign > Maximum_Alignment then
5409 elsif Ialign /= No_Uint and then Oalign /= No_Uint
5410 and then Ialign <= Oalign
5414 -- Otherwise, Gigi cannot handle this and we must make a temporary
5419 end Safe_Unchecked_Type_Conversion;
5421 ---------------------------------
5422 -- Set_Current_Value_Condition --
5423 ---------------------------------
5425 -- Note: the implementation of this procedure is very closely tied to the
5426 -- implementation of Get_Current_Value_Condition. Here we set required
5427 -- Current_Value fields, and in Get_Current_Value_Condition, we interpret
5428 -- them, so they must have a consistent view.
5430 procedure Set_Current_Value_Condition (Cnode : Node_Id) is
5432 procedure Set_Entity_Current_Value (N : Node_Id);
5433 -- If N is an entity reference, where the entity is of an appropriate
5434 -- kind, then set the current value of this entity to Cnode, unless
5435 -- there is already a definite value set there.
5437 procedure Set_Expression_Current_Value (N : Node_Id);
5438 -- If N is of an appropriate form, sets an appropriate entry in current
5439 -- value fields of relevant entities. Multiple entities can be affected
5440 -- in the case of an AND or AND THEN.
5442 ------------------------------
5443 -- Set_Entity_Current_Value --
5444 ------------------------------
5446 procedure Set_Entity_Current_Value (N : Node_Id) is
5448 if Is_Entity_Name (N) then
5450 Ent : constant Entity_Id := Entity (N);
5453 -- Don't capture if not safe to do so
5455 if not Safe_To_Capture_Value (N, Ent, Cond => True) then
5459 -- Here we have a case where the Current_Value field may
5460 -- need to be set. We set it if it is not already set to a
5461 -- compile time expression value.
5463 -- Note that this represents a decision that one condition
5464 -- blots out another previous one. That's certainly right
5465 -- if they occur at the same level. If the second one is
5466 -- nested, then the decision is neither right nor wrong (it
5467 -- would be equally OK to leave the outer one in place, or
5468 -- take the new inner one. Really we should record both, but
5469 -- our data structures are not that elaborate.
5471 if Nkind (Current_Value (Ent)) not in N_Subexpr then
5472 Set_Current_Value (Ent, Cnode);
5476 end Set_Entity_Current_Value;
5478 ----------------------------------
5479 -- Set_Expression_Current_Value --
5480 ----------------------------------
5482 procedure Set_Expression_Current_Value (N : Node_Id) is
5488 -- Loop to deal with (ignore for now) any NOT operators present. The
5489 -- presence of NOT operators will be handled properly when we call
5490 -- Get_Current_Value_Condition.
5492 while Nkind (Cond) = N_Op_Not loop
5493 Cond := Right_Opnd (Cond);
5496 -- For an AND or AND THEN, recursively process operands
5498 if Nkind (Cond) = N_Op_And or else Nkind (Cond) = N_And_Then then
5499 Set_Expression_Current_Value (Left_Opnd (Cond));
5500 Set_Expression_Current_Value (Right_Opnd (Cond));
5504 -- Check possible relational operator
5506 if Nkind (Cond) in N_Op_Compare then
5507 if Compile_Time_Known_Value (Right_Opnd (Cond)) then
5508 Set_Entity_Current_Value (Left_Opnd (Cond));
5509 elsif Compile_Time_Known_Value (Left_Opnd (Cond)) then
5510 Set_Entity_Current_Value (Right_Opnd (Cond));
5513 -- Check possible boolean variable reference
5516 Set_Entity_Current_Value (Cond);
5518 end Set_Expression_Current_Value;
5520 -- Start of processing for Set_Current_Value_Condition
5523 Set_Expression_Current_Value (Condition (Cnode));
5524 end Set_Current_Value_Condition;
5526 --------------------------
5527 -- Set_Elaboration_Flag --
5528 --------------------------
5530 procedure Set_Elaboration_Flag (N : Node_Id; Spec_Id : Entity_Id) is
5531 Loc : constant Source_Ptr := Sloc (N);
5532 Ent : constant Entity_Id := Elaboration_Entity (Spec_Id);
5536 if Present (Ent) then
5538 -- Nothing to do if at the compilation unit level, because in this
5539 -- case the flag is set by the binder generated elaboration routine.
5541 if Nkind (Parent (N)) = N_Compilation_Unit then
5544 -- Here we do need to generate an assignment statement
5547 Check_Restriction (No_Elaboration_Code, N);
5549 Make_Assignment_Statement (Loc,
5550 Name => New_Occurrence_Of (Ent, Loc),
5551 Expression => New_Occurrence_Of (Standard_True, Loc));
5553 if Nkind (Parent (N)) = N_Subunit then
5554 Insert_After (Corresponding_Stub (Parent (N)), Asn);
5556 Insert_After (N, Asn);
5561 -- Kill current value indication. This is necessary because the
5562 -- tests of this flag are inserted out of sequence and must not
5563 -- pick up bogus indications of the wrong constant value.
5565 Set_Current_Value (Ent, Empty);
5568 end Set_Elaboration_Flag;
5570 ----------------------------
5571 -- Set_Renamed_Subprogram --
5572 ----------------------------
5574 procedure Set_Renamed_Subprogram (N : Node_Id; E : Entity_Id) is
5576 -- If input node is an identifier, we can just reset it
5578 if Nkind (N) = N_Identifier then
5579 Set_Chars (N, Chars (E));
5582 -- Otherwise we have to do a rewrite, preserving Comes_From_Source
5586 CS : constant Boolean := Comes_From_Source (N);
5588 Rewrite (N, Make_Identifier (Sloc (N), Chars => Chars (E)));
5590 Set_Comes_From_Source (N, CS);
5591 Set_Analyzed (N, True);
5594 end Set_Renamed_Subprogram;
5596 ----------------------------------
5597 -- Silly_Boolean_Array_Not_Test --
5598 ----------------------------------
5600 -- This procedure implements an odd and silly test. We explicitly check
5601 -- for the case where the 'First of the component type is equal to the
5602 -- 'Last of this component type, and if this is the case, we make sure
5603 -- that constraint error is raised. The reason is that the NOT is bound
5604 -- to cause CE in this case, and we will not otherwise catch it.
5606 -- No such check is required for AND and OR, since for both these cases
5607 -- False op False = False, and True op True = True. For the XOR case,
5608 -- see Silly_Boolean_Array_Xor_Test.
5610 -- Believe it or not, this was reported as a bug. Note that nearly
5611 -- always, the test will evaluate statically to False, so the code will
5612 -- be statically removed, and no extra overhead caused.
5614 procedure Silly_Boolean_Array_Not_Test (N : Node_Id; T : Entity_Id) is
5615 Loc : constant Source_Ptr := Sloc (N);
5616 CT : constant Entity_Id := Component_Type (T);
5619 -- The check we install is
5621 -- constraint_error when
5622 -- component_type'first = component_type'last
5623 -- and then array_type'Length /= 0)
5625 -- We need the last guard because we don't want to raise CE for empty
5626 -- arrays since no out of range values result. (Empty arrays with a
5627 -- component type of True .. True -- very useful -- even the ACATS
5628 -- does not test that marginal case!)
5631 Make_Raise_Constraint_Error (Loc,
5637 Make_Attribute_Reference (Loc,
5638 Prefix => New_Occurrence_Of (CT, Loc),
5639 Attribute_Name => Name_First),
5642 Make_Attribute_Reference (Loc,
5643 Prefix => New_Occurrence_Of (CT, Loc),
5644 Attribute_Name => Name_Last)),
5646 Right_Opnd => Make_Non_Empty_Check (Loc, Right_Opnd (N))),
5647 Reason => CE_Range_Check_Failed));
5648 end Silly_Boolean_Array_Not_Test;
5650 ----------------------------------
5651 -- Silly_Boolean_Array_Xor_Test --
5652 ----------------------------------
5654 -- This procedure implements an odd and silly test. We explicitly check
5655 -- for the XOR case where the component type is True .. True, since this
5656 -- will raise constraint error. A special check is required since CE
5657 -- will not be generated otherwise (cf Expand_Packed_Not).
5659 -- No such check is required for AND and OR, since for both these cases
5660 -- False op False = False, and True op True = True, and no check is
5661 -- required for the case of False .. False, since False xor False = False.
5662 -- See also Silly_Boolean_Array_Not_Test
5664 procedure Silly_Boolean_Array_Xor_Test (N : Node_Id; T : Entity_Id) is
5665 Loc : constant Source_Ptr := Sloc (N);
5666 CT : constant Entity_Id := Component_Type (T);
5669 -- The check we install is
5671 -- constraint_error when
5672 -- Boolean (component_type'First)
5673 -- and then Boolean (component_type'Last)
5674 -- and then array_type'Length /= 0)
5676 -- We need the last guard because we don't want to raise CE for empty
5677 -- arrays since no out of range values result (Empty arrays with a
5678 -- component type of True .. True -- very useful -- even the ACATS
5679 -- does not test that marginal case!).
5682 Make_Raise_Constraint_Error (Loc,
5688 Convert_To (Standard_Boolean,
5689 Make_Attribute_Reference (Loc,
5690 Prefix => New_Occurrence_Of (CT, Loc),
5691 Attribute_Name => Name_First)),
5694 Convert_To (Standard_Boolean,
5695 Make_Attribute_Reference (Loc,
5696 Prefix => New_Occurrence_Of (CT, Loc),
5697 Attribute_Name => Name_Last))),
5699 Right_Opnd => Make_Non_Empty_Check (Loc, Right_Opnd (N))),
5700 Reason => CE_Range_Check_Failed));
5701 end Silly_Boolean_Array_Xor_Test;
5703 --------------------------
5704 -- Target_Has_Fixed_Ops --
5705 --------------------------
5707 Integer_Sized_Small : Ureal;
5708 -- Set to 2.0 ** -(Integer'Size - 1) the first time that this
5709 -- function is called (we don't want to compute it more than once!)
5711 Long_Integer_Sized_Small : Ureal;
5712 -- Set to 2.0 ** -(Long_Integer'Size - 1) the first time that this
5713 -- function is called (we don't want to compute it more than once)
5715 First_Time_For_THFO : Boolean := True;
5716 -- Set to False after first call (if Fractional_Fixed_Ops_On_Target)
5718 function Target_Has_Fixed_Ops
5719 (Left_Typ : Entity_Id;
5720 Right_Typ : Entity_Id;
5721 Result_Typ : Entity_Id) return Boolean
5723 function Is_Fractional_Type (Typ : Entity_Id) return Boolean;
5724 -- Return True if the given type is a fixed-point type with a small
5725 -- value equal to 2 ** (-(T'Object_Size - 1)) and whose values have
5726 -- an absolute value less than 1.0. This is currently limited
5727 -- to fixed-point types that map to Integer or Long_Integer.
5729 ------------------------
5730 -- Is_Fractional_Type --
5731 ------------------------
5733 function Is_Fractional_Type (Typ : Entity_Id) return Boolean is
5735 if Esize (Typ) = Standard_Integer_Size then
5736 return Small_Value (Typ) = Integer_Sized_Small;
5738 elsif Esize (Typ) = Standard_Long_Integer_Size then
5739 return Small_Value (Typ) = Long_Integer_Sized_Small;
5744 end Is_Fractional_Type;
5746 -- Start of processing for Target_Has_Fixed_Ops
5749 -- Return False if Fractional_Fixed_Ops_On_Target is false
5751 if not Fractional_Fixed_Ops_On_Target then
5755 -- Here the target has Fractional_Fixed_Ops, if first time, compute
5756 -- standard constants used by Is_Fractional_Type.
5758 if First_Time_For_THFO then
5759 First_Time_For_THFO := False;
5761 Integer_Sized_Small :=
5764 Den => UI_From_Int (Standard_Integer_Size - 1),
5767 Long_Integer_Sized_Small :=
5770 Den => UI_From_Int (Standard_Long_Integer_Size - 1),
5774 -- Return True if target supports fixed-by-fixed multiply/divide
5775 -- for fractional fixed-point types (see Is_Fractional_Type) and
5776 -- the operand and result types are equivalent fractional types.
5778 return Is_Fractional_Type (Base_Type (Left_Typ))
5779 and then Is_Fractional_Type (Base_Type (Right_Typ))
5780 and then Is_Fractional_Type (Base_Type (Result_Typ))
5781 and then Esize (Left_Typ) = Esize (Right_Typ)
5782 and then Esize (Left_Typ) = Esize (Result_Typ);
5783 end Target_Has_Fixed_Ops;
5785 ------------------------------------------
5786 -- Type_May_Have_Bit_Aligned_Components --
5787 ------------------------------------------
5789 function Type_May_Have_Bit_Aligned_Components
5790 (Typ : Entity_Id) return Boolean
5793 -- Array type, check component type
5795 if Is_Array_Type (Typ) then
5797 Type_May_Have_Bit_Aligned_Components (Component_Type (Typ));
5799 -- Record type, check components
5801 elsif Is_Record_Type (Typ) then
5806 E := First_Component_Or_Discriminant (Typ);
5807 while Present (E) loop
5808 if Component_May_Be_Bit_Aligned (E)
5809 or else Type_May_Have_Bit_Aligned_Components (Etype (E))
5814 Next_Component_Or_Discriminant (E);
5820 -- Type other than array or record is always OK
5825 end Type_May_Have_Bit_Aligned_Components;
5827 ----------------------------
5828 -- Wrap_Cleanup_Procedure --
5829 ----------------------------
5831 procedure Wrap_Cleanup_Procedure (N : Node_Id) is
5832 Loc : constant Source_Ptr := Sloc (N);
5833 Stseq : constant Node_Id := Handled_Statement_Sequence (N);
5834 Stmts : constant List_Id := Statements (Stseq);
5837 if Abort_Allowed then
5838 Prepend_To (Stmts, Build_Runtime_Call (Loc, RE_Abort_Defer));
5839 Append_To (Stmts, Build_Runtime_Call (Loc, RE_Abort_Undefer));
5841 end Wrap_Cleanup_Procedure;