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 Checks; use Checks;
28 with Debug; use Debug;
29 with Einfo; use Einfo;
30 with Elists; use Elists;
31 with Errout; use Errout;
32 with Exp_Aggr; use Exp_Aggr;
33 with Exp_Ch6; use Exp_Ch6;
34 with Exp_Ch7; use Exp_Ch7;
35 with Inline; use Inline;
36 with Itypes; use Itypes;
38 with Nlists; use Nlists;
39 with Nmake; use Nmake;
41 with Restrict; use Restrict;
42 with Rident; use Rident;
44 with Sem_Aux; use Sem_Aux;
45 with Sem_Ch8; use Sem_Ch8;
46 with Sem_Eval; use Sem_Eval;
47 with Sem_Res; use Sem_Res;
48 with Sem_Type; use Sem_Type;
49 with Sem_Util; use Sem_Util;
50 with Snames; use Snames;
51 with Stand; use Stand;
52 with Stringt; use Stringt;
53 with Targparm; use Targparm;
54 with Tbuild; use Tbuild;
55 with Ttypes; use Ttypes;
56 with Uintp; use Uintp;
57 with Urealp; use Urealp;
58 with Validsw; use Validsw;
60 package body Exp_Util is
62 -----------------------
63 -- Local Subprograms --
64 -----------------------
66 function Build_Task_Array_Image
70 Dyn : Boolean := False) return Node_Id;
71 -- Build function to generate the image string for a task that is an
72 -- array component, concatenating the images of each index. To avoid
73 -- storage leaks, the string is built with successive slice assignments.
74 -- The flag Dyn indicates whether this is called for the initialization
75 -- procedure of an array of tasks, or for the name of a dynamically
76 -- created task that is assigned to an indexed component.
78 function Build_Task_Image_Function
82 Res : Entity_Id) return Node_Id;
83 -- Common processing for Task_Array_Image and Task_Record_Image.
84 -- Build function body that computes image.
86 procedure Build_Task_Image_Prefix
95 -- Common processing for Task_Array_Image and Task_Record_Image.
96 -- Create local variables and assign prefix of name to result string.
98 function Build_Task_Record_Image
101 Dyn : Boolean := False) return Node_Id;
102 -- Build function to generate the image string for a task that is a
103 -- record component. Concatenate name of variable with that of selector.
104 -- The flag Dyn indicates whether this is called for the initialization
105 -- procedure of record with task components, or for a dynamically
106 -- created task that is assigned to a selected component.
108 function Make_CW_Equivalent_Type
110 E : Node_Id) return Entity_Id;
111 -- T is a class-wide type entity, E is the initial expression node that
112 -- constrains T in case such as: " X: T := E" or "new T'(E)"
113 -- This function returns the entity of the Equivalent type and inserts
114 -- on the fly the necessary declaration such as:
116 -- type anon is record
117 -- _parent : Root_Type (T); constrained with E discriminants (if any)
118 -- Extension : String (1 .. expr to match size of E);
121 -- This record is compatible with any object of the class of T thanks
122 -- to the first field and has the same size as E thanks to the second.
124 function Make_Literal_Range
126 Literal_Typ : Entity_Id) return Node_Id;
127 -- Produce a Range node whose bounds are:
128 -- Low_Bound (Literal_Type) ..
129 -- Low_Bound (Literal_Type) + (Length (Literal_Typ) - 1)
130 -- this is used for expanding declarations like X : String := "sdfgdfg";
132 -- If the index type of the target array is not integer, we generate:
133 -- Low_Bound (Literal_Type) ..
135 -- (Literal_Type'Pos (Low_Bound (Literal_Type))
136 -- + (Length (Literal_Typ) -1))
138 function Make_Non_Empty_Check
140 N : Node_Id) return Node_Id;
141 -- Produce a boolean expression checking that the unidimensional array
142 -- node N is not empty.
144 function New_Class_Wide_Subtype
146 N : Node_Id) return Entity_Id;
147 -- Create an implicit subtype of CW_Typ attached to node N
149 ----------------------
150 -- Adjust_Condition --
151 ----------------------
153 procedure Adjust_Condition (N : Node_Id) is
160 Loc : constant Source_Ptr := Sloc (N);
161 T : constant Entity_Id := Etype (N);
165 -- For now, we simply ignore a call where the argument has no
166 -- type (probably case of unanalyzed condition), or has a type
167 -- that is not Boolean. This is because this is a pretty marginal
168 -- piece of functionality, and violations of these rules are
169 -- likely to be truly marginal (how much code uses Fortran Logical
170 -- as the barrier to a protected entry?) and we do not want to
171 -- blow up existing programs. We can change this to an assertion
172 -- after 3.12a is released ???
174 if No (T) or else not Is_Boolean_Type (T) then
178 -- Apply validity checking if needed
180 if Validity_Checks_On and Validity_Check_Tests then
184 -- Immediate return if standard boolean, the most common case,
185 -- where nothing needs to be done.
187 if Base_Type (T) = Standard_Boolean then
191 -- Case of zero/non-zero semantics or non-standard enumeration
192 -- representation. In each case, we rewrite the node as:
194 -- ityp!(N) /= False'Enum_Rep
196 -- where ityp is an integer type with large enough size to hold
197 -- any value of type T.
199 if Nonzero_Is_True (T) or else Has_Non_Standard_Rep (T) then
200 if Esize (T) <= Esize (Standard_Integer) then
201 Ti := Standard_Integer;
203 Ti := Standard_Long_Long_Integer;
208 Left_Opnd => Unchecked_Convert_To (Ti, N),
210 Make_Attribute_Reference (Loc,
211 Attribute_Name => Name_Enum_Rep,
213 New_Occurrence_Of (First_Literal (T), Loc))));
214 Analyze_And_Resolve (N, Standard_Boolean);
217 Rewrite (N, Convert_To (Standard_Boolean, N));
218 Analyze_And_Resolve (N, Standard_Boolean);
221 end Adjust_Condition;
223 ------------------------
224 -- Adjust_Result_Type --
225 ------------------------
227 procedure Adjust_Result_Type (N : Node_Id; T : Entity_Id) is
229 -- Ignore call if current type is not Standard.Boolean
231 if Etype (N) /= Standard_Boolean then
235 -- If result is already of correct type, nothing to do. Note that
236 -- this will get the most common case where everything has a type
237 -- of Standard.Boolean.
239 if Base_Type (T) = Standard_Boolean then
244 KP : constant Node_Kind := Nkind (Parent (N));
247 -- If result is to be used as a Condition in the syntax, no need
248 -- to convert it back, since if it was changed to Standard.Boolean
249 -- using Adjust_Condition, that is just fine for this usage.
251 if KP in N_Raise_xxx_Error or else KP in N_Has_Condition then
254 -- If result is an operand of another logical operation, no need
255 -- to reset its type, since Standard.Boolean is just fine, and
256 -- such operations always do Adjust_Condition on their operands.
258 elsif KP in N_Op_Boolean
259 or else KP in N_Short_Circuit
260 or else KP = N_Op_Not
264 -- Otherwise we perform a conversion from the current type,
265 -- which must be Standard.Boolean, to the desired type.
269 Rewrite (N, Convert_To (T, N));
270 Analyze_And_Resolve (N, T);
274 end Adjust_Result_Type;
276 --------------------------
277 -- Append_Freeze_Action --
278 --------------------------
280 procedure Append_Freeze_Action (T : Entity_Id; N : Node_Id) is
284 Ensure_Freeze_Node (T);
285 Fnode := Freeze_Node (T);
287 if No (Actions (Fnode)) then
288 Set_Actions (Fnode, New_List);
291 Append (N, Actions (Fnode));
292 end Append_Freeze_Action;
294 ---------------------------
295 -- Append_Freeze_Actions --
296 ---------------------------
298 procedure Append_Freeze_Actions (T : Entity_Id; L : List_Id) is
299 Fnode : constant Node_Id := Freeze_Node (T);
306 if No (Actions (Fnode)) then
307 Set_Actions (Fnode, L);
309 Append_List (L, Actions (Fnode));
312 end Append_Freeze_Actions;
314 ------------------------
315 -- Build_Runtime_Call --
316 ------------------------
318 function Build_Runtime_Call (Loc : Source_Ptr; RE : RE_Id) return Node_Id is
320 -- If entity is not available, we can skip making the call (this avoids
321 -- junk duplicated error messages in a number of cases).
323 if not RTE_Available (RE) then
324 return Make_Null_Statement (Loc);
327 Make_Procedure_Call_Statement (Loc,
328 Name => New_Reference_To (RTE (RE), Loc));
330 end Build_Runtime_Call;
332 ----------------------------
333 -- Build_Task_Array_Image --
334 ----------------------------
336 -- This function generates the body for a function that constructs the
337 -- image string for a task that is an array component. The function is
338 -- local to the init proc for the array type, and is called for each one
339 -- of the components. The constructed image has the form of an indexed
340 -- component, whose prefix is the outer variable of the array type.
341 -- The n-dimensional array type has known indices Index, Index2...
342 -- Id_Ref is an indexed component form created by the enclosing init proc.
343 -- Its successive indices are Val1, Val2, ... which are the loop variables
344 -- in the loops that call the individual task init proc on each component.
346 -- The generated function has the following structure:
348 -- function F return String is
349 -- Pref : string renames Task_Name;
350 -- T1 : String := Index1'Image (Val1);
352 -- Tn : String := indexn'image (Valn);
353 -- Len : Integer := T1'Length + ... + Tn'Length + n + 1;
354 -- -- Len includes commas and the end parentheses.
355 -- Res : String (1..Len);
356 -- Pos : Integer := Pref'Length;
359 -- Res (1 .. Pos) := Pref;
363 -- Res (Pos .. Pos + T1'Length - 1) := T1;
364 -- Pos := Pos + T1'Length;
368 -- Res (Pos .. Pos + Tn'Length - 1) := Tn;
374 -- Needless to say, multidimensional arrays of tasks are rare enough
375 -- that the bulkiness of this code is not really a concern.
377 function Build_Task_Array_Image
381 Dyn : Boolean := False) return Node_Id
383 Dims : constant Nat := Number_Dimensions (A_Type);
384 -- Number of dimensions for array of tasks
386 Temps : array (1 .. Dims) of Entity_Id;
387 -- Array of temporaries to hold string for each index
393 -- Total length of generated name
396 -- Running index for substring assignments
398 Pref : constant Entity_Id := Make_Temporary (Loc, 'P');
399 -- Name of enclosing variable, prefix of resulting name
402 -- String to hold result
405 -- Value of successive indices
408 -- Expression to compute total size of string
411 -- Entity for name at one index position
413 Decls : constant List_Id := New_List;
414 Stats : constant List_Id := New_List;
417 -- For a dynamic task, the name comes from the target variable.
418 -- For a static one it is a formal of the enclosing init proc.
421 Get_Name_String (Chars (Entity (Prefix (Id_Ref))));
423 Make_Object_Declaration (Loc,
424 Defining_Identifier => Pref,
425 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
427 Make_String_Literal (Loc,
428 Strval => String_From_Name_Buffer)));
432 Make_Object_Renaming_Declaration (Loc,
433 Defining_Identifier => Pref,
434 Subtype_Mark => New_Occurrence_Of (Standard_String, Loc),
435 Name => Make_Identifier (Loc, Name_uTask_Name)));
438 Indx := First_Index (A_Type);
439 Val := First (Expressions (Id_Ref));
441 for J in 1 .. Dims loop
442 T := Make_Temporary (Loc, 'T');
446 Make_Object_Declaration (Loc,
447 Defining_Identifier => T,
448 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
450 Make_Attribute_Reference (Loc,
451 Attribute_Name => Name_Image,
452 Prefix => New_Occurrence_Of (Etype (Indx), Loc),
453 Expressions => New_List (New_Copy_Tree (Val)))));
459 Sum := Make_Integer_Literal (Loc, Dims + 1);
465 Make_Attribute_Reference (Loc,
466 Attribute_Name => Name_Length,
468 New_Occurrence_Of (Pref, Loc),
469 Expressions => New_List (Make_Integer_Literal (Loc, 1))));
471 for J in 1 .. Dims loop
476 Make_Attribute_Reference (Loc,
477 Attribute_Name => Name_Length,
479 New_Occurrence_Of (Temps (J), Loc),
480 Expressions => New_List (Make_Integer_Literal (Loc, 1))));
483 Build_Task_Image_Prefix (Loc, Len, Res, Pos, Pref, Sum, Decls, Stats);
485 Set_Character_Literal_Name (Char_Code (Character'Pos ('(')));
488 Make_Assignment_Statement (Loc,
489 Name => Make_Indexed_Component (Loc,
490 Prefix => New_Occurrence_Of (Res, Loc),
491 Expressions => New_List (New_Occurrence_Of (Pos, Loc))),
493 Make_Character_Literal (Loc,
495 Char_Literal_Value =>
496 UI_From_Int (Character'Pos ('(')))));
499 Make_Assignment_Statement (Loc,
500 Name => New_Occurrence_Of (Pos, Loc),
503 Left_Opnd => New_Occurrence_Of (Pos, Loc),
504 Right_Opnd => Make_Integer_Literal (Loc, 1))));
506 for J in 1 .. Dims loop
509 Make_Assignment_Statement (Loc,
510 Name => Make_Slice (Loc,
511 Prefix => New_Occurrence_Of (Res, Loc),
514 Low_Bound => New_Occurrence_Of (Pos, Loc),
515 High_Bound => Make_Op_Subtract (Loc,
518 Left_Opnd => New_Occurrence_Of (Pos, Loc),
520 Make_Attribute_Reference (Loc,
521 Attribute_Name => Name_Length,
523 New_Occurrence_Of (Temps (J), Loc),
525 New_List (Make_Integer_Literal (Loc, 1)))),
526 Right_Opnd => Make_Integer_Literal (Loc, 1)))),
528 Expression => New_Occurrence_Of (Temps (J), Loc)));
532 Make_Assignment_Statement (Loc,
533 Name => New_Occurrence_Of (Pos, Loc),
536 Left_Opnd => New_Occurrence_Of (Pos, Loc),
538 Make_Attribute_Reference (Loc,
539 Attribute_Name => Name_Length,
540 Prefix => New_Occurrence_Of (Temps (J), Loc),
542 New_List (Make_Integer_Literal (Loc, 1))))));
544 Set_Character_Literal_Name (Char_Code (Character'Pos (',')));
547 Make_Assignment_Statement (Loc,
548 Name => Make_Indexed_Component (Loc,
549 Prefix => New_Occurrence_Of (Res, Loc),
550 Expressions => New_List (New_Occurrence_Of (Pos, Loc))),
552 Make_Character_Literal (Loc,
554 Char_Literal_Value =>
555 UI_From_Int (Character'Pos (',')))));
558 Make_Assignment_Statement (Loc,
559 Name => New_Occurrence_Of (Pos, Loc),
562 Left_Opnd => New_Occurrence_Of (Pos, Loc),
563 Right_Opnd => Make_Integer_Literal (Loc, 1))));
567 Set_Character_Literal_Name (Char_Code (Character'Pos (')')));
570 Make_Assignment_Statement (Loc,
571 Name => Make_Indexed_Component (Loc,
572 Prefix => New_Occurrence_Of (Res, Loc),
573 Expressions => New_List (New_Occurrence_Of (Len, Loc))),
575 Make_Character_Literal (Loc,
577 Char_Literal_Value =>
578 UI_From_Int (Character'Pos (')')))));
579 return Build_Task_Image_Function (Loc, Decls, Stats, Res);
580 end Build_Task_Array_Image;
582 ----------------------------
583 -- Build_Task_Image_Decls --
584 ----------------------------
586 function Build_Task_Image_Decls
590 In_Init_Proc : Boolean := False) return List_Id
592 Decls : constant List_Id := New_List;
593 T_Id : Entity_Id := Empty;
595 Expr : Node_Id := Empty;
596 Fun : Node_Id := Empty;
597 Is_Dyn : constant Boolean :=
598 Nkind (Parent (Id_Ref)) = N_Assignment_Statement
600 Nkind (Expression (Parent (Id_Ref))) = N_Allocator;
603 -- If Discard_Names or No_Implicit_Heap_Allocations are in effect,
604 -- generate a dummy declaration only.
606 if Restriction_Active (No_Implicit_Heap_Allocations)
607 or else Global_Discard_Names
609 T_Id := Make_Temporary (Loc, 'J');
614 Make_Object_Declaration (Loc,
615 Defining_Identifier => T_Id,
616 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
618 Make_String_Literal (Loc,
619 Strval => String_From_Name_Buffer)));
622 if Nkind (Id_Ref) = N_Identifier
623 or else Nkind (Id_Ref) = N_Defining_Identifier
625 -- For a simple variable, the image of the task is built from
626 -- the name of the variable. To avoid possible conflict with
627 -- the anonymous type created for a single protected object,
628 -- add a numeric suffix.
631 Make_Defining_Identifier (Loc,
632 New_External_Name (Chars (Id_Ref), 'T', 1));
634 Get_Name_String (Chars (Id_Ref));
637 Make_String_Literal (Loc,
638 Strval => String_From_Name_Buffer);
640 elsif Nkind (Id_Ref) = N_Selected_Component then
642 Make_Defining_Identifier (Loc,
643 New_External_Name (Chars (Selector_Name (Id_Ref)), 'T'));
644 Fun := Build_Task_Record_Image (Loc, Id_Ref, Is_Dyn);
646 elsif Nkind (Id_Ref) = N_Indexed_Component then
648 Make_Defining_Identifier (Loc,
649 New_External_Name (Chars (A_Type), 'N'));
651 Fun := Build_Task_Array_Image (Loc, Id_Ref, A_Type, Is_Dyn);
655 if Present (Fun) then
657 Expr := Make_Function_Call (Loc,
658 Name => New_Occurrence_Of (Defining_Entity (Fun), Loc));
660 if not In_Init_Proc and then VM_Target = No_VM then
661 Set_Uses_Sec_Stack (Defining_Entity (Fun));
665 Decl := Make_Object_Declaration (Loc,
666 Defining_Identifier => T_Id,
667 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
668 Constant_Present => True,
671 Append (Decl, Decls);
673 end Build_Task_Image_Decls;
675 -------------------------------
676 -- Build_Task_Image_Function --
677 -------------------------------
679 function Build_Task_Image_Function
683 Res : Entity_Id) return Node_Id
689 Make_Simple_Return_Statement (Loc,
690 Expression => New_Occurrence_Of (Res, Loc)));
692 Spec := Make_Function_Specification (Loc,
693 Defining_Unit_Name => Make_Temporary (Loc, 'F'),
694 Result_Definition => New_Occurrence_Of (Standard_String, Loc));
696 -- Calls to 'Image use the secondary stack, which must be cleaned
697 -- up after the task name is built.
699 return Make_Subprogram_Body (Loc,
700 Specification => Spec,
701 Declarations => Decls,
702 Handled_Statement_Sequence =>
703 Make_Handled_Sequence_Of_Statements (Loc, Statements => Stats));
704 end Build_Task_Image_Function;
706 -----------------------------
707 -- Build_Task_Image_Prefix --
708 -----------------------------
710 procedure Build_Task_Image_Prefix
721 Len := Make_Temporary (Loc, 'L', Sum);
724 Make_Object_Declaration (Loc,
725 Defining_Identifier => Len,
726 Object_Definition => New_Occurrence_Of (Standard_Integer, Loc),
729 Res := Make_Temporary (Loc, 'R');
732 Make_Object_Declaration (Loc,
733 Defining_Identifier => Res,
735 Make_Subtype_Indication (Loc,
736 Subtype_Mark => New_Occurrence_Of (Standard_String, Loc),
738 Make_Index_Or_Discriminant_Constraint (Loc,
742 Low_Bound => Make_Integer_Literal (Loc, 1),
743 High_Bound => New_Occurrence_Of (Len, Loc)))))));
745 Pos := Make_Temporary (Loc, 'P');
748 Make_Object_Declaration (Loc,
749 Defining_Identifier => Pos,
750 Object_Definition => New_Occurrence_Of (Standard_Integer, Loc)));
752 -- Pos := Prefix'Length;
755 Make_Assignment_Statement (Loc,
756 Name => New_Occurrence_Of (Pos, Loc),
758 Make_Attribute_Reference (Loc,
759 Attribute_Name => Name_Length,
760 Prefix => New_Occurrence_Of (Prefix, Loc),
761 Expressions => New_List (Make_Integer_Literal (Loc, 1)))));
763 -- Res (1 .. Pos) := Prefix;
766 Make_Assignment_Statement (Loc,
769 Prefix => New_Occurrence_Of (Res, Loc),
772 Low_Bound => Make_Integer_Literal (Loc, 1),
773 High_Bound => New_Occurrence_Of (Pos, Loc))),
775 Expression => New_Occurrence_Of (Prefix, Loc)));
778 Make_Assignment_Statement (Loc,
779 Name => New_Occurrence_Of (Pos, Loc),
782 Left_Opnd => New_Occurrence_Of (Pos, Loc),
783 Right_Opnd => Make_Integer_Literal (Loc, 1))));
784 end Build_Task_Image_Prefix;
786 -----------------------------
787 -- Build_Task_Record_Image --
788 -----------------------------
790 function Build_Task_Record_Image
793 Dyn : Boolean := False) return Node_Id
796 -- Total length of generated name
802 -- String to hold result
804 Pref : constant Entity_Id := Make_Temporary (Loc, 'P');
805 -- Name of enclosing variable, prefix of resulting name
808 -- Expression to compute total size of string
811 -- Entity for selector name
813 Decls : constant List_Id := New_List;
814 Stats : constant List_Id := New_List;
817 -- For a dynamic task, the name comes from the target variable.
818 -- For a static one it is a formal of the enclosing init proc.
821 Get_Name_String (Chars (Entity (Prefix (Id_Ref))));
823 Make_Object_Declaration (Loc,
824 Defining_Identifier => Pref,
825 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
827 Make_String_Literal (Loc,
828 Strval => String_From_Name_Buffer)));
832 Make_Object_Renaming_Declaration (Loc,
833 Defining_Identifier => Pref,
834 Subtype_Mark => New_Occurrence_Of (Standard_String, Loc),
835 Name => Make_Identifier (Loc, Name_uTask_Name)));
838 Sel := Make_Temporary (Loc, 'S');
840 Get_Name_String (Chars (Selector_Name (Id_Ref)));
843 Make_Object_Declaration (Loc,
844 Defining_Identifier => Sel,
845 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
847 Make_String_Literal (Loc,
848 Strval => String_From_Name_Buffer)));
850 Sum := Make_Integer_Literal (Loc, Nat (Name_Len + 1));
856 Make_Attribute_Reference (Loc,
857 Attribute_Name => Name_Length,
859 New_Occurrence_Of (Pref, Loc),
860 Expressions => New_List (Make_Integer_Literal (Loc, 1))));
862 Build_Task_Image_Prefix (Loc, Len, Res, Pos, Pref, Sum, Decls, Stats);
864 Set_Character_Literal_Name (Char_Code (Character'Pos ('.')));
869 Make_Assignment_Statement (Loc,
870 Name => Make_Indexed_Component (Loc,
871 Prefix => New_Occurrence_Of (Res, Loc),
872 Expressions => New_List (New_Occurrence_Of (Pos, Loc))),
874 Make_Character_Literal (Loc,
876 Char_Literal_Value =>
877 UI_From_Int (Character'Pos ('.')))));
880 Make_Assignment_Statement (Loc,
881 Name => New_Occurrence_Of (Pos, Loc),
884 Left_Opnd => New_Occurrence_Of (Pos, Loc),
885 Right_Opnd => Make_Integer_Literal (Loc, 1))));
887 -- Res (Pos .. Len) := Selector;
890 Make_Assignment_Statement (Loc,
891 Name => Make_Slice (Loc,
892 Prefix => New_Occurrence_Of (Res, Loc),
895 Low_Bound => New_Occurrence_Of (Pos, Loc),
896 High_Bound => New_Occurrence_Of (Len, Loc))),
897 Expression => New_Occurrence_Of (Sel, Loc)));
899 return Build_Task_Image_Function (Loc, Decls, Stats, Res);
900 end Build_Task_Record_Image;
902 ----------------------------------
903 -- Component_May_Be_Bit_Aligned --
904 ----------------------------------
906 function Component_May_Be_Bit_Aligned (Comp : Entity_Id) return Boolean is
910 -- If no component clause, then everything is fine, since the back end
911 -- never bit-misaligns by default, even if there is a pragma Packed for
914 if No (Comp) or else No (Component_Clause (Comp)) then
918 UT := Underlying_Type (Etype (Comp));
920 -- It is only array and record types that cause trouble
922 if not Is_Record_Type (UT)
923 and then not Is_Array_Type (UT)
927 -- If we know that we have a small (64 bits or less) record or small
928 -- bit-packed array, then everything is fine, since the back end can
929 -- handle these cases correctly.
931 elsif Esize (Comp) <= 64
932 and then (Is_Record_Type (UT)
933 or else Is_Bit_Packed_Array (UT))
937 -- Otherwise if the component is not byte aligned, we know we have the
938 -- nasty unaligned case.
940 elsif Normalized_First_Bit (Comp) /= Uint_0
941 or else Esize (Comp) mod System_Storage_Unit /= Uint_0
945 -- If we are large and byte aligned, then OK at this level
950 end Component_May_Be_Bit_Aligned;
952 -----------------------------------
953 -- Corresponding_Runtime_Package --
954 -----------------------------------
956 function Corresponding_Runtime_Package (Typ : Entity_Id) return RTU_Id is
957 Pkg_Id : RTU_Id := RTU_Null;
960 pragma Assert (Is_Concurrent_Type (Typ));
962 if Ekind (Typ) in Protected_Kind then
964 or else Has_Interrupt_Handler (Typ)
965 or else (Has_Attach_Handler (Typ)
966 and then not Restricted_Profile)
968 -- A protected type without entries that covers an interface and
969 -- overrides the abstract routines with protected procedures is
970 -- considered equivalent to a protected type with entries in the
971 -- context of dispatching select statements. It is sufficient to
972 -- check for the presence of an interface list in the declaration
973 -- node to recognize this case.
975 or else Present (Interface_List (Parent (Typ)))
978 or else Restriction_Active (No_Entry_Queue) = False
979 or else Number_Entries (Typ) > 1
980 or else (Has_Attach_Handler (Typ)
981 and then not Restricted_Profile)
983 Pkg_Id := System_Tasking_Protected_Objects_Entries;
985 Pkg_Id := System_Tasking_Protected_Objects_Single_Entry;
989 Pkg_Id := System_Tasking_Protected_Objects;
994 end Corresponding_Runtime_Package;
996 -------------------------------
997 -- Convert_To_Actual_Subtype --
998 -------------------------------
1000 procedure Convert_To_Actual_Subtype (Exp : Entity_Id) is
1004 Act_ST := Get_Actual_Subtype (Exp);
1006 if Act_ST = Etype (Exp) then
1011 Convert_To (Act_ST, Relocate_Node (Exp)));
1012 Analyze_And_Resolve (Exp, Act_ST);
1014 end Convert_To_Actual_Subtype;
1016 -----------------------------------
1017 -- Current_Sem_Unit_Declarations --
1018 -----------------------------------
1020 function Current_Sem_Unit_Declarations return List_Id is
1021 U : Node_Id := Unit (Cunit (Current_Sem_Unit));
1025 -- If the current unit is a package body, locate the visible
1026 -- declarations of the package spec.
1028 if Nkind (U) = N_Package_Body then
1029 U := Unit (Library_Unit (Cunit (Current_Sem_Unit)));
1032 if Nkind (U) = N_Package_Declaration then
1033 U := Specification (U);
1034 Decls := Visible_Declarations (U);
1038 Set_Visible_Declarations (U, Decls);
1042 Decls := Declarations (U);
1046 Set_Declarations (U, Decls);
1051 end Current_Sem_Unit_Declarations;
1053 -----------------------
1054 -- Duplicate_Subexpr --
1055 -----------------------
1057 function Duplicate_Subexpr
1059 Name_Req : Boolean := False) return Node_Id
1062 Remove_Side_Effects (Exp, Name_Req);
1063 return New_Copy_Tree (Exp);
1064 end Duplicate_Subexpr;
1066 ---------------------------------
1067 -- Duplicate_Subexpr_No_Checks --
1068 ---------------------------------
1070 function Duplicate_Subexpr_No_Checks
1072 Name_Req : Boolean := False) return Node_Id
1077 Remove_Side_Effects (Exp, Name_Req);
1078 New_Exp := New_Copy_Tree (Exp);
1079 Remove_Checks (New_Exp);
1081 end Duplicate_Subexpr_No_Checks;
1083 -----------------------------------
1084 -- Duplicate_Subexpr_Move_Checks --
1085 -----------------------------------
1087 function Duplicate_Subexpr_Move_Checks
1089 Name_Req : Boolean := False) return Node_Id
1094 Remove_Side_Effects (Exp, Name_Req);
1095 New_Exp := New_Copy_Tree (Exp);
1096 Remove_Checks (Exp);
1098 end Duplicate_Subexpr_Move_Checks;
1100 --------------------
1101 -- Ensure_Defined --
1102 --------------------
1104 procedure Ensure_Defined (Typ : Entity_Id; N : Node_Id) is
1108 -- An itype reference must only be created if this is a local
1109 -- itype, so that gigi can elaborate it on the proper objstack.
1112 and then Scope (Typ) = Current_Scope
1114 IR := Make_Itype_Reference (Sloc (N));
1115 Set_Itype (IR, Typ);
1116 Insert_Action (N, IR);
1120 --------------------
1121 -- Entry_Names_OK --
1122 --------------------
1124 function Entry_Names_OK return Boolean is
1127 not Restricted_Profile
1128 and then not Global_Discard_Names
1129 and then not Restriction_Active (No_Implicit_Heap_Allocations)
1130 and then not Restriction_Active (No_Local_Allocators);
1133 ---------------------
1134 -- Evolve_And_Then --
1135 ---------------------
1137 procedure Evolve_And_Then (Cond : in out Node_Id; Cond1 : Node_Id) is
1143 Make_And_Then (Sloc (Cond1),
1145 Right_Opnd => Cond1);
1147 end Evolve_And_Then;
1149 --------------------
1150 -- Evolve_Or_Else --
1151 --------------------
1153 procedure Evolve_Or_Else (Cond : in out Node_Id; Cond1 : Node_Id) is
1159 Make_Or_Else (Sloc (Cond1),
1161 Right_Opnd => Cond1);
1165 ------------------------------
1166 -- Expand_Subtype_From_Expr --
1167 ------------------------------
1169 -- This function is applicable for both static and dynamic allocation of
1170 -- objects which are constrained by an initial expression. Basically it
1171 -- transforms an unconstrained subtype indication into a constrained one.
1172 -- The expression may also be transformed in certain cases in order to
1173 -- avoid multiple evaluation. In the static allocation case, the general
1178 -- is transformed into
1180 -- Val : Constrained_Subtype_of_T := Maybe_Modified_Expr;
1182 -- Here are the main cases :
1184 -- <if Expr is a Slice>
1185 -- Val : T ([Index_Subtype (Expr)]) := Expr;
1187 -- <elsif Expr is a String Literal>
1188 -- Val : T (T'First .. T'First + Length (string literal) - 1) := Expr;
1190 -- <elsif Expr is Constrained>
1191 -- subtype T is Type_Of_Expr
1194 -- <elsif Expr is an entity_name>
1195 -- Val : T (constraints taken from Expr) := Expr;
1198 -- type Axxx is access all T;
1199 -- Rval : Axxx := Expr'ref;
1200 -- Val : T (constraints taken from Rval) := Rval.all;
1202 -- ??? note: when the Expression is allocated in the secondary stack
1203 -- we could use it directly instead of copying it by declaring
1204 -- Val : T (...) renames Rval.all
1206 procedure Expand_Subtype_From_Expr
1208 Unc_Type : Entity_Id;
1209 Subtype_Indic : Node_Id;
1212 Loc : constant Source_Ptr := Sloc (N);
1213 Exp_Typ : constant Entity_Id := Etype (Exp);
1217 -- In general we cannot build the subtype if expansion is disabled,
1218 -- because internal entities may not have been defined. However, to
1219 -- avoid some cascaded errors, we try to continue when the expression
1220 -- is an array (or string), because it is safe to compute the bounds.
1221 -- It is in fact required to do so even in a generic context, because
1222 -- there may be constants that depend on bounds of string literal.
1224 if not Expander_Active
1225 and then (No (Etype (Exp))
1226 or else Base_Type (Etype (Exp)) /= Standard_String)
1231 if Nkind (Exp) = N_Slice then
1233 Slice_Type : constant Entity_Id := Etype (First_Index (Exp_Typ));
1236 Rewrite (Subtype_Indic,
1237 Make_Subtype_Indication (Loc,
1238 Subtype_Mark => New_Reference_To (Unc_Type, Loc),
1240 Make_Index_Or_Discriminant_Constraint (Loc,
1241 Constraints => New_List
1242 (New_Reference_To (Slice_Type, Loc)))));
1244 -- This subtype indication may be used later for constraint checks
1245 -- we better make sure that if a variable was used as a bound of
1246 -- of the original slice, its value is frozen.
1248 Force_Evaluation (Low_Bound (Scalar_Range (Slice_Type)));
1249 Force_Evaluation (High_Bound (Scalar_Range (Slice_Type)));
1252 elsif Ekind (Exp_Typ) = E_String_Literal_Subtype then
1253 Rewrite (Subtype_Indic,
1254 Make_Subtype_Indication (Loc,
1255 Subtype_Mark => New_Reference_To (Unc_Type, Loc),
1257 Make_Index_Or_Discriminant_Constraint (Loc,
1258 Constraints => New_List (
1259 Make_Literal_Range (Loc,
1260 Literal_Typ => Exp_Typ)))));
1262 elsif Is_Constrained (Exp_Typ)
1263 and then not Is_Class_Wide_Type (Unc_Type)
1265 if Is_Itype (Exp_Typ) then
1267 -- Within an initialization procedure, a selected component
1268 -- denotes a component of the enclosing record, and it appears
1269 -- as an actual in a call to its own initialization procedure.
1270 -- If this component depends on the outer discriminant, we must
1271 -- generate the proper actual subtype for it.
1273 if Nkind (Exp) = N_Selected_Component
1274 and then Within_Init_Proc
1277 Decl : constant Node_Id :=
1278 Build_Actual_Subtype_Of_Component (Exp_Typ, Exp);
1280 if Present (Decl) then
1281 Insert_Action (N, Decl);
1282 T := Defining_Identifier (Decl);
1288 -- No need to generate a new one (new what???)
1295 T := Make_Temporary (Loc, 'T');
1298 Make_Subtype_Declaration (Loc,
1299 Defining_Identifier => T,
1300 Subtype_Indication => New_Reference_To (Exp_Typ, Loc)));
1302 -- This type is marked as an itype even though it has an
1303 -- explicit declaration because otherwise it can be marked
1304 -- with Is_Generic_Actual_Type and generate spurious errors.
1305 -- (see sem_ch8.Analyze_Package_Renaming and sem_type.covers)
1308 Set_Associated_Node_For_Itype (T, Exp);
1311 Rewrite (Subtype_Indic, New_Reference_To (T, Loc));
1313 -- Nothing needs to be done for private types with unknown discriminants
1314 -- if the underlying type is not an unconstrained composite type or it
1315 -- is an unchecked union.
1317 elsif Is_Private_Type (Unc_Type)
1318 and then Has_Unknown_Discriminants (Unc_Type)
1319 and then (not Is_Composite_Type (Underlying_Type (Unc_Type))
1320 or else Is_Constrained (Underlying_Type (Unc_Type))
1321 or else Is_Unchecked_Union (Underlying_Type (Unc_Type)))
1325 -- Case of derived type with unknown discriminants where the parent type
1326 -- also has unknown discriminants.
1328 elsif Is_Record_Type (Unc_Type)
1329 and then not Is_Class_Wide_Type (Unc_Type)
1330 and then Has_Unknown_Discriminants (Unc_Type)
1331 and then Has_Unknown_Discriminants (Underlying_Type (Unc_Type))
1333 -- Nothing to be done if no underlying record view available
1335 if No (Underlying_Record_View (Unc_Type)) then
1338 -- Otherwise use the Underlying_Record_View to create the proper
1339 -- constrained subtype for an object of a derived type with unknown
1343 Remove_Side_Effects (Exp);
1344 Rewrite (Subtype_Indic,
1345 Make_Subtype_From_Expr (Exp, Underlying_Record_View (Unc_Type)));
1348 -- Renamings of class-wide interface types require no equivalent
1349 -- constrained type declarations because we only need to reference
1350 -- the tag component associated with the interface.
1353 and then Nkind (N) = N_Object_Renaming_Declaration
1354 and then Is_Interface (Unc_Type)
1356 pragma Assert (Is_Class_Wide_Type (Unc_Type));
1359 -- In Ada95, nothing to be done if the type of the expression is
1360 -- limited, because in this case the expression cannot be copied,
1361 -- and its use can only be by reference.
1363 -- In Ada2005, the context can be an object declaration whose expression
1364 -- is a function that returns in place. If the nominal subtype has
1365 -- unknown discriminants, the call still provides constraints on the
1366 -- object, and we have to create an actual subtype from it.
1368 -- If the type is class-wide, the expression is dynamically tagged and
1369 -- we do not create an actual subtype either. Ditto for an interface.
1371 elsif Is_Limited_Type (Exp_Typ)
1373 (Is_Class_Wide_Type (Exp_Typ)
1374 or else Is_Interface (Exp_Typ)
1375 or else not Has_Unknown_Discriminants (Exp_Typ)
1376 or else not Is_Composite_Type (Unc_Type))
1380 -- For limited objects initialized with build in place function calls,
1381 -- nothing to be done; otherwise we prematurely introduce an N_Reference
1382 -- node in the expression initializing the object, which breaks the
1383 -- circuitry that detects and adds the additional arguments to the
1386 elsif Is_Build_In_Place_Function_Call (Exp) then
1390 Remove_Side_Effects (Exp);
1391 Rewrite (Subtype_Indic,
1392 Make_Subtype_From_Expr (Exp, Unc_Type));
1394 end Expand_Subtype_From_Expr;
1396 --------------------
1397 -- Find_Init_Call --
1398 --------------------
1400 function Find_Init_Call
1402 Rep_Clause : Node_Id) return Node_Id
1404 Typ : constant Entity_Id := Etype (Var);
1406 Init_Proc : Entity_Id;
1407 -- Initialization procedure for Typ
1409 function Find_Init_Call_In_List (From : Node_Id) return Node_Id;
1410 -- Look for init call for Var starting at From and scanning the
1411 -- enclosing list until Rep_Clause or the end of the list is reached.
1413 ----------------------------
1414 -- Find_Init_Call_In_List --
1415 ----------------------------
1417 function Find_Init_Call_In_List (From : Node_Id) return Node_Id is
1418 Init_Call : Node_Id;
1422 while Present (Init_Call) and then Init_Call /= Rep_Clause loop
1423 if Nkind (Init_Call) = N_Procedure_Call_Statement
1424 and then Is_Entity_Name (Name (Init_Call))
1425 and then Entity (Name (Init_Call)) = Init_Proc
1433 end Find_Init_Call_In_List;
1435 Init_Call : Node_Id;
1437 -- Start of processing for Find_Init_Call
1440 if not Has_Non_Null_Base_Init_Proc (Typ) then
1441 -- No init proc for the type, so obviously no call to be found
1446 Init_Proc := Base_Init_Proc (Typ);
1448 -- First scan the list containing the declaration of Var
1450 Init_Call := Find_Init_Call_In_List (From => Next (Parent (Var)));
1452 -- If not found, also look on Var's freeze actions list, if any, since
1453 -- the init call may have been moved there (case of an address clause
1454 -- applying to Var).
1456 if No (Init_Call) and then Present (Freeze_Node (Var)) then
1457 Init_Call := Find_Init_Call_In_List
1458 (First (Actions (Freeze_Node (Var))));
1464 ------------------------
1465 -- Find_Interface_ADT --
1466 ------------------------
1468 function Find_Interface_ADT
1470 Iface : Entity_Id) return Elmt_Id
1473 Typ : Entity_Id := T;
1476 pragma Assert (Is_Interface (Iface));
1478 -- Handle private types
1480 if Has_Private_Declaration (Typ)
1481 and then Present (Full_View (Typ))
1483 Typ := Full_View (Typ);
1486 -- Handle access types
1488 if Is_Access_Type (Typ) then
1489 Typ := Designated_Type (Typ);
1492 -- Handle task and protected types implementing interfaces
1494 if Is_Concurrent_Type (Typ) then
1495 Typ := Corresponding_Record_Type (Typ);
1499 (not Is_Class_Wide_Type (Typ)
1500 and then Ekind (Typ) /= E_Incomplete_Type);
1502 if Is_Ancestor (Iface, Typ) then
1503 return First_Elmt (Access_Disp_Table (Typ));
1507 Next_Elmt (Next_Elmt (First_Elmt (Access_Disp_Table (Typ))));
1509 and then Present (Related_Type (Node (ADT)))
1510 and then Related_Type (Node (ADT)) /= Iface
1511 and then not Is_Ancestor (Iface, Related_Type (Node (ADT)))
1516 pragma Assert (Present (Related_Type (Node (ADT))));
1519 end Find_Interface_ADT;
1521 ------------------------
1522 -- Find_Interface_Tag --
1523 ------------------------
1525 function Find_Interface_Tag
1527 Iface : Entity_Id) return Entity_Id
1530 Found : Boolean := False;
1531 Typ : Entity_Id := T;
1533 procedure Find_Tag (Typ : Entity_Id);
1534 -- Internal subprogram used to recursively climb to the ancestors
1540 procedure Find_Tag (Typ : Entity_Id) is
1545 -- This routine does not handle the case in which the interface is an
1546 -- ancestor of Typ. That case is handled by the enclosing subprogram.
1548 pragma Assert (Typ /= Iface);
1550 -- Climb to the root type handling private types
1552 if Present (Full_View (Etype (Typ))) then
1553 if Full_View (Etype (Typ)) /= Typ then
1554 Find_Tag (Full_View (Etype (Typ)));
1557 elsif Etype (Typ) /= Typ then
1558 Find_Tag (Etype (Typ));
1561 -- Traverse the list of interfaces implemented by the type
1564 and then Present (Interfaces (Typ))
1565 and then not (Is_Empty_Elmt_List (Interfaces (Typ)))
1567 -- Skip the tag associated with the primary table
1569 pragma Assert (Etype (First_Tag_Component (Typ)) = RTE (RE_Tag));
1570 AI_Tag := Next_Tag_Component (First_Tag_Component (Typ));
1571 pragma Assert (Present (AI_Tag));
1573 AI_Elmt := First_Elmt (Interfaces (Typ));
1574 while Present (AI_Elmt) loop
1575 AI := Node (AI_Elmt);
1577 if AI = Iface or else Is_Ancestor (Iface, AI) then
1582 AI_Tag := Next_Tag_Component (AI_Tag);
1583 Next_Elmt (AI_Elmt);
1588 -- Start of processing for Find_Interface_Tag
1591 pragma Assert (Is_Interface (Iface));
1593 -- Handle access types
1595 if Is_Access_Type (Typ) then
1596 Typ := Designated_Type (Typ);
1599 -- Handle class-wide types
1601 if Is_Class_Wide_Type (Typ) then
1602 Typ := Root_Type (Typ);
1605 -- Handle private types
1607 if Has_Private_Declaration (Typ)
1608 and then Present (Full_View (Typ))
1610 Typ := Full_View (Typ);
1613 -- Handle entities from the limited view
1615 if Ekind (Typ) = E_Incomplete_Type then
1616 pragma Assert (Present (Non_Limited_View (Typ)));
1617 Typ := Non_Limited_View (Typ);
1620 -- Handle task and protected types implementing interfaces
1622 if Is_Concurrent_Type (Typ) then
1623 Typ := Corresponding_Record_Type (Typ);
1626 -- If the interface is an ancestor of the type, then it shared the
1627 -- primary dispatch table.
1629 if Is_Ancestor (Iface, Typ) then
1630 pragma Assert (Etype (First_Tag_Component (Typ)) = RTE (RE_Tag));
1631 return First_Tag_Component (Typ);
1633 -- Otherwise we need to search for its associated tag component
1637 pragma Assert (Found);
1640 end Find_Interface_Tag;
1646 function Find_Prim_Op (T : Entity_Id; Name : Name_Id) return Entity_Id is
1648 Typ : Entity_Id := T;
1652 if Is_Class_Wide_Type (Typ) then
1653 Typ := Root_Type (Typ);
1656 Typ := Underlying_Type (Typ);
1658 -- Loop through primitive operations
1660 Prim := First_Elmt (Primitive_Operations (Typ));
1661 while Present (Prim) loop
1664 -- We can retrieve primitive operations by name if it is an internal
1665 -- name. For equality we must check that both of its operands have
1666 -- the same type, to avoid confusion with user-defined equalities
1667 -- than may have a non-symmetric signature.
1669 exit when Chars (Op) = Name
1672 or else Etype (First_Formal (Op)) = Etype (Last_Formal (Op)));
1676 -- Raise Program_Error if no primitive found
1679 raise Program_Error;
1690 function Find_Prim_Op
1692 Name : TSS_Name_Type) return Entity_Id
1695 Typ : Entity_Id := T;
1698 if Is_Class_Wide_Type (Typ) then
1699 Typ := Root_Type (Typ);
1702 Typ := Underlying_Type (Typ);
1704 Prim := First_Elmt (Primitive_Operations (Typ));
1705 while not Is_TSS (Node (Prim), Name) loop
1708 -- Raise program error if no primitive found
1711 raise Program_Error;
1718 ----------------------------
1719 -- Find_Protection_Object --
1720 ----------------------------
1722 function Find_Protection_Object (Scop : Entity_Id) return Entity_Id is
1727 while Present (S) loop
1728 if (Ekind (S) = E_Entry
1729 or else Ekind (S) = E_Entry_Family
1730 or else Ekind (S) = E_Function
1731 or else Ekind (S) = E_Procedure)
1732 and then Present (Protection_Object (S))
1734 return Protection_Object (S);
1740 -- If we do not find a Protection object in the scope chain, then
1741 -- something has gone wrong, most likely the object was never created.
1743 raise Program_Error;
1744 end Find_Protection_Object;
1746 ----------------------
1747 -- Force_Evaluation --
1748 ----------------------
1750 procedure Force_Evaluation (Exp : Node_Id; Name_Req : Boolean := False) is
1752 Remove_Side_Effects (Exp, Name_Req, Variable_Ref => True);
1753 end Force_Evaluation;
1755 ------------------------
1756 -- Generate_Poll_Call --
1757 ------------------------
1759 procedure Generate_Poll_Call (N : Node_Id) is
1761 -- No poll call if polling not active
1763 if not Polling_Required then
1766 -- Otherwise generate require poll call
1769 Insert_Before_And_Analyze (N,
1770 Make_Procedure_Call_Statement (Sloc (N),
1771 Name => New_Occurrence_Of (RTE (RE_Poll), Sloc (N))));
1773 end Generate_Poll_Call;
1775 ---------------------------------
1776 -- Get_Current_Value_Condition --
1777 ---------------------------------
1779 -- Note: the implementation of this procedure is very closely tied to the
1780 -- implementation of Set_Current_Value_Condition. In the Get procedure, we
1781 -- interpret Current_Value fields set by the Set procedure, so the two
1782 -- procedures need to be closely coordinated.
1784 procedure Get_Current_Value_Condition
1789 Loc : constant Source_Ptr := Sloc (Var);
1790 Ent : constant Entity_Id := Entity (Var);
1792 procedure Process_Current_Value_Condition
1795 -- N is an expression which holds either True (S = True) or False (S =
1796 -- False) in the condition. This procedure digs out the expression and
1797 -- if it refers to Ent, sets Op and Val appropriately.
1799 -------------------------------------
1800 -- Process_Current_Value_Condition --
1801 -------------------------------------
1803 procedure Process_Current_Value_Condition
1814 -- Deal with NOT operators, inverting sense
1816 while Nkind (Cond) = N_Op_Not loop
1817 Cond := Right_Opnd (Cond);
1821 -- Deal with AND THEN and AND cases
1823 if Nkind (Cond) = N_And_Then
1824 or else Nkind (Cond) = N_Op_And
1826 -- Don't ever try to invert a condition that is of the form
1827 -- of an AND or AND THEN (since we are not doing sufficiently
1828 -- general processing to allow this).
1830 if Sens = False then
1836 -- Recursively process AND and AND THEN branches
1838 Process_Current_Value_Condition (Left_Opnd (Cond), True);
1840 if Op /= N_Empty then
1844 Process_Current_Value_Condition (Right_Opnd (Cond), True);
1847 -- Case of relational operator
1849 elsif Nkind (Cond) in N_Op_Compare then
1852 -- Invert sense of test if inverted test
1854 if Sens = False then
1856 when N_Op_Eq => Op := N_Op_Ne;
1857 when N_Op_Ne => Op := N_Op_Eq;
1858 when N_Op_Lt => Op := N_Op_Ge;
1859 when N_Op_Gt => Op := N_Op_Le;
1860 when N_Op_Le => Op := N_Op_Gt;
1861 when N_Op_Ge => Op := N_Op_Lt;
1862 when others => raise Program_Error;
1866 -- Case of entity op value
1868 if Is_Entity_Name (Left_Opnd (Cond))
1869 and then Ent = Entity (Left_Opnd (Cond))
1870 and then Compile_Time_Known_Value (Right_Opnd (Cond))
1872 Val := Right_Opnd (Cond);
1874 -- Case of value op entity
1876 elsif Is_Entity_Name (Right_Opnd (Cond))
1877 and then Ent = Entity (Right_Opnd (Cond))
1878 and then Compile_Time_Known_Value (Left_Opnd (Cond))
1880 Val := Left_Opnd (Cond);
1882 -- We are effectively swapping operands
1885 when N_Op_Eq => null;
1886 when N_Op_Ne => null;
1887 when N_Op_Lt => Op := N_Op_Gt;
1888 when N_Op_Gt => Op := N_Op_Lt;
1889 when N_Op_Le => Op := N_Op_Ge;
1890 when N_Op_Ge => Op := N_Op_Le;
1891 when others => raise Program_Error;
1900 -- Case of Boolean variable reference, return as though the
1901 -- reference had said var = True.
1904 if Is_Entity_Name (Cond)
1905 and then Ent = Entity (Cond)
1907 Val := New_Occurrence_Of (Standard_True, Sloc (Cond));
1909 if Sens = False then
1916 end Process_Current_Value_Condition;
1918 -- Start of processing for Get_Current_Value_Condition
1924 -- Immediate return, nothing doing, if this is not an object
1926 if Ekind (Ent) not in Object_Kind then
1930 -- Otherwise examine current value
1933 CV : constant Node_Id := Current_Value (Ent);
1938 -- If statement. Condition is known true in THEN section, known False
1939 -- in any ELSIF or ELSE part, and unknown outside the IF statement.
1941 if Nkind (CV) = N_If_Statement then
1943 -- Before start of IF statement
1945 if Loc < Sloc (CV) then
1948 -- After end of IF statement
1950 elsif Loc >= Sloc (CV) + Text_Ptr (UI_To_Int (End_Span (CV))) then
1954 -- At this stage we know that we are within the IF statement, but
1955 -- unfortunately, the tree does not record the SLOC of the ELSE so
1956 -- we cannot use a simple SLOC comparison to distinguish between
1957 -- the then/else statements, so we have to climb the tree.
1964 while Parent (N) /= CV loop
1967 -- If we fall off the top of the tree, then that's odd, but
1968 -- perhaps it could occur in some error situation, and the
1969 -- safest response is simply to assume that the outcome of
1970 -- the condition is unknown. No point in bombing during an
1971 -- attempt to optimize things.
1978 -- Now we have N pointing to a node whose parent is the IF
1979 -- statement in question, so now we can tell if we are within
1980 -- the THEN statements.
1982 if Is_List_Member (N)
1983 and then List_Containing (N) = Then_Statements (CV)
1987 -- If the variable reference does not come from source, we
1988 -- cannot reliably tell whether it appears in the else part.
1989 -- In particular, if it appears in generated code for a node
1990 -- that requires finalization, it may be attached to a list
1991 -- that has not been yet inserted into the code. For now,
1992 -- treat it as unknown.
1994 elsif not Comes_From_Source (N) then
1997 -- Otherwise we must be in ELSIF or ELSE part
2004 -- ELSIF part. Condition is known true within the referenced
2005 -- ELSIF, known False in any subsequent ELSIF or ELSE part, and
2006 -- unknown before the ELSE part or after the IF statement.
2008 elsif Nkind (CV) = N_Elsif_Part then
2010 -- if the Elsif_Part had condition_actions, the elsif has been
2011 -- rewritten as a nested if, and the original elsif_part is
2012 -- detached from the tree, so there is no way to obtain useful
2013 -- information on the current value of the variable.
2014 -- Can this be improved ???
2016 if No (Parent (CV)) then
2022 -- Before start of ELSIF part
2024 if Loc < Sloc (CV) then
2027 -- After end of IF statement
2029 elsif Loc >= Sloc (Stm) +
2030 Text_Ptr (UI_To_Int (End_Span (Stm)))
2035 -- Again we lack the SLOC of the ELSE, so we need to climb the
2036 -- tree to see if we are within the ELSIF part in question.
2043 while Parent (N) /= Stm loop
2046 -- If we fall off the top of the tree, then that's odd, but
2047 -- perhaps it could occur in some error situation, and the
2048 -- safest response is simply to assume that the outcome of
2049 -- the condition is unknown. No point in bombing during an
2050 -- attempt to optimize things.
2057 -- Now we have N pointing to a node whose parent is the IF
2058 -- statement in question, so see if is the ELSIF part we want.
2059 -- the THEN statements.
2064 -- Otherwise we must be in subsequent ELSIF or ELSE part
2071 -- Iteration scheme of while loop. The condition is known to be
2072 -- true within the body of the loop.
2074 elsif Nkind (CV) = N_Iteration_Scheme then
2076 Loop_Stmt : constant Node_Id := Parent (CV);
2079 -- Before start of body of loop
2081 if Loc < Sloc (Loop_Stmt) then
2084 -- After end of LOOP statement
2086 elsif Loc >= Sloc (End_Label (Loop_Stmt)) then
2089 -- We are within the body of the loop
2096 -- All other cases of Current_Value settings
2102 -- If we fall through here, then we have a reportable condition, Sens
2103 -- is True if the condition is true and False if it needs inverting.
2105 Process_Current_Value_Condition (Condition (CV), Sens);
2107 end Get_Current_Value_Condition;
2109 ---------------------------------
2110 -- Has_Controlled_Coextensions --
2111 ---------------------------------
2113 function Has_Controlled_Coextensions (Typ : Entity_Id) return Boolean is
2118 -- Only consider record types
2120 if not Ekind_In (Typ, E_Record_Type, E_Record_Subtype) then
2124 if Has_Discriminants (Typ) then
2125 Discr := First_Discriminant (Typ);
2126 while Present (Discr) loop
2127 D_Typ := Etype (Discr);
2129 if Ekind (D_Typ) = E_Anonymous_Access_Type
2131 (Is_Controlled (Designated_Type (D_Typ))
2133 Is_Concurrent_Type (Designated_Type (D_Typ)))
2138 Next_Discriminant (Discr);
2143 end Has_Controlled_Coextensions;
2145 ------------------------
2146 -- Has_Address_Clause --
2147 ------------------------
2149 -- Should this function check the private part in a package ???
2151 function Has_Following_Address_Clause (D : Node_Id) return Boolean is
2152 Id : constant Entity_Id := Defining_Identifier (D);
2157 while Present (Decl) loop
2158 if Nkind (Decl) = N_At_Clause
2159 and then Chars (Identifier (Decl)) = Chars (Id)
2163 elsif Nkind (Decl) = N_Attribute_Definition_Clause
2164 and then Chars (Decl) = Name_Address
2165 and then Chars (Name (Decl)) = Chars (Id)
2174 end Has_Following_Address_Clause;
2176 --------------------
2177 -- Homonym_Number --
2178 --------------------
2180 function Homonym_Number (Subp : Entity_Id) return Nat is
2186 Hom := Homonym (Subp);
2187 while Present (Hom) loop
2188 if Scope (Hom) = Scope (Subp) then
2192 Hom := Homonym (Hom);
2198 ------------------------------
2199 -- In_Unconditional_Context --
2200 ------------------------------
2202 function In_Unconditional_Context (Node : Node_Id) return Boolean is
2207 while Present (P) loop
2209 when N_Subprogram_Body =>
2212 when N_If_Statement =>
2215 when N_Loop_Statement =>
2218 when N_Case_Statement =>
2227 end In_Unconditional_Context;
2233 procedure Insert_Action (Assoc_Node : Node_Id; Ins_Action : Node_Id) is
2235 if Present (Ins_Action) then
2236 Insert_Actions (Assoc_Node, New_List (Ins_Action));
2240 -- Version with check(s) suppressed
2242 procedure Insert_Action
2243 (Assoc_Node : Node_Id; Ins_Action : Node_Id; Suppress : Check_Id)
2246 Insert_Actions (Assoc_Node, New_List (Ins_Action), Suppress);
2249 --------------------
2250 -- Insert_Actions --
2251 --------------------
2253 procedure Insert_Actions (Assoc_Node : Node_Id; Ins_Actions : List_Id) is
2257 Wrapped_Node : Node_Id := Empty;
2260 if No (Ins_Actions) or else Is_Empty_List (Ins_Actions) then
2264 -- Ignore insert of actions from inside default expression (or other
2265 -- similar "spec expression") in the special spec-expression analyze
2266 -- mode. Any insertions at this point have no relevance, since we are
2267 -- only doing the analyze to freeze the types of any static expressions.
2268 -- See section "Handling of Default Expressions" in the spec of package
2269 -- Sem for further details.
2271 if In_Spec_Expression then
2275 -- If the action derives from stuff inside a record, then the actions
2276 -- are attached to the current scope, to be inserted and analyzed on
2277 -- exit from the scope. The reason for this is that we may also
2278 -- be generating freeze actions at the same time, and they must
2279 -- eventually be elaborated in the correct order.
2281 if Is_Record_Type (Current_Scope)
2282 and then not Is_Frozen (Current_Scope)
2284 if No (Scope_Stack.Table
2285 (Scope_Stack.Last).Pending_Freeze_Actions)
2287 Scope_Stack.Table (Scope_Stack.Last).Pending_Freeze_Actions :=
2292 Scope_Stack.Table (Scope_Stack.Last).Pending_Freeze_Actions);
2298 -- We now intend to climb up the tree to find the right point to
2299 -- insert the actions. We start at Assoc_Node, unless this node is
2300 -- a subexpression in which case we start with its parent. We do this
2301 -- for two reasons. First it speeds things up. Second, if Assoc_Node
2302 -- is itself one of the special nodes like N_And_Then, then we assume
2303 -- that an initial request to insert actions for such a node does not
2304 -- expect the actions to get deposited in the node for later handling
2305 -- when the node is expanded, since clearly the node is being dealt
2306 -- with by the caller. Note that in the subexpression case, N is
2307 -- always the child we came from.
2309 -- N_Raise_xxx_Error is an annoying special case, it is a statement
2310 -- if it has type Standard_Void_Type, and a subexpression otherwise.
2311 -- otherwise. Procedure attribute references are also statements.
2313 if Nkind (Assoc_Node) in N_Subexpr
2314 and then (Nkind (Assoc_Node) in N_Raise_xxx_Error
2315 or else Etype (Assoc_Node) /= Standard_Void_Type)
2316 and then (Nkind (Assoc_Node) /= N_Attribute_Reference
2318 not Is_Procedure_Attribute_Name
2319 (Attribute_Name (Assoc_Node)))
2321 P := Assoc_Node; -- ??? does not agree with above!
2322 N := Parent (Assoc_Node);
2324 -- Non-subexpression case. Note that N is initially Empty in this
2325 -- case (N is only guaranteed Non-Empty in the subexpr case).
2332 -- Capture root of the transient scope
2334 if Scope_Is_Transient then
2335 Wrapped_Node := Node_To_Be_Wrapped;
2339 pragma Assert (Present (P));
2343 -- Case of right operand of AND THEN or OR ELSE. Put the actions
2344 -- in the Actions field of the right operand. They will be moved
2345 -- out further when the AND THEN or OR ELSE operator is expanded.
2346 -- Nothing special needs to be done for the left operand since
2347 -- in that case the actions are executed unconditionally.
2349 when N_Short_Circuit =>
2350 if N = Right_Opnd (P) then
2352 -- We are now going to either append the actions to the
2353 -- actions field of the short-circuit operation. We will
2354 -- also analyze the actions now.
2356 -- This analysis is really too early, the proper thing would
2357 -- be to just park them there now, and only analyze them if
2358 -- we find we really need them, and to it at the proper
2359 -- final insertion point. However attempting to this proved
2360 -- tricky, so for now we just kill current values before and
2361 -- after the analyze call to make sure we avoid peculiar
2362 -- optimizations from this out of order insertion.
2364 Kill_Current_Values;
2366 if Present (Actions (P)) then
2367 Insert_List_After_And_Analyze
2368 (Last (Actions (P)), Ins_Actions);
2370 Set_Actions (P, Ins_Actions);
2371 Analyze_List (Actions (P));
2374 Kill_Current_Values;
2379 -- Then or Else operand of conditional expression. Add actions to
2380 -- Then_Actions or Else_Actions field as appropriate. The actions
2381 -- will be moved further out when the conditional is expanded.
2383 when N_Conditional_Expression =>
2385 ThenX : constant Node_Id := Next (First (Expressions (P)));
2386 ElseX : constant Node_Id := Next (ThenX);
2389 -- Actions belong to the then expression, temporarily
2390 -- place them as Then_Actions of the conditional expr.
2391 -- They will be moved to the proper place later when
2392 -- the conditional expression is expanded.
2395 if Present (Then_Actions (P)) then
2396 Insert_List_After_And_Analyze
2397 (Last (Then_Actions (P)), Ins_Actions);
2399 Set_Then_Actions (P, Ins_Actions);
2400 Analyze_List (Then_Actions (P));
2405 -- Actions belong to the else expression, temporarily
2406 -- place them as Else_Actions of the conditional expr.
2407 -- They will be moved to the proper place later when
2408 -- the conditional expression is expanded.
2410 elsif N = ElseX then
2411 if Present (Else_Actions (P)) then
2412 Insert_List_After_And_Analyze
2413 (Last (Else_Actions (P)), Ins_Actions);
2415 Set_Else_Actions (P, Ins_Actions);
2416 Analyze_List (Else_Actions (P));
2421 -- Actions belong to the condition. In this case they are
2422 -- unconditionally executed, and so we can continue the
2423 -- search for the proper insert point.
2430 -- Alternative of case expression, we place the action in
2431 -- the Actions field of the case expression alternative, this
2432 -- will be handled when the case expression is expanded.
2434 when N_Case_Expression_Alternative =>
2435 if Present (Actions (P)) then
2436 Insert_List_After_And_Analyze
2437 (Last (Actions (P)), Ins_Actions);
2439 Set_Actions (P, Ins_Actions);
2440 Analyze_List (Then_Actions (P));
2445 -- Case of appearing within an Expressions_With_Actions node. We
2446 -- prepend the actions to the list of actions already there.
2448 when N_Expression_With_Actions =>
2449 Prepend_List (Ins_Actions, Actions (P));
2452 -- Case of appearing in the condition of a while expression or
2453 -- elsif. We insert the actions into the Condition_Actions field.
2454 -- They will be moved further out when the while loop or elsif
2457 when N_Iteration_Scheme |
2460 if N = Condition (P) then
2461 if Present (Condition_Actions (P)) then
2462 Insert_List_After_And_Analyze
2463 (Last (Condition_Actions (P)), Ins_Actions);
2465 Set_Condition_Actions (P, Ins_Actions);
2467 -- Set the parent of the insert actions explicitly.
2468 -- This is not a syntactic field, but we need the
2469 -- parent field set, in particular so that freeze
2470 -- can understand that it is dealing with condition
2471 -- actions, and properly insert the freezing actions.
2473 Set_Parent (Ins_Actions, P);
2474 Analyze_List (Condition_Actions (P));
2480 -- Statements, declarations, pragmas, representation clauses
2485 N_Procedure_Call_Statement |
2486 N_Statement_Other_Than_Procedure_Call |
2492 -- Representation_Clause
2495 N_Attribute_Definition_Clause |
2496 N_Enumeration_Representation_Clause |
2497 N_Record_Representation_Clause |
2501 N_Abstract_Subprogram_Declaration |
2503 N_Exception_Declaration |
2504 N_Exception_Renaming_Declaration |
2505 N_Formal_Abstract_Subprogram_Declaration |
2506 N_Formal_Concrete_Subprogram_Declaration |
2507 N_Formal_Object_Declaration |
2508 N_Formal_Type_Declaration |
2509 N_Full_Type_Declaration |
2510 N_Function_Instantiation |
2511 N_Generic_Function_Renaming_Declaration |
2512 N_Generic_Package_Declaration |
2513 N_Generic_Package_Renaming_Declaration |
2514 N_Generic_Procedure_Renaming_Declaration |
2515 N_Generic_Subprogram_Declaration |
2516 N_Implicit_Label_Declaration |
2517 N_Incomplete_Type_Declaration |
2518 N_Number_Declaration |
2519 N_Object_Declaration |
2520 N_Object_Renaming_Declaration |
2522 N_Package_Body_Stub |
2523 N_Package_Declaration |
2524 N_Package_Instantiation |
2525 N_Package_Renaming_Declaration |
2526 N_Private_Extension_Declaration |
2527 N_Private_Type_Declaration |
2528 N_Procedure_Instantiation |
2530 N_Protected_Body_Stub |
2531 N_Protected_Type_Declaration |
2532 N_Single_Task_Declaration |
2534 N_Subprogram_Body_Stub |
2535 N_Subprogram_Declaration |
2536 N_Subprogram_Renaming_Declaration |
2537 N_Subtype_Declaration |
2540 N_Task_Type_Declaration |
2542 -- Freeze entity behaves like a declaration or statement
2546 -- Do not insert here if the item is not a list member (this
2547 -- happens for example with a triggering statement, and the
2548 -- proper approach is to insert before the entire select).
2550 if not Is_List_Member (P) then
2553 -- Do not insert if parent of P is an N_Component_Association
2554 -- node (i.e. we are in the context of an N_Aggregate or
2555 -- N_Extension_Aggregate node. In this case we want to insert
2556 -- before the entire aggregate.
2558 elsif Nkind (Parent (P)) = N_Component_Association then
2561 -- Do not insert if the parent of P is either an N_Variant
2562 -- node or an N_Record_Definition node, meaning in either
2563 -- case that P is a member of a component list, and that
2564 -- therefore the actions should be inserted outside the
2565 -- complete record declaration.
2567 elsif Nkind (Parent (P)) = N_Variant
2568 or else Nkind (Parent (P)) = N_Record_Definition
2572 -- Do not insert freeze nodes within the loop generated for
2573 -- an aggregate, because they may be elaborated too late for
2574 -- subsequent use in the back end: within a package spec the
2575 -- loop is part of the elaboration procedure and is only
2576 -- elaborated during the second pass.
2577 -- If the loop comes from source, or the entity is local to
2578 -- the loop itself it must remain within.
2580 elsif Nkind (Parent (P)) = N_Loop_Statement
2581 and then not Comes_From_Source (Parent (P))
2582 and then Nkind (First (Ins_Actions)) = N_Freeze_Entity
2584 Scope (Entity (First (Ins_Actions))) /= Current_Scope
2588 -- Otherwise we can go ahead and do the insertion
2590 elsif P = Wrapped_Node then
2591 Store_Before_Actions_In_Scope (Ins_Actions);
2595 Insert_List_Before_And_Analyze (P, Ins_Actions);
2599 -- A special case, N_Raise_xxx_Error can act either as a
2600 -- statement or a subexpression. We tell the difference
2601 -- by looking at the Etype. It is set to Standard_Void_Type
2602 -- in the statement case.
2605 N_Raise_xxx_Error =>
2606 if Etype (P) = Standard_Void_Type then
2607 if P = Wrapped_Node then
2608 Store_Before_Actions_In_Scope (Ins_Actions);
2610 Insert_List_Before_And_Analyze (P, Ins_Actions);
2615 -- In the subexpression case, keep climbing
2621 -- If a component association appears within a loop created for
2622 -- an array aggregate, attach the actions to the association so
2623 -- they can be subsequently inserted within the loop. For other
2624 -- component associations insert outside of the aggregate. For
2625 -- an association that will generate a loop, its Loop_Actions
2626 -- attribute is already initialized (see exp_aggr.adb).
2628 -- The list of loop_actions can in turn generate additional ones,
2629 -- that are inserted before the associated node. If the associated
2630 -- node is outside the aggregate, the new actions are collected
2631 -- at the end of the loop actions, to respect the order in which
2632 -- they are to be elaborated.
2635 N_Component_Association =>
2636 if Nkind (Parent (P)) = N_Aggregate
2637 and then Present (Loop_Actions (P))
2639 if Is_Empty_List (Loop_Actions (P)) then
2640 Set_Loop_Actions (P, Ins_Actions);
2641 Analyze_List (Ins_Actions);
2648 -- Check whether these actions were generated
2649 -- by a declaration that is part of the loop_
2650 -- actions for the component_association.
2653 while Present (Decl) loop
2654 exit when Parent (Decl) = P
2655 and then Is_List_Member (Decl)
2657 List_Containing (Decl) = Loop_Actions (P);
2658 Decl := Parent (Decl);
2661 if Present (Decl) then
2662 Insert_List_Before_And_Analyze
2663 (Decl, Ins_Actions);
2665 Insert_List_After_And_Analyze
2666 (Last (Loop_Actions (P)), Ins_Actions);
2677 -- Another special case, an attribute denoting a procedure call
2680 N_Attribute_Reference =>
2681 if Is_Procedure_Attribute_Name (Attribute_Name (P)) then
2682 if P = Wrapped_Node then
2683 Store_Before_Actions_In_Scope (Ins_Actions);
2685 Insert_List_Before_And_Analyze (P, Ins_Actions);
2690 -- In the subexpression case, keep climbing
2696 -- For all other node types, keep climbing tree
2700 N_Accept_Alternative |
2701 N_Access_Definition |
2702 N_Access_Function_Definition |
2703 N_Access_Procedure_Definition |
2704 N_Access_To_Object_Definition |
2708 N_Case_Statement_Alternative |
2709 N_Character_Literal |
2710 N_Compilation_Unit |
2711 N_Compilation_Unit_Aux |
2712 N_Component_Clause |
2713 N_Component_Declaration |
2714 N_Component_Definition |
2716 N_Constrained_Array_Definition |
2717 N_Decimal_Fixed_Point_Definition |
2718 N_Defining_Character_Literal |
2719 N_Defining_Identifier |
2720 N_Defining_Operator_Symbol |
2721 N_Defining_Program_Unit_Name |
2722 N_Delay_Alternative |
2723 N_Delta_Constraint |
2724 N_Derived_Type_Definition |
2726 N_Digits_Constraint |
2727 N_Discriminant_Association |
2728 N_Discriminant_Specification |
2730 N_Entry_Body_Formal_Part |
2731 N_Entry_Call_Alternative |
2732 N_Entry_Declaration |
2733 N_Entry_Index_Specification |
2734 N_Enumeration_Type_Definition |
2736 N_Exception_Handler |
2738 N_Explicit_Dereference |
2739 N_Extension_Aggregate |
2740 N_Floating_Point_Definition |
2741 N_Formal_Decimal_Fixed_Point_Definition |
2742 N_Formal_Derived_Type_Definition |
2743 N_Formal_Discrete_Type_Definition |
2744 N_Formal_Floating_Point_Definition |
2745 N_Formal_Modular_Type_Definition |
2746 N_Formal_Ordinary_Fixed_Point_Definition |
2747 N_Formal_Package_Declaration |
2748 N_Formal_Private_Type_Definition |
2749 N_Formal_Signed_Integer_Type_Definition |
2751 N_Function_Specification |
2752 N_Generic_Association |
2753 N_Handled_Sequence_Of_Statements |
2756 N_Index_Or_Discriminant_Constraint |
2757 N_Indexed_Component |
2761 N_Loop_Parameter_Specification |
2763 N_Modular_Type_Definition |
2789 N_Op_Shift_Right_Arithmetic |
2793 N_Ordinary_Fixed_Point_Definition |
2795 N_Package_Specification |
2796 N_Parameter_Association |
2797 N_Parameter_Specification |
2798 N_Pop_Constraint_Error_Label |
2799 N_Pop_Program_Error_Label |
2800 N_Pop_Storage_Error_Label |
2801 N_Pragma_Argument_Association |
2802 N_Procedure_Specification |
2803 N_Protected_Definition |
2804 N_Push_Constraint_Error_Label |
2805 N_Push_Program_Error_Label |
2806 N_Push_Storage_Error_Label |
2807 N_Qualified_Expression |
2809 N_Range_Constraint |
2811 N_Real_Range_Specification |
2812 N_Record_Definition |
2814 N_SCIL_Dispatch_Table_Tag_Init |
2815 N_SCIL_Dispatching_Call |
2816 N_SCIL_Membership_Test |
2817 N_Selected_Component |
2818 N_Signed_Integer_Type_Definition |
2819 N_Single_Protected_Declaration |
2823 N_Subtype_Indication |
2826 N_Terminate_Alternative |
2827 N_Triggering_Alternative |
2829 N_Unchecked_Expression |
2830 N_Unchecked_Type_Conversion |
2831 N_Unconstrained_Array_Definition |
2834 N_Use_Package_Clause |
2838 N_Validate_Unchecked_Conversion |
2845 -- Make sure that inserted actions stay in the transient scope
2847 if P = Wrapped_Node then
2848 Store_Before_Actions_In_Scope (Ins_Actions);
2852 -- If we fall through above tests, keep climbing tree
2856 if Nkind (Parent (N)) = N_Subunit then
2858 -- This is the proper body corresponding to a stub. Insertion
2859 -- must be done at the point of the stub, which is in the decla-
2860 -- rative part of the parent unit.
2862 P := Corresponding_Stub (Parent (N));
2870 -- Version with check(s) suppressed
2872 procedure Insert_Actions
2873 (Assoc_Node : Node_Id;
2874 Ins_Actions : List_Id;
2875 Suppress : Check_Id)
2878 if Suppress = All_Checks then
2880 Svg : constant Suppress_Array := Scope_Suppress;
2882 Scope_Suppress := (others => True);
2883 Insert_Actions (Assoc_Node, Ins_Actions);
2884 Scope_Suppress := Svg;
2889 Svg : constant Boolean := Scope_Suppress (Suppress);
2891 Scope_Suppress (Suppress) := True;
2892 Insert_Actions (Assoc_Node, Ins_Actions);
2893 Scope_Suppress (Suppress) := Svg;
2898 --------------------------
2899 -- Insert_Actions_After --
2900 --------------------------
2902 procedure Insert_Actions_After
2903 (Assoc_Node : Node_Id;
2904 Ins_Actions : List_Id)
2907 if Scope_Is_Transient
2908 and then Assoc_Node = Node_To_Be_Wrapped
2910 Store_After_Actions_In_Scope (Ins_Actions);
2912 Insert_List_After_And_Analyze (Assoc_Node, Ins_Actions);
2914 end Insert_Actions_After;
2916 ---------------------------------
2917 -- Insert_Library_Level_Action --
2918 ---------------------------------
2920 procedure Insert_Library_Level_Action (N : Node_Id) is
2921 Aux : constant Node_Id := Aux_Decls_Node (Cunit (Main_Unit));
2924 Push_Scope (Cunit_Entity (Main_Unit));
2925 -- ??? should this be Current_Sem_Unit instead of Main_Unit?
2927 if No (Actions (Aux)) then
2928 Set_Actions (Aux, New_List (N));
2930 Append (N, Actions (Aux));
2935 end Insert_Library_Level_Action;
2937 ----------------------------------
2938 -- Insert_Library_Level_Actions --
2939 ----------------------------------
2941 procedure Insert_Library_Level_Actions (L : List_Id) is
2942 Aux : constant Node_Id := Aux_Decls_Node (Cunit (Main_Unit));
2945 if Is_Non_Empty_List (L) then
2946 Push_Scope (Cunit_Entity (Main_Unit));
2947 -- ??? should this be Current_Sem_Unit instead of Main_Unit?
2949 if No (Actions (Aux)) then
2950 Set_Actions (Aux, L);
2953 Insert_List_After_And_Analyze (Last (Actions (Aux)), L);
2958 end Insert_Library_Level_Actions;
2960 ----------------------
2961 -- Inside_Init_Proc --
2962 ----------------------
2964 function Inside_Init_Proc return Boolean is
2970 and then S /= Standard_Standard
2972 if Is_Init_Proc (S) then
2980 end Inside_Init_Proc;
2982 ----------------------------
2983 -- Is_All_Null_Statements --
2984 ----------------------------
2986 function Is_All_Null_Statements (L : List_Id) return Boolean is
2991 while Present (Stm) loop
2992 if Nkind (Stm) /= N_Null_Statement then
3000 end Is_All_Null_Statements;
3002 ---------------------------------
3003 -- Is_Fully_Repped_Tagged_Type --
3004 ---------------------------------
3006 function Is_Fully_Repped_Tagged_Type (T : Entity_Id) return Boolean is
3007 U : constant Entity_Id := Underlying_Type (T);
3011 if No (U) or else not Is_Tagged_Type (U) then
3013 elsif Has_Discriminants (U) then
3015 elsif not Has_Specified_Layout (U) then
3019 -- Here we have a tagged type, see if it has any unlayed out fields
3020 -- other than a possible tag and parent fields. If so, we return False.
3022 Comp := First_Component (U);
3023 while Present (Comp) loop
3024 if not Is_Tag (Comp)
3025 and then Chars (Comp) /= Name_uParent
3026 and then No (Component_Clause (Comp))
3030 Next_Component (Comp);
3034 -- All components are layed out
3037 end Is_Fully_Repped_Tagged_Type;
3039 ----------------------------------
3040 -- Is_Library_Level_Tagged_Type --
3041 ----------------------------------
3043 function Is_Library_Level_Tagged_Type (Typ : Entity_Id) return Boolean is
3045 return Is_Tagged_Type (Typ)
3046 and then Is_Library_Level_Entity (Typ);
3047 end Is_Library_Level_Tagged_Type;
3049 ----------------------------------
3050 -- Is_Possibly_Unaligned_Object --
3051 ----------------------------------
3053 function Is_Possibly_Unaligned_Object (N : Node_Id) return Boolean is
3054 T : constant Entity_Id := Etype (N);
3057 -- If renamed object, apply test to underlying object
3059 if Is_Entity_Name (N)
3060 and then Is_Object (Entity (N))
3061 and then Present (Renamed_Object (Entity (N)))
3063 return Is_Possibly_Unaligned_Object (Renamed_Object (Entity (N)));
3066 -- Tagged and controlled types and aliased types are always aligned,
3067 -- as are concurrent types.
3070 or else Has_Controlled_Component (T)
3071 or else Is_Concurrent_Type (T)
3072 or else Is_Tagged_Type (T)
3073 or else Is_Controlled (T)
3078 -- If this is an element of a packed array, may be unaligned
3080 if Is_Ref_To_Bit_Packed_Array (N) then
3084 -- Case of component reference
3086 if Nkind (N) = N_Selected_Component then
3088 P : constant Node_Id := Prefix (N);
3089 C : constant Entity_Id := Entity (Selector_Name (N));
3094 -- If component reference is for an array with non-static bounds,
3095 -- then it is always aligned: we can only process unaligned
3096 -- arrays with static bounds (more accurately bounds known at
3099 if Is_Array_Type (T)
3100 and then not Compile_Time_Known_Bounds (T)
3105 -- If component is aliased, it is definitely properly aligned
3107 if Is_Aliased (C) then
3111 -- If component is for a type implemented as a scalar, and the
3112 -- record is packed, and the component is other than the first
3113 -- component of the record, then the component may be unaligned.
3115 if Is_Packed (Etype (P))
3116 and then Represented_As_Scalar (Etype (C))
3117 and then First_Entity (Scope (C)) /= C
3122 -- Compute maximum possible alignment for T
3124 -- If alignment is known, then that settles things
3126 if Known_Alignment (T) then
3127 M := UI_To_Int (Alignment (T));
3129 -- If alignment is not known, tentatively set max alignment
3132 M := Ttypes.Maximum_Alignment;
3134 -- We can reduce this if the Esize is known since the default
3135 -- alignment will never be more than the smallest power of 2
3136 -- that does not exceed this Esize value.
3138 if Known_Esize (T) then
3139 S := UI_To_Int (Esize (T));
3141 while (M / 2) >= S loop
3147 -- The following code is historical, it used to be present but it
3148 -- is too cautious, because the front-end does not know the proper
3149 -- default alignments for the target. Also, if the alignment is
3150 -- not known, the front end can't know in any case! If a copy is
3151 -- needed, the back-end will take care of it. This whole section
3152 -- including this comment can be removed later ???
3154 -- If the component reference is for a record that has a specified
3155 -- alignment, and we either know it is too small, or cannot tell,
3156 -- then the component may be unaligned.
3158 -- if Known_Alignment (Etype (P))
3159 -- and then Alignment (Etype (P)) < Ttypes.Maximum_Alignment
3160 -- and then M > Alignment (Etype (P))
3165 -- Case of component clause present which may specify an
3166 -- unaligned position.
3168 if Present (Component_Clause (C)) then
3170 -- Otherwise we can do a test to make sure that the actual
3171 -- start position in the record, and the length, are both
3172 -- consistent with the required alignment. If not, we know
3173 -- that we are unaligned.
3176 Align_In_Bits : constant Nat := M * System_Storage_Unit;
3178 if Component_Bit_Offset (C) mod Align_In_Bits /= 0
3179 or else Esize (C) mod Align_In_Bits /= 0
3186 -- Otherwise, for a component reference, test prefix
3188 return Is_Possibly_Unaligned_Object (P);
3191 -- If not a component reference, must be aligned
3196 end Is_Possibly_Unaligned_Object;
3198 ---------------------------------
3199 -- Is_Possibly_Unaligned_Slice --
3200 ---------------------------------
3202 function Is_Possibly_Unaligned_Slice (N : Node_Id) return Boolean is
3204 -- Go to renamed object
3206 if Is_Entity_Name (N)
3207 and then Is_Object (Entity (N))
3208 and then Present (Renamed_Object (Entity (N)))
3210 return Is_Possibly_Unaligned_Slice (Renamed_Object (Entity (N)));
3213 -- The reference must be a slice
3215 if Nkind (N) /= N_Slice then
3219 -- Always assume the worst for a nested record component with a
3220 -- component clause, which gigi/gcc does not appear to handle well.
3221 -- It is not clear why this special test is needed at all ???
3223 if Nkind (Prefix (N)) = N_Selected_Component
3224 and then Nkind (Prefix (Prefix (N))) = N_Selected_Component
3226 Present (Component_Clause (Entity (Selector_Name (Prefix (N)))))
3231 -- We only need to worry if the target has strict alignment
3233 if not Target_Strict_Alignment then
3237 -- If it is a slice, then look at the array type being sliced
3240 Sarr : constant Node_Id := Prefix (N);
3241 -- Prefix of the slice, i.e. the array being sliced
3243 Styp : constant Entity_Id := Etype (Prefix (N));
3244 -- Type of the array being sliced
3250 -- The problems arise if the array object that is being sliced
3251 -- is a component of a record or array, and we cannot guarantee
3252 -- the alignment of the array within its containing object.
3254 -- To investigate this, we look at successive prefixes to see
3255 -- if we have a worrisome indexed or selected component.
3259 -- Case of array is part of an indexed component reference
3261 if Nkind (Pref) = N_Indexed_Component then
3262 Ptyp := Etype (Prefix (Pref));
3264 -- The only problematic case is when the array is packed,
3265 -- in which case we really know nothing about the alignment
3266 -- of individual components.
3268 if Is_Bit_Packed_Array (Ptyp) then
3272 -- Case of array is part of a selected component reference
3274 elsif Nkind (Pref) = N_Selected_Component then
3275 Ptyp := Etype (Prefix (Pref));
3277 -- We are definitely in trouble if the record in question
3278 -- has an alignment, and either we know this alignment is
3279 -- inconsistent with the alignment of the slice, or we
3280 -- don't know what the alignment of the slice should be.
3282 if Known_Alignment (Ptyp)
3283 and then (Unknown_Alignment (Styp)
3284 or else Alignment (Styp) > Alignment (Ptyp))
3289 -- We are in potential trouble if the record type is packed.
3290 -- We could special case when we know that the array is the
3291 -- first component, but that's not such a simple case ???
3293 if Is_Packed (Ptyp) then
3297 -- We are in trouble if there is a component clause, and
3298 -- either we do not know the alignment of the slice, or
3299 -- the alignment of the slice is inconsistent with the
3300 -- bit position specified by the component clause.
3303 Field : constant Entity_Id := Entity (Selector_Name (Pref));
3305 if Present (Component_Clause (Field))
3307 (Unknown_Alignment (Styp)
3309 (Component_Bit_Offset (Field) mod
3310 (System_Storage_Unit * Alignment (Styp))) /= 0)
3316 -- For cases other than selected or indexed components we
3317 -- know we are OK, since no issues arise over alignment.
3323 -- We processed an indexed component or selected component
3324 -- reference that looked safe, so keep checking prefixes.
3326 Pref := Prefix (Pref);
3329 end Is_Possibly_Unaligned_Slice;
3331 --------------------------------
3332 -- Is_Ref_To_Bit_Packed_Array --
3333 --------------------------------
3335 function Is_Ref_To_Bit_Packed_Array (N : Node_Id) return Boolean is
3340 if Is_Entity_Name (N)
3341 and then Is_Object (Entity (N))
3342 and then Present (Renamed_Object (Entity (N)))
3344 return Is_Ref_To_Bit_Packed_Array (Renamed_Object (Entity (N)));
3347 if Nkind (N) = N_Indexed_Component
3349 Nkind (N) = N_Selected_Component
3351 if Is_Bit_Packed_Array (Etype (Prefix (N))) then
3354 Result := Is_Ref_To_Bit_Packed_Array (Prefix (N));
3357 if Result and then Nkind (N) = N_Indexed_Component then
3358 Expr := First (Expressions (N));
3359 while Present (Expr) loop
3360 Force_Evaluation (Expr);
3370 end Is_Ref_To_Bit_Packed_Array;
3372 --------------------------------
3373 -- Is_Ref_To_Bit_Packed_Slice --
3374 --------------------------------
3376 function Is_Ref_To_Bit_Packed_Slice (N : Node_Id) return Boolean is
3378 if Nkind (N) = N_Type_Conversion then
3379 return Is_Ref_To_Bit_Packed_Slice (Expression (N));
3381 elsif Is_Entity_Name (N)
3382 and then Is_Object (Entity (N))
3383 and then Present (Renamed_Object (Entity (N)))
3385 return Is_Ref_To_Bit_Packed_Slice (Renamed_Object (Entity (N)));
3387 elsif Nkind (N) = N_Slice
3388 and then Is_Bit_Packed_Array (Etype (Prefix (N)))
3392 elsif Nkind (N) = N_Indexed_Component
3394 Nkind (N) = N_Selected_Component
3396 return Is_Ref_To_Bit_Packed_Slice (Prefix (N));
3401 end Is_Ref_To_Bit_Packed_Slice;
3403 -----------------------
3404 -- Is_Renamed_Object --
3405 -----------------------
3407 function Is_Renamed_Object (N : Node_Id) return Boolean is
3408 Pnod : constant Node_Id := Parent (N);
3409 Kind : constant Node_Kind := Nkind (Pnod);
3411 if Kind = N_Object_Renaming_Declaration then
3413 elsif Nkind_In (Kind, N_Indexed_Component, N_Selected_Component) then
3414 return Is_Renamed_Object (Pnod);
3418 end Is_Renamed_Object;
3420 ----------------------------
3421 -- Is_Untagged_Derivation --
3422 ----------------------------
3424 function Is_Untagged_Derivation (T : Entity_Id) return Boolean is
3426 return (not Is_Tagged_Type (T) and then Is_Derived_Type (T))
3428 (Is_Private_Type (T) and then Present (Full_View (T))
3429 and then not Is_Tagged_Type (Full_View (T))
3430 and then Is_Derived_Type (Full_View (T))
3431 and then Etype (Full_View (T)) /= T);
3432 end Is_Untagged_Derivation;
3434 ---------------------------
3435 -- Is_Volatile_Reference --
3436 ---------------------------
3438 function Is_Volatile_Reference (N : Node_Id) return Boolean is
3440 if Nkind (N) in N_Has_Etype
3441 and then Present (Etype (N))
3442 and then Treat_As_Volatile (Etype (N))
3446 elsif Is_Entity_Name (N) then
3447 return Treat_As_Volatile (Entity (N));
3449 elsif Nkind (N) = N_Slice then
3450 return Is_Volatile_Reference (Prefix (N));
3452 elsif Nkind_In (N, N_Indexed_Component, N_Selected_Component) then
3453 if (Is_Entity_Name (Prefix (N))
3454 and then Has_Volatile_Components (Entity (Prefix (N))))
3455 or else (Present (Etype (Prefix (N)))
3456 and then Has_Volatile_Components (Etype (Prefix (N))))
3460 return Is_Volatile_Reference (Prefix (N));
3466 end Is_Volatile_Reference;
3468 --------------------
3469 -- Kill_Dead_Code --
3470 --------------------
3472 procedure Kill_Dead_Code (N : Node_Id; Warn : Boolean := False) is
3473 W : Boolean := Warn;
3474 -- Set False if warnings suppressed
3478 Remove_Warning_Messages (N);
3480 -- Generate warning if appropriate
3484 -- We suppress the warning if this code is under control of an
3485 -- if statement, whose condition is a simple identifier, and
3486 -- either we are in an instance, or warnings off is set for this
3487 -- identifier. The reason for killing it in the instance case is
3488 -- that it is common and reasonable for code to be deleted in
3489 -- instances for various reasons.
3491 if Nkind (Parent (N)) = N_If_Statement then
3493 C : constant Node_Id := Condition (Parent (N));
3495 if Nkind (C) = N_Identifier
3498 or else (Present (Entity (C))
3499 and then Has_Warnings_Off (Entity (C))))
3506 -- Generate warning if not suppressed
3510 ("?this code can never be executed and has been deleted!", N);
3514 -- Recurse into block statements and bodies to process declarations
3517 if Nkind (N) = N_Block_Statement
3518 or else Nkind (N) = N_Subprogram_Body
3519 or else Nkind (N) = N_Package_Body
3521 Kill_Dead_Code (Declarations (N), False);
3522 Kill_Dead_Code (Statements (Handled_Statement_Sequence (N)));
3524 if Nkind (N) = N_Subprogram_Body then
3525 Set_Is_Eliminated (Defining_Entity (N));
3528 elsif Nkind (N) = N_Package_Declaration then
3529 Kill_Dead_Code (Visible_Declarations (Specification (N)));
3530 Kill_Dead_Code (Private_Declarations (Specification (N)));
3532 -- ??? After this point, Delete_Tree has been called on all
3533 -- declarations in Specification (N), so references to
3534 -- entities therein look suspicious.
3537 E : Entity_Id := First_Entity (Defining_Entity (N));
3539 while Present (E) loop
3540 if Ekind (E) = E_Operator then
3541 Set_Is_Eliminated (E);
3548 -- Recurse into composite statement to kill individual statements,
3549 -- in particular instantiations.
3551 elsif Nkind (N) = N_If_Statement then
3552 Kill_Dead_Code (Then_Statements (N));
3553 Kill_Dead_Code (Elsif_Parts (N));
3554 Kill_Dead_Code (Else_Statements (N));
3556 elsif Nkind (N) = N_Loop_Statement then
3557 Kill_Dead_Code (Statements (N));
3559 elsif Nkind (N) = N_Case_Statement then
3563 Alt := First (Alternatives (N));
3564 while Present (Alt) loop
3565 Kill_Dead_Code (Statements (Alt));
3570 elsif Nkind (N) = N_Case_Statement_Alternative then
3571 Kill_Dead_Code (Statements (N));
3573 -- Deal with dead instances caused by deleting instantiations
3575 elsif Nkind (N) in N_Generic_Instantiation then
3576 Remove_Dead_Instance (N);
3581 -- Case where argument is a list of nodes to be killed
3583 procedure Kill_Dead_Code (L : List_Id; Warn : Boolean := False) is
3588 if Is_Non_Empty_List (L) then
3590 while Present (N) loop
3591 Kill_Dead_Code (N, W);
3598 ------------------------
3599 -- Known_Non_Negative --
3600 ------------------------
3602 function Known_Non_Negative (Opnd : Node_Id) return Boolean is
3604 if Is_OK_Static_Expression (Opnd)
3605 and then Expr_Value (Opnd) >= 0
3611 Lo : constant Node_Id := Type_Low_Bound (Etype (Opnd));
3615 Is_OK_Static_Expression (Lo) and then Expr_Value (Lo) >= 0;
3618 end Known_Non_Negative;
3620 --------------------
3621 -- Known_Non_Null --
3622 --------------------
3624 function Known_Non_Null (N : Node_Id) return Boolean is
3626 -- Checks for case where N is an entity reference
3628 if Is_Entity_Name (N) and then Present (Entity (N)) then
3630 E : constant Entity_Id := Entity (N);
3635 -- First check if we are in decisive conditional
3637 Get_Current_Value_Condition (N, Op, Val);
3639 if Known_Null (Val) then
3640 if Op = N_Op_Eq then
3642 elsif Op = N_Op_Ne then
3647 -- If OK to do replacement, test Is_Known_Non_Null flag
3649 if OK_To_Do_Constant_Replacement (E) then
3650 return Is_Known_Non_Null (E);
3652 -- Otherwise if not safe to do replacement, then say so
3659 -- True if access attribute
3661 elsif Nkind (N) = N_Attribute_Reference
3662 and then (Attribute_Name (N) = Name_Access
3664 Attribute_Name (N) = Name_Unchecked_Access
3666 Attribute_Name (N) = Name_Unrestricted_Access)
3670 -- True if allocator
3672 elsif Nkind (N) = N_Allocator then
3675 -- For a conversion, true if expression is known non-null
3677 elsif Nkind (N) = N_Type_Conversion then
3678 return Known_Non_Null (Expression (N));
3680 -- Above are all cases where the value could be determined to be
3681 -- non-null. In all other cases, we don't know, so return False.
3692 function Known_Null (N : Node_Id) return Boolean is
3694 -- Checks for case where N is an entity reference
3696 if Is_Entity_Name (N) and then Present (Entity (N)) then
3698 E : constant Entity_Id := Entity (N);
3703 -- Constant null value is for sure null
3705 if Ekind (E) = E_Constant
3706 and then Known_Null (Constant_Value (E))
3711 -- First check if we are in decisive conditional
3713 Get_Current_Value_Condition (N, Op, Val);
3715 if Known_Null (Val) then
3716 if Op = N_Op_Eq then
3718 elsif Op = N_Op_Ne then
3723 -- If OK to do replacement, test Is_Known_Null flag
3725 if OK_To_Do_Constant_Replacement (E) then
3726 return Is_Known_Null (E);
3728 -- Otherwise if not safe to do replacement, then say so
3735 -- True if explicit reference to null
3737 elsif Nkind (N) = N_Null then
3740 -- For a conversion, true if expression is known null
3742 elsif Nkind (N) = N_Type_Conversion then
3743 return Known_Null (Expression (N));
3745 -- Above are all cases where the value could be determined to be null.
3746 -- In all other cases, we don't know, so return False.
3753 -----------------------------
3754 -- Make_CW_Equivalent_Type --
3755 -----------------------------
3757 -- Create a record type used as an equivalent of any member of the class
3758 -- which takes its size from exp.
3760 -- Generate the following code:
3762 -- type Equiv_T is record
3763 -- _parent : T (List of discriminant constraints taken from Exp);
3764 -- Ext__50 : Storage_Array (1 .. (Exp'size - Typ'object_size)/8);
3767 -- ??? Note that this type does not guarantee same alignment as all
3770 function Make_CW_Equivalent_Type
3772 E : Node_Id) return Entity_Id
3774 Loc : constant Source_Ptr := Sloc (E);
3775 Root_Typ : constant Entity_Id := Root_Type (T);
3776 List_Def : constant List_Id := Empty_List;
3777 Comp_List : constant List_Id := New_List;
3778 Equiv_Type : Entity_Id;
3779 Range_Type : Entity_Id;
3780 Str_Type : Entity_Id;
3781 Constr_Root : Entity_Id;
3785 -- If the root type is already constrained, there are no discriminants
3786 -- in the expression.
3788 if not Has_Discriminants (Root_Typ)
3789 or else Is_Constrained (Root_Typ)
3791 Constr_Root := Root_Typ;
3793 Constr_Root := Make_Temporary (Loc, 'R');
3795 -- subtype cstr__n is T (List of discr constraints taken from Exp)
3797 Append_To (List_Def,
3798 Make_Subtype_Declaration (Loc,
3799 Defining_Identifier => Constr_Root,
3800 Subtype_Indication => Make_Subtype_From_Expr (E, Root_Typ)));
3803 -- Generate the range subtype declaration
3805 Range_Type := Make_Temporary (Loc, 'G');
3807 if not Is_Interface (Root_Typ) then
3809 -- subtype rg__xx is
3810 -- Storage_Offset range 1 .. (Expr'size - typ'size) / Storage_Unit
3813 Make_Op_Subtract (Loc,
3815 Make_Attribute_Reference (Loc,
3817 OK_Convert_To (T, Duplicate_Subexpr_No_Checks (E)),
3818 Attribute_Name => Name_Size),
3820 Make_Attribute_Reference (Loc,
3821 Prefix => New_Reference_To (Constr_Root, Loc),
3822 Attribute_Name => Name_Object_Size));
3824 -- subtype rg__xx is
3825 -- Storage_Offset range 1 .. Expr'size / Storage_Unit
3828 Make_Attribute_Reference (Loc,
3830 OK_Convert_To (T, Duplicate_Subexpr_No_Checks (E)),
3831 Attribute_Name => Name_Size);
3834 Set_Paren_Count (Sizexpr, 1);
3836 Append_To (List_Def,
3837 Make_Subtype_Declaration (Loc,
3838 Defining_Identifier => Range_Type,
3839 Subtype_Indication =>
3840 Make_Subtype_Indication (Loc,
3841 Subtype_Mark => New_Reference_To (RTE (RE_Storage_Offset), Loc),
3842 Constraint => Make_Range_Constraint (Loc,
3845 Low_Bound => Make_Integer_Literal (Loc, 1),
3847 Make_Op_Divide (Loc,
3848 Left_Opnd => Sizexpr,
3849 Right_Opnd => Make_Integer_Literal (Loc,
3850 Intval => System_Storage_Unit)))))));
3852 -- subtype str__nn is Storage_Array (rg__x);
3854 Str_Type := Make_Temporary (Loc, 'S');
3855 Append_To (List_Def,
3856 Make_Subtype_Declaration (Loc,
3857 Defining_Identifier => Str_Type,
3858 Subtype_Indication =>
3859 Make_Subtype_Indication (Loc,
3860 Subtype_Mark => New_Reference_To (RTE (RE_Storage_Array), Loc),
3862 Make_Index_Or_Discriminant_Constraint (Loc,
3864 New_List (New_Reference_To (Range_Type, Loc))))));
3866 -- type Equiv_T is record
3867 -- [ _parent : Tnn; ]
3871 Equiv_Type := Make_Temporary (Loc, 'T');
3872 Set_Ekind (Equiv_Type, E_Record_Type);
3873 Set_Parent_Subtype (Equiv_Type, Constr_Root);
3875 -- Set Is_Class_Wide_Equivalent_Type very early to trigger the special
3876 -- treatment for this type. In particular, even though _parent's type
3877 -- is a controlled type or contains controlled components, we do not
3878 -- want to set Has_Controlled_Component on it to avoid making it gain
3879 -- an unwanted _controller component.
3881 Set_Is_Class_Wide_Equivalent_Type (Equiv_Type);
3883 if not Is_Interface (Root_Typ) then
3884 Append_To (Comp_List,
3885 Make_Component_Declaration (Loc,
3886 Defining_Identifier =>
3887 Make_Defining_Identifier (Loc, Name_uParent),
3888 Component_Definition =>
3889 Make_Component_Definition (Loc,
3890 Aliased_Present => False,
3891 Subtype_Indication => New_Reference_To (Constr_Root, Loc))));
3894 Append_To (Comp_List,
3895 Make_Component_Declaration (Loc,
3896 Defining_Identifier => Make_Temporary (Loc, 'C'),
3897 Component_Definition =>
3898 Make_Component_Definition (Loc,
3899 Aliased_Present => False,
3900 Subtype_Indication => New_Reference_To (Str_Type, Loc))));
3902 Append_To (List_Def,
3903 Make_Full_Type_Declaration (Loc,
3904 Defining_Identifier => Equiv_Type,
3906 Make_Record_Definition (Loc,
3908 Make_Component_List (Loc,
3909 Component_Items => Comp_List,
3910 Variant_Part => Empty))));
3912 -- Suppress all checks during the analysis of the expanded code
3913 -- to avoid the generation of spurious warnings under ZFP run-time.
3915 Insert_Actions (E, List_Def, Suppress => All_Checks);
3917 end Make_CW_Equivalent_Type;
3919 ------------------------
3920 -- Make_Literal_Range --
3921 ------------------------
3923 function Make_Literal_Range
3925 Literal_Typ : Entity_Id) return Node_Id
3927 Lo : constant Node_Id :=
3928 New_Copy_Tree (String_Literal_Low_Bound (Literal_Typ));
3929 Index : constant Entity_Id := Etype (Lo);
3932 Length_Expr : constant Node_Id :=
3933 Make_Op_Subtract (Loc,
3935 Make_Integer_Literal (Loc,
3936 Intval => String_Literal_Length (Literal_Typ)),
3938 Make_Integer_Literal (Loc, 1));
3941 Set_Analyzed (Lo, False);
3943 if Is_Integer_Type (Index) then
3946 Left_Opnd => New_Copy_Tree (Lo),
3947 Right_Opnd => Length_Expr);
3950 Make_Attribute_Reference (Loc,
3951 Attribute_Name => Name_Val,
3952 Prefix => New_Occurrence_Of (Index, Loc),
3953 Expressions => New_List (
3956 Make_Attribute_Reference (Loc,
3957 Attribute_Name => Name_Pos,
3958 Prefix => New_Occurrence_Of (Index, Loc),
3959 Expressions => New_List (New_Copy_Tree (Lo))),
3960 Right_Opnd => Length_Expr)));
3967 end Make_Literal_Range;
3969 --------------------------
3970 -- Make_Non_Empty_Check --
3971 --------------------------
3973 function Make_Non_Empty_Check
3975 N : Node_Id) return Node_Id
3981 Make_Attribute_Reference (Loc,
3982 Attribute_Name => Name_Length,
3983 Prefix => Duplicate_Subexpr_No_Checks (N, Name_Req => True)),
3985 Make_Integer_Literal (Loc, 0));
3986 end Make_Non_Empty_Check;
3988 ----------------------------
3989 -- Make_Subtype_From_Expr --
3990 ----------------------------
3992 -- 1. If Expr is an unconstrained array expression, creates
3993 -- Unc_Type(Expr'first(1)..Expr'last(1),..., Expr'first(n)..Expr'last(n))
3995 -- 2. If Expr is a unconstrained discriminated type expression, creates
3996 -- Unc_Type(Expr.Discr1, ... , Expr.Discr_n)
3998 -- 3. If Expr is class-wide, creates an implicit class wide subtype
4000 function Make_Subtype_From_Expr
4002 Unc_Typ : Entity_Id) return Node_Id
4004 Loc : constant Source_Ptr := Sloc (E);
4005 List_Constr : constant List_Id := New_List;
4008 Full_Subtyp : Entity_Id;
4009 Priv_Subtyp : Entity_Id;
4014 if Is_Private_Type (Unc_Typ)
4015 and then Has_Unknown_Discriminants (Unc_Typ)
4017 -- Prepare the subtype completion, Go to base type to
4018 -- find underlying type, because the type may be a generic
4019 -- actual or an explicit subtype.
4021 Utyp := Underlying_Type (Base_Type (Unc_Typ));
4022 Full_Subtyp := Make_Temporary (Loc, 'C');
4024 Unchecked_Convert_To (Utyp, Duplicate_Subexpr_No_Checks (E));
4025 Set_Parent (Full_Exp, Parent (E));
4027 Priv_Subtyp := Make_Temporary (Loc, 'P');
4030 Make_Subtype_Declaration (Loc,
4031 Defining_Identifier => Full_Subtyp,
4032 Subtype_Indication => Make_Subtype_From_Expr (Full_Exp, Utyp)));
4034 -- Define the dummy private subtype
4036 Set_Ekind (Priv_Subtyp, Subtype_Kind (Ekind (Unc_Typ)));
4037 Set_Etype (Priv_Subtyp, Base_Type (Unc_Typ));
4038 Set_Scope (Priv_Subtyp, Full_Subtyp);
4039 Set_Is_Constrained (Priv_Subtyp);
4040 Set_Is_Tagged_Type (Priv_Subtyp, Is_Tagged_Type (Unc_Typ));
4041 Set_Is_Itype (Priv_Subtyp);
4042 Set_Associated_Node_For_Itype (Priv_Subtyp, E);
4044 if Is_Tagged_Type (Priv_Subtyp) then
4046 (Base_Type (Priv_Subtyp), Class_Wide_Type (Unc_Typ));
4047 Set_Primitive_Operations (Priv_Subtyp,
4048 Primitive_Operations (Unc_Typ));
4051 Set_Full_View (Priv_Subtyp, Full_Subtyp);
4053 return New_Reference_To (Priv_Subtyp, Loc);
4055 elsif Is_Array_Type (Unc_Typ) then
4056 for J in 1 .. Number_Dimensions (Unc_Typ) loop
4057 Append_To (List_Constr,
4060 Make_Attribute_Reference (Loc,
4061 Prefix => Duplicate_Subexpr_No_Checks (E),
4062 Attribute_Name => Name_First,
4063 Expressions => New_List (
4064 Make_Integer_Literal (Loc, J))),
4067 Make_Attribute_Reference (Loc,
4068 Prefix => Duplicate_Subexpr_No_Checks (E),
4069 Attribute_Name => Name_Last,
4070 Expressions => New_List (
4071 Make_Integer_Literal (Loc, J)))));
4074 elsif Is_Class_Wide_Type (Unc_Typ) then
4076 CW_Subtype : Entity_Id;
4077 EQ_Typ : Entity_Id := Empty;
4080 -- A class-wide equivalent type is not needed when VM_Target
4081 -- because the VM back-ends handle the class-wide object
4082 -- initialization itself (and doesn't need or want the
4083 -- additional intermediate type to handle the assignment).
4085 if Expander_Active and then Tagged_Type_Expansion then
4087 -- If this is the class_wide type of a completion that is
4088 -- a record subtype, set the type of the class_wide type
4089 -- to be the full base type, for use in the expanded code
4090 -- for the equivalent type. Should this be done earlier when
4091 -- the completion is analyzed ???
4093 if Is_Private_Type (Etype (Unc_Typ))
4095 Ekind (Full_View (Etype (Unc_Typ))) = E_Record_Subtype
4097 Set_Etype (Unc_Typ, Base_Type (Full_View (Etype (Unc_Typ))));
4100 EQ_Typ := Make_CW_Equivalent_Type (Unc_Typ, E);
4103 CW_Subtype := New_Class_Wide_Subtype (Unc_Typ, E);
4104 Set_Equivalent_Type (CW_Subtype, EQ_Typ);
4105 Set_Cloned_Subtype (CW_Subtype, Base_Type (Unc_Typ));
4107 return New_Occurrence_Of (CW_Subtype, Loc);
4110 -- Indefinite record type with discriminants
4113 D := First_Discriminant (Unc_Typ);
4114 while Present (D) loop
4115 Append_To (List_Constr,
4116 Make_Selected_Component (Loc,
4117 Prefix => Duplicate_Subexpr_No_Checks (E),
4118 Selector_Name => New_Reference_To (D, Loc)));
4120 Next_Discriminant (D);
4125 Make_Subtype_Indication (Loc,
4126 Subtype_Mark => New_Reference_To (Unc_Typ, Loc),
4128 Make_Index_Or_Discriminant_Constraint (Loc,
4129 Constraints => List_Constr));
4130 end Make_Subtype_From_Expr;
4132 -----------------------------
4133 -- May_Generate_Large_Temp --
4134 -----------------------------
4136 -- At the current time, the only types that we return False for (i.e.
4137 -- where we decide we know they cannot generate large temps) are ones
4138 -- where we know the size is 256 bits or less at compile time, and we
4139 -- are still not doing a thorough job on arrays and records ???
4141 function May_Generate_Large_Temp (Typ : Entity_Id) return Boolean is
4143 if not Size_Known_At_Compile_Time (Typ) then
4146 elsif Esize (Typ) /= 0 and then Esize (Typ) <= 256 then
4149 elsif Is_Array_Type (Typ)
4150 and then Present (Packed_Array_Type (Typ))
4152 return May_Generate_Large_Temp (Packed_Array_Type (Typ));
4154 -- We could do more here to find other small types ???
4159 end May_Generate_Large_Temp;
4161 ----------------------------
4162 -- Needs_Constant_Address --
4163 ----------------------------
4165 function Needs_Constant_Address
4167 Typ : Entity_Id) return Boolean
4171 -- If we have no initialization of any kind, then we don't need to
4172 -- place any restrictions on the address clause, because the object
4173 -- will be elaborated after the address clause is evaluated. This
4174 -- happens if the declaration has no initial expression, or the type
4175 -- has no implicit initialization, or the object is imported.
4177 -- The same holds for all initialized scalar types and all access
4178 -- types. Packed bit arrays of size up to 64 are represented using a
4179 -- modular type with an initialization (to zero) and can be processed
4180 -- like other initialized scalar types.
4182 -- If the type is controlled, code to attach the object to a
4183 -- finalization chain is generated at the point of declaration,
4184 -- and therefore the elaboration of the object cannot be delayed:
4185 -- the address expression must be a constant.
4187 if No (Expression (Decl))
4188 and then not Needs_Finalization (Typ)
4190 (not Has_Non_Null_Base_Init_Proc (Typ)
4191 or else Is_Imported (Defining_Identifier (Decl)))
4195 elsif (Present (Expression (Decl)) and then Is_Scalar_Type (Typ))
4196 or else Is_Access_Type (Typ)
4198 (Is_Bit_Packed_Array (Typ)
4199 and then Is_Modular_Integer_Type (Packed_Array_Type (Typ)))
4205 -- Otherwise, we require the address clause to be constant because
4206 -- the call to the initialization procedure (or the attach code) has
4207 -- to happen at the point of the declaration.
4209 -- Actually the IP call has been moved to the freeze actions
4210 -- anyway, so maybe we can relax this restriction???
4214 end Needs_Constant_Address;
4216 ----------------------------
4217 -- New_Class_Wide_Subtype --
4218 ----------------------------
4220 function New_Class_Wide_Subtype
4221 (CW_Typ : Entity_Id;
4222 N : Node_Id) return Entity_Id
4224 Res : constant Entity_Id := Create_Itype (E_Void, N);
4225 Res_Name : constant Name_Id := Chars (Res);
4226 Res_Scope : constant Entity_Id := Scope (Res);
4229 Copy_Node (CW_Typ, Res);
4230 Set_Comes_From_Source (Res, False);
4231 Set_Sloc (Res, Sloc (N));
4233 Set_Associated_Node_For_Itype (Res, N);
4234 Set_Is_Public (Res, False); -- By default, may be changed below.
4235 Set_Public_Status (Res);
4236 Set_Chars (Res, Res_Name);
4237 Set_Scope (Res, Res_Scope);
4238 Set_Ekind (Res, E_Class_Wide_Subtype);
4239 Set_Next_Entity (Res, Empty);
4240 Set_Etype (Res, Base_Type (CW_Typ));
4241 Set_Is_Frozen (Res, False);
4242 Set_Freeze_Node (Res, Empty);
4244 end New_Class_Wide_Subtype;
4246 --------------------------------
4247 -- Non_Limited_Designated_Type --
4248 ---------------------------------
4250 function Non_Limited_Designated_Type (T : Entity_Id) return Entity_Id is
4251 Desig : constant Entity_Id := Designated_Type (T);
4253 if Ekind (Desig) = E_Incomplete_Type
4254 and then Present (Non_Limited_View (Desig))
4256 return Non_Limited_View (Desig);
4260 end Non_Limited_Designated_Type;
4262 -----------------------------------
4263 -- OK_To_Do_Constant_Replacement --
4264 -----------------------------------
4266 function OK_To_Do_Constant_Replacement (E : Entity_Id) return Boolean is
4267 ES : constant Entity_Id := Scope (E);
4271 -- Do not replace statically allocated objects, because they may be
4272 -- modified outside the current scope.
4274 if Is_Statically_Allocated (E) then
4277 -- Do not replace aliased or volatile objects, since we don't know what
4278 -- else might change the value.
4280 elsif Is_Aliased (E) or else Treat_As_Volatile (E) then
4283 -- Debug flag -gnatdM disconnects this optimization
4285 elsif Debug_Flag_MM then
4288 -- Otherwise check scopes
4291 CS := Current_Scope;
4294 -- If we are in right scope, replacement is safe
4299 -- Packages do not affect the determination of safety
4301 elsif Ekind (CS) = E_Package then
4302 exit when CS = Standard_Standard;
4305 -- Blocks do not affect the determination of safety
4307 elsif Ekind (CS) = E_Block then
4310 -- Loops do not affect the determination of safety. Note that we
4311 -- kill all current values on entry to a loop, so we are just
4312 -- talking about processing within a loop here.
4314 elsif Ekind (CS) = E_Loop then
4317 -- Otherwise, the reference is dubious, and we cannot be sure that
4318 -- it is safe to do the replacement.
4327 end OK_To_Do_Constant_Replacement;
4329 ------------------------------------
4330 -- Possible_Bit_Aligned_Component --
4331 ------------------------------------
4333 function Possible_Bit_Aligned_Component (N : Node_Id) return Boolean is
4337 -- Case of indexed component
4339 when N_Indexed_Component =>
4341 P : constant Node_Id := Prefix (N);
4342 Ptyp : constant Entity_Id := Etype (P);
4345 -- If we know the component size and it is less than 64, then
4346 -- we are definitely OK. The back end always does assignment of
4347 -- misaligned small objects correctly.
4349 if Known_Static_Component_Size (Ptyp)
4350 and then Component_Size (Ptyp) <= 64
4354 -- Otherwise, we need to test the prefix, to see if we are
4355 -- indexing from a possibly unaligned component.
4358 return Possible_Bit_Aligned_Component (P);
4362 -- Case of selected component
4364 when N_Selected_Component =>
4366 P : constant Node_Id := Prefix (N);
4367 Comp : constant Entity_Id := Entity (Selector_Name (N));
4370 -- If there is no component clause, then we are in the clear
4371 -- since the back end will never misalign a large component
4372 -- unless it is forced to do so. In the clear means we need
4373 -- only the recursive test on the prefix.
4375 if Component_May_Be_Bit_Aligned (Comp) then
4378 return Possible_Bit_Aligned_Component (P);
4382 -- For a slice, test the prefix, if that is possibly misaligned,
4383 -- then for sure the slice is!
4386 return Possible_Bit_Aligned_Component (Prefix (N));
4388 -- If we have none of the above, it means that we have fallen off the
4389 -- top testing prefixes recursively, and we now have a stand alone
4390 -- object, where we don't have a problem.
4396 end Possible_Bit_Aligned_Component;
4398 -------------------------
4399 -- Remove_Side_Effects --
4400 -------------------------
4402 procedure Remove_Side_Effects
4404 Name_Req : Boolean := False;
4405 Variable_Ref : Boolean := False)
4407 Loc : constant Source_Ptr := Sloc (Exp);
4408 Exp_Type : constant Entity_Id := Etype (Exp);
4409 Svg_Suppress : constant Suppress_Array := Scope_Suppress;
4411 Ref_Type : Entity_Id;
4413 Ptr_Typ_Decl : Node_Id;
4417 function Side_Effect_Free (N : Node_Id) return Boolean;
4418 -- Determines if the tree N represents an expression that is known not
4419 -- to have side effects, and for which no processing is required.
4421 function Side_Effect_Free (L : List_Id) return Boolean;
4422 -- Determines if all elements of the list L are side effect free
4424 function Safe_Prefixed_Reference (N : Node_Id) return Boolean;
4425 -- The argument N is a construct where the Prefix is dereferenced if it
4426 -- is an access type and the result is a variable. The call returns True
4427 -- if the construct is side effect free (not considering side effects in
4428 -- other than the prefix which are to be tested by the caller).
4430 function Within_In_Parameter (N : Node_Id) return Boolean;
4431 -- Determines if N is a subcomponent of a composite in-parameter. If so,
4432 -- N is not side-effect free when the actual is global and modifiable
4433 -- indirectly from within a subprogram, because it may be passed by
4434 -- reference. The front-end must be conservative here and assume that
4435 -- this may happen with any array or record type. On the other hand, we
4436 -- cannot create temporaries for all expressions for which this
4437 -- condition is true, for various reasons that might require clearing up
4438 -- ??? For example, discriminant references that appear out of place, or
4439 -- spurious type errors with class-wide expressions. As a result, we
4440 -- limit the transformation to loop bounds, which is so far the only
4441 -- case that requires it.
4443 -----------------------------
4444 -- Safe_Prefixed_Reference --
4445 -----------------------------
4447 function Safe_Prefixed_Reference (N : Node_Id) return Boolean is
4449 -- If prefix is not side effect free, definitely not safe
4451 if not Side_Effect_Free (Prefix (N)) then
4454 -- If the prefix is of an access type that is not access-to-constant,
4455 -- then this construct is a variable reference, which means it is to
4456 -- be considered to have side effects if Variable_Ref is set True
4457 -- Exception is an access to an entity that is a constant or an
4458 -- in-parameter which does not come from source, and is the result
4459 -- of a previous removal of side-effects.
4461 elsif Is_Access_Type (Etype (Prefix (N)))
4462 and then not Is_Access_Constant (Etype (Prefix (N)))
4463 and then Variable_Ref
4465 if not Is_Entity_Name (Prefix (N)) then
4468 return Ekind (Entity (Prefix (N))) = E_Constant
4469 or else Ekind (Entity (Prefix (N))) = E_In_Parameter;
4472 -- The following test is the simplest way of solving a complex
4473 -- problem uncovered by BB08-010: Side effect on loop bound that
4474 -- is a subcomponent of a global variable:
4475 -- If a loop bound is a subcomponent of a global variable, a
4476 -- modification of that variable within the loop may incorrectly
4477 -- affect the execution of the loop.
4480 (Nkind (Parent (Parent (N))) /= N_Loop_Parameter_Specification
4481 or else not Within_In_Parameter (Prefix (N)))
4485 -- All other cases are side effect free
4490 end Safe_Prefixed_Reference;
4492 ----------------------
4493 -- Side_Effect_Free --
4494 ----------------------
4496 function Side_Effect_Free (N : Node_Id) return Boolean is
4498 -- Note on checks that could raise Constraint_Error. Strictly, if
4499 -- we take advantage of 11.6, these checks do not count as side
4500 -- effects. However, we would just as soon consider that they are
4501 -- side effects, since the backend CSE does not work very well on
4502 -- expressions which can raise Constraint_Error. On the other
4503 -- hand, if we do not consider them to be side effect free, then
4504 -- we get some awkward expansions in -gnato mode, resulting in
4505 -- code insertions at a point where we do not have a clear model
4506 -- for performing the insertions.
4508 -- Special handling for entity names
4510 if Is_Entity_Name (N) then
4512 -- If the entity is a constant, it is definitely side effect
4513 -- free. Note that the test of Is_Variable (N) below might
4514 -- be expected to catch this case, but it does not, because
4515 -- this test goes to the original tree, and we may have
4516 -- already rewritten a variable node with a constant as
4517 -- a result of an earlier Force_Evaluation call.
4519 if Ekind_In (Entity (N), E_Constant, E_In_Parameter) then
4522 -- Functions are not side effect free
4524 elsif Ekind (Entity (N)) = E_Function then
4527 -- Variables are considered to be a side effect if Variable_Ref
4528 -- is set or if we have a volatile reference and Name_Req is off.
4529 -- If Name_Req is True then we can't help returning a name which
4530 -- effectively allows multiple references in any case.
4532 elsif Is_Variable (N) then
4533 return not Variable_Ref
4534 and then (not Is_Volatile_Reference (N) or else Name_Req);
4536 -- Any other entity (e.g. a subtype name) is definitely side
4543 -- A value known at compile time is always side effect free
4545 elsif Compile_Time_Known_Value (N) then
4548 -- A variable renaming is not side-effect free, because the
4549 -- renaming will function like a macro in the front-end in
4550 -- some cases, and an assignment can modify the component
4551 -- designated by N, so we need to create a temporary for it.
4553 elsif Is_Entity_Name (Original_Node (N))
4554 and then Is_Renaming_Of_Object (Entity (Original_Node (N)))
4555 and then Ekind (Entity (Original_Node (N))) /= E_Constant
4560 -- For other than entity names and compile time known values,
4561 -- check the node kind for special processing.
4565 -- An attribute reference is side effect free if its expressions
4566 -- are side effect free and its prefix is side effect free or
4567 -- is an entity reference.
4569 -- Is this right? what about x'first where x is a variable???
4571 when N_Attribute_Reference =>
4572 return Side_Effect_Free (Expressions (N))
4573 and then Attribute_Name (N) /= Name_Input
4574 and then (Is_Entity_Name (Prefix (N))
4575 or else Side_Effect_Free (Prefix (N)));
4577 -- A binary operator is side effect free if and both operands
4578 -- are side effect free. For this purpose binary operators
4579 -- include membership tests and short circuit forms
4581 when N_Binary_Op | N_Membership_Test | N_Short_Circuit =>
4582 return Side_Effect_Free (Left_Opnd (N))
4584 Side_Effect_Free (Right_Opnd (N));
4586 -- An explicit dereference is side effect free only if it is
4587 -- a side effect free prefixed reference.
4589 when N_Explicit_Dereference =>
4590 return Safe_Prefixed_Reference (N);
4592 -- A call to _rep_to_pos is side effect free, since we generate
4593 -- this pure function call ourselves. Moreover it is critically
4594 -- important to make this exception, since otherwise we can
4595 -- have discriminants in array components which don't look
4596 -- side effect free in the case of an array whose index type
4597 -- is an enumeration type with an enumeration rep clause.
4599 -- All other function calls are not side effect free
4601 when N_Function_Call =>
4602 return Nkind (Name (N)) = N_Identifier
4603 and then Is_TSS (Name (N), TSS_Rep_To_Pos)
4605 Side_Effect_Free (First (Parameter_Associations (N)));
4607 -- An indexed component is side effect free if it is a side
4608 -- effect free prefixed reference and all the indexing
4609 -- expressions are side effect free.
4611 when N_Indexed_Component =>
4612 return Side_Effect_Free (Expressions (N))
4613 and then Safe_Prefixed_Reference (N);
4615 -- A type qualification is side effect free if the expression
4616 -- is side effect free.
4618 when N_Qualified_Expression =>
4619 return Side_Effect_Free (Expression (N));
4621 -- A selected component is side effect free only if it is a
4622 -- side effect free prefixed reference. If it designates a
4623 -- component with a rep. clause it must be treated has having
4624 -- a potential side effect, because it may be modified through
4625 -- a renaming, and a subsequent use of the renaming as a macro
4626 -- will yield the wrong value. This complex interaction between
4627 -- renaming and removing side effects is a reminder that the
4628 -- latter has become a headache to maintain, and that it should
4629 -- be removed in favor of the gcc mechanism to capture values ???
4631 when N_Selected_Component =>
4632 if Nkind (Parent (N)) = N_Explicit_Dereference
4633 and then Has_Non_Standard_Rep (Designated_Type (Etype (N)))
4637 return Safe_Prefixed_Reference (N);
4640 -- A range is side effect free if the bounds are side effect free
4643 return Side_Effect_Free (Low_Bound (N))
4644 and then Side_Effect_Free (High_Bound (N));
4646 -- A slice is side effect free if it is a side effect free
4647 -- prefixed reference and the bounds are side effect free.
4650 return Side_Effect_Free (Discrete_Range (N))
4651 and then Safe_Prefixed_Reference (N);
4653 -- A type conversion is side effect free if the expression to be
4654 -- converted is side effect free.
4656 when N_Type_Conversion =>
4657 return Side_Effect_Free (Expression (N));
4659 -- A unary operator is side effect free if the operand
4660 -- is side effect free.
4663 return Side_Effect_Free (Right_Opnd (N));
4665 -- An unchecked type conversion is side effect free only if it
4666 -- is safe and its argument is side effect free.
4668 when N_Unchecked_Type_Conversion =>
4669 return Safe_Unchecked_Type_Conversion (N)
4670 and then Side_Effect_Free (Expression (N));
4672 -- An unchecked expression is side effect free if its expression
4673 -- is side effect free.
4675 when N_Unchecked_Expression =>
4676 return Side_Effect_Free (Expression (N));
4678 -- A literal is side effect free
4680 when N_Character_Literal |
4686 -- We consider that anything else has side effects. This is a bit
4687 -- crude, but we are pretty close for most common cases, and we
4688 -- are certainly correct (i.e. we never return True when the
4689 -- answer should be False).
4694 end Side_Effect_Free;
4696 -- A list is side effect free if all elements of the list are
4697 -- side effect free.
4699 function Side_Effect_Free (L : List_Id) return Boolean is
4703 if L = No_List or else L = Error_List then
4708 while Present (N) loop
4709 if not Side_Effect_Free (N) then
4718 end Side_Effect_Free;
4720 -------------------------
4721 -- Within_In_Parameter --
4722 -------------------------
4724 function Within_In_Parameter (N : Node_Id) return Boolean is
4726 if not Comes_From_Source (N) then
4729 elsif Is_Entity_Name (N) then
4730 return Ekind (Entity (N)) = E_In_Parameter;
4732 elsif Nkind (N) = N_Indexed_Component
4733 or else Nkind (N) = N_Selected_Component
4735 return Within_In_Parameter (Prefix (N));
4740 end Within_In_Parameter;
4742 -- Start of processing for Remove_Side_Effects
4745 -- If we are side effect free already or expansion is disabled,
4746 -- there is nothing to do.
4748 if Side_Effect_Free (Exp) or else not Expander_Active then
4752 -- All this must not have any checks
4754 Scope_Suppress := (others => True);
4756 -- If it is a scalar type and we need to capture the value, just make
4757 -- a copy. Likewise for a function call, an attribute reference, an
4758 -- allocator, or an operator. And if we have a volatile reference and
4759 -- Name_Req is not set (see comments above for Side_Effect_Free).
4761 if Is_Elementary_Type (Exp_Type)
4762 and then (Variable_Ref
4763 or else Nkind (Exp) = N_Function_Call
4764 or else Nkind (Exp) = N_Attribute_Reference
4765 or else Nkind (Exp) = N_Allocator
4766 or else Nkind (Exp) in N_Op
4767 or else (not Name_Req and then Is_Volatile_Reference (Exp)))
4769 Def_Id := Make_Temporary (Loc, 'R', Exp);
4770 Set_Etype (Def_Id, Exp_Type);
4771 Res := New_Reference_To (Def_Id, Loc);
4774 Make_Object_Declaration (Loc,
4775 Defining_Identifier => Def_Id,
4776 Object_Definition => New_Reference_To (Exp_Type, Loc),
4777 Constant_Present => True,
4778 Expression => Relocate_Node (Exp));
4780 Set_Assignment_OK (E);
4781 Insert_Action (Exp, E);
4783 -- If the expression has the form v.all then we can just capture
4784 -- the pointer, and then do an explicit dereference on the result.
4786 elsif Nkind (Exp) = N_Explicit_Dereference then
4787 Def_Id := Make_Temporary (Loc, 'R', Exp);
4789 Make_Explicit_Dereference (Loc, New_Reference_To (Def_Id, Loc));
4792 Make_Object_Declaration (Loc,
4793 Defining_Identifier => Def_Id,
4794 Object_Definition =>
4795 New_Reference_To (Etype (Prefix (Exp)), Loc),
4796 Constant_Present => True,
4797 Expression => Relocate_Node (Prefix (Exp))));
4799 -- Similar processing for an unchecked conversion of an expression
4800 -- of the form v.all, where we want the same kind of treatment.
4802 elsif Nkind (Exp) = N_Unchecked_Type_Conversion
4803 and then Nkind (Expression (Exp)) = N_Explicit_Dereference
4805 Remove_Side_Effects (Expression (Exp), Name_Req, Variable_Ref);
4806 Scope_Suppress := Svg_Suppress;
4809 -- If this is a type conversion, leave the type conversion and remove
4810 -- the side effects in the expression. This is important in several
4811 -- circumstances: for change of representations, and also when this is
4812 -- a view conversion to a smaller object, where gigi can end up creating
4813 -- its own temporary of the wrong size.
4815 elsif Nkind (Exp) = N_Type_Conversion then
4816 Remove_Side_Effects (Expression (Exp), Name_Req, Variable_Ref);
4817 Scope_Suppress := Svg_Suppress;
4820 -- If this is an unchecked conversion that Gigi can't handle, make
4821 -- a copy or a use a renaming to capture the value.
4823 elsif Nkind (Exp) = N_Unchecked_Type_Conversion
4824 and then not Safe_Unchecked_Type_Conversion (Exp)
4826 if CW_Or_Has_Controlled_Part (Exp_Type) then
4828 -- Use a renaming to capture the expression, rather than create
4829 -- a controlled temporary.
4831 Def_Id := Make_Temporary (Loc, 'R', Exp);
4832 Res := New_Reference_To (Def_Id, Loc);
4835 Make_Object_Renaming_Declaration (Loc,
4836 Defining_Identifier => Def_Id,
4837 Subtype_Mark => New_Reference_To (Exp_Type, Loc),
4838 Name => Relocate_Node (Exp)));
4841 Def_Id := Make_Temporary (Loc, 'R', Exp);
4842 Set_Etype (Def_Id, Exp_Type);
4843 Res := New_Reference_To (Def_Id, Loc);
4846 Make_Object_Declaration (Loc,
4847 Defining_Identifier => Def_Id,
4848 Object_Definition => New_Reference_To (Exp_Type, Loc),
4849 Constant_Present => not Is_Variable (Exp),
4850 Expression => Relocate_Node (Exp));
4852 Set_Assignment_OK (E);
4853 Insert_Action (Exp, E);
4856 -- For expressions that denote objects, we can use a renaming scheme.
4857 -- This is needed for correctness in the case of a volatile object
4858 -- of a non-volatile type because the Make_Reference call of the
4859 -- "default" approach would generate an illegal access value (an access
4860 -- value cannot designate such an object - see Analyze_Reference).
4861 -- We skip using this scheme if we have an object of a volatile type
4862 -- and we do not have Name_Req set true (see comments above for
4863 -- Side_Effect_Free).
4865 elsif Is_Object_Reference (Exp)
4866 and then Nkind (Exp) /= N_Function_Call
4867 and then (Name_Req or else not Treat_As_Volatile (Exp_Type))
4869 Def_Id := Make_Temporary (Loc, 'R', Exp);
4871 if Nkind (Exp) = N_Selected_Component
4872 and then Nkind (Prefix (Exp)) = N_Function_Call
4873 and then Is_Array_Type (Exp_Type)
4875 -- Avoid generating a variable-sized temporary, by generating
4876 -- the renaming declaration just for the function call. The
4877 -- transformation could be refined to apply only when the array
4878 -- component is constrained by a discriminant???
4881 Make_Selected_Component (Loc,
4882 Prefix => New_Occurrence_Of (Def_Id, Loc),
4883 Selector_Name => Selector_Name (Exp));
4886 Make_Object_Renaming_Declaration (Loc,
4887 Defining_Identifier => Def_Id,
4889 New_Reference_To (Base_Type (Etype (Prefix (Exp))), Loc),
4890 Name => Relocate_Node (Prefix (Exp))));
4893 Res := New_Reference_To (Def_Id, Loc);
4896 Make_Object_Renaming_Declaration (Loc,
4897 Defining_Identifier => Def_Id,
4898 Subtype_Mark => New_Reference_To (Exp_Type, Loc),
4899 Name => Relocate_Node (Exp)));
4902 -- If this is a packed reference, or a selected component with a
4903 -- non-standard representation, a reference to the temporary will
4904 -- be replaced by a copy of the original expression (see
4905 -- Exp_Ch2.Expand_Renaming). Otherwise the temporary must be
4906 -- elaborated by gigi, and is of course not to be replaced in-line
4907 -- by the expression it renames, which would defeat the purpose of
4908 -- removing the side-effect.
4910 if (Nkind (Exp) = N_Selected_Component
4911 or else Nkind (Exp) = N_Indexed_Component)
4912 and then Has_Non_Standard_Rep (Etype (Prefix (Exp)))
4916 Set_Is_Renaming_Of_Object (Def_Id, False);
4919 -- Otherwise we generate a reference to the value
4922 -- Special processing for function calls that return a limited type.
4923 -- We need to build a declaration that will enable build-in-place
4924 -- expansion of the call. This is not done if the context is already
4925 -- an object declaration, to prevent infinite recursion.
4927 -- This is relevant only in Ada 2005 mode. In Ada 95 programs we have
4928 -- to accommodate functions returning limited objects by reference.
4930 if Nkind (Exp) = N_Function_Call
4931 and then Is_Inherently_Limited_Type (Etype (Exp))
4932 and then Nkind (Parent (Exp)) /= N_Object_Declaration
4933 and then Ada_Version >= Ada_05
4936 Obj : constant Entity_Id := Make_Temporary (Loc, 'F', Exp);
4941 Make_Object_Declaration (Loc,
4942 Defining_Identifier => Obj,
4943 Object_Definition => New_Occurrence_Of (Exp_Type, Loc),
4944 Expression => Relocate_Node (Exp));
4946 Insert_Action (Exp, Decl);
4947 Set_Etype (Obj, Exp_Type);
4948 Rewrite (Exp, New_Occurrence_Of (Obj, Loc));
4953 Ref_Type := Make_Temporary (Loc, 'A');
4956 Make_Full_Type_Declaration (Loc,
4957 Defining_Identifier => Ref_Type,
4959 Make_Access_To_Object_Definition (Loc,
4960 All_Present => True,
4961 Subtype_Indication =>
4962 New_Reference_To (Exp_Type, Loc)));
4965 Insert_Action (Exp, Ptr_Typ_Decl);
4967 Def_Id := Make_Temporary (Loc, 'R', Exp);
4968 Set_Etype (Def_Id, Exp_Type);
4971 Make_Explicit_Dereference (Loc,
4972 Prefix => New_Reference_To (Def_Id, Loc));
4974 if Nkind (E) = N_Explicit_Dereference then
4975 New_Exp := Relocate_Node (Prefix (E));
4977 E := Relocate_Node (E);
4978 New_Exp := Make_Reference (Loc, E);
4981 if Is_Delayed_Aggregate (E) then
4983 -- The expansion of nested aggregates is delayed until the
4984 -- enclosing aggregate is expanded. As aggregates are often
4985 -- qualified, the predicate applies to qualified expressions
4986 -- as well, indicating that the enclosing aggregate has not
4987 -- been expanded yet. At this point the aggregate is part of
4988 -- a stand-alone declaration, and must be fully expanded.
4990 if Nkind (E) = N_Qualified_Expression then
4991 Set_Expansion_Delayed (Expression (E), False);
4992 Set_Analyzed (Expression (E), False);
4994 Set_Expansion_Delayed (E, False);
4997 Set_Analyzed (E, False);
5001 Make_Object_Declaration (Loc,
5002 Defining_Identifier => Def_Id,
5003 Object_Definition => New_Reference_To (Ref_Type, Loc),
5004 Constant_Present => True,
5005 Expression => New_Exp));
5008 -- Preserve the Assignment_OK flag in all copies, since at least
5009 -- one copy may be used in a context where this flag must be set
5010 -- (otherwise why would the flag be set in the first place).
5012 Set_Assignment_OK (Res, Assignment_OK (Exp));
5014 -- Finally rewrite the original expression and we are done
5017 Analyze_And_Resolve (Exp, Exp_Type);
5018 Scope_Suppress := Svg_Suppress;
5019 end Remove_Side_Effects;
5021 ---------------------------
5022 -- Represented_As_Scalar --
5023 ---------------------------
5025 function Represented_As_Scalar (T : Entity_Id) return Boolean is
5026 UT : constant Entity_Id := Underlying_Type (T);
5028 return Is_Scalar_Type (UT)
5029 or else (Is_Bit_Packed_Array (UT)
5030 and then Is_Scalar_Type (Packed_Array_Type (UT)));
5031 end Represented_As_Scalar;
5033 ------------------------------------
5034 -- Safe_Unchecked_Type_Conversion --
5035 ------------------------------------
5037 -- Note: this function knows quite a bit about the exact requirements
5038 -- of Gigi with respect to unchecked type conversions, and its code
5039 -- must be coordinated with any changes in Gigi in this area.
5041 -- The above requirements should be documented in Sinfo ???
5043 function Safe_Unchecked_Type_Conversion (Exp : Node_Id) return Boolean is
5048 Pexp : constant Node_Id := Parent (Exp);
5051 -- If the expression is the RHS of an assignment or object declaration
5052 -- we are always OK because there will always be a target.
5054 -- Object renaming declarations, (generated for view conversions of
5055 -- actuals in inlined calls), like object declarations, provide an
5056 -- explicit type, and are safe as well.
5058 if (Nkind (Pexp) = N_Assignment_Statement
5059 and then Expression (Pexp) = Exp)
5060 or else Nkind (Pexp) = N_Object_Declaration
5061 or else Nkind (Pexp) = N_Object_Renaming_Declaration
5065 -- If the expression is the prefix of an N_Selected_Component
5066 -- we should also be OK because GCC knows to look inside the
5067 -- conversion except if the type is discriminated. We assume
5068 -- that we are OK anyway if the type is not set yet or if it is
5069 -- controlled since we can't afford to introduce a temporary in
5072 elsif Nkind (Pexp) = N_Selected_Component
5073 and then Prefix (Pexp) = Exp
5075 if No (Etype (Pexp)) then
5079 not Has_Discriminants (Etype (Pexp))
5080 or else Is_Constrained (Etype (Pexp));
5084 -- Set the output type, this comes from Etype if it is set, otherwise
5085 -- we take it from the subtype mark, which we assume was already
5088 if Present (Etype (Exp)) then
5089 Otyp := Etype (Exp);
5091 Otyp := Entity (Subtype_Mark (Exp));
5094 -- The input type always comes from the expression, and we assume
5095 -- this is indeed always analyzed, so we can simply get the Etype.
5097 Ityp := Etype (Expression (Exp));
5099 -- Initialize alignments to unknown so far
5104 -- Replace a concurrent type by its corresponding record type
5105 -- and each type by its underlying type and do the tests on those.
5106 -- The original type may be a private type whose completion is a
5107 -- concurrent type, so find the underlying type first.
5109 if Present (Underlying_Type (Otyp)) then
5110 Otyp := Underlying_Type (Otyp);
5113 if Present (Underlying_Type (Ityp)) then
5114 Ityp := Underlying_Type (Ityp);
5117 if Is_Concurrent_Type (Otyp) then
5118 Otyp := Corresponding_Record_Type (Otyp);
5121 if Is_Concurrent_Type (Ityp) then
5122 Ityp := Corresponding_Record_Type (Ityp);
5125 -- If the base types are the same, we know there is no problem since
5126 -- this conversion will be a noop.
5128 if Implementation_Base_Type (Otyp) = Implementation_Base_Type (Ityp) then
5131 -- Same if this is an upwards conversion of an untagged type, and there
5132 -- are no constraints involved (could be more general???)
5134 elsif Etype (Ityp) = Otyp
5135 and then not Is_Tagged_Type (Ityp)
5136 and then not Has_Discriminants (Ityp)
5137 and then No (First_Rep_Item (Base_Type (Ityp)))
5141 -- If the expression has an access type (object or subprogram) we
5142 -- assume that the conversion is safe, because the size of the target
5143 -- is safe, even if it is a record (which might be treated as having
5144 -- unknown size at this point).
5146 elsif Is_Access_Type (Ityp) then
5149 -- If the size of output type is known at compile time, there is
5150 -- never a problem. Note that unconstrained records are considered
5151 -- to be of known size, but we can't consider them that way here,
5152 -- because we are talking about the actual size of the object.
5154 -- We also make sure that in addition to the size being known, we do
5155 -- not have a case which might generate an embarrassingly large temp
5156 -- in stack checking mode.
5158 elsif Size_Known_At_Compile_Time (Otyp)
5160 (not Stack_Checking_Enabled
5161 or else not May_Generate_Large_Temp (Otyp))
5162 and then not (Is_Record_Type (Otyp) and then not Is_Constrained (Otyp))
5166 -- If either type is tagged, then we know the alignment is OK so
5167 -- Gigi will be able to use pointer punning.
5169 elsif Is_Tagged_Type (Otyp) or else Is_Tagged_Type (Ityp) then
5172 -- If either type is a limited record type, we cannot do a copy, so
5173 -- say safe since there's nothing else we can do.
5175 elsif Is_Limited_Record (Otyp) or else Is_Limited_Record (Ityp) then
5178 -- Conversions to and from packed array types are always ignored and
5181 elsif Is_Packed_Array_Type (Otyp)
5182 or else Is_Packed_Array_Type (Ityp)
5187 -- The only other cases known to be safe is if the input type's
5188 -- alignment is known to be at least the maximum alignment for the
5189 -- target or if both alignments are known and the output type's
5190 -- alignment is no stricter than the input's. We can use the alignment
5191 -- of the component type of an array if a type is an unpacked
5194 if Present (Alignment_Clause (Otyp)) then
5195 Oalign := Expr_Value (Expression (Alignment_Clause (Otyp)));
5197 elsif Is_Array_Type (Otyp)
5198 and then Present (Alignment_Clause (Component_Type (Otyp)))
5200 Oalign := Expr_Value (Expression (Alignment_Clause
5201 (Component_Type (Otyp))));
5204 if Present (Alignment_Clause (Ityp)) then
5205 Ialign := Expr_Value (Expression (Alignment_Clause (Ityp)));
5207 elsif Is_Array_Type (Ityp)
5208 and then Present (Alignment_Clause (Component_Type (Ityp)))
5210 Ialign := Expr_Value (Expression (Alignment_Clause
5211 (Component_Type (Ityp))));
5214 if Ialign /= No_Uint and then Ialign > Maximum_Alignment then
5217 elsif Ialign /= No_Uint and then Oalign /= No_Uint
5218 and then Ialign <= Oalign
5222 -- Otherwise, Gigi cannot handle this and we must make a temporary
5227 end Safe_Unchecked_Type_Conversion;
5229 ---------------------------------
5230 -- Set_Current_Value_Condition --
5231 ---------------------------------
5233 -- Note: the implementation of this procedure is very closely tied to the
5234 -- implementation of Get_Current_Value_Condition. Here we set required
5235 -- Current_Value fields, and in Get_Current_Value_Condition, we interpret
5236 -- them, so they must have a consistent view.
5238 procedure Set_Current_Value_Condition (Cnode : Node_Id) is
5240 procedure Set_Entity_Current_Value (N : Node_Id);
5241 -- If N is an entity reference, where the entity is of an appropriate
5242 -- kind, then set the current value of this entity to Cnode, unless
5243 -- there is already a definite value set there.
5245 procedure Set_Expression_Current_Value (N : Node_Id);
5246 -- If N is of an appropriate form, sets an appropriate entry in current
5247 -- value fields of relevant entities. Multiple entities can be affected
5248 -- in the case of an AND or AND THEN.
5250 ------------------------------
5251 -- Set_Entity_Current_Value --
5252 ------------------------------
5254 procedure Set_Entity_Current_Value (N : Node_Id) is
5256 if Is_Entity_Name (N) then
5258 Ent : constant Entity_Id := Entity (N);
5261 -- Don't capture if not safe to do so
5263 if not Safe_To_Capture_Value (N, Ent, Cond => True) then
5267 -- Here we have a case where the Current_Value field may
5268 -- need to be set. We set it if it is not already set to a
5269 -- compile time expression value.
5271 -- Note that this represents a decision that one condition
5272 -- blots out another previous one. That's certainly right
5273 -- if they occur at the same level. If the second one is
5274 -- nested, then the decision is neither right nor wrong (it
5275 -- would be equally OK to leave the outer one in place, or
5276 -- take the new inner one. Really we should record both, but
5277 -- our data structures are not that elaborate.
5279 if Nkind (Current_Value (Ent)) not in N_Subexpr then
5280 Set_Current_Value (Ent, Cnode);
5284 end Set_Entity_Current_Value;
5286 ----------------------------------
5287 -- Set_Expression_Current_Value --
5288 ----------------------------------
5290 procedure Set_Expression_Current_Value (N : Node_Id) is
5296 -- Loop to deal with (ignore for now) any NOT operators present. The
5297 -- presence of NOT operators will be handled properly when we call
5298 -- Get_Current_Value_Condition.
5300 while Nkind (Cond) = N_Op_Not loop
5301 Cond := Right_Opnd (Cond);
5304 -- For an AND or AND THEN, recursively process operands
5306 if Nkind (Cond) = N_Op_And or else Nkind (Cond) = N_And_Then then
5307 Set_Expression_Current_Value (Left_Opnd (Cond));
5308 Set_Expression_Current_Value (Right_Opnd (Cond));
5312 -- Check possible relational operator
5314 if Nkind (Cond) in N_Op_Compare then
5315 if Compile_Time_Known_Value (Right_Opnd (Cond)) then
5316 Set_Entity_Current_Value (Left_Opnd (Cond));
5317 elsif Compile_Time_Known_Value (Left_Opnd (Cond)) then
5318 Set_Entity_Current_Value (Right_Opnd (Cond));
5321 -- Check possible boolean variable reference
5324 Set_Entity_Current_Value (Cond);
5326 end Set_Expression_Current_Value;
5328 -- Start of processing for Set_Current_Value_Condition
5331 Set_Expression_Current_Value (Condition (Cnode));
5332 end Set_Current_Value_Condition;
5334 --------------------------
5335 -- Set_Elaboration_Flag --
5336 --------------------------
5338 procedure Set_Elaboration_Flag (N : Node_Id; Spec_Id : Entity_Id) is
5339 Loc : constant Source_Ptr := Sloc (N);
5340 Ent : constant Entity_Id := Elaboration_Entity (Spec_Id);
5344 if Present (Ent) then
5346 -- Nothing to do if at the compilation unit level, because in this
5347 -- case the flag is set by the binder generated elaboration routine.
5349 if Nkind (Parent (N)) = N_Compilation_Unit then
5352 -- Here we do need to generate an assignment statement
5355 Check_Restriction (No_Elaboration_Code, N);
5357 Make_Assignment_Statement (Loc,
5358 Name => New_Occurrence_Of (Ent, Loc),
5359 Expression => New_Occurrence_Of (Standard_True, Loc));
5361 if Nkind (Parent (N)) = N_Subunit then
5362 Insert_After (Corresponding_Stub (Parent (N)), Asn);
5364 Insert_After (N, Asn);
5369 -- Kill current value indication. This is necessary because the
5370 -- tests of this flag are inserted out of sequence and must not
5371 -- pick up bogus indications of the wrong constant value.
5373 Set_Current_Value (Ent, Empty);
5376 end Set_Elaboration_Flag;
5378 ----------------------------
5379 -- Set_Renamed_Subprogram --
5380 ----------------------------
5382 procedure Set_Renamed_Subprogram (N : Node_Id; E : Entity_Id) is
5384 -- If input node is an identifier, we can just reset it
5386 if Nkind (N) = N_Identifier then
5387 Set_Chars (N, Chars (E));
5390 -- Otherwise we have to do a rewrite, preserving Comes_From_Source
5394 CS : constant Boolean := Comes_From_Source (N);
5396 Rewrite (N, Make_Identifier (Sloc (N), Chars => Chars (E)));
5398 Set_Comes_From_Source (N, CS);
5399 Set_Analyzed (N, True);
5402 end Set_Renamed_Subprogram;
5404 ----------------------------------
5405 -- Silly_Boolean_Array_Not_Test --
5406 ----------------------------------
5408 -- This procedure implements an odd and silly test. We explicitly check
5409 -- for the case where the 'First of the component type is equal to the
5410 -- 'Last of this component type, and if this is the case, we make sure
5411 -- that constraint error is raised. The reason is that the NOT is bound
5412 -- to cause CE in this case, and we will not otherwise catch it.
5414 -- No such check is required for AND and OR, since for both these cases
5415 -- False op False = False, and True op True = True. For the XOR case,
5416 -- see Silly_Boolean_Array_Xor_Test.
5418 -- Believe it or not, this was reported as a bug. Note that nearly
5419 -- always, the test will evaluate statically to False, so the code will
5420 -- be statically removed, and no extra overhead caused.
5422 procedure Silly_Boolean_Array_Not_Test (N : Node_Id; T : Entity_Id) is
5423 Loc : constant Source_Ptr := Sloc (N);
5424 CT : constant Entity_Id := Component_Type (T);
5427 -- The check we install is
5429 -- constraint_error when
5430 -- component_type'first = component_type'last
5431 -- and then array_type'Length /= 0)
5433 -- We need the last guard because we don't want to raise CE for empty
5434 -- arrays since no out of range values result. (Empty arrays with a
5435 -- component type of True .. True -- very useful -- even the ACATS
5436 -- does not test that marginal case!)
5439 Make_Raise_Constraint_Error (Loc,
5445 Make_Attribute_Reference (Loc,
5446 Prefix => New_Occurrence_Of (CT, Loc),
5447 Attribute_Name => Name_First),
5450 Make_Attribute_Reference (Loc,
5451 Prefix => New_Occurrence_Of (CT, Loc),
5452 Attribute_Name => Name_Last)),
5454 Right_Opnd => Make_Non_Empty_Check (Loc, Right_Opnd (N))),
5455 Reason => CE_Range_Check_Failed));
5456 end Silly_Boolean_Array_Not_Test;
5458 ----------------------------------
5459 -- Silly_Boolean_Array_Xor_Test --
5460 ----------------------------------
5462 -- This procedure implements an odd and silly test. We explicitly check
5463 -- for the XOR case where the component type is True .. True, since this
5464 -- will raise constraint error. A special check is required since CE
5465 -- will not be generated otherwise (cf Expand_Packed_Not).
5467 -- No such check is required for AND and OR, since for both these cases
5468 -- False op False = False, and True op True = True, and no check is
5469 -- required for the case of False .. False, since False xor False = False.
5470 -- See also Silly_Boolean_Array_Not_Test
5472 procedure Silly_Boolean_Array_Xor_Test (N : Node_Id; T : Entity_Id) is
5473 Loc : constant Source_Ptr := Sloc (N);
5474 CT : constant Entity_Id := Component_Type (T);
5477 -- The check we install is
5479 -- constraint_error when
5480 -- Boolean (component_type'First)
5481 -- and then Boolean (component_type'Last)
5482 -- and then array_type'Length /= 0)
5484 -- We need the last guard because we don't want to raise CE for empty
5485 -- arrays since no out of range values result (Empty arrays with a
5486 -- component type of True .. True -- very useful -- even the ACATS
5487 -- does not test that marginal case!).
5490 Make_Raise_Constraint_Error (Loc,
5496 Convert_To (Standard_Boolean,
5497 Make_Attribute_Reference (Loc,
5498 Prefix => New_Occurrence_Of (CT, Loc),
5499 Attribute_Name => Name_First)),
5502 Convert_To (Standard_Boolean,
5503 Make_Attribute_Reference (Loc,
5504 Prefix => New_Occurrence_Of (CT, Loc),
5505 Attribute_Name => Name_Last))),
5507 Right_Opnd => Make_Non_Empty_Check (Loc, Right_Opnd (N))),
5508 Reason => CE_Range_Check_Failed));
5509 end Silly_Boolean_Array_Xor_Test;
5511 --------------------------
5512 -- Target_Has_Fixed_Ops --
5513 --------------------------
5515 Integer_Sized_Small : Ureal;
5516 -- Set to 2.0 ** -(Integer'Size - 1) the first time that this
5517 -- function is called (we don't want to compute it more than once!)
5519 Long_Integer_Sized_Small : Ureal;
5520 -- Set to 2.0 ** -(Long_Integer'Size - 1) the first time that this
5521 -- function is called (we don't want to compute it more than once)
5523 First_Time_For_THFO : Boolean := True;
5524 -- Set to False after first call (if Fractional_Fixed_Ops_On_Target)
5526 function Target_Has_Fixed_Ops
5527 (Left_Typ : Entity_Id;
5528 Right_Typ : Entity_Id;
5529 Result_Typ : Entity_Id) return Boolean
5531 function Is_Fractional_Type (Typ : Entity_Id) return Boolean;
5532 -- Return True if the given type is a fixed-point type with a small
5533 -- value equal to 2 ** (-(T'Object_Size - 1)) and whose values have
5534 -- an absolute value less than 1.0. This is currently limited
5535 -- to fixed-point types that map to Integer or Long_Integer.
5537 ------------------------
5538 -- Is_Fractional_Type --
5539 ------------------------
5541 function Is_Fractional_Type (Typ : Entity_Id) return Boolean is
5543 if Esize (Typ) = Standard_Integer_Size then
5544 return Small_Value (Typ) = Integer_Sized_Small;
5546 elsif Esize (Typ) = Standard_Long_Integer_Size then
5547 return Small_Value (Typ) = Long_Integer_Sized_Small;
5552 end Is_Fractional_Type;
5554 -- Start of processing for Target_Has_Fixed_Ops
5557 -- Return False if Fractional_Fixed_Ops_On_Target is false
5559 if not Fractional_Fixed_Ops_On_Target then
5563 -- Here the target has Fractional_Fixed_Ops, if first time, compute
5564 -- standard constants used by Is_Fractional_Type.
5566 if First_Time_For_THFO then
5567 First_Time_For_THFO := False;
5569 Integer_Sized_Small :=
5572 Den => UI_From_Int (Standard_Integer_Size - 1),
5575 Long_Integer_Sized_Small :=
5578 Den => UI_From_Int (Standard_Long_Integer_Size - 1),
5582 -- Return True if target supports fixed-by-fixed multiply/divide
5583 -- for fractional fixed-point types (see Is_Fractional_Type) and
5584 -- the operand and result types are equivalent fractional types.
5586 return Is_Fractional_Type (Base_Type (Left_Typ))
5587 and then Is_Fractional_Type (Base_Type (Right_Typ))
5588 and then Is_Fractional_Type (Base_Type (Result_Typ))
5589 and then Esize (Left_Typ) = Esize (Right_Typ)
5590 and then Esize (Left_Typ) = Esize (Result_Typ);
5591 end Target_Has_Fixed_Ops;
5593 ------------------------------------------
5594 -- Type_May_Have_Bit_Aligned_Components --
5595 ------------------------------------------
5597 function Type_May_Have_Bit_Aligned_Components
5598 (Typ : Entity_Id) return Boolean
5601 -- Array type, check component type
5603 if Is_Array_Type (Typ) then
5605 Type_May_Have_Bit_Aligned_Components (Component_Type (Typ));
5607 -- Record type, check components
5609 elsif Is_Record_Type (Typ) then
5614 E := First_Component_Or_Discriminant (Typ);
5615 while Present (E) loop
5616 if Component_May_Be_Bit_Aligned (E)
5617 or else Type_May_Have_Bit_Aligned_Components (Etype (E))
5622 Next_Component_Or_Discriminant (E);
5628 -- Type other than array or record is always OK
5633 end Type_May_Have_Bit_Aligned_Components;
5635 ----------------------------
5636 -- Wrap_Cleanup_Procedure --
5637 ----------------------------
5639 procedure Wrap_Cleanup_Procedure (N : Node_Id) is
5640 Loc : constant Source_Ptr := Sloc (N);
5641 Stseq : constant Node_Id := Handled_Statement_Sequence (N);
5642 Stmts : constant List_Id := Statements (Stseq);
5645 if Abort_Allowed then
5646 Prepend_To (Stmts, Build_Runtime_Call (Loc, RE_Abort_Defer));
5647 Append_To (Stmts, Build_Runtime_Call (Loc, RE_Abort_Undefer));
5649 end Wrap_Cleanup_Procedure;