1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2019, 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 Freeze; use Freeze;
41 with Itypes; use Itypes;
43 with Namet; use Namet;
44 with Nmake; use Nmake;
45 with Nlists; use Nlists;
47 with Restrict; use Restrict;
48 with Rident; use Rident;
49 with Rtsfind; use Rtsfind;
50 with Ttypes; use Ttypes;
52 with Sem_Aggr; use Sem_Aggr;
53 with Sem_Aux; use Sem_Aux;
54 with Sem_Ch3; use Sem_Ch3;
55 with Sem_Eval; use Sem_Eval;
56 with Sem_Res; use Sem_Res;
57 with Sem_Util; use Sem_Util;
58 with Sinfo; use Sinfo;
59 with Snames; use Snames;
60 with Stand; use Stand;
61 with Stringt; use Stringt;
62 with Tbuild; use Tbuild;
63 with Uintp; use Uintp;
64 with Urealp; use Urealp;
66 package body Exp_Aggr is
68 type Case_Bounds is record
71 Choice_Node : Node_Id;
74 type Case_Table_Type is array (Nat range <>) of Case_Bounds;
75 -- Table type used by Check_Case_Choices procedure
77 procedure Collect_Initialization_Statements
80 Node_After : Node_Id);
81 -- If Obj is not frozen, collect actions inserted after N until, but not
82 -- including, Node_After, for initialization of Obj, and move them to an
83 -- expression with actions, which becomes the Initialization_Statements for
86 procedure Expand_Delta_Array_Aggregate (N : Node_Id; Deltas : List_Id);
87 procedure Expand_Delta_Record_Aggregate (N : Node_Id; Deltas : List_Id);
89 function Has_Default_Init_Comps (N : Node_Id) return Boolean;
90 -- N is an aggregate (record or array). Checks the presence of default
91 -- initialization (<>) in any component (Ada 2005: AI-287).
93 function Is_CCG_Supported_Aggregate (N : Node_Id) return Boolean;
94 -- Return True if aggregate N is located in a context supported by the
95 -- CCG backend; False otherwise.
97 function Is_Static_Dispatch_Table_Aggregate (N : Node_Id) return Boolean;
98 -- Returns true if N is an aggregate used to initialize the components
99 -- of a statically allocated dispatch table.
101 function Late_Expansion
104 Target : Node_Id) return List_Id;
105 -- This routine implements top-down expansion of nested aggregates. In
106 -- doing so, it avoids the generation of temporaries at each level. N is
107 -- a nested record or array aggregate with the Expansion_Delayed flag.
108 -- Typ is the expected type of the aggregate. Target is a (duplicatable)
109 -- expression that will hold the result of the aggregate expansion.
111 function Make_OK_Assignment_Statement
114 Expression : Node_Id) return Node_Id;
115 -- This is like Make_Assignment_Statement, except that Assignment_OK
116 -- is set in the left operand. All assignments built by this unit use
117 -- this routine. This is needed to deal with assignments to initialized
118 -- constants that are done in place.
121 (Obj_Type : Entity_Id;
122 Typ : Entity_Id) return Boolean;
123 -- A static array aggregate in an object declaration can in most cases be
124 -- expanded in place. The one exception is when the aggregate is given
125 -- with component associations that specify different bounds from those of
126 -- the type definition in the object declaration. In this pathological
127 -- case the aggregate must slide, and we must introduce an intermediate
128 -- temporary to hold it.
130 -- The same holds in an assignment to one-dimensional array of arrays,
131 -- when a component may be given with bounds that differ from those of the
134 function Number_Of_Choices (N : Node_Id) return Nat;
135 -- Returns the number of discrete choices (not including the others choice
136 -- if present) contained in (sub-)aggregate N.
138 procedure Process_Transient_Component
140 Comp_Typ : Entity_Id;
142 Fin_Call : out Node_Id;
143 Hook_Clear : out Node_Id;
144 Aggr : Node_Id := Empty;
145 Stmts : List_Id := No_List);
146 -- Subsidiary to the expansion of array and record aggregates. Generate
147 -- part of the necessary code to finalize a transient component. Comp_Typ
148 -- is the component type. Init_Expr is the initialization expression of the
149 -- component which is always a function call. Fin_Call is the finalization
150 -- call used to clean up the transient function result. Hook_Clear is the
151 -- hook reset statement. Aggr and Stmts both control the placement of the
152 -- generated code. Aggr is the related aggregate. If present, all code is
153 -- inserted prior to Aggr using Insert_Action. Stmts is the initialization
154 -- statements of the component. If present, all code is added to Stmts.
156 procedure Process_Transient_Component_Completion
160 Hook_Clear : Node_Id;
162 -- Subsidiary to the expansion of array and record aggregates. Generate
163 -- part of the necessary code to finalize a transient component. Aggr is
164 -- the related aggregate. Fin_Clear is the finalization call used to clean
165 -- up the transient component. Hook_Clear is the hook reset statment. Stmts
166 -- is the initialization statement list for the component. All generated
167 -- code is added to Stmts.
169 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type);
170 -- Sort the Case Table using the Lower Bound of each Choice as the key.
171 -- A simple insertion sort is used since the number of choices in a case
172 -- statement of variant part will usually be small and probably in near
175 ------------------------------------------------------
176 -- Local subprograms for Record Aggregate Expansion --
177 ------------------------------------------------------
179 function Is_Build_In_Place_Aggregate_Return (N : Node_Id) return Boolean;
180 -- True if N is an aggregate (possibly qualified or converted) that is
181 -- being returned from a build-in-place function.
183 function Build_Record_Aggr_Code
186 Lhs : Node_Id) return List_Id;
187 -- N is an N_Aggregate or an N_Extension_Aggregate. Typ is the type of the
188 -- aggregate. Target is an expression containing the location on which the
189 -- component by component assignments will take place. Returns the list of
190 -- assignments plus all other adjustments needed for tagged and controlled
193 procedure Convert_To_Assignments (N : Node_Id; Typ : Entity_Id);
194 -- Transform a record aggregate into a sequence of assignments performed
195 -- component by component. N is an N_Aggregate or N_Extension_Aggregate.
196 -- Typ is the type of the record aggregate.
198 procedure Expand_Record_Aggregate
200 Orig_Tag : Node_Id := Empty;
201 Parent_Expr : Node_Id := Empty);
202 -- This is the top level procedure for record aggregate expansion.
203 -- Expansion for record aggregates needs expand aggregates for tagged
204 -- record types. Specifically Expand_Record_Aggregate adds the Tag
205 -- field in front of the Component_Association list that was created
206 -- during resolution by Resolve_Record_Aggregate.
208 -- N is the record aggregate node.
209 -- Orig_Tag is the value of the Tag that has to be provided for this
210 -- specific aggregate. It carries the tag corresponding to the type
211 -- of the outermost aggregate during the recursive expansion
212 -- Parent_Expr is the ancestor part of the original extension
215 function Has_Mutable_Components (Typ : Entity_Id) return Boolean;
216 -- Return true if one of the components is of a discriminated type with
217 -- defaults. An aggregate for a type with mutable components must be
218 -- expanded into individual assignments.
220 function In_Place_Assign_OK (N : Node_Id) return Boolean;
221 -- Predicate to determine whether an aggregate assignment can be done in
222 -- place, because none of the new values can depend on the components of
223 -- the target of the assignment.
225 procedure Initialize_Discriminants (N : Node_Id; Typ : Entity_Id);
226 -- If the type of the aggregate is a type extension with renamed discrimi-
227 -- nants, we must initialize the hidden discriminants of the parent.
228 -- Otherwise, the target object must not be initialized. The discriminants
229 -- are initialized by calling the initialization procedure for the type.
230 -- This is incorrect if the initialization of other components has any
231 -- side effects. We restrict this call to the case where the parent type
232 -- has a variant part, because this is the only case where the hidden
233 -- discriminants are accessed, namely when calling discriminant checking
234 -- functions of the parent type, and when applying a stream attribute to
235 -- an object of the derived type.
237 -----------------------------------------------------
238 -- Local Subprograms for Array Aggregate Expansion --
239 -----------------------------------------------------
241 function Aggr_Size_OK (N : Node_Id; Typ : Entity_Id) return Boolean;
242 -- Very large static aggregates present problems to the back-end, and are
243 -- transformed into assignments and loops. This function verifies that the
244 -- total number of components of an aggregate is acceptable for rewriting
245 -- into a purely positional static form. Aggr_Size_OK must be called before
248 -- This function also detects and warns about one-component aggregates that
249 -- appear in a nonstatic context. Even if the component value is static,
250 -- such an aggregate must be expanded into an assignment.
252 function Backend_Processing_Possible (N : Node_Id) return Boolean;
253 -- This function checks if array aggregate N can be processed directly
254 -- by the backend. If this is the case, True is returned.
256 function Build_Array_Aggr_Code
261 Scalar_Comp : Boolean;
262 Indexes : List_Id := No_List) return List_Id;
263 -- This recursive routine returns a list of statements containing the
264 -- loops and assignments that are needed for the expansion of the array
267 -- N is the (sub-)aggregate node to be expanded into code. This node has
268 -- been fully analyzed, and its Etype is properly set.
270 -- Index is the index node corresponding to the array subaggregate N
272 -- Into is the target expression into which we are copying the aggregate.
273 -- Note that this node may not have been analyzed yet, and so the Etype
274 -- field may not be set.
276 -- Scalar_Comp is True if the component type of the aggregate is scalar
278 -- Indexes is the current list of expressions used to index the object we
281 procedure Convert_Array_Aggr_In_Allocator
285 -- If the aggregate appears within an allocator and can be expanded in
286 -- place, this routine generates the individual assignments to components
287 -- of the designated object. This is an optimization over the general
288 -- case, where a temporary is first created on the stack and then used to
289 -- construct the allocated object on the heap.
291 procedure Convert_To_Positional
293 Max_Others_Replicate : Nat := 32;
294 Handle_Bit_Packed : Boolean := False);
295 -- If possible, convert named notation to positional notation. This
296 -- conversion is possible only in some static cases. If the conversion is
297 -- possible, then N is rewritten with the analyzed converted aggregate.
298 -- The parameter Max_Others_Replicate controls the maximum number of
299 -- values corresponding to an others choice that will be converted to
300 -- positional notation (the default of 32 is the normal limit, and reflects
301 -- the fact that normally the loop is better than a lot of separate
302 -- assignments). Note that this limit gets overridden in any case if
303 -- either of the restrictions No_Elaboration_Code or No_Implicit_Loops is
304 -- set. The parameter Handle_Bit_Packed is usually set False (since we do
305 -- not expect the back end to handle bit packed arrays, so the normal case
306 -- of conversion is pointless), but in the special case of a call from
307 -- Packed_Array_Aggregate_Handled, we set this parameter to True, since
308 -- these are cases we handle in there.
310 procedure Expand_Array_Aggregate (N : Node_Id);
311 -- This is the top-level routine to perform array aggregate expansion.
312 -- N is the N_Aggregate node to be expanded.
314 function Is_Two_Dim_Packed_Array (Typ : Entity_Id) return Boolean;
315 -- For two-dimensional packed aggregates with constant bounds and constant
316 -- components, it is preferable to pack the inner aggregates because the
317 -- whole matrix can then be presented to the back-end as a one-dimensional
318 -- list of literals. This is much more efficient than expanding into single
319 -- component assignments. This function determines if the type Typ is for
320 -- an array that is suitable for this optimization: it returns True if Typ
321 -- is a two dimensional bit packed array with component size 1, 2, or 4.
323 function Packed_Array_Aggregate_Handled (N : Node_Id) return Boolean;
324 -- Given an array aggregate, this function handles the case of a packed
325 -- array aggregate with all constant values, where the aggregate can be
326 -- evaluated at compile time. If this is possible, then N is rewritten
327 -- to be its proper compile time value with all the components properly
328 -- assembled. The expression is analyzed and resolved and True is returned.
329 -- If this transformation is not possible, N is unchanged and False is
332 function Two_Dim_Packed_Array_Handled (N : Node_Id) return Boolean;
333 -- If the type of the aggregate is a two-dimensional bit_packed array
334 -- it may be transformed into an array of bytes with constant values,
335 -- and presented to the back-end as a static value. The function returns
336 -- false if this transformation cannot be performed. THis is similar to,
337 -- and reuses part of the machinery in Packed_Array_Aggregate_Handled.
343 function Aggr_Size_OK (N : Node_Id; Typ : Entity_Id) return Boolean is
352 -- Determines the maximum size of an array aggregate produced by
353 -- converting named to positional notation (e.g. from others clauses).
354 -- This avoids running away with attempts to convert huge aggregates,
355 -- which hit memory limits in the backend.
357 function Component_Count (T : Entity_Id) return Nat;
358 -- The limit is applied to the total number of subcomponents that the
359 -- aggregate will have, which is the number of static expressions
360 -- that will appear in the flattened array. This requires a recursive
361 -- computation of the number of scalar components of the structure.
363 ---------------------
364 -- Component_Count --
365 ---------------------
367 function Component_Count (T : Entity_Id) return Nat is
372 if Is_Scalar_Type (T) then
375 elsif Is_Record_Type (T) then
376 Comp := First_Component (T);
377 while Present (Comp) loop
378 Res := Res + Component_Count (Etype (Comp));
379 Next_Component (Comp);
384 elsif Is_Array_Type (T) then
386 Lo : constant Node_Id :=
387 Type_Low_Bound (Etype (First_Index (T)));
388 Hi : constant Node_Id :=
389 Type_High_Bound (Etype (First_Index (T)));
391 Siz : constant Nat := Component_Count (Component_Type (T));
394 -- Check for superflat arrays, i.e. arrays with such bounds
395 -- as 4 .. 2, to insure that this function never returns a
396 -- meaningless negative value.
398 if not Compile_Time_Known_Value (Lo)
399 or else not Compile_Time_Known_Value (Hi)
400 or else Expr_Value (Hi) < Expr_Value (Lo)
405 -- If the number of components is greater than Int'Last,
406 -- then return Int'Last, so caller will return False (Aggr
407 -- size is not OK). Otherwise, UI_To_Int will crash.
410 UI : constant Uint :=
411 Expr_Value (Hi) - Expr_Value (Lo) + 1;
413 if UI_Is_In_Int_Range (UI) then
414 return Siz * UI_To_Int (UI);
423 -- Can only be a null for an access type
429 -- Start of processing for Aggr_Size_OK
432 -- The normal aggregate limit is 500000, but we increase this limit to
433 -- 2**24 (about 16 million) if Restrictions (No_Elaboration_Code) or
434 -- Restrictions (No_Implicit_Loops) is specified, since in either case
435 -- we are at risk of declaring the program illegal because of this
436 -- limit. We also increase the limit when Static_Elaboration_Desired,
437 -- given that this means that objects are intended to be placed in data
440 -- We also increase the limit if the aggregate is for a packed two-
441 -- dimensional array, because if components are static it is much more
442 -- efficient to construct a one-dimensional equivalent array with static
445 -- Conversely, we decrease the maximum size if none of the above
446 -- requirements apply, and if the aggregate has a single component
447 -- association, which will be more efficient if implemented with a loop.
449 -- Finally, we use a small limit in CodePeer mode where we favor loops
450 -- instead of thousands of single assignments (from large aggregates).
452 Max_Aggr_Size := 500000;
454 if CodePeer_Mode then
455 Max_Aggr_Size := 100;
457 elsif Restriction_Active (No_Elaboration_Code)
458 or else Restriction_Active (No_Implicit_Loops)
459 or else Is_Two_Dim_Packed_Array (Typ)
460 or else (Ekind (Current_Scope) = E_Package
461 and then Static_Elaboration_Desired (Current_Scope))
463 Max_Aggr_Size := 2 ** 24;
465 elsif No (Expressions (N))
466 and then No (Next (First (Component_Associations (N))))
468 Max_Aggr_Size := 5000;
471 Size := UI_From_Int (Component_Count (Component_Type (Typ)));
473 Indx := First_Index (Typ);
474 while Present (Indx) loop
475 Lo := Type_Low_Bound (Etype (Indx));
476 Hi := Type_High_Bound (Etype (Indx));
478 -- Bounds need to be known at compile time
480 if not Compile_Time_Known_Value (Lo)
481 or else not Compile_Time_Known_Value (Hi)
486 Lov := Expr_Value (Lo);
487 Hiv := Expr_Value (Hi);
489 -- A flat array is always safe
495 -- One-component aggregates are suspicious, and if the context type
496 -- is an object declaration with nonstatic bounds it will trip gcc;
497 -- such an aggregate must be expanded into a single assignment.
499 if Hiv = Lov and then Nkind (Parent (N)) = N_Object_Declaration then
501 Index_Type : constant Entity_Id :=
503 (First_Index (Etype (Defining_Identifier (Parent (N)))));
507 if not Compile_Time_Known_Value (Type_Low_Bound (Index_Type))
508 or else not Compile_Time_Known_Value
509 (Type_High_Bound (Index_Type))
511 if Present (Component_Associations (N)) then
514 (Choice_List (First (Component_Associations (N))));
516 if Is_Entity_Name (Indx)
517 and then not Is_Type (Entity (Indx))
520 ("single component aggregate in "
521 & "non-static context??", Indx);
522 Error_Msg_N ("\maybe subtype name was meant??", Indx);
532 Rng : constant Uint := Hiv - Lov + 1;
535 -- Check if size is too large
537 if not UI_Is_In_Int_Range (Rng) then
541 -- Compute the size using universal arithmetic to avoid the
542 -- possibility of overflow on very large aggregates.
547 or else Size > Max_Aggr_Size
553 -- Bounds must be in integer range, for later array construction
555 if not UI_Is_In_Int_Range (Lov)
557 not UI_Is_In_Int_Range (Hiv)
568 ---------------------------------
569 -- Backend_Processing_Possible --
570 ---------------------------------
572 -- Backend processing by Gigi/gcc is possible only if all the following
573 -- conditions are met:
575 -- 1. N is fully positional
577 -- 2. N is not a bit-packed array aggregate;
579 -- 3. The size of N's array type must be known at compile time. Note
580 -- that this implies that the component size is also known
582 -- 4. The array type of N does not follow the Fortran layout convention
583 -- or if it does it must be 1 dimensional.
585 -- 5. The array component type may not be tagged (which could necessitate
586 -- reassignment of proper tags).
588 -- 6. The array component type must not have unaligned bit components
590 -- 7. None of the components of the aggregate may be bit unaligned
593 -- 8. There cannot be delayed components, since we do not know enough
594 -- at this stage to know if back end processing is possible.
596 -- 9. There cannot be any discriminated record components, since the
597 -- back end cannot handle this complex case.
599 -- 10. No controlled actions need to be generated for components
601 -- 11. When generating C code, N must be part of a N_Object_Declaration
603 -- 12. When generating C code, N must not include function calls
605 function Backend_Processing_Possible (N : Node_Id) return Boolean is
606 Typ : constant Entity_Id := Etype (N);
607 -- Typ is the correct constrained array subtype of the aggregate
609 function Component_Check (N : Node_Id; Index : Node_Id) return Boolean;
610 -- This routine checks components of aggregate N, enforcing checks
611 -- 1, 7, 8, 9, 11, and 12. In the multidimensional case, these checks
612 -- are performed on subaggregates. The Index value is the current index
613 -- being checked in the multidimensional case.
615 ---------------------
616 -- Component_Check --
617 ---------------------
619 function Component_Check (N : Node_Id; Index : Node_Id) return Boolean is
620 function Ultimate_Original_Expression (N : Node_Id) return Node_Id;
621 -- Given a type conversion or an unchecked type conversion N, return
622 -- its innermost original expression.
624 ----------------------------------
625 -- Ultimate_Original_Expression --
626 ----------------------------------
628 function Ultimate_Original_Expression (N : Node_Id) return Node_Id is
629 Expr : Node_Id := Original_Node (N);
632 while Nkind_In (Expr, N_Type_Conversion,
633 N_Unchecked_Type_Conversion)
635 Expr := Original_Node (Expression (Expr));
639 end Ultimate_Original_Expression;
645 -- Start of processing for Component_Check
648 -- Checks 1: (no component associations)
650 if Present (Component_Associations (N)) then
654 -- Checks 11: The C code generator cannot handle aggregates that are
655 -- not part of an object declaration.
657 if Modify_Tree_For_C and then not Is_CCG_Supported_Aggregate (N) then
661 -- Checks on components
663 -- Recurse to check subaggregates, which may appear in qualified
664 -- expressions. If delayed, the front-end will have to expand.
665 -- If the component is a discriminated record, treat as nonstatic,
666 -- as the back-end cannot handle this properly.
668 Expr := First (Expressions (N));
669 while Present (Expr) loop
671 -- Checks 8: (no delayed components)
673 if Is_Delayed_Aggregate (Expr) then
677 -- Checks 9: (no discriminated records)
679 if Present (Etype (Expr))
680 and then Is_Record_Type (Etype (Expr))
681 and then Has_Discriminants (Etype (Expr))
686 -- Checks 7. Component must not be bit aligned component
688 if Possible_Bit_Aligned_Component (Expr) then
692 -- Checks 12: (no function call)
696 Nkind (Ultimate_Original_Expression (Expr)) = N_Function_Call
701 -- Recursion to following indexes for multiple dimension case
703 if Present (Next_Index (Index))
704 and then not Component_Check (Expr, Next_Index (Index))
709 -- All checks for that component finished, on to next
717 -- Start of processing for Backend_Processing_Possible
720 -- Checks 2 (array not bit packed) and 10 (no controlled actions)
722 if Is_Bit_Packed_Array (Typ) or else Needs_Finalization (Typ) then
726 -- If component is limited, aggregate must be expanded because each
727 -- component assignment must be built in place.
729 if Is_Limited_View (Component_Type (Typ)) then
733 -- Checks 4 (array must not be multidimensional Fortran case)
735 if Convention (Typ) = Convention_Fortran
736 and then Number_Dimensions (Typ) > 1
741 -- Checks 3 (size of array must be known at compile time)
743 if not Size_Known_At_Compile_Time (Typ) then
747 -- Checks on components
749 if not Component_Check (N, First_Index (Typ)) then
753 -- Checks 5 (if the component type is tagged, then we may need to do
754 -- tag adjustments. Perhaps this should be refined to check for any
755 -- component associations that actually need tag adjustment, similar
756 -- to the test in Component_OK_For_Backend for record aggregates with
757 -- tagged components, but not clear whether it's worthwhile ???; in the
758 -- case of virtual machines (no Tagged_Type_Expansion), object tags are
759 -- handled implicitly).
761 if Is_Tagged_Type (Component_Type (Typ))
762 and then Tagged_Type_Expansion
767 -- Checks 6 (component type must not have bit aligned components)
769 if Type_May_Have_Bit_Aligned_Components (Component_Type (Typ)) then
773 -- Backend processing is possible
775 Set_Size_Known_At_Compile_Time (Etype (N), True);
777 end Backend_Processing_Possible;
779 ---------------------------
780 -- Build_Array_Aggr_Code --
781 ---------------------------
783 -- The code that we generate from a one dimensional aggregate is
785 -- 1. If the subaggregate contains discrete choices we
787 -- (a) Sort the discrete choices
789 -- (b) Otherwise for each discrete choice that specifies a range we
790 -- emit a loop. If a range specifies a maximum of three values, or
791 -- we are dealing with an expression we emit a sequence of
792 -- assignments instead of a loop.
794 -- (c) Generate the remaining loops to cover the others choice if any
796 -- 2. If the aggregate contains positional elements we
798 -- (a) translate the positional elements in a series of assignments
800 -- (b) Generate a final loop to cover the others choice if any.
801 -- Note that this final loop has to be a while loop since the case
803 -- L : Integer := Integer'Last;
804 -- H : Integer := Integer'Last;
805 -- A : array (L .. H) := (1, others =>0);
807 -- cannot be handled by a for loop. Thus for the following
809 -- array (L .. H) := (.. positional elements.., others =>E);
811 -- we always generate something like:
813 -- J : Index_Type := Index_Of_Last_Positional_Element;
815 -- J := Index_Base'Succ (J)
819 function Build_Array_Aggr_Code
824 Scalar_Comp : Boolean;
825 Indexes : List_Id := No_List) return List_Id
827 Loc : constant Source_Ptr := Sloc (N);
828 Index_Base : constant Entity_Id := Base_Type (Etype (Index));
829 Index_Base_L : constant Node_Id := Type_Low_Bound (Index_Base);
830 Index_Base_H : constant Node_Id := Type_High_Bound (Index_Base);
832 function Add (Val : Int; To : Node_Id) return Node_Id;
833 -- Returns an expression where Val is added to expression To, unless
834 -- To+Val is provably out of To's base type range. To must be an
835 -- already analyzed expression.
837 function Empty_Range (L, H : Node_Id) return Boolean;
838 -- Returns True if the range defined by L .. H is certainly empty
840 function Equal (L, H : Node_Id) return Boolean;
841 -- Returns True if L = H for sure
843 function Index_Base_Name return Node_Id;
844 -- Returns a new reference to the index type name
849 In_Loop : Boolean := False) return List_Id;
850 -- Ind must be a side-effect-free expression. If the input aggregate N
851 -- to Build_Loop contains no subaggregates, then this function returns
852 -- the assignment statement:
854 -- Into (Indexes, Ind) := Expr;
856 -- Otherwise we call Build_Code recursively. Flag In_Loop should be set
857 -- when the assignment appears within a generated loop.
859 -- Ada 2005 (AI-287): In case of default initialized component, Expr
860 -- is empty and we generate a call to the corresponding IP subprogram.
862 function Gen_Loop (L, H : Node_Id; Expr : Node_Id) return List_Id;
863 -- Nodes L and H must be side-effect-free expressions. If the input
864 -- aggregate N to Build_Loop contains no subaggregates, this routine
865 -- returns the for loop statement:
867 -- for J in Index_Base'(L) .. Index_Base'(H) loop
868 -- Into (Indexes, J) := Expr;
871 -- Otherwise we call Build_Code recursively. As an optimization if the
872 -- loop covers 3 or fewer scalar elements we generate a sequence of
874 -- If the component association that generates the loop comes from an
875 -- Iterated_Component_Association, the loop parameter has the name of
876 -- the corresponding parameter in the original construct.
878 function Gen_While (L, H : Node_Id; Expr : Node_Id) return List_Id;
879 -- Nodes L and H must be side-effect-free expressions. If the input
880 -- aggregate N to Build_Loop contains no subaggregates, this routine
881 -- returns the while loop statement:
883 -- J : Index_Base := L;
885 -- J := Index_Base'Succ (J);
886 -- Into (Indexes, J) := Expr;
889 -- Otherwise we call Build_Code recursively
891 function Get_Assoc_Expr (Assoc : Node_Id) return Node_Id;
892 -- For an association with a box, use value given by aspect
893 -- Default_Component_Value of array type if specified, else use
894 -- value given by aspect Default_Value for component type itself
895 -- if specified, else return Empty.
897 function Local_Compile_Time_Known_Value (E : Node_Id) return Boolean;
898 function Local_Expr_Value (E : Node_Id) return Uint;
899 -- These two Local routines are used to replace the corresponding ones
900 -- in sem_eval because while processing the bounds of an aggregate with
901 -- discrete choices whose index type is an enumeration, we build static
902 -- expressions not recognized by Compile_Time_Known_Value as such since
903 -- they have not yet been analyzed and resolved. All the expressions in
904 -- question are things like Index_Base_Name'Val (Const) which we can
905 -- easily recognize as being constant.
911 function Add (Val : Int; To : Node_Id) return Node_Id is
916 U_Val : constant Uint := UI_From_Int (Val);
919 -- Note: do not try to optimize the case of Val = 0, because
920 -- we need to build a new node with the proper Sloc value anyway.
922 -- First test if we can do constant folding
924 if Local_Compile_Time_Known_Value (To) then
925 U_To := Local_Expr_Value (To) + Val;
927 -- Determine if our constant is outside the range of the index.
928 -- If so return an Empty node. This empty node will be caught
929 -- by Empty_Range below.
931 if Compile_Time_Known_Value (Index_Base_L)
932 and then U_To < Expr_Value (Index_Base_L)
936 elsif Compile_Time_Known_Value (Index_Base_H)
937 and then U_To > Expr_Value (Index_Base_H)
942 Expr_Pos := Make_Integer_Literal (Loc, U_To);
943 Set_Is_Static_Expression (Expr_Pos);
945 if not Is_Enumeration_Type (Index_Base) then
948 -- If we are dealing with enumeration return
949 -- Index_Base'Val (Expr_Pos)
953 Make_Attribute_Reference
955 Prefix => Index_Base_Name,
956 Attribute_Name => Name_Val,
957 Expressions => New_List (Expr_Pos));
963 -- If we are here no constant folding possible
965 if not Is_Enumeration_Type (Index_Base) then
968 Left_Opnd => Duplicate_Subexpr (To),
969 Right_Opnd => Make_Integer_Literal (Loc, U_Val));
971 -- If we are dealing with enumeration return
972 -- Index_Base'Val (Index_Base'Pos (To) + Val)
976 Make_Attribute_Reference
978 Prefix => Index_Base_Name,
979 Attribute_Name => Name_Pos,
980 Expressions => New_List (Duplicate_Subexpr (To)));
985 Right_Opnd => Make_Integer_Literal (Loc, U_Val));
988 Make_Attribute_Reference
990 Prefix => Index_Base_Name,
991 Attribute_Name => Name_Val,
992 Expressions => New_List (Expr_Pos));
1002 function Empty_Range (L, H : Node_Id) return Boolean is
1003 Is_Empty : Boolean := False;
1008 -- First check if L or H were already detected as overflowing the
1009 -- index base range type by function Add above. If this is so Add
1010 -- returns the empty node.
1012 if No (L) or else No (H) then
1016 for J in 1 .. 3 loop
1019 -- L > H range is empty
1025 -- B_L > H range must be empty
1028 Low := Index_Base_L;
1031 -- L > B_H range must be empty
1035 High := Index_Base_H;
1038 if Local_Compile_Time_Known_Value (Low)
1040 Local_Compile_Time_Known_Value (High)
1043 UI_Gt (Local_Expr_Value (Low), Local_Expr_Value (High));
1056 function Equal (L, H : Node_Id) return Boolean is
1061 elsif Local_Compile_Time_Known_Value (L)
1063 Local_Compile_Time_Known_Value (H)
1065 return UI_Eq (Local_Expr_Value (L), Local_Expr_Value (H));
1078 In_Loop : Boolean := False) return List_Id
1080 function Add_Loop_Actions (Lis : List_Id) return List_Id;
1081 -- Collect insert_actions generated in the construction of a loop,
1082 -- and prepend them to the sequence of assignments to complete the
1083 -- eventual body of the loop.
1085 procedure Initialize_Array_Component
1086 (Arr_Comp : Node_Id;
1088 Init_Expr : Node_Id;
1090 -- Perform the initialization of array component Arr_Comp with
1091 -- expected type Comp_Typ. Init_Expr denotes the initialization
1092 -- expression of the array component. All generated code is added
1095 procedure Initialize_Ctrl_Array_Component
1096 (Arr_Comp : Node_Id;
1097 Comp_Typ : Entity_Id;
1098 Init_Expr : Node_Id;
1100 -- Perform the initialization of array component Arr_Comp when its
1101 -- expected type Comp_Typ needs finalization actions. Init_Expr is
1102 -- the initialization expression of the array component. All hook-
1103 -- related declarations are inserted prior to aggregate N. Remaining
1104 -- code is added to list Stmts.
1106 ----------------------
1107 -- Add_Loop_Actions --
1108 ----------------------
1110 function Add_Loop_Actions (Lis : List_Id) return List_Id is
1114 -- Ada 2005 (AI-287): Do nothing else in case of default
1115 -- initialized component.
1120 elsif Nkind (Parent (Expr)) = N_Component_Association
1121 and then Present (Loop_Actions (Parent (Expr)))
1123 Append_List (Lis, Loop_Actions (Parent (Expr)));
1124 Res := Loop_Actions (Parent (Expr));
1125 Set_Loop_Actions (Parent (Expr), No_List);
1131 end Add_Loop_Actions;
1133 --------------------------------
1134 -- Initialize_Array_Component --
1135 --------------------------------
1137 procedure Initialize_Array_Component
1138 (Arr_Comp : Node_Id;
1140 Init_Expr : Node_Id;
1143 Exceptions_OK : constant Boolean :=
1144 not Restriction_Active
1145 (No_Exception_Propagation);
1147 Finalization_OK : constant Boolean :=
1149 and then Needs_Finalization (Comp_Typ);
1151 Full_Typ : constant Entity_Id := Underlying_Type (Comp_Typ);
1153 Blk_Stmts : List_Id;
1154 Init_Stmt : Node_Id;
1157 -- Protect the initialization statements from aborts. Generate:
1161 if Finalization_OK and Abort_Allowed then
1162 if Exceptions_OK then
1163 Blk_Stmts := New_List;
1168 Append_To (Blk_Stmts, Build_Runtime_Call (Loc, RE_Abort_Defer));
1170 -- Otherwise aborts are not allowed. All generated code is added
1171 -- directly to the input list.
1177 -- Initialize the array element. Generate:
1179 -- Arr_Comp := Init_Expr;
1181 -- Note that the initialization expression is replicated because
1182 -- it has to be reevaluated within a generated loop.
1185 Make_OK_Assignment_Statement (Loc,
1186 Name => New_Copy_Tree (Arr_Comp),
1187 Expression => New_Copy_Tree (Init_Expr));
1188 Set_No_Ctrl_Actions (Init_Stmt);
1190 -- If this is an aggregate for an array of arrays, each
1191 -- subaggregate will be expanded as well, and even with
1192 -- No_Ctrl_Actions the assignments of inner components will
1193 -- require attachment in their assignments to temporaries. These
1194 -- temporaries must be finalized for each subaggregate. Generate:
1197 -- Arr_Comp := Init_Expr;
1200 if Finalization_OK and then Is_Array_Type (Comp_Typ) then
1202 Make_Block_Statement (Loc,
1203 Handled_Statement_Sequence =>
1204 Make_Handled_Sequence_Of_Statements (Loc,
1205 Statements => New_List (Init_Stmt)));
1208 Append_To (Blk_Stmts, Init_Stmt);
1210 -- Adjust the tag due to a possible view conversion. Generate:
1212 -- Arr_Comp._tag := Full_TypP;
1214 if Tagged_Type_Expansion
1215 and then Present (Comp_Typ)
1216 and then Is_Tagged_Type (Comp_Typ)
1218 Append_To (Blk_Stmts,
1219 Make_OK_Assignment_Statement (Loc,
1221 Make_Selected_Component (Loc,
1222 Prefix => New_Copy_Tree (Arr_Comp),
1225 (First_Tag_Component (Full_Typ), Loc)),
1228 Unchecked_Convert_To (RTE (RE_Tag),
1230 (Node (First_Elmt (Access_Disp_Table (Full_Typ))),
1234 -- Adjust the array component. Controlled subaggregates are not
1235 -- considered because each of their individual elements will
1236 -- receive an adjustment of its own. Generate:
1238 -- [Deep_]Adjust (Arr_Comp);
1241 and then not Is_Limited_Type (Comp_Typ)
1242 and then not Is_Build_In_Place_Function_Call (Init_Expr)
1244 (Is_Array_Type (Comp_Typ)
1245 and then Is_Controlled (Component_Type (Comp_Typ))
1246 and then Nkind (Expr) = N_Aggregate)
1250 (Obj_Ref => New_Copy_Tree (Arr_Comp),
1253 -- Guard against a missing [Deep_]Adjust when the component
1254 -- type was not frozen properly.
1256 if Present (Adj_Call) then
1257 Append_To (Blk_Stmts, Adj_Call);
1261 -- Complete the protection of the initialization statements
1263 if Finalization_OK and Abort_Allowed then
1265 -- Wrap the initialization statements in a block to catch a
1266 -- potential exception. Generate:
1270 -- Arr_Comp := Init_Expr;
1271 -- Arr_Comp._tag := Full_TypP;
1272 -- [Deep_]Adjust (Arr_Comp);
1274 -- Abort_Undefer_Direct;
1277 if Exceptions_OK then
1279 Build_Abort_Undefer_Block (Loc,
1283 -- Otherwise exceptions are not propagated. Generate:
1286 -- Arr_Comp := Init_Expr;
1287 -- Arr_Comp._tag := Full_TypP;
1288 -- [Deep_]Adjust (Arr_Comp);
1292 Append_To (Blk_Stmts,
1293 Build_Runtime_Call (Loc, RE_Abort_Undefer));
1296 end Initialize_Array_Component;
1298 -------------------------------------
1299 -- Initialize_Ctrl_Array_Component --
1300 -------------------------------------
1302 procedure Initialize_Ctrl_Array_Component
1303 (Arr_Comp : Node_Id;
1304 Comp_Typ : Entity_Id;
1305 Init_Expr : Node_Id;
1309 Act_Stmts : List_Id;
1312 Hook_Clear : Node_Id;
1314 In_Place_Expansion : Boolean;
1315 -- Flag set when a nonlimited controlled function call requires
1316 -- in-place expansion.
1319 -- Duplicate the initialization expression in case the context is
1320 -- a multi choice list or an "others" choice which plugs various
1321 -- holes in the aggregate. As a result the expression is no longer
1322 -- shared between the various components and is reevaluated for
1323 -- each such component.
1325 Expr := New_Copy_Tree (Init_Expr);
1326 Set_Parent (Expr, Parent (Init_Expr));
1328 -- Perform a preliminary analysis and resolution to determine what
1329 -- the initialization expression denotes. An unanalyzed function
1330 -- call may appear as an identifier or an indexed component.
1332 if Nkind_In (Expr, N_Function_Call,
1334 N_Indexed_Component)
1335 and then not Analyzed (Expr)
1337 Preanalyze_And_Resolve (Expr, Comp_Typ);
1340 In_Place_Expansion :=
1341 Nkind (Expr) = N_Function_Call
1342 and then not Is_Build_In_Place_Result_Type (Comp_Typ);
1344 -- The initialization expression is a controlled function call.
1345 -- Perform in-place removal of side effects to avoid creating a
1346 -- transient scope, which leads to premature finalization.
1348 -- This in-place expansion is not performed for limited transient
1349 -- objects, because the initialization is already done in place.
1351 if In_Place_Expansion then
1353 -- Suppress the removal of side effects by general analysis,
1354 -- because this behavior is emulated here. This avoids the
1355 -- generation of a transient scope, which leads to out-of-order
1356 -- adjustment and finalization.
1358 Set_No_Side_Effect_Removal (Expr);
1360 -- When the transient component initialization is related to a
1361 -- range or an "others", keep all generated statements within
1362 -- the enclosing loop. This way the controlled function call
1363 -- will be evaluated at each iteration, and its result will be
1364 -- finalized at the end of each iteration.
1370 -- Otherwise this is a single component initialization. Hook-
1371 -- related statements are inserted prior to the aggregate.
1375 Act_Stmts := No_List;
1378 -- Install all hook-related declarations and prepare the clean
1381 Process_Transient_Component
1383 Comp_Typ => Comp_Typ,
1385 Fin_Call => Fin_Call,
1386 Hook_Clear => Hook_Clear,
1388 Stmts => Act_Stmts);
1391 -- Use the noncontrolled component initialization circuitry to
1392 -- assign the result of the function call to the array element.
1393 -- This also performs subaggregate wrapping, tag adjustment, and
1394 -- [deep] adjustment of the array element.
1396 Initialize_Array_Component
1397 (Arr_Comp => Arr_Comp,
1398 Comp_Typ => Comp_Typ,
1402 -- At this point the array element is fully initialized. Complete
1403 -- the processing of the controlled array component by finalizing
1404 -- the transient function result.
1406 if In_Place_Expansion then
1407 Process_Transient_Component_Completion
1410 Fin_Call => Fin_Call,
1411 Hook_Clear => Hook_Clear,
1414 end Initialize_Ctrl_Array_Component;
1418 Stmts : constant List_Id := New_List;
1420 Comp_Typ : Entity_Id := Empty;
1422 Indexed_Comp : Node_Id;
1423 Init_Call : Node_Id;
1424 New_Indexes : List_Id;
1426 -- Start of processing for Gen_Assign
1429 if No (Indexes) then
1430 New_Indexes := New_List;
1432 New_Indexes := New_Copy_List_Tree (Indexes);
1435 Append_To (New_Indexes, Ind);
1437 if Present (Next_Index (Index)) then
1440 Build_Array_Aggr_Code
1443 Index => Next_Index (Index),
1445 Scalar_Comp => Scalar_Comp,
1446 Indexes => New_Indexes));
1449 -- If we get here then we are at a bottom-level (sub-)aggregate
1453 (Make_Indexed_Component (Loc,
1454 Prefix => New_Copy_Tree (Into),
1455 Expressions => New_Indexes));
1457 Set_Assignment_OK (Indexed_Comp);
1459 -- Ada 2005 (AI-287): In case of default initialized component, Expr
1460 -- is not present (and therefore we also initialize Expr_Q to empty).
1464 elsif Nkind (Expr) = N_Qualified_Expression then
1465 Expr_Q := Expression (Expr);
1470 if Present (Etype (N)) and then Etype (N) /= Any_Composite then
1471 Comp_Typ := Component_Type (Etype (N));
1472 pragma Assert (Comp_Typ = Ctype); -- AI-287
1474 elsif Present (Next (First (New_Indexes))) then
1476 -- Ada 2005 (AI-287): Do nothing in case of default initialized
1477 -- component because we have received the component type in
1478 -- the formal parameter Ctype.
1480 -- ??? Some assert pragmas have been added to check if this new
1481 -- formal can be used to replace this code in all cases.
1483 if Present (Expr) then
1485 -- This is a multidimensional array. Recover the component type
1486 -- from the outermost aggregate, because subaggregates do not
1487 -- have an assigned type.
1494 while Present (P) loop
1495 if Nkind (P) = N_Aggregate
1496 and then Present (Etype (P))
1498 Comp_Typ := Component_Type (Etype (P));
1506 pragma Assert (Comp_Typ = Ctype); -- AI-287
1511 -- Ada 2005 (AI-287): We only analyze the expression in case of non-
1512 -- default initialized components (otherwise Expr_Q is not present).
1515 and then Nkind_In (Expr_Q, N_Aggregate, N_Extension_Aggregate)
1517 -- At this stage the Expression may not have been analyzed yet
1518 -- because the array aggregate code has not been updated to use
1519 -- the Expansion_Delayed flag and avoid analysis altogether to
1520 -- solve the same problem (see Resolve_Aggr_Expr). So let us do
1521 -- the analysis of non-array aggregates now in order to get the
1522 -- value of Expansion_Delayed flag for the inner aggregate ???
1524 -- In the case of an iterated component association, the analysis
1525 -- of the generated loop will analyze the expression in the
1526 -- proper context, in which the loop parameter is visible.
1528 if Present (Comp_Typ) and then not Is_Array_Type (Comp_Typ) then
1529 if Nkind (Parent (Expr_Q)) = N_Iterated_Component_Association
1530 or else Nkind (Parent (Parent ((Expr_Q)))) =
1531 N_Iterated_Component_Association
1535 Analyze_And_Resolve (Expr_Q, Comp_Typ);
1539 if Is_Delayed_Aggregate (Expr_Q) then
1541 -- This is either a subaggregate of a multidimensional array,
1542 -- or a component of an array type whose component type is
1543 -- also an array. In the latter case, the expression may have
1544 -- component associations that provide different bounds from
1545 -- those of the component type, and sliding must occur. Instead
1546 -- of decomposing the current aggregate assignment, force the
1547 -- reanalysis of the assignment, so that a temporary will be
1548 -- generated in the usual fashion, and sliding will take place.
1550 if Nkind (Parent (N)) = N_Assignment_Statement
1551 and then Is_Array_Type (Comp_Typ)
1552 and then Present (Component_Associations (Expr_Q))
1553 and then Must_Slide (Comp_Typ, Etype (Expr_Q))
1555 Set_Expansion_Delayed (Expr_Q, False);
1556 Set_Analyzed (Expr_Q, False);
1561 Late_Expansion (Expr_Q, Etype (Expr_Q), Indexed_Comp));
1566 if Present (Expr) then
1568 -- Handle an initialization expression of a controlled type in
1569 -- case it denotes a function call. In general such a scenario
1570 -- will produce a transient scope, but this will lead to wrong
1571 -- order of initialization, adjustment, and finalization in the
1572 -- context of aggregates.
1574 -- Target (1) := Ctrl_Func_Call;
1577 -- Trans_Obj : ... := Ctrl_Func_Call; -- object
1578 -- Target (1) := Trans_Obj;
1579 -- Finalize (Trans_Obj);
1581 -- Target (1)._tag := ...;
1582 -- Adjust (Target (1));
1584 -- In the example above, the call to Finalize occurs too early
1585 -- and as a result it may leave the array component in a bad
1586 -- state. Finalization of the transient object should really
1587 -- happen after adjustment.
1589 -- To avoid this scenario, perform in-place side-effect removal
1590 -- of the function call. This eliminates the transient property
1591 -- of the function result and ensures correct order of actions.
1593 -- Res : ... := Ctrl_Func_Call;
1594 -- Target (1) := Res;
1595 -- Target (1)._tag := ...;
1596 -- Adjust (Target (1));
1599 if Present (Comp_Typ)
1600 and then Needs_Finalization (Comp_Typ)
1601 and then Nkind (Expr) /= N_Aggregate
1603 Initialize_Ctrl_Array_Component
1604 (Arr_Comp => Indexed_Comp,
1605 Comp_Typ => Comp_Typ,
1609 -- Otherwise perform simple component initialization
1612 Initialize_Array_Component
1613 (Arr_Comp => Indexed_Comp,
1614 Comp_Typ => Comp_Typ,
1619 -- Ada 2005 (AI-287): In case of default initialized component, call
1620 -- the initialization subprogram associated with the component type.
1621 -- If the component type is an access type, add an explicit null
1622 -- assignment, because for the back-end there is an initialization
1623 -- present for the whole aggregate, and no default initialization
1626 -- In addition, if the component type is controlled, we must call
1627 -- its Initialize procedure explicitly, because there is no explicit
1628 -- object creation that will invoke it otherwise.
1631 if Present (Base_Init_Proc (Base_Type (Ctype)))
1632 or else Has_Task (Base_Type (Ctype))
1634 Append_List_To (Stmts,
1635 Build_Initialization_Call (Loc,
1636 Id_Ref => Indexed_Comp,
1638 With_Default_Init => True));
1640 -- If the component type has invariants, add an invariant
1641 -- check after the component is default-initialized. It will
1642 -- be analyzed and resolved before the code for initialization
1643 -- of other components.
1645 if Has_Invariants (Ctype) then
1646 Set_Etype (Indexed_Comp, Ctype);
1647 Append_To (Stmts, Make_Invariant_Call (Indexed_Comp));
1650 elsif Is_Access_Type (Ctype) then
1652 Make_Assignment_Statement (Loc,
1653 Name => New_Copy_Tree (Indexed_Comp),
1654 Expression => Make_Null (Loc)));
1657 if Needs_Finalization (Ctype) then
1660 (Obj_Ref => New_Copy_Tree (Indexed_Comp),
1663 -- Guard against a missing [Deep_]Initialize when the component
1664 -- type was not properly frozen.
1666 if Present (Init_Call) then
1667 Append_To (Stmts, Init_Call);
1672 return Add_Loop_Actions (Stmts);
1679 function Gen_Loop (L, H : Node_Id; Expr : Node_Id) return List_Id is
1680 Is_Iterated_Component : constant Boolean :=
1681 Nkind (Parent (Expr)) = N_Iterated_Component_Association;
1692 -- Index_Base'(L) .. Index_Base'(H)
1694 L_Iteration_Scheme : Node_Id;
1695 -- L_J in Index_Base'(L) .. Index_Base'(H)
1698 -- The statements to execute in the loop
1700 S : constant List_Id := New_List;
1701 -- List of statements
1704 -- Copy of expression tree, used for checking purposes
1707 -- If loop bounds define an empty range return the null statement
1709 if Empty_Range (L, H) then
1710 Append_To (S, Make_Null_Statement (Loc));
1712 -- Ada 2005 (AI-287): Nothing else need to be done in case of
1713 -- default initialized component.
1719 -- The expression must be type-checked even though no component
1720 -- of the aggregate will have this value. This is done only for
1721 -- actual components of the array, not for subaggregates. Do
1722 -- the check on a copy, because the expression may be shared
1723 -- among several choices, some of which might be non-null.
1725 if Present (Etype (N))
1726 and then Is_Array_Type (Etype (N))
1727 and then No (Next_Index (Index))
1729 Expander_Mode_Save_And_Set (False);
1730 Tcopy := New_Copy_Tree (Expr);
1731 Set_Parent (Tcopy, N);
1732 Analyze_And_Resolve (Tcopy, Component_Type (Etype (N)));
1733 Expander_Mode_Restore;
1739 -- If loop bounds are the same then generate an assignment, unless
1740 -- the parent construct is an Iterated_Component_Association.
1742 elsif Equal (L, H) and then not Is_Iterated_Component then
1743 return Gen_Assign (New_Copy_Tree (L), Expr);
1745 -- If H - L <= 2 then generate a sequence of assignments when we are
1746 -- processing the bottom most aggregate and it contains scalar
1749 elsif No (Next_Index (Index))
1750 and then Scalar_Comp
1751 and then Local_Compile_Time_Known_Value (L)
1752 and then Local_Compile_Time_Known_Value (H)
1753 and then Local_Expr_Value (H) - Local_Expr_Value (L) <= 2
1754 and then not Is_Iterated_Component
1756 Append_List_To (S, Gen_Assign (New_Copy_Tree (L), Expr));
1757 Append_List_To (S, Gen_Assign (Add (1, To => L), Expr));
1759 if Local_Expr_Value (H) - Local_Expr_Value (L) = 2 then
1760 Append_List_To (S, Gen_Assign (Add (2, To => L), Expr));
1766 -- Otherwise construct the loop, starting with the loop index L_J
1768 if Is_Iterated_Component then
1770 Make_Defining_Identifier (Loc,
1771 Chars => (Chars (Defining_Identifier (Parent (Expr)))));
1774 L_J := Make_Temporary (Loc, 'J', L);
1777 -- Construct "L .. H" in Index_Base. We use a qualified expression
1778 -- for the bound to convert to the index base, but we don't need
1779 -- to do that if we already have the base type at hand.
1781 if Etype (L) = Index_Base then
1785 Make_Qualified_Expression (Loc,
1786 Subtype_Mark => Index_Base_Name,
1787 Expression => New_Copy_Tree (L));
1790 if Etype (H) = Index_Base then
1794 Make_Qualified_Expression (Loc,
1795 Subtype_Mark => Index_Base_Name,
1796 Expression => New_Copy_Tree (H));
1804 -- Construct "for L_J in Index_Base range L .. H"
1806 L_Iteration_Scheme :=
1807 Make_Iteration_Scheme
1809 Loop_Parameter_Specification =>
1810 Make_Loop_Parameter_Specification
1812 Defining_Identifier => L_J,
1813 Discrete_Subtype_Definition => L_Range));
1815 -- Construct the statements to execute in the loop body
1818 Gen_Assign (New_Occurrence_Of (L_J, Loc), Expr, In_Loop => True);
1820 -- Construct the final loop
1823 Make_Implicit_Loop_Statement
1825 Identifier => Empty,
1826 Iteration_Scheme => L_Iteration_Scheme,
1827 Statements => L_Body));
1829 -- A small optimization: if the aggregate is initialized with a box
1830 -- and the component type has no initialization procedure, remove the
1831 -- useless empty loop.
1833 if Nkind (First (S)) = N_Loop_Statement
1834 and then Is_Empty_List (Statements (First (S)))
1836 return New_List (Make_Null_Statement (Loc));
1846 -- The code built is
1848 -- W_J : Index_Base := L;
1849 -- while W_J < H loop
1850 -- W_J := Index_Base'Succ (W);
1854 function Gen_While (L, H : Node_Id; Expr : Node_Id) return List_Id is
1858 -- W_J : Base_Type := L;
1860 W_Iteration_Scheme : Node_Id;
1863 W_Index_Succ : Node_Id;
1864 -- Index_Base'Succ (J)
1866 W_Increment : Node_Id;
1867 -- W_J := Index_Base'Succ (W)
1869 W_Body : constant List_Id := New_List;
1870 -- The statements to execute in the loop
1872 S : constant List_Id := New_List;
1873 -- list of statement
1876 -- If loop bounds define an empty range or are equal return null
1878 if Empty_Range (L, H) or else Equal (L, H) then
1879 Append_To (S, Make_Null_Statement (Loc));
1883 -- Build the decl of W_J
1885 W_J := Make_Temporary (Loc, 'J', L);
1887 Make_Object_Declaration
1889 Defining_Identifier => W_J,
1890 Object_Definition => Index_Base_Name,
1893 -- Theoretically we should do a New_Copy_Tree (L) here, but we know
1894 -- that in this particular case L is a fresh Expr generated by
1895 -- Add which we are the only ones to use.
1897 Append_To (S, W_Decl);
1899 -- Construct " while W_J < H"
1901 W_Iteration_Scheme :=
1902 Make_Iteration_Scheme
1904 Condition => Make_Op_Lt
1906 Left_Opnd => New_Occurrence_Of (W_J, Loc),
1907 Right_Opnd => New_Copy_Tree (H)));
1909 -- Construct the statements to execute in the loop body
1912 Make_Attribute_Reference
1914 Prefix => Index_Base_Name,
1915 Attribute_Name => Name_Succ,
1916 Expressions => New_List (New_Occurrence_Of (W_J, Loc)));
1919 Make_OK_Assignment_Statement
1921 Name => New_Occurrence_Of (W_J, Loc),
1922 Expression => W_Index_Succ);
1924 Append_To (W_Body, W_Increment);
1926 Append_List_To (W_Body,
1927 Gen_Assign (New_Occurrence_Of (W_J, Loc), Expr, In_Loop => True));
1929 -- Construct the final loop
1932 Make_Implicit_Loop_Statement
1934 Identifier => Empty,
1935 Iteration_Scheme => W_Iteration_Scheme,
1936 Statements => W_Body));
1941 --------------------
1942 -- Get_Assoc_Expr --
1943 --------------------
1945 function Get_Assoc_Expr (Assoc : Node_Id) return Node_Id is
1946 Typ : constant Entity_Id := Base_Type (Etype (N));
1949 if Box_Present (Assoc) then
1950 if Is_Scalar_Type (Ctype) then
1951 if Present (Default_Aspect_Component_Value (Typ)) then
1952 return Default_Aspect_Component_Value (Typ);
1953 elsif Present (Default_Aspect_Value (Ctype)) then
1954 return Default_Aspect_Value (Ctype);
1964 return Expression (Assoc);
1968 ---------------------
1969 -- Index_Base_Name --
1970 ---------------------
1972 function Index_Base_Name return Node_Id is
1974 return New_Occurrence_Of (Index_Base, Sloc (N));
1975 end Index_Base_Name;
1977 ------------------------------------
1978 -- Local_Compile_Time_Known_Value --
1979 ------------------------------------
1981 function Local_Compile_Time_Known_Value (E : Node_Id) return Boolean is
1983 return Compile_Time_Known_Value (E)
1985 (Nkind (E) = N_Attribute_Reference
1986 and then Attribute_Name (E) = Name_Val
1987 and then Compile_Time_Known_Value (First (Expressions (E))));
1988 end Local_Compile_Time_Known_Value;
1990 ----------------------
1991 -- Local_Expr_Value --
1992 ----------------------
1994 function Local_Expr_Value (E : Node_Id) return Uint is
1996 if Compile_Time_Known_Value (E) then
1997 return Expr_Value (E);
1999 return Expr_Value (First (Expressions (E)));
2001 end Local_Expr_Value;
2005 New_Code : constant List_Id := New_List;
2007 Aggr_L : constant Node_Id := Low_Bound (Aggregate_Bounds (N));
2008 Aggr_H : constant Node_Id := High_Bound (Aggregate_Bounds (N));
2009 -- The aggregate bounds of this specific subaggregate. Note that if the
2010 -- code generated by Build_Array_Aggr_Code is executed then these bounds
2011 -- are OK. Otherwise a Constraint_Error would have been raised.
2013 Aggr_Low : constant Node_Id := Duplicate_Subexpr_No_Checks (Aggr_L);
2014 Aggr_High : constant Node_Id := Duplicate_Subexpr_No_Checks (Aggr_H);
2015 -- After Duplicate_Subexpr these are side-effect free
2024 Nb_Choices : Nat := 0;
2025 Table : Case_Table_Type (1 .. Number_Of_Choices (N));
2026 -- Used to sort all the different choice values
2029 -- Number of elements in the positional aggregate
2031 Others_Assoc : Node_Id := Empty;
2033 -- Start of processing for Build_Array_Aggr_Code
2036 -- First before we start, a special case. if we have a bit packed
2037 -- array represented as a modular type, then clear the value to
2038 -- zero first, to ensure that unused bits are properly cleared.
2043 and then Is_Bit_Packed_Array (Typ)
2044 and then Is_Modular_Integer_Type (Packed_Array_Impl_Type (Typ))
2047 Zero : constant Node_Id := Make_Integer_Literal (Loc, Uint_0);
2049 Analyze_And_Resolve (Zero, Packed_Array_Impl_Type (Typ));
2050 Append_To (New_Code,
2051 Make_Assignment_Statement (Loc,
2052 Name => New_Copy_Tree (Into),
2053 Expression => Unchecked_Convert_To (Typ, Zero)));
2057 -- If the component type contains tasks, we need to build a Master
2058 -- entity in the current scope, because it will be needed if build-
2059 -- in-place functions are called in the expanded code.
2061 if Nkind (Parent (N)) = N_Object_Declaration and then Has_Task (Typ) then
2062 Build_Master_Entity (Defining_Identifier (Parent (N)));
2065 -- STEP 1: Process component associations
2067 -- For those associations that may generate a loop, initialize
2068 -- Loop_Actions to collect inserted actions that may be crated.
2070 -- Skip this if no component associations
2072 if No (Expressions (N)) then
2074 -- STEP 1 (a): Sort the discrete choices
2076 Assoc := First (Component_Associations (N));
2077 while Present (Assoc) loop
2078 Choice := First (Choice_List (Assoc));
2079 while Present (Choice) loop
2080 if Nkind (Choice) = N_Others_Choice then
2081 Others_Assoc := Assoc;
2085 Get_Index_Bounds (Choice, Low, High);
2088 Set_Loop_Actions (Assoc, New_List);
2091 Nb_Choices := Nb_Choices + 1;
2093 Table (Nb_Choices) :=
2096 Choice_Node => Get_Assoc_Expr (Assoc));
2104 -- If there is more than one set of choices these must be static
2105 -- and we can therefore sort them. Remember that Nb_Choices does not
2106 -- account for an others choice.
2108 if Nb_Choices > 1 then
2109 Sort_Case_Table (Table);
2112 -- STEP 1 (b): take care of the whole set of discrete choices
2114 for J in 1 .. Nb_Choices loop
2115 Low := Table (J).Choice_Lo;
2116 High := Table (J).Choice_Hi;
2117 Expr := Table (J).Choice_Node;
2118 Append_List (Gen_Loop (Low, High, Expr), To => New_Code);
2121 -- STEP 1 (c): generate the remaining loops to cover others choice
2122 -- We don't need to generate loops over empty gaps, but if there is
2123 -- a single empty range we must analyze the expression for semantics
2125 if Present (Others_Assoc) then
2127 First : Boolean := True;
2131 for J in 0 .. Nb_Choices loop
2135 Low := Add (1, To => Table (J).Choice_Hi);
2138 if J = Nb_Choices then
2141 High := Add (-1, To => Table (J + 1).Choice_Lo);
2144 -- If this is an expansion within an init proc, make
2145 -- sure that discriminant references are replaced by
2146 -- the corresponding discriminal.
2148 if Inside_Init_Proc then
2149 if Is_Entity_Name (Low)
2150 and then Ekind (Entity (Low)) = E_Discriminant
2152 Set_Entity (Low, Discriminal (Entity (Low)));
2155 if Is_Entity_Name (High)
2156 and then Ekind (Entity (High)) = E_Discriminant
2158 Set_Entity (High, Discriminal (Entity (High)));
2163 or else not Empty_Range (Low, High)
2167 -- Duplicate the expression in case we will be generating
2168 -- several loops. As a result the expression is no longer
2169 -- shared between the loops and is reevaluated for each
2172 Expr := Get_Assoc_Expr (Others_Assoc);
2173 Dup_Expr := New_Copy_Tree (Expr);
2174 Set_Parent (Dup_Expr, Parent (Expr));
2176 Set_Loop_Actions (Others_Assoc, New_List);
2178 (Gen_Loop (Low, High, Dup_Expr), To => New_Code);
2184 -- STEP 2: Process positional components
2187 -- STEP 2 (a): Generate the assignments for each positional element
2188 -- Note that here we have to use Aggr_L rather than Aggr_Low because
2189 -- Aggr_L is analyzed and Add wants an analyzed expression.
2191 Expr := First (Expressions (N));
2193 while Present (Expr) loop
2194 Nb_Elements := Nb_Elements + 1;
2195 Append_List (Gen_Assign (Add (Nb_Elements, To => Aggr_L), Expr),
2200 -- STEP 2 (b): Generate final loop if an others choice is present
2201 -- Here Nb_Elements gives the offset of the last positional element.
2203 if Present (Component_Associations (N)) then
2204 Assoc := Last (Component_Associations (N));
2206 -- Ada 2005 (AI-287)
2208 Append_List (Gen_While (Add (Nb_Elements, To => Aggr_L),
2210 Get_Assoc_Expr (Assoc)), -- AI-287
2216 end Build_Array_Aggr_Code;
2218 ----------------------------
2219 -- Build_Record_Aggr_Code --
2220 ----------------------------
2222 function Build_Record_Aggr_Code
2225 Lhs : Node_Id) return List_Id
2227 Loc : constant Source_Ptr := Sloc (N);
2228 L : constant List_Id := New_List;
2229 N_Typ : constant Entity_Id := Etype (N);
2235 Comp_Type : Entity_Id;
2236 Selector : Entity_Id;
2237 Comp_Expr : Node_Id;
2240 -- If this is an internal aggregate, the External_Final_List is an
2241 -- expression for the controller record of the enclosing type.
2243 -- If the current aggregate has several controlled components, this
2244 -- expression will appear in several calls to attach to the finali-
2245 -- zation list, and it must not be shared.
2247 Ancestor_Is_Expression : Boolean := False;
2248 Ancestor_Is_Subtype_Mark : Boolean := False;
2250 Init_Typ : Entity_Id := Empty;
2252 Finalization_Done : Boolean := False;
2253 -- True if Generate_Finalization_Actions has already been called; calls
2254 -- after the first do nothing.
2256 function Ancestor_Discriminant_Value (Disc : Entity_Id) return Node_Id;
2257 -- Returns the value that the given discriminant of an ancestor type
2258 -- should receive (in the absence of a conflict with the value provided
2259 -- by an ancestor part of an extension aggregate).
2261 procedure Check_Ancestor_Discriminants (Anc_Typ : Entity_Id);
2262 -- Check that each of the discriminant values defined by the ancestor
2263 -- part of an extension aggregate match the corresponding values
2264 -- provided by either an association of the aggregate or by the
2265 -- constraint imposed by a parent type (RM95-4.3.2(8)).
2267 function Compatible_Int_Bounds
2268 (Agg_Bounds : Node_Id;
2269 Typ_Bounds : Node_Id) return Boolean;
2270 -- Return true if Agg_Bounds are equal or within Typ_Bounds. It is
2271 -- assumed that both bounds are integer ranges.
2273 procedure Generate_Finalization_Actions;
2274 -- Deal with the various controlled type data structure initializations
2275 -- (but only if it hasn't been done already).
2277 function Get_Constraint_Association (T : Entity_Id) return Node_Id;
2278 -- Returns the first discriminant association in the constraint
2279 -- associated with T, if any, otherwise returns Empty.
2281 function Get_Explicit_Discriminant_Value (D : Entity_Id) return Node_Id;
2282 -- If the ancestor part is an unconstrained type and further ancestors
2283 -- do not provide discriminants for it, check aggregate components for
2284 -- values of the discriminants.
2286 procedure Init_Hidden_Discriminants (Typ : Entity_Id; List : List_Id);
2287 -- If Typ is derived, and constrains discriminants of the parent type,
2288 -- these discriminants are not components of the aggregate, and must be
2289 -- initialized. The assignments are appended to List. The same is done
2290 -- if Typ derives fron an already constrained subtype of a discriminated
2293 procedure Init_Stored_Discriminants;
2294 -- If the type is derived and has inherited discriminants, generate
2295 -- explicit assignments for each, using the store constraint of the
2296 -- type. Note that both visible and stored discriminants must be
2297 -- initialized in case the derived type has some renamed and some
2298 -- constrained discriminants.
2300 procedure Init_Visible_Discriminants;
2301 -- If type has discriminants, retrieve their values from aggregate,
2302 -- and generate explicit assignments for each. This does not include
2303 -- discriminants inherited from ancestor, which are handled above.
2304 -- The type of the aggregate is a subtype created ealier using the
2305 -- given values of the discriminant components of the aggregate.
2307 procedure Initialize_Ctrl_Record_Component
2308 (Rec_Comp : Node_Id;
2309 Comp_Typ : Entity_Id;
2310 Init_Expr : Node_Id;
2312 -- Perform the initialization of controlled record component Rec_Comp.
2313 -- Comp_Typ is the component type. Init_Expr is the initialization
2314 -- expression for the record component. Hook-related declarations are
2315 -- inserted prior to aggregate N using Insert_Action. All remaining
2316 -- generated code is added to list Stmts.
2318 procedure Initialize_Record_Component
2319 (Rec_Comp : Node_Id;
2320 Comp_Typ : Entity_Id;
2321 Init_Expr : Node_Id;
2323 -- Perform the initialization of record component Rec_Comp. Comp_Typ
2324 -- is the component type. Init_Expr is the initialization expression
2325 -- of the record component. All generated code is added to list Stmts.
2327 function Is_Int_Range_Bounds (Bounds : Node_Id) return Boolean;
2328 -- Check whether Bounds is a range node and its lower and higher bounds
2329 -- are integers literals.
2331 function Replace_Type (Expr : Node_Id) return Traverse_Result;
2332 -- If the aggregate contains a self-reference, traverse each expression
2333 -- to replace a possible self-reference with a reference to the proper
2334 -- component of the target of the assignment.
2336 function Rewrite_Discriminant (Expr : Node_Id) return Traverse_Result;
2337 -- If default expression of a component mentions a discriminant of the
2338 -- type, it must be rewritten as the discriminant of the target object.
2340 ---------------------------------
2341 -- Ancestor_Discriminant_Value --
2342 ---------------------------------
2344 function Ancestor_Discriminant_Value (Disc : Entity_Id) return Node_Id is
2346 Assoc_Elmt : Elmt_Id;
2347 Aggr_Comp : Entity_Id;
2348 Corresp_Disc : Entity_Id;
2349 Current_Typ : Entity_Id := Base_Type (Typ);
2350 Parent_Typ : Entity_Id;
2351 Parent_Disc : Entity_Id;
2352 Save_Assoc : Node_Id := Empty;
2355 -- First check any discriminant associations to see if any of them
2356 -- provide a value for the discriminant.
2358 if Present (Discriminant_Specifications (Parent (Current_Typ))) then
2359 Assoc := First (Component_Associations (N));
2360 while Present (Assoc) loop
2361 Aggr_Comp := Entity (First (Choices (Assoc)));
2363 if Ekind (Aggr_Comp) = E_Discriminant then
2364 Save_Assoc := Expression (Assoc);
2366 Corresp_Disc := Corresponding_Discriminant (Aggr_Comp);
2367 while Present (Corresp_Disc) loop
2369 -- If found a corresponding discriminant then return the
2370 -- value given in the aggregate. (Note: this is not
2371 -- correct in the presence of side effects. ???)
2373 if Disc = Corresp_Disc then
2374 return Duplicate_Subexpr (Expression (Assoc));
2377 Corresp_Disc := Corresponding_Discriminant (Corresp_Disc);
2385 -- No match found in aggregate, so chain up parent types to find
2386 -- a constraint that defines the value of the discriminant.
2388 Parent_Typ := Etype (Current_Typ);
2389 while Current_Typ /= Parent_Typ loop
2390 if Has_Discriminants (Parent_Typ)
2391 and then not Has_Unknown_Discriminants (Parent_Typ)
2393 Parent_Disc := First_Discriminant (Parent_Typ);
2395 -- We either get the association from the subtype indication
2396 -- of the type definition itself, or from the discriminant
2397 -- constraint associated with the type entity (which is
2398 -- preferable, but it's not always present ???)
2400 if Is_Empty_Elmt_List (Discriminant_Constraint (Current_Typ))
2402 Assoc := Get_Constraint_Association (Current_Typ);
2403 Assoc_Elmt := No_Elmt;
2406 First_Elmt (Discriminant_Constraint (Current_Typ));
2407 Assoc := Node (Assoc_Elmt);
2410 -- Traverse the discriminants of the parent type looking
2411 -- for one that corresponds.
2413 while Present (Parent_Disc) and then Present (Assoc) loop
2414 Corresp_Disc := Parent_Disc;
2415 while Present (Corresp_Disc)
2416 and then Disc /= Corresp_Disc
2418 Corresp_Disc := Corresponding_Discriminant (Corresp_Disc);
2421 if Disc = Corresp_Disc then
2422 if Nkind (Assoc) = N_Discriminant_Association then
2423 Assoc := Expression (Assoc);
2426 -- If the located association directly denotes
2427 -- a discriminant, then use the value of a saved
2428 -- association of the aggregate. This is an approach
2429 -- used to handle certain cases involving multiple
2430 -- discriminants mapped to a single discriminant of
2431 -- a descendant. It's not clear how to locate the
2432 -- appropriate discriminant value for such cases. ???
2434 if Is_Entity_Name (Assoc)
2435 and then Ekind (Entity (Assoc)) = E_Discriminant
2437 Assoc := Save_Assoc;
2440 return Duplicate_Subexpr (Assoc);
2443 Next_Discriminant (Parent_Disc);
2445 if No (Assoc_Elmt) then
2449 Next_Elmt (Assoc_Elmt);
2451 if Present (Assoc_Elmt) then
2452 Assoc := Node (Assoc_Elmt);
2460 Current_Typ := Parent_Typ;
2461 Parent_Typ := Etype (Current_Typ);
2464 -- In some cases there's no ancestor value to locate (such as
2465 -- when an ancestor part given by an expression defines the
2466 -- discriminant value).
2469 end Ancestor_Discriminant_Value;
2471 ----------------------------------
2472 -- Check_Ancestor_Discriminants --
2473 ----------------------------------
2475 procedure Check_Ancestor_Discriminants (Anc_Typ : Entity_Id) is
2477 Disc_Value : Node_Id;
2481 Discr := First_Discriminant (Base_Type (Anc_Typ));
2482 while Present (Discr) loop
2483 Disc_Value := Ancestor_Discriminant_Value (Discr);
2485 if Present (Disc_Value) then
2486 Cond := Make_Op_Ne (Loc,
2488 Make_Selected_Component (Loc,
2489 Prefix => New_Copy_Tree (Target),
2490 Selector_Name => New_Occurrence_Of (Discr, Loc)),
2491 Right_Opnd => Disc_Value);
2494 Make_Raise_Constraint_Error (Loc,
2496 Reason => CE_Discriminant_Check_Failed));
2499 Next_Discriminant (Discr);
2501 end Check_Ancestor_Discriminants;
2503 ---------------------------
2504 -- Compatible_Int_Bounds --
2505 ---------------------------
2507 function Compatible_Int_Bounds
2508 (Agg_Bounds : Node_Id;
2509 Typ_Bounds : Node_Id) return Boolean
2511 Agg_Lo : constant Uint := Intval (Low_Bound (Agg_Bounds));
2512 Agg_Hi : constant Uint := Intval (High_Bound (Agg_Bounds));
2513 Typ_Lo : constant Uint := Intval (Low_Bound (Typ_Bounds));
2514 Typ_Hi : constant Uint := Intval (High_Bound (Typ_Bounds));
2516 return Typ_Lo <= Agg_Lo and then Agg_Hi <= Typ_Hi;
2517 end Compatible_Int_Bounds;
2519 -----------------------------------
2520 -- Generate_Finalization_Actions --
2521 -----------------------------------
2523 procedure Generate_Finalization_Actions is
2525 -- Do the work only the first time this is called
2527 if Finalization_Done then
2531 Finalization_Done := True;
2533 -- Determine the external finalization list. It is either the
2534 -- finalization list of the outer scope or the one coming from an
2535 -- outer aggregate. When the target is not a temporary, the proper
2536 -- scope is the scope of the target rather than the potentially
2537 -- transient current scope.
2539 if Is_Controlled (Typ) and then Ancestor_Is_Subtype_Mark then
2540 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
2541 Set_Assignment_OK (Ref);
2544 Make_Procedure_Call_Statement (Loc,
2547 (Find_Prim_Op (Init_Typ, Name_Initialize), Loc),
2548 Parameter_Associations => New_List (New_Copy_Tree (Ref))));
2550 end Generate_Finalization_Actions;
2552 --------------------------------
2553 -- Get_Constraint_Association --
2554 --------------------------------
2556 function Get_Constraint_Association (T : Entity_Id) return Node_Id is
2563 -- If type is private, get constraint from full view. This was
2564 -- previously done in an instance context, but is needed whenever
2565 -- the ancestor part has a discriminant, possibly inherited through
2566 -- multiple derivations.
2568 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
2569 Typ := Full_View (Typ);
2572 Indic := Subtype_Indication (Type_Definition (Parent (Typ)));
2574 -- Verify that the subtype indication carries a constraint
2576 if Nkind (Indic) = N_Subtype_Indication
2577 and then Present (Constraint (Indic))
2579 return First (Constraints (Constraint (Indic)));
2583 end Get_Constraint_Association;
2585 -------------------------------------
2586 -- Get_Explicit_Discriminant_Value --
2587 -------------------------------------
2589 function Get_Explicit_Discriminant_Value
2590 (D : Entity_Id) return Node_Id
2597 -- The aggregate has been normalized and all associations have a
2600 Assoc := First (Component_Associations (N));
2601 while Present (Assoc) loop
2602 Choice := First (Choices (Assoc));
2604 if Chars (Choice) = Chars (D) then
2605 Val := Expression (Assoc);
2614 end Get_Explicit_Discriminant_Value;
2616 -------------------------------
2617 -- Init_Hidden_Discriminants --
2618 -------------------------------
2620 procedure Init_Hidden_Discriminants (Typ : Entity_Id; List : List_Id) is
2621 function Is_Completely_Hidden_Discriminant
2622 (Discr : Entity_Id) return Boolean;
2623 -- Determine whether Discr is a completely hidden discriminant of
2626 ---------------------------------------
2627 -- Is_Completely_Hidden_Discriminant --
2628 ---------------------------------------
2630 function Is_Completely_Hidden_Discriminant
2631 (Discr : Entity_Id) return Boolean
2636 -- Use First/Next_Entity as First/Next_Discriminant do not yield
2637 -- completely hidden discriminants.
2639 Item := First_Entity (Typ);
2640 while Present (Item) loop
2641 if Ekind (Item) = E_Discriminant
2642 and then Is_Completely_Hidden (Item)
2643 and then Chars (Original_Record_Component (Item)) =
2653 end Is_Completely_Hidden_Discriminant;
2657 Base_Typ : Entity_Id;
2659 Discr_Constr : Elmt_Id;
2660 Discr_Init : Node_Id;
2661 Discr_Val : Node_Id;
2662 In_Aggr_Type : Boolean;
2663 Par_Typ : Entity_Id;
2665 -- Start of processing for Init_Hidden_Discriminants
2668 -- The constraints on the hidden discriminants, if present, are kept
2669 -- in the Stored_Constraint list of the type itself, or in that of
2670 -- the base type. If not in the constraints of the aggregate itself,
2671 -- we examine ancestors to find discriminants that are not renamed
2672 -- by other discriminants but constrained explicitly.
2674 In_Aggr_Type := True;
2676 Base_Typ := Base_Type (Typ);
2677 while Is_Derived_Type (Base_Typ)
2679 (Present (Stored_Constraint (Base_Typ))
2681 (In_Aggr_Type and then Present (Stored_Constraint (Typ))))
2683 Par_Typ := Etype (Base_Typ);
2685 if not Has_Discriminants (Par_Typ) then
2689 Discr := First_Discriminant (Par_Typ);
2691 -- We know that one of the stored-constraint lists is present
2693 if Present (Stored_Constraint (Base_Typ)) then
2694 Discr_Constr := First_Elmt (Stored_Constraint (Base_Typ));
2696 -- For private extension, stored constraint may be on full view
2698 elsif Is_Private_Type (Base_Typ)
2699 and then Present (Full_View (Base_Typ))
2700 and then Present (Stored_Constraint (Full_View (Base_Typ)))
2703 First_Elmt (Stored_Constraint (Full_View (Base_Typ)));
2705 -- Otherwise, no discriminant to process
2708 Discr_Constr := No_Elmt;
2711 while Present (Discr) and then Present (Discr_Constr) loop
2712 Discr_Val := Node (Discr_Constr);
2714 -- The parent discriminant is renamed in the derived type,
2715 -- nothing to initialize.
2717 -- type Deriv_Typ (Discr : ...)
2718 -- is new Parent_Typ (Discr => Discr);
2720 if Is_Entity_Name (Discr_Val)
2721 and then Ekind (Entity (Discr_Val)) = E_Discriminant
2725 -- When the parent discriminant is constrained at the type
2726 -- extension level, it does not appear in the derived type.
2728 -- type Deriv_Typ (Discr : ...)
2729 -- is new Parent_Typ (Discr => Discr,
2730 -- Hidden_Discr => Expression);
2732 elsif Is_Completely_Hidden_Discriminant (Discr) then
2735 -- Otherwise initialize the discriminant
2739 Make_OK_Assignment_Statement (Loc,
2741 Make_Selected_Component (Loc,
2742 Prefix => New_Copy_Tree (Target),
2743 Selector_Name => New_Occurrence_Of (Discr, Loc)),
2744 Expression => New_Copy_Tree (Discr_Val));
2746 Append_To (List, Discr_Init);
2749 Next_Elmt (Discr_Constr);
2750 Next_Discriminant (Discr);
2753 In_Aggr_Type := False;
2754 Base_Typ := Base_Type (Par_Typ);
2756 end Init_Hidden_Discriminants;
2758 --------------------------------
2759 -- Init_Visible_Discriminants --
2760 --------------------------------
2762 procedure Init_Visible_Discriminants is
2763 Discriminant : Entity_Id;
2764 Discriminant_Value : Node_Id;
2767 Discriminant := First_Discriminant (Typ);
2768 while Present (Discriminant) loop
2770 Make_Selected_Component (Loc,
2771 Prefix => New_Copy_Tree (Target),
2772 Selector_Name => New_Occurrence_Of (Discriminant, Loc));
2774 Discriminant_Value :=
2775 Get_Discriminant_Value
2776 (Discriminant, Typ, Discriminant_Constraint (N_Typ));
2779 Make_OK_Assignment_Statement (Loc,
2781 Expression => New_Copy_Tree (Discriminant_Value));
2783 Append_To (L, Instr);
2785 Next_Discriminant (Discriminant);
2787 end Init_Visible_Discriminants;
2789 -------------------------------
2790 -- Init_Stored_Discriminants --
2791 -------------------------------
2793 procedure Init_Stored_Discriminants is
2794 Discriminant : Entity_Id;
2795 Discriminant_Value : Node_Id;
2798 Discriminant := First_Stored_Discriminant (Typ);
2799 while Present (Discriminant) loop
2801 Make_Selected_Component (Loc,
2802 Prefix => New_Copy_Tree (Target),
2803 Selector_Name => New_Occurrence_Of (Discriminant, Loc));
2805 Discriminant_Value :=
2806 Get_Discriminant_Value
2807 (Discriminant, N_Typ, Discriminant_Constraint (N_Typ));
2810 Make_OK_Assignment_Statement (Loc,
2812 Expression => New_Copy_Tree (Discriminant_Value));
2814 Append_To (L, Instr);
2816 Next_Stored_Discriminant (Discriminant);
2818 end Init_Stored_Discriminants;
2820 --------------------------------------
2821 -- Initialize_Ctrl_Record_Component --
2822 --------------------------------------
2824 procedure Initialize_Ctrl_Record_Component
2825 (Rec_Comp : Node_Id;
2826 Comp_Typ : Entity_Id;
2827 Init_Expr : Node_Id;
2831 Hook_Clear : Node_Id;
2833 In_Place_Expansion : Boolean;
2834 -- Flag set when a nonlimited controlled function call requires
2835 -- in-place expansion.
2838 -- Perform a preliminary analysis and resolution to determine what
2839 -- the initialization expression denotes. Unanalyzed function calls
2840 -- may appear as identifiers or indexed components.
2842 if Nkind_In (Init_Expr, N_Function_Call,
2844 N_Indexed_Component)
2845 and then not Analyzed (Init_Expr)
2847 Preanalyze_And_Resolve (Init_Expr, Comp_Typ);
2850 In_Place_Expansion :=
2851 Nkind (Init_Expr) = N_Function_Call
2852 and then not Is_Build_In_Place_Result_Type (Comp_Typ);
2854 -- The initialization expression is a controlled function call.
2855 -- Perform in-place removal of side effects to avoid creating a
2858 -- This in-place expansion is not performed for limited transient
2859 -- objects because the initialization is already done in place.
2861 if In_Place_Expansion then
2863 -- Suppress the removal of side effects by general analysis
2864 -- because this behavior is emulated here. This avoids the
2865 -- generation of a transient scope, which leads to out-of-order
2866 -- adjustment and finalization.
2868 Set_No_Side_Effect_Removal (Init_Expr);
2870 -- Install all hook-related declarations and prepare the clean up
2871 -- statements. The generated code follows the initialization order
2872 -- of individual components and discriminants, rather than being
2873 -- inserted prior to the aggregate. This ensures that a transient
2874 -- component which mentions a discriminant has proper visibility
2875 -- of the discriminant.
2877 Process_Transient_Component
2879 Comp_Typ => Comp_Typ,
2880 Init_Expr => Init_Expr,
2881 Fin_Call => Fin_Call,
2882 Hook_Clear => Hook_Clear,
2886 -- Use the noncontrolled component initialization circuitry to
2887 -- assign the result of the function call to the record component.
2888 -- This also performs tag adjustment and [deep] adjustment of the
2889 -- record component.
2891 Initialize_Record_Component
2892 (Rec_Comp => Rec_Comp,
2893 Comp_Typ => Comp_Typ,
2894 Init_Expr => Init_Expr,
2897 -- At this point the record component is fully initialized. Complete
2898 -- the processing of the controlled record component by finalizing
2899 -- the transient function result.
2901 if In_Place_Expansion then
2902 Process_Transient_Component_Completion
2905 Fin_Call => Fin_Call,
2906 Hook_Clear => Hook_Clear,
2909 end Initialize_Ctrl_Record_Component;
2911 ---------------------------------
2912 -- Initialize_Record_Component --
2913 ---------------------------------
2915 procedure Initialize_Record_Component
2916 (Rec_Comp : Node_Id;
2917 Comp_Typ : Entity_Id;
2918 Init_Expr : Node_Id;
2921 Exceptions_OK : constant Boolean :=
2922 not Restriction_Active (No_Exception_Propagation);
2924 Finalization_OK : constant Boolean := Needs_Finalization (Comp_Typ);
2926 Full_Typ : constant Entity_Id := Underlying_Type (Comp_Typ);
2928 Blk_Stmts : List_Id;
2929 Init_Stmt : Node_Id;
2932 -- Protect the initialization statements from aborts. Generate:
2936 if Finalization_OK and Abort_Allowed then
2937 if Exceptions_OK then
2938 Blk_Stmts := New_List;
2943 Append_To (Blk_Stmts, Build_Runtime_Call (Loc, RE_Abort_Defer));
2945 -- Otherwise aborts are not allowed. All generated code is added
2946 -- directly to the input list.
2952 -- Initialize the record component. Generate:
2954 -- Rec_Comp := Init_Expr;
2956 -- Note that the initialization expression is NOT replicated because
2957 -- only a single component may be initialized by it.
2960 Make_OK_Assignment_Statement (Loc,
2961 Name => New_Copy_Tree (Rec_Comp),
2962 Expression => Init_Expr);
2963 Set_No_Ctrl_Actions (Init_Stmt);
2965 Append_To (Blk_Stmts, Init_Stmt);
2967 -- Adjust the tag due to a possible view conversion. Generate:
2969 -- Rec_Comp._tag := Full_TypeP;
2971 if Tagged_Type_Expansion and then Is_Tagged_Type (Comp_Typ) then
2972 Append_To (Blk_Stmts,
2973 Make_OK_Assignment_Statement (Loc,
2975 Make_Selected_Component (Loc,
2976 Prefix => New_Copy_Tree (Rec_Comp),
2979 (First_Tag_Component (Full_Typ), Loc)),
2982 Unchecked_Convert_To (RTE (RE_Tag),
2984 (Node (First_Elmt (Access_Disp_Table (Full_Typ))),
2988 -- Adjust the component. Generate:
2990 -- [Deep_]Adjust (Rec_Comp);
2993 and then not Is_Limited_Type (Comp_Typ)
2994 and then not Is_Build_In_Place_Function_Call (Init_Expr)
2998 (Obj_Ref => New_Copy_Tree (Rec_Comp),
3001 -- Guard against a missing [Deep_]Adjust when the component type
3002 -- was not properly frozen.
3004 if Present (Adj_Call) then
3005 Append_To (Blk_Stmts, Adj_Call);
3009 -- Complete the protection of the initialization statements
3011 if Finalization_OK and Abort_Allowed then
3013 -- Wrap the initialization statements in a block to catch a
3014 -- potential exception. Generate:
3018 -- Rec_Comp := Init_Expr;
3019 -- Rec_Comp._tag := Full_TypP;
3020 -- [Deep_]Adjust (Rec_Comp);
3022 -- Abort_Undefer_Direct;
3025 if Exceptions_OK then
3027 Build_Abort_Undefer_Block (Loc,
3031 -- Otherwise exceptions are not propagated. Generate:
3034 -- Rec_Comp := Init_Expr;
3035 -- Rec_Comp._tag := Full_TypP;
3036 -- [Deep_]Adjust (Rec_Comp);
3040 Append_To (Blk_Stmts,
3041 Build_Runtime_Call (Loc, RE_Abort_Undefer));
3044 end Initialize_Record_Component;
3046 -------------------------
3047 -- Is_Int_Range_Bounds --
3048 -------------------------
3050 function Is_Int_Range_Bounds (Bounds : Node_Id) return Boolean is
3052 return Nkind (Bounds) = N_Range
3053 and then Nkind (Low_Bound (Bounds)) = N_Integer_Literal
3054 and then Nkind (High_Bound (Bounds)) = N_Integer_Literal;
3055 end Is_Int_Range_Bounds;
3061 function Replace_Type (Expr : Node_Id) return Traverse_Result is
3063 -- Note regarding the Root_Type test below: Aggregate components for
3064 -- self-referential types include attribute references to the current
3065 -- instance, of the form: Typ'access, etc.. These references are
3066 -- rewritten as references to the target of the aggregate: the
3067 -- left-hand side of an assignment, the entity in a declaration,
3068 -- or a temporary. Without this test, we would improperly extended
3069 -- this rewriting to attribute references whose prefix was not the
3070 -- type of the aggregate.
3072 if Nkind (Expr) = N_Attribute_Reference
3073 and then Is_Entity_Name (Prefix (Expr))
3074 and then Is_Type (Entity (Prefix (Expr)))
3075 and then Root_Type (Etype (N)) = Root_Type (Entity (Prefix (Expr)))
3077 if Is_Entity_Name (Lhs) then
3078 Rewrite (Prefix (Expr), New_Occurrence_Of (Entity (Lhs), Loc));
3082 Make_Attribute_Reference (Loc,
3083 Attribute_Name => Name_Unrestricted_Access,
3084 Prefix => New_Copy_Tree (Lhs)));
3085 Set_Analyzed (Parent (Expr), False);
3092 --------------------------
3093 -- Rewrite_Discriminant --
3094 --------------------------
3096 function Rewrite_Discriminant (Expr : Node_Id) return Traverse_Result is
3098 if Is_Entity_Name (Expr)
3099 and then Present (Entity (Expr))
3100 and then Ekind (Entity (Expr)) = E_In_Parameter
3101 and then Present (Discriminal_Link (Entity (Expr)))
3102 and then Scope (Discriminal_Link (Entity (Expr))) =
3103 Base_Type (Etype (N))
3106 Make_Selected_Component (Loc,
3107 Prefix => New_Copy_Tree (Lhs),
3108 Selector_Name => Make_Identifier (Loc, Chars (Expr))));
3110 -- The generated code will be reanalyzed, but if the reference
3111 -- to the discriminant appears within an already analyzed
3112 -- expression (e.g. a conditional) we must set its proper entity
3113 -- now. Context is an initialization procedure.
3119 end Rewrite_Discriminant;
3121 procedure Replace_Discriminants is
3122 new Traverse_Proc (Rewrite_Discriminant);
3124 procedure Replace_Self_Reference is
3125 new Traverse_Proc (Replace_Type);
3127 -- Start of processing for Build_Record_Aggr_Code
3130 if Has_Self_Reference (N) then
3131 Replace_Self_Reference (N);
3134 -- If the target of the aggregate is class-wide, we must convert it
3135 -- to the actual type of the aggregate, so that the proper components
3136 -- are visible. We know already that the types are compatible.
3138 if Present (Etype (Lhs))
3139 and then Is_Class_Wide_Type (Etype (Lhs))
3141 Target := Unchecked_Convert_To (Typ, Lhs);
3146 -- Deal with the ancestor part of extension aggregates or with the
3147 -- discriminants of the root type.
3149 if Nkind (N) = N_Extension_Aggregate then
3151 Ancestor : constant Node_Id := Ancestor_Part (N);
3156 -- If the ancestor part is a subtype mark "T", we generate
3158 -- init-proc (T (tmp)); if T is constrained and
3159 -- init-proc (S (tmp)); where S applies an appropriate
3160 -- constraint if T is unconstrained
3162 if Is_Entity_Name (Ancestor)
3163 and then Is_Type (Entity (Ancestor))
3165 Ancestor_Is_Subtype_Mark := True;
3167 if Is_Constrained (Entity (Ancestor)) then
3168 Init_Typ := Entity (Ancestor);
3170 -- For an ancestor part given by an unconstrained type mark,
3171 -- create a subtype constrained by appropriate corresponding
3172 -- discriminant values coming from either associations of the
3173 -- aggregate or a constraint on a parent type. The subtype will
3174 -- be used to generate the correct default value for the
3177 elsif Has_Discriminants (Entity (Ancestor)) then
3179 Anc_Typ : constant Entity_Id := Entity (Ancestor);
3180 Anc_Constr : constant List_Id := New_List;
3181 Discrim : Entity_Id;
3182 Disc_Value : Node_Id;
3183 New_Indic : Node_Id;
3184 Subt_Decl : Node_Id;
3187 Discrim := First_Discriminant (Anc_Typ);
3188 while Present (Discrim) loop
3189 Disc_Value := Ancestor_Discriminant_Value (Discrim);
3191 -- If no usable discriminant in ancestors, check
3192 -- whether aggregate has an explicit value for it.
3194 if No (Disc_Value) then
3196 Get_Explicit_Discriminant_Value (Discrim);
3199 Append_To (Anc_Constr, Disc_Value);
3200 Next_Discriminant (Discrim);
3204 Make_Subtype_Indication (Loc,
3205 Subtype_Mark => New_Occurrence_Of (Anc_Typ, Loc),
3207 Make_Index_Or_Discriminant_Constraint (Loc,
3208 Constraints => Anc_Constr));
3210 Init_Typ := Create_Itype (Ekind (Anc_Typ), N);
3213 Make_Subtype_Declaration (Loc,
3214 Defining_Identifier => Init_Typ,
3215 Subtype_Indication => New_Indic);
3217 -- Itypes must be analyzed with checks off Declaration
3218 -- must have a parent for proper handling of subsidiary
3221 Set_Parent (Subt_Decl, N);
3222 Analyze (Subt_Decl, Suppress => All_Checks);
3226 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
3227 Set_Assignment_OK (Ref);
3229 if not Is_Interface (Init_Typ) then
3231 Build_Initialization_Call (Loc,
3234 In_Init_Proc => Within_Init_Proc,
3235 With_Default_Init => Has_Default_Init_Comps (N)
3237 Has_Task (Base_Type (Init_Typ))));
3239 if Is_Constrained (Entity (Ancestor))
3240 and then Has_Discriminants (Entity (Ancestor))
3242 Check_Ancestor_Discriminants (Entity (Ancestor));
3246 -- Handle calls to C++ constructors
3248 elsif Is_CPP_Constructor_Call (Ancestor) then
3249 Init_Typ := Etype (Ancestor);
3250 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
3251 Set_Assignment_OK (Ref);
3254 Build_Initialization_Call (Loc,
3257 In_Init_Proc => Within_Init_Proc,
3258 With_Default_Init => Has_Default_Init_Comps (N),
3259 Constructor_Ref => Ancestor));
3261 -- Ada 2005 (AI-287): If the ancestor part is an aggregate of
3262 -- limited type, a recursive call expands the ancestor. Note that
3263 -- in the limited case, the ancestor part must be either a
3264 -- function call (possibly qualified) or aggregate (definitely
3267 elsif Is_Limited_Type (Etype (Ancestor))
3268 and then Nkind_In (Unqualify (Ancestor), N_Aggregate,
3269 N_Extension_Aggregate)
3271 Ancestor_Is_Expression := True;
3273 -- Set up finalization data for enclosing record, because
3274 -- controlled subcomponents of the ancestor part will be
3277 Generate_Finalization_Actions;
3280 Build_Record_Aggr_Code
3281 (N => Unqualify (Ancestor),
3282 Typ => Etype (Unqualify (Ancestor)),
3285 -- If the ancestor part is an expression "E", we generate
3289 -- In Ada 2005, this includes the case of a (possibly qualified)
3290 -- limited function call. The assignment will turn into a
3291 -- build-in-place function call (for further details, see
3292 -- Make_Build_In_Place_Call_In_Assignment).
3295 Ancestor_Is_Expression := True;
3296 Init_Typ := Etype (Ancestor);
3298 -- If the ancestor part is an aggregate, force its full
3299 -- expansion, which was delayed.
3301 if Nkind_In (Unqualify (Ancestor), N_Aggregate,
3302 N_Extension_Aggregate)
3304 Set_Analyzed (Ancestor, False);
3305 Set_Analyzed (Expression (Ancestor), False);
3308 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
3309 Set_Assignment_OK (Ref);
3311 -- Make the assignment without usual controlled actions, since
3312 -- we only want to Adjust afterwards, but not to Finalize
3313 -- beforehand. Add manual Adjust when necessary.
3315 Assign := New_List (
3316 Make_OK_Assignment_Statement (Loc,
3318 Expression => Ancestor));
3319 Set_No_Ctrl_Actions (First (Assign));
3321 -- Assign the tag now to make sure that the dispatching call in
3322 -- the subsequent deep_adjust works properly (unless
3323 -- Tagged_Type_Expansion where tags are implicit).
3325 if Tagged_Type_Expansion then
3327 Make_OK_Assignment_Statement (Loc,
3329 Make_Selected_Component (Loc,
3330 Prefix => New_Copy_Tree (Target),
3333 (First_Tag_Component (Base_Type (Typ)), Loc)),
3336 Unchecked_Convert_To (RTE (RE_Tag),
3339 (Access_Disp_Table (Base_Type (Typ)))),
3342 Set_Assignment_OK (Name (Instr));
3343 Append_To (Assign, Instr);
3345 -- Ada 2005 (AI-251): If tagged type has progenitors we must
3346 -- also initialize tags of the secondary dispatch tables.
3348 if Has_Interfaces (Base_Type (Typ)) then
3350 (Typ => Base_Type (Typ),
3352 Stmts_List => Assign,
3353 Init_Tags_List => Assign);
3357 -- Call Adjust manually
3359 if Needs_Finalization (Etype (Ancestor))
3360 and then not Is_Limited_Type (Etype (Ancestor))
3361 and then not Is_Build_In_Place_Function_Call (Ancestor)
3365 (Obj_Ref => New_Copy_Tree (Ref),
3366 Typ => Etype (Ancestor));
3368 -- Guard against a missing [Deep_]Adjust when the ancestor
3369 -- type was not properly frozen.
3371 if Present (Adj_Call) then
3372 Append_To (Assign, Adj_Call);
3377 Make_Unsuppress_Block (Loc, Name_Discriminant_Check, Assign));
3379 if Has_Discriminants (Init_Typ) then
3380 Check_Ancestor_Discriminants (Init_Typ);
3384 pragma Assert (Nkind (N) = N_Extension_Aggregate);
3386 (not (Ancestor_Is_Expression and Ancestor_Is_Subtype_Mark));
3389 -- Generate assignments of hidden discriminants. If the base type is
3390 -- an unchecked union, the discriminants are unknown to the back-end
3391 -- and absent from a value of the type, so assignments for them are
3394 if Has_Discriminants (Typ)
3395 and then not Is_Unchecked_Union (Base_Type (Typ))
3397 Init_Hidden_Discriminants (Typ, L);
3400 -- Normal case (not an extension aggregate)
3403 -- Generate the discriminant expressions, component by component.
3404 -- If the base type is an unchecked union, the discriminants are
3405 -- unknown to the back-end and absent from a value of the type, so
3406 -- assignments for them are not emitted.
3408 if Has_Discriminants (Typ)
3409 and then not Is_Unchecked_Union (Base_Type (Typ))
3411 Init_Hidden_Discriminants (Typ, L);
3413 -- Generate discriminant init values for the visible discriminants
3415 Init_Visible_Discriminants;
3417 if Is_Derived_Type (N_Typ) then
3418 Init_Stored_Discriminants;
3423 -- For CPP types we generate an implicit call to the C++ default
3424 -- constructor to ensure the proper initialization of the _Tag
3427 if Is_CPP_Class (Root_Type (Typ)) and then CPP_Num_Prims (Typ) > 0 then
3428 Invoke_Constructor : declare
3429 CPP_Parent : constant Entity_Id := Enclosing_CPP_Parent (Typ);
3431 procedure Invoke_IC_Proc (T : Entity_Id);
3432 -- Recursive routine used to climb to parents. Required because
3433 -- parents must be initialized before descendants to ensure
3434 -- propagation of inherited C++ slots.
3436 --------------------
3437 -- Invoke_IC_Proc --
3438 --------------------
3440 procedure Invoke_IC_Proc (T : Entity_Id) is
3442 -- Avoid generating extra calls. Initialization required
3443 -- only for types defined from the level of derivation of
3444 -- type of the constructor and the type of the aggregate.
3446 if T = CPP_Parent then
3450 Invoke_IC_Proc (Etype (T));
3452 -- Generate call to the IC routine
3454 if Present (CPP_Init_Proc (T)) then
3456 Make_Procedure_Call_Statement (Loc,
3457 Name => New_Occurrence_Of (CPP_Init_Proc (T), Loc)));
3461 -- Start of processing for Invoke_Constructor
3464 -- Implicit invocation of the C++ constructor
3466 if Nkind (N) = N_Aggregate then
3468 Make_Procedure_Call_Statement (Loc,
3470 New_Occurrence_Of (Base_Init_Proc (CPP_Parent), Loc),
3471 Parameter_Associations => New_List (
3472 Unchecked_Convert_To (CPP_Parent,
3473 New_Copy_Tree (Lhs)))));
3476 Invoke_IC_Proc (Typ);
3477 end Invoke_Constructor;
3480 -- Generate the assignments, component by component
3482 -- tmp.comp1 := Expr1_From_Aggr;
3483 -- tmp.comp2 := Expr2_From_Aggr;
3486 Comp := First (Component_Associations (N));
3487 while Present (Comp) loop
3488 Selector := Entity (First (Choices (Comp)));
3492 if Is_CPP_Constructor_Call (Expression (Comp)) then
3494 Build_Initialization_Call (Loc,
3496 Make_Selected_Component (Loc,
3497 Prefix => New_Copy_Tree (Target),
3498 Selector_Name => New_Occurrence_Of (Selector, Loc)),
3499 Typ => Etype (Selector),
3501 With_Default_Init => True,
3502 Constructor_Ref => Expression (Comp)));
3504 -- Ada 2005 (AI-287): For each default-initialized component generate
3505 -- a call to the corresponding IP subprogram if available.
3507 elsif Box_Present (Comp)
3508 and then Has_Non_Null_Base_Init_Proc (Etype (Selector))
3510 if Ekind (Selector) /= E_Discriminant then
3511 Generate_Finalization_Actions;
3514 -- Ada 2005 (AI-287): If the component type has tasks then
3515 -- generate the activation chain and master entities (except
3516 -- in case of an allocator because in that case these entities
3517 -- are generated by Build_Task_Allocate_Block_With_Init_Stmts).
3520 Ctype : constant Entity_Id := Etype (Selector);
3521 Inside_Allocator : Boolean := False;
3522 P : Node_Id := Parent (N);
3525 if Is_Task_Type (Ctype) or else Has_Task (Ctype) then
3526 while Present (P) loop
3527 if Nkind (P) = N_Allocator then
3528 Inside_Allocator := True;
3535 if not Inside_Init_Proc and not Inside_Allocator then
3536 Build_Activation_Chain_Entity (N);
3542 Build_Initialization_Call (Loc,
3543 Id_Ref => Make_Selected_Component (Loc,
3544 Prefix => New_Copy_Tree (Target),
3546 New_Occurrence_Of (Selector, Loc)),
3547 Typ => Etype (Selector),
3549 With_Default_Init => True));
3551 -- Prepare for component assignment
3553 elsif Ekind (Selector) /= E_Discriminant
3554 or else Nkind (N) = N_Extension_Aggregate
3556 -- All the discriminants have now been assigned
3558 -- This is now a good moment to initialize and attach all the
3559 -- controllers. Their position may depend on the discriminants.
3561 if Ekind (Selector) /= E_Discriminant then
3562 Generate_Finalization_Actions;
3565 Comp_Type := Underlying_Type (Etype (Selector));
3567 Make_Selected_Component (Loc,
3568 Prefix => New_Copy_Tree (Target),
3569 Selector_Name => New_Occurrence_Of (Selector, Loc));
3571 if Nkind (Expression (Comp)) = N_Qualified_Expression then
3572 Expr_Q := Expression (Expression (Comp));
3574 Expr_Q := Expression (Comp);
3577 -- Now either create the assignment or generate the code for the
3578 -- inner aggregate top-down.
3580 if Is_Delayed_Aggregate (Expr_Q) then
3582 -- We have the following case of aggregate nesting inside
3583 -- an object declaration:
3585 -- type Arr_Typ is array (Integer range <>) of ...;
3587 -- type Rec_Typ (...) is record
3588 -- Obj_Arr_Typ : Arr_Typ (A .. B);
3591 -- Obj_Rec_Typ : Rec_Typ := (...,
3592 -- Obj_Arr_Typ => (X => (...), Y => (...)));
3594 -- The length of the ranges of the aggregate and Obj_Add_Typ
3595 -- are equal (B - A = Y - X), but they do not coincide (X /=
3596 -- A and B /= Y). This case requires array sliding which is
3597 -- performed in the following manner:
3599 -- subtype Arr_Sub is Arr_Typ (X .. Y);
3601 -- Temp (X) := (...);
3603 -- Temp (Y) := (...);
3604 -- Obj_Rec_Typ.Obj_Arr_Typ := Temp;
3606 if Ekind (Comp_Type) = E_Array_Subtype
3607 and then Is_Int_Range_Bounds (Aggregate_Bounds (Expr_Q))
3608 and then Is_Int_Range_Bounds (First_Index (Comp_Type))
3610 Compatible_Int_Bounds
3611 (Agg_Bounds => Aggregate_Bounds (Expr_Q),
3612 Typ_Bounds => First_Index (Comp_Type))
3614 -- Create the array subtype with bounds equal to those of
3615 -- the corresponding aggregate.
3618 SubE : constant Entity_Id := Make_Temporary (Loc, 'T');
3620 SubD : constant Node_Id :=
3621 Make_Subtype_Declaration (Loc,
3622 Defining_Identifier => SubE,
3623 Subtype_Indication =>
3624 Make_Subtype_Indication (Loc,
3626 New_Occurrence_Of (Etype (Comp_Type), Loc),
3628 Make_Index_Or_Discriminant_Constraint
3630 Constraints => New_List (
3632 (Aggregate_Bounds (Expr_Q))))));
3634 -- Create a temporary array of the above subtype which
3635 -- will be used to capture the aggregate assignments.
3637 TmpE : constant Entity_Id := Make_Temporary (Loc, 'A', N);
3639 TmpD : constant Node_Id :=
3640 Make_Object_Declaration (Loc,
3641 Defining_Identifier => TmpE,
3642 Object_Definition => New_Occurrence_Of (SubE, Loc));
3645 Set_No_Initialization (TmpD);
3646 Append_To (L, SubD);
3647 Append_To (L, TmpD);
3649 -- Expand aggregate into assignments to the temp array
3652 Late_Expansion (Expr_Q, Comp_Type,
3653 New_Occurrence_Of (TmpE, Loc)));
3658 Make_Assignment_Statement (Loc,
3659 Name => New_Copy_Tree (Comp_Expr),
3660 Expression => New_Occurrence_Of (TmpE, Loc)));
3663 -- Normal case (sliding not required)
3667 Late_Expansion (Expr_Q, Comp_Type, Comp_Expr));
3670 -- Expr_Q is not delayed aggregate
3673 if Has_Discriminants (Typ) then
3674 Replace_Discriminants (Expr_Q);
3676 -- If the component is an array type that depends on
3677 -- discriminants, and the expression is a single Others
3678 -- clause, create an explicit subtype for it because the
3679 -- backend has troubles recovering the actual bounds.
3681 if Nkind (Expr_Q) = N_Aggregate
3682 and then Is_Array_Type (Comp_Type)
3683 and then Present (Component_Associations (Expr_Q))
3686 Assoc : constant Node_Id :=
3687 First (Component_Associations (Expr_Q));
3691 if Nkind (First (Choices (Assoc))) = N_Others_Choice
3694 Build_Actual_Subtype_Of_Component
3695 (Comp_Type, Comp_Expr);
3697 -- If the component type does not in fact depend on
3698 -- discriminants, the subtype declaration is empty.
3700 if Present (Decl) then
3701 Append_To (L, Decl);
3702 Set_Etype (Comp_Expr, Defining_Entity (Decl));
3709 if Modify_Tree_For_C
3710 and then Nkind (Expr_Q) = N_Aggregate
3711 and then Is_Array_Type (Etype (Expr_Q))
3712 and then Present (First_Index (Etype (Expr_Q)))
3715 Expr_Q_Type : constant Node_Id := Etype (Expr_Q);
3718 Build_Array_Aggr_Code
3720 Ctype => Component_Type (Expr_Q_Type),
3721 Index => First_Index (Expr_Q_Type),
3724 Is_Scalar_Type (Component_Type (Expr_Q_Type))));
3728 -- Handle an initialization expression of a controlled type
3729 -- in case it denotes a function call. In general such a
3730 -- scenario will produce a transient scope, but this will
3731 -- lead to wrong order of initialization, adjustment, and
3732 -- finalization in the context of aggregates.
3734 -- Target.Comp := Ctrl_Func_Call;
3737 -- Trans_Obj : ... := Ctrl_Func_Call; -- object
3738 -- Target.Comp := Trans_Obj;
3739 -- Finalize (Trans_Obj);
3741 -- Target.Comp._tag := ...;
3742 -- Adjust (Target.Comp);
3744 -- In the example above, the call to Finalize occurs too
3745 -- early and as a result it may leave the record component
3746 -- in a bad state. Finalization of the transient object
3747 -- should really happen after adjustment.
3749 -- To avoid this scenario, perform in-place side-effect
3750 -- removal of the function call. This eliminates the
3751 -- transient property of the function result and ensures
3752 -- correct order of actions.
3754 -- Res : ... := Ctrl_Func_Call;
3755 -- Target.Comp := Res;
3756 -- Target.Comp._tag := ...;
3757 -- Adjust (Target.Comp);
3760 if Needs_Finalization (Comp_Type)
3761 and then Nkind (Expr_Q) /= N_Aggregate
3763 Initialize_Ctrl_Record_Component
3764 (Rec_Comp => Comp_Expr,
3765 Comp_Typ => Etype (Selector),
3766 Init_Expr => Expr_Q,
3769 -- Otherwise perform single component initialization
3772 Initialize_Record_Component
3773 (Rec_Comp => Comp_Expr,
3774 Comp_Typ => Etype (Selector),
3775 Init_Expr => Expr_Q,
3781 -- comment would be good here ???
3783 elsif Ekind (Selector) = E_Discriminant
3784 and then Nkind (N) /= N_Extension_Aggregate
3785 and then Nkind (Parent (N)) = N_Component_Association
3786 and then Is_Constrained (Typ)
3788 -- We must check that the discriminant value imposed by the
3789 -- context is the same as the value given in the subaggregate,
3790 -- because after the expansion into assignments there is no
3791 -- record on which to perform a regular discriminant check.
3798 D_Val := First_Elmt (Discriminant_Constraint (Typ));
3799 Disc := First_Discriminant (Typ);
3800 while Chars (Disc) /= Chars (Selector) loop
3801 Next_Discriminant (Disc);
3805 pragma Assert (Present (D_Val));
3807 -- This check cannot performed for components that are
3808 -- constrained by a current instance, because this is not a
3809 -- value that can be compared with the actual constraint.
3811 if Nkind (Node (D_Val)) /= N_Attribute_Reference
3812 or else not Is_Entity_Name (Prefix (Node (D_Val)))
3813 or else not Is_Type (Entity (Prefix (Node (D_Val))))
3816 Make_Raise_Constraint_Error (Loc,
3819 Left_Opnd => New_Copy_Tree (Node (D_Val)),
3820 Right_Opnd => Expression (Comp)),
3821 Reason => CE_Discriminant_Check_Failed));
3824 -- Find self-reference in previous discriminant assignment,
3825 -- and replace with proper expression.
3832 while Present (Ass) loop
3833 if Nkind (Ass) = N_Assignment_Statement
3834 and then Nkind (Name (Ass)) = N_Selected_Component
3835 and then Chars (Selector_Name (Name (Ass))) =
3839 (Ass, New_Copy_Tree (Expression (Comp)));
3852 -- If the type is tagged, the tag needs to be initialized (unless we
3853 -- are in VM-mode where tags are implicit). It is done late in the
3854 -- initialization process because in some cases, we call the init
3855 -- proc of an ancestor which will not leave out the right tag.
3857 if Ancestor_Is_Expression then
3860 -- For CPP types we generated a call to the C++ default constructor
3861 -- before the components have been initialized to ensure the proper
3862 -- initialization of the _Tag component (see above).
3864 elsif Is_CPP_Class (Typ) then
3867 elsif Is_Tagged_Type (Typ) and then Tagged_Type_Expansion then
3869 Make_OK_Assignment_Statement (Loc,
3871 Make_Selected_Component (Loc,
3872 Prefix => New_Copy_Tree (Target),
3875 (First_Tag_Component (Base_Type (Typ)), Loc)),
3878 Unchecked_Convert_To (RTE (RE_Tag),
3880 (Node (First_Elmt (Access_Disp_Table (Base_Type (Typ)))),
3883 Append_To (L, Instr);
3885 -- Ada 2005 (AI-251): If the tagged type has been derived from an
3886 -- abstract interfaces we must also initialize the tags of the
3887 -- secondary dispatch tables.
3889 if Has_Interfaces (Base_Type (Typ)) then
3891 (Typ => Base_Type (Typ),
3894 Init_Tags_List => L);
3898 -- If the controllers have not been initialized yet (by lack of non-
3899 -- discriminant components), let's do it now.
3901 Generate_Finalization_Actions;
3904 end Build_Record_Aggr_Code;
3906 ---------------------------------------
3907 -- Collect_Initialization_Statements --
3908 ---------------------------------------
3910 procedure Collect_Initialization_Statements
3913 Node_After : Node_Id)
3915 Loc : constant Source_Ptr := Sloc (N);
3916 Init_Actions : constant List_Id := New_List;
3917 Init_Node : Node_Id;
3918 Comp_Stmt : Node_Id;
3921 -- Nothing to do if Obj is already frozen, as in this case we known we
3922 -- won't need to move the initialization statements about later on.
3924 if Is_Frozen (Obj) then
3929 while Next (Init_Node) /= Node_After loop
3930 Append_To (Init_Actions, Remove_Next (Init_Node));
3933 if not Is_Empty_List (Init_Actions) then
3934 Comp_Stmt := Make_Compound_Statement (Loc, Actions => Init_Actions);
3935 Insert_Action_After (Init_Node, Comp_Stmt);
3936 Set_Initialization_Statements (Obj, Comp_Stmt);
3938 end Collect_Initialization_Statements;
3940 -------------------------------
3941 -- Convert_Aggr_In_Allocator --
3942 -------------------------------
3944 procedure Convert_Aggr_In_Allocator
3949 Loc : constant Source_Ptr := Sloc (Aggr);
3950 Typ : constant Entity_Id := Etype (Aggr);
3951 Temp : constant Entity_Id := Defining_Identifier (Decl);
3953 Occ : constant Node_Id :=
3954 Unchecked_Convert_To (Typ,
3955 Make_Explicit_Dereference (Loc, New_Occurrence_Of (Temp, Loc)));
3958 if Is_Array_Type (Typ) then
3959 Convert_Array_Aggr_In_Allocator (Decl, Aggr, Occ);
3961 elsif Has_Default_Init_Comps (Aggr) then
3963 L : constant List_Id := New_List;
3964 Init_Stmts : List_Id;
3967 Init_Stmts := Late_Expansion (Aggr, Typ, Occ);
3969 if Has_Task (Typ) then
3970 Build_Task_Allocate_Block_With_Init_Stmts (L, Aggr, Init_Stmts);
3971 Insert_Actions (Alloc, L);
3973 Insert_Actions (Alloc, Init_Stmts);
3978 Insert_Actions (Alloc, Late_Expansion (Aggr, Typ, Occ));
3980 end Convert_Aggr_In_Allocator;
3982 --------------------------------
3983 -- Convert_Aggr_In_Assignment --
3984 --------------------------------
3986 procedure Convert_Aggr_In_Assignment (N : Node_Id) is
3987 Aggr : Node_Id := Expression (N);
3988 Typ : constant Entity_Id := Etype (Aggr);
3989 Occ : constant Node_Id := New_Copy_Tree (Name (N));
3992 if Nkind (Aggr) = N_Qualified_Expression then
3993 Aggr := Expression (Aggr);
3996 Insert_Actions_After (N, Late_Expansion (Aggr, Typ, Occ));
3997 end Convert_Aggr_In_Assignment;
3999 ---------------------------------
4000 -- Convert_Aggr_In_Object_Decl --
4001 ---------------------------------
4003 procedure Convert_Aggr_In_Object_Decl (N : Node_Id) is
4004 Obj : constant Entity_Id := Defining_Identifier (N);
4005 Aggr : Node_Id := Expression (N);
4006 Loc : constant Source_Ptr := Sloc (Aggr);
4007 Typ : constant Entity_Id := Etype (Aggr);
4008 Occ : constant Node_Id := New_Occurrence_Of (Obj, Loc);
4010 function Discriminants_Ok return Boolean;
4011 -- If the object type is constrained, the discriminants in the
4012 -- aggregate must be checked against the discriminants of the subtype.
4013 -- This cannot be done using Apply_Discriminant_Checks because after
4014 -- expansion there is no aggregate left to check.
4016 ----------------------
4017 -- Discriminants_Ok --
4018 ----------------------
4020 function Discriminants_Ok return Boolean is
4021 Cond : Node_Id := Empty;
4030 D := First_Discriminant (Typ);
4031 Disc1 := First_Elmt (Discriminant_Constraint (Typ));
4032 Disc2 := First_Elmt (Discriminant_Constraint (Etype (Obj)));
4033 while Present (Disc1) and then Present (Disc2) loop
4034 Val1 := Node (Disc1);
4035 Val2 := Node (Disc2);
4037 if not Is_OK_Static_Expression (Val1)
4038 or else not Is_OK_Static_Expression (Val2)
4040 Check := Make_Op_Ne (Loc,
4041 Left_Opnd => Duplicate_Subexpr (Val1),
4042 Right_Opnd => Duplicate_Subexpr (Val2));
4048 Cond := Make_Or_Else (Loc,
4050 Right_Opnd => Check);
4053 elsif Expr_Value (Val1) /= Expr_Value (Val2) then
4054 Apply_Compile_Time_Constraint_Error (Aggr,
4055 Msg => "incorrect value for discriminant&??",
4056 Reason => CE_Discriminant_Check_Failed,
4061 Next_Discriminant (D);
4066 -- If any discriminant constraint is nonstatic, emit a check
4068 if Present (Cond) then
4070 Make_Raise_Constraint_Error (Loc,
4072 Reason => CE_Discriminant_Check_Failed));
4076 end Discriminants_Ok;
4078 -- Start of processing for Convert_Aggr_In_Object_Decl
4081 Set_Assignment_OK (Occ);
4083 if Nkind (Aggr) = N_Qualified_Expression then
4084 Aggr := Expression (Aggr);
4087 if Has_Discriminants (Typ)
4088 and then Typ /= Etype (Obj)
4089 and then Is_Constrained (Etype (Obj))
4090 and then not Discriminants_Ok
4095 -- If the context is an extended return statement, it has its own
4096 -- finalization machinery (i.e. works like a transient scope) and
4097 -- we do not want to create an additional one, because objects on
4098 -- the finalization list of the return must be moved to the caller's
4099 -- finalization list to complete the return.
4101 -- However, if the aggregate is limited, it is built in place, and the
4102 -- controlled components are not assigned to intermediate temporaries
4103 -- so there is no need for a transient scope in this case either.
4105 if Requires_Transient_Scope (Typ)
4106 and then Ekind (Current_Scope) /= E_Return_Statement
4107 and then not Is_Limited_Type (Typ)
4109 Establish_Transient_Scope (Aggr, Manage_Sec_Stack => False);
4113 Node_After : constant Node_Id := Next (N);
4115 Insert_Actions_After (N, Late_Expansion (Aggr, Typ, Occ));
4116 Collect_Initialization_Statements (Obj, N, Node_After);
4119 Set_No_Initialization (N);
4120 Initialize_Discriminants (N, Typ);
4121 end Convert_Aggr_In_Object_Decl;
4123 -------------------------------------
4124 -- Convert_Array_Aggr_In_Allocator --
4125 -------------------------------------
4127 procedure Convert_Array_Aggr_In_Allocator
4132 Aggr_Code : List_Id;
4133 Typ : constant Entity_Id := Etype (Aggr);
4134 Ctyp : constant Entity_Id := Component_Type (Typ);
4137 -- The target is an explicit dereference of the allocated object.
4138 -- Generate component assignments to it, as for an aggregate that
4139 -- appears on the right-hand side of an assignment statement.
4142 Build_Array_Aggr_Code (Aggr,
4144 Index => First_Index (Typ),
4146 Scalar_Comp => Is_Scalar_Type (Ctyp));
4148 Insert_Actions_After (Decl, Aggr_Code);
4149 end Convert_Array_Aggr_In_Allocator;
4151 ------------------------
4152 -- In_Place_Assign_OK --
4153 ------------------------
4155 function In_Place_Assign_OK (N : Node_Id) return Boolean is
4156 Is_Array : constant Boolean := Is_Array_Type (Etype (N));
4165 function Safe_Aggregate (Aggr : Node_Id) return Boolean;
4166 -- Check recursively that each component of a (sub)aggregate does not
4167 -- depend on the variable being assigned to.
4169 function Safe_Component (Expr : Node_Id) return Boolean;
4170 -- Verify that an expression cannot depend on the variable being
4171 -- assigned to. Room for improvement here (but less than before).
4173 --------------------
4174 -- Safe_Aggregate --
4175 --------------------
4177 function Safe_Aggregate (Aggr : Node_Id) return Boolean is
4181 if Nkind (Parent (Aggr)) = N_Iterated_Component_Association then
4185 if Present (Expressions (Aggr)) then
4186 Expr := First (Expressions (Aggr));
4187 while Present (Expr) loop
4188 if Nkind (Expr) = N_Aggregate then
4189 if not Safe_Aggregate (Expr) then
4193 elsif not Safe_Component (Expr) then
4201 if Present (Component_Associations (Aggr)) then
4202 Expr := First (Component_Associations (Aggr));
4203 while Present (Expr) loop
4204 if Nkind (Expression (Expr)) = N_Aggregate then
4205 if not Safe_Aggregate (Expression (Expr)) then
4209 -- If association has a box, no way to determine yet whether
4210 -- default can be assigned in place.
4212 elsif Box_Present (Expr) then
4215 elsif not Safe_Component (Expression (Expr)) then
4226 --------------------
4227 -- Safe_Component --
4228 --------------------
4230 function Safe_Component (Expr : Node_Id) return Boolean is
4231 Comp : Node_Id := Expr;
4233 function Check_Component (Comp : Node_Id) return Boolean;
4234 -- Do the recursive traversal, after copy
4236 ---------------------
4237 -- Check_Component --
4238 ---------------------
4240 function Check_Component (Comp : Node_Id) return Boolean is
4242 if Is_Overloaded (Comp) then
4246 return Compile_Time_Known_Value (Comp)
4248 or else (Is_Entity_Name (Comp)
4249 and then Present (Entity (Comp))
4250 and then Ekind (Entity (Comp)) not in Type_Kind
4251 and then No (Renamed_Object (Entity (Comp))))
4253 or else (Nkind (Comp) = N_Attribute_Reference
4254 and then Check_Component (Prefix (Comp)))
4256 or else (Nkind (Comp) in N_Binary_Op
4257 and then Check_Component (Left_Opnd (Comp))
4258 and then Check_Component (Right_Opnd (Comp)))
4260 or else (Nkind (Comp) in N_Unary_Op
4261 and then Check_Component (Right_Opnd (Comp)))
4263 or else (Nkind (Comp) = N_Selected_Component
4265 and then Check_Component (Prefix (Comp)))
4267 or else (Nkind_In (Comp, N_Type_Conversion,
4268 N_Unchecked_Type_Conversion)
4269 and then Check_Component (Expression (Comp)));
4270 end Check_Component;
4272 -- Start of processing for Safe_Component
4275 -- If the component appears in an association that may correspond
4276 -- to more than one element, it is not analyzed before expansion
4277 -- into assignments, to avoid side effects. We analyze, but do not
4278 -- resolve the copy, to obtain sufficient entity information for
4279 -- the checks that follow. If component is overloaded we assume
4280 -- an unsafe function call.
4282 if not Analyzed (Comp) then
4283 if Is_Overloaded (Expr) then
4286 elsif Nkind (Expr) = N_Aggregate
4287 and then not Is_Others_Aggregate (Expr)
4291 elsif Nkind (Expr) = N_Allocator then
4293 -- For now, too complex to analyze
4297 elsif Nkind (Parent (Expr)) = N_Iterated_Component_Association then
4299 -- Ditto for iterated component associations, which in general
4300 -- require an enclosing loop and involve nonstatic expressions.
4305 Comp := New_Copy_Tree (Expr);
4306 Set_Parent (Comp, Parent (Expr));
4310 if Nkind (Comp) = N_Aggregate then
4311 return Safe_Aggregate (Comp);
4313 return Check_Component (Comp);
4317 -- Start of processing for In_Place_Assign_OK
4320 -- By-copy semantic cannot be guaranteed for controlled objects or
4321 -- objects with discriminants.
4323 if Needs_Finalization (Etype (N))
4324 or else Has_Discriminants (Etype (N))
4328 elsif Is_Array and then Present (Component_Associations (N)) then
4330 -- On assignment, sliding can take place, so we cannot do the
4331 -- assignment in place unless the bounds of the aggregate are
4332 -- statically equal to those of the target.
4334 -- If the aggregate is given by an others choice, the bounds are
4335 -- derived from the left-hand side, and the assignment is safe if
4336 -- the expression is.
4338 if Is_Others_Aggregate (N) then
4341 (Expression (First (Component_Associations (N))));
4344 Aggr_In := First_Index (Etype (N));
4346 if Nkind (Parent (N)) = N_Assignment_Statement then
4347 Obj_In := First_Index (Etype (Name (Parent (N))));
4350 -- Context is an allocator. Check bounds of aggregate against
4351 -- given type in qualified expression.
4353 pragma Assert (Nkind (Parent (Parent (N))) = N_Allocator);
4354 Obj_In := First_Index (Etype (Entity (Subtype_Mark (Parent (N)))));
4357 while Present (Aggr_In) loop
4358 Get_Index_Bounds (Aggr_In, Aggr_Lo, Aggr_Hi);
4359 Get_Index_Bounds (Obj_In, Obj_Lo, Obj_Hi);
4361 if not Compile_Time_Known_Value (Aggr_Lo)
4362 or else not Compile_Time_Known_Value (Obj_Lo)
4363 or else not Compile_Time_Known_Value (Obj_Hi)
4364 or else Expr_Value (Aggr_Lo) /= Expr_Value (Obj_Lo)
4368 -- For an assignment statement we require static matching of
4369 -- bounds. Ditto for an allocator whose qualified expression
4370 -- is a constrained type. If the expression in the allocator
4371 -- is an unconstrained array, we accept an upper bound that
4372 -- is not static, to allow for nonstatic expressions of the
4373 -- base type. Clearly there are further possibilities (with
4374 -- diminishing returns) for safely building arrays in place
4377 elsif Nkind (Parent (N)) = N_Assignment_Statement
4378 or else Is_Constrained (Etype (Parent (N)))
4380 if not Compile_Time_Known_Value (Aggr_Hi)
4381 or else Expr_Value (Aggr_Hi) /= Expr_Value (Obj_Hi)
4387 Next_Index (Aggr_In);
4388 Next_Index (Obj_In);
4392 -- Now check the component values themselves
4394 return Safe_Aggregate (N);
4395 end In_Place_Assign_OK;
4397 ----------------------------
4398 -- Convert_To_Assignments --
4399 ----------------------------
4401 procedure Convert_To_Assignments (N : Node_Id; Typ : Entity_Id) is
4402 Loc : constant Source_Ptr := Sloc (N);
4406 Aggr_Code : List_Id;
4408 Target_Expr : Node_Id;
4409 Parent_Kind : Node_Kind;
4410 Unc_Decl : Boolean := False;
4411 Parent_Node : Node_Id;
4414 pragma Assert (Nkind_In (N, N_Aggregate, N_Extension_Aggregate));
4415 pragma Assert (not Is_Static_Dispatch_Table_Aggregate (N));
4416 pragma Assert (Is_Record_Type (Typ));
4418 Parent_Node := Parent (N);
4419 Parent_Kind := Nkind (Parent_Node);
4421 if Parent_Kind = N_Qualified_Expression then
4422 -- Check if we are in an unconstrained declaration because in this
4423 -- case the current delayed expansion mechanism doesn't work when
4424 -- the declared object size depends on the initializing expr.
4426 Parent_Node := Parent (Parent_Node);
4427 Parent_Kind := Nkind (Parent_Node);
4429 if Parent_Kind = N_Object_Declaration then
4431 not Is_Entity_Name (Object_Definition (Parent_Node))
4432 or else (Nkind (N) = N_Aggregate
4435 (Entity (Object_Definition (Parent_Node))))
4436 or else Is_Class_Wide_Type
4437 (Entity (Object_Definition (Parent_Node)));
4441 -- Just set the Delay flag in the cases where the transformation will be
4442 -- done top down from above.
4446 -- Internal aggregate (transformed when expanding the parent)
4448 or else Parent_Kind = N_Aggregate
4449 or else Parent_Kind = N_Extension_Aggregate
4450 or else Parent_Kind = N_Component_Association
4452 -- Allocator (see Convert_Aggr_In_Allocator)
4454 or else Parent_Kind = N_Allocator
4456 -- Object declaration (see Convert_Aggr_In_Object_Decl)
4458 or else (Parent_Kind = N_Object_Declaration and then not Unc_Decl)
4460 -- Safe assignment (see Convert_Aggr_Assignments). So far only the
4461 -- assignments in init procs are taken into account.
4463 or else (Parent_Kind = N_Assignment_Statement
4464 and then Inside_Init_Proc)
4466 -- (Ada 2005) An inherently limited type in a return statement, which
4467 -- will be handled in a build-in-place fashion, and may be rewritten
4468 -- as an extended return and have its own finalization machinery.
4469 -- In the case of a simple return, the aggregate needs to be delayed
4470 -- until the scope for the return statement has been created, so
4471 -- that any finalization chain will be associated with that scope.
4472 -- For extended returns, we delay expansion to avoid the creation
4473 -- of an unwanted transient scope that could result in premature
4474 -- finalization of the return object (which is built in place
4475 -- within the caller's scope).
4477 or else Is_Build_In_Place_Aggregate_Return (N)
4479 Set_Expansion_Delayed (N);
4483 -- Otherwise, if a transient scope is required, create it now. If we
4484 -- are within an initialization procedure do not create such, because
4485 -- the target of the assignment must not be declared within a local
4486 -- block, and because cleanup will take place on return from the
4487 -- initialization procedure.
4489 -- Should the condition be more restrictive ???
4491 if Requires_Transient_Scope (Typ) and then not Inside_Init_Proc then
4492 Establish_Transient_Scope (N, Manage_Sec_Stack => False);
4495 -- If the aggregate is nonlimited, create a temporary, since aggregates
4496 -- have "by copy" semantics. If it is limited and context is an
4497 -- assignment, this is a subaggregate for an enclosing aggregate being
4498 -- expanded. It must be built in place, so use target of the current
4501 if Is_Limited_Type (Typ)
4502 and then Nkind (Parent (N)) = N_Assignment_Statement
4504 Target_Expr := New_Copy_Tree (Name (Parent (N)));
4505 Insert_Actions (Parent (N),
4506 Build_Record_Aggr_Code (N, Typ, Target_Expr));
4507 Rewrite (Parent (N), Make_Null_Statement (Loc));
4509 -- Do not declare a temporary to initialize an aggregate assigned to an
4510 -- identifier when in-place assignment is possible, preserving the
4511 -- by-copy semantic of aggregates. This avoids large stack usage and
4512 -- generates more efficient code.
4514 elsif Nkind (Parent (N)) = N_Assignment_Statement
4515 and then Nkind (Name (Parent (N))) = N_Identifier
4516 and then In_Place_Assign_OK (N)
4518 Target_Expr := New_Copy_Tree (Name (Parent (N)));
4519 Insert_Actions (Parent (N),
4520 Build_Record_Aggr_Code (N, Typ, Target_Expr));
4521 Rewrite (Parent (N), Make_Null_Statement (Loc));
4524 Temp := Make_Temporary (Loc, 'A', N);
4526 -- If the type inherits unknown discriminants, use the view with
4527 -- known discriminants if available.
4529 if Has_Unknown_Discriminants (Typ)
4530 and then Present (Underlying_Record_View (Typ))
4532 T := Underlying_Record_View (Typ);
4538 Make_Object_Declaration (Loc,
4539 Defining_Identifier => Temp,
4540 Object_Definition => New_Occurrence_Of (T, Loc));
4542 Set_No_Initialization (Instr);
4543 Insert_Action (N, Instr);
4544 Initialize_Discriminants (Instr, T);
4546 Target_Expr := New_Occurrence_Of (Temp, Loc);
4547 Aggr_Code := Build_Record_Aggr_Code (N, T, Target_Expr);
4549 -- Save the last assignment statement associated with the aggregate
4550 -- when building a controlled object. This reference is utilized by
4551 -- the finalization machinery when marking an object as successfully
4554 if Needs_Finalization (T) then
4555 Set_Last_Aggregate_Assignment (Temp, Last (Aggr_Code));
4558 Insert_Actions (N, Aggr_Code);
4559 Rewrite (N, New_Occurrence_Of (Temp, Loc));
4560 Analyze_And_Resolve (N, T);
4562 end Convert_To_Assignments;
4564 ---------------------------
4565 -- Convert_To_Positional --
4566 ---------------------------
4568 procedure Convert_To_Positional
4570 Max_Others_Replicate : Nat := 32;
4571 Handle_Bit_Packed : Boolean := False)
4573 Typ : constant Entity_Id := Etype (N);
4575 Static_Components : Boolean := True;
4577 procedure Check_Static_Components;
4578 -- Check whether all components of the aggregate are compile-time known
4579 -- values, and can be passed as is to the back-end without further
4585 Ixb : Node_Id) return Boolean;
4586 -- Convert the aggregate into a purely positional form if possible. On
4587 -- entry the bounds of all dimensions are known to be static, and the
4588 -- total number of components is safe enough to expand.
4590 function Is_Flat (N : Node_Id; Dims : Int) return Boolean;
4591 -- Return True iff the array N is flat (which is not trivial in the case
4592 -- of multidimensional aggregates).
4594 function Is_Static_Element (N : Node_Id) return Boolean;
4595 -- Return True if N, an element of a component association list, i.e.
4596 -- N_Component_Association or N_Iterated_Component_Association, has a
4597 -- compile-time known value and can be passed as is to the back-end
4598 -- without further expansion.
4599 -- An Iterated_Component_Association is treated as nonstatic in most
4600 -- cases for now, so there are possibilities for optimization.
4602 -----------------------------
4603 -- Check_Static_Components --
4604 -----------------------------
4606 -- Could use some comments in this body ???
4608 procedure Check_Static_Components is
4613 Static_Components := True;
4615 if Nkind (N) = N_String_Literal then
4618 elsif Present (Expressions (N)) then
4619 Expr := First (Expressions (N));
4620 while Present (Expr) loop
4621 if Nkind (Expr) /= N_Aggregate
4622 or else not Compile_Time_Known_Aggregate (Expr)
4623 or else Expansion_Delayed (Expr)
4625 Static_Components := False;
4633 if Nkind (N) = N_Aggregate
4634 and then Present (Component_Associations (N))
4636 Assoc := First (Component_Associations (N));
4637 while Present (Assoc) loop
4638 if not Is_Static_Element (Assoc) then
4639 Static_Components := False;
4646 end Check_Static_Components;
4655 Ixb : Node_Id) return Boolean
4657 Loc : constant Source_Ptr := Sloc (N);
4658 Blo : constant Node_Id := Type_Low_Bound (Etype (Ixb));
4659 Lo : constant Node_Id := Type_Low_Bound (Etype (Ix));
4660 Hi : constant Node_Id := Type_High_Bound (Etype (Ix));
4664 Others_Present : Boolean := False;
4667 if Nkind (Original_Node (N)) = N_String_Literal then
4671 if not Compile_Time_Known_Value (Lo)
4672 or else not Compile_Time_Known_Value (Hi)
4677 Lov := Expr_Value (Lo);
4678 Hiv := Expr_Value (Hi);
4680 -- Check if there is an others choice
4682 if Present (Component_Associations (N)) then
4688 Assoc := First (Component_Associations (N));
4689 while Present (Assoc) loop
4691 -- If this is a box association, flattening is in general
4692 -- not possible because at this point we cannot tell if the
4693 -- default is static or even exists.
4695 if Box_Present (Assoc) then
4698 elsif Nkind (Assoc) = N_Iterated_Component_Association then
4702 Choice := First (Choice_List (Assoc));
4704 while Present (Choice) loop
4705 if Nkind (Choice) = N_Others_Choice then
4706 Others_Present := True;
4717 -- If the low bound is not known at compile time and others is not
4718 -- present we can proceed since the bounds can be obtained from the
4722 or else (not Compile_Time_Known_Value (Blo) and then Others_Present)
4727 -- Determine if set of alternatives is suitable for conversion and
4728 -- build an array containing the values in sequence.
4731 Vals : array (UI_To_Int (Lov) .. UI_To_Int (Hiv))
4732 of Node_Id := (others => Empty);
4733 -- The values in the aggregate sorted appropriately
4736 -- Same data as Vals in list form
4739 -- Used to validate Max_Others_Replicate limit
4742 Num : Int := UI_To_Int (Lov);
4748 if Present (Expressions (N)) then
4749 Elmt := First (Expressions (N));
4750 while Present (Elmt) loop
4751 if Nkind (Elmt) = N_Aggregate
4752 and then Present (Next_Index (Ix))
4754 not Flatten (Elmt, Next_Index (Ix), Next_Index (Ixb))
4759 -- Duplicate expression for each index it covers
4761 Vals (Num) := New_Copy_Tree (Elmt);
4768 if No (Component_Associations (N)) then
4772 Elmt := First (Component_Associations (N));
4774 if Nkind (Expression (Elmt)) = N_Aggregate then
4775 if Present (Next_Index (Ix))
4778 (Expression (Elmt), Next_Index (Ix), Next_Index (Ixb))
4784 Component_Loop : while Present (Elmt) loop
4785 Choice := First (Choice_List (Elmt));
4786 Choice_Loop : while Present (Choice) loop
4788 -- If we have an others choice, fill in the missing elements
4789 -- subject to the limit established by Max_Others_Replicate.
4791 if Nkind (Choice) = N_Others_Choice then
4794 -- If the expression involves a construct that generates
4795 -- a loop, we must generate individual assignments and
4796 -- no flattening is possible.
4798 if Nkind (Expression (Elmt)) = N_Quantified_Expression
4803 for J in Vals'Range loop
4804 if No (Vals (J)) then
4805 Vals (J) := New_Copy_Tree (Expression (Elmt));
4806 Rep_Count := Rep_Count + 1;
4808 -- Check for maximum others replication. Note that
4809 -- we skip this test if either of the restrictions
4810 -- No_Elaboration_Code or No_Implicit_Loops is
4811 -- active, if this is a preelaborable unit or
4812 -- a predefined unit, or if the unit must be
4813 -- placed in data memory. This also ensures that
4814 -- predefined units get the same level of constant
4815 -- folding in Ada 95 and Ada 2005, where their
4816 -- categorization has changed.
4819 P : constant Entity_Id :=
4820 Cunit_Entity (Current_Sem_Unit);
4823 -- Check if duplication is always OK and, if so,
4824 -- continue processing.
4826 if Restriction_Active (No_Elaboration_Code)
4827 or else Restriction_Active (No_Implicit_Loops)
4829 (Ekind (Current_Scope) = E_Package
4830 and then Static_Elaboration_Desired
4832 or else Is_Preelaborated (P)
4833 or else (Ekind (P) = E_Package_Body
4835 Is_Preelaborated (Spec_Entity (P)))
4837 Is_Predefined_Unit (Get_Source_Unit (P))
4841 -- If duplication is not always OK, continue
4842 -- only if either the element is static or is
4843 -- an aggregate which can itself be flattened,
4844 -- and the replication count is not too high.
4846 elsif (Is_Static_Element (Elmt)
4848 (Nkind (Expression (Elmt)) = N_Aggregate
4849 and then Present (Next_Index (Ix))))
4850 and then Rep_Count <= Max_Others_Replicate
4854 -- Return False in all the other cases
4864 and then Warn_On_Redundant_Constructs
4866 Error_Msg_N ("there are no others?r?", Elmt);
4869 exit Component_Loop;
4871 -- Case of a subtype mark, identifier or expanded name
4873 elsif Is_Entity_Name (Choice)
4874 and then Is_Type (Entity (Choice))
4876 Lo := Type_Low_Bound (Etype (Choice));
4877 Hi := Type_High_Bound (Etype (Choice));
4879 -- Case of subtype indication
4881 elsif Nkind (Choice) = N_Subtype_Indication then
4882 Lo := Low_Bound (Range_Expression (Constraint (Choice)));
4883 Hi := High_Bound (Range_Expression (Constraint (Choice)));
4887 elsif Nkind (Choice) = N_Range then
4888 Lo := Low_Bound (Choice);
4889 Hi := High_Bound (Choice);
4891 -- Normal subexpression case
4893 else pragma Assert (Nkind (Choice) in N_Subexpr);
4894 if not Compile_Time_Known_Value (Choice) then
4898 Choice_Index := UI_To_Int (Expr_Value (Choice));
4900 if Choice_Index in Vals'Range then
4901 Vals (Choice_Index) :=
4902 New_Copy_Tree (Expression (Elmt));
4905 -- Choice is statically out-of-range, will be
4906 -- rewritten to raise Constraint_Error.
4914 -- Range cases merge with Lo,Hi set
4916 if not Compile_Time_Known_Value (Lo)
4918 not Compile_Time_Known_Value (Hi)
4923 for J in UI_To_Int (Expr_Value (Lo)) ..
4924 UI_To_Int (Expr_Value (Hi))
4926 Vals (J) := New_Copy_Tree (Expression (Elmt));
4932 end loop Choice_Loop;
4935 end loop Component_Loop;
4937 -- If we get here the conversion is possible
4940 for J in Vals'Range loop
4941 Append (Vals (J), Vlist);
4944 Rewrite (N, Make_Aggregate (Loc, Expressions => Vlist));
4945 Set_Aggregate_Bounds (N, Aggregate_Bounds (Original_Node (N)));
4954 function Is_Flat (N : Node_Id; Dims : Int) return Boolean is
4961 elsif Nkind (N) = N_Aggregate then
4962 if Present (Component_Associations (N)) then
4966 Elmt := First (Expressions (N));
4967 while Present (Elmt) loop
4968 if not Is_Flat (Elmt, Dims - 1) then
4982 -------------------------
4983 -- Is_Static_Element --
4984 -------------------------
4986 function Is_Static_Element (N : Node_Id) return Boolean is
4987 Expr : constant Node_Id := Expression (N);
4990 if Nkind_In (Expr, N_Integer_Literal, N_Real_Literal) then
4993 elsif Is_Entity_Name (Expr)
4994 and then Present (Entity (Expr))
4995 and then Ekind (Entity (Expr)) = E_Enumeration_Literal
4999 elsif Nkind (N) = N_Iterated_Component_Association then
5002 elsif Nkind (Expr) = N_Aggregate
5003 and then Compile_Time_Known_Aggregate (Expr)
5004 and then not Expansion_Delayed (Expr)
5011 end Is_Static_Element;
5013 -- Start of processing for Convert_To_Positional
5016 -- Only convert to positional when generating C in case of an
5017 -- object declaration, this is the only case where aggregates are
5020 if Modify_Tree_For_C and then not Is_CCG_Supported_Aggregate (N) then
5024 -- Ada 2005 (AI-287): Do not convert in case of default initialized
5025 -- components because in this case will need to call the corresponding
5028 if Has_Default_Init_Comps (N) then
5032 -- A subaggregate may have been flattened but is not known to be
5033 -- Compile_Time_Known. Set that flag in cases that cannot require
5034 -- elaboration code, so that the aggregate can be used as the
5035 -- initial value of a thread-local variable.
5037 if Is_Flat (N, Number_Dimensions (Typ)) then
5038 if Static_Array_Aggregate (N) then
5039 Set_Compile_Time_Known_Aggregate (N);
5045 if Is_Bit_Packed_Array (Typ) and then not Handle_Bit_Packed then
5049 -- Do not convert to positional if controlled components are involved
5050 -- since these require special processing
5052 if Has_Controlled_Component (Typ) then
5056 Check_Static_Components;
5058 -- If the size is known, or all the components are static, try to
5059 -- build a fully positional aggregate.
5061 -- The size of the type may not be known for an aggregate with
5062 -- discriminated array components, but if the components are static
5063 -- it is still possible to verify statically that the length is
5064 -- compatible with the upper bound of the type, and therefore it is
5065 -- worth flattening such aggregates as well.
5067 -- For now the back-end expands these aggregates into individual
5068 -- assignments to the target anyway, but it is conceivable that
5069 -- it will eventually be able to treat such aggregates statically???
5071 if Aggr_Size_OK (N, Typ)
5072 and then Flatten (N, First_Index (Typ), First_Index (Base_Type (Typ)))
5074 if Static_Components then
5075 Set_Compile_Time_Known_Aggregate (N);
5076 Set_Expansion_Delayed (N, False);
5079 Analyze_And_Resolve (N, Typ);
5082 -- If Static_Elaboration_Desired has been specified, diagnose aggregates
5083 -- that will still require initialization code.
5085 if (Ekind (Current_Scope) = E_Package
5086 and then Static_Elaboration_Desired (Current_Scope))
5087 and then Nkind (Parent (N)) = N_Object_Declaration
5093 if Nkind (N) = N_Aggregate and then Present (Expressions (N)) then
5094 Expr := First (Expressions (N));
5095 while Present (Expr) loop
5096 if Nkind_In (Expr, N_Integer_Literal, N_Real_Literal)
5098 (Is_Entity_Name (Expr)
5099 and then Ekind (Entity (Expr)) = E_Enumeration_Literal)
5105 ("non-static object requires elaboration code??", N);
5112 if Present (Component_Associations (N)) then
5113 Error_Msg_N ("object requires elaboration code??", N);
5118 end Convert_To_Positional;
5120 ----------------------------
5121 -- Expand_Array_Aggregate --
5122 ----------------------------
5124 -- Array aggregate expansion proceeds as follows:
5126 -- 1. If requested we generate code to perform all the array aggregate
5127 -- bound checks, specifically
5129 -- (a) Check that the index range defined by aggregate bounds is
5130 -- compatible with corresponding index subtype.
5132 -- (b) If an others choice is present check that no aggregate
5133 -- index is outside the bounds of the index constraint.
5135 -- (c) For multidimensional arrays make sure that all subaggregates
5136 -- corresponding to the same dimension have the same bounds.
5138 -- 2. Check for packed array aggregate which can be converted to a
5139 -- constant so that the aggregate disappears completely.
5141 -- 3. Check case of nested aggregate. Generally nested aggregates are
5142 -- handled during the processing of the parent aggregate.
5144 -- 4. Check if the aggregate can be statically processed. If this is the
5145 -- case pass it as is to Gigi. Note that a necessary condition for
5146 -- static processing is that the aggregate be fully positional.
5148 -- 5. If in-place aggregate expansion is possible (i.e. no need to create
5149 -- a temporary) then mark the aggregate as such and return. Otherwise
5150 -- create a new temporary and generate the appropriate initialization
5153 procedure Expand_Array_Aggregate (N : Node_Id) is
5154 Loc : constant Source_Ptr := Sloc (N);
5156 Typ : constant Entity_Id := Etype (N);
5157 Ctyp : constant Entity_Id := Component_Type (Typ);
5158 -- Typ is the correct constrained array subtype of the aggregate
5159 -- Ctyp is the corresponding component type.
5161 Aggr_Dimension : constant Pos := Number_Dimensions (Typ);
5162 -- Number of aggregate index dimensions
5164 Aggr_Low : array (1 .. Aggr_Dimension) of Node_Id;
5165 Aggr_High : array (1 .. Aggr_Dimension) of Node_Id;
5166 -- Low and High bounds of the constraint for each aggregate index
5168 Aggr_Index_Typ : array (1 .. Aggr_Dimension) of Entity_Id;
5169 -- The type of each index
5171 In_Place_Assign_OK_For_Declaration : Boolean := False;
5172 -- True if we are to generate an in-place assignment for a declaration
5174 Maybe_In_Place_OK : Boolean;
5175 -- If the type is neither controlled nor packed and the aggregate
5176 -- is the expression in an assignment, assignment in place may be
5177 -- possible, provided other conditions are met on the LHS.
5179 Others_Present : array (1 .. Aggr_Dimension) of Boolean :=
5181 -- If Others_Present (J) is True, then there is an others choice in one
5182 -- of the subaggregates of N at dimension J.
5184 function Aggr_Assignment_OK_For_Backend (N : Node_Id) return Boolean;
5185 -- Returns true if an aggregate assignment can be done by the back end
5187 procedure Build_Constrained_Type (Positional : Boolean);
5188 -- If the subtype is not static or unconstrained, build a constrained
5189 -- type using the computable sizes of the aggregate and its sub-
5192 procedure Check_Bounds (Aggr_Bounds : Node_Id; Index_Bounds : Node_Id);
5193 -- Checks that the bounds of Aggr_Bounds are within the bounds defined
5196 procedure Check_Same_Aggr_Bounds (Sub_Aggr : Node_Id; Dim : Pos);
5197 -- Checks that in a multidimensional array aggregate all subaggregates
5198 -- corresponding to the same dimension have the same bounds. Sub_Aggr is
5199 -- an array subaggregate. Dim is the dimension corresponding to the
5202 procedure Compute_Others_Present (Sub_Aggr : Node_Id; Dim : Pos);
5203 -- Computes the values of array Others_Present. Sub_Aggr is the array
5204 -- subaggregate we start the computation from. Dim is the dimension
5205 -- corresponding to the subaggregate.
5207 procedure Others_Check (Sub_Aggr : Node_Id; Dim : Pos);
5208 -- Checks that if an others choice is present in any subaggregate, no
5209 -- aggregate index is outside the bounds of the index constraint.
5210 -- Sub_Aggr is an array subaggregate. Dim is the dimension corresponding
5211 -- to the subaggregate.
5213 function Safe_Left_Hand_Side (N : Node_Id) return Boolean;
5214 -- In addition to Maybe_In_Place_OK, in order for an aggregate to be
5215 -- built directly into the target of the assignment it must be free
5218 ------------------------------------
5219 -- Aggr_Assignment_OK_For_Backend --
5220 ------------------------------------
5222 -- Backend processing by Gigi/gcc is possible only if all the following
5223 -- conditions are met:
5225 -- 1. N consists of a single OTHERS choice, possibly recursively
5227 -- 2. The array type has no null ranges (the purpose of this is to
5228 -- avoid a bogus warning for an out-of-range value).
5230 -- 3. The array type has no atomic components
5232 -- 4. The component type is elementary
5234 -- 5. The component size is a multiple of Storage_Unit
5236 -- 6. The component size is Storage_Unit or the value is of the form
5237 -- M * (1 + A**1 + A**2 + .. A**(K-1)) where A = 2**(Storage_Unit)
5238 -- and M in 1 .. A-1. This can also be viewed as K occurrences of
5239 -- the 8-bit value M, concatenated together.
5241 -- The ultimate goal is to generate a call to a fast memset routine
5242 -- specifically optimized for the target.
5244 function Aggr_Assignment_OK_For_Backend (N : Node_Id) return Boolean is
5245 Csiz : Uint := No_Uint;
5256 -- Back end doesn't know about <>
5258 if Has_Default_Init_Comps (N) then
5262 -- Recurse as far as possible to find the innermost component type
5266 while Is_Array_Type (Ctyp) loop
5267 if Nkind (Expr) /= N_Aggregate
5268 or else not Is_Others_Aggregate (Expr)
5273 Index := First_Index (Ctyp);
5274 while Present (Index) loop
5275 Get_Index_Bounds (Index, Low, High);
5277 if Is_Null_Range (Low, High) then
5284 Expr := Expression (First (Component_Associations (Expr)));
5286 for J in 1 .. Number_Dimensions (Ctyp) - 1 loop
5287 if Nkind (Expr) /= N_Aggregate
5288 or else not Is_Others_Aggregate (Expr)
5293 Expr := Expression (First (Component_Associations (Expr)));
5296 if Has_Atomic_Components (Ctyp) then
5300 Csiz := Component_Size (Ctyp);
5301 Ctyp := Component_Type (Ctyp);
5303 if Is_Atomic_Or_VFA (Ctyp) then
5308 -- An Iterated_Component_Association involves a loop (in most cases)
5309 -- and is never static.
5311 if Nkind (Parent (Expr)) = N_Iterated_Component_Association then
5315 -- Access types need to be dealt with specially
5317 if Is_Access_Type (Ctyp) then
5319 -- Component_Size is not set by Layout_Type if the component
5320 -- type is an access type ???
5322 Csiz := Esize (Ctyp);
5324 -- Fat pointers are rejected as they are not really elementary
5327 if Csiz /= System_Address_Size then
5331 -- The supported expressions are NULL and constants, others are
5332 -- rejected upfront to avoid being analyzed below, which can be
5333 -- problematic for some of them, for example allocators.
5335 if Nkind (Expr) /= N_Null and then not Is_Entity_Name (Expr) then
5339 -- Scalar types are OK if their size is a multiple of Storage_Unit
5341 elsif Is_Scalar_Type (Ctyp) then
5342 pragma Assert (Csiz /= No_Uint);
5344 if Csiz mod System_Storage_Unit /= 0 then
5348 -- Composite types are rejected
5354 -- If the expression has side effects (e.g. contains calls with
5355 -- potential side effects) reject as well. We only preanalyze the
5356 -- expression to prevent the removal of intended side effects.
5358 Preanalyze_And_Resolve (Expr, Ctyp);
5360 if not Side_Effect_Free (Expr) then
5364 -- The expression needs to be analyzed if True is returned
5366 Analyze_And_Resolve (Expr, Ctyp);
5368 -- Strip away any conversions from the expression as they simply
5369 -- qualify the real expression.
5371 while Nkind_In (Expr, N_Unchecked_Type_Conversion,
5374 Expr := Expression (Expr);
5377 Nunits := UI_To_Int (Csiz) / System_Storage_Unit;
5383 if not Compile_Time_Known_Value (Expr) then
5387 -- The only supported value for floating point is 0.0
5389 if Is_Floating_Point_Type (Ctyp) then
5390 return Expr_Value_R (Expr) = Ureal_0;
5393 -- For other types, we can look into the value as an integer
5395 Value := Expr_Value (Expr);
5397 if Has_Biased_Representation (Ctyp) then
5398 Value := Value - Expr_Value (Type_Low_Bound (Ctyp));
5401 -- Values 0 and -1 immediately satisfy the last check
5403 if Value = Uint_0 or else Value = Uint_Minus_1 then
5407 -- We need to work with an unsigned value
5410 Value := Value + 2**(System_Storage_Unit * Nunits);
5413 Remainder := Value rem 2**System_Storage_Unit;
5415 for J in 1 .. Nunits - 1 loop
5416 Value := Value / 2**System_Storage_Unit;
5418 if Value rem 2**System_Storage_Unit /= Remainder then
5424 end Aggr_Assignment_OK_For_Backend;
5426 ----------------------------
5427 -- Build_Constrained_Type --
5428 ----------------------------
5430 procedure Build_Constrained_Type (Positional : Boolean) is
5431 Loc : constant Source_Ptr := Sloc (N);
5432 Agg_Type : constant Entity_Id := Make_Temporary (Loc, 'A');
5435 Typ : constant Entity_Id := Etype (N);
5436 Indexes : constant List_Id := New_List;
5441 -- If the aggregate is purely positional, all its subaggregates
5442 -- have the same size. We collect the dimensions from the first
5443 -- subaggregate at each level.
5448 for D in 1 .. Number_Dimensions (Typ) loop
5449 Sub_Agg := First (Expressions (Sub_Agg));
5453 while Present (Comp) loop
5460 Low_Bound => Make_Integer_Literal (Loc, 1),
5461 High_Bound => Make_Integer_Literal (Loc, Num)));
5465 -- We know the aggregate type is unconstrained and the aggregate
5466 -- is not processable by the back end, therefore not necessarily
5467 -- positional. Retrieve each dimension bounds (computed earlier).
5469 for D in 1 .. Number_Dimensions (Typ) loop
5472 Low_Bound => Aggr_Low (D),
5473 High_Bound => Aggr_High (D)));
5478 Make_Full_Type_Declaration (Loc,
5479 Defining_Identifier => Agg_Type,
5481 Make_Constrained_Array_Definition (Loc,
5482 Discrete_Subtype_Definitions => Indexes,
5483 Component_Definition =>
5484 Make_Component_Definition (Loc,
5485 Aliased_Present => False,
5486 Subtype_Indication =>
5487 New_Occurrence_Of (Component_Type (Typ), Loc))));
5489 Insert_Action (N, Decl);
5491 Set_Etype (N, Agg_Type);
5492 Set_Is_Itype (Agg_Type);
5493 Freeze_Itype (Agg_Type, N);
5494 end Build_Constrained_Type;
5500 procedure Check_Bounds (Aggr_Bounds : Node_Id; Index_Bounds : Node_Id) is
5507 Cond : Node_Id := Empty;
5510 Get_Index_Bounds (Aggr_Bounds, Aggr_Lo, Aggr_Hi);
5511 Get_Index_Bounds (Index_Bounds, Ind_Lo, Ind_Hi);
5513 -- Generate the following test:
5515 -- [constraint_error when
5516 -- Aggr_Lo <= Aggr_Hi and then
5517 -- (Aggr_Lo < Ind_Lo or else Aggr_Hi > Ind_Hi)]
5519 -- As an optimization try to see if some tests are trivially vacuous
5520 -- because we are comparing an expression against itself.
5522 if Aggr_Lo = Ind_Lo and then Aggr_Hi = Ind_Hi then
5525 elsif Aggr_Hi = Ind_Hi then
5528 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
5529 Right_Opnd => Duplicate_Subexpr_Move_Checks (Ind_Lo));
5531 elsif Aggr_Lo = Ind_Lo then
5534 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi),
5535 Right_Opnd => Duplicate_Subexpr_Move_Checks (Ind_Hi));
5542 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
5543 Right_Opnd => Duplicate_Subexpr_Move_Checks (Ind_Lo)),
5547 Left_Opnd => Duplicate_Subexpr (Aggr_Hi),
5548 Right_Opnd => Duplicate_Subexpr (Ind_Hi)));
5551 if Present (Cond) then
5556 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
5557 Right_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi)),
5559 Right_Opnd => Cond);
5561 Set_Analyzed (Left_Opnd (Left_Opnd (Cond)), False);
5562 Set_Analyzed (Right_Opnd (Left_Opnd (Cond)), False);
5564 Make_Raise_Constraint_Error (Loc,
5566 Reason => CE_Range_Check_Failed));
5570 ----------------------------
5571 -- Check_Same_Aggr_Bounds --
5572 ----------------------------
5574 procedure Check_Same_Aggr_Bounds (Sub_Aggr : Node_Id; Dim : Pos) is
5575 Sub_Lo : constant Node_Id := Low_Bound (Aggregate_Bounds (Sub_Aggr));
5576 Sub_Hi : constant Node_Id := High_Bound (Aggregate_Bounds (Sub_Aggr));
5577 -- The bounds of this specific subaggregate
5579 Aggr_Lo : constant Node_Id := Aggr_Low (Dim);
5580 Aggr_Hi : constant Node_Id := Aggr_High (Dim);
5581 -- The bounds of the aggregate for this dimension
5583 Ind_Typ : constant Entity_Id := Aggr_Index_Typ (Dim);
5584 -- The index type for this dimension.xxx
5586 Cond : Node_Id := Empty;
5591 -- If index checks are on generate the test
5593 -- [constraint_error when
5594 -- Aggr_Lo /= Sub_Lo or else Aggr_Hi /= Sub_Hi]
5596 -- As an optimization try to see if some tests are trivially vacuos
5597 -- because we are comparing an expression against itself. Also for
5598 -- the first dimension the test is trivially vacuous because there
5599 -- is just one aggregate for dimension 1.
5601 if Index_Checks_Suppressed (Ind_Typ) then
5604 elsif Dim = 1 or else (Aggr_Lo = Sub_Lo and then Aggr_Hi = Sub_Hi)
5608 elsif Aggr_Hi = Sub_Hi then
5611 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
5612 Right_Opnd => Duplicate_Subexpr_Move_Checks (Sub_Lo));
5614 elsif Aggr_Lo = Sub_Lo then
5617 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi),
5618 Right_Opnd => Duplicate_Subexpr_Move_Checks (Sub_Hi));
5625 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
5626 Right_Opnd => Duplicate_Subexpr_Move_Checks (Sub_Lo)),
5630 Left_Opnd => Duplicate_Subexpr (Aggr_Hi),
5631 Right_Opnd => Duplicate_Subexpr (Sub_Hi)));
5634 if Present (Cond) then
5636 Make_Raise_Constraint_Error (Loc,
5638 Reason => CE_Length_Check_Failed));
5641 -- Now look inside the subaggregate to see if there is more work
5643 if Dim < Aggr_Dimension then
5645 -- Process positional components
5647 if Present (Expressions (Sub_Aggr)) then
5648 Expr := First (Expressions (Sub_Aggr));
5649 while Present (Expr) loop
5650 Check_Same_Aggr_Bounds (Expr, Dim + 1);
5655 -- Process component associations
5657 if Present (Component_Associations (Sub_Aggr)) then
5658 Assoc := First (Component_Associations (Sub_Aggr));
5659 while Present (Assoc) loop
5660 Expr := Expression (Assoc);
5661 Check_Same_Aggr_Bounds (Expr, Dim + 1);
5666 end Check_Same_Aggr_Bounds;
5668 ----------------------------
5669 -- Compute_Others_Present --
5670 ----------------------------
5672 procedure Compute_Others_Present (Sub_Aggr : Node_Id; Dim : Pos) is
5677 if Present (Component_Associations (Sub_Aggr)) then
5678 Assoc := Last (Component_Associations (Sub_Aggr));
5680 if Nkind (First (Choice_List (Assoc))) = N_Others_Choice then
5681 Others_Present (Dim) := True;
5685 -- Now look inside the subaggregate to see if there is more work
5687 if Dim < Aggr_Dimension then
5689 -- Process positional components
5691 if Present (Expressions (Sub_Aggr)) then
5692 Expr := First (Expressions (Sub_Aggr));
5693 while Present (Expr) loop
5694 Compute_Others_Present (Expr, Dim + 1);
5699 -- Process component associations
5701 if Present (Component_Associations (Sub_Aggr)) then
5702 Assoc := First (Component_Associations (Sub_Aggr));
5703 while Present (Assoc) loop
5704 Expr := Expression (Assoc);
5705 Compute_Others_Present (Expr, Dim + 1);
5710 end Compute_Others_Present;
5716 procedure Others_Check (Sub_Aggr : Node_Id; Dim : Pos) is
5717 Aggr_Lo : constant Node_Id := Aggr_Low (Dim);
5718 Aggr_Hi : constant Node_Id := Aggr_High (Dim);
5719 -- The bounds of the aggregate for this dimension
5721 Ind_Typ : constant Entity_Id := Aggr_Index_Typ (Dim);
5722 -- The index type for this dimension
5724 Need_To_Check : Boolean := False;
5726 Choices_Lo : Node_Id := Empty;
5727 Choices_Hi : Node_Id := Empty;
5728 -- The lowest and highest discrete choices for a named subaggregate
5730 Nb_Choices : Int := -1;
5731 -- The number of discrete non-others choices in this subaggregate
5733 Nb_Elements : Uint := Uint_0;
5734 -- The number of elements in a positional aggregate
5736 Cond : Node_Id := Empty;
5743 -- Check if we have an others choice. If we do make sure that this
5744 -- subaggregate contains at least one element in addition to the
5747 if Range_Checks_Suppressed (Ind_Typ) then
5748 Need_To_Check := False;
5750 elsif Present (Expressions (Sub_Aggr))
5751 and then Present (Component_Associations (Sub_Aggr))
5753 Need_To_Check := True;
5755 elsif Present (Component_Associations (Sub_Aggr)) then
5756 Assoc := Last (Component_Associations (Sub_Aggr));
5758 if Nkind (First (Choice_List (Assoc))) /= N_Others_Choice then
5759 Need_To_Check := False;
5762 -- Count the number of discrete choices. Start with -1 because
5763 -- the others choice does not count.
5765 -- Is there some reason we do not use List_Length here ???
5768 Assoc := First (Component_Associations (Sub_Aggr));
5769 while Present (Assoc) loop
5770 Choice := First (Choice_List (Assoc));
5771 while Present (Choice) loop
5772 Nb_Choices := Nb_Choices + 1;
5779 -- If there is only an others choice nothing to do
5781 Need_To_Check := (Nb_Choices > 0);
5785 Need_To_Check := False;
5788 -- If we are dealing with a positional subaggregate with an others
5789 -- choice then compute the number or positional elements.
5791 if Need_To_Check and then Present (Expressions (Sub_Aggr)) then
5792 Expr := First (Expressions (Sub_Aggr));
5793 Nb_Elements := Uint_0;
5794 while Present (Expr) loop
5795 Nb_Elements := Nb_Elements + 1;
5799 -- If the aggregate contains discrete choices and an others choice
5800 -- compute the smallest and largest discrete choice values.
5802 elsif Need_To_Check then
5803 Compute_Choices_Lo_And_Choices_Hi : declare
5805 Table : Case_Table_Type (1 .. Nb_Choices);
5806 -- Used to sort all the different choice values
5813 Assoc := First (Component_Associations (Sub_Aggr));
5814 while Present (Assoc) loop
5815 Choice := First (Choice_List (Assoc));
5816 while Present (Choice) loop
5817 if Nkind (Choice) = N_Others_Choice then
5821 Get_Index_Bounds (Choice, Low, High);
5822 Table (J).Choice_Lo := Low;
5823 Table (J).Choice_Hi := High;
5832 -- Sort the discrete choices
5834 Sort_Case_Table (Table);
5836 Choices_Lo := Table (1).Choice_Lo;
5837 Choices_Hi := Table (Nb_Choices).Choice_Hi;
5838 end Compute_Choices_Lo_And_Choices_Hi;
5841 -- If no others choice in this subaggregate, or the aggregate
5842 -- comprises only an others choice, nothing to do.
5844 if not Need_To_Check then
5847 -- If we are dealing with an aggregate containing an others choice
5848 -- and positional components, we generate the following test:
5850 -- if Ind_Typ'Pos (Aggr_Lo) + (Nb_Elements - 1) >
5851 -- Ind_Typ'Pos (Aggr_Hi)
5853 -- raise Constraint_Error;
5856 -- in the general case, but the following simpler test:
5858 -- [constraint_error when
5859 -- Aggr_Lo + (Nb_Elements - 1) > Aggr_Hi];
5861 -- instead if the index type is a signed integer.
5863 elsif Nb_Elements > Uint_0 then
5864 if Nb_Elements = Uint_1 then
5867 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
5868 Right_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi));
5870 elsif Is_Signed_Integer_Type (Ind_Typ) then
5875 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
5877 Make_Integer_Literal (Loc, Nb_Elements - 1)),
5878 Right_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi));
5886 Make_Attribute_Reference (Loc,
5887 Prefix => New_Occurrence_Of (Ind_Typ, Loc),
5888 Attribute_Name => Name_Pos,
5891 (Duplicate_Subexpr_Move_Checks (Aggr_Lo))),
5892 Right_Opnd => Make_Integer_Literal (Loc, Nb_Elements - 1)),
5895 Make_Attribute_Reference (Loc,
5896 Prefix => New_Occurrence_Of (Ind_Typ, Loc),
5897 Attribute_Name => Name_Pos,
5898 Expressions => New_List (
5899 Duplicate_Subexpr_Move_Checks (Aggr_Hi))));
5902 -- If we are dealing with an aggregate containing an others choice
5903 -- and discrete choices we generate the following test:
5905 -- [constraint_error when
5906 -- Choices_Lo < Aggr_Lo or else Choices_Hi > Aggr_Hi];
5913 Left_Opnd => Duplicate_Subexpr_Move_Checks (Choices_Lo),
5914 Right_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo)),
5918 Left_Opnd => Duplicate_Subexpr (Choices_Hi),
5919 Right_Opnd => Duplicate_Subexpr (Aggr_Hi)));
5922 if Present (Cond) then
5924 Make_Raise_Constraint_Error (Loc,
5926 Reason => CE_Length_Check_Failed));
5927 -- Questionable reason code, shouldn't that be a
5928 -- CE_Range_Check_Failed ???
5931 -- Now look inside the subaggregate to see if there is more work
5933 if Dim < Aggr_Dimension then
5935 -- Process positional components
5937 if Present (Expressions (Sub_Aggr)) then
5938 Expr := First (Expressions (Sub_Aggr));
5939 while Present (Expr) loop
5940 Others_Check (Expr, Dim + 1);
5945 -- Process component associations
5947 if Present (Component_Associations (Sub_Aggr)) then
5948 Assoc := First (Component_Associations (Sub_Aggr));
5949 while Present (Assoc) loop
5950 Expr := Expression (Assoc);
5951 Others_Check (Expr, Dim + 1);
5958 -------------------------
5959 -- Safe_Left_Hand_Side --
5960 -------------------------
5962 function Safe_Left_Hand_Side (N : Node_Id) return Boolean is
5963 function Is_Safe_Index (Indx : Node_Id) return Boolean;
5964 -- If the left-hand side includes an indexed component, check that
5965 -- the indexes are free of side effects.
5971 function Is_Safe_Index (Indx : Node_Id) return Boolean is
5973 if Is_Entity_Name (Indx) then
5976 elsif Nkind (Indx) = N_Integer_Literal then
5979 elsif Nkind (Indx) = N_Function_Call
5980 and then Is_Entity_Name (Name (Indx))
5981 and then Has_Pragma_Pure_Function (Entity (Name (Indx)))
5985 elsif Nkind (Indx) = N_Type_Conversion
5986 and then Is_Safe_Index (Expression (Indx))
5995 -- Start of processing for Safe_Left_Hand_Side
5998 if Is_Entity_Name (N) then
6001 elsif Nkind_In (N, N_Explicit_Dereference, N_Selected_Component)
6002 and then Safe_Left_Hand_Side (Prefix (N))
6006 elsif Nkind (N) = N_Indexed_Component
6007 and then Safe_Left_Hand_Side (Prefix (N))
6008 and then Is_Safe_Index (First (Expressions (N)))
6012 elsif Nkind (N) = N_Unchecked_Type_Conversion then
6013 return Safe_Left_Hand_Side (Expression (N));
6018 end Safe_Left_Hand_Side;
6023 -- Holds the temporary aggregate value
6026 -- Holds the declaration of Tmp
6028 Aggr_Code : List_Id;
6029 Parent_Node : Node_Id;
6030 Parent_Kind : Node_Kind;
6032 -- Start of processing for Expand_Array_Aggregate
6035 -- Do not touch the special aggregates of attributes used for Asm calls
6037 if Is_RTE (Ctyp, RE_Asm_Input_Operand)
6038 or else Is_RTE (Ctyp, RE_Asm_Output_Operand)
6042 -- Do not expand an aggregate for an array type which contains tasks if
6043 -- the aggregate is associated with an unexpanded return statement of a
6044 -- build-in-place function. The aggregate is expanded when the related
6045 -- return statement (rewritten into an extended return) is processed.
6046 -- This delay ensures that any temporaries and initialization code
6047 -- generated for the aggregate appear in the proper return block and
6048 -- use the correct _chain and _master.
6050 elsif Has_Task (Base_Type (Etype (N)))
6051 and then Nkind (Parent (N)) = N_Simple_Return_Statement
6052 and then Is_Build_In_Place_Function
6053 (Return_Applies_To (Return_Statement_Entity (Parent (N))))
6057 -- Do not attempt expansion if error already detected. We may reach this
6058 -- point in spite of previous errors when compiling with -gnatq, to
6059 -- force all possible errors (this is the usual ACATS mode).
6061 elsif Error_Posted (N) then
6065 -- If the semantic analyzer has determined that aggregate N will raise
6066 -- Constraint_Error at run time, then the aggregate node has been
6067 -- replaced with an N_Raise_Constraint_Error node and we should
6070 pragma Assert (not Raises_Constraint_Error (N));
6074 -- Check that the index range defined by aggregate bounds is
6075 -- compatible with corresponding index subtype.
6077 Index_Compatibility_Check : declare
6078 Aggr_Index_Range : Node_Id := First_Index (Typ);
6079 -- The current aggregate index range
6081 Index_Constraint : Node_Id := First_Index (Etype (Typ));
6082 -- The corresponding index constraint against which we have to
6083 -- check the above aggregate index range.
6086 Compute_Others_Present (N, 1);
6088 for J in 1 .. Aggr_Dimension loop
6089 -- There is no need to emit a check if an others choice is present
6090 -- for this array aggregate dimension since in this case one of
6091 -- N's subaggregates has taken its bounds from the context and
6092 -- these bounds must have been checked already. In addition all
6093 -- subaggregates corresponding to the same dimension must all have
6094 -- the same bounds (checked in (c) below).
6096 if not Range_Checks_Suppressed (Etype (Index_Constraint))
6097 and then not Others_Present (J)
6099 -- We don't use Checks.Apply_Range_Check here because it emits
6100 -- a spurious check. Namely it checks that the range defined by
6101 -- the aggregate bounds is nonempty. But we know this already
6104 Check_Bounds (Aggr_Index_Range, Index_Constraint);
6107 -- Save the low and high bounds of the aggregate index as well as
6108 -- the index type for later use in checks (b) and (c) below.
6110 Aggr_Low (J) := Low_Bound (Aggr_Index_Range);
6111 Aggr_High (J) := High_Bound (Aggr_Index_Range);
6113 Aggr_Index_Typ (J) := Etype (Index_Constraint);
6115 Next_Index (Aggr_Index_Range);
6116 Next_Index (Index_Constraint);
6118 end Index_Compatibility_Check;
6122 -- If an others choice is present check that no aggregate index is
6123 -- outside the bounds of the index constraint.
6125 Others_Check (N, 1);
6129 -- For multidimensional arrays make sure that all subaggregates
6130 -- corresponding to the same dimension have the same bounds.
6132 if Aggr_Dimension > 1 then
6133 Check_Same_Aggr_Bounds (N, 1);
6138 -- If we have a default component value, or simple initialization is
6139 -- required for the component type, then we replace <> in component
6140 -- associations by the required default value.
6143 Default_Val : Node_Id;
6147 if (Present (Default_Aspect_Component_Value (Typ))
6148 or else Needs_Simple_Initialization (Ctyp))
6149 and then Present (Component_Associations (N))
6151 Assoc := First (Component_Associations (N));
6152 while Present (Assoc) loop
6153 if Nkind (Assoc) = N_Component_Association
6154 and then Box_Present (Assoc)
6156 Set_Box_Present (Assoc, False);
6158 if Present (Default_Aspect_Component_Value (Typ)) then
6159 Default_Val := Default_Aspect_Component_Value (Typ);
6161 Default_Val := Get_Simple_Init_Val (Ctyp, N);
6164 Set_Expression (Assoc, New_Copy_Tree (Default_Val));
6165 Analyze_And_Resolve (Expression (Assoc), Ctyp);
6175 -- Here we test for is packed array aggregate that we can handle at
6176 -- compile time. If so, return with transformation done. Note that we do
6177 -- this even if the aggregate is nested, because once we have done this
6178 -- processing, there is no more nested aggregate.
6180 if Packed_Array_Aggregate_Handled (N) then
6184 -- At this point we try to convert to positional form
6186 if Ekind (Current_Scope) = E_Package
6187 and then Static_Elaboration_Desired (Current_Scope)
6189 Convert_To_Positional (N, Max_Others_Replicate => 100);
6191 Convert_To_Positional (N);
6194 -- if the result is no longer an aggregate (e.g. it may be a string
6195 -- literal, or a temporary which has the needed value), then we are
6196 -- done, since there is no longer a nested aggregate.
6198 if Nkind (N) /= N_Aggregate then
6201 -- We are also done if the result is an analyzed aggregate, indicating
6202 -- that Convert_To_Positional succeeded and reanalyzed the rewritten
6205 elsif Analyzed (N) and then Is_Rewrite_Substitution (N) then
6209 -- If all aggregate components are compile-time known and the aggregate
6210 -- has been flattened, nothing left to do. The same occurs if the
6211 -- aggregate is used to initialize the components of a statically
6212 -- allocated dispatch table.
6214 if Compile_Time_Known_Aggregate (N)
6215 or else Is_Static_Dispatch_Table_Aggregate (N)
6217 Set_Expansion_Delayed (N, False);
6221 -- Now see if back end processing is possible
6223 if Backend_Processing_Possible (N) then
6225 -- If the aggregate is static but the constraints are not, build
6226 -- a static subtype for the aggregate, so that Gigi can place it
6227 -- in static memory. Perform an unchecked_conversion to the non-
6228 -- static type imposed by the context.
6231 Itype : constant Entity_Id := Etype (N);
6233 Needs_Type : Boolean := False;
6236 Index := First_Index (Itype);
6237 while Present (Index) loop
6238 if not Is_OK_Static_Subtype (Etype (Index)) then
6247 Build_Constrained_Type (Positional => True);
6248 Rewrite (N, Unchecked_Convert_To (Itype, N));
6258 -- Delay expansion for nested aggregates: it will be taken care of when
6259 -- the parent aggregate is expanded.
6261 Parent_Node := Parent (N);
6262 Parent_Kind := Nkind (Parent_Node);
6264 if Parent_Kind = N_Qualified_Expression then
6265 Parent_Node := Parent (Parent_Node);
6266 Parent_Kind := Nkind (Parent_Node);
6269 if Parent_Kind = N_Aggregate
6270 or else Parent_Kind = N_Extension_Aggregate
6271 or else Parent_Kind = N_Component_Association
6272 or else (Parent_Kind = N_Object_Declaration
6273 and then Needs_Finalization (Typ))
6274 or else (Parent_Kind = N_Assignment_Statement
6275 and then Inside_Init_Proc)
6277 Set_Expansion_Delayed (N, not Static_Array_Aggregate (N));
6283 -- Check whether in-place aggregate expansion is possible
6285 -- For object declarations we build the aggregate in place, unless
6286 -- the array is bit-packed.
6288 -- For assignments we do the assignment in place if all the component
6289 -- associations have compile-time known values, or are default-
6290 -- initialized limited components, e.g. tasks. For other cases we
6291 -- create a temporary. The analysis for safety of on-line assignment
6292 -- is delicate, i.e. we don't know how to do it fully yet ???
6294 -- For allocators we assign to the designated object in place if the
6295 -- aggregate meets the same conditions as other in-place assignments.
6296 -- In this case the aggregate may not come from source but was created
6297 -- for default initialization, e.g. with Initialize_Scalars.
6299 if Requires_Transient_Scope (Typ) then
6300 Establish_Transient_Scope (N, Manage_Sec_Stack => False);
6303 -- An array of limited components is built in place
6305 if Is_Limited_Type (Typ) then
6306 Maybe_In_Place_OK := True;
6308 elsif Has_Default_Init_Comps (N) then
6309 Maybe_In_Place_OK := False;
6311 elsif Is_Bit_Packed_Array (Typ)
6312 or else Has_Controlled_Component (Typ)
6314 Maybe_In_Place_OK := False;
6317 Maybe_In_Place_OK :=
6318 (Nkind (Parent (N)) = N_Assignment_Statement
6319 and then In_Place_Assign_OK (N))
6322 (Nkind (Parent (Parent (N))) = N_Allocator
6323 and then In_Place_Assign_OK (N));
6326 -- If this is an array of tasks, it will be expanded into build-in-place
6327 -- assignments. Build an activation chain for the tasks now.
6329 if Has_Task (Etype (N)) then
6330 Build_Activation_Chain_Entity (N);
6333 -- Perform in-place expansion of aggregate in an object declaration.
6334 -- Note: actions generated for the aggregate will be captured in an
6335 -- expression-with-actions statement so that they can be transferred
6336 -- to freeze actions later if there is an address clause for the
6337 -- object. (Note: we don't use a block statement because this would
6338 -- cause generated freeze nodes to be elaborated in the wrong scope).
6340 -- Do not perform in-place expansion for SPARK 05 because aggregates are
6341 -- expected to appear in qualified form. In-place expansion eliminates
6342 -- the qualification and eventually violates this SPARK 05 restiction.
6344 -- Arrays of limited components must be built in place. The code
6345 -- previously excluded controlled components but this is an old
6346 -- oversight: the rules in 7.6 (17) are clear.
6348 if Comes_From_Source (Parent_Node)
6349 and then Parent_Kind = N_Object_Declaration
6350 and then Present (Expression (Parent_Node))
6352 Must_Slide (Etype (Defining_Identifier (Parent_Node)), Typ)
6353 and then not Is_Bit_Packed_Array (Typ)
6354 and then not Restriction_Check_Required (SPARK_05)
6356 In_Place_Assign_OK_For_Declaration := True;
6357 Tmp := Defining_Identifier (Parent_Node);
6358 Set_No_Initialization (Parent_Node);
6359 Set_Expression (Parent_Node, Empty);
6361 -- Set kind and type of the entity, for use in the analysis
6362 -- of the subsequent assignments. If the nominal type is not
6363 -- constrained, build a subtype from the known bounds of the
6364 -- aggregate. If the declaration has a subtype mark, use it,
6365 -- otherwise use the itype of the aggregate.
6367 Set_Ekind (Tmp, E_Variable);
6369 if not Is_Constrained (Typ) then
6370 Build_Constrained_Type (Positional => False);
6372 elsif Is_Entity_Name (Object_Definition (Parent_Node))
6373 and then Is_Constrained (Entity (Object_Definition (Parent_Node)))
6375 Set_Etype (Tmp, Entity (Object_Definition (Parent_Node)));
6378 Set_Size_Known_At_Compile_Time (Typ, False);
6379 Set_Etype (Tmp, Typ);
6382 elsif Maybe_In_Place_OK
6383 and then Nkind (Parent (N)) = N_Qualified_Expression
6384 and then Nkind (Parent (Parent (N))) = N_Allocator
6386 Set_Expansion_Delayed (N);
6389 -- Limited arrays in return statements are expanded when
6390 -- enclosing construct is expanded.
6392 elsif Maybe_In_Place_OK
6393 and then Nkind (Parent (N)) = N_Simple_Return_Statement
6395 Set_Expansion_Delayed (N);
6398 -- In the remaining cases the aggregate is the RHS of an assignment
6400 elsif Maybe_In_Place_OK
6401 and then Safe_Left_Hand_Side (Name (Parent (N)))
6403 Tmp := Name (Parent (N));
6405 if Etype (Tmp) /= Etype (N) then
6406 Apply_Length_Check (N, Etype (Tmp));
6408 if Nkind (N) = N_Raise_Constraint_Error then
6410 -- Static error, nothing further to expand
6416 -- If a slice assignment has an aggregate with a single others_choice,
6417 -- the assignment can be done in place even if bounds are not static,
6418 -- by converting it into a loop over the discrete range of the slice.
6420 elsif Maybe_In_Place_OK
6421 and then Nkind (Name (Parent (N))) = N_Slice
6422 and then Is_Others_Aggregate (N)
6424 Tmp := Name (Parent (N));
6426 -- Set type of aggregate to be type of lhs in assignment, in order
6427 -- to suppress redundant length checks.
6429 Set_Etype (N, Etype (Tmp));
6433 -- In-place aggregate expansion is not possible
6436 Maybe_In_Place_OK := False;
6437 Tmp := Make_Temporary (Loc, 'A', N);
6439 Make_Object_Declaration (Loc,
6440 Defining_Identifier => Tmp,
6441 Object_Definition => New_Occurrence_Of (Typ, Loc));
6442 Set_No_Initialization (Tmp_Decl, True);
6443 Set_Warnings_Off (Tmp);
6445 -- If we are within a loop, the temporary will be pushed on the
6446 -- stack at each iteration. If the aggregate is the expression
6447 -- for an allocator, it will be immediately copied to the heap
6448 -- and can be reclaimed at once. We create a transient scope
6449 -- around the aggregate for this purpose.
6451 if Ekind (Current_Scope) = E_Loop
6452 and then Nkind (Parent (Parent (N))) = N_Allocator
6454 Establish_Transient_Scope (N, Manage_Sec_Stack => False);
6457 Insert_Action (N, Tmp_Decl);
6460 -- Construct and insert the aggregate code. We can safely suppress index
6461 -- checks because this code is guaranteed not to raise CE on index
6462 -- checks. However we should *not* suppress all checks.
6468 if Nkind (Tmp) = N_Defining_Identifier then
6469 Target := New_Occurrence_Of (Tmp, Loc);
6472 if Has_Default_Init_Comps (N)
6473 and then not Maybe_In_Place_OK
6475 -- Ada 2005 (AI-287): This case has not been analyzed???
6477 raise Program_Error;
6480 -- Name in assignment is explicit dereference
6482 Target := New_Copy (Tmp);
6485 -- If we are to generate an in-place assignment for a declaration or
6486 -- an assignment statement, and the assignment can be done directly
6487 -- by the back end, then do not expand further.
6489 -- ??? We can also do that if in-place expansion is not possible but
6490 -- then we could go into an infinite recursion.
6492 if (In_Place_Assign_OK_For_Declaration or else Maybe_In_Place_OK)
6493 and then not CodePeer_Mode
6494 and then not Modify_Tree_For_C
6495 and then not Possible_Bit_Aligned_Component (Target)
6496 and then not Is_Possibly_Unaligned_Slice (Target)
6497 and then Aggr_Assignment_OK_For_Backend (N)
6499 if Maybe_In_Place_OK then
6505 Make_Assignment_Statement (Loc,
6507 Expression => New_Copy_Tree (N)));
6511 Build_Array_Aggr_Code (N,
6513 Index => First_Index (Typ),
6515 Scalar_Comp => Is_Scalar_Type (Ctyp));
6518 -- Save the last assignment statement associated with the aggregate
6519 -- when building a controlled object. This reference is utilized by
6520 -- the finalization machinery when marking an object as successfully
6523 if Needs_Finalization (Typ)
6524 and then Is_Entity_Name (Target)
6525 and then Present (Entity (Target))
6526 and then Ekind_In (Entity (Target), E_Constant, E_Variable)
6528 Set_Last_Aggregate_Assignment (Entity (Target), Last (Aggr_Code));
6532 -- If the aggregate is the expression in a declaration, the expanded
6533 -- code must be inserted after it. The defining entity might not come
6534 -- from source if this is part of an inlined body, but the declaration
6537 if Comes_From_Source (Tmp)
6539 (Nkind (Parent (N)) = N_Object_Declaration
6540 and then Comes_From_Source (Parent (N))
6541 and then Tmp = Defining_Entity (Parent (N)))
6544 Node_After : constant Node_Id := Next (Parent_Node);
6547 Insert_Actions_After (Parent_Node, Aggr_Code);
6549 if Parent_Kind = N_Object_Declaration then
6550 Collect_Initialization_Statements
6551 (Obj => Tmp, N => Parent_Node, Node_After => Node_After);
6556 Insert_Actions (N, Aggr_Code);
6559 -- If the aggregate has been assigned in place, remove the original
6562 if Nkind (Parent (N)) = N_Assignment_Statement
6563 and then Maybe_In_Place_OK
6565 Rewrite (Parent (N), Make_Null_Statement (Loc));
6567 elsif Nkind (Parent (N)) /= N_Object_Declaration
6568 or else Tmp /= Defining_Identifier (Parent (N))
6570 Rewrite (N, New_Occurrence_Of (Tmp, Loc));
6571 Analyze_And_Resolve (N, Typ);
6573 end Expand_Array_Aggregate;
6575 ------------------------
6576 -- Expand_N_Aggregate --
6577 ------------------------
6579 procedure Expand_N_Aggregate (N : Node_Id) is
6581 -- Record aggregate case
6583 if Is_Record_Type (Etype (N)) then
6584 Expand_Record_Aggregate (N);
6586 -- Array aggregate case
6589 -- A special case, if we have a string subtype with bounds 1 .. N,
6590 -- where N is known at compile time, and the aggregate is of the
6591 -- form (others => 'x'), with a single choice and no expressions,
6592 -- and N is less than 80 (an arbitrary limit for now), then replace
6593 -- the aggregate by the equivalent string literal (but do not mark
6594 -- it as static since it is not).
6596 -- Note: this entire circuit is redundant with respect to code in
6597 -- Expand_Array_Aggregate that collapses others choices to positional
6598 -- form, but there are two problems with that circuit:
6600 -- a) It is limited to very small cases due to ill-understood
6601 -- interactions with bootstrapping. That limit is removed by
6602 -- use of the No_Implicit_Loops restriction.
6604 -- b) It incorrectly ends up with the resulting expressions being
6605 -- considered static when they are not. For example, the
6606 -- following test should fail:
6608 -- pragma Restrictions (No_Implicit_Loops);
6609 -- package NonSOthers4 is
6610 -- B : constant String (1 .. 6) := (others => 'A');
6611 -- DH : constant String (1 .. 8) := B & "BB";
6613 -- pragma Export (C, X, Link_Name => DH);
6616 -- But it succeeds (DH looks static to pragma Export)
6618 -- To be sorted out ???
6620 if Present (Component_Associations (N)) then
6622 CA : constant Node_Id := First (Component_Associations (N));
6623 MX : constant := 80;
6626 if Nkind (First (Choice_List (CA))) = N_Others_Choice
6627 and then Nkind (Expression (CA)) = N_Character_Literal
6628 and then No (Expressions (N))
6631 T : constant Entity_Id := Etype (N);
6632 X : constant Node_Id := First_Index (T);
6633 EC : constant Node_Id := Expression (CA);
6634 CV : constant Uint := Char_Literal_Value (EC);
6635 CC : constant Int := UI_To_Int (CV);
6638 if Nkind (X) = N_Range
6639 and then Compile_Time_Known_Value (Low_Bound (X))
6640 and then Expr_Value (Low_Bound (X)) = 1
6641 and then Compile_Time_Known_Value (High_Bound (X))
6644 Hi : constant Uint := Expr_Value (High_Bound (X));
6650 for J in 1 .. UI_To_Int (Hi) loop
6651 Store_String_Char (Char_Code (CC));
6655 Make_String_Literal (Sloc (N),
6656 Strval => End_String));
6658 if CC >= Int (2 ** 16) then
6659 Set_Has_Wide_Wide_Character (N);
6660 elsif CC >= Int (2 ** 8) then
6661 Set_Has_Wide_Character (N);
6664 Analyze_And_Resolve (N, T);
6665 Set_Is_Static_Expression (N, False);
6675 -- Not that special case, so normal expansion of array aggregate
6677 Expand_Array_Aggregate (N);
6681 when RE_Not_Available =>
6683 end Expand_N_Aggregate;
6685 ------------------------------
6686 -- Expand_N_Delta_Aggregate --
6687 ------------------------------
6689 procedure Expand_N_Delta_Aggregate (N : Node_Id) is
6690 Loc : constant Source_Ptr := Sloc (N);
6691 Typ : constant Entity_Id := Etype (N);
6696 Make_Object_Declaration (Loc,
6697 Defining_Identifier => Make_Temporary (Loc, 'T'),
6698 Object_Definition => New_Occurrence_Of (Typ, Loc),
6699 Expression => New_Copy_Tree (Expression (N)));
6701 if Is_Array_Type (Etype (N)) then
6702 Expand_Delta_Array_Aggregate (N, New_List (Decl));
6704 Expand_Delta_Record_Aggregate (N, New_List (Decl));
6706 end Expand_N_Delta_Aggregate;
6708 ----------------------------------
6709 -- Expand_Delta_Array_Aggregate --
6710 ----------------------------------
6712 procedure Expand_Delta_Array_Aggregate (N : Node_Id; Deltas : List_Id) is
6713 Loc : constant Source_Ptr := Sloc (N);
6714 Temp : constant Entity_Id := Defining_Identifier (First (Deltas));
6717 function Generate_Loop (C : Node_Id) return Node_Id;
6718 -- Generate a loop containing individual component assignments for
6719 -- choices that are ranges, subtype indications, subtype names, and
6720 -- iterated component associations.
6726 function Generate_Loop (C : Node_Id) return Node_Id is
6727 Sl : constant Source_Ptr := Sloc (C);
6731 if Nkind (Parent (C)) = N_Iterated_Component_Association then
6733 Make_Defining_Identifier (Loc,
6734 Chars => (Chars (Defining_Identifier (Parent (C)))));
6736 Ix := Make_Temporary (Sl, 'I');
6740 Make_Loop_Statement (Loc,
6742 Make_Iteration_Scheme (Sl,
6743 Loop_Parameter_Specification =>
6744 Make_Loop_Parameter_Specification (Sl,
6745 Defining_Identifier => Ix,
6746 Discrete_Subtype_Definition => New_Copy_Tree (C))),
6748 Statements => New_List (
6749 Make_Assignment_Statement (Sl,
6751 Make_Indexed_Component (Sl,
6752 Prefix => New_Occurrence_Of (Temp, Sl),
6753 Expressions => New_List (New_Occurrence_Of (Ix, Sl))),
6754 Expression => New_Copy_Tree (Expression (Assoc)))),
6755 End_Label => Empty);
6762 -- Start of processing for Expand_Delta_Array_Aggregate
6765 Assoc := First (Component_Associations (N));
6766 while Present (Assoc) loop
6767 Choice := First (Choice_List (Assoc));
6768 if Nkind (Assoc) = N_Iterated_Component_Association then
6769 while Present (Choice) loop
6770 Append_To (Deltas, Generate_Loop (Choice));
6775 while Present (Choice) loop
6777 -- Choice can be given by a range, a subtype indication, a
6778 -- subtype name, a scalar value, or an entity.
6780 if Nkind (Choice) = N_Range
6781 or else (Is_Entity_Name (Choice)
6782 and then Is_Type (Entity (Choice)))
6784 Append_To (Deltas, Generate_Loop (Choice));
6786 elsif Nkind (Choice) = N_Subtype_Indication then
6788 Generate_Loop (Range_Expression (Constraint (Choice))));
6792 Make_Assignment_Statement (Sloc (Choice),
6794 Make_Indexed_Component (Sloc (Choice),
6795 Prefix => New_Occurrence_Of (Temp, Loc),
6796 Expressions => New_List (New_Copy_Tree (Choice))),
6797 Expression => New_Copy_Tree (Expression (Assoc))));
6807 Insert_Actions (N, Deltas);
6808 Rewrite (N, New_Occurrence_Of (Temp, Loc));
6809 end Expand_Delta_Array_Aggregate;
6811 -----------------------------------
6812 -- Expand_Delta_Record_Aggregate --
6813 -----------------------------------
6815 procedure Expand_Delta_Record_Aggregate (N : Node_Id; Deltas : List_Id) is
6816 Loc : constant Source_Ptr := Sloc (N);
6817 Temp : constant Entity_Id := Defining_Identifier (First (Deltas));
6822 Assoc := First (Component_Associations (N));
6824 while Present (Assoc) loop
6825 Choice := First (Choice_List (Assoc));
6826 while Present (Choice) loop
6828 Make_Assignment_Statement (Sloc (Choice),
6830 Make_Selected_Component (Sloc (Choice),
6831 Prefix => New_Occurrence_Of (Temp, Loc),
6832 Selector_Name => Make_Identifier (Loc, Chars (Choice))),
6833 Expression => New_Copy_Tree (Expression (Assoc))));
6840 Insert_Actions (N, Deltas);
6841 Rewrite (N, New_Occurrence_Of (Temp, Loc));
6842 end Expand_Delta_Record_Aggregate;
6844 ----------------------------------
6845 -- Expand_N_Extension_Aggregate --
6846 ----------------------------------
6848 -- If the ancestor part is an expression, add a component association for
6849 -- the parent field. If the type of the ancestor part is not the direct
6850 -- parent of the expected type, build recursively the needed ancestors.
6851 -- If the ancestor part is a subtype_mark, replace aggregate with a
6852 -- declaration for a temporary of the expected type, followed by
6853 -- individual assignments to the given components.
6855 procedure Expand_N_Extension_Aggregate (N : Node_Id) is
6856 A : constant Node_Id := Ancestor_Part (N);
6857 Loc : constant Source_Ptr := Sloc (N);
6858 Typ : constant Entity_Id := Etype (N);
6861 -- If the ancestor is a subtype mark, an init proc must be called
6862 -- on the resulting object which thus has to be materialized in
6865 if Is_Entity_Name (A) and then Is_Type (Entity (A)) then
6866 Convert_To_Assignments (N, Typ);
6868 -- The extension aggregate is transformed into a record aggregate
6869 -- of the following form (c1 and c2 are inherited components)
6871 -- (Exp with c3 => a, c4 => b)
6872 -- ==> (c1 => Exp.c1, c2 => Exp.c2, c3 => a, c4 => b)
6877 if Tagged_Type_Expansion then
6878 Expand_Record_Aggregate (N,
6881 (Node (First_Elmt (Access_Disp_Table (Typ))), Loc),
6884 -- No tag is needed in the case of a VM
6887 Expand_Record_Aggregate (N, Parent_Expr => A);
6892 when RE_Not_Available =>
6894 end Expand_N_Extension_Aggregate;
6896 -----------------------------
6897 -- Expand_Record_Aggregate --
6898 -----------------------------
6900 procedure Expand_Record_Aggregate
6902 Orig_Tag : Node_Id := Empty;
6903 Parent_Expr : Node_Id := Empty)
6905 Loc : constant Source_Ptr := Sloc (N);
6906 Comps : constant List_Id := Component_Associations (N);
6907 Typ : constant Entity_Id := Etype (N);
6908 Base_Typ : constant Entity_Id := Base_Type (Typ);
6910 Static_Components : Boolean := True;
6911 -- Flag to indicate whether all components are compile-time known,
6912 -- and the aggregate can be constructed statically and handled by
6913 -- the back-end. Set to False by Component_OK_For_Backend.
6915 procedure Build_Back_End_Aggregate;
6916 -- Build a proper aggregate to be handled by the back-end
6918 function Compile_Time_Known_Composite_Value (N : Node_Id) return Boolean;
6919 -- Returns true if N is an expression of composite type which can be
6920 -- fully evaluated at compile time without raising constraint error.
6921 -- Such expressions can be passed as is to Gigi without any expansion.
6923 -- This returns true for N_Aggregate with Compile_Time_Known_Aggregate
6924 -- set and constants whose expression is such an aggregate, recursively.
6926 function Component_OK_For_Backend return Boolean;
6927 -- Check for presence of a component which makes it impossible for the
6928 -- backend to process the aggregate, thus requiring the use of a series
6929 -- of assignment statements. Cases checked for are a nested aggregate
6930 -- needing Late_Expansion, the presence of a tagged component which may
6931 -- need tag adjustment, and a bit unaligned component reference.
6933 -- We also force expansion into assignments if a component is of a
6934 -- mutable type (including a private type with discriminants) because
6935 -- in that case the size of the component to be copied may be smaller
6936 -- than the side of the target, and there is no simple way for gigi
6937 -- to compute the size of the object to be copied.
6939 -- NOTE: This is part of the ongoing work to define precisely the
6940 -- interface between front-end and back-end handling of aggregates.
6941 -- In general it is desirable to pass aggregates as they are to gigi,
6942 -- in order to minimize elaboration code. This is one case where the
6943 -- semantics of Ada complicate the analysis and lead to anomalies in
6944 -- the gcc back-end if the aggregate is not expanded into assignments.
6946 -- NOTE: This sets the global Static_Components to False in most, but
6947 -- not all, cases when it returns False.
6949 function Has_Per_Object_Constraint (L : List_Id) return Boolean;
6950 -- Return True if any element of L has Has_Per_Object_Constraint set.
6951 -- L should be the Choices component of an N_Component_Association.
6953 function Has_Visible_Private_Ancestor (Id : E) return Boolean;
6954 -- If any ancestor of the current type is private, the aggregate
6955 -- cannot be built in place. We cannot rely on Has_Private_Ancestor,
6956 -- because it will not be set when type and its parent are in the
6957 -- same scope, and the parent component needs expansion.
6959 function Top_Level_Aggregate (N : Node_Id) return Node_Id;
6960 -- For nested aggregates return the ultimate enclosing aggregate; for
6961 -- non-nested aggregates return N.
6963 ------------------------------
6964 -- Build_Back_End_Aggregate --
6965 ------------------------------
6967 procedure Build_Back_End_Aggregate is
6970 Tag_Value : Node_Id;
6973 if Nkind (N) = N_Aggregate then
6975 -- If the aggregate is static and can be handled by the back-end,
6976 -- nothing left to do.
6978 if Static_Components then
6979 Set_Compile_Time_Known_Aggregate (N);
6980 Set_Expansion_Delayed (N, False);
6984 -- If no discriminants, nothing special to do
6986 if not Has_Discriminants (Typ) then
6989 -- Case of discriminants present
6991 elsif Is_Derived_Type (Typ) then
6993 -- For untagged types, non-stored discriminants are replaced with
6994 -- stored discriminants, which are the ones that gigi uses to
6995 -- describe the type and its components.
6997 Generate_Aggregate_For_Derived_Type : declare
6998 procedure Prepend_Stored_Values (T : Entity_Id);
6999 -- Scan the list of stored discriminants of the type, and add
7000 -- their values to the aggregate being built.
7002 ---------------------------
7003 -- Prepend_Stored_Values --
7004 ---------------------------
7006 procedure Prepend_Stored_Values (T : Entity_Id) is
7008 First_Comp : Node_Id := Empty;
7011 Discr := First_Stored_Discriminant (T);
7012 while Present (Discr) loop
7014 Make_Component_Association (Loc,
7015 Choices => New_List (
7016 New_Occurrence_Of (Discr, Loc)),
7019 (Get_Discriminant_Value
7022 Discriminant_Constraint (Typ))));
7024 if No (First_Comp) then
7025 Prepend_To (Component_Associations (N), New_Comp);
7027 Insert_After (First_Comp, New_Comp);
7030 First_Comp := New_Comp;
7031 Next_Stored_Discriminant (Discr);
7033 end Prepend_Stored_Values;
7037 Constraints : constant List_Id := New_List;
7041 Num_Disc : Nat := 0;
7042 Num_Gird : Nat := 0;
7044 -- Start of processing for Generate_Aggregate_For_Derived_Type
7047 -- Remove the associations for the discriminant of derived type
7050 First_Comp : Node_Id;
7053 First_Comp := First (Component_Associations (N));
7054 while Present (First_Comp) loop
7058 if Ekind (Entity (First (Choices (Comp)))) =
7062 Num_Disc := Num_Disc + 1;
7067 -- Insert stored discriminant associations in the correct
7068 -- order. If there are more stored discriminants than new
7069 -- discriminants, there is at least one new discriminant that
7070 -- constrains more than one of the stored discriminants. In
7071 -- this case we need to construct a proper subtype of the
7072 -- parent type, in order to supply values to all the
7073 -- components. Otherwise there is one-one correspondence
7074 -- between the constraints and the stored discriminants.
7076 Discr := First_Stored_Discriminant (Base_Type (Typ));
7077 while Present (Discr) loop
7078 Num_Gird := Num_Gird + 1;
7079 Next_Stored_Discriminant (Discr);
7082 -- Case of more stored discriminants than new discriminants
7084 if Num_Gird > Num_Disc then
7086 -- Create a proper subtype of the parent type, which is the
7087 -- proper implementation type for the aggregate, and convert
7088 -- it to the intended target type.
7090 Discr := First_Stored_Discriminant (Base_Type (Typ));
7091 while Present (Discr) loop
7094 (Get_Discriminant_Value
7097 Discriminant_Constraint (Typ)));
7099 Append (New_Comp, Constraints);
7100 Next_Stored_Discriminant (Discr);
7104 Make_Subtype_Declaration (Loc,
7105 Defining_Identifier => Make_Temporary (Loc, 'T'),
7106 Subtype_Indication =>
7107 Make_Subtype_Indication (Loc,
7109 New_Occurrence_Of (Etype (Base_Type (Typ)), Loc),
7111 Make_Index_Or_Discriminant_Constraint
7112 (Loc, Constraints)));
7114 Insert_Action (N, Decl);
7115 Prepend_Stored_Values (Base_Type (Typ));
7117 Set_Etype (N, Defining_Identifier (Decl));
7120 Rewrite (N, Unchecked_Convert_To (Typ, N));
7123 -- Case where we do not have fewer new discriminants than
7124 -- stored discriminants, so in this case we can simply use the
7125 -- stored discriminants of the subtype.
7128 Prepend_Stored_Values (Typ);
7130 end Generate_Aggregate_For_Derived_Type;
7133 if Is_Tagged_Type (Typ) then
7135 -- In the tagged case, _parent and _tag component must be created
7137 -- Reset Null_Present unconditionally. Tagged records always have
7138 -- at least one field (the tag or the parent).
7140 Set_Null_Record_Present (N, False);
7142 -- When the current aggregate comes from the expansion of an
7143 -- extension aggregate, the parent expr is replaced by an
7144 -- aggregate formed by selected components of this expr.
7146 if Present (Parent_Expr) and then Is_Empty_List (Comps) then
7147 Comp := First_Component_Or_Discriminant (Typ);
7148 while Present (Comp) loop
7150 -- Skip all expander-generated components
7152 if not Comes_From_Source (Original_Record_Component (Comp))
7158 Make_Selected_Component (Loc,
7160 Unchecked_Convert_To (Typ,
7161 Duplicate_Subexpr (Parent_Expr, True)),
7162 Selector_Name => New_Occurrence_Of (Comp, Loc));
7165 Make_Component_Association (Loc,
7166 Choices => New_List (
7167 New_Occurrence_Of (Comp, Loc)),
7168 Expression => New_Comp));
7170 Analyze_And_Resolve (New_Comp, Etype (Comp));
7173 Next_Component_Or_Discriminant (Comp);
7177 -- Compute the value for the Tag now, if the type is a root it
7178 -- will be included in the aggregate right away, otherwise it will
7179 -- be propagated to the parent aggregate.
7181 if Present (Orig_Tag) then
7182 Tag_Value := Orig_Tag;
7184 elsif not Tagged_Type_Expansion then
7190 (Node (First_Elmt (Access_Disp_Table (Typ))), Loc);
7193 -- For a derived type, an aggregate for the parent is formed with
7194 -- all the inherited components.
7196 if Is_Derived_Type (Typ) then
7198 First_Comp : Node_Id;
7199 Parent_Comps : List_Id;
7200 Parent_Aggr : Node_Id;
7201 Parent_Name : Node_Id;
7204 -- Remove the inherited component association from the
7205 -- aggregate and store them in the parent aggregate
7207 First_Comp := First (Component_Associations (N));
7208 Parent_Comps := New_List;
7209 while Present (First_Comp)
7211 Scope (Original_Record_Component
7212 (Entity (First (Choices (First_Comp))))) /=
7218 Append (Comp, Parent_Comps);
7222 Make_Aggregate (Loc,
7223 Component_Associations => Parent_Comps);
7224 Set_Etype (Parent_Aggr, Etype (Base_Type (Typ)));
7226 -- Find the _parent component
7228 Comp := First_Component (Typ);
7229 while Chars (Comp) /= Name_uParent loop
7230 Comp := Next_Component (Comp);
7233 Parent_Name := New_Occurrence_Of (Comp, Loc);
7235 -- Insert the parent aggregate
7237 Prepend_To (Component_Associations (N),
7238 Make_Component_Association (Loc,
7239 Choices => New_List (Parent_Name),
7240 Expression => Parent_Aggr));
7242 -- Expand recursively the parent propagating the right Tag
7244 Expand_Record_Aggregate
7245 (Parent_Aggr, Tag_Value, Parent_Expr);
7247 -- The ancestor part may be a nested aggregate that has
7248 -- delayed expansion: recheck now.
7250 if not Component_OK_For_Backend then
7251 Convert_To_Assignments (N, Typ);
7255 -- For a root type, the tag component is added (unless compiling
7256 -- for the VMs, where tags are implicit).
7258 elsif Tagged_Type_Expansion then
7260 Tag_Name : constant Node_Id :=
7262 (First_Tag_Component (Typ), Loc);
7263 Typ_Tag : constant Entity_Id := RTE (RE_Tag);
7264 Conv_Node : constant Node_Id :=
7265 Unchecked_Convert_To (Typ_Tag, Tag_Value);
7268 Set_Etype (Conv_Node, Typ_Tag);
7269 Prepend_To (Component_Associations (N),
7270 Make_Component_Association (Loc,
7271 Choices => New_List (Tag_Name),
7272 Expression => Conv_Node));
7276 end Build_Back_End_Aggregate;
7278 ----------------------------------------
7279 -- Compile_Time_Known_Composite_Value --
7280 ----------------------------------------
7282 function Compile_Time_Known_Composite_Value
7283 (N : Node_Id) return Boolean
7286 -- If we have an entity name, then see if it is the name of a
7287 -- constant and if so, test the corresponding constant value.
7289 if Is_Entity_Name (N) then
7291 E : constant Entity_Id := Entity (N);
7294 if Ekind (E) /= E_Constant then
7297 V := Constant_Value (E);
7299 and then Compile_Time_Known_Composite_Value (V);
7303 -- We have a value, see if it is compile time known
7306 if Nkind (N) = N_Aggregate then
7307 return Compile_Time_Known_Aggregate (N);
7310 -- All other types of values are not known at compile time
7315 end Compile_Time_Known_Composite_Value;
7317 ------------------------------
7318 -- Component_OK_For_Backend --
7319 ------------------------------
7321 function Component_OK_For_Backend return Boolean is
7331 while Present (C) loop
7333 -- If the component has box initialization, expansion is needed
7334 -- and component is not ready for backend.
7336 if Box_Present (C) then
7340 if Nkind (Expression (C)) = N_Qualified_Expression then
7341 Expr_Q := Expression (Expression (C));
7343 Expr_Q := Expression (C);
7346 -- Return False for array components whose bounds raise
7347 -- constraint error.
7350 Comp : constant Entity_Id := First (Choices (C));
7354 if Present (Etype (Comp))
7355 and then Is_Array_Type (Etype (Comp))
7357 Indx := First_Index (Etype (Comp));
7358 while Present (Indx) loop
7359 if Nkind (Type_Low_Bound (Etype (Indx))) =
7360 N_Raise_Constraint_Error
7361 or else Nkind (Type_High_Bound (Etype (Indx))) =
7362 N_Raise_Constraint_Error
7367 Indx := Next_Index (Indx);
7372 -- Return False if the aggregate has any associations for tagged
7373 -- components that may require tag adjustment.
7375 -- These are cases where the source expression may have a tag that
7376 -- could differ from the component tag (e.g., can occur for type
7377 -- conversions and formal parameters). (Tag adjustment not needed
7378 -- if Tagged_Type_Expansion because object tags are implicit in
7381 if Is_Tagged_Type (Etype (Expr_Q))
7383 (Nkind (Expr_Q) = N_Type_Conversion
7385 (Is_Entity_Name (Expr_Q)
7386 and then Is_Formal (Entity (Expr_Q))))
7387 and then Tagged_Type_Expansion
7389 Static_Components := False;
7392 elsif Is_Delayed_Aggregate (Expr_Q) then
7393 Static_Components := False;
7396 elsif Nkind (Expr_Q) = N_Quantified_Expression then
7397 Static_Components := False;
7400 elsif Possible_Bit_Aligned_Component (Expr_Q) then
7401 Static_Components := False;
7404 elsif Modify_Tree_For_C
7405 and then Nkind (C) = N_Component_Association
7406 and then Has_Per_Object_Constraint (Choices (C))
7408 Static_Components := False;
7411 elsif Modify_Tree_For_C
7412 and then Nkind (Expr_Q) = N_Identifier
7413 and then Is_Array_Type (Etype (Expr_Q))
7415 Static_Components := False;
7418 elsif Modify_Tree_For_C
7419 and then Nkind (Expr_Q) = N_Type_Conversion
7420 and then Is_Array_Type (Etype (Expr_Q))
7422 Static_Components := False;
7426 if Is_Elementary_Type (Etype (Expr_Q)) then
7427 if not Compile_Time_Known_Value (Expr_Q) then
7428 Static_Components := False;
7431 elsif not Compile_Time_Known_Composite_Value (Expr_Q) then
7432 Static_Components := False;
7434 if Is_Private_Type (Etype (Expr_Q))
7435 and then Has_Discriminants (Etype (Expr_Q))
7445 end Component_OK_For_Backend;
7447 -------------------------------
7448 -- Has_Per_Object_Constraint --
7449 -------------------------------
7451 function Has_Per_Object_Constraint (L : List_Id) return Boolean is
7452 N : Node_Id := First (L);
7454 while Present (N) loop
7455 if Is_Entity_Name (N)
7456 and then Present (Entity (N))
7457 and then Has_Per_Object_Constraint (Entity (N))
7466 end Has_Per_Object_Constraint;
7468 -----------------------------------
7469 -- Has_Visible_Private_Ancestor --
7470 -----------------------------------
7472 function Has_Visible_Private_Ancestor (Id : E) return Boolean is
7473 R : constant Entity_Id := Root_Type (Id);
7474 T1 : Entity_Id := Id;
7478 if Is_Private_Type (T1) then
7488 end Has_Visible_Private_Ancestor;
7490 -------------------------
7491 -- Top_Level_Aggregate --
7492 -------------------------
7494 function Top_Level_Aggregate (N : Node_Id) return Node_Id is
7499 while Present (Parent (Aggr))
7500 and then Nkind_In (Parent (Aggr), N_Aggregate,
7501 N_Component_Association)
7503 Aggr := Parent (Aggr);
7507 end Top_Level_Aggregate;
7511 Top_Level_Aggr : constant Node_Id := Top_Level_Aggregate (N);
7513 -- Start of processing for Expand_Record_Aggregate
7516 -- If the aggregate is to be assigned to an atomic/VFA variable, we have
7517 -- to prevent a piecemeal assignment even if the aggregate is to be
7518 -- expanded. We create a temporary for the aggregate, and assign the
7519 -- temporary instead, so that the back end can generate an atomic move
7522 if Is_Atomic_VFA_Aggregate (N) then
7525 -- No special management required for aggregates used to initialize
7526 -- statically allocated dispatch tables
7528 elsif Is_Static_Dispatch_Table_Aggregate (N) then
7532 -- If the pragma Aggregate_Individually_Assign is set, always convert to
7535 if Aggregate_Individually_Assign then
7536 Convert_To_Assignments (N, Typ);
7538 -- Ada 2005 (AI-318-2): We need to convert to assignments if components
7539 -- are build-in-place function calls. The assignments will each turn
7540 -- into a build-in-place function call. If components are all static,
7541 -- we can pass the aggregate to the back end regardless of limitedness.
7543 -- Extension aggregates, aggregates in extended return statements, and
7544 -- aggregates for C++ imported types must be expanded.
7546 elsif Ada_Version >= Ada_2005 and then Is_Limited_View (Typ) then
7547 if not Nkind_In (Parent (N), N_Component_Association,
7548 N_Object_Declaration)
7550 Convert_To_Assignments (N, Typ);
7552 elsif Nkind (N) = N_Extension_Aggregate
7553 or else Convention (Typ) = Convention_CPP
7555 Convert_To_Assignments (N, Typ);
7557 elsif not Size_Known_At_Compile_Time (Typ)
7558 or else not Component_OK_For_Backend
7559 or else not Static_Components
7561 Convert_To_Assignments (N, Typ);
7563 -- In all other cases, build a proper aggregate to be handled by
7567 Build_Back_End_Aggregate;
7570 -- Gigi doesn't properly handle temporaries of variable size so we
7571 -- generate it in the front-end
7573 elsif not Size_Known_At_Compile_Time (Typ)
7574 and then Tagged_Type_Expansion
7576 Convert_To_Assignments (N, Typ);
7578 -- An aggregate used to initialize a controlled object must be turned
7579 -- into component assignments as the components themselves may require
7580 -- finalization actions such as adjustment.
7582 elsif Needs_Finalization (Typ) then
7583 Convert_To_Assignments (N, Typ);
7585 -- Ada 2005 (AI-287): In case of default initialized components we
7586 -- convert the aggregate into assignments.
7588 elsif Has_Default_Init_Comps (N) then
7589 Convert_To_Assignments (N, Typ);
7593 elsif not Component_OK_For_Backend then
7594 Convert_To_Assignments (N, Typ);
7596 -- If an ancestor is private, some components are not inherited and we
7597 -- cannot expand into a record aggregate.
7599 elsif Has_Visible_Private_Ancestor (Typ) then
7600 Convert_To_Assignments (N, Typ);
7602 -- ??? The following was done to compile fxacc00.ads in the ACVCs. Gigi
7603 -- is not able to handle the aggregate for Late_Request.
7605 elsif Is_Tagged_Type (Typ) and then Has_Discriminants (Typ) then
7606 Convert_To_Assignments (N, Typ);
7608 -- If the tagged types covers interface types we need to initialize all
7609 -- hidden components containing pointers to secondary dispatch tables.
7611 elsif Is_Tagged_Type (Typ) and then Has_Interfaces (Typ) then
7612 Convert_To_Assignments (N, Typ);
7614 -- If some components are mutable, the size of the aggregate component
7615 -- may be distinct from the default size of the type component, so
7616 -- we need to expand to insure that the back-end copies the proper
7617 -- size of the data. However, if the aggregate is the initial value of
7618 -- a constant, the target is immutable and might be built statically
7619 -- if components are appropriate.
7621 elsif Has_Mutable_Components (Typ)
7623 (Nkind (Parent (Top_Level_Aggr)) /= N_Object_Declaration
7624 or else not Constant_Present (Parent (Top_Level_Aggr))
7625 or else not Static_Components)
7627 Convert_To_Assignments (N, Typ);
7629 -- If the type involved has bit aligned components, then we are not sure
7630 -- that the back end can handle this case correctly.
7632 elsif Type_May_Have_Bit_Aligned_Components (Typ) then
7633 Convert_To_Assignments (N, Typ);
7635 -- When generating C, only generate an aggregate when declaring objects
7636 -- since C does not support aggregates in e.g. assignment statements.
7638 elsif Modify_Tree_For_C and then not Is_CCG_Supported_Aggregate (N) then
7639 Convert_To_Assignments (N, Typ);
7641 -- In all other cases, build a proper aggregate to be handled by gigi
7644 Build_Back_End_Aggregate;
7646 end Expand_Record_Aggregate;
7648 ----------------------------
7649 -- Has_Default_Init_Comps --
7650 ----------------------------
7652 function Has_Default_Init_Comps (N : Node_Id) return Boolean is
7653 Comps : constant List_Id := Component_Associations (N);
7658 pragma Assert (Nkind_In (N, N_Aggregate, N_Extension_Aggregate));
7664 if Has_Self_Reference (N) then
7668 -- Check if any direct component has default initialized components
7671 while Present (C) loop
7672 if Box_Present (C) then
7679 -- Recursive call in case of aggregate expression
7682 while Present (C) loop
7683 Expr := Expression (C);
7686 and then Nkind_In (Expr, N_Aggregate, N_Extension_Aggregate)
7687 and then Has_Default_Init_Comps (Expr)
7696 end Has_Default_Init_Comps;
7698 ----------------------------------------
7699 -- Is_Build_In_Place_Aggregate_Return --
7700 ----------------------------------------
7702 function Is_Build_In_Place_Aggregate_Return (N : Node_Id) return Boolean is
7703 P : Node_Id := Parent (N);
7706 while Nkind (P) = N_Qualified_Expression loop
7710 if Nkind (P) = N_Simple_Return_Statement then
7713 elsif Nkind (Parent (P)) = N_Extended_Return_Statement then
7721 Is_Build_In_Place_Function
7722 (Return_Applies_To (Return_Statement_Entity (P)));
7723 end Is_Build_In_Place_Aggregate_Return;
7725 --------------------------
7726 -- Is_Delayed_Aggregate --
7727 --------------------------
7729 function Is_Delayed_Aggregate (N : Node_Id) return Boolean is
7730 Node : Node_Id := N;
7731 Kind : Node_Kind := Nkind (Node);
7734 if Kind = N_Qualified_Expression then
7735 Node := Expression (Node);
7736 Kind := Nkind (Node);
7739 if not Nkind_In (Kind, N_Aggregate, N_Extension_Aggregate) then
7742 return Expansion_Delayed (Node);
7744 end Is_Delayed_Aggregate;
7746 --------------------------------
7747 -- Is_CCG_Supported_Aggregate --
7748 --------------------------------
7750 function Is_CCG_Supported_Aggregate
7751 (N : Node_Id) return Boolean
7753 P : Node_Id := Parent (N);
7756 -- Aggregates are not supported for nonstandard rep clauses, since they
7757 -- may lead to extra padding fields in CCG.
7759 if Ekind (Etype (N)) in Record_Kind
7760 and then Has_Non_Standard_Rep (Etype (N))
7765 while Present (P) and then Nkind (P) = N_Aggregate loop
7769 -- Check cases where aggregates are supported by the CCG backend
7771 if Nkind (P) = N_Object_Declaration then
7773 P_Typ : constant Entity_Id := Etype (Defining_Identifier (P));
7776 if Is_Record_Type (P_Typ) then
7779 return Compile_Time_Known_Bounds (P_Typ);
7783 elsif Nkind (P) = N_Qualified_Expression then
7784 if Nkind (Parent (P)) = N_Object_Declaration then
7786 P_Typ : constant Entity_Id :=
7787 Etype (Defining_Identifier (Parent (P)));
7789 if Is_Record_Type (P_Typ) then
7792 return Compile_Time_Known_Bounds (P_Typ);
7796 elsif Nkind (Parent (P)) = N_Allocator then
7802 end Is_CCG_Supported_Aggregate;
7804 ----------------------------------------
7805 -- Is_Static_Dispatch_Table_Aggregate --
7806 ----------------------------------------
7808 function Is_Static_Dispatch_Table_Aggregate (N : Node_Id) return Boolean is
7809 Typ : constant Entity_Id := Base_Type (Etype (N));
7812 return Building_Static_Dispatch_Tables
7813 and then Tagged_Type_Expansion
7814 and then RTU_Loaded (Ada_Tags)
7816 -- Avoid circularity when rebuilding the compiler
7818 and then Cunit_Entity (Get_Source_Unit (N)) /= RTU_Entity (Ada_Tags)
7819 and then (Typ = RTE (RE_Dispatch_Table_Wrapper)
7821 Typ = RTE (RE_Address_Array)
7823 Typ = RTE (RE_Type_Specific_Data)
7825 Typ = RTE (RE_Tag_Table)
7827 (RTE_Available (RE_Interface_Data)
7828 and then Typ = RTE (RE_Interface_Data))
7830 (RTE_Available (RE_Interfaces_Array)
7831 and then Typ = RTE (RE_Interfaces_Array))
7833 (RTE_Available (RE_Interface_Data_Element)
7834 and then Typ = RTE (RE_Interface_Data_Element)));
7835 end Is_Static_Dispatch_Table_Aggregate;
7837 -----------------------------
7838 -- Is_Two_Dim_Packed_Array --
7839 -----------------------------
7841 function Is_Two_Dim_Packed_Array (Typ : Entity_Id) return Boolean is
7842 C : constant Int := UI_To_Int (Component_Size (Typ));
7844 return Number_Dimensions (Typ) = 2
7845 and then Is_Bit_Packed_Array (Typ)
7846 and then (C = 1 or else C = 2 or else C = 4);
7847 end Is_Two_Dim_Packed_Array;
7849 --------------------
7850 -- Late_Expansion --
7851 --------------------
7853 function Late_Expansion
7856 Target : Node_Id) return List_Id
7858 Aggr_Code : List_Id;
7861 if Is_Array_Type (Etype (N)) then
7863 Build_Array_Aggr_Code
7865 Ctype => Component_Type (Etype (N)),
7866 Index => First_Index (Typ),
7868 Scalar_Comp => Is_Scalar_Type (Component_Type (Typ)),
7869 Indexes => No_List);
7871 -- Directly or indirectly (e.g. access protected procedure) a record
7874 Aggr_Code := Build_Record_Aggr_Code (N, Typ, Target);
7877 -- Save the last assignment statement associated with the aggregate
7878 -- when building a controlled object. This reference is utilized by
7879 -- the finalization machinery when marking an object as successfully
7882 if Needs_Finalization (Typ)
7883 and then Is_Entity_Name (Target)
7884 and then Present (Entity (Target))
7885 and then Ekind_In (Entity (Target), E_Constant, E_Variable)
7887 Set_Last_Aggregate_Assignment (Entity (Target), Last (Aggr_Code));
7893 ----------------------------------
7894 -- Make_OK_Assignment_Statement --
7895 ----------------------------------
7897 function Make_OK_Assignment_Statement
7900 Expression : Node_Id) return Node_Id
7903 Set_Assignment_OK (Name);
7904 return Make_Assignment_Statement (Sloc, Name, Expression);
7905 end Make_OK_Assignment_Statement;
7907 -----------------------
7908 -- Number_Of_Choices --
7909 -----------------------
7911 function Number_Of_Choices (N : Node_Id) return Nat is
7915 Nb_Choices : Nat := 0;
7918 if Present (Expressions (N)) then
7922 Assoc := First (Component_Associations (N));
7923 while Present (Assoc) loop
7924 Choice := First (Choice_List (Assoc));
7925 while Present (Choice) loop
7926 if Nkind (Choice) /= N_Others_Choice then
7927 Nb_Choices := Nb_Choices + 1;
7937 end Number_Of_Choices;
7939 ------------------------------------
7940 -- Packed_Array_Aggregate_Handled --
7941 ------------------------------------
7943 -- The current version of this procedure will handle at compile time
7944 -- any array aggregate that meets these conditions:
7946 -- One and two dimensional, bit packed
7947 -- Underlying packed type is modular type
7948 -- Bounds are within 32-bit Int range
7949 -- All bounds and values are static
7951 -- Note: for now, in the 2-D case, we only handle component sizes of
7952 -- 1, 2, 4 (cases where an integral number of elements occupies a byte).
7954 function Packed_Array_Aggregate_Handled (N : Node_Id) return Boolean is
7955 Loc : constant Source_Ptr := Sloc (N);
7956 Typ : constant Entity_Id := Etype (N);
7957 Ctyp : constant Entity_Id := Component_Type (Typ);
7959 Not_Handled : exception;
7960 -- Exception raised if this aggregate cannot be handled
7963 -- Handle one- or two dimensional bit packed array
7965 if not Is_Bit_Packed_Array (Typ)
7966 or else Number_Dimensions (Typ) > 2
7971 -- If two-dimensional, check whether it can be folded, and transformed
7972 -- into a one-dimensional aggregate for the Packed_Array_Impl_Type of
7973 -- the original type.
7975 if Number_Dimensions (Typ) = 2 then
7976 return Two_Dim_Packed_Array_Handled (N);
7979 if not Is_Modular_Integer_Type (Packed_Array_Impl_Type (Typ)) then
7983 if not Is_Scalar_Type (Ctyp) then
7988 Csiz : constant Nat := UI_To_Int (Component_Size (Typ));
7992 -- Bounds of index type
7996 -- Values of bounds if compile time known
7998 function Get_Component_Val (N : Node_Id) return Uint;
7999 -- Given a expression value N of the component type Ctyp, returns a
8000 -- value of Csiz (component size) bits representing this value. If
8001 -- the value is nonstatic or any other reason exists why the value
8002 -- cannot be returned, then Not_Handled is raised.
8004 -----------------------
8005 -- Get_Component_Val --
8006 -----------------------
8008 function Get_Component_Val (N : Node_Id) return Uint is
8012 -- We have to analyze the expression here before doing any further
8013 -- processing here. The analysis of such expressions is deferred
8014 -- till expansion to prevent some problems of premature analysis.
8016 Analyze_And_Resolve (N, Ctyp);
8018 -- Must have a compile time value. String literals have to be
8019 -- converted into temporaries as well, because they cannot easily
8020 -- be converted into their bit representation.
8022 if not Compile_Time_Known_Value (N)
8023 or else Nkind (N) = N_String_Literal
8028 Val := Expr_Rep_Value (N);
8030 -- Adjust for bias, and strip proper number of bits
8032 if Has_Biased_Representation (Ctyp) then
8033 Val := Val - Expr_Value (Type_Low_Bound (Ctyp));
8036 return Val mod Uint_2 ** Csiz;
8037 end Get_Component_Val;
8039 -- Here we know we have a one dimensional bit packed array
8042 Get_Index_Bounds (First_Index (Typ), Lo, Hi);
8044 -- Cannot do anything if bounds are dynamic
8046 if not Compile_Time_Known_Value (Lo)
8048 not Compile_Time_Known_Value (Hi)
8053 -- Or are silly out of range of int bounds
8055 Lob := Expr_Value (Lo);
8056 Hib := Expr_Value (Hi);
8058 if not UI_Is_In_Int_Range (Lob)
8060 not UI_Is_In_Int_Range (Hib)
8065 -- At this stage we have a suitable aggregate for handling at compile
8066 -- time. The only remaining checks are that the values of expressions
8067 -- in the aggregate are compile-time known (checks are performed by
8068 -- Get_Component_Val), and that any subtypes or ranges are statically
8071 -- If the aggregate is not fully positional at this stage, then
8072 -- convert it to positional form. Either this will fail, in which
8073 -- case we can do nothing, or it will succeed, in which case we have
8074 -- succeeded in handling the aggregate and transforming it into a
8075 -- modular value, or it will stay an aggregate, in which case we
8076 -- have failed to create a packed value for it.
8078 if Present (Component_Associations (N)) then
8079 Convert_To_Positional
8080 (N, Max_Others_Replicate => 64, Handle_Bit_Packed => True);
8081 return Nkind (N) /= N_Aggregate;
8084 -- Otherwise we are all positional, so convert to proper value
8087 Lov : constant Int := UI_To_Int (Lob);
8088 Hiv : constant Int := UI_To_Int (Hib);
8090 Len : constant Nat := Int'Max (0, Hiv - Lov + 1);
8091 -- The length of the array (number of elements)
8093 Aggregate_Val : Uint;
8094 -- Value of aggregate. The value is set in the low order bits of
8095 -- this value. For the little-endian case, the values are stored
8096 -- from low-order to high-order and for the big-endian case the
8097 -- values are stored from high-order to low-order. Note that gigi
8098 -- will take care of the conversions to left justify the value in
8099 -- the big endian case (because of left justified modular type
8100 -- processing), so we do not have to worry about that here.
8103 -- Integer literal for resulting constructed value
8106 -- Shift count from low order for next value
8109 -- Shift increment for loop
8112 -- Next expression from positional parameters of aggregate
8114 Left_Justified : Boolean;
8115 -- Set True if we are filling the high order bits of the target
8116 -- value (i.e. the value is left justified).
8119 -- For little endian, we fill up the low order bits of the target
8120 -- value. For big endian we fill up the high order bits of the
8121 -- target value (which is a left justified modular value).
8123 Left_Justified := Bytes_Big_Endian;
8125 -- Switch justification if using -gnatd8
8127 if Debug_Flag_8 then
8128 Left_Justified := not Left_Justified;
8131 -- Switch justfification if reverse storage order
8133 if Reverse_Storage_Order (Base_Type (Typ)) then
8134 Left_Justified := not Left_Justified;
8137 if Left_Justified then
8138 Shift := Csiz * (Len - 1);
8145 -- Loop to set the values
8148 Aggregate_Val := Uint_0;
8150 Expr := First (Expressions (N));
8151 Aggregate_Val := Get_Component_Val (Expr) * Uint_2 ** Shift;
8153 for J in 2 .. Len loop
8154 Shift := Shift + Incr;
8157 Aggregate_Val + Get_Component_Val (Expr) * Uint_2 ** Shift;
8161 -- Now we can rewrite with the proper value
8163 Lit := Make_Integer_Literal (Loc, Intval => Aggregate_Val);
8164 Set_Print_In_Hex (Lit);
8166 -- Construct the expression using this literal. Note that it is
8167 -- important to qualify the literal with its proper modular type
8168 -- since universal integer does not have the required range and
8169 -- also this is a left justified modular type, which is important
8170 -- in the big-endian case.
8173 Unchecked_Convert_To (Typ,
8174 Make_Qualified_Expression (Loc,
8176 New_Occurrence_Of (Packed_Array_Impl_Type (Typ), Loc),
8177 Expression => Lit)));
8179 Analyze_And_Resolve (N, Typ);
8187 end Packed_Array_Aggregate_Handled;
8189 ----------------------------
8190 -- Has_Mutable_Components --
8191 ----------------------------
8193 function Has_Mutable_Components (Typ : Entity_Id) return Boolean is
8198 Comp := First_Component (Typ);
8199 while Present (Comp) loop
8200 Ctyp := Underlying_Type (Etype (Comp));
8201 if Is_Record_Type (Ctyp)
8202 and then Has_Discriminants (Ctyp)
8203 and then not Is_Constrained (Ctyp)
8208 Next_Component (Comp);
8212 end Has_Mutable_Components;
8214 ------------------------------
8215 -- Initialize_Discriminants --
8216 ------------------------------
8218 procedure Initialize_Discriminants (N : Node_Id; Typ : Entity_Id) is
8219 Loc : constant Source_Ptr := Sloc (N);
8220 Bas : constant Entity_Id := Base_Type (Typ);
8221 Par : constant Entity_Id := Etype (Bas);
8222 Decl : constant Node_Id := Parent (Par);
8226 if Is_Tagged_Type (Bas)
8227 and then Is_Derived_Type (Bas)
8228 and then Has_Discriminants (Par)
8229 and then Has_Discriminants (Bas)
8230 and then Number_Discriminants (Bas) /= Number_Discriminants (Par)
8231 and then Nkind (Decl) = N_Full_Type_Declaration
8232 and then Nkind (Type_Definition (Decl)) = N_Record_Definition
8234 Present (Variant_Part (Component_List (Type_Definition (Decl))))
8235 and then Nkind (N) /= N_Extension_Aggregate
8238 -- Call init proc to set discriminants.
8239 -- There should eventually be a special procedure for this ???
8241 Ref := New_Occurrence_Of (Defining_Identifier (N), Loc);
8242 Insert_Actions_After (N,
8243 Build_Initialization_Call (Sloc (N), Ref, Typ));
8245 end Initialize_Discriminants;
8252 (Obj_Type : Entity_Id;
8253 Typ : Entity_Id) return Boolean
8255 L1, L2, H1, H2 : Node_Id;
8258 -- No sliding if the type of the object is not established yet, if it is
8259 -- an unconstrained type whose actual subtype comes from the aggregate,
8260 -- or if the two types are identical.
8262 if not Is_Array_Type (Obj_Type) then
8265 elsif not Is_Constrained (Obj_Type) then
8268 elsif Typ = Obj_Type then
8272 -- Sliding can only occur along the first dimension
8274 Get_Index_Bounds (First_Index (Typ), L1, H1);
8275 Get_Index_Bounds (First_Index (Obj_Type), L2, H2);
8277 if not Is_OK_Static_Expression (L1) or else
8278 not Is_OK_Static_Expression (L2) or else
8279 not Is_OK_Static_Expression (H1) or else
8280 not Is_OK_Static_Expression (H2)
8284 return Expr_Value (L1) /= Expr_Value (L2)
8286 Expr_Value (H1) /= Expr_Value (H2);
8291 ---------------------------------
8292 -- Process_Transient_Component --
8293 ---------------------------------
8295 procedure Process_Transient_Component
8297 Comp_Typ : Entity_Id;
8298 Init_Expr : Node_Id;
8299 Fin_Call : out Node_Id;
8300 Hook_Clear : out Node_Id;
8301 Aggr : Node_Id := Empty;
8302 Stmts : List_Id := No_List)
8304 procedure Add_Item (Item : Node_Id);
8305 -- Insert arbitrary node Item into the tree depending on the values of
8312 procedure Add_Item (Item : Node_Id) is
8314 if Present (Aggr) then
8315 Insert_Action (Aggr, Item);
8317 pragma Assert (Present (Stmts));
8318 Append_To (Stmts, Item);
8324 Hook_Assign : Node_Id;
8325 Hook_Decl : Node_Id;
8329 Res_Typ : Entity_Id;
8331 -- Start of processing for Process_Transient_Component
8334 -- Add the access type, which provides a reference to the function
8335 -- result. Generate:
8337 -- type Res_Typ is access all Comp_Typ;
8339 Res_Typ := Make_Temporary (Loc, 'A');
8340 Set_Ekind (Res_Typ, E_General_Access_Type);
8341 Set_Directly_Designated_Type (Res_Typ, Comp_Typ);
8344 (Make_Full_Type_Declaration (Loc,
8345 Defining_Identifier => Res_Typ,
8347 Make_Access_To_Object_Definition (Loc,
8348 All_Present => True,
8349 Subtype_Indication => New_Occurrence_Of (Comp_Typ, Loc))));
8351 -- Add the temporary which captures the result of the function call.
8354 -- Res : constant Res_Typ := Init_Expr'Reference;
8356 -- Note that this temporary is effectively a transient object because
8357 -- its lifetime is bounded by the current array or record component.
8359 Res_Id := Make_Temporary (Loc, 'R');
8360 Set_Ekind (Res_Id, E_Constant);
8361 Set_Etype (Res_Id, Res_Typ);
8363 -- Mark the transient object as successfully processed to avoid double
8366 Set_Is_Finalized_Transient (Res_Id);
8368 -- Signal the general finalization machinery that this transient object
8369 -- should not be considered for finalization actions because its cleanup
8370 -- will be performed by Process_Transient_Component_Completion.
8372 Set_Is_Ignored_Transient (Res_Id);
8375 Make_Object_Declaration (Loc,
8376 Defining_Identifier => Res_Id,
8377 Constant_Present => True,
8378 Object_Definition => New_Occurrence_Of (Res_Typ, Loc),
8380 Make_Reference (Loc, New_Copy_Tree (Init_Expr)));
8382 Add_Item (Res_Decl);
8384 -- Construct all pieces necessary to hook and finalize the transient
8387 Build_Transient_Object_Statements
8388 (Obj_Decl => Res_Decl,
8389 Fin_Call => Fin_Call,
8390 Hook_Assign => Hook_Assign,
8391 Hook_Clear => Hook_Clear,
8392 Hook_Decl => Hook_Decl,
8393 Ptr_Decl => Ptr_Decl);
8395 -- Add the access type which provides a reference to the transient
8396 -- result. Generate:
8398 -- type Ptr_Typ is access all Comp_Typ;
8400 Add_Item (Ptr_Decl);
8402 -- Add the temporary which acts as a hook to the transient result.
8405 -- Hook : Ptr_Typ := null;
8407 Add_Item (Hook_Decl);
8409 -- Attach the transient result to the hook. Generate:
8411 -- Hook := Ptr_Typ (Res);
8413 Add_Item (Hook_Assign);
8415 -- The original initialization expression now references the value of
8416 -- the temporary function result. Generate:
8421 Make_Explicit_Dereference (Loc,
8422 Prefix => New_Occurrence_Of (Res_Id, Loc)));
8423 end Process_Transient_Component;
8425 --------------------------------------------
8426 -- Process_Transient_Component_Completion --
8427 --------------------------------------------
8429 procedure Process_Transient_Component_Completion
8433 Hook_Clear : Node_Id;
8436 Exceptions_OK : constant Boolean :=
8437 not Restriction_Active (No_Exception_Propagation);
8440 pragma Assert (Present (Hook_Clear));
8442 -- Generate the following code if exception propagation is allowed:
8445 -- Abort : constant Boolean := Triggered_By_Abort;
8447 -- Abort : constant Boolean := False; -- no abort
8449 -- E : Exception_Occurrence;
8450 -- Raised : Boolean := False;
8457 -- [Deep_]Finalize (Res.all);
8461 -- if not Raised then
8463 -- Save_Occurrence (E,
8464 -- Get_Curent_Excep.all.all);
8470 -- if Raised and then not Abort then
8471 -- Raise_From_Controlled_Operation (E);
8475 if Exceptions_OK then
8476 Abort_And_Exception : declare
8477 Blk_Decls : constant List_Id := New_List;
8478 Blk_Stmts : constant List_Id := New_List;
8479 Fin_Stmts : constant List_Id := New_List;
8481 Fin_Data : Finalization_Exception_Data;
8484 -- Create the declarations of the two flags and the exception
8487 Build_Object_Declarations (Fin_Data, Blk_Decls, Loc);
8492 if Abort_Allowed then
8493 Append_To (Blk_Stmts,
8494 Build_Runtime_Call (Loc, RE_Abort_Defer));
8497 -- Wrap the hook clear and the finalization call in order to trap
8498 -- a potential exception.
8500 Append_To (Fin_Stmts, Hook_Clear);
8502 if Present (Fin_Call) then
8503 Append_To (Fin_Stmts, Fin_Call);
8506 Append_To (Blk_Stmts,
8507 Make_Block_Statement (Loc,
8508 Handled_Statement_Sequence =>
8509 Make_Handled_Sequence_Of_Statements (Loc,
8510 Statements => Fin_Stmts,
8511 Exception_Handlers => New_List (
8512 Build_Exception_Handler (Fin_Data)))));
8517 if Abort_Allowed then
8518 Append_To (Blk_Stmts,
8519 Build_Runtime_Call (Loc, RE_Abort_Undefer));
8522 -- Reraise the potential exception with a proper "upgrade" to
8523 -- Program_Error if needed.
8525 Append_To (Blk_Stmts, Build_Raise_Statement (Fin_Data));
8527 -- Wrap everything in a block
8530 Make_Block_Statement (Loc,
8531 Declarations => Blk_Decls,
8532 Handled_Statement_Sequence =>
8533 Make_Handled_Sequence_Of_Statements (Loc,
8534 Statements => Blk_Stmts)));
8535 end Abort_And_Exception;
8537 -- Generate the following code if exception propagation is not allowed
8538 -- and aborts are allowed:
8543 -- [Deep_]Finalize (Res.all);
8545 -- Abort_Undefer_Direct;
8548 elsif Abort_Allowed then
8549 Abort_Only : declare
8550 Blk_Stmts : constant List_Id := New_List;
8553 Append_To (Blk_Stmts, Build_Runtime_Call (Loc, RE_Abort_Defer));
8554 Append_To (Blk_Stmts, Hook_Clear);
8556 if Present (Fin_Call) then
8557 Append_To (Blk_Stmts, Fin_Call);
8561 Build_Abort_Undefer_Block (Loc,
8566 -- Otherwise generate:
8569 -- [Deep_]Finalize (Res.all);
8572 Append_To (Stmts, Hook_Clear);
8574 if Present (Fin_Call) then
8575 Append_To (Stmts, Fin_Call);
8578 end Process_Transient_Component_Completion;
8580 ---------------------
8581 -- Sort_Case_Table --
8582 ---------------------
8584 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type) is
8585 L : constant Int := Case_Table'First;
8586 U : constant Int := Case_Table'Last;
8594 T := Case_Table (K + 1);
8598 and then Expr_Value (Case_Table (J - 1).Choice_Lo) >
8599 Expr_Value (T.Choice_Lo)
8601 Case_Table (J) := Case_Table (J - 1);
8605 Case_Table (J) := T;
8608 end Sort_Case_Table;
8610 ----------------------------
8611 -- Static_Array_Aggregate --
8612 ----------------------------
8614 function Static_Array_Aggregate (N : Node_Id) return Boolean is
8615 function Is_Static_Component (Nod : Node_Id) return Boolean;
8616 -- Return True if Nod has a compile-time known value and can be passed
8617 -- as is to the back-end without further expansion.
8619 ---------------------------
8620 -- Is_Static_Component --
8621 ---------------------------
8623 function Is_Static_Component (Nod : Node_Id) return Boolean is
8625 if Nkind_In (Nod, N_Integer_Literal, N_Real_Literal) then
8628 elsif Is_Entity_Name (Nod)
8629 and then Present (Entity (Nod))
8630 and then Ekind (Entity (Nod)) = E_Enumeration_Literal
8634 elsif Nkind (Nod) = N_Aggregate
8635 and then Compile_Time_Known_Aggregate (Nod)
8642 end Is_Static_Component;
8646 Bounds : constant Node_Id := Aggregate_Bounds (N);
8647 Typ : constant Entity_Id := Etype (N);
8654 -- Start of processing for Static_Array_Aggregate
8657 if Is_Packed (Typ) or else Has_Discriminants (Component_Type (Typ)) then
8662 and then Nkind (Bounds) = N_Range
8663 and then Nkind (Low_Bound (Bounds)) = N_Integer_Literal
8664 and then Nkind (High_Bound (Bounds)) = N_Integer_Literal
8666 Lo := Low_Bound (Bounds);
8667 Hi := High_Bound (Bounds);
8669 if No (Component_Associations (N)) then
8671 -- Verify that all components are static
8673 Expr := First (Expressions (N));
8674 while Present (Expr) loop
8675 if not Is_Static_Component (Expr) then
8685 -- We allow only a single named association, either a static
8686 -- range or an others_clause, with a static expression.
8688 Expr := First (Component_Associations (N));
8690 if Present (Expressions (N)) then
8693 elsif Present (Next (Expr)) then
8696 elsif Present (Next (First (Choice_List (Expr)))) then
8700 -- The aggregate is static if all components are literals,
8701 -- or else all its components are static aggregates for the
8702 -- component type. We also limit the size of a static aggregate
8703 -- to prevent runaway static expressions.
8705 if not Is_Static_Component (Expression (Expr)) then
8709 if not Aggr_Size_OK (N, Typ) then
8713 -- Create a positional aggregate with the right number of
8714 -- copies of the expression.
8716 Agg := Make_Aggregate (Sloc (N), New_List, No_List);
8718 for I in UI_To_Int (Intval (Lo)) .. UI_To_Int (Intval (Hi))
8720 Append_To (Expressions (Agg), New_Copy (Expression (Expr)));
8722 -- The copied expression must be analyzed and resolved.
8723 -- Besides setting the type, this ensures that static
8724 -- expressions are appropriately marked as such.
8727 (Last (Expressions (Agg)), Component_Type (Typ));
8730 Set_Aggregate_Bounds (Agg, Bounds);
8731 Set_Etype (Agg, Typ);
8734 Set_Compile_Time_Known_Aggregate (N);
8743 end Static_Array_Aggregate;
8745 ----------------------------------
8746 -- Two_Dim_Packed_Array_Handled --
8747 ----------------------------------
8749 function Two_Dim_Packed_Array_Handled (N : Node_Id) return Boolean is
8750 Loc : constant Source_Ptr := Sloc (N);
8751 Typ : constant Entity_Id := Etype (N);
8752 Ctyp : constant Entity_Id := Component_Type (Typ);
8753 Comp_Size : constant Int := UI_To_Int (Component_Size (Typ));
8754 Packed_Array : constant Entity_Id :=
8755 Packed_Array_Impl_Type (Base_Type (Typ));
8758 -- Expression in original aggregate
8761 -- One-dimensional subaggregate
8765 -- For now, only deal with cases where an integral number of elements
8766 -- fit in a single byte. This includes the most common boolean case.
8768 if not (Comp_Size = 1 or else
8769 Comp_Size = 2 or else
8775 Convert_To_Positional
8776 (N, Max_Others_Replicate => 64, Handle_Bit_Packed => True);
8778 -- Verify that all components are static
8780 if Nkind (N) = N_Aggregate
8781 and then Compile_Time_Known_Aggregate (N)
8785 -- The aggregate may have been reanalyzed and converted already
8787 elsif Nkind (N) /= N_Aggregate then
8790 -- If component associations remain, the aggregate is not static
8792 elsif Present (Component_Associations (N)) then
8796 One_Dim := First (Expressions (N));
8797 while Present (One_Dim) loop
8798 if Present (Component_Associations (One_Dim)) then
8802 One_Comp := First (Expressions (One_Dim));
8803 while Present (One_Comp) loop
8804 if not Is_OK_Static_Expression (One_Comp) then
8815 -- Two-dimensional aggregate is now fully positional so pack one
8816 -- dimension to create a static one-dimensional array, and rewrite
8817 -- as an unchecked conversion to the original type.
8820 Byte_Size : constant Int := UI_To_Int (Component_Size (Packed_Array));
8821 -- The packed array type is a byte array
8824 -- Number of components accumulated in current byte
8827 -- Assembled list of packed values for equivalent aggregate
8830 -- Integer value of component
8833 -- Step size for packing
8836 -- Endian-dependent start position for packing
8839 -- Current insertion position
8842 -- Component of packed array being assembled
8849 -- Account for endianness. See corresponding comment in
8850 -- Packed_Array_Aggregate_Handled concerning the following.
8854 xor Reverse_Storage_Order (Base_Type (Typ))
8856 Init_Shift := Byte_Size - Comp_Size;
8863 -- Iterate over each subaggregate
8865 Shift := Init_Shift;
8866 One_Dim := First (Expressions (N));
8867 while Present (One_Dim) loop
8868 One_Comp := First (Expressions (One_Dim));
8869 while Present (One_Comp) loop
8870 if Packed_Num = Byte_Size / Comp_Size then
8872 -- Byte is complete, add to list of expressions
8874 Append (Make_Integer_Literal (Sloc (One_Dim), Val), Comps);
8876 Shift := Init_Shift;
8880 Comp_Val := Expr_Rep_Value (One_Comp);
8882 -- Adjust for bias, and strip proper number of bits
8884 if Has_Biased_Representation (Ctyp) then
8885 Comp_Val := Comp_Val - Expr_Value (Type_Low_Bound (Ctyp));
8888 Comp_Val := Comp_Val mod Uint_2 ** Comp_Size;
8889 Val := UI_To_Int (Val + Comp_Val * Uint_2 ** Shift);
8890 Shift := Shift + Incr;
8891 One_Comp := Next (One_Comp);
8892 Packed_Num := Packed_Num + 1;
8896 One_Dim := Next (One_Dim);
8899 if Packed_Num > 0 then
8901 -- Add final incomplete byte if present
8903 Append (Make_Integer_Literal (Sloc (One_Dim), Val), Comps);
8907 Unchecked_Convert_To (Typ,
8908 Make_Qualified_Expression (Loc,
8909 Subtype_Mark => New_Occurrence_Of (Packed_Array, Loc),
8910 Expression => Make_Aggregate (Loc, Expressions => Comps))));
8911 Analyze_And_Resolve (N);
8914 end Two_Dim_Packed_Array_Handled;