1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2014, 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_Ch6; use Exp_Ch6;
36 with Exp_Ch7; use Exp_Ch7;
37 with Exp_Ch9; use Exp_Ch9;
38 with Exp_Disp; use Exp_Disp;
39 with Exp_Tss; use Exp_Tss;
40 with Fname; use Fname;
41 with Freeze; use Freeze;
42 with Itypes; use Itypes;
44 with Namet; use Namet;
45 with Nmake; use Nmake;
46 with Nlists; use Nlists;
48 with Restrict; use Restrict;
49 with Rident; use Rident;
50 with Rtsfind; use Rtsfind;
51 with Ttypes; use Ttypes;
53 with Sem_Aggr; use Sem_Aggr;
54 with Sem_Aux; use Sem_Aux;
55 with Sem_Ch3; use Sem_Ch3;
56 with Sem_Eval; use Sem_Eval;
57 with Sem_Res; use Sem_Res;
58 with Sem_Util; use Sem_Util;
59 with Sinfo; use Sinfo;
60 with Snames; use Snames;
61 with Stand; use Stand;
62 with Stringt; use Stringt;
63 with Targparm; use Targparm;
64 with Tbuild; use Tbuild;
65 with Uintp; use Uintp;
67 package body Exp_Aggr is
69 type Case_Bounds is record
72 Choice_Node : Node_Id;
75 type Case_Table_Type is array (Nat range <>) of Case_Bounds;
76 -- Table type used by Check_Case_Choices procedure
78 function Has_Default_Init_Comps (N : Node_Id) return Boolean;
79 -- N is an aggregate (record or array). Checks the presence of default
80 -- initialization (<>) in any component (Ada 2005: AI-287).
82 function Is_Static_Dispatch_Table_Aggregate (N : Node_Id) return Boolean;
83 -- Returns true if N is an aggregate used to initialize the components
84 -- of a statically allocated dispatch table.
87 (Obj_Type : Entity_Id;
88 Typ : Entity_Id) return Boolean;
89 -- A static array aggregate in an object declaration can in most cases be
90 -- expanded in place. The one exception is when the aggregate is given
91 -- with component associations that specify different bounds from those of
92 -- the type definition in the object declaration. In this pathological
93 -- case the aggregate must slide, and we must introduce an intermediate
94 -- temporary to hold it.
96 -- The same holds in an assignment to one-dimensional array of arrays,
97 -- when a component may be given with bounds that differ from those of the
100 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type);
101 -- Sort the Case Table using the Lower Bound of each Choice as the key.
102 -- A simple insertion sort is used since the number of choices in a case
103 -- statement of variant part will usually be small and probably in near
106 procedure Collect_Initialization_Statements
109 Node_After : Node_Id);
110 -- If Obj is not frozen, collect actions inserted after N until, but not
111 -- including, Node_After, for initialization of Obj, and move them to an
112 -- expression with actions, which becomes the Initialization_Statements for
115 ------------------------------------------------------
116 -- Local subprograms for Record Aggregate Expansion --
117 ------------------------------------------------------
119 function Build_Record_Aggr_Code
122 Lhs : Node_Id) return List_Id;
123 -- N is an N_Aggregate or an N_Extension_Aggregate. Typ is the type of the
124 -- aggregate. Target is an expression containing the location on which the
125 -- component by component assignments will take place. Returns the list of
126 -- assignments plus all other adjustments needed for tagged and controlled
129 procedure Convert_To_Assignments (N : Node_Id; Typ : Entity_Id);
130 -- N is an N_Aggregate or an N_Extension_Aggregate. Typ is the type of the
131 -- aggregate (which can only be a record type, this procedure is only used
132 -- for record types). Transform the given aggregate into a sequence of
133 -- assignments performed component by component.
135 procedure Expand_Record_Aggregate
137 Orig_Tag : Node_Id := Empty;
138 Parent_Expr : Node_Id := Empty);
139 -- This is the top level procedure for record aggregate expansion.
140 -- Expansion for record aggregates needs expand aggregates for tagged
141 -- record types. Specifically Expand_Record_Aggregate adds the Tag
142 -- field in front of the Component_Association list that was created
143 -- during resolution by Resolve_Record_Aggregate.
145 -- N is the record aggregate node.
146 -- Orig_Tag is the value of the Tag that has to be provided for this
147 -- specific aggregate. It carries the tag corresponding to the type
148 -- of the outermost aggregate during the recursive expansion
149 -- Parent_Expr is the ancestor part of the original extension
152 function Has_Mutable_Components (Typ : Entity_Id) return Boolean;
153 -- Return true if one of the components is of a discriminated type with
154 -- defaults. An aggregate for a type with mutable components must be
155 -- expanded into individual assignments.
157 procedure Initialize_Discriminants (N : Node_Id; Typ : Entity_Id);
158 -- If the type of the aggregate is a type extension with renamed discrimi-
159 -- nants, we must initialize the hidden discriminants of the parent.
160 -- Otherwise, the target object must not be initialized. The discriminants
161 -- are initialized by calling the initialization procedure for the type.
162 -- This is incorrect if the initialization of other components has any
163 -- side effects. We restrict this call to the case where the parent type
164 -- has a variant part, because this is the only case where the hidden
165 -- discriminants are accessed, namely when calling discriminant checking
166 -- functions of the parent type, and when applying a stream attribute to
167 -- an object of the derived type.
169 -----------------------------------------------------
170 -- Local Subprograms for Array Aggregate Expansion --
171 -----------------------------------------------------
173 function Aggr_Size_OK (N : Node_Id; Typ : Entity_Id) return Boolean;
174 -- Very large static aggregates present problems to the back-end, and are
175 -- transformed into assignments and loops. This function verifies that the
176 -- total number of components of an aggregate is acceptable for rewriting
177 -- into a purely positional static form. Aggr_Size_OK must be called before
180 -- This function also detects and warns about one-component aggregates that
181 -- appear in a non-static context. Even if the component value is static,
182 -- such an aggregate must be expanded into an assignment.
184 function Backend_Processing_Possible (N : Node_Id) return Boolean;
185 -- This function checks if array aggregate N can be processed directly
186 -- by the backend. If this is the case, True is returned.
188 function Build_Array_Aggr_Code
193 Scalar_Comp : Boolean;
194 Indexes : List_Id := No_List) return List_Id;
195 -- This recursive routine returns a list of statements containing the
196 -- loops and assignments that are needed for the expansion of the array
199 -- N is the (sub-)aggregate node to be expanded into code. This node has
200 -- been fully analyzed, and its Etype is properly set.
202 -- Index is the index node corresponding to the array sub-aggregate N
204 -- Into is the target expression into which we are copying the aggregate.
205 -- Note that this node may not have been analyzed yet, and so the Etype
206 -- field may not be set.
208 -- Scalar_Comp is True if the component type of the aggregate is scalar
210 -- Indexes is the current list of expressions used to index the object we
213 procedure Convert_Array_Aggr_In_Allocator
217 -- If the aggregate appears within an allocator and can be expanded in
218 -- place, this routine generates the individual assignments to components
219 -- of the designated object. This is an optimization over the general
220 -- case, where a temporary is first created on the stack and then used to
221 -- construct the allocated object on the heap.
223 procedure Convert_To_Positional
225 Max_Others_Replicate : Nat := 5;
226 Handle_Bit_Packed : Boolean := False);
227 -- If possible, convert named notation to positional notation. This
228 -- conversion is possible only in some static cases. If the conversion is
229 -- possible, then N is rewritten with the analyzed converted aggregate.
230 -- The parameter Max_Others_Replicate controls the maximum number of
231 -- values corresponding to an others choice that will be converted to
232 -- positional notation (the default of 5 is the normal limit, and reflects
233 -- the fact that normally the loop is better than a lot of separate
234 -- assignments). Note that this limit gets overridden in any case if
235 -- either of the restrictions No_Elaboration_Code or No_Implicit_Loops is
236 -- set. The parameter Handle_Bit_Packed is usually set False (since we do
237 -- not expect the back end to handle bit packed arrays, so the normal case
238 -- of conversion is pointless), but in the special case of a call from
239 -- Packed_Array_Aggregate_Handled, we set this parameter to True, since
240 -- these are cases we handle in there.
242 -- It would seem worthwhile to have a higher default value for Max_Others_
243 -- replicate, but aggregates in the compiler make this impossible: the
244 -- compiler bootstrap fails if Max_Others_Replicate is greater than 25.
245 -- This is unexpected ???
247 procedure Expand_Array_Aggregate (N : Node_Id);
248 -- This is the top-level routine to perform array aggregate expansion.
249 -- N is the N_Aggregate node to be expanded.
251 function Is_Two_Dim_Packed_Array (Typ : Entity_Id) return Boolean;
252 -- For two-dimensional packed aggregates with constant bounds and constant
253 -- components, it is preferable to pack the inner aggregates because the
254 -- whole matrix can then be presented to the back-end as a one-dimensional
255 -- list of literals. This is much more efficient than expanding into single
256 -- component assignments. This function determines if the type Typ is for
257 -- an array that is suitable for this optimization: it returns True if Typ
258 -- is a two dimensional bit packed array with component size 1, 2, or 4.
260 function Late_Expansion
263 Target : Node_Id) return List_Id;
264 -- This routine implements top-down expansion of nested aggregates. In
265 -- doing so, it avoids the generation of temporaries at each level. N is
266 -- a nested record or array aggregate with the Expansion_Delayed flag.
267 -- Typ is the expected type of the aggregate. Target is a (duplicatable)
268 -- expression that will hold the result of the aggregate expansion.
270 function Make_OK_Assignment_Statement
273 Expression : Node_Id) return Node_Id;
274 -- This is like Make_Assignment_Statement, except that Assignment_OK
275 -- is set in the left operand. All assignments built by this unit use
276 -- this routine. This is needed to deal with assignments to initialized
277 -- constants that are done in place.
279 function Number_Of_Choices (N : Node_Id) return Nat;
280 -- Returns the number of discrete choices (not including the others choice
281 -- if present) contained in (sub-)aggregate N.
283 function Packed_Array_Aggregate_Handled (N : Node_Id) return Boolean;
284 -- Given an array aggregate, this function handles the case of a packed
285 -- array aggregate with all constant values, where the aggregate can be
286 -- evaluated at compile time. If this is possible, then N is rewritten
287 -- to be its proper compile time value with all the components properly
288 -- assembled. The expression is analyzed and resolved and True is returned.
289 -- If this transformation is not possible, N is unchanged and False is
292 function Safe_Slice_Assignment (N : Node_Id) return Boolean;
293 -- If a slice assignment has an aggregate with a single others_choice,
294 -- the assignment can be done in place even if bounds are not static,
295 -- by converting it into a loop over the discrete range of the slice.
297 function Two_Dim_Packed_Array_Handled (N : Node_Id) return Boolean;
298 -- If the type of the aggregate is a two-dimensional bit_packed array
299 -- it may be transformed into an array of bytes with constant values,
300 -- and presented to the back-end as a static value. The function returns
301 -- false if this transformation cannot be performed. THis is similar to,
302 -- and reuses part of the machinery in Packed_Array_Aggregate_Handled.
308 function Aggr_Size_OK (N : Node_Id; Typ : Entity_Id) return Boolean is
317 -- Determines the maximum size of an array aggregate produced by
318 -- converting named to positional notation (e.g. from others clauses).
319 -- This avoids running away with attempts to convert huge aggregates,
320 -- which hit memory limits in the backend.
322 function Component_Count (T : Entity_Id) return Int;
323 -- The limit is applied to the total number of components that the
324 -- aggregate will have, which is the number of static expressions
325 -- that will appear in the flattened array. This requires a recursive
326 -- computation of the number of scalar components of the structure.
328 ---------------------
329 -- Component_Count --
330 ---------------------
332 function Component_Count (T : Entity_Id) return Int is
337 if Is_Scalar_Type (T) then
340 elsif Is_Record_Type (T) then
341 Comp := First_Component (T);
342 while Present (Comp) loop
343 Res := Res + Component_Count (Etype (Comp));
344 Next_Component (Comp);
349 elsif Is_Array_Type (T) then
351 Lo : constant Node_Id :=
352 Type_Low_Bound (Etype (First_Index (T)));
353 Hi : constant Node_Id :=
354 Type_High_Bound (Etype (First_Index (T)));
356 Siz : constant Int := Component_Count (Component_Type (T));
359 if not Compile_Time_Known_Value (Lo)
360 or else not Compile_Time_Known_Value (Hi)
365 Siz * UI_To_Int (Expr_Value (Hi) - Expr_Value (Lo) + 1);
370 -- Can only be a null for an access type
376 -- Start of processing for Aggr_Size_OK
379 -- The normal aggregate limit is 50000, but we increase this limit to
380 -- 2**24 (about 16 million) if Restrictions (No_Elaboration_Code) or
381 -- Restrictions (No_Implicit_Loops) is specified, since in either case
382 -- we are at risk of declaring the program illegal because of this
383 -- limit. We also increase the limit when Static_Elaboration_Desired,
384 -- given that this means that objects are intended to be placed in data
387 -- We also increase the limit if the aggregate is for a packed two-
388 -- dimensional array, because if components are static it is much more
389 -- efficient to construct a one-dimensional equivalent array with static
392 -- Conversely, we decrease the maximum size if none of the above
393 -- requirements apply, and if the aggregate has a single component
394 -- association, which will be more efficient if implemented with a loop.
396 -- Finally, we use a small limit in CodePeer mode where we favor loops
397 -- instead of thousands of single assignments (from large aggregates).
399 Max_Aggr_Size := 50000;
401 if CodePeer_Mode then
402 Max_Aggr_Size := 100;
404 elsif Restriction_Active (No_Elaboration_Code)
405 or else Restriction_Active (No_Implicit_Loops)
406 or else Is_Two_Dim_Packed_Array (Typ)
407 or else ((Ekind (Current_Scope) = E_Package
408 and then Static_Elaboration_Desired (Current_Scope)))
410 Max_Aggr_Size := 2 ** 24;
412 elsif No (Expressions (N))
413 and then No (Next (First (Component_Associations (N))))
415 Max_Aggr_Size := 5000;
418 Siz := Component_Count (Component_Type (Typ));
420 Indx := First_Index (Typ);
421 while Present (Indx) loop
422 Lo := Type_Low_Bound (Etype (Indx));
423 Hi := Type_High_Bound (Etype (Indx));
425 -- Bounds need to be known at compile time
427 if not Compile_Time_Known_Value (Lo)
428 or else not Compile_Time_Known_Value (Hi)
433 Lov := Expr_Value (Lo);
434 Hiv := Expr_Value (Hi);
436 -- A flat array is always safe
442 -- One-component aggregates are suspicious, and if the context type
443 -- is an object declaration with non-static bounds it will trip gcc;
444 -- such an aggregate must be expanded into a single assignment.
447 and then Nkind (Parent (N)) = N_Object_Declaration
450 Index_Type : constant Entity_Id :=
452 (First_Index (Etype (Defining_Identifier (Parent (N)))));
456 if not Compile_Time_Known_Value (Type_Low_Bound (Index_Type))
457 or else not Compile_Time_Known_Value
458 (Type_High_Bound (Index_Type))
460 if Present (Component_Associations (N)) then
462 First (Choices (First (Component_Associations (N))));
464 if Is_Entity_Name (Indx)
465 and then not Is_Type (Entity (Indx))
468 ("single component aggregate in "
469 & "non-static context??", Indx);
470 Error_Msg_N ("\maybe subtype name was meant??", Indx);
480 Rng : constant Uint := Hiv - Lov + 1;
483 -- Check if size is too large
485 if not UI_Is_In_Int_Range (Rng) then
489 Siz := Siz * UI_To_Int (Rng);
493 or else Siz > Max_Aggr_Size
498 -- Bounds must be in integer range, for later array construction
500 if not UI_Is_In_Int_Range (Lov)
502 not UI_Is_In_Int_Range (Hiv)
513 ---------------------------------
514 -- Backend_Processing_Possible --
515 ---------------------------------
517 -- Backend processing by Gigi/gcc is possible only if all the following
518 -- conditions are met:
520 -- 1. N is fully positional
522 -- 2. N is not a bit-packed array aggregate;
524 -- 3. The size of N's array type must be known at compile time. Note
525 -- that this implies that the component size is also known
527 -- 4. The array type of N does not follow the Fortran layout convention
528 -- or if it does it must be 1 dimensional.
530 -- 5. The array component type may not be tagged (which could necessitate
531 -- reassignment of proper tags).
533 -- 6. The array component type must not have unaligned bit components
535 -- 7. None of the components of the aggregate may be bit unaligned
538 -- 8. There cannot be delayed components, since we do not know enough
539 -- at this stage to know if back end processing is possible.
541 -- 9. There cannot be any discriminated record components, since the
542 -- back end cannot handle this complex case.
544 -- 10. No controlled actions need to be generated for components
546 -- 11. For a VM back end, the array should have no aliased components
548 function Backend_Processing_Possible (N : Node_Id) return Boolean is
549 Typ : constant Entity_Id := Etype (N);
550 -- Typ is the correct constrained array subtype of the aggregate
552 function Component_Check (N : Node_Id; Index : Node_Id) return Boolean;
553 -- This routine checks components of aggregate N, enforcing checks
554 -- 1, 7, 8, and 9. In the multi-dimensional case, these checks are
555 -- performed on subaggregates. The Index value is the current index
556 -- being checked in the multi-dimensional case.
558 ---------------------
559 -- Component_Check --
560 ---------------------
562 function Component_Check (N : Node_Id; Index : Node_Id) return Boolean is
566 -- Checks 1: (no component associations)
568 if Present (Component_Associations (N)) then
572 -- Checks on components
574 -- Recurse to check subaggregates, which may appear in qualified
575 -- expressions. If delayed, the front-end will have to expand.
576 -- If the component is a discriminated record, treat as non-static,
577 -- as the back-end cannot handle this properly.
579 Expr := First (Expressions (N));
580 while Present (Expr) loop
582 -- Checks 8: (no delayed components)
584 if Is_Delayed_Aggregate (Expr) then
588 -- Checks 9: (no discriminated records)
590 if Present (Etype (Expr))
591 and then Is_Record_Type (Etype (Expr))
592 and then Has_Discriminants (Etype (Expr))
597 -- Checks 7. Component must not be bit aligned component
599 if Possible_Bit_Aligned_Component (Expr) then
603 -- Recursion to following indexes for multiple dimension case
605 if Present (Next_Index (Index))
606 and then not Component_Check (Expr, Next_Index (Index))
611 -- All checks for that component finished, on to next
619 -- Start of processing for Backend_Processing_Possible
622 -- Checks 2 (array not bit packed) and 10 (no controlled actions)
624 if Is_Bit_Packed_Array (Typ) or else Needs_Finalization (Typ) then
628 -- If component is limited, aggregate must be expanded because each
629 -- component assignment must be built in place.
631 if Is_Limited_View (Component_Type (Typ)) then
635 -- Checks 4 (array must not be multi-dimensional Fortran case)
637 if Convention (Typ) = Convention_Fortran
638 and then Number_Dimensions (Typ) > 1
643 -- Checks 3 (size of array must be known at compile time)
645 if not Size_Known_At_Compile_Time (Typ) then
649 -- Checks on components
651 if not Component_Check (N, First_Index (Typ)) then
655 -- Checks 5 (if the component type is tagged, then we may need to do
656 -- tag adjustments. Perhaps this should be refined to check for any
657 -- component associations that actually need tag adjustment, similar
658 -- to the test in Component_Not_OK_For_Backend for record aggregates
659 -- with tagged components, but not clear whether it's worthwhile ???;
660 -- in the case of the JVM, object tags are handled implicitly)
662 if Is_Tagged_Type (Component_Type (Typ))
663 and then Tagged_Type_Expansion
668 -- Checks 6 (component type must not have bit aligned components)
670 if Type_May_Have_Bit_Aligned_Components (Component_Type (Typ)) then
674 -- Checks 11: Array aggregates with aliased components are currently
675 -- not well supported by the VM backend; disable temporarily this
676 -- backend processing until it is definitely supported.
678 if VM_Target /= No_VM
679 and then Has_Aliased_Components (Base_Type (Typ))
684 -- Backend processing is possible
686 Set_Size_Known_At_Compile_Time (Etype (N), True);
688 end Backend_Processing_Possible;
690 ---------------------------
691 -- Build_Array_Aggr_Code --
692 ---------------------------
694 -- The code that we generate from a one dimensional aggregate is
696 -- 1. If the sub-aggregate contains discrete choices we
698 -- (a) Sort the discrete choices
700 -- (b) Otherwise for each discrete choice that specifies a range we
701 -- emit a loop. If a range specifies a maximum of three values, or
702 -- we are dealing with an expression we emit a sequence of
703 -- assignments instead of a loop.
705 -- (c) Generate the remaining loops to cover the others choice if any
707 -- 2. If the aggregate contains positional elements we
709 -- (a) translate the positional elements in a series of assignments
711 -- (b) Generate a final loop to cover the others choice if any.
712 -- Note that this final loop has to be a while loop since the case
714 -- L : Integer := Integer'Last;
715 -- H : Integer := Integer'Last;
716 -- A : array (L .. H) := (1, others =>0);
718 -- cannot be handled by a for loop. Thus for the following
720 -- array (L .. H) := (.. positional elements.., others =>E);
722 -- we always generate something like:
724 -- J : Index_Type := Index_Of_Last_Positional_Element;
726 -- J := Index_Base'Succ (J)
730 function Build_Array_Aggr_Code
735 Scalar_Comp : Boolean;
736 Indexes : List_Id := No_List) return List_Id
738 Loc : constant Source_Ptr := Sloc (N);
739 Index_Base : constant Entity_Id := Base_Type (Etype (Index));
740 Index_Base_L : constant Node_Id := Type_Low_Bound (Index_Base);
741 Index_Base_H : constant Node_Id := Type_High_Bound (Index_Base);
743 function Add (Val : Int; To : Node_Id) return Node_Id;
744 -- Returns an expression where Val is added to expression To, unless
745 -- To+Val is provably out of To's base type range. To must be an
746 -- already analyzed expression.
748 function Empty_Range (L, H : Node_Id) return Boolean;
749 -- Returns True if the range defined by L .. H is certainly empty
751 function Equal (L, H : Node_Id) return Boolean;
752 -- Returns True if L = H for sure
754 function Index_Base_Name return Node_Id;
755 -- Returns a new reference to the index type name
757 function Gen_Assign (Ind : Node_Id; Expr : Node_Id) return List_Id;
758 -- Ind must be a side-effect free expression. If the input aggregate
759 -- N to Build_Loop contains no sub-aggregates, then this function
760 -- returns the assignment statement:
762 -- Into (Indexes, Ind) := Expr;
764 -- Otherwise we call Build_Code recursively
766 -- Ada 2005 (AI-287): In case of default initialized component, Expr
767 -- is empty and we generate a call to the corresponding IP subprogram.
769 function Gen_Loop (L, H : Node_Id; Expr : Node_Id) return List_Id;
770 -- Nodes L and H must be side-effect free expressions.
771 -- If the input aggregate N to Build_Loop contains no sub-aggregates,
772 -- This routine returns the for loop statement
774 -- for J in Index_Base'(L) .. Index_Base'(H) loop
775 -- Into (Indexes, J) := Expr;
778 -- Otherwise we call Build_Code recursively.
779 -- As an optimization if the loop covers 3 or less scalar elements we
780 -- generate a sequence of assignments.
782 function Gen_While (L, H : Node_Id; Expr : Node_Id) return List_Id;
783 -- Nodes L and H must be side-effect free expressions.
784 -- If the input aggregate N to Build_Loop contains no sub-aggregates,
785 -- This routine returns the while loop statement
787 -- J : Index_Base := L;
789 -- J := Index_Base'Succ (J);
790 -- Into (Indexes, J) := Expr;
793 -- Otherwise we call Build_Code recursively
795 function Local_Compile_Time_Known_Value (E : Node_Id) return Boolean;
796 function Local_Expr_Value (E : Node_Id) return Uint;
797 -- These two Local routines are used to replace the corresponding ones
798 -- in sem_eval because while processing the bounds of an aggregate with
799 -- discrete choices whose index type is an enumeration, we build static
800 -- expressions not recognized by Compile_Time_Known_Value as such since
801 -- they have not yet been analyzed and resolved. All the expressions in
802 -- question are things like Index_Base_Name'Val (Const) which we can
803 -- easily recognize as being constant.
809 function Add (Val : Int; To : Node_Id) return Node_Id is
814 U_Val : constant Uint := UI_From_Int (Val);
817 -- Note: do not try to optimize the case of Val = 0, because
818 -- we need to build a new node with the proper Sloc value anyway.
820 -- First test if we can do constant folding
822 if Local_Compile_Time_Known_Value (To) then
823 U_To := Local_Expr_Value (To) + Val;
825 -- Determine if our constant is outside the range of the index.
826 -- If so return an Empty node. This empty node will be caught
827 -- by Empty_Range below.
829 if Compile_Time_Known_Value (Index_Base_L)
830 and then U_To < Expr_Value (Index_Base_L)
834 elsif Compile_Time_Known_Value (Index_Base_H)
835 and then U_To > Expr_Value (Index_Base_H)
840 Expr_Pos := Make_Integer_Literal (Loc, U_To);
841 Set_Is_Static_Expression (Expr_Pos);
843 if not Is_Enumeration_Type (Index_Base) then
846 -- If we are dealing with enumeration return
847 -- Index_Base'Val (Expr_Pos)
851 Make_Attribute_Reference
853 Prefix => Index_Base_Name,
854 Attribute_Name => Name_Val,
855 Expressions => New_List (Expr_Pos));
861 -- If we are here no constant folding possible
863 if not Is_Enumeration_Type (Index_Base) then
866 Left_Opnd => Duplicate_Subexpr (To),
867 Right_Opnd => Make_Integer_Literal (Loc, U_Val));
869 -- If we are dealing with enumeration return
870 -- Index_Base'Val (Index_Base'Pos (To) + Val)
874 Make_Attribute_Reference
876 Prefix => Index_Base_Name,
877 Attribute_Name => Name_Pos,
878 Expressions => New_List (Duplicate_Subexpr (To)));
883 Right_Opnd => Make_Integer_Literal (Loc, U_Val));
886 Make_Attribute_Reference
888 Prefix => Index_Base_Name,
889 Attribute_Name => Name_Val,
890 Expressions => New_List (Expr_Pos));
900 function Empty_Range (L, H : Node_Id) return Boolean is
901 Is_Empty : Boolean := False;
906 -- First check if L or H were already detected as overflowing the
907 -- index base range type by function Add above. If this is so Add
908 -- returns the empty node.
910 if No (L) or else No (H) then
917 -- L > H range is empty
923 -- B_L > H range must be empty
929 -- L > B_H range must be empty
933 High := Index_Base_H;
936 if Local_Compile_Time_Known_Value (Low)
937 and then Local_Compile_Time_Known_Value (High)
940 UI_Gt (Local_Expr_Value (Low), Local_Expr_Value (High));
953 function Equal (L, H : Node_Id) return Boolean is
958 elsif Local_Compile_Time_Known_Value (L)
959 and then Local_Compile_Time_Known_Value (H)
961 return UI_Eq (Local_Expr_Value (L), Local_Expr_Value (H));
971 function Gen_Assign (Ind : Node_Id; Expr : Node_Id) return List_Id is
972 L : constant List_Id := New_List;
975 New_Indexes : List_Id;
976 Indexed_Comp : Node_Id;
978 Comp_Type : Entity_Id := Empty;
980 function Add_Loop_Actions (Lis : List_Id) return List_Id;
981 -- Collect insert_actions generated in the construction of a
982 -- loop, and prepend them to the sequence of assignments to
983 -- complete the eventual body of the loop.
985 ----------------------
986 -- Add_Loop_Actions --
987 ----------------------
989 function Add_Loop_Actions (Lis : List_Id) return List_Id is
993 -- Ada 2005 (AI-287): Do nothing else in case of default
994 -- initialized component.
999 elsif Nkind (Parent (Expr)) = N_Component_Association
1000 and then Present (Loop_Actions (Parent (Expr)))
1002 Append_List (Lis, Loop_Actions (Parent (Expr)));
1003 Res := Loop_Actions (Parent (Expr));
1004 Set_Loop_Actions (Parent (Expr), No_List);
1010 end Add_Loop_Actions;
1012 -- Start of processing for Gen_Assign
1015 if No (Indexes) then
1016 New_Indexes := New_List;
1018 New_Indexes := New_Copy_List_Tree (Indexes);
1021 Append_To (New_Indexes, Ind);
1023 if Present (Next_Index (Index)) then
1026 Build_Array_Aggr_Code
1029 Index => Next_Index (Index),
1031 Scalar_Comp => Scalar_Comp,
1032 Indexes => New_Indexes));
1035 -- If we get here then we are at a bottom-level (sub-)aggregate
1039 (Make_Indexed_Component (Loc,
1040 Prefix => New_Copy_Tree (Into),
1041 Expressions => New_Indexes));
1043 Set_Assignment_OK (Indexed_Comp);
1045 -- Ada 2005 (AI-287): In case of default initialized component, Expr
1046 -- is not present (and therefore we also initialize Expr_Q to empty).
1050 elsif Nkind (Expr) = N_Qualified_Expression then
1051 Expr_Q := Expression (Expr);
1056 if Present (Etype (N))
1057 and then Etype (N) /= Any_Composite
1059 Comp_Type := Component_Type (Etype (N));
1060 pragma Assert (Comp_Type = Ctype); -- AI-287
1062 elsif Present (Next (First (New_Indexes))) then
1064 -- Ada 2005 (AI-287): Do nothing in case of default initialized
1065 -- component because we have received the component type in
1066 -- the formal parameter Ctype.
1068 -- ??? Some assert pragmas have been added to check if this new
1069 -- formal can be used to replace this code in all cases.
1071 if Present (Expr) then
1073 -- This is a multidimensional array. Recover the component
1074 -- type from the outermost aggregate, because subaggregates
1075 -- do not have an assigned type.
1082 while Present (P) loop
1083 if Nkind (P) = N_Aggregate
1084 and then Present (Etype (P))
1086 Comp_Type := Component_Type (Etype (P));
1094 pragma Assert (Comp_Type = Ctype); -- AI-287
1099 -- Ada 2005 (AI-287): We only analyze the expression in case of non-
1100 -- default initialized components (otherwise Expr_Q is not present).
1103 and then Nkind_In (Expr_Q, N_Aggregate, N_Extension_Aggregate)
1105 -- At this stage the Expression may not have been analyzed yet
1106 -- because the array aggregate code has not been updated to use
1107 -- the Expansion_Delayed flag and avoid analysis altogether to
1108 -- solve the same problem (see Resolve_Aggr_Expr). So let us do
1109 -- the analysis of non-array aggregates now in order to get the
1110 -- value of Expansion_Delayed flag for the inner aggregate ???
1112 if Present (Comp_Type) and then not Is_Array_Type (Comp_Type) then
1113 Analyze_And_Resolve (Expr_Q, Comp_Type);
1116 if Is_Delayed_Aggregate (Expr_Q) then
1118 -- This is either a subaggregate of a multidimensional array,
1119 -- or a component of an array type whose component type is
1120 -- also an array. In the latter case, the expression may have
1121 -- component associations that provide different bounds from
1122 -- those of the component type, and sliding must occur. Instead
1123 -- of decomposing the current aggregate assignment, force the
1124 -- re-analysis of the assignment, so that a temporary will be
1125 -- generated in the usual fashion, and sliding will take place.
1127 if Nkind (Parent (N)) = N_Assignment_Statement
1128 and then Is_Array_Type (Comp_Type)
1129 and then Present (Component_Associations (Expr_Q))
1130 and then Must_Slide (Comp_Type, Etype (Expr_Q))
1132 Set_Expansion_Delayed (Expr_Q, False);
1133 Set_Analyzed (Expr_Q, False);
1138 Late_Expansion (Expr_Q, Etype (Expr_Q), Indexed_Comp));
1143 -- Ada 2005 (AI-287): In case of default initialized component, call
1144 -- the initialization subprogram associated with the component type.
1145 -- If the component type is an access type, add an explicit null
1146 -- assignment, because for the back-end there is an initialization
1147 -- present for the whole aggregate, and no default initialization
1150 -- In addition, if the component type is controlled, we must call
1151 -- its Initialize procedure explicitly, because there is no explicit
1152 -- object creation that will invoke it otherwise.
1155 if Present (Base_Init_Proc (Base_Type (Ctype)))
1156 or else Has_Task (Base_Type (Ctype))
1159 Build_Initialization_Call (Loc,
1160 Id_Ref => Indexed_Comp,
1162 With_Default_Init => True));
1164 elsif Is_Access_Type (Ctype) then
1166 Make_Assignment_Statement (Loc,
1167 Name => Indexed_Comp,
1168 Expression => Make_Null (Loc)));
1171 if Needs_Finalization (Ctype) then
1174 Obj_Ref => New_Copy_Tree (Indexed_Comp),
1180 Make_OK_Assignment_Statement (Loc,
1181 Name => Indexed_Comp,
1182 Expression => New_Copy_Tree (Expr));
1184 -- The target of the assignment may not have been initialized,
1185 -- so it is not possible to call Finalize as expected in normal
1186 -- controlled assignments. We must also avoid using the primitive
1187 -- _assign (which depends on a valid target, and may for example
1188 -- perform discriminant checks on it).
1190 -- Both Finalize and usage of _assign are disabled by setting
1191 -- No_Ctrl_Actions on the assignment. The rest of the controlled
1192 -- actions are done manually with the proper finalization list
1193 -- coming from the context.
1195 Set_No_Ctrl_Actions (A);
1197 -- If this is an aggregate for an array of arrays, each
1198 -- sub-aggregate will be expanded as well, and even with
1199 -- No_Ctrl_Actions the assignments of inner components will
1200 -- require attachment in their assignments to temporaries. These
1201 -- temporaries must be finalized for each subaggregate, to prevent
1202 -- multiple attachments of the same temporary location to same
1203 -- finalization chain (and consequently circular lists). To ensure
1204 -- that finalization takes place for each subaggregate we wrap the
1205 -- assignment in a block.
1207 if Present (Comp_Type)
1208 and then Needs_Finalization (Comp_Type)
1209 and then Is_Array_Type (Comp_Type)
1210 and then Present (Expr)
1213 Make_Block_Statement (Loc,
1214 Handled_Statement_Sequence =>
1215 Make_Handled_Sequence_Of_Statements (Loc,
1216 Statements => New_List (A)));
1221 -- Adjust the tag if tagged (because of possible view
1222 -- conversions), unless compiling for a VM where tags
1225 if Present (Comp_Type)
1226 and then Is_Tagged_Type (Comp_Type)
1227 and then Tagged_Type_Expansion
1230 Full_Typ : constant Entity_Id := Underlying_Type (Comp_Type);
1234 Make_OK_Assignment_Statement (Loc,
1236 Make_Selected_Component (Loc,
1237 Prefix => New_Copy_Tree (Indexed_Comp),
1240 (First_Tag_Component (Full_Typ), Loc)),
1243 Unchecked_Convert_To (RTE (RE_Tag),
1245 (Node (First_Elmt (Access_Disp_Table (Full_Typ))),
1252 -- Adjust and attach the component to the proper final list, which
1253 -- can be the controller of the outer record object or the final
1254 -- list associated with the scope.
1256 -- If the component is itself an array of controlled types, whose
1257 -- value is given by a sub-aggregate, then the attach calls have
1258 -- been generated when individual subcomponent are assigned, and
1259 -- must not be done again to prevent malformed finalization chains
1260 -- (see comments above, concerning the creation of a block to hold
1261 -- inner finalization actions).
1263 if Present (Comp_Type)
1264 and then Needs_Finalization (Comp_Type)
1265 and then not Is_Limited_Type (Comp_Type)
1267 (Is_Array_Type (Comp_Type)
1268 and then Is_Controlled (Component_Type (Comp_Type))
1269 and then Nkind (Expr) = N_Aggregate)
1273 Obj_Ref => New_Copy_Tree (Indexed_Comp),
1278 return Add_Loop_Actions (L);
1285 function Gen_Loop (L, H : Node_Id; Expr : Node_Id) return List_Id is
1295 -- Index_Base'(L) .. Index_Base'(H)
1297 L_Iteration_Scheme : Node_Id;
1298 -- L_J in Index_Base'(L) .. Index_Base'(H)
1301 -- The statements to execute in the loop
1303 S : constant List_Id := New_List;
1304 -- List of statements
1307 -- Copy of expression tree, used for checking purposes
1310 -- If loop bounds define an empty range return the null statement
1312 if Empty_Range (L, H) then
1313 Append_To (S, Make_Null_Statement (Loc));
1315 -- Ada 2005 (AI-287): Nothing else need to be done in case of
1316 -- default initialized component.
1322 -- The expression must be type-checked even though no component
1323 -- of the aggregate will have this value. This is done only for
1324 -- actual components of the array, not for subaggregates. Do
1325 -- the check on a copy, because the expression may be shared
1326 -- among several choices, some of which might be non-null.
1328 if Present (Etype (N))
1329 and then Is_Array_Type (Etype (N))
1330 and then No (Next_Index (Index))
1332 Expander_Mode_Save_And_Set (False);
1333 Tcopy := New_Copy_Tree (Expr);
1334 Set_Parent (Tcopy, N);
1335 Analyze_And_Resolve (Tcopy, Component_Type (Etype (N)));
1336 Expander_Mode_Restore;
1342 -- If loop bounds are the same then generate an assignment
1344 elsif Equal (L, H) then
1345 return Gen_Assign (New_Copy_Tree (L), Expr);
1347 -- If H - L <= 2 then generate a sequence of assignments when we are
1348 -- processing the bottom most aggregate and it contains scalar
1351 elsif No (Next_Index (Index))
1352 and then Scalar_Comp
1353 and then Local_Compile_Time_Known_Value (L)
1354 and then Local_Compile_Time_Known_Value (H)
1355 and then Local_Expr_Value (H) - Local_Expr_Value (L) <= 2
1358 Append_List_To (S, Gen_Assign (New_Copy_Tree (L), Expr));
1359 Append_List_To (S, Gen_Assign (Add (1, To => L), Expr));
1361 if Local_Expr_Value (H) - Local_Expr_Value (L) = 2 then
1362 Append_List_To (S, Gen_Assign (Add (2, To => L), Expr));
1368 -- Otherwise construct the loop, starting with the loop index L_J
1370 L_J := Make_Temporary (Loc, 'J', L);
1372 -- Construct "L .. H" in Index_Base. We use a qualified expression
1373 -- for the bound to convert to the index base, but we don't need
1374 -- to do that if we already have the base type at hand.
1376 if Etype (L) = Index_Base then
1380 Make_Qualified_Expression (Loc,
1381 Subtype_Mark => Index_Base_Name,
1385 if Etype (H) = Index_Base then
1389 Make_Qualified_Expression (Loc,
1390 Subtype_Mark => Index_Base_Name,
1399 -- Construct "for L_J in Index_Base range L .. H"
1401 L_Iteration_Scheme :=
1402 Make_Iteration_Scheme
1404 Loop_Parameter_Specification =>
1405 Make_Loop_Parameter_Specification
1407 Defining_Identifier => L_J,
1408 Discrete_Subtype_Definition => L_Range));
1410 -- Construct the statements to execute in the loop body
1412 L_Body := Gen_Assign (New_Occurrence_Of (L_J, Loc), Expr);
1414 -- Construct the final loop
1416 Append_To (S, Make_Implicit_Loop_Statement
1418 Identifier => Empty,
1419 Iteration_Scheme => L_Iteration_Scheme,
1420 Statements => L_Body));
1422 -- A small optimization: if the aggregate is initialized with a box
1423 -- and the component type has no initialization procedure, remove the
1424 -- useless empty loop.
1426 if Nkind (First (S)) = N_Loop_Statement
1427 and then Is_Empty_List (Statements (First (S)))
1429 return New_List (Make_Null_Statement (Loc));
1439 -- The code built is
1441 -- W_J : Index_Base := L;
1442 -- while W_J < H loop
1443 -- W_J := Index_Base'Succ (W);
1447 function Gen_While (L, H : Node_Id; Expr : Node_Id) return List_Id is
1451 -- W_J : Base_Type := L;
1453 W_Iteration_Scheme : Node_Id;
1456 W_Index_Succ : Node_Id;
1457 -- Index_Base'Succ (J)
1459 W_Increment : Node_Id;
1460 -- W_J := Index_Base'Succ (W)
1462 W_Body : constant List_Id := New_List;
1463 -- The statements to execute in the loop
1465 S : constant List_Id := New_List;
1466 -- list of statement
1469 -- If loop bounds define an empty range or are equal return null
1471 if Empty_Range (L, H) or else Equal (L, H) then
1472 Append_To (S, Make_Null_Statement (Loc));
1476 -- Build the decl of W_J
1478 W_J := Make_Temporary (Loc, 'J', L);
1480 Make_Object_Declaration
1482 Defining_Identifier => W_J,
1483 Object_Definition => Index_Base_Name,
1486 -- Theoretically we should do a New_Copy_Tree (L) here, but we know
1487 -- that in this particular case L is a fresh Expr generated by
1488 -- Add which we are the only ones to use.
1490 Append_To (S, W_Decl);
1492 -- Construct " while W_J < H"
1494 W_Iteration_Scheme :=
1495 Make_Iteration_Scheme
1497 Condition => Make_Op_Lt
1499 Left_Opnd => New_Occurrence_Of (W_J, Loc),
1500 Right_Opnd => New_Copy_Tree (H)));
1502 -- Construct the statements to execute in the loop body
1505 Make_Attribute_Reference
1507 Prefix => Index_Base_Name,
1508 Attribute_Name => Name_Succ,
1509 Expressions => New_List (New_Occurrence_Of (W_J, Loc)));
1512 Make_OK_Assignment_Statement
1514 Name => New_Occurrence_Of (W_J, Loc),
1515 Expression => W_Index_Succ);
1517 Append_To (W_Body, W_Increment);
1518 Append_List_To (W_Body,
1519 Gen_Assign (New_Occurrence_Of (W_J, Loc), Expr));
1521 -- Construct the final loop
1523 Append_To (S, Make_Implicit_Loop_Statement
1525 Identifier => Empty,
1526 Iteration_Scheme => W_Iteration_Scheme,
1527 Statements => W_Body));
1532 ---------------------
1533 -- Index_Base_Name --
1534 ---------------------
1536 function Index_Base_Name return Node_Id is
1538 return New_Occurrence_Of (Index_Base, Sloc (N));
1539 end Index_Base_Name;
1541 ------------------------------------
1542 -- Local_Compile_Time_Known_Value --
1543 ------------------------------------
1545 function Local_Compile_Time_Known_Value (E : Node_Id) return Boolean is
1547 return Compile_Time_Known_Value (E)
1549 (Nkind (E) = N_Attribute_Reference
1550 and then Attribute_Name (E) = Name_Val
1551 and then Compile_Time_Known_Value (First (Expressions (E))));
1552 end Local_Compile_Time_Known_Value;
1554 ----------------------
1555 -- Local_Expr_Value --
1556 ----------------------
1558 function Local_Expr_Value (E : Node_Id) return Uint is
1560 if Compile_Time_Known_Value (E) then
1561 return Expr_Value (E);
1563 return Expr_Value (First (Expressions (E)));
1565 end Local_Expr_Value;
1567 -- Build_Array_Aggr_Code Variables
1574 Others_Expr : Node_Id := Empty;
1575 Others_Box_Present : Boolean := False;
1577 Aggr_L : constant Node_Id := Low_Bound (Aggregate_Bounds (N));
1578 Aggr_H : constant Node_Id := High_Bound (Aggregate_Bounds (N));
1579 -- The aggregate bounds of this specific sub-aggregate. Note that if
1580 -- the code generated by Build_Array_Aggr_Code is executed then these
1581 -- bounds are OK. Otherwise a Constraint_Error would have been raised.
1583 Aggr_Low : constant Node_Id := Duplicate_Subexpr_No_Checks (Aggr_L);
1584 Aggr_High : constant Node_Id := Duplicate_Subexpr_No_Checks (Aggr_H);
1585 -- After Duplicate_Subexpr these are side-effect free
1590 Nb_Choices : Nat := 0;
1591 Table : Case_Table_Type (1 .. Number_Of_Choices (N));
1592 -- Used to sort all the different choice values
1595 -- Number of elements in the positional aggregate
1597 New_Code : constant List_Id := New_List;
1599 -- Start of processing for Build_Array_Aggr_Code
1602 -- First before we start, a special case. if we have a bit packed
1603 -- array represented as a modular type, then clear the value to
1604 -- zero first, to ensure that unused bits are properly cleared.
1609 and then Is_Bit_Packed_Array (Typ)
1610 and then Is_Modular_Integer_Type (Packed_Array_Type (Typ))
1612 Append_To (New_Code,
1613 Make_Assignment_Statement (Loc,
1614 Name => New_Copy_Tree (Into),
1616 Unchecked_Convert_To (Typ,
1617 Make_Integer_Literal (Loc, Uint_0))));
1620 -- If the component type contains tasks, we need to build a Master
1621 -- entity in the current scope, because it will be needed if build-
1622 -- in-place functions are called in the expanded code.
1624 if Nkind (Parent (N)) = N_Object_Declaration
1625 and then Has_Task (Typ)
1627 Build_Master_Entity (Defining_Identifier (Parent (N)));
1630 -- STEP 1: Process component associations
1632 -- For those associations that may generate a loop, initialize
1633 -- Loop_Actions to collect inserted actions that may be crated.
1635 -- Skip this if no component associations
1637 if No (Expressions (N)) then
1639 -- STEP 1 (a): Sort the discrete choices
1641 Assoc := First (Component_Associations (N));
1642 while Present (Assoc) loop
1643 Choice := First (Choices (Assoc));
1644 while Present (Choice) loop
1645 if Nkind (Choice) = N_Others_Choice then
1646 Set_Loop_Actions (Assoc, New_List);
1648 if Box_Present (Assoc) then
1649 Others_Box_Present := True;
1651 Others_Expr := Expression (Assoc);
1656 Get_Index_Bounds (Choice, Low, High);
1659 Set_Loop_Actions (Assoc, New_List);
1662 Nb_Choices := Nb_Choices + 1;
1663 if Box_Present (Assoc) then
1664 Table (Nb_Choices) := (Choice_Lo => Low,
1666 Choice_Node => Empty);
1668 Table (Nb_Choices) := (Choice_Lo => Low,
1670 Choice_Node => Expression (Assoc));
1678 -- If there is more than one set of choices these must be static
1679 -- and we can therefore sort them. Remember that Nb_Choices does not
1680 -- account for an others choice.
1682 if Nb_Choices > 1 then
1683 Sort_Case_Table (Table);
1686 -- STEP 1 (b): take care of the whole set of discrete choices
1688 for J in 1 .. Nb_Choices loop
1689 Low := Table (J).Choice_Lo;
1690 High := Table (J).Choice_Hi;
1691 Expr := Table (J).Choice_Node;
1692 Append_List (Gen_Loop (Low, High, Expr), To => New_Code);
1695 -- STEP 1 (c): generate the remaining loops to cover others choice
1696 -- We don't need to generate loops over empty gaps, but if there is
1697 -- a single empty range we must analyze the expression for semantics
1699 if Present (Others_Expr) or else Others_Box_Present then
1701 First : Boolean := True;
1704 for J in 0 .. Nb_Choices loop
1708 Low := Add (1, To => Table (J).Choice_Hi);
1711 if J = Nb_Choices then
1714 High := Add (-1, To => Table (J + 1).Choice_Lo);
1717 -- If this is an expansion within an init proc, make
1718 -- sure that discriminant references are replaced by
1719 -- the corresponding discriminal.
1721 if Inside_Init_Proc then
1722 if Is_Entity_Name (Low)
1723 and then Ekind (Entity (Low)) = E_Discriminant
1725 Set_Entity (Low, Discriminal (Entity (Low)));
1728 if Is_Entity_Name (High)
1729 and then Ekind (Entity (High)) = E_Discriminant
1731 Set_Entity (High, Discriminal (Entity (High)));
1736 or else not Empty_Range (Low, High)
1740 (Gen_Loop (Low, High, Others_Expr), To => New_Code);
1746 -- STEP 2: Process positional components
1749 -- STEP 2 (a): Generate the assignments for each positional element
1750 -- Note that here we have to use Aggr_L rather than Aggr_Low because
1751 -- Aggr_L is analyzed and Add wants an analyzed expression.
1753 Expr := First (Expressions (N));
1755 while Present (Expr) loop
1756 Nb_Elements := Nb_Elements + 1;
1757 Append_List (Gen_Assign (Add (Nb_Elements, To => Aggr_L), Expr),
1762 -- STEP 2 (b): Generate final loop if an others choice is present
1763 -- Here Nb_Elements gives the offset of the last positional element.
1765 if Present (Component_Associations (N)) then
1766 Assoc := Last (Component_Associations (N));
1768 -- Ada 2005 (AI-287)
1770 if Box_Present (Assoc) then
1771 Append_List (Gen_While (Add (Nb_Elements, To => Aggr_L),
1776 Expr := Expression (Assoc);
1778 Append_List (Gen_While (Add (Nb_Elements, To => Aggr_L),
1787 end Build_Array_Aggr_Code;
1789 ----------------------------
1790 -- Build_Record_Aggr_Code --
1791 ----------------------------
1793 function Build_Record_Aggr_Code
1796 Lhs : Node_Id) return List_Id
1798 Loc : constant Source_Ptr := Sloc (N);
1799 L : constant List_Id := New_List;
1800 N_Typ : constant Entity_Id := Etype (N);
1806 Comp_Type : Entity_Id;
1807 Selector : Entity_Id;
1808 Comp_Expr : Node_Id;
1811 -- If this is an internal aggregate, the External_Final_List is an
1812 -- expression for the controller record of the enclosing type.
1814 -- If the current aggregate has several controlled components, this
1815 -- expression will appear in several calls to attach to the finali-
1816 -- zation list, and it must not be shared.
1818 Ancestor_Is_Expression : Boolean := False;
1819 Ancestor_Is_Subtype_Mark : Boolean := False;
1821 Init_Typ : Entity_Id := Empty;
1823 Finalization_Done : Boolean := False;
1824 -- True if Generate_Finalization_Actions has already been called; calls
1825 -- after the first do nothing.
1827 function Ancestor_Discriminant_Value (Disc : Entity_Id) return Node_Id;
1828 -- Returns the value that the given discriminant of an ancestor type
1829 -- should receive (in the absence of a conflict with the value provided
1830 -- by an ancestor part of an extension aggregate).
1832 procedure Check_Ancestor_Discriminants (Anc_Typ : Entity_Id);
1833 -- Check that each of the discriminant values defined by the ancestor
1834 -- part of an extension aggregate match the corresponding values
1835 -- provided by either an association of the aggregate or by the
1836 -- constraint imposed by a parent type (RM95-4.3.2(8)).
1838 function Compatible_Int_Bounds
1839 (Agg_Bounds : Node_Id;
1840 Typ_Bounds : Node_Id) return Boolean;
1841 -- Return true if Agg_Bounds are equal or within Typ_Bounds. It is
1842 -- assumed that both bounds are integer ranges.
1844 procedure Generate_Finalization_Actions;
1845 -- Deal with the various controlled type data structure initializations
1846 -- (but only if it hasn't been done already).
1848 function Get_Constraint_Association (T : Entity_Id) return Node_Id;
1849 -- Returns the first discriminant association in the constraint
1850 -- associated with T, if any, otherwise returns Empty.
1852 procedure Init_Hidden_Discriminants (Typ : Entity_Id; List : List_Id);
1853 -- If Typ is derived, and constrains discriminants of the parent type,
1854 -- these discriminants are not components of the aggregate, and must be
1855 -- initialized. The assignments are appended to List.
1857 function Get_Explicit_Discriminant_Value (D : Entity_Id) return Node_Id;
1858 -- If the ancestor part is an unconstrained type and further ancestors
1859 -- do not provide discriminants for it, check aggregate components for
1860 -- values of the discriminants.
1862 function Is_Int_Range_Bounds (Bounds : Node_Id) return Boolean;
1863 -- Check whether Bounds is a range node and its lower and higher bounds
1864 -- are integers literals.
1866 ---------------------------------
1867 -- Ancestor_Discriminant_Value --
1868 ---------------------------------
1870 function Ancestor_Discriminant_Value (Disc : Entity_Id) return Node_Id is
1872 Assoc_Elmt : Elmt_Id;
1873 Aggr_Comp : Entity_Id;
1874 Corresp_Disc : Entity_Id;
1875 Current_Typ : Entity_Id := Base_Type (Typ);
1876 Parent_Typ : Entity_Id;
1877 Parent_Disc : Entity_Id;
1878 Save_Assoc : Node_Id := Empty;
1881 -- First check any discriminant associations to see if any of them
1882 -- provide a value for the discriminant.
1884 if Present (Discriminant_Specifications (Parent (Current_Typ))) then
1885 Assoc := First (Component_Associations (N));
1886 while Present (Assoc) loop
1887 Aggr_Comp := Entity (First (Choices (Assoc)));
1889 if Ekind (Aggr_Comp) = E_Discriminant then
1890 Save_Assoc := Expression (Assoc);
1892 Corresp_Disc := Corresponding_Discriminant (Aggr_Comp);
1893 while Present (Corresp_Disc) loop
1895 -- If found a corresponding discriminant then return the
1896 -- value given in the aggregate. (Note: this is not
1897 -- correct in the presence of side effects. ???)
1899 if Disc = Corresp_Disc then
1900 return Duplicate_Subexpr (Expression (Assoc));
1904 Corresponding_Discriminant (Corresp_Disc);
1912 -- No match found in aggregate, so chain up parent types to find
1913 -- a constraint that defines the value of the discriminant.
1915 Parent_Typ := Etype (Current_Typ);
1916 while Current_Typ /= Parent_Typ loop
1917 if Has_Discriminants (Parent_Typ)
1918 and then not Has_Unknown_Discriminants (Parent_Typ)
1920 Parent_Disc := First_Discriminant (Parent_Typ);
1922 -- We either get the association from the subtype indication
1923 -- of the type definition itself, or from the discriminant
1924 -- constraint associated with the type entity (which is
1925 -- preferable, but it's not always present ???)
1927 if Is_Empty_Elmt_List (
1928 Discriminant_Constraint (Current_Typ))
1930 Assoc := Get_Constraint_Association (Current_Typ);
1931 Assoc_Elmt := No_Elmt;
1934 First_Elmt (Discriminant_Constraint (Current_Typ));
1935 Assoc := Node (Assoc_Elmt);
1938 -- Traverse the discriminants of the parent type looking
1939 -- for one that corresponds.
1941 while Present (Parent_Disc) and then Present (Assoc) loop
1942 Corresp_Disc := Parent_Disc;
1943 while Present (Corresp_Disc)
1944 and then Disc /= Corresp_Disc
1947 Corresponding_Discriminant (Corresp_Disc);
1950 if Disc = Corresp_Disc then
1951 if Nkind (Assoc) = N_Discriminant_Association then
1952 Assoc := Expression (Assoc);
1955 -- If the located association directly denotes a
1956 -- discriminant, then use the value of a saved
1957 -- association of the aggregate. This is a kludge to
1958 -- handle certain cases involving multiple discriminants
1959 -- mapped to a single discriminant of a descendant. It's
1960 -- not clear how to locate the appropriate discriminant
1961 -- value for such cases. ???
1963 if Is_Entity_Name (Assoc)
1964 and then Ekind (Entity (Assoc)) = E_Discriminant
1966 Assoc := Save_Assoc;
1969 return Duplicate_Subexpr (Assoc);
1972 Next_Discriminant (Parent_Disc);
1974 if No (Assoc_Elmt) then
1977 Next_Elmt (Assoc_Elmt);
1978 if Present (Assoc_Elmt) then
1979 Assoc := Node (Assoc_Elmt);
1987 Current_Typ := Parent_Typ;
1988 Parent_Typ := Etype (Current_Typ);
1991 -- In some cases there's no ancestor value to locate (such as
1992 -- when an ancestor part given by an expression defines the
1993 -- discriminant value).
1996 end Ancestor_Discriminant_Value;
1998 ----------------------------------
1999 -- Check_Ancestor_Discriminants --
2000 ----------------------------------
2002 procedure Check_Ancestor_Discriminants (Anc_Typ : Entity_Id) is
2004 Disc_Value : Node_Id;
2008 Discr := First_Discriminant (Base_Type (Anc_Typ));
2009 while Present (Discr) loop
2010 Disc_Value := Ancestor_Discriminant_Value (Discr);
2012 if Present (Disc_Value) then
2013 Cond := Make_Op_Ne (Loc,
2015 Make_Selected_Component (Loc,
2016 Prefix => New_Copy_Tree (Target),
2017 Selector_Name => New_Occurrence_Of (Discr, Loc)),
2018 Right_Opnd => Disc_Value);
2021 Make_Raise_Constraint_Error (Loc,
2023 Reason => CE_Discriminant_Check_Failed));
2026 Next_Discriminant (Discr);
2028 end Check_Ancestor_Discriminants;
2030 ---------------------------
2031 -- Compatible_Int_Bounds --
2032 ---------------------------
2034 function Compatible_Int_Bounds
2035 (Agg_Bounds : Node_Id;
2036 Typ_Bounds : Node_Id) return Boolean
2038 Agg_Lo : constant Uint := Intval (Low_Bound (Agg_Bounds));
2039 Agg_Hi : constant Uint := Intval (High_Bound (Agg_Bounds));
2040 Typ_Lo : constant Uint := Intval (Low_Bound (Typ_Bounds));
2041 Typ_Hi : constant Uint := Intval (High_Bound (Typ_Bounds));
2043 return Typ_Lo <= Agg_Lo and then Agg_Hi <= Typ_Hi;
2044 end Compatible_Int_Bounds;
2046 --------------------------------
2047 -- Get_Constraint_Association --
2048 --------------------------------
2050 function Get_Constraint_Association (T : Entity_Id) return Node_Id is
2057 -- Handle private types in instances
2060 and then Is_Private_Type (Typ)
2061 and then Present (Full_View (Typ))
2063 Typ := Full_View (Typ);
2066 Indic := Subtype_Indication (Type_Definition (Parent (Typ)));
2068 -- ??? Also need to cover case of a type mark denoting a subtype
2071 if Nkind (Indic) = N_Subtype_Indication
2072 and then Present (Constraint (Indic))
2074 return First (Constraints (Constraint (Indic)));
2078 end Get_Constraint_Association;
2080 -------------------------------------
2081 -- Get_Explicit_Discriminant_Value --
2082 -------------------------------------
2084 function Get_Explicit_Discriminant_Value
2085 (D : Entity_Id) return Node_Id
2092 -- The aggregate has been normalized and all associations have a
2095 Assoc := First (Component_Associations (N));
2096 while Present (Assoc) loop
2097 Choice := First (Choices (Assoc));
2099 if Chars (Choice) = Chars (D) then
2100 Val := Expression (Assoc);
2109 end Get_Explicit_Discriminant_Value;
2111 -------------------------------
2112 -- Init_Hidden_Discriminants --
2113 -------------------------------
2115 procedure Init_Hidden_Discriminants (Typ : Entity_Id; List : List_Id) is
2117 Parent_Type : Entity_Id;
2119 Discr_Val : Elmt_Id;
2122 Btype := Base_Type (Typ);
2123 while Is_Derived_Type (Btype)
2124 and then Present (Stored_Constraint (Btype))
2126 Parent_Type := Etype (Btype);
2128 Disc := First_Discriminant (Parent_Type);
2129 Discr_Val := First_Elmt (Stored_Constraint (Base_Type (Typ)));
2130 while Present (Discr_Val) loop
2132 -- Only those discriminants of the parent that are not
2133 -- renamed by discriminants of the derived type need to
2134 -- be added explicitly.
2136 if not Is_Entity_Name (Node (Discr_Val))
2137 or else Ekind (Entity (Node (Discr_Val))) /= E_Discriminant
2140 Make_Selected_Component (Loc,
2141 Prefix => New_Copy_Tree (Target),
2142 Selector_Name => New_Occurrence_Of (Disc, Loc));
2145 Make_OK_Assignment_Statement (Loc,
2147 Expression => New_Copy_Tree (Node (Discr_Val)));
2149 Set_No_Ctrl_Actions (Instr);
2150 Append_To (List, Instr);
2153 Next_Discriminant (Disc);
2154 Next_Elmt (Discr_Val);
2157 Btype := Base_Type (Parent_Type);
2159 end Init_Hidden_Discriminants;
2161 -------------------------
2162 -- Is_Int_Range_Bounds --
2163 -------------------------
2165 function Is_Int_Range_Bounds (Bounds : Node_Id) return Boolean is
2167 return Nkind (Bounds) = N_Range
2168 and then Nkind (Low_Bound (Bounds)) = N_Integer_Literal
2169 and then Nkind (High_Bound (Bounds)) = N_Integer_Literal;
2170 end Is_Int_Range_Bounds;
2172 -----------------------------------
2173 -- Generate_Finalization_Actions --
2174 -----------------------------------
2176 procedure Generate_Finalization_Actions is
2178 -- Do the work only the first time this is called
2180 if Finalization_Done then
2184 Finalization_Done := True;
2186 -- Determine the external finalization list. It is either the
2187 -- finalization list of the outer-scope or the one coming from
2188 -- an outer aggregate. When the target is not a temporary, the
2189 -- proper scope is the scope of the target rather than the
2190 -- potentially transient current scope.
2192 if Is_Controlled (Typ)
2193 and then Ancestor_Is_Subtype_Mark
2195 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
2196 Set_Assignment_OK (Ref);
2199 Make_Procedure_Call_Statement (Loc,
2202 (Find_Prim_Op (Init_Typ, Name_Initialize), Loc),
2203 Parameter_Associations => New_List (New_Copy_Tree (Ref))));
2205 end Generate_Finalization_Actions;
2207 function Rewrite_Discriminant (Expr : Node_Id) return Traverse_Result;
2208 -- If default expression of a component mentions a discriminant of the
2209 -- type, it must be rewritten as the discriminant of the target object.
2211 function Replace_Type (Expr : Node_Id) return Traverse_Result;
2212 -- If the aggregate contains a self-reference, traverse each expression
2213 -- to replace a possible self-reference with a reference to the proper
2214 -- component of the target of the assignment.
2216 --------------------------
2217 -- Rewrite_Discriminant --
2218 --------------------------
2220 function Rewrite_Discriminant (Expr : Node_Id) return Traverse_Result is
2222 if Is_Entity_Name (Expr)
2223 and then Present (Entity (Expr))
2224 and then Ekind (Entity (Expr)) = E_In_Parameter
2225 and then Present (Discriminal_Link (Entity (Expr)))
2226 and then Scope (Discriminal_Link (Entity (Expr)))
2227 = Base_Type (Etype (N))
2230 Make_Selected_Component (Loc,
2231 Prefix => New_Copy_Tree (Lhs),
2232 Selector_Name => Make_Identifier (Loc, Chars (Expr))));
2235 end Rewrite_Discriminant;
2241 function Replace_Type (Expr : Node_Id) return Traverse_Result is
2243 -- Note regarding the Root_Type test below: Aggregate components for
2244 -- self-referential types include attribute references to the current
2245 -- instance, of the form: Typ'access, etc.. These references are
2246 -- rewritten as references to the target of the aggregate: the
2247 -- left-hand side of an assignment, the entity in a declaration,
2248 -- or a temporary. Without this test, we would improperly extended
2249 -- this rewriting to attribute references whose prefix was not the
2250 -- type of the aggregate.
2252 if Nkind (Expr) = N_Attribute_Reference
2253 and then Is_Entity_Name (Prefix (Expr))
2254 and then Is_Type (Entity (Prefix (Expr)))
2255 and then Root_Type (Etype (N)) = Root_Type (Entity (Prefix (Expr)))
2257 if Is_Entity_Name (Lhs) then
2258 Rewrite (Prefix (Expr),
2259 New_Occurrence_Of (Entity (Lhs), Loc));
2261 elsif Nkind (Lhs) = N_Selected_Component then
2263 Make_Attribute_Reference (Loc,
2264 Attribute_Name => Name_Unrestricted_Access,
2265 Prefix => New_Copy_Tree (Lhs)));
2266 Set_Analyzed (Parent (Expr), False);
2270 Make_Attribute_Reference (Loc,
2271 Attribute_Name => Name_Unrestricted_Access,
2272 Prefix => New_Copy_Tree (Lhs)));
2273 Set_Analyzed (Parent (Expr), False);
2280 procedure Replace_Self_Reference is
2281 new Traverse_Proc (Replace_Type);
2283 procedure Replace_Discriminants is
2284 new Traverse_Proc (Rewrite_Discriminant);
2286 -- Start of processing for Build_Record_Aggr_Code
2289 if Has_Self_Reference (N) then
2290 Replace_Self_Reference (N);
2293 -- If the target of the aggregate is class-wide, we must convert it
2294 -- to the actual type of the aggregate, so that the proper components
2295 -- are visible. We know already that the types are compatible.
2297 if Present (Etype (Lhs))
2298 and then Is_Class_Wide_Type (Etype (Lhs))
2300 Target := Unchecked_Convert_To (Typ, Lhs);
2305 -- Deal with the ancestor part of extension aggregates or with the
2306 -- discriminants of the root type.
2308 if Nkind (N) = N_Extension_Aggregate then
2310 Ancestor : constant Node_Id := Ancestor_Part (N);
2314 -- If the ancestor part is a subtype mark "T", we generate
2316 -- init-proc (T (tmp)); if T is constrained and
2317 -- init-proc (S (tmp)); where S applies an appropriate
2318 -- constraint if T is unconstrained
2320 if Is_Entity_Name (Ancestor)
2321 and then Is_Type (Entity (Ancestor))
2323 Ancestor_Is_Subtype_Mark := True;
2325 if Is_Constrained (Entity (Ancestor)) then
2326 Init_Typ := Entity (Ancestor);
2328 -- For an ancestor part given by an unconstrained type mark,
2329 -- create a subtype constrained by appropriate corresponding
2330 -- discriminant values coming from either associations of the
2331 -- aggregate or a constraint on a parent type. The subtype will
2332 -- be used to generate the correct default value for the
2335 elsif Has_Discriminants (Entity (Ancestor)) then
2337 Anc_Typ : constant Entity_Id := Entity (Ancestor);
2338 Anc_Constr : constant List_Id := New_List;
2339 Discrim : Entity_Id;
2340 Disc_Value : Node_Id;
2341 New_Indic : Node_Id;
2342 Subt_Decl : Node_Id;
2345 Discrim := First_Discriminant (Anc_Typ);
2346 while Present (Discrim) loop
2347 Disc_Value := Ancestor_Discriminant_Value (Discrim);
2349 -- If no usable discriminant in ancestors, check
2350 -- whether aggregate has an explicit value for it.
2352 if No (Disc_Value) then
2354 Get_Explicit_Discriminant_Value (Discrim);
2357 Append_To (Anc_Constr, Disc_Value);
2358 Next_Discriminant (Discrim);
2362 Make_Subtype_Indication (Loc,
2363 Subtype_Mark => New_Occurrence_Of (Anc_Typ, Loc),
2365 Make_Index_Or_Discriminant_Constraint (Loc,
2366 Constraints => Anc_Constr));
2368 Init_Typ := Create_Itype (Ekind (Anc_Typ), N);
2371 Make_Subtype_Declaration (Loc,
2372 Defining_Identifier => Init_Typ,
2373 Subtype_Indication => New_Indic);
2375 -- Itypes must be analyzed with checks off Declaration
2376 -- must have a parent for proper handling of subsidiary
2379 Set_Parent (Subt_Decl, N);
2380 Analyze (Subt_Decl, Suppress => All_Checks);
2384 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
2385 Set_Assignment_OK (Ref);
2387 if not Is_Interface (Init_Typ) then
2389 Build_Initialization_Call (Loc,
2392 In_Init_Proc => Within_Init_Proc,
2393 With_Default_Init => Has_Default_Init_Comps (N)
2395 Has_Task (Base_Type (Init_Typ))));
2397 if Is_Constrained (Entity (Ancestor))
2398 and then Has_Discriminants (Entity (Ancestor))
2400 Check_Ancestor_Discriminants (Entity (Ancestor));
2404 -- Handle calls to C++ constructors
2406 elsif Is_CPP_Constructor_Call (Ancestor) then
2407 Init_Typ := Etype (Ancestor);
2408 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
2409 Set_Assignment_OK (Ref);
2412 Build_Initialization_Call (Loc,
2415 In_Init_Proc => Within_Init_Proc,
2416 With_Default_Init => Has_Default_Init_Comps (N),
2417 Constructor_Ref => Ancestor));
2419 -- Ada 2005 (AI-287): If the ancestor part is an aggregate of
2420 -- limited type, a recursive call expands the ancestor. Note that
2421 -- in the limited case, the ancestor part must be either a
2422 -- function call (possibly qualified, or wrapped in an unchecked
2423 -- conversion) or aggregate (definitely qualified).
2424 -- The ancestor part can also be a function call (that may be
2425 -- transformed into an explicit dereference) or a qualification
2428 elsif Is_Limited_Type (Etype (Ancestor))
2429 and then Nkind_In (Unqualify (Ancestor), N_Aggregate,
2430 N_Extension_Aggregate)
2432 Ancestor_Is_Expression := True;
2434 -- Set up finalization data for enclosing record, because
2435 -- controlled subcomponents of the ancestor part will be
2438 Generate_Finalization_Actions;
2441 Build_Record_Aggr_Code
2442 (N => Unqualify (Ancestor),
2443 Typ => Etype (Unqualify (Ancestor)),
2446 -- If the ancestor part is an expression "E", we generate
2450 -- In Ada 2005, this includes the case of a (possibly qualified)
2451 -- limited function call. The assignment will turn into a
2452 -- build-in-place function call (for further details, see
2453 -- Make_Build_In_Place_Call_In_Assignment).
2456 Ancestor_Is_Expression := True;
2457 Init_Typ := Etype (Ancestor);
2459 -- If the ancestor part is an aggregate, force its full
2460 -- expansion, which was delayed.
2462 if Nkind_In (Unqualify (Ancestor), N_Aggregate,
2463 N_Extension_Aggregate)
2465 Set_Analyzed (Ancestor, False);
2466 Set_Analyzed (Expression (Ancestor), False);
2469 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
2470 Set_Assignment_OK (Ref);
2472 -- Make the assignment without usual controlled actions, since
2473 -- we only want to Adjust afterwards, but not to Finalize
2474 -- beforehand. Add manual Adjust when necessary.
2476 Assign := New_List (
2477 Make_OK_Assignment_Statement (Loc,
2479 Expression => Ancestor));
2480 Set_No_Ctrl_Actions (First (Assign));
2482 -- Assign the tag now to make sure that the dispatching call in
2483 -- the subsequent deep_adjust works properly (unless VM_Target,
2484 -- where tags are implicit).
2486 if Tagged_Type_Expansion then
2488 Make_OK_Assignment_Statement (Loc,
2490 Make_Selected_Component (Loc,
2491 Prefix => New_Copy_Tree (Target),
2494 (First_Tag_Component (Base_Type (Typ)), Loc)),
2497 Unchecked_Convert_To (RTE (RE_Tag),
2500 (Access_Disp_Table (Base_Type (Typ)))),
2503 Set_Assignment_OK (Name (Instr));
2504 Append_To (Assign, Instr);
2506 -- Ada 2005 (AI-251): If tagged type has progenitors we must
2507 -- also initialize tags of the secondary dispatch tables.
2509 if Has_Interfaces (Base_Type (Typ)) then
2511 (Typ => Base_Type (Typ),
2513 Stmts_List => Assign);
2517 -- Call Adjust manually
2519 if Needs_Finalization (Etype (Ancestor))
2520 and then not Is_Limited_Type (Etype (Ancestor))
2524 Obj_Ref => New_Copy_Tree (Ref),
2525 Typ => Etype (Ancestor)));
2529 Make_Unsuppress_Block (Loc, Name_Discriminant_Check, Assign));
2531 if Has_Discriminants (Init_Typ) then
2532 Check_Ancestor_Discriminants (Init_Typ);
2537 -- Generate assignments of hidden discriminants. If the base type is
2538 -- an unchecked union, the discriminants are unknown to the back-end
2539 -- and absent from a value of the type, so assignments for them are
2542 if Has_Discriminants (Typ)
2543 and then not Is_Unchecked_Union (Base_Type (Typ))
2545 Init_Hidden_Discriminants (Typ, L);
2548 -- Normal case (not an extension aggregate)
2551 -- Generate the discriminant expressions, component by component.
2552 -- If the base type is an unchecked union, the discriminants are
2553 -- unknown to the back-end and absent from a value of the type, so
2554 -- assignments for them are not emitted.
2556 if Has_Discriminants (Typ)
2557 and then not Is_Unchecked_Union (Base_Type (Typ))
2559 Init_Hidden_Discriminants (Typ, L);
2561 -- Generate discriminant init values for the visible discriminants
2564 Discriminant : Entity_Id;
2565 Discriminant_Value : Node_Id;
2568 Discriminant := First_Stored_Discriminant (Typ);
2569 while Present (Discriminant) loop
2571 Make_Selected_Component (Loc,
2572 Prefix => New_Copy_Tree (Target),
2573 Selector_Name => New_Occurrence_Of (Discriminant, Loc));
2575 Discriminant_Value :=
2576 Get_Discriminant_Value (
2579 Discriminant_Constraint (N_Typ));
2582 Make_OK_Assignment_Statement (Loc,
2584 Expression => New_Copy_Tree (Discriminant_Value));
2586 Set_No_Ctrl_Actions (Instr);
2587 Append_To (L, Instr);
2589 Next_Stored_Discriminant (Discriminant);
2595 -- For CPP types we generate an implicit call to the C++ default
2596 -- constructor to ensure the proper initialization of the _Tag
2599 if Is_CPP_Class (Root_Type (Typ))
2600 and then CPP_Num_Prims (Typ) > 0
2602 Invoke_Constructor : declare
2603 CPP_Parent : constant Entity_Id := Enclosing_CPP_Parent (Typ);
2605 procedure Invoke_IC_Proc (T : Entity_Id);
2606 -- Recursive routine used to climb to parents. Required because
2607 -- parents must be initialized before descendants to ensure
2608 -- propagation of inherited C++ slots.
2610 --------------------
2611 -- Invoke_IC_Proc --
2612 --------------------
2614 procedure Invoke_IC_Proc (T : Entity_Id) is
2616 -- Avoid generating extra calls. Initialization required
2617 -- only for types defined from the level of derivation of
2618 -- type of the constructor and the type of the aggregate.
2620 if T = CPP_Parent then
2624 Invoke_IC_Proc (Etype (T));
2626 -- Generate call to the IC routine
2628 if Present (CPP_Init_Proc (T)) then
2630 Make_Procedure_Call_Statement (Loc,
2631 New_Occurrence_Of (CPP_Init_Proc (T), Loc)));
2635 -- Start of processing for Invoke_Constructor
2638 -- Implicit invocation of the C++ constructor
2640 if Nkind (N) = N_Aggregate then
2642 Make_Procedure_Call_Statement (Loc,
2645 (Base_Init_Proc (CPP_Parent), Loc),
2646 Parameter_Associations => New_List (
2647 Unchecked_Convert_To (CPP_Parent,
2648 New_Copy_Tree (Lhs)))));
2651 Invoke_IC_Proc (Typ);
2652 end Invoke_Constructor;
2655 -- Generate the assignments, component by component
2657 -- tmp.comp1 := Expr1_From_Aggr;
2658 -- tmp.comp2 := Expr2_From_Aggr;
2661 Comp := First (Component_Associations (N));
2662 while Present (Comp) loop
2663 Selector := Entity (First (Choices (Comp)));
2667 if Is_CPP_Constructor_Call (Expression (Comp)) then
2669 Build_Initialization_Call (Loc,
2670 Id_Ref => Make_Selected_Component (Loc,
2671 Prefix => New_Copy_Tree (Target),
2673 New_Occurrence_Of (Selector, Loc)),
2674 Typ => Etype (Selector),
2676 With_Default_Init => True,
2677 Constructor_Ref => Expression (Comp)));
2679 -- Ada 2005 (AI-287): For each default-initialized component generate
2680 -- a call to the corresponding IP subprogram if available.
2682 elsif Box_Present (Comp)
2683 and then Has_Non_Null_Base_Init_Proc (Etype (Selector))
2685 if Ekind (Selector) /= E_Discriminant then
2686 Generate_Finalization_Actions;
2689 -- Ada 2005 (AI-287): If the component type has tasks then
2690 -- generate the activation chain and master entities (except
2691 -- in case of an allocator because in that case these entities
2692 -- are generated by Build_Task_Allocate_Block_With_Init_Stmts).
2695 Ctype : constant Entity_Id := Etype (Selector);
2696 Inside_Allocator : Boolean := False;
2697 P : Node_Id := Parent (N);
2700 if Is_Task_Type (Ctype) or else Has_Task (Ctype) then
2701 while Present (P) loop
2702 if Nkind (P) = N_Allocator then
2703 Inside_Allocator := True;
2710 if not Inside_Init_Proc and not Inside_Allocator then
2711 Build_Activation_Chain_Entity (N);
2717 Build_Initialization_Call (Loc,
2718 Id_Ref => Make_Selected_Component (Loc,
2719 Prefix => New_Copy_Tree (Target),
2721 New_Occurrence_Of (Selector, Loc)),
2722 Typ => Etype (Selector),
2724 With_Default_Init => True));
2726 -- Prepare for component assignment
2728 elsif Ekind (Selector) /= E_Discriminant
2729 or else Nkind (N) = N_Extension_Aggregate
2731 -- All the discriminants have now been assigned
2733 -- This is now a good moment to initialize and attach all the
2734 -- controllers. Their position may depend on the discriminants.
2736 if Ekind (Selector) /= E_Discriminant then
2737 Generate_Finalization_Actions;
2740 Comp_Type := Underlying_Type (Etype (Selector));
2742 Make_Selected_Component (Loc,
2743 Prefix => New_Copy_Tree (Target),
2744 Selector_Name => New_Occurrence_Of (Selector, Loc));
2746 if Nkind (Expression (Comp)) = N_Qualified_Expression then
2747 Expr_Q := Expression (Expression (Comp));
2749 Expr_Q := Expression (Comp);
2752 -- Now either create the assignment or generate the code for the
2753 -- inner aggregate top-down.
2755 if Is_Delayed_Aggregate (Expr_Q) then
2757 -- We have the following case of aggregate nesting inside
2758 -- an object declaration:
2760 -- type Arr_Typ is array (Integer range <>) of ...;
2762 -- type Rec_Typ (...) is record
2763 -- Obj_Arr_Typ : Arr_Typ (A .. B);
2766 -- Obj_Rec_Typ : Rec_Typ := (...,
2767 -- Obj_Arr_Typ => (X => (...), Y => (...)));
2769 -- The length of the ranges of the aggregate and Obj_Add_Typ
2770 -- are equal (B - A = Y - X), but they do not coincide (X /=
2771 -- A and B /= Y). This case requires array sliding which is
2772 -- performed in the following manner:
2774 -- subtype Arr_Sub is Arr_Typ (X .. Y);
2776 -- Temp (X) := (...);
2778 -- Temp (Y) := (...);
2779 -- Obj_Rec_Typ.Obj_Arr_Typ := Temp;
2781 if Ekind (Comp_Type) = E_Array_Subtype
2782 and then Is_Int_Range_Bounds (Aggregate_Bounds (Expr_Q))
2783 and then Is_Int_Range_Bounds (First_Index (Comp_Type))
2785 Compatible_Int_Bounds
2786 (Agg_Bounds => Aggregate_Bounds (Expr_Q),
2787 Typ_Bounds => First_Index (Comp_Type))
2789 -- Create the array subtype with bounds equal to those of
2790 -- the corresponding aggregate.
2793 SubE : constant Entity_Id := Make_Temporary (Loc, 'T');
2795 SubD : constant Node_Id :=
2796 Make_Subtype_Declaration (Loc,
2797 Defining_Identifier => SubE,
2798 Subtype_Indication =>
2799 Make_Subtype_Indication (Loc,
2801 New_Occurrence_Of (Etype (Comp_Type), Loc),
2803 Make_Index_Or_Discriminant_Constraint
2805 Constraints => New_List (
2807 (Aggregate_Bounds (Expr_Q))))));
2809 -- Create a temporary array of the above subtype which
2810 -- will be used to capture the aggregate assignments.
2812 TmpE : constant Entity_Id := Make_Temporary (Loc, 'A', N);
2814 TmpD : constant Node_Id :=
2815 Make_Object_Declaration (Loc,
2816 Defining_Identifier => TmpE,
2817 Object_Definition => New_Occurrence_Of (SubE, Loc));
2820 Set_No_Initialization (TmpD);
2821 Append_To (L, SubD);
2822 Append_To (L, TmpD);
2824 -- Expand aggregate into assignments to the temp array
2827 Late_Expansion (Expr_Q, Comp_Type,
2828 New_Occurrence_Of (TmpE, Loc)));
2833 Make_Assignment_Statement (Loc,
2834 Name => New_Copy_Tree (Comp_Expr),
2835 Expression => New_Occurrence_Of (TmpE, Loc)));
2838 -- Normal case (sliding not required)
2842 Late_Expansion (Expr_Q, Comp_Type, Comp_Expr));
2845 -- Expr_Q is not delayed aggregate
2848 if Has_Discriminants (Typ) then
2849 Replace_Discriminants (Expr_Q);
2853 Make_OK_Assignment_Statement (Loc,
2855 Expression => Expr_Q);
2857 Set_No_Ctrl_Actions (Instr);
2858 Append_To (L, Instr);
2860 -- Adjust the tag if tagged (because of possible view
2861 -- conversions), unless compiling for a VM where tags are
2864 -- tmp.comp._tag := comp_typ'tag;
2866 if Is_Tagged_Type (Comp_Type)
2867 and then Tagged_Type_Expansion
2870 Make_OK_Assignment_Statement (Loc,
2872 Make_Selected_Component (Loc,
2873 Prefix => New_Copy_Tree (Comp_Expr),
2876 (First_Tag_Component (Comp_Type), Loc)),
2879 Unchecked_Convert_To (RTE (RE_Tag),
2881 (Node (First_Elmt (Access_Disp_Table (Comp_Type))),
2884 Append_To (L, Instr);
2888 -- Adjust (tmp.comp);
2890 if Needs_Finalization (Comp_Type)
2891 and then not Is_Limited_Type (Comp_Type)
2895 Obj_Ref => New_Copy_Tree (Comp_Expr),
2902 elsif Ekind (Selector) = E_Discriminant
2903 and then Nkind (N) /= N_Extension_Aggregate
2904 and then Nkind (Parent (N)) = N_Component_Association
2905 and then Is_Constrained (Typ)
2907 -- We must check that the discriminant value imposed by the
2908 -- context is the same as the value given in the subaggregate,
2909 -- because after the expansion into assignments there is no
2910 -- record on which to perform a regular discriminant check.
2917 D_Val := First_Elmt (Discriminant_Constraint (Typ));
2918 Disc := First_Discriminant (Typ);
2919 while Chars (Disc) /= Chars (Selector) loop
2920 Next_Discriminant (Disc);
2924 pragma Assert (Present (D_Val));
2926 -- This check cannot performed for components that are
2927 -- constrained by a current instance, because this is not a
2928 -- value that can be compared with the actual constraint.
2930 if Nkind (Node (D_Val)) /= N_Attribute_Reference
2931 or else not Is_Entity_Name (Prefix (Node (D_Val)))
2932 or else not Is_Type (Entity (Prefix (Node (D_Val))))
2935 Make_Raise_Constraint_Error (Loc,
2938 Left_Opnd => New_Copy_Tree (Node (D_Val)),
2939 Right_Opnd => Expression (Comp)),
2940 Reason => CE_Discriminant_Check_Failed));
2943 -- Find self-reference in previous discriminant assignment,
2944 -- and replace with proper expression.
2951 while Present (Ass) loop
2952 if Nkind (Ass) = N_Assignment_Statement
2953 and then Nkind (Name (Ass)) = N_Selected_Component
2954 and then Chars (Selector_Name (Name (Ass))) =
2958 (Ass, New_Copy_Tree (Expression (Comp)));
2971 -- If the type is tagged, the tag needs to be initialized (unless
2972 -- compiling for the Java VM where tags are implicit). It is done
2973 -- late in the initialization process because in some cases, we call
2974 -- the init proc of an ancestor which will not leave out the right tag
2976 if Ancestor_Is_Expression then
2979 -- For CPP types we generated a call to the C++ default constructor
2980 -- before the components have been initialized to ensure the proper
2981 -- initialization of the _Tag component (see above).
2983 elsif Is_CPP_Class (Typ) then
2986 elsif Is_Tagged_Type (Typ) and then Tagged_Type_Expansion then
2988 Make_OK_Assignment_Statement (Loc,
2990 Make_Selected_Component (Loc,
2991 Prefix => New_Copy_Tree (Target),
2994 (First_Tag_Component (Base_Type (Typ)), Loc)),
2997 Unchecked_Convert_To (RTE (RE_Tag),
2999 (Node (First_Elmt (Access_Disp_Table (Base_Type (Typ)))),
3002 Append_To (L, Instr);
3004 -- Ada 2005 (AI-251): If the tagged type has been derived from
3005 -- abstract interfaces we must also initialize the tags of the
3006 -- secondary dispatch tables.
3008 if Has_Interfaces (Base_Type (Typ)) then
3010 (Typ => Base_Type (Typ),
3016 -- If the controllers have not been initialized yet (by lack of non-
3017 -- discriminant components), let's do it now.
3019 Generate_Finalization_Actions;
3022 end Build_Record_Aggr_Code;
3024 ---------------------------------------
3025 -- Collect_Initialization_Statements --
3026 ---------------------------------------
3028 procedure Collect_Initialization_Statements
3031 Node_After : Node_Id)
3033 Loc : constant Source_Ptr := Sloc (N);
3034 Init_Actions : constant List_Id := New_List;
3035 Init_Node : Node_Id;
3036 Comp_Stmt : Node_Id;
3039 -- Nothing to do if Obj is already frozen, as in this case we known we
3040 -- won't need to move the initialization statements about later on.
3042 if Is_Frozen (Obj) then
3047 while Next (Init_Node) /= Node_After loop
3048 Append_To (Init_Actions, Remove_Next (Init_Node));
3051 if not Is_Empty_List (Init_Actions) then
3052 Comp_Stmt := Make_Compound_Statement (Loc, Actions => Init_Actions);
3053 Insert_Action_After (Init_Node, Comp_Stmt);
3054 Set_Initialization_Statements (Obj, Comp_Stmt);
3056 end Collect_Initialization_Statements;
3058 -------------------------------
3059 -- Convert_Aggr_In_Allocator --
3060 -------------------------------
3062 procedure Convert_Aggr_In_Allocator
3067 Loc : constant Source_Ptr := Sloc (Aggr);
3068 Typ : constant Entity_Id := Etype (Aggr);
3069 Temp : constant Entity_Id := Defining_Identifier (Decl);
3071 Occ : constant Node_Id :=
3072 Unchecked_Convert_To (Typ,
3073 Make_Explicit_Dereference (Loc, New_Occurrence_Of (Temp, Loc)));
3076 if Is_Array_Type (Typ) then
3077 Convert_Array_Aggr_In_Allocator (Decl, Aggr, Occ);
3079 elsif Has_Default_Init_Comps (Aggr) then
3081 L : constant List_Id := New_List;
3082 Init_Stmts : List_Id;
3085 Init_Stmts := Late_Expansion (Aggr, Typ, Occ);
3087 if Has_Task (Typ) then
3088 Build_Task_Allocate_Block_With_Init_Stmts (L, Aggr, Init_Stmts);
3089 Insert_Actions (Alloc, L);
3091 Insert_Actions (Alloc, Init_Stmts);
3096 Insert_Actions (Alloc, Late_Expansion (Aggr, Typ, Occ));
3098 end Convert_Aggr_In_Allocator;
3100 --------------------------------
3101 -- Convert_Aggr_In_Assignment --
3102 --------------------------------
3104 procedure Convert_Aggr_In_Assignment (N : Node_Id) is
3105 Aggr : Node_Id := Expression (N);
3106 Typ : constant Entity_Id := Etype (Aggr);
3107 Occ : constant Node_Id := New_Copy_Tree (Name (N));
3110 if Nkind (Aggr) = N_Qualified_Expression then
3111 Aggr := Expression (Aggr);
3114 Insert_Actions_After (N, Late_Expansion (Aggr, Typ, Occ));
3115 end Convert_Aggr_In_Assignment;
3117 ---------------------------------
3118 -- Convert_Aggr_In_Object_Decl --
3119 ---------------------------------
3121 procedure Convert_Aggr_In_Object_Decl (N : Node_Id) is
3122 Obj : constant Entity_Id := Defining_Identifier (N);
3123 Aggr : Node_Id := Expression (N);
3124 Loc : constant Source_Ptr := Sloc (Aggr);
3125 Typ : constant Entity_Id := Etype (Aggr);
3126 Occ : constant Node_Id := New_Occurrence_Of (Obj, Loc);
3128 function Discriminants_Ok return Boolean;
3129 -- If the object type is constrained, the discriminants in the
3130 -- aggregate must be checked against the discriminants of the subtype.
3131 -- This cannot be done using Apply_Discriminant_Checks because after
3132 -- expansion there is no aggregate left to check.
3134 ----------------------
3135 -- Discriminants_Ok --
3136 ----------------------
3138 function Discriminants_Ok return Boolean is
3139 Cond : Node_Id := Empty;
3148 D := First_Discriminant (Typ);
3149 Disc1 := First_Elmt (Discriminant_Constraint (Typ));
3150 Disc2 := First_Elmt (Discriminant_Constraint (Etype (Obj)));
3151 while Present (Disc1) and then Present (Disc2) loop
3152 Val1 := Node (Disc1);
3153 Val2 := Node (Disc2);
3155 if not Is_OK_Static_Expression (Val1)
3156 or else not Is_OK_Static_Expression (Val2)
3158 Check := Make_Op_Ne (Loc,
3159 Left_Opnd => Duplicate_Subexpr (Val1),
3160 Right_Opnd => Duplicate_Subexpr (Val2));
3166 Cond := Make_Or_Else (Loc,
3168 Right_Opnd => Check);
3171 elsif Expr_Value (Val1) /= Expr_Value (Val2) then
3172 Apply_Compile_Time_Constraint_Error (Aggr,
3173 Msg => "incorrect value for discriminant&??",
3174 Reason => CE_Discriminant_Check_Failed,
3179 Next_Discriminant (D);
3184 -- If any discriminant constraint is non-static, emit a check
3186 if Present (Cond) then
3188 Make_Raise_Constraint_Error (Loc,
3190 Reason => CE_Discriminant_Check_Failed));
3194 end Discriminants_Ok;
3196 -- Start of processing for Convert_Aggr_In_Object_Decl
3199 Set_Assignment_OK (Occ);
3201 if Nkind (Aggr) = N_Qualified_Expression then
3202 Aggr := Expression (Aggr);
3205 if Has_Discriminants (Typ)
3206 and then Typ /= Etype (Obj)
3207 and then Is_Constrained (Etype (Obj))
3208 and then not Discriminants_Ok
3213 -- If the context is an extended return statement, it has its own
3214 -- finalization machinery (i.e. works like a transient scope) and
3215 -- we do not want to create an additional one, because objects on
3216 -- the finalization list of the return must be moved to the caller's
3217 -- finalization list to complete the return.
3219 -- However, if the aggregate is limited, it is built in place, and the
3220 -- controlled components are not assigned to intermediate temporaries
3221 -- so there is no need for a transient scope in this case either.
3223 if Requires_Transient_Scope (Typ)
3224 and then Ekind (Current_Scope) /= E_Return_Statement
3225 and then not Is_Limited_Type (Typ)
3227 Establish_Transient_Scope
3230 Is_Controlled (Typ) or else Has_Controlled_Component (Typ));
3234 Node_After : constant Node_Id := Next (N);
3236 Insert_Actions_After (N, Late_Expansion (Aggr, Typ, Occ));
3237 Collect_Initialization_Statements (Obj, N, Node_After);
3239 Set_No_Initialization (N);
3240 Initialize_Discriminants (N, Typ);
3241 end Convert_Aggr_In_Object_Decl;
3243 -------------------------------------
3244 -- Convert_Array_Aggr_In_Allocator --
3245 -------------------------------------
3247 procedure Convert_Array_Aggr_In_Allocator
3252 Aggr_Code : List_Id;
3253 Typ : constant Entity_Id := Etype (Aggr);
3254 Ctyp : constant Entity_Id := Component_Type (Typ);
3257 -- The target is an explicit dereference of the allocated object.
3258 -- Generate component assignments to it, as for an aggregate that
3259 -- appears on the right-hand side of an assignment statement.
3262 Build_Array_Aggr_Code (Aggr,
3264 Index => First_Index (Typ),
3266 Scalar_Comp => Is_Scalar_Type (Ctyp));
3268 Insert_Actions_After (Decl, Aggr_Code);
3269 end Convert_Array_Aggr_In_Allocator;
3271 ----------------------------
3272 -- Convert_To_Assignments --
3273 ----------------------------
3275 procedure Convert_To_Assignments (N : Node_Id; Typ : Entity_Id) is
3276 Loc : constant Source_Ptr := Sloc (N);
3280 Aggr_Code : List_Id;
3282 Target_Expr : Node_Id;
3283 Parent_Kind : Node_Kind;
3284 Unc_Decl : Boolean := False;
3285 Parent_Node : Node_Id;
3288 pragma Assert (not Is_Static_Dispatch_Table_Aggregate (N));
3289 pragma Assert (Is_Record_Type (Typ));
3291 Parent_Node := Parent (N);
3292 Parent_Kind := Nkind (Parent_Node);
3294 if Parent_Kind = N_Qualified_Expression then
3296 -- Check if we are in a unconstrained declaration because in this
3297 -- case the current delayed expansion mechanism doesn't work when
3298 -- the declared object size depend on the initializing expr.
3301 Parent_Node := Parent (Parent_Node);
3302 Parent_Kind := Nkind (Parent_Node);
3304 if Parent_Kind = N_Object_Declaration then
3306 not Is_Entity_Name (Object_Definition (Parent_Node))
3307 or else Has_Discriminants
3308 (Entity (Object_Definition (Parent_Node)))
3309 or else Is_Class_Wide_Type
3310 (Entity (Object_Definition (Parent_Node)));
3315 -- Just set the Delay flag in the cases where the transformation will be
3316 -- done top down from above.
3320 -- Internal aggregate (transformed when expanding the parent)
3322 or else Parent_Kind = N_Aggregate
3323 or else Parent_Kind = N_Extension_Aggregate
3324 or else Parent_Kind = N_Component_Association
3326 -- Allocator (see Convert_Aggr_In_Allocator)
3328 or else Parent_Kind = N_Allocator
3330 -- Object declaration (see Convert_Aggr_In_Object_Decl)
3332 or else (Parent_Kind = N_Object_Declaration and then not Unc_Decl)
3334 -- Safe assignment (see Convert_Aggr_Assignments). So far only the
3335 -- assignments in init procs are taken into account.
3337 or else (Parent_Kind = N_Assignment_Statement
3338 and then Inside_Init_Proc)
3340 -- (Ada 2005) An inherently limited type in a return statement,
3341 -- which will be handled in a build-in-place fashion, and may be
3342 -- rewritten as an extended return and have its own finalization
3343 -- machinery. In the case of a simple return, the aggregate needs
3344 -- to be delayed until the scope for the return statement has been
3345 -- created, so that any finalization chain will be associated with
3346 -- that scope. For extended returns, we delay expansion to avoid the
3347 -- creation of an unwanted transient scope that could result in
3348 -- premature finalization of the return object (which is built in
3349 -- in place within the caller's scope).
3352 (Is_Limited_View (Typ)
3354 (Nkind (Parent (Parent_Node)) = N_Extended_Return_Statement
3355 or else Nkind (Parent_Node) = N_Simple_Return_Statement))
3357 Set_Expansion_Delayed (N);
3361 if Requires_Transient_Scope (Typ) then
3362 Establish_Transient_Scope (N, Sec_Stack => Needs_Finalization (Typ));
3365 -- If the aggregate is non-limited, create a temporary. If it is limited
3366 -- and the context is an assignment, this is a subaggregate for an
3367 -- enclosing aggregate being expanded. It must be built in place, so use
3368 -- the target of the current assignment.
3370 if Is_Limited_Type (Typ)
3371 and then Nkind (Parent (N)) = N_Assignment_Statement
3373 Target_Expr := New_Copy_Tree (Name (Parent (N)));
3374 Insert_Actions (Parent (N),
3375 Build_Record_Aggr_Code (N, Typ, Target_Expr));
3376 Rewrite (Parent (N), Make_Null_Statement (Loc));
3379 Temp := Make_Temporary (Loc, 'A', N);
3381 -- If the type inherits unknown discriminants, use the view with
3382 -- known discriminants if available.
3384 if Has_Unknown_Discriminants (Typ)
3385 and then Present (Underlying_Record_View (Typ))
3387 T := Underlying_Record_View (Typ);
3393 Make_Object_Declaration (Loc,
3394 Defining_Identifier => Temp,
3395 Object_Definition => New_Occurrence_Of (T, Loc));
3397 Set_No_Initialization (Instr);
3398 Insert_Action (N, Instr);
3399 Initialize_Discriminants (Instr, T);
3401 Target_Expr := New_Occurrence_Of (Temp, Loc);
3402 Aggr_Code := Build_Record_Aggr_Code (N, T, Target_Expr);
3404 -- Save the last assignment statement associated with the aggregate
3405 -- when building a controlled object. This reference is utilized by
3406 -- the finalization machinery when marking an object as successfully
3409 if Needs_Finalization (T) then
3410 Set_Last_Aggregate_Assignment (Temp, Last (Aggr_Code));
3413 Insert_Actions (N, Aggr_Code);
3414 Rewrite (N, New_Occurrence_Of (Temp, Loc));
3415 Analyze_And_Resolve (N, T);
3417 end Convert_To_Assignments;
3419 ---------------------------
3420 -- Convert_To_Positional --
3421 ---------------------------
3423 procedure Convert_To_Positional
3425 Max_Others_Replicate : Nat := 5;
3426 Handle_Bit_Packed : Boolean := False)
3428 Typ : constant Entity_Id := Etype (N);
3430 Static_Components : Boolean := True;
3432 procedure Check_Static_Components;
3433 -- Check whether all components of the aggregate are compile-time known
3434 -- values, and can be passed as is to the back-end without further
3440 Ixb : Node_Id) return Boolean;
3441 -- Convert the aggregate into a purely positional form if possible. On
3442 -- entry the bounds of all dimensions are known to be static, and the
3443 -- total number of components is safe enough to expand.
3445 function Is_Flat (N : Node_Id; Dims : Int) return Boolean;
3446 -- Return True iff the array N is flat (which is not trivial in the case
3447 -- of multidimensional aggregates).
3449 -----------------------------
3450 -- Check_Static_Components --
3451 -----------------------------
3453 procedure Check_Static_Components is
3457 Static_Components := True;
3459 if Nkind (N) = N_String_Literal then
3462 elsif Present (Expressions (N)) then
3463 Expr := First (Expressions (N));
3464 while Present (Expr) loop
3465 if Nkind (Expr) /= N_Aggregate
3466 or else not Compile_Time_Known_Aggregate (Expr)
3467 or else Expansion_Delayed (Expr)
3469 Static_Components := False;
3477 if Nkind (N) = N_Aggregate
3478 and then Present (Component_Associations (N))
3480 Expr := First (Component_Associations (N));
3481 while Present (Expr) loop
3482 if Nkind_In (Expression (Expr), N_Integer_Literal,
3487 elsif Is_Entity_Name (Expression (Expr))
3488 and then Present (Entity (Expression (Expr)))
3489 and then Ekind (Entity (Expression (Expr))) =
3490 E_Enumeration_Literal
3494 elsif Nkind (Expression (Expr)) /= N_Aggregate
3495 or else not Compile_Time_Known_Aggregate (Expression (Expr))
3496 or else Expansion_Delayed (Expression (Expr))
3498 Static_Components := False;
3505 end Check_Static_Components;
3514 Ixb : Node_Id) return Boolean
3516 Loc : constant Source_Ptr := Sloc (N);
3517 Blo : constant Node_Id := Type_Low_Bound (Etype (Ixb));
3518 Lo : constant Node_Id := Type_Low_Bound (Etype (Ix));
3519 Hi : constant Node_Id := Type_High_Bound (Etype (Ix));
3523 Others_Present : Boolean := False;
3526 if Nkind (Original_Node (N)) = N_String_Literal then
3530 if not Compile_Time_Known_Value (Lo)
3531 or else not Compile_Time_Known_Value (Hi)
3536 Lov := Expr_Value (Lo);
3537 Hiv := Expr_Value (Hi);
3539 -- Check if there is an others choice
3541 if Present (Component_Associations (N)) then
3547 Assoc := First (Component_Associations (N));
3548 while Present (Assoc) loop
3550 -- If this is a box association, flattening is in general
3551 -- not possible because at this point we cannot tell if the
3552 -- default is static or even exists.
3554 if Box_Present (Assoc) then
3558 Choice := First (Choices (Assoc));
3560 while Present (Choice) loop
3561 if Nkind (Choice) = N_Others_Choice then
3562 Others_Present := True;
3573 -- If the low bound is not known at compile time and others is not
3574 -- present we can proceed since the bounds can be obtained from the
3577 -- Note: This case is required in VM platforms since their backends
3578 -- normalize array indexes in the range 0 .. N-1. Hence, if we do
3579 -- not flat an array whose bounds cannot be obtained from the type
3580 -- of the index the backend has no way to properly generate the code.
3581 -- See ACATS c460010 for an example.
3584 or else (not Compile_Time_Known_Value (Blo)
3585 and then Others_Present)
3590 -- Determine if set of alternatives is suitable for conversion and
3591 -- build an array containing the values in sequence.
3594 Vals : array (UI_To_Int (Lov) .. UI_To_Int (Hiv))
3595 of Node_Id := (others => Empty);
3596 -- The values in the aggregate sorted appropriately
3599 -- Same data as Vals in list form
3602 -- Used to validate Max_Others_Replicate limit
3605 Num : Int := UI_To_Int (Lov);
3611 if Present (Expressions (N)) then
3612 Elmt := First (Expressions (N));
3613 while Present (Elmt) loop
3614 if Nkind (Elmt) = N_Aggregate
3615 and then Present (Next_Index (Ix))
3617 not Flatten (Elmt, Next_Index (Ix), Next_Index (Ixb))
3622 Vals (Num) := Relocate_Node (Elmt);
3629 if No (Component_Associations (N)) then
3633 Elmt := First (Component_Associations (N));
3635 if Nkind (Expression (Elmt)) = N_Aggregate then
3636 if Present (Next_Index (Ix))
3639 (Expression (Elmt), Next_Index (Ix), Next_Index (Ixb))
3645 Component_Loop : while Present (Elmt) loop
3646 Choice := First (Choices (Elmt));
3647 Choice_Loop : while Present (Choice) loop
3649 -- If we have an others choice, fill in the missing elements
3650 -- subject to the limit established by Max_Others_Replicate.
3652 if Nkind (Choice) = N_Others_Choice then
3655 for J in Vals'Range loop
3656 if No (Vals (J)) then
3657 Vals (J) := New_Copy_Tree (Expression (Elmt));
3658 Rep_Count := Rep_Count + 1;
3660 -- Check for maximum others replication. Note that
3661 -- we skip this test if either of the restrictions
3662 -- No_Elaboration_Code or No_Implicit_Loops is
3663 -- active, if this is a preelaborable unit or
3664 -- a predefined unit, or if the unit must be
3665 -- placed in data memory. This also ensures that
3666 -- predefined units get the same level of constant
3667 -- folding in Ada 95 and Ada 2005, where their
3668 -- categorization has changed.
3671 P : constant Entity_Id :=
3672 Cunit_Entity (Current_Sem_Unit);
3675 -- Check if duplication OK and if so continue
3678 if Restriction_Active (No_Elaboration_Code)
3679 or else Restriction_Active (No_Implicit_Loops)
3681 (Ekind (Current_Scope) = E_Package
3683 Static_Elaboration_Desired
3685 or else Is_Preelaborated (P)
3686 or else (Ekind (P) = E_Package_Body
3688 Is_Preelaborated (Spec_Entity (P)))
3690 Is_Predefined_File_Name
3691 (Unit_File_Name (Get_Source_Unit (P)))
3695 -- If duplication not OK, then we return False
3696 -- if the replication count is too high
3698 elsif Rep_Count > Max_Others_Replicate then
3701 -- Continue on if duplication not OK, but the
3702 -- replication count is not excessive.
3711 exit Component_Loop;
3713 -- Case of a subtype mark, identifier or expanded name
3715 elsif Is_Entity_Name (Choice)
3716 and then Is_Type (Entity (Choice))
3718 Lo := Type_Low_Bound (Etype (Choice));
3719 Hi := Type_High_Bound (Etype (Choice));
3721 -- Case of subtype indication
3723 elsif Nkind (Choice) = N_Subtype_Indication then
3724 Lo := Low_Bound (Range_Expression (Constraint (Choice)));
3725 Hi := High_Bound (Range_Expression (Constraint (Choice)));
3729 elsif Nkind (Choice) = N_Range then
3730 Lo := Low_Bound (Choice);
3731 Hi := High_Bound (Choice);
3733 -- Normal subexpression case
3735 else pragma Assert (Nkind (Choice) in N_Subexpr);
3736 if not Compile_Time_Known_Value (Choice) then
3740 Choice_Index := UI_To_Int (Expr_Value (Choice));
3741 if Choice_Index in Vals'Range then
3742 Vals (Choice_Index) :=
3743 New_Copy_Tree (Expression (Elmt));
3747 -- Choice is statically out-of-range, will be
3748 -- rewritten to raise Constraint_Error.
3755 -- Range cases merge with Lo,Hi set
3757 if not Compile_Time_Known_Value (Lo)
3759 not Compile_Time_Known_Value (Hi)
3763 for J in UI_To_Int (Expr_Value (Lo)) ..
3764 UI_To_Int (Expr_Value (Hi))
3766 Vals (J) := New_Copy_Tree (Expression (Elmt));
3772 end loop Choice_Loop;
3775 end loop Component_Loop;
3777 -- If we get here the conversion is possible
3780 for J in Vals'Range loop
3781 Append (Vals (J), Vlist);
3784 Rewrite (N, Make_Aggregate (Loc, Expressions => Vlist));
3785 Set_Aggregate_Bounds (N, Aggregate_Bounds (Original_Node (N)));
3794 function Is_Flat (N : Node_Id; Dims : Int) return Boolean is
3801 elsif Nkind (N) = N_Aggregate then
3802 if Present (Component_Associations (N)) then
3806 Elmt := First (Expressions (N));
3807 while Present (Elmt) loop
3808 if not Is_Flat (Elmt, Dims - 1) then
3822 -- Start of processing for Convert_To_Positional
3825 -- Ada 2005 (AI-287): Do not convert in case of default initialized
3826 -- components because in this case will need to call the corresponding
3829 if Has_Default_Init_Comps (N) then
3833 if Is_Flat (N, Number_Dimensions (Typ)) then
3837 if Is_Bit_Packed_Array (Typ)
3838 and then not Handle_Bit_Packed
3843 -- Do not convert to positional if controlled components are involved
3844 -- since these require special processing
3846 if Has_Controlled_Component (Typ) then
3850 Check_Static_Components;
3852 -- If the size is known, or all the components are static, try to
3853 -- build a fully positional aggregate.
3855 -- The size of the type may not be known for an aggregate with
3856 -- discriminated array components, but if the components are static
3857 -- it is still possible to verify statically that the length is
3858 -- compatible with the upper bound of the type, and therefore it is
3859 -- worth flattening such aggregates as well.
3861 -- For now the back-end expands these aggregates into individual
3862 -- assignments to the target anyway, but it is conceivable that
3863 -- it will eventually be able to treat such aggregates statically???
3865 if Aggr_Size_OK (N, Typ)
3866 and then Flatten (N, First_Index (Typ), First_Index (Base_Type (Typ)))
3868 if Static_Components then
3869 Set_Compile_Time_Known_Aggregate (N);
3870 Set_Expansion_Delayed (N, False);
3873 Analyze_And_Resolve (N, Typ);
3876 -- Is Static_Eaboration_Desired has been specified, diagnose aggregates
3877 -- that will still require initialization code.
3879 if (Ekind (Current_Scope) = E_Package
3880 and then Static_Elaboration_Desired (Current_Scope))
3881 and then Nkind (Parent (N)) = N_Object_Declaration
3887 if Nkind (N) = N_Aggregate and then Present (Expressions (N)) then
3888 Expr := First (Expressions (N));
3889 while Present (Expr) loop
3890 if Nkind_In (Expr, N_Integer_Literal, N_Real_Literal)
3892 (Is_Entity_Name (Expr)
3893 and then Ekind (Entity (Expr)) = E_Enumeration_Literal)
3899 ("non-static object requires elaboration code??", N);
3906 if Present (Component_Associations (N)) then
3907 Error_Msg_N ("object requires elaboration code??", N);
3912 end Convert_To_Positional;
3914 ----------------------------
3915 -- Expand_Array_Aggregate --
3916 ----------------------------
3918 -- Array aggregate expansion proceeds as follows:
3920 -- 1. If requested we generate code to perform all the array aggregate
3921 -- bound checks, specifically
3923 -- (a) Check that the index range defined by aggregate bounds is
3924 -- compatible with corresponding index subtype.
3926 -- (b) If an others choice is present check that no aggregate
3927 -- index is outside the bounds of the index constraint.
3929 -- (c) For multidimensional arrays make sure that all subaggregates
3930 -- corresponding to the same dimension have the same bounds.
3932 -- 2. Check for packed array aggregate which can be converted to a
3933 -- constant so that the aggregate disappears completely.
3935 -- 3. Check case of nested aggregate. Generally nested aggregates are
3936 -- handled during the processing of the parent aggregate.
3938 -- 4. Check if the aggregate can be statically processed. If this is the
3939 -- case pass it as is to Gigi. Note that a necessary condition for
3940 -- static processing is that the aggregate be fully positional.
3942 -- 5. If in place aggregate expansion is possible (i.e. no need to create
3943 -- a temporary) then mark the aggregate as such and return. Otherwise
3944 -- create a new temporary and generate the appropriate initialization
3947 procedure Expand_Array_Aggregate (N : Node_Id) is
3948 Loc : constant Source_Ptr := Sloc (N);
3950 Typ : constant Entity_Id := Etype (N);
3951 Ctyp : constant Entity_Id := Component_Type (Typ);
3952 -- Typ is the correct constrained array subtype of the aggregate
3953 -- Ctyp is the corresponding component type.
3955 Aggr_Dimension : constant Pos := Number_Dimensions (Typ);
3956 -- Number of aggregate index dimensions
3958 Aggr_Low : array (1 .. Aggr_Dimension) of Node_Id;
3959 Aggr_High : array (1 .. Aggr_Dimension) of Node_Id;
3960 -- Low and High bounds of the constraint for each aggregate index
3962 Aggr_Index_Typ : array (1 .. Aggr_Dimension) of Entity_Id;
3963 -- The type of each index
3965 Maybe_In_Place_OK : Boolean;
3966 -- If the type is neither controlled nor packed and the aggregate
3967 -- is the expression in an assignment, assignment in place may be
3968 -- possible, provided other conditions are met on the LHS.
3970 Others_Present : array (1 .. Aggr_Dimension) of Boolean :=
3972 -- If Others_Present (J) is True, then there is an others choice
3973 -- in one of the sub-aggregates of N at dimension J.
3975 procedure Build_Constrained_Type (Positional : Boolean);
3976 -- If the subtype is not static or unconstrained, build a constrained
3977 -- type using the computable sizes of the aggregate and its sub-
3980 procedure Check_Bounds (Aggr_Bounds : Node_Id; Index_Bounds : Node_Id);
3981 -- Checks that the bounds of Aggr_Bounds are within the bounds defined
3984 procedure Check_Same_Aggr_Bounds (Sub_Aggr : Node_Id; Dim : Pos);
3985 -- Checks that in a multi-dimensional array aggregate all subaggregates
3986 -- corresponding to the same dimension have the same bounds.
3987 -- Sub_Aggr is an array sub-aggregate. Dim is the dimension
3988 -- corresponding to the sub-aggregate.
3990 procedure Compute_Others_Present (Sub_Aggr : Node_Id; Dim : Pos);
3991 -- Computes the values of array Others_Present. Sub_Aggr is the
3992 -- array sub-aggregate we start the computation from. Dim is the
3993 -- dimension corresponding to the sub-aggregate.
3995 function In_Place_Assign_OK return Boolean;
3996 -- Simple predicate to determine whether an aggregate assignment can
3997 -- be done in place, because none of the new values can depend on the
3998 -- components of the target of the assignment.
4000 procedure Others_Check (Sub_Aggr : Node_Id; Dim : Pos);
4001 -- Checks that if an others choice is present in any sub-aggregate no
4002 -- aggregate index is outside the bounds of the index constraint.
4003 -- Sub_Aggr is an array sub-aggregate. Dim is the dimension
4004 -- corresponding to the sub-aggregate.
4006 function Safe_Left_Hand_Side (N : Node_Id) return Boolean;
4007 -- In addition to Maybe_In_Place_OK, in order for an aggregate to be
4008 -- built directly into the target of the assignment it must be free
4011 ----------------------------
4012 -- Build_Constrained_Type --
4013 ----------------------------
4015 procedure Build_Constrained_Type (Positional : Boolean) is
4016 Loc : constant Source_Ptr := Sloc (N);
4017 Agg_Type : constant Entity_Id := Make_Temporary (Loc, 'A');
4020 Typ : constant Entity_Id := Etype (N);
4021 Indexes : constant List_Id := New_List;
4026 -- If the aggregate is purely positional, all its subaggregates
4027 -- have the same size. We collect the dimensions from the first
4028 -- subaggregate at each level.
4033 for D in 1 .. Number_Dimensions (Typ) loop
4034 Sub_Agg := First (Expressions (Sub_Agg));
4038 while Present (Comp) loop
4045 Low_Bound => Make_Integer_Literal (Loc, 1),
4046 High_Bound => Make_Integer_Literal (Loc, Num)));
4050 -- We know the aggregate type is unconstrained and the aggregate
4051 -- is not processable by the back end, therefore not necessarily
4052 -- positional. Retrieve each dimension bounds (computed earlier).
4054 for D in 1 .. Number_Dimensions (Typ) loop
4057 Low_Bound => Aggr_Low (D),
4058 High_Bound => Aggr_High (D)),
4064 Make_Full_Type_Declaration (Loc,
4065 Defining_Identifier => Agg_Type,
4067 Make_Constrained_Array_Definition (Loc,
4068 Discrete_Subtype_Definitions => Indexes,
4069 Component_Definition =>
4070 Make_Component_Definition (Loc,
4071 Aliased_Present => False,
4072 Subtype_Indication =>
4073 New_Occurrence_Of (Component_Type (Typ), Loc))));
4075 Insert_Action (N, Decl);
4077 Set_Etype (N, Agg_Type);
4078 Set_Is_Itype (Agg_Type);
4079 Freeze_Itype (Agg_Type, N);
4080 end Build_Constrained_Type;
4086 procedure Check_Bounds (Aggr_Bounds : Node_Id; Index_Bounds : Node_Id) is
4093 Cond : Node_Id := Empty;
4096 Get_Index_Bounds (Aggr_Bounds, Aggr_Lo, Aggr_Hi);
4097 Get_Index_Bounds (Index_Bounds, Ind_Lo, Ind_Hi);
4099 -- Generate the following test:
4101 -- [constraint_error when
4102 -- Aggr_Lo <= Aggr_Hi and then
4103 -- (Aggr_Lo < Ind_Lo or else Aggr_Hi > Ind_Hi)]
4105 -- As an optimization try to see if some tests are trivially vacuous
4106 -- because we are comparing an expression against itself.
4108 if Aggr_Lo = Ind_Lo and then Aggr_Hi = Ind_Hi then
4111 elsif Aggr_Hi = Ind_Hi then
4114 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
4115 Right_Opnd => Duplicate_Subexpr_Move_Checks (Ind_Lo));
4117 elsif Aggr_Lo = Ind_Lo then
4120 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi),
4121 Right_Opnd => Duplicate_Subexpr_Move_Checks (Ind_Hi));
4128 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
4129 Right_Opnd => Duplicate_Subexpr_Move_Checks (Ind_Lo)),
4133 Left_Opnd => Duplicate_Subexpr (Aggr_Hi),
4134 Right_Opnd => Duplicate_Subexpr (Ind_Hi)));
4137 if Present (Cond) then
4142 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
4143 Right_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi)),
4145 Right_Opnd => Cond);
4147 Set_Analyzed (Left_Opnd (Left_Opnd (Cond)), False);
4148 Set_Analyzed (Right_Opnd (Left_Opnd (Cond)), False);
4150 Make_Raise_Constraint_Error (Loc,
4152 Reason => CE_Range_Check_Failed));
4156 ----------------------------
4157 -- Check_Same_Aggr_Bounds --
4158 ----------------------------
4160 procedure Check_Same_Aggr_Bounds (Sub_Aggr : Node_Id; Dim : Pos) is
4161 Sub_Lo : constant Node_Id := Low_Bound (Aggregate_Bounds (Sub_Aggr));
4162 Sub_Hi : constant Node_Id := High_Bound (Aggregate_Bounds (Sub_Aggr));
4163 -- The bounds of this specific sub-aggregate
4165 Aggr_Lo : constant Node_Id := Aggr_Low (Dim);
4166 Aggr_Hi : constant Node_Id := Aggr_High (Dim);
4167 -- The bounds of the aggregate for this dimension
4169 Ind_Typ : constant Entity_Id := Aggr_Index_Typ (Dim);
4170 -- The index type for this dimension.xxx
4172 Cond : Node_Id := Empty;
4177 -- If index checks are on generate the test
4179 -- [constraint_error when
4180 -- Aggr_Lo /= Sub_Lo or else Aggr_Hi /= Sub_Hi]
4182 -- As an optimization try to see if some tests are trivially vacuos
4183 -- because we are comparing an expression against itself. Also for
4184 -- the first dimension the test is trivially vacuous because there
4185 -- is just one aggregate for dimension 1.
4187 if Index_Checks_Suppressed (Ind_Typ) then
4191 or else (Aggr_Lo = Sub_Lo and then Aggr_Hi = Sub_Hi)
4195 elsif Aggr_Hi = Sub_Hi then
4198 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
4199 Right_Opnd => Duplicate_Subexpr_Move_Checks (Sub_Lo));
4201 elsif Aggr_Lo = Sub_Lo then
4204 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi),
4205 Right_Opnd => Duplicate_Subexpr_Move_Checks (Sub_Hi));
4212 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
4213 Right_Opnd => Duplicate_Subexpr_Move_Checks (Sub_Lo)),
4217 Left_Opnd => Duplicate_Subexpr (Aggr_Hi),
4218 Right_Opnd => Duplicate_Subexpr (Sub_Hi)));
4221 if Present (Cond) then
4223 Make_Raise_Constraint_Error (Loc,
4225 Reason => CE_Length_Check_Failed));
4228 -- Now look inside the sub-aggregate to see if there is more work
4230 if Dim < Aggr_Dimension then
4232 -- Process positional components
4234 if Present (Expressions (Sub_Aggr)) then
4235 Expr := First (Expressions (Sub_Aggr));
4236 while Present (Expr) loop
4237 Check_Same_Aggr_Bounds (Expr, Dim + 1);
4242 -- Process component associations
4244 if Present (Component_Associations (Sub_Aggr)) then
4245 Assoc := First (Component_Associations (Sub_Aggr));
4246 while Present (Assoc) loop
4247 Expr := Expression (Assoc);
4248 Check_Same_Aggr_Bounds (Expr, Dim + 1);
4253 end Check_Same_Aggr_Bounds;
4255 ----------------------------
4256 -- Compute_Others_Present --
4257 ----------------------------
4259 procedure Compute_Others_Present (Sub_Aggr : Node_Id; Dim : Pos) is
4264 if Present (Component_Associations (Sub_Aggr)) then
4265 Assoc := Last (Component_Associations (Sub_Aggr));
4267 if Nkind (First (Choices (Assoc))) = N_Others_Choice then
4268 Others_Present (Dim) := True;
4272 -- Now look inside the sub-aggregate to see if there is more work
4274 if Dim < Aggr_Dimension then
4276 -- Process positional components
4278 if Present (Expressions (Sub_Aggr)) then
4279 Expr := First (Expressions (Sub_Aggr));
4280 while Present (Expr) loop
4281 Compute_Others_Present (Expr, Dim + 1);
4286 -- Process component associations
4288 if Present (Component_Associations (Sub_Aggr)) then
4289 Assoc := First (Component_Associations (Sub_Aggr));
4290 while Present (Assoc) loop
4291 Expr := Expression (Assoc);
4292 Compute_Others_Present (Expr, Dim + 1);
4297 end Compute_Others_Present;
4299 ------------------------
4300 -- In_Place_Assign_OK --
4301 ------------------------
4303 function In_Place_Assign_OK return Boolean is
4311 function Safe_Aggregate (Aggr : Node_Id) return Boolean;
4312 -- Check recursively that each component of a (sub)aggregate does
4313 -- not depend on the variable being assigned to.
4315 function Safe_Component (Expr : Node_Id) return Boolean;
4316 -- Verify that an expression cannot depend on the variable being
4317 -- assigned to. Room for improvement here (but less than before).
4319 --------------------
4320 -- Safe_Aggregate --
4321 --------------------
4323 function Safe_Aggregate (Aggr : Node_Id) return Boolean is
4327 if Present (Expressions (Aggr)) then
4328 Expr := First (Expressions (Aggr));
4329 while Present (Expr) loop
4330 if Nkind (Expr) = N_Aggregate then
4331 if not Safe_Aggregate (Expr) then
4335 elsif not Safe_Component (Expr) then
4343 if Present (Component_Associations (Aggr)) then
4344 Expr := First (Component_Associations (Aggr));
4345 while Present (Expr) loop
4346 if Nkind (Expression (Expr)) = N_Aggregate then
4347 if not Safe_Aggregate (Expression (Expr)) then
4351 -- If association has a box, no way to determine yet
4352 -- whether default can be assigned in place.
4354 elsif Box_Present (Expr) then
4357 elsif not Safe_Component (Expression (Expr)) then
4368 --------------------
4369 -- Safe_Component --
4370 --------------------
4372 function Safe_Component (Expr : Node_Id) return Boolean is
4373 Comp : Node_Id := Expr;
4375 function Check_Component (Comp : Node_Id) return Boolean;
4376 -- Do the recursive traversal, after copy
4378 ---------------------
4379 -- Check_Component --
4380 ---------------------
4382 function Check_Component (Comp : Node_Id) return Boolean is
4384 if Is_Overloaded (Comp) then
4388 return Compile_Time_Known_Value (Comp)
4390 or else (Is_Entity_Name (Comp)
4391 and then Present (Entity (Comp))
4392 and then No (Renamed_Object (Entity (Comp))))
4394 or else (Nkind (Comp) = N_Attribute_Reference
4395 and then Check_Component (Prefix (Comp)))
4397 or else (Nkind (Comp) in N_Binary_Op
4398 and then Check_Component (Left_Opnd (Comp))
4399 and then Check_Component (Right_Opnd (Comp)))
4401 or else (Nkind (Comp) in N_Unary_Op
4402 and then Check_Component (Right_Opnd (Comp)))
4404 or else (Nkind (Comp) = N_Selected_Component
4405 and then Check_Component (Prefix (Comp)))
4407 or else (Nkind (Comp) = N_Unchecked_Type_Conversion
4408 and then Check_Component (Expression (Comp)));
4409 end Check_Component;
4411 -- Start of processing for Safe_Component
4414 -- If the component appears in an association that may
4415 -- correspond to more than one element, it is not analyzed
4416 -- before the expansion into assignments, to avoid side effects.
4417 -- We analyze, but do not resolve the copy, to obtain sufficient
4418 -- entity information for the checks that follow. If component is
4419 -- overloaded we assume an unsafe function call.
4421 if not Analyzed (Comp) then
4422 if Is_Overloaded (Expr) then
4425 elsif Nkind (Expr) = N_Aggregate
4426 and then not Is_Others_Aggregate (Expr)
4430 elsif Nkind (Expr) = N_Allocator then
4432 -- For now, too complex to analyze
4437 Comp := New_Copy_Tree (Expr);
4438 Set_Parent (Comp, Parent (Expr));
4442 if Nkind (Comp) = N_Aggregate then
4443 return Safe_Aggregate (Comp);
4445 return Check_Component (Comp);
4449 -- Start of processing for In_Place_Assign_OK
4452 if Present (Component_Associations (N)) then
4454 -- On assignment, sliding can take place, so we cannot do the
4455 -- assignment in place unless the bounds of the aggregate are
4456 -- statically equal to those of the target.
4458 -- If the aggregate is given by an others choice, the bounds
4459 -- are derived from the left-hand side, and the assignment is
4460 -- safe if the expression is.
4462 if Is_Others_Aggregate (N) then
4465 (Expression (First (Component_Associations (N))));
4468 Aggr_In := First_Index (Etype (N));
4470 if Nkind (Parent (N)) = N_Assignment_Statement then
4471 Obj_In := First_Index (Etype (Name (Parent (N))));
4474 -- Context is an allocator. Check bounds of aggregate
4475 -- against given type in qualified expression.
4477 pragma Assert (Nkind (Parent (Parent (N))) = N_Allocator);
4479 First_Index (Etype (Entity (Subtype_Mark (Parent (N)))));
4482 while Present (Aggr_In) loop
4483 Get_Index_Bounds (Aggr_In, Aggr_Lo, Aggr_Hi);
4484 Get_Index_Bounds (Obj_In, Obj_Lo, Obj_Hi);
4486 if not Compile_Time_Known_Value (Aggr_Lo)
4487 or else not Compile_Time_Known_Value (Aggr_Hi)
4488 or else not Compile_Time_Known_Value (Obj_Lo)
4489 or else not Compile_Time_Known_Value (Obj_Hi)
4490 or else Expr_Value (Aggr_Lo) /= Expr_Value (Obj_Lo)
4491 or else Expr_Value (Aggr_Hi) /= Expr_Value (Obj_Hi)
4496 Next_Index (Aggr_In);
4497 Next_Index (Obj_In);
4501 -- Now check the component values themselves
4503 return Safe_Aggregate (N);
4504 end In_Place_Assign_OK;
4510 procedure Others_Check (Sub_Aggr : Node_Id; Dim : Pos) is
4511 Aggr_Lo : constant Node_Id := Aggr_Low (Dim);
4512 Aggr_Hi : constant Node_Id := Aggr_High (Dim);
4513 -- The bounds of the aggregate for this dimension
4515 Ind_Typ : constant Entity_Id := Aggr_Index_Typ (Dim);
4516 -- The index type for this dimension
4518 Need_To_Check : Boolean := False;
4520 Choices_Lo : Node_Id := Empty;
4521 Choices_Hi : Node_Id := Empty;
4522 -- The lowest and highest discrete choices for a named sub-aggregate
4524 Nb_Choices : Int := -1;
4525 -- The number of discrete non-others choices in this sub-aggregate
4527 Nb_Elements : Uint := Uint_0;
4528 -- The number of elements in a positional aggregate
4530 Cond : Node_Id := Empty;
4537 -- Check if we have an others choice. If we do make sure that this
4538 -- sub-aggregate contains at least one element in addition to the
4541 if Range_Checks_Suppressed (Ind_Typ) then
4542 Need_To_Check := False;
4544 elsif Present (Expressions (Sub_Aggr))
4545 and then Present (Component_Associations (Sub_Aggr))
4547 Need_To_Check := True;
4549 elsif Present (Component_Associations (Sub_Aggr)) then
4550 Assoc := Last (Component_Associations (Sub_Aggr));
4552 if Nkind (First (Choices (Assoc))) /= N_Others_Choice then
4553 Need_To_Check := False;
4556 -- Count the number of discrete choices. Start with -1 because
4557 -- the others choice does not count.
4560 Assoc := First (Component_Associations (Sub_Aggr));
4561 while Present (Assoc) loop
4562 Choice := First (Choices (Assoc));
4563 while Present (Choice) loop
4564 Nb_Choices := Nb_Choices + 1;
4571 -- If there is only an others choice nothing to do
4573 Need_To_Check := (Nb_Choices > 0);
4577 Need_To_Check := False;
4580 -- If we are dealing with a positional sub-aggregate with an others
4581 -- choice then compute the number or positional elements.
4583 if Need_To_Check and then Present (Expressions (Sub_Aggr)) then
4584 Expr := First (Expressions (Sub_Aggr));
4585 Nb_Elements := Uint_0;
4586 while Present (Expr) loop
4587 Nb_Elements := Nb_Elements + 1;
4591 -- If the aggregate contains discrete choices and an others choice
4592 -- compute the smallest and largest discrete choice values.
4594 elsif Need_To_Check then
4595 Compute_Choices_Lo_And_Choices_Hi : declare
4597 Table : Case_Table_Type (1 .. Nb_Choices);
4598 -- Used to sort all the different choice values
4605 Assoc := First (Component_Associations (Sub_Aggr));
4606 while Present (Assoc) loop
4607 Choice := First (Choices (Assoc));
4608 while Present (Choice) loop
4609 if Nkind (Choice) = N_Others_Choice then
4613 Get_Index_Bounds (Choice, Low, High);
4614 Table (J).Choice_Lo := Low;
4615 Table (J).Choice_Hi := High;
4624 -- Sort the discrete choices
4626 Sort_Case_Table (Table);
4628 Choices_Lo := Table (1).Choice_Lo;
4629 Choices_Hi := Table (Nb_Choices).Choice_Hi;
4630 end Compute_Choices_Lo_And_Choices_Hi;
4633 -- If no others choice in this sub-aggregate, or the aggregate
4634 -- comprises only an others choice, nothing to do.
4636 if not Need_To_Check then
4639 -- If we are dealing with an aggregate containing an others choice
4640 -- and positional components, we generate the following test:
4642 -- if Ind_Typ'Pos (Aggr_Lo) + (Nb_Elements - 1) >
4643 -- Ind_Typ'Pos (Aggr_Hi)
4645 -- raise Constraint_Error;
4648 elsif Nb_Elements > Uint_0 then
4654 Make_Attribute_Reference (Loc,
4655 Prefix => New_Occurrence_Of (Ind_Typ, Loc),
4656 Attribute_Name => Name_Pos,
4659 (Duplicate_Subexpr_Move_Checks (Aggr_Lo))),
4660 Right_Opnd => Make_Integer_Literal (Loc, Nb_Elements - 1)),
4663 Make_Attribute_Reference (Loc,
4664 Prefix => New_Occurrence_Of (Ind_Typ, Loc),
4665 Attribute_Name => Name_Pos,
4666 Expressions => New_List (
4667 Duplicate_Subexpr_Move_Checks (Aggr_Hi))));
4669 -- If we are dealing with an aggregate containing an others choice
4670 -- and discrete choices we generate the following test:
4672 -- [constraint_error when
4673 -- Choices_Lo < Aggr_Lo or else Choices_Hi > Aggr_Hi];
4681 Duplicate_Subexpr_Move_Checks (Choices_Lo),
4683 Duplicate_Subexpr_Move_Checks (Aggr_Lo)),
4688 Duplicate_Subexpr (Choices_Hi),
4690 Duplicate_Subexpr (Aggr_Hi)));
4693 if Present (Cond) then
4695 Make_Raise_Constraint_Error (Loc,
4697 Reason => CE_Length_Check_Failed));
4698 -- Questionable reason code, shouldn't that be a
4699 -- CE_Range_Check_Failed ???
4702 -- Now look inside the sub-aggregate to see if there is more work
4704 if Dim < Aggr_Dimension then
4706 -- Process positional components
4708 if Present (Expressions (Sub_Aggr)) then
4709 Expr := First (Expressions (Sub_Aggr));
4710 while Present (Expr) loop
4711 Others_Check (Expr, Dim + 1);
4716 -- Process component associations
4718 if Present (Component_Associations (Sub_Aggr)) then
4719 Assoc := First (Component_Associations (Sub_Aggr));
4720 while Present (Assoc) loop
4721 Expr := Expression (Assoc);
4722 Others_Check (Expr, Dim + 1);
4729 -------------------------
4730 -- Safe_Left_Hand_Side --
4731 -------------------------
4733 function Safe_Left_Hand_Side (N : Node_Id) return Boolean is
4734 function Is_Safe_Index (Indx : Node_Id) return Boolean;
4735 -- If the left-hand side includes an indexed component, check that
4736 -- the indexes are free of side-effect.
4742 function Is_Safe_Index (Indx : Node_Id) return Boolean is
4744 if Is_Entity_Name (Indx) then
4747 elsif Nkind (Indx) = N_Integer_Literal then
4750 elsif Nkind (Indx) = N_Function_Call
4751 and then Is_Entity_Name (Name (Indx))
4753 Has_Pragma_Pure_Function (Entity (Name (Indx)))
4757 elsif Nkind (Indx) = N_Type_Conversion
4758 and then Is_Safe_Index (Expression (Indx))
4767 -- Start of processing for Safe_Left_Hand_Side
4770 if Is_Entity_Name (N) then
4773 elsif Nkind_In (N, N_Explicit_Dereference, N_Selected_Component)
4774 and then Safe_Left_Hand_Side (Prefix (N))
4778 elsif Nkind (N) = N_Indexed_Component
4779 and then Safe_Left_Hand_Side (Prefix (N))
4781 Is_Safe_Index (First (Expressions (N)))
4785 elsif Nkind (N) = N_Unchecked_Type_Conversion then
4786 return Safe_Left_Hand_Side (Expression (N));
4791 end Safe_Left_Hand_Side;
4796 -- Holds the temporary aggregate value
4799 -- Holds the declaration of Tmp
4801 Aggr_Code : List_Id;
4802 Parent_Node : Node_Id;
4803 Parent_Kind : Node_Kind;
4805 -- Start of processing for Expand_Array_Aggregate
4808 -- Do not touch the special aggregates of attributes used for Asm calls
4810 if Is_RTE (Ctyp, RE_Asm_Input_Operand)
4811 or else Is_RTE (Ctyp, RE_Asm_Output_Operand)
4815 -- Do not expand an aggregate for an array type which contains tasks if
4816 -- the aggregate is associated with an unexpanded return statement of a
4817 -- build-in-place function. The aggregate is expanded when the related
4818 -- return statement (rewritten into an extended return) is processed.
4819 -- This delay ensures that any temporaries and initialization code
4820 -- generated for the aggregate appear in the proper return block and
4821 -- use the correct _chain and _master.
4823 elsif Has_Task (Base_Type (Etype (N)))
4824 and then Nkind (Parent (N)) = N_Simple_Return_Statement
4825 and then Is_Build_In_Place_Function
4826 (Return_Applies_To (Return_Statement_Entity (Parent (N))))
4831 -- If the semantic analyzer has determined that aggregate N will raise
4832 -- Constraint_Error at run time, then the aggregate node has been
4833 -- replaced with an N_Raise_Constraint_Error node and we should
4836 pragma Assert (not Raises_Constraint_Error (N));
4840 -- Check that the index range defined by aggregate bounds is
4841 -- compatible with corresponding index subtype.
4843 Index_Compatibility_Check : declare
4844 Aggr_Index_Range : Node_Id := First_Index (Typ);
4845 -- The current aggregate index range
4847 Index_Constraint : Node_Id := First_Index (Etype (Typ));
4848 -- The corresponding index constraint against which we have to
4849 -- check the above aggregate index range.
4852 Compute_Others_Present (N, 1);
4854 for J in 1 .. Aggr_Dimension loop
4855 -- There is no need to emit a check if an others choice is
4856 -- present for this array aggregate dimension since in this
4857 -- case one of N's sub-aggregates has taken its bounds from the
4858 -- context and these bounds must have been checked already. In
4859 -- addition all sub-aggregates corresponding to the same
4860 -- dimension must all have the same bounds (checked in (c) below).
4862 if not Range_Checks_Suppressed (Etype (Index_Constraint))
4863 and then not Others_Present (J)
4865 -- We don't use Checks.Apply_Range_Check here because it emits
4866 -- a spurious check. Namely it checks that the range defined by
4867 -- the aggregate bounds is non empty. But we know this already
4870 Check_Bounds (Aggr_Index_Range, Index_Constraint);
4873 -- Save the low and high bounds of the aggregate index as well as
4874 -- the index type for later use in checks (b) and (c) below.
4876 Aggr_Low (J) := Low_Bound (Aggr_Index_Range);
4877 Aggr_High (J) := High_Bound (Aggr_Index_Range);
4879 Aggr_Index_Typ (J) := Etype (Index_Constraint);
4881 Next_Index (Aggr_Index_Range);
4882 Next_Index (Index_Constraint);
4884 end Index_Compatibility_Check;
4888 -- If an others choice is present check that no aggregate index is
4889 -- outside the bounds of the index constraint.
4891 Others_Check (N, 1);
4895 -- For multidimensional arrays make sure that all subaggregates
4896 -- corresponding to the same dimension have the same bounds.
4898 if Aggr_Dimension > 1 then
4899 Check_Same_Aggr_Bounds (N, 1);
4904 -- If we have a default component value, or simple initialization is
4905 -- required for the component type, then we replace <> in component
4906 -- associations by the required default value.
4909 Default_Val : Node_Id;
4913 if (Present (Default_Aspect_Component_Value (Typ))
4914 or else Needs_Simple_Initialization (Ctyp))
4915 and then Present (Component_Associations (N))
4917 Assoc := First (Component_Associations (N));
4918 while Present (Assoc) loop
4919 if Nkind (Assoc) = N_Component_Association
4920 and then Box_Present (Assoc)
4922 Set_Box_Present (Assoc, False);
4924 if Present (Default_Aspect_Component_Value (Typ)) then
4925 Default_Val := Default_Aspect_Component_Value (Typ);
4927 Default_Val := Get_Simple_Init_Val (Ctyp, N);
4930 Set_Expression (Assoc, New_Copy_Tree (Default_Val));
4931 Analyze_And_Resolve (Expression (Assoc), Ctyp);
4941 -- Here we test for is packed array aggregate that we can handle at
4942 -- compile time. If so, return with transformation done. Note that we do
4943 -- this even if the aggregate is nested, because once we have done this
4944 -- processing, there is no more nested aggregate.
4946 if Packed_Array_Aggregate_Handled (N) then
4950 -- At this point we try to convert to positional form
4952 if Ekind (Current_Scope) = E_Package
4953 and then Static_Elaboration_Desired (Current_Scope)
4955 Convert_To_Positional (N, Max_Others_Replicate => 100);
4957 Convert_To_Positional (N);
4960 -- if the result is no longer an aggregate (e.g. it may be a string
4961 -- literal, or a temporary which has the needed value), then we are
4962 -- done, since there is no longer a nested aggregate.
4964 if Nkind (N) /= N_Aggregate then
4967 -- We are also done if the result is an analyzed aggregate, indicating
4968 -- that Convert_To_Positional succeeded and reanalyzed the rewritten
4972 and then N /= Original_Node (N)
4977 -- If all aggregate components are compile-time known and the aggregate
4978 -- has been flattened, nothing left to do. The same occurs if the
4979 -- aggregate is used to initialize the components of a statically
4980 -- allocated dispatch table.
4982 if Compile_Time_Known_Aggregate (N)
4983 or else Is_Static_Dispatch_Table_Aggregate (N)
4985 Set_Expansion_Delayed (N, False);
4989 -- Now see if back end processing is possible
4991 if Backend_Processing_Possible (N) then
4993 -- If the aggregate is static but the constraints are not, build
4994 -- a static subtype for the aggregate, so that Gigi can place it
4995 -- in static memory. Perform an unchecked_conversion to the non-
4996 -- static type imposed by the context.
4999 Itype : constant Entity_Id := Etype (N);
5001 Needs_Type : Boolean := False;
5004 Index := First_Index (Itype);
5005 while Present (Index) loop
5006 if not Is_Static_Subtype (Etype (Index)) then
5015 Build_Constrained_Type (Positional => True);
5016 Rewrite (N, Unchecked_Convert_To (Itype, N));
5026 -- Delay expansion for nested aggregates: it will be taken care of
5027 -- when the parent aggregate is expanded.
5029 Parent_Node := Parent (N);
5030 Parent_Kind := Nkind (Parent_Node);
5032 if Parent_Kind = N_Qualified_Expression then
5033 Parent_Node := Parent (Parent_Node);
5034 Parent_Kind := Nkind (Parent_Node);
5037 if Parent_Kind = N_Aggregate
5038 or else Parent_Kind = N_Extension_Aggregate
5039 or else Parent_Kind = N_Component_Association
5040 or else (Parent_Kind = N_Object_Declaration
5041 and then Needs_Finalization (Typ))
5042 or else (Parent_Kind = N_Assignment_Statement
5043 and then Inside_Init_Proc)
5045 if Static_Array_Aggregate (N)
5046 or else Compile_Time_Known_Aggregate (N)
5048 Set_Expansion_Delayed (N, False);
5051 Set_Expansion_Delayed (N);
5058 -- Look if in place aggregate expansion is possible
5060 -- For object declarations we build the aggregate in place, unless
5061 -- the array is bit-packed or the component is controlled.
5063 -- For assignments we do the assignment in place if all the component
5064 -- associations have compile-time known values. For other cases we
5065 -- create a temporary. The analysis for safety of on-line assignment
5066 -- is delicate, i.e. we don't know how to do it fully yet ???
5068 -- For allocators we assign to the designated object in place if the
5069 -- aggregate meets the same conditions as other in-place assignments.
5070 -- In this case the aggregate may not come from source but was created
5071 -- for default initialization, e.g. with Initialize_Scalars.
5073 if Requires_Transient_Scope (Typ) then
5074 Establish_Transient_Scope
5075 (N, Sec_Stack => Has_Controlled_Component (Typ));
5078 if Has_Default_Init_Comps (N) then
5079 Maybe_In_Place_OK := False;
5081 elsif Is_Bit_Packed_Array (Typ)
5082 or else Has_Controlled_Component (Typ)
5084 Maybe_In_Place_OK := False;
5087 Maybe_In_Place_OK :=
5088 (Nkind (Parent (N)) = N_Assignment_Statement
5089 and then Comes_From_Source (N)
5090 and then In_Place_Assign_OK)
5093 (Nkind (Parent (Parent (N))) = N_Allocator
5094 and then In_Place_Assign_OK);
5097 -- If this is an array of tasks, it will be expanded into build-in-place
5098 -- assignments. Build an activation chain for the tasks now.
5100 if Has_Task (Etype (N)) then
5101 Build_Activation_Chain_Entity (N);
5104 -- Perform in-place expansion of aggregate in an object declaration.
5105 -- Note: actions generated for the aggregate will be captured in an
5106 -- expression-with-actions statement so that they can be transferred
5107 -- to freeze actions later if there is an address clause for the
5108 -- object. (Note: we don't use a block statement because this would
5109 -- cause generated freeze nodes to be elaborated in the wrong scope).
5111 -- Should document these individual tests ???
5113 if not Has_Default_Init_Comps (N)
5114 and then Comes_From_Source (Parent_Node)
5115 and then Parent_Kind = N_Object_Declaration
5117 Must_Slide (Etype (Defining_Identifier (Parent_Node)), Typ)
5118 and then N = Expression (Parent_Node)
5119 and then not Is_Bit_Packed_Array (Typ)
5120 and then not Has_Controlled_Component (Typ)
5122 Tmp := Defining_Identifier (Parent (N));
5123 Set_No_Initialization (Parent (N));
5124 Set_Expression (Parent (N), Empty);
5126 -- Set the type of the entity, for use in the analysis of the
5127 -- subsequent indexed assignments. If the nominal type is not
5128 -- constrained, build a subtype from the known bounds of the
5129 -- aggregate. If the declaration has a subtype mark, use it,
5130 -- otherwise use the itype of the aggregate.
5132 if not Is_Constrained (Typ) then
5133 Build_Constrained_Type (Positional => False);
5134 elsif Is_Entity_Name (Object_Definition (Parent (N)))
5135 and then Is_Constrained (Entity (Object_Definition (Parent (N))))
5137 Set_Etype (Tmp, Entity (Object_Definition (Parent (N))));
5139 Set_Size_Known_At_Compile_Time (Typ, False);
5140 Set_Etype (Tmp, Typ);
5143 elsif Maybe_In_Place_OK
5144 and then Nkind (Parent (N)) = N_Qualified_Expression
5145 and then Nkind (Parent (Parent (N))) = N_Allocator
5147 Set_Expansion_Delayed (N);
5150 -- In the remaining cases the aggregate is the RHS of an assignment
5152 elsif Maybe_In_Place_OK
5153 and then Safe_Left_Hand_Side (Name (Parent (N)))
5155 Tmp := Name (Parent (N));
5157 if Etype (Tmp) /= Etype (N) then
5158 Apply_Length_Check (N, Etype (Tmp));
5160 if Nkind (N) = N_Raise_Constraint_Error then
5162 -- Static error, nothing further to expand
5168 elsif Maybe_In_Place_OK
5169 and then Nkind (Name (Parent (N))) = N_Slice
5170 and then Safe_Slice_Assignment (N)
5172 -- Safe_Slice_Assignment rewrites assignment as a loop
5178 -- In place aggregate expansion is not possible
5181 Maybe_In_Place_OK := False;
5182 Tmp := Make_Temporary (Loc, 'A', N);
5184 Make_Object_Declaration
5186 Defining_Identifier => Tmp,
5187 Object_Definition => New_Occurrence_Of (Typ, Loc));
5188 Set_No_Initialization (Tmp_Decl, True);
5190 -- If we are within a loop, the temporary will be pushed on the
5191 -- stack at each iteration. If the aggregate is the expression for an
5192 -- allocator, it will be immediately copied to the heap and can
5193 -- be reclaimed at once. We create a transient scope around the
5194 -- aggregate for this purpose.
5196 if Ekind (Current_Scope) = E_Loop
5197 and then Nkind (Parent (Parent (N))) = N_Allocator
5199 Establish_Transient_Scope (N, False);
5202 Insert_Action (N, Tmp_Decl);
5205 -- Construct and insert the aggregate code. We can safely suppress index
5206 -- checks because this code is guaranteed not to raise CE on index
5207 -- checks. However we should *not* suppress all checks.
5213 if Nkind (Tmp) = N_Defining_Identifier then
5214 Target := New_Occurrence_Of (Tmp, Loc);
5218 if Has_Default_Init_Comps (N) then
5220 -- Ada 2005 (AI-287): This case has not been analyzed???
5222 raise Program_Error;
5225 -- Name in assignment is explicit dereference
5227 Target := New_Copy (Tmp);
5231 Build_Array_Aggr_Code (N,
5233 Index => First_Index (Typ),
5235 Scalar_Comp => Is_Scalar_Type (Ctyp));
5238 if Comes_From_Source (Tmp) then
5240 Node_After : constant Node_Id := Next (Parent_Node);
5243 Insert_Actions_After (Parent_Node, Aggr_Code);
5245 if Parent_Kind = N_Object_Declaration then
5246 Collect_Initialization_Statements
5247 (Obj => Tmp, N => Parent_Node, Node_After => Node_After);
5252 Insert_Actions (N, Aggr_Code);
5255 -- If the aggregate has been assigned in place, remove the original
5258 if Nkind (Parent (N)) = N_Assignment_Statement
5259 and then Maybe_In_Place_OK
5261 Rewrite (Parent (N), Make_Null_Statement (Loc));
5263 elsif Nkind (Parent (N)) /= N_Object_Declaration
5264 or else Tmp /= Defining_Identifier (Parent (N))
5266 Rewrite (N, New_Occurrence_Of (Tmp, Loc));
5267 Analyze_And_Resolve (N, Typ);
5269 end Expand_Array_Aggregate;
5271 ------------------------
5272 -- Expand_N_Aggregate --
5273 ------------------------
5275 procedure Expand_N_Aggregate (N : Node_Id) is
5277 -- Record aggregate case
5279 if Is_Record_Type (Etype (N)) then
5280 Expand_Record_Aggregate (N);
5282 -- Array aggregate case
5285 -- A special case, if we have a string subtype with bounds 1 .. N,
5286 -- where N is known at compile time, and the aggregate is of the
5287 -- form (others => 'x'), with a single choice and no expressions,
5288 -- and N is less than 80 (an arbitrary limit for now), then replace
5289 -- the aggregate by the equivalent string literal (but do not mark
5290 -- it as static since it is not).
5292 -- Note: this entire circuit is redundant with respect to code in
5293 -- Expand_Array_Aggregate that collapses others choices to positional
5294 -- form, but there are two problems with that circuit:
5296 -- a) It is limited to very small cases due to ill-understood
5297 -- interactions with bootstrapping. That limit is removed by
5298 -- use of the No_Implicit_Loops restriction.
5300 -- b) It incorrectly ends up with the resulting expressions being
5301 -- considered static when they are not. For example, the
5302 -- following test should fail:
5304 -- pragma Restrictions (No_Implicit_Loops);
5305 -- package NonSOthers4 is
5306 -- B : constant String (1 .. 6) := (others => 'A');
5307 -- DH : constant String (1 .. 8) := B & "BB";
5309 -- pragma Export (C, X, Link_Name => DH);
5312 -- But it succeeds (DH looks static to pragma Export)
5314 -- To be sorted out ???
5316 if Present (Component_Associations (N)) then
5318 CA : constant Node_Id := First (Component_Associations (N));
5319 MX : constant := 80;
5322 if Nkind (First (Choices (CA))) = N_Others_Choice
5323 and then Nkind (Expression (CA)) = N_Character_Literal
5324 and then No (Expressions (N))
5327 T : constant Entity_Id := Etype (N);
5328 X : constant Node_Id := First_Index (T);
5329 EC : constant Node_Id := Expression (CA);
5330 CV : constant Uint := Char_Literal_Value (EC);
5331 CC : constant Int := UI_To_Int (CV);
5334 if Nkind (X) = N_Range
5335 and then Compile_Time_Known_Value (Low_Bound (X))
5336 and then Expr_Value (Low_Bound (X)) = 1
5337 and then Compile_Time_Known_Value (High_Bound (X))
5340 Hi : constant Uint := Expr_Value (High_Bound (X));
5346 for J in 1 .. UI_To_Int (Hi) loop
5347 Store_String_Char (Char_Code (CC));
5351 Make_String_Literal (Sloc (N),
5352 Strval => End_String));
5354 if CC >= Int (2 ** 16) then
5355 Set_Has_Wide_Wide_Character (N);
5356 elsif CC >= Int (2 ** 8) then
5357 Set_Has_Wide_Character (N);
5360 Analyze_And_Resolve (N, T);
5361 Set_Is_Static_Expression (N, False);
5371 -- Not that special case, so normal expansion of array aggregate
5373 Expand_Array_Aggregate (N);
5376 when RE_Not_Available =>
5378 end Expand_N_Aggregate;
5380 ----------------------------------
5381 -- Expand_N_Extension_Aggregate --
5382 ----------------------------------
5384 -- If the ancestor part is an expression, add a component association for
5385 -- the parent field. If the type of the ancestor part is not the direct
5386 -- parent of the expected type, build recursively the needed ancestors.
5387 -- If the ancestor part is a subtype_mark, replace aggregate with a decla-
5388 -- ration for a temporary of the expected type, followed by individual
5389 -- assignments to the given components.
5391 procedure Expand_N_Extension_Aggregate (N : Node_Id) is
5392 Loc : constant Source_Ptr := Sloc (N);
5393 A : constant Node_Id := Ancestor_Part (N);
5394 Typ : constant Entity_Id := Etype (N);
5397 -- If the ancestor is a subtype mark, an init proc must be called
5398 -- on the resulting object which thus has to be materialized in
5401 if Is_Entity_Name (A) and then Is_Type (Entity (A)) then
5402 Convert_To_Assignments (N, Typ);
5404 -- The extension aggregate is transformed into a record aggregate
5405 -- of the following form (c1 and c2 are inherited components)
5407 -- (Exp with c3 => a, c4 => b)
5408 -- ==> (c1 => Exp.c1, c2 => Exp.c2, c3 => a, c4 => b)
5413 if Tagged_Type_Expansion then
5414 Expand_Record_Aggregate (N,
5417 (Node (First_Elmt (Access_Disp_Table (Typ))), Loc),
5420 -- No tag is needed in the case of a VM
5423 Expand_Record_Aggregate (N, Parent_Expr => A);
5428 when RE_Not_Available =>
5430 end Expand_N_Extension_Aggregate;
5432 -----------------------------
5433 -- Expand_Record_Aggregate --
5434 -----------------------------
5436 procedure Expand_Record_Aggregate
5438 Orig_Tag : Node_Id := Empty;
5439 Parent_Expr : Node_Id := Empty)
5441 Loc : constant Source_Ptr := Sloc (N);
5442 Comps : constant List_Id := Component_Associations (N);
5443 Typ : constant Entity_Id := Etype (N);
5444 Base_Typ : constant Entity_Id := Base_Type (Typ);
5446 Static_Components : Boolean := True;
5447 -- Flag to indicate whether all components are compile-time known,
5448 -- and the aggregate can be constructed statically and handled by
5451 function Compile_Time_Known_Composite_Value (N : Node_Id) return Boolean;
5452 -- Returns true if N is an expression of composite type which can be
5453 -- fully evaluated at compile time without raising constraint error.
5454 -- Such expressions can be passed as is to Gigi without any expansion.
5456 -- This returns true for N_Aggregate with Compile_Time_Known_Aggregate
5457 -- set and constants whose expression is such an aggregate, recursively.
5459 function Component_Not_OK_For_Backend return Boolean;
5460 -- Check for presence of a component which makes it impossible for the
5461 -- backend to process the aggregate, thus requiring the use of a series
5462 -- of assignment statements. Cases checked for are a nested aggregate
5463 -- needing Late_Expansion, the presence of a tagged component which may
5464 -- need tag adjustment, and a bit unaligned component reference.
5466 -- We also force expansion into assignments if a component is of a
5467 -- mutable type (including a private type with discriminants) because
5468 -- in that case the size of the component to be copied may be smaller
5469 -- than the side of the target, and there is no simple way for gigi
5470 -- to compute the size of the object to be copied.
5472 -- NOTE: This is part of the ongoing work to define precisely the
5473 -- interface between front-end and back-end handling of aggregates.
5474 -- In general it is desirable to pass aggregates as they are to gigi,
5475 -- in order to minimize elaboration code. This is one case where the
5476 -- semantics of Ada complicate the analysis and lead to anomalies in
5477 -- the gcc back-end if the aggregate is not expanded into assignments.
5479 function Has_Visible_Private_Ancestor (Id : E) return Boolean;
5480 -- If any ancestor of the current type is private, the aggregate
5481 -- cannot be built in place. We cannot rely on Has_Private_Ancestor,
5482 -- because it will not be set when type and its parent are in the
5483 -- same scope, and the parent component needs expansion.
5485 function Top_Level_Aggregate (N : Node_Id) return Node_Id;
5486 -- For nested aggregates return the ultimate enclosing aggregate; for
5487 -- non-nested aggregates return N.
5489 ----------------------------------------
5490 -- Compile_Time_Known_Composite_Value --
5491 ----------------------------------------
5493 function Compile_Time_Known_Composite_Value
5494 (N : Node_Id) return Boolean
5497 -- If we have an entity name, then see if it is the name of a
5498 -- constant and if so, test the corresponding constant value.
5500 if Is_Entity_Name (N) then
5502 E : constant Entity_Id := Entity (N);
5505 if Ekind (E) /= E_Constant then
5508 V := Constant_Value (E);
5510 and then Compile_Time_Known_Composite_Value (V);
5514 -- We have a value, see if it is compile time known
5517 if Nkind (N) = N_Aggregate then
5518 return Compile_Time_Known_Aggregate (N);
5521 -- All other types of values are not known at compile time
5526 end Compile_Time_Known_Composite_Value;
5528 ----------------------------------
5529 -- Component_Not_OK_For_Backend --
5530 ----------------------------------
5532 function Component_Not_OK_For_Backend return Boolean is
5542 while Present (C) loop
5544 -- If the component has box initialization, expansion is needed
5545 -- and component is not ready for backend.
5547 if Box_Present (C) then
5551 if Nkind (Expression (C)) = N_Qualified_Expression then
5552 Expr_Q := Expression (Expression (C));
5554 Expr_Q := Expression (C);
5557 -- Return true if the aggregate has any associations for tagged
5558 -- components that may require tag adjustment.
5560 -- These are cases where the source expression may have a tag that
5561 -- could differ from the component tag (e.g., can occur for type
5562 -- conversions and formal parameters). (Tag adjustment not needed
5563 -- if VM_Target because object tags are implicit in the machine.)
5565 if Is_Tagged_Type (Etype (Expr_Q))
5566 and then (Nkind (Expr_Q) = N_Type_Conversion
5567 or else (Is_Entity_Name (Expr_Q)
5569 Ekind (Entity (Expr_Q)) in Formal_Kind))
5570 and then Tagged_Type_Expansion
5572 Static_Components := False;
5575 elsif Is_Delayed_Aggregate (Expr_Q) then
5576 Static_Components := False;
5579 elsif Possible_Bit_Aligned_Component (Expr_Q) then
5580 Static_Components := False;
5584 if Is_Elementary_Type (Etype (Expr_Q)) then
5585 if not Compile_Time_Known_Value (Expr_Q) then
5586 Static_Components := False;
5589 elsif not Compile_Time_Known_Composite_Value (Expr_Q) then
5590 Static_Components := False;
5592 if Is_Private_Type (Etype (Expr_Q))
5593 and then Has_Discriminants (Etype (Expr_Q))
5603 end Component_Not_OK_For_Backend;
5605 -----------------------------------
5606 -- Has_Visible_Private_Ancestor --
5607 -----------------------------------
5609 function Has_Visible_Private_Ancestor (Id : E) return Boolean is
5610 R : constant Entity_Id := Root_Type (Id);
5611 T1 : Entity_Id := Id;
5615 if Is_Private_Type (T1) then
5625 end Has_Visible_Private_Ancestor;
5627 -------------------------
5628 -- Top_Level_Aggregate --
5629 -------------------------
5631 function Top_Level_Aggregate (N : Node_Id) return Node_Id is
5636 while Present (Parent (Aggr))
5637 and then Nkind_In (Parent (Aggr), N_Component_Association,
5640 Aggr := Parent (Aggr);
5644 end Top_Level_Aggregate;
5648 Top_Level_Aggr : constant Node_Id := Top_Level_Aggregate (N);
5649 Tag_Value : Node_Id;
5653 -- Start of processing for Expand_Record_Aggregate
5656 -- If the aggregate is to be assigned to an atomic variable, we
5657 -- have to prevent a piecemeal assignment even if the aggregate
5658 -- is to be expanded. We create a temporary for the aggregate, and
5659 -- assign the temporary instead, so that the back end can generate
5660 -- an atomic move for it.
5663 and then Comes_From_Source (Parent (N))
5664 and then Is_Atomic_Aggregate (N, Typ)
5668 -- No special management required for aggregates used to initialize
5669 -- statically allocated dispatch tables
5671 elsif Is_Static_Dispatch_Table_Aggregate (N) then
5675 -- Ada 2005 (AI-318-2): We need to convert to assignments if components
5676 -- are build-in-place function calls. The assignments will each turn
5677 -- into a build-in-place function call. If components are all static,
5678 -- we can pass the aggregate to the backend regardless of limitedness.
5680 -- Extension aggregates, aggregates in extended return statements, and
5681 -- aggregates for C++ imported types must be expanded.
5683 if Ada_Version >= Ada_2005 and then Is_Limited_View (Typ) then
5684 if not Nkind_In (Parent (N), N_Object_Declaration,
5685 N_Component_Association)
5687 Convert_To_Assignments (N, Typ);
5689 elsif Nkind (N) = N_Extension_Aggregate
5690 or else Convention (Typ) = Convention_CPP
5692 Convert_To_Assignments (N, Typ);
5694 elsif not Size_Known_At_Compile_Time (Typ)
5695 or else Component_Not_OK_For_Backend
5696 or else not Static_Components
5698 Convert_To_Assignments (N, Typ);
5701 Set_Compile_Time_Known_Aggregate (N);
5702 Set_Expansion_Delayed (N, False);
5705 -- Gigi doesn't properly handle temporaries of variable size so we
5706 -- generate it in the front-end
5708 elsif not Size_Known_At_Compile_Time (Typ)
5709 and then Tagged_Type_Expansion
5711 Convert_To_Assignments (N, Typ);
5713 -- An aggregate used to initialize a controlled object must be turned
5714 -- into component assignments as the components themselves may require
5715 -- finalization actions such as adjustment.
5717 elsif Needs_Finalization (Typ) then
5718 Convert_To_Assignments (N, Typ);
5720 -- Ada 2005 (AI-287): In case of default initialized components we
5721 -- convert the aggregate into assignments.
5723 elsif Has_Default_Init_Comps (N) then
5724 Convert_To_Assignments (N, Typ);
5728 elsif Component_Not_OK_For_Backend then
5729 Convert_To_Assignments (N, Typ);
5731 -- If an ancestor is private, some components are not inherited and we
5732 -- cannot expand into a record aggregate.
5734 elsif Has_Visible_Private_Ancestor (Typ) then
5735 Convert_To_Assignments (N, Typ);
5737 -- ??? The following was done to compile fxacc00.ads in the ACVCs. Gigi
5738 -- is not able to handle the aggregate for Late_Request.
5740 elsif Is_Tagged_Type (Typ) and then Has_Discriminants (Typ) then
5741 Convert_To_Assignments (N, Typ);
5743 -- If the tagged types covers interface types we need to initialize all
5744 -- hidden components containing pointers to secondary dispatch tables.
5746 elsif Is_Tagged_Type (Typ) and then Has_Interfaces (Typ) then
5747 Convert_To_Assignments (N, Typ);
5749 -- If some components are mutable, the size of the aggregate component
5750 -- may be distinct from the default size of the type component, so
5751 -- we need to expand to insure that the back-end copies the proper
5752 -- size of the data. However, if the aggregate is the initial value of
5753 -- a constant, the target is immutable and might be built statically
5754 -- if components are appropriate.
5756 elsif Has_Mutable_Components (Typ)
5758 (Nkind (Parent (Top_Level_Aggr)) /= N_Object_Declaration
5759 or else not Constant_Present (Parent (Top_Level_Aggr))
5760 or else not Static_Components)
5762 Convert_To_Assignments (N, Typ);
5764 -- If the type involved has bit aligned components, then we are not sure
5765 -- that the back end can handle this case correctly.
5767 elsif Type_May_Have_Bit_Aligned_Components (Typ) then
5768 Convert_To_Assignments (N, Typ);
5770 -- In all other cases, build a proper aggregate to be handled by gigi
5773 if Nkind (N) = N_Aggregate then
5775 -- If the aggregate is static and can be handled by the back-end,
5776 -- nothing left to do.
5778 if Static_Components then
5779 Set_Compile_Time_Known_Aggregate (N);
5780 Set_Expansion_Delayed (N, False);
5784 -- If no discriminants, nothing special to do
5786 if not Has_Discriminants (Typ) then
5789 -- Case of discriminants present
5791 elsif Is_Derived_Type (Typ) then
5793 -- For untagged types, non-stored discriminants are replaced
5794 -- with stored discriminants, which are the ones that gigi uses
5795 -- to describe the type and its components.
5797 Generate_Aggregate_For_Derived_Type : declare
5798 Constraints : constant List_Id := New_List;
5799 First_Comp : Node_Id;
5800 Discriminant : Entity_Id;
5802 Num_Disc : Int := 0;
5803 Num_Gird : Int := 0;
5805 procedure Prepend_Stored_Values (T : Entity_Id);
5806 -- Scan the list of stored discriminants of the type, and add
5807 -- their values to the aggregate being built.
5809 ---------------------------
5810 -- Prepend_Stored_Values --
5811 ---------------------------
5813 procedure Prepend_Stored_Values (T : Entity_Id) is
5815 Discriminant := First_Stored_Discriminant (T);
5816 while Present (Discriminant) loop
5818 Make_Component_Association (Loc,
5820 New_List (New_Occurrence_Of (Discriminant, Loc)),
5824 Get_Discriminant_Value (
5827 Discriminant_Constraint (Typ))));
5829 if No (First_Comp) then
5830 Prepend_To (Component_Associations (N), New_Comp);
5832 Insert_After (First_Comp, New_Comp);
5835 First_Comp := New_Comp;
5836 Next_Stored_Discriminant (Discriminant);
5838 end Prepend_Stored_Values;
5840 -- Start of processing for Generate_Aggregate_For_Derived_Type
5843 -- Remove the associations for the discriminant of derived type
5845 First_Comp := First (Component_Associations (N));
5846 while Present (First_Comp) loop
5851 (First (Choices (Comp)))) = E_Discriminant
5854 Num_Disc := Num_Disc + 1;
5858 -- Insert stored discriminant associations in the correct
5859 -- order. If there are more stored discriminants than new
5860 -- discriminants, there is at least one new discriminant that
5861 -- constrains more than one of the stored discriminants. In
5862 -- this case we need to construct a proper subtype of the
5863 -- parent type, in order to supply values to all the
5864 -- components. Otherwise there is one-one correspondence
5865 -- between the constraints and the stored discriminants.
5867 First_Comp := Empty;
5869 Discriminant := First_Stored_Discriminant (Base_Type (Typ));
5870 while Present (Discriminant) loop
5871 Num_Gird := Num_Gird + 1;
5872 Next_Stored_Discriminant (Discriminant);
5875 -- Case of more stored discriminants than new discriminants
5877 if Num_Gird > Num_Disc then
5879 -- Create a proper subtype of the parent type, which is the
5880 -- proper implementation type for the aggregate, and convert
5881 -- it to the intended target type.
5883 Discriminant := First_Stored_Discriminant (Base_Type (Typ));
5884 while Present (Discriminant) loop
5887 Get_Discriminant_Value (
5890 Discriminant_Constraint (Typ)));
5891 Append (New_Comp, Constraints);
5892 Next_Stored_Discriminant (Discriminant);
5896 Make_Subtype_Declaration (Loc,
5897 Defining_Identifier => Make_Temporary (Loc, 'T'),
5898 Subtype_Indication =>
5899 Make_Subtype_Indication (Loc,
5901 New_Occurrence_Of (Etype (Base_Type (Typ)), Loc),
5903 Make_Index_Or_Discriminant_Constraint
5904 (Loc, Constraints)));
5906 Insert_Action (N, Decl);
5907 Prepend_Stored_Values (Base_Type (Typ));
5909 Set_Etype (N, Defining_Identifier (Decl));
5912 Rewrite (N, Unchecked_Convert_To (Typ, N));
5915 -- Case where we do not have fewer new discriminants than
5916 -- stored discriminants, so in this case we can simply use the
5917 -- stored discriminants of the subtype.
5920 Prepend_Stored_Values (Typ);
5922 end Generate_Aggregate_For_Derived_Type;
5925 if Is_Tagged_Type (Typ) then
5927 -- In the tagged case, _parent and _tag component must be created
5929 -- Reset Null_Present unconditionally. Tagged records always have
5930 -- at least one field (the tag or the parent).
5932 Set_Null_Record_Present (N, False);
5934 -- When the current aggregate comes from the expansion of an
5935 -- extension aggregate, the parent expr is replaced by an
5936 -- aggregate formed by selected components of this expr.
5938 if Present (Parent_Expr)
5939 and then Is_Empty_List (Comps)
5941 Comp := First_Component_Or_Discriminant (Typ);
5942 while Present (Comp) loop
5944 -- Skip all expander-generated components
5947 not Comes_From_Source (Original_Record_Component (Comp))
5953 Make_Selected_Component (Loc,
5955 Unchecked_Convert_To (Typ,
5956 Duplicate_Subexpr (Parent_Expr, True)),
5958 Selector_Name => New_Occurrence_Of (Comp, Loc));
5961 Make_Component_Association (Loc,
5963 New_List (New_Occurrence_Of (Comp, Loc)),
5967 Analyze_And_Resolve (New_Comp, Etype (Comp));
5970 Next_Component_Or_Discriminant (Comp);
5974 -- Compute the value for the Tag now, if the type is a root it
5975 -- will be included in the aggregate right away, otherwise it will
5976 -- be propagated to the parent aggregate.
5978 if Present (Orig_Tag) then
5979 Tag_Value := Orig_Tag;
5980 elsif not Tagged_Type_Expansion then
5985 (Node (First_Elmt (Access_Disp_Table (Typ))), Loc);
5988 -- For a derived type, an aggregate for the parent is formed with
5989 -- all the inherited components.
5991 if Is_Derived_Type (Typ) then
5994 First_Comp : Node_Id;
5995 Parent_Comps : List_Id;
5996 Parent_Aggr : Node_Id;
5997 Parent_Name : Node_Id;
6000 -- Remove the inherited component association from the
6001 -- aggregate and store them in the parent aggregate
6003 First_Comp := First (Component_Associations (N));
6004 Parent_Comps := New_List;
6005 while Present (First_Comp)
6006 and then Scope (Original_Record_Component (
6007 Entity (First (Choices (First_Comp))))) /= Base_Typ
6012 Append (Comp, Parent_Comps);
6015 Parent_Aggr := Make_Aggregate (Loc,
6016 Component_Associations => Parent_Comps);
6017 Set_Etype (Parent_Aggr, Etype (Base_Type (Typ)));
6019 -- Find the _parent component
6021 Comp := First_Component (Typ);
6022 while Chars (Comp) /= Name_uParent loop
6023 Comp := Next_Component (Comp);
6026 Parent_Name := New_Occurrence_Of (Comp, Loc);
6028 -- Insert the parent aggregate
6030 Prepend_To (Component_Associations (N),
6031 Make_Component_Association (Loc,
6032 Choices => New_List (Parent_Name),
6033 Expression => Parent_Aggr));
6035 -- Expand recursively the parent propagating the right Tag
6037 Expand_Record_Aggregate
6038 (Parent_Aggr, Tag_Value, Parent_Expr);
6040 -- The ancestor part may be a nested aggregate that has
6041 -- delayed expansion: recheck now.
6043 if Component_Not_OK_For_Backend then
6044 Convert_To_Assignments (N, Typ);
6048 -- For a root type, the tag component is added (unless compiling
6049 -- for the VMs, where tags are implicit).
6051 elsif Tagged_Type_Expansion then
6053 Tag_Name : constant Node_Id :=
6054 New_Occurrence_Of (First_Tag_Component (Typ), Loc);
6055 Typ_Tag : constant Entity_Id := RTE (RE_Tag);
6056 Conv_Node : constant Node_Id :=
6057 Unchecked_Convert_To (Typ_Tag, Tag_Value);
6060 Set_Etype (Conv_Node, Typ_Tag);
6061 Prepend_To (Component_Associations (N),
6062 Make_Component_Association (Loc,
6063 Choices => New_List (Tag_Name),
6064 Expression => Conv_Node));
6070 end Expand_Record_Aggregate;
6072 ----------------------------
6073 -- Has_Default_Init_Comps --
6074 ----------------------------
6076 function Has_Default_Init_Comps (N : Node_Id) return Boolean is
6077 Comps : constant List_Id := Component_Associations (N);
6081 pragma Assert (Nkind_In (N, N_Aggregate, N_Extension_Aggregate));
6087 if Has_Self_Reference (N) then
6091 -- Check if any direct component has default initialized components
6094 while Present (C) loop
6095 if Box_Present (C) then
6102 -- Recursive call in case of aggregate expression
6105 while Present (C) loop
6106 Expr := Expression (C);
6110 Nkind_In (Expr, N_Aggregate, N_Extension_Aggregate)
6111 and then Has_Default_Init_Comps (Expr)
6120 end Has_Default_Init_Comps;
6122 --------------------------
6123 -- Is_Delayed_Aggregate --
6124 --------------------------
6126 function Is_Delayed_Aggregate (N : Node_Id) return Boolean is
6127 Node : Node_Id := N;
6128 Kind : Node_Kind := Nkind (Node);
6131 if Kind = N_Qualified_Expression then
6132 Node := Expression (Node);
6133 Kind := Nkind (Node);
6136 if Kind /= N_Aggregate and then Kind /= N_Extension_Aggregate then
6139 return Expansion_Delayed (Node);
6141 end Is_Delayed_Aggregate;
6143 ----------------------------------------
6144 -- Is_Static_Dispatch_Table_Aggregate --
6145 ----------------------------------------
6147 function Is_Static_Dispatch_Table_Aggregate (N : Node_Id) return Boolean is
6148 Typ : constant Entity_Id := Base_Type (Etype (N));
6151 return Static_Dispatch_Tables
6152 and then Tagged_Type_Expansion
6153 and then RTU_Loaded (Ada_Tags)
6155 -- Avoid circularity when rebuilding the compiler
6157 and then Cunit_Entity (Get_Source_Unit (N)) /= RTU_Entity (Ada_Tags)
6158 and then (Typ = RTE (RE_Dispatch_Table_Wrapper)
6160 Typ = RTE (RE_Address_Array)
6162 Typ = RTE (RE_Type_Specific_Data)
6164 Typ = RTE (RE_Tag_Table)
6166 (RTE_Available (RE_Interface_Data)
6167 and then Typ = RTE (RE_Interface_Data))
6169 (RTE_Available (RE_Interfaces_Array)
6170 and then Typ = RTE (RE_Interfaces_Array))
6172 (RTE_Available (RE_Interface_Data_Element)
6173 and then Typ = RTE (RE_Interface_Data_Element)));
6174 end Is_Static_Dispatch_Table_Aggregate;
6176 -----------------------------
6177 -- Is_Two_Dim_Packed_Array --
6178 -----------------------------
6180 function Is_Two_Dim_Packed_Array (Typ : Entity_Id) return Boolean is
6181 C : constant Int := UI_To_Int (Component_Size (Typ));
6183 return Number_Dimensions (Typ) = 2
6184 and then Is_Bit_Packed_Array (Typ)
6185 and then (C = 1 or else C = 2 or else C = 4);
6186 end Is_Two_Dim_Packed_Array;
6188 --------------------
6189 -- Late_Expansion --
6190 --------------------
6192 function Late_Expansion
6195 Target : Node_Id) return List_Id
6197 Aggr_Code : List_Id;
6200 if Is_Record_Type (Etype (N)) then
6201 Aggr_Code := Build_Record_Aggr_Code (N, Typ, Target);
6203 -- Save the last assignment statement associated with the aggregate
6204 -- when building a controlled object. This reference is utilized by
6205 -- the finalization machinery when marking an object as successfully
6208 if Needs_Finalization (Typ)
6209 and then Is_Entity_Name (Target)
6210 and then Present (Entity (Target))
6211 and then Ekind (Entity (Target)) = E_Variable
6213 Set_Last_Aggregate_Assignment (Entity (Target), Last (Aggr_Code));
6218 else pragma Assert (Is_Array_Type (Etype (N)));
6220 Build_Array_Aggr_Code
6222 Ctype => Component_Type (Etype (N)),
6223 Index => First_Index (Typ),
6225 Scalar_Comp => Is_Scalar_Type (Component_Type (Typ)),
6226 Indexes => No_List);
6230 ----------------------------------
6231 -- Make_OK_Assignment_Statement --
6232 ----------------------------------
6234 function Make_OK_Assignment_Statement
6237 Expression : Node_Id) return Node_Id
6240 Set_Assignment_OK (Name);
6242 return Make_Assignment_Statement (Sloc, Name, Expression);
6243 end Make_OK_Assignment_Statement;
6245 -----------------------
6246 -- Number_Of_Choices --
6247 -----------------------
6249 function Number_Of_Choices (N : Node_Id) return Nat is
6253 Nb_Choices : Nat := 0;
6256 if Present (Expressions (N)) then
6260 Assoc := First (Component_Associations (N));
6261 while Present (Assoc) loop
6262 Choice := First (Choices (Assoc));
6263 while Present (Choice) loop
6264 if Nkind (Choice) /= N_Others_Choice then
6265 Nb_Choices := Nb_Choices + 1;
6275 end Number_Of_Choices;
6277 ------------------------------------
6278 -- Packed_Array_Aggregate_Handled --
6279 ------------------------------------
6281 -- The current version of this procedure will handle at compile time
6282 -- any array aggregate that meets these conditions:
6284 -- One and two dimensional, bit packed
6285 -- Underlying packed type is modular type
6286 -- Bounds are within 32-bit Int range
6287 -- All bounds and values are static
6289 -- Note: for now, in the 2-D case, we only handle component sizes of
6290 -- 1, 2, 4 (cases where an integral number of elements occupies a byte).
6292 function Packed_Array_Aggregate_Handled (N : Node_Id) return Boolean is
6293 Loc : constant Source_Ptr := Sloc (N);
6294 Typ : constant Entity_Id := Etype (N);
6295 Ctyp : constant Entity_Id := Component_Type (Typ);
6297 Not_Handled : exception;
6298 -- Exception raised if this aggregate cannot be handled
6301 -- Handle one- or two dimensional bit packed array
6303 if not Is_Bit_Packed_Array (Typ)
6304 or else Number_Dimensions (Typ) > 2
6309 -- If two-dimensional, check whether it can be folded, and transformed
6310 -- into a one-dimensional aggregate for the Packed_Array_Type of the
6313 if Number_Dimensions (Typ) = 2 then
6314 return Two_Dim_Packed_Array_Handled (N);
6317 if not Is_Modular_Integer_Type (Packed_Array_Type (Typ)) then
6321 if not Is_Scalar_Type (Component_Type (Typ))
6322 and then Has_Non_Standard_Rep (Component_Type (Typ))
6328 Csiz : constant Nat := UI_To_Int (Component_Size (Typ));
6332 -- Bounds of index type
6336 -- Values of bounds if compile time known
6338 function Get_Component_Val (N : Node_Id) return Uint;
6339 -- Given a expression value N of the component type Ctyp, returns a
6340 -- value of Csiz (component size) bits representing this value. If
6341 -- the value is non-static or any other reason exists why the value
6342 -- cannot be returned, then Not_Handled is raised.
6344 -----------------------
6345 -- Get_Component_Val --
6346 -----------------------
6348 function Get_Component_Val (N : Node_Id) return Uint is
6352 -- We have to analyze the expression here before doing any further
6353 -- processing here. The analysis of such expressions is deferred
6354 -- till expansion to prevent some problems of premature analysis.
6356 Analyze_And_Resolve (N, Ctyp);
6358 -- Must have a compile time value. String literals have to be
6359 -- converted into temporaries as well, because they cannot easily
6360 -- be converted into their bit representation.
6362 if not Compile_Time_Known_Value (N)
6363 or else Nkind (N) = N_String_Literal
6368 Val := Expr_Rep_Value (N);
6370 -- Adjust for bias, and strip proper number of bits
6372 if Has_Biased_Representation (Ctyp) then
6373 Val := Val - Expr_Value (Type_Low_Bound (Ctyp));
6376 return Val mod Uint_2 ** Csiz;
6377 end Get_Component_Val;
6379 -- Here we know we have a one dimensional bit packed array
6382 Get_Index_Bounds (First_Index (Typ), Lo, Hi);
6384 -- Cannot do anything if bounds are dynamic
6386 if not Compile_Time_Known_Value (Lo)
6388 not Compile_Time_Known_Value (Hi)
6393 -- Or are silly out of range of int bounds
6395 Lob := Expr_Value (Lo);
6396 Hib := Expr_Value (Hi);
6398 if not UI_Is_In_Int_Range (Lob)
6400 not UI_Is_In_Int_Range (Hib)
6405 -- At this stage we have a suitable aggregate for handling at compile
6406 -- time. The only remaining checks are that the values of expressions
6407 -- in the aggregate are compile-time known (checks are performed by
6408 -- Get_Component_Val), and that any subtypes or ranges are statically
6411 -- If the aggregate is not fully positional at this stage, then
6412 -- convert it to positional form. Either this will fail, in which
6413 -- case we can do nothing, or it will succeed, in which case we have
6414 -- succeeded in handling the aggregate and transforming it into a
6415 -- modular value, or it will stay an aggregate, in which case we
6416 -- have failed to create a packed value for it.
6418 if Present (Component_Associations (N)) then
6419 Convert_To_Positional
6420 (N, Max_Others_Replicate => 64, Handle_Bit_Packed => True);
6421 return Nkind (N) /= N_Aggregate;
6424 -- Otherwise we are all positional, so convert to proper value
6427 Lov : constant Int := UI_To_Int (Lob);
6428 Hiv : constant Int := UI_To_Int (Hib);
6430 Len : constant Nat := Int'Max (0, Hiv - Lov + 1);
6431 -- The length of the array (number of elements)
6433 Aggregate_Val : Uint;
6434 -- Value of aggregate. The value is set in the low order bits of
6435 -- this value. For the little-endian case, the values are stored
6436 -- from low-order to high-order and for the big-endian case the
6437 -- values are stored from high-order to low-order. Note that gigi
6438 -- will take care of the conversions to left justify the value in
6439 -- the big endian case (because of left justified modular type
6440 -- processing), so we do not have to worry about that here.
6443 -- Integer literal for resulting constructed value
6446 -- Shift count from low order for next value
6449 -- Shift increment for loop
6452 -- Next expression from positional parameters of aggregate
6454 Left_Justified : Boolean;
6455 -- Set True if we are filling the high order bits of the target
6456 -- value (i.e. the value is left justified).
6459 -- For little endian, we fill up the low order bits of the target
6460 -- value. For big endian we fill up the high order bits of the
6461 -- target value (which is a left justified modular value).
6463 Left_Justified := Bytes_Big_Endian;
6465 -- Switch justification if using -gnatd8
6467 if Debug_Flag_8 then
6468 Left_Justified := not Left_Justified;
6471 -- Switch justfification if reverse storage order
6473 if Reverse_Storage_Order (Base_Type (Typ)) then
6474 Left_Justified := not Left_Justified;
6477 if Left_Justified then
6478 Shift := Csiz * (Len - 1);
6485 -- Loop to set the values
6488 Aggregate_Val := Uint_0;
6490 Expr := First (Expressions (N));
6491 Aggregate_Val := Get_Component_Val (Expr) * Uint_2 ** Shift;
6493 for J in 2 .. Len loop
6494 Shift := Shift + Incr;
6497 Aggregate_Val + Get_Component_Val (Expr) * Uint_2 ** Shift;
6501 -- Now we can rewrite with the proper value
6503 Lit := Make_Integer_Literal (Loc, Intval => Aggregate_Val);
6504 Set_Print_In_Hex (Lit);
6506 -- Construct the expression using this literal. Note that it is
6507 -- important to qualify the literal with its proper modular type
6508 -- since universal integer does not have the required range and
6509 -- also this is a left justified modular type, which is important
6510 -- in the big-endian case.
6513 Unchecked_Convert_To (Typ,
6514 Make_Qualified_Expression (Loc,
6516 New_Occurrence_Of (Packed_Array_Type (Typ), Loc),
6517 Expression => Lit)));
6519 Analyze_And_Resolve (N, Typ);
6527 end Packed_Array_Aggregate_Handled;
6529 ----------------------------
6530 -- Has_Mutable_Components --
6531 ----------------------------
6533 function Has_Mutable_Components (Typ : Entity_Id) return Boolean is
6537 Comp := First_Component (Typ);
6538 while Present (Comp) loop
6539 if Is_Record_Type (Etype (Comp))
6540 and then Has_Discriminants (Etype (Comp))
6541 and then not Is_Constrained (Etype (Comp))
6546 Next_Component (Comp);
6550 end Has_Mutable_Components;
6552 ------------------------------
6553 -- Initialize_Discriminants --
6554 ------------------------------
6556 procedure Initialize_Discriminants (N : Node_Id; Typ : Entity_Id) is
6557 Loc : constant Source_Ptr := Sloc (N);
6558 Bas : constant Entity_Id := Base_Type (Typ);
6559 Par : constant Entity_Id := Etype (Bas);
6560 Decl : constant Node_Id := Parent (Par);
6564 if Is_Tagged_Type (Bas)
6565 and then Is_Derived_Type (Bas)
6566 and then Has_Discriminants (Par)
6567 and then Has_Discriminants (Bas)
6568 and then Number_Discriminants (Bas) /= Number_Discriminants (Par)
6569 and then Nkind (Decl) = N_Full_Type_Declaration
6570 and then Nkind (Type_Definition (Decl)) = N_Record_Definition
6572 (Variant_Part (Component_List (Type_Definition (Decl))))
6573 and then Nkind (N) /= N_Extension_Aggregate
6576 -- Call init proc to set discriminants.
6577 -- There should eventually be a special procedure for this ???
6579 Ref := New_Occurrence_Of (Defining_Identifier (N), Loc);
6580 Insert_Actions_After (N,
6581 Build_Initialization_Call (Sloc (N), Ref, Typ));
6583 end Initialize_Discriminants;
6590 (Obj_Type : Entity_Id;
6591 Typ : Entity_Id) return Boolean
6593 L1, L2, H1, H2 : Node_Id;
6595 -- No sliding if the type of the object is not established yet, if it is
6596 -- an unconstrained type whose actual subtype comes from the aggregate,
6597 -- or if the two types are identical.
6599 if not Is_Array_Type (Obj_Type) then
6602 elsif not Is_Constrained (Obj_Type) then
6605 elsif Typ = Obj_Type then
6609 -- Sliding can only occur along the first dimension
6611 Get_Index_Bounds (First_Index (Typ), L1, H1);
6612 Get_Index_Bounds (First_Index (Obj_Type), L2, H2);
6614 if not Is_Static_Expression (L1)
6615 or else not Is_Static_Expression (L2)
6616 or else not Is_Static_Expression (H1)
6617 or else not Is_Static_Expression (H2)
6621 return Expr_Value (L1) /= Expr_Value (L2)
6623 Expr_Value (H1) /= Expr_Value (H2);
6628 ---------------------------
6629 -- Safe_Slice_Assignment --
6630 ---------------------------
6632 function Safe_Slice_Assignment (N : Node_Id) return Boolean is
6633 Loc : constant Source_Ptr := Sloc (Parent (N));
6634 Pref : constant Node_Id := Prefix (Name (Parent (N)));
6635 Range_Node : constant Node_Id := Discrete_Range (Name (Parent (N)));
6643 -- Generate: for J in Range loop Pref (J) := Expr; end loop;
6645 if Comes_From_Source (N)
6646 and then No (Expressions (N))
6647 and then Nkind (First (Choices (First (Component_Associations (N)))))
6650 Expr := Expression (First (Component_Associations (N)));
6651 L_J := Make_Temporary (Loc, 'J');
6654 Make_Iteration_Scheme (Loc,
6655 Loop_Parameter_Specification =>
6656 Make_Loop_Parameter_Specification
6658 Defining_Identifier => L_J,
6659 Discrete_Subtype_Definition => Relocate_Node (Range_Node)));
6662 Make_Assignment_Statement (Loc,
6664 Make_Indexed_Component (Loc,
6665 Prefix => Relocate_Node (Pref),
6666 Expressions => New_List (New_Occurrence_Of (L_J, Loc))),
6667 Expression => Relocate_Node (Expr));
6669 -- Construct the final loop
6672 Make_Implicit_Loop_Statement
6673 (Node => Parent (N),
6674 Identifier => Empty,
6675 Iteration_Scheme => L_Iter,
6676 Statements => New_List (L_Body));
6678 -- Set type of aggregate to be type of lhs in assignment,
6679 -- to suppress redundant length checks.
6681 Set_Etype (N, Etype (Name (Parent (N))));
6683 Rewrite (Parent (N), Stat);
6684 Analyze (Parent (N));
6690 end Safe_Slice_Assignment;
6692 ----------------------------------
6693 -- Two_Dim_Packed_Array_Handled --
6694 ----------------------------------
6696 function Two_Dim_Packed_Array_Handled (N : Node_Id) return Boolean is
6697 Loc : constant Source_Ptr := Sloc (N);
6698 Typ : constant Entity_Id := Etype (N);
6699 Ctyp : constant Entity_Id := Component_Type (Typ);
6700 Comp_Size : constant Int := UI_To_Int (Component_Size (Typ));
6701 Packed_Array : constant Entity_Id := Packed_Array_Type (Base_Type (Typ));
6704 -- Expression in original aggregate
6707 -- One-dimensional subaggregate
6711 -- For now, only deal with cases where an integral number of elements
6712 -- fit in a single byte. This includes the most common boolean case.
6714 if not (Comp_Size = 1 or else
6715 Comp_Size = 2 or else
6721 Convert_To_Positional
6722 (N, Max_Others_Replicate => 64, Handle_Bit_Packed => True);
6724 -- Verify that all components are static
6726 if Nkind (N) = N_Aggregate
6727 and then Compile_Time_Known_Aggregate (N)
6731 -- The aggregate may have been re-analyzed and converted already
6733 elsif Nkind (N) /= N_Aggregate then
6736 -- If component associations remain, the aggregate is not static
6738 elsif Present (Component_Associations (N)) then
6742 One_Dim := First (Expressions (N));
6743 while Present (One_Dim) loop
6744 if Present (Component_Associations (One_Dim)) then
6748 One_Comp := First (Expressions (One_Dim));
6749 while Present (One_Comp) loop
6750 if not Is_OK_Static_Expression (One_Comp) then
6761 -- Two-dimensional aggregate is now fully positional so pack one
6762 -- dimension to create a static one-dimensional array, and rewrite
6763 -- as an unchecked conversion to the original type.
6766 Byte_Size : constant Int := UI_To_Int (Component_Size (Packed_Array));
6767 -- The packed array type is a byte array
6770 -- Number of components accumulated in current byte
6773 -- Assembled list of packed values for equivalent aggregate
6776 -- integer value of component
6779 -- Step size for packing
6782 -- Endian-dependent start position for packing
6785 -- Current insertion position
6788 -- Component of packed array being assembled.
6795 -- Account for endianness. See corresponding comment in
6796 -- Packed_Array_Aggregate_Handled concerning the following.
6800 xor Reverse_Storage_Order (Base_Type (Typ))
6802 Init_Shift := Byte_Size - Comp_Size;
6809 Shift := Init_Shift;
6810 One_Dim := First (Expressions (N));
6812 -- Iterate over each subaggregate
6814 while Present (One_Dim) loop
6815 One_Comp := First (Expressions (One_Dim));
6817 while Present (One_Comp) loop
6818 if Packed_Num = Byte_Size / Comp_Size then
6820 -- Byte is complete, add to list of expressions
6822 Append (Make_Integer_Literal (Sloc (One_Dim), Val), Comps);
6824 Shift := Init_Shift;
6828 Comp_Val := Expr_Rep_Value (One_Comp);
6830 -- Adjust for bias, and strip proper number of bits
6832 if Has_Biased_Representation (Ctyp) then
6833 Comp_Val := Comp_Val - Expr_Value (Type_Low_Bound (Ctyp));
6836 Comp_Val := Comp_Val mod Uint_2 ** Comp_Size;
6837 Val := UI_To_Int (Val + Comp_Val * Uint_2 ** Shift);
6838 Shift := Shift + Incr;
6839 One_Comp := Next (One_Comp);
6840 Packed_Num := Packed_Num + 1;
6844 One_Dim := Next (One_Dim);
6847 if Packed_Num > 0 then
6849 -- Add final incomplete byte if present
6851 Append (Make_Integer_Literal (Sloc (One_Dim), Val), Comps);
6855 Unchecked_Convert_To (Typ,
6856 Make_Qualified_Expression (Loc,
6857 Subtype_Mark => New_Occurrence_Of (Packed_Array, Loc),
6859 Make_Aggregate (Loc, Expressions => Comps))));
6860 Analyze_And_Resolve (N);
6863 end Two_Dim_Packed_Array_Handled;
6865 ---------------------
6866 -- Sort_Case_Table --
6867 ---------------------
6869 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type) is
6870 L : constant Int := Case_Table'First;
6871 U : constant Int := Case_Table'Last;
6879 T := Case_Table (K + 1);
6883 and then Expr_Value (Case_Table (J - 1).Choice_Lo) >
6884 Expr_Value (T.Choice_Lo)
6886 Case_Table (J) := Case_Table (J - 1);
6890 Case_Table (J) := T;
6893 end Sort_Case_Table;
6895 ----------------------------
6896 -- Static_Array_Aggregate --
6897 ----------------------------
6899 function Static_Array_Aggregate (N : Node_Id) return Boolean is
6900 Bounds : constant Node_Id := Aggregate_Bounds (N);
6902 Typ : constant Entity_Id := Etype (N);
6903 Comp_Type : constant Entity_Id := Component_Type (Typ);
6910 if Is_Tagged_Type (Typ)
6911 or else Is_Controlled (Typ)
6912 or else Is_Packed (Typ)
6918 and then Nkind (Bounds) = N_Range
6919 and then Nkind (Low_Bound (Bounds)) = N_Integer_Literal
6920 and then Nkind (High_Bound (Bounds)) = N_Integer_Literal
6922 Lo := Low_Bound (Bounds);
6923 Hi := High_Bound (Bounds);
6925 if No (Component_Associations (N)) then
6927 -- Verify that all components are static integers
6929 Expr := First (Expressions (N));
6930 while Present (Expr) loop
6931 if Nkind (Expr) /= N_Integer_Literal then
6941 -- We allow only a single named association, either a static
6942 -- range or an others_clause, with a static expression.
6944 Expr := First (Component_Associations (N));
6946 if Present (Expressions (N)) then
6949 elsif Present (Next (Expr)) then
6952 elsif Present (Next (First (Choices (Expr)))) then
6956 -- The aggregate is static if all components are literals,
6957 -- or else all its components are static aggregates for the
6958 -- component type. We also limit the size of a static aggregate
6959 -- to prevent runaway static expressions.
6961 if Is_Array_Type (Comp_Type)
6962 or else Is_Record_Type (Comp_Type)
6964 if Nkind (Expression (Expr)) /= N_Aggregate
6966 not Compile_Time_Known_Aggregate (Expression (Expr))
6971 elsif Nkind (Expression (Expr)) /= N_Integer_Literal then
6975 if not Aggr_Size_OK (N, Typ) then
6979 -- Create a positional aggregate with the right number of
6980 -- copies of the expression.
6982 Agg := Make_Aggregate (Sloc (N), New_List, No_List);
6984 for I in UI_To_Int (Intval (Lo)) .. UI_To_Int (Intval (Hi))
6987 (Expressions (Agg), New_Copy (Expression (Expr)));
6989 -- The copied expression must be analyzed and resolved.
6990 -- Besides setting the type, this ensures that static
6991 -- expressions are appropriately marked as such.
6994 (Last (Expressions (Agg)), Component_Type (Typ));
6997 Set_Aggregate_Bounds (Agg, Bounds);
6998 Set_Etype (Agg, Typ);
7001 Set_Compile_Time_Known_Aggregate (N);
7010 end Static_Array_Aggregate;