1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2016, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Atree; use Atree;
27 with Checks; use Checks;
28 with Debug; use Debug;
29 with Einfo; use Einfo;
30 with Elists; use Elists;
31 with Errout; use Errout;
32 with Expander; use Expander;
33 with Exp_Util; use Exp_Util;
34 with Exp_Ch3; use Exp_Ch3;
35 with Exp_Ch6; use Exp_Ch6;
36 with Exp_Ch7; use Exp_Ch7;
37 with Exp_Ch9; use Exp_Ch9;
38 with Exp_Disp; use Exp_Disp;
39 with Exp_Tss; use Exp_Tss;
40 with Fname; use Fname;
41 with Freeze; use Freeze;
42 with Itypes; use Itypes;
44 with Namet; use Namet;
45 with Nmake; use Nmake;
46 with Nlists; use Nlists;
48 with Restrict; use Restrict;
49 with Rident; use Rident;
50 with Rtsfind; use Rtsfind;
51 with Ttypes; use Ttypes;
53 with Sem_Aggr; use Sem_Aggr;
54 with Sem_Aux; use Sem_Aux;
55 with Sem_Ch3; use Sem_Ch3;
56 with Sem_Eval; use Sem_Eval;
57 with Sem_Res; use Sem_Res;
58 with Sem_Util; use Sem_Util;
59 with Sinfo; use Sinfo;
60 with Snames; use Snames;
61 with Stand; use Stand;
62 with Stringt; use Stringt;
63 with Targparm; use Targparm;
64 with Tbuild; use Tbuild;
65 with Uintp; use Uintp;
67 package body Exp_Aggr is
69 type Case_Bounds is record
72 Choice_Node : Node_Id;
75 type Case_Table_Type is array (Nat range <>) of Case_Bounds;
76 -- Table type used by Check_Case_Choices procedure
78 procedure Collect_Initialization_Statements
81 Node_After : Node_Id);
82 -- If Obj is not frozen, collect actions inserted after N until, but not
83 -- including, Node_After, for initialization of Obj, and move them to an
84 -- expression with actions, which becomes the Initialization_Statements for
87 function Has_Default_Init_Comps (N : Node_Id) return Boolean;
88 -- N is an aggregate (record or array). Checks the presence of default
89 -- initialization (<>) in any component (Ada 2005: AI-287).
91 function In_Object_Declaration (N : Node_Id) return Boolean;
92 -- Return True if N is part of an object declaration, False otherwise
94 function Is_Static_Dispatch_Table_Aggregate (N : Node_Id) return Boolean;
95 -- Returns true if N is an aggregate used to initialize the components
96 -- of a statically allocated dispatch table.
98 function Late_Expansion
101 Target : Node_Id) return List_Id;
102 -- This routine implements top-down expansion of nested aggregates. In
103 -- doing so, it avoids the generation of temporaries at each level. N is
104 -- a nested record or array aggregate with the Expansion_Delayed flag.
105 -- Typ is the expected type of the aggregate. Target is a (duplicatable)
106 -- expression that will hold the result of the aggregate expansion.
108 function Make_OK_Assignment_Statement
111 Expression : Node_Id) return Node_Id;
112 -- This is like Make_Assignment_Statement, except that Assignment_OK
113 -- is set in the left operand. All assignments built by this unit use
114 -- this routine. This is needed to deal with assignments to initialized
115 -- constants that are done in place.
118 (Obj_Type : Entity_Id;
119 Typ : Entity_Id) return Boolean;
120 -- A static array aggregate in an object declaration can in most cases be
121 -- expanded in place. The one exception is when the aggregate is given
122 -- with component associations that specify different bounds from those of
123 -- the type definition in the object declaration. In this pathological
124 -- case the aggregate must slide, and we must introduce an intermediate
125 -- temporary to hold it.
127 -- The same holds in an assignment to one-dimensional array of arrays,
128 -- when a component may be given with bounds that differ from those of the
131 function Number_Of_Choices (N : Node_Id) return Nat;
132 -- Returns the number of discrete choices (not including the others choice
133 -- if present) contained in (sub-)aggregate N.
135 procedure Process_Transient_Component
137 Comp_Typ : Entity_Id;
139 Fin_Call : out Node_Id;
140 Hook_Clear : out Node_Id;
141 Aggr : Node_Id := Empty;
142 Stmts : List_Id := No_List);
143 -- Subsidiary to the expansion of array and record aggregates. Generate
144 -- part of the necessary code to finalize a transient component. Comp_Typ
145 -- is the component type. Init_Expr is the initialization expression of the
146 -- component which is always a function call. Fin_Call is the finalization
147 -- call used to clean up the transient function result. Hook_Clear is the
148 -- hook reset statement. Aggr and Stmts both control the placement of the
149 -- generated code. Aggr is the related aggregate. If present, all code is
150 -- inserted prior to Aggr using Insert_Action. Stmts is the initialization
151 -- statements of the component. If present, all code is added to Stmts.
153 procedure Process_Transient_Component_Completion
157 Hook_Clear : Node_Id;
159 -- Subsidiary to the expansion of array and record aggregates. Generate
160 -- part of the necessary code to finalize a transient component. Aggr is
161 -- the related aggregate. Fin_Clear is the finalization call used to clean
162 -- up the transient component. Hook_Clear is the hook reset statment. Stmts
163 -- is the initialization statement list for the component. All generated
164 -- code is added to Stmts.
166 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type);
167 -- Sort the Case Table using the Lower Bound of each Choice as the key.
168 -- A simple insertion sort is used since the number of choices in a case
169 -- statement of variant part will usually be small and probably in near
172 ------------------------------------------------------
173 -- Local subprograms for Record Aggregate Expansion --
174 ------------------------------------------------------
176 function Build_Record_Aggr_Code
179 Lhs : Node_Id) return List_Id;
180 -- N is an N_Aggregate or an N_Extension_Aggregate. Typ is the type of the
181 -- aggregate. Target is an expression containing the location on which the
182 -- component by component assignments will take place. Returns the list of
183 -- assignments plus all other adjustments needed for tagged and controlled
186 procedure Convert_To_Assignments (N : Node_Id; Typ : Entity_Id);
187 -- N is an N_Aggregate or an N_Extension_Aggregate. Typ is the type of the
188 -- aggregate (which can only be a record type, this procedure is only used
189 -- for record types). Transform the given aggregate into a sequence of
190 -- assignments performed component by component.
192 procedure Expand_Record_Aggregate
194 Orig_Tag : Node_Id := Empty;
195 Parent_Expr : Node_Id := Empty);
196 -- This is the top level procedure for record aggregate expansion.
197 -- Expansion for record aggregates needs expand aggregates for tagged
198 -- record types. Specifically Expand_Record_Aggregate adds the Tag
199 -- field in front of the Component_Association list that was created
200 -- during resolution by Resolve_Record_Aggregate.
202 -- N is the record aggregate node.
203 -- Orig_Tag is the value of the Tag that has to be provided for this
204 -- specific aggregate. It carries the tag corresponding to the type
205 -- of the outermost aggregate during the recursive expansion
206 -- Parent_Expr is the ancestor part of the original extension
209 function Has_Mutable_Components (Typ : Entity_Id) return Boolean;
210 -- Return true if one of the components is of a discriminated type with
211 -- defaults. An aggregate for a type with mutable components must be
212 -- expanded into individual assignments.
214 procedure Initialize_Discriminants (N : Node_Id; Typ : Entity_Id);
215 -- If the type of the aggregate is a type extension with renamed discrimi-
216 -- nants, we must initialize the hidden discriminants of the parent.
217 -- Otherwise, the target object must not be initialized. The discriminants
218 -- are initialized by calling the initialization procedure for the type.
219 -- This is incorrect if the initialization of other components has any
220 -- side effects. We restrict this call to the case where the parent type
221 -- has a variant part, because this is the only case where the hidden
222 -- discriminants are accessed, namely when calling discriminant checking
223 -- functions of the parent type, and when applying a stream attribute to
224 -- an object of the derived type.
226 -----------------------------------------------------
227 -- Local Subprograms for Array Aggregate Expansion --
228 -----------------------------------------------------
230 function Aggr_Size_OK (N : Node_Id; Typ : Entity_Id) return Boolean;
231 -- Very large static aggregates present problems to the back-end, and are
232 -- transformed into assignments and loops. This function verifies that the
233 -- total number of components of an aggregate is acceptable for rewriting
234 -- into a purely positional static form. Aggr_Size_OK must be called before
237 -- This function also detects and warns about one-component aggregates that
238 -- appear in a non-static context. Even if the component value is static,
239 -- such an aggregate must be expanded into an assignment.
241 function Backend_Processing_Possible (N : Node_Id) return Boolean;
242 -- This function checks if array aggregate N can be processed directly
243 -- by the backend. If this is the case, True is returned.
245 function Build_Array_Aggr_Code
250 Scalar_Comp : Boolean;
251 Indexes : List_Id := No_List) return List_Id;
252 -- This recursive routine returns a list of statements containing the
253 -- loops and assignments that are needed for the expansion of the array
256 -- N is the (sub-)aggregate node to be expanded into code. This node has
257 -- been fully analyzed, and its Etype is properly set.
259 -- Index is the index node corresponding to the array subaggregate N
261 -- Into is the target expression into which we are copying the aggregate.
262 -- Note that this node may not have been analyzed yet, and so the Etype
263 -- field may not be set.
265 -- Scalar_Comp is True if the component type of the aggregate is scalar
267 -- Indexes is the current list of expressions used to index the object we
270 procedure Convert_Array_Aggr_In_Allocator
274 -- If the aggregate appears within an allocator and can be expanded in
275 -- place, this routine generates the individual assignments to components
276 -- of the designated object. This is an optimization over the general
277 -- case, where a temporary is first created on the stack and then used to
278 -- construct the allocated object on the heap.
280 procedure Convert_To_Positional
282 Max_Others_Replicate : Nat := 5;
283 Handle_Bit_Packed : Boolean := False);
284 -- If possible, convert named notation to positional notation. This
285 -- conversion is possible only in some static cases. If the conversion is
286 -- possible, then N is rewritten with the analyzed converted aggregate.
287 -- The parameter Max_Others_Replicate controls the maximum number of
288 -- values corresponding to an others choice that will be converted to
289 -- positional notation (the default of 5 is the normal limit, and reflects
290 -- the fact that normally the loop is better than a lot of separate
291 -- assignments). Note that this limit gets overridden in any case if
292 -- either of the restrictions No_Elaboration_Code or No_Implicit_Loops is
293 -- set. The parameter Handle_Bit_Packed is usually set False (since we do
294 -- not expect the back end to handle bit packed arrays, so the normal case
295 -- of conversion is pointless), but in the special case of a call from
296 -- Packed_Array_Aggregate_Handled, we set this parameter to True, since
297 -- these are cases we handle in there.
299 -- It would seem useful to have a higher default for Max_Others_Replicate,
300 -- but aggregates in the compiler make this impossible: the compiler
301 -- bootstrap fails if Max_Others_Replicate is greater than 25. This
304 procedure Expand_Array_Aggregate (N : Node_Id);
305 -- This is the top-level routine to perform array aggregate expansion.
306 -- N is the N_Aggregate node to be expanded.
308 function Is_Two_Dim_Packed_Array (Typ : Entity_Id) return Boolean;
309 -- For two-dimensional packed aggregates with constant bounds and constant
310 -- components, it is preferable to pack the inner aggregates because the
311 -- whole matrix can then be presented to the back-end as a one-dimensional
312 -- list of literals. This is much more efficient than expanding into single
313 -- component assignments. This function determines if the type Typ is for
314 -- an array that is suitable for this optimization: it returns True if Typ
315 -- is a two dimensional bit packed array with component size 1, 2, or 4.
317 function Packed_Array_Aggregate_Handled (N : Node_Id) return Boolean;
318 -- Given an array aggregate, this function handles the case of a packed
319 -- array aggregate with all constant values, where the aggregate can be
320 -- evaluated at compile time. If this is possible, then N is rewritten
321 -- to be its proper compile time value with all the components properly
322 -- assembled. The expression is analyzed and resolved and True is returned.
323 -- If this transformation is not possible, N is unchanged and False is
326 function Two_Dim_Packed_Array_Handled (N : Node_Id) return Boolean;
327 -- If the type of the aggregate is a two-dimensional bit_packed array
328 -- it may be transformed into an array of bytes with constant values,
329 -- and presented to the back-end as a static value. The function returns
330 -- false if this transformation cannot be performed. THis is similar to,
331 -- and reuses part of the machinery in Packed_Array_Aggregate_Handled.
337 function Aggr_Size_OK (N : Node_Id; Typ : Entity_Id) return Boolean is
346 -- Determines the maximum size of an array aggregate produced by
347 -- converting named to positional notation (e.g. from others clauses).
348 -- This avoids running away with attempts to convert huge aggregates,
349 -- which hit memory limits in the backend.
351 function Component_Count (T : Entity_Id) return Nat;
352 -- The limit is applied to the total number of components that the
353 -- aggregate will have, which is the number of static expressions
354 -- that will appear in the flattened array. This requires a recursive
355 -- computation of the number of scalar components of the structure.
357 ---------------------
358 -- Component_Count --
359 ---------------------
361 function Component_Count (T : Entity_Id) return Nat is
366 if Is_Scalar_Type (T) then
369 elsif Is_Record_Type (T) then
370 Comp := First_Component (T);
371 while Present (Comp) loop
372 Res := Res + Component_Count (Etype (Comp));
373 Next_Component (Comp);
378 elsif Is_Array_Type (T) then
380 Lo : constant Node_Id :=
381 Type_Low_Bound (Etype (First_Index (T)));
382 Hi : constant Node_Id :=
383 Type_High_Bound (Etype (First_Index (T)));
385 Siz : constant Nat := Component_Count (Component_Type (T));
388 -- Check for superflat arrays, i.e. arrays with such bounds
389 -- as 4 .. 2, to insure that this function never returns a
390 -- meaningless negative value.
392 if not Compile_Time_Known_Value (Lo)
393 or else not Compile_Time_Known_Value (Hi)
394 or else Expr_Value (Hi) < Expr_Value (Lo)
400 Siz * UI_To_Int (Expr_Value (Hi) - Expr_Value (Lo) + 1);
405 -- Can only be a null for an access type
411 -- Start of processing for Aggr_Size_OK
414 -- The normal aggregate limit is 50000, but we increase this limit to
415 -- 2**24 (about 16 million) if Restrictions (No_Elaboration_Code) or
416 -- Restrictions (No_Implicit_Loops) is specified, since in either case
417 -- we are at risk of declaring the program illegal because of this
418 -- limit. We also increase the limit when Static_Elaboration_Desired,
419 -- given that this means that objects are intended to be placed in data
422 -- We also increase the limit if the aggregate is for a packed two-
423 -- dimensional array, because if components are static it is much more
424 -- efficient to construct a one-dimensional equivalent array with static
427 -- Conversely, we decrease the maximum size if none of the above
428 -- requirements apply, and if the aggregate has a single component
429 -- association, which will be more efficient if implemented with a loop.
431 -- Finally, we use a small limit in CodePeer mode where we favor loops
432 -- instead of thousands of single assignments (from large aggregates).
434 Max_Aggr_Size := 50000;
436 if CodePeer_Mode then
437 Max_Aggr_Size := 100;
439 elsif Restriction_Active (No_Elaboration_Code)
440 or else Restriction_Active (No_Implicit_Loops)
441 or else Is_Two_Dim_Packed_Array (Typ)
442 or else (Ekind (Current_Scope) = E_Package
443 and then Static_Elaboration_Desired (Current_Scope))
445 Max_Aggr_Size := 2 ** 24;
447 elsif No (Expressions (N))
448 and then No (Next (First (Component_Associations (N))))
450 Max_Aggr_Size := 5000;
453 Siz := Component_Count (Component_Type (Typ));
455 Indx := First_Index (Typ);
456 while Present (Indx) loop
457 Lo := Type_Low_Bound (Etype (Indx));
458 Hi := Type_High_Bound (Etype (Indx));
460 -- Bounds need to be known at compile time
462 if not Compile_Time_Known_Value (Lo)
463 or else not Compile_Time_Known_Value (Hi)
468 Lov := Expr_Value (Lo);
469 Hiv := Expr_Value (Hi);
471 -- A flat array is always safe
477 -- One-component aggregates are suspicious, and if the context type
478 -- is an object declaration with non-static bounds it will trip gcc;
479 -- such an aggregate must be expanded into a single assignment.
481 if Hiv = Lov and then Nkind (Parent (N)) = N_Object_Declaration then
483 Index_Type : constant Entity_Id :=
485 (First_Index (Etype (Defining_Identifier (Parent (N)))));
489 if not Compile_Time_Known_Value (Type_Low_Bound (Index_Type))
490 or else not Compile_Time_Known_Value
491 (Type_High_Bound (Index_Type))
493 if Present (Component_Associations (N)) then
496 (Choice_List (First (Component_Associations (N))));
498 if Is_Entity_Name (Indx)
499 and then not Is_Type (Entity (Indx))
502 ("single component aggregate in "
503 & "non-static context??", Indx);
504 Error_Msg_N ("\maybe subtype name was meant??", Indx);
514 Rng : constant Uint := Hiv - Lov + 1;
517 -- Check if size is too large
519 if not UI_Is_In_Int_Range (Rng) then
523 Siz := Siz * UI_To_Int (Rng);
527 or else Siz > Max_Aggr_Size
532 -- Bounds must be in integer range, for later array construction
534 if not UI_Is_In_Int_Range (Lov)
536 not UI_Is_In_Int_Range (Hiv)
547 ---------------------------------
548 -- Backend_Processing_Possible --
549 ---------------------------------
551 -- Backend processing by Gigi/gcc is possible only if all the following
552 -- conditions are met:
554 -- 1. N is fully positional
556 -- 2. N is not a bit-packed array aggregate;
558 -- 3. The size of N's array type must be known at compile time. Note
559 -- that this implies that the component size is also known
561 -- 4. The array type of N does not follow the Fortran layout convention
562 -- or if it does it must be 1 dimensional.
564 -- 5. The array component type may not be tagged (which could necessitate
565 -- reassignment of proper tags).
567 -- 6. The array component type must not have unaligned bit components
569 -- 7. None of the components of the aggregate may be bit unaligned
572 -- 8. There cannot be delayed components, since we do not know enough
573 -- at this stage to know if back end processing is possible.
575 -- 9. There cannot be any discriminated record components, since the
576 -- back end cannot handle this complex case.
578 -- 10. No controlled actions need to be generated for components
580 -- 11. When generating C code, N must be part of a N_Object_Declaration
582 -- 12. When generating C code, N must not include function calls
584 function Backend_Processing_Possible (N : Node_Id) return Boolean is
585 Typ : constant Entity_Id := Etype (N);
586 -- Typ is the correct constrained array subtype of the aggregate
588 function Component_Check (N : Node_Id; Index : Node_Id) return Boolean;
589 -- This routine checks components of aggregate N, enforcing checks
590 -- 1, 7, 8, 9, 11, and 12. In the multidimensional case, these checks
591 -- are performed on subaggregates. The Index value is the current index
592 -- being checked in the multidimensional case.
594 ---------------------
595 -- Component_Check --
596 ---------------------
598 function Component_Check (N : Node_Id; Index : Node_Id) return Boolean is
599 function Ultimate_Original_Expression (N : Node_Id) return Node_Id;
600 -- Given a type conversion or an unchecked type conversion N, return
601 -- its innermost original expression.
603 ----------------------------------
604 -- Ultimate_Original_Expression --
605 ----------------------------------
607 function Ultimate_Original_Expression (N : Node_Id) return Node_Id is
608 Expr : Node_Id := Original_Node (N);
611 while Nkind_In (Expr, N_Type_Conversion,
612 N_Unchecked_Type_Conversion)
614 Expr := Original_Node (Expression (Expr));
618 end Ultimate_Original_Expression;
624 -- Start of processing for Component_Check
627 -- Checks 1: (no component associations)
629 if Present (Component_Associations (N)) then
633 -- Checks 11: (part of an object declaration)
636 and then Nkind (Parent (N)) /= N_Object_Declaration
638 (Nkind (Parent (N)) /= N_Qualified_Expression
639 or else Nkind (Parent (Parent (N))) /= N_Object_Declaration)
644 -- Checks on components
646 -- Recurse to check subaggregates, which may appear in qualified
647 -- expressions. If delayed, the front-end will have to expand.
648 -- If the component is a discriminated record, treat as non-static,
649 -- as the back-end cannot handle this properly.
651 Expr := First (Expressions (N));
652 while Present (Expr) loop
654 -- Checks 8: (no delayed components)
656 if Is_Delayed_Aggregate (Expr) then
660 -- Checks 9: (no discriminated records)
662 if Present (Etype (Expr))
663 and then Is_Record_Type (Etype (Expr))
664 and then Has_Discriminants (Etype (Expr))
669 -- Checks 7. Component must not be bit aligned component
671 if Possible_Bit_Aligned_Component (Expr) then
675 -- Checks 12: (no function call)
679 Nkind (Ultimate_Original_Expression (Expr)) = N_Function_Call
684 -- Recursion to following indexes for multiple dimension case
686 if Present (Next_Index (Index))
687 and then not Component_Check (Expr, Next_Index (Index))
692 -- All checks for that component finished, on to next
700 -- Start of processing for Backend_Processing_Possible
703 -- Checks 2 (array not bit packed) and 10 (no controlled actions)
705 if Is_Bit_Packed_Array (Typ) or else Needs_Finalization (Typ) then
709 -- If component is limited, aggregate must be expanded because each
710 -- component assignment must be built in place.
712 if Is_Limited_View (Component_Type (Typ)) then
716 -- Checks 4 (array must not be multidimensional Fortran case)
718 if Convention (Typ) = Convention_Fortran
719 and then Number_Dimensions (Typ) > 1
724 -- Checks 3 (size of array must be known at compile time)
726 if not Size_Known_At_Compile_Time (Typ) then
730 -- Checks on components
732 if not Component_Check (N, First_Index (Typ)) then
736 -- Checks 5 (if the component type is tagged, then we may need to do
737 -- tag adjustments. Perhaps this should be refined to check for any
738 -- component associations that actually need tag adjustment, similar
739 -- to the test in Component_Not_OK_For_Backend for record aggregates
740 -- with tagged components, but not clear whether it's worthwhile ???;
741 -- in the case of virtual machines (no Tagged_Type_Expansion), object
742 -- tags are handled implicitly).
744 if Is_Tagged_Type (Component_Type (Typ))
745 and then Tagged_Type_Expansion
750 -- Checks 6 (component type must not have bit aligned components)
752 if Type_May_Have_Bit_Aligned_Components (Component_Type (Typ)) then
756 -- Backend processing is possible
758 Set_Size_Known_At_Compile_Time (Etype (N), True);
760 end Backend_Processing_Possible;
762 ---------------------------
763 -- Build_Array_Aggr_Code --
764 ---------------------------
766 -- The code that we generate from a one dimensional aggregate is
768 -- 1. If the subaggregate contains discrete choices we
770 -- (a) Sort the discrete choices
772 -- (b) Otherwise for each discrete choice that specifies a range we
773 -- emit a loop. If a range specifies a maximum of three values, or
774 -- we are dealing with an expression we emit a sequence of
775 -- assignments instead of a loop.
777 -- (c) Generate the remaining loops to cover the others choice if any
779 -- 2. If the aggregate contains positional elements we
781 -- (a) translate the positional elements in a series of assignments
783 -- (b) Generate a final loop to cover the others choice if any.
784 -- Note that this final loop has to be a while loop since the case
786 -- L : Integer := Integer'Last;
787 -- H : Integer := Integer'Last;
788 -- A : array (L .. H) := (1, others =>0);
790 -- cannot be handled by a for loop. Thus for the following
792 -- array (L .. H) := (.. positional elements.., others =>E);
794 -- we always generate something like:
796 -- J : Index_Type := Index_Of_Last_Positional_Element;
798 -- J := Index_Base'Succ (J)
802 function Build_Array_Aggr_Code
807 Scalar_Comp : Boolean;
808 Indexes : List_Id := No_List) return List_Id
810 Loc : constant Source_Ptr := Sloc (N);
811 Index_Base : constant Entity_Id := Base_Type (Etype (Index));
812 Index_Base_L : constant Node_Id := Type_Low_Bound (Index_Base);
813 Index_Base_H : constant Node_Id := Type_High_Bound (Index_Base);
815 function Add (Val : Int; To : Node_Id) return Node_Id;
816 -- Returns an expression where Val is added to expression To, unless
817 -- To+Val is provably out of To's base type range. To must be an
818 -- already analyzed expression.
820 function Empty_Range (L, H : Node_Id) return Boolean;
821 -- Returns True if the range defined by L .. H is certainly empty
823 function Equal (L, H : Node_Id) return Boolean;
824 -- Returns True if L = H for sure
826 function Index_Base_Name return Node_Id;
827 -- Returns a new reference to the index type name
832 In_Loop : Boolean := False) return List_Id;
833 -- Ind must be a side-effect-free expression. If the input aggregate N
834 -- to Build_Loop contains no subaggregates, then this function returns
835 -- the assignment statement:
837 -- Into (Indexes, Ind) := Expr;
839 -- Otherwise we call Build_Code recursively. Flag In_Loop should be set
840 -- when the assignment appears within a generated loop.
842 -- Ada 2005 (AI-287): In case of default initialized component, Expr
843 -- is empty and we generate a call to the corresponding IP subprogram.
845 function Gen_Loop (L, H : Node_Id; Expr : Node_Id) return List_Id;
846 -- Nodes L and H must be side-effect-free expressions. If the input
847 -- aggregate N to Build_Loop contains no subaggregates, this routine
848 -- returns the for loop statement:
850 -- for J in Index_Base'(L) .. Index_Base'(H) loop
851 -- Into (Indexes, J) := Expr;
854 -- Otherwise we call Build_Code recursively. As an optimization if the
855 -- loop covers 3 or fewer scalar elements we generate a sequence of
857 -- If the component association that generates the loop comes from an
858 -- Iterated_Component_Association, the loop parameter has the name of
859 -- the corresponding parameter in the original construct.
861 function Gen_While (L, H : Node_Id; Expr : Node_Id) return List_Id;
862 -- Nodes L and H must be side-effect-free expressions. If the input
863 -- aggregate N to Build_Loop contains no subaggregates, this routine
864 -- returns the while loop statement:
866 -- J : Index_Base := L;
868 -- J := Index_Base'Succ (J);
869 -- Into (Indexes, J) := Expr;
872 -- Otherwise we call Build_Code recursively
874 function Get_Assoc_Expr (Assoc : Node_Id) return Node_Id;
875 -- For an association with a box, use value given by aspect
876 -- Default_Component_Value of array type if specified, else use
877 -- value given by aspect Default_Value for component type itself
878 -- if specified, else return Empty.
880 function Local_Compile_Time_Known_Value (E : Node_Id) return Boolean;
881 function Local_Expr_Value (E : Node_Id) return Uint;
882 -- These two Local routines are used to replace the corresponding ones
883 -- in sem_eval because while processing the bounds of an aggregate with
884 -- discrete choices whose index type is an enumeration, we build static
885 -- expressions not recognized by Compile_Time_Known_Value as such since
886 -- they have not yet been analyzed and resolved. All the expressions in
887 -- question are things like Index_Base_Name'Val (Const) which we can
888 -- easily recognize as being constant.
894 function Add (Val : Int; To : Node_Id) return Node_Id is
899 U_Val : constant Uint := UI_From_Int (Val);
902 -- Note: do not try to optimize the case of Val = 0, because
903 -- we need to build a new node with the proper Sloc value anyway.
905 -- First test if we can do constant folding
907 if Local_Compile_Time_Known_Value (To) then
908 U_To := Local_Expr_Value (To) + Val;
910 -- Determine if our constant is outside the range of the index.
911 -- If so return an Empty node. This empty node will be caught
912 -- by Empty_Range below.
914 if Compile_Time_Known_Value (Index_Base_L)
915 and then U_To < Expr_Value (Index_Base_L)
919 elsif Compile_Time_Known_Value (Index_Base_H)
920 and then U_To > Expr_Value (Index_Base_H)
925 Expr_Pos := Make_Integer_Literal (Loc, U_To);
926 Set_Is_Static_Expression (Expr_Pos);
928 if not Is_Enumeration_Type (Index_Base) then
931 -- If we are dealing with enumeration return
932 -- Index_Base'Val (Expr_Pos)
936 Make_Attribute_Reference
938 Prefix => Index_Base_Name,
939 Attribute_Name => Name_Val,
940 Expressions => New_List (Expr_Pos));
946 -- If we are here no constant folding possible
948 if not Is_Enumeration_Type (Index_Base) then
951 Left_Opnd => Duplicate_Subexpr (To),
952 Right_Opnd => Make_Integer_Literal (Loc, U_Val));
954 -- If we are dealing with enumeration return
955 -- Index_Base'Val (Index_Base'Pos (To) + Val)
959 Make_Attribute_Reference
961 Prefix => Index_Base_Name,
962 Attribute_Name => Name_Pos,
963 Expressions => New_List (Duplicate_Subexpr (To)));
968 Right_Opnd => Make_Integer_Literal (Loc, U_Val));
971 Make_Attribute_Reference
973 Prefix => Index_Base_Name,
974 Attribute_Name => Name_Val,
975 Expressions => New_List (Expr_Pos));
985 function Empty_Range (L, H : Node_Id) return Boolean is
986 Is_Empty : Boolean := False;
991 -- First check if L or H were already detected as overflowing the
992 -- index base range type by function Add above. If this is so Add
993 -- returns the empty node.
995 if No (L) or else No (H) then
1002 -- L > H range is empty
1008 -- B_L > H range must be empty
1011 Low := Index_Base_L;
1014 -- L > B_H range must be empty
1018 High := Index_Base_H;
1021 if Local_Compile_Time_Known_Value (Low)
1023 Local_Compile_Time_Known_Value (High)
1026 UI_Gt (Local_Expr_Value (Low), Local_Expr_Value (High));
1039 function Equal (L, H : Node_Id) return Boolean is
1044 elsif Local_Compile_Time_Known_Value (L)
1046 Local_Compile_Time_Known_Value (H)
1048 return UI_Eq (Local_Expr_Value (L), Local_Expr_Value (H));
1061 In_Loop : Boolean := False) return List_Id
1063 function Add_Loop_Actions (Lis : List_Id) return List_Id;
1064 -- Collect insert_actions generated in the construction of a loop,
1065 -- and prepend them to the sequence of assignments to complete the
1066 -- eventual body of the loop.
1068 procedure Initialize_Array_Component
1069 (Arr_Comp : Node_Id;
1071 Init_Expr : Node_Id;
1073 -- Perform the initialization of array component Arr_Comp with
1074 -- expected type Comp_Typ. Init_Expr denotes the initialization
1075 -- expression of the array component. All generated code is added
1078 procedure Initialize_Ctrl_Array_Component
1079 (Arr_Comp : Node_Id;
1080 Comp_Typ : Entity_Id;
1081 Init_Expr : Node_Id;
1083 -- Perform the initialization of array component Arr_Comp when its
1084 -- expected type Comp_Typ needs finalization actions. Init_Expr is
1085 -- the initialization expression of the array component. All hook-
1086 -- related declarations are inserted prior to aggregate N. Remaining
1087 -- code is added to list Stmts.
1089 ----------------------
1090 -- Add_Loop_Actions --
1091 ----------------------
1093 function Add_Loop_Actions (Lis : List_Id) return List_Id is
1097 -- Ada 2005 (AI-287): Do nothing else in case of default
1098 -- initialized component.
1103 elsif Nkind (Parent (Expr)) = N_Component_Association
1104 and then Present (Loop_Actions (Parent (Expr)))
1106 Append_List (Lis, Loop_Actions (Parent (Expr)));
1107 Res := Loop_Actions (Parent (Expr));
1108 Set_Loop_Actions (Parent (Expr), No_List);
1114 end Add_Loop_Actions;
1116 --------------------------------
1117 -- Initialize_Array_Component --
1118 --------------------------------
1120 procedure Initialize_Array_Component
1121 (Arr_Comp : Node_Id;
1123 Init_Expr : Node_Id;
1126 Exceptions_OK : constant Boolean :=
1127 not Restriction_Active
1128 (No_Exception_Propagation);
1130 Finalization_OK : constant Boolean :=
1132 and then Needs_Finalization (Comp_Typ);
1134 Full_Typ : constant Entity_Id := Underlying_Type (Comp_Typ);
1136 Blk_Stmts : List_Id;
1137 Init_Stmt : Node_Id;
1140 -- Protect the initialization statements from aborts. Generate:
1144 if Finalization_OK and Abort_Allowed then
1145 if Exceptions_OK then
1146 Blk_Stmts := New_List;
1151 Append_To (Blk_Stmts, Build_Runtime_Call (Loc, RE_Abort_Defer));
1153 -- Otherwise aborts are not allowed. All generated code is added
1154 -- directly to the input list.
1160 -- Initialize the array element. Generate:
1162 -- Arr_Comp := Init_Expr;
1164 -- Note that the initialization expression is replicated because
1165 -- it has to be reevaluated within a generated loop.
1168 Make_OK_Assignment_Statement (Loc,
1169 Name => New_Copy_Tree (Arr_Comp),
1170 Expression => New_Copy_Tree (Init_Expr));
1171 Set_No_Ctrl_Actions (Init_Stmt);
1173 -- If this is an aggregate for an array of arrays, each
1174 -- subaggregate will be expanded as well, and even with
1175 -- No_Ctrl_Actions the assignments of inner components will
1176 -- require attachment in their assignments to temporaries. These
1177 -- temporaries must be finalized for each subaggregate. Generate:
1180 -- Arr_Comp := Init_Expr;
1183 if Finalization_OK and then Is_Array_Type (Comp_Typ) then
1185 Make_Block_Statement (Loc,
1186 Handled_Statement_Sequence =>
1187 Make_Handled_Sequence_Of_Statements (Loc,
1188 Statements => New_List (Init_Stmt)));
1191 Append_To (Blk_Stmts, Init_Stmt);
1193 -- Adjust the tag due to a possible view conversion. Generate:
1195 -- Arr_Comp._tag := Full_TypP;
1197 if Tagged_Type_Expansion
1198 and then Present (Comp_Typ)
1199 and then Is_Tagged_Type (Comp_Typ)
1201 Append_To (Blk_Stmts,
1202 Make_OK_Assignment_Statement (Loc,
1204 Make_Selected_Component (Loc,
1205 Prefix => New_Copy_Tree (Arr_Comp),
1208 (First_Tag_Component (Full_Typ), Loc)),
1211 Unchecked_Convert_To (RTE (RE_Tag),
1213 (Node (First_Elmt (Access_Disp_Table (Full_Typ))),
1217 -- Adjust the array component. Controlled subaggregates are not
1218 -- considered because each of their individual elements will
1219 -- receive an adjustment of its own. Generate:
1221 -- [Deep_]Adjust (Arr_Comp);
1224 and then not Is_Limited_Type (Comp_Typ)
1226 (Is_Array_Type (Comp_Typ)
1227 and then Is_Controlled (Component_Type (Comp_Typ))
1228 and then Nkind (Expr) = N_Aggregate)
1232 (Obj_Ref => New_Copy_Tree (Arr_Comp),
1235 -- Guard against a missing [Deep_]Adjust when the component
1236 -- type was not frozen properly.
1238 if Present (Adj_Call) then
1239 Append_To (Blk_Stmts, Adj_Call);
1243 -- Complete the protection of the initialization statements
1245 if Finalization_OK and Abort_Allowed then
1247 -- Wrap the initialization statements in a block to catch a
1248 -- potential exception. Generate:
1252 -- Arr_Comp := Init_Expr;
1253 -- Arr_Comp._tag := Full_TypP;
1254 -- [Deep_]Adjust (Arr_Comp);
1256 -- Abort_Undefer_Direct;
1259 if Exceptions_OK then
1261 Build_Abort_Undefer_Block (Loc,
1265 -- Otherwise exceptions are not propagated. Generate:
1268 -- Arr_Comp := Init_Expr;
1269 -- Arr_Comp._tag := Full_TypP;
1270 -- [Deep_]Adjust (Arr_Comp);
1274 Append_To (Blk_Stmts,
1275 Build_Runtime_Call (Loc, RE_Abort_Undefer));
1278 end Initialize_Array_Component;
1280 -------------------------------------
1281 -- Initialize_Ctrl_Array_Component --
1282 -------------------------------------
1284 procedure Initialize_Ctrl_Array_Component
1285 (Arr_Comp : Node_Id;
1286 Comp_Typ : Entity_Id;
1287 Init_Expr : Node_Id;
1291 Act_Stmts : List_Id;
1294 Hook_Clear : Node_Id;
1296 In_Place_Expansion : Boolean;
1297 -- Flag set when a nonlimited controlled function call requires
1298 -- in-place expansion.
1301 -- Duplicate the initialization expression in case the context is
1302 -- a multi choice list or an "others" choice which plugs various
1303 -- holes in the aggregate. As a result the expression is no longer
1304 -- shared between the various components and is reevaluated for
1305 -- each such component.
1307 Expr := New_Copy_Tree (Init_Expr);
1308 Set_Parent (Expr, Parent (Init_Expr));
1310 -- Perform a preliminary analysis and resolution to determine what
1311 -- the initialization expression denotes. An unanalyzed function
1312 -- call may appear as an identifier or an indexed component.
1314 if Nkind_In (Expr, N_Function_Call,
1316 N_Indexed_Component)
1317 and then not Analyzed (Expr)
1319 Preanalyze_And_Resolve (Expr, Comp_Typ);
1322 In_Place_Expansion :=
1323 Nkind (Expr) = N_Function_Call
1324 and then not Is_Limited_Type (Comp_Typ);
1326 -- The initialization expression is a controlled function call.
1327 -- Perform in-place removal of side effects to avoid creating a
1328 -- transient scope, which leads to premature finalization.
1330 -- This in-place expansion is not performed for limited transient
1331 -- objects because the initialization is already done in-place.
1333 if In_Place_Expansion then
1335 -- Suppress the removal of side effects by general analysis
1336 -- because this behavior is emulated here. This avoids the
1337 -- generation of a transient scope, which leads to out-of-order
1338 -- adjustment and finalization.
1340 Set_No_Side_Effect_Removal (Expr);
1342 -- When the transient component initialization is related to a
1343 -- range or an "others", keep all generated statements within
1344 -- the enclosing loop. This way the controlled function call
1345 -- will be evaluated at each iteration, and its result will be
1346 -- finalized at the end of each iteration.
1352 -- Otherwise this is a single component initialization. Hook-
1353 -- related statements are inserted prior to the aggregate.
1357 Act_Stmts := No_List;
1360 -- Install all hook-related declarations and prepare the clean
1363 Process_Transient_Component
1365 Comp_Typ => Comp_Typ,
1367 Fin_Call => Fin_Call,
1368 Hook_Clear => Hook_Clear,
1370 Stmts => Act_Stmts);
1373 -- Use the noncontrolled component initialization circuitry to
1374 -- assign the result of the function call to the array element.
1375 -- This also performs subaggregate wrapping, tag adjustment, and
1376 -- [deep] adjustment of the array element.
1378 Initialize_Array_Component
1379 (Arr_Comp => Arr_Comp,
1380 Comp_Typ => Comp_Typ,
1384 -- At this point the array element is fully initialized. Complete
1385 -- the processing of the controlled array component by finalizing
1386 -- the transient function result.
1388 if In_Place_Expansion then
1389 Process_Transient_Component_Completion
1392 Fin_Call => Fin_Call,
1393 Hook_Clear => Hook_Clear,
1396 end Initialize_Ctrl_Array_Component;
1400 Stmts : constant List_Id := New_List;
1402 Comp_Typ : Entity_Id := Empty;
1404 Indexed_Comp : Node_Id;
1405 Init_Call : Node_Id;
1406 New_Indexes : List_Id;
1408 -- Start of processing for Gen_Assign
1411 if No (Indexes) then
1412 New_Indexes := New_List;
1414 New_Indexes := New_Copy_List_Tree (Indexes);
1417 Append_To (New_Indexes, Ind);
1419 if Present (Next_Index (Index)) then
1422 Build_Array_Aggr_Code
1425 Index => Next_Index (Index),
1427 Scalar_Comp => Scalar_Comp,
1428 Indexes => New_Indexes));
1431 -- If we get here then we are at a bottom-level (sub-)aggregate
1435 (Make_Indexed_Component (Loc,
1436 Prefix => New_Copy_Tree (Into),
1437 Expressions => New_Indexes));
1439 Set_Assignment_OK (Indexed_Comp);
1441 -- Ada 2005 (AI-287): In case of default initialized component, Expr
1442 -- is not present (and therefore we also initialize Expr_Q to empty).
1446 elsif Nkind (Expr) = N_Qualified_Expression then
1447 Expr_Q := Expression (Expr);
1452 if Present (Etype (N)) and then Etype (N) /= Any_Composite then
1453 Comp_Typ := Component_Type (Etype (N));
1454 pragma Assert (Comp_Typ = Ctype); -- AI-287
1456 elsif Present (Next (First (New_Indexes))) then
1458 -- Ada 2005 (AI-287): Do nothing in case of default initialized
1459 -- component because we have received the component type in
1460 -- the formal parameter Ctype.
1462 -- ??? Some assert pragmas have been added to check if this new
1463 -- formal can be used to replace this code in all cases.
1465 if Present (Expr) then
1467 -- This is a multidimensional array. Recover the component type
1468 -- from the outermost aggregate, because subaggregates do not
1469 -- have an assigned type.
1476 while Present (P) loop
1477 if Nkind (P) = N_Aggregate
1478 and then Present (Etype (P))
1480 Comp_Typ := Component_Type (Etype (P));
1488 pragma Assert (Comp_Typ = Ctype); -- AI-287
1493 -- Ada 2005 (AI-287): We only analyze the expression in case of non-
1494 -- default initialized components (otherwise Expr_Q is not present).
1497 and then Nkind_In (Expr_Q, N_Aggregate, N_Extension_Aggregate)
1499 -- At this stage the Expression may not have been analyzed yet
1500 -- because the array aggregate code has not been updated to use
1501 -- the Expansion_Delayed flag and avoid analysis altogether to
1502 -- solve the same problem (see Resolve_Aggr_Expr). So let us do
1503 -- the analysis of non-array aggregates now in order to get the
1504 -- value of Expansion_Delayed flag for the inner aggregate ???
1506 if Present (Comp_Typ) and then not Is_Array_Type (Comp_Typ) then
1507 Analyze_And_Resolve (Expr_Q, Comp_Typ);
1510 if Is_Delayed_Aggregate (Expr_Q) then
1512 -- This is either a subaggregate of a multidimensional array,
1513 -- or a component of an array type whose component type is
1514 -- also an array. In the latter case, the expression may have
1515 -- component associations that provide different bounds from
1516 -- those of the component type, and sliding must occur. Instead
1517 -- of decomposing the current aggregate assignment, force the
1518 -- reanalysis of the assignment, so that a temporary will be
1519 -- generated in the usual fashion, and sliding will take place.
1521 if Nkind (Parent (N)) = N_Assignment_Statement
1522 and then Is_Array_Type (Comp_Typ)
1523 and then Present (Component_Associations (Expr_Q))
1524 and then Must_Slide (Comp_Typ, Etype (Expr_Q))
1526 Set_Expansion_Delayed (Expr_Q, False);
1527 Set_Analyzed (Expr_Q, False);
1532 Late_Expansion (Expr_Q, Etype (Expr_Q), Indexed_Comp));
1537 if Present (Expr) then
1539 -- Handle an initialization expression of a controlled type in
1540 -- case it denotes a function call. In general such a scenario
1541 -- will produce a transient scope, but this will lead to wrong
1542 -- order of initialization, adjustment, and finalization in the
1543 -- context of aggregates.
1545 -- Target (1) := Ctrl_Func_Call;
1548 -- Trans_Obj : ... := Ctrl_Func_Call; -- object
1549 -- Target (1) := Trans_Obj;
1550 -- Finalize (Trans_Obj);
1552 -- Target (1)._tag := ...;
1553 -- Adjust (Target (1));
1555 -- In the example above, the call to Finalize occurs too early
1556 -- and as a result it may leave the array component in a bad
1557 -- state. Finalization of the transient object should really
1558 -- happen after adjustment.
1560 -- To avoid this scenario, perform in-place side-effect removal
1561 -- of the function call. This eliminates the transient property
1562 -- of the function result and ensures correct order of actions.
1564 -- Res : ... := Ctrl_Func_Call;
1565 -- Target (1) := Res;
1566 -- Target (1)._tag := ...;
1567 -- Adjust (Target (1));
1570 if Present (Comp_Typ)
1571 and then Needs_Finalization (Comp_Typ)
1572 and then Nkind (Expr) /= N_Aggregate
1574 Initialize_Ctrl_Array_Component
1575 (Arr_Comp => Indexed_Comp,
1576 Comp_Typ => Comp_Typ,
1580 -- Otherwise perform simple component initialization
1583 Initialize_Array_Component
1584 (Arr_Comp => Indexed_Comp,
1585 Comp_Typ => Comp_Typ,
1590 -- Ada 2005 (AI-287): In case of default initialized component, call
1591 -- the initialization subprogram associated with the component type.
1592 -- If the component type is an access type, add an explicit null
1593 -- assignment, because for the back-end there is an initialization
1594 -- present for the whole aggregate, and no default initialization
1597 -- In addition, if the component type is controlled, we must call
1598 -- its Initialize procedure explicitly, because there is no explicit
1599 -- object creation that will invoke it otherwise.
1602 if Present (Base_Init_Proc (Base_Type (Ctype)))
1603 or else Has_Task (Base_Type (Ctype))
1605 Append_List_To (Stmts,
1606 Build_Initialization_Call (Loc,
1607 Id_Ref => Indexed_Comp,
1609 With_Default_Init => True));
1611 -- If the component type has invariants, add an invariant
1612 -- check after the component is default-initialized. It will
1613 -- be analyzed and resolved before the code for initialization
1614 -- of other components.
1616 if Has_Invariants (Ctype) then
1617 Set_Etype (Indexed_Comp, Ctype);
1618 Append_To (Stmts, Make_Invariant_Call (Indexed_Comp));
1621 elsif Is_Access_Type (Ctype) then
1623 Make_Assignment_Statement (Loc,
1624 Name => New_Copy_Tree (Indexed_Comp),
1625 Expression => Make_Null (Loc)));
1628 if Needs_Finalization (Ctype) then
1631 (Obj_Ref => New_Copy_Tree (Indexed_Comp),
1634 -- Guard against a missing [Deep_]Initialize when the component
1635 -- type was not properly frozen.
1637 if Present (Init_Call) then
1638 Append_To (Stmts, Init_Call);
1643 return Add_Loop_Actions (Stmts);
1650 function Gen_Loop (L, H : Node_Id; Expr : Node_Id) return List_Id is
1651 Is_Iterated_Component : constant Boolean :=
1652 Nkind (Parent (Expr)) = N_Iterated_Component_Association;
1663 -- Index_Base'(L) .. Index_Base'(H)
1665 L_Iteration_Scheme : Node_Id;
1666 -- L_J in Index_Base'(L) .. Index_Base'(H)
1669 -- The statements to execute in the loop
1671 S : constant List_Id := New_List;
1672 -- List of statements
1675 -- Copy of expression tree, used for checking purposes
1678 -- If loop bounds define an empty range return the null statement
1680 if Empty_Range (L, H) then
1681 Append_To (S, Make_Null_Statement (Loc));
1683 -- Ada 2005 (AI-287): Nothing else need to be done in case of
1684 -- default initialized component.
1690 -- The expression must be type-checked even though no component
1691 -- of the aggregate will have this value. This is done only for
1692 -- actual components of the array, not for subaggregates. Do
1693 -- the check on a copy, because the expression may be shared
1694 -- among several choices, some of which might be non-null.
1696 if Present (Etype (N))
1697 and then Is_Array_Type (Etype (N))
1698 and then No (Next_Index (Index))
1700 Expander_Mode_Save_And_Set (False);
1701 Tcopy := New_Copy_Tree (Expr);
1702 Set_Parent (Tcopy, N);
1703 Analyze_And_Resolve (Tcopy, Component_Type (Etype (N)));
1704 Expander_Mode_Restore;
1710 -- If loop bounds are the same then generate an assignment, unless
1711 -- the parent construct is an Iterated_Component_Association.
1713 elsif Equal (L, H) and then not Is_Iterated_Component then
1714 return Gen_Assign (New_Copy_Tree (L), Expr);
1716 -- If H - L <= 2 then generate a sequence of assignments when we are
1717 -- processing the bottom most aggregate and it contains scalar
1720 elsif No (Next_Index (Index))
1721 and then Scalar_Comp
1722 and then Local_Compile_Time_Known_Value (L)
1723 and then Local_Compile_Time_Known_Value (H)
1724 and then Local_Expr_Value (H) - Local_Expr_Value (L) <= 2
1725 and then not Is_Iterated_Component
1727 Append_List_To (S, Gen_Assign (New_Copy_Tree (L), Expr));
1728 Append_List_To (S, Gen_Assign (Add (1, To => L), Expr));
1730 if Local_Expr_Value (H) - Local_Expr_Value (L) = 2 then
1731 Append_List_To (S, Gen_Assign (Add (2, To => L), Expr));
1737 -- Otherwise construct the loop, starting with the loop index L_J
1739 if Is_Iterated_Component then
1741 Make_Defining_Identifier (Loc,
1742 Chars => (Chars (Defining_Identifier (Parent (Expr)))));
1745 L_J := Make_Temporary (Loc, 'J', L);
1748 -- Construct "L .. H" in Index_Base. We use a qualified expression
1749 -- for the bound to convert to the index base, but we don't need
1750 -- to do that if we already have the base type at hand.
1752 if Etype (L) = Index_Base then
1756 Make_Qualified_Expression (Loc,
1757 Subtype_Mark => Index_Base_Name,
1758 Expression => New_Copy_Tree (L));
1761 if Etype (H) = Index_Base then
1765 Make_Qualified_Expression (Loc,
1766 Subtype_Mark => Index_Base_Name,
1767 Expression => New_Copy_Tree (H));
1775 -- Construct "for L_J in Index_Base range L .. H"
1777 L_Iteration_Scheme :=
1778 Make_Iteration_Scheme
1780 Loop_Parameter_Specification =>
1781 Make_Loop_Parameter_Specification
1783 Defining_Identifier => L_J,
1784 Discrete_Subtype_Definition => L_Range));
1786 -- Construct the statements to execute in the loop body
1789 Gen_Assign (New_Occurrence_Of (L_J, Loc), Expr, In_Loop => True);
1791 -- Construct the final loop
1794 Make_Implicit_Loop_Statement
1796 Identifier => Empty,
1797 Iteration_Scheme => L_Iteration_Scheme,
1798 Statements => L_Body));
1800 -- A small optimization: if the aggregate is initialized with a box
1801 -- and the component type has no initialization procedure, remove the
1802 -- useless empty loop.
1804 if Nkind (First (S)) = N_Loop_Statement
1805 and then Is_Empty_List (Statements (First (S)))
1807 return New_List (Make_Null_Statement (Loc));
1817 -- The code built is
1819 -- W_J : Index_Base := L;
1820 -- while W_J < H loop
1821 -- W_J := Index_Base'Succ (W);
1825 function Gen_While (L, H : Node_Id; Expr : Node_Id) return List_Id is
1829 -- W_J : Base_Type := L;
1831 W_Iteration_Scheme : Node_Id;
1834 W_Index_Succ : Node_Id;
1835 -- Index_Base'Succ (J)
1837 W_Increment : Node_Id;
1838 -- W_J := Index_Base'Succ (W)
1840 W_Body : constant List_Id := New_List;
1841 -- The statements to execute in the loop
1843 S : constant List_Id := New_List;
1844 -- list of statement
1847 -- If loop bounds define an empty range or are equal return null
1849 if Empty_Range (L, H) or else Equal (L, H) then
1850 Append_To (S, Make_Null_Statement (Loc));
1854 -- Build the decl of W_J
1856 W_J := Make_Temporary (Loc, 'J', L);
1858 Make_Object_Declaration
1860 Defining_Identifier => W_J,
1861 Object_Definition => Index_Base_Name,
1864 -- Theoretically we should do a New_Copy_Tree (L) here, but we know
1865 -- that in this particular case L is a fresh Expr generated by
1866 -- Add which we are the only ones to use.
1868 Append_To (S, W_Decl);
1870 -- Construct " while W_J < H"
1872 W_Iteration_Scheme :=
1873 Make_Iteration_Scheme
1875 Condition => Make_Op_Lt
1877 Left_Opnd => New_Occurrence_Of (W_J, Loc),
1878 Right_Opnd => New_Copy_Tree (H)));
1880 -- Construct the statements to execute in the loop body
1883 Make_Attribute_Reference
1885 Prefix => Index_Base_Name,
1886 Attribute_Name => Name_Succ,
1887 Expressions => New_List (New_Occurrence_Of (W_J, Loc)));
1890 Make_OK_Assignment_Statement
1892 Name => New_Occurrence_Of (W_J, Loc),
1893 Expression => W_Index_Succ);
1895 Append_To (W_Body, W_Increment);
1897 Append_List_To (W_Body,
1898 Gen_Assign (New_Occurrence_Of (W_J, Loc), Expr, In_Loop => True));
1900 -- Construct the final loop
1903 Make_Implicit_Loop_Statement
1905 Identifier => Empty,
1906 Iteration_Scheme => W_Iteration_Scheme,
1907 Statements => W_Body));
1912 --------------------
1913 -- Get_Assoc_Expr --
1914 --------------------
1916 function Get_Assoc_Expr (Assoc : Node_Id) return Node_Id is
1917 Typ : constant Entity_Id := Base_Type (Etype (N));
1920 if Box_Present (Assoc) then
1921 if Is_Scalar_Type (Ctype) then
1922 if Present (Default_Aspect_Component_Value (Typ)) then
1923 return Default_Aspect_Component_Value (Typ);
1924 elsif Present (Default_Aspect_Value (Ctype)) then
1925 return Default_Aspect_Value (Ctype);
1935 return Expression (Assoc);
1939 ---------------------
1940 -- Index_Base_Name --
1941 ---------------------
1943 function Index_Base_Name return Node_Id is
1945 return New_Occurrence_Of (Index_Base, Sloc (N));
1946 end Index_Base_Name;
1948 ------------------------------------
1949 -- Local_Compile_Time_Known_Value --
1950 ------------------------------------
1952 function Local_Compile_Time_Known_Value (E : Node_Id) return Boolean is
1954 return Compile_Time_Known_Value (E)
1956 (Nkind (E) = N_Attribute_Reference
1957 and then Attribute_Name (E) = Name_Val
1958 and then Compile_Time_Known_Value (First (Expressions (E))));
1959 end Local_Compile_Time_Known_Value;
1961 ----------------------
1962 -- Local_Expr_Value --
1963 ----------------------
1965 function Local_Expr_Value (E : Node_Id) return Uint is
1967 if Compile_Time_Known_Value (E) then
1968 return Expr_Value (E);
1970 return Expr_Value (First (Expressions (E)));
1972 end Local_Expr_Value;
1976 New_Code : constant List_Id := New_List;
1978 Aggr_L : constant Node_Id := Low_Bound (Aggregate_Bounds (N));
1979 Aggr_H : constant Node_Id := High_Bound (Aggregate_Bounds (N));
1980 -- The aggregate bounds of this specific subaggregate. Note that if the
1981 -- code generated by Build_Array_Aggr_Code is executed then these bounds
1982 -- are OK. Otherwise a Constraint_Error would have been raised.
1984 Aggr_Low : constant Node_Id := Duplicate_Subexpr_No_Checks (Aggr_L);
1985 Aggr_High : constant Node_Id := Duplicate_Subexpr_No_Checks (Aggr_H);
1986 -- After Duplicate_Subexpr these are side-effect free
1995 Nb_Choices : Nat := 0;
1996 Table : Case_Table_Type (1 .. Number_Of_Choices (N));
1997 -- Used to sort all the different choice values
2000 -- Number of elements in the positional aggregate
2002 Others_Assoc : Node_Id := Empty;
2004 -- Start of processing for Build_Array_Aggr_Code
2007 -- First before we start, a special case. if we have a bit packed
2008 -- array represented as a modular type, then clear the value to
2009 -- zero first, to ensure that unused bits are properly cleared.
2014 and then Is_Bit_Packed_Array (Typ)
2015 and then Is_Modular_Integer_Type (Packed_Array_Impl_Type (Typ))
2017 Append_To (New_Code,
2018 Make_Assignment_Statement (Loc,
2019 Name => New_Copy_Tree (Into),
2021 Unchecked_Convert_To (Typ,
2022 Make_Integer_Literal (Loc, Uint_0))));
2025 -- If the component type contains tasks, we need to build a Master
2026 -- entity in the current scope, because it will be needed if build-
2027 -- in-place functions are called in the expanded code.
2029 if Nkind (Parent (N)) = N_Object_Declaration and then Has_Task (Typ) then
2030 Build_Master_Entity (Defining_Identifier (Parent (N)));
2033 -- STEP 1: Process component associations
2035 -- For those associations that may generate a loop, initialize
2036 -- Loop_Actions to collect inserted actions that may be crated.
2038 -- Skip this if no component associations
2040 if No (Expressions (N)) then
2042 -- STEP 1 (a): Sort the discrete choices
2044 Assoc := First (Component_Associations (N));
2045 while Present (Assoc) loop
2046 Choice := First (Choice_List (Assoc));
2047 while Present (Choice) loop
2048 if Nkind (Choice) = N_Others_Choice then
2049 Set_Loop_Actions (Assoc, New_List);
2050 Others_Assoc := Assoc;
2054 Get_Index_Bounds (Choice, Low, High);
2057 Set_Loop_Actions (Assoc, New_List);
2060 Nb_Choices := Nb_Choices + 1;
2062 Table (Nb_Choices) :=
2065 Choice_Node => Get_Assoc_Expr (Assoc));
2073 -- If there is more than one set of choices these must be static
2074 -- and we can therefore sort them. Remember that Nb_Choices does not
2075 -- account for an others choice.
2077 if Nb_Choices > 1 then
2078 Sort_Case_Table (Table);
2081 -- STEP 1 (b): take care of the whole set of discrete choices
2083 for J in 1 .. Nb_Choices loop
2084 Low := Table (J).Choice_Lo;
2085 High := Table (J).Choice_Hi;
2086 Expr := Table (J).Choice_Node;
2087 Append_List (Gen_Loop (Low, High, Expr), To => New_Code);
2090 -- STEP 1 (c): generate the remaining loops to cover others choice
2091 -- We don't need to generate loops over empty gaps, but if there is
2092 -- a single empty range we must analyze the expression for semantics
2094 if Present (Others_Assoc) then
2096 First : Boolean := True;
2099 for J in 0 .. Nb_Choices loop
2103 Low := Add (1, To => Table (J).Choice_Hi);
2106 if J = Nb_Choices then
2109 High := Add (-1, To => Table (J + 1).Choice_Lo);
2112 -- If this is an expansion within an init proc, make
2113 -- sure that discriminant references are replaced by
2114 -- the corresponding discriminal.
2116 if Inside_Init_Proc then
2117 if Is_Entity_Name (Low)
2118 and then Ekind (Entity (Low)) = E_Discriminant
2120 Set_Entity (Low, Discriminal (Entity (Low)));
2123 if Is_Entity_Name (High)
2124 and then Ekind (Entity (High)) = E_Discriminant
2126 Set_Entity (High, Discriminal (Entity (High)));
2131 or else not Empty_Range (Low, High)
2135 (Gen_Loop (Low, High,
2136 Get_Assoc_Expr (Others_Assoc)), To => New_Code);
2142 -- STEP 2: Process positional components
2145 -- STEP 2 (a): Generate the assignments for each positional element
2146 -- Note that here we have to use Aggr_L rather than Aggr_Low because
2147 -- Aggr_L is analyzed and Add wants an analyzed expression.
2149 Expr := First (Expressions (N));
2151 while Present (Expr) loop
2152 Nb_Elements := Nb_Elements + 1;
2153 Append_List (Gen_Assign (Add (Nb_Elements, To => Aggr_L), Expr),
2158 -- STEP 2 (b): Generate final loop if an others choice is present
2159 -- Here Nb_Elements gives the offset of the last positional element.
2161 if Present (Component_Associations (N)) then
2162 Assoc := Last (Component_Associations (N));
2164 -- Ada 2005 (AI-287)
2166 Append_List (Gen_While (Add (Nb_Elements, To => Aggr_L),
2168 Get_Assoc_Expr (Assoc)), -- AI-287
2174 end Build_Array_Aggr_Code;
2176 ----------------------------
2177 -- Build_Record_Aggr_Code --
2178 ----------------------------
2180 function Build_Record_Aggr_Code
2183 Lhs : Node_Id) return List_Id
2185 Loc : constant Source_Ptr := Sloc (N);
2186 L : constant List_Id := New_List;
2187 N_Typ : constant Entity_Id := Etype (N);
2193 Comp_Type : Entity_Id;
2194 Selector : Entity_Id;
2195 Comp_Expr : Node_Id;
2198 -- If this is an internal aggregate, the External_Final_List is an
2199 -- expression for the controller record of the enclosing type.
2201 -- If the current aggregate has several controlled components, this
2202 -- expression will appear in several calls to attach to the finali-
2203 -- zation list, and it must not be shared.
2205 Ancestor_Is_Expression : Boolean := False;
2206 Ancestor_Is_Subtype_Mark : Boolean := False;
2208 Init_Typ : Entity_Id := Empty;
2210 Finalization_Done : Boolean := False;
2211 -- True if Generate_Finalization_Actions has already been called; calls
2212 -- after the first do nothing.
2214 function Ancestor_Discriminant_Value (Disc : Entity_Id) return Node_Id;
2215 -- Returns the value that the given discriminant of an ancestor type
2216 -- should receive (in the absence of a conflict with the value provided
2217 -- by an ancestor part of an extension aggregate).
2219 procedure Check_Ancestor_Discriminants (Anc_Typ : Entity_Id);
2220 -- Check that each of the discriminant values defined by the ancestor
2221 -- part of an extension aggregate match the corresponding values
2222 -- provided by either an association of the aggregate or by the
2223 -- constraint imposed by a parent type (RM95-4.3.2(8)).
2225 function Compatible_Int_Bounds
2226 (Agg_Bounds : Node_Id;
2227 Typ_Bounds : Node_Id) return Boolean;
2228 -- Return true if Agg_Bounds are equal or within Typ_Bounds. It is
2229 -- assumed that both bounds are integer ranges.
2231 procedure Generate_Finalization_Actions;
2232 -- Deal with the various controlled type data structure initializations
2233 -- (but only if it hasn't been done already).
2235 function Get_Constraint_Association (T : Entity_Id) return Node_Id;
2236 -- Returns the first discriminant association in the constraint
2237 -- associated with T, if any, otherwise returns Empty.
2239 function Get_Explicit_Discriminant_Value (D : Entity_Id) return Node_Id;
2240 -- If the ancestor part is an unconstrained type and further ancestors
2241 -- do not provide discriminants for it, check aggregate components for
2242 -- values of the discriminants.
2244 procedure Init_Hidden_Discriminants (Typ : Entity_Id; List : List_Id);
2245 -- If Typ is derived, and constrains discriminants of the parent type,
2246 -- these discriminants are not components of the aggregate, and must be
2247 -- initialized. The assignments are appended to List. The same is done
2248 -- if Typ derives fron an already constrained subtype of a discriminated
2251 procedure Init_Stored_Discriminants;
2252 -- If the type is derived and has inherited discriminants, generate
2253 -- explicit assignments for each, using the store constraint of the
2254 -- type. Note that both visible and stored discriminants must be
2255 -- initialized in case the derived type has some renamed and some
2256 -- constrained discriminants.
2258 procedure Init_Visible_Discriminants;
2259 -- If type has discriminants, retrieve their values from aggregate,
2260 -- and generate explicit assignments for each. This does not include
2261 -- discriminants inherited from ancestor, which are handled above.
2262 -- The type of the aggregate is a subtype created ealier using the
2263 -- given values of the discriminant components of the aggregate.
2265 procedure Initialize_Ctrl_Record_Component
2266 (Rec_Comp : Node_Id;
2267 Comp_Typ : Entity_Id;
2268 Init_Expr : Node_Id;
2270 -- Perform the initialization of controlled record component Rec_Comp.
2271 -- Comp_Typ is the component type. Init_Expr is the initialization
2272 -- expression for the record component. Hook-related declarations are
2273 -- inserted prior to aggregate N using Insert_Action. All remaining
2274 -- generated code is added to list Stmts.
2276 procedure Initialize_Record_Component
2277 (Rec_Comp : Node_Id;
2278 Comp_Typ : Entity_Id;
2279 Init_Expr : Node_Id;
2281 -- Perform the initialization of record component Rec_Comp. Comp_Typ
2282 -- is the component type. Init_Expr is the initialization expression
2283 -- of the record component. All generated code is added to list Stmts.
2285 function Is_Int_Range_Bounds (Bounds : Node_Id) return Boolean;
2286 -- Check whether Bounds is a range node and its lower and higher bounds
2287 -- are integers literals.
2289 function Replace_Type (Expr : Node_Id) return Traverse_Result;
2290 -- If the aggregate contains a self-reference, traverse each expression
2291 -- to replace a possible self-reference with a reference to the proper
2292 -- component of the target of the assignment.
2294 function Rewrite_Discriminant (Expr : Node_Id) return Traverse_Result;
2295 -- If default expression of a component mentions a discriminant of the
2296 -- type, it must be rewritten as the discriminant of the target object.
2298 ---------------------------------
2299 -- Ancestor_Discriminant_Value --
2300 ---------------------------------
2302 function Ancestor_Discriminant_Value (Disc : Entity_Id) return Node_Id is
2304 Assoc_Elmt : Elmt_Id;
2305 Aggr_Comp : Entity_Id;
2306 Corresp_Disc : Entity_Id;
2307 Current_Typ : Entity_Id := Base_Type (Typ);
2308 Parent_Typ : Entity_Id;
2309 Parent_Disc : Entity_Id;
2310 Save_Assoc : Node_Id := Empty;
2313 -- First check any discriminant associations to see if any of them
2314 -- provide a value for the discriminant.
2316 if Present (Discriminant_Specifications (Parent (Current_Typ))) then
2317 Assoc := First (Component_Associations (N));
2318 while Present (Assoc) loop
2319 Aggr_Comp := Entity (First (Choices (Assoc)));
2321 if Ekind (Aggr_Comp) = E_Discriminant then
2322 Save_Assoc := Expression (Assoc);
2324 Corresp_Disc := Corresponding_Discriminant (Aggr_Comp);
2325 while Present (Corresp_Disc) loop
2327 -- If found a corresponding discriminant then return the
2328 -- value given in the aggregate. (Note: this is not
2329 -- correct in the presence of side effects. ???)
2331 if Disc = Corresp_Disc then
2332 return Duplicate_Subexpr (Expression (Assoc));
2335 Corresp_Disc := Corresponding_Discriminant (Corresp_Disc);
2343 -- No match found in aggregate, so chain up parent types to find
2344 -- a constraint that defines the value of the discriminant.
2346 Parent_Typ := Etype (Current_Typ);
2347 while Current_Typ /= Parent_Typ loop
2348 if Has_Discriminants (Parent_Typ)
2349 and then not Has_Unknown_Discriminants (Parent_Typ)
2351 Parent_Disc := First_Discriminant (Parent_Typ);
2353 -- We either get the association from the subtype indication
2354 -- of the type definition itself, or from the discriminant
2355 -- constraint associated with the type entity (which is
2356 -- preferable, but it's not always present ???)
2358 if Is_Empty_Elmt_List (Discriminant_Constraint (Current_Typ))
2360 Assoc := Get_Constraint_Association (Current_Typ);
2361 Assoc_Elmt := No_Elmt;
2364 First_Elmt (Discriminant_Constraint (Current_Typ));
2365 Assoc := Node (Assoc_Elmt);
2368 -- Traverse the discriminants of the parent type looking
2369 -- for one that corresponds.
2371 while Present (Parent_Disc) and then Present (Assoc) loop
2372 Corresp_Disc := Parent_Disc;
2373 while Present (Corresp_Disc)
2374 and then Disc /= Corresp_Disc
2376 Corresp_Disc := Corresponding_Discriminant (Corresp_Disc);
2379 if Disc = Corresp_Disc then
2380 if Nkind (Assoc) = N_Discriminant_Association then
2381 Assoc := Expression (Assoc);
2384 -- If the located association directly denotes
2385 -- a discriminant, then use the value of a saved
2386 -- association of the aggregate. This is an approach
2387 -- used to handle certain cases involving multiple
2388 -- discriminants mapped to a single discriminant of
2389 -- a descendant. It's not clear how to locate the
2390 -- appropriate discriminant value for such cases. ???
2392 if Is_Entity_Name (Assoc)
2393 and then Ekind (Entity (Assoc)) = E_Discriminant
2395 Assoc := Save_Assoc;
2398 return Duplicate_Subexpr (Assoc);
2401 Next_Discriminant (Parent_Disc);
2403 if No (Assoc_Elmt) then
2407 Next_Elmt (Assoc_Elmt);
2409 if Present (Assoc_Elmt) then
2410 Assoc := Node (Assoc_Elmt);
2418 Current_Typ := Parent_Typ;
2419 Parent_Typ := Etype (Current_Typ);
2422 -- In some cases there's no ancestor value to locate (such as
2423 -- when an ancestor part given by an expression defines the
2424 -- discriminant value).
2427 end Ancestor_Discriminant_Value;
2429 ----------------------------------
2430 -- Check_Ancestor_Discriminants --
2431 ----------------------------------
2433 procedure Check_Ancestor_Discriminants (Anc_Typ : Entity_Id) is
2435 Disc_Value : Node_Id;
2439 Discr := First_Discriminant (Base_Type (Anc_Typ));
2440 while Present (Discr) loop
2441 Disc_Value := Ancestor_Discriminant_Value (Discr);
2443 if Present (Disc_Value) then
2444 Cond := Make_Op_Ne (Loc,
2446 Make_Selected_Component (Loc,
2447 Prefix => New_Copy_Tree (Target),
2448 Selector_Name => New_Occurrence_Of (Discr, Loc)),
2449 Right_Opnd => Disc_Value);
2452 Make_Raise_Constraint_Error (Loc,
2454 Reason => CE_Discriminant_Check_Failed));
2457 Next_Discriminant (Discr);
2459 end Check_Ancestor_Discriminants;
2461 ---------------------------
2462 -- Compatible_Int_Bounds --
2463 ---------------------------
2465 function Compatible_Int_Bounds
2466 (Agg_Bounds : Node_Id;
2467 Typ_Bounds : Node_Id) return Boolean
2469 Agg_Lo : constant Uint := Intval (Low_Bound (Agg_Bounds));
2470 Agg_Hi : constant Uint := Intval (High_Bound (Agg_Bounds));
2471 Typ_Lo : constant Uint := Intval (Low_Bound (Typ_Bounds));
2472 Typ_Hi : constant Uint := Intval (High_Bound (Typ_Bounds));
2474 return Typ_Lo <= Agg_Lo and then Agg_Hi <= Typ_Hi;
2475 end Compatible_Int_Bounds;
2477 -----------------------------------
2478 -- Generate_Finalization_Actions --
2479 -----------------------------------
2481 procedure Generate_Finalization_Actions is
2483 -- Do the work only the first time this is called
2485 if Finalization_Done then
2489 Finalization_Done := True;
2491 -- Determine the external finalization list. It is either the
2492 -- finalization list of the outer scope or the one coming from an
2493 -- outer aggregate. When the target is not a temporary, the proper
2494 -- scope is the scope of the target rather than the potentially
2495 -- transient current scope.
2497 if Is_Controlled (Typ) and then Ancestor_Is_Subtype_Mark then
2498 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
2499 Set_Assignment_OK (Ref);
2502 Make_Procedure_Call_Statement (Loc,
2505 (Find_Prim_Op (Init_Typ, Name_Initialize), Loc),
2506 Parameter_Associations => New_List (New_Copy_Tree (Ref))));
2508 end Generate_Finalization_Actions;
2510 --------------------------------
2511 -- Get_Constraint_Association --
2512 --------------------------------
2514 function Get_Constraint_Association (T : Entity_Id) return Node_Id is
2521 -- If type is private, get constraint from full view. This was
2522 -- previously done in an instance context, but is needed whenever
2523 -- the ancestor part has a discriminant, possibly inherited through
2524 -- multiple derivations.
2526 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
2527 Typ := Full_View (Typ);
2530 Indic := Subtype_Indication (Type_Definition (Parent (Typ)));
2532 -- Verify that the subtype indication carries a constraint
2534 if Nkind (Indic) = N_Subtype_Indication
2535 and then Present (Constraint (Indic))
2537 return First (Constraints (Constraint (Indic)));
2541 end Get_Constraint_Association;
2543 -------------------------------------
2544 -- Get_Explicit_Discriminant_Value --
2545 -------------------------------------
2547 function Get_Explicit_Discriminant_Value
2548 (D : Entity_Id) return Node_Id
2555 -- The aggregate has been normalized and all associations have a
2558 Assoc := First (Component_Associations (N));
2559 while Present (Assoc) loop
2560 Choice := First (Choices (Assoc));
2562 if Chars (Choice) = Chars (D) then
2563 Val := Expression (Assoc);
2572 end Get_Explicit_Discriminant_Value;
2574 -------------------------------
2575 -- Init_Hidden_Discriminants --
2576 -------------------------------
2578 procedure Init_Hidden_Discriminants (Typ : Entity_Id; List : List_Id) is
2579 function Is_Completely_Hidden_Discriminant
2580 (Discr : Entity_Id) return Boolean;
2581 -- Determine whether Discr is a completely hidden discriminant of
2584 ---------------------------------------
2585 -- Is_Completely_Hidden_Discriminant --
2586 ---------------------------------------
2588 function Is_Completely_Hidden_Discriminant
2589 (Discr : Entity_Id) return Boolean
2594 -- Use First/Next_Entity as First/Next_Discriminant do not yield
2595 -- completely hidden discriminants.
2597 Item := First_Entity (Typ);
2598 while Present (Item) loop
2599 if Ekind (Item) = E_Discriminant
2600 and then Is_Completely_Hidden (Item)
2601 and then Chars (Original_Record_Component (Item)) =
2611 end Is_Completely_Hidden_Discriminant;
2615 Base_Typ : Entity_Id;
2617 Discr_Constr : Elmt_Id;
2618 Discr_Init : Node_Id;
2619 Discr_Val : Node_Id;
2620 In_Aggr_Type : Boolean;
2621 Par_Typ : Entity_Id;
2623 -- Start of processing for Init_Hidden_Discriminants
2626 -- The constraints on the hidden discriminants, if present, are kept
2627 -- in the Stored_Constraint list of the type itself, or in that of
2628 -- the base type. If not in the constraints of the aggregate itself,
2629 -- we examine ancestors to find discriminants that are not renamed
2630 -- by other discriminants but constrained explicitly.
2632 In_Aggr_Type := True;
2634 Base_Typ := Base_Type (Typ);
2635 while Is_Derived_Type (Base_Typ)
2637 (Present (Stored_Constraint (Base_Typ))
2639 (In_Aggr_Type and then Present (Stored_Constraint (Typ))))
2641 Par_Typ := Etype (Base_Typ);
2643 if not Has_Discriminants (Par_Typ) then
2647 Discr := First_Discriminant (Par_Typ);
2649 -- We know that one of the stored-constraint lists is present
2651 if Present (Stored_Constraint (Base_Typ)) then
2652 Discr_Constr := First_Elmt (Stored_Constraint (Base_Typ));
2654 -- For private extension, stored constraint may be on full view
2656 elsif Is_Private_Type (Base_Typ)
2657 and then Present (Full_View (Base_Typ))
2658 and then Present (Stored_Constraint (Full_View (Base_Typ)))
2661 First_Elmt (Stored_Constraint (Full_View (Base_Typ)));
2664 Discr_Constr := First_Elmt (Stored_Constraint (Typ));
2667 while Present (Discr) and then Present (Discr_Constr) loop
2668 Discr_Val := Node (Discr_Constr);
2670 -- The parent discriminant is renamed in the derived type,
2671 -- nothing to initialize.
2673 -- type Deriv_Typ (Discr : ...)
2674 -- is new Parent_Typ (Discr => Discr);
2676 if Is_Entity_Name (Discr_Val)
2677 and then Ekind (Entity (Discr_Val)) = E_Discriminant
2681 -- When the parent discriminant is constrained at the type
2682 -- extension level, it does not appear in the derived type.
2684 -- type Deriv_Typ (Discr : ...)
2685 -- is new Parent_Typ (Discr => Discr,
2686 -- Hidden_Discr => Expression);
2688 elsif Is_Completely_Hidden_Discriminant (Discr) then
2691 -- Otherwise initialize the discriminant
2695 Make_OK_Assignment_Statement (Loc,
2697 Make_Selected_Component (Loc,
2698 Prefix => New_Copy_Tree (Target),
2699 Selector_Name => New_Occurrence_Of (Discr, Loc)),
2700 Expression => New_Copy_Tree (Discr_Val));
2702 Set_No_Ctrl_Actions (Discr_Init);
2703 Append_To (List, Discr_Init);
2706 Next_Elmt (Discr_Constr);
2707 Next_Discriminant (Discr);
2710 In_Aggr_Type := False;
2711 Base_Typ := Base_Type (Par_Typ);
2713 end Init_Hidden_Discriminants;
2715 --------------------------------
2716 -- Init_Visible_Discriminants --
2717 --------------------------------
2719 procedure Init_Visible_Discriminants is
2720 Discriminant : Entity_Id;
2721 Discriminant_Value : Node_Id;
2724 Discriminant := First_Discriminant (Typ);
2725 while Present (Discriminant) loop
2727 Make_Selected_Component (Loc,
2728 Prefix => New_Copy_Tree (Target),
2729 Selector_Name => New_Occurrence_Of (Discriminant, Loc));
2731 Discriminant_Value :=
2732 Get_Discriminant_Value
2733 (Discriminant, Typ, Discriminant_Constraint (N_Typ));
2736 Make_OK_Assignment_Statement (Loc,
2738 Expression => New_Copy_Tree (Discriminant_Value));
2740 Set_No_Ctrl_Actions (Instr);
2741 Append_To (L, Instr);
2743 Next_Discriminant (Discriminant);
2745 end Init_Visible_Discriminants;
2747 -------------------------------
2748 -- Init_Stored_Discriminants --
2749 -------------------------------
2751 procedure Init_Stored_Discriminants is
2752 Discriminant : Entity_Id;
2753 Discriminant_Value : Node_Id;
2756 Discriminant := First_Stored_Discriminant (Typ);
2757 while Present (Discriminant) loop
2759 Make_Selected_Component (Loc,
2760 Prefix => New_Copy_Tree (Target),
2761 Selector_Name => New_Occurrence_Of (Discriminant, Loc));
2763 Discriminant_Value :=
2764 Get_Discriminant_Value
2765 (Discriminant, N_Typ, Discriminant_Constraint (N_Typ));
2768 Make_OK_Assignment_Statement (Loc,
2770 Expression => New_Copy_Tree (Discriminant_Value));
2772 Set_No_Ctrl_Actions (Instr);
2773 Append_To (L, Instr);
2775 Next_Stored_Discriminant (Discriminant);
2777 end Init_Stored_Discriminants;
2779 --------------------------------------
2780 -- Initialize_Ctrl_Record_Component --
2781 --------------------------------------
2783 procedure Initialize_Ctrl_Record_Component
2784 (Rec_Comp : Node_Id;
2785 Comp_Typ : Entity_Id;
2786 Init_Expr : Node_Id;
2790 Hook_Clear : Node_Id;
2792 In_Place_Expansion : Boolean;
2793 -- Flag set when a nonlimited controlled function call requires
2794 -- in-place expansion.
2797 -- Perform a preliminary analysis and resolution to determine what
2798 -- the initialization expression denotes. Unanalyzed function calls
2799 -- may appear as identifiers or indexed components.
2801 if Nkind_In (Init_Expr, N_Function_Call,
2803 N_Indexed_Component)
2804 and then not Analyzed (Init_Expr)
2806 Preanalyze_And_Resolve (Init_Expr, Comp_Typ);
2809 In_Place_Expansion :=
2810 Nkind (Init_Expr) = N_Function_Call
2811 and then not Is_Limited_Type (Comp_Typ);
2813 -- The initialization expression is a controlled function call.
2814 -- Perform in-place removal of side effects to avoid creating a
2817 -- This in-place expansion is not performed for limited transient
2818 -- objects because the initialization is already done in place.
2820 if In_Place_Expansion then
2822 -- Suppress the removal of side effects by general analysis
2823 -- because this behavior is emulated here. This avoids the
2824 -- generation of a transient scope, which leads to out-of-order
2825 -- adjustment and finalization.
2827 Set_No_Side_Effect_Removal (Init_Expr);
2829 -- Install all hook-related declarations and prepare the clean up
2832 Process_Transient_Component
2834 Comp_Typ => Comp_Typ,
2835 Init_Expr => Init_Expr,
2836 Fin_Call => Fin_Call,
2837 Hook_Clear => Hook_Clear,
2841 -- Use the noncontrolled component initialization circuitry to
2842 -- assign the result of the function call to the record component.
2843 -- This also performs tag adjustment and [deep] adjustment of the
2844 -- record component.
2846 Initialize_Record_Component
2847 (Rec_Comp => Rec_Comp,
2848 Comp_Typ => Comp_Typ,
2849 Init_Expr => Init_Expr,
2852 -- At this point the record component is fully initialized. Complete
2853 -- the processing of the controlled record component by finalizing
2854 -- the transient function result.
2856 if In_Place_Expansion then
2857 Process_Transient_Component_Completion
2860 Fin_Call => Fin_Call,
2861 Hook_Clear => Hook_Clear,
2864 end Initialize_Ctrl_Record_Component;
2866 ---------------------------------
2867 -- Initialize_Record_Component --
2868 ---------------------------------
2870 procedure Initialize_Record_Component
2871 (Rec_Comp : Node_Id;
2872 Comp_Typ : Entity_Id;
2873 Init_Expr : Node_Id;
2876 Exceptions_OK : constant Boolean :=
2877 not Restriction_Active (No_Exception_Propagation);
2879 Finalization_OK : constant Boolean := Needs_Finalization (Comp_Typ);
2881 Full_Typ : constant Entity_Id := Underlying_Type (Comp_Typ);
2883 Blk_Stmts : List_Id;
2884 Init_Stmt : Node_Id;
2887 -- Protect the initialization statements from aborts. Generate:
2891 if Finalization_OK and Abort_Allowed then
2892 if Exceptions_OK then
2893 Blk_Stmts := New_List;
2898 Append_To (Blk_Stmts, Build_Runtime_Call (Loc, RE_Abort_Defer));
2900 -- Otherwise aborts are not allowed. All generated code is added
2901 -- directly to the input list.
2907 -- Initialize the record component. Generate:
2909 -- Rec_Comp := Init_Expr;
2911 -- Note that the initialization expression is NOT replicated because
2912 -- only a single component may be initialized by it.
2915 Make_OK_Assignment_Statement (Loc,
2916 Name => New_Copy_Tree (Rec_Comp),
2917 Expression => Init_Expr);
2918 Set_No_Ctrl_Actions (Init_Stmt);
2920 Append_To (Blk_Stmts, Init_Stmt);
2922 -- Adjust the tag due to a possible view conversion. Generate:
2924 -- Rec_Comp._tag := Full_TypeP;
2926 if Tagged_Type_Expansion and then Is_Tagged_Type (Comp_Typ) then
2927 Append_To (Blk_Stmts,
2928 Make_OK_Assignment_Statement (Loc,
2930 Make_Selected_Component (Loc,
2931 Prefix => New_Copy_Tree (Rec_Comp),
2934 (First_Tag_Component (Full_Typ), Loc)),
2937 Unchecked_Convert_To (RTE (RE_Tag),
2939 (Node (First_Elmt (Access_Disp_Table (Full_Typ))),
2943 -- Adjust the component. Generate:
2945 -- [Deep_]Adjust (Rec_Comp);
2947 if Finalization_OK and then not Is_Limited_Type (Comp_Typ) then
2950 (Obj_Ref => New_Copy_Tree (Rec_Comp),
2953 -- Guard against a missing [Deep_]Adjust when the component type
2954 -- was not properly frozen.
2956 if Present (Adj_Call) then
2957 Append_To (Blk_Stmts, Adj_Call);
2961 -- Complete the protection of the initialization statements
2963 if Finalization_OK and Abort_Allowed then
2965 -- Wrap the initialization statements in a block to catch a
2966 -- potential exception. Generate:
2970 -- Rec_Comp := Init_Expr;
2971 -- Rec_Comp._tag := Full_TypP;
2972 -- [Deep_]Adjust (Rec_Comp);
2974 -- Abort_Undefer_Direct;
2977 if Exceptions_OK then
2979 Build_Abort_Undefer_Block (Loc,
2983 -- Otherwise exceptions are not propagated. Generate:
2986 -- Rec_Comp := Init_Expr;
2987 -- Rec_Comp._tag := Full_TypP;
2988 -- [Deep_]Adjust (Rec_Comp);
2992 Append_To (Blk_Stmts,
2993 Build_Runtime_Call (Loc, RE_Abort_Undefer));
2996 end Initialize_Record_Component;
2998 -------------------------
2999 -- Is_Int_Range_Bounds --
3000 -------------------------
3002 function Is_Int_Range_Bounds (Bounds : Node_Id) return Boolean is
3004 return Nkind (Bounds) = N_Range
3005 and then Nkind (Low_Bound (Bounds)) = N_Integer_Literal
3006 and then Nkind (High_Bound (Bounds)) = N_Integer_Literal;
3007 end Is_Int_Range_Bounds;
3013 function Replace_Type (Expr : Node_Id) return Traverse_Result is
3015 -- Note regarding the Root_Type test below: Aggregate components for
3016 -- self-referential types include attribute references to the current
3017 -- instance, of the form: Typ'access, etc.. These references are
3018 -- rewritten as references to the target of the aggregate: the
3019 -- left-hand side of an assignment, the entity in a declaration,
3020 -- or a temporary. Without this test, we would improperly extended
3021 -- this rewriting to attribute references whose prefix was not the
3022 -- type of the aggregate.
3024 if Nkind (Expr) = N_Attribute_Reference
3025 and then Is_Entity_Name (Prefix (Expr))
3026 and then Is_Type (Entity (Prefix (Expr)))
3027 and then Root_Type (Etype (N)) = Root_Type (Entity (Prefix (Expr)))
3029 if Is_Entity_Name (Lhs) then
3030 Rewrite (Prefix (Expr),
3031 New_Occurrence_Of (Entity (Lhs), Loc));
3033 elsif Nkind (Lhs) = N_Selected_Component then
3035 Make_Attribute_Reference (Loc,
3036 Attribute_Name => Name_Unrestricted_Access,
3037 Prefix => New_Copy_Tree (Lhs)));
3038 Set_Analyzed (Parent (Expr), False);
3042 Make_Attribute_Reference (Loc,
3043 Attribute_Name => Name_Unrestricted_Access,
3044 Prefix => New_Copy_Tree (Lhs)));
3045 Set_Analyzed (Parent (Expr), False);
3052 --------------------------
3053 -- Rewrite_Discriminant --
3054 --------------------------
3056 function Rewrite_Discriminant (Expr : Node_Id) return Traverse_Result is
3058 if Is_Entity_Name (Expr)
3059 and then Present (Entity (Expr))
3060 and then Ekind (Entity (Expr)) = E_In_Parameter
3061 and then Present (Discriminal_Link (Entity (Expr)))
3062 and then Scope (Discriminal_Link (Entity (Expr))) =
3063 Base_Type (Etype (N))
3066 Make_Selected_Component (Loc,
3067 Prefix => New_Copy_Tree (Lhs),
3068 Selector_Name => Make_Identifier (Loc, Chars (Expr))));
3072 end Rewrite_Discriminant;
3074 procedure Replace_Discriminants is
3075 new Traverse_Proc (Rewrite_Discriminant);
3077 procedure Replace_Self_Reference is
3078 new Traverse_Proc (Replace_Type);
3080 -- Start of processing for Build_Record_Aggr_Code
3083 if Has_Self_Reference (N) then
3084 Replace_Self_Reference (N);
3087 -- If the target of the aggregate is class-wide, we must convert it
3088 -- to the actual type of the aggregate, so that the proper components
3089 -- are visible. We know already that the types are compatible.
3091 if Present (Etype (Lhs))
3092 and then Is_Class_Wide_Type (Etype (Lhs))
3094 Target := Unchecked_Convert_To (Typ, Lhs);
3099 -- Deal with the ancestor part of extension aggregates or with the
3100 -- discriminants of the root type.
3102 if Nkind (N) = N_Extension_Aggregate then
3104 Ancestor : constant Node_Id := Ancestor_Part (N);
3109 -- If the ancestor part is a subtype mark "T", we generate
3111 -- init-proc (T (tmp)); if T is constrained and
3112 -- init-proc (S (tmp)); where S applies an appropriate
3113 -- constraint if T is unconstrained
3115 if Is_Entity_Name (Ancestor)
3116 and then Is_Type (Entity (Ancestor))
3118 Ancestor_Is_Subtype_Mark := True;
3120 if Is_Constrained (Entity (Ancestor)) then
3121 Init_Typ := Entity (Ancestor);
3123 -- For an ancestor part given by an unconstrained type mark,
3124 -- create a subtype constrained by appropriate corresponding
3125 -- discriminant values coming from either associations of the
3126 -- aggregate or a constraint on a parent type. The subtype will
3127 -- be used to generate the correct default value for the
3130 elsif Has_Discriminants (Entity (Ancestor)) then
3132 Anc_Typ : constant Entity_Id := Entity (Ancestor);
3133 Anc_Constr : constant List_Id := New_List;
3134 Discrim : Entity_Id;
3135 Disc_Value : Node_Id;
3136 New_Indic : Node_Id;
3137 Subt_Decl : Node_Id;
3140 Discrim := First_Discriminant (Anc_Typ);
3141 while Present (Discrim) loop
3142 Disc_Value := Ancestor_Discriminant_Value (Discrim);
3144 -- If no usable discriminant in ancestors, check
3145 -- whether aggregate has an explicit value for it.
3147 if No (Disc_Value) then
3149 Get_Explicit_Discriminant_Value (Discrim);
3152 Append_To (Anc_Constr, Disc_Value);
3153 Next_Discriminant (Discrim);
3157 Make_Subtype_Indication (Loc,
3158 Subtype_Mark => New_Occurrence_Of (Anc_Typ, Loc),
3160 Make_Index_Or_Discriminant_Constraint (Loc,
3161 Constraints => Anc_Constr));
3163 Init_Typ := Create_Itype (Ekind (Anc_Typ), N);
3166 Make_Subtype_Declaration (Loc,
3167 Defining_Identifier => Init_Typ,
3168 Subtype_Indication => New_Indic);
3170 -- Itypes must be analyzed with checks off Declaration
3171 -- must have a parent for proper handling of subsidiary
3174 Set_Parent (Subt_Decl, N);
3175 Analyze (Subt_Decl, Suppress => All_Checks);
3179 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
3180 Set_Assignment_OK (Ref);
3182 if not Is_Interface (Init_Typ) then
3184 Build_Initialization_Call (Loc,
3187 In_Init_Proc => Within_Init_Proc,
3188 With_Default_Init => Has_Default_Init_Comps (N)
3190 Has_Task (Base_Type (Init_Typ))));
3192 if Is_Constrained (Entity (Ancestor))
3193 and then Has_Discriminants (Entity (Ancestor))
3195 Check_Ancestor_Discriminants (Entity (Ancestor));
3199 -- Handle calls to C++ constructors
3201 elsif Is_CPP_Constructor_Call (Ancestor) then
3202 Init_Typ := Etype (Ancestor);
3203 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
3204 Set_Assignment_OK (Ref);
3207 Build_Initialization_Call (Loc,
3210 In_Init_Proc => Within_Init_Proc,
3211 With_Default_Init => Has_Default_Init_Comps (N),
3212 Constructor_Ref => Ancestor));
3214 -- Ada 2005 (AI-287): If the ancestor part is an aggregate of
3215 -- limited type, a recursive call expands the ancestor. Note that
3216 -- in the limited case, the ancestor part must be either a
3217 -- function call (possibly qualified, or wrapped in an unchecked
3218 -- conversion) or aggregate (definitely qualified).
3220 -- The ancestor part can also be a function call (that may be
3221 -- transformed into an explicit dereference) or a qualification
3224 elsif Is_Limited_Type (Etype (Ancestor))
3225 and then Nkind_In (Unqualify (Ancestor), N_Aggregate,
3226 N_Extension_Aggregate)
3228 Ancestor_Is_Expression := True;
3230 -- Set up finalization data for enclosing record, because
3231 -- controlled subcomponents of the ancestor part will be
3234 Generate_Finalization_Actions;
3237 Build_Record_Aggr_Code
3238 (N => Unqualify (Ancestor),
3239 Typ => Etype (Unqualify (Ancestor)),
3242 -- If the ancestor part is an expression "E", we generate
3246 -- In Ada 2005, this includes the case of a (possibly qualified)
3247 -- limited function call. The assignment will turn into a
3248 -- build-in-place function call (for further details, see
3249 -- Make_Build_In_Place_Call_In_Assignment).
3252 Ancestor_Is_Expression := True;
3253 Init_Typ := Etype (Ancestor);
3255 -- If the ancestor part is an aggregate, force its full
3256 -- expansion, which was delayed.
3258 if Nkind_In (Unqualify (Ancestor), N_Aggregate,
3259 N_Extension_Aggregate)
3261 Set_Analyzed (Ancestor, False);
3262 Set_Analyzed (Expression (Ancestor), False);
3265 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
3266 Set_Assignment_OK (Ref);
3268 -- Make the assignment without usual controlled actions, since
3269 -- we only want to Adjust afterwards, but not to Finalize
3270 -- beforehand. Add manual Adjust when necessary.
3272 Assign := New_List (
3273 Make_OK_Assignment_Statement (Loc,
3275 Expression => Ancestor));
3276 Set_No_Ctrl_Actions (First (Assign));
3278 -- Assign the tag now to make sure that the dispatching call in
3279 -- the subsequent deep_adjust works properly (unless
3280 -- Tagged_Type_Expansion where tags are implicit).
3282 if Tagged_Type_Expansion then
3284 Make_OK_Assignment_Statement (Loc,
3286 Make_Selected_Component (Loc,
3287 Prefix => New_Copy_Tree (Target),
3290 (First_Tag_Component (Base_Type (Typ)), Loc)),
3293 Unchecked_Convert_To (RTE (RE_Tag),
3296 (Access_Disp_Table (Base_Type (Typ)))),
3299 Set_Assignment_OK (Name (Instr));
3300 Append_To (Assign, Instr);
3302 -- Ada 2005 (AI-251): If tagged type has progenitors we must
3303 -- also initialize tags of the secondary dispatch tables.
3305 if Has_Interfaces (Base_Type (Typ)) then
3307 (Typ => Base_Type (Typ),
3309 Stmts_List => Assign);
3313 -- Call Adjust manually
3315 if Needs_Finalization (Etype (Ancestor))
3316 and then not Is_Limited_Type (Etype (Ancestor))
3320 (Obj_Ref => New_Copy_Tree (Ref),
3321 Typ => Etype (Ancestor));
3323 -- Guard against a missing [Deep_]Adjust when the ancestor
3324 -- type was not properly frozen.
3326 if Present (Adj_Call) then
3327 Append_To (Assign, Adj_Call);
3332 Make_Unsuppress_Block (Loc, Name_Discriminant_Check, Assign));
3334 if Has_Discriminants (Init_Typ) then
3335 Check_Ancestor_Discriminants (Init_Typ);
3340 -- Generate assignments of hidden discriminants. If the base type is
3341 -- an unchecked union, the discriminants are unknown to the back-end
3342 -- and absent from a value of the type, so assignments for them are
3345 if Has_Discriminants (Typ)
3346 and then not Is_Unchecked_Union (Base_Type (Typ))
3348 Init_Hidden_Discriminants (Typ, L);
3351 -- Normal case (not an extension aggregate)
3354 -- Generate the discriminant expressions, component by component.
3355 -- If the base type is an unchecked union, the discriminants are
3356 -- unknown to the back-end and absent from a value of the type, so
3357 -- assignments for them are not emitted.
3359 if Has_Discriminants (Typ)
3360 and then not Is_Unchecked_Union (Base_Type (Typ))
3362 Init_Hidden_Discriminants (Typ, L);
3364 -- Generate discriminant init values for the visible discriminants
3366 Init_Visible_Discriminants;
3368 if Is_Derived_Type (N_Typ) then
3369 Init_Stored_Discriminants;
3374 -- For CPP types we generate an implicit call to the C++ default
3375 -- constructor to ensure the proper initialization of the _Tag
3378 if Is_CPP_Class (Root_Type (Typ)) and then CPP_Num_Prims (Typ) > 0 then
3379 Invoke_Constructor : declare
3380 CPP_Parent : constant Entity_Id := Enclosing_CPP_Parent (Typ);
3382 procedure Invoke_IC_Proc (T : Entity_Id);
3383 -- Recursive routine used to climb to parents. Required because
3384 -- parents must be initialized before descendants to ensure
3385 -- propagation of inherited C++ slots.
3387 --------------------
3388 -- Invoke_IC_Proc --
3389 --------------------
3391 procedure Invoke_IC_Proc (T : Entity_Id) is
3393 -- Avoid generating extra calls. Initialization required
3394 -- only for types defined from the level of derivation of
3395 -- type of the constructor and the type of the aggregate.
3397 if T = CPP_Parent then
3401 Invoke_IC_Proc (Etype (T));
3403 -- Generate call to the IC routine
3405 if Present (CPP_Init_Proc (T)) then
3407 Make_Procedure_Call_Statement (Loc,
3408 Name => New_Occurrence_Of (CPP_Init_Proc (T), Loc)));
3412 -- Start of processing for Invoke_Constructor
3415 -- Implicit invocation of the C++ constructor
3417 if Nkind (N) = N_Aggregate then
3419 Make_Procedure_Call_Statement (Loc,
3421 New_Occurrence_Of (Base_Init_Proc (CPP_Parent), Loc),
3422 Parameter_Associations => New_List (
3423 Unchecked_Convert_To (CPP_Parent,
3424 New_Copy_Tree (Lhs)))));
3427 Invoke_IC_Proc (Typ);
3428 end Invoke_Constructor;
3431 -- Generate the assignments, component by component
3433 -- tmp.comp1 := Expr1_From_Aggr;
3434 -- tmp.comp2 := Expr2_From_Aggr;
3437 Comp := First (Component_Associations (N));
3438 while Present (Comp) loop
3439 Selector := Entity (First (Choices (Comp)));
3443 if Is_CPP_Constructor_Call (Expression (Comp)) then
3445 Build_Initialization_Call (Loc,
3447 Make_Selected_Component (Loc,
3448 Prefix => New_Copy_Tree (Target),
3449 Selector_Name => New_Occurrence_Of (Selector, Loc)),
3450 Typ => Etype (Selector),
3452 With_Default_Init => True,
3453 Constructor_Ref => Expression (Comp)));
3455 -- Ada 2005 (AI-287): For each default-initialized component generate
3456 -- a call to the corresponding IP subprogram if available.
3458 elsif Box_Present (Comp)
3459 and then Has_Non_Null_Base_Init_Proc (Etype (Selector))
3461 if Ekind (Selector) /= E_Discriminant then
3462 Generate_Finalization_Actions;
3465 -- Ada 2005 (AI-287): If the component type has tasks then
3466 -- generate the activation chain and master entities (except
3467 -- in case of an allocator because in that case these entities
3468 -- are generated by Build_Task_Allocate_Block_With_Init_Stmts).
3471 Ctype : constant Entity_Id := Etype (Selector);
3472 Inside_Allocator : Boolean := False;
3473 P : Node_Id := Parent (N);
3476 if Is_Task_Type (Ctype) or else Has_Task (Ctype) then
3477 while Present (P) loop
3478 if Nkind (P) = N_Allocator then
3479 Inside_Allocator := True;
3486 if not Inside_Init_Proc and not Inside_Allocator then
3487 Build_Activation_Chain_Entity (N);
3493 Build_Initialization_Call (Loc,
3494 Id_Ref => Make_Selected_Component (Loc,
3495 Prefix => New_Copy_Tree (Target),
3497 New_Occurrence_Of (Selector, Loc)),
3498 Typ => Etype (Selector),
3500 With_Default_Init => True));
3502 -- Prepare for component assignment
3504 elsif Ekind (Selector) /= E_Discriminant
3505 or else Nkind (N) = N_Extension_Aggregate
3507 -- All the discriminants have now been assigned
3509 -- This is now a good moment to initialize and attach all the
3510 -- controllers. Their position may depend on the discriminants.
3512 if Ekind (Selector) /= E_Discriminant then
3513 Generate_Finalization_Actions;
3516 Comp_Type := Underlying_Type (Etype (Selector));
3518 Make_Selected_Component (Loc,
3519 Prefix => New_Copy_Tree (Target),
3520 Selector_Name => New_Occurrence_Of (Selector, Loc));
3522 if Nkind (Expression (Comp)) = N_Qualified_Expression then
3523 Expr_Q := Expression (Expression (Comp));
3525 Expr_Q := Expression (Comp);
3528 -- Now either create the assignment or generate the code for the
3529 -- inner aggregate top-down.
3531 if Is_Delayed_Aggregate (Expr_Q) then
3533 -- We have the following case of aggregate nesting inside
3534 -- an object declaration:
3536 -- type Arr_Typ is array (Integer range <>) of ...;
3538 -- type Rec_Typ (...) is record
3539 -- Obj_Arr_Typ : Arr_Typ (A .. B);
3542 -- Obj_Rec_Typ : Rec_Typ := (...,
3543 -- Obj_Arr_Typ => (X => (...), Y => (...)));
3545 -- The length of the ranges of the aggregate and Obj_Add_Typ
3546 -- are equal (B - A = Y - X), but they do not coincide (X /=
3547 -- A and B /= Y). This case requires array sliding which is
3548 -- performed in the following manner:
3550 -- subtype Arr_Sub is Arr_Typ (X .. Y);
3552 -- Temp (X) := (...);
3554 -- Temp (Y) := (...);
3555 -- Obj_Rec_Typ.Obj_Arr_Typ := Temp;
3557 if Ekind (Comp_Type) = E_Array_Subtype
3558 and then Is_Int_Range_Bounds (Aggregate_Bounds (Expr_Q))
3559 and then Is_Int_Range_Bounds (First_Index (Comp_Type))
3561 Compatible_Int_Bounds
3562 (Agg_Bounds => Aggregate_Bounds (Expr_Q),
3563 Typ_Bounds => First_Index (Comp_Type))
3565 -- Create the array subtype with bounds equal to those of
3566 -- the corresponding aggregate.
3569 SubE : constant Entity_Id := Make_Temporary (Loc, 'T');
3571 SubD : constant Node_Id :=
3572 Make_Subtype_Declaration (Loc,
3573 Defining_Identifier => SubE,
3574 Subtype_Indication =>
3575 Make_Subtype_Indication (Loc,
3577 New_Occurrence_Of (Etype (Comp_Type), Loc),
3579 Make_Index_Or_Discriminant_Constraint
3581 Constraints => New_List (
3583 (Aggregate_Bounds (Expr_Q))))));
3585 -- Create a temporary array of the above subtype which
3586 -- will be used to capture the aggregate assignments.
3588 TmpE : constant Entity_Id := Make_Temporary (Loc, 'A', N);
3590 TmpD : constant Node_Id :=
3591 Make_Object_Declaration (Loc,
3592 Defining_Identifier => TmpE,
3593 Object_Definition => New_Occurrence_Of (SubE, Loc));
3596 Set_No_Initialization (TmpD);
3597 Append_To (L, SubD);
3598 Append_To (L, TmpD);
3600 -- Expand aggregate into assignments to the temp array
3603 Late_Expansion (Expr_Q, Comp_Type,
3604 New_Occurrence_Of (TmpE, Loc)));
3609 Make_Assignment_Statement (Loc,
3610 Name => New_Copy_Tree (Comp_Expr),
3611 Expression => New_Occurrence_Of (TmpE, Loc)));
3614 -- Normal case (sliding not required)
3618 Late_Expansion (Expr_Q, Comp_Type, Comp_Expr));
3621 -- Expr_Q is not delayed aggregate
3624 if Has_Discriminants (Typ) then
3625 Replace_Discriminants (Expr_Q);
3627 -- If the component is an array type that depends on
3628 -- discriminants, and the expression is a single Others
3629 -- clause, create an explicit subtype for it because the
3630 -- backend has troubles recovering the actual bounds.
3632 if Nkind (Expr_Q) = N_Aggregate
3633 and then Is_Array_Type (Comp_Type)
3634 and then Present (Component_Associations (Expr_Q))
3637 Assoc : constant Node_Id :=
3638 First (Component_Associations (Expr_Q));
3642 if Nkind (First (Choices (Assoc))) = N_Others_Choice
3645 Build_Actual_Subtype_Of_Component
3646 (Comp_Type, Comp_Expr);
3648 -- If the component type does not in fact depend on
3649 -- discriminants, the subtype declaration is empty.
3651 if Present (Decl) then
3652 Append_To (L, Decl);
3653 Set_Etype (Comp_Expr, Defining_Entity (Decl));
3661 and then Nkind (Expr_Q) = N_Aggregate
3662 and then Is_Array_Type (Etype (Expr_Q))
3663 and then Present (First_Index (Etype (Expr_Q)))
3666 Expr_Q_Type : constant Node_Id := Etype (Expr_Q);
3669 Build_Array_Aggr_Code
3671 Ctype => Component_Type (Expr_Q_Type),
3672 Index => First_Index (Expr_Q_Type),
3675 Is_Scalar_Type (Component_Type (Expr_Q_Type))));
3679 -- Handle an initialization expression of a controlled type
3680 -- in case it denotes a function call. In general such a
3681 -- scenario will produce a transient scope, but this will
3682 -- lead to wrong order of initialization, adjustment, and
3683 -- finalization in the context of aggregates.
3685 -- Target.Comp := Ctrl_Func_Call;
3688 -- Trans_Obj : ... := Ctrl_Func_Call; -- object
3689 -- Target.Comp := Trans_Obj;
3690 -- Finalize (Trans_Obj);
3692 -- Target.Comp._tag := ...;
3693 -- Adjust (Target.Comp);
3695 -- In the example above, the call to Finalize occurs too
3696 -- early and as a result it may leave the record component
3697 -- in a bad state. Finalization of the transient object
3698 -- should really happen after adjustment.
3700 -- To avoid this scenario, perform in-place side-effect
3701 -- removal of the function call. This eliminates the
3702 -- transient property of the function result and ensures
3703 -- correct order of actions.
3705 -- Res : ... := Ctrl_Func_Call;
3706 -- Target.Comp := Res;
3707 -- Target.Comp._tag := ...;
3708 -- Adjust (Target.Comp);
3711 if Needs_Finalization (Comp_Type)
3712 and then Nkind (Expr_Q) /= N_Aggregate
3714 Initialize_Ctrl_Record_Component
3715 (Rec_Comp => Comp_Expr,
3716 Comp_Typ => Etype (Selector),
3717 Init_Expr => Expr_Q,
3720 -- Otherwise perform single component initialization
3723 Initialize_Record_Component
3724 (Rec_Comp => Comp_Expr,
3725 Comp_Typ => Etype (Selector),
3726 Init_Expr => Expr_Q,
3732 -- comment would be good here ???
3734 elsif Ekind (Selector) = E_Discriminant
3735 and then Nkind (N) /= N_Extension_Aggregate
3736 and then Nkind (Parent (N)) = N_Component_Association
3737 and then Is_Constrained (Typ)
3739 -- We must check that the discriminant value imposed by the
3740 -- context is the same as the value given in the subaggregate,
3741 -- because after the expansion into assignments there is no
3742 -- record on which to perform a regular discriminant check.
3749 D_Val := First_Elmt (Discriminant_Constraint (Typ));
3750 Disc := First_Discriminant (Typ);
3751 while Chars (Disc) /= Chars (Selector) loop
3752 Next_Discriminant (Disc);
3756 pragma Assert (Present (D_Val));
3758 -- This check cannot performed for components that are
3759 -- constrained by a current instance, because this is not a
3760 -- value that can be compared with the actual constraint.
3762 if Nkind (Node (D_Val)) /= N_Attribute_Reference
3763 or else not Is_Entity_Name (Prefix (Node (D_Val)))
3764 or else not Is_Type (Entity (Prefix (Node (D_Val))))
3767 Make_Raise_Constraint_Error (Loc,
3770 Left_Opnd => New_Copy_Tree (Node (D_Val)),
3771 Right_Opnd => Expression (Comp)),
3772 Reason => CE_Discriminant_Check_Failed));
3775 -- Find self-reference in previous discriminant assignment,
3776 -- and replace with proper expression.
3783 while Present (Ass) loop
3784 if Nkind (Ass) = N_Assignment_Statement
3785 and then Nkind (Name (Ass)) = N_Selected_Component
3786 and then Chars (Selector_Name (Name (Ass))) =
3790 (Ass, New_Copy_Tree (Expression (Comp)));
3803 -- If the type is tagged, the tag needs to be initialized (unless we
3804 -- are in VM-mode where tags are implicit). It is done late in the
3805 -- initialization process because in some cases, we call the init
3806 -- proc of an ancestor which will not leave out the right tag.
3808 if Ancestor_Is_Expression then
3811 -- For CPP types we generated a call to the C++ default constructor
3812 -- before the components have been initialized to ensure the proper
3813 -- initialization of the _Tag component (see above).
3815 elsif Is_CPP_Class (Typ) then
3818 elsif Is_Tagged_Type (Typ) and then Tagged_Type_Expansion then
3820 Make_OK_Assignment_Statement (Loc,
3822 Make_Selected_Component (Loc,
3823 Prefix => New_Copy_Tree (Target),
3826 (First_Tag_Component (Base_Type (Typ)), Loc)),
3829 Unchecked_Convert_To (RTE (RE_Tag),
3831 (Node (First_Elmt (Access_Disp_Table (Base_Type (Typ)))),
3834 Append_To (L, Instr);
3836 -- Ada 2005 (AI-251): If the tagged type has been derived from an
3837 -- abstract interfaces we must also initialize the tags of the
3838 -- secondary dispatch tables.
3840 if Has_Interfaces (Base_Type (Typ)) then
3842 (Typ => Base_Type (Typ),
3848 -- If the controllers have not been initialized yet (by lack of non-
3849 -- discriminant components), let's do it now.
3851 Generate_Finalization_Actions;
3854 end Build_Record_Aggr_Code;
3856 ---------------------------------------
3857 -- Collect_Initialization_Statements --
3858 ---------------------------------------
3860 procedure Collect_Initialization_Statements
3863 Node_After : Node_Id)
3865 Loc : constant Source_Ptr := Sloc (N);
3866 Init_Actions : constant List_Id := New_List;
3867 Init_Node : Node_Id;
3868 Comp_Stmt : Node_Id;
3871 -- Nothing to do if Obj is already frozen, as in this case we known we
3872 -- won't need to move the initialization statements about later on.
3874 if Is_Frozen (Obj) then
3879 while Next (Init_Node) /= Node_After loop
3880 Append_To (Init_Actions, Remove_Next (Init_Node));
3883 if not Is_Empty_List (Init_Actions) then
3884 Comp_Stmt := Make_Compound_Statement (Loc, Actions => Init_Actions);
3885 Insert_Action_After (Init_Node, Comp_Stmt);
3886 Set_Initialization_Statements (Obj, Comp_Stmt);
3888 end Collect_Initialization_Statements;
3890 -------------------------------
3891 -- Convert_Aggr_In_Allocator --
3892 -------------------------------
3894 procedure Convert_Aggr_In_Allocator
3899 Loc : constant Source_Ptr := Sloc (Aggr);
3900 Typ : constant Entity_Id := Etype (Aggr);
3901 Temp : constant Entity_Id := Defining_Identifier (Decl);
3903 Occ : constant Node_Id :=
3904 Unchecked_Convert_To (Typ,
3905 Make_Explicit_Dereference (Loc, New_Occurrence_Of (Temp, Loc)));
3908 if Is_Array_Type (Typ) then
3909 Convert_Array_Aggr_In_Allocator (Decl, Aggr, Occ);
3911 elsif Has_Default_Init_Comps (Aggr) then
3913 L : constant List_Id := New_List;
3914 Init_Stmts : List_Id;
3917 Init_Stmts := Late_Expansion (Aggr, Typ, Occ);
3919 if Has_Task (Typ) then
3920 Build_Task_Allocate_Block_With_Init_Stmts (L, Aggr, Init_Stmts);
3921 Insert_Actions (Alloc, L);
3923 Insert_Actions (Alloc, Init_Stmts);
3928 Insert_Actions (Alloc, Late_Expansion (Aggr, Typ, Occ));
3930 end Convert_Aggr_In_Allocator;
3932 --------------------------------
3933 -- Convert_Aggr_In_Assignment --
3934 --------------------------------
3936 procedure Convert_Aggr_In_Assignment (N : Node_Id) is
3937 Aggr : Node_Id := Expression (N);
3938 Typ : constant Entity_Id := Etype (Aggr);
3939 Occ : constant Node_Id := New_Copy_Tree (Name (N));
3942 if Nkind (Aggr) = N_Qualified_Expression then
3943 Aggr := Expression (Aggr);
3946 Insert_Actions_After (N, Late_Expansion (Aggr, Typ, Occ));
3947 end Convert_Aggr_In_Assignment;
3949 ---------------------------------
3950 -- Convert_Aggr_In_Object_Decl --
3951 ---------------------------------
3953 procedure Convert_Aggr_In_Object_Decl (N : Node_Id) is
3954 Obj : constant Entity_Id := Defining_Identifier (N);
3955 Aggr : Node_Id := Expression (N);
3956 Loc : constant Source_Ptr := Sloc (Aggr);
3957 Typ : constant Entity_Id := Etype (Aggr);
3958 Occ : constant Node_Id := New_Occurrence_Of (Obj, Loc);
3960 function Discriminants_Ok return Boolean;
3961 -- If the object type is constrained, the discriminants in the
3962 -- aggregate must be checked against the discriminants of the subtype.
3963 -- This cannot be done using Apply_Discriminant_Checks because after
3964 -- expansion there is no aggregate left to check.
3966 ----------------------
3967 -- Discriminants_Ok --
3968 ----------------------
3970 function Discriminants_Ok return Boolean is
3971 Cond : Node_Id := Empty;
3980 D := First_Discriminant (Typ);
3981 Disc1 := First_Elmt (Discriminant_Constraint (Typ));
3982 Disc2 := First_Elmt (Discriminant_Constraint (Etype (Obj)));
3983 while Present (Disc1) and then Present (Disc2) loop
3984 Val1 := Node (Disc1);
3985 Val2 := Node (Disc2);
3987 if not Is_OK_Static_Expression (Val1)
3988 or else not Is_OK_Static_Expression (Val2)
3990 Check := Make_Op_Ne (Loc,
3991 Left_Opnd => Duplicate_Subexpr (Val1),
3992 Right_Opnd => Duplicate_Subexpr (Val2));
3998 Cond := Make_Or_Else (Loc,
4000 Right_Opnd => Check);
4003 elsif Expr_Value (Val1) /= Expr_Value (Val2) then
4004 Apply_Compile_Time_Constraint_Error (Aggr,
4005 Msg => "incorrect value for discriminant&??",
4006 Reason => CE_Discriminant_Check_Failed,
4011 Next_Discriminant (D);
4016 -- If any discriminant constraint is non-static, emit a check
4018 if Present (Cond) then
4020 Make_Raise_Constraint_Error (Loc,
4022 Reason => CE_Discriminant_Check_Failed));
4026 end Discriminants_Ok;
4028 -- Start of processing for Convert_Aggr_In_Object_Decl
4031 Set_Assignment_OK (Occ);
4033 if Nkind (Aggr) = N_Qualified_Expression then
4034 Aggr := Expression (Aggr);
4037 if Has_Discriminants (Typ)
4038 and then Typ /= Etype (Obj)
4039 and then Is_Constrained (Etype (Obj))
4040 and then not Discriminants_Ok
4045 -- If the context is an extended return statement, it has its own
4046 -- finalization machinery (i.e. works like a transient scope) and
4047 -- we do not want to create an additional one, because objects on
4048 -- the finalization list of the return must be moved to the caller's
4049 -- finalization list to complete the return.
4051 -- However, if the aggregate is limited, it is built in place, and the
4052 -- controlled components are not assigned to intermediate temporaries
4053 -- so there is no need for a transient scope in this case either.
4055 if Requires_Transient_Scope (Typ)
4056 and then Ekind (Current_Scope) /= E_Return_Statement
4057 and then not Is_Limited_Type (Typ)
4059 Establish_Transient_Scope
4062 Is_Controlled (Typ) or else Has_Controlled_Component (Typ));
4066 Node_After : constant Node_Id := Next (N);
4068 Insert_Actions_After (N, Late_Expansion (Aggr, Typ, Occ));
4069 Collect_Initialization_Statements (Obj, N, Node_After);
4071 Set_No_Initialization (N);
4072 Initialize_Discriminants (N, Typ);
4073 end Convert_Aggr_In_Object_Decl;
4075 -------------------------------------
4076 -- Convert_Array_Aggr_In_Allocator --
4077 -------------------------------------
4079 procedure Convert_Array_Aggr_In_Allocator
4084 Aggr_Code : List_Id;
4085 Typ : constant Entity_Id := Etype (Aggr);
4086 Ctyp : constant Entity_Id := Component_Type (Typ);
4089 -- The target is an explicit dereference of the allocated object.
4090 -- Generate component assignments to it, as for an aggregate that
4091 -- appears on the right-hand side of an assignment statement.
4094 Build_Array_Aggr_Code (Aggr,
4096 Index => First_Index (Typ),
4098 Scalar_Comp => Is_Scalar_Type (Ctyp));
4100 Insert_Actions_After (Decl, Aggr_Code);
4101 end Convert_Array_Aggr_In_Allocator;
4103 ----------------------------
4104 -- Convert_To_Assignments --
4105 ----------------------------
4107 procedure Convert_To_Assignments (N : Node_Id; Typ : Entity_Id) is
4108 Loc : constant Source_Ptr := Sloc (N);
4112 Aggr_Code : List_Id;
4114 Target_Expr : Node_Id;
4115 Parent_Kind : Node_Kind;
4116 Unc_Decl : Boolean := False;
4117 Parent_Node : Node_Id;
4120 pragma Assert (not Is_Static_Dispatch_Table_Aggregate (N));
4121 pragma Assert (Is_Record_Type (Typ));
4123 Parent_Node := Parent (N);
4124 Parent_Kind := Nkind (Parent_Node);
4126 if Parent_Kind = N_Qualified_Expression then
4128 -- Check if we are in a unconstrained declaration because in this
4129 -- case the current delayed expansion mechanism doesn't work when
4130 -- the declared object size depend on the initializing expr.
4132 Parent_Node := Parent (Parent_Node);
4133 Parent_Kind := Nkind (Parent_Node);
4135 if Parent_Kind = N_Object_Declaration then
4137 not Is_Entity_Name (Object_Definition (Parent_Node))
4138 or else Has_Discriminants
4139 (Entity (Object_Definition (Parent_Node)))
4140 or else Is_Class_Wide_Type
4141 (Entity (Object_Definition (Parent_Node)));
4145 -- Just set the Delay flag in the cases where the transformation will be
4146 -- done top down from above.
4150 -- Internal aggregate (transformed when expanding the parent)
4152 or else Parent_Kind = N_Aggregate
4153 or else Parent_Kind = N_Extension_Aggregate
4154 or else Parent_Kind = N_Component_Association
4156 -- Allocator (see Convert_Aggr_In_Allocator)
4158 or else Parent_Kind = N_Allocator
4160 -- Object declaration (see Convert_Aggr_In_Object_Decl)
4162 or else (Parent_Kind = N_Object_Declaration and then not Unc_Decl)
4164 -- Safe assignment (see Convert_Aggr_Assignments). So far only the
4165 -- assignments in init procs are taken into account.
4167 or else (Parent_Kind = N_Assignment_Statement
4168 and then Inside_Init_Proc)
4170 -- (Ada 2005) An inherently limited type in a return statement, which
4171 -- will be handled in a build-in-place fashion, and may be rewritten
4172 -- as an extended return and have its own finalization machinery.
4173 -- In the case of a simple return, the aggregate needs to be delayed
4174 -- until the scope for the return statement has been created, so
4175 -- that any finalization chain will be associated with that scope.
4176 -- For extended returns, we delay expansion to avoid the creation
4177 -- of an unwanted transient scope that could result in premature
4178 -- finalization of the return object (which is built in place
4179 -- within the caller's scope).
4182 (Is_Limited_View (Typ)
4184 (Nkind (Parent (Parent_Node)) = N_Extended_Return_Statement
4185 or else Nkind (Parent_Node) = N_Simple_Return_Statement))
4187 Set_Expansion_Delayed (N);
4191 -- Otherwise, if a transient scope is required, create it now. If we
4192 -- are within an initialization procedure do not create such, because
4193 -- the target of the assignment must not be declared within a local
4194 -- block, and because cleanup will take place on return from the
4195 -- initialization procedure.
4197 -- Should the condition be more restrictive ???
4199 if Requires_Transient_Scope (Typ) and then not Inside_Init_Proc then
4200 Establish_Transient_Scope (N, Sec_Stack => Needs_Finalization (Typ));
4203 -- If the aggregate is nonlimited, create a temporary. If it is limited
4204 -- and context is an assignment, this is a subaggregate for an enclosing
4205 -- aggregate being expanded. It must be built in place, so use target of
4206 -- the current assignment.
4208 if Is_Limited_Type (Typ)
4209 and then Nkind (Parent (N)) = N_Assignment_Statement
4211 Target_Expr := New_Copy_Tree (Name (Parent (N)));
4212 Insert_Actions (Parent (N),
4213 Build_Record_Aggr_Code (N, Typ, Target_Expr));
4214 Rewrite (Parent (N), Make_Null_Statement (Loc));
4217 Temp := Make_Temporary (Loc, 'A', N);
4219 -- If the type inherits unknown discriminants, use the view with
4220 -- known discriminants if available.
4222 if Has_Unknown_Discriminants (Typ)
4223 and then Present (Underlying_Record_View (Typ))
4225 T := Underlying_Record_View (Typ);
4231 Make_Object_Declaration (Loc,
4232 Defining_Identifier => Temp,
4233 Object_Definition => New_Occurrence_Of (T, Loc));
4235 Set_No_Initialization (Instr);
4236 Insert_Action (N, Instr);
4237 Initialize_Discriminants (Instr, T);
4239 Target_Expr := New_Occurrence_Of (Temp, Loc);
4240 Aggr_Code := Build_Record_Aggr_Code (N, T, Target_Expr);
4242 -- Save the last assignment statement associated with the aggregate
4243 -- when building a controlled object. This reference is utilized by
4244 -- the finalization machinery when marking an object as successfully
4247 if Needs_Finalization (T) then
4248 Set_Last_Aggregate_Assignment (Temp, Last (Aggr_Code));
4251 Insert_Actions (N, Aggr_Code);
4252 Rewrite (N, New_Occurrence_Of (Temp, Loc));
4253 Analyze_And_Resolve (N, T);
4255 end Convert_To_Assignments;
4257 ---------------------------
4258 -- Convert_To_Positional --
4259 ---------------------------
4261 procedure Convert_To_Positional
4263 Max_Others_Replicate : Nat := 5;
4264 Handle_Bit_Packed : Boolean := False)
4266 Typ : constant Entity_Id := Etype (N);
4268 Static_Components : Boolean := True;
4270 procedure Check_Static_Components;
4271 -- Check whether all components of the aggregate are compile-time known
4272 -- values, and can be passed as is to the back-end without further
4274 -- An Iterated_component_Association is treated as non-static, but there
4275 -- are possibilities for optimization here.
4280 Ixb : Node_Id) return Boolean;
4281 -- Convert the aggregate into a purely positional form if possible. On
4282 -- entry the bounds of all dimensions are known to be static, and the
4283 -- total number of components is safe enough to expand.
4285 function Is_Flat (N : Node_Id; Dims : Int) return Boolean;
4286 -- Return True iff the array N is flat (which is not trivial in the case
4287 -- of multidimensional aggregates).
4289 -----------------------------
4290 -- Check_Static_Components --
4291 -----------------------------
4293 -- Could use some comments in this body ???
4295 procedure Check_Static_Components is
4299 Static_Components := True;
4301 if Nkind (N) = N_String_Literal then
4304 elsif Present (Expressions (N)) then
4305 Expr := First (Expressions (N));
4306 while Present (Expr) loop
4307 if Nkind (Expr) /= N_Aggregate
4308 or else not Compile_Time_Known_Aggregate (Expr)
4309 or else Expansion_Delayed (Expr)
4311 Static_Components := False;
4319 if Nkind (N) = N_Aggregate
4320 and then Present (Component_Associations (N))
4322 Expr := First (Component_Associations (N));
4323 while Present (Expr) loop
4324 if Nkind_In (Expression (Expr), N_Integer_Literal,
4329 elsif Is_Entity_Name (Expression (Expr))
4330 and then Present (Entity (Expression (Expr)))
4331 and then Ekind (Entity (Expression (Expr))) =
4332 E_Enumeration_Literal
4336 elsif Nkind (Expression (Expr)) /= N_Aggregate
4337 or else not Compile_Time_Known_Aggregate (Expression (Expr))
4338 or else Expansion_Delayed (Expression (Expr))
4339 or else Nkind (Expr) = N_Iterated_Component_Association
4341 Static_Components := False;
4348 end Check_Static_Components;
4357 Ixb : Node_Id) return Boolean
4359 Loc : constant Source_Ptr := Sloc (N);
4360 Blo : constant Node_Id := Type_Low_Bound (Etype (Ixb));
4361 Lo : constant Node_Id := Type_Low_Bound (Etype (Ix));
4362 Hi : constant Node_Id := Type_High_Bound (Etype (Ix));
4366 Others_Present : Boolean := False;
4369 if Nkind (Original_Node (N)) = N_String_Literal then
4373 if not Compile_Time_Known_Value (Lo)
4374 or else not Compile_Time_Known_Value (Hi)
4379 Lov := Expr_Value (Lo);
4380 Hiv := Expr_Value (Hi);
4382 -- Check if there is an others choice
4384 if Present (Component_Associations (N)) then
4390 Assoc := First (Component_Associations (N));
4391 while Present (Assoc) loop
4393 -- If this is a box association, flattening is in general
4394 -- not possible because at this point we cannot tell if the
4395 -- default is static or even exists.
4397 if Box_Present (Assoc) then
4400 elsif Nkind (Assoc) = N_Iterated_Component_Association then
4404 Choice := First (Choice_List (Assoc));
4406 while Present (Choice) loop
4407 if Nkind (Choice) = N_Others_Choice then
4408 Others_Present := True;
4419 -- If the low bound is not known at compile time and others is not
4420 -- present we can proceed since the bounds can be obtained from the
4424 or else (not Compile_Time_Known_Value (Blo) and then Others_Present)
4429 -- Determine if set of alternatives is suitable for conversion and
4430 -- build an array containing the values in sequence.
4433 Vals : array (UI_To_Int (Lov) .. UI_To_Int (Hiv))
4434 of Node_Id := (others => Empty);
4435 -- The values in the aggregate sorted appropriately
4438 -- Same data as Vals in list form
4441 -- Used to validate Max_Others_Replicate limit
4444 Num : Int := UI_To_Int (Lov);
4450 if Present (Expressions (N)) then
4451 Elmt := First (Expressions (N));
4452 while Present (Elmt) loop
4453 if Nkind (Elmt) = N_Aggregate
4454 and then Present (Next_Index (Ix))
4456 not Flatten (Elmt, Next_Index (Ix), Next_Index (Ixb))
4461 Vals (Num) := Relocate_Node (Elmt);
4468 if No (Component_Associations (N)) then
4472 Elmt := First (Component_Associations (N));
4474 if Nkind (Expression (Elmt)) = N_Aggregate then
4475 if Present (Next_Index (Ix))
4478 (Expression (Elmt), Next_Index (Ix), Next_Index (Ixb))
4484 Component_Loop : while Present (Elmt) loop
4485 Choice := First (Choice_List (Elmt));
4486 Choice_Loop : while Present (Choice) loop
4488 -- If we have an others choice, fill in the missing elements
4489 -- subject to the limit established by Max_Others_Replicate.
4491 if Nkind (Choice) = N_Others_Choice then
4494 for J in Vals'Range loop
4495 if No (Vals (J)) then
4496 Vals (J) := New_Copy_Tree (Expression (Elmt));
4497 Rep_Count := Rep_Count + 1;
4499 -- Check for maximum others replication. Note that
4500 -- we skip this test if either of the restrictions
4501 -- No_Elaboration_Code or No_Implicit_Loops is
4502 -- active, if this is a preelaborable unit or
4503 -- a predefined unit, or if the unit must be
4504 -- placed in data memory. This also ensures that
4505 -- predefined units get the same level of constant
4506 -- folding in Ada 95 and Ada 2005, where their
4507 -- categorization has changed.
4510 P : constant Entity_Id :=
4511 Cunit_Entity (Current_Sem_Unit);
4514 -- Check if duplication OK and if so continue
4517 if Restriction_Active (No_Elaboration_Code)
4518 or else Restriction_Active (No_Implicit_Loops)
4520 (Ekind (Current_Scope) = E_Package
4521 and then Static_Elaboration_Desired
4523 or else Is_Preelaborated (P)
4524 or else (Ekind (P) = E_Package_Body
4526 Is_Preelaborated (Spec_Entity (P)))
4528 Is_Predefined_File_Name
4529 (Unit_File_Name (Get_Source_Unit (P)))
4533 -- If duplication not OK, then we return False
4534 -- if the replication count is too high
4536 elsif Rep_Count > Max_Others_Replicate then
4539 -- Continue on if duplication not OK, but the
4540 -- replication count is not excessive.
4549 exit Component_Loop;
4551 -- Case of a subtype mark, identifier or expanded name
4553 elsif Is_Entity_Name (Choice)
4554 and then Is_Type (Entity (Choice))
4556 Lo := Type_Low_Bound (Etype (Choice));
4557 Hi := Type_High_Bound (Etype (Choice));
4559 -- Case of subtype indication
4561 elsif Nkind (Choice) = N_Subtype_Indication then
4562 Lo := Low_Bound (Range_Expression (Constraint (Choice)));
4563 Hi := High_Bound (Range_Expression (Constraint (Choice)));
4567 elsif Nkind (Choice) = N_Range then
4568 Lo := Low_Bound (Choice);
4569 Hi := High_Bound (Choice);
4571 -- Normal subexpression case
4573 else pragma Assert (Nkind (Choice) in N_Subexpr);
4574 if not Compile_Time_Known_Value (Choice) then
4578 Choice_Index := UI_To_Int (Expr_Value (Choice));
4580 if Choice_Index in Vals'Range then
4581 Vals (Choice_Index) :=
4582 New_Copy_Tree (Expression (Elmt));
4585 -- Choice is statically out-of-range, will be
4586 -- rewritten to raise Constraint_Error.
4594 -- Range cases merge with Lo,Hi set
4596 if not Compile_Time_Known_Value (Lo)
4598 not Compile_Time_Known_Value (Hi)
4603 for J in UI_To_Int (Expr_Value (Lo)) ..
4604 UI_To_Int (Expr_Value (Hi))
4606 Vals (J) := New_Copy_Tree (Expression (Elmt));
4612 end loop Choice_Loop;
4615 end loop Component_Loop;
4617 -- If we get here the conversion is possible
4620 for J in Vals'Range loop
4621 Append (Vals (J), Vlist);
4624 Rewrite (N, Make_Aggregate (Loc, Expressions => Vlist));
4625 Set_Aggregate_Bounds (N, Aggregate_Bounds (Original_Node (N)));
4634 function Is_Flat (N : Node_Id; Dims : Int) return Boolean is
4641 elsif Nkind (N) = N_Aggregate then
4642 if Present (Component_Associations (N)) then
4646 Elmt := First (Expressions (N));
4647 while Present (Elmt) loop
4648 if not Is_Flat (Elmt, Dims - 1) then
4662 -- Start of processing for Convert_To_Positional
4665 -- Only convert to positional when generating C in case of an
4666 -- object declaration, this is the only case where aggregates are
4669 if Modify_Tree_For_C and then not In_Object_Declaration (N) then
4673 -- Ada 2005 (AI-287): Do not convert in case of default initialized
4674 -- components because in this case will need to call the corresponding
4677 if Has_Default_Init_Comps (N) then
4681 if Is_Flat (N, Number_Dimensions (Typ)) then
4685 if Is_Bit_Packed_Array (Typ) and then not Handle_Bit_Packed then
4689 -- Do not convert to positional if controlled components are involved
4690 -- since these require special processing
4692 if Has_Controlled_Component (Typ) then
4696 Check_Static_Components;
4698 -- If the size is known, or all the components are static, try to
4699 -- build a fully positional aggregate.
4701 -- The size of the type may not be known for an aggregate with
4702 -- discriminated array components, but if the components are static
4703 -- it is still possible to verify statically that the length is
4704 -- compatible with the upper bound of the type, and therefore it is
4705 -- worth flattening such aggregates as well.
4707 -- For now the back-end expands these aggregates into individual
4708 -- assignments to the target anyway, but it is conceivable that
4709 -- it will eventually be able to treat such aggregates statically???
4711 if Aggr_Size_OK (N, Typ)
4712 and then Flatten (N, First_Index (Typ), First_Index (Base_Type (Typ)))
4714 if Static_Components then
4715 Set_Compile_Time_Known_Aggregate (N);
4716 Set_Expansion_Delayed (N, False);
4719 Analyze_And_Resolve (N, Typ);
4722 -- If Static_Elaboration_Desired has been specified, diagnose aggregates
4723 -- that will still require initialization code.
4725 if (Ekind (Current_Scope) = E_Package
4726 and then Static_Elaboration_Desired (Current_Scope))
4727 and then Nkind (Parent (N)) = N_Object_Declaration
4733 if Nkind (N) = N_Aggregate and then Present (Expressions (N)) then
4734 Expr := First (Expressions (N));
4735 while Present (Expr) loop
4736 if Nkind_In (Expr, N_Integer_Literal, N_Real_Literal)
4738 (Is_Entity_Name (Expr)
4739 and then Ekind (Entity (Expr)) = E_Enumeration_Literal)
4745 ("non-static object requires elaboration code??", N);
4752 if Present (Component_Associations (N)) then
4753 Error_Msg_N ("object requires elaboration code??", N);
4758 end Convert_To_Positional;
4760 ----------------------------
4761 -- Expand_Array_Aggregate --
4762 ----------------------------
4764 -- Array aggregate expansion proceeds as follows:
4766 -- 1. If requested we generate code to perform all the array aggregate
4767 -- bound checks, specifically
4769 -- (a) Check that the index range defined by aggregate bounds is
4770 -- compatible with corresponding index subtype.
4772 -- (b) If an others choice is present check that no aggregate
4773 -- index is outside the bounds of the index constraint.
4775 -- (c) For multidimensional arrays make sure that all subaggregates
4776 -- corresponding to the same dimension have the same bounds.
4778 -- 2. Check for packed array aggregate which can be converted to a
4779 -- constant so that the aggregate disappears completely.
4781 -- 3. Check case of nested aggregate. Generally nested aggregates are
4782 -- handled during the processing of the parent aggregate.
4784 -- 4. Check if the aggregate can be statically processed. If this is the
4785 -- case pass it as is to Gigi. Note that a necessary condition for
4786 -- static processing is that the aggregate be fully positional.
4788 -- 5. If in place aggregate expansion is possible (i.e. no need to create
4789 -- a temporary) then mark the aggregate as such and return. Otherwise
4790 -- create a new temporary and generate the appropriate initialization
4793 procedure Expand_Array_Aggregate (N : Node_Id) is
4794 Loc : constant Source_Ptr := Sloc (N);
4796 Typ : constant Entity_Id := Etype (N);
4797 Ctyp : constant Entity_Id := Component_Type (Typ);
4798 -- Typ is the correct constrained array subtype of the aggregate
4799 -- Ctyp is the corresponding component type.
4801 Aggr_Dimension : constant Pos := Number_Dimensions (Typ);
4802 -- Number of aggregate index dimensions
4804 Aggr_Low : array (1 .. Aggr_Dimension) of Node_Id;
4805 Aggr_High : array (1 .. Aggr_Dimension) of Node_Id;
4806 -- Low and High bounds of the constraint for each aggregate index
4808 Aggr_Index_Typ : array (1 .. Aggr_Dimension) of Entity_Id;
4809 -- The type of each index
4811 In_Place_Assign_OK_For_Declaration : Boolean := False;
4812 -- True if we are to generate an in place assignment for a declaration
4814 Maybe_In_Place_OK : Boolean;
4815 -- If the type is neither controlled nor packed and the aggregate
4816 -- is the expression in an assignment, assignment in place may be
4817 -- possible, provided other conditions are met on the LHS.
4819 Others_Present : array (1 .. Aggr_Dimension) of Boolean :=
4821 -- If Others_Present (J) is True, then there is an others choice in one
4822 -- of the subaggregates of N at dimension J.
4824 function Aggr_Assignment_OK_For_Backend (N : Node_Id) return Boolean;
4825 -- Returns true if an aggregate assignment can be done by the back end
4827 procedure Build_Constrained_Type (Positional : Boolean);
4828 -- If the subtype is not static or unconstrained, build a constrained
4829 -- type using the computable sizes of the aggregate and its sub-
4832 procedure Check_Bounds (Aggr_Bounds : Node_Id; Index_Bounds : Node_Id);
4833 -- Checks that the bounds of Aggr_Bounds are within the bounds defined
4836 procedure Check_Same_Aggr_Bounds (Sub_Aggr : Node_Id; Dim : Pos);
4837 -- Checks that in a multidimensional array aggregate all subaggregates
4838 -- corresponding to the same dimension have the same bounds. Sub_Aggr is
4839 -- an array subaggregate. Dim is the dimension corresponding to the
4842 procedure Compute_Others_Present (Sub_Aggr : Node_Id; Dim : Pos);
4843 -- Computes the values of array Others_Present. Sub_Aggr is the array
4844 -- subaggregate we start the computation from. Dim is the dimension
4845 -- corresponding to the subaggregate.
4847 function In_Place_Assign_OK return Boolean;
4848 -- Simple predicate to determine whether an aggregate assignment can
4849 -- be done in place, because none of the new values can depend on the
4850 -- components of the target of the assignment.
4852 procedure Others_Check (Sub_Aggr : Node_Id; Dim : Pos);
4853 -- Checks that if an others choice is present in any subaggregate, no
4854 -- aggregate index is outside the bounds of the index constraint.
4855 -- Sub_Aggr is an array subaggregate. Dim is the dimension corresponding
4856 -- to the subaggregate.
4858 function Safe_Left_Hand_Side (N : Node_Id) return Boolean;
4859 -- In addition to Maybe_In_Place_OK, in order for an aggregate to be
4860 -- built directly into the target of the assignment it must be free
4863 ------------------------------------
4864 -- Aggr_Assignment_OK_For_Backend --
4865 ------------------------------------
4867 -- Backend processing by Gigi/gcc is possible only if all the following
4868 -- conditions are met:
4870 -- 1. N consists of a single OTHERS choice, possibly recursively
4872 -- 2. The array type is not packed
4874 -- 3. The array type has no atomic components
4876 -- 4. The array type has no null ranges (the purpose of this is to
4877 -- avoid a bogus warning for an out-of-range value).
4879 -- 5. The component type is discrete
4881 -- 6. The component size is Storage_Unit or the value is of the form
4882 -- M * (1 + A**1 + A**2 + .. A**(K-1)) where A = 2**(Storage_Unit)
4883 -- and M in 1 .. A-1. This can also be viewed as K occurrences of
4884 -- the 8-bit value M, concatenated together.
4886 -- The ultimate goal is to generate a call to a fast memset routine
4887 -- specifically optimized for the target.
4889 function Aggr_Assignment_OK_For_Backend (N : Node_Id) return Boolean is
4892 Expr : Node_Id := N;
4900 -- Recurse as far as possible to find the innermost component type
4903 while Is_Array_Type (Ctyp) loop
4904 if Nkind (Expr) /= N_Aggregate
4905 or else not Is_Others_Aggregate (Expr)
4910 if Present (Packed_Array_Impl_Type (Ctyp)) then
4914 if Has_Atomic_Components (Ctyp) then
4918 Index := First_Index (Ctyp);
4919 while Present (Index) loop
4920 Get_Index_Bounds (Index, Low, High);
4922 if Is_Null_Range (Low, High) then
4929 Expr := Expression (First (Component_Associations (Expr)));
4931 for J in 1 .. Number_Dimensions (Ctyp) - 1 loop
4932 if Nkind (Expr) /= N_Aggregate
4933 or else not Is_Others_Aggregate (Expr)
4938 Expr := Expression (First (Component_Associations (Expr)));
4941 Ctyp := Component_Type (Ctyp);
4943 if Is_Atomic_Or_VFA (Ctyp) then
4948 -- An Iterated_Component_Association involves a loop (in most cases)
4949 -- and is never static.
4951 if Nkind (Parent (Expr)) = N_Iterated_Component_Association then
4955 if not Is_Discrete_Type (Ctyp) then
4959 -- The expression needs to be analyzed if True is returned
4961 Analyze_And_Resolve (Expr, Ctyp);
4963 -- The back end uses the Esize as the precision of the type
4965 Nunits := UI_To_Int (Esize (Ctyp)) / System_Storage_Unit;
4971 if not Compile_Time_Known_Value (Expr) then
4975 Value := Expr_Value (Expr);
4977 if Has_Biased_Representation (Ctyp) then
4978 Value := Value - Expr_Value (Type_Low_Bound (Ctyp));
4981 -- Values 0 and -1 immediately satisfy the last check
4983 if Value = Uint_0 or else Value = Uint_Minus_1 then
4987 -- We need to work with an unsigned value
4990 Value := Value + 2**(System_Storage_Unit * Nunits);
4993 Remainder := Value rem 2**System_Storage_Unit;
4995 for J in 1 .. Nunits - 1 loop
4996 Value := Value / 2**System_Storage_Unit;
4998 if Value rem 2**System_Storage_Unit /= Remainder then
5004 end Aggr_Assignment_OK_For_Backend;
5006 ----------------------------
5007 -- Build_Constrained_Type --
5008 ----------------------------
5010 procedure Build_Constrained_Type (Positional : Boolean) is
5011 Loc : constant Source_Ptr := Sloc (N);
5012 Agg_Type : constant Entity_Id := Make_Temporary (Loc, 'A');
5015 Typ : constant Entity_Id := Etype (N);
5016 Indexes : constant List_Id := New_List;
5021 -- If the aggregate is purely positional, all its subaggregates
5022 -- have the same size. We collect the dimensions from the first
5023 -- subaggregate at each level.
5028 for D in 1 .. Number_Dimensions (Typ) loop
5029 Sub_Agg := First (Expressions (Sub_Agg));
5033 while Present (Comp) loop
5040 Low_Bound => Make_Integer_Literal (Loc, 1),
5041 High_Bound => Make_Integer_Literal (Loc, Num)));
5045 -- We know the aggregate type is unconstrained and the aggregate
5046 -- is not processable by the back end, therefore not necessarily
5047 -- positional. Retrieve each dimension bounds (computed earlier).
5049 for D in 1 .. Number_Dimensions (Typ) loop
5052 Low_Bound => Aggr_Low (D),
5053 High_Bound => Aggr_High (D)));
5058 Make_Full_Type_Declaration (Loc,
5059 Defining_Identifier => Agg_Type,
5061 Make_Constrained_Array_Definition (Loc,
5062 Discrete_Subtype_Definitions => Indexes,
5063 Component_Definition =>
5064 Make_Component_Definition (Loc,
5065 Aliased_Present => False,
5066 Subtype_Indication =>
5067 New_Occurrence_Of (Component_Type (Typ), Loc))));
5069 Insert_Action (N, Decl);
5071 Set_Etype (N, Agg_Type);
5072 Set_Is_Itype (Agg_Type);
5073 Freeze_Itype (Agg_Type, N);
5074 end Build_Constrained_Type;
5080 procedure Check_Bounds (Aggr_Bounds : Node_Id; Index_Bounds : Node_Id) is
5087 Cond : Node_Id := Empty;
5090 Get_Index_Bounds (Aggr_Bounds, Aggr_Lo, Aggr_Hi);
5091 Get_Index_Bounds (Index_Bounds, Ind_Lo, Ind_Hi);
5093 -- Generate the following test:
5095 -- [constraint_error when
5096 -- Aggr_Lo <= Aggr_Hi and then
5097 -- (Aggr_Lo < Ind_Lo or else Aggr_Hi > Ind_Hi)]
5099 -- As an optimization try to see if some tests are trivially vacuous
5100 -- because we are comparing an expression against itself.
5102 if Aggr_Lo = Ind_Lo and then Aggr_Hi = Ind_Hi then
5105 elsif Aggr_Hi = Ind_Hi then
5108 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
5109 Right_Opnd => Duplicate_Subexpr_Move_Checks (Ind_Lo));
5111 elsif Aggr_Lo = Ind_Lo then
5114 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi),
5115 Right_Opnd => Duplicate_Subexpr_Move_Checks (Ind_Hi));
5122 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
5123 Right_Opnd => Duplicate_Subexpr_Move_Checks (Ind_Lo)),
5127 Left_Opnd => Duplicate_Subexpr (Aggr_Hi),
5128 Right_Opnd => Duplicate_Subexpr (Ind_Hi)));
5131 if Present (Cond) then
5136 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
5137 Right_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi)),
5139 Right_Opnd => Cond);
5141 Set_Analyzed (Left_Opnd (Left_Opnd (Cond)), False);
5142 Set_Analyzed (Right_Opnd (Left_Opnd (Cond)), False);
5144 Make_Raise_Constraint_Error (Loc,
5146 Reason => CE_Range_Check_Failed));
5150 ----------------------------
5151 -- Check_Same_Aggr_Bounds --
5152 ----------------------------
5154 procedure Check_Same_Aggr_Bounds (Sub_Aggr : Node_Id; Dim : Pos) is
5155 Sub_Lo : constant Node_Id := Low_Bound (Aggregate_Bounds (Sub_Aggr));
5156 Sub_Hi : constant Node_Id := High_Bound (Aggregate_Bounds (Sub_Aggr));
5157 -- The bounds of this specific subaggregate
5159 Aggr_Lo : constant Node_Id := Aggr_Low (Dim);
5160 Aggr_Hi : constant Node_Id := Aggr_High (Dim);
5161 -- The bounds of the aggregate for this dimension
5163 Ind_Typ : constant Entity_Id := Aggr_Index_Typ (Dim);
5164 -- The index type for this dimension.xxx
5166 Cond : Node_Id := Empty;
5171 -- If index checks are on generate the test
5173 -- [constraint_error when
5174 -- Aggr_Lo /= Sub_Lo or else Aggr_Hi /= Sub_Hi]
5176 -- As an optimization try to see if some tests are trivially vacuos
5177 -- because we are comparing an expression against itself. Also for
5178 -- the first dimension the test is trivially vacuous because there
5179 -- is just one aggregate for dimension 1.
5181 if Index_Checks_Suppressed (Ind_Typ) then
5184 elsif Dim = 1 or else (Aggr_Lo = Sub_Lo and then Aggr_Hi = Sub_Hi)
5188 elsif Aggr_Hi = Sub_Hi then
5191 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
5192 Right_Opnd => Duplicate_Subexpr_Move_Checks (Sub_Lo));
5194 elsif Aggr_Lo = Sub_Lo then
5197 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi),
5198 Right_Opnd => Duplicate_Subexpr_Move_Checks (Sub_Hi));
5205 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
5206 Right_Opnd => Duplicate_Subexpr_Move_Checks (Sub_Lo)),
5210 Left_Opnd => Duplicate_Subexpr (Aggr_Hi),
5211 Right_Opnd => Duplicate_Subexpr (Sub_Hi)));
5214 if Present (Cond) then
5216 Make_Raise_Constraint_Error (Loc,
5218 Reason => CE_Length_Check_Failed));
5221 -- Now look inside the subaggregate to see if there is more work
5223 if Dim < Aggr_Dimension then
5225 -- Process positional components
5227 if Present (Expressions (Sub_Aggr)) then
5228 Expr := First (Expressions (Sub_Aggr));
5229 while Present (Expr) loop
5230 Check_Same_Aggr_Bounds (Expr, Dim + 1);
5235 -- Process component associations
5237 if Present (Component_Associations (Sub_Aggr)) then
5238 Assoc := First (Component_Associations (Sub_Aggr));
5239 while Present (Assoc) loop
5240 Expr := Expression (Assoc);
5241 Check_Same_Aggr_Bounds (Expr, Dim + 1);
5246 end Check_Same_Aggr_Bounds;
5248 ----------------------------
5249 -- Compute_Others_Present --
5250 ----------------------------
5252 procedure Compute_Others_Present (Sub_Aggr : Node_Id; Dim : Pos) is
5257 if Present (Component_Associations (Sub_Aggr)) then
5258 Assoc := Last (Component_Associations (Sub_Aggr));
5260 if Nkind (First (Choice_List (Assoc))) = N_Others_Choice then
5261 Others_Present (Dim) := True;
5265 -- Now look inside the subaggregate to see if there is more work
5267 if Dim < Aggr_Dimension then
5269 -- Process positional components
5271 if Present (Expressions (Sub_Aggr)) then
5272 Expr := First (Expressions (Sub_Aggr));
5273 while Present (Expr) loop
5274 Compute_Others_Present (Expr, Dim + 1);
5279 -- Process component associations
5281 if Present (Component_Associations (Sub_Aggr)) then
5282 Assoc := First (Component_Associations (Sub_Aggr));
5283 while Present (Assoc) loop
5284 Expr := Expression (Assoc);
5285 Compute_Others_Present (Expr, Dim + 1);
5290 end Compute_Others_Present;
5292 ------------------------
5293 -- In_Place_Assign_OK --
5294 ------------------------
5296 function In_Place_Assign_OK return Boolean is
5304 function Safe_Aggregate (Aggr : Node_Id) return Boolean;
5305 -- Check recursively that each component of a (sub)aggregate does not
5306 -- depend on the variable being assigned to.
5308 function Safe_Component (Expr : Node_Id) return Boolean;
5309 -- Verify that an expression cannot depend on the variable being
5310 -- assigned to. Room for improvement here (but less than before).
5312 --------------------
5313 -- Safe_Aggregate --
5314 --------------------
5316 function Safe_Aggregate (Aggr : Node_Id) return Boolean is
5320 if Present (Expressions (Aggr)) then
5321 Expr := First (Expressions (Aggr));
5322 while Present (Expr) loop
5323 if Nkind (Expr) = N_Aggregate then
5324 if not Safe_Aggregate (Expr) then
5328 elsif not Safe_Component (Expr) then
5336 if Present (Component_Associations (Aggr)) then
5337 Expr := First (Component_Associations (Aggr));
5338 while Present (Expr) loop
5339 if Nkind (Expression (Expr)) = N_Aggregate then
5340 if not Safe_Aggregate (Expression (Expr)) then
5344 -- If association has a box, no way to determine yet
5345 -- whether default can be assigned in place.
5347 elsif Box_Present (Expr) then
5350 elsif not Safe_Component (Expression (Expr)) then
5361 --------------------
5362 -- Safe_Component --
5363 --------------------
5365 function Safe_Component (Expr : Node_Id) return Boolean is
5366 Comp : Node_Id := Expr;
5368 function Check_Component (Comp : Node_Id) return Boolean;
5369 -- Do the recursive traversal, after copy
5371 ---------------------
5372 -- Check_Component --
5373 ---------------------
5375 function Check_Component (Comp : Node_Id) return Boolean is
5377 if Is_Overloaded (Comp) then
5381 return Compile_Time_Known_Value (Comp)
5383 or else (Is_Entity_Name (Comp)
5384 and then Present (Entity (Comp))
5385 and then No (Renamed_Object (Entity (Comp))))
5387 or else (Nkind (Comp) = N_Attribute_Reference
5388 and then Check_Component (Prefix (Comp)))
5390 or else (Nkind (Comp) in N_Binary_Op
5391 and then Check_Component (Left_Opnd (Comp))
5392 and then Check_Component (Right_Opnd (Comp)))
5394 or else (Nkind (Comp) in N_Unary_Op
5395 and then Check_Component (Right_Opnd (Comp)))
5397 or else (Nkind (Comp) = N_Selected_Component
5398 and then Check_Component (Prefix (Comp)))
5400 or else (Nkind (Comp) = N_Unchecked_Type_Conversion
5401 and then Check_Component (Expression (Comp)));
5402 end Check_Component;
5404 -- Start of processing for Safe_Component
5407 -- If the component appears in an association that may correspond
5408 -- to more than one element, it is not analyzed before expansion
5409 -- into assignments, to avoid side effects. We analyze, but do not
5410 -- resolve the copy, to obtain sufficient entity information for
5411 -- the checks that follow. If component is overloaded we assume
5412 -- an unsafe function call.
5414 if not Analyzed (Comp) then
5415 if Is_Overloaded (Expr) then
5418 elsif Nkind (Expr) = N_Aggregate
5419 and then not Is_Others_Aggregate (Expr)
5423 elsif Nkind (Expr) = N_Allocator then
5425 -- For now, too complex to analyze
5430 Comp := New_Copy_Tree (Expr);
5431 Set_Parent (Comp, Parent (Expr));
5435 if Nkind (Comp) = N_Aggregate then
5436 return Safe_Aggregate (Comp);
5438 return Check_Component (Comp);
5442 -- Start of processing for In_Place_Assign_OK
5445 if Present (Component_Associations (N)) then
5447 -- On assignment, sliding can take place, so we cannot do the
5448 -- assignment in place unless the bounds of the aggregate are
5449 -- statically equal to those of the target.
5451 -- If the aggregate is given by an others choice, the bounds are
5452 -- derived from the left-hand side, and the assignment is safe if
5453 -- the expression is.
5455 if Is_Others_Aggregate (N) then
5458 (Expression (First (Component_Associations (N))));
5461 Aggr_In := First_Index (Etype (N));
5463 if Nkind (Parent (N)) = N_Assignment_Statement then
5464 Obj_In := First_Index (Etype (Name (Parent (N))));
5467 -- Context is an allocator. Check bounds of aggregate against
5468 -- given type in qualified expression.
5470 pragma Assert (Nkind (Parent (Parent (N))) = N_Allocator);
5472 First_Index (Etype (Entity (Subtype_Mark (Parent (N)))));
5475 while Present (Aggr_In) loop
5476 Get_Index_Bounds (Aggr_In, Aggr_Lo, Aggr_Hi);
5477 Get_Index_Bounds (Obj_In, Obj_Lo, Obj_Hi);
5479 if not Compile_Time_Known_Value (Aggr_Lo)
5480 or else not Compile_Time_Known_Value (Aggr_Hi)
5481 or else not Compile_Time_Known_Value (Obj_Lo)
5482 or else not Compile_Time_Known_Value (Obj_Hi)
5483 or else Expr_Value (Aggr_Lo) /= Expr_Value (Obj_Lo)
5484 or else Expr_Value (Aggr_Hi) /= Expr_Value (Obj_Hi)
5489 Next_Index (Aggr_In);
5490 Next_Index (Obj_In);
5494 -- Now check the component values themselves
5496 return Safe_Aggregate (N);
5497 end In_Place_Assign_OK;
5503 procedure Others_Check (Sub_Aggr : Node_Id; Dim : Pos) is
5504 Aggr_Lo : constant Node_Id := Aggr_Low (Dim);
5505 Aggr_Hi : constant Node_Id := Aggr_High (Dim);
5506 -- The bounds of the aggregate for this dimension
5508 Ind_Typ : constant Entity_Id := Aggr_Index_Typ (Dim);
5509 -- The index type for this dimension
5511 Need_To_Check : Boolean := False;
5513 Choices_Lo : Node_Id := Empty;
5514 Choices_Hi : Node_Id := Empty;
5515 -- The lowest and highest discrete choices for a named subaggregate
5517 Nb_Choices : Int := -1;
5518 -- The number of discrete non-others choices in this subaggregate
5520 Nb_Elements : Uint := Uint_0;
5521 -- The number of elements in a positional aggregate
5523 Cond : Node_Id := Empty;
5530 -- Check if we have an others choice. If we do make sure that this
5531 -- subaggregate contains at least one element in addition to the
5534 if Range_Checks_Suppressed (Ind_Typ) then
5535 Need_To_Check := False;
5537 elsif Present (Expressions (Sub_Aggr))
5538 and then Present (Component_Associations (Sub_Aggr))
5540 Need_To_Check := True;
5542 elsif Present (Component_Associations (Sub_Aggr)) then
5543 Assoc := Last (Component_Associations (Sub_Aggr));
5545 if Nkind (First (Choice_List (Assoc))) /= N_Others_Choice then
5546 Need_To_Check := False;
5549 -- Count the number of discrete choices. Start with -1 because
5550 -- the others choice does not count.
5552 -- Is there some reason we do not use List_Length here ???
5555 Assoc := First (Component_Associations (Sub_Aggr));
5556 while Present (Assoc) loop
5557 Choice := First (Choice_List (Assoc));
5558 while Present (Choice) loop
5559 Nb_Choices := Nb_Choices + 1;
5566 -- If there is only an others choice nothing to do
5568 Need_To_Check := (Nb_Choices > 0);
5572 Need_To_Check := False;
5575 -- If we are dealing with a positional subaggregate with an others
5576 -- choice then compute the number or positional elements.
5578 if Need_To_Check and then Present (Expressions (Sub_Aggr)) then
5579 Expr := First (Expressions (Sub_Aggr));
5580 Nb_Elements := Uint_0;
5581 while Present (Expr) loop
5582 Nb_Elements := Nb_Elements + 1;
5586 -- If the aggregate contains discrete choices and an others choice
5587 -- compute the smallest and largest discrete choice values.
5589 elsif Need_To_Check then
5590 Compute_Choices_Lo_And_Choices_Hi : declare
5592 Table : Case_Table_Type (1 .. Nb_Choices);
5593 -- Used to sort all the different choice values
5600 Assoc := First (Component_Associations (Sub_Aggr));
5601 while Present (Assoc) loop
5602 Choice := First (Choice_List (Assoc));
5603 while Present (Choice) loop
5604 if Nkind (Choice) = N_Others_Choice then
5608 Get_Index_Bounds (Choice, Low, High);
5609 Table (J).Choice_Lo := Low;
5610 Table (J).Choice_Hi := High;
5619 -- Sort the discrete choices
5621 Sort_Case_Table (Table);
5623 Choices_Lo := Table (1).Choice_Lo;
5624 Choices_Hi := Table (Nb_Choices).Choice_Hi;
5625 end Compute_Choices_Lo_And_Choices_Hi;
5628 -- If no others choice in this subaggregate, or the aggregate
5629 -- comprises only an others choice, nothing to do.
5631 if not Need_To_Check then
5634 -- If we are dealing with an aggregate containing an others choice
5635 -- and positional components, we generate the following test:
5637 -- if Ind_Typ'Pos (Aggr_Lo) + (Nb_Elements - 1) >
5638 -- Ind_Typ'Pos (Aggr_Hi)
5640 -- raise Constraint_Error;
5643 elsif Nb_Elements > Uint_0 then
5649 Make_Attribute_Reference (Loc,
5650 Prefix => New_Occurrence_Of (Ind_Typ, Loc),
5651 Attribute_Name => Name_Pos,
5654 (Duplicate_Subexpr_Move_Checks (Aggr_Lo))),
5655 Right_Opnd => Make_Integer_Literal (Loc, Nb_Elements - 1)),
5658 Make_Attribute_Reference (Loc,
5659 Prefix => New_Occurrence_Of (Ind_Typ, Loc),
5660 Attribute_Name => Name_Pos,
5661 Expressions => New_List (
5662 Duplicate_Subexpr_Move_Checks (Aggr_Hi))));
5664 -- If we are dealing with an aggregate containing an others choice
5665 -- and discrete choices we generate the following test:
5667 -- [constraint_error when
5668 -- Choices_Lo < Aggr_Lo or else Choices_Hi > Aggr_Hi];
5675 Left_Opnd => Duplicate_Subexpr_Move_Checks (Choices_Lo),
5676 Right_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo)),
5680 Left_Opnd => Duplicate_Subexpr (Choices_Hi),
5681 Right_Opnd => Duplicate_Subexpr (Aggr_Hi)));
5684 if Present (Cond) then
5686 Make_Raise_Constraint_Error (Loc,
5688 Reason => CE_Length_Check_Failed));
5689 -- Questionable reason code, shouldn't that be a
5690 -- CE_Range_Check_Failed ???
5693 -- Now look inside the subaggregate to see if there is more work
5695 if Dim < Aggr_Dimension then
5697 -- Process positional components
5699 if Present (Expressions (Sub_Aggr)) then
5700 Expr := First (Expressions (Sub_Aggr));
5701 while Present (Expr) loop
5702 Others_Check (Expr, Dim + 1);
5707 -- Process component associations
5709 if Present (Component_Associations (Sub_Aggr)) then
5710 Assoc := First (Component_Associations (Sub_Aggr));
5711 while Present (Assoc) loop
5712 Expr := Expression (Assoc);
5713 Others_Check (Expr, Dim + 1);
5720 -------------------------
5721 -- Safe_Left_Hand_Side --
5722 -------------------------
5724 function Safe_Left_Hand_Side (N : Node_Id) return Boolean is
5725 function Is_Safe_Index (Indx : Node_Id) return Boolean;
5726 -- If the left-hand side includes an indexed component, check that
5727 -- the indexes are free of side effects.
5733 function Is_Safe_Index (Indx : Node_Id) return Boolean is
5735 if Is_Entity_Name (Indx) then
5738 elsif Nkind (Indx) = N_Integer_Literal then
5741 elsif Nkind (Indx) = N_Function_Call
5742 and then Is_Entity_Name (Name (Indx))
5743 and then Has_Pragma_Pure_Function (Entity (Name (Indx)))
5747 elsif Nkind (Indx) = N_Type_Conversion
5748 and then Is_Safe_Index (Expression (Indx))
5757 -- Start of processing for Safe_Left_Hand_Side
5760 if Is_Entity_Name (N) then
5763 elsif Nkind_In (N, N_Explicit_Dereference, N_Selected_Component)
5764 and then Safe_Left_Hand_Side (Prefix (N))
5768 elsif Nkind (N) = N_Indexed_Component
5769 and then Safe_Left_Hand_Side (Prefix (N))
5770 and then Is_Safe_Index (First (Expressions (N)))
5774 elsif Nkind (N) = N_Unchecked_Type_Conversion then
5775 return Safe_Left_Hand_Side (Expression (N));
5780 end Safe_Left_Hand_Side;
5785 -- Holds the temporary aggregate value
5788 -- Holds the declaration of Tmp
5790 Aggr_Code : List_Id;
5791 Parent_Node : Node_Id;
5792 Parent_Kind : Node_Kind;
5794 -- Start of processing for Expand_Array_Aggregate
5797 -- Do not touch the special aggregates of attributes used for Asm calls
5799 if Is_RTE (Ctyp, RE_Asm_Input_Operand)
5800 or else Is_RTE (Ctyp, RE_Asm_Output_Operand)
5804 -- Do not expand an aggregate for an array type which contains tasks if
5805 -- the aggregate is associated with an unexpanded return statement of a
5806 -- build-in-place function. The aggregate is expanded when the related
5807 -- return statement (rewritten into an extended return) is processed.
5808 -- This delay ensures that any temporaries and initialization code
5809 -- generated for the aggregate appear in the proper return block and
5810 -- use the correct _chain and _master.
5812 elsif Has_Task (Base_Type (Etype (N)))
5813 and then Nkind (Parent (N)) = N_Simple_Return_Statement
5814 and then Is_Build_In_Place_Function
5815 (Return_Applies_To (Return_Statement_Entity (Parent (N))))
5819 -- Do not attempt expansion if error already detected. We may reach this
5820 -- point in spite of previous errors when compiling with -gnatq, to
5821 -- force all possible errors (this is the usual ACATS mode).
5823 elsif Error_Posted (N) then
5827 -- If the semantic analyzer has determined that aggregate N will raise
5828 -- Constraint_Error at run time, then the aggregate node has been
5829 -- replaced with an N_Raise_Constraint_Error node and we should
5832 pragma Assert (not Raises_Constraint_Error (N));
5836 -- Check that the index range defined by aggregate bounds is
5837 -- compatible with corresponding index subtype.
5839 Index_Compatibility_Check : declare
5840 Aggr_Index_Range : Node_Id := First_Index (Typ);
5841 -- The current aggregate index range
5843 Index_Constraint : Node_Id := First_Index (Etype (Typ));
5844 -- The corresponding index constraint against which we have to
5845 -- check the above aggregate index range.
5848 Compute_Others_Present (N, 1);
5850 for J in 1 .. Aggr_Dimension loop
5851 -- There is no need to emit a check if an others choice is present
5852 -- for this array aggregate dimension since in this case one of
5853 -- N's subaggregates has taken its bounds from the context and
5854 -- these bounds must have been checked already. In addition all
5855 -- subaggregates corresponding to the same dimension must all have
5856 -- the same bounds (checked in (c) below).
5858 if not Range_Checks_Suppressed (Etype (Index_Constraint))
5859 and then not Others_Present (J)
5861 -- We don't use Checks.Apply_Range_Check here because it emits
5862 -- a spurious check. Namely it checks that the range defined by
5863 -- the aggregate bounds is nonempty. But we know this already
5866 Check_Bounds (Aggr_Index_Range, Index_Constraint);
5869 -- Save the low and high bounds of the aggregate index as well as
5870 -- the index type for later use in checks (b) and (c) below.
5872 Aggr_Low (J) := Low_Bound (Aggr_Index_Range);
5873 Aggr_High (J) := High_Bound (Aggr_Index_Range);
5875 Aggr_Index_Typ (J) := Etype (Index_Constraint);
5877 Next_Index (Aggr_Index_Range);
5878 Next_Index (Index_Constraint);
5880 end Index_Compatibility_Check;
5884 -- If an others choice is present check that no aggregate index is
5885 -- outside the bounds of the index constraint.
5887 Others_Check (N, 1);
5891 -- For multidimensional arrays make sure that all subaggregates
5892 -- corresponding to the same dimension have the same bounds.
5894 if Aggr_Dimension > 1 then
5895 Check_Same_Aggr_Bounds (N, 1);
5900 -- If we have a default component value, or simple initialization is
5901 -- required for the component type, then we replace <> in component
5902 -- associations by the required default value.
5905 Default_Val : Node_Id;
5909 if (Present (Default_Aspect_Component_Value (Typ))
5910 or else Needs_Simple_Initialization (Ctyp))
5911 and then Present (Component_Associations (N))
5913 Assoc := First (Component_Associations (N));
5914 while Present (Assoc) loop
5915 if Nkind (Assoc) = N_Component_Association
5916 and then Box_Present (Assoc)
5918 Set_Box_Present (Assoc, False);
5920 if Present (Default_Aspect_Component_Value (Typ)) then
5921 Default_Val := Default_Aspect_Component_Value (Typ);
5923 Default_Val := Get_Simple_Init_Val (Ctyp, N);
5926 Set_Expression (Assoc, New_Copy_Tree (Default_Val));
5927 Analyze_And_Resolve (Expression (Assoc), Ctyp);
5937 -- Here we test for is packed array aggregate that we can handle at
5938 -- compile time. If so, return with transformation done. Note that we do
5939 -- this even if the aggregate is nested, because once we have done this
5940 -- processing, there is no more nested aggregate.
5942 if Packed_Array_Aggregate_Handled (N) then
5946 -- At this point we try to convert to positional form
5948 if Ekind (Current_Scope) = E_Package
5949 and then Static_Elaboration_Desired (Current_Scope)
5951 Convert_To_Positional (N, Max_Others_Replicate => 100);
5953 Convert_To_Positional (N);
5956 -- if the result is no longer an aggregate (e.g. it may be a string
5957 -- literal, or a temporary which has the needed value), then we are
5958 -- done, since there is no longer a nested aggregate.
5960 if Nkind (N) /= N_Aggregate then
5963 -- We are also done if the result is an analyzed aggregate, indicating
5964 -- that Convert_To_Positional succeeded and reanalyzed the rewritten
5967 elsif Analyzed (N) and then N /= Original_Node (N) then
5971 -- If all aggregate components are compile-time known and the aggregate
5972 -- has been flattened, nothing left to do. The same occurs if the
5973 -- aggregate is used to initialize the components of a statically
5974 -- allocated dispatch table.
5976 if Compile_Time_Known_Aggregate (N)
5977 or else Is_Static_Dispatch_Table_Aggregate (N)
5979 Set_Expansion_Delayed (N, False);
5983 -- Now see if back end processing is possible
5985 if Backend_Processing_Possible (N) then
5987 -- If the aggregate is static but the constraints are not, build
5988 -- a static subtype for the aggregate, so that Gigi can place it
5989 -- in static memory. Perform an unchecked_conversion to the non-
5990 -- static type imposed by the context.
5993 Itype : constant Entity_Id := Etype (N);
5995 Needs_Type : Boolean := False;
5998 Index := First_Index (Itype);
5999 while Present (Index) loop
6000 if not Is_OK_Static_Subtype (Etype (Index)) then
6009 Build_Constrained_Type (Positional => True);
6010 Rewrite (N, Unchecked_Convert_To (Itype, N));
6020 -- Delay expansion for nested aggregates: it will be taken care of when
6021 -- the parent aggregate is expanded.
6023 Parent_Node := Parent (N);
6024 Parent_Kind := Nkind (Parent_Node);
6026 if Parent_Kind = N_Qualified_Expression then
6027 Parent_Node := Parent (Parent_Node);
6028 Parent_Kind := Nkind (Parent_Node);
6031 if Parent_Kind = N_Aggregate
6032 or else Parent_Kind = N_Extension_Aggregate
6033 or else Parent_Kind = N_Component_Association
6034 or else (Parent_Kind = N_Object_Declaration
6035 and then Needs_Finalization (Typ))
6036 or else (Parent_Kind = N_Assignment_Statement
6037 and then Inside_Init_Proc)
6039 if Static_Array_Aggregate (N)
6040 or else Compile_Time_Known_Aggregate (N)
6042 Set_Expansion_Delayed (N, False);
6045 Set_Expansion_Delayed (N);
6052 -- Look if in place aggregate expansion is possible
6054 -- For object declarations we build the aggregate in place, unless
6055 -- the array is bit-packed or the component is controlled.
6057 -- For assignments we do the assignment in place if all the component
6058 -- associations have compile-time known values. For other cases we
6059 -- create a temporary. The analysis for safety of on-line assignment
6060 -- is delicate, i.e. we don't know how to do it fully yet ???
6062 -- For allocators we assign to the designated object in place if the
6063 -- aggregate meets the same conditions as other in-place assignments.
6064 -- In this case the aggregate may not come from source but was created
6065 -- for default initialization, e.g. with Initialize_Scalars.
6067 if Requires_Transient_Scope (Typ) then
6068 Establish_Transient_Scope
6069 (N, Sec_Stack => Has_Controlled_Component (Typ));
6072 if Has_Default_Init_Comps (N) then
6073 Maybe_In_Place_OK := False;
6075 elsif Is_Bit_Packed_Array (Typ)
6076 or else Has_Controlled_Component (Typ)
6078 Maybe_In_Place_OK := False;
6081 Maybe_In_Place_OK :=
6082 (Nkind (Parent (N)) = N_Assignment_Statement
6083 and then In_Place_Assign_OK)
6086 (Nkind (Parent (Parent (N))) = N_Allocator
6087 and then In_Place_Assign_OK);
6090 -- If this is an array of tasks, it will be expanded into build-in-place
6091 -- assignments. Build an activation chain for the tasks now.
6093 if Has_Task (Etype (N)) then
6094 Build_Activation_Chain_Entity (N);
6097 -- Perform in-place expansion of aggregate in an object declaration.
6098 -- Note: actions generated for the aggregate will be captured in an
6099 -- expression-with-actions statement so that they can be transferred
6100 -- to freeze actions later if there is an address clause for the
6101 -- object. (Note: we don't use a block statement because this would
6102 -- cause generated freeze nodes to be elaborated in the wrong scope).
6104 -- Do not perform in-place expansion for SPARK 05 because aggregates are
6105 -- expected to appear in qualified form. In-place expansion eliminates
6106 -- the qualification and eventually violates this SPARK 05 restiction.
6108 -- Should document the rest of the guards ???
6110 if not Has_Default_Init_Comps (N)
6111 and then Comes_From_Source (Parent_Node)
6112 and then Parent_Kind = N_Object_Declaration
6113 and then Present (Expression (Parent_Node))
6115 Must_Slide (Etype (Defining_Identifier (Parent_Node)), Typ)
6116 and then not Has_Controlled_Component (Typ)
6117 and then not Is_Bit_Packed_Array (Typ)
6118 and then not Restriction_Check_Required (SPARK_05)
6120 In_Place_Assign_OK_For_Declaration := True;
6121 Tmp := Defining_Identifier (Parent_Node);
6122 Set_No_Initialization (Parent_Node);
6123 Set_Expression (Parent_Node, Empty);
6125 -- Set kind and type of the entity, for use in the analysis
6126 -- of the subsequent assignments. If the nominal type is not
6127 -- constrained, build a subtype from the known bounds of the
6128 -- aggregate. If the declaration has a subtype mark, use it,
6129 -- otherwise use the itype of the aggregate.
6131 Set_Ekind (Tmp, E_Variable);
6133 if not Is_Constrained (Typ) then
6134 Build_Constrained_Type (Positional => False);
6136 elsif Is_Entity_Name (Object_Definition (Parent_Node))
6137 and then Is_Constrained (Entity (Object_Definition (Parent_Node)))
6139 Set_Etype (Tmp, Entity (Object_Definition (Parent_Node)));
6142 Set_Size_Known_At_Compile_Time (Typ, False);
6143 Set_Etype (Tmp, Typ);
6146 elsif Maybe_In_Place_OK
6147 and then Nkind (Parent (N)) = N_Qualified_Expression
6148 and then Nkind (Parent (Parent (N))) = N_Allocator
6150 Set_Expansion_Delayed (N);
6153 -- In the remaining cases the aggregate is the RHS of an assignment
6155 elsif Maybe_In_Place_OK
6156 and then Safe_Left_Hand_Side (Name (Parent (N)))
6158 Tmp := Name (Parent (N));
6160 if Etype (Tmp) /= Etype (N) then
6161 Apply_Length_Check (N, Etype (Tmp));
6163 if Nkind (N) = N_Raise_Constraint_Error then
6165 -- Static error, nothing further to expand
6171 -- If a slice assignment has an aggregate with a single others_choice,
6172 -- the assignment can be done in place even if bounds are not static,
6173 -- by converting it into a loop over the discrete range of the slice.
6175 elsif Maybe_In_Place_OK
6176 and then Nkind (Name (Parent (N))) = N_Slice
6177 and then Is_Others_Aggregate (N)
6179 Tmp := Name (Parent (N));
6181 -- Set type of aggregate to be type of lhs in assignment, in order
6182 -- to suppress redundant length checks.
6184 Set_Etype (N, Etype (Tmp));
6188 -- In place aggregate expansion is not possible
6191 Maybe_In_Place_OK := False;
6192 Tmp := Make_Temporary (Loc, 'A', N);
6194 Make_Object_Declaration (Loc,
6195 Defining_Identifier => Tmp,
6196 Object_Definition => New_Occurrence_Of (Typ, Loc));
6197 Set_No_Initialization (Tmp_Decl, True);
6199 -- If we are within a loop, the temporary will be pushed on the
6200 -- stack at each iteration. If the aggregate is the expression for an
6201 -- allocator, it will be immediately copied to the heap and can
6202 -- be reclaimed at once. We create a transient scope around the
6203 -- aggregate for this purpose.
6205 if Ekind (Current_Scope) = E_Loop
6206 and then Nkind (Parent (Parent (N))) = N_Allocator
6208 Establish_Transient_Scope (N, False);
6211 Insert_Action (N, Tmp_Decl);
6214 -- Construct and insert the aggregate code. We can safely suppress index
6215 -- checks because this code is guaranteed not to raise CE on index
6216 -- checks. However we should *not* suppress all checks.
6222 if Nkind (Tmp) = N_Defining_Identifier then
6223 Target := New_Occurrence_Of (Tmp, Loc);
6226 if Has_Default_Init_Comps (N) then
6228 -- Ada 2005 (AI-287): This case has not been analyzed???
6230 raise Program_Error;
6233 -- Name in assignment is explicit dereference
6235 Target := New_Copy (Tmp);
6238 -- If we are to generate an in place assignment for a declaration or
6239 -- an assignment statement, and the assignment can be done directly
6240 -- by the back end, then do not expand further.
6242 -- ??? We can also do that if in place expansion is not possible but
6243 -- then we could go into an infinite recursion.
6245 if (In_Place_Assign_OK_For_Declaration or else Maybe_In_Place_OK)
6246 and then not AAMP_On_Target
6247 and then not CodePeer_Mode
6248 and then not Generate_C_Code
6249 and then not Possible_Bit_Aligned_Component (Target)
6250 and then not Is_Possibly_Unaligned_Slice (Target)
6251 and then Aggr_Assignment_OK_For_Backend (N)
6253 if Maybe_In_Place_OK then
6259 Make_Assignment_Statement (Loc,
6261 Expression => New_Copy (N)));
6265 Build_Array_Aggr_Code (N,
6267 Index => First_Index (Typ),
6269 Scalar_Comp => Is_Scalar_Type (Ctyp));
6272 -- Save the last assignment statement associated with the aggregate
6273 -- when building a controlled object. This reference is utilized by
6274 -- the finalization machinery when marking an object as successfully
6277 if Needs_Finalization (Typ)
6278 and then Is_Entity_Name (Target)
6279 and then Present (Entity (Target))
6280 and then Ekind_In (Entity (Target), E_Constant, E_Variable)
6282 Set_Last_Aggregate_Assignment (Entity (Target), Last (Aggr_Code));
6286 -- If the aggregate is the expression in a declaration, the expanded
6287 -- code must be inserted after it. The defining entity might not come
6288 -- from source if this is part of an inlined body, but the declaration
6291 if Comes_From_Source (Tmp)
6293 (Nkind (Parent (N)) = N_Object_Declaration
6294 and then Comes_From_Source (Parent (N))
6295 and then Tmp = Defining_Entity (Parent (N)))
6298 Node_After : constant Node_Id := Next (Parent_Node);
6301 Insert_Actions_After (Parent_Node, Aggr_Code);
6303 if Parent_Kind = N_Object_Declaration then
6304 Collect_Initialization_Statements
6305 (Obj => Tmp, N => Parent_Node, Node_After => Node_After);
6310 Insert_Actions (N, Aggr_Code);
6313 -- If the aggregate has been assigned in place, remove the original
6316 if Nkind (Parent (N)) = N_Assignment_Statement
6317 and then Maybe_In_Place_OK
6319 Rewrite (Parent (N), Make_Null_Statement (Loc));
6321 elsif Nkind (Parent (N)) /= N_Object_Declaration
6322 or else Tmp /= Defining_Identifier (Parent (N))
6324 Rewrite (N, New_Occurrence_Of (Tmp, Loc));
6325 Analyze_And_Resolve (N, Typ);
6327 end Expand_Array_Aggregate;
6329 ------------------------
6330 -- Expand_N_Aggregate --
6331 ------------------------
6333 procedure Expand_N_Aggregate (N : Node_Id) is
6335 -- Record aggregate case
6337 if Is_Record_Type (Etype (N)) then
6338 Expand_Record_Aggregate (N);
6340 -- Array aggregate case
6343 -- A special case, if we have a string subtype with bounds 1 .. N,
6344 -- where N is known at compile time, and the aggregate is of the
6345 -- form (others => 'x'), with a single choice and no expressions,
6346 -- and N is less than 80 (an arbitrary limit for now), then replace
6347 -- the aggregate by the equivalent string literal (but do not mark
6348 -- it as static since it is not).
6350 -- Note: this entire circuit is redundant with respect to code in
6351 -- Expand_Array_Aggregate that collapses others choices to positional
6352 -- form, but there are two problems with that circuit:
6354 -- a) It is limited to very small cases due to ill-understood
6355 -- interactions with bootstrapping. That limit is removed by
6356 -- use of the No_Implicit_Loops restriction.
6358 -- b) It incorrectly ends up with the resulting expressions being
6359 -- considered static when they are not. For example, the
6360 -- following test should fail:
6362 -- pragma Restrictions (No_Implicit_Loops);
6363 -- package NonSOthers4 is
6364 -- B : constant String (1 .. 6) := (others => 'A');
6365 -- DH : constant String (1 .. 8) := B & "BB";
6367 -- pragma Export (C, X, Link_Name => DH);
6370 -- But it succeeds (DH looks static to pragma Export)
6372 -- To be sorted out ???
6374 if Present (Component_Associations (N)) then
6376 CA : constant Node_Id := First (Component_Associations (N));
6377 MX : constant := 80;
6380 if Nkind (First (Choice_List (CA))) = N_Others_Choice
6381 and then Nkind (Expression (CA)) = N_Character_Literal
6382 and then No (Expressions (N))
6385 T : constant Entity_Id := Etype (N);
6386 X : constant Node_Id := First_Index (T);
6387 EC : constant Node_Id := Expression (CA);
6388 CV : constant Uint := Char_Literal_Value (EC);
6389 CC : constant Int := UI_To_Int (CV);
6392 if Nkind (X) = N_Range
6393 and then Compile_Time_Known_Value (Low_Bound (X))
6394 and then Expr_Value (Low_Bound (X)) = 1
6395 and then Compile_Time_Known_Value (High_Bound (X))
6398 Hi : constant Uint := Expr_Value (High_Bound (X));
6404 for J in 1 .. UI_To_Int (Hi) loop
6405 Store_String_Char (Char_Code (CC));
6409 Make_String_Literal (Sloc (N),
6410 Strval => End_String));
6412 if CC >= Int (2 ** 16) then
6413 Set_Has_Wide_Wide_Character (N);
6414 elsif CC >= Int (2 ** 8) then
6415 Set_Has_Wide_Character (N);
6418 Analyze_And_Resolve (N, T);
6419 Set_Is_Static_Expression (N, False);
6429 -- Not that special case, so normal expansion of array aggregate
6431 Expand_Array_Aggregate (N);
6435 when RE_Not_Available =>
6437 end Expand_N_Aggregate;
6439 ----------------------------------
6440 -- Expand_N_Extension_Aggregate --
6441 ----------------------------------
6443 -- If the ancestor part is an expression, add a component association for
6444 -- the parent field. If the type of the ancestor part is not the direct
6445 -- parent of the expected type, build recursively the needed ancestors.
6446 -- If the ancestor part is a subtype_mark, replace aggregate with a decla-
6447 -- ration for a temporary of the expected type, followed by individual
6448 -- assignments to the given components.
6450 procedure Expand_N_Extension_Aggregate (N : Node_Id) is
6451 Loc : constant Source_Ptr := Sloc (N);
6452 A : constant Node_Id := Ancestor_Part (N);
6453 Typ : constant Entity_Id := Etype (N);
6456 -- If the ancestor is a subtype mark, an init proc must be called
6457 -- on the resulting object which thus has to be materialized in
6460 if Is_Entity_Name (A) and then Is_Type (Entity (A)) then
6461 Convert_To_Assignments (N, Typ);
6463 -- The extension aggregate is transformed into a record aggregate
6464 -- of the following form (c1 and c2 are inherited components)
6466 -- (Exp with c3 => a, c4 => b)
6467 -- ==> (c1 => Exp.c1, c2 => Exp.c2, c3 => a, c4 => b)
6472 if Tagged_Type_Expansion then
6473 Expand_Record_Aggregate (N,
6476 (Node (First_Elmt (Access_Disp_Table (Typ))), Loc),
6479 -- No tag is needed in the case of a VM
6482 Expand_Record_Aggregate (N, Parent_Expr => A);
6487 when RE_Not_Available =>
6489 end Expand_N_Extension_Aggregate;
6491 -----------------------------
6492 -- Expand_Record_Aggregate --
6493 -----------------------------
6495 procedure Expand_Record_Aggregate
6497 Orig_Tag : Node_Id := Empty;
6498 Parent_Expr : Node_Id := Empty)
6500 Loc : constant Source_Ptr := Sloc (N);
6501 Comps : constant List_Id := Component_Associations (N);
6502 Typ : constant Entity_Id := Etype (N);
6503 Base_Typ : constant Entity_Id := Base_Type (Typ);
6505 Static_Components : Boolean := True;
6506 -- Flag to indicate whether all components are compile-time known,
6507 -- and the aggregate can be constructed statically and handled by
6510 function Compile_Time_Known_Composite_Value (N : Node_Id) return Boolean;
6511 -- Returns true if N is an expression of composite type which can be
6512 -- fully evaluated at compile time without raising constraint error.
6513 -- Such expressions can be passed as is to Gigi without any expansion.
6515 -- This returns true for N_Aggregate with Compile_Time_Known_Aggregate
6516 -- set and constants whose expression is such an aggregate, recursively.
6518 function Component_Not_OK_For_Backend return Boolean;
6519 -- Check for presence of a component which makes it impossible for the
6520 -- backend to process the aggregate, thus requiring the use of a series
6521 -- of assignment statements. Cases checked for are a nested aggregate
6522 -- needing Late_Expansion, the presence of a tagged component which may
6523 -- need tag adjustment, and a bit unaligned component reference.
6525 -- We also force expansion into assignments if a component is of a
6526 -- mutable type (including a private type with discriminants) because
6527 -- in that case the size of the component to be copied may be smaller
6528 -- than the side of the target, and there is no simple way for gigi
6529 -- to compute the size of the object to be copied.
6531 -- NOTE: This is part of the ongoing work to define precisely the
6532 -- interface between front-end and back-end handling of aggregates.
6533 -- In general it is desirable to pass aggregates as they are to gigi,
6534 -- in order to minimize elaboration code. This is one case where the
6535 -- semantics of Ada complicate the analysis and lead to anomalies in
6536 -- the gcc back-end if the aggregate is not expanded into assignments.
6538 function Has_Per_Object_Constraint (L : List_Id) return Boolean;
6539 -- Return True if any element of L has Has_Per_Object_Constraint set.
6540 -- L should be the Choices component of an N_Component_Association.
6542 function Has_Visible_Private_Ancestor (Id : E) return Boolean;
6543 -- If any ancestor of the current type is private, the aggregate
6544 -- cannot be built in place. We cannot rely on Has_Private_Ancestor,
6545 -- because it will not be set when type and its parent are in the
6546 -- same scope, and the parent component needs expansion.
6548 function Top_Level_Aggregate (N : Node_Id) return Node_Id;
6549 -- For nested aggregates return the ultimate enclosing aggregate; for
6550 -- non-nested aggregates return N.
6552 ----------------------------------------
6553 -- Compile_Time_Known_Composite_Value --
6554 ----------------------------------------
6556 function Compile_Time_Known_Composite_Value
6557 (N : Node_Id) return Boolean
6560 -- If we have an entity name, then see if it is the name of a
6561 -- constant and if so, test the corresponding constant value.
6563 if Is_Entity_Name (N) then
6565 E : constant Entity_Id := Entity (N);
6568 if Ekind (E) /= E_Constant then
6571 V := Constant_Value (E);
6573 and then Compile_Time_Known_Composite_Value (V);
6577 -- We have a value, see if it is compile time known
6580 if Nkind (N) = N_Aggregate then
6581 return Compile_Time_Known_Aggregate (N);
6584 -- All other types of values are not known at compile time
6589 end Compile_Time_Known_Composite_Value;
6591 ----------------------------------
6592 -- Component_Not_OK_For_Backend --
6593 ----------------------------------
6595 function Component_Not_OK_For_Backend return Boolean is
6605 while Present (C) loop
6607 -- If the component has box initialization, expansion is needed
6608 -- and component is not ready for backend.
6610 if Box_Present (C) then
6614 if Nkind (Expression (C)) = N_Qualified_Expression then
6615 Expr_Q := Expression (Expression (C));
6617 Expr_Q := Expression (C);
6620 -- Return true if the aggregate has any associations for tagged
6621 -- components that may require tag adjustment.
6623 -- These are cases where the source expression may have a tag that
6624 -- could differ from the component tag (e.g., can occur for type
6625 -- conversions and formal parameters). (Tag adjustment not needed
6626 -- if Tagged_Type_Expansion because object tags are implicit in
6629 if Is_Tagged_Type (Etype (Expr_Q))
6630 and then (Nkind (Expr_Q) = N_Type_Conversion
6631 or else (Is_Entity_Name (Expr_Q)
6633 Ekind (Entity (Expr_Q)) in Formal_Kind))
6634 and then Tagged_Type_Expansion
6636 Static_Components := False;
6639 elsif Is_Delayed_Aggregate (Expr_Q) then
6640 Static_Components := False;
6643 elsif Possible_Bit_Aligned_Component (Expr_Q) then
6644 Static_Components := False;
6647 elsif Modify_Tree_For_C
6648 and then Nkind (C) = N_Component_Association
6649 and then Has_Per_Object_Constraint (Choices (C))
6651 Static_Components := False;
6654 elsif Modify_Tree_For_C
6655 and then Nkind (Expr_Q) = N_Identifier
6656 and then Is_Array_Type (Etype (Expr_Q))
6658 Static_Components := False;
6662 if Is_Elementary_Type (Etype (Expr_Q)) then
6663 if not Compile_Time_Known_Value (Expr_Q) then
6664 Static_Components := False;
6667 elsif not Compile_Time_Known_Composite_Value (Expr_Q) then
6668 Static_Components := False;
6670 if Is_Private_Type (Etype (Expr_Q))
6671 and then Has_Discriminants (Etype (Expr_Q))
6681 end Component_Not_OK_For_Backend;
6683 -------------------------------
6684 -- Has_Per_Object_Constraint --
6685 -------------------------------
6687 function Has_Per_Object_Constraint (L : List_Id) return Boolean is
6688 N : Node_Id := First (L);
6690 while Present (N) loop
6691 if Is_Entity_Name (N)
6692 and then Present (Entity (N))
6693 and then Has_Per_Object_Constraint (Entity (N))
6702 end Has_Per_Object_Constraint;
6704 -----------------------------------
6705 -- Has_Visible_Private_Ancestor --
6706 -----------------------------------
6708 function Has_Visible_Private_Ancestor (Id : E) return Boolean is
6709 R : constant Entity_Id := Root_Type (Id);
6710 T1 : Entity_Id := Id;
6714 if Is_Private_Type (T1) then
6724 end Has_Visible_Private_Ancestor;
6726 -------------------------
6727 -- Top_Level_Aggregate --
6728 -------------------------
6730 function Top_Level_Aggregate (N : Node_Id) return Node_Id is
6735 while Present (Parent (Aggr))
6736 and then Nkind_In (Parent (Aggr), N_Component_Association,
6739 Aggr := Parent (Aggr);
6743 end Top_Level_Aggregate;
6747 Top_Level_Aggr : constant Node_Id := Top_Level_Aggregate (N);
6748 Tag_Value : Node_Id;
6752 -- Start of processing for Expand_Record_Aggregate
6755 -- If the aggregate is to be assigned to an atomic/VFA variable, we have
6756 -- to prevent a piecemeal assignment even if the aggregate is to be
6757 -- expanded. We create a temporary for the aggregate, and assign the
6758 -- temporary instead, so that the back end can generate an atomic move
6761 if Is_Atomic_VFA_Aggregate (N) then
6764 -- No special management required for aggregates used to initialize
6765 -- statically allocated dispatch tables
6767 elsif Is_Static_Dispatch_Table_Aggregate (N) then
6771 -- Ada 2005 (AI-318-2): We need to convert to assignments if components
6772 -- are build-in-place function calls. The assignments will each turn
6773 -- into a build-in-place function call. If components are all static,
6774 -- we can pass the aggregate to the backend regardless of limitedness.
6776 -- Extension aggregates, aggregates in extended return statements, and
6777 -- aggregates for C++ imported types must be expanded.
6779 if Ada_Version >= Ada_2005 and then Is_Limited_View (Typ) then
6780 if not Nkind_In (Parent (N), N_Object_Declaration,
6781 N_Component_Association)
6783 Convert_To_Assignments (N, Typ);
6785 elsif Nkind (N) = N_Extension_Aggregate
6786 or else Convention (Typ) = Convention_CPP
6788 Convert_To_Assignments (N, Typ);
6790 elsif not Size_Known_At_Compile_Time (Typ)
6791 or else Component_Not_OK_For_Backend
6792 or else not Static_Components
6794 Convert_To_Assignments (N, Typ);
6797 Set_Compile_Time_Known_Aggregate (N);
6798 Set_Expansion_Delayed (N, False);
6801 -- Gigi doesn't properly handle temporaries of variable size so we
6802 -- generate it in the front-end
6804 elsif not Size_Known_At_Compile_Time (Typ)
6805 and then Tagged_Type_Expansion
6807 Convert_To_Assignments (N, Typ);
6809 -- An aggregate used to initialize a controlled object must be turned
6810 -- into component assignments as the components themselves may require
6811 -- finalization actions such as adjustment.
6813 elsif Needs_Finalization (Typ) then
6814 Convert_To_Assignments (N, Typ);
6816 -- Ada 2005 (AI-287): In case of default initialized components we
6817 -- convert the aggregate into assignments.
6819 elsif Has_Default_Init_Comps (N) then
6820 Convert_To_Assignments (N, Typ);
6824 elsif Component_Not_OK_For_Backend then
6825 Convert_To_Assignments (N, Typ);
6827 -- If an ancestor is private, some components are not inherited and we
6828 -- cannot expand into a record aggregate.
6830 elsif Has_Visible_Private_Ancestor (Typ) then
6831 Convert_To_Assignments (N, Typ);
6833 -- ??? The following was done to compile fxacc00.ads in the ACVCs. Gigi
6834 -- is not able to handle the aggregate for Late_Request.
6836 elsif Is_Tagged_Type (Typ) and then Has_Discriminants (Typ) then
6837 Convert_To_Assignments (N, Typ);
6839 -- If the tagged types covers interface types we need to initialize all
6840 -- hidden components containing pointers to secondary dispatch tables.
6842 elsif Is_Tagged_Type (Typ) and then Has_Interfaces (Typ) then
6843 Convert_To_Assignments (N, Typ);
6845 -- If some components are mutable, the size of the aggregate component
6846 -- may be distinct from the default size of the type component, so
6847 -- we need to expand to insure that the back-end copies the proper
6848 -- size of the data. However, if the aggregate is the initial value of
6849 -- a constant, the target is immutable and might be built statically
6850 -- if components are appropriate.
6852 elsif Has_Mutable_Components (Typ)
6854 (Nkind (Parent (Top_Level_Aggr)) /= N_Object_Declaration
6855 or else not Constant_Present (Parent (Top_Level_Aggr))
6856 or else not Static_Components)
6858 Convert_To_Assignments (N, Typ);
6860 -- If the type involved has bit aligned components, then we are not sure
6861 -- that the back end can handle this case correctly.
6863 elsif Type_May_Have_Bit_Aligned_Components (Typ) then
6864 Convert_To_Assignments (N, Typ);
6866 -- When generating C, only generate an aggregate when declaring objects
6867 -- since C does not support aggregates in e.g. assignment statements.
6869 elsif Modify_Tree_For_C and then not In_Object_Declaration (N) then
6870 Convert_To_Assignments (N, Typ);
6872 -- In all other cases, build a proper aggregate to be handled by gigi
6875 if Nkind (N) = N_Aggregate then
6877 -- If the aggregate is static and can be handled by the back-end,
6878 -- nothing left to do.
6880 if Static_Components then
6881 Set_Compile_Time_Known_Aggregate (N);
6882 Set_Expansion_Delayed (N, False);
6886 -- If no discriminants, nothing special to do
6888 if not Has_Discriminants (Typ) then
6891 -- Case of discriminants present
6893 elsif Is_Derived_Type (Typ) then
6895 -- For untagged types, non-stored discriminants are replaced
6896 -- with stored discriminants, which are the ones that gigi uses
6897 -- to describe the type and its components.
6899 Generate_Aggregate_For_Derived_Type : declare
6900 Constraints : constant List_Id := New_List;
6901 First_Comp : Node_Id;
6902 Discriminant : Entity_Id;
6904 Num_Disc : Nat := 0;
6905 Num_Gird : Nat := 0;
6907 procedure Prepend_Stored_Values (T : Entity_Id);
6908 -- Scan the list of stored discriminants of the type, and add
6909 -- their values to the aggregate being built.
6911 ---------------------------
6912 -- Prepend_Stored_Values --
6913 ---------------------------
6915 procedure Prepend_Stored_Values (T : Entity_Id) is
6917 Discriminant := First_Stored_Discriminant (T);
6918 while Present (Discriminant) loop
6920 Make_Component_Association (Loc,
6922 New_List (New_Occurrence_Of (Discriminant, Loc)),
6926 (Get_Discriminant_Value
6929 Discriminant_Constraint (Typ))));
6931 if No (First_Comp) then
6932 Prepend_To (Component_Associations (N), New_Comp);
6934 Insert_After (First_Comp, New_Comp);
6937 First_Comp := New_Comp;
6938 Next_Stored_Discriminant (Discriminant);
6940 end Prepend_Stored_Values;
6942 -- Start of processing for Generate_Aggregate_For_Derived_Type
6945 -- Remove the associations for the discriminant of derived type
6947 First_Comp := First (Component_Associations (N));
6948 while Present (First_Comp) loop
6952 if Ekind (Entity (First (Choices (Comp)))) = E_Discriminant
6955 Num_Disc := Num_Disc + 1;
6959 -- Insert stored discriminant associations in the correct
6960 -- order. If there are more stored discriminants than new
6961 -- discriminants, there is at least one new discriminant that
6962 -- constrains more than one of the stored discriminants. In
6963 -- this case we need to construct a proper subtype of the
6964 -- parent type, in order to supply values to all the
6965 -- components. Otherwise there is one-one correspondence
6966 -- between the constraints and the stored discriminants.
6968 First_Comp := Empty;
6970 Discriminant := First_Stored_Discriminant (Base_Type (Typ));
6971 while Present (Discriminant) loop
6972 Num_Gird := Num_Gird + 1;
6973 Next_Stored_Discriminant (Discriminant);
6976 -- Case of more stored discriminants than new discriminants
6978 if Num_Gird > Num_Disc then
6980 -- Create a proper subtype of the parent type, which is the
6981 -- proper implementation type for the aggregate, and convert
6982 -- it to the intended target type.
6984 Discriminant := First_Stored_Discriminant (Base_Type (Typ));
6985 while Present (Discriminant) loop
6988 (Get_Discriminant_Value
6991 Discriminant_Constraint (Typ)));
6992 Append (New_Comp, Constraints);
6993 Next_Stored_Discriminant (Discriminant);
6997 Make_Subtype_Declaration (Loc,
6998 Defining_Identifier => Make_Temporary (Loc, 'T'),
6999 Subtype_Indication =>
7000 Make_Subtype_Indication (Loc,
7002 New_Occurrence_Of (Etype (Base_Type (Typ)), Loc),
7004 Make_Index_Or_Discriminant_Constraint
7005 (Loc, Constraints)));
7007 Insert_Action (N, Decl);
7008 Prepend_Stored_Values (Base_Type (Typ));
7010 Set_Etype (N, Defining_Identifier (Decl));
7013 Rewrite (N, Unchecked_Convert_To (Typ, N));
7016 -- Case where we do not have fewer new discriminants than
7017 -- stored discriminants, so in this case we can simply use the
7018 -- stored discriminants of the subtype.
7021 Prepend_Stored_Values (Typ);
7023 end Generate_Aggregate_For_Derived_Type;
7026 if Is_Tagged_Type (Typ) then
7028 -- In the tagged case, _parent and _tag component must be created
7030 -- Reset Null_Present unconditionally. Tagged records always have
7031 -- at least one field (the tag or the parent).
7033 Set_Null_Record_Present (N, False);
7035 -- When the current aggregate comes from the expansion of an
7036 -- extension aggregate, the parent expr is replaced by an
7037 -- aggregate formed by selected components of this expr.
7039 if Present (Parent_Expr) and then Is_Empty_List (Comps) then
7040 Comp := First_Component_Or_Discriminant (Typ);
7041 while Present (Comp) loop
7043 -- Skip all expander-generated components
7045 if not Comes_From_Source (Original_Record_Component (Comp))
7051 Make_Selected_Component (Loc,
7053 Unchecked_Convert_To (Typ,
7054 Duplicate_Subexpr (Parent_Expr, True)),
7055 Selector_Name => New_Occurrence_Of (Comp, Loc));
7058 Make_Component_Association (Loc,
7060 New_List (New_Occurrence_Of (Comp, Loc)),
7061 Expression => New_Comp));
7063 Analyze_And_Resolve (New_Comp, Etype (Comp));
7066 Next_Component_Or_Discriminant (Comp);
7070 -- Compute the value for the Tag now, if the type is a root it
7071 -- will be included in the aggregate right away, otherwise it will
7072 -- be propagated to the parent aggregate.
7074 if Present (Orig_Tag) then
7075 Tag_Value := Orig_Tag;
7076 elsif not Tagged_Type_Expansion then
7081 (Node (First_Elmt (Access_Disp_Table (Typ))), Loc);
7084 -- For a derived type, an aggregate for the parent is formed with
7085 -- all the inherited components.
7087 if Is_Derived_Type (Typ) then
7090 First_Comp : Node_Id;
7091 Parent_Comps : List_Id;
7092 Parent_Aggr : Node_Id;
7093 Parent_Name : Node_Id;
7096 -- Remove the inherited component association from the
7097 -- aggregate and store them in the parent aggregate
7099 First_Comp := First (Component_Associations (N));
7100 Parent_Comps := New_List;
7101 while Present (First_Comp)
7103 Scope (Original_Record_Component
7104 (Entity (First (Choices (First_Comp))))) /=
7110 Append (Comp, Parent_Comps);
7114 Make_Aggregate (Loc,
7115 Component_Associations => Parent_Comps);
7116 Set_Etype (Parent_Aggr, Etype (Base_Type (Typ)));
7118 -- Find the _parent component
7120 Comp := First_Component (Typ);
7121 while Chars (Comp) /= Name_uParent loop
7122 Comp := Next_Component (Comp);
7125 Parent_Name := New_Occurrence_Of (Comp, Loc);
7127 -- Insert the parent aggregate
7129 Prepend_To (Component_Associations (N),
7130 Make_Component_Association (Loc,
7131 Choices => New_List (Parent_Name),
7132 Expression => Parent_Aggr));
7134 -- Expand recursively the parent propagating the right Tag
7136 Expand_Record_Aggregate
7137 (Parent_Aggr, Tag_Value, Parent_Expr);
7139 -- The ancestor part may be a nested aggregate that has
7140 -- delayed expansion: recheck now.
7142 if Component_Not_OK_For_Backend then
7143 Convert_To_Assignments (N, Typ);
7147 -- For a root type, the tag component is added (unless compiling
7148 -- for the VMs, where tags are implicit).
7150 elsif Tagged_Type_Expansion then
7152 Tag_Name : constant Node_Id :=
7153 New_Occurrence_Of (First_Tag_Component (Typ), Loc);
7154 Typ_Tag : constant Entity_Id := RTE (RE_Tag);
7155 Conv_Node : constant Node_Id :=
7156 Unchecked_Convert_To (Typ_Tag, Tag_Value);
7159 Set_Etype (Conv_Node, Typ_Tag);
7160 Prepend_To (Component_Associations (N),
7161 Make_Component_Association (Loc,
7162 Choices => New_List (Tag_Name),
7163 Expression => Conv_Node));
7169 end Expand_Record_Aggregate;
7171 ----------------------------
7172 -- Has_Default_Init_Comps --
7173 ----------------------------
7175 function Has_Default_Init_Comps (N : Node_Id) return Boolean is
7176 Comps : constant List_Id := Component_Associations (N);
7181 pragma Assert (Nkind_In (N, N_Aggregate, N_Extension_Aggregate));
7187 if Has_Self_Reference (N) then
7191 -- Check if any direct component has default initialized components
7194 while Present (C) loop
7195 if Box_Present (C) then
7202 -- Recursive call in case of aggregate expression
7205 while Present (C) loop
7206 Expr := Expression (C);
7209 and then Nkind_In (Expr, N_Aggregate, N_Extension_Aggregate)
7210 and then Has_Default_Init_Comps (Expr)
7219 end Has_Default_Init_Comps;
7221 --------------------------
7222 -- Is_Delayed_Aggregate --
7223 --------------------------
7225 function Is_Delayed_Aggregate (N : Node_Id) return Boolean is
7226 Node : Node_Id := N;
7227 Kind : Node_Kind := Nkind (Node);
7230 if Kind = N_Qualified_Expression then
7231 Node := Expression (Node);
7232 Kind := Nkind (Node);
7235 if not Nkind_In (Kind, N_Aggregate, N_Extension_Aggregate) then
7238 return Expansion_Delayed (Node);
7240 end Is_Delayed_Aggregate;
7242 ---------------------------
7243 -- In_Object_Declaration --
7244 ---------------------------
7246 function In_Object_Declaration (N : Node_Id) return Boolean is
7247 P : Node_Id := Parent (N);
7249 while Present (P) loop
7250 if Nkind (P) = N_Object_Declaration then
7258 end In_Object_Declaration;
7260 ----------------------------------------
7261 -- Is_Static_Dispatch_Table_Aggregate --
7262 ----------------------------------------
7264 function Is_Static_Dispatch_Table_Aggregate (N : Node_Id) return Boolean is
7265 Typ : constant Entity_Id := Base_Type (Etype (N));
7268 return Static_Dispatch_Tables
7269 and then Tagged_Type_Expansion
7270 and then RTU_Loaded (Ada_Tags)
7272 -- Avoid circularity when rebuilding the compiler
7274 and then Cunit_Entity (Get_Source_Unit (N)) /= RTU_Entity (Ada_Tags)
7275 and then (Typ = RTE (RE_Dispatch_Table_Wrapper)
7277 Typ = RTE (RE_Address_Array)
7279 Typ = RTE (RE_Type_Specific_Data)
7281 Typ = RTE (RE_Tag_Table)
7283 (RTE_Available (RE_Interface_Data)
7284 and then Typ = RTE (RE_Interface_Data))
7286 (RTE_Available (RE_Interfaces_Array)
7287 and then Typ = RTE (RE_Interfaces_Array))
7289 (RTE_Available (RE_Interface_Data_Element)
7290 and then Typ = RTE (RE_Interface_Data_Element)));
7291 end Is_Static_Dispatch_Table_Aggregate;
7293 -----------------------------
7294 -- Is_Two_Dim_Packed_Array --
7295 -----------------------------
7297 function Is_Two_Dim_Packed_Array (Typ : Entity_Id) return Boolean is
7298 C : constant Int := UI_To_Int (Component_Size (Typ));
7300 return Number_Dimensions (Typ) = 2
7301 and then Is_Bit_Packed_Array (Typ)
7302 and then (C = 1 or else C = 2 or else C = 4);
7303 end Is_Two_Dim_Packed_Array;
7305 --------------------
7306 -- Late_Expansion --
7307 --------------------
7309 function Late_Expansion
7312 Target : Node_Id) return List_Id
7314 Aggr_Code : List_Id;
7317 if Is_Array_Type (Etype (N)) then
7319 Build_Array_Aggr_Code
7321 Ctype => Component_Type (Etype (N)),
7322 Index => First_Index (Typ),
7324 Scalar_Comp => Is_Scalar_Type (Component_Type (Typ)),
7325 Indexes => No_List);
7327 -- Directly or indirectly (e.g. access protected procedure) a record
7330 Aggr_Code := Build_Record_Aggr_Code (N, Typ, Target);
7333 -- Save the last assignment statement associated with the aggregate
7334 -- when building a controlled object. This reference is utilized by
7335 -- the finalization machinery when marking an object as successfully
7338 if Needs_Finalization (Typ)
7339 and then Is_Entity_Name (Target)
7340 and then Present (Entity (Target))
7341 and then Ekind_In (Entity (Target), E_Constant, E_Variable)
7343 Set_Last_Aggregate_Assignment (Entity (Target), Last (Aggr_Code));
7349 ----------------------------------
7350 -- Make_OK_Assignment_Statement --
7351 ----------------------------------
7353 function Make_OK_Assignment_Statement
7356 Expression : Node_Id) return Node_Id
7359 Set_Assignment_OK (Name);
7360 return Make_Assignment_Statement (Sloc, Name, Expression);
7361 end Make_OK_Assignment_Statement;
7363 -----------------------
7364 -- Number_Of_Choices --
7365 -----------------------
7367 function Number_Of_Choices (N : Node_Id) return Nat is
7371 Nb_Choices : Nat := 0;
7374 if Present (Expressions (N)) then
7378 Assoc := First (Component_Associations (N));
7379 while Present (Assoc) loop
7380 Choice := First (Choice_List (Assoc));
7381 while Present (Choice) loop
7382 if Nkind (Choice) /= N_Others_Choice then
7383 Nb_Choices := Nb_Choices + 1;
7393 end Number_Of_Choices;
7395 ------------------------------------
7396 -- Packed_Array_Aggregate_Handled --
7397 ------------------------------------
7399 -- The current version of this procedure will handle at compile time
7400 -- any array aggregate that meets these conditions:
7402 -- One and two dimensional, bit packed
7403 -- Underlying packed type is modular type
7404 -- Bounds are within 32-bit Int range
7405 -- All bounds and values are static
7407 -- Note: for now, in the 2-D case, we only handle component sizes of
7408 -- 1, 2, 4 (cases where an integral number of elements occupies a byte).
7410 function Packed_Array_Aggregate_Handled (N : Node_Id) return Boolean is
7411 Loc : constant Source_Ptr := Sloc (N);
7412 Typ : constant Entity_Id := Etype (N);
7413 Ctyp : constant Entity_Id := Component_Type (Typ);
7415 Not_Handled : exception;
7416 -- Exception raised if this aggregate cannot be handled
7419 -- Handle one- or two dimensional bit packed array
7421 if not Is_Bit_Packed_Array (Typ)
7422 or else Number_Dimensions (Typ) > 2
7427 -- If two-dimensional, check whether it can be folded, and transformed
7428 -- into a one-dimensional aggregate for the Packed_Array_Impl_Type of
7429 -- the original type.
7431 if Number_Dimensions (Typ) = 2 then
7432 return Two_Dim_Packed_Array_Handled (N);
7435 if not Is_Modular_Integer_Type (Packed_Array_Impl_Type (Typ)) then
7439 if not Is_Scalar_Type (Component_Type (Typ))
7440 and then Has_Non_Standard_Rep (Component_Type (Typ))
7446 Csiz : constant Nat := UI_To_Int (Component_Size (Typ));
7450 -- Bounds of index type
7454 -- Values of bounds if compile time known
7456 function Get_Component_Val (N : Node_Id) return Uint;
7457 -- Given a expression value N of the component type Ctyp, returns a
7458 -- value of Csiz (component size) bits representing this value. If
7459 -- the value is non-static or any other reason exists why the value
7460 -- cannot be returned, then Not_Handled is raised.
7462 -----------------------
7463 -- Get_Component_Val --
7464 -----------------------
7466 function Get_Component_Val (N : Node_Id) return Uint is
7470 -- We have to analyze the expression here before doing any further
7471 -- processing here. The analysis of such expressions is deferred
7472 -- till expansion to prevent some problems of premature analysis.
7474 Analyze_And_Resolve (N, Ctyp);
7476 -- Must have a compile time value. String literals have to be
7477 -- converted into temporaries as well, because they cannot easily
7478 -- be converted into their bit representation.
7480 if not Compile_Time_Known_Value (N)
7481 or else Nkind (N) = N_String_Literal
7486 Val := Expr_Rep_Value (N);
7488 -- Adjust for bias, and strip proper number of bits
7490 if Has_Biased_Representation (Ctyp) then
7491 Val := Val - Expr_Value (Type_Low_Bound (Ctyp));
7494 return Val mod Uint_2 ** Csiz;
7495 end Get_Component_Val;
7497 -- Here we know we have a one dimensional bit packed array
7500 Get_Index_Bounds (First_Index (Typ), Lo, Hi);
7502 -- Cannot do anything if bounds are dynamic
7504 if not Compile_Time_Known_Value (Lo)
7506 not Compile_Time_Known_Value (Hi)
7511 -- Or are silly out of range of int bounds
7513 Lob := Expr_Value (Lo);
7514 Hib := Expr_Value (Hi);
7516 if not UI_Is_In_Int_Range (Lob)
7518 not UI_Is_In_Int_Range (Hib)
7523 -- At this stage we have a suitable aggregate for handling at compile
7524 -- time. The only remaining checks are that the values of expressions
7525 -- in the aggregate are compile-time known (checks are performed by
7526 -- Get_Component_Val), and that any subtypes or ranges are statically
7529 -- If the aggregate is not fully positional at this stage, then
7530 -- convert it to positional form. Either this will fail, in which
7531 -- case we can do nothing, or it will succeed, in which case we have
7532 -- succeeded in handling the aggregate and transforming it into a
7533 -- modular value, or it will stay an aggregate, in which case we
7534 -- have failed to create a packed value for it.
7536 if Present (Component_Associations (N)) then
7537 Convert_To_Positional
7538 (N, Max_Others_Replicate => 64, Handle_Bit_Packed => True);
7539 return Nkind (N) /= N_Aggregate;
7542 -- Otherwise we are all positional, so convert to proper value
7545 Lov : constant Int := UI_To_Int (Lob);
7546 Hiv : constant Int := UI_To_Int (Hib);
7548 Len : constant Nat := Int'Max (0, Hiv - Lov + 1);
7549 -- The length of the array (number of elements)
7551 Aggregate_Val : Uint;
7552 -- Value of aggregate. The value is set in the low order bits of
7553 -- this value. For the little-endian case, the values are stored
7554 -- from low-order to high-order and for the big-endian case the
7555 -- values are stored from high-order to low-order. Note that gigi
7556 -- will take care of the conversions to left justify the value in
7557 -- the big endian case (because of left justified modular type
7558 -- processing), so we do not have to worry about that here.
7561 -- Integer literal for resulting constructed value
7564 -- Shift count from low order for next value
7567 -- Shift increment for loop
7570 -- Next expression from positional parameters of aggregate
7572 Left_Justified : Boolean;
7573 -- Set True if we are filling the high order bits of the target
7574 -- value (i.e. the value is left justified).
7577 -- For little endian, we fill up the low order bits of the target
7578 -- value. For big endian we fill up the high order bits of the
7579 -- target value (which is a left justified modular value).
7581 Left_Justified := Bytes_Big_Endian;
7583 -- Switch justification if using -gnatd8
7585 if Debug_Flag_8 then
7586 Left_Justified := not Left_Justified;
7589 -- Switch justfification if reverse storage order
7591 if Reverse_Storage_Order (Base_Type (Typ)) then
7592 Left_Justified := not Left_Justified;
7595 if Left_Justified then
7596 Shift := Csiz * (Len - 1);
7603 -- Loop to set the values
7606 Aggregate_Val := Uint_0;
7608 Expr := First (Expressions (N));
7609 Aggregate_Val := Get_Component_Val (Expr) * Uint_2 ** Shift;
7611 for J in 2 .. Len loop
7612 Shift := Shift + Incr;
7615 Aggregate_Val + Get_Component_Val (Expr) * Uint_2 ** Shift;
7619 -- Now we can rewrite with the proper value
7621 Lit := Make_Integer_Literal (Loc, Intval => Aggregate_Val);
7622 Set_Print_In_Hex (Lit);
7624 -- Construct the expression using this literal. Note that it is
7625 -- important to qualify the literal with its proper modular type
7626 -- since universal integer does not have the required range and
7627 -- also this is a left justified modular type, which is important
7628 -- in the big-endian case.
7631 Unchecked_Convert_To (Typ,
7632 Make_Qualified_Expression (Loc,
7634 New_Occurrence_Of (Packed_Array_Impl_Type (Typ), Loc),
7635 Expression => Lit)));
7637 Analyze_And_Resolve (N, Typ);
7645 end Packed_Array_Aggregate_Handled;
7647 ----------------------------
7648 -- Has_Mutable_Components --
7649 ----------------------------
7651 function Has_Mutable_Components (Typ : Entity_Id) return Boolean is
7655 Comp := First_Component (Typ);
7656 while Present (Comp) loop
7657 if Is_Record_Type (Etype (Comp))
7658 and then Has_Discriminants (Etype (Comp))
7659 and then not Is_Constrained (Etype (Comp))
7664 Next_Component (Comp);
7668 end Has_Mutable_Components;
7670 ------------------------------
7671 -- Initialize_Discriminants --
7672 ------------------------------
7674 procedure Initialize_Discriminants (N : Node_Id; Typ : Entity_Id) is
7675 Loc : constant Source_Ptr := Sloc (N);
7676 Bas : constant Entity_Id := Base_Type (Typ);
7677 Par : constant Entity_Id := Etype (Bas);
7678 Decl : constant Node_Id := Parent (Par);
7682 if Is_Tagged_Type (Bas)
7683 and then Is_Derived_Type (Bas)
7684 and then Has_Discriminants (Par)
7685 and then Has_Discriminants (Bas)
7686 and then Number_Discriminants (Bas) /= Number_Discriminants (Par)
7687 and then Nkind (Decl) = N_Full_Type_Declaration
7688 and then Nkind (Type_Definition (Decl)) = N_Record_Definition
7690 Present (Variant_Part (Component_List (Type_Definition (Decl))))
7691 and then Nkind (N) /= N_Extension_Aggregate
7694 -- Call init proc to set discriminants.
7695 -- There should eventually be a special procedure for this ???
7697 Ref := New_Occurrence_Of (Defining_Identifier (N), Loc);
7698 Insert_Actions_After (N,
7699 Build_Initialization_Call (Sloc (N), Ref, Typ));
7701 end Initialize_Discriminants;
7708 (Obj_Type : Entity_Id;
7709 Typ : Entity_Id) return Boolean
7711 L1, L2, H1, H2 : Node_Id;
7714 -- No sliding if the type of the object is not established yet, if it is
7715 -- an unconstrained type whose actual subtype comes from the aggregate,
7716 -- or if the two types are identical.
7718 if not Is_Array_Type (Obj_Type) then
7721 elsif not Is_Constrained (Obj_Type) then
7724 elsif Typ = Obj_Type then
7728 -- Sliding can only occur along the first dimension
7730 Get_Index_Bounds (First_Index (Typ), L1, H1);
7731 Get_Index_Bounds (First_Index (Obj_Type), L2, H2);
7733 if not Is_OK_Static_Expression (L1) or else
7734 not Is_OK_Static_Expression (L2) or else
7735 not Is_OK_Static_Expression (H1) or else
7736 not Is_OK_Static_Expression (H2)
7740 return Expr_Value (L1) /= Expr_Value (L2)
7742 Expr_Value (H1) /= Expr_Value (H2);
7747 ---------------------------------
7748 -- Process_Transient_Component --
7749 ---------------------------------
7751 procedure Process_Transient_Component
7753 Comp_Typ : Entity_Id;
7754 Init_Expr : Node_Id;
7755 Fin_Call : out Node_Id;
7756 Hook_Clear : out Node_Id;
7757 Aggr : Node_Id := Empty;
7758 Stmts : List_Id := No_List)
7760 procedure Add_Item (Item : Node_Id);
7761 -- Insert arbitrary node Item into the tree depending on the values of
7768 procedure Add_Item (Item : Node_Id) is
7770 if Present (Aggr) then
7771 Insert_Action (Aggr, Item);
7773 pragma Assert (Present (Stmts));
7774 Append_To (Stmts, Item);
7780 Hook_Assign : Node_Id;
7781 Hook_Decl : Node_Id;
7785 Res_Typ : Entity_Id;
7787 -- Start of processing for Process_Transient_Component
7790 -- Add the access type, which provides a reference to the function
7791 -- result. Generate:
7793 -- type Res_Typ is access all Comp_Typ;
7795 Res_Typ := Make_Temporary (Loc, 'A');
7796 Set_Ekind (Res_Typ, E_General_Access_Type);
7797 Set_Directly_Designated_Type (Res_Typ, Comp_Typ);
7800 (Make_Full_Type_Declaration (Loc,
7801 Defining_Identifier => Res_Typ,
7803 Make_Access_To_Object_Definition (Loc,
7804 All_Present => True,
7805 Subtype_Indication => New_Occurrence_Of (Comp_Typ, Loc))));
7807 -- Add the temporary which captures the result of the function call.
7810 -- Res : constant Res_Typ := Init_Expr'Reference;
7812 -- Note that this temporary is effectively a transient object because
7813 -- its lifetime is bounded by the current array or record component.
7815 Res_Id := Make_Temporary (Loc, 'R');
7816 Set_Ekind (Res_Id, E_Constant);
7817 Set_Etype (Res_Id, Res_Typ);
7819 -- Mark the transient object as successfully processed to avoid double
7822 Set_Is_Finalized_Transient (Res_Id);
7824 -- Signal the general finalization machinery that this transient object
7825 -- should not be considered for finalization actions because its cleanup
7826 -- will be performed by Process_Transient_Component_Completion.
7828 Set_Is_Ignored_Transient (Res_Id);
7831 Make_Object_Declaration (Loc,
7832 Defining_Identifier => Res_Id,
7833 Constant_Present => True,
7834 Object_Definition => New_Occurrence_Of (Res_Typ, Loc),
7836 Make_Reference (Loc, New_Copy_Tree (Init_Expr)));
7838 Add_Item (Res_Decl);
7840 -- Construct all pieces necessary to hook and finalize the transient
7843 Build_Transient_Object_Statements
7844 (Obj_Decl => Res_Decl,
7845 Fin_Call => Fin_Call,
7846 Hook_Assign => Hook_Assign,
7847 Hook_Clear => Hook_Clear,
7848 Hook_Decl => Hook_Decl,
7849 Ptr_Decl => Ptr_Decl);
7851 -- Add the access type which provides a reference to the transient
7852 -- result. Generate:
7854 -- type Ptr_Typ is access all Comp_Typ;
7856 Add_Item (Ptr_Decl);
7858 -- Add the temporary which acts as a hook to the transient result.
7861 -- Hook : Ptr_Typ := null;
7863 Add_Item (Hook_Decl);
7865 -- Attach the transient result to the hook. Generate:
7867 -- Hook := Ptr_Typ (Res);
7869 Add_Item (Hook_Assign);
7871 -- The original initialization expression now references the value of
7872 -- the temporary function result. Generate:
7877 Make_Explicit_Dereference (Loc,
7878 Prefix => New_Occurrence_Of (Res_Id, Loc)));
7879 end Process_Transient_Component;
7881 --------------------------------------------
7882 -- Process_Transient_Component_Completion --
7883 --------------------------------------------
7885 procedure Process_Transient_Component_Completion
7889 Hook_Clear : Node_Id;
7892 Exceptions_OK : constant Boolean :=
7893 not Restriction_Active (No_Exception_Propagation);
7896 pragma Assert (Present (Hook_Clear));
7898 -- Generate the following code if exception propagation is allowed:
7901 -- Abort : constant Boolean := Triggered_By_Abort;
7903 -- Abort : constant Boolean := False; -- no abort
7905 -- E : Exception_Occurrence;
7906 -- Raised : Boolean := False;
7913 -- [Deep_]Finalize (Res.all);
7917 -- if not Raised then
7919 -- Save_Occurrence (E,
7920 -- Get_Curent_Excep.all.all);
7926 -- if Raised and then not Abort then
7927 -- Raise_From_Controlled_Operation (E);
7931 if Exceptions_OK then
7932 Abort_And_Exception : declare
7933 Blk_Decls : constant List_Id := New_List;
7934 Blk_Stmts : constant List_Id := New_List;
7935 Fin_Stmts : constant List_Id := New_List;
7937 Fin_Data : Finalization_Exception_Data;
7940 -- Create the declarations of the two flags and the exception
7943 Build_Object_Declarations (Fin_Data, Blk_Decls, Loc);
7948 if Abort_Allowed then
7949 Append_To (Blk_Stmts,
7950 Build_Runtime_Call (Loc, RE_Abort_Defer));
7953 -- Wrap the hook clear and the finalization call in order to trap
7954 -- a potential exception.
7956 Append_To (Fin_Stmts, Hook_Clear);
7958 if Present (Fin_Call) then
7959 Append_To (Fin_Stmts, Fin_Call);
7962 Append_To (Blk_Stmts,
7963 Make_Block_Statement (Loc,
7964 Handled_Statement_Sequence =>
7965 Make_Handled_Sequence_Of_Statements (Loc,
7966 Statements => Fin_Stmts,
7967 Exception_Handlers => New_List (
7968 Build_Exception_Handler (Fin_Data)))));
7973 if Abort_Allowed then
7974 Append_To (Blk_Stmts,
7975 Build_Runtime_Call (Loc, RE_Abort_Undefer));
7978 -- Reraise the potential exception with a proper "upgrade" to
7979 -- Program_Error if needed.
7981 Append_To (Blk_Stmts, Build_Raise_Statement (Fin_Data));
7983 -- Wrap everything in a block
7986 Make_Block_Statement (Loc,
7987 Declarations => Blk_Decls,
7988 Handled_Statement_Sequence =>
7989 Make_Handled_Sequence_Of_Statements (Loc,
7990 Statements => Blk_Stmts)));
7991 end Abort_And_Exception;
7993 -- Generate the following code if exception propagation is not allowed
7994 -- and aborts are allowed:
7999 -- [Deep_]Finalize (Res.all);
8001 -- Abort_Undefer_Direct;
8004 elsif Abort_Allowed then
8005 Abort_Only : declare
8006 Blk_Stmts : constant List_Id := New_List;
8009 Append_To (Blk_Stmts, Build_Runtime_Call (Loc, RE_Abort_Defer));
8010 Append_To (Blk_Stmts, Hook_Clear);
8012 if Present (Fin_Call) then
8013 Append_To (Blk_Stmts, Fin_Call);
8017 Build_Abort_Undefer_Block (Loc,
8022 -- Otherwise generate:
8025 -- [Deep_]Finalize (Res.all);
8028 Append_To (Stmts, Hook_Clear);
8030 if Present (Fin_Call) then
8031 Append_To (Stmts, Fin_Call);
8034 end Process_Transient_Component_Completion;
8036 ---------------------
8037 -- Sort_Case_Table --
8038 ---------------------
8040 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type) is
8041 L : constant Int := Case_Table'First;
8042 U : constant Int := Case_Table'Last;
8050 T := Case_Table (K + 1);
8054 and then Expr_Value (Case_Table (J - 1).Choice_Lo) >
8055 Expr_Value (T.Choice_Lo)
8057 Case_Table (J) := Case_Table (J - 1);
8061 Case_Table (J) := T;
8064 end Sort_Case_Table;
8066 ----------------------------
8067 -- Static_Array_Aggregate --
8068 ----------------------------
8070 function Static_Array_Aggregate (N : Node_Id) return Boolean is
8071 Bounds : constant Node_Id := Aggregate_Bounds (N);
8073 Typ : constant Entity_Id := Etype (N);
8074 Comp_Type : constant Entity_Id := Component_Type (Typ);
8081 if Is_Tagged_Type (Typ)
8082 or else Is_Controlled (Typ)
8083 or else Is_Packed (Typ)
8089 and then Nkind (Bounds) = N_Range
8090 and then Nkind (Low_Bound (Bounds)) = N_Integer_Literal
8091 and then Nkind (High_Bound (Bounds)) = N_Integer_Literal
8093 Lo := Low_Bound (Bounds);
8094 Hi := High_Bound (Bounds);
8096 if No (Component_Associations (N)) then
8098 -- Verify that all components are static integers
8100 Expr := First (Expressions (N));
8101 while Present (Expr) loop
8102 if Nkind (Expr) /= N_Integer_Literal then
8112 -- We allow only a single named association, either a static
8113 -- range or an others_clause, with a static expression.
8115 Expr := First (Component_Associations (N));
8117 if Present (Expressions (N)) then
8120 elsif Present (Next (Expr)) then
8123 elsif Present (Next (First (Choice_List (Expr)))) then
8127 -- The aggregate is static if all components are literals,
8128 -- or else all its components are static aggregates for the
8129 -- component type. We also limit the size of a static aggregate
8130 -- to prevent runaway static expressions.
8132 if Is_Array_Type (Comp_Type)
8133 or else Is_Record_Type (Comp_Type)
8135 if Nkind (Expression (Expr)) /= N_Aggregate
8137 not Compile_Time_Known_Aggregate (Expression (Expr))
8142 elsif Nkind (Expression (Expr)) /= N_Integer_Literal then
8146 if not Aggr_Size_OK (N, Typ) then
8150 -- Create a positional aggregate with the right number of
8151 -- copies of the expression.
8153 Agg := Make_Aggregate (Sloc (N), New_List, No_List);
8155 for I in UI_To_Int (Intval (Lo)) .. UI_To_Int (Intval (Hi))
8157 Append_To (Expressions (Agg), New_Copy (Expression (Expr)));
8159 -- The copied expression must be analyzed and resolved.
8160 -- Besides setting the type, this ensures that static
8161 -- expressions are appropriately marked as such.
8164 (Last (Expressions (Agg)), Component_Type (Typ));
8167 Set_Aggregate_Bounds (Agg, Bounds);
8168 Set_Etype (Agg, Typ);
8171 Set_Compile_Time_Known_Aggregate (N);
8180 end Static_Array_Aggregate;
8182 ----------------------------------
8183 -- Two_Dim_Packed_Array_Handled --
8184 ----------------------------------
8186 function Two_Dim_Packed_Array_Handled (N : Node_Id) return Boolean is
8187 Loc : constant Source_Ptr := Sloc (N);
8188 Typ : constant Entity_Id := Etype (N);
8189 Ctyp : constant Entity_Id := Component_Type (Typ);
8190 Comp_Size : constant Int := UI_To_Int (Component_Size (Typ));
8191 Packed_Array : constant Entity_Id :=
8192 Packed_Array_Impl_Type (Base_Type (Typ));
8195 -- Expression in original aggregate
8198 -- One-dimensional subaggregate
8202 -- For now, only deal with cases where an integral number of elements
8203 -- fit in a single byte. This includes the most common boolean case.
8205 if not (Comp_Size = 1 or else
8206 Comp_Size = 2 or else
8212 Convert_To_Positional
8213 (N, Max_Others_Replicate => 64, Handle_Bit_Packed => True);
8215 -- Verify that all components are static
8217 if Nkind (N) = N_Aggregate
8218 and then Compile_Time_Known_Aggregate (N)
8222 -- The aggregate may have been reanalyzed and converted already
8224 elsif Nkind (N) /= N_Aggregate then
8227 -- If component associations remain, the aggregate is not static
8229 elsif Present (Component_Associations (N)) then
8233 One_Dim := First (Expressions (N));
8234 while Present (One_Dim) loop
8235 if Present (Component_Associations (One_Dim)) then
8239 One_Comp := First (Expressions (One_Dim));
8240 while Present (One_Comp) loop
8241 if not Is_OK_Static_Expression (One_Comp) then
8252 -- Two-dimensional aggregate is now fully positional so pack one
8253 -- dimension to create a static one-dimensional array, and rewrite
8254 -- as an unchecked conversion to the original type.
8257 Byte_Size : constant Int := UI_To_Int (Component_Size (Packed_Array));
8258 -- The packed array type is a byte array
8261 -- Number of components accumulated in current byte
8264 -- Assembled list of packed values for equivalent aggregate
8267 -- Integer value of component
8270 -- Step size for packing
8273 -- Endian-dependent start position for packing
8276 -- Current insertion position
8279 -- Component of packed array being assembled
8286 -- Account for endianness. See corresponding comment in
8287 -- Packed_Array_Aggregate_Handled concerning the following.
8291 xor Reverse_Storage_Order (Base_Type (Typ))
8293 Init_Shift := Byte_Size - Comp_Size;
8300 -- Iterate over each subaggregate
8302 Shift := Init_Shift;
8303 One_Dim := First (Expressions (N));
8304 while Present (One_Dim) loop
8305 One_Comp := First (Expressions (One_Dim));
8306 while Present (One_Comp) loop
8307 if Packed_Num = Byte_Size / Comp_Size then
8309 -- Byte is complete, add to list of expressions
8311 Append (Make_Integer_Literal (Sloc (One_Dim), Val), Comps);
8313 Shift := Init_Shift;
8317 Comp_Val := Expr_Rep_Value (One_Comp);
8319 -- Adjust for bias, and strip proper number of bits
8321 if Has_Biased_Representation (Ctyp) then
8322 Comp_Val := Comp_Val - Expr_Value (Type_Low_Bound (Ctyp));
8325 Comp_Val := Comp_Val mod Uint_2 ** Comp_Size;
8326 Val := UI_To_Int (Val + Comp_Val * Uint_2 ** Shift);
8327 Shift := Shift + Incr;
8328 One_Comp := Next (One_Comp);
8329 Packed_Num := Packed_Num + 1;
8333 One_Dim := Next (One_Dim);
8336 if Packed_Num > 0 then
8338 -- Add final incomplete byte if present
8340 Append (Make_Integer_Literal (Sloc (One_Dim), Val), Comps);
8344 Unchecked_Convert_To (Typ,
8345 Make_Qualified_Expression (Loc,
8346 Subtype_Mark => New_Occurrence_Of (Packed_Array, Loc),
8347 Expression => Make_Aggregate (Loc, Expressions => Comps))));
8348 Analyze_And_Resolve (N);
8351 end Two_Dim_Packed_Array_Handled;