1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2011, 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 Expander; use Expander;
33 with Exp_Util; use Exp_Util;
34 with Exp_Ch3; use Exp_Ch3;
35 with Exp_Ch7; use Exp_Ch7;
36 with Exp_Ch9; use Exp_Ch9;
37 with Exp_Disp; use Exp_Disp;
38 with Exp_Tss; use Exp_Tss;
39 with Fname; use Fname;
40 with Freeze; use Freeze;
41 with Itypes; use Itypes;
43 with Namet; use Namet;
44 with Nmake; use Nmake;
45 with Nlists; use Nlists;
47 with Restrict; use Restrict;
48 with Rident; use Rident;
49 with Rtsfind; use Rtsfind;
50 with Ttypes; use Ttypes;
52 with Sem_Aggr; use Sem_Aggr;
53 with Sem_Aux; use Sem_Aux;
54 with Sem_Ch3; use Sem_Ch3;
55 with Sem_Eval; use Sem_Eval;
56 with Sem_Res; use Sem_Res;
57 with Sem_Util; use Sem_Util;
58 with Sinfo; use Sinfo;
59 with Snames; use Snames;
60 with Stand; use Stand;
61 with Targparm; use Targparm;
62 with Tbuild; use Tbuild;
63 with Uintp; use Uintp;
65 package body Exp_Aggr is
67 type Case_Bounds is record
70 Choice_Node : Node_Id;
73 type Case_Table_Type is array (Nat range <>) of Case_Bounds;
74 -- Table type used by Check_Case_Choices procedure
76 function Has_Default_Init_Comps (N : Node_Id) return Boolean;
77 -- N is an aggregate (record or array). Checks the presence of default
78 -- initialization (<>) in any component (Ada 2005: AI-287).
80 function Is_Static_Dispatch_Table_Aggregate (N : Node_Id) return Boolean;
81 -- Returns true if N is an aggregate used to initialize the components
82 -- of an statically allocated dispatch table.
85 (Obj_Type : Entity_Id;
86 Typ : Entity_Id) return Boolean;
87 -- A static array aggregate in an object declaration can in most cases be
88 -- expanded in place. The one exception is when the aggregate is given
89 -- with component associations that specify different bounds from those of
90 -- the type definition in the object declaration. In this pathological
91 -- case the aggregate must slide, and we must introduce an intermediate
92 -- temporary to hold it.
94 -- The same holds in an assignment to one-dimensional array of arrays,
95 -- when a component may be given with bounds that differ from those of the
98 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type);
99 -- Sort the Case Table using the Lower Bound of each Choice as the key.
100 -- A simple insertion sort is used since the number of choices in a case
101 -- statement of variant part will usually be small and probably in near
104 ------------------------------------------------------
105 -- Local subprograms for Record Aggregate Expansion --
106 ------------------------------------------------------
108 function Build_Record_Aggr_Code
112 Is_Limited_Ancestor_Expansion : Boolean := False) return List_Id;
113 -- N is an N_Aggregate or an N_Extension_Aggregate. Typ is the type of the
114 -- aggregate. Target is an expression containing the location on which the
115 -- component by component assignments will take place. Returns the list of
116 -- assignments plus all other adjustments needed for tagged and controlled
117 -- types. Is_Limited_Ancestor_Expansion indicates that the function has
118 -- been called recursively to expand the limited ancestor to avoid copying
121 procedure Convert_To_Assignments (N : Node_Id; Typ : Entity_Id);
122 -- N is an N_Aggregate or an N_Extension_Aggregate. Typ is the type of the
123 -- aggregate (which can only be a record type, this procedure is only used
124 -- for record types). Transform the given aggregate into a sequence of
125 -- assignments performed component by component.
127 procedure Expand_Record_Aggregate
129 Orig_Tag : Node_Id := Empty;
130 Parent_Expr : Node_Id := Empty);
131 -- This is the top level procedure for record aggregate expansion.
132 -- Expansion for record aggregates needs expand aggregates for tagged
133 -- record types. Specifically Expand_Record_Aggregate adds the Tag
134 -- field in front of the Component_Association list that was created
135 -- during resolution by Resolve_Record_Aggregate.
137 -- N is the record aggregate node.
138 -- Orig_Tag is the value of the Tag that has to be provided for this
139 -- specific aggregate. It carries the tag corresponding to the type
140 -- of the outermost aggregate during the recursive expansion
141 -- Parent_Expr is the ancestor part of the original extension
144 function Has_Mutable_Components (Typ : Entity_Id) return Boolean;
145 -- Return true if one of the component is of a discriminated type with
146 -- defaults. An aggregate for a type with mutable components must be
147 -- expanded into individual assignments.
149 procedure Initialize_Discriminants (N : Node_Id; Typ : Entity_Id);
150 -- If the type of the aggregate is a type extension with renamed discrimi-
151 -- nants, we must initialize the hidden discriminants of the parent.
152 -- Otherwise, the target object must not be initialized. The discriminants
153 -- are initialized by calling the initialization procedure for the type.
154 -- This is incorrect if the initialization of other components has any
155 -- side effects. We restrict this call to the case where the parent type
156 -- has a variant part, because this is the only case where the hidden
157 -- discriminants are accessed, namely when calling discriminant checking
158 -- functions of the parent type, and when applying a stream attribute to
159 -- an object of the derived type.
161 -----------------------------------------------------
162 -- Local Subprograms for Array Aggregate Expansion --
163 -----------------------------------------------------
165 function Aggr_Size_OK (N : Node_Id; Typ : Entity_Id) return Boolean;
166 -- Very large static aggregates present problems to the back-end, and are
167 -- transformed into assignments and loops. This function verifies that the
168 -- total number of components of an aggregate is acceptable for rewriting
169 -- into a purely positional static form. Aggr_Size_OK must be called before
172 -- This function also detects and warns about one-component aggregates that
173 -- appear in a non-static context. Even if the component value is static,
174 -- such an aggregate must be expanded into an assignment.
176 function Backend_Processing_Possible (N : Node_Id) return Boolean;
177 -- This function checks if array aggregate N can be processed directly
178 -- by the backend. If this is the case True is returned.
180 function Build_Array_Aggr_Code
185 Scalar_Comp : Boolean;
186 Indexes : List_Id := No_List) return List_Id;
187 -- This recursive routine returns a list of statements containing the
188 -- loops and assignments that are needed for the expansion of the array
191 -- N is the (sub-)aggregate node to be expanded into code. This node has
192 -- been fully analyzed, and its Etype is properly set.
194 -- Index is the index node corresponding to the array sub-aggregate N
196 -- Into is the target expression into which we are copying the aggregate.
197 -- Note that this node may not have been analyzed yet, and so the Etype
198 -- field may not be set.
200 -- Scalar_Comp is True if the component type of the aggregate is scalar
202 -- Indexes is the current list of expressions used to index the object we
205 procedure Convert_Array_Aggr_In_Allocator
209 -- If the aggregate appears within an allocator and can be expanded in
210 -- place, this routine generates the individual assignments to components
211 -- of the designated object. This is an optimization over the general
212 -- case, where a temporary is first created on the stack and then used to
213 -- construct the allocated object on the heap.
215 procedure Convert_To_Positional
217 Max_Others_Replicate : Nat := 5;
218 Handle_Bit_Packed : Boolean := False);
219 -- If possible, convert named notation to positional notation. This
220 -- conversion is possible only in some static cases. If the conversion is
221 -- possible, then N is rewritten with the analyzed converted aggregate.
222 -- The parameter Max_Others_Replicate controls the maximum number of
223 -- values corresponding to an others choice that will be converted to
224 -- positional notation (the default of 5 is the normal limit, and reflects
225 -- the fact that normally the loop is better than a lot of separate
226 -- assignments). Note that this limit gets overridden in any case if
227 -- either of the restrictions No_Elaboration_Code or No_Implicit_Loops is
228 -- set. The parameter Handle_Bit_Packed is usually set False (since we do
229 -- not expect the back end to handle bit packed arrays, so the normal case
230 -- of conversion is pointless), but in the special case of a call from
231 -- Packed_Array_Aggregate_Handled, we set this parameter to True, since
232 -- these are cases we handle in there.
234 procedure Expand_Array_Aggregate (N : Node_Id);
235 -- This is the top-level routine to perform array aggregate expansion.
236 -- N is the N_Aggregate node to be expanded.
238 function Late_Expansion
241 Target : Node_Id) return List_Id;
242 -- This routine implements top-down expansion of nested aggregates. In
243 -- doing so, it avoids the generation of temporaries at each level. N is a
244 -- nested (record or array) aggregate that has been marked with 'Delay_
245 -- Expansion'. Typ is the expected type of the aggregate. Target is a
246 -- (duplicable) expression that will hold the result of the aggregate
249 function Make_OK_Assignment_Statement
252 Expression : Node_Id) return Node_Id;
253 -- This is like Make_Assignment_Statement, except that Assignment_OK
254 -- is set in the left operand. All assignments built by this unit
255 -- use this routine. This is needed to deal with assignments to
256 -- initialized constants that are done in place.
258 function Number_Of_Choices (N : Node_Id) return Nat;
259 -- Returns the number of discrete choices (not including the others choice
260 -- if present) contained in (sub-)aggregate N.
262 function Packed_Array_Aggregate_Handled (N : Node_Id) return Boolean;
263 -- Given an array aggregate, this function handles the case of a packed
264 -- array aggregate with all constant values, where the aggregate can be
265 -- evaluated at compile time. If this is possible, then N is rewritten
266 -- to be its proper compile time value with all the components properly
267 -- assembled. The expression is analyzed and resolved and True is
268 -- returned. If this transformation is not possible, N is unchanged
269 -- and False is returned
271 function Safe_Slice_Assignment (N : Node_Id) return Boolean;
272 -- If a slice assignment has an aggregate with a single others_choice,
273 -- the assignment can be done in place even if bounds are not static,
274 -- by converting it into a loop over the discrete range of the slice.
280 function Aggr_Size_OK (N : Node_Id; Typ : Entity_Id) return Boolean is
288 -- The following constant determines the maximum size of an array
289 -- aggregate produced by converting named to positional notation (e.g.
290 -- from others clauses). This avoids running away with attempts to
291 -- convert huge aggregates, which hit memory limits in the backend.
293 -- The normal limit is 5000, but we increase this limit to 2**24 (about
294 -- 16 million) if Restrictions (No_Elaboration_Code) or Restrictions
295 -- (No_Implicit_Loops) is specified, since in either case, we are at
296 -- risk of declaring the program illegal because of this limit.
298 Max_Aggr_Size : constant Nat :=
299 5000 + (2 ** 24 - 5000) *
301 (Restriction_Active (No_Elaboration_Code)
303 Restriction_Active (No_Implicit_Loops));
305 function Component_Count (T : Entity_Id) return Int;
306 -- The limit is applied to the total number of components that the
307 -- aggregate will have, which is the number of static expressions
308 -- that will appear in the flattened array. This requires a recursive
309 -- computation of the number of scalar components of the structure.
311 ---------------------
312 -- Component_Count --
313 ---------------------
315 function Component_Count (T : Entity_Id) return Int is
320 if Is_Scalar_Type (T) then
323 elsif Is_Record_Type (T) then
324 Comp := First_Component (T);
325 while Present (Comp) loop
326 Res := Res + Component_Count (Etype (Comp));
327 Next_Component (Comp);
332 elsif Is_Array_Type (T) then
334 Lo : constant Node_Id :=
335 Type_Low_Bound (Etype (First_Index (T)));
336 Hi : constant Node_Id :=
337 Type_High_Bound (Etype (First_Index (T)));
339 Siz : constant Int := Component_Count (Component_Type (T));
342 if not Compile_Time_Known_Value (Lo)
343 or else not Compile_Time_Known_Value (Hi)
348 Siz * UI_To_Int (Expr_Value (Hi) - Expr_Value (Lo) + 1);
353 -- Can only be a null for an access type
359 -- Start of processing for Aggr_Size_OK
362 Siz := Component_Count (Component_Type (Typ));
364 Indx := First_Index (Typ);
365 while Present (Indx) loop
366 Lo := Type_Low_Bound (Etype (Indx));
367 Hi := Type_High_Bound (Etype (Indx));
369 -- Bounds need to be known at compile time
371 if not Compile_Time_Known_Value (Lo)
372 or else not Compile_Time_Known_Value (Hi)
377 Lov := Expr_Value (Lo);
378 Hiv := Expr_Value (Hi);
380 -- A flat array is always safe
386 -- One-component aggregates are suspicious, and if the context type
387 -- is an object declaration with non-static bounds it will trip gcc;
388 -- such an aggregate must be expanded into a single assignment.
391 and then Nkind (Parent (N)) = N_Object_Declaration
394 Index_Type : constant Entity_Id :=
397 (Etype (Defining_Identifier (Parent (N)))));
401 if not Compile_Time_Known_Value (Type_Low_Bound (Index_Type))
402 or else not Compile_Time_Known_Value
403 (Type_High_Bound (Index_Type))
405 if Present (Component_Associations (N)) then
407 First (Choices (First (Component_Associations (N))));
408 if Is_Entity_Name (Indx)
409 and then not Is_Type (Entity (Indx))
412 ("single component aggregate in non-static context?",
414 Error_Msg_N ("\maybe subtype name was meant?", Indx);
424 Rng : constant Uint := Hiv - Lov + 1;
427 -- Check if size is too large
429 if not UI_Is_In_Int_Range (Rng) then
433 Siz := Siz * UI_To_Int (Rng);
437 or else Siz > Max_Aggr_Size
442 -- Bounds must be in integer range, for later array construction
444 if not UI_Is_In_Int_Range (Lov)
446 not UI_Is_In_Int_Range (Hiv)
457 ---------------------------------
458 -- Backend_Processing_Possible --
459 ---------------------------------
461 -- Backend processing by Gigi/gcc is possible only if all the following
462 -- conditions are met:
464 -- 1. N is fully positional
466 -- 2. N is not a bit-packed array aggregate;
468 -- 3. The size of N's array type must be known at compile time. Note
469 -- that this implies that the component size is also known
471 -- 4. The array type of N does not follow the Fortran layout convention
472 -- or if it does it must be 1 dimensional.
474 -- 5. The array component type may not be tagged (which could necessitate
475 -- reassignment of proper tags).
477 -- 6. The array component type must not have unaligned bit components
479 -- 7. None of the components of the aggregate may be bit unaligned
482 -- 8. There cannot be delayed components, since we do not know enough
483 -- at this stage to know if back end processing is possible.
485 -- 9. There cannot be any discriminated record components, since the
486 -- back end cannot handle this complex case.
488 -- 10. No controlled actions need to be generated for components
490 -- 11. For a VM back end, the array should have no aliased components
492 function Backend_Processing_Possible (N : Node_Id) return Boolean is
493 Typ : constant Entity_Id := Etype (N);
494 -- Typ is the correct constrained array subtype of the aggregate
496 function Component_Check (N : Node_Id; Index : Node_Id) return Boolean;
497 -- This routine checks components of aggregate N, enforcing checks
498 -- 1, 7, 8, and 9. In the multi-dimensional case, these checks are
499 -- performed on subaggregates. The Index value is the current index
500 -- being checked in the multi-dimensional case.
502 ---------------------
503 -- Component_Check --
504 ---------------------
506 function Component_Check (N : Node_Id; Index : Node_Id) return Boolean is
510 -- Checks 1: (no component associations)
512 if Present (Component_Associations (N)) then
516 -- Checks on components
518 -- Recurse to check subaggregates, which may appear in qualified
519 -- expressions. If delayed, the front-end will have to expand.
520 -- If the component is a discriminated record, treat as non-static,
521 -- as the back-end cannot handle this properly.
523 Expr := First (Expressions (N));
524 while Present (Expr) loop
526 -- Checks 8: (no delayed components)
528 if Is_Delayed_Aggregate (Expr) then
532 -- Checks 9: (no discriminated records)
534 if Present (Etype (Expr))
535 and then Is_Record_Type (Etype (Expr))
536 and then Has_Discriminants (Etype (Expr))
541 -- Checks 7. Component must not be bit aligned component
543 if Possible_Bit_Aligned_Component (Expr) then
547 -- Recursion to following indexes for multiple dimension case
549 if Present (Next_Index (Index))
550 and then not Component_Check (Expr, Next_Index (Index))
555 -- All checks for that component finished, on to next
563 -- Start of processing for Backend_Processing_Possible
566 -- Checks 2 (array not bit packed) and 10 (no controlled actions)
568 if Is_Bit_Packed_Array (Typ) or else Needs_Finalization (Typ) then
572 -- If component is limited, aggregate must be expanded because each
573 -- component assignment must be built in place.
575 if Is_Immutably_Limited_Type (Component_Type (Typ)) then
579 -- Checks 4 (array must not be multi-dimensional Fortran case)
581 if Convention (Typ) = Convention_Fortran
582 and then Number_Dimensions (Typ) > 1
587 -- Checks 3 (size of array must be known at compile time)
589 if not Size_Known_At_Compile_Time (Typ) then
593 -- Checks on components
595 if not Component_Check (N, First_Index (Typ)) then
599 -- Checks 5 (if the component type is tagged, then we may need to do
600 -- tag adjustments. Perhaps this should be refined to check for any
601 -- component associations that actually need tag adjustment, similar
602 -- to the test in Component_Not_OK_For_Backend for record aggregates
603 -- with tagged components, but not clear whether it's worthwhile ???;
604 -- in the case of the JVM, object tags are handled implicitly)
606 if Is_Tagged_Type (Component_Type (Typ))
607 and then Tagged_Type_Expansion
612 -- Checks 6 (component type must not have bit aligned components)
614 if Type_May_Have_Bit_Aligned_Components (Component_Type (Typ)) then
618 -- Checks 11: Array aggregates with aliased components are currently
619 -- not well supported by the VM backend; disable temporarily this
620 -- backend processing until it is definitely supported.
622 if VM_Target /= No_VM
623 and then Has_Aliased_Components (Base_Type (Typ))
628 -- Backend processing is possible
630 Set_Size_Known_At_Compile_Time (Etype (N), True);
632 end Backend_Processing_Possible;
634 ---------------------------
635 -- Build_Array_Aggr_Code --
636 ---------------------------
638 -- The code that we generate from a one dimensional aggregate is
640 -- 1. If the sub-aggregate contains discrete choices we
642 -- (a) Sort the discrete choices
644 -- (b) Otherwise for each discrete choice that specifies a range we
645 -- emit a loop. If a range specifies a maximum of three values, or
646 -- we are dealing with an expression we emit a sequence of
647 -- assignments instead of a loop.
649 -- (c) Generate the remaining loops to cover the others choice if any
651 -- 2. If the aggregate contains positional elements we
653 -- (a) translate the positional elements in a series of assignments
655 -- (b) Generate a final loop to cover the others choice if any.
656 -- Note that this final loop has to be a while loop since the case
658 -- L : Integer := Integer'Last;
659 -- H : Integer := Integer'Last;
660 -- A : array (L .. H) := (1, others =>0);
662 -- cannot be handled by a for loop. Thus for the following
664 -- array (L .. H) := (.. positional elements.., others =>E);
666 -- we always generate something like:
668 -- J : Index_Type := Index_Of_Last_Positional_Element;
670 -- J := Index_Base'Succ (J)
674 function Build_Array_Aggr_Code
679 Scalar_Comp : Boolean;
680 Indexes : List_Id := No_List) return List_Id
682 Loc : constant Source_Ptr := Sloc (N);
683 Index_Base : constant Entity_Id := Base_Type (Etype (Index));
684 Index_Base_L : constant Node_Id := Type_Low_Bound (Index_Base);
685 Index_Base_H : constant Node_Id := Type_High_Bound (Index_Base);
687 function Add (Val : Int; To : Node_Id) return Node_Id;
688 -- Returns an expression where Val is added to expression To, unless
689 -- To+Val is provably out of To's base type range. To must be an
690 -- already analyzed expression.
692 function Empty_Range (L, H : Node_Id) return Boolean;
693 -- Returns True if the range defined by L .. H is certainly empty
695 function Equal (L, H : Node_Id) return Boolean;
696 -- Returns True if L = H for sure
698 function Index_Base_Name return Node_Id;
699 -- Returns a new reference to the index type name
701 function Gen_Assign (Ind : Node_Id; Expr : Node_Id) return List_Id;
702 -- Ind must be a side-effect free expression. If the input aggregate
703 -- N to Build_Loop contains no sub-aggregates, then this function
704 -- returns the assignment statement:
706 -- Into (Indexes, Ind) := Expr;
708 -- Otherwise we call Build_Code recursively
710 -- Ada 2005 (AI-287): In case of default initialized component, Expr
711 -- is empty and we generate a call to the corresponding IP subprogram.
713 function Gen_Loop (L, H : Node_Id; Expr : Node_Id) return List_Id;
714 -- Nodes L and H must be side-effect free expressions.
715 -- If the input aggregate N to Build_Loop contains no sub-aggregates,
716 -- This routine returns the for loop statement
718 -- for J in Index_Base'(L) .. Index_Base'(H) loop
719 -- Into (Indexes, J) := Expr;
722 -- Otherwise we call Build_Code recursively.
723 -- As an optimization if the loop covers 3 or less scalar elements we
724 -- generate a sequence of assignments.
726 function Gen_While (L, H : Node_Id; Expr : Node_Id) return List_Id;
727 -- Nodes L and H must be side-effect free expressions.
728 -- If the input aggregate N to Build_Loop contains no sub-aggregates,
729 -- This routine returns the while loop statement
731 -- J : Index_Base := L;
733 -- J := Index_Base'Succ (J);
734 -- Into (Indexes, J) := Expr;
737 -- Otherwise we call Build_Code recursively
739 function Local_Compile_Time_Known_Value (E : Node_Id) return Boolean;
740 function Local_Expr_Value (E : Node_Id) return Uint;
741 -- These two Local routines are used to replace the corresponding ones
742 -- in sem_eval because while processing the bounds of an aggregate with
743 -- discrete choices whose index type is an enumeration, we build static
744 -- expressions not recognized by Compile_Time_Known_Value as such since
745 -- they have not yet been analyzed and resolved. All the expressions in
746 -- question are things like Index_Base_Name'Val (Const) which we can
747 -- easily recognize as being constant.
753 function Add (Val : Int; To : Node_Id) return Node_Id is
758 U_Val : constant Uint := UI_From_Int (Val);
761 -- Note: do not try to optimize the case of Val = 0, because
762 -- we need to build a new node with the proper Sloc value anyway.
764 -- First test if we can do constant folding
766 if Local_Compile_Time_Known_Value (To) then
767 U_To := Local_Expr_Value (To) + Val;
769 -- Determine if our constant is outside the range of the index.
770 -- If so return an Empty node. This empty node will be caught
771 -- by Empty_Range below.
773 if Compile_Time_Known_Value (Index_Base_L)
774 and then U_To < Expr_Value (Index_Base_L)
778 elsif Compile_Time_Known_Value (Index_Base_H)
779 and then U_To > Expr_Value (Index_Base_H)
784 Expr_Pos := Make_Integer_Literal (Loc, U_To);
785 Set_Is_Static_Expression (Expr_Pos);
787 if not Is_Enumeration_Type (Index_Base) then
790 -- If we are dealing with enumeration return
791 -- Index_Base'Val (Expr_Pos)
795 Make_Attribute_Reference
797 Prefix => Index_Base_Name,
798 Attribute_Name => Name_Val,
799 Expressions => New_List (Expr_Pos));
805 -- If we are here no constant folding possible
807 if not Is_Enumeration_Type (Index_Base) then
810 Left_Opnd => Duplicate_Subexpr (To),
811 Right_Opnd => Make_Integer_Literal (Loc, U_Val));
813 -- If we are dealing with enumeration return
814 -- Index_Base'Val (Index_Base'Pos (To) + Val)
818 Make_Attribute_Reference
820 Prefix => Index_Base_Name,
821 Attribute_Name => Name_Pos,
822 Expressions => New_List (Duplicate_Subexpr (To)));
827 Right_Opnd => Make_Integer_Literal (Loc, U_Val));
830 Make_Attribute_Reference
832 Prefix => Index_Base_Name,
833 Attribute_Name => Name_Val,
834 Expressions => New_List (Expr_Pos));
844 function Empty_Range (L, H : Node_Id) return Boolean is
845 Is_Empty : Boolean := False;
850 -- First check if L or H were already detected as overflowing the
851 -- index base range type by function Add above. If this is so Add
852 -- returns the empty node.
854 if No (L) or else No (H) then
861 -- L > H range is empty
867 -- B_L > H range must be empty
873 -- L > B_H range must be empty
877 High := Index_Base_H;
880 if Local_Compile_Time_Known_Value (Low)
881 and then Local_Compile_Time_Known_Value (High)
884 UI_Gt (Local_Expr_Value (Low), Local_Expr_Value (High));
897 function Equal (L, H : Node_Id) return Boolean is
902 elsif Local_Compile_Time_Known_Value (L)
903 and then Local_Compile_Time_Known_Value (H)
905 return UI_Eq (Local_Expr_Value (L), Local_Expr_Value (H));
915 function Gen_Assign (Ind : Node_Id; Expr : Node_Id) return List_Id is
916 L : constant List_Id := New_List;
919 New_Indexes : List_Id;
920 Indexed_Comp : Node_Id;
922 Comp_Type : Entity_Id := Empty;
924 function Add_Loop_Actions (Lis : List_Id) return List_Id;
925 -- Collect insert_actions generated in the construction of a
926 -- loop, and prepend them to the sequence of assignments to
927 -- complete the eventual body of the loop.
929 ----------------------
930 -- Add_Loop_Actions --
931 ----------------------
933 function Add_Loop_Actions (Lis : List_Id) return List_Id is
937 -- Ada 2005 (AI-287): Do nothing else in case of default
938 -- initialized component.
943 elsif Nkind (Parent (Expr)) = N_Component_Association
944 and then Present (Loop_Actions (Parent (Expr)))
946 Append_List (Lis, Loop_Actions (Parent (Expr)));
947 Res := Loop_Actions (Parent (Expr));
948 Set_Loop_Actions (Parent (Expr), No_List);
954 end Add_Loop_Actions;
956 -- Start of processing for Gen_Assign
960 New_Indexes := New_List;
962 New_Indexes := New_Copy_List_Tree (Indexes);
965 Append_To (New_Indexes, Ind);
967 if Present (Next_Index (Index)) then
970 Build_Array_Aggr_Code
973 Index => Next_Index (Index),
975 Scalar_Comp => Scalar_Comp,
976 Indexes => New_Indexes));
979 -- If we get here then we are at a bottom-level (sub-)aggregate
983 (Make_Indexed_Component (Loc,
984 Prefix => New_Copy_Tree (Into),
985 Expressions => New_Indexes));
987 Set_Assignment_OK (Indexed_Comp);
989 -- Ada 2005 (AI-287): In case of default initialized component, Expr
990 -- is not present (and therefore we also initialize Expr_Q to empty).
994 elsif Nkind (Expr) = N_Qualified_Expression then
995 Expr_Q := Expression (Expr);
1000 if Present (Etype (N))
1001 and then Etype (N) /= Any_Composite
1003 Comp_Type := Component_Type (Etype (N));
1004 pragma Assert (Comp_Type = Ctype); -- AI-287
1006 elsif Present (Next (First (New_Indexes))) then
1008 -- Ada 2005 (AI-287): Do nothing in case of default initialized
1009 -- component because we have received the component type in
1010 -- the formal parameter Ctype.
1012 -- ??? Some assert pragmas have been added to check if this new
1013 -- formal can be used to replace this code in all cases.
1015 if Present (Expr) then
1017 -- This is a multidimensional array. Recover the component
1018 -- type from the outermost aggregate, because subaggregates
1019 -- do not have an assigned type.
1026 while Present (P) loop
1027 if Nkind (P) = N_Aggregate
1028 and then Present (Etype (P))
1030 Comp_Type := Component_Type (Etype (P));
1038 pragma Assert (Comp_Type = Ctype); -- AI-287
1043 -- Ada 2005 (AI-287): We only analyze the expression in case of non-
1044 -- default initialized components (otherwise Expr_Q is not present).
1047 and then Nkind_In (Expr_Q, N_Aggregate, N_Extension_Aggregate)
1049 -- At this stage the Expression may not have been analyzed yet
1050 -- because the array aggregate code has not been updated to use
1051 -- the Expansion_Delayed flag and avoid analysis altogether to
1052 -- solve the same problem (see Resolve_Aggr_Expr). So let us do
1053 -- the analysis of non-array aggregates now in order to get the
1054 -- value of Expansion_Delayed flag for the inner aggregate ???
1056 if Present (Comp_Type) and then not Is_Array_Type (Comp_Type) then
1057 Analyze_And_Resolve (Expr_Q, Comp_Type);
1060 if Is_Delayed_Aggregate (Expr_Q) then
1062 -- This is either a subaggregate of a multidimensional array,
1063 -- or a component of an array type whose component type is
1064 -- also an array. In the latter case, the expression may have
1065 -- component associations that provide different bounds from
1066 -- those of the component type, and sliding must occur. Instead
1067 -- of decomposing the current aggregate assignment, force the
1068 -- re-analysis of the assignment, so that a temporary will be
1069 -- generated in the usual fashion, and sliding will take place.
1071 if Nkind (Parent (N)) = N_Assignment_Statement
1072 and then Is_Array_Type (Comp_Type)
1073 and then Present (Component_Associations (Expr_Q))
1074 and then Must_Slide (Comp_Type, Etype (Expr_Q))
1076 Set_Expansion_Delayed (Expr_Q, False);
1077 Set_Analyzed (Expr_Q, False);
1082 Late_Expansion (Expr_Q, Etype (Expr_Q), Indexed_Comp));
1087 -- Ada 2005 (AI-287): In case of default initialized component, call
1088 -- the initialization subprogram associated with the component type.
1089 -- If the component type is an access type, add an explicit null
1090 -- assignment, because for the back-end there is an initialization
1091 -- present for the whole aggregate, and no default initialization
1094 -- In addition, if the component type is controlled, we must call
1095 -- its Initialize procedure explicitly, because there is no explicit
1096 -- object creation that will invoke it otherwise.
1099 if Present (Base_Init_Proc (Base_Type (Ctype)))
1100 or else Has_Task (Base_Type (Ctype))
1103 Build_Initialization_Call (Loc,
1104 Id_Ref => Indexed_Comp,
1106 With_Default_Init => True));
1108 elsif Is_Access_Type (Ctype) then
1110 Make_Assignment_Statement (Loc,
1111 Name => Indexed_Comp,
1112 Expression => Make_Null (Loc)));
1115 if Needs_Finalization (Ctype) then
1118 Obj_Ref => New_Copy_Tree (Indexed_Comp),
1123 -- Now generate the assignment with no associated controlled
1124 -- actions since the target of the assignment may not have been
1125 -- initialized, it is not possible to Finalize it as expected by
1126 -- normal controlled assignment. The rest of the controlled
1127 -- actions are done manually with the proper finalization list
1128 -- coming from the context.
1131 Make_OK_Assignment_Statement (Loc,
1132 Name => Indexed_Comp,
1133 Expression => New_Copy_Tree (Expr));
1135 if Present (Comp_Type) and then Needs_Finalization (Comp_Type) then
1136 Set_No_Ctrl_Actions (A);
1138 -- If this is an aggregate for an array of arrays, each
1139 -- sub-aggregate will be expanded as well, and even with
1140 -- No_Ctrl_Actions the assignments of inner components will
1141 -- require attachment in their assignments to temporaries.
1142 -- These temporaries must be finalized for each subaggregate,
1143 -- to prevent multiple attachments of the same temporary
1144 -- location to same finalization chain (and consequently
1145 -- circular lists). To ensure that finalization takes place
1146 -- for each subaggregate we wrap the assignment in a block.
1148 if Is_Array_Type (Comp_Type)
1149 and then Nkind (Expr) = N_Aggregate
1152 Make_Block_Statement (Loc,
1153 Handled_Statement_Sequence =>
1154 Make_Handled_Sequence_Of_Statements (Loc,
1155 Statements => New_List (A)));
1161 -- Adjust the tag if tagged (because of possible view
1162 -- conversions), unless compiling for a VM where
1163 -- tags are implicit.
1165 if Present (Comp_Type)
1166 and then Is_Tagged_Type (Comp_Type)
1167 and then Tagged_Type_Expansion
1170 Full_Typ : constant Entity_Id := Underlying_Type (Comp_Type);
1174 Make_OK_Assignment_Statement (Loc,
1176 Make_Selected_Component (Loc,
1177 Prefix => New_Copy_Tree (Indexed_Comp),
1180 (First_Tag_Component (Full_Typ), Loc)),
1183 Unchecked_Convert_To (RTE (RE_Tag),
1185 (Node (First_Elmt (Access_Disp_Table (Full_Typ))),
1192 -- Adjust and attach the component to the proper final list, which
1193 -- can be the controller of the outer record object or the final
1194 -- list associated with the scope.
1196 -- If the component is itself an array of controlled types, whose
1197 -- value is given by a sub-aggregate, then the attach calls have
1198 -- been generated when individual subcomponent are assigned, and
1199 -- must not be done again to prevent malformed finalization chains
1200 -- (see comments above, concerning the creation of a block to hold
1201 -- inner finalization actions).
1203 if Present (Comp_Type)
1204 and then Needs_Finalization (Comp_Type)
1205 and then not Is_Limited_Type (Comp_Type)
1207 (Is_Array_Type (Comp_Type)
1208 and then Is_Controlled (Component_Type (Comp_Type))
1209 and then Nkind (Expr) = N_Aggregate)
1213 Obj_Ref => New_Copy_Tree (Indexed_Comp),
1218 return Add_Loop_Actions (L);
1225 function Gen_Loop (L, H : Node_Id; Expr : Node_Id) return List_Id is
1235 -- Index_Base'(L) .. Index_Base'(H)
1237 L_Iteration_Scheme : Node_Id;
1238 -- L_J in Index_Base'(L) .. Index_Base'(H)
1241 -- The statements to execute in the loop
1243 S : constant List_Id := New_List;
1244 -- List of statements
1247 -- Copy of expression tree, used for checking purposes
1250 -- If loop bounds define an empty range return the null statement
1252 if Empty_Range (L, H) then
1253 Append_To (S, Make_Null_Statement (Loc));
1255 -- Ada 2005 (AI-287): Nothing else need to be done in case of
1256 -- default initialized component.
1262 -- The expression must be type-checked even though no component
1263 -- of the aggregate will have this value. This is done only for
1264 -- actual components of the array, not for subaggregates. Do
1265 -- the check on a copy, because the expression may be shared
1266 -- among several choices, some of which might be non-null.
1268 if Present (Etype (N))
1269 and then Is_Array_Type (Etype (N))
1270 and then No (Next_Index (Index))
1272 Expander_Mode_Save_And_Set (False);
1273 Tcopy := New_Copy_Tree (Expr);
1274 Set_Parent (Tcopy, N);
1275 Analyze_And_Resolve (Tcopy, Component_Type (Etype (N)));
1276 Expander_Mode_Restore;
1282 -- If loop bounds are the same then generate an assignment
1284 elsif Equal (L, H) then
1285 return Gen_Assign (New_Copy_Tree (L), Expr);
1287 -- If H - L <= 2 then generate a sequence of assignments when we are
1288 -- processing the bottom most aggregate and it contains scalar
1291 elsif No (Next_Index (Index))
1292 and then Scalar_Comp
1293 and then Local_Compile_Time_Known_Value (L)
1294 and then Local_Compile_Time_Known_Value (H)
1295 and then Local_Expr_Value (H) - Local_Expr_Value (L) <= 2
1298 Append_List_To (S, Gen_Assign (New_Copy_Tree (L), Expr));
1299 Append_List_To (S, Gen_Assign (Add (1, To => L), Expr));
1301 if Local_Expr_Value (H) - Local_Expr_Value (L) = 2 then
1302 Append_List_To (S, Gen_Assign (Add (2, To => L), Expr));
1308 -- Otherwise construct the loop, starting with the loop index L_J
1310 L_J := Make_Temporary (Loc, 'J', L);
1312 -- Construct "L .. H" in Index_Base. We use a qualified expression
1313 -- for the bound to convert to the index base, but we don't need
1314 -- to do that if we already have the base type at hand.
1316 if Etype (L) = Index_Base then
1320 Make_Qualified_Expression (Loc,
1321 Subtype_Mark => Index_Base_Name,
1325 if Etype (H) = Index_Base then
1329 Make_Qualified_Expression (Loc,
1330 Subtype_Mark => Index_Base_Name,
1339 -- Construct "for L_J in Index_Base range L .. H"
1341 L_Iteration_Scheme :=
1342 Make_Iteration_Scheme
1344 Loop_Parameter_Specification =>
1345 Make_Loop_Parameter_Specification
1347 Defining_Identifier => L_J,
1348 Discrete_Subtype_Definition => L_Range));
1350 -- Construct the statements to execute in the loop body
1352 L_Body := Gen_Assign (New_Reference_To (L_J, Loc), Expr);
1354 -- Construct the final loop
1356 Append_To (S, Make_Implicit_Loop_Statement
1358 Identifier => Empty,
1359 Iteration_Scheme => L_Iteration_Scheme,
1360 Statements => L_Body));
1362 -- A small optimization: if the aggregate is initialized with a box
1363 -- and the component type has no initialization procedure, remove the
1364 -- useless empty loop.
1366 if Nkind (First (S)) = N_Loop_Statement
1367 and then Is_Empty_List (Statements (First (S)))
1369 return New_List (Make_Null_Statement (Loc));
1379 -- The code built is
1381 -- W_J : Index_Base := L;
1382 -- while W_J < H loop
1383 -- W_J := Index_Base'Succ (W);
1387 function Gen_While (L, H : Node_Id; Expr : Node_Id) return List_Id is
1391 -- W_J : Base_Type := L;
1393 W_Iteration_Scheme : Node_Id;
1396 W_Index_Succ : Node_Id;
1397 -- Index_Base'Succ (J)
1399 W_Increment : Node_Id;
1400 -- W_J := Index_Base'Succ (W)
1402 W_Body : constant List_Id := New_List;
1403 -- The statements to execute in the loop
1405 S : constant List_Id := New_List;
1406 -- list of statement
1409 -- If loop bounds define an empty range or are equal return null
1411 if Empty_Range (L, H) or else Equal (L, H) then
1412 Append_To (S, Make_Null_Statement (Loc));
1416 -- Build the decl of W_J
1418 W_J := Make_Temporary (Loc, 'J', L);
1420 Make_Object_Declaration
1422 Defining_Identifier => W_J,
1423 Object_Definition => Index_Base_Name,
1426 -- Theoretically we should do a New_Copy_Tree (L) here, but we know
1427 -- that in this particular case L is a fresh Expr generated by
1428 -- Add which we are the only ones to use.
1430 Append_To (S, W_Decl);
1432 -- Construct " while W_J < H"
1434 W_Iteration_Scheme :=
1435 Make_Iteration_Scheme
1437 Condition => Make_Op_Lt
1439 Left_Opnd => New_Reference_To (W_J, Loc),
1440 Right_Opnd => New_Copy_Tree (H)));
1442 -- Construct the statements to execute in the loop body
1445 Make_Attribute_Reference
1447 Prefix => Index_Base_Name,
1448 Attribute_Name => Name_Succ,
1449 Expressions => New_List (New_Reference_To (W_J, Loc)));
1452 Make_OK_Assignment_Statement
1454 Name => New_Reference_To (W_J, Loc),
1455 Expression => W_Index_Succ);
1457 Append_To (W_Body, W_Increment);
1458 Append_List_To (W_Body,
1459 Gen_Assign (New_Reference_To (W_J, Loc), Expr));
1461 -- Construct the final loop
1463 Append_To (S, Make_Implicit_Loop_Statement
1465 Identifier => Empty,
1466 Iteration_Scheme => W_Iteration_Scheme,
1467 Statements => W_Body));
1472 ---------------------
1473 -- Index_Base_Name --
1474 ---------------------
1476 function Index_Base_Name return Node_Id is
1478 return New_Reference_To (Index_Base, Sloc (N));
1479 end Index_Base_Name;
1481 ------------------------------------
1482 -- Local_Compile_Time_Known_Value --
1483 ------------------------------------
1485 function Local_Compile_Time_Known_Value (E : Node_Id) return Boolean is
1487 return Compile_Time_Known_Value (E)
1489 (Nkind (E) = N_Attribute_Reference
1490 and then Attribute_Name (E) = Name_Val
1491 and then Compile_Time_Known_Value (First (Expressions (E))));
1492 end Local_Compile_Time_Known_Value;
1494 ----------------------
1495 -- Local_Expr_Value --
1496 ----------------------
1498 function Local_Expr_Value (E : Node_Id) return Uint is
1500 if Compile_Time_Known_Value (E) then
1501 return Expr_Value (E);
1503 return Expr_Value (First (Expressions (E)));
1505 end Local_Expr_Value;
1507 -- Build_Array_Aggr_Code Variables
1514 Others_Expr : Node_Id := Empty;
1515 Others_Box_Present : Boolean := False;
1517 Aggr_L : constant Node_Id := Low_Bound (Aggregate_Bounds (N));
1518 Aggr_H : constant Node_Id := High_Bound (Aggregate_Bounds (N));
1519 -- The aggregate bounds of this specific sub-aggregate. Note that if
1520 -- the code generated by Build_Array_Aggr_Code is executed then these
1521 -- bounds are OK. Otherwise a Constraint_Error would have been raised.
1523 Aggr_Low : constant Node_Id := Duplicate_Subexpr_No_Checks (Aggr_L);
1524 Aggr_High : constant Node_Id := Duplicate_Subexpr_No_Checks (Aggr_H);
1525 -- After Duplicate_Subexpr these are side-effect free
1530 Nb_Choices : Nat := 0;
1531 Table : Case_Table_Type (1 .. Number_Of_Choices (N));
1532 -- Used to sort all the different choice values
1535 -- Number of elements in the positional aggregate
1537 New_Code : constant List_Id := New_List;
1539 -- Start of processing for Build_Array_Aggr_Code
1542 -- First before we start, a special case. if we have a bit packed
1543 -- array represented as a modular type, then clear the value to
1544 -- zero first, to ensure that unused bits are properly cleared.
1549 and then Is_Bit_Packed_Array (Typ)
1550 and then Is_Modular_Integer_Type (Packed_Array_Type (Typ))
1552 Append_To (New_Code,
1553 Make_Assignment_Statement (Loc,
1554 Name => New_Copy_Tree (Into),
1556 Unchecked_Convert_To (Typ,
1557 Make_Integer_Literal (Loc, Uint_0))));
1560 -- If the component type contains tasks, we need to build a Master
1561 -- entity in the current scope, because it will be needed if build-
1562 -- in-place functions are called in the expanded code.
1564 if Nkind (Parent (N)) = N_Object_Declaration
1565 and then Has_Task (Typ)
1567 Build_Master_Entity (Defining_Identifier (Parent (N)));
1570 -- STEP 1: Process component associations
1572 -- For those associations that may generate a loop, initialize
1573 -- Loop_Actions to collect inserted actions that may be crated.
1575 -- Skip this if no component associations
1577 if No (Expressions (N)) then
1579 -- STEP 1 (a): Sort the discrete choices
1581 Assoc := First (Component_Associations (N));
1582 while Present (Assoc) loop
1583 Choice := First (Choices (Assoc));
1584 while Present (Choice) loop
1585 if Nkind (Choice) = N_Others_Choice then
1586 Set_Loop_Actions (Assoc, New_List);
1588 if Box_Present (Assoc) then
1589 Others_Box_Present := True;
1591 Others_Expr := Expression (Assoc);
1596 Get_Index_Bounds (Choice, Low, High);
1599 Set_Loop_Actions (Assoc, New_List);
1602 Nb_Choices := Nb_Choices + 1;
1603 if Box_Present (Assoc) then
1604 Table (Nb_Choices) := (Choice_Lo => Low,
1606 Choice_Node => Empty);
1608 Table (Nb_Choices) := (Choice_Lo => Low,
1610 Choice_Node => Expression (Assoc));
1618 -- If there is more than one set of choices these must be static
1619 -- and we can therefore sort them. Remember that Nb_Choices does not
1620 -- account for an others choice.
1622 if Nb_Choices > 1 then
1623 Sort_Case_Table (Table);
1626 -- STEP 1 (b): take care of the whole set of discrete choices
1628 for J in 1 .. Nb_Choices loop
1629 Low := Table (J).Choice_Lo;
1630 High := Table (J).Choice_Hi;
1631 Expr := Table (J).Choice_Node;
1632 Append_List (Gen_Loop (Low, High, Expr), To => New_Code);
1635 -- STEP 1 (c): generate the remaining loops to cover others choice
1636 -- We don't need to generate loops over empty gaps, but if there is
1637 -- a single empty range we must analyze the expression for semantics
1639 if Present (Others_Expr) or else Others_Box_Present then
1641 First : Boolean := True;
1644 for J in 0 .. Nb_Choices loop
1648 Low := Add (1, To => Table (J).Choice_Hi);
1651 if J = Nb_Choices then
1654 High := Add (-1, To => Table (J + 1).Choice_Lo);
1657 -- If this is an expansion within an init proc, make
1658 -- sure that discriminant references are replaced by
1659 -- the corresponding discriminal.
1661 if Inside_Init_Proc then
1662 if Is_Entity_Name (Low)
1663 and then Ekind (Entity (Low)) = E_Discriminant
1665 Set_Entity (Low, Discriminal (Entity (Low)));
1668 if Is_Entity_Name (High)
1669 and then Ekind (Entity (High)) = E_Discriminant
1671 Set_Entity (High, Discriminal (Entity (High)));
1676 or else not Empty_Range (Low, High)
1680 (Gen_Loop (Low, High, Others_Expr), To => New_Code);
1686 -- STEP 2: Process positional components
1689 -- STEP 2 (a): Generate the assignments for each positional element
1690 -- Note that here we have to use Aggr_L rather than Aggr_Low because
1691 -- Aggr_L is analyzed and Add wants an analyzed expression.
1693 Expr := First (Expressions (N));
1695 while Present (Expr) loop
1696 Nb_Elements := Nb_Elements + 1;
1697 Append_List (Gen_Assign (Add (Nb_Elements, To => Aggr_L), Expr),
1702 -- STEP 2 (b): Generate final loop if an others choice is present
1703 -- Here Nb_Elements gives the offset of the last positional element.
1705 if Present (Component_Associations (N)) then
1706 Assoc := Last (Component_Associations (N));
1708 -- Ada 2005 (AI-287)
1710 if Box_Present (Assoc) then
1711 Append_List (Gen_While (Add (Nb_Elements, To => Aggr_L),
1716 Expr := Expression (Assoc);
1718 Append_List (Gen_While (Add (Nb_Elements, To => Aggr_L),
1727 end Build_Array_Aggr_Code;
1729 ----------------------------
1730 -- Build_Record_Aggr_Code --
1731 ----------------------------
1733 function Build_Record_Aggr_Code
1737 Is_Limited_Ancestor_Expansion : Boolean := False) return List_Id
1739 Loc : constant Source_Ptr := Sloc (N);
1740 L : constant List_Id := New_List;
1741 N_Typ : constant Entity_Id := Etype (N);
1747 Comp_Type : Entity_Id;
1748 Selector : Entity_Id;
1749 Comp_Expr : Node_Id;
1752 -- If this is an internal aggregate, the External_Final_List is an
1753 -- expression for the controller record of the enclosing type.
1755 -- If the current aggregate has several controlled components, this
1756 -- expression will appear in several calls to attach to the finali-
1757 -- zation list, and it must not be shared.
1759 Ancestor_Is_Expression : Boolean := False;
1760 Ancestor_Is_Subtype_Mark : Boolean := False;
1762 Init_Typ : Entity_Id := Empty;
1764 Finalization_Done : Boolean := False;
1765 -- True if Generate_Finalization_Actions has already been called; calls
1766 -- after the first do nothing.
1768 function Ancestor_Discriminant_Value (Disc : Entity_Id) return Node_Id;
1769 -- Returns the value that the given discriminant of an ancestor type
1770 -- should receive (in the absence of a conflict with the value provided
1771 -- by an ancestor part of an extension aggregate).
1773 procedure Check_Ancestor_Discriminants (Anc_Typ : Entity_Id);
1774 -- Check that each of the discriminant values defined by the ancestor
1775 -- part of an extension aggregate match the corresponding values
1776 -- provided by either an association of the aggregate or by the
1777 -- constraint imposed by a parent type (RM95-4.3.2(8)).
1779 function Compatible_Int_Bounds
1780 (Agg_Bounds : Node_Id;
1781 Typ_Bounds : Node_Id) return Boolean;
1782 -- Return true if Agg_Bounds are equal or within Typ_Bounds. It is
1783 -- assumed that both bounds are integer ranges.
1785 procedure Generate_Finalization_Actions;
1786 -- Deal with the various controlled type data structure initializations
1787 -- (but only if it hasn't been done already).
1789 function Get_Constraint_Association (T : Entity_Id) return Node_Id;
1790 -- Returns the first discriminant association in the constraint
1791 -- associated with T, if any, otherwise returns Empty.
1793 procedure Init_Hidden_Discriminants (Typ : Entity_Id; List : List_Id);
1794 -- If Typ is derived, and constrains discriminants of the parent type,
1795 -- these discriminants are not components of the aggregate, and must be
1796 -- initialized. The assignments are appended to List.
1798 function Is_Int_Range_Bounds (Bounds : Node_Id) return Boolean;
1799 -- Check whether Bounds is a range node and its lower and higher bounds
1800 -- are integers literals.
1802 ---------------------------------
1803 -- Ancestor_Discriminant_Value --
1804 ---------------------------------
1806 function Ancestor_Discriminant_Value (Disc : Entity_Id) return Node_Id is
1808 Assoc_Elmt : Elmt_Id;
1809 Aggr_Comp : Entity_Id;
1810 Corresp_Disc : Entity_Id;
1811 Current_Typ : Entity_Id := Base_Type (Typ);
1812 Parent_Typ : Entity_Id;
1813 Parent_Disc : Entity_Id;
1814 Save_Assoc : Node_Id := Empty;
1817 -- First check any discriminant associations to see if any of them
1818 -- provide a value for the discriminant.
1820 if Present (Discriminant_Specifications (Parent (Current_Typ))) then
1821 Assoc := First (Component_Associations (N));
1822 while Present (Assoc) loop
1823 Aggr_Comp := Entity (First (Choices (Assoc)));
1825 if Ekind (Aggr_Comp) = E_Discriminant then
1826 Save_Assoc := Expression (Assoc);
1828 Corresp_Disc := Corresponding_Discriminant (Aggr_Comp);
1829 while Present (Corresp_Disc) loop
1831 -- If found a corresponding discriminant then return the
1832 -- value given in the aggregate. (Note: this is not
1833 -- correct in the presence of side effects. ???)
1835 if Disc = Corresp_Disc then
1836 return Duplicate_Subexpr (Expression (Assoc));
1840 Corresponding_Discriminant (Corresp_Disc);
1848 -- No match found in aggregate, so chain up parent types to find
1849 -- a constraint that defines the value of the discriminant.
1851 Parent_Typ := Etype (Current_Typ);
1852 while Current_Typ /= Parent_Typ loop
1853 if Has_Discriminants (Parent_Typ)
1854 and then not Has_Unknown_Discriminants (Parent_Typ)
1856 Parent_Disc := First_Discriminant (Parent_Typ);
1858 -- We either get the association from the subtype indication
1859 -- of the type definition itself, or from the discriminant
1860 -- constraint associated with the type entity (which is
1861 -- preferable, but it's not always present ???)
1863 if Is_Empty_Elmt_List (
1864 Discriminant_Constraint (Current_Typ))
1866 Assoc := Get_Constraint_Association (Current_Typ);
1867 Assoc_Elmt := No_Elmt;
1870 First_Elmt (Discriminant_Constraint (Current_Typ));
1871 Assoc := Node (Assoc_Elmt);
1874 -- Traverse the discriminants of the parent type looking
1875 -- for one that corresponds.
1877 while Present (Parent_Disc) and then Present (Assoc) loop
1878 Corresp_Disc := Parent_Disc;
1879 while Present (Corresp_Disc)
1880 and then Disc /= Corresp_Disc
1883 Corresponding_Discriminant (Corresp_Disc);
1886 if Disc = Corresp_Disc then
1887 if Nkind (Assoc) = N_Discriminant_Association then
1888 Assoc := Expression (Assoc);
1891 -- If the located association directly denotes a
1892 -- discriminant, then use the value of a saved
1893 -- association of the aggregate. This is a kludge to
1894 -- handle certain cases involving multiple discriminants
1895 -- mapped to a single discriminant of a descendant. It's
1896 -- not clear how to locate the appropriate discriminant
1897 -- value for such cases. ???
1899 if Is_Entity_Name (Assoc)
1900 and then Ekind (Entity (Assoc)) = E_Discriminant
1902 Assoc := Save_Assoc;
1905 return Duplicate_Subexpr (Assoc);
1908 Next_Discriminant (Parent_Disc);
1910 if No (Assoc_Elmt) then
1913 Next_Elmt (Assoc_Elmt);
1914 if Present (Assoc_Elmt) then
1915 Assoc := Node (Assoc_Elmt);
1923 Current_Typ := Parent_Typ;
1924 Parent_Typ := Etype (Current_Typ);
1927 -- In some cases there's no ancestor value to locate (such as
1928 -- when an ancestor part given by an expression defines the
1929 -- discriminant value).
1932 end Ancestor_Discriminant_Value;
1934 ----------------------------------
1935 -- Check_Ancestor_Discriminants --
1936 ----------------------------------
1938 procedure Check_Ancestor_Discriminants (Anc_Typ : Entity_Id) is
1940 Disc_Value : Node_Id;
1944 Discr := First_Discriminant (Base_Type (Anc_Typ));
1945 while Present (Discr) loop
1946 Disc_Value := Ancestor_Discriminant_Value (Discr);
1948 if Present (Disc_Value) then
1949 Cond := Make_Op_Ne (Loc,
1951 Make_Selected_Component (Loc,
1952 Prefix => New_Copy_Tree (Target),
1953 Selector_Name => New_Occurrence_Of (Discr, Loc)),
1954 Right_Opnd => Disc_Value);
1957 Make_Raise_Constraint_Error (Loc,
1959 Reason => CE_Discriminant_Check_Failed));
1962 Next_Discriminant (Discr);
1964 end Check_Ancestor_Discriminants;
1966 ---------------------------
1967 -- Compatible_Int_Bounds --
1968 ---------------------------
1970 function Compatible_Int_Bounds
1971 (Agg_Bounds : Node_Id;
1972 Typ_Bounds : Node_Id) return Boolean
1974 Agg_Lo : constant Uint := Intval (Low_Bound (Agg_Bounds));
1975 Agg_Hi : constant Uint := Intval (High_Bound (Agg_Bounds));
1976 Typ_Lo : constant Uint := Intval (Low_Bound (Typ_Bounds));
1977 Typ_Hi : constant Uint := Intval (High_Bound (Typ_Bounds));
1979 return Typ_Lo <= Agg_Lo and then Agg_Hi <= Typ_Hi;
1980 end Compatible_Int_Bounds;
1982 --------------------------------
1983 -- Get_Constraint_Association --
1984 --------------------------------
1986 function Get_Constraint_Association (T : Entity_Id) return Node_Id is
1987 Typ_Def : constant Node_Id := Type_Definition (Parent (T));
1988 Indic : constant Node_Id := Subtype_Indication (Typ_Def);
1991 -- ??? Also need to cover case of a type mark denoting a subtype
1994 if Nkind (Indic) = N_Subtype_Indication
1995 and then Present (Constraint (Indic))
1997 return First (Constraints (Constraint (Indic)));
2001 end Get_Constraint_Association;
2003 -------------------------------
2004 -- Init_Hidden_Discriminants --
2005 -------------------------------
2007 procedure Init_Hidden_Discriminants (Typ : Entity_Id; List : List_Id) is
2009 Parent_Type : Entity_Id;
2011 Discr_Val : Elmt_Id;
2014 Btype := Base_Type (Typ);
2015 while Is_Derived_Type (Btype)
2016 and then Present (Stored_Constraint (Btype))
2018 Parent_Type := Etype (Btype);
2020 Disc := First_Discriminant (Parent_Type);
2021 Discr_Val := First_Elmt (Stored_Constraint (Base_Type (Typ)));
2022 while Present (Discr_Val) loop
2024 -- Only those discriminants of the parent that are not
2025 -- renamed by discriminants of the derived type need to
2026 -- be added explicitly.
2028 if not Is_Entity_Name (Node (Discr_Val))
2029 or else Ekind (Entity (Node (Discr_Val))) /= E_Discriminant
2032 Make_Selected_Component (Loc,
2033 Prefix => New_Copy_Tree (Target),
2034 Selector_Name => New_Occurrence_Of (Disc, Loc));
2037 Make_OK_Assignment_Statement (Loc,
2039 Expression => New_Copy_Tree (Node (Discr_Val)));
2041 Set_No_Ctrl_Actions (Instr);
2042 Append_To (List, Instr);
2045 Next_Discriminant (Disc);
2046 Next_Elmt (Discr_Val);
2049 Btype := Base_Type (Parent_Type);
2051 end Init_Hidden_Discriminants;
2053 -------------------------
2054 -- Is_Int_Range_Bounds --
2055 -------------------------
2057 function Is_Int_Range_Bounds (Bounds : Node_Id) return Boolean is
2059 return Nkind (Bounds) = N_Range
2060 and then Nkind (Low_Bound (Bounds)) = N_Integer_Literal
2061 and then Nkind (High_Bound (Bounds)) = N_Integer_Literal;
2062 end Is_Int_Range_Bounds;
2064 -----------------------------------
2065 -- Generate_Finalization_Actions --
2066 -----------------------------------
2068 procedure Generate_Finalization_Actions is
2070 -- Do the work only the first time this is called
2072 if Finalization_Done then
2076 Finalization_Done := True;
2078 -- Determine the external finalization list. It is either the
2079 -- finalization list of the outer-scope or the one coming from
2080 -- an outer aggregate. When the target is not a temporary, the
2081 -- proper scope is the scope of the target rather than the
2082 -- potentially transient current scope.
2084 if Is_Controlled (Typ)
2085 and then Ancestor_Is_Subtype_Mark
2087 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
2088 Set_Assignment_OK (Ref);
2091 Make_Procedure_Call_Statement (Loc,
2094 (Find_Prim_Op (Init_Typ, Name_Initialize), Loc),
2095 Parameter_Associations => New_List (New_Copy_Tree (Ref))));
2097 end Generate_Finalization_Actions;
2099 function Rewrite_Discriminant (Expr : Node_Id) return Traverse_Result;
2100 -- If default expression of a component mentions a discriminant of the
2101 -- type, it must be rewritten as the discriminant of the target object.
2103 function Replace_Type (Expr : Node_Id) return Traverse_Result;
2104 -- If the aggregate contains a self-reference, traverse each expression
2105 -- to replace a possible self-reference with a reference to the proper
2106 -- component of the target of the assignment.
2108 --------------------------
2109 -- Rewrite_Discriminant --
2110 --------------------------
2112 function Rewrite_Discriminant (Expr : Node_Id) return Traverse_Result is
2114 if Is_Entity_Name (Expr)
2115 and then Present (Entity (Expr))
2116 and then Ekind (Entity (Expr)) = E_In_Parameter
2117 and then Present (Discriminal_Link (Entity (Expr)))
2118 and then Scope (Discriminal_Link (Entity (Expr)))
2119 = Base_Type (Etype (N))
2122 Make_Selected_Component (Loc,
2123 Prefix => New_Copy_Tree (Lhs),
2124 Selector_Name => Make_Identifier (Loc, Chars (Expr))));
2127 end Rewrite_Discriminant;
2133 function Replace_Type (Expr : Node_Id) return Traverse_Result is
2135 -- Note regarding the Root_Type test below: Aggregate components for
2136 -- self-referential types include attribute references to the current
2137 -- instance, of the form: Typ'access, etc.. These references are
2138 -- rewritten as references to the target of the aggregate: the
2139 -- left-hand side of an assignment, the entity in a declaration,
2140 -- or a temporary. Without this test, we would improperly extended
2141 -- this rewriting to attribute references whose prefix was not the
2142 -- type of the aggregate.
2144 if Nkind (Expr) = N_Attribute_Reference
2145 and then Is_Entity_Name (Prefix (Expr))
2146 and then Is_Type (Entity (Prefix (Expr)))
2147 and then Root_Type (Etype (N)) = Root_Type (Entity (Prefix (Expr)))
2149 if Is_Entity_Name (Lhs) then
2150 Rewrite (Prefix (Expr),
2151 New_Occurrence_Of (Entity (Lhs), Loc));
2153 elsif Nkind (Lhs) = N_Selected_Component then
2155 Make_Attribute_Reference (Loc,
2156 Attribute_Name => Name_Unrestricted_Access,
2157 Prefix => New_Copy_Tree (Prefix (Lhs))));
2158 Set_Analyzed (Parent (Expr), False);
2162 Make_Attribute_Reference (Loc,
2163 Attribute_Name => Name_Unrestricted_Access,
2164 Prefix => New_Copy_Tree (Lhs)));
2165 Set_Analyzed (Parent (Expr), False);
2172 procedure Replace_Self_Reference is
2173 new Traverse_Proc (Replace_Type);
2175 procedure Replace_Discriminants is
2176 new Traverse_Proc (Rewrite_Discriminant);
2178 -- Start of processing for Build_Record_Aggr_Code
2181 if Has_Self_Reference (N) then
2182 Replace_Self_Reference (N);
2185 -- If the target of the aggregate is class-wide, we must convert it
2186 -- to the actual type of the aggregate, so that the proper components
2187 -- are visible. We know already that the types are compatible.
2189 if Present (Etype (Lhs))
2190 and then Is_Class_Wide_Type (Etype (Lhs))
2192 Target := Unchecked_Convert_To (Typ, Lhs);
2197 -- Deal with the ancestor part of extension aggregates or with the
2198 -- discriminants of the root type.
2200 if Nkind (N) = N_Extension_Aggregate then
2202 Ancestor : constant Node_Id := Ancestor_Part (N);
2206 -- If the ancestor part is a subtype mark "T", we generate
2208 -- init-proc (T (tmp)); if T is constrained and
2209 -- init-proc (S (tmp)); where S applies an appropriate
2210 -- constraint if T is unconstrained
2212 if Is_Entity_Name (Ancestor)
2213 and then Is_Type (Entity (Ancestor))
2215 Ancestor_Is_Subtype_Mark := True;
2217 if Is_Constrained (Entity (Ancestor)) then
2218 Init_Typ := Entity (Ancestor);
2220 -- For an ancestor part given by an unconstrained type mark,
2221 -- create a subtype constrained by appropriate corresponding
2222 -- discriminant values coming from either associations of the
2223 -- aggregate or a constraint on a parent type. The subtype will
2224 -- be used to generate the correct default value for the
2227 elsif Has_Discriminants (Entity (Ancestor)) then
2229 Anc_Typ : constant Entity_Id := Entity (Ancestor);
2230 Anc_Constr : constant List_Id := New_List;
2231 Discrim : Entity_Id;
2232 Disc_Value : Node_Id;
2233 New_Indic : Node_Id;
2234 Subt_Decl : Node_Id;
2237 Discrim := First_Discriminant (Anc_Typ);
2238 while Present (Discrim) loop
2239 Disc_Value := Ancestor_Discriminant_Value (Discrim);
2240 Append_To (Anc_Constr, Disc_Value);
2241 Next_Discriminant (Discrim);
2245 Make_Subtype_Indication (Loc,
2246 Subtype_Mark => New_Occurrence_Of (Anc_Typ, Loc),
2248 Make_Index_Or_Discriminant_Constraint (Loc,
2249 Constraints => Anc_Constr));
2251 Init_Typ := Create_Itype (Ekind (Anc_Typ), N);
2254 Make_Subtype_Declaration (Loc,
2255 Defining_Identifier => Init_Typ,
2256 Subtype_Indication => New_Indic);
2258 -- Itypes must be analyzed with checks off Declaration
2259 -- must have a parent for proper handling of subsidiary
2262 Set_Parent (Subt_Decl, N);
2263 Analyze (Subt_Decl, Suppress => All_Checks);
2267 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
2268 Set_Assignment_OK (Ref);
2270 if not Is_Interface (Init_Typ) then
2272 Build_Initialization_Call (Loc,
2275 In_Init_Proc => Within_Init_Proc,
2276 With_Default_Init => Has_Default_Init_Comps (N)
2278 Has_Task (Base_Type (Init_Typ))));
2280 if Is_Constrained (Entity (Ancestor))
2281 and then Has_Discriminants (Entity (Ancestor))
2283 Check_Ancestor_Discriminants (Entity (Ancestor));
2287 -- Handle calls to C++ constructors
2289 elsif Is_CPP_Constructor_Call (Ancestor) then
2290 Init_Typ := Etype (Ancestor);
2291 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
2292 Set_Assignment_OK (Ref);
2295 Build_Initialization_Call (Loc,
2298 In_Init_Proc => Within_Init_Proc,
2299 With_Default_Init => Has_Default_Init_Comps (N),
2300 Constructor_Ref => Ancestor));
2302 -- Ada 2005 (AI-287): If the ancestor part is an aggregate of
2303 -- limited type, a recursive call expands the ancestor. Note that
2304 -- in the limited case, the ancestor part must be either a
2305 -- function call (possibly qualified, or wrapped in an unchecked
2306 -- conversion) or aggregate (definitely qualified).
2307 -- The ancestor part can also be a function call (that may be
2308 -- transformed into an explicit dereference) or a qualification
2311 elsif Is_Limited_Type (Etype (Ancestor))
2312 and then Nkind_In (Unqualify (Ancestor), N_Aggregate,
2313 N_Extension_Aggregate)
2315 Ancestor_Is_Expression := True;
2317 -- Set up finalization data for enclosing record, because
2318 -- controlled subcomponents of the ancestor part will be
2321 Generate_Finalization_Actions;
2324 Build_Record_Aggr_Code (
2325 N => Unqualify (Ancestor),
2326 Typ => Etype (Unqualify (Ancestor)),
2328 Is_Limited_Ancestor_Expansion => True));
2330 -- If the ancestor part is an expression "E", we generate
2334 -- In Ada 2005, this includes the case of a (possibly qualified)
2335 -- limited function call. The assignment will turn into a
2336 -- build-in-place function call (for further details, see
2337 -- Make_Build_In_Place_Call_In_Assignment).
2340 Ancestor_Is_Expression := True;
2341 Init_Typ := Etype (Ancestor);
2343 -- If the ancestor part is an aggregate, force its full
2344 -- expansion, which was delayed.
2346 if Nkind_In (Unqualify (Ancestor), N_Aggregate,
2347 N_Extension_Aggregate)
2349 Set_Analyzed (Ancestor, False);
2350 Set_Analyzed (Expression (Ancestor), False);
2353 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
2354 Set_Assignment_OK (Ref);
2356 -- Make the assignment without usual controlled actions since
2357 -- we only want the post adjust but not the pre finalize here
2358 -- Add manual adjust when necessary.
2360 Assign := New_List (
2361 Make_OK_Assignment_Statement (Loc,
2363 Expression => Ancestor));
2364 Set_No_Ctrl_Actions (First (Assign));
2366 -- Assign the tag now to make sure that the dispatching call in
2367 -- the subsequent deep_adjust works properly (unless VM_Target,
2368 -- where tags are implicit).
2370 if Tagged_Type_Expansion then
2372 Make_OK_Assignment_Statement (Loc,
2374 Make_Selected_Component (Loc,
2375 Prefix => New_Copy_Tree (Target),
2378 (First_Tag_Component (Base_Type (Typ)), Loc)),
2381 Unchecked_Convert_To (RTE (RE_Tag),
2384 (Access_Disp_Table (Base_Type (Typ)))),
2387 Set_Assignment_OK (Name (Instr));
2388 Append_To (Assign, Instr);
2390 -- Ada 2005 (AI-251): If tagged type has progenitors we must
2391 -- also initialize tags of the secondary dispatch tables.
2393 if Has_Interfaces (Base_Type (Typ)) then
2395 (Typ => Base_Type (Typ),
2397 Stmts_List => Assign);
2401 -- Call Adjust manually
2403 if Needs_Finalization (Etype (Ancestor))
2404 and then not Is_Limited_Type (Etype (Ancestor))
2408 Obj_Ref => New_Copy_Tree (Ref),
2409 Typ => Etype (Ancestor)));
2413 Make_Unsuppress_Block (Loc, Name_Discriminant_Check, Assign));
2415 if Has_Discriminants (Init_Typ) then
2416 Check_Ancestor_Discriminants (Init_Typ);
2421 -- Generate assignments of hidden assignments. If the base type is an
2422 -- unchecked union, the discriminants are unknown to the back-end and
2423 -- absent from a value of the type, so assignments for them are not
2426 if Has_Discriminants (Typ)
2427 and then not Is_Unchecked_Union (Base_Type (Typ))
2429 Init_Hidden_Discriminants (Typ, L);
2432 -- Normal case (not an extension aggregate)
2435 -- Generate the discriminant expressions, component by component.
2436 -- If the base type is an unchecked union, the discriminants are
2437 -- unknown to the back-end and absent from a value of the type, so
2438 -- assignments for them are not emitted.
2440 if Has_Discriminants (Typ)
2441 and then not Is_Unchecked_Union (Base_Type (Typ))
2443 Init_Hidden_Discriminants (Typ, L);
2445 -- Generate discriminant init values for the visible discriminants
2448 Discriminant : Entity_Id;
2449 Discriminant_Value : Node_Id;
2452 Discriminant := First_Stored_Discriminant (Typ);
2453 while Present (Discriminant) loop
2455 Make_Selected_Component (Loc,
2456 Prefix => New_Copy_Tree (Target),
2457 Selector_Name => New_Occurrence_Of (Discriminant, Loc));
2459 Discriminant_Value :=
2460 Get_Discriminant_Value (
2463 Discriminant_Constraint (N_Typ));
2466 Make_OK_Assignment_Statement (Loc,
2468 Expression => New_Copy_Tree (Discriminant_Value));
2470 Set_No_Ctrl_Actions (Instr);
2471 Append_To (L, Instr);
2473 Next_Stored_Discriminant (Discriminant);
2479 -- For CPP types we generate an implicit call to the C++ default
2480 -- constructor to ensure the proper initialization of the _Tag
2483 if Is_CPP_Class (Root_Type (Typ))
2484 and then CPP_Num_Prims (Typ) > 0
2486 Invoke_Constructor : declare
2487 CPP_Parent : constant Entity_Id :=
2488 Enclosing_CPP_Parent (Typ);
2490 procedure Invoke_IC_Proc (T : Entity_Id);
2491 -- Recursive routine used to climb to parents. Required because
2492 -- parents must be initialized before descendants to ensure
2493 -- propagation of inherited C++ slots.
2495 --------------------
2496 -- Invoke_IC_Proc --
2497 --------------------
2499 procedure Invoke_IC_Proc (T : Entity_Id) is
2501 -- Avoid generating extra calls. Initialization required
2502 -- only for types defined from the level of derivation of
2503 -- type of the constructor and the type of the aggregate.
2505 if T = CPP_Parent then
2509 Invoke_IC_Proc (Etype (T));
2511 -- Generate call to the IC routine
2513 if Present (CPP_Init_Proc (T)) then
2515 Make_Procedure_Call_Statement (Loc,
2516 New_Reference_To (CPP_Init_Proc (T), Loc)));
2520 -- Start of processing for Invoke_Constructor
2523 -- Implicit invocation of the C++ constructor
2525 if Nkind (N) = N_Aggregate then
2527 Make_Procedure_Call_Statement (Loc,
2530 (Base_Init_Proc (CPP_Parent), Loc),
2531 Parameter_Associations => New_List (
2532 Unchecked_Convert_To (CPP_Parent,
2533 New_Copy_Tree (Lhs)))));
2536 Invoke_IC_Proc (Typ);
2537 end Invoke_Constructor;
2540 -- Generate the assignments, component by component
2542 -- tmp.comp1 := Expr1_From_Aggr;
2543 -- tmp.comp2 := Expr2_From_Aggr;
2546 Comp := First (Component_Associations (N));
2547 while Present (Comp) loop
2548 Selector := Entity (First (Choices (Comp)));
2552 if Is_CPP_Constructor_Call (Expression (Comp)) then
2554 Build_Initialization_Call (Loc,
2555 Id_Ref => Make_Selected_Component (Loc,
2556 Prefix => New_Copy_Tree (Target),
2558 New_Occurrence_Of (Selector, Loc)),
2559 Typ => Etype (Selector),
2561 With_Default_Init => True,
2562 Constructor_Ref => Expression (Comp)));
2564 -- Ada 2005 (AI-287): For each default-initialized component generate
2565 -- a call to the corresponding IP subprogram if available.
2567 elsif Box_Present (Comp)
2568 and then Has_Non_Null_Base_Init_Proc (Etype (Selector))
2570 if Ekind (Selector) /= E_Discriminant then
2571 Generate_Finalization_Actions;
2574 -- Ada 2005 (AI-287): If the component type has tasks then
2575 -- generate the activation chain and master entities (except
2576 -- in case of an allocator because in that case these entities
2577 -- are generated by Build_Task_Allocate_Block_With_Init_Stmts).
2580 Ctype : constant Entity_Id := Etype (Selector);
2581 Inside_Allocator : Boolean := False;
2582 P : Node_Id := Parent (N);
2585 if Is_Task_Type (Ctype) or else Has_Task (Ctype) then
2586 while Present (P) loop
2587 if Nkind (P) = N_Allocator then
2588 Inside_Allocator := True;
2595 if not Inside_Init_Proc and not Inside_Allocator then
2596 Build_Activation_Chain_Entity (N);
2602 Build_Initialization_Call (Loc,
2603 Id_Ref => Make_Selected_Component (Loc,
2604 Prefix => New_Copy_Tree (Target),
2606 New_Occurrence_Of (Selector, Loc)),
2607 Typ => Etype (Selector),
2609 With_Default_Init => True));
2611 -- Prepare for component assignment
2613 elsif Ekind (Selector) /= E_Discriminant
2614 or else Nkind (N) = N_Extension_Aggregate
2616 -- All the discriminants have now been assigned
2618 -- This is now a good moment to initialize and attach all the
2619 -- controllers. Their position may depend on the discriminants.
2621 if Ekind (Selector) /= E_Discriminant then
2622 Generate_Finalization_Actions;
2625 Comp_Type := Underlying_Type (Etype (Selector));
2627 Make_Selected_Component (Loc,
2628 Prefix => New_Copy_Tree (Target),
2629 Selector_Name => New_Occurrence_Of (Selector, Loc));
2631 if Nkind (Expression (Comp)) = N_Qualified_Expression then
2632 Expr_Q := Expression (Expression (Comp));
2634 Expr_Q := Expression (Comp);
2637 -- Now either create the assignment or generate the code for the
2638 -- inner aggregate top-down.
2640 if Is_Delayed_Aggregate (Expr_Q) then
2642 -- We have the following case of aggregate nesting inside
2643 -- an object declaration:
2645 -- type Arr_Typ is array (Integer range <>) of ...;
2647 -- type Rec_Typ (...) is record
2648 -- Obj_Arr_Typ : Arr_Typ (A .. B);
2651 -- Obj_Rec_Typ : Rec_Typ := (...,
2652 -- Obj_Arr_Typ => (X => (...), Y => (...)));
2654 -- The length of the ranges of the aggregate and Obj_Add_Typ
2655 -- are equal (B - A = Y - X), but they do not coincide (X /=
2656 -- A and B /= Y). This case requires array sliding which is
2657 -- performed in the following manner:
2659 -- subtype Arr_Sub is Arr_Typ (X .. Y);
2661 -- Temp (X) := (...);
2663 -- Temp (Y) := (...);
2664 -- Obj_Rec_Typ.Obj_Arr_Typ := Temp;
2666 if Ekind (Comp_Type) = E_Array_Subtype
2667 and then Is_Int_Range_Bounds (Aggregate_Bounds (Expr_Q))
2668 and then Is_Int_Range_Bounds (First_Index (Comp_Type))
2670 Compatible_Int_Bounds
2671 (Agg_Bounds => Aggregate_Bounds (Expr_Q),
2672 Typ_Bounds => First_Index (Comp_Type))
2674 -- Create the array subtype with bounds equal to those of
2675 -- the corresponding aggregate.
2678 SubE : constant Entity_Id := Make_Temporary (Loc, 'T');
2680 SubD : constant Node_Id :=
2681 Make_Subtype_Declaration (Loc,
2682 Defining_Identifier => SubE,
2683 Subtype_Indication =>
2684 Make_Subtype_Indication (Loc,
2687 (Etype (Comp_Type), Loc),
2689 Make_Index_Or_Discriminant_Constraint
2691 Constraints => New_List (
2693 (Aggregate_Bounds (Expr_Q))))));
2695 -- Create a temporary array of the above subtype which
2696 -- will be used to capture the aggregate assignments.
2698 TmpE : constant Entity_Id := Make_Temporary (Loc, 'A', N);
2700 TmpD : constant Node_Id :=
2701 Make_Object_Declaration (Loc,
2702 Defining_Identifier => TmpE,
2703 Object_Definition =>
2704 New_Reference_To (SubE, Loc));
2707 Set_No_Initialization (TmpD);
2708 Append_To (L, SubD);
2709 Append_To (L, TmpD);
2711 -- Expand aggregate into assignments to the temp array
2714 Late_Expansion (Expr_Q, Comp_Type,
2715 New_Reference_To (TmpE, Loc)));
2720 Make_Assignment_Statement (Loc,
2721 Name => New_Copy_Tree (Comp_Expr),
2722 Expression => New_Reference_To (TmpE, Loc)));
2725 -- Normal case (sliding not required)
2729 Late_Expansion (Expr_Q, Comp_Type, Comp_Expr));
2732 -- Expr_Q is not delayed aggregate
2735 if Has_Discriminants (Typ) then
2736 Replace_Discriminants (Expr_Q);
2740 Make_OK_Assignment_Statement (Loc,
2742 Expression => Expr_Q);
2744 Set_No_Ctrl_Actions (Instr);
2745 Append_To (L, Instr);
2747 -- Adjust the tag if tagged (because of possible view
2748 -- conversions), unless compiling for a VM where tags are
2751 -- tmp.comp._tag := comp_typ'tag;
2753 if Is_Tagged_Type (Comp_Type)
2754 and then Tagged_Type_Expansion
2757 Make_OK_Assignment_Statement (Loc,
2759 Make_Selected_Component (Loc,
2760 Prefix => New_Copy_Tree (Comp_Expr),
2763 (First_Tag_Component (Comp_Type), Loc)),
2766 Unchecked_Convert_To (RTE (RE_Tag),
2768 (Node (First_Elmt (Access_Disp_Table (Comp_Type))),
2771 Append_To (L, Instr);
2775 -- Adjust (tmp.comp);
2777 if Needs_Finalization (Comp_Type)
2778 and then not Is_Limited_Type (Comp_Type)
2782 Obj_Ref => New_Copy_Tree (Comp_Expr),
2789 elsif Ekind (Selector) = E_Discriminant
2790 and then Nkind (N) /= N_Extension_Aggregate
2791 and then Nkind (Parent (N)) = N_Component_Association
2792 and then Is_Constrained (Typ)
2794 -- We must check that the discriminant value imposed by the
2795 -- context is the same as the value given in the subaggregate,
2796 -- because after the expansion into assignments there is no
2797 -- record on which to perform a regular discriminant check.
2804 D_Val := First_Elmt (Discriminant_Constraint (Typ));
2805 Disc := First_Discriminant (Typ);
2806 while Chars (Disc) /= Chars (Selector) loop
2807 Next_Discriminant (Disc);
2811 pragma Assert (Present (D_Val));
2813 -- This check cannot performed for components that are
2814 -- constrained by a current instance, because this is not a
2815 -- value that can be compared with the actual constraint.
2817 if Nkind (Node (D_Val)) /= N_Attribute_Reference
2818 or else not Is_Entity_Name (Prefix (Node (D_Val)))
2819 or else not Is_Type (Entity (Prefix (Node (D_Val))))
2822 Make_Raise_Constraint_Error (Loc,
2825 Left_Opnd => New_Copy_Tree (Node (D_Val)),
2826 Right_Opnd => Expression (Comp)),
2827 Reason => CE_Discriminant_Check_Failed));
2830 -- Find self-reference in previous discriminant assignment,
2831 -- and replace with proper expression.
2838 while Present (Ass) loop
2839 if Nkind (Ass) = N_Assignment_Statement
2840 and then Nkind (Name (Ass)) = N_Selected_Component
2841 and then Chars (Selector_Name (Name (Ass))) =
2845 (Ass, New_Copy_Tree (Expression (Comp)));
2858 -- If the type is tagged, the tag needs to be initialized (unless
2859 -- compiling for the Java VM where tags are implicit). It is done
2860 -- late in the initialization process because in some cases, we call
2861 -- the init proc of an ancestor which will not leave out the right tag
2863 if Ancestor_Is_Expression then
2866 -- For CPP types we generated a call to the C++ default constructor
2867 -- before the components have been initialized to ensure the proper
2868 -- initialization of the _Tag component (see above).
2870 elsif Is_CPP_Class (Typ) then
2873 elsif Is_Tagged_Type (Typ) and then Tagged_Type_Expansion then
2875 Make_OK_Assignment_Statement (Loc,
2877 Make_Selected_Component (Loc,
2878 Prefix => New_Copy_Tree (Target),
2881 (First_Tag_Component (Base_Type (Typ)), Loc)),
2884 Unchecked_Convert_To (RTE (RE_Tag),
2886 (Node (First_Elmt (Access_Disp_Table (Base_Type (Typ)))),
2889 Append_To (L, Instr);
2891 -- Ada 2005 (AI-251): If the tagged type has been derived from
2892 -- abstract interfaces we must also initialize the tags of the
2893 -- secondary dispatch tables.
2895 if Has_Interfaces (Base_Type (Typ)) then
2897 (Typ => Base_Type (Typ),
2903 -- If the controllers have not been initialized yet (by lack of non-
2904 -- discriminant components), let's do it now.
2906 Generate_Finalization_Actions;
2909 end Build_Record_Aggr_Code;
2911 -------------------------------
2912 -- Convert_Aggr_In_Allocator --
2913 -------------------------------
2915 procedure Convert_Aggr_In_Allocator
2920 Loc : constant Source_Ptr := Sloc (Aggr);
2921 Typ : constant Entity_Id := Etype (Aggr);
2922 Temp : constant Entity_Id := Defining_Identifier (Decl);
2924 Occ : constant Node_Id :=
2925 Unchecked_Convert_To (Typ,
2926 Make_Explicit_Dereference (Loc,
2927 New_Reference_To (Temp, Loc)));
2930 if Is_Array_Type (Typ) then
2931 Convert_Array_Aggr_In_Allocator (Decl, Aggr, Occ);
2933 elsif Has_Default_Init_Comps (Aggr) then
2935 L : constant List_Id := New_List;
2936 Init_Stmts : List_Id;
2939 Init_Stmts := Late_Expansion (Aggr, Typ, Occ);
2941 if Has_Task (Typ) then
2942 Build_Task_Allocate_Block_With_Init_Stmts (L, Aggr, Init_Stmts);
2943 Insert_Actions (Alloc, L);
2945 Insert_Actions (Alloc, Init_Stmts);
2950 Insert_Actions (Alloc, Late_Expansion (Aggr, Typ, Occ));
2952 end Convert_Aggr_In_Allocator;
2954 --------------------------------
2955 -- Convert_Aggr_In_Assignment --
2956 --------------------------------
2958 procedure Convert_Aggr_In_Assignment (N : Node_Id) is
2959 Aggr : Node_Id := Expression (N);
2960 Typ : constant Entity_Id := Etype (Aggr);
2961 Occ : constant Node_Id := New_Copy_Tree (Name (N));
2964 if Nkind (Aggr) = N_Qualified_Expression then
2965 Aggr := Expression (Aggr);
2968 Insert_Actions_After (N, Late_Expansion (Aggr, Typ, Occ));
2969 end Convert_Aggr_In_Assignment;
2971 ---------------------------------
2972 -- Convert_Aggr_In_Object_Decl --
2973 ---------------------------------
2975 procedure Convert_Aggr_In_Object_Decl (N : Node_Id) is
2976 Obj : constant Entity_Id := Defining_Identifier (N);
2977 Aggr : Node_Id := Expression (N);
2978 Loc : constant Source_Ptr := Sloc (Aggr);
2979 Typ : constant Entity_Id := Etype (Aggr);
2980 Occ : constant Node_Id := New_Occurrence_Of (Obj, Loc);
2982 function Discriminants_Ok return Boolean;
2983 -- If the object type is constrained, the discriminants in the
2984 -- aggregate must be checked against the discriminants of the subtype.
2985 -- This cannot be done using Apply_Discriminant_Checks because after
2986 -- expansion there is no aggregate left to check.
2988 ----------------------
2989 -- Discriminants_Ok --
2990 ----------------------
2992 function Discriminants_Ok return Boolean is
2993 Cond : Node_Id := Empty;
3002 D := First_Discriminant (Typ);
3003 Disc1 := First_Elmt (Discriminant_Constraint (Typ));
3004 Disc2 := First_Elmt (Discriminant_Constraint (Etype (Obj)));
3005 while Present (Disc1) and then Present (Disc2) loop
3006 Val1 := Node (Disc1);
3007 Val2 := Node (Disc2);
3009 if not Is_OK_Static_Expression (Val1)
3010 or else not Is_OK_Static_Expression (Val2)
3012 Check := Make_Op_Ne (Loc,
3013 Left_Opnd => Duplicate_Subexpr (Val1),
3014 Right_Opnd => Duplicate_Subexpr (Val2));
3020 Cond := Make_Or_Else (Loc,
3022 Right_Opnd => Check);
3025 elsif Expr_Value (Val1) /= Expr_Value (Val2) then
3026 Apply_Compile_Time_Constraint_Error (Aggr,
3027 Msg => "incorrect value for discriminant&?",
3028 Reason => CE_Discriminant_Check_Failed,
3033 Next_Discriminant (D);
3038 -- If any discriminant constraint is non-static, emit a check
3040 if Present (Cond) then
3042 Make_Raise_Constraint_Error (Loc,
3044 Reason => CE_Discriminant_Check_Failed));
3048 end Discriminants_Ok;
3050 -- Start of processing for Convert_Aggr_In_Object_Decl
3053 Set_Assignment_OK (Occ);
3055 if Nkind (Aggr) = N_Qualified_Expression then
3056 Aggr := Expression (Aggr);
3059 if Has_Discriminants (Typ)
3060 and then Typ /= Etype (Obj)
3061 and then Is_Constrained (Etype (Obj))
3062 and then not Discriminants_Ok
3067 -- If the context is an extended return statement, it has its own
3068 -- finalization machinery (i.e. works like a transient scope) and
3069 -- we do not want to create an additional one, because objects on
3070 -- the finalization list of the return must be moved to the caller's
3071 -- finalization list to complete the return.
3073 -- However, if the aggregate is limited, it is built in place, and the
3074 -- controlled components are not assigned to intermediate temporaries
3075 -- so there is no need for a transient scope in this case either.
3077 if Requires_Transient_Scope (Typ)
3078 and then Ekind (Current_Scope) /= E_Return_Statement
3079 and then not Is_Limited_Type (Typ)
3081 Establish_Transient_Scope
3084 Is_Controlled (Typ) or else Has_Controlled_Component (Typ));
3087 Insert_Actions_After (N, Late_Expansion (Aggr, Typ, Occ));
3088 Set_No_Initialization (N);
3089 Initialize_Discriminants (N, Typ);
3090 end Convert_Aggr_In_Object_Decl;
3092 -------------------------------------
3093 -- Convert_Array_Aggr_In_Allocator --
3094 -------------------------------------
3096 procedure Convert_Array_Aggr_In_Allocator
3101 Aggr_Code : List_Id;
3102 Typ : constant Entity_Id := Etype (Aggr);
3103 Ctyp : constant Entity_Id := Component_Type (Typ);
3106 -- The target is an explicit dereference of the allocated object.
3107 -- Generate component assignments to it, as for an aggregate that
3108 -- appears on the right-hand side of an assignment statement.
3111 Build_Array_Aggr_Code (Aggr,
3113 Index => First_Index (Typ),
3115 Scalar_Comp => Is_Scalar_Type (Ctyp));
3117 Insert_Actions_After (Decl, Aggr_Code);
3118 end Convert_Array_Aggr_In_Allocator;
3120 ----------------------------
3121 -- Convert_To_Assignments --
3122 ----------------------------
3124 procedure Convert_To_Assignments (N : Node_Id; Typ : Entity_Id) is
3125 Loc : constant Source_Ptr := Sloc (N);
3130 Target_Expr : Node_Id;
3131 Parent_Kind : Node_Kind;
3132 Unc_Decl : Boolean := False;
3133 Parent_Node : Node_Id;
3136 pragma Assert (not Is_Static_Dispatch_Table_Aggregate (N));
3137 pragma Assert (Is_Record_Type (Typ));
3139 Parent_Node := Parent (N);
3140 Parent_Kind := Nkind (Parent_Node);
3142 if Parent_Kind = N_Qualified_Expression then
3144 -- Check if we are in a unconstrained declaration because in this
3145 -- case the current delayed expansion mechanism doesn't work when
3146 -- the declared object size depend on the initializing expr.
3149 Parent_Node := Parent (Parent_Node);
3150 Parent_Kind := Nkind (Parent_Node);
3152 if Parent_Kind = N_Object_Declaration then
3154 not Is_Entity_Name (Object_Definition (Parent_Node))
3155 or else Has_Discriminants
3156 (Entity (Object_Definition (Parent_Node)))
3157 or else Is_Class_Wide_Type
3158 (Entity (Object_Definition (Parent_Node)));
3163 -- Just set the Delay flag in the cases where the transformation will be
3164 -- done top down from above.
3168 -- Internal aggregate (transformed when expanding the parent)
3170 or else Parent_Kind = N_Aggregate
3171 or else Parent_Kind = N_Extension_Aggregate
3172 or else Parent_Kind = N_Component_Association
3174 -- Allocator (see Convert_Aggr_In_Allocator)
3176 or else Parent_Kind = N_Allocator
3178 -- Object declaration (see Convert_Aggr_In_Object_Decl)
3180 or else (Parent_Kind = N_Object_Declaration and then not Unc_Decl)
3182 -- Safe assignment (see Convert_Aggr_Assignments). So far only the
3183 -- assignments in init procs are taken into account.
3185 or else (Parent_Kind = N_Assignment_Statement
3186 and then Inside_Init_Proc)
3188 -- (Ada 2005) An inherently limited type in a return statement,
3189 -- which will be handled in a build-in-place fashion, and may be
3190 -- rewritten as an extended return and have its own finalization
3191 -- machinery. In the case of a simple return, the aggregate needs
3192 -- to be delayed until the scope for the return statement has been
3193 -- created, so that any finalization chain will be associated with
3194 -- that scope. For extended returns, we delay expansion to avoid the
3195 -- creation of an unwanted transient scope that could result in
3196 -- premature finalization of the return object (which is built in
3197 -- in place within the caller's scope).
3200 (Is_Immutably_Limited_Type (Typ)
3202 (Nkind (Parent (Parent_Node)) = N_Extended_Return_Statement
3203 or else Nkind (Parent_Node) = N_Simple_Return_Statement))
3205 Set_Expansion_Delayed (N);
3209 if Requires_Transient_Scope (Typ) then
3210 Establish_Transient_Scope
3212 Is_Controlled (Typ) or else Has_Controlled_Component (Typ));
3215 -- If the aggregate is non-limited, create a temporary. If it is limited
3216 -- and the context is an assignment, this is a subaggregate for an
3217 -- enclosing aggregate being expanded. It must be built in place, so use
3218 -- the target of the current assignment.
3220 if Is_Limited_Type (Typ)
3221 and then Nkind (Parent (N)) = N_Assignment_Statement
3223 Target_Expr := New_Copy_Tree (Name (Parent (N)));
3224 Insert_Actions (Parent (N),
3225 Build_Record_Aggr_Code (N, Typ, Target_Expr));
3226 Rewrite (Parent (N), Make_Null_Statement (Loc));
3229 Temp := Make_Temporary (Loc, 'A', N);
3231 -- If the type inherits unknown discriminants, use the view with
3232 -- known discriminants if available.
3234 if Has_Unknown_Discriminants (Typ)
3235 and then Present (Underlying_Record_View (Typ))
3237 T := Underlying_Record_View (Typ);
3243 Make_Object_Declaration (Loc,
3244 Defining_Identifier => Temp,
3245 Object_Definition => New_Occurrence_Of (T, Loc));
3247 Set_No_Initialization (Instr);
3248 Insert_Action (N, Instr);
3249 Initialize_Discriminants (Instr, T);
3250 Target_Expr := New_Occurrence_Of (Temp, Loc);
3251 Insert_Actions (N, Build_Record_Aggr_Code (N, T, Target_Expr));
3252 Rewrite (N, New_Occurrence_Of (Temp, Loc));
3253 Analyze_And_Resolve (N, T);
3255 end Convert_To_Assignments;
3257 ---------------------------
3258 -- Convert_To_Positional --
3259 ---------------------------
3261 procedure Convert_To_Positional
3263 Max_Others_Replicate : Nat := 5;
3264 Handle_Bit_Packed : Boolean := False)
3266 Typ : constant Entity_Id := Etype (N);
3268 Static_Components : Boolean := True;
3270 procedure Check_Static_Components;
3271 -- Check whether all components of the aggregate are compile-time known
3272 -- values, and can be passed as is to the back-end without further
3278 Ixb : Node_Id) return Boolean;
3279 -- Convert the aggregate into a purely positional form if possible. On
3280 -- entry the bounds of all dimensions are known to be static, and the
3281 -- total number of components is safe enough to expand.
3283 function Is_Flat (N : Node_Id; Dims : Int) return Boolean;
3284 -- Return True iff the array N is flat (which is not trivial in the case
3285 -- of multidimensional aggregates).
3287 -----------------------------
3288 -- Check_Static_Components --
3289 -----------------------------
3291 procedure Check_Static_Components is
3295 Static_Components := True;
3297 if Nkind (N) = N_String_Literal then
3300 elsif Present (Expressions (N)) then
3301 Expr := First (Expressions (N));
3302 while Present (Expr) loop
3303 if Nkind (Expr) /= N_Aggregate
3304 or else not Compile_Time_Known_Aggregate (Expr)
3305 or else Expansion_Delayed (Expr)
3307 Static_Components := False;
3315 if Nkind (N) = N_Aggregate
3316 and then Present (Component_Associations (N))
3318 Expr := First (Component_Associations (N));
3319 while Present (Expr) loop
3320 if Nkind_In (Expression (Expr), N_Integer_Literal,
3325 elsif Is_Entity_Name (Expression (Expr))
3326 and then Present (Entity (Expression (Expr)))
3327 and then Ekind (Entity (Expression (Expr))) =
3328 E_Enumeration_Literal
3332 elsif Nkind (Expression (Expr)) /= N_Aggregate
3333 or else not Compile_Time_Known_Aggregate (Expression (Expr))
3334 or else Expansion_Delayed (Expression (Expr))
3336 Static_Components := False;
3343 end Check_Static_Components;
3352 Ixb : Node_Id) return Boolean
3354 Loc : constant Source_Ptr := Sloc (N);
3355 Blo : constant Node_Id := Type_Low_Bound (Etype (Ixb));
3356 Lo : constant Node_Id := Type_Low_Bound (Etype (Ix));
3357 Hi : constant Node_Id := Type_High_Bound (Etype (Ix));
3361 Others_Present : Boolean := False;
3364 if Nkind (Original_Node (N)) = N_String_Literal then
3368 if not Compile_Time_Known_Value (Lo)
3369 or else not Compile_Time_Known_Value (Hi)
3374 Lov := Expr_Value (Lo);
3375 Hiv := Expr_Value (Hi);
3377 -- Check if there is an others choice
3379 if Present (Component_Associations (N)) then
3385 Assoc := First (Component_Associations (N));
3386 while Present (Assoc) loop
3387 Choice := First (Choices (Assoc));
3389 while Present (Choice) loop
3390 if Nkind (Choice) = N_Others_Choice then
3391 Others_Present := True;
3402 -- If the low bound is not known at compile time and others is not
3403 -- present we can proceed since the bounds can be obtained from the
3406 -- Note: This case is required in VM platforms since their backends
3407 -- normalize array indexes in the range 0 .. N-1. Hence, if we do
3408 -- not flat an array whose bounds cannot be obtained from the type
3409 -- of the index the backend has no way to properly generate the code.
3410 -- See ACATS c460010 for an example.
3413 or else (not Compile_Time_Known_Value (Blo)
3414 and then Others_Present)
3419 -- Determine if set of alternatives is suitable for conversion and
3420 -- build an array containing the values in sequence.
3423 Vals : array (UI_To_Int (Lov) .. UI_To_Int (Hiv))
3424 of Node_Id := (others => Empty);
3425 -- The values in the aggregate sorted appropriately
3428 -- Same data as Vals in list form
3431 -- Used to validate Max_Others_Replicate limit
3434 Num : Int := UI_To_Int (Lov);
3440 if Present (Expressions (N)) then
3441 Elmt := First (Expressions (N));
3442 while Present (Elmt) loop
3443 if Nkind (Elmt) = N_Aggregate
3444 and then Present (Next_Index (Ix))
3446 not Flatten (Elmt, Next_Index (Ix), Next_Index (Ixb))
3451 Vals (Num) := Relocate_Node (Elmt);
3458 if No (Component_Associations (N)) then
3462 Elmt := First (Component_Associations (N));
3464 if Nkind (Expression (Elmt)) = N_Aggregate then
3465 if Present (Next_Index (Ix))
3468 (Expression (Elmt), Next_Index (Ix), Next_Index (Ixb))
3474 Component_Loop : while Present (Elmt) loop
3475 Choice := First (Choices (Elmt));
3476 Choice_Loop : while Present (Choice) loop
3478 -- If we have an others choice, fill in the missing elements
3479 -- subject to the limit established by Max_Others_Replicate.
3481 if Nkind (Choice) = N_Others_Choice then
3484 for J in Vals'Range loop
3485 if No (Vals (J)) then
3486 Vals (J) := New_Copy_Tree (Expression (Elmt));
3487 Rep_Count := Rep_Count + 1;
3489 -- Check for maximum others replication. Note that
3490 -- we skip this test if either of the restrictions
3491 -- No_Elaboration_Code or No_Implicit_Loops is
3492 -- active, if this is a preelaborable unit or a
3493 -- predefined unit. This ensures that predefined
3494 -- units get the same level of constant folding in
3495 -- Ada 95 and Ada 05, where their categorization
3499 P : constant Entity_Id :=
3500 Cunit_Entity (Current_Sem_Unit);
3503 -- Check if duplication OK and if so continue
3506 if Restriction_Active (No_Elaboration_Code)
3507 or else Restriction_Active (No_Implicit_Loops)
3508 or else Is_Preelaborated (P)
3509 or else (Ekind (P) = E_Package_Body
3511 Is_Preelaborated (Spec_Entity (P)))
3513 Is_Predefined_File_Name
3514 (Unit_File_Name (Get_Source_Unit (P)))
3518 -- If duplication not OK, then we return False
3519 -- if the replication count is too high
3521 elsif Rep_Count > Max_Others_Replicate then
3524 -- Continue on if duplication not OK, but the
3525 -- replication count is not excessive.
3534 exit Component_Loop;
3536 -- Case of a subtype mark, identifier or expanded name
3538 elsif Is_Entity_Name (Choice)
3539 and then Is_Type (Entity (Choice))
3541 Lo := Type_Low_Bound (Etype (Choice));
3542 Hi := Type_High_Bound (Etype (Choice));
3544 -- Case of subtype indication
3546 elsif Nkind (Choice) = N_Subtype_Indication then
3547 Lo := Low_Bound (Range_Expression (Constraint (Choice)));
3548 Hi := High_Bound (Range_Expression (Constraint (Choice)));
3552 elsif Nkind (Choice) = N_Range then
3553 Lo := Low_Bound (Choice);
3554 Hi := High_Bound (Choice);
3556 -- Normal subexpression case
3558 else pragma Assert (Nkind (Choice) in N_Subexpr);
3559 if not Compile_Time_Known_Value (Choice) then
3563 Choice_Index := UI_To_Int (Expr_Value (Choice));
3564 if Choice_Index in Vals'Range then
3565 Vals (Choice_Index) :=
3566 New_Copy_Tree (Expression (Elmt));
3570 -- Choice is statically out-of-range, will be
3571 -- rewritten to raise Constraint_Error.
3578 -- Range cases merge with Lo,Hi set
3580 if not Compile_Time_Known_Value (Lo)
3582 not Compile_Time_Known_Value (Hi)
3586 for J in UI_To_Int (Expr_Value (Lo)) ..
3587 UI_To_Int (Expr_Value (Hi))
3589 Vals (J) := New_Copy_Tree (Expression (Elmt));
3595 end loop Choice_Loop;
3598 end loop Component_Loop;
3600 -- If we get here the conversion is possible
3603 for J in Vals'Range loop
3604 Append (Vals (J), Vlist);
3607 Rewrite (N, Make_Aggregate (Loc, Expressions => Vlist));
3608 Set_Aggregate_Bounds (N, Aggregate_Bounds (Original_Node (N)));
3617 function Is_Flat (N : Node_Id; Dims : Int) return Boolean is
3624 elsif Nkind (N) = N_Aggregate then
3625 if Present (Component_Associations (N)) then
3629 Elmt := First (Expressions (N));
3630 while Present (Elmt) loop
3631 if not Is_Flat (Elmt, Dims - 1) then
3645 -- Start of processing for Convert_To_Positional
3648 -- Ada 2005 (AI-287): Do not convert in case of default initialized
3649 -- components because in this case will need to call the corresponding
3652 if Has_Default_Init_Comps (N) then
3656 if Is_Flat (N, Number_Dimensions (Typ)) then
3660 if Is_Bit_Packed_Array (Typ)
3661 and then not Handle_Bit_Packed
3666 -- Do not convert to positional if controlled components are involved
3667 -- since these require special processing
3669 if Has_Controlled_Component (Typ) then
3673 Check_Static_Components;
3675 -- If the size is known, or all the components are static, try to
3676 -- build a fully positional aggregate.
3678 -- The size of the type may not be known for an aggregate with
3679 -- discriminated array components, but if the components are static
3680 -- it is still possible to verify statically that the length is
3681 -- compatible with the upper bound of the type, and therefore it is
3682 -- worth flattening such aggregates as well.
3684 -- For now the back-end expands these aggregates into individual
3685 -- assignments to the target anyway, but it is conceivable that
3686 -- it will eventually be able to treat such aggregates statically???
3688 if Aggr_Size_OK (N, Typ)
3689 and then Flatten (N, First_Index (Typ), First_Index (Base_Type (Typ)))
3691 if Static_Components then
3692 Set_Compile_Time_Known_Aggregate (N);
3693 Set_Expansion_Delayed (N, False);
3696 Analyze_And_Resolve (N, Typ);
3698 end Convert_To_Positional;
3700 ----------------------------
3701 -- Expand_Array_Aggregate --
3702 ----------------------------
3704 -- Array aggregate expansion proceeds as follows:
3706 -- 1. If requested we generate code to perform all the array aggregate
3707 -- bound checks, specifically
3709 -- (a) Check that the index range defined by aggregate bounds is
3710 -- compatible with corresponding index subtype.
3712 -- (b) If an others choice is present check that no aggregate
3713 -- index is outside the bounds of the index constraint.
3715 -- (c) For multidimensional arrays make sure that all subaggregates
3716 -- corresponding to the same dimension have the same bounds.
3718 -- 2. Check for packed array aggregate which can be converted to a
3719 -- constant so that the aggregate disappeares completely.
3721 -- 3. Check case of nested aggregate. Generally nested aggregates are
3722 -- handled during the processing of the parent aggregate.
3724 -- 4. Check if the aggregate can be statically processed. If this is the
3725 -- case pass it as is to Gigi. Note that a necessary condition for
3726 -- static processing is that the aggregate be fully positional.
3728 -- 5. If in place aggregate expansion is possible (i.e. no need to create
3729 -- a temporary) then mark the aggregate as such and return. Otherwise
3730 -- create a new temporary and generate the appropriate initialization
3733 procedure Expand_Array_Aggregate (N : Node_Id) is
3734 Loc : constant Source_Ptr := Sloc (N);
3736 Typ : constant Entity_Id := Etype (N);
3737 Ctyp : constant Entity_Id := Component_Type (Typ);
3738 -- Typ is the correct constrained array subtype of the aggregate
3739 -- Ctyp is the corresponding component type.
3741 Aggr_Dimension : constant Pos := Number_Dimensions (Typ);
3742 -- Number of aggregate index dimensions
3744 Aggr_Low : array (1 .. Aggr_Dimension) of Node_Id;
3745 Aggr_High : array (1 .. Aggr_Dimension) of Node_Id;
3746 -- Low and High bounds of the constraint for each aggregate index
3748 Aggr_Index_Typ : array (1 .. Aggr_Dimension) of Entity_Id;
3749 -- The type of each index
3751 Maybe_In_Place_OK : Boolean;
3752 -- If the type is neither controlled nor packed and the aggregate
3753 -- is the expression in an assignment, assignment in place may be
3754 -- possible, provided other conditions are met on the LHS.
3756 Others_Present : array (1 .. Aggr_Dimension) of Boolean :=
3758 -- If Others_Present (J) is True, then there is an others choice
3759 -- in one of the sub-aggregates of N at dimension J.
3761 procedure Build_Constrained_Type (Positional : Boolean);
3762 -- If the subtype is not static or unconstrained, build a constrained
3763 -- type using the computable sizes of the aggregate and its sub-
3766 procedure Check_Bounds (Aggr_Bounds : Node_Id; Index_Bounds : Node_Id);
3767 -- Checks that the bounds of Aggr_Bounds are within the bounds defined
3770 procedure Check_Same_Aggr_Bounds (Sub_Aggr : Node_Id; Dim : Pos);
3771 -- Checks that in a multi-dimensional array aggregate all subaggregates
3772 -- corresponding to the same dimension have the same bounds.
3773 -- Sub_Aggr is an array sub-aggregate. Dim is the dimension
3774 -- corresponding to the sub-aggregate.
3776 procedure Compute_Others_Present (Sub_Aggr : Node_Id; Dim : Pos);
3777 -- Computes the values of array Others_Present. Sub_Aggr is the
3778 -- array sub-aggregate we start the computation from. Dim is the
3779 -- dimension corresponding to the sub-aggregate.
3781 function In_Place_Assign_OK return Boolean;
3782 -- Simple predicate to determine whether an aggregate assignment can
3783 -- be done in place, because none of the new values can depend on the
3784 -- components of the target of the assignment.
3786 procedure Others_Check (Sub_Aggr : Node_Id; Dim : Pos);
3787 -- Checks that if an others choice is present in any sub-aggregate no
3788 -- aggregate index is outside the bounds of the index constraint.
3789 -- Sub_Aggr is an array sub-aggregate. Dim is the dimension
3790 -- corresponding to the sub-aggregate.
3792 function Safe_Left_Hand_Side (N : Node_Id) return Boolean;
3793 -- In addition to Maybe_In_Place_OK, in order for an aggregate to be
3794 -- built directly into the target of the assignment it must be free
3797 ----------------------------
3798 -- Build_Constrained_Type --
3799 ----------------------------
3801 procedure Build_Constrained_Type (Positional : Boolean) is
3802 Loc : constant Source_Ptr := Sloc (N);
3803 Agg_Type : constant Entity_Id := Make_Temporary (Loc, 'A');
3806 Typ : constant Entity_Id := Etype (N);
3807 Indexes : constant List_Id := New_List;
3812 -- If the aggregate is purely positional, all its subaggregates
3813 -- have the same size. We collect the dimensions from the first
3814 -- subaggregate at each level.
3819 for D in 1 .. Number_Dimensions (Typ) loop
3820 Sub_Agg := First (Expressions (Sub_Agg));
3824 while Present (Comp) loop
3831 Low_Bound => Make_Integer_Literal (Loc, 1),
3832 High_Bound => Make_Integer_Literal (Loc, Num)));
3836 -- We know the aggregate type is unconstrained and the aggregate
3837 -- is not processable by the back end, therefore not necessarily
3838 -- positional. Retrieve each dimension bounds (computed earlier).
3840 for D in 1 .. Number_Dimensions (Typ) loop
3843 Low_Bound => Aggr_Low (D),
3844 High_Bound => Aggr_High (D)),
3850 Make_Full_Type_Declaration (Loc,
3851 Defining_Identifier => Agg_Type,
3853 Make_Constrained_Array_Definition (Loc,
3854 Discrete_Subtype_Definitions => Indexes,
3855 Component_Definition =>
3856 Make_Component_Definition (Loc,
3857 Aliased_Present => False,
3858 Subtype_Indication =>
3859 New_Occurrence_Of (Component_Type (Typ), Loc))));
3861 Insert_Action (N, Decl);
3863 Set_Etype (N, Agg_Type);
3864 Set_Is_Itype (Agg_Type);
3865 Freeze_Itype (Agg_Type, N);
3866 end Build_Constrained_Type;
3872 procedure Check_Bounds (Aggr_Bounds : Node_Id; Index_Bounds : Node_Id) is
3879 Cond : Node_Id := Empty;
3882 Get_Index_Bounds (Aggr_Bounds, Aggr_Lo, Aggr_Hi);
3883 Get_Index_Bounds (Index_Bounds, Ind_Lo, Ind_Hi);
3885 -- Generate the following test:
3887 -- [constraint_error when
3888 -- Aggr_Lo <= Aggr_Hi and then
3889 -- (Aggr_Lo < Ind_Lo or else Aggr_Hi > Ind_Hi)]
3891 -- As an optimization try to see if some tests are trivially vacuous
3892 -- because we are comparing an expression against itself.
3894 if Aggr_Lo = Ind_Lo and then Aggr_Hi = Ind_Hi then
3897 elsif Aggr_Hi = Ind_Hi then
3900 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
3901 Right_Opnd => Duplicate_Subexpr_Move_Checks (Ind_Lo));
3903 elsif Aggr_Lo = Ind_Lo then
3906 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi),
3907 Right_Opnd => Duplicate_Subexpr_Move_Checks (Ind_Hi));
3914 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
3915 Right_Opnd => Duplicate_Subexpr_Move_Checks (Ind_Lo)),
3919 Left_Opnd => Duplicate_Subexpr (Aggr_Hi),
3920 Right_Opnd => Duplicate_Subexpr (Ind_Hi)));
3923 if Present (Cond) then
3928 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
3929 Right_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi)),
3931 Right_Opnd => Cond);
3933 Set_Analyzed (Left_Opnd (Left_Opnd (Cond)), False);
3934 Set_Analyzed (Right_Opnd (Left_Opnd (Cond)), False);
3936 Make_Raise_Constraint_Error (Loc,
3938 Reason => CE_Length_Check_Failed));
3942 ----------------------------
3943 -- Check_Same_Aggr_Bounds --
3944 ----------------------------
3946 procedure Check_Same_Aggr_Bounds (Sub_Aggr : Node_Id; Dim : Pos) is
3947 Sub_Lo : constant Node_Id := Low_Bound (Aggregate_Bounds (Sub_Aggr));
3948 Sub_Hi : constant Node_Id := High_Bound (Aggregate_Bounds (Sub_Aggr));
3949 -- The bounds of this specific sub-aggregate
3951 Aggr_Lo : constant Node_Id := Aggr_Low (Dim);
3952 Aggr_Hi : constant Node_Id := Aggr_High (Dim);
3953 -- The bounds of the aggregate for this dimension
3955 Ind_Typ : constant Entity_Id := Aggr_Index_Typ (Dim);
3956 -- The index type for this dimension.xxx
3958 Cond : Node_Id := Empty;
3963 -- If index checks are on generate the test
3965 -- [constraint_error when
3966 -- Aggr_Lo /= Sub_Lo or else Aggr_Hi /= Sub_Hi]
3968 -- As an optimization try to see if some tests are trivially vacuos
3969 -- because we are comparing an expression against itself. Also for
3970 -- the first dimension the test is trivially vacuous because there
3971 -- is just one aggregate for dimension 1.
3973 if Index_Checks_Suppressed (Ind_Typ) then
3977 or else (Aggr_Lo = Sub_Lo and then Aggr_Hi = Sub_Hi)
3981 elsif Aggr_Hi = Sub_Hi then
3984 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
3985 Right_Opnd => Duplicate_Subexpr_Move_Checks (Sub_Lo));
3987 elsif Aggr_Lo = Sub_Lo then
3990 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi),
3991 Right_Opnd => Duplicate_Subexpr_Move_Checks (Sub_Hi));
3998 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
3999 Right_Opnd => Duplicate_Subexpr_Move_Checks (Sub_Lo)),
4003 Left_Opnd => Duplicate_Subexpr (Aggr_Hi),
4004 Right_Opnd => Duplicate_Subexpr (Sub_Hi)));
4007 if Present (Cond) then
4009 Make_Raise_Constraint_Error (Loc,
4011 Reason => CE_Length_Check_Failed));
4014 -- Now look inside the sub-aggregate to see if there is more work
4016 if Dim < Aggr_Dimension then
4018 -- Process positional components
4020 if Present (Expressions (Sub_Aggr)) then
4021 Expr := First (Expressions (Sub_Aggr));
4022 while Present (Expr) loop
4023 Check_Same_Aggr_Bounds (Expr, Dim + 1);
4028 -- Process component associations
4030 if Present (Component_Associations (Sub_Aggr)) then
4031 Assoc := First (Component_Associations (Sub_Aggr));
4032 while Present (Assoc) loop
4033 Expr := Expression (Assoc);
4034 Check_Same_Aggr_Bounds (Expr, Dim + 1);
4039 end Check_Same_Aggr_Bounds;
4041 ----------------------------
4042 -- Compute_Others_Present --
4043 ----------------------------
4045 procedure Compute_Others_Present (Sub_Aggr : Node_Id; Dim : Pos) is
4050 if Present (Component_Associations (Sub_Aggr)) then
4051 Assoc := Last (Component_Associations (Sub_Aggr));
4053 if Nkind (First (Choices (Assoc))) = N_Others_Choice then
4054 Others_Present (Dim) := True;
4058 -- Now look inside the sub-aggregate to see if there is more work
4060 if Dim < Aggr_Dimension then
4062 -- Process positional components
4064 if Present (Expressions (Sub_Aggr)) then
4065 Expr := First (Expressions (Sub_Aggr));
4066 while Present (Expr) loop
4067 Compute_Others_Present (Expr, Dim + 1);
4072 -- Process component associations
4074 if Present (Component_Associations (Sub_Aggr)) then
4075 Assoc := First (Component_Associations (Sub_Aggr));
4076 while Present (Assoc) loop
4077 Expr := Expression (Assoc);
4078 Compute_Others_Present (Expr, Dim + 1);
4083 end Compute_Others_Present;
4085 ------------------------
4086 -- In_Place_Assign_OK --
4087 ------------------------
4089 function In_Place_Assign_OK return Boolean is
4097 function Safe_Aggregate (Aggr : Node_Id) return Boolean;
4098 -- Check recursively that each component of a (sub)aggregate does
4099 -- not depend on the variable being assigned to.
4101 function Safe_Component (Expr : Node_Id) return Boolean;
4102 -- Verify that an expression cannot depend on the variable being
4103 -- assigned to. Room for improvement here (but less than before).
4105 --------------------
4106 -- Safe_Aggregate --
4107 --------------------
4109 function Safe_Aggregate (Aggr : Node_Id) return Boolean is
4113 if Present (Expressions (Aggr)) then
4114 Expr := First (Expressions (Aggr));
4115 while Present (Expr) loop
4116 if Nkind (Expr) = N_Aggregate then
4117 if not Safe_Aggregate (Expr) then
4121 elsif not Safe_Component (Expr) then
4129 if Present (Component_Associations (Aggr)) then
4130 Expr := First (Component_Associations (Aggr));
4131 while Present (Expr) loop
4132 if Nkind (Expression (Expr)) = N_Aggregate then
4133 if not Safe_Aggregate (Expression (Expr)) then
4137 elsif not Safe_Component (Expression (Expr)) then
4148 --------------------
4149 -- Safe_Component --
4150 --------------------
4152 function Safe_Component (Expr : Node_Id) return Boolean is
4153 Comp : Node_Id := Expr;
4155 function Check_Component (Comp : Node_Id) return Boolean;
4156 -- Do the recursive traversal, after copy
4158 ---------------------
4159 -- Check_Component --
4160 ---------------------
4162 function Check_Component (Comp : Node_Id) return Boolean is
4164 if Is_Overloaded (Comp) then
4168 return Compile_Time_Known_Value (Comp)
4170 or else (Is_Entity_Name (Comp)
4171 and then Present (Entity (Comp))
4172 and then No (Renamed_Object (Entity (Comp))))
4174 or else (Nkind (Comp) = N_Attribute_Reference
4175 and then Check_Component (Prefix (Comp)))
4177 or else (Nkind (Comp) in N_Binary_Op
4178 and then Check_Component (Left_Opnd (Comp))
4179 and then Check_Component (Right_Opnd (Comp)))
4181 or else (Nkind (Comp) in N_Unary_Op
4182 and then Check_Component (Right_Opnd (Comp)))
4184 or else (Nkind (Comp) = N_Selected_Component
4185 and then Check_Component (Prefix (Comp)))
4187 or else (Nkind (Comp) = N_Unchecked_Type_Conversion
4188 and then Check_Component (Expression (Comp)));
4189 end Check_Component;
4191 -- Start of processing for Safe_Component
4194 -- If the component appears in an association that may
4195 -- correspond to more than one element, it is not analyzed
4196 -- before the expansion into assignments, to avoid side effects.
4197 -- We analyze, but do not resolve the copy, to obtain sufficient
4198 -- entity information for the checks that follow. If component is
4199 -- overloaded we assume an unsafe function call.
4201 if not Analyzed (Comp) then
4202 if Is_Overloaded (Expr) then
4205 elsif Nkind (Expr) = N_Aggregate
4206 and then not Is_Others_Aggregate (Expr)
4210 elsif Nkind (Expr) = N_Allocator then
4212 -- For now, too complex to analyze
4217 Comp := New_Copy_Tree (Expr);
4218 Set_Parent (Comp, Parent (Expr));
4222 if Nkind (Comp) = N_Aggregate then
4223 return Safe_Aggregate (Comp);
4225 return Check_Component (Comp);
4229 -- Start of processing for In_Place_Assign_OK
4232 if Present (Component_Associations (N)) then
4234 -- On assignment, sliding can take place, so we cannot do the
4235 -- assignment in place unless the bounds of the aggregate are
4236 -- statically equal to those of the target.
4238 -- If the aggregate is given by an others choice, the bounds
4239 -- are derived from the left-hand side, and the assignment is
4240 -- safe if the expression is.
4242 if Is_Others_Aggregate (N) then
4245 (Expression (First (Component_Associations (N))));
4248 Aggr_In := First_Index (Etype (N));
4250 if Nkind (Parent (N)) = N_Assignment_Statement then
4251 Obj_In := First_Index (Etype (Name (Parent (N))));
4254 -- Context is an allocator. Check bounds of aggregate
4255 -- against given type in qualified expression.
4257 pragma Assert (Nkind (Parent (Parent (N))) = N_Allocator);
4259 First_Index (Etype (Entity (Subtype_Mark (Parent (N)))));
4262 while Present (Aggr_In) loop
4263 Get_Index_Bounds (Aggr_In, Aggr_Lo, Aggr_Hi);
4264 Get_Index_Bounds (Obj_In, Obj_Lo, Obj_Hi);
4266 if not Compile_Time_Known_Value (Aggr_Lo)
4267 or else not Compile_Time_Known_Value (Aggr_Hi)
4268 or else not Compile_Time_Known_Value (Obj_Lo)
4269 or else not Compile_Time_Known_Value (Obj_Hi)
4270 or else Expr_Value (Aggr_Lo) /= Expr_Value (Obj_Lo)
4271 or else Expr_Value (Aggr_Hi) /= Expr_Value (Obj_Hi)
4276 Next_Index (Aggr_In);
4277 Next_Index (Obj_In);
4281 -- Now check the component values themselves
4283 return Safe_Aggregate (N);
4284 end In_Place_Assign_OK;
4290 procedure Others_Check (Sub_Aggr : Node_Id; Dim : Pos) is
4291 Aggr_Lo : constant Node_Id := Aggr_Low (Dim);
4292 Aggr_Hi : constant Node_Id := Aggr_High (Dim);
4293 -- The bounds of the aggregate for this dimension
4295 Ind_Typ : constant Entity_Id := Aggr_Index_Typ (Dim);
4296 -- The index type for this dimension
4298 Need_To_Check : Boolean := False;
4300 Choices_Lo : Node_Id := Empty;
4301 Choices_Hi : Node_Id := Empty;
4302 -- The lowest and highest discrete choices for a named sub-aggregate
4304 Nb_Choices : Int := -1;
4305 -- The number of discrete non-others choices in this sub-aggregate
4307 Nb_Elements : Uint := Uint_0;
4308 -- The number of elements in a positional aggregate
4310 Cond : Node_Id := Empty;
4317 -- Check if we have an others choice. If we do make sure that this
4318 -- sub-aggregate contains at least one element in addition to the
4321 if Range_Checks_Suppressed (Ind_Typ) then
4322 Need_To_Check := False;
4324 elsif Present (Expressions (Sub_Aggr))
4325 and then Present (Component_Associations (Sub_Aggr))
4327 Need_To_Check := True;
4329 elsif Present (Component_Associations (Sub_Aggr)) then
4330 Assoc := Last (Component_Associations (Sub_Aggr));
4332 if Nkind (First (Choices (Assoc))) /= N_Others_Choice then
4333 Need_To_Check := False;
4336 -- Count the number of discrete choices. Start with -1 because
4337 -- the others choice does not count.
4340 Assoc := First (Component_Associations (Sub_Aggr));
4341 while Present (Assoc) loop
4342 Choice := First (Choices (Assoc));
4343 while Present (Choice) loop
4344 Nb_Choices := Nb_Choices + 1;
4351 -- If there is only an others choice nothing to do
4353 Need_To_Check := (Nb_Choices > 0);
4357 Need_To_Check := False;
4360 -- If we are dealing with a positional sub-aggregate with an others
4361 -- choice then compute the number or positional elements.
4363 if Need_To_Check and then Present (Expressions (Sub_Aggr)) then
4364 Expr := First (Expressions (Sub_Aggr));
4365 Nb_Elements := Uint_0;
4366 while Present (Expr) loop
4367 Nb_Elements := Nb_Elements + 1;
4371 -- If the aggregate contains discrete choices and an others choice
4372 -- compute the smallest and largest discrete choice values.
4374 elsif Need_To_Check then
4375 Compute_Choices_Lo_And_Choices_Hi : declare
4377 Table : Case_Table_Type (1 .. Nb_Choices);
4378 -- Used to sort all the different choice values
4385 Assoc := First (Component_Associations (Sub_Aggr));
4386 while Present (Assoc) loop
4387 Choice := First (Choices (Assoc));
4388 while Present (Choice) loop
4389 if Nkind (Choice) = N_Others_Choice then
4393 Get_Index_Bounds (Choice, Low, High);
4394 Table (J).Choice_Lo := Low;
4395 Table (J).Choice_Hi := High;
4404 -- Sort the discrete choices
4406 Sort_Case_Table (Table);
4408 Choices_Lo := Table (1).Choice_Lo;
4409 Choices_Hi := Table (Nb_Choices).Choice_Hi;
4410 end Compute_Choices_Lo_And_Choices_Hi;
4413 -- If no others choice in this sub-aggregate, or the aggregate
4414 -- comprises only an others choice, nothing to do.
4416 if not Need_To_Check then
4419 -- If we are dealing with an aggregate containing an others choice
4420 -- and positional components, we generate the following test:
4422 -- if Ind_Typ'Pos (Aggr_Lo) + (Nb_Elements - 1) >
4423 -- Ind_Typ'Pos (Aggr_Hi)
4425 -- raise Constraint_Error;
4428 elsif Nb_Elements > Uint_0 then
4434 Make_Attribute_Reference (Loc,
4435 Prefix => New_Reference_To (Ind_Typ, Loc),
4436 Attribute_Name => Name_Pos,
4439 (Duplicate_Subexpr_Move_Checks (Aggr_Lo))),
4440 Right_Opnd => Make_Integer_Literal (Loc, Nb_Elements - 1)),
4443 Make_Attribute_Reference (Loc,
4444 Prefix => New_Reference_To (Ind_Typ, Loc),
4445 Attribute_Name => Name_Pos,
4446 Expressions => New_List (
4447 Duplicate_Subexpr_Move_Checks (Aggr_Hi))));
4449 -- If we are dealing with an aggregate containing an others choice
4450 -- and discrete choices we generate the following test:
4452 -- [constraint_error when
4453 -- Choices_Lo < Aggr_Lo or else Choices_Hi > Aggr_Hi];
4461 Duplicate_Subexpr_Move_Checks (Choices_Lo),
4463 Duplicate_Subexpr_Move_Checks (Aggr_Lo)),
4468 Duplicate_Subexpr (Choices_Hi),
4470 Duplicate_Subexpr (Aggr_Hi)));
4473 if Present (Cond) then
4475 Make_Raise_Constraint_Error (Loc,
4477 Reason => CE_Length_Check_Failed));
4478 -- Questionable reason code, shouldn't that be a
4479 -- CE_Range_Check_Failed ???
4482 -- Now look inside the sub-aggregate to see if there is more work
4484 if Dim < Aggr_Dimension then
4486 -- Process positional components
4488 if Present (Expressions (Sub_Aggr)) then
4489 Expr := First (Expressions (Sub_Aggr));
4490 while Present (Expr) loop
4491 Others_Check (Expr, Dim + 1);
4496 -- Process component associations
4498 if Present (Component_Associations (Sub_Aggr)) then
4499 Assoc := First (Component_Associations (Sub_Aggr));
4500 while Present (Assoc) loop
4501 Expr := Expression (Assoc);
4502 Others_Check (Expr, Dim + 1);
4509 -------------------------
4510 -- Safe_Left_Hand_Side --
4511 -------------------------
4513 function Safe_Left_Hand_Side (N : Node_Id) return Boolean is
4514 function Is_Safe_Index (Indx : Node_Id) return Boolean;
4515 -- If the left-hand side includes an indexed component, check that
4516 -- the indexes are free of side-effect.
4522 function Is_Safe_Index (Indx : Node_Id) return Boolean is
4524 if Is_Entity_Name (Indx) then
4527 elsif Nkind (Indx) = N_Integer_Literal then
4530 elsif Nkind (Indx) = N_Function_Call
4531 and then Is_Entity_Name (Name (Indx))
4533 Has_Pragma_Pure_Function (Entity (Name (Indx)))
4537 elsif Nkind (Indx) = N_Type_Conversion
4538 and then Is_Safe_Index (Expression (Indx))
4547 -- Start of processing for Safe_Left_Hand_Side
4550 if Is_Entity_Name (N) then
4553 elsif Nkind_In (N, N_Explicit_Dereference, N_Selected_Component)
4554 and then Safe_Left_Hand_Side (Prefix (N))
4558 elsif Nkind (N) = N_Indexed_Component
4559 and then Safe_Left_Hand_Side (Prefix (N))
4561 Is_Safe_Index (First (Expressions (N)))
4565 elsif Nkind (N) = N_Unchecked_Type_Conversion then
4566 return Safe_Left_Hand_Side (Expression (N));
4571 end Safe_Left_Hand_Side;
4576 -- Holds the temporary aggregate value
4579 -- Holds the declaration of Tmp
4581 Aggr_Code : List_Id;
4582 Parent_Node : Node_Id;
4583 Parent_Kind : Node_Kind;
4585 -- Start of processing for Expand_Array_Aggregate
4588 -- Do not touch the special aggregates of attributes used for Asm calls
4590 if Is_RTE (Ctyp, RE_Asm_Input_Operand)
4591 or else Is_RTE (Ctyp, RE_Asm_Output_Operand)
4596 -- If the semantic analyzer has determined that aggregate N will raise
4597 -- Constraint_Error at run time, then the aggregate node has been
4598 -- replaced with an N_Raise_Constraint_Error node and we should
4601 pragma Assert (not Raises_Constraint_Error (N));
4605 -- Check that the index range defined by aggregate bounds is
4606 -- compatible with corresponding index subtype.
4608 Index_Compatibility_Check : declare
4609 Aggr_Index_Range : Node_Id := First_Index (Typ);
4610 -- The current aggregate index range
4612 Index_Constraint : Node_Id := First_Index (Etype (Typ));
4613 -- The corresponding index constraint against which we have to
4614 -- check the above aggregate index range.
4617 Compute_Others_Present (N, 1);
4619 for J in 1 .. Aggr_Dimension loop
4620 -- There is no need to emit a check if an others choice is
4621 -- present for this array aggregate dimension since in this
4622 -- case one of N's sub-aggregates has taken its bounds from the
4623 -- context and these bounds must have been checked already. In
4624 -- addition all sub-aggregates corresponding to the same
4625 -- dimension must all have the same bounds (checked in (c) below).
4627 if not Range_Checks_Suppressed (Etype (Index_Constraint))
4628 and then not Others_Present (J)
4630 -- We don't use Checks.Apply_Range_Check here because it emits
4631 -- a spurious check. Namely it checks that the range defined by
4632 -- the aggregate bounds is non empty. But we know this already
4635 Check_Bounds (Aggr_Index_Range, Index_Constraint);
4638 -- Save the low and high bounds of the aggregate index as well as
4639 -- the index type for later use in checks (b) and (c) below.
4641 Aggr_Low (J) := Low_Bound (Aggr_Index_Range);
4642 Aggr_High (J) := High_Bound (Aggr_Index_Range);
4644 Aggr_Index_Typ (J) := Etype (Index_Constraint);
4646 Next_Index (Aggr_Index_Range);
4647 Next_Index (Index_Constraint);
4649 end Index_Compatibility_Check;
4653 -- If an others choice is present check that no aggregate index is
4654 -- outside the bounds of the index constraint.
4656 Others_Check (N, 1);
4660 -- For multidimensional arrays make sure that all subaggregates
4661 -- corresponding to the same dimension have the same bounds.
4663 if Aggr_Dimension > 1 then
4664 Check_Same_Aggr_Bounds (N, 1);
4667 -- In formal verification mode, leave the aggregate non-expanded
4675 -- Here we test for is packed array aggregate that we can handle at
4676 -- compile time. If so, return with transformation done. Note that we do
4677 -- this even if the aggregate is nested, because once we have done this
4678 -- processing, there is no more nested aggregate!
4680 if Packed_Array_Aggregate_Handled (N) then
4684 -- At this point we try to convert to positional form
4686 if Ekind (Current_Scope) = E_Package
4687 and then Static_Elaboration_Desired (Current_Scope)
4689 Convert_To_Positional (N, Max_Others_Replicate => 100);
4692 Convert_To_Positional (N);
4695 -- if the result is no longer an aggregate (e.g. it may be a string
4696 -- literal, or a temporary which has the needed value), then we are
4697 -- done, since there is no longer a nested aggregate.
4699 if Nkind (N) /= N_Aggregate then
4702 -- We are also done if the result is an analyzed aggregate
4703 -- This case could use more comments ???
4706 and then N /= Original_Node (N)
4711 -- If all aggregate components are compile-time known and the aggregate
4712 -- has been flattened, nothing left to do. The same occurs if the
4713 -- aggregate is used to initialize the components of an statically
4714 -- allocated dispatch table.
4716 if Compile_Time_Known_Aggregate (N)
4717 or else Is_Static_Dispatch_Table_Aggregate (N)
4719 Set_Expansion_Delayed (N, False);
4723 -- Now see if back end processing is possible
4725 if Backend_Processing_Possible (N) then
4727 -- If the aggregate is static but the constraints are not, build
4728 -- a static subtype for the aggregate, so that Gigi can place it
4729 -- in static memory. Perform an unchecked_conversion to the non-
4730 -- static type imposed by the context.
4733 Itype : constant Entity_Id := Etype (N);
4735 Needs_Type : Boolean := False;
4738 Index := First_Index (Itype);
4739 while Present (Index) loop
4740 if not Is_Static_Subtype (Etype (Index)) then
4749 Build_Constrained_Type (Positional => True);
4750 Rewrite (N, Unchecked_Convert_To (Itype, N));
4760 -- Delay expansion for nested aggregates: it will be taken care of
4761 -- when the parent aggregate is expanded.
4763 Parent_Node := Parent (N);
4764 Parent_Kind := Nkind (Parent_Node);
4766 if Parent_Kind = N_Qualified_Expression then
4767 Parent_Node := Parent (Parent_Node);
4768 Parent_Kind := Nkind (Parent_Node);
4771 if Parent_Kind = N_Aggregate
4772 or else Parent_Kind = N_Extension_Aggregate
4773 or else Parent_Kind = N_Component_Association
4774 or else (Parent_Kind = N_Object_Declaration
4775 and then Needs_Finalization (Typ))
4776 or else (Parent_Kind = N_Assignment_Statement
4777 and then Inside_Init_Proc)
4779 if Static_Array_Aggregate (N)
4780 or else Compile_Time_Known_Aggregate (N)
4782 Set_Expansion_Delayed (N, False);
4785 Set_Expansion_Delayed (N);
4792 -- Look if in place aggregate expansion is possible
4794 -- For object declarations we build the aggregate in place, unless
4795 -- the array is bit-packed or the component is controlled.
4797 -- For assignments we do the assignment in place if all the component
4798 -- associations have compile-time known values. For other cases we
4799 -- create a temporary. The analysis for safety of on-line assignment
4800 -- is delicate, i.e. we don't know how to do it fully yet ???
4802 -- For allocators we assign to the designated object in place if the
4803 -- aggregate meets the same conditions as other in-place assignments.
4804 -- In this case the aggregate may not come from source but was created
4805 -- for default initialization, e.g. with Initialize_Scalars.
4807 if Requires_Transient_Scope (Typ) then
4808 Establish_Transient_Scope
4809 (N, Sec_Stack => Has_Controlled_Component (Typ));
4812 if Has_Default_Init_Comps (N) then
4813 Maybe_In_Place_OK := False;
4815 elsif Is_Bit_Packed_Array (Typ)
4816 or else Has_Controlled_Component (Typ)
4818 Maybe_In_Place_OK := False;
4821 Maybe_In_Place_OK :=
4822 (Nkind (Parent (N)) = N_Assignment_Statement
4823 and then Comes_From_Source (N)
4824 and then In_Place_Assign_OK)
4827 (Nkind (Parent (Parent (N))) = N_Allocator
4828 and then In_Place_Assign_OK);
4831 -- If this is an array of tasks, it will be expanded into build-in-place
4832 -- assignments. Build an activation chain for the tasks now.
4834 if Has_Task (Etype (N)) then
4835 Build_Activation_Chain_Entity (N);
4838 -- Should document these individual tests ???
4840 if not Has_Default_Init_Comps (N)
4841 and then Comes_From_Source (Parent (N))
4842 and then Nkind (Parent (N)) = N_Object_Declaration
4844 Must_Slide (Etype (Defining_Identifier (Parent (N))), Typ)
4845 and then N = Expression (Parent (N))
4846 and then not Is_Bit_Packed_Array (Typ)
4847 and then not Has_Controlled_Component (Typ)
4849 -- If the aggregate is the expression in an object declaration, it
4850 -- cannot be expanded in place. Lookahead in the current declarative
4851 -- part to find an address clause for the object being declared. If
4852 -- one is present, we cannot build in place. Unclear comment???
4854 and then not Has_Following_Address_Clause (Parent (N))
4856 Tmp := Defining_Identifier (Parent (N));
4857 Set_No_Initialization (Parent (N));
4858 Set_Expression (Parent (N), Empty);
4860 -- Set the type of the entity, for use in the analysis of the
4861 -- subsequent indexed assignments. If the nominal type is not
4862 -- constrained, build a subtype from the known bounds of the
4863 -- aggregate. If the declaration has a subtype mark, use it,
4864 -- otherwise use the itype of the aggregate.
4866 if not Is_Constrained (Typ) then
4867 Build_Constrained_Type (Positional => False);
4868 elsif Is_Entity_Name (Object_Definition (Parent (N)))
4869 and then Is_Constrained (Entity (Object_Definition (Parent (N))))
4871 Set_Etype (Tmp, Entity (Object_Definition (Parent (N))));
4873 Set_Size_Known_At_Compile_Time (Typ, False);
4874 Set_Etype (Tmp, Typ);
4877 elsif Maybe_In_Place_OK
4878 and then Nkind (Parent (N)) = N_Qualified_Expression
4879 and then Nkind (Parent (Parent (N))) = N_Allocator
4881 Set_Expansion_Delayed (N);
4884 -- In the remaining cases the aggregate is the RHS of an assignment
4886 elsif Maybe_In_Place_OK
4887 and then Safe_Left_Hand_Side (Name (Parent (N)))
4889 Tmp := Name (Parent (N));
4891 if Etype (Tmp) /= Etype (N) then
4892 Apply_Length_Check (N, Etype (Tmp));
4894 if Nkind (N) = N_Raise_Constraint_Error then
4896 -- Static error, nothing further to expand
4902 elsif Maybe_In_Place_OK
4903 and then Nkind (Name (Parent (N))) = N_Slice
4904 and then Safe_Slice_Assignment (N)
4906 -- Safe_Slice_Assignment rewrites assignment as a loop
4912 -- In place aggregate expansion is not possible
4915 Maybe_In_Place_OK := False;
4916 Tmp := Make_Temporary (Loc, 'A', N);
4918 Make_Object_Declaration
4920 Defining_Identifier => Tmp,
4921 Object_Definition => New_Occurrence_Of (Typ, Loc));
4922 Set_No_Initialization (Tmp_Decl, True);
4924 -- If we are within a loop, the temporary will be pushed on the
4925 -- stack at each iteration. If the aggregate is the expression for an
4926 -- allocator, it will be immediately copied to the heap and can
4927 -- be reclaimed at once. We create a transient scope around the
4928 -- aggregate for this purpose.
4930 if Ekind (Current_Scope) = E_Loop
4931 and then Nkind (Parent (Parent (N))) = N_Allocator
4933 Establish_Transient_Scope (N, False);
4936 Insert_Action (N, Tmp_Decl);
4939 -- Construct and insert the aggregate code. We can safely suppress index
4940 -- checks because this code is guaranteed not to raise CE on index
4941 -- checks. However we should *not* suppress all checks.
4947 if Nkind (Tmp) = N_Defining_Identifier then
4948 Target := New_Reference_To (Tmp, Loc);
4952 if Has_Default_Init_Comps (N) then
4954 -- Ada 2005 (AI-287): This case has not been analyzed???
4956 raise Program_Error;
4959 -- Name in assignment is explicit dereference
4961 Target := New_Copy (Tmp);
4965 Build_Array_Aggr_Code (N,
4967 Index => First_Index (Typ),
4969 Scalar_Comp => Is_Scalar_Type (Ctyp));
4972 if Comes_From_Source (Tmp) then
4973 Insert_Actions_After (Parent (N), Aggr_Code);
4976 Insert_Actions (N, Aggr_Code);
4979 -- If the aggregate has been assigned in place, remove the original
4982 if Nkind (Parent (N)) = N_Assignment_Statement
4983 and then Maybe_In_Place_OK
4985 Rewrite (Parent (N), Make_Null_Statement (Loc));
4987 elsif Nkind (Parent (N)) /= N_Object_Declaration
4988 or else Tmp /= Defining_Identifier (Parent (N))
4990 Rewrite (N, New_Occurrence_Of (Tmp, Loc));
4991 Analyze_And_Resolve (N, Typ);
4993 end Expand_Array_Aggregate;
4995 ------------------------
4996 -- Expand_N_Aggregate --
4997 ------------------------
4999 procedure Expand_N_Aggregate (N : Node_Id) is
5001 if Is_Record_Type (Etype (N)) then
5002 Expand_Record_Aggregate (N);
5004 Expand_Array_Aggregate (N);
5007 when RE_Not_Available =>
5009 end Expand_N_Aggregate;
5011 ----------------------------------
5012 -- Expand_N_Extension_Aggregate --
5013 ----------------------------------
5015 -- If the ancestor part is an expression, add a component association for
5016 -- the parent field. If the type of the ancestor part is not the direct
5017 -- parent of the expected type, build recursively the needed ancestors.
5018 -- If the ancestor part is a subtype_mark, replace aggregate with a decla-
5019 -- ration for a temporary of the expected type, followed by individual
5020 -- assignments to the given components.
5022 procedure Expand_N_Extension_Aggregate (N : Node_Id) is
5023 Loc : constant Source_Ptr := Sloc (N);
5024 A : constant Node_Id := Ancestor_Part (N);
5025 Typ : constant Entity_Id := Etype (N);
5028 -- If the ancestor is a subtype mark, an init proc must be called
5029 -- on the resulting object which thus has to be materialized in
5032 if Is_Entity_Name (A) and then Is_Type (Entity (A)) then
5033 Convert_To_Assignments (N, Typ);
5035 -- The extension aggregate is transformed into a record aggregate
5036 -- of the following form (c1 and c2 are inherited components)
5038 -- (Exp with c3 => a, c4 => b)
5039 -- ==> (c1 => Exp.c1, c2 => Exp.c2, c3 => a, c4 => b)
5044 if Tagged_Type_Expansion then
5045 Expand_Record_Aggregate (N,
5048 (Node (First_Elmt (Access_Disp_Table (Typ))), Loc),
5051 -- No tag is needed in the case of a VM
5054 Expand_Record_Aggregate (N, Parent_Expr => A);
5059 when RE_Not_Available =>
5061 end Expand_N_Extension_Aggregate;
5063 -----------------------------
5064 -- Expand_Record_Aggregate --
5065 -----------------------------
5067 procedure Expand_Record_Aggregate
5069 Orig_Tag : Node_Id := Empty;
5070 Parent_Expr : Node_Id := Empty)
5072 Loc : constant Source_Ptr := Sloc (N);
5073 Comps : constant List_Id := Component_Associations (N);
5074 Typ : constant Entity_Id := Etype (N);
5075 Base_Typ : constant Entity_Id := Base_Type (Typ);
5077 Static_Components : Boolean := True;
5078 -- Flag to indicate whether all components are compile-time known,
5079 -- and the aggregate can be constructed statically and handled by
5082 function Component_Not_OK_For_Backend return Boolean;
5083 -- Check for presence of component which makes it impossible for the
5084 -- backend to process the aggregate, thus requiring the use of a series
5085 -- of assignment statements. Cases checked for are a nested aggregate
5086 -- needing Late_Expansion, the presence of a tagged component which may
5087 -- need tag adjustment, and a bit unaligned component reference.
5089 -- We also force expansion into assignments if a component is of a
5090 -- mutable type (including a private type with discriminants) because
5091 -- in that case the size of the component to be copied may be smaller
5092 -- than the side of the target, and there is no simple way for gigi
5093 -- to compute the size of the object to be copied.
5095 -- NOTE: This is part of the ongoing work to define precisely the
5096 -- interface between front-end and back-end handling of aggregates.
5097 -- In general it is desirable to pass aggregates as they are to gigi,
5098 -- in order to minimize elaboration code. This is one case where the
5099 -- semantics of Ada complicate the analysis and lead to anomalies in
5100 -- the gcc back-end if the aggregate is not expanded into assignments.
5102 ----------------------------------
5103 -- Component_Not_OK_For_Backend --
5104 ----------------------------------
5106 function Component_Not_OK_For_Backend return Boolean is
5116 while Present (C) loop
5118 -- If the component has box initialization, expansion is needed
5119 -- and component is not ready for backend.
5121 if Box_Present (C) then
5125 if Nkind (Expression (C)) = N_Qualified_Expression then
5126 Expr_Q := Expression (Expression (C));
5128 Expr_Q := Expression (C);
5131 -- Return true if the aggregate has any associations for tagged
5132 -- components that may require tag adjustment.
5134 -- These are cases where the source expression may have a tag that
5135 -- could differ from the component tag (e.g., can occur for type
5136 -- conversions and formal parameters). (Tag adjustment not needed
5137 -- if VM_Target because object tags are implicit in the machine.)
5139 if Is_Tagged_Type (Etype (Expr_Q))
5140 and then (Nkind (Expr_Q) = N_Type_Conversion
5141 or else (Is_Entity_Name (Expr_Q)
5143 Ekind (Entity (Expr_Q)) in Formal_Kind))
5144 and then Tagged_Type_Expansion
5146 Static_Components := False;
5149 elsif Is_Delayed_Aggregate (Expr_Q) then
5150 Static_Components := False;
5153 elsif Possible_Bit_Aligned_Component (Expr_Q) then
5154 Static_Components := False;
5158 if Is_Scalar_Type (Etype (Expr_Q)) then
5159 if not Compile_Time_Known_Value (Expr_Q) then
5160 Static_Components := False;
5163 elsif Nkind (Expr_Q) /= N_Aggregate
5164 or else not Compile_Time_Known_Aggregate (Expr_Q)
5166 Static_Components := False;
5168 if Is_Private_Type (Etype (Expr_Q))
5169 and then Has_Discriminants (Etype (Expr_Q))
5179 end Component_Not_OK_For_Backend;
5181 -- Remaining Expand_Record_Aggregate variables
5183 Tag_Value : Node_Id;
5187 function Has_Visible_Private_Ancestor (Id : E) return Boolean;
5188 -- If any ancestor of the current type is private, the aggregate
5189 -- cannot be built in place. We canot rely on Has_Private_Ancestor,
5190 -- because it will not be set when type and its parent are in the
5191 -- same scope, and the parent component needs expansion.
5193 -----------------------------------
5194 -- Has_Visible_Private_Ancestor --
5195 -----------------------------------
5197 function Has_Visible_Private_Ancestor (Id : E) return Boolean is
5198 R : constant Entity_Id := Root_Type (Id);
5199 T1 : Entity_Id := Id;
5202 if Is_Private_Type (T1) then
5212 end Has_Visible_Private_Ancestor;
5214 -- Start of processing for Expand_Record_Aggregate
5217 -- If the aggregate is to be assigned to an atomic variable, we
5218 -- have to prevent a piecemeal assignment even if the aggregate
5219 -- is to be expanded. We create a temporary for the aggregate, and
5220 -- assign the temporary instead, so that the back end can generate
5221 -- an atomic move for it.
5224 and then Comes_From_Source (Parent (N))
5225 and then Is_Atomic_Aggregate (N, Typ)
5229 -- No special management required for aggregates used to initialize
5230 -- statically allocated dispatch tables
5232 elsif Is_Static_Dispatch_Table_Aggregate (N) then
5236 -- Ada 2005 (AI-318-2): We need to convert to assignments if components
5237 -- are build-in-place function calls. The assignments will each turn
5238 -- into a build-in-place function call. If components are all static,
5239 -- we can pass the aggregate to the backend regardless of limitedness.
5241 -- Extension aggregates, aggregates in extended return statements, and
5242 -- aggregates for C++ imported types must be expanded.
5244 if Ada_Version >= Ada_2005 and then Is_Immutably_Limited_Type (Typ) then
5245 if not Nkind_In (Parent (N), N_Object_Declaration,
5246 N_Component_Association)
5248 Convert_To_Assignments (N, Typ);
5250 elsif Nkind (N) = N_Extension_Aggregate
5251 or else Convention (Typ) = Convention_CPP
5253 Convert_To_Assignments (N, Typ);
5255 elsif not Size_Known_At_Compile_Time (Typ)
5256 or else Component_Not_OK_For_Backend
5257 or else not Static_Components
5259 Convert_To_Assignments (N, Typ);
5262 Set_Compile_Time_Known_Aggregate (N);
5263 Set_Expansion_Delayed (N, False);
5266 -- Gigi doesn't properly handle temporaries of variable size so we
5267 -- generate it in the front-end
5269 elsif not Size_Known_At_Compile_Time (Typ)
5270 and then Tagged_Type_Expansion
5272 Convert_To_Assignments (N, Typ);
5274 -- Temporaries for controlled aggregates need to be attached to a final
5275 -- chain in order to be properly finalized, so it has to be created in
5278 elsif Is_Controlled (Typ)
5279 or else Has_Controlled_Component (Base_Type (Typ))
5281 Convert_To_Assignments (N, Typ);
5283 -- Ada 2005 (AI-287): In case of default initialized components we
5284 -- convert the aggregate into assignments.
5286 elsif Has_Default_Init_Comps (N) then
5287 Convert_To_Assignments (N, Typ);
5291 elsif Component_Not_OK_For_Backend then
5292 Convert_To_Assignments (N, Typ);
5294 -- If an ancestor is private, some components are not inherited and
5295 -- we cannot expand into a record aggregate
5297 elsif Has_Visible_Private_Ancestor (Typ) then
5298 Convert_To_Assignments (N, Typ);
5300 -- ??? The following was done to compile fxacc00.ads in the ACVCs. Gigi
5301 -- is not able to handle the aggregate for Late_Request.
5303 elsif Is_Tagged_Type (Typ) and then Has_Discriminants (Typ) then
5304 Convert_To_Assignments (N, Typ);
5306 -- If the tagged types covers interface types we need to initialize all
5307 -- hidden components containing pointers to secondary dispatch tables.
5309 elsif Is_Tagged_Type (Typ) and then Has_Interfaces (Typ) then
5310 Convert_To_Assignments (N, Typ);
5312 -- If some components are mutable, the size of the aggregate component
5313 -- may be distinct from the default size of the type component, so
5314 -- we need to expand to insure that the back-end copies the proper
5315 -- size of the data. However, if the aggregate is the initial value of
5316 -- a constant, the target is immutable and may be built statically.
5318 elsif Has_Mutable_Components (Typ)
5320 (Nkind (Parent (N)) /= N_Object_Declaration
5321 or else not Constant_Present (Parent (N)))
5323 Convert_To_Assignments (N, Typ);
5325 -- If the type involved has any non-bit aligned components, then we are
5326 -- not sure that the back end can handle this case correctly.
5328 elsif Type_May_Have_Bit_Aligned_Components (Typ) then
5329 Convert_To_Assignments (N, Typ);
5331 -- In all other cases, build a proper aggregate handlable by gigi
5334 if Nkind (N) = N_Aggregate then
5336 -- If the aggregate is static and can be handled by the back-end,
5337 -- nothing left to do.
5339 if Static_Components then
5340 Set_Compile_Time_Known_Aggregate (N);
5341 Set_Expansion_Delayed (N, False);
5345 -- If no discriminants, nothing special to do
5347 if not Has_Discriminants (Typ) then
5350 -- Case of discriminants present
5352 elsif Is_Derived_Type (Typ) then
5354 -- For untagged types, non-stored discriminants are replaced
5355 -- with stored discriminants, which are the ones that gigi uses
5356 -- to describe the type and its components.
5358 Generate_Aggregate_For_Derived_Type : declare
5359 Constraints : constant List_Id := New_List;
5360 First_Comp : Node_Id;
5361 Discriminant : Entity_Id;
5363 Num_Disc : Int := 0;
5364 Num_Gird : Int := 0;
5366 procedure Prepend_Stored_Values (T : Entity_Id);
5367 -- Scan the list of stored discriminants of the type, and add
5368 -- their values to the aggregate being built.
5370 ---------------------------
5371 -- Prepend_Stored_Values --
5372 ---------------------------
5374 procedure Prepend_Stored_Values (T : Entity_Id) is
5376 Discriminant := First_Stored_Discriminant (T);
5377 while Present (Discriminant) loop
5379 Make_Component_Association (Loc,
5381 New_List (New_Occurrence_Of (Discriminant, Loc)),
5385 Get_Discriminant_Value (
5388 Discriminant_Constraint (Typ))));
5390 if No (First_Comp) then
5391 Prepend_To (Component_Associations (N), New_Comp);
5393 Insert_After (First_Comp, New_Comp);
5396 First_Comp := New_Comp;
5397 Next_Stored_Discriminant (Discriminant);
5399 end Prepend_Stored_Values;
5401 -- Start of processing for Generate_Aggregate_For_Derived_Type
5404 -- Remove the associations for the discriminant of derived type
5406 First_Comp := First (Component_Associations (N));
5407 while Present (First_Comp) loop
5412 (First (Choices (Comp)))) = E_Discriminant
5415 Num_Disc := Num_Disc + 1;
5419 -- Insert stored discriminant associations in the correct
5420 -- order. If there are more stored discriminants than new
5421 -- discriminants, there is at least one new discriminant that
5422 -- constrains more than one of the stored discriminants. In
5423 -- this case we need to construct a proper subtype of the
5424 -- parent type, in order to supply values to all the
5425 -- components. Otherwise there is one-one correspondence
5426 -- between the constraints and the stored discriminants.
5428 First_Comp := Empty;
5430 Discriminant := First_Stored_Discriminant (Base_Type (Typ));
5431 while Present (Discriminant) loop
5432 Num_Gird := Num_Gird + 1;
5433 Next_Stored_Discriminant (Discriminant);
5436 -- Case of more stored discriminants than new discriminants
5438 if Num_Gird > Num_Disc then
5440 -- Create a proper subtype of the parent type, which is the
5441 -- proper implementation type for the aggregate, and convert
5442 -- it to the intended target type.
5444 Discriminant := First_Stored_Discriminant (Base_Type (Typ));
5445 while Present (Discriminant) loop
5448 Get_Discriminant_Value (
5451 Discriminant_Constraint (Typ)));
5452 Append (New_Comp, Constraints);
5453 Next_Stored_Discriminant (Discriminant);
5457 Make_Subtype_Declaration (Loc,
5458 Defining_Identifier => Make_Temporary (Loc, 'T'),
5459 Subtype_Indication =>
5460 Make_Subtype_Indication (Loc,
5462 New_Occurrence_Of (Etype (Base_Type (Typ)), Loc),
5464 Make_Index_Or_Discriminant_Constraint
5465 (Loc, Constraints)));
5467 Insert_Action (N, Decl);
5468 Prepend_Stored_Values (Base_Type (Typ));
5470 Set_Etype (N, Defining_Identifier (Decl));
5473 Rewrite (N, Unchecked_Convert_To (Typ, N));
5476 -- Case where we do not have fewer new discriminants than
5477 -- stored discriminants, so in this case we can simply use the
5478 -- stored discriminants of the subtype.
5481 Prepend_Stored_Values (Typ);
5483 end Generate_Aggregate_For_Derived_Type;
5486 if Is_Tagged_Type (Typ) then
5488 -- The tagged case, _parent and _tag component must be created
5490 -- Reset null_present unconditionally. tagged records always have
5491 -- at least one field (the tag or the parent)
5493 Set_Null_Record_Present (N, False);
5495 -- When the current aggregate comes from the expansion of an
5496 -- extension aggregate, the parent expr is replaced by an
5497 -- aggregate formed by selected components of this expr
5499 if Present (Parent_Expr)
5500 and then Is_Empty_List (Comps)
5502 Comp := First_Component_Or_Discriminant (Typ);
5503 while Present (Comp) loop
5505 -- Skip all expander-generated components
5508 not Comes_From_Source (Original_Record_Component (Comp))
5514 Make_Selected_Component (Loc,
5516 Unchecked_Convert_To (Typ,
5517 Duplicate_Subexpr (Parent_Expr, True)),
5519 Selector_Name => New_Occurrence_Of (Comp, Loc));
5522 Make_Component_Association (Loc,
5524 New_List (New_Occurrence_Of (Comp, Loc)),
5528 Analyze_And_Resolve (New_Comp, Etype (Comp));
5531 Next_Component_Or_Discriminant (Comp);
5535 -- Compute the value for the Tag now, if the type is a root it
5536 -- will be included in the aggregate right away, otherwise it will
5537 -- be propagated to the parent aggregate
5539 if Present (Orig_Tag) then
5540 Tag_Value := Orig_Tag;
5541 elsif not Tagged_Type_Expansion then
5546 (Node (First_Elmt (Access_Disp_Table (Typ))), Loc);
5549 -- For a derived type, an aggregate for the parent is formed with
5550 -- all the inherited components.
5552 if Is_Derived_Type (Typ) then
5555 First_Comp : Node_Id;
5556 Parent_Comps : List_Id;
5557 Parent_Aggr : Node_Id;
5558 Parent_Name : Node_Id;
5561 -- Remove the inherited component association from the
5562 -- aggregate and store them in the parent aggregate
5564 First_Comp := First (Component_Associations (N));
5565 Parent_Comps := New_List;
5566 while Present (First_Comp)
5567 and then Scope (Original_Record_Component (
5568 Entity (First (Choices (First_Comp))))) /= Base_Typ
5573 Append (Comp, Parent_Comps);
5576 Parent_Aggr := Make_Aggregate (Loc,
5577 Component_Associations => Parent_Comps);
5578 Set_Etype (Parent_Aggr, Etype (Base_Type (Typ)));
5580 -- Find the _parent component
5582 Comp := First_Component (Typ);
5583 while Chars (Comp) /= Name_uParent loop
5584 Comp := Next_Component (Comp);
5587 Parent_Name := New_Occurrence_Of (Comp, Loc);
5589 -- Insert the parent aggregate
5591 Prepend_To (Component_Associations (N),
5592 Make_Component_Association (Loc,
5593 Choices => New_List (Parent_Name),
5594 Expression => Parent_Aggr));
5596 -- Expand recursively the parent propagating the right Tag
5598 Expand_Record_Aggregate (
5599 Parent_Aggr, Tag_Value, Parent_Expr);
5602 -- For a root type, the tag component is added (unless compiling
5603 -- for the VMs, where tags are implicit).
5605 elsif Tagged_Type_Expansion then
5607 Tag_Name : constant Node_Id :=
5609 (First_Tag_Component (Typ), Loc);
5610 Typ_Tag : constant Entity_Id := RTE (RE_Tag);
5611 Conv_Node : constant Node_Id :=
5612 Unchecked_Convert_To (Typ_Tag, Tag_Value);
5615 Set_Etype (Conv_Node, Typ_Tag);
5616 Prepend_To (Component_Associations (N),
5617 Make_Component_Association (Loc,
5618 Choices => New_List (Tag_Name),
5619 Expression => Conv_Node));
5625 end Expand_Record_Aggregate;
5627 ----------------------------
5628 -- Has_Default_Init_Comps --
5629 ----------------------------
5631 function Has_Default_Init_Comps (N : Node_Id) return Boolean is
5632 Comps : constant List_Id := Component_Associations (N);
5636 pragma Assert (Nkind_In (N, N_Aggregate, N_Extension_Aggregate));
5642 if Has_Self_Reference (N) then
5646 -- Check if any direct component has default initialized components
5649 while Present (C) loop
5650 if Box_Present (C) then
5657 -- Recursive call in case of aggregate expression
5660 while Present (C) loop
5661 Expr := Expression (C);
5665 Nkind_In (Expr, N_Aggregate, N_Extension_Aggregate)
5666 and then Has_Default_Init_Comps (Expr)
5675 end Has_Default_Init_Comps;
5677 --------------------------
5678 -- Is_Delayed_Aggregate --
5679 --------------------------
5681 function Is_Delayed_Aggregate (N : Node_Id) return Boolean is
5682 Node : Node_Id := N;
5683 Kind : Node_Kind := Nkind (Node);
5686 if Kind = N_Qualified_Expression then
5687 Node := Expression (Node);
5688 Kind := Nkind (Node);
5691 if Kind /= N_Aggregate and then Kind /= N_Extension_Aggregate then
5694 return Expansion_Delayed (Node);
5696 end Is_Delayed_Aggregate;
5698 ----------------------------------------
5699 -- Is_Static_Dispatch_Table_Aggregate --
5700 ----------------------------------------
5702 function Is_Static_Dispatch_Table_Aggregate (N : Node_Id) return Boolean is
5703 Typ : constant Entity_Id := Base_Type (Etype (N));
5706 return Static_Dispatch_Tables
5707 and then Tagged_Type_Expansion
5708 and then RTU_Loaded (Ada_Tags)
5710 -- Avoid circularity when rebuilding the compiler
5712 and then Cunit_Entity (Get_Source_Unit (N)) /= RTU_Entity (Ada_Tags)
5713 and then (Typ = RTE (RE_Dispatch_Table_Wrapper)
5715 Typ = RTE (RE_Address_Array)
5717 Typ = RTE (RE_Type_Specific_Data)
5719 Typ = RTE (RE_Tag_Table)
5721 (RTE_Available (RE_Interface_Data)
5722 and then Typ = RTE (RE_Interface_Data))
5724 (RTE_Available (RE_Interfaces_Array)
5725 and then Typ = RTE (RE_Interfaces_Array))
5727 (RTE_Available (RE_Interface_Data_Element)
5728 and then Typ = RTE (RE_Interface_Data_Element)));
5729 end Is_Static_Dispatch_Table_Aggregate;
5731 --------------------
5732 -- Late_Expansion --
5733 --------------------
5735 function Late_Expansion
5738 Target : Node_Id) return List_Id
5741 if Is_Record_Type (Etype (N)) then
5742 return Build_Record_Aggr_Code (N, Typ, Target);
5744 else pragma Assert (Is_Array_Type (Etype (N)));
5746 Build_Array_Aggr_Code
5748 Ctype => Component_Type (Etype (N)),
5749 Index => First_Index (Typ),
5751 Scalar_Comp => Is_Scalar_Type (Component_Type (Typ)),
5752 Indexes => No_List);
5756 ----------------------------------
5757 -- Make_OK_Assignment_Statement --
5758 ----------------------------------
5760 function Make_OK_Assignment_Statement
5763 Expression : Node_Id) return Node_Id
5766 Set_Assignment_OK (Name);
5768 return Make_Assignment_Statement (Sloc, Name, Expression);
5769 end Make_OK_Assignment_Statement;
5771 -----------------------
5772 -- Number_Of_Choices --
5773 -----------------------
5775 function Number_Of_Choices (N : Node_Id) return Nat is
5779 Nb_Choices : Nat := 0;
5782 if Present (Expressions (N)) then
5786 Assoc := First (Component_Associations (N));
5787 while Present (Assoc) loop
5788 Choice := First (Choices (Assoc));
5789 while Present (Choice) loop
5790 if Nkind (Choice) /= N_Others_Choice then
5791 Nb_Choices := Nb_Choices + 1;
5801 end Number_Of_Choices;
5803 ------------------------------------
5804 -- Packed_Array_Aggregate_Handled --
5805 ------------------------------------
5807 -- The current version of this procedure will handle at compile time
5808 -- any array aggregate that meets these conditions:
5810 -- One dimensional, bit packed
5811 -- Underlying packed type is modular type
5812 -- Bounds are within 32-bit Int range
5813 -- All bounds and values are static
5815 function Packed_Array_Aggregate_Handled (N : Node_Id) return Boolean is
5816 Loc : constant Source_Ptr := Sloc (N);
5817 Typ : constant Entity_Id := Etype (N);
5818 Ctyp : constant Entity_Id := Component_Type (Typ);
5820 Not_Handled : exception;
5821 -- Exception raised if this aggregate cannot be handled
5824 -- For now, handle only one dimensional bit packed arrays
5826 if not Is_Bit_Packed_Array (Typ)
5827 or else Number_Dimensions (Typ) > 1
5828 or else not Is_Modular_Integer_Type (Packed_Array_Type (Typ))
5833 if not Is_Scalar_Type (Component_Type (Typ))
5834 and then Has_Non_Standard_Rep (Component_Type (Typ))
5840 Csiz : constant Nat := UI_To_Int (Component_Size (Typ));
5844 -- Bounds of index type
5848 -- Values of bounds if compile time known
5850 function Get_Component_Val (N : Node_Id) return Uint;
5851 -- Given a expression value N of the component type Ctyp, returns a
5852 -- value of Csiz (component size) bits representing this value. If
5853 -- the value is non-static or any other reason exists why the value
5854 -- cannot be returned, then Not_Handled is raised.
5856 -----------------------
5857 -- Get_Component_Val --
5858 -----------------------
5860 function Get_Component_Val (N : Node_Id) return Uint is
5864 -- We have to analyze the expression here before doing any further
5865 -- processing here. The analysis of such expressions is deferred
5866 -- till expansion to prevent some problems of premature analysis.
5868 Analyze_And_Resolve (N, Ctyp);
5870 -- Must have a compile time value. String literals have to be
5871 -- converted into temporaries as well, because they cannot easily
5872 -- be converted into their bit representation.
5874 if not Compile_Time_Known_Value (N)
5875 or else Nkind (N) = N_String_Literal
5880 Val := Expr_Rep_Value (N);
5882 -- Adjust for bias, and strip proper number of bits
5884 if Has_Biased_Representation (Ctyp) then
5885 Val := Val - Expr_Value (Type_Low_Bound (Ctyp));
5888 return Val mod Uint_2 ** Csiz;
5889 end Get_Component_Val;
5891 -- Here we know we have a one dimensional bit packed array
5894 Get_Index_Bounds (First_Index (Typ), Lo, Hi);
5896 -- Cannot do anything if bounds are dynamic
5898 if not Compile_Time_Known_Value (Lo)
5900 not Compile_Time_Known_Value (Hi)
5905 -- Or are silly out of range of int bounds
5907 Lob := Expr_Value (Lo);
5908 Hib := Expr_Value (Hi);
5910 if not UI_Is_In_Int_Range (Lob)
5912 not UI_Is_In_Int_Range (Hib)
5917 -- At this stage we have a suitable aggregate for handling at compile
5918 -- time (the only remaining checks are that the values of expressions
5919 -- in the aggregate are compile time known (check is performed by
5920 -- Get_Component_Val), and that any subtypes or ranges are statically
5923 -- If the aggregate is not fully positional at this stage, then
5924 -- convert it to positional form. Either this will fail, in which
5925 -- case we can do nothing, or it will succeed, in which case we have
5926 -- succeeded in handling the aggregate, or it will stay an aggregate,
5927 -- in which case we have failed to handle this case.
5929 if Present (Component_Associations (N)) then
5930 Convert_To_Positional
5931 (N, Max_Others_Replicate => 64, Handle_Bit_Packed => True);
5932 return Nkind (N) /= N_Aggregate;
5935 -- Otherwise we are all positional, so convert to proper value
5938 Lov : constant Int := UI_To_Int (Lob);
5939 Hiv : constant Int := UI_To_Int (Hib);
5941 Len : constant Nat := Int'Max (0, Hiv - Lov + 1);
5942 -- The length of the array (number of elements)
5944 Aggregate_Val : Uint;
5945 -- Value of aggregate. The value is set in the low order bits of
5946 -- this value. For the little-endian case, the values are stored
5947 -- from low-order to high-order and for the big-endian case the
5948 -- values are stored from high-order to low-order. Note that gigi
5949 -- will take care of the conversions to left justify the value in
5950 -- the big endian case (because of left justified modular type
5951 -- processing), so we do not have to worry about that here.
5954 -- Integer literal for resulting constructed value
5957 -- Shift count from low order for next value
5960 -- Shift increment for loop
5963 -- Next expression from positional parameters of aggregate
5966 -- For little endian, we fill up the low order bits of the target
5967 -- value. For big endian we fill up the high order bits of the
5968 -- target value (which is a left justified modular value).
5970 if Bytes_Big_Endian xor Debug_Flag_8 then
5971 Shift := Csiz * (Len - 1);
5978 -- Loop to set the values
5981 Aggregate_Val := Uint_0;
5983 Expr := First (Expressions (N));
5984 Aggregate_Val := Get_Component_Val (Expr) * Uint_2 ** Shift;
5986 for J in 2 .. Len loop
5987 Shift := Shift + Incr;
5990 Aggregate_Val + Get_Component_Val (Expr) * Uint_2 ** Shift;
5994 -- Now we can rewrite with the proper value
5997 Make_Integer_Literal (Loc,
5998 Intval => Aggregate_Val);
5999 Set_Print_In_Hex (Lit);
6001 -- Construct the expression using this literal. Note that it is
6002 -- important to qualify the literal with its proper modular type
6003 -- since universal integer does not have the required range and
6004 -- also this is a left justified modular type, which is important
6005 -- in the big-endian case.
6008 Unchecked_Convert_To (Typ,
6009 Make_Qualified_Expression (Loc,
6011 New_Occurrence_Of (Packed_Array_Type (Typ), Loc),
6012 Expression => Lit)));
6014 Analyze_And_Resolve (N, Typ);
6022 end Packed_Array_Aggregate_Handled;
6024 ----------------------------
6025 -- Has_Mutable_Components --
6026 ----------------------------
6028 function Has_Mutable_Components (Typ : Entity_Id) return Boolean is
6032 Comp := First_Component (Typ);
6033 while Present (Comp) loop
6034 if Is_Record_Type (Etype (Comp))
6035 and then Has_Discriminants (Etype (Comp))
6036 and then not Is_Constrained (Etype (Comp))
6041 Next_Component (Comp);
6045 end Has_Mutable_Components;
6047 ------------------------------
6048 -- Initialize_Discriminants --
6049 ------------------------------
6051 procedure Initialize_Discriminants (N : Node_Id; Typ : Entity_Id) is
6052 Loc : constant Source_Ptr := Sloc (N);
6053 Bas : constant Entity_Id := Base_Type (Typ);
6054 Par : constant Entity_Id := Etype (Bas);
6055 Decl : constant Node_Id := Parent (Par);
6059 if Is_Tagged_Type (Bas)
6060 and then Is_Derived_Type (Bas)
6061 and then Has_Discriminants (Par)
6062 and then Has_Discriminants (Bas)
6063 and then Number_Discriminants (Bas) /= Number_Discriminants (Par)
6064 and then Nkind (Decl) = N_Full_Type_Declaration
6065 and then Nkind (Type_Definition (Decl)) = N_Record_Definition
6067 (Variant_Part (Component_List (Type_Definition (Decl))))
6068 and then Nkind (N) /= N_Extension_Aggregate
6071 -- Call init proc to set discriminants.
6072 -- There should eventually be a special procedure for this ???
6074 Ref := New_Reference_To (Defining_Identifier (N), Loc);
6075 Insert_Actions_After (N,
6076 Build_Initialization_Call (Sloc (N), Ref, Typ));
6078 end Initialize_Discriminants;
6085 (Obj_Type : Entity_Id;
6086 Typ : Entity_Id) return Boolean
6088 L1, L2, H1, H2 : Node_Id;
6090 -- No sliding if the type of the object is not established yet, if it is
6091 -- an unconstrained type whose actual subtype comes from the aggregate,
6092 -- or if the two types are identical.
6094 if not Is_Array_Type (Obj_Type) then
6097 elsif not Is_Constrained (Obj_Type) then
6100 elsif Typ = Obj_Type then
6104 -- Sliding can only occur along the first dimension
6106 Get_Index_Bounds (First_Index (Typ), L1, H1);
6107 Get_Index_Bounds (First_Index (Obj_Type), L2, H2);
6109 if not Is_Static_Expression (L1)
6110 or else not Is_Static_Expression (L2)
6111 or else not Is_Static_Expression (H1)
6112 or else not Is_Static_Expression (H2)
6116 return Expr_Value (L1) /= Expr_Value (L2)
6117 or else Expr_Value (H1) /= Expr_Value (H2);
6122 ---------------------------
6123 -- Safe_Slice_Assignment --
6124 ---------------------------
6126 function Safe_Slice_Assignment (N : Node_Id) return Boolean is
6127 Loc : constant Source_Ptr := Sloc (Parent (N));
6128 Pref : constant Node_Id := Prefix (Name (Parent (N)));
6129 Range_Node : constant Node_Id := Discrete_Range (Name (Parent (N)));
6137 -- Generate: for J in Range loop Pref (J) := Expr; end loop;
6139 if Comes_From_Source (N)
6140 and then No (Expressions (N))
6141 and then Nkind (First (Choices (First (Component_Associations (N)))))
6144 Expr := Expression (First (Component_Associations (N)));
6145 L_J := Make_Temporary (Loc, 'J');
6148 Make_Iteration_Scheme (Loc,
6149 Loop_Parameter_Specification =>
6150 Make_Loop_Parameter_Specification
6152 Defining_Identifier => L_J,
6153 Discrete_Subtype_Definition => Relocate_Node (Range_Node)));
6156 Make_Assignment_Statement (Loc,
6158 Make_Indexed_Component (Loc,
6159 Prefix => Relocate_Node (Pref),
6160 Expressions => New_List (New_Occurrence_Of (L_J, Loc))),
6161 Expression => Relocate_Node (Expr));
6163 -- Construct the final loop
6166 Make_Implicit_Loop_Statement
6167 (Node => Parent (N),
6168 Identifier => Empty,
6169 Iteration_Scheme => L_Iter,
6170 Statements => New_List (L_Body));
6172 -- Set type of aggregate to be type of lhs in assignment,
6173 -- to suppress redundant length checks.
6175 Set_Etype (N, Etype (Name (Parent (N))));
6177 Rewrite (Parent (N), Stat);
6178 Analyze (Parent (N));
6184 end Safe_Slice_Assignment;
6186 ---------------------
6187 -- Sort_Case_Table --
6188 ---------------------
6190 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type) is
6191 L : constant Int := Case_Table'First;
6192 U : constant Int := Case_Table'Last;
6200 T := Case_Table (K + 1);
6204 and then Expr_Value (Case_Table (J - 1).Choice_Lo) >
6205 Expr_Value (T.Choice_Lo)
6207 Case_Table (J) := Case_Table (J - 1);
6211 Case_Table (J) := T;
6214 end Sort_Case_Table;
6216 ----------------------------
6217 -- Static_Array_Aggregate --
6218 ----------------------------
6220 function Static_Array_Aggregate (N : Node_Id) return Boolean is
6221 Bounds : constant Node_Id := Aggregate_Bounds (N);
6223 Typ : constant Entity_Id := Etype (N);
6224 Comp_Type : constant Entity_Id := Component_Type (Typ);
6231 if Is_Tagged_Type (Typ)
6232 or else Is_Controlled (Typ)
6233 or else Is_Packed (Typ)
6239 and then Nkind (Bounds) = N_Range
6240 and then Nkind (Low_Bound (Bounds)) = N_Integer_Literal
6241 and then Nkind (High_Bound (Bounds)) = N_Integer_Literal
6243 Lo := Low_Bound (Bounds);
6244 Hi := High_Bound (Bounds);
6246 if No (Component_Associations (N)) then
6248 -- Verify that all components are static integers
6250 Expr := First (Expressions (N));
6251 while Present (Expr) loop
6252 if Nkind (Expr) /= N_Integer_Literal then
6262 -- We allow only a single named association, either a static
6263 -- range or an others_clause, with a static expression.
6265 Expr := First (Component_Associations (N));
6267 if Present (Expressions (N)) then
6270 elsif Present (Next (Expr)) then
6273 elsif Present (Next (First (Choices (Expr)))) then
6277 -- The aggregate is static if all components are literals,
6278 -- or else all its components are static aggregates for the
6279 -- component type. We also limit the size of a static aggregate
6280 -- to prevent runaway static expressions.
6282 if Is_Array_Type (Comp_Type)
6283 or else Is_Record_Type (Comp_Type)
6285 if Nkind (Expression (Expr)) /= N_Aggregate
6287 not Compile_Time_Known_Aggregate (Expression (Expr))
6292 elsif Nkind (Expression (Expr)) /= N_Integer_Literal then
6296 if not Aggr_Size_OK (N, Typ) then
6300 -- Create a positional aggregate with the right number of
6301 -- copies of the expression.
6303 Agg := Make_Aggregate (Sloc (N), New_List, No_List);
6305 for I in UI_To_Int (Intval (Lo)) .. UI_To_Int (Intval (Hi))
6308 (Expressions (Agg), New_Copy (Expression (Expr)));
6310 -- The copied expression must be analyzed and resolved.
6311 -- Besides setting the type, this ensures that static
6312 -- expressions are appropriately marked as such.
6315 (Last (Expressions (Agg)), Component_Type (Typ));
6318 Set_Aggregate_Bounds (Agg, Bounds);
6319 Set_Etype (Agg, Typ);
6322 Set_Compile_Time_Known_Aggregate (N);
6331 end Static_Array_Aggregate;