1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2009, 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_SCIL; use Sem_SCIL;
47 with Sem_Eval; use Sem_Eval;
48 with Sem_Res; use Sem_Res;
49 with Sem_Type; use Sem_Type;
50 with Sem_Util; use Sem_Util;
51 with Snames; use Snames;
52 with Stand; use Stand;
53 with Stringt; use Stringt;
54 with Targparm; use Targparm;
55 with Tbuild; use Tbuild;
56 with Ttypes; use Ttypes;
57 with Uintp; use Uintp;
58 with Urealp; use Urealp;
59 with Validsw; use Validsw;
61 package body Exp_Util is
63 -----------------------
64 -- Local Subprograms --
65 -----------------------
67 function Build_Task_Array_Image
71 Dyn : Boolean := False) return Node_Id;
72 -- Build function to generate the image string for a task that is an
73 -- array component, concatenating the images of each index. To avoid
74 -- storage leaks, the string is built with successive slice assignments.
75 -- The flag Dyn indicates whether this is called for the initialization
76 -- procedure of an array of tasks, or for the name of a dynamically
77 -- created task that is assigned to an indexed component.
79 function Build_Task_Image_Function
83 Res : Entity_Id) return Node_Id;
84 -- Common processing for Task_Array_Image and Task_Record_Image.
85 -- Build function body that computes image.
87 procedure Build_Task_Image_Prefix
96 -- Common processing for Task_Array_Image and Task_Record_Image.
97 -- Create local variables and assign prefix of name to result string.
99 function Build_Task_Record_Image
102 Dyn : Boolean := False) return Node_Id;
103 -- Build function to generate the image string for a task that is a
104 -- record component. Concatenate name of variable with that of selector.
105 -- The flag Dyn indicates whether this is called for the initialization
106 -- procedure of record with task components, or for a dynamically
107 -- created task that is assigned to a selected component.
109 function Make_CW_Equivalent_Type
111 E : Node_Id) return Entity_Id;
112 -- T is a class-wide type entity, E is the initial expression node that
113 -- constrains T in case such as: " X: T := E" or "new T'(E)"
114 -- This function returns the entity of the Equivalent type and inserts
115 -- on the fly the necessary declaration such as:
117 -- type anon is record
118 -- _parent : Root_Type (T); constrained with E discriminants (if any)
119 -- Extension : String (1 .. expr to match size of E);
122 -- This record is compatible with any object of the class of T thanks
123 -- to the first field and has the same size as E thanks to the second.
125 function Make_Literal_Range
127 Literal_Typ : Entity_Id) return Node_Id;
128 -- Produce a Range node whose bounds are:
129 -- Low_Bound (Literal_Type) ..
130 -- Low_Bound (Literal_Type) + (Length (Literal_Typ) - 1)
131 -- this is used for expanding declarations like X : String := "sdfgdfg";
133 -- If the index type of the target array is not integer, we generate:
134 -- Low_Bound (Literal_Type) ..
136 -- (Literal_Type'Pos (Low_Bound (Literal_Type))
137 -- + (Length (Literal_Typ) -1))
139 function Make_Non_Empty_Check
141 N : Node_Id) return Node_Id;
142 -- Produce a boolean expression checking that the unidimensional array
143 -- node N is not empty.
145 function New_Class_Wide_Subtype
147 N : Node_Id) return Entity_Id;
148 -- Create an implicit subtype of CW_Typ attached to node N
150 ----------------------
151 -- Adjust_Condition --
152 ----------------------
154 procedure Adjust_Condition (N : Node_Id) is
161 Loc : constant Source_Ptr := Sloc (N);
162 T : constant Entity_Id := Etype (N);
166 -- For now, we simply ignore a call where the argument has no
167 -- type (probably case of unanalyzed condition), or has a type
168 -- that is not Boolean. This is because this is a pretty marginal
169 -- piece of functionality, and violations of these rules are
170 -- likely to be truly marginal (how much code uses Fortran Logical
171 -- as the barrier to a protected entry?) and we do not want to
172 -- blow up existing programs. We can change this to an assertion
173 -- after 3.12a is released ???
175 if No (T) or else not Is_Boolean_Type (T) then
179 -- Apply validity checking if needed
181 if Validity_Checks_On and Validity_Check_Tests then
185 -- Immediate return if standard boolean, the most common case,
186 -- where nothing needs to be done.
188 if Base_Type (T) = Standard_Boolean then
192 -- Case of zero/non-zero semantics or non-standard enumeration
193 -- representation. In each case, we rewrite the node as:
195 -- ityp!(N) /= False'Enum_Rep
197 -- where ityp is an integer type with large enough size to hold
198 -- any value of type T.
200 if Nonzero_Is_True (T) or else Has_Non_Standard_Rep (T) then
201 if Esize (T) <= Esize (Standard_Integer) then
202 Ti := Standard_Integer;
204 Ti := Standard_Long_Long_Integer;
209 Left_Opnd => Unchecked_Convert_To (Ti, N),
211 Make_Attribute_Reference (Loc,
212 Attribute_Name => Name_Enum_Rep,
214 New_Occurrence_Of (First_Literal (T), Loc))));
215 Analyze_And_Resolve (N, Standard_Boolean);
218 Rewrite (N, Convert_To (Standard_Boolean, N));
219 Analyze_And_Resolve (N, Standard_Boolean);
222 end Adjust_Condition;
224 ------------------------
225 -- Adjust_Result_Type --
226 ------------------------
228 procedure Adjust_Result_Type (N : Node_Id; T : Entity_Id) is
230 -- Ignore call if current type is not Standard.Boolean
232 if Etype (N) /= Standard_Boolean then
236 -- If result is already of correct type, nothing to do. Note that
237 -- this will get the most common case where everything has a type
238 -- of Standard.Boolean.
240 if Base_Type (T) = Standard_Boolean then
245 KP : constant Node_Kind := Nkind (Parent (N));
248 -- If result is to be used as a Condition in the syntax, no need
249 -- to convert it back, since if it was changed to Standard.Boolean
250 -- using Adjust_Condition, that is just fine for this usage.
252 if KP in N_Raise_xxx_Error or else KP in N_Has_Condition then
255 -- If result is an operand of another logical operation, no need
256 -- to reset its type, since Standard.Boolean is just fine, and
257 -- such operations always do Adjust_Condition on their operands.
259 elsif KP in N_Op_Boolean
260 or else KP in N_Short_Circuit
261 or else KP = N_Op_Not
265 -- Otherwise we perform a conversion from the current type,
266 -- which must be Standard.Boolean, to the desired type.
270 Rewrite (N, Convert_To (T, N));
271 Analyze_And_Resolve (N, T);
275 end Adjust_Result_Type;
277 --------------------------
278 -- Append_Freeze_Action --
279 --------------------------
281 procedure Append_Freeze_Action (T : Entity_Id; N : Node_Id) is
285 Ensure_Freeze_Node (T);
286 Fnode := Freeze_Node (T);
288 if No (Actions (Fnode)) then
289 Set_Actions (Fnode, New_List);
292 Append (N, Actions (Fnode));
293 end Append_Freeze_Action;
295 ---------------------------
296 -- Append_Freeze_Actions --
297 ---------------------------
299 procedure Append_Freeze_Actions (T : Entity_Id; L : List_Id) is
300 Fnode : constant Node_Id := Freeze_Node (T);
307 if No (Actions (Fnode)) then
308 Set_Actions (Fnode, L);
311 Append_List (L, Actions (Fnode));
315 end Append_Freeze_Actions;
317 ------------------------
318 -- Build_Runtime_Call --
319 ------------------------
321 function Build_Runtime_Call (Loc : Source_Ptr; RE : RE_Id) return Node_Id is
323 -- If entity is not available, we can skip making the call (this avoids
324 -- junk duplicated error messages in a number of cases).
326 if not RTE_Available (RE) then
327 return Make_Null_Statement (Loc);
330 Make_Procedure_Call_Statement (Loc,
331 Name => New_Reference_To (RTE (RE), Loc));
333 end Build_Runtime_Call;
335 ----------------------------
336 -- Build_Task_Array_Image --
337 ----------------------------
339 -- This function generates the body for a function that constructs the
340 -- image string for a task that is an array component. The function is
341 -- local to the init proc for the array type, and is called for each one
342 -- of the components. The constructed image has the form of an indexed
343 -- component, whose prefix is the outer variable of the array type.
344 -- The n-dimensional array type has known indices Index, Index2...
345 -- Id_Ref is an indexed component form created by the enclosing init proc.
346 -- Its successive indices are Val1, Val2, ... which are the loop variables
347 -- in the loops that call the individual task init proc on each component.
349 -- The generated function has the following structure:
351 -- function F return String is
352 -- Pref : string renames Task_Name;
353 -- T1 : String := Index1'Image (Val1);
355 -- Tn : String := indexn'image (Valn);
356 -- Len : Integer := T1'Length + ... + Tn'Length + n + 1;
357 -- -- Len includes commas and the end parentheses.
358 -- Res : String (1..Len);
359 -- Pos : Integer := Pref'Length;
362 -- Res (1 .. Pos) := Pref;
366 -- Res (Pos .. Pos + T1'Length - 1) := T1;
367 -- Pos := Pos + T1'Length;
371 -- Res (Pos .. Pos + Tn'Length - 1) := Tn;
377 -- Needless to say, multidimensional arrays of tasks are rare enough
378 -- that the bulkiness of this code is not really a concern.
380 function Build_Task_Array_Image
384 Dyn : Boolean := False) return Node_Id
386 Dims : constant Nat := Number_Dimensions (A_Type);
387 -- Number of dimensions for array of tasks
389 Temps : array (1 .. Dims) of Entity_Id;
390 -- Array of temporaries to hold string for each index
396 -- Total length of generated name
399 -- Running index for substring assignments
401 Pref : constant Entity_Id := Make_Temporary (Loc, 'P');
402 -- Name of enclosing variable, prefix of resulting name
405 -- String to hold result
408 -- Value of successive indices
411 -- Expression to compute total size of string
414 -- Entity for name at one index position
416 Decls : constant List_Id := New_List;
417 Stats : constant List_Id := New_List;
420 -- For a dynamic task, the name comes from the target variable.
421 -- For a static one it is a formal of the enclosing init proc.
424 Get_Name_String (Chars (Entity (Prefix (Id_Ref))));
426 Make_Object_Declaration (Loc,
427 Defining_Identifier => Pref,
428 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
430 Make_String_Literal (Loc,
431 Strval => String_From_Name_Buffer)));
435 Make_Object_Renaming_Declaration (Loc,
436 Defining_Identifier => Pref,
437 Subtype_Mark => New_Occurrence_Of (Standard_String, Loc),
438 Name => Make_Identifier (Loc, Name_uTask_Name)));
441 Indx := First_Index (A_Type);
442 Val := First (Expressions (Id_Ref));
444 for J in 1 .. Dims loop
445 T := Make_Temporary (Loc, 'T');
449 Make_Object_Declaration (Loc,
450 Defining_Identifier => T,
451 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
453 Make_Attribute_Reference (Loc,
454 Attribute_Name => Name_Image,
455 Prefix => New_Occurrence_Of (Etype (Indx), Loc),
456 Expressions => New_List (New_Copy_Tree (Val)))));
462 Sum := Make_Integer_Literal (Loc, Dims + 1);
468 Make_Attribute_Reference (Loc,
469 Attribute_Name => Name_Length,
471 New_Occurrence_Of (Pref, Loc),
472 Expressions => New_List (Make_Integer_Literal (Loc, 1))));
474 for J in 1 .. Dims loop
479 Make_Attribute_Reference (Loc,
480 Attribute_Name => Name_Length,
482 New_Occurrence_Of (Temps (J), Loc),
483 Expressions => New_List (Make_Integer_Literal (Loc, 1))));
486 Build_Task_Image_Prefix (Loc, Len, Res, Pos, Pref, Sum, Decls, Stats);
488 Set_Character_Literal_Name (Char_Code (Character'Pos ('(')));
491 Make_Assignment_Statement (Loc,
492 Name => Make_Indexed_Component (Loc,
493 Prefix => New_Occurrence_Of (Res, Loc),
494 Expressions => New_List (New_Occurrence_Of (Pos, Loc))),
496 Make_Character_Literal (Loc,
498 Char_Literal_Value =>
499 UI_From_Int (Character'Pos ('(')))));
502 Make_Assignment_Statement (Loc,
503 Name => New_Occurrence_Of (Pos, Loc),
506 Left_Opnd => New_Occurrence_Of (Pos, Loc),
507 Right_Opnd => Make_Integer_Literal (Loc, 1))));
509 for J in 1 .. Dims loop
512 Make_Assignment_Statement (Loc,
513 Name => Make_Slice (Loc,
514 Prefix => New_Occurrence_Of (Res, Loc),
517 Low_Bound => New_Occurrence_Of (Pos, Loc),
518 High_Bound => Make_Op_Subtract (Loc,
521 Left_Opnd => New_Occurrence_Of (Pos, Loc),
523 Make_Attribute_Reference (Loc,
524 Attribute_Name => Name_Length,
526 New_Occurrence_Of (Temps (J), Loc),
528 New_List (Make_Integer_Literal (Loc, 1)))),
529 Right_Opnd => Make_Integer_Literal (Loc, 1)))),
531 Expression => New_Occurrence_Of (Temps (J), Loc)));
535 Make_Assignment_Statement (Loc,
536 Name => New_Occurrence_Of (Pos, Loc),
539 Left_Opnd => New_Occurrence_Of (Pos, Loc),
541 Make_Attribute_Reference (Loc,
542 Attribute_Name => Name_Length,
543 Prefix => New_Occurrence_Of (Temps (J), Loc),
545 New_List (Make_Integer_Literal (Loc, 1))))));
547 Set_Character_Literal_Name (Char_Code (Character'Pos (',')));
550 Make_Assignment_Statement (Loc,
551 Name => Make_Indexed_Component (Loc,
552 Prefix => New_Occurrence_Of (Res, Loc),
553 Expressions => New_List (New_Occurrence_Of (Pos, Loc))),
555 Make_Character_Literal (Loc,
557 Char_Literal_Value =>
558 UI_From_Int (Character'Pos (',')))));
561 Make_Assignment_Statement (Loc,
562 Name => New_Occurrence_Of (Pos, Loc),
565 Left_Opnd => New_Occurrence_Of (Pos, Loc),
566 Right_Opnd => Make_Integer_Literal (Loc, 1))));
570 Set_Character_Literal_Name (Char_Code (Character'Pos (')')));
573 Make_Assignment_Statement (Loc,
574 Name => Make_Indexed_Component (Loc,
575 Prefix => New_Occurrence_Of (Res, Loc),
576 Expressions => New_List (New_Occurrence_Of (Len, Loc))),
578 Make_Character_Literal (Loc,
580 Char_Literal_Value =>
581 UI_From_Int (Character'Pos (')')))));
582 return Build_Task_Image_Function (Loc, Decls, Stats, Res);
583 end Build_Task_Array_Image;
585 ----------------------------
586 -- Build_Task_Image_Decls --
587 ----------------------------
589 function Build_Task_Image_Decls
593 In_Init_Proc : Boolean := False) return List_Id
595 Decls : constant List_Id := New_List;
596 T_Id : Entity_Id := Empty;
598 Expr : Node_Id := Empty;
599 Fun : Node_Id := Empty;
600 Is_Dyn : constant Boolean :=
601 Nkind (Parent (Id_Ref)) = N_Assignment_Statement
603 Nkind (Expression (Parent (Id_Ref))) = N_Allocator;
606 -- If Discard_Names or No_Implicit_Heap_Allocations are in effect,
607 -- generate a dummy declaration only.
609 if Restriction_Active (No_Implicit_Heap_Allocations)
610 or else Global_Discard_Names
612 T_Id := Make_Temporary (Loc, 'J');
617 Make_Object_Declaration (Loc,
618 Defining_Identifier => T_Id,
619 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
621 Make_String_Literal (Loc,
622 Strval => String_From_Name_Buffer)));
625 if Nkind (Id_Ref) = N_Identifier
626 or else Nkind (Id_Ref) = N_Defining_Identifier
628 -- For a simple variable, the image of the task is built from
629 -- the name of the variable. To avoid possible conflict with
630 -- the anonymous type created for a single protected object,
631 -- add a numeric suffix.
634 Make_Defining_Identifier (Loc,
635 New_External_Name (Chars (Id_Ref), 'T', 1));
637 Get_Name_String (Chars (Id_Ref));
640 Make_String_Literal (Loc,
641 Strval => String_From_Name_Buffer);
643 elsif Nkind (Id_Ref) = N_Selected_Component then
645 Make_Defining_Identifier (Loc,
646 New_External_Name (Chars (Selector_Name (Id_Ref)), 'T'));
647 Fun := Build_Task_Record_Image (Loc, Id_Ref, Is_Dyn);
649 elsif Nkind (Id_Ref) = N_Indexed_Component then
651 Make_Defining_Identifier (Loc,
652 New_External_Name (Chars (A_Type), 'N'));
654 Fun := Build_Task_Array_Image (Loc, Id_Ref, A_Type, Is_Dyn);
658 if Present (Fun) then
660 Expr := Make_Function_Call (Loc,
661 Name => New_Occurrence_Of (Defining_Entity (Fun), Loc));
663 if not In_Init_Proc and then VM_Target = No_VM then
664 Set_Uses_Sec_Stack (Defining_Entity (Fun));
668 Decl := Make_Object_Declaration (Loc,
669 Defining_Identifier => T_Id,
670 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
671 Constant_Present => True,
674 Append (Decl, Decls);
676 end Build_Task_Image_Decls;
678 -------------------------------
679 -- Build_Task_Image_Function --
680 -------------------------------
682 function Build_Task_Image_Function
686 Res : Entity_Id) return Node_Id
692 Make_Simple_Return_Statement (Loc,
693 Expression => New_Occurrence_Of (Res, Loc)));
695 Spec := Make_Function_Specification (Loc,
696 Defining_Unit_Name => Make_Temporary (Loc, 'F'),
697 Result_Definition => New_Occurrence_Of (Standard_String, Loc));
699 -- Calls to 'Image use the secondary stack, which must be cleaned
700 -- up after the task name is built.
702 return Make_Subprogram_Body (Loc,
703 Specification => Spec,
704 Declarations => Decls,
705 Handled_Statement_Sequence =>
706 Make_Handled_Sequence_Of_Statements (Loc, Statements => Stats));
707 end Build_Task_Image_Function;
709 -----------------------------
710 -- Build_Task_Image_Prefix --
711 -----------------------------
713 procedure Build_Task_Image_Prefix
724 Len := Make_Temporary (Loc, 'L', Sum);
727 Make_Object_Declaration (Loc,
728 Defining_Identifier => Len,
729 Object_Definition => New_Occurrence_Of (Standard_Integer, Loc),
732 Res := Make_Temporary (Loc, 'R');
735 Make_Object_Declaration (Loc,
736 Defining_Identifier => Res,
738 Make_Subtype_Indication (Loc,
739 Subtype_Mark => New_Occurrence_Of (Standard_String, Loc),
741 Make_Index_Or_Discriminant_Constraint (Loc,
745 Low_Bound => Make_Integer_Literal (Loc, 1),
746 High_Bound => New_Occurrence_Of (Len, Loc)))))));
748 Pos := Make_Temporary (Loc, 'P');
751 Make_Object_Declaration (Loc,
752 Defining_Identifier => Pos,
753 Object_Definition => New_Occurrence_Of (Standard_Integer, Loc)));
755 -- Pos := Prefix'Length;
758 Make_Assignment_Statement (Loc,
759 Name => New_Occurrence_Of (Pos, Loc),
761 Make_Attribute_Reference (Loc,
762 Attribute_Name => Name_Length,
763 Prefix => New_Occurrence_Of (Prefix, Loc),
764 Expressions => New_List (Make_Integer_Literal (Loc, 1)))));
766 -- Res (1 .. Pos) := Prefix;
769 Make_Assignment_Statement (Loc,
772 Prefix => New_Occurrence_Of (Res, Loc),
775 Low_Bound => Make_Integer_Literal (Loc, 1),
776 High_Bound => New_Occurrence_Of (Pos, Loc))),
778 Expression => New_Occurrence_Of (Prefix, Loc)));
781 Make_Assignment_Statement (Loc,
782 Name => New_Occurrence_Of (Pos, Loc),
785 Left_Opnd => New_Occurrence_Of (Pos, Loc),
786 Right_Opnd => Make_Integer_Literal (Loc, 1))));
787 end Build_Task_Image_Prefix;
789 -----------------------------
790 -- Build_Task_Record_Image --
791 -----------------------------
793 function Build_Task_Record_Image
796 Dyn : Boolean := False) return Node_Id
799 -- Total length of generated name
805 -- String to hold result
807 Pref : constant Entity_Id := Make_Temporary (Loc, 'P');
808 -- Name of enclosing variable, prefix of resulting name
811 -- Expression to compute total size of string
814 -- Entity for selector name
816 Decls : constant List_Id := New_List;
817 Stats : constant List_Id := New_List;
820 -- For a dynamic task, the name comes from the target variable.
821 -- For a static one it is a formal of the enclosing init proc.
824 Get_Name_String (Chars (Entity (Prefix (Id_Ref))));
826 Make_Object_Declaration (Loc,
827 Defining_Identifier => Pref,
828 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
830 Make_String_Literal (Loc,
831 Strval => String_From_Name_Buffer)));
835 Make_Object_Renaming_Declaration (Loc,
836 Defining_Identifier => Pref,
837 Subtype_Mark => New_Occurrence_Of (Standard_String, Loc),
838 Name => Make_Identifier (Loc, Name_uTask_Name)));
841 Sel := Make_Temporary (Loc, 'S');
843 Get_Name_String (Chars (Selector_Name (Id_Ref)));
846 Make_Object_Declaration (Loc,
847 Defining_Identifier => Sel,
848 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
850 Make_String_Literal (Loc,
851 Strval => String_From_Name_Buffer)));
853 Sum := Make_Integer_Literal (Loc, Nat (Name_Len + 1));
859 Make_Attribute_Reference (Loc,
860 Attribute_Name => Name_Length,
862 New_Occurrence_Of (Pref, Loc),
863 Expressions => New_List (Make_Integer_Literal (Loc, 1))));
865 Build_Task_Image_Prefix (Loc, Len, Res, Pos, Pref, Sum, Decls, Stats);
867 Set_Character_Literal_Name (Char_Code (Character'Pos ('.')));
872 Make_Assignment_Statement (Loc,
873 Name => Make_Indexed_Component (Loc,
874 Prefix => New_Occurrence_Of (Res, Loc),
875 Expressions => New_List (New_Occurrence_Of (Pos, Loc))),
877 Make_Character_Literal (Loc,
879 Char_Literal_Value =>
880 UI_From_Int (Character'Pos ('.')))));
883 Make_Assignment_Statement (Loc,
884 Name => New_Occurrence_Of (Pos, Loc),
887 Left_Opnd => New_Occurrence_Of (Pos, Loc),
888 Right_Opnd => Make_Integer_Literal (Loc, 1))));
890 -- Res (Pos .. Len) := Selector;
893 Make_Assignment_Statement (Loc,
894 Name => Make_Slice (Loc,
895 Prefix => New_Occurrence_Of (Res, Loc),
898 Low_Bound => New_Occurrence_Of (Pos, Loc),
899 High_Bound => New_Occurrence_Of (Len, Loc))),
900 Expression => New_Occurrence_Of (Sel, Loc)));
902 return Build_Task_Image_Function (Loc, Decls, Stats, Res);
903 end Build_Task_Record_Image;
905 ----------------------------------
906 -- Component_May_Be_Bit_Aligned --
907 ----------------------------------
909 function Component_May_Be_Bit_Aligned (Comp : Entity_Id) return Boolean is
910 UT : constant Entity_Id := Underlying_Type (Etype (Comp));
913 -- If no component clause, then everything is fine, since the back end
914 -- never bit-misaligns by default, even if there is a pragma Packed for
917 if No (Component_Clause (Comp)) then
921 -- It is only array and record types that cause trouble
923 if not Is_Record_Type (UT)
924 and then not Is_Array_Type (UT)
928 -- If we know that we have a small (64 bits or less) record or small
929 -- bit-packed array, then everything is fine, since the back end can
930 -- handle these cases correctly.
932 elsif Esize (Comp) <= 64
933 and then (Is_Record_Type (UT)
934 or else Is_Bit_Packed_Array (UT))
938 -- Otherwise if the component is not byte aligned, we know we have the
939 -- nasty unaligned case.
941 elsif Normalized_First_Bit (Comp) /= Uint_0
942 or else Esize (Comp) mod System_Storage_Unit /= Uint_0
946 -- If we are large and byte aligned, then OK at this level
951 end Component_May_Be_Bit_Aligned;
953 -----------------------------------
954 -- Corresponding_Runtime_Package --
955 -----------------------------------
957 function Corresponding_Runtime_Package (Typ : Entity_Id) return RTU_Id is
958 Pkg_Id : RTU_Id := RTU_Null;
961 pragma Assert (Is_Concurrent_Type (Typ));
963 if Ekind (Typ) in Protected_Kind then
965 or else Has_Interrupt_Handler (Typ)
966 or else (Has_Attach_Handler (Typ)
967 and then not Restricted_Profile)
969 -- A protected type without entries that covers an interface and
970 -- overrides the abstract routines with protected procedures is
971 -- considered equivalent to a protected type with entries in the
972 -- context of dispatching select statements. It is sufficient to
973 -- check for the presence of an interface list in the declaration
974 -- node to recognize this case.
976 or else Present (Interface_List (Parent (Typ)))
979 or else Restriction_Active (No_Entry_Queue) = False
980 or else Number_Entries (Typ) > 1
981 or else (Has_Attach_Handler (Typ)
982 and then not Restricted_Profile)
984 Pkg_Id := System_Tasking_Protected_Objects_Entries;
986 Pkg_Id := System_Tasking_Protected_Objects_Single_Entry;
990 Pkg_Id := System_Tasking_Protected_Objects;
995 end Corresponding_Runtime_Package;
997 -------------------------------
998 -- Convert_To_Actual_Subtype --
999 -------------------------------
1001 procedure Convert_To_Actual_Subtype (Exp : Entity_Id) is
1005 Act_ST := Get_Actual_Subtype (Exp);
1007 if Act_ST = Etype (Exp) then
1012 Convert_To (Act_ST, Relocate_Node (Exp)));
1013 Analyze_And_Resolve (Exp, Act_ST);
1015 end Convert_To_Actual_Subtype;
1017 -----------------------------------
1018 -- Current_Sem_Unit_Declarations --
1019 -----------------------------------
1021 function Current_Sem_Unit_Declarations return List_Id is
1022 U : Node_Id := Unit (Cunit (Current_Sem_Unit));
1026 -- If the current unit is a package body, locate the visible
1027 -- declarations of the package spec.
1029 if Nkind (U) = N_Package_Body then
1030 U := Unit (Library_Unit (Cunit (Current_Sem_Unit)));
1033 if Nkind (U) = N_Package_Declaration then
1034 U := Specification (U);
1035 Decls := Visible_Declarations (U);
1039 Set_Visible_Declarations (U, Decls);
1043 Decls := Declarations (U);
1047 Set_Declarations (U, Decls);
1052 end Current_Sem_Unit_Declarations;
1054 -----------------------
1055 -- Duplicate_Subexpr --
1056 -----------------------
1058 function Duplicate_Subexpr
1060 Name_Req : Boolean := False) return Node_Id
1063 Remove_Side_Effects (Exp, Name_Req);
1064 return New_Copy_Tree (Exp);
1065 end Duplicate_Subexpr;
1067 ---------------------------------
1068 -- Duplicate_Subexpr_No_Checks --
1069 ---------------------------------
1071 function Duplicate_Subexpr_No_Checks
1073 Name_Req : Boolean := False) return Node_Id
1078 Remove_Side_Effects (Exp, Name_Req);
1079 New_Exp := New_Copy_Tree (Exp);
1080 Remove_Checks (New_Exp);
1082 end Duplicate_Subexpr_No_Checks;
1084 -----------------------------------
1085 -- Duplicate_Subexpr_Move_Checks --
1086 -----------------------------------
1088 function Duplicate_Subexpr_Move_Checks
1090 Name_Req : Boolean := False) return Node_Id
1095 Remove_Side_Effects (Exp, Name_Req);
1096 New_Exp := New_Copy_Tree (Exp);
1097 Remove_Checks (Exp);
1099 end Duplicate_Subexpr_Move_Checks;
1101 --------------------
1102 -- Ensure_Defined --
1103 --------------------
1105 procedure Ensure_Defined (Typ : Entity_Id; N : Node_Id) is
1109 -- An itype reference must only be created if this is a local
1110 -- itype, so that gigi can elaborate it on the proper objstack.
1113 and then Scope (Typ) = Current_Scope
1115 IR := Make_Itype_Reference (Sloc (N));
1116 Set_Itype (IR, Typ);
1117 Insert_Action (N, IR);
1121 --------------------
1122 -- Entry_Names_OK --
1123 --------------------
1125 function Entry_Names_OK return Boolean is
1128 not Restricted_Profile
1129 and then not Global_Discard_Names
1130 and then not Restriction_Active (No_Implicit_Heap_Allocations)
1131 and then not Restriction_Active (No_Local_Allocators);
1134 ---------------------
1135 -- Evolve_And_Then --
1136 ---------------------
1138 procedure Evolve_And_Then (Cond : in out Node_Id; Cond1 : Node_Id) is
1144 Make_And_Then (Sloc (Cond1),
1146 Right_Opnd => Cond1);
1148 end Evolve_And_Then;
1150 --------------------
1151 -- Evolve_Or_Else --
1152 --------------------
1154 procedure Evolve_Or_Else (Cond : in out Node_Id; Cond1 : Node_Id) is
1160 Make_Or_Else (Sloc (Cond1),
1162 Right_Opnd => Cond1);
1166 ------------------------------
1167 -- Expand_Subtype_From_Expr --
1168 ------------------------------
1170 -- This function is applicable for both static and dynamic allocation of
1171 -- objects which are constrained by an initial expression. Basically it
1172 -- transforms an unconstrained subtype indication into a constrained one.
1173 -- The expression may also be transformed in certain cases in order to
1174 -- avoid multiple evaluation. In the static allocation case, the general
1179 -- is transformed into
1181 -- Val : Constrained_Subtype_of_T := Maybe_Modified_Expr;
1183 -- Here are the main cases :
1185 -- <if Expr is a Slice>
1186 -- Val : T ([Index_Subtype (Expr)]) := Expr;
1188 -- <elsif Expr is a String Literal>
1189 -- Val : T (T'First .. T'First + Length (string literal) - 1) := Expr;
1191 -- <elsif Expr is Constrained>
1192 -- subtype T is Type_Of_Expr
1195 -- <elsif Expr is an entity_name>
1196 -- Val : T (constraints taken from Expr) := Expr;
1199 -- type Axxx is access all T;
1200 -- Rval : Axxx := Expr'ref;
1201 -- Val : T (constraints taken from Rval) := Rval.all;
1203 -- ??? note: when the Expression is allocated in the secondary stack
1204 -- we could use it directly instead of copying it by declaring
1205 -- Val : T (...) renames Rval.all
1207 procedure Expand_Subtype_From_Expr
1209 Unc_Type : Entity_Id;
1210 Subtype_Indic : Node_Id;
1213 Loc : constant Source_Ptr := Sloc (N);
1214 Exp_Typ : constant Entity_Id := Etype (Exp);
1218 -- In general we cannot build the subtype if expansion is disabled,
1219 -- because internal entities may not have been defined. However, to
1220 -- avoid some cascaded errors, we try to continue when the expression
1221 -- is an array (or string), because it is safe to compute the bounds.
1222 -- It is in fact required to do so even in a generic context, because
1223 -- there may be constants that depend on bounds of string literal.
1225 if not Expander_Active
1226 and then (No (Etype (Exp))
1227 or else Base_Type (Etype (Exp)) /= Standard_String)
1232 if Nkind (Exp) = N_Slice then
1234 Slice_Type : constant Entity_Id := Etype (First_Index (Exp_Typ));
1237 Rewrite (Subtype_Indic,
1238 Make_Subtype_Indication (Loc,
1239 Subtype_Mark => New_Reference_To (Unc_Type, Loc),
1241 Make_Index_Or_Discriminant_Constraint (Loc,
1242 Constraints => New_List
1243 (New_Reference_To (Slice_Type, Loc)))));
1245 -- This subtype indication may be used later for constraint checks
1246 -- we better make sure that if a variable was used as a bound of
1247 -- of the original slice, its value is frozen.
1249 Force_Evaluation (Low_Bound (Scalar_Range (Slice_Type)));
1250 Force_Evaluation (High_Bound (Scalar_Range (Slice_Type)));
1253 elsif Ekind (Exp_Typ) = E_String_Literal_Subtype then
1254 Rewrite (Subtype_Indic,
1255 Make_Subtype_Indication (Loc,
1256 Subtype_Mark => New_Reference_To (Unc_Type, Loc),
1258 Make_Index_Or_Discriminant_Constraint (Loc,
1259 Constraints => New_List (
1260 Make_Literal_Range (Loc,
1261 Literal_Typ => Exp_Typ)))));
1263 elsif Is_Constrained (Exp_Typ)
1264 and then not Is_Class_Wide_Type (Unc_Type)
1266 if Is_Itype (Exp_Typ) then
1268 -- Within an initialization procedure, a selected component
1269 -- denotes a component of the enclosing record, and it appears
1270 -- as an actual in a call to its own initialization procedure.
1271 -- If this component depends on the outer discriminant, we must
1272 -- generate the proper actual subtype for it.
1274 if Nkind (Exp) = N_Selected_Component
1275 and then Within_Init_Proc
1278 Decl : constant Node_Id :=
1279 Build_Actual_Subtype_Of_Component (Exp_Typ, Exp);
1281 if Present (Decl) then
1282 Insert_Action (N, Decl);
1283 T := Defining_Identifier (Decl);
1289 -- No need to generate a new one (new what???)
1296 T := Make_Temporary (Loc, 'T');
1299 Make_Subtype_Declaration (Loc,
1300 Defining_Identifier => T,
1301 Subtype_Indication => New_Reference_To (Exp_Typ, Loc)));
1303 -- This type is marked as an itype even though it has an
1304 -- explicit declaration because otherwise it can be marked
1305 -- with Is_Generic_Actual_Type and generate spurious errors.
1306 -- (see sem_ch8.Analyze_Package_Renaming and sem_type.covers)
1309 Set_Associated_Node_For_Itype (T, Exp);
1312 Rewrite (Subtype_Indic, New_Reference_To (T, Loc));
1314 -- Nothing needs to be done for private types with unknown discriminants
1315 -- if the underlying type is not an unconstrained composite type or it
1316 -- is an unchecked union.
1318 elsif Is_Private_Type (Unc_Type)
1319 and then Has_Unknown_Discriminants (Unc_Type)
1320 and then (not Is_Composite_Type (Underlying_Type (Unc_Type))
1321 or else Is_Constrained (Underlying_Type (Unc_Type))
1322 or else Is_Unchecked_Union (Underlying_Type (Unc_Type)))
1326 -- Case of derived type with unknown discriminants where the parent type
1327 -- also has unknown discriminants.
1329 elsif Is_Record_Type (Unc_Type)
1330 and then not Is_Class_Wide_Type (Unc_Type)
1331 and then Has_Unknown_Discriminants (Unc_Type)
1332 and then Has_Unknown_Discriminants (Underlying_Type (Unc_Type))
1334 -- Nothing to be done if no underlying record view available
1336 if No (Underlying_Record_View (Unc_Type)) then
1339 -- Otherwise use the Underlying_Record_View to create the proper
1340 -- constrained subtype for an object of a derived type with unknown
1344 Remove_Side_Effects (Exp);
1345 Rewrite (Subtype_Indic,
1346 Make_Subtype_From_Expr (Exp, Underlying_Record_View (Unc_Type)));
1349 -- Renamings of class-wide interface types require no equivalent
1350 -- constrained type declarations because we only need to reference
1351 -- the tag component associated with the interface.
1354 and then Nkind (N) = N_Object_Renaming_Declaration
1355 and then Is_Interface (Unc_Type)
1357 pragma Assert (Is_Class_Wide_Type (Unc_Type));
1360 -- In Ada95, nothing to be done if the type of the expression is
1361 -- limited, because in this case the expression cannot be copied,
1362 -- and its use can only be by reference.
1364 -- In Ada2005, the context can be an object declaration whose expression
1365 -- is a function that returns in place. If the nominal subtype has
1366 -- unknown discriminants, the call still provides constraints on the
1367 -- object, and we have to create an actual subtype from it.
1369 -- If the type is class-wide, the expression is dynamically tagged and
1370 -- we do not create an actual subtype either. Ditto for an interface.
1372 elsif Is_Limited_Type (Exp_Typ)
1374 (Is_Class_Wide_Type (Exp_Typ)
1375 or else Is_Interface (Exp_Typ)
1376 or else not Has_Unknown_Discriminants (Exp_Typ)
1377 or else not Is_Composite_Type (Unc_Type))
1381 -- For limited objects initialized with build in place function calls,
1382 -- nothing to be done; otherwise we prematurely introduce an N_Reference
1383 -- node in the expression initializing the object, which breaks the
1384 -- circuitry that detects and adds the additional arguments to the
1387 elsif Is_Build_In_Place_Function_Call (Exp) then
1391 Remove_Side_Effects (Exp);
1392 Rewrite (Subtype_Indic,
1393 Make_Subtype_From_Expr (Exp, Unc_Type));
1395 end Expand_Subtype_From_Expr;
1397 --------------------
1398 -- Find_Init_Call --
1399 --------------------
1401 function Find_Init_Call
1403 Rep_Clause : Node_Id) return Node_Id
1405 Typ : constant Entity_Id := Etype (Var);
1407 Init_Proc : Entity_Id;
1408 -- Initialization procedure for Typ
1410 function Find_Init_Call_In_List (From : Node_Id) return Node_Id;
1411 -- Look for init call for Var starting at From and scanning the
1412 -- enclosing list until Rep_Clause or the end of the list is reached.
1414 ----------------------------
1415 -- Find_Init_Call_In_List --
1416 ----------------------------
1418 function Find_Init_Call_In_List (From : Node_Id) return Node_Id is
1419 Init_Call : Node_Id;
1423 while Present (Init_Call) and then Init_Call /= Rep_Clause loop
1424 if Nkind (Init_Call) = N_Procedure_Call_Statement
1425 and then Is_Entity_Name (Name (Init_Call))
1426 and then Entity (Name (Init_Call)) = Init_Proc
1434 end Find_Init_Call_In_List;
1436 Init_Call : Node_Id;
1438 -- Start of processing for Find_Init_Call
1441 if not Has_Non_Null_Base_Init_Proc (Typ) then
1442 -- No init proc for the type, so obviously no call to be found
1447 Init_Proc := Base_Init_Proc (Typ);
1449 -- First scan the list containing the declaration of Var
1451 Init_Call := Find_Init_Call_In_List (From => Next (Parent (Var)));
1453 -- If not found, also look on Var's freeze actions list, if any, since
1454 -- the init call may have been moved there (case of an address clause
1455 -- applying to Var).
1457 if No (Init_Call) and then Present (Freeze_Node (Var)) then
1458 Init_Call := Find_Init_Call_In_List
1459 (First (Actions (Freeze_Node (Var))));
1465 ------------------------
1466 -- Find_Interface_ADT --
1467 ------------------------
1469 function Find_Interface_ADT
1471 Iface : Entity_Id) return Elmt_Id
1474 Typ : Entity_Id := T;
1477 pragma Assert (Is_Interface (Iface));
1479 -- Handle private types
1481 if Has_Private_Declaration (Typ)
1482 and then Present (Full_View (Typ))
1484 Typ := Full_View (Typ);
1487 -- Handle access types
1489 if Is_Access_Type (Typ) then
1490 Typ := Directly_Designated_Type (Typ);
1493 -- Handle task and protected types implementing interfaces
1495 if Is_Concurrent_Type (Typ) then
1496 Typ := Corresponding_Record_Type (Typ);
1500 (not Is_Class_Wide_Type (Typ)
1501 and then Ekind (Typ) /= E_Incomplete_Type);
1503 if Is_Ancestor (Iface, Typ) then
1504 return First_Elmt (Access_Disp_Table (Typ));
1508 Next_Elmt (Next_Elmt (First_Elmt (Access_Disp_Table (Typ))));
1510 and then Present (Related_Type (Node (ADT)))
1511 and then Related_Type (Node (ADT)) /= Iface
1512 and then not Is_Ancestor (Iface, Related_Type (Node (ADT)))
1517 pragma Assert (Present (Related_Type (Node (ADT))));
1520 end Find_Interface_ADT;
1522 ------------------------
1523 -- Find_Interface_Tag --
1524 ------------------------
1526 function Find_Interface_Tag
1528 Iface : Entity_Id) return Entity_Id
1531 Found : Boolean := False;
1532 Typ : Entity_Id := T;
1534 procedure Find_Tag (Typ : Entity_Id);
1535 -- Internal subprogram used to recursively climb to the ancestors
1541 procedure Find_Tag (Typ : Entity_Id) is
1546 -- This routine does not handle the case in which the interface is an
1547 -- ancestor of Typ. That case is handled by the enclosing subprogram.
1549 pragma Assert (Typ /= Iface);
1551 -- Climb to the root type handling private types
1553 if Present (Full_View (Etype (Typ))) then
1554 if Full_View (Etype (Typ)) /= Typ then
1555 Find_Tag (Full_View (Etype (Typ)));
1558 elsif Etype (Typ) /= Typ then
1559 Find_Tag (Etype (Typ));
1562 -- Traverse the list of interfaces implemented by the type
1565 and then Present (Interfaces (Typ))
1566 and then not (Is_Empty_Elmt_List (Interfaces (Typ)))
1568 -- Skip the tag associated with the primary table
1570 pragma Assert (Etype (First_Tag_Component (Typ)) = RTE (RE_Tag));
1571 AI_Tag := Next_Tag_Component (First_Tag_Component (Typ));
1572 pragma Assert (Present (AI_Tag));
1574 AI_Elmt := First_Elmt (Interfaces (Typ));
1575 while Present (AI_Elmt) loop
1576 AI := Node (AI_Elmt);
1578 if AI = Iface or else Is_Ancestor (Iface, AI) then
1583 AI_Tag := Next_Tag_Component (AI_Tag);
1584 Next_Elmt (AI_Elmt);
1589 -- Start of processing for Find_Interface_Tag
1592 pragma Assert (Is_Interface (Iface));
1594 -- Handle access types
1596 if Is_Access_Type (Typ) then
1597 Typ := Directly_Designated_Type (Typ);
1600 -- Handle class-wide types
1602 if Is_Class_Wide_Type (Typ) then
1603 Typ := Root_Type (Typ);
1606 -- Handle private types
1608 if Has_Private_Declaration (Typ)
1609 and then Present (Full_View (Typ))
1611 Typ := Full_View (Typ);
1614 -- Handle entities from the limited view
1616 if Ekind (Typ) = E_Incomplete_Type then
1617 pragma Assert (Present (Non_Limited_View (Typ)));
1618 Typ := Non_Limited_View (Typ);
1621 -- Handle task and protected types implementing interfaces
1623 if Is_Concurrent_Type (Typ) then
1624 Typ := Corresponding_Record_Type (Typ);
1627 -- If the interface is an ancestor of the type, then it shared the
1628 -- primary dispatch table.
1630 if Is_Ancestor (Iface, Typ) then
1631 pragma Assert (Etype (First_Tag_Component (Typ)) = RTE (RE_Tag));
1632 return First_Tag_Component (Typ);
1634 -- Otherwise we need to search for its associated tag component
1638 pragma Assert (Found);
1641 end Find_Interface_Tag;
1647 function Find_Prim_Op (T : Entity_Id; Name : Name_Id) return Entity_Id is
1649 Typ : Entity_Id := T;
1653 if Is_Class_Wide_Type (Typ) then
1654 Typ := Root_Type (Typ);
1657 Typ := Underlying_Type (Typ);
1659 -- Loop through primitive operations
1661 Prim := First_Elmt (Primitive_Operations (Typ));
1662 while Present (Prim) loop
1665 -- We can retrieve primitive operations by name if it is an internal
1666 -- name. For equality we must check that both of its operands have
1667 -- the same type, to avoid confusion with user-defined equalities
1668 -- than may have a non-symmetric signature.
1670 exit when Chars (Op) = Name
1673 or else Etype (First_Entity (Op)) = Etype (Last_Entity (Op)));
1677 -- Raise Program_Error if no primitive found
1680 raise Program_Error;
1691 function Find_Prim_Op
1693 Name : TSS_Name_Type) return Entity_Id
1696 Typ : Entity_Id := T;
1699 if Is_Class_Wide_Type (Typ) then
1700 Typ := Root_Type (Typ);
1703 Typ := Underlying_Type (Typ);
1705 Prim := First_Elmt (Primitive_Operations (Typ));
1706 while not Is_TSS (Node (Prim), Name) loop
1709 -- Raise program error if no primitive found
1712 raise Program_Error;
1719 ----------------------------
1720 -- Find_Protection_Object --
1721 ----------------------------
1723 function Find_Protection_Object (Scop : Entity_Id) return Entity_Id is
1728 while Present (S) loop
1729 if (Ekind (S) = E_Entry
1730 or else Ekind (S) = E_Entry_Family
1731 or else Ekind (S) = E_Function
1732 or else Ekind (S) = E_Procedure)
1733 and then Present (Protection_Object (S))
1735 return Protection_Object (S);
1741 -- If we do not find a Protection object in the scope chain, then
1742 -- something has gone wrong, most likely the object was never created.
1744 raise Program_Error;
1745 end Find_Protection_Object;
1747 ----------------------
1748 -- Force_Evaluation --
1749 ----------------------
1751 procedure Force_Evaluation (Exp : Node_Id; Name_Req : Boolean := False) is
1753 Remove_Side_Effects (Exp, Name_Req, Variable_Ref => True);
1754 end Force_Evaluation;
1756 ------------------------
1757 -- Generate_Poll_Call --
1758 ------------------------
1760 procedure Generate_Poll_Call (N : Node_Id) is
1762 -- No poll call if polling not active
1764 if not Polling_Required then
1767 -- Otherwise generate require poll call
1770 Insert_Before_And_Analyze (N,
1771 Make_Procedure_Call_Statement (Sloc (N),
1772 Name => New_Occurrence_Of (RTE (RE_Poll), Sloc (N))));
1774 end Generate_Poll_Call;
1776 ---------------------------------
1777 -- Get_Current_Value_Condition --
1778 ---------------------------------
1780 -- Note: the implementation of this procedure is very closely tied to the
1781 -- implementation of Set_Current_Value_Condition. In the Get procedure, we
1782 -- interpret Current_Value fields set by the Set procedure, so the two
1783 -- procedures need to be closely coordinated.
1785 procedure Get_Current_Value_Condition
1790 Loc : constant Source_Ptr := Sloc (Var);
1791 Ent : constant Entity_Id := Entity (Var);
1793 procedure Process_Current_Value_Condition
1796 -- N is an expression which holds either True (S = True) or False (S =
1797 -- False) in the condition. This procedure digs out the expression and
1798 -- if it refers to Ent, sets Op and Val appropriately.
1800 -------------------------------------
1801 -- Process_Current_Value_Condition --
1802 -------------------------------------
1804 procedure Process_Current_Value_Condition
1815 -- Deal with NOT operators, inverting sense
1817 while Nkind (Cond) = N_Op_Not loop
1818 Cond := Right_Opnd (Cond);
1822 -- Deal with AND THEN and AND cases
1824 if Nkind (Cond) = N_And_Then
1825 or else Nkind (Cond) = N_Op_And
1827 -- Don't ever try to invert a condition that is of the form
1828 -- of an AND or AND THEN (since we are not doing sufficiently
1829 -- general processing to allow this).
1831 if Sens = False then
1837 -- Recursively process AND and AND THEN branches
1839 Process_Current_Value_Condition (Left_Opnd (Cond), True);
1841 if Op /= N_Empty then
1845 Process_Current_Value_Condition (Right_Opnd (Cond), True);
1848 -- Case of relational operator
1850 elsif Nkind (Cond) in N_Op_Compare then
1853 -- Invert sense of test if inverted test
1855 if Sens = False then
1857 when N_Op_Eq => Op := N_Op_Ne;
1858 when N_Op_Ne => Op := N_Op_Eq;
1859 when N_Op_Lt => Op := N_Op_Ge;
1860 when N_Op_Gt => Op := N_Op_Le;
1861 when N_Op_Le => Op := N_Op_Gt;
1862 when N_Op_Ge => Op := N_Op_Lt;
1863 when others => raise Program_Error;
1867 -- Case of entity op value
1869 if Is_Entity_Name (Left_Opnd (Cond))
1870 and then Ent = Entity (Left_Opnd (Cond))
1871 and then Compile_Time_Known_Value (Right_Opnd (Cond))
1873 Val := Right_Opnd (Cond);
1875 -- Case of value op entity
1877 elsif Is_Entity_Name (Right_Opnd (Cond))
1878 and then Ent = Entity (Right_Opnd (Cond))
1879 and then Compile_Time_Known_Value (Left_Opnd (Cond))
1881 Val := Left_Opnd (Cond);
1883 -- We are effectively swapping operands
1886 when N_Op_Eq => null;
1887 when N_Op_Ne => null;
1888 when N_Op_Lt => Op := N_Op_Gt;
1889 when N_Op_Gt => Op := N_Op_Lt;
1890 when N_Op_Le => Op := N_Op_Ge;
1891 when N_Op_Ge => Op := N_Op_Le;
1892 when others => raise Program_Error;
1901 -- Case of Boolean variable reference, return as though the
1902 -- reference had said var = True.
1905 if Is_Entity_Name (Cond)
1906 and then Ent = Entity (Cond)
1908 Val := New_Occurrence_Of (Standard_True, Sloc (Cond));
1910 if Sens = False then
1917 end Process_Current_Value_Condition;
1919 -- Start of processing for Get_Current_Value_Condition
1925 -- Immediate return, nothing doing, if this is not an object
1927 if Ekind (Ent) not in Object_Kind then
1931 -- Otherwise examine current value
1934 CV : constant Node_Id := Current_Value (Ent);
1939 -- If statement. Condition is known true in THEN section, known False
1940 -- in any ELSIF or ELSE part, and unknown outside the IF statement.
1942 if Nkind (CV) = N_If_Statement then
1944 -- Before start of IF statement
1946 if Loc < Sloc (CV) then
1949 -- After end of IF statement
1951 elsif Loc >= Sloc (CV) + Text_Ptr (UI_To_Int (End_Span (CV))) then
1955 -- At this stage we know that we are within the IF statement, but
1956 -- unfortunately, the tree does not record the SLOC of the ELSE so
1957 -- we cannot use a simple SLOC comparison to distinguish between
1958 -- the then/else statements, so we have to climb the tree.
1965 while Parent (N) /= CV loop
1968 -- If we fall off the top of the tree, then that's odd, but
1969 -- perhaps it could occur in some error situation, and the
1970 -- safest response is simply to assume that the outcome of
1971 -- the condition is unknown. No point in bombing during an
1972 -- attempt to optimize things.
1979 -- Now we have N pointing to a node whose parent is the IF
1980 -- statement in question, so now we can tell if we are within
1981 -- the THEN statements.
1983 if Is_List_Member (N)
1984 and then List_Containing (N) = Then_Statements (CV)
1988 -- If the variable reference does not come from source, we
1989 -- cannot reliably tell whether it appears in the else part.
1990 -- In particular, if it appears in generated code for a node
1991 -- that requires finalization, it may be attached to a list
1992 -- that has not been yet inserted into the code. For now,
1993 -- treat it as unknown.
1995 elsif not Comes_From_Source (N) then
1998 -- Otherwise we must be in ELSIF or ELSE part
2005 -- ELSIF part. Condition is known true within the referenced
2006 -- ELSIF, known False in any subsequent ELSIF or ELSE part, and
2007 -- unknown before the ELSE part or after the IF statement.
2009 elsif Nkind (CV) = N_Elsif_Part then
2012 -- Before start of ELSIF part
2014 if Loc < Sloc (CV) then
2017 -- After end of IF statement
2019 elsif Loc >= Sloc (Stm) +
2020 Text_Ptr (UI_To_Int (End_Span (Stm)))
2025 -- Again we lack the SLOC of the ELSE, so we need to climb the
2026 -- tree to see if we are within the ELSIF part in question.
2033 while Parent (N) /= Stm loop
2036 -- If we fall off the top of the tree, then that's odd, but
2037 -- perhaps it could occur in some error situation, and the
2038 -- safest response is simply to assume that the outcome of
2039 -- the condition is unknown. No point in bombing during an
2040 -- attempt to optimize things.
2047 -- Now we have N pointing to a node whose parent is the IF
2048 -- statement in question, so see if is the ELSIF part we want.
2049 -- the THEN statements.
2054 -- Otherwise we must be in subsequent ELSIF or ELSE part
2061 -- Iteration scheme of while loop. The condition is known to be
2062 -- true within the body of the loop.
2064 elsif Nkind (CV) = N_Iteration_Scheme then
2066 Loop_Stmt : constant Node_Id := Parent (CV);
2069 -- Before start of body of loop
2071 if Loc < Sloc (Loop_Stmt) then
2074 -- After end of LOOP statement
2076 elsif Loc >= Sloc (End_Label (Loop_Stmt)) then
2079 -- We are within the body of the loop
2086 -- All other cases of Current_Value settings
2092 -- If we fall through here, then we have a reportable condition, Sens
2093 -- is True if the condition is true and False if it needs inverting.
2095 Process_Current_Value_Condition (Condition (CV), Sens);
2097 end Get_Current_Value_Condition;
2099 ---------------------------------
2100 -- Has_Controlled_Coextensions --
2101 ---------------------------------
2103 function Has_Controlled_Coextensions (Typ : Entity_Id) return Boolean is
2108 -- Only consider record types
2110 if Ekind (Typ) /= E_Record_Type
2111 and then Ekind (Typ) /= E_Record_Subtype
2116 if Has_Discriminants (Typ) then
2117 Discr := First_Discriminant (Typ);
2118 while Present (Discr) loop
2119 D_Typ := Etype (Discr);
2121 if Ekind (D_Typ) = E_Anonymous_Access_Type
2123 (Is_Controlled (Directly_Designated_Type (D_Typ))
2125 Is_Concurrent_Type (Directly_Designated_Type (D_Typ)))
2130 Next_Discriminant (Discr);
2135 end Has_Controlled_Coextensions;
2137 ------------------------
2138 -- Has_Address_Clause --
2139 ------------------------
2141 -- Should this function check the private part in a package ???
2143 function Has_Following_Address_Clause (D : Node_Id) return Boolean is
2144 Id : constant Entity_Id := Defining_Identifier (D);
2149 while Present (Decl) loop
2150 if Nkind (Decl) = N_At_Clause
2151 and then Chars (Identifier (Decl)) = Chars (Id)
2155 elsif Nkind (Decl) = N_Attribute_Definition_Clause
2156 and then Chars (Decl) = Name_Address
2157 and then Chars (Name (Decl)) = Chars (Id)
2166 end Has_Following_Address_Clause;
2168 --------------------
2169 -- Homonym_Number --
2170 --------------------
2172 function Homonym_Number (Subp : Entity_Id) return Nat is
2178 Hom := Homonym (Subp);
2179 while Present (Hom) loop
2180 if Scope (Hom) = Scope (Subp) then
2184 Hom := Homonym (Hom);
2190 ------------------------------
2191 -- In_Unconditional_Context --
2192 ------------------------------
2194 function In_Unconditional_Context (Node : Node_Id) return Boolean is
2199 while Present (P) loop
2201 when N_Subprogram_Body =>
2204 when N_If_Statement =>
2207 when N_Loop_Statement =>
2210 when N_Case_Statement =>
2219 end In_Unconditional_Context;
2225 procedure Insert_Action (Assoc_Node : Node_Id; Ins_Action : Node_Id) is
2227 if Present (Ins_Action) then
2228 Insert_Actions (Assoc_Node, New_List (Ins_Action));
2232 -- Version with check(s) suppressed
2234 procedure Insert_Action
2235 (Assoc_Node : Node_Id; Ins_Action : Node_Id; Suppress : Check_Id)
2238 Insert_Actions (Assoc_Node, New_List (Ins_Action), Suppress);
2241 --------------------
2242 -- Insert_Actions --
2243 --------------------
2245 procedure Insert_Actions (Assoc_Node : Node_Id; Ins_Actions : List_Id) is
2249 Wrapped_Node : Node_Id := Empty;
2252 if No (Ins_Actions) or else Is_Empty_List (Ins_Actions) then
2256 -- Ignore insert of actions from inside default expression (or other
2257 -- similar "spec expression") in the special spec-expression analyze
2258 -- mode. Any insertions at this point have no relevance, since we are
2259 -- only doing the analyze to freeze the types of any static expressions.
2260 -- See section "Handling of Default Expressions" in the spec of package
2261 -- Sem for further details.
2263 if In_Spec_Expression then
2267 -- If the action derives from stuff inside a record, then the actions
2268 -- are attached to the current scope, to be inserted and analyzed on
2269 -- exit from the scope. The reason for this is that we may also
2270 -- be generating freeze actions at the same time, and they must
2271 -- eventually be elaborated in the correct order.
2273 if Is_Record_Type (Current_Scope)
2274 and then not Is_Frozen (Current_Scope)
2276 if No (Scope_Stack.Table
2277 (Scope_Stack.Last).Pending_Freeze_Actions)
2279 Scope_Stack.Table (Scope_Stack.Last).Pending_Freeze_Actions :=
2284 Scope_Stack.Table (Scope_Stack.Last).Pending_Freeze_Actions);
2290 -- We now intend to climb up the tree to find the right point to
2291 -- insert the actions. We start at Assoc_Node, unless this node is
2292 -- a subexpression in which case we start with its parent. We do this
2293 -- for two reasons. First it speeds things up. Second, if Assoc_Node
2294 -- is itself one of the special nodes like N_And_Then, then we assume
2295 -- that an initial request to insert actions for such a node does not
2296 -- expect the actions to get deposited in the node for later handling
2297 -- when the node is expanded, since clearly the node is being dealt
2298 -- with by the caller. Note that in the subexpression case, N is
2299 -- always the child we came from.
2301 -- N_Raise_xxx_Error is an annoying special case, it is a statement
2302 -- if it has type Standard_Void_Type, and a subexpression otherwise.
2303 -- otherwise. Procedure attribute references are also statements.
2305 if Nkind (Assoc_Node) in N_Subexpr
2306 and then (Nkind (Assoc_Node) in N_Raise_xxx_Error
2307 or else Etype (Assoc_Node) /= Standard_Void_Type)
2308 and then (Nkind (Assoc_Node) /= N_Attribute_Reference
2310 not Is_Procedure_Attribute_Name
2311 (Attribute_Name (Assoc_Node)))
2313 P := Assoc_Node; -- ??? does not agree with above!
2314 N := Parent (Assoc_Node);
2316 -- Non-subexpression case. Note that N is initially Empty in this
2317 -- case (N is only guaranteed Non-Empty in the subexpr case).
2324 -- Capture root of the transient scope
2326 if Scope_Is_Transient then
2327 Wrapped_Node := Node_To_Be_Wrapped;
2331 pragma Assert (Present (P));
2335 -- Case of right operand of AND THEN or OR ELSE. Put the actions
2336 -- in the Actions field of the right operand. They will be moved
2337 -- out further when the AND THEN or OR ELSE operator is expanded.
2338 -- Nothing special needs to be done for the left operand since
2339 -- in that case the actions are executed unconditionally.
2341 when N_Short_Circuit =>
2342 if N = Right_Opnd (P) then
2344 -- We are now going to either append the actions to the
2345 -- actions field of the short-circuit operation. We will
2346 -- also analyze the actions now.
2348 -- This analysis is really too early, the proper thing would
2349 -- be to just park them there now, and only analyze them if
2350 -- we find we really need them, and to it at the proper
2351 -- final insertion point. However attempting to this proved
2352 -- tricky, so for now we just kill current values before and
2353 -- after the analyze call to make sure we avoid peculiar
2354 -- optimizations from this out of order insertion.
2356 Kill_Current_Values;
2358 if Present (Actions (P)) then
2359 Insert_List_After_And_Analyze
2360 (Last (Actions (P)), Ins_Actions);
2362 Set_Actions (P, Ins_Actions);
2363 Analyze_List (Actions (P));
2366 Kill_Current_Values;
2371 -- Then or Else operand of conditional expression. Add actions to
2372 -- Then_Actions or Else_Actions field as appropriate. The actions
2373 -- will be moved further out when the conditional is expanded.
2375 when N_Conditional_Expression =>
2377 ThenX : constant Node_Id := Next (First (Expressions (P)));
2378 ElseX : constant Node_Id := Next (ThenX);
2381 -- Actions belong to the then expression, temporarily
2382 -- place them as Then_Actions of the conditional expr.
2383 -- They will be moved to the proper place later when
2384 -- the conditional expression is expanded.
2387 if Present (Then_Actions (P)) then
2388 Insert_List_After_And_Analyze
2389 (Last (Then_Actions (P)), Ins_Actions);
2391 Set_Then_Actions (P, Ins_Actions);
2392 Analyze_List (Then_Actions (P));
2397 -- Actions belong to the else expression, temporarily
2398 -- place them as Else_Actions of the conditional expr.
2399 -- They will be moved to the proper place later when
2400 -- the conditional expression is expanded.
2402 elsif N = ElseX then
2403 if Present (Else_Actions (P)) then
2404 Insert_List_After_And_Analyze
2405 (Last (Else_Actions (P)), Ins_Actions);
2407 Set_Else_Actions (P, Ins_Actions);
2408 Analyze_List (Else_Actions (P));
2413 -- Actions belong to the condition. In this case they are
2414 -- unconditionally executed, and so we can continue the
2415 -- search for the proper insert point.
2422 -- Case of appearing in the condition of a while expression or
2423 -- elsif. We insert the actions into the Condition_Actions field.
2424 -- They will be moved further out when the while loop or elsif
2427 when N_Iteration_Scheme |
2430 if N = Condition (P) then
2431 if Present (Condition_Actions (P)) then
2432 Insert_List_After_And_Analyze
2433 (Last (Condition_Actions (P)), Ins_Actions);
2435 Set_Condition_Actions (P, Ins_Actions);
2437 -- Set the parent of the insert actions explicitly.
2438 -- This is not a syntactic field, but we need the
2439 -- parent field set, in particular so that freeze
2440 -- can understand that it is dealing with condition
2441 -- actions, and properly insert the freezing actions.
2443 Set_Parent (Ins_Actions, P);
2444 Analyze_List (Condition_Actions (P));
2450 -- Statements, declarations, pragmas, representation clauses
2455 N_Procedure_Call_Statement |
2456 N_Statement_Other_Than_Procedure_Call |
2462 -- Representation_Clause
2465 N_Attribute_Definition_Clause |
2466 N_Enumeration_Representation_Clause |
2467 N_Record_Representation_Clause |
2471 N_Abstract_Subprogram_Declaration |
2473 N_Exception_Declaration |
2474 N_Exception_Renaming_Declaration |
2475 N_Formal_Abstract_Subprogram_Declaration |
2476 N_Formal_Concrete_Subprogram_Declaration |
2477 N_Formal_Object_Declaration |
2478 N_Formal_Type_Declaration |
2479 N_Full_Type_Declaration |
2480 N_Function_Instantiation |
2481 N_Generic_Function_Renaming_Declaration |
2482 N_Generic_Package_Declaration |
2483 N_Generic_Package_Renaming_Declaration |
2484 N_Generic_Procedure_Renaming_Declaration |
2485 N_Generic_Subprogram_Declaration |
2486 N_Implicit_Label_Declaration |
2487 N_Incomplete_Type_Declaration |
2488 N_Number_Declaration |
2489 N_Object_Declaration |
2490 N_Object_Renaming_Declaration |
2492 N_Package_Body_Stub |
2493 N_Package_Declaration |
2494 N_Package_Instantiation |
2495 N_Package_Renaming_Declaration |
2496 N_Private_Extension_Declaration |
2497 N_Private_Type_Declaration |
2498 N_Procedure_Instantiation |
2500 N_Protected_Body_Stub |
2501 N_Protected_Type_Declaration |
2502 N_Single_Task_Declaration |
2504 N_Subprogram_Body_Stub |
2505 N_Subprogram_Declaration |
2506 N_Subprogram_Renaming_Declaration |
2507 N_Subtype_Declaration |
2510 N_Task_Type_Declaration |
2512 -- Freeze entity behaves like a declaration or statement
2516 -- Do not insert here if the item is not a list member (this
2517 -- happens for example with a triggering statement, and the
2518 -- proper approach is to insert before the entire select).
2520 if not Is_List_Member (P) then
2523 -- Do not insert if parent of P is an N_Component_Association
2524 -- node (i.e. we are in the context of an N_Aggregate or
2525 -- N_Extension_Aggregate node. In this case we want to insert
2526 -- before the entire aggregate.
2528 elsif Nkind (Parent (P)) = N_Component_Association then
2531 -- Do not insert if the parent of P is either an N_Variant
2532 -- node or an N_Record_Definition node, meaning in either
2533 -- case that P is a member of a component list, and that
2534 -- therefore the actions should be inserted outside the
2535 -- complete record declaration.
2537 elsif Nkind (Parent (P)) = N_Variant
2538 or else Nkind (Parent (P)) = N_Record_Definition
2542 -- Do not insert freeze nodes within the loop generated for
2543 -- an aggregate, because they may be elaborated too late for
2544 -- subsequent use in the back end: within a package spec the
2545 -- loop is part of the elaboration procedure and is only
2546 -- elaborated during the second pass.
2547 -- If the loop comes from source, or the entity is local to
2548 -- the loop itself it must remain within.
2550 elsif Nkind (Parent (P)) = N_Loop_Statement
2551 and then not Comes_From_Source (Parent (P))
2552 and then Nkind (First (Ins_Actions)) = N_Freeze_Entity
2554 Scope (Entity (First (Ins_Actions))) /= Current_Scope
2558 -- Otherwise we can go ahead and do the insertion
2560 elsif P = Wrapped_Node then
2561 Store_Before_Actions_In_Scope (Ins_Actions);
2565 Insert_List_Before_And_Analyze (P, Ins_Actions);
2569 -- A special case, N_Raise_xxx_Error can act either as a
2570 -- statement or a subexpression. We tell the difference
2571 -- by looking at the Etype. It is set to Standard_Void_Type
2572 -- in the statement case.
2575 N_Raise_xxx_Error =>
2576 if Etype (P) = Standard_Void_Type then
2577 if P = Wrapped_Node then
2578 Store_Before_Actions_In_Scope (Ins_Actions);
2580 Insert_List_Before_And_Analyze (P, Ins_Actions);
2585 -- In the subexpression case, keep climbing
2591 -- If a component association appears within a loop created for
2592 -- an array aggregate, attach the actions to the association so
2593 -- they can be subsequently inserted within the loop. For other
2594 -- component associations insert outside of the aggregate. For
2595 -- an association that will generate a loop, its Loop_Actions
2596 -- attribute is already initialized (see exp_aggr.adb).
2598 -- The list of loop_actions can in turn generate additional ones,
2599 -- that are inserted before the associated node. If the associated
2600 -- node is outside the aggregate, the new actions are collected
2601 -- at the end of the loop actions, to respect the order in which
2602 -- they are to be elaborated.
2605 N_Component_Association =>
2606 if Nkind (Parent (P)) = N_Aggregate
2607 and then Present (Loop_Actions (P))
2609 if Is_Empty_List (Loop_Actions (P)) then
2610 Set_Loop_Actions (P, Ins_Actions);
2611 Analyze_List (Ins_Actions);
2618 -- Check whether these actions were generated
2619 -- by a declaration that is part of the loop_
2620 -- actions for the component_association.
2623 while Present (Decl) loop
2624 exit when Parent (Decl) = P
2625 and then Is_List_Member (Decl)
2627 List_Containing (Decl) = Loop_Actions (P);
2628 Decl := Parent (Decl);
2631 if Present (Decl) then
2632 Insert_List_Before_And_Analyze
2633 (Decl, Ins_Actions);
2635 Insert_List_After_And_Analyze
2636 (Last (Loop_Actions (P)), Ins_Actions);
2647 -- Another special case, an attribute denoting a procedure call
2650 N_Attribute_Reference =>
2651 if Is_Procedure_Attribute_Name (Attribute_Name (P)) then
2652 if P = Wrapped_Node then
2653 Store_Before_Actions_In_Scope (Ins_Actions);
2655 Insert_List_Before_And_Analyze (P, Ins_Actions);
2660 -- In the subexpression case, keep climbing
2666 -- For all other node types, keep climbing tree
2670 N_Accept_Alternative |
2671 N_Access_Definition |
2672 N_Access_Function_Definition |
2673 N_Access_Procedure_Definition |
2674 N_Access_To_Object_Definition |
2677 N_Case_Statement_Alternative |
2678 N_Character_Literal |
2679 N_Compilation_Unit |
2680 N_Compilation_Unit_Aux |
2681 N_Component_Clause |
2682 N_Component_Declaration |
2683 N_Component_Definition |
2685 N_Constrained_Array_Definition |
2686 N_Decimal_Fixed_Point_Definition |
2687 N_Defining_Character_Literal |
2688 N_Defining_Identifier |
2689 N_Defining_Operator_Symbol |
2690 N_Defining_Program_Unit_Name |
2691 N_Delay_Alternative |
2692 N_Delta_Constraint |
2693 N_Derived_Type_Definition |
2695 N_Digits_Constraint |
2696 N_Discriminant_Association |
2697 N_Discriminant_Specification |
2699 N_Entry_Body_Formal_Part |
2700 N_Entry_Call_Alternative |
2701 N_Entry_Declaration |
2702 N_Entry_Index_Specification |
2703 N_Enumeration_Type_Definition |
2705 N_Exception_Handler |
2707 N_Explicit_Dereference |
2708 N_Extension_Aggregate |
2709 N_Floating_Point_Definition |
2710 N_Formal_Decimal_Fixed_Point_Definition |
2711 N_Formal_Derived_Type_Definition |
2712 N_Formal_Discrete_Type_Definition |
2713 N_Formal_Floating_Point_Definition |
2714 N_Formal_Modular_Type_Definition |
2715 N_Formal_Ordinary_Fixed_Point_Definition |
2716 N_Formal_Package_Declaration |
2717 N_Formal_Private_Type_Definition |
2718 N_Formal_Signed_Integer_Type_Definition |
2720 N_Function_Specification |
2721 N_Generic_Association |
2722 N_Handled_Sequence_Of_Statements |
2725 N_Index_Or_Discriminant_Constraint |
2726 N_Indexed_Component |
2730 N_Loop_Parameter_Specification |
2732 N_Modular_Type_Definition |
2758 N_Op_Shift_Right_Arithmetic |
2762 N_Ordinary_Fixed_Point_Definition |
2764 N_Package_Specification |
2765 N_Parameter_Association |
2766 N_Parameter_Specification |
2767 N_Pop_Constraint_Error_Label |
2768 N_Pop_Program_Error_Label |
2769 N_Pop_Storage_Error_Label |
2770 N_Pragma_Argument_Association |
2771 N_Procedure_Specification |
2772 N_Protected_Definition |
2773 N_Push_Constraint_Error_Label |
2774 N_Push_Program_Error_Label |
2775 N_Push_Storage_Error_Label |
2776 N_Qualified_Expression |
2778 N_Range_Constraint |
2780 N_Real_Range_Specification |
2781 N_Record_Definition |
2783 N_SCIL_Dispatch_Table_Object_Init |
2784 N_SCIL_Dispatch_Table_Tag_Init |
2785 N_SCIL_Dispatching_Call |
2786 N_SCIL_Membership_Test |
2788 N_Selected_Component |
2789 N_Signed_Integer_Type_Definition |
2790 N_Single_Protected_Declaration |
2794 N_Subtype_Indication |
2797 N_Terminate_Alternative |
2798 N_Triggering_Alternative |
2800 N_Unchecked_Expression |
2801 N_Unchecked_Type_Conversion |
2802 N_Unconstrained_Array_Definition |
2805 N_Use_Package_Clause |
2809 N_Validate_Unchecked_Conversion |
2816 -- Make sure that inserted actions stay in the transient scope
2818 if P = Wrapped_Node then
2819 Store_Before_Actions_In_Scope (Ins_Actions);
2823 -- If we fall through above tests, keep climbing tree
2827 if Nkind (Parent (N)) = N_Subunit then
2829 -- This is the proper body corresponding to a stub. Insertion
2830 -- must be done at the point of the stub, which is in the decla-
2831 -- rative part of the parent unit.
2833 P := Corresponding_Stub (Parent (N));
2841 -- Version with check(s) suppressed
2843 procedure Insert_Actions
2844 (Assoc_Node : Node_Id;
2845 Ins_Actions : List_Id;
2846 Suppress : Check_Id)
2849 if Suppress = All_Checks then
2851 Svg : constant Suppress_Array := Scope_Suppress;
2853 Scope_Suppress := (others => True);
2854 Insert_Actions (Assoc_Node, Ins_Actions);
2855 Scope_Suppress := Svg;
2860 Svg : constant Boolean := Scope_Suppress (Suppress);
2862 Scope_Suppress (Suppress) := True;
2863 Insert_Actions (Assoc_Node, Ins_Actions);
2864 Scope_Suppress (Suppress) := Svg;
2869 --------------------------
2870 -- Insert_Actions_After --
2871 --------------------------
2873 procedure Insert_Actions_After
2874 (Assoc_Node : Node_Id;
2875 Ins_Actions : List_Id)
2878 if Scope_Is_Transient
2879 and then Assoc_Node = Node_To_Be_Wrapped
2881 Store_After_Actions_In_Scope (Ins_Actions);
2883 Insert_List_After_And_Analyze (Assoc_Node, Ins_Actions);
2885 end Insert_Actions_After;
2887 ---------------------------------
2888 -- Insert_Library_Level_Action --
2889 ---------------------------------
2891 procedure Insert_Library_Level_Action (N : Node_Id) is
2892 Aux : constant Node_Id := Aux_Decls_Node (Cunit (Main_Unit));
2895 Push_Scope (Cunit_Entity (Main_Unit));
2896 -- ??? should this be Current_Sem_Unit instead of Main_Unit?
2898 if No (Actions (Aux)) then
2899 Set_Actions (Aux, New_List (N));
2901 Append (N, Actions (Aux));
2906 end Insert_Library_Level_Action;
2908 ----------------------------------
2909 -- Insert_Library_Level_Actions --
2910 ----------------------------------
2912 procedure Insert_Library_Level_Actions (L : List_Id) is
2913 Aux : constant Node_Id := Aux_Decls_Node (Cunit (Main_Unit));
2916 if Is_Non_Empty_List (L) then
2917 Push_Scope (Cunit_Entity (Main_Unit));
2918 -- ??? should this be Current_Sem_Unit instead of Main_Unit?
2920 if No (Actions (Aux)) then
2921 Set_Actions (Aux, L);
2924 Insert_List_After_And_Analyze (Last (Actions (Aux)), L);
2929 end Insert_Library_Level_Actions;
2931 ----------------------
2932 -- Inside_Init_Proc --
2933 ----------------------
2935 function Inside_Init_Proc return Boolean is
2941 and then S /= Standard_Standard
2943 if Is_Init_Proc (S) then
2951 end Inside_Init_Proc;
2953 ----------------------------
2954 -- Is_All_Null_Statements --
2955 ----------------------------
2957 function Is_All_Null_Statements (L : List_Id) return Boolean is
2962 while Present (Stm) loop
2963 if Nkind (Stm) /= N_Null_Statement then
2971 end Is_All_Null_Statements;
2973 ---------------------------------
2974 -- Is_Fully_Repped_Tagged_Type --
2975 ---------------------------------
2977 function Is_Fully_Repped_Tagged_Type (T : Entity_Id) return Boolean is
2978 U : constant Entity_Id := Underlying_Type (T);
2982 if No (U) or else not Is_Tagged_Type (U) then
2984 elsif Has_Discriminants (U) then
2986 elsif not Has_Specified_Layout (U) then
2990 -- Here we have a tagged type, see if it has any unlayed out fields
2991 -- other than a possible tag and parent fields. If so, we return False.
2993 Comp := First_Component (U);
2994 while Present (Comp) loop
2995 if not Is_Tag (Comp)
2996 and then Chars (Comp) /= Name_uParent
2997 and then No (Component_Clause (Comp))
3001 Next_Component (Comp);
3005 -- All components are layed out
3008 end Is_Fully_Repped_Tagged_Type;
3010 ----------------------------------
3011 -- Is_Library_Level_Tagged_Type --
3012 ----------------------------------
3014 function Is_Library_Level_Tagged_Type (Typ : Entity_Id) return Boolean is
3016 return Is_Tagged_Type (Typ)
3017 and then Is_Library_Level_Entity (Typ);
3018 end Is_Library_Level_Tagged_Type;
3020 ----------------------------------
3021 -- Is_Possibly_Unaligned_Object --
3022 ----------------------------------
3024 function Is_Possibly_Unaligned_Object (N : Node_Id) return Boolean is
3025 T : constant Entity_Id := Etype (N);
3028 -- If renamed object, apply test to underlying object
3030 if Is_Entity_Name (N)
3031 and then Is_Object (Entity (N))
3032 and then Present (Renamed_Object (Entity (N)))
3034 return Is_Possibly_Unaligned_Object (Renamed_Object (Entity (N)));
3037 -- Tagged and controlled types and aliased types are always aligned,
3038 -- as are concurrent types.
3041 or else Has_Controlled_Component (T)
3042 or else Is_Concurrent_Type (T)
3043 or else Is_Tagged_Type (T)
3044 or else Is_Controlled (T)
3049 -- If this is an element of a packed array, may be unaligned
3051 if Is_Ref_To_Bit_Packed_Array (N) then
3055 -- Case of component reference
3057 if Nkind (N) = N_Selected_Component then
3059 P : constant Node_Id := Prefix (N);
3060 C : constant Entity_Id := Entity (Selector_Name (N));
3065 -- If component reference is for an array with non-static bounds,
3066 -- then it is always aligned: we can only process unaligned
3067 -- arrays with static bounds (more accurately bounds known at
3070 if Is_Array_Type (T)
3071 and then not Compile_Time_Known_Bounds (T)
3076 -- If component is aliased, it is definitely properly aligned
3078 if Is_Aliased (C) then
3082 -- If component is for a type implemented as a scalar, and the
3083 -- record is packed, and the component is other than the first
3084 -- component of the record, then the component may be unaligned.
3086 if Is_Packed (Etype (P))
3087 and then Represented_As_Scalar (Etype (C))
3088 and then First_Entity (Scope (C)) /= C
3093 -- Compute maximum possible alignment for T
3095 -- If alignment is known, then that settles things
3097 if Known_Alignment (T) then
3098 M := UI_To_Int (Alignment (T));
3100 -- If alignment is not known, tentatively set max alignment
3103 M := Ttypes.Maximum_Alignment;
3105 -- We can reduce this if the Esize is known since the default
3106 -- alignment will never be more than the smallest power of 2
3107 -- that does not exceed this Esize value.
3109 if Known_Esize (T) then
3110 S := UI_To_Int (Esize (T));
3112 while (M / 2) >= S loop
3118 -- If the component reference is for a record that has a specified
3119 -- alignment, and we either know it is too small, or cannot tell,
3120 -- then the component may be unaligned
3122 if Known_Alignment (Etype (P))
3123 and then Alignment (Etype (P)) < Ttypes.Maximum_Alignment
3124 and then M > Alignment (Etype (P))
3129 -- Case of component clause present which may specify an
3130 -- unaligned position.
3132 if Present (Component_Clause (C)) then
3134 -- Otherwise we can do a test to make sure that the actual
3135 -- start position in the record, and the length, are both
3136 -- consistent with the required alignment. If not, we know
3137 -- that we are unaligned.
3140 Align_In_Bits : constant Nat := M * System_Storage_Unit;
3142 if Component_Bit_Offset (C) mod Align_In_Bits /= 0
3143 or else Esize (C) mod Align_In_Bits /= 0
3150 -- Otherwise, for a component reference, test prefix
3152 return Is_Possibly_Unaligned_Object (P);
3155 -- If not a component reference, must be aligned
3160 end Is_Possibly_Unaligned_Object;
3162 ---------------------------------
3163 -- Is_Possibly_Unaligned_Slice --
3164 ---------------------------------
3166 function Is_Possibly_Unaligned_Slice (N : Node_Id) return Boolean is
3168 -- Go to renamed object
3170 if Is_Entity_Name (N)
3171 and then Is_Object (Entity (N))
3172 and then Present (Renamed_Object (Entity (N)))
3174 return Is_Possibly_Unaligned_Slice (Renamed_Object (Entity (N)));
3177 -- The reference must be a slice
3179 if Nkind (N) /= N_Slice then
3183 -- Always assume the worst for a nested record component with a
3184 -- component clause, which gigi/gcc does not appear to handle well.
3185 -- It is not clear why this special test is needed at all ???
3187 if Nkind (Prefix (N)) = N_Selected_Component
3188 and then Nkind (Prefix (Prefix (N))) = N_Selected_Component
3190 Present (Component_Clause (Entity (Selector_Name (Prefix (N)))))
3195 -- We only need to worry if the target has strict alignment
3197 if not Target_Strict_Alignment then
3201 -- If it is a slice, then look at the array type being sliced
3204 Sarr : constant Node_Id := Prefix (N);
3205 -- Prefix of the slice, i.e. the array being sliced
3207 Styp : constant Entity_Id := Etype (Prefix (N));
3208 -- Type of the array being sliced
3214 -- The problems arise if the array object that is being sliced
3215 -- is a component of a record or array, and we cannot guarantee
3216 -- the alignment of the array within its containing object.
3218 -- To investigate this, we look at successive prefixes to see
3219 -- if we have a worrisome indexed or selected component.
3223 -- Case of array is part of an indexed component reference
3225 if Nkind (Pref) = N_Indexed_Component then
3226 Ptyp := Etype (Prefix (Pref));
3228 -- The only problematic case is when the array is packed,
3229 -- in which case we really know nothing about the alignment
3230 -- of individual components.
3232 if Is_Bit_Packed_Array (Ptyp) then
3236 -- Case of array is part of a selected component reference
3238 elsif Nkind (Pref) = N_Selected_Component then
3239 Ptyp := Etype (Prefix (Pref));
3241 -- We are definitely in trouble if the record in question
3242 -- has an alignment, and either we know this alignment is
3243 -- inconsistent with the alignment of the slice, or we
3244 -- don't know what the alignment of the slice should be.
3246 if Known_Alignment (Ptyp)
3247 and then (Unknown_Alignment (Styp)
3248 or else Alignment (Styp) > Alignment (Ptyp))
3253 -- We are in potential trouble if the record type is packed.
3254 -- We could special case when we know that the array is the
3255 -- first component, but that's not such a simple case ???
3257 if Is_Packed (Ptyp) then
3261 -- We are in trouble if there is a component clause, and
3262 -- either we do not know the alignment of the slice, or
3263 -- the alignment of the slice is inconsistent with the
3264 -- bit position specified by the component clause.
3267 Field : constant Entity_Id := Entity (Selector_Name (Pref));
3269 if Present (Component_Clause (Field))
3271 (Unknown_Alignment (Styp)
3273 (Component_Bit_Offset (Field) mod
3274 (System_Storage_Unit * Alignment (Styp))) /= 0)
3280 -- For cases other than selected or indexed components we
3281 -- know we are OK, since no issues arise over alignment.
3287 -- We processed an indexed component or selected component
3288 -- reference that looked safe, so keep checking prefixes.
3290 Pref := Prefix (Pref);
3293 end Is_Possibly_Unaligned_Slice;
3295 --------------------------------
3296 -- Is_Ref_To_Bit_Packed_Array --
3297 --------------------------------
3299 function Is_Ref_To_Bit_Packed_Array (N : Node_Id) return Boolean is
3304 if Is_Entity_Name (N)
3305 and then Is_Object (Entity (N))
3306 and then Present (Renamed_Object (Entity (N)))
3308 return Is_Ref_To_Bit_Packed_Array (Renamed_Object (Entity (N)));
3311 if Nkind (N) = N_Indexed_Component
3313 Nkind (N) = N_Selected_Component
3315 if Is_Bit_Packed_Array (Etype (Prefix (N))) then
3318 Result := Is_Ref_To_Bit_Packed_Array (Prefix (N));
3321 if Result and then Nkind (N) = N_Indexed_Component then
3322 Expr := First (Expressions (N));
3323 while Present (Expr) loop
3324 Force_Evaluation (Expr);
3334 end Is_Ref_To_Bit_Packed_Array;
3336 --------------------------------
3337 -- Is_Ref_To_Bit_Packed_Slice --
3338 --------------------------------
3340 function Is_Ref_To_Bit_Packed_Slice (N : Node_Id) return Boolean is
3342 if Nkind (N) = N_Type_Conversion then
3343 return Is_Ref_To_Bit_Packed_Slice (Expression (N));
3345 elsif Is_Entity_Name (N)
3346 and then Is_Object (Entity (N))
3347 and then Present (Renamed_Object (Entity (N)))
3349 return Is_Ref_To_Bit_Packed_Slice (Renamed_Object (Entity (N)));
3351 elsif Nkind (N) = N_Slice
3352 and then Is_Bit_Packed_Array (Etype (Prefix (N)))
3356 elsif Nkind (N) = N_Indexed_Component
3358 Nkind (N) = N_Selected_Component
3360 return Is_Ref_To_Bit_Packed_Slice (Prefix (N));
3365 end Is_Ref_To_Bit_Packed_Slice;
3367 -----------------------
3368 -- Is_Renamed_Object --
3369 -----------------------
3371 function Is_Renamed_Object (N : Node_Id) return Boolean is
3372 Pnod : constant Node_Id := Parent (N);
3373 Kind : constant Node_Kind := Nkind (Pnod);
3375 if Kind = N_Object_Renaming_Declaration then
3377 elsif Nkind_In (Kind, N_Indexed_Component, N_Selected_Component) then
3378 return Is_Renamed_Object (Pnod);
3382 end Is_Renamed_Object;
3384 ----------------------------
3385 -- Is_Untagged_Derivation --
3386 ----------------------------
3388 function Is_Untagged_Derivation (T : Entity_Id) return Boolean is
3390 return (not Is_Tagged_Type (T) and then Is_Derived_Type (T))
3392 (Is_Private_Type (T) and then Present (Full_View (T))
3393 and then not Is_Tagged_Type (Full_View (T))
3394 and then Is_Derived_Type (Full_View (T))
3395 and then Etype (Full_View (T)) /= T);
3396 end Is_Untagged_Derivation;
3398 ---------------------------
3399 -- Is_Volatile_Reference --
3400 ---------------------------
3402 function Is_Volatile_Reference (N : Node_Id) return Boolean is
3404 if Nkind (N) in N_Has_Etype
3405 and then Present (Etype (N))
3406 and then Treat_As_Volatile (Etype (N))
3410 elsif Is_Entity_Name (N) then
3411 return Treat_As_Volatile (Entity (N));
3413 elsif Nkind (N) = N_Slice then
3414 return Is_Volatile_Reference (Prefix (N));
3416 elsif Nkind_In (N, N_Indexed_Component, N_Selected_Component) then
3417 if (Is_Entity_Name (Prefix (N))
3418 and then Has_Volatile_Components (Entity (Prefix (N))))
3419 or else (Present (Etype (Prefix (N)))
3420 and then Has_Volatile_Components (Etype (Prefix (N))))
3424 return Is_Volatile_Reference (Prefix (N));
3430 end Is_Volatile_Reference;
3432 --------------------
3433 -- Kill_Dead_Code --
3434 --------------------
3436 procedure Kill_Dead_Code (N : Node_Id; Warn : Boolean := False) is
3437 W : Boolean := Warn;
3438 -- Set False if warnings suppressed
3442 Remove_Warning_Messages (N);
3444 -- Generate warning if appropriate
3448 -- We suppress the warning if this code is under control of an
3449 -- if statement, whose condition is a simple identifier, and
3450 -- either we are in an instance, or warnings off is set for this
3451 -- identifier. The reason for killing it in the instance case is
3452 -- that it is common and reasonable for code to be deleted in
3453 -- instances for various reasons.
3455 if Nkind (Parent (N)) = N_If_Statement then
3457 C : constant Node_Id := Condition (Parent (N));
3459 if Nkind (C) = N_Identifier
3462 or else (Present (Entity (C))
3463 and then Has_Warnings_Off (Entity (C))))
3470 -- Generate warning if not suppressed
3474 ("?this code can never be executed and has been deleted!", N);
3478 -- Recurse into block statements and bodies to process declarations
3481 if Nkind (N) = N_Block_Statement
3482 or else Nkind (N) = N_Subprogram_Body
3483 or else Nkind (N) = N_Package_Body
3485 Kill_Dead_Code (Declarations (N), False);
3486 Kill_Dead_Code (Statements (Handled_Statement_Sequence (N)));
3488 if Nkind (N) = N_Subprogram_Body then
3489 Set_Is_Eliminated (Defining_Entity (N));
3492 elsif Nkind (N) = N_Package_Declaration then
3493 Kill_Dead_Code (Visible_Declarations (Specification (N)));
3494 Kill_Dead_Code (Private_Declarations (Specification (N)));
3496 -- ??? After this point, Delete_Tree has been called on all
3497 -- declarations in Specification (N), so references to
3498 -- entities therein look suspicious.
3501 E : Entity_Id := First_Entity (Defining_Entity (N));
3503 while Present (E) loop
3504 if Ekind (E) = E_Operator then
3505 Set_Is_Eliminated (E);
3512 -- Recurse into composite statement to kill individual statements,
3513 -- in particular instantiations.
3515 elsif Nkind (N) = N_If_Statement then
3516 Kill_Dead_Code (Then_Statements (N));
3517 Kill_Dead_Code (Elsif_Parts (N));
3518 Kill_Dead_Code (Else_Statements (N));
3520 elsif Nkind (N) = N_Loop_Statement then
3521 Kill_Dead_Code (Statements (N));
3523 elsif Nkind (N) = N_Case_Statement then
3527 Alt := First (Alternatives (N));
3528 while Present (Alt) loop
3529 Kill_Dead_Code (Statements (Alt));
3534 elsif Nkind (N) = N_Case_Statement_Alternative then
3535 Kill_Dead_Code (Statements (N));
3537 -- Deal with dead instances caused by deleting instantiations
3539 elsif Nkind (N) in N_Generic_Instantiation then
3540 Remove_Dead_Instance (N);
3545 -- Case where argument is a list of nodes to be killed
3547 procedure Kill_Dead_Code (L : List_Id; Warn : Boolean := False) is
3552 if Is_Non_Empty_List (L) then
3554 while Present (N) loop
3555 Kill_Dead_Code (N, W);
3562 ------------------------
3563 -- Known_Non_Negative --
3564 ------------------------
3566 function Known_Non_Negative (Opnd : Node_Id) return Boolean is
3568 if Is_OK_Static_Expression (Opnd)
3569 and then Expr_Value (Opnd) >= 0
3575 Lo : constant Node_Id := Type_Low_Bound (Etype (Opnd));
3579 Is_OK_Static_Expression (Lo) and then Expr_Value (Lo) >= 0;
3582 end Known_Non_Negative;
3584 --------------------
3585 -- Known_Non_Null --
3586 --------------------
3588 function Known_Non_Null (N : Node_Id) return Boolean is
3590 -- Checks for case where N is an entity reference
3592 if Is_Entity_Name (N) and then Present (Entity (N)) then
3594 E : constant Entity_Id := Entity (N);
3599 -- First check if we are in decisive conditional
3601 Get_Current_Value_Condition (N, Op, Val);
3603 if Known_Null (Val) then
3604 if Op = N_Op_Eq then
3606 elsif Op = N_Op_Ne then
3611 -- If OK to do replacement, test Is_Known_Non_Null flag
3613 if OK_To_Do_Constant_Replacement (E) then
3614 return Is_Known_Non_Null (E);
3616 -- Otherwise if not safe to do replacement, then say so
3623 -- True if access attribute
3625 elsif Nkind (N) = N_Attribute_Reference
3626 and then (Attribute_Name (N) = Name_Access
3628 Attribute_Name (N) = Name_Unchecked_Access
3630 Attribute_Name (N) = Name_Unrestricted_Access)
3634 -- True if allocator
3636 elsif Nkind (N) = N_Allocator then
3639 -- For a conversion, true if expression is known non-null
3641 elsif Nkind (N) = N_Type_Conversion then
3642 return Known_Non_Null (Expression (N));
3644 -- Above are all cases where the value could be determined to be
3645 -- non-null. In all other cases, we don't know, so return False.
3656 function Known_Null (N : Node_Id) return Boolean is
3658 -- Checks for case where N is an entity reference
3660 if Is_Entity_Name (N) and then Present (Entity (N)) then
3662 E : constant Entity_Id := Entity (N);
3667 -- Constant null value is for sure null
3669 if Ekind (E) = E_Constant
3670 and then Known_Null (Constant_Value (E))
3675 -- First check if we are in decisive conditional
3677 Get_Current_Value_Condition (N, Op, Val);
3679 if Known_Null (Val) then
3680 if Op = N_Op_Eq then
3682 elsif Op = N_Op_Ne then
3687 -- If OK to do replacement, test Is_Known_Null flag
3689 if OK_To_Do_Constant_Replacement (E) then
3690 return Is_Known_Null (E);
3692 -- Otherwise if not safe to do replacement, then say so
3699 -- True if explicit reference to null
3701 elsif Nkind (N) = N_Null then
3704 -- For a conversion, true if expression is known null
3706 elsif Nkind (N) = N_Type_Conversion then
3707 return Known_Null (Expression (N));
3709 -- Above are all cases where the value could be determined to be null.
3710 -- In all other cases, we don't know, so return False.
3717 -----------------------------
3718 -- Make_CW_Equivalent_Type --
3719 -----------------------------
3721 -- Create a record type used as an equivalent of any member of the class
3722 -- which takes its size from exp.
3724 -- Generate the following code:
3726 -- type Equiv_T is record
3727 -- _parent : T (List of discriminant constraints taken from Exp);
3728 -- Ext__50 : Storage_Array (1 .. (Exp'size - Typ'object_size)/8);
3731 -- ??? Note that this type does not guarantee same alignment as all
3734 function Make_CW_Equivalent_Type
3736 E : Node_Id) return Entity_Id
3738 Loc : constant Source_Ptr := Sloc (E);
3739 Root_Typ : constant Entity_Id := Root_Type (T);
3740 List_Def : constant List_Id := Empty_List;
3741 Comp_List : constant List_Id := New_List;
3742 Equiv_Type : Entity_Id;
3743 Range_Type : Entity_Id;
3744 Str_Type : Entity_Id;
3745 Constr_Root : Entity_Id;
3749 if not Has_Discriminants (Root_Typ) then
3750 Constr_Root := Root_Typ;
3752 Constr_Root := Make_Temporary (Loc, 'R');
3754 -- subtype cstr__n is T (List of discr constraints taken from Exp)
3756 Append_To (List_Def,
3757 Make_Subtype_Declaration (Loc,
3758 Defining_Identifier => Constr_Root,
3759 Subtype_Indication => Make_Subtype_From_Expr (E, Root_Typ)));
3762 -- Generate the range subtype declaration
3764 Range_Type := Make_Temporary (Loc, 'G');
3766 if not Is_Interface (Root_Typ) then
3768 -- subtype rg__xx is
3769 -- Storage_Offset range 1 .. (Expr'size - typ'size) / Storage_Unit
3772 Make_Op_Subtract (Loc,
3774 Make_Attribute_Reference (Loc,
3776 OK_Convert_To (T, Duplicate_Subexpr_No_Checks (E)),
3777 Attribute_Name => Name_Size),
3779 Make_Attribute_Reference (Loc,
3780 Prefix => New_Reference_To (Constr_Root, Loc),
3781 Attribute_Name => Name_Object_Size));
3783 -- subtype rg__xx is
3784 -- Storage_Offset range 1 .. Expr'size / Storage_Unit
3787 Make_Attribute_Reference (Loc,
3789 OK_Convert_To (T, Duplicate_Subexpr_No_Checks (E)),
3790 Attribute_Name => Name_Size);
3793 Set_Paren_Count (Sizexpr, 1);
3795 Append_To (List_Def,
3796 Make_Subtype_Declaration (Loc,
3797 Defining_Identifier => Range_Type,
3798 Subtype_Indication =>
3799 Make_Subtype_Indication (Loc,
3800 Subtype_Mark => New_Reference_To (RTE (RE_Storage_Offset), Loc),
3801 Constraint => Make_Range_Constraint (Loc,
3804 Low_Bound => Make_Integer_Literal (Loc, 1),
3806 Make_Op_Divide (Loc,
3807 Left_Opnd => Sizexpr,
3808 Right_Opnd => Make_Integer_Literal (Loc,
3809 Intval => System_Storage_Unit)))))));
3811 -- subtype str__nn is Storage_Array (rg__x);
3813 Str_Type := Make_Temporary (Loc, 'S');
3814 Append_To (List_Def,
3815 Make_Subtype_Declaration (Loc,
3816 Defining_Identifier => Str_Type,
3817 Subtype_Indication =>
3818 Make_Subtype_Indication (Loc,
3819 Subtype_Mark => New_Reference_To (RTE (RE_Storage_Array), Loc),
3821 Make_Index_Or_Discriminant_Constraint (Loc,
3823 New_List (New_Reference_To (Range_Type, Loc))))));
3825 -- type Equiv_T is record
3826 -- [ _parent : Tnn; ]
3830 Equiv_Type := Make_Temporary (Loc, 'T');
3831 Set_Ekind (Equiv_Type, E_Record_Type);
3832 Set_Parent_Subtype (Equiv_Type, Constr_Root);
3834 -- Set Is_Class_Wide_Equivalent_Type very early to trigger the special
3835 -- treatment for this type. In particular, even though _parent's type
3836 -- is a controlled type or contains controlled components, we do not
3837 -- want to set Has_Controlled_Component on it to avoid making it gain
3838 -- an unwanted _controller component.
3840 Set_Is_Class_Wide_Equivalent_Type (Equiv_Type);
3842 if not Is_Interface (Root_Typ) then
3843 Append_To (Comp_List,
3844 Make_Component_Declaration (Loc,
3845 Defining_Identifier =>
3846 Make_Defining_Identifier (Loc, Name_uParent),
3847 Component_Definition =>
3848 Make_Component_Definition (Loc,
3849 Aliased_Present => False,
3850 Subtype_Indication => New_Reference_To (Constr_Root, Loc))));
3853 Append_To (Comp_List,
3854 Make_Component_Declaration (Loc,
3855 Defining_Identifier => Make_Temporary (Loc, 'C'),
3856 Component_Definition =>
3857 Make_Component_Definition (Loc,
3858 Aliased_Present => False,
3859 Subtype_Indication => New_Reference_To (Str_Type, Loc))));
3861 Append_To (List_Def,
3862 Make_Full_Type_Declaration (Loc,
3863 Defining_Identifier => Equiv_Type,
3865 Make_Record_Definition (Loc,
3867 Make_Component_List (Loc,
3868 Component_Items => Comp_List,
3869 Variant_Part => Empty))));
3871 -- Suppress all checks during the analysis of the expanded code
3872 -- to avoid the generation of spurious warnings under ZFP run-time.
3874 Insert_Actions (E, List_Def, Suppress => All_Checks);
3876 end Make_CW_Equivalent_Type;
3878 ------------------------
3879 -- Make_Literal_Range --
3880 ------------------------
3882 function Make_Literal_Range
3884 Literal_Typ : Entity_Id) return Node_Id
3886 Lo : constant Node_Id :=
3887 New_Copy_Tree (String_Literal_Low_Bound (Literal_Typ));
3888 Index : constant Entity_Id := Etype (Lo);
3891 Length_Expr : constant Node_Id :=
3892 Make_Op_Subtract (Loc,
3894 Make_Integer_Literal (Loc,
3895 Intval => String_Literal_Length (Literal_Typ)),
3897 Make_Integer_Literal (Loc, 1));
3900 Set_Analyzed (Lo, False);
3902 if Is_Integer_Type (Index) then
3905 Left_Opnd => New_Copy_Tree (Lo),
3906 Right_Opnd => Length_Expr);
3909 Make_Attribute_Reference (Loc,
3910 Attribute_Name => Name_Val,
3911 Prefix => New_Occurrence_Of (Index, Loc),
3912 Expressions => New_List (
3915 Make_Attribute_Reference (Loc,
3916 Attribute_Name => Name_Pos,
3917 Prefix => New_Occurrence_Of (Index, Loc),
3918 Expressions => New_List (New_Copy_Tree (Lo))),
3919 Right_Opnd => Length_Expr)));
3926 end Make_Literal_Range;
3928 --------------------------
3929 -- Make_Non_Empty_Check --
3930 --------------------------
3932 function Make_Non_Empty_Check
3934 N : Node_Id) return Node_Id
3940 Make_Attribute_Reference (Loc,
3941 Attribute_Name => Name_Length,
3942 Prefix => Duplicate_Subexpr_No_Checks (N, Name_Req => True)),
3944 Make_Integer_Literal (Loc, 0));
3945 end Make_Non_Empty_Check;
3947 ----------------------------
3948 -- Make_Subtype_From_Expr --
3949 ----------------------------
3951 -- 1. If Expr is an unconstrained array expression, creates
3952 -- Unc_Type(Expr'first(1)..Expr'last(1),..., Expr'first(n)..Expr'last(n))
3954 -- 2. If Expr is a unconstrained discriminated type expression, creates
3955 -- Unc_Type(Expr.Discr1, ... , Expr.Discr_n)
3957 -- 3. If Expr is class-wide, creates an implicit class wide subtype
3959 function Make_Subtype_From_Expr
3961 Unc_Typ : Entity_Id) return Node_Id
3963 Loc : constant Source_Ptr := Sloc (E);
3964 List_Constr : constant List_Id := New_List;
3967 Full_Subtyp : Entity_Id;
3968 Priv_Subtyp : Entity_Id;
3973 if Is_Private_Type (Unc_Typ)
3974 and then Has_Unknown_Discriminants (Unc_Typ)
3976 -- Prepare the subtype completion, Go to base type to
3977 -- find underlying type, because the type may be a generic
3978 -- actual or an explicit subtype.
3980 Utyp := Underlying_Type (Base_Type (Unc_Typ));
3981 Full_Subtyp := Make_Temporary (Loc, 'C');
3983 Unchecked_Convert_To (Utyp, Duplicate_Subexpr_No_Checks (E));
3984 Set_Parent (Full_Exp, Parent (E));
3986 Priv_Subtyp := Make_Temporary (Loc, 'P');
3989 Make_Subtype_Declaration (Loc,
3990 Defining_Identifier => Full_Subtyp,
3991 Subtype_Indication => Make_Subtype_From_Expr (Full_Exp, Utyp)));
3993 -- Define the dummy private subtype
3995 Set_Ekind (Priv_Subtyp, Subtype_Kind (Ekind (Unc_Typ)));
3996 Set_Etype (Priv_Subtyp, Base_Type (Unc_Typ));
3997 Set_Scope (Priv_Subtyp, Full_Subtyp);
3998 Set_Is_Constrained (Priv_Subtyp);
3999 Set_Is_Tagged_Type (Priv_Subtyp, Is_Tagged_Type (Unc_Typ));
4000 Set_Is_Itype (Priv_Subtyp);
4001 Set_Associated_Node_For_Itype (Priv_Subtyp, E);
4003 if Is_Tagged_Type (Priv_Subtyp) then
4005 (Base_Type (Priv_Subtyp), Class_Wide_Type (Unc_Typ));
4006 Set_Primitive_Operations (Priv_Subtyp,
4007 Primitive_Operations (Unc_Typ));
4010 Set_Full_View (Priv_Subtyp, Full_Subtyp);
4012 return New_Reference_To (Priv_Subtyp, Loc);
4014 elsif Is_Array_Type (Unc_Typ) then
4015 for J in 1 .. Number_Dimensions (Unc_Typ) loop
4016 Append_To (List_Constr,
4019 Make_Attribute_Reference (Loc,
4020 Prefix => Duplicate_Subexpr_No_Checks (E),
4021 Attribute_Name => Name_First,
4022 Expressions => New_List (
4023 Make_Integer_Literal (Loc, J))),
4026 Make_Attribute_Reference (Loc,
4027 Prefix => Duplicate_Subexpr_No_Checks (E),
4028 Attribute_Name => Name_Last,
4029 Expressions => New_List (
4030 Make_Integer_Literal (Loc, J)))));
4033 elsif Is_Class_Wide_Type (Unc_Typ) then
4035 CW_Subtype : Entity_Id;
4036 EQ_Typ : Entity_Id := Empty;
4039 -- A class-wide equivalent type is not needed when VM_Target
4040 -- because the VM back-ends handle the class-wide object
4041 -- initialization itself (and doesn't need or want the
4042 -- additional intermediate type to handle the assignment).
4044 if Expander_Active and then Tagged_Type_Expansion then
4045 EQ_Typ := Make_CW_Equivalent_Type (Unc_Typ, E);
4048 CW_Subtype := New_Class_Wide_Subtype (Unc_Typ, E);
4049 Set_Equivalent_Type (CW_Subtype, EQ_Typ);
4050 Set_Cloned_Subtype (CW_Subtype, Base_Type (Unc_Typ));
4052 return New_Occurrence_Of (CW_Subtype, Loc);
4055 -- Indefinite record type with discriminants
4058 D := First_Discriminant (Unc_Typ);
4059 while Present (D) loop
4060 Append_To (List_Constr,
4061 Make_Selected_Component (Loc,
4062 Prefix => Duplicate_Subexpr_No_Checks (E),
4063 Selector_Name => New_Reference_To (D, Loc)));
4065 Next_Discriminant (D);
4070 Make_Subtype_Indication (Loc,
4071 Subtype_Mark => New_Reference_To (Unc_Typ, Loc),
4073 Make_Index_Or_Discriminant_Constraint (Loc,
4074 Constraints => List_Constr));
4075 end Make_Subtype_From_Expr;
4077 -----------------------------
4078 -- May_Generate_Large_Temp --
4079 -----------------------------
4081 -- At the current time, the only types that we return False for (i.e.
4082 -- where we decide we know they cannot generate large temps) are ones
4083 -- where we know the size is 256 bits or less at compile time, and we
4084 -- are still not doing a thorough job on arrays and records ???
4086 function May_Generate_Large_Temp (Typ : Entity_Id) return Boolean is
4088 if not Size_Known_At_Compile_Time (Typ) then
4091 elsif Esize (Typ) /= 0 and then Esize (Typ) <= 256 then
4094 elsif Is_Array_Type (Typ)
4095 and then Present (Packed_Array_Type (Typ))
4097 return May_Generate_Large_Temp (Packed_Array_Type (Typ));
4099 -- We could do more here to find other small types ???
4104 end May_Generate_Large_Temp;
4106 ----------------------------
4107 -- New_Class_Wide_Subtype --
4108 ----------------------------
4110 function New_Class_Wide_Subtype
4111 (CW_Typ : Entity_Id;
4112 N : Node_Id) return Entity_Id
4114 Res : constant Entity_Id := Create_Itype (E_Void, N);
4115 Res_Name : constant Name_Id := Chars (Res);
4116 Res_Scope : constant Entity_Id := Scope (Res);
4119 Copy_Node (CW_Typ, Res);
4120 Set_Comes_From_Source (Res, False);
4121 Set_Sloc (Res, Sloc (N));
4123 Set_Associated_Node_For_Itype (Res, N);
4124 Set_Is_Public (Res, False); -- By default, may be changed below.
4125 Set_Public_Status (Res);
4126 Set_Chars (Res, Res_Name);
4127 Set_Scope (Res, Res_Scope);
4128 Set_Ekind (Res, E_Class_Wide_Subtype);
4129 Set_Next_Entity (Res, Empty);
4130 Set_Etype (Res, Base_Type (CW_Typ));
4131 Set_Is_Frozen (Res, False);
4132 Set_Freeze_Node (Res, Empty);
4134 end New_Class_Wide_Subtype;
4136 --------------------------------
4137 -- Non_Limited_Designated_Type --
4138 ---------------------------------
4140 function Non_Limited_Designated_Type (T : Entity_Id) return Entity_Id is
4141 Desig : constant Entity_Id := Designated_Type (T);
4143 if Ekind (Desig) = E_Incomplete_Type
4144 and then Present (Non_Limited_View (Desig))
4146 return Non_Limited_View (Desig);
4150 end Non_Limited_Designated_Type;
4152 -----------------------------------
4153 -- OK_To_Do_Constant_Replacement --
4154 -----------------------------------
4156 function OK_To_Do_Constant_Replacement (E : Entity_Id) return Boolean is
4157 ES : constant Entity_Id := Scope (E);
4161 -- Do not replace statically allocated objects, because they may be
4162 -- modified outside the current scope.
4164 if Is_Statically_Allocated (E) then
4167 -- Do not replace aliased or volatile objects, since we don't know what
4168 -- else might change the value.
4170 elsif Is_Aliased (E) or else Treat_As_Volatile (E) then
4173 -- Debug flag -gnatdM disconnects this optimization
4175 elsif Debug_Flag_MM then
4178 -- Otherwise check scopes
4181 CS := Current_Scope;
4184 -- If we are in right scope, replacement is safe
4189 -- Packages do not affect the determination of safety
4191 elsif Ekind (CS) = E_Package then
4192 exit when CS = Standard_Standard;
4195 -- Blocks do not affect the determination of safety
4197 elsif Ekind (CS) = E_Block then
4200 -- Loops do not affect the determination of safety. Note that we
4201 -- kill all current values on entry to a loop, so we are just
4202 -- talking about processing within a loop here.
4204 elsif Ekind (CS) = E_Loop then
4207 -- Otherwise, the reference is dubious, and we cannot be sure that
4208 -- it is safe to do the replacement.
4217 end OK_To_Do_Constant_Replacement;
4219 ------------------------------------
4220 -- Possible_Bit_Aligned_Component --
4221 ------------------------------------
4223 function Possible_Bit_Aligned_Component (N : Node_Id) return Boolean is
4227 -- Case of indexed component
4229 when N_Indexed_Component =>
4231 P : constant Node_Id := Prefix (N);
4232 Ptyp : constant Entity_Id := Etype (P);
4235 -- If we know the component size and it is less than 64, then
4236 -- we are definitely OK. The back end always does assignment of
4237 -- misaligned small objects correctly.
4239 if Known_Static_Component_Size (Ptyp)
4240 and then Component_Size (Ptyp) <= 64
4244 -- Otherwise, we need to test the prefix, to see if we are
4245 -- indexing from a possibly unaligned component.
4248 return Possible_Bit_Aligned_Component (P);
4252 -- Case of selected component
4254 when N_Selected_Component =>
4256 P : constant Node_Id := Prefix (N);
4257 Comp : constant Entity_Id := Entity (Selector_Name (N));
4260 -- If there is no component clause, then we are in the clear
4261 -- since the back end will never misalign a large component
4262 -- unless it is forced to do so. In the clear means we need
4263 -- only the recursive test on the prefix.
4265 if Component_May_Be_Bit_Aligned (Comp) then
4268 return Possible_Bit_Aligned_Component (P);
4272 -- For a slice, test the prefix, if that is possibly misaligned,
4273 -- then for sure the slice is!
4276 return Possible_Bit_Aligned_Component (Prefix (N));
4278 -- If we have none of the above, it means that we have fallen off the
4279 -- top testing prefixes recursively, and we now have a stand alone
4280 -- object, where we don't have a problem.
4286 end Possible_Bit_Aligned_Component;
4288 -------------------------
4289 -- Remove_Side_Effects --
4290 -------------------------
4292 procedure Remove_Side_Effects
4294 Name_Req : Boolean := False;
4295 Variable_Ref : Boolean := False)
4297 Loc : constant Source_Ptr := Sloc (Exp);
4298 Exp_Type : constant Entity_Id := Etype (Exp);
4299 Svg_Suppress : constant Suppress_Array := Scope_Suppress;
4301 Ref_Type : Entity_Id;
4303 Ptr_Typ_Decl : Node_Id;
4307 function Side_Effect_Free (N : Node_Id) return Boolean;
4308 -- Determines if the tree N represents an expression that is known not
4309 -- to have side effects, and for which no processing is required.
4311 function Side_Effect_Free (L : List_Id) return Boolean;
4312 -- Determines if all elements of the list L are side effect free
4314 function Safe_Prefixed_Reference (N : Node_Id) return Boolean;
4315 -- The argument N is a construct where the Prefix is dereferenced if it
4316 -- is an access type and the result is a variable. The call returns True
4317 -- if the construct is side effect free (not considering side effects in
4318 -- other than the prefix which are to be tested by the caller).
4320 function Within_In_Parameter (N : Node_Id) return Boolean;
4321 -- Determines if N is a subcomponent of a composite in-parameter. If so,
4322 -- N is not side-effect free when the actual is global and modifiable
4323 -- indirectly from within a subprogram, because it may be passed by
4324 -- reference. The front-end must be conservative here and assume that
4325 -- this may happen with any array or record type. On the other hand, we
4326 -- cannot create temporaries for all expressions for which this
4327 -- condition is true, for various reasons that might require clearing up
4328 -- ??? For example, discriminant references that appear out of place, or
4329 -- spurious type errors with class-wide expressions. As a result, we
4330 -- limit the transformation to loop bounds, which is so far the only
4331 -- case that requires it.
4333 -----------------------------
4334 -- Safe_Prefixed_Reference --
4335 -----------------------------
4337 function Safe_Prefixed_Reference (N : Node_Id) return Boolean is
4339 -- If prefix is not side effect free, definitely not safe
4341 if not Side_Effect_Free (Prefix (N)) then
4344 -- If the prefix is of an access type that is not access-to-constant,
4345 -- then this construct is a variable reference, which means it is to
4346 -- be considered to have side effects if Variable_Ref is set True
4347 -- Exception is an access to an entity that is a constant or an
4348 -- in-parameter which does not come from source, and is the result
4349 -- of a previous removal of side-effects.
4351 elsif Is_Access_Type (Etype (Prefix (N)))
4352 and then not Is_Access_Constant (Etype (Prefix (N)))
4353 and then Variable_Ref
4355 if not Is_Entity_Name (Prefix (N)) then
4358 return Ekind (Entity (Prefix (N))) = E_Constant
4359 or else Ekind (Entity (Prefix (N))) = E_In_Parameter;
4362 -- The following test is the simplest way of solving a complex
4363 -- problem uncovered by BB08-010: Side effect on loop bound that
4364 -- is a subcomponent of a global variable:
4365 -- If a loop bound is a subcomponent of a global variable, a
4366 -- modification of that variable within the loop may incorrectly
4367 -- affect the execution of the loop.
4370 (Nkind (Parent (Parent (N))) /= N_Loop_Parameter_Specification
4371 or else not Within_In_Parameter (Prefix (N)))
4375 -- All other cases are side effect free
4380 end Safe_Prefixed_Reference;
4382 ----------------------
4383 -- Side_Effect_Free --
4384 ----------------------
4386 function Side_Effect_Free (N : Node_Id) return Boolean is
4388 -- Note on checks that could raise Constraint_Error. Strictly, if
4389 -- we take advantage of 11.6, these checks do not count as side
4390 -- effects. However, we would just as soon consider that they are
4391 -- side effects, since the backend CSE does not work very well on
4392 -- expressions which can raise Constraint_Error. On the other
4393 -- hand, if we do not consider them to be side effect free, then
4394 -- we get some awkward expansions in -gnato mode, resulting in
4395 -- code insertions at a point where we do not have a clear model
4396 -- for performing the insertions.
4398 -- Special handling for entity names
4400 if Is_Entity_Name (N) then
4402 -- If the entity is a constant, it is definitely side effect
4403 -- free. Note that the test of Is_Variable (N) below might
4404 -- be expected to catch this case, but it does not, because
4405 -- this test goes to the original tree, and we may have
4406 -- already rewritten a variable node with a constant as
4407 -- a result of an earlier Force_Evaluation call.
4409 if Ekind (Entity (N)) = E_Constant
4410 or else Ekind (Entity (N)) = E_In_Parameter
4414 -- Functions are not side effect free
4416 elsif Ekind (Entity (N)) = E_Function then
4419 -- Variables are considered to be a side effect if Variable_Ref
4420 -- is set or if we have a volatile reference and Name_Req is off.
4421 -- If Name_Req is True then we can't help returning a name which
4422 -- effectively allows multiple references in any case.
4424 elsif Is_Variable (N) then
4425 return not Variable_Ref
4426 and then (not Is_Volatile_Reference (N) or else Name_Req);
4428 -- Any other entity (e.g. a subtype name) is definitely side
4435 -- A value known at compile time is always side effect free
4437 elsif Compile_Time_Known_Value (N) then
4440 -- A variable renaming is not side-effect free, because the
4441 -- renaming will function like a macro in the front-end in
4442 -- some cases, and an assignment can modify the component
4443 -- designated by N, so we need to create a temporary for it.
4445 elsif Is_Entity_Name (Original_Node (N))
4446 and then Is_Renaming_Of_Object (Entity (Original_Node (N)))
4447 and then Ekind (Entity (Original_Node (N))) /= E_Constant
4452 -- For other than entity names and compile time known values,
4453 -- check the node kind for special processing.
4457 -- An attribute reference is side effect free if its expressions
4458 -- are side effect free and its prefix is side effect free or
4459 -- is an entity reference.
4461 -- Is this right? what about x'first where x is a variable???
4463 when N_Attribute_Reference =>
4464 return Side_Effect_Free (Expressions (N))
4465 and then Attribute_Name (N) /= Name_Input
4466 and then (Is_Entity_Name (Prefix (N))
4467 or else Side_Effect_Free (Prefix (N)));
4469 -- A binary operator is side effect free if and both operands
4470 -- are side effect free. For this purpose binary operators
4471 -- include membership tests and short circuit forms
4473 when N_Binary_Op | N_Membership_Test | N_Short_Circuit =>
4474 return Side_Effect_Free (Left_Opnd (N))
4476 Side_Effect_Free (Right_Opnd (N));
4478 -- An explicit dereference is side effect free only if it is
4479 -- a side effect free prefixed reference.
4481 when N_Explicit_Dereference =>
4482 return Safe_Prefixed_Reference (N);
4484 -- A call to _rep_to_pos is side effect free, since we generate
4485 -- this pure function call ourselves. Moreover it is critically
4486 -- important to make this exception, since otherwise we can
4487 -- have discriminants in array components which don't look
4488 -- side effect free in the case of an array whose index type
4489 -- is an enumeration type with an enumeration rep clause.
4491 -- All other function calls are not side effect free
4493 when N_Function_Call =>
4494 return Nkind (Name (N)) = N_Identifier
4495 and then Is_TSS (Name (N), TSS_Rep_To_Pos)
4497 Side_Effect_Free (First (Parameter_Associations (N)));
4499 -- An indexed component is side effect free if it is a side
4500 -- effect free prefixed reference and all the indexing
4501 -- expressions are side effect free.
4503 when N_Indexed_Component =>
4504 return Side_Effect_Free (Expressions (N))
4505 and then Safe_Prefixed_Reference (N);
4507 -- A type qualification is side effect free if the expression
4508 -- is side effect free.
4510 when N_Qualified_Expression =>
4511 return Side_Effect_Free (Expression (N));
4513 -- A selected component is side effect free only if it is a
4514 -- side effect free prefixed reference. If it designates a
4515 -- component with a rep. clause it must be treated has having
4516 -- a potential side effect, because it may be modified through
4517 -- a renaming, and a subsequent use of the renaming as a macro
4518 -- will yield the wrong value. This complex interaction between
4519 -- renaming and removing side effects is a reminder that the
4520 -- latter has become a headache to maintain, and that it should
4521 -- be removed in favor of the gcc mechanism to capture values ???
4523 when N_Selected_Component =>
4524 if Nkind (Parent (N)) = N_Explicit_Dereference
4525 and then Has_Non_Standard_Rep (Designated_Type (Etype (N)))
4529 return Safe_Prefixed_Reference (N);
4532 -- A range is side effect free if the bounds are side effect free
4535 return Side_Effect_Free (Low_Bound (N))
4536 and then Side_Effect_Free (High_Bound (N));
4538 -- A slice is side effect free if it is a side effect free
4539 -- prefixed reference and the bounds are side effect free.
4542 return Side_Effect_Free (Discrete_Range (N))
4543 and then Safe_Prefixed_Reference (N);
4545 -- A type conversion is side effect free if the expression to be
4546 -- converted is side effect free.
4548 when N_Type_Conversion =>
4549 return Side_Effect_Free (Expression (N));
4551 -- A unary operator is side effect free if the operand
4552 -- is side effect free.
4555 return Side_Effect_Free (Right_Opnd (N));
4557 -- An unchecked type conversion is side effect free only if it
4558 -- is safe and its argument is side effect free.
4560 when N_Unchecked_Type_Conversion =>
4561 return Safe_Unchecked_Type_Conversion (N)
4562 and then Side_Effect_Free (Expression (N));
4564 -- An unchecked expression is side effect free if its expression
4565 -- is side effect free.
4567 when N_Unchecked_Expression =>
4568 return Side_Effect_Free (Expression (N));
4570 -- A literal is side effect free
4572 when N_Character_Literal |
4578 -- We consider that anything else has side effects. This is a bit
4579 -- crude, but we are pretty close for most common cases, and we
4580 -- are certainly correct (i.e. we never return True when the
4581 -- answer should be False).
4586 end Side_Effect_Free;
4588 -- A list is side effect free if all elements of the list are
4589 -- side effect free.
4591 function Side_Effect_Free (L : List_Id) return Boolean is
4595 if L = No_List or else L = Error_List then
4600 while Present (N) loop
4601 if not Side_Effect_Free (N) then
4610 end Side_Effect_Free;
4612 -------------------------
4613 -- Within_In_Parameter --
4614 -------------------------
4616 function Within_In_Parameter (N : Node_Id) return Boolean is
4618 if not Comes_From_Source (N) then
4621 elsif Is_Entity_Name (N) then
4622 return Ekind (Entity (N)) = E_In_Parameter;
4624 elsif Nkind (N) = N_Indexed_Component
4625 or else Nkind (N) = N_Selected_Component
4627 return Within_In_Parameter (Prefix (N));
4632 end Within_In_Parameter;
4634 -- Start of processing for Remove_Side_Effects
4637 -- If we are side effect free already or expansion is disabled,
4638 -- there is nothing to do.
4640 if Side_Effect_Free (Exp) or else not Expander_Active then
4644 -- All this must not have any checks
4646 Scope_Suppress := (others => True);
4648 -- If it is a scalar type and we need to capture the value, just make
4649 -- a copy. Likewise for a function call, an attribute reference or an
4650 -- operator. And if we have a volatile reference and Name_Req is not
4651 -- set (see comments above for Side_Effect_Free).
4653 if Is_Elementary_Type (Exp_Type)
4654 and then (Variable_Ref
4655 or else Nkind (Exp) = N_Function_Call
4656 or else Nkind (Exp) = N_Attribute_Reference
4657 or else Nkind (Exp) in N_Op
4658 or else (not Name_Req and then Is_Volatile_Reference (Exp)))
4660 Def_Id := Make_Temporary (Loc, 'R', Exp);
4661 Set_Etype (Def_Id, Exp_Type);
4662 Res := New_Reference_To (Def_Id, Loc);
4665 Make_Object_Declaration (Loc,
4666 Defining_Identifier => Def_Id,
4667 Object_Definition => New_Reference_To (Exp_Type, Loc),
4668 Constant_Present => True,
4669 Expression => Relocate_Node (Exp));
4671 -- Check if the previous node relocation requires readjustment of
4672 -- some SCIL Dispatching node.
4675 and then Nkind (Exp) = N_Function_Call
4677 Adjust_SCIL_Node (Exp, Expression (E));
4680 Set_Assignment_OK (E);
4681 Insert_Action (Exp, E);
4683 -- If the expression has the form v.all then we can just capture
4684 -- the pointer, and then do an explicit dereference on the result.
4686 elsif Nkind (Exp) = N_Explicit_Dereference then
4687 Def_Id := Make_Temporary (Loc, 'R', Exp);
4689 Make_Explicit_Dereference (Loc, New_Reference_To (Def_Id, Loc));
4692 Make_Object_Declaration (Loc,
4693 Defining_Identifier => Def_Id,
4694 Object_Definition =>
4695 New_Reference_To (Etype (Prefix (Exp)), Loc),
4696 Constant_Present => True,
4697 Expression => Relocate_Node (Prefix (Exp))));
4699 -- Similar processing for an unchecked conversion of an expression
4700 -- of the form v.all, where we want the same kind of treatment.
4702 elsif Nkind (Exp) = N_Unchecked_Type_Conversion
4703 and then Nkind (Expression (Exp)) = N_Explicit_Dereference
4705 Remove_Side_Effects (Expression (Exp), Name_Req, Variable_Ref);
4706 Scope_Suppress := Svg_Suppress;
4709 -- If this is a type conversion, leave the type conversion and remove
4710 -- the side effects in the expression. This is important in several
4711 -- circumstances: for change of representations, and also when this is
4712 -- a view conversion to a smaller object, where gigi can end up creating
4713 -- its own temporary of the wrong size.
4715 elsif Nkind (Exp) = N_Type_Conversion then
4716 Remove_Side_Effects (Expression (Exp), Name_Req, Variable_Ref);
4717 Scope_Suppress := Svg_Suppress;
4720 -- If this is an unchecked conversion that Gigi can't handle, make
4721 -- a copy or a use a renaming to capture the value.
4723 elsif Nkind (Exp) = N_Unchecked_Type_Conversion
4724 and then not Safe_Unchecked_Type_Conversion (Exp)
4726 if CW_Or_Has_Controlled_Part (Exp_Type) then
4728 -- Use a renaming to capture the expression, rather than create
4729 -- a controlled temporary.
4731 Def_Id := Make_Temporary (Loc, 'R', Exp);
4732 Res := New_Reference_To (Def_Id, Loc);
4735 Make_Object_Renaming_Declaration (Loc,
4736 Defining_Identifier => Def_Id,
4737 Subtype_Mark => New_Reference_To (Exp_Type, Loc),
4738 Name => Relocate_Node (Exp)));
4741 Def_Id := Make_Temporary (Loc, 'R', Exp);
4742 Set_Etype (Def_Id, Exp_Type);
4743 Res := New_Reference_To (Def_Id, Loc);
4746 Make_Object_Declaration (Loc,
4747 Defining_Identifier => Def_Id,
4748 Object_Definition => New_Reference_To (Exp_Type, Loc),
4749 Constant_Present => not Is_Variable (Exp),
4750 Expression => Relocate_Node (Exp));
4752 Set_Assignment_OK (E);
4753 Insert_Action (Exp, E);
4756 -- For expressions that denote objects, we can use a renaming scheme.
4757 -- We skip using this if we have a volatile reference and we do not
4758 -- have Name_Req set true (see comments above for Side_Effect_Free).
4760 elsif Is_Object_Reference (Exp)
4761 and then Nkind (Exp) /= N_Function_Call
4762 and then (Name_Req or else not Is_Volatile_Reference (Exp))
4764 Def_Id := Make_Temporary (Loc, 'R', Exp);
4766 if Nkind (Exp) = N_Selected_Component
4767 and then Nkind (Prefix (Exp)) = N_Function_Call
4768 and then Is_Array_Type (Exp_Type)
4770 -- Avoid generating a variable-sized temporary, by generating
4771 -- the renaming declaration just for the function call. The
4772 -- transformation could be refined to apply only when the array
4773 -- component is constrained by a discriminant???
4776 Make_Selected_Component (Loc,
4777 Prefix => New_Occurrence_Of (Def_Id, Loc),
4778 Selector_Name => Selector_Name (Exp));
4781 Make_Object_Renaming_Declaration (Loc,
4782 Defining_Identifier => Def_Id,
4784 New_Reference_To (Base_Type (Etype (Prefix (Exp))), Loc),
4785 Name => Relocate_Node (Prefix (Exp))));
4788 Res := New_Reference_To (Def_Id, Loc);
4791 Make_Object_Renaming_Declaration (Loc,
4792 Defining_Identifier => Def_Id,
4793 Subtype_Mark => New_Reference_To (Exp_Type, Loc),
4794 Name => Relocate_Node (Exp)));
4797 -- If this is a packed reference, or a selected component with a
4798 -- non-standard representation, a reference to the temporary will
4799 -- be replaced by a copy of the original expression (see
4800 -- Exp_Ch2.Expand_Renaming). Otherwise the temporary must be
4801 -- elaborated by gigi, and is of course not to be replaced in-line
4802 -- by the expression it renames, which would defeat the purpose of
4803 -- removing the side-effect.
4805 if (Nkind (Exp) = N_Selected_Component
4806 or else Nkind (Exp) = N_Indexed_Component)
4807 and then Has_Non_Standard_Rep (Etype (Prefix (Exp)))
4811 Set_Is_Renaming_Of_Object (Def_Id, False);
4814 -- Otherwise we generate a reference to the value
4817 -- Special processing for function calls that return a limited type.
4818 -- We need to build a declaration that will enable build-in-place
4819 -- expansion of the call. This is not done if the context is already
4820 -- an object declaration, to prevent infinite recursion.
4822 -- This is relevant only in Ada 2005 mode. In Ada 95 programs we have
4823 -- to accommodate functions returning limited objects by reference.
4825 if Nkind (Exp) = N_Function_Call
4826 and then Is_Inherently_Limited_Type (Etype (Exp))
4827 and then Nkind (Parent (Exp)) /= N_Object_Declaration
4828 and then Ada_Version >= Ada_05
4831 Obj : constant Entity_Id := Make_Temporary (Loc, 'F', Exp);
4836 Make_Object_Declaration (Loc,
4837 Defining_Identifier => Obj,
4838 Object_Definition => New_Occurrence_Of (Exp_Type, Loc),
4839 Expression => Relocate_Node (Exp));
4841 -- Check if the previous node relocation requires readjustment
4842 -- of some SCIL Dispatching node.
4845 and then Nkind (Exp) = N_Function_Call
4847 Adjust_SCIL_Node (Exp, Expression (Decl));
4850 Insert_Action (Exp, Decl);
4851 Set_Etype (Obj, Exp_Type);
4852 Rewrite (Exp, New_Occurrence_Of (Obj, Loc));
4857 Ref_Type := Make_Temporary (Loc, 'A');
4860 Make_Full_Type_Declaration (Loc,
4861 Defining_Identifier => Ref_Type,
4863 Make_Access_To_Object_Definition (Loc,
4864 All_Present => True,
4865 Subtype_Indication =>
4866 New_Reference_To (Exp_Type, Loc)));
4869 Insert_Action (Exp, Ptr_Typ_Decl);
4871 Def_Id := Make_Temporary (Loc, 'R', Exp);
4872 Set_Etype (Def_Id, Exp_Type);
4875 Make_Explicit_Dereference (Loc,
4876 Prefix => New_Reference_To (Def_Id, Loc));
4878 if Nkind (E) = N_Explicit_Dereference then
4879 New_Exp := Relocate_Node (Prefix (E));
4881 E := Relocate_Node (E);
4882 New_Exp := Make_Reference (Loc, E);
4885 if Is_Delayed_Aggregate (E) then
4887 -- The expansion of nested aggregates is delayed until the
4888 -- enclosing aggregate is expanded. As aggregates are often
4889 -- qualified, the predicate applies to qualified expressions
4890 -- as well, indicating that the enclosing aggregate has not
4891 -- been expanded yet. At this point the aggregate is part of
4892 -- a stand-alone declaration, and must be fully expanded.
4894 if Nkind (E) = N_Qualified_Expression then
4895 Set_Expansion_Delayed (Expression (E), False);
4896 Set_Analyzed (Expression (E), False);
4898 Set_Expansion_Delayed (E, False);
4901 Set_Analyzed (E, False);
4905 Make_Object_Declaration (Loc,
4906 Defining_Identifier => Def_Id,
4907 Object_Definition => New_Reference_To (Ref_Type, Loc),
4908 Expression => New_Exp));
4910 -- Check if the previous node relocation requires readjustment
4911 -- of some SCIL Dispatching node.
4914 and then Nkind (Exp) = N_Function_Call
4916 Adjust_SCIL_Node (Exp, Prefix (New_Exp));
4920 -- Preserve the Assignment_OK flag in all copies, since at least
4921 -- one copy may be used in a context where this flag must be set
4922 -- (otherwise why would the flag be set in the first place).
4924 Set_Assignment_OK (Res, Assignment_OK (Exp));
4926 -- Finally rewrite the original expression and we are done
4929 Analyze_And_Resolve (Exp, Exp_Type);
4930 Scope_Suppress := Svg_Suppress;
4931 end Remove_Side_Effects;
4933 ---------------------------
4934 -- Represented_As_Scalar --
4935 ---------------------------
4937 function Represented_As_Scalar (T : Entity_Id) return Boolean is
4938 UT : constant Entity_Id := Underlying_Type (T);
4940 return Is_Scalar_Type (UT)
4941 or else (Is_Bit_Packed_Array (UT)
4942 and then Is_Scalar_Type (Packed_Array_Type (UT)));
4943 end Represented_As_Scalar;
4945 ------------------------------------
4946 -- Safe_Unchecked_Type_Conversion --
4947 ------------------------------------
4949 -- Note: this function knows quite a bit about the exact requirements
4950 -- of Gigi with respect to unchecked type conversions, and its code
4951 -- must be coordinated with any changes in Gigi in this area.
4953 -- The above requirements should be documented in Sinfo ???
4955 function Safe_Unchecked_Type_Conversion (Exp : Node_Id) return Boolean is
4960 Pexp : constant Node_Id := Parent (Exp);
4963 -- If the expression is the RHS of an assignment or object declaration
4964 -- we are always OK because there will always be a target.
4966 -- Object renaming declarations, (generated for view conversions of
4967 -- actuals in inlined calls), like object declarations, provide an
4968 -- explicit type, and are safe as well.
4970 if (Nkind (Pexp) = N_Assignment_Statement
4971 and then Expression (Pexp) = Exp)
4972 or else Nkind (Pexp) = N_Object_Declaration
4973 or else Nkind (Pexp) = N_Object_Renaming_Declaration
4977 -- If the expression is the prefix of an N_Selected_Component
4978 -- we should also be OK because GCC knows to look inside the
4979 -- conversion except if the type is discriminated. We assume
4980 -- that we are OK anyway if the type is not set yet or if it is
4981 -- controlled since we can't afford to introduce a temporary in
4984 elsif Nkind (Pexp) = N_Selected_Component
4985 and then Prefix (Pexp) = Exp
4987 if No (Etype (Pexp)) then
4991 not Has_Discriminants (Etype (Pexp))
4992 or else Is_Constrained (Etype (Pexp));
4996 -- Set the output type, this comes from Etype if it is set, otherwise
4997 -- we take it from the subtype mark, which we assume was already
5000 if Present (Etype (Exp)) then
5001 Otyp := Etype (Exp);
5003 Otyp := Entity (Subtype_Mark (Exp));
5006 -- The input type always comes from the expression, and we assume
5007 -- this is indeed always analyzed, so we can simply get the Etype.
5009 Ityp := Etype (Expression (Exp));
5011 -- Initialize alignments to unknown so far
5016 -- Replace a concurrent type by its corresponding record type
5017 -- and each type by its underlying type and do the tests on those.
5018 -- The original type may be a private type whose completion is a
5019 -- concurrent type, so find the underlying type first.
5021 if Present (Underlying_Type (Otyp)) then
5022 Otyp := Underlying_Type (Otyp);
5025 if Present (Underlying_Type (Ityp)) then
5026 Ityp := Underlying_Type (Ityp);
5029 if Is_Concurrent_Type (Otyp) then
5030 Otyp := Corresponding_Record_Type (Otyp);
5033 if Is_Concurrent_Type (Ityp) then
5034 Ityp := Corresponding_Record_Type (Ityp);
5037 -- If the base types are the same, we know there is no problem since
5038 -- this conversion will be a noop.
5040 if Implementation_Base_Type (Otyp) = Implementation_Base_Type (Ityp) then
5043 -- Same if this is an upwards conversion of an untagged type, and there
5044 -- are no constraints involved (could be more general???)
5046 elsif Etype (Ityp) = Otyp
5047 and then not Is_Tagged_Type (Ityp)
5048 and then not Has_Discriminants (Ityp)
5049 and then No (First_Rep_Item (Base_Type (Ityp)))
5053 -- If the expression has an access type (object or subprogram) we
5054 -- assume that the conversion is safe, because the size of the target
5055 -- is safe, even if it is a record (which might be treated as having
5056 -- unknown size at this point).
5058 elsif Is_Access_Type (Ityp) then
5061 -- If the size of output type is known at compile time, there is
5062 -- never a problem. Note that unconstrained records are considered
5063 -- to be of known size, but we can't consider them that way here,
5064 -- because we are talking about the actual size of the object.
5066 -- We also make sure that in addition to the size being known, we do
5067 -- not have a case which might generate an embarrassingly large temp
5068 -- in stack checking mode.
5070 elsif Size_Known_At_Compile_Time (Otyp)
5072 (not Stack_Checking_Enabled
5073 or else not May_Generate_Large_Temp (Otyp))
5074 and then not (Is_Record_Type (Otyp) and then not Is_Constrained (Otyp))
5078 -- If either type is tagged, then we know the alignment is OK so
5079 -- Gigi will be able to use pointer punning.
5081 elsif Is_Tagged_Type (Otyp) or else Is_Tagged_Type (Ityp) then
5084 -- If either type is a limited record type, we cannot do a copy, so
5085 -- say safe since there's nothing else we can do.
5087 elsif Is_Limited_Record (Otyp) or else Is_Limited_Record (Ityp) then
5090 -- Conversions to and from packed array types are always ignored and
5093 elsif Is_Packed_Array_Type (Otyp)
5094 or else Is_Packed_Array_Type (Ityp)
5099 -- The only other cases known to be safe is if the input type's
5100 -- alignment is known to be at least the maximum alignment for the
5101 -- target or if both alignments are known and the output type's
5102 -- alignment is no stricter than the input's. We can use the alignment
5103 -- of the component type of an array if a type is an unpacked
5106 if Present (Alignment_Clause (Otyp)) then
5107 Oalign := Expr_Value (Expression (Alignment_Clause (Otyp)));
5109 elsif Is_Array_Type (Otyp)
5110 and then Present (Alignment_Clause (Component_Type (Otyp)))
5112 Oalign := Expr_Value (Expression (Alignment_Clause
5113 (Component_Type (Otyp))));
5116 if Present (Alignment_Clause (Ityp)) then
5117 Ialign := Expr_Value (Expression (Alignment_Clause (Ityp)));
5119 elsif Is_Array_Type (Ityp)
5120 and then Present (Alignment_Clause (Component_Type (Ityp)))
5122 Ialign := Expr_Value (Expression (Alignment_Clause
5123 (Component_Type (Ityp))));
5126 if Ialign /= No_Uint and then Ialign > Maximum_Alignment then
5129 elsif Ialign /= No_Uint and then Oalign /= No_Uint
5130 and then Ialign <= Oalign
5134 -- Otherwise, Gigi cannot handle this and we must make a temporary
5139 end Safe_Unchecked_Type_Conversion;
5141 ---------------------------------
5142 -- Set_Current_Value_Condition --
5143 ---------------------------------
5145 -- Note: the implementation of this procedure is very closely tied to the
5146 -- implementation of Get_Current_Value_Condition. Here we set required
5147 -- Current_Value fields, and in Get_Current_Value_Condition, we interpret
5148 -- them, so they must have a consistent view.
5150 procedure Set_Current_Value_Condition (Cnode : Node_Id) is
5152 procedure Set_Entity_Current_Value (N : Node_Id);
5153 -- If N is an entity reference, where the entity is of an appropriate
5154 -- kind, then set the current value of this entity to Cnode, unless
5155 -- there is already a definite value set there.
5157 procedure Set_Expression_Current_Value (N : Node_Id);
5158 -- If N is of an appropriate form, sets an appropriate entry in current
5159 -- value fields of relevant entities. Multiple entities can be affected
5160 -- in the case of an AND or AND THEN.
5162 ------------------------------
5163 -- Set_Entity_Current_Value --
5164 ------------------------------
5166 procedure Set_Entity_Current_Value (N : Node_Id) is
5168 if Is_Entity_Name (N) then
5170 Ent : constant Entity_Id := Entity (N);
5173 -- Don't capture if not safe to do so
5175 if not Safe_To_Capture_Value (N, Ent, Cond => True) then
5179 -- Here we have a case where the Current_Value field may
5180 -- need to be set. We set it if it is not already set to a
5181 -- compile time expression value.
5183 -- Note that this represents a decision that one condition
5184 -- blots out another previous one. That's certainly right
5185 -- if they occur at the same level. If the second one is
5186 -- nested, then the decision is neither right nor wrong (it
5187 -- would be equally OK to leave the outer one in place, or
5188 -- take the new inner one. Really we should record both, but
5189 -- our data structures are not that elaborate.
5191 if Nkind (Current_Value (Ent)) not in N_Subexpr then
5192 Set_Current_Value (Ent, Cnode);
5196 end Set_Entity_Current_Value;
5198 ----------------------------------
5199 -- Set_Expression_Current_Value --
5200 ----------------------------------
5202 procedure Set_Expression_Current_Value (N : Node_Id) is
5208 -- Loop to deal with (ignore for now) any NOT operators present. The
5209 -- presence of NOT operators will be handled properly when we call
5210 -- Get_Current_Value_Condition.
5212 while Nkind (Cond) = N_Op_Not loop
5213 Cond := Right_Opnd (Cond);
5216 -- For an AND or AND THEN, recursively process operands
5218 if Nkind (Cond) = N_Op_And or else Nkind (Cond) = N_And_Then then
5219 Set_Expression_Current_Value (Left_Opnd (Cond));
5220 Set_Expression_Current_Value (Right_Opnd (Cond));
5224 -- Check possible relational operator
5226 if Nkind (Cond) in N_Op_Compare then
5227 if Compile_Time_Known_Value (Right_Opnd (Cond)) then
5228 Set_Entity_Current_Value (Left_Opnd (Cond));
5229 elsif Compile_Time_Known_Value (Left_Opnd (Cond)) then
5230 Set_Entity_Current_Value (Right_Opnd (Cond));
5233 -- Check possible boolean variable reference
5236 Set_Entity_Current_Value (Cond);
5238 end Set_Expression_Current_Value;
5240 -- Start of processing for Set_Current_Value_Condition
5243 Set_Expression_Current_Value (Condition (Cnode));
5244 end Set_Current_Value_Condition;
5246 --------------------------
5247 -- Set_Elaboration_Flag --
5248 --------------------------
5250 procedure Set_Elaboration_Flag (N : Node_Id; Spec_Id : Entity_Id) is
5251 Loc : constant Source_Ptr := Sloc (N);
5252 Ent : constant Entity_Id := Elaboration_Entity (Spec_Id);
5256 if Present (Ent) then
5258 -- Nothing to do if at the compilation unit level, because in this
5259 -- case the flag is set by the binder generated elaboration routine.
5261 if Nkind (Parent (N)) = N_Compilation_Unit then
5264 -- Here we do need to generate an assignment statement
5267 Check_Restriction (No_Elaboration_Code, N);
5269 Make_Assignment_Statement (Loc,
5270 Name => New_Occurrence_Of (Ent, Loc),
5271 Expression => New_Occurrence_Of (Standard_True, Loc));
5273 if Nkind (Parent (N)) = N_Subunit then
5274 Insert_After (Corresponding_Stub (Parent (N)), Asn);
5276 Insert_After (N, Asn);
5281 -- Kill current value indication. This is necessary because the
5282 -- tests of this flag are inserted out of sequence and must not
5283 -- pick up bogus indications of the wrong constant value.
5285 Set_Current_Value (Ent, Empty);
5288 end Set_Elaboration_Flag;
5290 ----------------------------
5291 -- Set_Renamed_Subprogram --
5292 ----------------------------
5294 procedure Set_Renamed_Subprogram (N : Node_Id; E : Entity_Id) is
5296 -- If input node is an identifier, we can just reset it
5298 if Nkind (N) = N_Identifier then
5299 Set_Chars (N, Chars (E));
5302 -- Otherwise we have to do a rewrite, preserving Comes_From_Source
5306 CS : constant Boolean := Comes_From_Source (N);
5308 Rewrite (N, Make_Identifier (Sloc (N), Chars => Chars (E)));
5310 Set_Comes_From_Source (N, CS);
5311 Set_Analyzed (N, True);
5314 end Set_Renamed_Subprogram;
5316 ----------------------------------
5317 -- Silly_Boolean_Array_Not_Test --
5318 ----------------------------------
5320 -- This procedure implements an odd and silly test. We explicitly check
5321 -- for the case where the 'First of the component type is equal to the
5322 -- 'Last of this component type, and if this is the case, we make sure
5323 -- that constraint error is raised. The reason is that the NOT is bound
5324 -- to cause CE in this case, and we will not otherwise catch it.
5326 -- No such check is required for AND and OR, since for both these cases
5327 -- False op False = False, and True op True = True. For the XOR case,
5328 -- see Silly_Boolean_Array_Xor_Test.
5330 -- Believe it or not, this was reported as a bug. Note that nearly
5331 -- always, the test will evaluate statically to False, so the code will
5332 -- be statically removed, and no extra overhead caused.
5334 procedure Silly_Boolean_Array_Not_Test (N : Node_Id; T : Entity_Id) is
5335 Loc : constant Source_Ptr := Sloc (N);
5336 CT : constant Entity_Id := Component_Type (T);
5339 -- The check we install is
5341 -- constraint_error when
5342 -- component_type'first = component_type'last
5343 -- and then array_type'Length /= 0)
5345 -- We need the last guard because we don't want to raise CE for empty
5346 -- arrays since no out of range values result. (Empty arrays with a
5347 -- component type of True .. True -- very useful -- even the ACATS
5348 -- does not test that marginal case!)
5351 Make_Raise_Constraint_Error (Loc,
5357 Make_Attribute_Reference (Loc,
5358 Prefix => New_Occurrence_Of (CT, Loc),
5359 Attribute_Name => Name_First),
5362 Make_Attribute_Reference (Loc,
5363 Prefix => New_Occurrence_Of (CT, Loc),
5364 Attribute_Name => Name_Last)),
5366 Right_Opnd => Make_Non_Empty_Check (Loc, Right_Opnd (N))),
5367 Reason => CE_Range_Check_Failed));
5368 end Silly_Boolean_Array_Not_Test;
5370 ----------------------------------
5371 -- Silly_Boolean_Array_Xor_Test --
5372 ----------------------------------
5374 -- This procedure implements an odd and silly test. We explicitly check
5375 -- for the XOR case where the component type is True .. True, since this
5376 -- will raise constraint error. A special check is required since CE
5377 -- will not be generated otherwise (cf Expand_Packed_Not).
5379 -- No such check is required for AND and OR, since for both these cases
5380 -- False op False = False, and True op True = True, and no check is
5381 -- required for the case of False .. False, since False xor False = False.
5382 -- See also Silly_Boolean_Array_Not_Test
5384 procedure Silly_Boolean_Array_Xor_Test (N : Node_Id; T : Entity_Id) is
5385 Loc : constant Source_Ptr := Sloc (N);
5386 CT : constant Entity_Id := Component_Type (T);
5389 -- The check we install is
5391 -- constraint_error when
5392 -- Boolean (component_type'First)
5393 -- and then Boolean (component_type'Last)
5394 -- and then array_type'Length /= 0)
5396 -- We need the last guard because we don't want to raise CE for empty
5397 -- arrays since no out of range values result (Empty arrays with a
5398 -- component type of True .. True -- very useful -- even the ACATS
5399 -- does not test that marginal case!).
5402 Make_Raise_Constraint_Error (Loc,
5408 Convert_To (Standard_Boolean,
5409 Make_Attribute_Reference (Loc,
5410 Prefix => New_Occurrence_Of (CT, Loc),
5411 Attribute_Name => Name_First)),
5414 Convert_To (Standard_Boolean,
5415 Make_Attribute_Reference (Loc,
5416 Prefix => New_Occurrence_Of (CT, Loc),
5417 Attribute_Name => Name_Last))),
5419 Right_Opnd => Make_Non_Empty_Check (Loc, Right_Opnd (N))),
5420 Reason => CE_Range_Check_Failed));
5421 end Silly_Boolean_Array_Xor_Test;
5423 --------------------------
5424 -- Target_Has_Fixed_Ops --
5425 --------------------------
5427 Integer_Sized_Small : Ureal;
5428 -- Set to 2.0 ** -(Integer'Size - 1) the first time that this
5429 -- function is called (we don't want to compute it more than once!)
5431 Long_Integer_Sized_Small : Ureal;
5432 -- Set to 2.0 ** -(Long_Integer'Size - 1) the first time that this
5433 -- function is called (we don't want to compute it more than once)
5435 First_Time_For_THFO : Boolean := True;
5436 -- Set to False after first call (if Fractional_Fixed_Ops_On_Target)
5438 function Target_Has_Fixed_Ops
5439 (Left_Typ : Entity_Id;
5440 Right_Typ : Entity_Id;
5441 Result_Typ : Entity_Id) return Boolean
5443 function Is_Fractional_Type (Typ : Entity_Id) return Boolean;
5444 -- Return True if the given type is a fixed-point type with a small
5445 -- value equal to 2 ** (-(T'Object_Size - 1)) and whose values have
5446 -- an absolute value less than 1.0. This is currently limited
5447 -- to fixed-point types that map to Integer or Long_Integer.
5449 ------------------------
5450 -- Is_Fractional_Type --
5451 ------------------------
5453 function Is_Fractional_Type (Typ : Entity_Id) return Boolean is
5455 if Esize (Typ) = Standard_Integer_Size then
5456 return Small_Value (Typ) = Integer_Sized_Small;
5458 elsif Esize (Typ) = Standard_Long_Integer_Size then
5459 return Small_Value (Typ) = Long_Integer_Sized_Small;
5464 end Is_Fractional_Type;
5466 -- Start of processing for Target_Has_Fixed_Ops
5469 -- Return False if Fractional_Fixed_Ops_On_Target is false
5471 if not Fractional_Fixed_Ops_On_Target then
5475 -- Here the target has Fractional_Fixed_Ops, if first time, compute
5476 -- standard constants used by Is_Fractional_Type.
5478 if First_Time_For_THFO then
5479 First_Time_For_THFO := False;
5481 Integer_Sized_Small :=
5484 Den => UI_From_Int (Standard_Integer_Size - 1),
5487 Long_Integer_Sized_Small :=
5490 Den => UI_From_Int (Standard_Long_Integer_Size - 1),
5494 -- Return True if target supports fixed-by-fixed multiply/divide
5495 -- for fractional fixed-point types (see Is_Fractional_Type) and
5496 -- the operand and result types are equivalent fractional types.
5498 return Is_Fractional_Type (Base_Type (Left_Typ))
5499 and then Is_Fractional_Type (Base_Type (Right_Typ))
5500 and then Is_Fractional_Type (Base_Type (Result_Typ))
5501 and then Esize (Left_Typ) = Esize (Right_Typ)
5502 and then Esize (Left_Typ) = Esize (Result_Typ);
5503 end Target_Has_Fixed_Ops;
5505 ------------------------------------------
5506 -- Type_May_Have_Bit_Aligned_Components --
5507 ------------------------------------------
5509 function Type_May_Have_Bit_Aligned_Components
5510 (Typ : Entity_Id) return Boolean
5513 -- Array type, check component type
5515 if Is_Array_Type (Typ) then
5517 Type_May_Have_Bit_Aligned_Components (Component_Type (Typ));
5519 -- Record type, check components
5521 elsif Is_Record_Type (Typ) then
5526 E := First_Component_Or_Discriminant (Typ);
5527 while Present (E) loop
5528 if Component_May_Be_Bit_Aligned (E)
5529 or else Type_May_Have_Bit_Aligned_Components (Etype (E))
5534 Next_Component_Or_Discriminant (E);
5540 -- Type other than array or record is always OK
5545 end Type_May_Have_Bit_Aligned_Components;
5547 ----------------------------
5548 -- Wrap_Cleanup_Procedure --
5549 ----------------------------
5551 procedure Wrap_Cleanup_Procedure (N : Node_Id) is
5552 Loc : constant Source_Ptr := Sloc (N);
5553 Stseq : constant Node_Id := Handled_Statement_Sequence (N);
5554 Stmts : constant List_Id := Statements (Stseq);
5557 if Abort_Allowed then
5558 Prepend_To (Stmts, Build_Runtime_Call (Loc, RE_Abort_Defer));
5559 Append_To (Stmts, Build_Runtime_Call (Loc, RE_Abort_Undefer));
5561 end Wrap_Cleanup_Procedure;