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 procedure Expand_Delta_Array_Aggregate (N : Node_Id; Deltas : List_Id);
88 procedure Expand_Delta_Record_Aggregate (N : Node_Id; Deltas : List_Id);
90 function Has_Default_Init_Comps (N : Node_Id) return Boolean;
91 -- N is an aggregate (record or array). Checks the presence of default
92 -- initialization (<>) in any component (Ada 2005: AI-287).
94 function In_Object_Declaration (N : Node_Id) return Boolean;
95 -- Return True if N is part of an object declaration, False otherwise
97 function Is_Static_Dispatch_Table_Aggregate (N : Node_Id) return Boolean;
98 -- Returns true if N is an aggregate used to initialize the components
99 -- of a statically allocated dispatch table.
101 function Late_Expansion
104 Target : Node_Id) return List_Id;
105 -- This routine implements top-down expansion of nested aggregates. In
106 -- doing so, it avoids the generation of temporaries at each level. N is
107 -- a nested record or array aggregate with the Expansion_Delayed flag.
108 -- Typ is the expected type of the aggregate. Target is a (duplicatable)
109 -- expression that will hold the result of the aggregate expansion.
111 function Make_OK_Assignment_Statement
114 Expression : Node_Id) return Node_Id;
115 -- This is like Make_Assignment_Statement, except that Assignment_OK
116 -- is set in the left operand. All assignments built by this unit use
117 -- this routine. This is needed to deal with assignments to initialized
118 -- constants that are done in place.
121 (Obj_Type : Entity_Id;
122 Typ : Entity_Id) return Boolean;
123 -- A static array aggregate in an object declaration can in most cases be
124 -- expanded in place. The one exception is when the aggregate is given
125 -- with component associations that specify different bounds from those of
126 -- the type definition in the object declaration. In this pathological
127 -- case the aggregate must slide, and we must introduce an intermediate
128 -- temporary to hold it.
130 -- The same holds in an assignment to one-dimensional array of arrays,
131 -- when a component may be given with bounds that differ from those of the
134 function Number_Of_Choices (N : Node_Id) return Nat;
135 -- Returns the number of discrete choices (not including the others choice
136 -- if present) contained in (sub-)aggregate N.
138 procedure Process_Transient_Component
140 Comp_Typ : Entity_Id;
142 Fin_Call : out Node_Id;
143 Hook_Clear : out Node_Id;
144 Aggr : Node_Id := Empty;
145 Stmts : List_Id := No_List);
146 -- Subsidiary to the expansion of array and record aggregates. Generate
147 -- part of the necessary code to finalize a transient component. Comp_Typ
148 -- is the component type. Init_Expr is the initialization expression of the
149 -- component which is always a function call. Fin_Call is the finalization
150 -- call used to clean up the transient function result. Hook_Clear is the
151 -- hook reset statement. Aggr and Stmts both control the placement of the
152 -- generated code. Aggr is the related aggregate. If present, all code is
153 -- inserted prior to Aggr using Insert_Action. Stmts is the initialization
154 -- statements of the component. If present, all code is added to Stmts.
156 procedure Process_Transient_Component_Completion
160 Hook_Clear : Node_Id;
162 -- Subsidiary to the expansion of array and record aggregates. Generate
163 -- part of the necessary code to finalize a transient component. Aggr is
164 -- the related aggregate. Fin_Clear is the finalization call used to clean
165 -- up the transient component. Hook_Clear is the hook reset statment. Stmts
166 -- is the initialization statement list for the component. All generated
167 -- code is added to Stmts.
169 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type);
170 -- Sort the Case Table using the Lower Bound of each Choice as the key.
171 -- A simple insertion sort is used since the number of choices in a case
172 -- statement of variant part will usually be small and probably in near
175 ------------------------------------------------------
176 -- Local subprograms for Record Aggregate Expansion --
177 ------------------------------------------------------
179 function Build_Record_Aggr_Code
182 Lhs : Node_Id) return List_Id;
183 -- N is an N_Aggregate or an N_Extension_Aggregate. Typ is the type of the
184 -- aggregate. Target is an expression containing the location on which the
185 -- component by component assignments will take place. Returns the list of
186 -- assignments plus all other adjustments needed for tagged and controlled
189 procedure Convert_To_Assignments (N : Node_Id; Typ : Entity_Id);
190 -- N is an N_Aggregate or an N_Extension_Aggregate. Typ is the type of the
191 -- aggregate (which can only be a record type, this procedure is only used
192 -- for record types). Transform the given aggregate into a sequence of
193 -- assignments performed component by component.
195 procedure Expand_Record_Aggregate
197 Orig_Tag : Node_Id := Empty;
198 Parent_Expr : Node_Id := Empty);
199 -- This is the top level procedure for record aggregate expansion.
200 -- Expansion for record aggregates needs expand aggregates for tagged
201 -- record types. Specifically Expand_Record_Aggregate adds the Tag
202 -- field in front of the Component_Association list that was created
203 -- during resolution by Resolve_Record_Aggregate.
205 -- N is the record aggregate node.
206 -- Orig_Tag is the value of the Tag that has to be provided for this
207 -- specific aggregate. It carries the tag corresponding to the type
208 -- of the outermost aggregate during the recursive expansion
209 -- Parent_Expr is the ancestor part of the original extension
212 function Has_Mutable_Components (Typ : Entity_Id) return Boolean;
213 -- Return true if one of the components is of a discriminated type with
214 -- defaults. An aggregate for a type with mutable components must be
215 -- expanded into individual assignments.
217 procedure Initialize_Discriminants (N : Node_Id; Typ : Entity_Id);
218 -- If the type of the aggregate is a type extension with renamed discrimi-
219 -- nants, we must initialize the hidden discriminants of the parent.
220 -- Otherwise, the target object must not be initialized. The discriminants
221 -- are initialized by calling the initialization procedure for the type.
222 -- This is incorrect if the initialization of other components has any
223 -- side effects. We restrict this call to the case where the parent type
224 -- has a variant part, because this is the only case where the hidden
225 -- discriminants are accessed, namely when calling discriminant checking
226 -- functions of the parent type, and when applying a stream attribute to
227 -- an object of the derived type.
229 -----------------------------------------------------
230 -- Local Subprograms for Array Aggregate Expansion --
231 -----------------------------------------------------
233 function Aggr_Size_OK (N : Node_Id; Typ : Entity_Id) return Boolean;
234 -- Very large static aggregates present problems to the back-end, and are
235 -- transformed into assignments and loops. This function verifies that the
236 -- total number of components of an aggregate is acceptable for rewriting
237 -- into a purely positional static form. Aggr_Size_OK must be called before
240 -- This function also detects and warns about one-component aggregates that
241 -- appear in a non-static context. Even if the component value is static,
242 -- such an aggregate must be expanded into an assignment.
244 function Backend_Processing_Possible (N : Node_Id) return Boolean;
245 -- This function checks if array aggregate N can be processed directly
246 -- by the backend. If this is the case, True is returned.
248 function Build_Array_Aggr_Code
253 Scalar_Comp : Boolean;
254 Indexes : List_Id := No_List) return List_Id;
255 -- This recursive routine returns a list of statements containing the
256 -- loops and assignments that are needed for the expansion of the array
259 -- N is the (sub-)aggregate node to be expanded into code. This node has
260 -- been fully analyzed, and its Etype is properly set.
262 -- Index is the index node corresponding to the array subaggregate N
264 -- Into is the target expression into which we are copying the aggregate.
265 -- Note that this node may not have been analyzed yet, and so the Etype
266 -- field may not be set.
268 -- Scalar_Comp is True if the component type of the aggregate is scalar
270 -- Indexes is the current list of expressions used to index the object we
273 procedure Convert_Array_Aggr_In_Allocator
277 -- If the aggregate appears within an allocator and can be expanded in
278 -- place, this routine generates the individual assignments to components
279 -- of the designated object. This is an optimization over the general
280 -- case, where a temporary is first created on the stack and then used to
281 -- construct the allocated object on the heap.
283 procedure Convert_To_Positional
285 Max_Others_Replicate : Nat := 5;
286 Handle_Bit_Packed : Boolean := False);
287 -- If possible, convert named notation to positional notation. This
288 -- conversion is possible only in some static cases. If the conversion is
289 -- possible, then N is rewritten with the analyzed converted aggregate.
290 -- The parameter Max_Others_Replicate controls the maximum number of
291 -- values corresponding to an others choice that will be converted to
292 -- positional notation (the default of 5 is the normal limit, and reflects
293 -- the fact that normally the loop is better than a lot of separate
294 -- assignments). Note that this limit gets overridden in any case if
295 -- either of the restrictions No_Elaboration_Code or No_Implicit_Loops is
296 -- set. The parameter Handle_Bit_Packed is usually set False (since we do
297 -- not expect the back end to handle bit packed arrays, so the normal case
298 -- of conversion is pointless), but in the special case of a call from
299 -- Packed_Array_Aggregate_Handled, we set this parameter to True, since
300 -- these are cases we handle in there.
302 -- It would seem useful to have a higher default for Max_Others_Replicate,
303 -- but aggregates in the compiler make this impossible: the compiler
304 -- bootstrap fails if Max_Others_Replicate is greater than 25. This
307 procedure Expand_Array_Aggregate (N : Node_Id);
308 -- This is the top-level routine to perform array aggregate expansion.
309 -- N is the N_Aggregate node to be expanded.
311 function Is_Two_Dim_Packed_Array (Typ : Entity_Id) return Boolean;
312 -- For two-dimensional packed aggregates with constant bounds and constant
313 -- components, it is preferable to pack the inner aggregates because the
314 -- whole matrix can then be presented to the back-end as a one-dimensional
315 -- list of literals. This is much more efficient than expanding into single
316 -- component assignments. This function determines if the type Typ is for
317 -- an array that is suitable for this optimization: it returns True if Typ
318 -- is a two dimensional bit packed array with component size 1, 2, or 4.
320 function Packed_Array_Aggregate_Handled (N : Node_Id) return Boolean;
321 -- Given an array aggregate, this function handles the case of a packed
322 -- array aggregate with all constant values, where the aggregate can be
323 -- evaluated at compile time. If this is possible, then N is rewritten
324 -- to be its proper compile time value with all the components properly
325 -- assembled. The expression is analyzed and resolved and True is returned.
326 -- If this transformation is not possible, N is unchanged and False is
329 function Two_Dim_Packed_Array_Handled (N : Node_Id) return Boolean;
330 -- If the type of the aggregate is a two-dimensional bit_packed array
331 -- it may be transformed into an array of bytes with constant values,
332 -- and presented to the back-end as a static value. The function returns
333 -- false if this transformation cannot be performed. THis is similar to,
334 -- and reuses part of the machinery in Packed_Array_Aggregate_Handled.
340 function Aggr_Size_OK (N : Node_Id; Typ : Entity_Id) return Boolean is
349 -- Determines the maximum size of an array aggregate produced by
350 -- converting named to positional notation (e.g. from others clauses).
351 -- This avoids running away with attempts to convert huge aggregates,
352 -- which hit memory limits in the backend.
354 function Component_Count (T : Entity_Id) return Nat;
355 -- The limit is applied to the total number of subcomponents that the
356 -- aggregate will have, which is the number of static expressions
357 -- that will appear in the flattened array. This requires a recursive
358 -- computation of the number of scalar components of the structure.
360 ---------------------
361 -- Component_Count --
362 ---------------------
364 function Component_Count (T : Entity_Id) return Nat is
369 if Is_Scalar_Type (T) then
372 elsif Is_Record_Type (T) then
373 Comp := First_Component (T);
374 while Present (Comp) loop
375 Res := Res + Component_Count (Etype (Comp));
376 Next_Component (Comp);
381 elsif Is_Array_Type (T) then
383 Lo : constant Node_Id :=
384 Type_Low_Bound (Etype (First_Index (T)));
385 Hi : constant Node_Id :=
386 Type_High_Bound (Etype (First_Index (T)));
388 Siz : constant Nat := Component_Count (Component_Type (T));
391 -- Check for superflat arrays, i.e. arrays with such bounds
392 -- as 4 .. 2, to insure that this function never returns a
393 -- meaningless negative value.
395 if not Compile_Time_Known_Value (Lo)
396 or else not Compile_Time_Known_Value (Hi)
397 or else Expr_Value (Hi) < Expr_Value (Lo)
402 -- If the number of components is greater than Int'Last,
403 -- then return Int'Last, so caller will return False (Aggr
404 -- size is not OK). Otherwise, UI_To_Int will crash.
407 UI : constant Uint :=
408 Expr_Value (Hi) - Expr_Value (Lo) + 1;
410 if UI_Is_In_Int_Range (UI) then
411 return Siz * UI_To_Int (UI);
420 -- Can only be a null for an access type
426 -- Start of processing for Aggr_Size_OK
429 -- The normal aggregate limit is 50000, but we increase this limit to
430 -- 2**24 (about 16 million) if Restrictions (No_Elaboration_Code) or
431 -- Restrictions (No_Implicit_Loops) is specified, since in either case
432 -- we are at risk of declaring the program illegal because of this
433 -- limit. We also increase the limit when Static_Elaboration_Desired,
434 -- given that this means that objects are intended to be placed in data
437 -- We also increase the limit if the aggregate is for a packed two-
438 -- dimensional array, because if components are static it is much more
439 -- efficient to construct a one-dimensional equivalent array with static
442 -- Conversely, we decrease the maximum size if none of the above
443 -- requirements apply, and if the aggregate has a single component
444 -- association, which will be more efficient if implemented with a loop.
446 -- Finally, we use a small limit in CodePeer mode where we favor loops
447 -- instead of thousands of single assignments (from large aggregates).
449 Max_Aggr_Size := 50000;
451 if CodePeer_Mode then
452 Max_Aggr_Size := 100;
454 elsif Restriction_Active (No_Elaboration_Code)
455 or else Restriction_Active (No_Implicit_Loops)
456 or else Is_Two_Dim_Packed_Array (Typ)
457 or else (Ekind (Current_Scope) = E_Package
458 and then Static_Elaboration_Desired (Current_Scope))
460 Max_Aggr_Size := 2 ** 24;
462 elsif No (Expressions (N))
463 and then No (Next (First (Component_Associations (N))))
465 Max_Aggr_Size := 5000;
468 Siz := Component_Count (Component_Type (Typ));
470 Indx := First_Index (Typ);
471 while Present (Indx) loop
472 Lo := Type_Low_Bound (Etype (Indx));
473 Hi := Type_High_Bound (Etype (Indx));
475 -- Bounds need to be known at compile time
477 if not Compile_Time_Known_Value (Lo)
478 or else not Compile_Time_Known_Value (Hi)
483 Lov := Expr_Value (Lo);
484 Hiv := Expr_Value (Hi);
486 -- A flat array is always safe
492 -- One-component aggregates are suspicious, and if the context type
493 -- is an object declaration with non-static bounds it will trip gcc;
494 -- such an aggregate must be expanded into a single assignment.
496 if Hiv = Lov and then Nkind (Parent (N)) = N_Object_Declaration then
498 Index_Type : constant Entity_Id :=
500 (First_Index (Etype (Defining_Identifier (Parent (N)))));
504 if not Compile_Time_Known_Value (Type_Low_Bound (Index_Type))
505 or else not Compile_Time_Known_Value
506 (Type_High_Bound (Index_Type))
508 if Present (Component_Associations (N)) then
511 (Choice_List (First (Component_Associations (N))));
513 if Is_Entity_Name (Indx)
514 and then not Is_Type (Entity (Indx))
517 ("single component aggregate in "
518 & "non-static context??", Indx);
519 Error_Msg_N ("\maybe subtype name was meant??", Indx);
529 Rng : constant Uint := Hiv - Lov + 1;
532 -- Check if size is too large
534 if not UI_Is_In_Int_Range (Rng) then
538 Siz := Siz * UI_To_Int (Rng);
542 or else Siz > Max_Aggr_Size
547 -- Bounds must be in integer range, for later array construction
549 if not UI_Is_In_Int_Range (Lov)
551 not UI_Is_In_Int_Range (Hiv)
562 ---------------------------------
563 -- Backend_Processing_Possible --
564 ---------------------------------
566 -- Backend processing by Gigi/gcc is possible only if all the following
567 -- conditions are met:
569 -- 1. N is fully positional
571 -- 2. N is not a bit-packed array aggregate;
573 -- 3. The size of N's array type must be known at compile time. Note
574 -- that this implies that the component size is also known
576 -- 4. The array type of N does not follow the Fortran layout convention
577 -- or if it does it must be 1 dimensional.
579 -- 5. The array component type may not be tagged (which could necessitate
580 -- reassignment of proper tags).
582 -- 6. The array component type must not have unaligned bit components
584 -- 7. None of the components of the aggregate may be bit unaligned
587 -- 8. There cannot be delayed components, since we do not know enough
588 -- at this stage to know if back end processing is possible.
590 -- 9. There cannot be any discriminated record components, since the
591 -- back end cannot handle this complex case.
593 -- 10. No controlled actions need to be generated for components
595 -- 11. When generating C code, N must be part of a N_Object_Declaration
597 -- 12. When generating C code, N must not include function calls
599 function Backend_Processing_Possible (N : Node_Id) return Boolean is
600 Typ : constant Entity_Id := Etype (N);
601 -- Typ is the correct constrained array subtype of the aggregate
603 function Component_Check (N : Node_Id; Index : Node_Id) return Boolean;
604 -- This routine checks components of aggregate N, enforcing checks
605 -- 1, 7, 8, 9, 11, and 12. In the multidimensional case, these checks
606 -- are performed on subaggregates. The Index value is the current index
607 -- being checked in the multidimensional case.
609 ---------------------
610 -- Component_Check --
611 ---------------------
613 function Component_Check (N : Node_Id; Index : Node_Id) return Boolean is
614 function Ultimate_Original_Expression (N : Node_Id) return Node_Id;
615 -- Given a type conversion or an unchecked type conversion N, return
616 -- its innermost original expression.
618 ----------------------------------
619 -- Ultimate_Original_Expression --
620 ----------------------------------
622 function Ultimate_Original_Expression (N : Node_Id) return Node_Id is
623 Expr : Node_Id := Original_Node (N);
626 while Nkind_In (Expr, N_Type_Conversion,
627 N_Unchecked_Type_Conversion)
629 Expr := Original_Node (Expression (Expr));
633 end Ultimate_Original_Expression;
639 -- Start of processing for Component_Check
642 -- Checks 1: (no component associations)
644 if Present (Component_Associations (N)) then
648 -- Checks 11: (part of an object declaration)
651 and then Nkind (Parent (N)) /= N_Object_Declaration
653 (Nkind (Parent (N)) /= N_Qualified_Expression
654 or else Nkind (Parent (Parent (N))) /= N_Object_Declaration)
659 -- Checks on components
661 -- Recurse to check subaggregates, which may appear in qualified
662 -- expressions. If delayed, the front-end will have to expand.
663 -- If the component is a discriminated record, treat as non-static,
664 -- as the back-end cannot handle this properly.
666 Expr := First (Expressions (N));
667 while Present (Expr) loop
669 -- Checks 8: (no delayed components)
671 if Is_Delayed_Aggregate (Expr) then
675 -- Checks 9: (no discriminated records)
677 if Present (Etype (Expr))
678 and then Is_Record_Type (Etype (Expr))
679 and then Has_Discriminants (Etype (Expr))
684 -- Checks 7. Component must not be bit aligned component
686 if Possible_Bit_Aligned_Component (Expr) then
690 -- Checks 12: (no function call)
694 Nkind (Ultimate_Original_Expression (Expr)) = N_Function_Call
699 -- Recursion to following indexes for multiple dimension case
701 if Present (Next_Index (Index))
702 and then not Component_Check (Expr, Next_Index (Index))
707 -- All checks for that component finished, on to next
715 -- Start of processing for Backend_Processing_Possible
718 -- Checks 2 (array not bit packed) and 10 (no controlled actions)
720 if Is_Bit_Packed_Array (Typ) or else Needs_Finalization (Typ) then
724 -- If component is limited, aggregate must be expanded because each
725 -- component assignment must be built in place.
727 if Is_Limited_View (Component_Type (Typ)) then
731 -- Checks 4 (array must not be multidimensional Fortran case)
733 if Convention (Typ) = Convention_Fortran
734 and then Number_Dimensions (Typ) > 1
739 -- Checks 3 (size of array must be known at compile time)
741 if not Size_Known_At_Compile_Time (Typ) then
745 -- Checks on components
747 if not Component_Check (N, First_Index (Typ)) then
751 -- Checks 5 (if the component type is tagged, then we may need to do
752 -- tag adjustments. Perhaps this should be refined to check for any
753 -- component associations that actually need tag adjustment, similar
754 -- to the test in Component_Not_OK_For_Backend for record aggregates
755 -- with tagged components, but not clear whether it's worthwhile ???;
756 -- in the case of virtual machines (no Tagged_Type_Expansion), object
757 -- tags are handled implicitly).
759 if Is_Tagged_Type (Component_Type (Typ))
760 and then Tagged_Type_Expansion
765 -- Checks 6 (component type must not have bit aligned components)
767 if Type_May_Have_Bit_Aligned_Components (Component_Type (Typ)) then
771 -- Backend processing is possible
773 Set_Size_Known_At_Compile_Time (Etype (N), True);
775 end Backend_Processing_Possible;
777 ---------------------------
778 -- Build_Array_Aggr_Code --
779 ---------------------------
781 -- The code that we generate from a one dimensional aggregate is
783 -- 1. If the subaggregate contains discrete choices we
785 -- (a) Sort the discrete choices
787 -- (b) Otherwise for each discrete choice that specifies a range we
788 -- emit a loop. If a range specifies a maximum of three values, or
789 -- we are dealing with an expression we emit a sequence of
790 -- assignments instead of a loop.
792 -- (c) Generate the remaining loops to cover the others choice if any
794 -- 2. If the aggregate contains positional elements we
796 -- (a) translate the positional elements in a series of assignments
798 -- (b) Generate a final loop to cover the others choice if any.
799 -- Note that this final loop has to be a while loop since the case
801 -- L : Integer := Integer'Last;
802 -- H : Integer := Integer'Last;
803 -- A : array (L .. H) := (1, others =>0);
805 -- cannot be handled by a for loop. Thus for the following
807 -- array (L .. H) := (.. positional elements.., others =>E);
809 -- we always generate something like:
811 -- J : Index_Type := Index_Of_Last_Positional_Element;
813 -- J := Index_Base'Succ (J)
817 function Build_Array_Aggr_Code
822 Scalar_Comp : Boolean;
823 Indexes : List_Id := No_List) return List_Id
825 Loc : constant Source_Ptr := Sloc (N);
826 Index_Base : constant Entity_Id := Base_Type (Etype (Index));
827 Index_Base_L : constant Node_Id := Type_Low_Bound (Index_Base);
828 Index_Base_H : constant Node_Id := Type_High_Bound (Index_Base);
830 function Add (Val : Int; To : Node_Id) return Node_Id;
831 -- Returns an expression where Val is added to expression To, unless
832 -- To+Val is provably out of To's base type range. To must be an
833 -- already analyzed expression.
835 function Empty_Range (L, H : Node_Id) return Boolean;
836 -- Returns True if the range defined by L .. H is certainly empty
838 function Equal (L, H : Node_Id) return Boolean;
839 -- Returns True if L = H for sure
841 function Index_Base_Name return Node_Id;
842 -- Returns a new reference to the index type name
847 In_Loop : Boolean := False) return List_Id;
848 -- Ind must be a side-effect-free expression. If the input aggregate N
849 -- to Build_Loop contains no subaggregates, then this function returns
850 -- the assignment statement:
852 -- Into (Indexes, Ind) := Expr;
854 -- Otherwise we call Build_Code recursively. Flag In_Loop should be set
855 -- when the assignment appears within a generated loop.
857 -- Ada 2005 (AI-287): In case of default initialized component, Expr
858 -- is empty and we generate a call to the corresponding IP subprogram.
860 function Gen_Loop (L, H : Node_Id; Expr : Node_Id) return List_Id;
861 -- Nodes L and H must be side-effect-free expressions. If the input
862 -- aggregate N to Build_Loop contains no subaggregates, this routine
863 -- returns the for loop statement:
865 -- for J in Index_Base'(L) .. Index_Base'(H) loop
866 -- Into (Indexes, J) := Expr;
869 -- Otherwise we call Build_Code recursively. As an optimization if the
870 -- loop covers 3 or fewer scalar elements we generate a sequence of
872 -- If the component association that generates the loop comes from an
873 -- Iterated_Component_Association, the loop parameter has the name of
874 -- the corresponding parameter in the original construct.
876 function Gen_While (L, H : Node_Id; Expr : Node_Id) return List_Id;
877 -- Nodes L and H must be side-effect-free expressions. If the input
878 -- aggregate N to Build_Loop contains no subaggregates, this routine
879 -- returns the while loop statement:
881 -- J : Index_Base := L;
883 -- J := Index_Base'Succ (J);
884 -- Into (Indexes, J) := Expr;
887 -- Otherwise we call Build_Code recursively
889 function Get_Assoc_Expr (Assoc : Node_Id) return Node_Id;
890 -- For an association with a box, use value given by aspect
891 -- Default_Component_Value of array type if specified, else use
892 -- value given by aspect Default_Value for component type itself
893 -- if specified, else return Empty.
895 function Local_Compile_Time_Known_Value (E : Node_Id) return Boolean;
896 function Local_Expr_Value (E : Node_Id) return Uint;
897 -- These two Local routines are used to replace the corresponding ones
898 -- in sem_eval because while processing the bounds of an aggregate with
899 -- discrete choices whose index type is an enumeration, we build static
900 -- expressions not recognized by Compile_Time_Known_Value as such since
901 -- they have not yet been analyzed and resolved. All the expressions in
902 -- question are things like Index_Base_Name'Val (Const) which we can
903 -- easily recognize as being constant.
909 function Add (Val : Int; To : Node_Id) return Node_Id is
914 U_Val : constant Uint := UI_From_Int (Val);
917 -- Note: do not try to optimize the case of Val = 0, because
918 -- we need to build a new node with the proper Sloc value anyway.
920 -- First test if we can do constant folding
922 if Local_Compile_Time_Known_Value (To) then
923 U_To := Local_Expr_Value (To) + Val;
925 -- Determine if our constant is outside the range of the index.
926 -- If so return an Empty node. This empty node will be caught
927 -- by Empty_Range below.
929 if Compile_Time_Known_Value (Index_Base_L)
930 and then U_To < Expr_Value (Index_Base_L)
934 elsif Compile_Time_Known_Value (Index_Base_H)
935 and then U_To > Expr_Value (Index_Base_H)
940 Expr_Pos := Make_Integer_Literal (Loc, U_To);
941 Set_Is_Static_Expression (Expr_Pos);
943 if not Is_Enumeration_Type (Index_Base) then
946 -- If we are dealing with enumeration return
947 -- Index_Base'Val (Expr_Pos)
951 Make_Attribute_Reference
953 Prefix => Index_Base_Name,
954 Attribute_Name => Name_Val,
955 Expressions => New_List (Expr_Pos));
961 -- If we are here no constant folding possible
963 if not Is_Enumeration_Type (Index_Base) then
966 Left_Opnd => Duplicate_Subexpr (To),
967 Right_Opnd => Make_Integer_Literal (Loc, U_Val));
969 -- If we are dealing with enumeration return
970 -- Index_Base'Val (Index_Base'Pos (To) + Val)
974 Make_Attribute_Reference
976 Prefix => Index_Base_Name,
977 Attribute_Name => Name_Pos,
978 Expressions => New_List (Duplicate_Subexpr (To)));
983 Right_Opnd => Make_Integer_Literal (Loc, U_Val));
986 Make_Attribute_Reference
988 Prefix => Index_Base_Name,
989 Attribute_Name => Name_Val,
990 Expressions => New_List (Expr_Pos));
1000 function Empty_Range (L, H : Node_Id) return Boolean is
1001 Is_Empty : Boolean := False;
1006 -- First check if L or H were already detected as overflowing the
1007 -- index base range type by function Add above. If this is so Add
1008 -- returns the empty node.
1010 if No (L) or else No (H) then
1014 for J in 1 .. 3 loop
1017 -- L > H range is empty
1023 -- B_L > H range must be empty
1026 Low := Index_Base_L;
1029 -- L > B_H range must be empty
1033 High := Index_Base_H;
1036 if Local_Compile_Time_Known_Value (Low)
1038 Local_Compile_Time_Known_Value (High)
1041 UI_Gt (Local_Expr_Value (Low), Local_Expr_Value (High));
1054 function Equal (L, H : Node_Id) return Boolean is
1059 elsif Local_Compile_Time_Known_Value (L)
1061 Local_Compile_Time_Known_Value (H)
1063 return UI_Eq (Local_Expr_Value (L), Local_Expr_Value (H));
1076 In_Loop : Boolean := False) return List_Id
1078 function Add_Loop_Actions (Lis : List_Id) return List_Id;
1079 -- Collect insert_actions generated in the construction of a loop,
1080 -- and prepend them to the sequence of assignments to complete the
1081 -- eventual body of the loop.
1083 procedure Initialize_Array_Component
1084 (Arr_Comp : Node_Id;
1086 Init_Expr : Node_Id;
1088 -- Perform the initialization of array component Arr_Comp with
1089 -- expected type Comp_Typ. Init_Expr denotes the initialization
1090 -- expression of the array component. All generated code is added
1093 procedure Initialize_Ctrl_Array_Component
1094 (Arr_Comp : Node_Id;
1095 Comp_Typ : Entity_Id;
1096 Init_Expr : Node_Id;
1098 -- Perform the initialization of array component Arr_Comp when its
1099 -- expected type Comp_Typ needs finalization actions. Init_Expr is
1100 -- the initialization expression of the array component. All hook-
1101 -- related declarations are inserted prior to aggregate N. Remaining
1102 -- code is added to list Stmts.
1104 ----------------------
1105 -- Add_Loop_Actions --
1106 ----------------------
1108 function Add_Loop_Actions (Lis : List_Id) return List_Id is
1112 -- Ada 2005 (AI-287): Do nothing else in case of default
1113 -- initialized component.
1118 elsif Nkind (Parent (Expr)) = N_Component_Association
1119 and then Present (Loop_Actions (Parent (Expr)))
1121 Append_List (Lis, Loop_Actions (Parent (Expr)));
1122 Res := Loop_Actions (Parent (Expr));
1123 Set_Loop_Actions (Parent (Expr), No_List);
1129 end Add_Loop_Actions;
1131 --------------------------------
1132 -- Initialize_Array_Component --
1133 --------------------------------
1135 procedure Initialize_Array_Component
1136 (Arr_Comp : Node_Id;
1138 Init_Expr : Node_Id;
1141 Exceptions_OK : constant Boolean :=
1142 not Restriction_Active
1143 (No_Exception_Propagation);
1145 Finalization_OK : constant Boolean :=
1147 and then Needs_Finalization (Comp_Typ);
1149 Full_Typ : constant Entity_Id := Underlying_Type (Comp_Typ);
1151 Blk_Stmts : List_Id;
1152 Init_Stmt : Node_Id;
1155 -- Protect the initialization statements from aborts. Generate:
1159 if Finalization_OK and Abort_Allowed then
1160 if Exceptions_OK then
1161 Blk_Stmts := New_List;
1166 Append_To (Blk_Stmts, Build_Runtime_Call (Loc, RE_Abort_Defer));
1168 -- Otherwise aborts are not allowed. All generated code is added
1169 -- directly to the input list.
1175 -- Initialize the array element. Generate:
1177 -- Arr_Comp := Init_Expr;
1179 -- Note that the initialization expression is replicated because
1180 -- it has to be reevaluated within a generated loop.
1183 Make_OK_Assignment_Statement (Loc,
1184 Name => New_Copy_Tree (Arr_Comp),
1185 Expression => New_Copy_Tree (Init_Expr));
1186 Set_No_Ctrl_Actions (Init_Stmt);
1188 -- If this is an aggregate for an array of arrays, each
1189 -- subaggregate will be expanded as well, and even with
1190 -- No_Ctrl_Actions the assignments of inner components will
1191 -- require attachment in their assignments to temporaries. These
1192 -- temporaries must be finalized for each subaggregate. Generate:
1195 -- Arr_Comp := Init_Expr;
1198 if Finalization_OK and then Is_Array_Type (Comp_Typ) then
1200 Make_Block_Statement (Loc,
1201 Handled_Statement_Sequence =>
1202 Make_Handled_Sequence_Of_Statements (Loc,
1203 Statements => New_List (Init_Stmt)));
1206 Append_To (Blk_Stmts, Init_Stmt);
1208 -- Adjust the tag due to a possible view conversion. Generate:
1210 -- Arr_Comp._tag := Full_TypP;
1212 if Tagged_Type_Expansion
1213 and then Present (Comp_Typ)
1214 and then Is_Tagged_Type (Comp_Typ)
1216 Append_To (Blk_Stmts,
1217 Make_OK_Assignment_Statement (Loc,
1219 Make_Selected_Component (Loc,
1220 Prefix => New_Copy_Tree (Arr_Comp),
1223 (First_Tag_Component (Full_Typ), Loc)),
1226 Unchecked_Convert_To (RTE (RE_Tag),
1228 (Node (First_Elmt (Access_Disp_Table (Full_Typ))),
1232 -- Adjust the array component. Controlled subaggregates are not
1233 -- considered because each of their individual elements will
1234 -- receive an adjustment of its own. Generate:
1236 -- [Deep_]Adjust (Arr_Comp);
1239 and then not Is_Limited_Type (Comp_Typ)
1241 (Is_Array_Type (Comp_Typ)
1242 and then Is_Controlled (Component_Type (Comp_Typ))
1243 and then Nkind (Expr) = N_Aggregate)
1247 (Obj_Ref => New_Copy_Tree (Arr_Comp),
1250 -- Guard against a missing [Deep_]Adjust when the component
1251 -- type was not frozen properly.
1253 if Present (Adj_Call) then
1254 Append_To (Blk_Stmts, Adj_Call);
1258 -- Complete the protection of the initialization statements
1260 if Finalization_OK and Abort_Allowed then
1262 -- Wrap the initialization statements in a block to catch a
1263 -- potential exception. Generate:
1267 -- Arr_Comp := Init_Expr;
1268 -- Arr_Comp._tag := Full_TypP;
1269 -- [Deep_]Adjust (Arr_Comp);
1271 -- Abort_Undefer_Direct;
1274 if Exceptions_OK then
1276 Build_Abort_Undefer_Block (Loc,
1280 -- Otherwise exceptions are not propagated. Generate:
1283 -- Arr_Comp := Init_Expr;
1284 -- Arr_Comp._tag := Full_TypP;
1285 -- [Deep_]Adjust (Arr_Comp);
1289 Append_To (Blk_Stmts,
1290 Build_Runtime_Call (Loc, RE_Abort_Undefer));
1293 end Initialize_Array_Component;
1295 -------------------------------------
1296 -- Initialize_Ctrl_Array_Component --
1297 -------------------------------------
1299 procedure Initialize_Ctrl_Array_Component
1300 (Arr_Comp : Node_Id;
1301 Comp_Typ : Entity_Id;
1302 Init_Expr : Node_Id;
1306 Act_Stmts : List_Id;
1309 Hook_Clear : Node_Id;
1311 In_Place_Expansion : Boolean;
1312 -- Flag set when a nonlimited controlled function call requires
1313 -- in-place expansion.
1316 -- Duplicate the initialization expression in case the context is
1317 -- a multi choice list or an "others" choice which plugs various
1318 -- holes in the aggregate. As a result the expression is no longer
1319 -- shared between the various components and is reevaluated for
1320 -- each such component.
1322 Expr := New_Copy_Tree (Init_Expr);
1323 Set_Parent (Expr, Parent (Init_Expr));
1325 -- Perform a preliminary analysis and resolution to determine what
1326 -- the initialization expression denotes. An unanalyzed function
1327 -- call may appear as an identifier or an indexed component.
1329 if Nkind_In (Expr, N_Function_Call,
1331 N_Indexed_Component)
1332 and then not Analyzed (Expr)
1334 Preanalyze_And_Resolve (Expr, Comp_Typ);
1337 In_Place_Expansion :=
1338 Nkind (Expr) = N_Function_Call
1339 and then not Is_Limited_Type (Comp_Typ);
1341 -- The initialization expression is a controlled function call.
1342 -- Perform in-place removal of side effects to avoid creating a
1343 -- transient scope, which leads to premature finalization.
1345 -- This in-place expansion is not performed for limited transient
1346 -- objects because the initialization is already done in-place.
1348 if In_Place_Expansion then
1350 -- Suppress the removal of side effects by general analysis
1351 -- because this behavior is emulated here. This avoids the
1352 -- generation of a transient scope, which leads to out-of-order
1353 -- adjustment and finalization.
1355 Set_No_Side_Effect_Removal (Expr);
1357 -- When the transient component initialization is related to a
1358 -- range or an "others", keep all generated statements within
1359 -- the enclosing loop. This way the controlled function call
1360 -- will be evaluated at each iteration, and its result will be
1361 -- finalized at the end of each iteration.
1367 -- Otherwise this is a single component initialization. Hook-
1368 -- related statements are inserted prior to the aggregate.
1372 Act_Stmts := No_List;
1375 -- Install all hook-related declarations and prepare the clean
1378 Process_Transient_Component
1380 Comp_Typ => Comp_Typ,
1382 Fin_Call => Fin_Call,
1383 Hook_Clear => Hook_Clear,
1385 Stmts => Act_Stmts);
1388 -- Use the noncontrolled component initialization circuitry to
1389 -- assign the result of the function call to the array element.
1390 -- This also performs subaggregate wrapping, tag adjustment, and
1391 -- [deep] adjustment of the array element.
1393 Initialize_Array_Component
1394 (Arr_Comp => Arr_Comp,
1395 Comp_Typ => Comp_Typ,
1399 -- At this point the array element is fully initialized. Complete
1400 -- the processing of the controlled array component by finalizing
1401 -- the transient function result.
1403 if In_Place_Expansion then
1404 Process_Transient_Component_Completion
1407 Fin_Call => Fin_Call,
1408 Hook_Clear => Hook_Clear,
1411 end Initialize_Ctrl_Array_Component;
1415 Stmts : constant List_Id := New_List;
1417 Comp_Typ : Entity_Id := Empty;
1419 Indexed_Comp : Node_Id;
1420 Init_Call : Node_Id;
1421 New_Indexes : List_Id;
1423 -- Start of processing for Gen_Assign
1426 if No (Indexes) then
1427 New_Indexes := New_List;
1429 New_Indexes := New_Copy_List_Tree (Indexes);
1432 Append_To (New_Indexes, Ind);
1434 if Present (Next_Index (Index)) then
1437 Build_Array_Aggr_Code
1440 Index => Next_Index (Index),
1442 Scalar_Comp => Scalar_Comp,
1443 Indexes => New_Indexes));
1446 -- If we get here then we are at a bottom-level (sub-)aggregate
1450 (Make_Indexed_Component (Loc,
1451 Prefix => New_Copy_Tree (Into),
1452 Expressions => New_Indexes));
1454 Set_Assignment_OK (Indexed_Comp);
1456 -- Ada 2005 (AI-287): In case of default initialized component, Expr
1457 -- is not present (and therefore we also initialize Expr_Q to empty).
1461 elsif Nkind (Expr) = N_Qualified_Expression then
1462 Expr_Q := Expression (Expr);
1467 if Present (Etype (N)) and then Etype (N) /= Any_Composite then
1468 Comp_Typ := Component_Type (Etype (N));
1469 pragma Assert (Comp_Typ = Ctype); -- AI-287
1471 elsif Present (Next (First (New_Indexes))) then
1473 -- Ada 2005 (AI-287): Do nothing in case of default initialized
1474 -- component because we have received the component type in
1475 -- the formal parameter Ctype.
1477 -- ??? Some assert pragmas have been added to check if this new
1478 -- formal can be used to replace this code in all cases.
1480 if Present (Expr) then
1482 -- This is a multidimensional array. Recover the component type
1483 -- from the outermost aggregate, because subaggregates do not
1484 -- have an assigned type.
1491 while Present (P) loop
1492 if Nkind (P) = N_Aggregate
1493 and then Present (Etype (P))
1495 Comp_Typ := Component_Type (Etype (P));
1503 pragma Assert (Comp_Typ = Ctype); -- AI-287
1508 -- Ada 2005 (AI-287): We only analyze the expression in case of non-
1509 -- default initialized components (otherwise Expr_Q is not present).
1512 and then Nkind_In (Expr_Q, N_Aggregate, N_Extension_Aggregate)
1514 -- At this stage the Expression may not have been analyzed yet
1515 -- because the array aggregate code has not been updated to use
1516 -- the Expansion_Delayed flag and avoid analysis altogether to
1517 -- solve the same problem (see Resolve_Aggr_Expr). So let us do
1518 -- the analysis of non-array aggregates now in order to get the
1519 -- value of Expansion_Delayed flag for the inner aggregate ???
1521 if Present (Comp_Typ) and then not Is_Array_Type (Comp_Typ) then
1522 Analyze_And_Resolve (Expr_Q, Comp_Typ);
1525 if Is_Delayed_Aggregate (Expr_Q) then
1527 -- This is either a subaggregate of a multidimensional array,
1528 -- or a component of an array type whose component type is
1529 -- also an array. In the latter case, the expression may have
1530 -- component associations that provide different bounds from
1531 -- those of the component type, and sliding must occur. Instead
1532 -- of decomposing the current aggregate assignment, force the
1533 -- reanalysis of the assignment, so that a temporary will be
1534 -- generated in the usual fashion, and sliding will take place.
1536 if Nkind (Parent (N)) = N_Assignment_Statement
1537 and then Is_Array_Type (Comp_Typ)
1538 and then Present (Component_Associations (Expr_Q))
1539 and then Must_Slide (Comp_Typ, Etype (Expr_Q))
1541 Set_Expansion_Delayed (Expr_Q, False);
1542 Set_Analyzed (Expr_Q, False);
1547 Late_Expansion (Expr_Q, Etype (Expr_Q), Indexed_Comp));
1552 if Present (Expr) then
1554 -- Handle an initialization expression of a controlled type in
1555 -- case it denotes a function call. In general such a scenario
1556 -- will produce a transient scope, but this will lead to wrong
1557 -- order of initialization, adjustment, and finalization in the
1558 -- context of aggregates.
1560 -- Target (1) := Ctrl_Func_Call;
1563 -- Trans_Obj : ... := Ctrl_Func_Call; -- object
1564 -- Target (1) := Trans_Obj;
1565 -- Finalize (Trans_Obj);
1567 -- Target (1)._tag := ...;
1568 -- Adjust (Target (1));
1570 -- In the example above, the call to Finalize occurs too early
1571 -- and as a result it may leave the array component in a bad
1572 -- state. Finalization of the transient object should really
1573 -- happen after adjustment.
1575 -- To avoid this scenario, perform in-place side-effect removal
1576 -- of the function call. This eliminates the transient property
1577 -- of the function result and ensures correct order of actions.
1579 -- Res : ... := Ctrl_Func_Call;
1580 -- Target (1) := Res;
1581 -- Target (1)._tag := ...;
1582 -- Adjust (Target (1));
1585 if Present (Comp_Typ)
1586 and then Needs_Finalization (Comp_Typ)
1587 and then Nkind (Expr) /= N_Aggregate
1589 Initialize_Ctrl_Array_Component
1590 (Arr_Comp => Indexed_Comp,
1591 Comp_Typ => Comp_Typ,
1595 -- Otherwise perform simple component initialization
1598 Initialize_Array_Component
1599 (Arr_Comp => Indexed_Comp,
1600 Comp_Typ => Comp_Typ,
1605 -- Ada 2005 (AI-287): In case of default initialized component, call
1606 -- the initialization subprogram associated with the component type.
1607 -- If the component type is an access type, add an explicit null
1608 -- assignment, because for the back-end there is an initialization
1609 -- present for the whole aggregate, and no default initialization
1612 -- In addition, if the component type is controlled, we must call
1613 -- its Initialize procedure explicitly, because there is no explicit
1614 -- object creation that will invoke it otherwise.
1617 if Present (Base_Init_Proc (Base_Type (Ctype)))
1618 or else Has_Task (Base_Type (Ctype))
1620 Append_List_To (Stmts,
1621 Build_Initialization_Call (Loc,
1622 Id_Ref => Indexed_Comp,
1624 With_Default_Init => True));
1626 -- If the component type has invariants, add an invariant
1627 -- check after the component is default-initialized. It will
1628 -- be analyzed and resolved before the code for initialization
1629 -- of other components.
1631 if Has_Invariants (Ctype) then
1632 Set_Etype (Indexed_Comp, Ctype);
1633 Append_To (Stmts, Make_Invariant_Call (Indexed_Comp));
1636 elsif Is_Access_Type (Ctype) then
1638 Make_Assignment_Statement (Loc,
1639 Name => New_Copy_Tree (Indexed_Comp),
1640 Expression => Make_Null (Loc)));
1643 if Needs_Finalization (Ctype) then
1646 (Obj_Ref => New_Copy_Tree (Indexed_Comp),
1649 -- Guard against a missing [Deep_]Initialize when the component
1650 -- type was not properly frozen.
1652 if Present (Init_Call) then
1653 Append_To (Stmts, Init_Call);
1658 return Add_Loop_Actions (Stmts);
1665 function Gen_Loop (L, H : Node_Id; Expr : Node_Id) return List_Id is
1666 Is_Iterated_Component : constant Boolean :=
1667 Nkind (Parent (Expr)) = N_Iterated_Component_Association;
1678 -- Index_Base'(L) .. Index_Base'(H)
1680 L_Iteration_Scheme : Node_Id;
1681 -- L_J in Index_Base'(L) .. Index_Base'(H)
1684 -- The statements to execute in the loop
1686 S : constant List_Id := New_List;
1687 -- List of statements
1690 -- Copy of expression tree, used for checking purposes
1693 -- If loop bounds define an empty range return the null statement
1695 if Empty_Range (L, H) then
1696 Append_To (S, Make_Null_Statement (Loc));
1698 -- Ada 2005 (AI-287): Nothing else need to be done in case of
1699 -- default initialized component.
1705 -- The expression must be type-checked even though no component
1706 -- of the aggregate will have this value. This is done only for
1707 -- actual components of the array, not for subaggregates. Do
1708 -- the check on a copy, because the expression may be shared
1709 -- among several choices, some of which might be non-null.
1711 if Present (Etype (N))
1712 and then Is_Array_Type (Etype (N))
1713 and then No (Next_Index (Index))
1715 Expander_Mode_Save_And_Set (False);
1716 Tcopy := New_Copy_Tree (Expr);
1717 Set_Parent (Tcopy, N);
1718 Analyze_And_Resolve (Tcopy, Component_Type (Etype (N)));
1719 Expander_Mode_Restore;
1725 -- If loop bounds are the same then generate an assignment, unless
1726 -- the parent construct is an Iterated_Component_Association.
1728 elsif Equal (L, H) and then not Is_Iterated_Component then
1729 return Gen_Assign (New_Copy_Tree (L), Expr);
1731 -- If H - L <= 2 then generate a sequence of assignments when we are
1732 -- processing the bottom most aggregate and it contains scalar
1735 elsif No (Next_Index (Index))
1736 and then Scalar_Comp
1737 and then Local_Compile_Time_Known_Value (L)
1738 and then Local_Compile_Time_Known_Value (H)
1739 and then Local_Expr_Value (H) - Local_Expr_Value (L) <= 2
1740 and then not Is_Iterated_Component
1742 Append_List_To (S, Gen_Assign (New_Copy_Tree (L), Expr));
1743 Append_List_To (S, Gen_Assign (Add (1, To => L), Expr));
1745 if Local_Expr_Value (H) - Local_Expr_Value (L) = 2 then
1746 Append_List_To (S, Gen_Assign (Add (2, To => L), Expr));
1752 -- Otherwise construct the loop, starting with the loop index L_J
1754 if Is_Iterated_Component then
1756 Make_Defining_Identifier (Loc,
1757 Chars => (Chars (Defining_Identifier (Parent (Expr)))));
1760 L_J := Make_Temporary (Loc, 'J', L);
1763 -- Construct "L .. H" in Index_Base. We use a qualified expression
1764 -- for the bound to convert to the index base, but we don't need
1765 -- to do that if we already have the base type at hand.
1767 if Etype (L) = Index_Base then
1771 Make_Qualified_Expression (Loc,
1772 Subtype_Mark => Index_Base_Name,
1773 Expression => New_Copy_Tree (L));
1776 if Etype (H) = Index_Base then
1780 Make_Qualified_Expression (Loc,
1781 Subtype_Mark => Index_Base_Name,
1782 Expression => New_Copy_Tree (H));
1790 -- Construct "for L_J in Index_Base range L .. H"
1792 L_Iteration_Scheme :=
1793 Make_Iteration_Scheme
1795 Loop_Parameter_Specification =>
1796 Make_Loop_Parameter_Specification
1798 Defining_Identifier => L_J,
1799 Discrete_Subtype_Definition => L_Range));
1801 -- Construct the statements to execute in the loop body
1804 Gen_Assign (New_Occurrence_Of (L_J, Loc), Expr, In_Loop => True);
1806 -- Construct the final loop
1809 Make_Implicit_Loop_Statement
1811 Identifier => Empty,
1812 Iteration_Scheme => L_Iteration_Scheme,
1813 Statements => L_Body));
1815 -- A small optimization: if the aggregate is initialized with a box
1816 -- and the component type has no initialization procedure, remove the
1817 -- useless empty loop.
1819 if Nkind (First (S)) = N_Loop_Statement
1820 and then Is_Empty_List (Statements (First (S)))
1822 return New_List (Make_Null_Statement (Loc));
1832 -- The code built is
1834 -- W_J : Index_Base := L;
1835 -- while W_J < H loop
1836 -- W_J := Index_Base'Succ (W);
1840 function Gen_While (L, H : Node_Id; Expr : Node_Id) return List_Id is
1844 -- W_J : Base_Type := L;
1846 W_Iteration_Scheme : Node_Id;
1849 W_Index_Succ : Node_Id;
1850 -- Index_Base'Succ (J)
1852 W_Increment : Node_Id;
1853 -- W_J := Index_Base'Succ (W)
1855 W_Body : constant List_Id := New_List;
1856 -- The statements to execute in the loop
1858 S : constant List_Id := New_List;
1859 -- list of statement
1862 -- If loop bounds define an empty range or are equal return null
1864 if Empty_Range (L, H) or else Equal (L, H) then
1865 Append_To (S, Make_Null_Statement (Loc));
1869 -- Build the decl of W_J
1871 W_J := Make_Temporary (Loc, 'J', L);
1873 Make_Object_Declaration
1875 Defining_Identifier => W_J,
1876 Object_Definition => Index_Base_Name,
1879 -- Theoretically we should do a New_Copy_Tree (L) here, but we know
1880 -- that in this particular case L is a fresh Expr generated by
1881 -- Add which we are the only ones to use.
1883 Append_To (S, W_Decl);
1885 -- Construct " while W_J < H"
1887 W_Iteration_Scheme :=
1888 Make_Iteration_Scheme
1890 Condition => Make_Op_Lt
1892 Left_Opnd => New_Occurrence_Of (W_J, Loc),
1893 Right_Opnd => New_Copy_Tree (H)));
1895 -- Construct the statements to execute in the loop body
1898 Make_Attribute_Reference
1900 Prefix => Index_Base_Name,
1901 Attribute_Name => Name_Succ,
1902 Expressions => New_List (New_Occurrence_Of (W_J, Loc)));
1905 Make_OK_Assignment_Statement
1907 Name => New_Occurrence_Of (W_J, Loc),
1908 Expression => W_Index_Succ);
1910 Append_To (W_Body, W_Increment);
1912 Append_List_To (W_Body,
1913 Gen_Assign (New_Occurrence_Of (W_J, Loc), Expr, In_Loop => True));
1915 -- Construct the final loop
1918 Make_Implicit_Loop_Statement
1920 Identifier => Empty,
1921 Iteration_Scheme => W_Iteration_Scheme,
1922 Statements => W_Body));
1927 --------------------
1928 -- Get_Assoc_Expr --
1929 --------------------
1931 function Get_Assoc_Expr (Assoc : Node_Id) return Node_Id is
1932 Typ : constant Entity_Id := Base_Type (Etype (N));
1935 if Box_Present (Assoc) then
1936 if Is_Scalar_Type (Ctype) then
1937 if Present (Default_Aspect_Component_Value (Typ)) then
1938 return Default_Aspect_Component_Value (Typ);
1939 elsif Present (Default_Aspect_Value (Ctype)) then
1940 return Default_Aspect_Value (Ctype);
1950 return Expression (Assoc);
1954 ---------------------
1955 -- Index_Base_Name --
1956 ---------------------
1958 function Index_Base_Name return Node_Id is
1960 return New_Occurrence_Of (Index_Base, Sloc (N));
1961 end Index_Base_Name;
1963 ------------------------------------
1964 -- Local_Compile_Time_Known_Value --
1965 ------------------------------------
1967 function Local_Compile_Time_Known_Value (E : Node_Id) return Boolean is
1969 return Compile_Time_Known_Value (E)
1971 (Nkind (E) = N_Attribute_Reference
1972 and then Attribute_Name (E) = Name_Val
1973 and then Compile_Time_Known_Value (First (Expressions (E))));
1974 end Local_Compile_Time_Known_Value;
1976 ----------------------
1977 -- Local_Expr_Value --
1978 ----------------------
1980 function Local_Expr_Value (E : Node_Id) return Uint is
1982 if Compile_Time_Known_Value (E) then
1983 return Expr_Value (E);
1985 return Expr_Value (First (Expressions (E)));
1987 end Local_Expr_Value;
1991 New_Code : constant List_Id := New_List;
1993 Aggr_L : constant Node_Id := Low_Bound (Aggregate_Bounds (N));
1994 Aggr_H : constant Node_Id := High_Bound (Aggregate_Bounds (N));
1995 -- The aggregate bounds of this specific subaggregate. Note that if the
1996 -- code generated by Build_Array_Aggr_Code is executed then these bounds
1997 -- are OK. Otherwise a Constraint_Error would have been raised.
1999 Aggr_Low : constant Node_Id := Duplicate_Subexpr_No_Checks (Aggr_L);
2000 Aggr_High : constant Node_Id := Duplicate_Subexpr_No_Checks (Aggr_H);
2001 -- After Duplicate_Subexpr these are side-effect free
2010 Nb_Choices : Nat := 0;
2011 Table : Case_Table_Type (1 .. Number_Of_Choices (N));
2012 -- Used to sort all the different choice values
2015 -- Number of elements in the positional aggregate
2017 Others_Assoc : Node_Id := Empty;
2019 -- Start of processing for Build_Array_Aggr_Code
2022 -- First before we start, a special case. if we have a bit packed
2023 -- array represented as a modular type, then clear the value to
2024 -- zero first, to ensure that unused bits are properly cleared.
2029 and then Is_Bit_Packed_Array (Typ)
2030 and then Is_Modular_Integer_Type (Packed_Array_Impl_Type (Typ))
2032 Append_To (New_Code,
2033 Make_Assignment_Statement (Loc,
2034 Name => New_Copy_Tree (Into),
2036 Unchecked_Convert_To (Typ,
2037 Make_Integer_Literal (Loc, Uint_0))));
2040 -- If the component type contains tasks, we need to build a Master
2041 -- entity in the current scope, because it will be needed if build-
2042 -- in-place functions are called in the expanded code.
2044 if Nkind (Parent (N)) = N_Object_Declaration and then Has_Task (Typ) then
2045 Build_Master_Entity (Defining_Identifier (Parent (N)));
2048 -- STEP 1: Process component associations
2050 -- For those associations that may generate a loop, initialize
2051 -- Loop_Actions to collect inserted actions that may be crated.
2053 -- Skip this if no component associations
2055 if No (Expressions (N)) then
2057 -- STEP 1 (a): Sort the discrete choices
2059 Assoc := First (Component_Associations (N));
2060 while Present (Assoc) loop
2061 Choice := First (Choice_List (Assoc));
2062 while Present (Choice) loop
2063 if Nkind (Choice) = N_Others_Choice then
2064 Set_Loop_Actions (Assoc, New_List);
2065 Others_Assoc := Assoc;
2069 Get_Index_Bounds (Choice, Low, High);
2072 Set_Loop_Actions (Assoc, New_List);
2075 Nb_Choices := Nb_Choices + 1;
2077 Table (Nb_Choices) :=
2080 Choice_Node => Get_Assoc_Expr (Assoc));
2088 -- If there is more than one set of choices these must be static
2089 -- and we can therefore sort them. Remember that Nb_Choices does not
2090 -- account for an others choice.
2092 if Nb_Choices > 1 then
2093 Sort_Case_Table (Table);
2096 -- STEP 1 (b): take care of the whole set of discrete choices
2098 for J in 1 .. Nb_Choices loop
2099 Low := Table (J).Choice_Lo;
2100 High := Table (J).Choice_Hi;
2101 Expr := Table (J).Choice_Node;
2102 Append_List (Gen_Loop (Low, High, Expr), To => New_Code);
2105 -- STEP 1 (c): generate the remaining loops to cover others choice
2106 -- We don't need to generate loops over empty gaps, but if there is
2107 -- a single empty range we must analyze the expression for semantics
2109 if Present (Others_Assoc) then
2111 First : Boolean := True;
2114 for J in 0 .. Nb_Choices loop
2118 Low := Add (1, To => Table (J).Choice_Hi);
2121 if J = Nb_Choices then
2124 High := Add (-1, To => Table (J + 1).Choice_Lo);
2127 -- If this is an expansion within an init proc, make
2128 -- sure that discriminant references are replaced by
2129 -- the corresponding discriminal.
2131 if Inside_Init_Proc then
2132 if Is_Entity_Name (Low)
2133 and then Ekind (Entity (Low)) = E_Discriminant
2135 Set_Entity (Low, Discriminal (Entity (Low)));
2138 if Is_Entity_Name (High)
2139 and then Ekind (Entity (High)) = E_Discriminant
2141 Set_Entity (High, Discriminal (Entity (High)));
2146 or else not Empty_Range (Low, High)
2150 (Gen_Loop (Low, High,
2151 Get_Assoc_Expr (Others_Assoc)), To => New_Code);
2157 -- STEP 2: Process positional components
2160 -- STEP 2 (a): Generate the assignments for each positional element
2161 -- Note that here we have to use Aggr_L rather than Aggr_Low because
2162 -- Aggr_L is analyzed and Add wants an analyzed expression.
2164 Expr := First (Expressions (N));
2166 while Present (Expr) loop
2167 Nb_Elements := Nb_Elements + 1;
2168 Append_List (Gen_Assign (Add (Nb_Elements, To => Aggr_L), Expr),
2173 -- STEP 2 (b): Generate final loop if an others choice is present
2174 -- Here Nb_Elements gives the offset of the last positional element.
2176 if Present (Component_Associations (N)) then
2177 Assoc := Last (Component_Associations (N));
2179 -- Ada 2005 (AI-287)
2181 Append_List (Gen_While (Add (Nb_Elements, To => Aggr_L),
2183 Get_Assoc_Expr (Assoc)), -- AI-287
2189 end Build_Array_Aggr_Code;
2191 ----------------------------
2192 -- Build_Record_Aggr_Code --
2193 ----------------------------
2195 function Build_Record_Aggr_Code
2198 Lhs : Node_Id) return List_Id
2200 Loc : constant Source_Ptr := Sloc (N);
2201 L : constant List_Id := New_List;
2202 N_Typ : constant Entity_Id := Etype (N);
2208 Comp_Type : Entity_Id;
2209 Selector : Entity_Id;
2210 Comp_Expr : Node_Id;
2213 -- If this is an internal aggregate, the External_Final_List is an
2214 -- expression for the controller record of the enclosing type.
2216 -- If the current aggregate has several controlled components, this
2217 -- expression will appear in several calls to attach to the finali-
2218 -- zation list, and it must not be shared.
2220 Ancestor_Is_Expression : Boolean := False;
2221 Ancestor_Is_Subtype_Mark : Boolean := False;
2223 Init_Typ : Entity_Id := Empty;
2225 Finalization_Done : Boolean := False;
2226 -- True if Generate_Finalization_Actions has already been called; calls
2227 -- after the first do nothing.
2229 function Ancestor_Discriminant_Value (Disc : Entity_Id) return Node_Id;
2230 -- Returns the value that the given discriminant of an ancestor type
2231 -- should receive (in the absence of a conflict with the value provided
2232 -- by an ancestor part of an extension aggregate).
2234 procedure Check_Ancestor_Discriminants (Anc_Typ : Entity_Id);
2235 -- Check that each of the discriminant values defined by the ancestor
2236 -- part of an extension aggregate match the corresponding values
2237 -- provided by either an association of the aggregate or by the
2238 -- constraint imposed by a parent type (RM95-4.3.2(8)).
2240 function Compatible_Int_Bounds
2241 (Agg_Bounds : Node_Id;
2242 Typ_Bounds : Node_Id) return Boolean;
2243 -- Return true if Agg_Bounds are equal or within Typ_Bounds. It is
2244 -- assumed that both bounds are integer ranges.
2246 procedure Generate_Finalization_Actions;
2247 -- Deal with the various controlled type data structure initializations
2248 -- (but only if it hasn't been done already).
2250 function Get_Constraint_Association (T : Entity_Id) return Node_Id;
2251 -- Returns the first discriminant association in the constraint
2252 -- associated with T, if any, otherwise returns Empty.
2254 function Get_Explicit_Discriminant_Value (D : Entity_Id) return Node_Id;
2255 -- If the ancestor part is an unconstrained type and further ancestors
2256 -- do not provide discriminants for it, check aggregate components for
2257 -- values of the discriminants.
2259 procedure Init_Hidden_Discriminants (Typ : Entity_Id; List : List_Id);
2260 -- If Typ is derived, and constrains discriminants of the parent type,
2261 -- these discriminants are not components of the aggregate, and must be
2262 -- initialized. The assignments are appended to List. The same is done
2263 -- if Typ derives fron an already constrained subtype of a discriminated
2266 procedure Init_Stored_Discriminants;
2267 -- If the type is derived and has inherited discriminants, generate
2268 -- explicit assignments for each, using the store constraint of the
2269 -- type. Note that both visible and stored discriminants must be
2270 -- initialized in case the derived type has some renamed and some
2271 -- constrained discriminants.
2273 procedure Init_Visible_Discriminants;
2274 -- If type has discriminants, retrieve their values from aggregate,
2275 -- and generate explicit assignments for each. This does not include
2276 -- discriminants inherited from ancestor, which are handled above.
2277 -- The type of the aggregate is a subtype created ealier using the
2278 -- given values of the discriminant components of the aggregate.
2280 procedure Initialize_Ctrl_Record_Component
2281 (Rec_Comp : Node_Id;
2282 Comp_Typ : Entity_Id;
2283 Init_Expr : Node_Id;
2285 -- Perform the initialization of controlled record component Rec_Comp.
2286 -- Comp_Typ is the component type. Init_Expr is the initialization
2287 -- expression for the record component. Hook-related declarations are
2288 -- inserted prior to aggregate N using Insert_Action. All remaining
2289 -- generated code is added to list Stmts.
2291 procedure Initialize_Record_Component
2292 (Rec_Comp : Node_Id;
2293 Comp_Typ : Entity_Id;
2294 Init_Expr : Node_Id;
2296 -- Perform the initialization of record component Rec_Comp. Comp_Typ
2297 -- is the component type. Init_Expr is the initialization expression
2298 -- of the record component. All generated code is added to list Stmts.
2300 function Is_Int_Range_Bounds (Bounds : Node_Id) return Boolean;
2301 -- Check whether Bounds is a range node and its lower and higher bounds
2302 -- are integers literals.
2304 function Replace_Type (Expr : Node_Id) return Traverse_Result;
2305 -- If the aggregate contains a self-reference, traverse each expression
2306 -- to replace a possible self-reference with a reference to the proper
2307 -- component of the target of the assignment.
2309 function Rewrite_Discriminant (Expr : Node_Id) return Traverse_Result;
2310 -- If default expression of a component mentions a discriminant of the
2311 -- type, it must be rewritten as the discriminant of the target object.
2313 ---------------------------------
2314 -- Ancestor_Discriminant_Value --
2315 ---------------------------------
2317 function Ancestor_Discriminant_Value (Disc : Entity_Id) return Node_Id is
2319 Assoc_Elmt : Elmt_Id;
2320 Aggr_Comp : Entity_Id;
2321 Corresp_Disc : Entity_Id;
2322 Current_Typ : Entity_Id := Base_Type (Typ);
2323 Parent_Typ : Entity_Id;
2324 Parent_Disc : Entity_Id;
2325 Save_Assoc : Node_Id := Empty;
2328 -- First check any discriminant associations to see if any of them
2329 -- provide a value for the discriminant.
2331 if Present (Discriminant_Specifications (Parent (Current_Typ))) then
2332 Assoc := First (Component_Associations (N));
2333 while Present (Assoc) loop
2334 Aggr_Comp := Entity (First (Choices (Assoc)));
2336 if Ekind (Aggr_Comp) = E_Discriminant then
2337 Save_Assoc := Expression (Assoc);
2339 Corresp_Disc := Corresponding_Discriminant (Aggr_Comp);
2340 while Present (Corresp_Disc) loop
2342 -- If found a corresponding discriminant then return the
2343 -- value given in the aggregate. (Note: this is not
2344 -- correct in the presence of side effects. ???)
2346 if Disc = Corresp_Disc then
2347 return Duplicate_Subexpr (Expression (Assoc));
2350 Corresp_Disc := Corresponding_Discriminant (Corresp_Disc);
2358 -- No match found in aggregate, so chain up parent types to find
2359 -- a constraint that defines the value of the discriminant.
2361 Parent_Typ := Etype (Current_Typ);
2362 while Current_Typ /= Parent_Typ loop
2363 if Has_Discriminants (Parent_Typ)
2364 and then not Has_Unknown_Discriminants (Parent_Typ)
2366 Parent_Disc := First_Discriminant (Parent_Typ);
2368 -- We either get the association from the subtype indication
2369 -- of the type definition itself, or from the discriminant
2370 -- constraint associated with the type entity (which is
2371 -- preferable, but it's not always present ???)
2373 if Is_Empty_Elmt_List (Discriminant_Constraint (Current_Typ))
2375 Assoc := Get_Constraint_Association (Current_Typ);
2376 Assoc_Elmt := No_Elmt;
2379 First_Elmt (Discriminant_Constraint (Current_Typ));
2380 Assoc := Node (Assoc_Elmt);
2383 -- Traverse the discriminants of the parent type looking
2384 -- for one that corresponds.
2386 while Present (Parent_Disc) and then Present (Assoc) loop
2387 Corresp_Disc := Parent_Disc;
2388 while Present (Corresp_Disc)
2389 and then Disc /= Corresp_Disc
2391 Corresp_Disc := Corresponding_Discriminant (Corresp_Disc);
2394 if Disc = Corresp_Disc then
2395 if Nkind (Assoc) = N_Discriminant_Association then
2396 Assoc := Expression (Assoc);
2399 -- If the located association directly denotes
2400 -- a discriminant, then use the value of a saved
2401 -- association of the aggregate. This is an approach
2402 -- used to handle certain cases involving multiple
2403 -- discriminants mapped to a single discriminant of
2404 -- a descendant. It's not clear how to locate the
2405 -- appropriate discriminant value for such cases. ???
2407 if Is_Entity_Name (Assoc)
2408 and then Ekind (Entity (Assoc)) = E_Discriminant
2410 Assoc := Save_Assoc;
2413 return Duplicate_Subexpr (Assoc);
2416 Next_Discriminant (Parent_Disc);
2418 if No (Assoc_Elmt) then
2422 Next_Elmt (Assoc_Elmt);
2424 if Present (Assoc_Elmt) then
2425 Assoc := Node (Assoc_Elmt);
2433 Current_Typ := Parent_Typ;
2434 Parent_Typ := Etype (Current_Typ);
2437 -- In some cases there's no ancestor value to locate (such as
2438 -- when an ancestor part given by an expression defines the
2439 -- discriminant value).
2442 end Ancestor_Discriminant_Value;
2444 ----------------------------------
2445 -- Check_Ancestor_Discriminants --
2446 ----------------------------------
2448 procedure Check_Ancestor_Discriminants (Anc_Typ : Entity_Id) is
2450 Disc_Value : Node_Id;
2454 Discr := First_Discriminant (Base_Type (Anc_Typ));
2455 while Present (Discr) loop
2456 Disc_Value := Ancestor_Discriminant_Value (Discr);
2458 if Present (Disc_Value) then
2459 Cond := Make_Op_Ne (Loc,
2461 Make_Selected_Component (Loc,
2462 Prefix => New_Copy_Tree (Target),
2463 Selector_Name => New_Occurrence_Of (Discr, Loc)),
2464 Right_Opnd => Disc_Value);
2467 Make_Raise_Constraint_Error (Loc,
2469 Reason => CE_Discriminant_Check_Failed));
2472 Next_Discriminant (Discr);
2474 end Check_Ancestor_Discriminants;
2476 ---------------------------
2477 -- Compatible_Int_Bounds --
2478 ---------------------------
2480 function Compatible_Int_Bounds
2481 (Agg_Bounds : Node_Id;
2482 Typ_Bounds : Node_Id) return Boolean
2484 Agg_Lo : constant Uint := Intval (Low_Bound (Agg_Bounds));
2485 Agg_Hi : constant Uint := Intval (High_Bound (Agg_Bounds));
2486 Typ_Lo : constant Uint := Intval (Low_Bound (Typ_Bounds));
2487 Typ_Hi : constant Uint := Intval (High_Bound (Typ_Bounds));
2489 return Typ_Lo <= Agg_Lo and then Agg_Hi <= Typ_Hi;
2490 end Compatible_Int_Bounds;
2492 -----------------------------------
2493 -- Generate_Finalization_Actions --
2494 -----------------------------------
2496 procedure Generate_Finalization_Actions is
2498 -- Do the work only the first time this is called
2500 if Finalization_Done then
2504 Finalization_Done := True;
2506 -- Determine the external finalization list. It is either the
2507 -- finalization list of the outer scope or the one coming from an
2508 -- outer aggregate. When the target is not a temporary, the proper
2509 -- scope is the scope of the target rather than the potentially
2510 -- transient current scope.
2512 if Is_Controlled (Typ) and then Ancestor_Is_Subtype_Mark then
2513 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
2514 Set_Assignment_OK (Ref);
2517 Make_Procedure_Call_Statement (Loc,
2520 (Find_Prim_Op (Init_Typ, Name_Initialize), Loc),
2521 Parameter_Associations => New_List (New_Copy_Tree (Ref))));
2523 end Generate_Finalization_Actions;
2525 --------------------------------
2526 -- Get_Constraint_Association --
2527 --------------------------------
2529 function Get_Constraint_Association (T : Entity_Id) return Node_Id is
2536 -- If type is private, get constraint from full view. This was
2537 -- previously done in an instance context, but is needed whenever
2538 -- the ancestor part has a discriminant, possibly inherited through
2539 -- multiple derivations.
2541 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
2542 Typ := Full_View (Typ);
2545 Indic := Subtype_Indication (Type_Definition (Parent (Typ)));
2547 -- Verify that the subtype indication carries a constraint
2549 if Nkind (Indic) = N_Subtype_Indication
2550 and then Present (Constraint (Indic))
2552 return First (Constraints (Constraint (Indic)));
2556 end Get_Constraint_Association;
2558 -------------------------------------
2559 -- Get_Explicit_Discriminant_Value --
2560 -------------------------------------
2562 function Get_Explicit_Discriminant_Value
2563 (D : Entity_Id) return Node_Id
2570 -- The aggregate has been normalized and all associations have a
2573 Assoc := First (Component_Associations (N));
2574 while Present (Assoc) loop
2575 Choice := First (Choices (Assoc));
2577 if Chars (Choice) = Chars (D) then
2578 Val := Expression (Assoc);
2587 end Get_Explicit_Discriminant_Value;
2589 -------------------------------
2590 -- Init_Hidden_Discriminants --
2591 -------------------------------
2593 procedure Init_Hidden_Discriminants (Typ : Entity_Id; List : List_Id) is
2594 function Is_Completely_Hidden_Discriminant
2595 (Discr : Entity_Id) return Boolean;
2596 -- Determine whether Discr is a completely hidden discriminant of
2599 ---------------------------------------
2600 -- Is_Completely_Hidden_Discriminant --
2601 ---------------------------------------
2603 function Is_Completely_Hidden_Discriminant
2604 (Discr : Entity_Id) return Boolean
2609 -- Use First/Next_Entity as First/Next_Discriminant do not yield
2610 -- completely hidden discriminants.
2612 Item := First_Entity (Typ);
2613 while Present (Item) loop
2614 if Ekind (Item) = E_Discriminant
2615 and then Is_Completely_Hidden (Item)
2616 and then Chars (Original_Record_Component (Item)) =
2626 end Is_Completely_Hidden_Discriminant;
2630 Base_Typ : Entity_Id;
2632 Discr_Constr : Elmt_Id;
2633 Discr_Init : Node_Id;
2634 Discr_Val : Node_Id;
2635 In_Aggr_Type : Boolean;
2636 Par_Typ : Entity_Id;
2638 -- Start of processing for Init_Hidden_Discriminants
2641 -- The constraints on the hidden discriminants, if present, are kept
2642 -- in the Stored_Constraint list of the type itself, or in that of
2643 -- the base type. If not in the constraints of the aggregate itself,
2644 -- we examine ancestors to find discriminants that are not renamed
2645 -- by other discriminants but constrained explicitly.
2647 In_Aggr_Type := True;
2649 Base_Typ := Base_Type (Typ);
2650 while Is_Derived_Type (Base_Typ)
2652 (Present (Stored_Constraint (Base_Typ))
2654 (In_Aggr_Type and then Present (Stored_Constraint (Typ))))
2656 Par_Typ := Etype (Base_Typ);
2658 if not Has_Discriminants (Par_Typ) then
2662 Discr := First_Discriminant (Par_Typ);
2664 -- We know that one of the stored-constraint lists is present
2666 if Present (Stored_Constraint (Base_Typ)) then
2667 Discr_Constr := First_Elmt (Stored_Constraint (Base_Typ));
2669 -- For private extension, stored constraint may be on full view
2671 elsif Is_Private_Type (Base_Typ)
2672 and then Present (Full_View (Base_Typ))
2673 and then Present (Stored_Constraint (Full_View (Base_Typ)))
2676 First_Elmt (Stored_Constraint (Full_View (Base_Typ)));
2679 Discr_Constr := First_Elmt (Stored_Constraint (Typ));
2682 while Present (Discr) and then Present (Discr_Constr) loop
2683 Discr_Val := Node (Discr_Constr);
2685 -- The parent discriminant is renamed in the derived type,
2686 -- nothing to initialize.
2688 -- type Deriv_Typ (Discr : ...)
2689 -- is new Parent_Typ (Discr => Discr);
2691 if Is_Entity_Name (Discr_Val)
2692 and then Ekind (Entity (Discr_Val)) = E_Discriminant
2696 -- When the parent discriminant is constrained at the type
2697 -- extension level, it does not appear in the derived type.
2699 -- type Deriv_Typ (Discr : ...)
2700 -- is new Parent_Typ (Discr => Discr,
2701 -- Hidden_Discr => Expression);
2703 elsif Is_Completely_Hidden_Discriminant (Discr) then
2706 -- Otherwise initialize the discriminant
2710 Make_OK_Assignment_Statement (Loc,
2712 Make_Selected_Component (Loc,
2713 Prefix => New_Copy_Tree (Target),
2714 Selector_Name => New_Occurrence_Of (Discr, Loc)),
2715 Expression => New_Copy_Tree (Discr_Val));
2717 Set_No_Ctrl_Actions (Discr_Init);
2718 Append_To (List, Discr_Init);
2721 Next_Elmt (Discr_Constr);
2722 Next_Discriminant (Discr);
2725 In_Aggr_Type := False;
2726 Base_Typ := Base_Type (Par_Typ);
2728 end Init_Hidden_Discriminants;
2730 --------------------------------
2731 -- Init_Visible_Discriminants --
2732 --------------------------------
2734 procedure Init_Visible_Discriminants is
2735 Discriminant : Entity_Id;
2736 Discriminant_Value : Node_Id;
2739 Discriminant := First_Discriminant (Typ);
2740 while Present (Discriminant) loop
2742 Make_Selected_Component (Loc,
2743 Prefix => New_Copy_Tree (Target),
2744 Selector_Name => New_Occurrence_Of (Discriminant, Loc));
2746 Discriminant_Value :=
2747 Get_Discriminant_Value
2748 (Discriminant, Typ, Discriminant_Constraint (N_Typ));
2751 Make_OK_Assignment_Statement (Loc,
2753 Expression => New_Copy_Tree (Discriminant_Value));
2755 Set_No_Ctrl_Actions (Instr);
2756 Append_To (L, Instr);
2758 Next_Discriminant (Discriminant);
2760 end Init_Visible_Discriminants;
2762 -------------------------------
2763 -- Init_Stored_Discriminants --
2764 -------------------------------
2766 procedure Init_Stored_Discriminants is
2767 Discriminant : Entity_Id;
2768 Discriminant_Value : Node_Id;
2771 Discriminant := First_Stored_Discriminant (Typ);
2772 while Present (Discriminant) loop
2774 Make_Selected_Component (Loc,
2775 Prefix => New_Copy_Tree (Target),
2776 Selector_Name => New_Occurrence_Of (Discriminant, Loc));
2778 Discriminant_Value :=
2779 Get_Discriminant_Value
2780 (Discriminant, N_Typ, Discriminant_Constraint (N_Typ));
2783 Make_OK_Assignment_Statement (Loc,
2785 Expression => New_Copy_Tree (Discriminant_Value));
2787 Set_No_Ctrl_Actions (Instr);
2788 Append_To (L, Instr);
2790 Next_Stored_Discriminant (Discriminant);
2792 end Init_Stored_Discriminants;
2794 --------------------------------------
2795 -- Initialize_Ctrl_Record_Component --
2796 --------------------------------------
2798 procedure Initialize_Ctrl_Record_Component
2799 (Rec_Comp : Node_Id;
2800 Comp_Typ : Entity_Id;
2801 Init_Expr : Node_Id;
2805 Hook_Clear : Node_Id;
2807 In_Place_Expansion : Boolean;
2808 -- Flag set when a nonlimited controlled function call requires
2809 -- in-place expansion.
2812 -- Perform a preliminary analysis and resolution to determine what
2813 -- the initialization expression denotes. Unanalyzed function calls
2814 -- may appear as identifiers or indexed components.
2816 if Nkind_In (Init_Expr, N_Function_Call,
2818 N_Indexed_Component)
2819 and then not Analyzed (Init_Expr)
2821 Preanalyze_And_Resolve (Init_Expr, Comp_Typ);
2824 In_Place_Expansion :=
2825 Nkind (Init_Expr) = N_Function_Call
2826 and then not Is_Limited_Type (Comp_Typ);
2828 -- The initialization expression is a controlled function call.
2829 -- Perform in-place removal of side effects to avoid creating a
2832 -- This in-place expansion is not performed for limited transient
2833 -- objects because the initialization is already done in place.
2835 if In_Place_Expansion then
2837 -- Suppress the removal of side effects by general analysis
2838 -- because this behavior is emulated here. This avoids the
2839 -- generation of a transient scope, which leads to out-of-order
2840 -- adjustment and finalization.
2842 Set_No_Side_Effect_Removal (Init_Expr);
2844 -- Install all hook-related declarations and prepare the clean up
2847 Process_Transient_Component
2849 Comp_Typ => Comp_Typ,
2850 Init_Expr => Init_Expr,
2851 Fin_Call => Fin_Call,
2852 Hook_Clear => Hook_Clear,
2856 -- Use the noncontrolled component initialization circuitry to
2857 -- assign the result of the function call to the record component.
2858 -- This also performs tag adjustment and [deep] adjustment of the
2859 -- record component.
2861 Initialize_Record_Component
2862 (Rec_Comp => Rec_Comp,
2863 Comp_Typ => Comp_Typ,
2864 Init_Expr => Init_Expr,
2867 -- At this point the record component is fully initialized. Complete
2868 -- the processing of the controlled record component by finalizing
2869 -- the transient function result.
2871 if In_Place_Expansion then
2872 Process_Transient_Component_Completion
2875 Fin_Call => Fin_Call,
2876 Hook_Clear => Hook_Clear,
2879 end Initialize_Ctrl_Record_Component;
2881 ---------------------------------
2882 -- Initialize_Record_Component --
2883 ---------------------------------
2885 procedure Initialize_Record_Component
2886 (Rec_Comp : Node_Id;
2887 Comp_Typ : Entity_Id;
2888 Init_Expr : Node_Id;
2891 Exceptions_OK : constant Boolean :=
2892 not Restriction_Active (No_Exception_Propagation);
2894 Finalization_OK : constant Boolean := Needs_Finalization (Comp_Typ);
2896 Full_Typ : constant Entity_Id := Underlying_Type (Comp_Typ);
2898 Blk_Stmts : List_Id;
2899 Init_Stmt : Node_Id;
2902 -- Protect the initialization statements from aborts. Generate:
2906 if Finalization_OK and Abort_Allowed then
2907 if Exceptions_OK then
2908 Blk_Stmts := New_List;
2913 Append_To (Blk_Stmts, Build_Runtime_Call (Loc, RE_Abort_Defer));
2915 -- Otherwise aborts are not allowed. All generated code is added
2916 -- directly to the input list.
2922 -- Initialize the record component. Generate:
2924 -- Rec_Comp := Init_Expr;
2926 -- Note that the initialization expression is NOT replicated because
2927 -- only a single component may be initialized by it.
2930 Make_OK_Assignment_Statement (Loc,
2931 Name => New_Copy_Tree (Rec_Comp),
2932 Expression => Init_Expr);
2933 Set_No_Ctrl_Actions (Init_Stmt);
2935 Append_To (Blk_Stmts, Init_Stmt);
2937 -- Adjust the tag due to a possible view conversion. Generate:
2939 -- Rec_Comp._tag := Full_TypeP;
2941 if Tagged_Type_Expansion and then Is_Tagged_Type (Comp_Typ) then
2942 Append_To (Blk_Stmts,
2943 Make_OK_Assignment_Statement (Loc,
2945 Make_Selected_Component (Loc,
2946 Prefix => New_Copy_Tree (Rec_Comp),
2949 (First_Tag_Component (Full_Typ), Loc)),
2952 Unchecked_Convert_To (RTE (RE_Tag),
2954 (Node (First_Elmt (Access_Disp_Table (Full_Typ))),
2958 -- Adjust the component. Generate:
2960 -- [Deep_]Adjust (Rec_Comp);
2962 if Finalization_OK and then not Is_Limited_Type (Comp_Typ) then
2965 (Obj_Ref => New_Copy_Tree (Rec_Comp),
2968 -- Guard against a missing [Deep_]Adjust when the component type
2969 -- was not properly frozen.
2971 if Present (Adj_Call) then
2972 Append_To (Blk_Stmts, Adj_Call);
2976 -- Complete the protection of the initialization statements
2978 if Finalization_OK and Abort_Allowed then
2980 -- Wrap the initialization statements in a block to catch a
2981 -- potential exception. Generate:
2985 -- Rec_Comp := Init_Expr;
2986 -- Rec_Comp._tag := Full_TypP;
2987 -- [Deep_]Adjust (Rec_Comp);
2989 -- Abort_Undefer_Direct;
2992 if Exceptions_OK then
2994 Build_Abort_Undefer_Block (Loc,
2998 -- Otherwise exceptions are not propagated. Generate:
3001 -- Rec_Comp := Init_Expr;
3002 -- Rec_Comp._tag := Full_TypP;
3003 -- [Deep_]Adjust (Rec_Comp);
3007 Append_To (Blk_Stmts,
3008 Build_Runtime_Call (Loc, RE_Abort_Undefer));
3011 end Initialize_Record_Component;
3013 -------------------------
3014 -- Is_Int_Range_Bounds --
3015 -------------------------
3017 function Is_Int_Range_Bounds (Bounds : Node_Id) return Boolean is
3019 return Nkind (Bounds) = N_Range
3020 and then Nkind (Low_Bound (Bounds)) = N_Integer_Literal
3021 and then Nkind (High_Bound (Bounds)) = N_Integer_Literal;
3022 end Is_Int_Range_Bounds;
3028 function Replace_Type (Expr : Node_Id) return Traverse_Result is
3030 -- Note regarding the Root_Type test below: Aggregate components for
3031 -- self-referential types include attribute references to the current
3032 -- instance, of the form: Typ'access, etc.. These references are
3033 -- rewritten as references to the target of the aggregate: the
3034 -- left-hand side of an assignment, the entity in a declaration,
3035 -- or a temporary. Without this test, we would improperly extended
3036 -- this rewriting to attribute references whose prefix was not the
3037 -- type of the aggregate.
3039 if Nkind (Expr) = N_Attribute_Reference
3040 and then Is_Entity_Name (Prefix (Expr))
3041 and then Is_Type (Entity (Prefix (Expr)))
3042 and then Root_Type (Etype (N)) = Root_Type (Entity (Prefix (Expr)))
3044 if Is_Entity_Name (Lhs) then
3045 Rewrite (Prefix (Expr),
3046 New_Occurrence_Of (Entity (Lhs), Loc));
3048 elsif Nkind (Lhs) = N_Selected_Component then
3050 Make_Attribute_Reference (Loc,
3051 Attribute_Name => Name_Unrestricted_Access,
3052 Prefix => New_Copy_Tree (Lhs)));
3053 Set_Analyzed (Parent (Expr), False);
3057 Make_Attribute_Reference (Loc,
3058 Attribute_Name => Name_Unrestricted_Access,
3059 Prefix => New_Copy_Tree (Lhs)));
3060 Set_Analyzed (Parent (Expr), False);
3067 --------------------------
3068 -- Rewrite_Discriminant --
3069 --------------------------
3071 function Rewrite_Discriminant (Expr : Node_Id) return Traverse_Result is
3073 if Is_Entity_Name (Expr)
3074 and then Present (Entity (Expr))
3075 and then Ekind (Entity (Expr)) = E_In_Parameter
3076 and then Present (Discriminal_Link (Entity (Expr)))
3077 and then Scope (Discriminal_Link (Entity (Expr))) =
3078 Base_Type (Etype (N))
3081 Make_Selected_Component (Loc,
3082 Prefix => New_Copy_Tree (Lhs),
3083 Selector_Name => Make_Identifier (Loc, Chars (Expr))));
3087 end Rewrite_Discriminant;
3089 procedure Replace_Discriminants is
3090 new Traverse_Proc (Rewrite_Discriminant);
3092 procedure Replace_Self_Reference is
3093 new Traverse_Proc (Replace_Type);
3095 -- Start of processing for Build_Record_Aggr_Code
3098 if Has_Self_Reference (N) then
3099 Replace_Self_Reference (N);
3102 -- If the target of the aggregate is class-wide, we must convert it
3103 -- to the actual type of the aggregate, so that the proper components
3104 -- are visible. We know already that the types are compatible.
3106 if Present (Etype (Lhs))
3107 and then Is_Class_Wide_Type (Etype (Lhs))
3109 Target := Unchecked_Convert_To (Typ, Lhs);
3114 -- Deal with the ancestor part of extension aggregates or with the
3115 -- discriminants of the root type.
3117 if Nkind (N) = N_Extension_Aggregate then
3119 Ancestor : constant Node_Id := Ancestor_Part (N);
3124 -- If the ancestor part is a subtype mark "T", we generate
3126 -- init-proc (T (tmp)); if T is constrained and
3127 -- init-proc (S (tmp)); where S applies an appropriate
3128 -- constraint if T is unconstrained
3130 if Is_Entity_Name (Ancestor)
3131 and then Is_Type (Entity (Ancestor))
3133 Ancestor_Is_Subtype_Mark := True;
3135 if Is_Constrained (Entity (Ancestor)) then
3136 Init_Typ := Entity (Ancestor);
3138 -- For an ancestor part given by an unconstrained type mark,
3139 -- create a subtype constrained by appropriate corresponding
3140 -- discriminant values coming from either associations of the
3141 -- aggregate or a constraint on a parent type. The subtype will
3142 -- be used to generate the correct default value for the
3145 elsif Has_Discriminants (Entity (Ancestor)) then
3147 Anc_Typ : constant Entity_Id := Entity (Ancestor);
3148 Anc_Constr : constant List_Id := New_List;
3149 Discrim : Entity_Id;
3150 Disc_Value : Node_Id;
3151 New_Indic : Node_Id;
3152 Subt_Decl : Node_Id;
3155 Discrim := First_Discriminant (Anc_Typ);
3156 while Present (Discrim) loop
3157 Disc_Value := Ancestor_Discriminant_Value (Discrim);
3159 -- If no usable discriminant in ancestors, check
3160 -- whether aggregate has an explicit value for it.
3162 if No (Disc_Value) then
3164 Get_Explicit_Discriminant_Value (Discrim);
3167 Append_To (Anc_Constr, Disc_Value);
3168 Next_Discriminant (Discrim);
3172 Make_Subtype_Indication (Loc,
3173 Subtype_Mark => New_Occurrence_Of (Anc_Typ, Loc),
3175 Make_Index_Or_Discriminant_Constraint (Loc,
3176 Constraints => Anc_Constr));
3178 Init_Typ := Create_Itype (Ekind (Anc_Typ), N);
3181 Make_Subtype_Declaration (Loc,
3182 Defining_Identifier => Init_Typ,
3183 Subtype_Indication => New_Indic);
3185 -- Itypes must be analyzed with checks off Declaration
3186 -- must have a parent for proper handling of subsidiary
3189 Set_Parent (Subt_Decl, N);
3190 Analyze (Subt_Decl, Suppress => All_Checks);
3194 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
3195 Set_Assignment_OK (Ref);
3197 if not Is_Interface (Init_Typ) then
3199 Build_Initialization_Call (Loc,
3202 In_Init_Proc => Within_Init_Proc,
3203 With_Default_Init => Has_Default_Init_Comps (N)
3205 Has_Task (Base_Type (Init_Typ))));
3207 if Is_Constrained (Entity (Ancestor))
3208 and then Has_Discriminants (Entity (Ancestor))
3210 Check_Ancestor_Discriminants (Entity (Ancestor));
3214 -- Handle calls to C++ constructors
3216 elsif Is_CPP_Constructor_Call (Ancestor) then
3217 Init_Typ := Etype (Ancestor);
3218 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
3219 Set_Assignment_OK (Ref);
3222 Build_Initialization_Call (Loc,
3225 In_Init_Proc => Within_Init_Proc,
3226 With_Default_Init => Has_Default_Init_Comps (N),
3227 Constructor_Ref => Ancestor));
3229 -- Ada 2005 (AI-287): If the ancestor part is an aggregate of
3230 -- limited type, a recursive call expands the ancestor. Note that
3231 -- in the limited case, the ancestor part must be either a
3232 -- function call (possibly qualified, or wrapped in an unchecked
3233 -- conversion) or aggregate (definitely qualified).
3235 -- The ancestor part can also be a function call (that may be
3236 -- transformed into an explicit dereference) or a qualification
3239 elsif Is_Limited_Type (Etype (Ancestor))
3240 and then Nkind_In (Unqualify (Ancestor), N_Aggregate,
3241 N_Extension_Aggregate)
3243 Ancestor_Is_Expression := True;
3245 -- Set up finalization data for enclosing record, because
3246 -- controlled subcomponents of the ancestor part will be
3249 Generate_Finalization_Actions;
3252 Build_Record_Aggr_Code
3253 (N => Unqualify (Ancestor),
3254 Typ => Etype (Unqualify (Ancestor)),
3257 -- If the ancestor part is an expression "E", we generate
3261 -- In Ada 2005, this includes the case of a (possibly qualified)
3262 -- limited function call. The assignment will turn into a
3263 -- build-in-place function call (for further details, see
3264 -- Make_Build_In_Place_Call_In_Assignment).
3267 Ancestor_Is_Expression := True;
3268 Init_Typ := Etype (Ancestor);
3270 -- If the ancestor part is an aggregate, force its full
3271 -- expansion, which was delayed.
3273 if Nkind_In (Unqualify (Ancestor), N_Aggregate,
3274 N_Extension_Aggregate)
3276 Set_Analyzed (Ancestor, False);
3277 Set_Analyzed (Expression (Ancestor), False);
3280 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
3281 Set_Assignment_OK (Ref);
3283 -- Make the assignment without usual controlled actions, since
3284 -- we only want to Adjust afterwards, but not to Finalize
3285 -- beforehand. Add manual Adjust when necessary.
3287 Assign := New_List (
3288 Make_OK_Assignment_Statement (Loc,
3290 Expression => Ancestor));
3291 Set_No_Ctrl_Actions (First (Assign));
3293 -- Assign the tag now to make sure that the dispatching call in
3294 -- the subsequent deep_adjust works properly (unless
3295 -- Tagged_Type_Expansion where tags are implicit).
3297 if Tagged_Type_Expansion then
3299 Make_OK_Assignment_Statement (Loc,
3301 Make_Selected_Component (Loc,
3302 Prefix => New_Copy_Tree (Target),
3305 (First_Tag_Component (Base_Type (Typ)), Loc)),
3308 Unchecked_Convert_To (RTE (RE_Tag),
3311 (Access_Disp_Table (Base_Type (Typ)))),
3314 Set_Assignment_OK (Name (Instr));
3315 Append_To (Assign, Instr);
3317 -- Ada 2005 (AI-251): If tagged type has progenitors we must
3318 -- also initialize tags of the secondary dispatch tables.
3320 if Has_Interfaces (Base_Type (Typ)) then
3322 (Typ => Base_Type (Typ),
3324 Stmts_List => Assign);
3328 -- Call Adjust manually
3330 if Needs_Finalization (Etype (Ancestor))
3331 and then not Is_Limited_Type (Etype (Ancestor))
3335 (Obj_Ref => New_Copy_Tree (Ref),
3336 Typ => Etype (Ancestor));
3338 -- Guard against a missing [Deep_]Adjust when the ancestor
3339 -- type was not properly frozen.
3341 if Present (Adj_Call) then
3342 Append_To (Assign, Adj_Call);
3347 Make_Unsuppress_Block (Loc, Name_Discriminant_Check, Assign));
3349 if Has_Discriminants (Init_Typ) then
3350 Check_Ancestor_Discriminants (Init_Typ);
3355 -- Generate assignments of hidden discriminants. If the base type is
3356 -- an unchecked union, the discriminants are unknown to the back-end
3357 -- and absent from a value of the type, so assignments for them are
3360 if Has_Discriminants (Typ)
3361 and then not Is_Unchecked_Union (Base_Type (Typ))
3363 Init_Hidden_Discriminants (Typ, L);
3366 -- Normal case (not an extension aggregate)
3369 -- Generate the discriminant expressions, component by component.
3370 -- If the base type is an unchecked union, the discriminants are
3371 -- unknown to the back-end and absent from a value of the type, so
3372 -- assignments for them are not emitted.
3374 if Has_Discriminants (Typ)
3375 and then not Is_Unchecked_Union (Base_Type (Typ))
3377 Init_Hidden_Discriminants (Typ, L);
3379 -- Generate discriminant init values for the visible discriminants
3381 Init_Visible_Discriminants;
3383 if Is_Derived_Type (N_Typ) then
3384 Init_Stored_Discriminants;
3389 -- For CPP types we generate an implicit call to the C++ default
3390 -- constructor to ensure the proper initialization of the _Tag
3393 if Is_CPP_Class (Root_Type (Typ)) and then CPP_Num_Prims (Typ) > 0 then
3394 Invoke_Constructor : declare
3395 CPP_Parent : constant Entity_Id := Enclosing_CPP_Parent (Typ);
3397 procedure Invoke_IC_Proc (T : Entity_Id);
3398 -- Recursive routine used to climb to parents. Required because
3399 -- parents must be initialized before descendants to ensure
3400 -- propagation of inherited C++ slots.
3402 --------------------
3403 -- Invoke_IC_Proc --
3404 --------------------
3406 procedure Invoke_IC_Proc (T : Entity_Id) is
3408 -- Avoid generating extra calls. Initialization required
3409 -- only for types defined from the level of derivation of
3410 -- type of the constructor and the type of the aggregate.
3412 if T = CPP_Parent then
3416 Invoke_IC_Proc (Etype (T));
3418 -- Generate call to the IC routine
3420 if Present (CPP_Init_Proc (T)) then
3422 Make_Procedure_Call_Statement (Loc,
3423 Name => New_Occurrence_Of (CPP_Init_Proc (T), Loc)));
3427 -- Start of processing for Invoke_Constructor
3430 -- Implicit invocation of the C++ constructor
3432 if Nkind (N) = N_Aggregate then
3434 Make_Procedure_Call_Statement (Loc,
3436 New_Occurrence_Of (Base_Init_Proc (CPP_Parent), Loc),
3437 Parameter_Associations => New_List (
3438 Unchecked_Convert_To (CPP_Parent,
3439 New_Copy_Tree (Lhs)))));
3442 Invoke_IC_Proc (Typ);
3443 end Invoke_Constructor;
3446 -- Generate the assignments, component by component
3448 -- tmp.comp1 := Expr1_From_Aggr;
3449 -- tmp.comp2 := Expr2_From_Aggr;
3452 Comp := First (Component_Associations (N));
3453 while Present (Comp) loop
3454 Selector := Entity (First (Choices (Comp)));
3458 if Is_CPP_Constructor_Call (Expression (Comp)) then
3460 Build_Initialization_Call (Loc,
3462 Make_Selected_Component (Loc,
3463 Prefix => New_Copy_Tree (Target),
3464 Selector_Name => New_Occurrence_Of (Selector, Loc)),
3465 Typ => Etype (Selector),
3467 With_Default_Init => True,
3468 Constructor_Ref => Expression (Comp)));
3470 -- Ada 2005 (AI-287): For each default-initialized component generate
3471 -- a call to the corresponding IP subprogram if available.
3473 elsif Box_Present (Comp)
3474 and then Has_Non_Null_Base_Init_Proc (Etype (Selector))
3476 if Ekind (Selector) /= E_Discriminant then
3477 Generate_Finalization_Actions;
3480 -- Ada 2005 (AI-287): If the component type has tasks then
3481 -- generate the activation chain and master entities (except
3482 -- in case of an allocator because in that case these entities
3483 -- are generated by Build_Task_Allocate_Block_With_Init_Stmts).
3486 Ctype : constant Entity_Id := Etype (Selector);
3487 Inside_Allocator : Boolean := False;
3488 P : Node_Id := Parent (N);
3491 if Is_Task_Type (Ctype) or else Has_Task (Ctype) then
3492 while Present (P) loop
3493 if Nkind (P) = N_Allocator then
3494 Inside_Allocator := True;
3501 if not Inside_Init_Proc and not Inside_Allocator then
3502 Build_Activation_Chain_Entity (N);
3508 Build_Initialization_Call (Loc,
3509 Id_Ref => Make_Selected_Component (Loc,
3510 Prefix => New_Copy_Tree (Target),
3512 New_Occurrence_Of (Selector, Loc)),
3513 Typ => Etype (Selector),
3515 With_Default_Init => True));
3517 -- Prepare for component assignment
3519 elsif Ekind (Selector) /= E_Discriminant
3520 or else Nkind (N) = N_Extension_Aggregate
3522 -- All the discriminants have now been assigned
3524 -- This is now a good moment to initialize and attach all the
3525 -- controllers. Their position may depend on the discriminants.
3527 if Ekind (Selector) /= E_Discriminant then
3528 Generate_Finalization_Actions;
3531 Comp_Type := Underlying_Type (Etype (Selector));
3533 Make_Selected_Component (Loc,
3534 Prefix => New_Copy_Tree (Target),
3535 Selector_Name => New_Occurrence_Of (Selector, Loc));
3537 if Nkind (Expression (Comp)) = N_Qualified_Expression then
3538 Expr_Q := Expression (Expression (Comp));
3540 Expr_Q := Expression (Comp);
3543 -- Now either create the assignment or generate the code for the
3544 -- inner aggregate top-down.
3546 if Is_Delayed_Aggregate (Expr_Q) then
3548 -- We have the following case of aggregate nesting inside
3549 -- an object declaration:
3551 -- type Arr_Typ is array (Integer range <>) of ...;
3553 -- type Rec_Typ (...) is record
3554 -- Obj_Arr_Typ : Arr_Typ (A .. B);
3557 -- Obj_Rec_Typ : Rec_Typ := (...,
3558 -- Obj_Arr_Typ => (X => (...), Y => (...)));
3560 -- The length of the ranges of the aggregate and Obj_Add_Typ
3561 -- are equal (B - A = Y - X), but they do not coincide (X /=
3562 -- A and B /= Y). This case requires array sliding which is
3563 -- performed in the following manner:
3565 -- subtype Arr_Sub is Arr_Typ (X .. Y);
3567 -- Temp (X) := (...);
3569 -- Temp (Y) := (...);
3570 -- Obj_Rec_Typ.Obj_Arr_Typ := Temp;
3572 if Ekind (Comp_Type) = E_Array_Subtype
3573 and then Is_Int_Range_Bounds (Aggregate_Bounds (Expr_Q))
3574 and then Is_Int_Range_Bounds (First_Index (Comp_Type))
3576 Compatible_Int_Bounds
3577 (Agg_Bounds => Aggregate_Bounds (Expr_Q),
3578 Typ_Bounds => First_Index (Comp_Type))
3580 -- Create the array subtype with bounds equal to those of
3581 -- the corresponding aggregate.
3584 SubE : constant Entity_Id := Make_Temporary (Loc, 'T');
3586 SubD : constant Node_Id :=
3587 Make_Subtype_Declaration (Loc,
3588 Defining_Identifier => SubE,
3589 Subtype_Indication =>
3590 Make_Subtype_Indication (Loc,
3592 New_Occurrence_Of (Etype (Comp_Type), Loc),
3594 Make_Index_Or_Discriminant_Constraint
3596 Constraints => New_List (
3598 (Aggregate_Bounds (Expr_Q))))));
3600 -- Create a temporary array of the above subtype which
3601 -- will be used to capture the aggregate assignments.
3603 TmpE : constant Entity_Id := Make_Temporary (Loc, 'A', N);
3605 TmpD : constant Node_Id :=
3606 Make_Object_Declaration (Loc,
3607 Defining_Identifier => TmpE,
3608 Object_Definition => New_Occurrence_Of (SubE, Loc));
3611 Set_No_Initialization (TmpD);
3612 Append_To (L, SubD);
3613 Append_To (L, TmpD);
3615 -- Expand aggregate into assignments to the temp array
3618 Late_Expansion (Expr_Q, Comp_Type,
3619 New_Occurrence_Of (TmpE, Loc)));
3624 Make_Assignment_Statement (Loc,
3625 Name => New_Copy_Tree (Comp_Expr),
3626 Expression => New_Occurrence_Of (TmpE, Loc)));
3629 -- Normal case (sliding not required)
3633 Late_Expansion (Expr_Q, Comp_Type, Comp_Expr));
3636 -- Expr_Q is not delayed aggregate
3639 if Has_Discriminants (Typ) then
3640 Replace_Discriminants (Expr_Q);
3642 -- If the component is an array type that depends on
3643 -- discriminants, and the expression is a single Others
3644 -- clause, create an explicit subtype for it because the
3645 -- backend has troubles recovering the actual bounds.
3647 if Nkind (Expr_Q) = N_Aggregate
3648 and then Is_Array_Type (Comp_Type)
3649 and then Present (Component_Associations (Expr_Q))
3652 Assoc : constant Node_Id :=
3653 First (Component_Associations (Expr_Q));
3657 if Nkind (First (Choices (Assoc))) = N_Others_Choice
3660 Build_Actual_Subtype_Of_Component
3661 (Comp_Type, Comp_Expr);
3663 -- If the component type does not in fact depend on
3664 -- discriminants, the subtype declaration is empty.
3666 if Present (Decl) then
3667 Append_To (L, Decl);
3668 Set_Etype (Comp_Expr, Defining_Entity (Decl));
3675 if Modify_Tree_For_C
3676 and then Nkind (Expr_Q) = N_Aggregate
3677 and then Is_Array_Type (Etype (Expr_Q))
3678 and then Present (First_Index (Etype (Expr_Q)))
3681 Expr_Q_Type : constant Node_Id := Etype (Expr_Q);
3684 Build_Array_Aggr_Code
3686 Ctype => Component_Type (Expr_Q_Type),
3687 Index => First_Index (Expr_Q_Type),
3690 Is_Scalar_Type (Component_Type (Expr_Q_Type))));
3694 -- Handle an initialization expression of a controlled type
3695 -- in case it denotes a function call. In general such a
3696 -- scenario will produce a transient scope, but this will
3697 -- lead to wrong order of initialization, adjustment, and
3698 -- finalization in the context of aggregates.
3700 -- Target.Comp := Ctrl_Func_Call;
3703 -- Trans_Obj : ... := Ctrl_Func_Call; -- object
3704 -- Target.Comp := Trans_Obj;
3705 -- Finalize (Trans_Obj);
3707 -- Target.Comp._tag := ...;
3708 -- Adjust (Target.Comp);
3710 -- In the example above, the call to Finalize occurs too
3711 -- early and as a result it may leave the record component
3712 -- in a bad state. Finalization of the transient object
3713 -- should really happen after adjustment.
3715 -- To avoid this scenario, perform in-place side-effect
3716 -- removal of the function call. This eliminates the
3717 -- transient property of the function result and ensures
3718 -- correct order of actions.
3720 -- Res : ... := Ctrl_Func_Call;
3721 -- Target.Comp := Res;
3722 -- Target.Comp._tag := ...;
3723 -- Adjust (Target.Comp);
3726 if Needs_Finalization (Comp_Type)
3727 and then Nkind (Expr_Q) /= N_Aggregate
3729 Initialize_Ctrl_Record_Component
3730 (Rec_Comp => Comp_Expr,
3731 Comp_Typ => Etype (Selector),
3732 Init_Expr => Expr_Q,
3735 -- Otherwise perform single component initialization
3738 Initialize_Record_Component
3739 (Rec_Comp => Comp_Expr,
3740 Comp_Typ => Etype (Selector),
3741 Init_Expr => Expr_Q,
3747 -- comment would be good here ???
3749 elsif Ekind (Selector) = E_Discriminant
3750 and then Nkind (N) /= N_Extension_Aggregate
3751 and then Nkind (Parent (N)) = N_Component_Association
3752 and then Is_Constrained (Typ)
3754 -- We must check that the discriminant value imposed by the
3755 -- context is the same as the value given in the subaggregate,
3756 -- because after the expansion into assignments there is no
3757 -- record on which to perform a regular discriminant check.
3764 D_Val := First_Elmt (Discriminant_Constraint (Typ));
3765 Disc := First_Discriminant (Typ);
3766 while Chars (Disc) /= Chars (Selector) loop
3767 Next_Discriminant (Disc);
3771 pragma Assert (Present (D_Val));
3773 -- This check cannot performed for components that are
3774 -- constrained by a current instance, because this is not a
3775 -- value that can be compared with the actual constraint.
3777 if Nkind (Node (D_Val)) /= N_Attribute_Reference
3778 or else not Is_Entity_Name (Prefix (Node (D_Val)))
3779 or else not Is_Type (Entity (Prefix (Node (D_Val))))
3782 Make_Raise_Constraint_Error (Loc,
3785 Left_Opnd => New_Copy_Tree (Node (D_Val)),
3786 Right_Opnd => Expression (Comp)),
3787 Reason => CE_Discriminant_Check_Failed));
3790 -- Find self-reference in previous discriminant assignment,
3791 -- and replace with proper expression.
3798 while Present (Ass) loop
3799 if Nkind (Ass) = N_Assignment_Statement
3800 and then Nkind (Name (Ass)) = N_Selected_Component
3801 and then Chars (Selector_Name (Name (Ass))) =
3805 (Ass, New_Copy_Tree (Expression (Comp)));
3818 -- If the type is tagged, the tag needs to be initialized (unless we
3819 -- are in VM-mode where tags are implicit). It is done late in the
3820 -- initialization process because in some cases, we call the init
3821 -- proc of an ancestor which will not leave out the right tag.
3823 if Ancestor_Is_Expression then
3826 -- For CPP types we generated a call to the C++ default constructor
3827 -- before the components have been initialized to ensure the proper
3828 -- initialization of the _Tag component (see above).
3830 elsif Is_CPP_Class (Typ) then
3833 elsif Is_Tagged_Type (Typ) and then Tagged_Type_Expansion then
3835 Make_OK_Assignment_Statement (Loc,
3837 Make_Selected_Component (Loc,
3838 Prefix => New_Copy_Tree (Target),
3841 (First_Tag_Component (Base_Type (Typ)), Loc)),
3844 Unchecked_Convert_To (RTE (RE_Tag),
3846 (Node (First_Elmt (Access_Disp_Table (Base_Type (Typ)))),
3849 Append_To (L, Instr);
3851 -- Ada 2005 (AI-251): If the tagged type has been derived from an
3852 -- abstract interfaces we must also initialize the tags of the
3853 -- secondary dispatch tables.
3855 if Has_Interfaces (Base_Type (Typ)) then
3857 (Typ => Base_Type (Typ),
3863 -- If the controllers have not been initialized yet (by lack of non-
3864 -- discriminant components), let's do it now.
3866 Generate_Finalization_Actions;
3869 end Build_Record_Aggr_Code;
3871 ---------------------------------------
3872 -- Collect_Initialization_Statements --
3873 ---------------------------------------
3875 procedure Collect_Initialization_Statements
3878 Node_After : Node_Id)
3880 Loc : constant Source_Ptr := Sloc (N);
3881 Init_Actions : constant List_Id := New_List;
3882 Init_Node : Node_Id;
3883 Comp_Stmt : Node_Id;
3886 -- Nothing to do if Obj is already frozen, as in this case we known we
3887 -- won't need to move the initialization statements about later on.
3889 if Is_Frozen (Obj) then
3894 while Next (Init_Node) /= Node_After loop
3895 Append_To (Init_Actions, Remove_Next (Init_Node));
3898 if not Is_Empty_List (Init_Actions) then
3899 Comp_Stmt := Make_Compound_Statement (Loc, Actions => Init_Actions);
3900 Insert_Action_After (Init_Node, Comp_Stmt);
3901 Set_Initialization_Statements (Obj, Comp_Stmt);
3903 end Collect_Initialization_Statements;
3905 -------------------------------
3906 -- Convert_Aggr_In_Allocator --
3907 -------------------------------
3909 procedure Convert_Aggr_In_Allocator
3914 Loc : constant Source_Ptr := Sloc (Aggr);
3915 Typ : constant Entity_Id := Etype (Aggr);
3916 Temp : constant Entity_Id := Defining_Identifier (Decl);
3918 Occ : constant Node_Id :=
3919 Unchecked_Convert_To (Typ,
3920 Make_Explicit_Dereference (Loc, New_Occurrence_Of (Temp, Loc)));
3923 if Is_Array_Type (Typ) then
3924 Convert_Array_Aggr_In_Allocator (Decl, Aggr, Occ);
3926 elsif Has_Default_Init_Comps (Aggr) then
3928 L : constant List_Id := New_List;
3929 Init_Stmts : List_Id;
3932 Init_Stmts := Late_Expansion (Aggr, Typ, Occ);
3934 if Has_Task (Typ) then
3935 Build_Task_Allocate_Block_With_Init_Stmts (L, Aggr, Init_Stmts);
3936 Insert_Actions (Alloc, L);
3938 Insert_Actions (Alloc, Init_Stmts);
3943 Insert_Actions (Alloc, Late_Expansion (Aggr, Typ, Occ));
3945 end Convert_Aggr_In_Allocator;
3947 --------------------------------
3948 -- Convert_Aggr_In_Assignment --
3949 --------------------------------
3951 procedure Convert_Aggr_In_Assignment (N : Node_Id) is
3952 Aggr : Node_Id := Expression (N);
3953 Typ : constant Entity_Id := Etype (Aggr);
3954 Occ : constant Node_Id := New_Copy_Tree (Name (N));
3957 if Nkind (Aggr) = N_Qualified_Expression then
3958 Aggr := Expression (Aggr);
3961 Insert_Actions_After (N, Late_Expansion (Aggr, Typ, Occ));
3962 end Convert_Aggr_In_Assignment;
3964 ---------------------------------
3965 -- Convert_Aggr_In_Object_Decl --
3966 ---------------------------------
3968 procedure Convert_Aggr_In_Object_Decl (N : Node_Id) is
3969 Obj : constant Entity_Id := Defining_Identifier (N);
3970 Aggr : Node_Id := Expression (N);
3971 Loc : constant Source_Ptr := Sloc (Aggr);
3972 Typ : constant Entity_Id := Etype (Aggr);
3973 Occ : constant Node_Id := New_Occurrence_Of (Obj, Loc);
3975 function Discriminants_Ok return Boolean;
3976 -- If the object type is constrained, the discriminants in the
3977 -- aggregate must be checked against the discriminants of the subtype.
3978 -- This cannot be done using Apply_Discriminant_Checks because after
3979 -- expansion there is no aggregate left to check.
3981 ----------------------
3982 -- Discriminants_Ok --
3983 ----------------------
3985 function Discriminants_Ok return Boolean is
3986 Cond : Node_Id := Empty;
3995 D := First_Discriminant (Typ);
3996 Disc1 := First_Elmt (Discriminant_Constraint (Typ));
3997 Disc2 := First_Elmt (Discriminant_Constraint (Etype (Obj)));
3998 while Present (Disc1) and then Present (Disc2) loop
3999 Val1 := Node (Disc1);
4000 Val2 := Node (Disc2);
4002 if not Is_OK_Static_Expression (Val1)
4003 or else not Is_OK_Static_Expression (Val2)
4005 Check := Make_Op_Ne (Loc,
4006 Left_Opnd => Duplicate_Subexpr (Val1),
4007 Right_Opnd => Duplicate_Subexpr (Val2));
4013 Cond := Make_Or_Else (Loc,
4015 Right_Opnd => Check);
4018 elsif Expr_Value (Val1) /= Expr_Value (Val2) then
4019 Apply_Compile_Time_Constraint_Error (Aggr,
4020 Msg => "incorrect value for discriminant&??",
4021 Reason => CE_Discriminant_Check_Failed,
4026 Next_Discriminant (D);
4031 -- If any discriminant constraint is non-static, emit a check
4033 if Present (Cond) then
4035 Make_Raise_Constraint_Error (Loc,
4037 Reason => CE_Discriminant_Check_Failed));
4041 end Discriminants_Ok;
4043 -- Start of processing for Convert_Aggr_In_Object_Decl
4046 Set_Assignment_OK (Occ);
4048 if Nkind (Aggr) = N_Qualified_Expression then
4049 Aggr := Expression (Aggr);
4052 if Has_Discriminants (Typ)
4053 and then Typ /= Etype (Obj)
4054 and then Is_Constrained (Etype (Obj))
4055 and then not Discriminants_Ok
4060 -- If the context is an extended return statement, it has its own
4061 -- finalization machinery (i.e. works like a transient scope) and
4062 -- we do not want to create an additional one, because objects on
4063 -- the finalization list of the return must be moved to the caller's
4064 -- finalization list to complete the return.
4066 -- However, if the aggregate is limited, it is built in place, and the
4067 -- controlled components are not assigned to intermediate temporaries
4068 -- so there is no need for a transient scope in this case either.
4070 if Requires_Transient_Scope (Typ)
4071 and then Ekind (Current_Scope) /= E_Return_Statement
4072 and then not Is_Limited_Type (Typ)
4074 Establish_Transient_Scope
4077 Is_Controlled (Typ) or else Has_Controlled_Component (Typ));
4081 Node_After : constant Node_Id := Next (N);
4083 Insert_Actions_After (N, Late_Expansion (Aggr, Typ, Occ));
4084 Collect_Initialization_Statements (Obj, N, Node_After);
4086 Set_No_Initialization (N);
4087 Initialize_Discriminants (N, Typ);
4088 end Convert_Aggr_In_Object_Decl;
4090 -------------------------------------
4091 -- Convert_Array_Aggr_In_Allocator --
4092 -------------------------------------
4094 procedure Convert_Array_Aggr_In_Allocator
4099 Aggr_Code : List_Id;
4100 Typ : constant Entity_Id := Etype (Aggr);
4101 Ctyp : constant Entity_Id := Component_Type (Typ);
4104 -- The target is an explicit dereference of the allocated object.
4105 -- Generate component assignments to it, as for an aggregate that
4106 -- appears on the right-hand side of an assignment statement.
4109 Build_Array_Aggr_Code (Aggr,
4111 Index => First_Index (Typ),
4113 Scalar_Comp => Is_Scalar_Type (Ctyp));
4115 Insert_Actions_After (Decl, Aggr_Code);
4116 end Convert_Array_Aggr_In_Allocator;
4118 ----------------------------
4119 -- Convert_To_Assignments --
4120 ----------------------------
4122 procedure Convert_To_Assignments (N : Node_Id; Typ : Entity_Id) is
4123 Loc : constant Source_Ptr := Sloc (N);
4127 Aggr_Code : List_Id;
4129 Target_Expr : Node_Id;
4130 Parent_Kind : Node_Kind;
4131 Unc_Decl : Boolean := False;
4132 Parent_Node : Node_Id;
4135 pragma Assert (not Is_Static_Dispatch_Table_Aggregate (N));
4136 pragma Assert (Is_Record_Type (Typ));
4138 Parent_Node := Parent (N);
4139 Parent_Kind := Nkind (Parent_Node);
4141 if Parent_Kind = N_Qualified_Expression then
4143 -- Check if we are in a unconstrained declaration because in this
4144 -- case the current delayed expansion mechanism doesn't work when
4145 -- the declared object size depend on the initializing expr.
4147 Parent_Node := Parent (Parent_Node);
4148 Parent_Kind := Nkind (Parent_Node);
4150 if Parent_Kind = N_Object_Declaration then
4152 not Is_Entity_Name (Object_Definition (Parent_Node))
4153 or else Has_Discriminants
4154 (Entity (Object_Definition (Parent_Node)))
4155 or else Is_Class_Wide_Type
4156 (Entity (Object_Definition (Parent_Node)));
4160 -- Just set the Delay flag in the cases where the transformation will be
4161 -- done top down from above.
4165 -- Internal aggregate (transformed when expanding the parent)
4167 or else Parent_Kind = N_Aggregate
4168 or else Parent_Kind = N_Extension_Aggregate
4169 or else Parent_Kind = N_Component_Association
4171 -- Allocator (see Convert_Aggr_In_Allocator)
4173 or else Parent_Kind = N_Allocator
4175 -- Object declaration (see Convert_Aggr_In_Object_Decl)
4177 or else (Parent_Kind = N_Object_Declaration and then not Unc_Decl)
4179 -- Safe assignment (see Convert_Aggr_Assignments). So far only the
4180 -- assignments in init procs are taken into account.
4182 or else (Parent_Kind = N_Assignment_Statement
4183 and then Inside_Init_Proc)
4185 -- (Ada 2005) An inherently limited type in a return statement, which
4186 -- will be handled in a build-in-place fashion, and may be rewritten
4187 -- as an extended return and have its own finalization machinery.
4188 -- In the case of a simple return, the aggregate needs to be delayed
4189 -- until the scope for the return statement has been created, so
4190 -- that any finalization chain will be associated with that scope.
4191 -- For extended returns, we delay expansion to avoid the creation
4192 -- of an unwanted transient scope that could result in premature
4193 -- finalization of the return object (which is built in place
4194 -- within the caller's scope).
4197 (Is_Limited_View (Typ)
4199 (Nkind (Parent (Parent_Node)) = N_Extended_Return_Statement
4200 or else Nkind (Parent_Node) = N_Simple_Return_Statement))
4202 Set_Expansion_Delayed (N);
4206 -- Otherwise, if a transient scope is required, create it now. If we
4207 -- are within an initialization procedure do not create such, because
4208 -- the target of the assignment must not be declared within a local
4209 -- block, and because cleanup will take place on return from the
4210 -- initialization procedure.
4212 -- Should the condition be more restrictive ???
4214 if Requires_Transient_Scope (Typ) and then not Inside_Init_Proc then
4215 Establish_Transient_Scope (N, Sec_Stack => Needs_Finalization (Typ));
4218 -- If the aggregate is nonlimited, create a temporary. If it is limited
4219 -- and context is an assignment, this is a subaggregate for an enclosing
4220 -- aggregate being expanded. It must be built in place, so use target of
4221 -- the current assignment.
4223 if Is_Limited_Type (Typ)
4224 and then Nkind (Parent (N)) = N_Assignment_Statement
4226 Target_Expr := New_Copy_Tree (Name (Parent (N)));
4227 Insert_Actions (Parent (N),
4228 Build_Record_Aggr_Code (N, Typ, Target_Expr));
4229 Rewrite (Parent (N), Make_Null_Statement (Loc));
4232 Temp := Make_Temporary (Loc, 'A', N);
4234 -- If the type inherits unknown discriminants, use the view with
4235 -- known discriminants if available.
4237 if Has_Unknown_Discriminants (Typ)
4238 and then Present (Underlying_Record_View (Typ))
4240 T := Underlying_Record_View (Typ);
4246 Make_Object_Declaration (Loc,
4247 Defining_Identifier => Temp,
4248 Object_Definition => New_Occurrence_Of (T, Loc));
4250 Set_No_Initialization (Instr);
4251 Insert_Action (N, Instr);
4252 Initialize_Discriminants (Instr, T);
4254 Target_Expr := New_Occurrence_Of (Temp, Loc);
4255 Aggr_Code := Build_Record_Aggr_Code (N, T, Target_Expr);
4257 -- Save the last assignment statement associated with the aggregate
4258 -- when building a controlled object. This reference is utilized by
4259 -- the finalization machinery when marking an object as successfully
4262 if Needs_Finalization (T) then
4263 Set_Last_Aggregate_Assignment (Temp, Last (Aggr_Code));
4266 Insert_Actions (N, Aggr_Code);
4267 Rewrite (N, New_Occurrence_Of (Temp, Loc));
4268 Analyze_And_Resolve (N, T);
4270 end Convert_To_Assignments;
4272 ---------------------------
4273 -- Convert_To_Positional --
4274 ---------------------------
4276 procedure Convert_To_Positional
4278 Max_Others_Replicate : Nat := 5;
4279 Handle_Bit_Packed : Boolean := False)
4281 Typ : constant Entity_Id := Etype (N);
4283 Static_Components : Boolean := True;
4285 procedure Check_Static_Components;
4286 -- Check whether all components of the aggregate are compile-time known
4287 -- values, and can be passed as is to the back-end without further
4289 -- An Iterated_Component_Association is treated as non-static, but there
4290 -- are possibilities for optimization here.
4295 Ixb : Node_Id) return Boolean;
4296 -- Convert the aggregate into a purely positional form if possible. On
4297 -- entry the bounds of all dimensions are known to be static, and the
4298 -- total number of components is safe enough to expand.
4300 function Is_Flat (N : Node_Id; Dims : Int) return Boolean;
4301 -- Return True iff the array N is flat (which is not trivial in the case
4302 -- of multidimensional aggregates).
4304 -----------------------------
4305 -- Check_Static_Components --
4306 -----------------------------
4308 -- Could use some comments in this body ???
4310 procedure Check_Static_Components is
4314 Static_Components := True;
4316 if Nkind (N) = N_String_Literal then
4319 elsif Present (Expressions (N)) then
4320 Expr := First (Expressions (N));
4321 while Present (Expr) loop
4322 if Nkind (Expr) /= N_Aggregate
4323 or else not Compile_Time_Known_Aggregate (Expr)
4324 or else Expansion_Delayed (Expr)
4326 Static_Components := False;
4334 if Nkind (N) = N_Aggregate
4335 and then Present (Component_Associations (N))
4337 Expr := First (Component_Associations (N));
4338 while Present (Expr) loop
4339 if Nkind_In (Expression (Expr), N_Integer_Literal,
4344 elsif Is_Entity_Name (Expression (Expr))
4345 and then Present (Entity (Expression (Expr)))
4346 and then Ekind (Entity (Expression (Expr))) =
4347 E_Enumeration_Literal
4351 elsif Nkind (Expression (Expr)) /= N_Aggregate
4352 or else not Compile_Time_Known_Aggregate (Expression (Expr))
4353 or else Expansion_Delayed (Expression (Expr))
4354 or else Nkind (Expr) = N_Iterated_Component_Association
4356 Static_Components := False;
4363 end Check_Static_Components;
4372 Ixb : Node_Id) return Boolean
4374 Loc : constant Source_Ptr := Sloc (N);
4375 Blo : constant Node_Id := Type_Low_Bound (Etype (Ixb));
4376 Lo : constant Node_Id := Type_Low_Bound (Etype (Ix));
4377 Hi : constant Node_Id := Type_High_Bound (Etype (Ix));
4381 Others_Present : Boolean := False;
4384 if Nkind (Original_Node (N)) = N_String_Literal then
4388 if not Compile_Time_Known_Value (Lo)
4389 or else not Compile_Time_Known_Value (Hi)
4394 Lov := Expr_Value (Lo);
4395 Hiv := Expr_Value (Hi);
4397 -- Check if there is an others choice
4399 if Present (Component_Associations (N)) then
4405 Assoc := First (Component_Associations (N));
4406 while Present (Assoc) loop
4408 -- If this is a box association, flattening is in general
4409 -- not possible because at this point we cannot tell if the
4410 -- default is static or even exists.
4412 if Box_Present (Assoc) then
4415 elsif Nkind (Assoc) = N_Iterated_Component_Association then
4419 Choice := First (Choice_List (Assoc));
4421 while Present (Choice) loop
4422 if Nkind (Choice) = N_Others_Choice then
4423 Others_Present := True;
4434 -- If the low bound is not known at compile time and others is not
4435 -- present we can proceed since the bounds can be obtained from the
4439 or else (not Compile_Time_Known_Value (Blo) and then Others_Present)
4444 -- Determine if set of alternatives is suitable for conversion and
4445 -- build an array containing the values in sequence.
4448 Vals : array (UI_To_Int (Lov) .. UI_To_Int (Hiv))
4449 of Node_Id := (others => Empty);
4450 -- The values in the aggregate sorted appropriately
4453 -- Same data as Vals in list form
4456 -- Used to validate Max_Others_Replicate limit
4459 Num : Int := UI_To_Int (Lov);
4465 if Present (Expressions (N)) then
4466 Elmt := First (Expressions (N));
4467 while Present (Elmt) loop
4468 if Nkind (Elmt) = N_Aggregate
4469 and then Present (Next_Index (Ix))
4471 not Flatten (Elmt, Next_Index (Ix), Next_Index (Ixb))
4476 Vals (Num) := Relocate_Node (Elmt);
4483 if No (Component_Associations (N)) then
4487 Elmt := First (Component_Associations (N));
4489 if Nkind (Expression (Elmt)) = N_Aggregate then
4490 if Present (Next_Index (Ix))
4493 (Expression (Elmt), Next_Index (Ix), Next_Index (Ixb))
4499 Component_Loop : while Present (Elmt) loop
4500 Choice := First (Choice_List (Elmt));
4501 Choice_Loop : while Present (Choice) loop
4503 -- If we have an others choice, fill in the missing elements
4504 -- subject to the limit established by Max_Others_Replicate.
4506 if Nkind (Choice) = N_Others_Choice then
4509 for J in Vals'Range loop
4510 if No (Vals (J)) then
4511 Vals (J) := New_Copy_Tree (Expression (Elmt));
4512 Rep_Count := Rep_Count + 1;
4514 -- Check for maximum others replication. Note that
4515 -- we skip this test if either of the restrictions
4516 -- No_Elaboration_Code or No_Implicit_Loops is
4517 -- active, if this is a preelaborable unit or
4518 -- a predefined unit, or if the unit must be
4519 -- placed in data memory. This also ensures that
4520 -- predefined units get the same level of constant
4521 -- folding in Ada 95 and Ada 2005, where their
4522 -- categorization has changed.
4525 P : constant Entity_Id :=
4526 Cunit_Entity (Current_Sem_Unit);
4529 -- Check if duplication OK and if so continue
4532 if Restriction_Active (No_Elaboration_Code)
4533 or else Restriction_Active (No_Implicit_Loops)
4535 (Ekind (Current_Scope) = E_Package
4536 and then Static_Elaboration_Desired
4538 or else Is_Preelaborated (P)
4539 or else (Ekind (P) = E_Package_Body
4541 Is_Preelaborated (Spec_Entity (P)))
4543 Is_Predefined_File_Name
4544 (Unit_File_Name (Get_Source_Unit (P)))
4548 -- If duplication not OK, then we return False
4549 -- if the replication count is too high
4551 elsif Rep_Count > Max_Others_Replicate then
4554 -- Continue on if duplication not OK, but the
4555 -- replication count is not excessive.
4564 exit Component_Loop;
4566 -- Case of a subtype mark, identifier or expanded name
4568 elsif Is_Entity_Name (Choice)
4569 and then Is_Type (Entity (Choice))
4571 Lo := Type_Low_Bound (Etype (Choice));
4572 Hi := Type_High_Bound (Etype (Choice));
4574 -- Case of subtype indication
4576 elsif Nkind (Choice) = N_Subtype_Indication then
4577 Lo := Low_Bound (Range_Expression (Constraint (Choice)));
4578 Hi := High_Bound (Range_Expression (Constraint (Choice)));
4582 elsif Nkind (Choice) = N_Range then
4583 Lo := Low_Bound (Choice);
4584 Hi := High_Bound (Choice);
4586 -- Normal subexpression case
4588 else pragma Assert (Nkind (Choice) in N_Subexpr);
4589 if not Compile_Time_Known_Value (Choice) then
4593 Choice_Index := UI_To_Int (Expr_Value (Choice));
4595 if Choice_Index in Vals'Range then
4596 Vals (Choice_Index) :=
4597 New_Copy_Tree (Expression (Elmt));
4600 -- Choice is statically out-of-range, will be
4601 -- rewritten to raise Constraint_Error.
4609 -- Range cases merge with Lo,Hi set
4611 if not Compile_Time_Known_Value (Lo)
4613 not Compile_Time_Known_Value (Hi)
4618 for J in UI_To_Int (Expr_Value (Lo)) ..
4619 UI_To_Int (Expr_Value (Hi))
4621 Vals (J) := New_Copy_Tree (Expression (Elmt));
4627 end loop Choice_Loop;
4630 end loop Component_Loop;
4632 -- If we get here the conversion is possible
4635 for J in Vals'Range loop
4636 Append (Vals (J), Vlist);
4639 Rewrite (N, Make_Aggregate (Loc, Expressions => Vlist));
4640 Set_Aggregate_Bounds (N, Aggregate_Bounds (Original_Node (N)));
4649 function Is_Flat (N : Node_Id; Dims : Int) return Boolean is
4656 elsif Nkind (N) = N_Aggregate then
4657 if Present (Component_Associations (N)) then
4661 Elmt := First (Expressions (N));
4662 while Present (Elmt) loop
4663 if not Is_Flat (Elmt, Dims - 1) then
4677 -- Start of processing for Convert_To_Positional
4680 -- Only convert to positional when generating C in case of an
4681 -- object declaration, this is the only case where aggregates are
4684 if Modify_Tree_For_C and then not In_Object_Declaration (N) then
4688 -- Ada 2005 (AI-287): Do not convert in case of default initialized
4689 -- components because in this case will need to call the corresponding
4692 if Has_Default_Init_Comps (N) then
4696 if Is_Flat (N, Number_Dimensions (Typ)) then
4700 if Is_Bit_Packed_Array (Typ) and then not Handle_Bit_Packed then
4704 -- Do not convert to positional if controlled components are involved
4705 -- since these require special processing
4707 if Has_Controlled_Component (Typ) then
4711 Check_Static_Components;
4713 -- If the size is known, or all the components are static, try to
4714 -- build a fully positional aggregate.
4716 -- The size of the type may not be known for an aggregate with
4717 -- discriminated array components, but if the components are static
4718 -- it is still possible to verify statically that the length is
4719 -- compatible with the upper bound of the type, and therefore it is
4720 -- worth flattening such aggregates as well.
4722 -- For now the back-end expands these aggregates into individual
4723 -- assignments to the target anyway, but it is conceivable that
4724 -- it will eventually be able to treat such aggregates statically???
4726 if Aggr_Size_OK (N, Typ)
4727 and then Flatten (N, First_Index (Typ), First_Index (Base_Type (Typ)))
4729 if Static_Components then
4730 Set_Compile_Time_Known_Aggregate (N);
4731 Set_Expansion_Delayed (N, False);
4734 Analyze_And_Resolve (N, Typ);
4737 -- If Static_Elaboration_Desired has been specified, diagnose aggregates
4738 -- that will still require initialization code.
4740 if (Ekind (Current_Scope) = E_Package
4741 and then Static_Elaboration_Desired (Current_Scope))
4742 and then Nkind (Parent (N)) = N_Object_Declaration
4748 if Nkind (N) = N_Aggregate and then Present (Expressions (N)) then
4749 Expr := First (Expressions (N));
4750 while Present (Expr) loop
4751 if Nkind_In (Expr, N_Integer_Literal, N_Real_Literal)
4753 (Is_Entity_Name (Expr)
4754 and then Ekind (Entity (Expr)) = E_Enumeration_Literal)
4760 ("non-static object requires elaboration code??", N);
4767 if Present (Component_Associations (N)) then
4768 Error_Msg_N ("object requires elaboration code??", N);
4773 end Convert_To_Positional;
4775 ----------------------------
4776 -- Expand_Array_Aggregate --
4777 ----------------------------
4779 -- Array aggregate expansion proceeds as follows:
4781 -- 1. If requested we generate code to perform all the array aggregate
4782 -- bound checks, specifically
4784 -- (a) Check that the index range defined by aggregate bounds is
4785 -- compatible with corresponding index subtype.
4787 -- (b) If an others choice is present check that no aggregate
4788 -- index is outside the bounds of the index constraint.
4790 -- (c) For multidimensional arrays make sure that all subaggregates
4791 -- corresponding to the same dimension have the same bounds.
4793 -- 2. Check for packed array aggregate which can be converted to a
4794 -- constant so that the aggregate disappears completely.
4796 -- 3. Check case of nested aggregate. Generally nested aggregates are
4797 -- handled during the processing of the parent aggregate.
4799 -- 4. Check if the aggregate can be statically processed. If this is the
4800 -- case pass it as is to Gigi. Note that a necessary condition for
4801 -- static processing is that the aggregate be fully positional.
4803 -- 5. If in place aggregate expansion is possible (i.e. no need to create
4804 -- a temporary) then mark the aggregate as such and return. Otherwise
4805 -- create a new temporary and generate the appropriate initialization
4808 procedure Expand_Array_Aggregate (N : Node_Id) is
4809 Loc : constant Source_Ptr := Sloc (N);
4811 Typ : constant Entity_Id := Etype (N);
4812 Ctyp : constant Entity_Id := Component_Type (Typ);
4813 -- Typ is the correct constrained array subtype of the aggregate
4814 -- Ctyp is the corresponding component type.
4816 Aggr_Dimension : constant Pos := Number_Dimensions (Typ);
4817 -- Number of aggregate index dimensions
4819 Aggr_Low : array (1 .. Aggr_Dimension) of Node_Id;
4820 Aggr_High : array (1 .. Aggr_Dimension) of Node_Id;
4821 -- Low and High bounds of the constraint for each aggregate index
4823 Aggr_Index_Typ : array (1 .. Aggr_Dimension) of Entity_Id;
4824 -- The type of each index
4826 In_Place_Assign_OK_For_Declaration : Boolean := False;
4827 -- True if we are to generate an in place assignment for a declaration
4829 Maybe_In_Place_OK : Boolean;
4830 -- If the type is neither controlled nor packed and the aggregate
4831 -- is the expression in an assignment, assignment in place may be
4832 -- possible, provided other conditions are met on the LHS.
4834 Others_Present : array (1 .. Aggr_Dimension) of Boolean :=
4836 -- If Others_Present (J) is True, then there is an others choice in one
4837 -- of the subaggregates of N at dimension J.
4839 function Aggr_Assignment_OK_For_Backend (N : Node_Id) return Boolean;
4840 -- Returns true if an aggregate assignment can be done by the back end
4842 procedure Build_Constrained_Type (Positional : Boolean);
4843 -- If the subtype is not static or unconstrained, build a constrained
4844 -- type using the computable sizes of the aggregate and its sub-
4847 procedure Check_Bounds (Aggr_Bounds : Node_Id; Index_Bounds : Node_Id);
4848 -- Checks that the bounds of Aggr_Bounds are within the bounds defined
4851 procedure Check_Same_Aggr_Bounds (Sub_Aggr : Node_Id; Dim : Pos);
4852 -- Checks that in a multidimensional array aggregate all subaggregates
4853 -- corresponding to the same dimension have the same bounds. Sub_Aggr is
4854 -- an array subaggregate. Dim is the dimension corresponding to the
4857 procedure Compute_Others_Present (Sub_Aggr : Node_Id; Dim : Pos);
4858 -- Computes the values of array Others_Present. Sub_Aggr is the array
4859 -- subaggregate we start the computation from. Dim is the dimension
4860 -- corresponding to the subaggregate.
4862 function In_Place_Assign_OK return Boolean;
4863 -- Simple predicate to determine whether an aggregate assignment can
4864 -- be done in place, because none of the new values can depend on the
4865 -- components of the target of the assignment.
4867 procedure Others_Check (Sub_Aggr : Node_Id; Dim : Pos);
4868 -- Checks that if an others choice is present in any subaggregate, no
4869 -- aggregate index is outside the bounds of the index constraint.
4870 -- Sub_Aggr is an array subaggregate. Dim is the dimension corresponding
4871 -- to the subaggregate.
4873 function Safe_Left_Hand_Side (N : Node_Id) return Boolean;
4874 -- In addition to Maybe_In_Place_OK, in order for an aggregate to be
4875 -- built directly into the target of the assignment it must be free
4878 ------------------------------------
4879 -- Aggr_Assignment_OK_For_Backend --
4880 ------------------------------------
4882 -- Backend processing by Gigi/gcc is possible only if all the following
4883 -- conditions are met:
4885 -- 1. N consists of a single OTHERS choice, possibly recursively
4887 -- 2. The array type is not packed
4889 -- 3. The array type has no atomic components
4891 -- 4. The array type has no null ranges (the purpose of this is to
4892 -- avoid a bogus warning for an out-of-range value).
4894 -- 5. The component type is discrete
4896 -- 6. The component size is Storage_Unit or the value is of the form
4897 -- M * (1 + A**1 + A**2 + .. A**(K-1)) where A = 2**(Storage_Unit)
4898 -- and M in 1 .. A-1. This can also be viewed as K occurrences of
4899 -- the 8-bit value M, concatenated together.
4901 -- The ultimate goal is to generate a call to a fast memset routine
4902 -- specifically optimized for the target.
4904 function Aggr_Assignment_OK_For_Backend (N : Node_Id) return Boolean is
4907 Expr : Node_Id := N;
4915 -- Recurse as far as possible to find the innermost component type
4918 while Is_Array_Type (Ctyp) loop
4919 if Nkind (Expr) /= N_Aggregate
4920 or else not Is_Others_Aggregate (Expr)
4925 if Present (Packed_Array_Impl_Type (Ctyp)) then
4929 if Has_Atomic_Components (Ctyp) then
4933 Index := First_Index (Ctyp);
4934 while Present (Index) loop
4935 Get_Index_Bounds (Index, Low, High);
4937 if Is_Null_Range (Low, High) then
4944 Expr := Expression (First (Component_Associations (Expr)));
4946 for J in 1 .. Number_Dimensions (Ctyp) - 1 loop
4947 if Nkind (Expr) /= N_Aggregate
4948 or else not Is_Others_Aggregate (Expr)
4953 Expr := Expression (First (Component_Associations (Expr)));
4956 Ctyp := Component_Type (Ctyp);
4958 if Is_Atomic_Or_VFA (Ctyp) then
4963 -- An Iterated_Component_Association involves a loop (in most cases)
4964 -- and is never static.
4966 if Nkind (Parent (Expr)) = N_Iterated_Component_Association then
4970 if not Is_Discrete_Type (Ctyp) then
4974 -- The expression needs to be analyzed if True is returned
4976 Analyze_And_Resolve (Expr, Ctyp);
4978 -- The back end uses the Esize as the precision of the type
4980 Nunits := UI_To_Int (Esize (Ctyp)) / System_Storage_Unit;
4986 if not Compile_Time_Known_Value (Expr) then
4990 Value := Expr_Value (Expr);
4992 if Has_Biased_Representation (Ctyp) then
4993 Value := Value - Expr_Value (Type_Low_Bound (Ctyp));
4996 -- Values 0 and -1 immediately satisfy the last check
4998 if Value = Uint_0 or else Value = Uint_Minus_1 then
5002 -- We need to work with an unsigned value
5005 Value := Value + 2**(System_Storage_Unit * Nunits);
5008 Remainder := Value rem 2**System_Storage_Unit;
5010 for J in 1 .. Nunits - 1 loop
5011 Value := Value / 2**System_Storage_Unit;
5013 if Value rem 2**System_Storage_Unit /= Remainder then
5019 end Aggr_Assignment_OK_For_Backend;
5021 ----------------------------
5022 -- Build_Constrained_Type --
5023 ----------------------------
5025 procedure Build_Constrained_Type (Positional : Boolean) is
5026 Loc : constant Source_Ptr := Sloc (N);
5027 Agg_Type : constant Entity_Id := Make_Temporary (Loc, 'A');
5030 Typ : constant Entity_Id := Etype (N);
5031 Indexes : constant List_Id := New_List;
5036 -- If the aggregate is purely positional, all its subaggregates
5037 -- have the same size. We collect the dimensions from the first
5038 -- subaggregate at each level.
5043 for D in 1 .. Number_Dimensions (Typ) loop
5044 Sub_Agg := First (Expressions (Sub_Agg));
5048 while Present (Comp) loop
5055 Low_Bound => Make_Integer_Literal (Loc, 1),
5056 High_Bound => Make_Integer_Literal (Loc, Num)));
5060 -- We know the aggregate type is unconstrained and the aggregate
5061 -- is not processable by the back end, therefore not necessarily
5062 -- positional. Retrieve each dimension bounds (computed earlier).
5064 for D in 1 .. Number_Dimensions (Typ) loop
5067 Low_Bound => Aggr_Low (D),
5068 High_Bound => Aggr_High (D)));
5073 Make_Full_Type_Declaration (Loc,
5074 Defining_Identifier => Agg_Type,
5076 Make_Constrained_Array_Definition (Loc,
5077 Discrete_Subtype_Definitions => Indexes,
5078 Component_Definition =>
5079 Make_Component_Definition (Loc,
5080 Aliased_Present => False,
5081 Subtype_Indication =>
5082 New_Occurrence_Of (Component_Type (Typ), Loc))));
5084 Insert_Action (N, Decl);
5086 Set_Etype (N, Agg_Type);
5087 Set_Is_Itype (Agg_Type);
5088 Freeze_Itype (Agg_Type, N);
5089 end Build_Constrained_Type;
5095 procedure Check_Bounds (Aggr_Bounds : Node_Id; Index_Bounds : Node_Id) is
5102 Cond : Node_Id := Empty;
5105 Get_Index_Bounds (Aggr_Bounds, Aggr_Lo, Aggr_Hi);
5106 Get_Index_Bounds (Index_Bounds, Ind_Lo, Ind_Hi);
5108 -- Generate the following test:
5110 -- [constraint_error when
5111 -- Aggr_Lo <= Aggr_Hi and then
5112 -- (Aggr_Lo < Ind_Lo or else Aggr_Hi > Ind_Hi)]
5114 -- As an optimization try to see if some tests are trivially vacuous
5115 -- because we are comparing an expression against itself.
5117 if Aggr_Lo = Ind_Lo and then Aggr_Hi = Ind_Hi then
5120 elsif Aggr_Hi = Ind_Hi then
5123 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
5124 Right_Opnd => Duplicate_Subexpr_Move_Checks (Ind_Lo));
5126 elsif Aggr_Lo = Ind_Lo then
5129 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi),
5130 Right_Opnd => Duplicate_Subexpr_Move_Checks (Ind_Hi));
5137 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
5138 Right_Opnd => Duplicate_Subexpr_Move_Checks (Ind_Lo)),
5142 Left_Opnd => Duplicate_Subexpr (Aggr_Hi),
5143 Right_Opnd => Duplicate_Subexpr (Ind_Hi)));
5146 if Present (Cond) then
5151 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
5152 Right_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi)),
5154 Right_Opnd => Cond);
5156 Set_Analyzed (Left_Opnd (Left_Opnd (Cond)), False);
5157 Set_Analyzed (Right_Opnd (Left_Opnd (Cond)), False);
5159 Make_Raise_Constraint_Error (Loc,
5161 Reason => CE_Range_Check_Failed));
5165 ----------------------------
5166 -- Check_Same_Aggr_Bounds --
5167 ----------------------------
5169 procedure Check_Same_Aggr_Bounds (Sub_Aggr : Node_Id; Dim : Pos) is
5170 Sub_Lo : constant Node_Id := Low_Bound (Aggregate_Bounds (Sub_Aggr));
5171 Sub_Hi : constant Node_Id := High_Bound (Aggregate_Bounds (Sub_Aggr));
5172 -- The bounds of this specific subaggregate
5174 Aggr_Lo : constant Node_Id := Aggr_Low (Dim);
5175 Aggr_Hi : constant Node_Id := Aggr_High (Dim);
5176 -- The bounds of the aggregate for this dimension
5178 Ind_Typ : constant Entity_Id := Aggr_Index_Typ (Dim);
5179 -- The index type for this dimension.xxx
5181 Cond : Node_Id := Empty;
5186 -- If index checks are on generate the test
5188 -- [constraint_error when
5189 -- Aggr_Lo /= Sub_Lo or else Aggr_Hi /= Sub_Hi]
5191 -- As an optimization try to see if some tests are trivially vacuos
5192 -- because we are comparing an expression against itself. Also for
5193 -- the first dimension the test is trivially vacuous because there
5194 -- is just one aggregate for dimension 1.
5196 if Index_Checks_Suppressed (Ind_Typ) then
5199 elsif Dim = 1 or else (Aggr_Lo = Sub_Lo and then Aggr_Hi = Sub_Hi)
5203 elsif Aggr_Hi = Sub_Hi then
5206 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
5207 Right_Opnd => Duplicate_Subexpr_Move_Checks (Sub_Lo));
5209 elsif Aggr_Lo = Sub_Lo then
5212 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi),
5213 Right_Opnd => Duplicate_Subexpr_Move_Checks (Sub_Hi));
5220 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
5221 Right_Opnd => Duplicate_Subexpr_Move_Checks (Sub_Lo)),
5225 Left_Opnd => Duplicate_Subexpr (Aggr_Hi),
5226 Right_Opnd => Duplicate_Subexpr (Sub_Hi)));
5229 if Present (Cond) then
5231 Make_Raise_Constraint_Error (Loc,
5233 Reason => CE_Length_Check_Failed));
5236 -- Now look inside the subaggregate to see if there is more work
5238 if Dim < Aggr_Dimension then
5240 -- Process positional components
5242 if Present (Expressions (Sub_Aggr)) then
5243 Expr := First (Expressions (Sub_Aggr));
5244 while Present (Expr) loop
5245 Check_Same_Aggr_Bounds (Expr, Dim + 1);
5250 -- Process component associations
5252 if Present (Component_Associations (Sub_Aggr)) then
5253 Assoc := First (Component_Associations (Sub_Aggr));
5254 while Present (Assoc) loop
5255 Expr := Expression (Assoc);
5256 Check_Same_Aggr_Bounds (Expr, Dim + 1);
5261 end Check_Same_Aggr_Bounds;
5263 ----------------------------
5264 -- Compute_Others_Present --
5265 ----------------------------
5267 procedure Compute_Others_Present (Sub_Aggr : Node_Id; Dim : Pos) is
5272 if Present (Component_Associations (Sub_Aggr)) then
5273 Assoc := Last (Component_Associations (Sub_Aggr));
5275 if Nkind (First (Choice_List (Assoc))) = N_Others_Choice then
5276 Others_Present (Dim) := True;
5280 -- Now look inside the subaggregate to see if there is more work
5282 if Dim < Aggr_Dimension then
5284 -- Process positional components
5286 if Present (Expressions (Sub_Aggr)) then
5287 Expr := First (Expressions (Sub_Aggr));
5288 while Present (Expr) loop
5289 Compute_Others_Present (Expr, Dim + 1);
5294 -- Process component associations
5296 if Present (Component_Associations (Sub_Aggr)) then
5297 Assoc := First (Component_Associations (Sub_Aggr));
5298 while Present (Assoc) loop
5299 Expr := Expression (Assoc);
5300 Compute_Others_Present (Expr, Dim + 1);
5305 end Compute_Others_Present;
5307 ------------------------
5308 -- In_Place_Assign_OK --
5309 ------------------------
5311 function In_Place_Assign_OK return Boolean is
5319 function Safe_Aggregate (Aggr : Node_Id) return Boolean;
5320 -- Check recursively that each component of a (sub)aggregate does not
5321 -- depend on the variable being assigned to.
5323 function Safe_Component (Expr : Node_Id) return Boolean;
5324 -- Verify that an expression cannot depend on the variable being
5325 -- assigned to. Room for improvement here (but less than before).
5327 --------------------
5328 -- Safe_Aggregate --
5329 --------------------
5331 function Safe_Aggregate (Aggr : Node_Id) return Boolean is
5335 if Present (Expressions (Aggr)) then
5336 Expr := First (Expressions (Aggr));
5337 while Present (Expr) loop
5338 if Nkind (Expr) = N_Aggregate then
5339 if not Safe_Aggregate (Expr) then
5343 elsif not Safe_Component (Expr) then
5351 if Present (Component_Associations (Aggr)) then
5352 Expr := First (Component_Associations (Aggr));
5353 while Present (Expr) loop
5354 if Nkind (Expression (Expr)) = N_Aggregate then
5355 if not Safe_Aggregate (Expression (Expr)) then
5359 -- If association has a box, no way to determine yet
5360 -- whether default can be assigned in place.
5362 elsif Box_Present (Expr) then
5365 elsif not Safe_Component (Expression (Expr)) then
5376 --------------------
5377 -- Safe_Component --
5378 --------------------
5380 function Safe_Component (Expr : Node_Id) return Boolean is
5381 Comp : Node_Id := Expr;
5383 function Check_Component (Comp : Node_Id) return Boolean;
5384 -- Do the recursive traversal, after copy
5386 ---------------------
5387 -- Check_Component --
5388 ---------------------
5390 function Check_Component (Comp : Node_Id) return Boolean is
5392 if Is_Overloaded (Comp) then
5396 return Compile_Time_Known_Value (Comp)
5398 or else (Is_Entity_Name (Comp)
5399 and then Present (Entity (Comp))
5400 and then No (Renamed_Object (Entity (Comp))))
5402 or else (Nkind (Comp) = N_Attribute_Reference
5403 and then Check_Component (Prefix (Comp)))
5405 or else (Nkind (Comp) in N_Binary_Op
5406 and then Check_Component (Left_Opnd (Comp))
5407 and then Check_Component (Right_Opnd (Comp)))
5409 or else (Nkind (Comp) in N_Unary_Op
5410 and then Check_Component (Right_Opnd (Comp)))
5412 or else (Nkind (Comp) = N_Selected_Component
5413 and then Check_Component (Prefix (Comp)))
5415 or else (Nkind (Comp) = N_Unchecked_Type_Conversion
5416 and then Check_Component (Expression (Comp)));
5417 end Check_Component;
5419 -- Start of processing for Safe_Component
5422 -- If the component appears in an association that may correspond
5423 -- to more than one element, it is not analyzed before expansion
5424 -- into assignments, to avoid side effects. We analyze, but do not
5425 -- resolve the copy, to obtain sufficient entity information for
5426 -- the checks that follow. If component is overloaded we assume
5427 -- an unsafe function call.
5429 if not Analyzed (Comp) then
5430 if Is_Overloaded (Expr) then
5433 elsif Nkind (Expr) = N_Aggregate
5434 and then not Is_Others_Aggregate (Expr)
5438 elsif Nkind (Expr) = N_Allocator then
5440 -- For now, too complex to analyze
5445 Comp := New_Copy_Tree (Expr);
5446 Set_Parent (Comp, Parent (Expr));
5450 if Nkind (Comp) = N_Aggregate then
5451 return Safe_Aggregate (Comp);
5453 return Check_Component (Comp);
5457 -- Start of processing for In_Place_Assign_OK
5460 if Present (Component_Associations (N)) then
5462 -- On assignment, sliding can take place, so we cannot do the
5463 -- assignment in place unless the bounds of the aggregate are
5464 -- statically equal to those of the target.
5466 -- If the aggregate is given by an others choice, the bounds are
5467 -- derived from the left-hand side, and the assignment is safe if
5468 -- the expression is.
5470 if Is_Others_Aggregate (N) then
5473 (Expression (First (Component_Associations (N))));
5476 Aggr_In := First_Index (Etype (N));
5478 if Nkind (Parent (N)) = N_Assignment_Statement then
5479 Obj_In := First_Index (Etype (Name (Parent (N))));
5482 -- Context is an allocator. Check bounds of aggregate against
5483 -- given type in qualified expression.
5485 pragma Assert (Nkind (Parent (Parent (N))) = N_Allocator);
5487 First_Index (Etype (Entity (Subtype_Mark (Parent (N)))));
5490 while Present (Aggr_In) loop
5491 Get_Index_Bounds (Aggr_In, Aggr_Lo, Aggr_Hi);
5492 Get_Index_Bounds (Obj_In, Obj_Lo, Obj_Hi);
5494 if not Compile_Time_Known_Value (Aggr_Lo)
5495 or else not Compile_Time_Known_Value (Aggr_Hi)
5496 or else not Compile_Time_Known_Value (Obj_Lo)
5497 or else not Compile_Time_Known_Value (Obj_Hi)
5498 or else Expr_Value (Aggr_Lo) /= Expr_Value (Obj_Lo)
5499 or else Expr_Value (Aggr_Hi) /= Expr_Value (Obj_Hi)
5504 Next_Index (Aggr_In);
5505 Next_Index (Obj_In);
5509 -- Now check the component values themselves
5511 return Safe_Aggregate (N);
5512 end In_Place_Assign_OK;
5518 procedure Others_Check (Sub_Aggr : Node_Id; Dim : Pos) is
5519 Aggr_Lo : constant Node_Id := Aggr_Low (Dim);
5520 Aggr_Hi : constant Node_Id := Aggr_High (Dim);
5521 -- The bounds of the aggregate for this dimension
5523 Ind_Typ : constant Entity_Id := Aggr_Index_Typ (Dim);
5524 -- The index type for this dimension
5526 Need_To_Check : Boolean := False;
5528 Choices_Lo : Node_Id := Empty;
5529 Choices_Hi : Node_Id := Empty;
5530 -- The lowest and highest discrete choices for a named subaggregate
5532 Nb_Choices : Int := -1;
5533 -- The number of discrete non-others choices in this subaggregate
5535 Nb_Elements : Uint := Uint_0;
5536 -- The number of elements in a positional aggregate
5538 Cond : Node_Id := Empty;
5545 -- Check if we have an others choice. If we do make sure that this
5546 -- subaggregate contains at least one element in addition to the
5549 if Range_Checks_Suppressed (Ind_Typ) then
5550 Need_To_Check := False;
5552 elsif Present (Expressions (Sub_Aggr))
5553 and then Present (Component_Associations (Sub_Aggr))
5555 Need_To_Check := True;
5557 elsif Present (Component_Associations (Sub_Aggr)) then
5558 Assoc := Last (Component_Associations (Sub_Aggr));
5560 if Nkind (First (Choice_List (Assoc))) /= N_Others_Choice then
5561 Need_To_Check := False;
5564 -- Count the number of discrete choices. Start with -1 because
5565 -- the others choice does not count.
5567 -- Is there some reason we do not use List_Length here ???
5570 Assoc := First (Component_Associations (Sub_Aggr));
5571 while Present (Assoc) loop
5572 Choice := First (Choice_List (Assoc));
5573 while Present (Choice) loop
5574 Nb_Choices := Nb_Choices + 1;
5581 -- If there is only an others choice nothing to do
5583 Need_To_Check := (Nb_Choices > 0);
5587 Need_To_Check := False;
5590 -- If we are dealing with a positional subaggregate with an others
5591 -- choice then compute the number or positional elements.
5593 if Need_To_Check and then Present (Expressions (Sub_Aggr)) then
5594 Expr := First (Expressions (Sub_Aggr));
5595 Nb_Elements := Uint_0;
5596 while Present (Expr) loop
5597 Nb_Elements := Nb_Elements + 1;
5601 -- If the aggregate contains discrete choices and an others choice
5602 -- compute the smallest and largest discrete choice values.
5604 elsif Need_To_Check then
5605 Compute_Choices_Lo_And_Choices_Hi : declare
5607 Table : Case_Table_Type (1 .. Nb_Choices);
5608 -- Used to sort all the different choice values
5615 Assoc := First (Component_Associations (Sub_Aggr));
5616 while Present (Assoc) loop
5617 Choice := First (Choice_List (Assoc));
5618 while Present (Choice) loop
5619 if Nkind (Choice) = N_Others_Choice then
5623 Get_Index_Bounds (Choice, Low, High);
5624 Table (J).Choice_Lo := Low;
5625 Table (J).Choice_Hi := High;
5634 -- Sort the discrete choices
5636 Sort_Case_Table (Table);
5638 Choices_Lo := Table (1).Choice_Lo;
5639 Choices_Hi := Table (Nb_Choices).Choice_Hi;
5640 end Compute_Choices_Lo_And_Choices_Hi;
5643 -- If no others choice in this subaggregate, or the aggregate
5644 -- comprises only an others choice, nothing to do.
5646 if not Need_To_Check then
5649 -- If we are dealing with an aggregate containing an others choice
5650 -- and positional components, we generate the following test:
5652 -- if Ind_Typ'Pos (Aggr_Lo) + (Nb_Elements - 1) >
5653 -- Ind_Typ'Pos (Aggr_Hi)
5655 -- raise Constraint_Error;
5658 elsif Nb_Elements > Uint_0 then
5664 Make_Attribute_Reference (Loc,
5665 Prefix => New_Occurrence_Of (Ind_Typ, Loc),
5666 Attribute_Name => Name_Pos,
5669 (Duplicate_Subexpr_Move_Checks (Aggr_Lo))),
5670 Right_Opnd => Make_Integer_Literal (Loc, Nb_Elements - 1)),
5673 Make_Attribute_Reference (Loc,
5674 Prefix => New_Occurrence_Of (Ind_Typ, Loc),
5675 Attribute_Name => Name_Pos,
5676 Expressions => New_List (
5677 Duplicate_Subexpr_Move_Checks (Aggr_Hi))));
5679 -- If we are dealing with an aggregate containing an others choice
5680 -- and discrete choices we generate the following test:
5682 -- [constraint_error when
5683 -- Choices_Lo < Aggr_Lo or else Choices_Hi > Aggr_Hi];
5690 Left_Opnd => Duplicate_Subexpr_Move_Checks (Choices_Lo),
5691 Right_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo)),
5695 Left_Opnd => Duplicate_Subexpr (Choices_Hi),
5696 Right_Opnd => Duplicate_Subexpr (Aggr_Hi)));
5699 if Present (Cond) then
5701 Make_Raise_Constraint_Error (Loc,
5703 Reason => CE_Length_Check_Failed));
5704 -- Questionable reason code, shouldn't that be a
5705 -- CE_Range_Check_Failed ???
5708 -- Now look inside the subaggregate to see if there is more work
5710 if Dim < Aggr_Dimension then
5712 -- Process positional components
5714 if Present (Expressions (Sub_Aggr)) then
5715 Expr := First (Expressions (Sub_Aggr));
5716 while Present (Expr) loop
5717 Others_Check (Expr, Dim + 1);
5722 -- Process component associations
5724 if Present (Component_Associations (Sub_Aggr)) then
5725 Assoc := First (Component_Associations (Sub_Aggr));
5726 while Present (Assoc) loop
5727 Expr := Expression (Assoc);
5728 Others_Check (Expr, Dim + 1);
5735 -------------------------
5736 -- Safe_Left_Hand_Side --
5737 -------------------------
5739 function Safe_Left_Hand_Side (N : Node_Id) return Boolean is
5740 function Is_Safe_Index (Indx : Node_Id) return Boolean;
5741 -- If the left-hand side includes an indexed component, check that
5742 -- the indexes are free of side effects.
5748 function Is_Safe_Index (Indx : Node_Id) return Boolean is
5750 if Is_Entity_Name (Indx) then
5753 elsif Nkind (Indx) = N_Integer_Literal then
5756 elsif Nkind (Indx) = N_Function_Call
5757 and then Is_Entity_Name (Name (Indx))
5758 and then Has_Pragma_Pure_Function (Entity (Name (Indx)))
5762 elsif Nkind (Indx) = N_Type_Conversion
5763 and then Is_Safe_Index (Expression (Indx))
5772 -- Start of processing for Safe_Left_Hand_Side
5775 if Is_Entity_Name (N) then
5778 elsif Nkind_In (N, N_Explicit_Dereference, N_Selected_Component)
5779 and then Safe_Left_Hand_Side (Prefix (N))
5783 elsif Nkind (N) = N_Indexed_Component
5784 and then Safe_Left_Hand_Side (Prefix (N))
5785 and then Is_Safe_Index (First (Expressions (N)))
5789 elsif Nkind (N) = N_Unchecked_Type_Conversion then
5790 return Safe_Left_Hand_Side (Expression (N));
5795 end Safe_Left_Hand_Side;
5800 -- Holds the temporary aggregate value
5803 -- Holds the declaration of Tmp
5805 Aggr_Code : List_Id;
5806 Parent_Node : Node_Id;
5807 Parent_Kind : Node_Kind;
5809 -- Start of processing for Expand_Array_Aggregate
5812 -- Do not touch the special aggregates of attributes used for Asm calls
5814 if Is_RTE (Ctyp, RE_Asm_Input_Operand)
5815 or else Is_RTE (Ctyp, RE_Asm_Output_Operand)
5819 -- Do not expand an aggregate for an array type which contains tasks if
5820 -- the aggregate is associated with an unexpanded return statement of a
5821 -- build-in-place function. The aggregate is expanded when the related
5822 -- return statement (rewritten into an extended return) is processed.
5823 -- This delay ensures that any temporaries and initialization code
5824 -- generated for the aggregate appear in the proper return block and
5825 -- use the correct _chain and _master.
5827 elsif Has_Task (Base_Type (Etype (N)))
5828 and then Nkind (Parent (N)) = N_Simple_Return_Statement
5829 and then Is_Build_In_Place_Function
5830 (Return_Applies_To (Return_Statement_Entity (Parent (N))))
5834 -- Do not attempt expansion if error already detected. We may reach this
5835 -- point in spite of previous errors when compiling with -gnatq, to
5836 -- force all possible errors (this is the usual ACATS mode).
5838 elsif Error_Posted (N) then
5842 -- If the semantic analyzer has determined that aggregate N will raise
5843 -- Constraint_Error at run time, then the aggregate node has been
5844 -- replaced with an N_Raise_Constraint_Error node and we should
5847 pragma Assert (not Raises_Constraint_Error (N));
5851 -- Check that the index range defined by aggregate bounds is
5852 -- compatible with corresponding index subtype.
5854 Index_Compatibility_Check : declare
5855 Aggr_Index_Range : Node_Id := First_Index (Typ);
5856 -- The current aggregate index range
5858 Index_Constraint : Node_Id := First_Index (Etype (Typ));
5859 -- The corresponding index constraint against which we have to
5860 -- check the above aggregate index range.
5863 Compute_Others_Present (N, 1);
5865 for J in 1 .. Aggr_Dimension loop
5866 -- There is no need to emit a check if an others choice is present
5867 -- for this array aggregate dimension since in this case one of
5868 -- N's subaggregates has taken its bounds from the context and
5869 -- these bounds must have been checked already. In addition all
5870 -- subaggregates corresponding to the same dimension must all have
5871 -- the same bounds (checked in (c) below).
5873 if not Range_Checks_Suppressed (Etype (Index_Constraint))
5874 and then not Others_Present (J)
5876 -- We don't use Checks.Apply_Range_Check here because it emits
5877 -- a spurious check. Namely it checks that the range defined by
5878 -- the aggregate bounds is nonempty. But we know this already
5881 Check_Bounds (Aggr_Index_Range, Index_Constraint);
5884 -- Save the low and high bounds of the aggregate index as well as
5885 -- the index type for later use in checks (b) and (c) below.
5887 Aggr_Low (J) := Low_Bound (Aggr_Index_Range);
5888 Aggr_High (J) := High_Bound (Aggr_Index_Range);
5890 Aggr_Index_Typ (J) := Etype (Index_Constraint);
5892 Next_Index (Aggr_Index_Range);
5893 Next_Index (Index_Constraint);
5895 end Index_Compatibility_Check;
5899 -- If an others choice is present check that no aggregate index is
5900 -- outside the bounds of the index constraint.
5902 Others_Check (N, 1);
5906 -- For multidimensional arrays make sure that all subaggregates
5907 -- corresponding to the same dimension have the same bounds.
5909 if Aggr_Dimension > 1 then
5910 Check_Same_Aggr_Bounds (N, 1);
5915 -- If we have a default component value, or simple initialization is
5916 -- required for the component type, then we replace <> in component
5917 -- associations by the required default value.
5920 Default_Val : Node_Id;
5924 if (Present (Default_Aspect_Component_Value (Typ))
5925 or else Needs_Simple_Initialization (Ctyp))
5926 and then Present (Component_Associations (N))
5928 Assoc := First (Component_Associations (N));
5929 while Present (Assoc) loop
5930 if Nkind (Assoc) = N_Component_Association
5931 and then Box_Present (Assoc)
5933 Set_Box_Present (Assoc, False);
5935 if Present (Default_Aspect_Component_Value (Typ)) then
5936 Default_Val := Default_Aspect_Component_Value (Typ);
5938 Default_Val := Get_Simple_Init_Val (Ctyp, N);
5941 Set_Expression (Assoc, New_Copy_Tree (Default_Val));
5942 Analyze_And_Resolve (Expression (Assoc), Ctyp);
5952 -- Here we test for is packed array aggregate that we can handle at
5953 -- compile time. If so, return with transformation done. Note that we do
5954 -- this even if the aggregate is nested, because once we have done this
5955 -- processing, there is no more nested aggregate.
5957 if Packed_Array_Aggregate_Handled (N) then
5961 -- At this point we try to convert to positional form
5963 if Ekind (Current_Scope) = E_Package
5964 and then Static_Elaboration_Desired (Current_Scope)
5966 Convert_To_Positional (N, Max_Others_Replicate => 100);
5968 Convert_To_Positional (N);
5971 -- if the result is no longer an aggregate (e.g. it may be a string
5972 -- literal, or a temporary which has the needed value), then we are
5973 -- done, since there is no longer a nested aggregate.
5975 if Nkind (N) /= N_Aggregate then
5978 -- We are also done if the result is an analyzed aggregate, indicating
5979 -- that Convert_To_Positional succeeded and reanalyzed the rewritten
5982 elsif Analyzed (N) and then N /= Original_Node (N) then
5986 -- If all aggregate components are compile-time known and the aggregate
5987 -- has been flattened, nothing left to do. The same occurs if the
5988 -- aggregate is used to initialize the components of a statically
5989 -- allocated dispatch table.
5991 if Compile_Time_Known_Aggregate (N)
5992 or else Is_Static_Dispatch_Table_Aggregate (N)
5994 Set_Expansion_Delayed (N, False);
5998 -- Now see if back end processing is possible
6000 if Backend_Processing_Possible (N) then
6002 -- If the aggregate is static but the constraints are not, build
6003 -- a static subtype for the aggregate, so that Gigi can place it
6004 -- in static memory. Perform an unchecked_conversion to the non-
6005 -- static type imposed by the context.
6008 Itype : constant Entity_Id := Etype (N);
6010 Needs_Type : Boolean := False;
6013 Index := First_Index (Itype);
6014 while Present (Index) loop
6015 if not Is_OK_Static_Subtype (Etype (Index)) then
6024 Build_Constrained_Type (Positional => True);
6025 Rewrite (N, Unchecked_Convert_To (Itype, N));
6035 -- Delay expansion for nested aggregates: it will be taken care of when
6036 -- the parent aggregate is expanded.
6038 Parent_Node := Parent (N);
6039 Parent_Kind := Nkind (Parent_Node);
6041 if Parent_Kind = N_Qualified_Expression then
6042 Parent_Node := Parent (Parent_Node);
6043 Parent_Kind := Nkind (Parent_Node);
6046 if Parent_Kind = N_Aggregate
6047 or else Parent_Kind = N_Extension_Aggregate
6048 or else Parent_Kind = N_Component_Association
6049 or else (Parent_Kind = N_Object_Declaration
6050 and then Needs_Finalization (Typ))
6051 or else (Parent_Kind = N_Assignment_Statement
6052 and then Inside_Init_Proc)
6054 if Static_Array_Aggregate (N)
6055 or else Compile_Time_Known_Aggregate (N)
6057 Set_Expansion_Delayed (N, False);
6060 Set_Expansion_Delayed (N);
6067 -- Look if in place aggregate expansion is possible
6069 -- For object declarations we build the aggregate in place, unless
6070 -- the array is bit-packed or the component is controlled.
6072 -- For assignments we do the assignment in place if all the component
6073 -- associations have compile-time known values. For other cases we
6074 -- create a temporary. The analysis for safety of on-line assignment
6075 -- is delicate, i.e. we don't know how to do it fully yet ???
6077 -- For allocators we assign to the designated object in place if the
6078 -- aggregate meets the same conditions as other in-place assignments.
6079 -- In this case the aggregate may not come from source but was created
6080 -- for default initialization, e.g. with Initialize_Scalars.
6082 if Requires_Transient_Scope (Typ) then
6083 Establish_Transient_Scope
6084 (N, Sec_Stack => Has_Controlled_Component (Typ));
6087 if Has_Default_Init_Comps (N) then
6088 Maybe_In_Place_OK := False;
6090 elsif Is_Bit_Packed_Array (Typ)
6091 or else Has_Controlled_Component (Typ)
6093 Maybe_In_Place_OK := False;
6096 Maybe_In_Place_OK :=
6097 (Nkind (Parent (N)) = N_Assignment_Statement
6098 and then In_Place_Assign_OK)
6101 (Nkind (Parent (Parent (N))) = N_Allocator
6102 and then In_Place_Assign_OK);
6105 -- If this is an array of tasks, it will be expanded into build-in-place
6106 -- assignments. Build an activation chain for the tasks now.
6108 if Has_Task (Etype (N)) then
6109 Build_Activation_Chain_Entity (N);
6112 -- Perform in-place expansion of aggregate in an object declaration.
6113 -- Note: actions generated for the aggregate will be captured in an
6114 -- expression-with-actions statement so that they can be transferred
6115 -- to freeze actions later if there is an address clause for the
6116 -- object. (Note: we don't use a block statement because this would
6117 -- cause generated freeze nodes to be elaborated in the wrong scope).
6119 -- Do not perform in-place expansion for SPARK 05 because aggregates are
6120 -- expected to appear in qualified form. In-place expansion eliminates
6121 -- the qualification and eventually violates this SPARK 05 restiction.
6123 -- Should document the rest of the guards ???
6125 if not Has_Default_Init_Comps (N)
6126 and then Comes_From_Source (Parent_Node)
6127 and then Parent_Kind = N_Object_Declaration
6128 and then Present (Expression (Parent_Node))
6130 Must_Slide (Etype (Defining_Identifier (Parent_Node)), Typ)
6131 and then not Has_Controlled_Component (Typ)
6132 and then not Is_Bit_Packed_Array (Typ)
6133 and then not Restriction_Check_Required (SPARK_05)
6135 In_Place_Assign_OK_For_Declaration := True;
6136 Tmp := Defining_Identifier (Parent_Node);
6137 Set_No_Initialization (Parent_Node);
6138 Set_Expression (Parent_Node, Empty);
6140 -- Set kind and type of the entity, for use in the analysis
6141 -- of the subsequent assignments. If the nominal type is not
6142 -- constrained, build a subtype from the known bounds of the
6143 -- aggregate. If the declaration has a subtype mark, use it,
6144 -- otherwise use the itype of the aggregate.
6146 Set_Ekind (Tmp, E_Variable);
6148 if not Is_Constrained (Typ) then
6149 Build_Constrained_Type (Positional => False);
6151 elsif Is_Entity_Name (Object_Definition (Parent_Node))
6152 and then Is_Constrained (Entity (Object_Definition (Parent_Node)))
6154 Set_Etype (Tmp, Entity (Object_Definition (Parent_Node)));
6157 Set_Size_Known_At_Compile_Time (Typ, False);
6158 Set_Etype (Tmp, Typ);
6161 elsif Maybe_In_Place_OK
6162 and then Nkind (Parent (N)) = N_Qualified_Expression
6163 and then Nkind (Parent (Parent (N))) = N_Allocator
6165 Set_Expansion_Delayed (N);
6168 -- In the remaining cases the aggregate is the RHS of an assignment
6170 elsif Maybe_In_Place_OK
6171 and then Safe_Left_Hand_Side (Name (Parent (N)))
6173 Tmp := Name (Parent (N));
6175 if Etype (Tmp) /= Etype (N) then
6176 Apply_Length_Check (N, Etype (Tmp));
6178 if Nkind (N) = N_Raise_Constraint_Error then
6180 -- Static error, nothing further to expand
6186 -- If a slice assignment has an aggregate with a single others_choice,
6187 -- the assignment can be done in place even if bounds are not static,
6188 -- by converting it into a loop over the discrete range of the slice.
6190 elsif Maybe_In_Place_OK
6191 and then Nkind (Name (Parent (N))) = N_Slice
6192 and then Is_Others_Aggregate (N)
6194 Tmp := Name (Parent (N));
6196 -- Set type of aggregate to be type of lhs in assignment, in order
6197 -- to suppress redundant length checks.
6199 Set_Etype (N, Etype (Tmp));
6203 -- In place aggregate expansion is not possible
6206 Maybe_In_Place_OK := False;
6207 Tmp := Make_Temporary (Loc, 'A', N);
6209 Make_Object_Declaration (Loc,
6210 Defining_Identifier => Tmp,
6211 Object_Definition => New_Occurrence_Of (Typ, Loc));
6212 Set_No_Initialization (Tmp_Decl, True);
6214 -- If we are within a loop, the temporary will be pushed on the
6215 -- stack at each iteration. If the aggregate is the expression for an
6216 -- allocator, it will be immediately copied to the heap and can
6217 -- be reclaimed at once. We create a transient scope around the
6218 -- aggregate for this purpose.
6220 if Ekind (Current_Scope) = E_Loop
6221 and then Nkind (Parent (Parent (N))) = N_Allocator
6223 Establish_Transient_Scope (N, False);
6226 Insert_Action (N, Tmp_Decl);
6229 -- Construct and insert the aggregate code. We can safely suppress index
6230 -- checks because this code is guaranteed not to raise CE on index
6231 -- checks. However we should *not* suppress all checks.
6237 if Nkind (Tmp) = N_Defining_Identifier then
6238 Target := New_Occurrence_Of (Tmp, Loc);
6241 if Has_Default_Init_Comps (N) then
6243 -- Ada 2005 (AI-287): This case has not been analyzed???
6245 raise Program_Error;
6248 -- Name in assignment is explicit dereference
6250 Target := New_Copy (Tmp);
6253 -- If we are to generate an in place assignment for a declaration or
6254 -- an assignment statement, and the assignment can be done directly
6255 -- by the back end, then do not expand further.
6257 -- ??? We can also do that if in place expansion is not possible but
6258 -- then we could go into an infinite recursion.
6260 if (In_Place_Assign_OK_For_Declaration or else Maybe_In_Place_OK)
6261 and then not AAMP_On_Target
6262 and then not CodePeer_Mode
6263 and then not Modify_Tree_For_C
6264 and then not Possible_Bit_Aligned_Component (Target)
6265 and then not Is_Possibly_Unaligned_Slice (Target)
6266 and then Aggr_Assignment_OK_For_Backend (N)
6268 if Maybe_In_Place_OK then
6274 Make_Assignment_Statement (Loc,
6276 Expression => New_Copy (N)));
6280 Build_Array_Aggr_Code (N,
6282 Index => First_Index (Typ),
6284 Scalar_Comp => Is_Scalar_Type (Ctyp));
6287 -- Save the last assignment statement associated with the aggregate
6288 -- when building a controlled object. This reference is utilized by
6289 -- the finalization machinery when marking an object as successfully
6292 if Needs_Finalization (Typ)
6293 and then Is_Entity_Name (Target)
6294 and then Present (Entity (Target))
6295 and then Ekind_In (Entity (Target), E_Constant, E_Variable)
6297 Set_Last_Aggregate_Assignment (Entity (Target), Last (Aggr_Code));
6301 -- If the aggregate is the expression in a declaration, the expanded
6302 -- code must be inserted after it. The defining entity might not come
6303 -- from source if this is part of an inlined body, but the declaration
6306 if Comes_From_Source (Tmp)
6308 (Nkind (Parent (N)) = N_Object_Declaration
6309 and then Comes_From_Source (Parent (N))
6310 and then Tmp = Defining_Entity (Parent (N)))
6313 Node_After : constant Node_Id := Next (Parent_Node);
6316 Insert_Actions_After (Parent_Node, Aggr_Code);
6318 if Parent_Kind = N_Object_Declaration then
6319 Collect_Initialization_Statements
6320 (Obj => Tmp, N => Parent_Node, Node_After => Node_After);
6325 Insert_Actions (N, Aggr_Code);
6328 -- If the aggregate has been assigned in place, remove the original
6331 if Nkind (Parent (N)) = N_Assignment_Statement
6332 and then Maybe_In_Place_OK
6334 Rewrite (Parent (N), Make_Null_Statement (Loc));
6336 elsif Nkind (Parent (N)) /= N_Object_Declaration
6337 or else Tmp /= Defining_Identifier (Parent (N))
6339 Rewrite (N, New_Occurrence_Of (Tmp, Loc));
6340 Analyze_And_Resolve (N, Typ);
6342 end Expand_Array_Aggregate;
6344 ------------------------
6345 -- Expand_N_Aggregate --
6346 ------------------------
6348 procedure Expand_N_Aggregate (N : Node_Id) is
6350 -- Record aggregate case
6352 if Is_Record_Type (Etype (N)) then
6353 Expand_Record_Aggregate (N);
6355 -- Array aggregate case
6358 -- A special case, if we have a string subtype with bounds 1 .. N,
6359 -- where N is known at compile time, and the aggregate is of the
6360 -- form (others => 'x'), with a single choice and no expressions,
6361 -- and N is less than 80 (an arbitrary limit for now), then replace
6362 -- the aggregate by the equivalent string literal (but do not mark
6363 -- it as static since it is not).
6365 -- Note: this entire circuit is redundant with respect to code in
6366 -- Expand_Array_Aggregate that collapses others choices to positional
6367 -- form, but there are two problems with that circuit:
6369 -- a) It is limited to very small cases due to ill-understood
6370 -- interactions with bootstrapping. That limit is removed by
6371 -- use of the No_Implicit_Loops restriction.
6373 -- b) It incorrectly ends up with the resulting expressions being
6374 -- considered static when they are not. For example, the
6375 -- following test should fail:
6377 -- pragma Restrictions (No_Implicit_Loops);
6378 -- package NonSOthers4 is
6379 -- B : constant String (1 .. 6) := (others => 'A');
6380 -- DH : constant String (1 .. 8) := B & "BB";
6382 -- pragma Export (C, X, Link_Name => DH);
6385 -- But it succeeds (DH looks static to pragma Export)
6387 -- To be sorted out ???
6389 if Present (Component_Associations (N)) then
6391 CA : constant Node_Id := First (Component_Associations (N));
6392 MX : constant := 80;
6395 if Nkind (First (Choice_List (CA))) = N_Others_Choice
6396 and then Nkind (Expression (CA)) = N_Character_Literal
6397 and then No (Expressions (N))
6400 T : constant Entity_Id := Etype (N);
6401 X : constant Node_Id := First_Index (T);
6402 EC : constant Node_Id := Expression (CA);
6403 CV : constant Uint := Char_Literal_Value (EC);
6404 CC : constant Int := UI_To_Int (CV);
6407 if Nkind (X) = N_Range
6408 and then Compile_Time_Known_Value (Low_Bound (X))
6409 and then Expr_Value (Low_Bound (X)) = 1
6410 and then Compile_Time_Known_Value (High_Bound (X))
6413 Hi : constant Uint := Expr_Value (High_Bound (X));
6419 for J in 1 .. UI_To_Int (Hi) loop
6420 Store_String_Char (Char_Code (CC));
6424 Make_String_Literal (Sloc (N),
6425 Strval => End_String));
6427 if CC >= Int (2 ** 16) then
6428 Set_Has_Wide_Wide_Character (N);
6429 elsif CC >= Int (2 ** 8) then
6430 Set_Has_Wide_Character (N);
6433 Analyze_And_Resolve (N, T);
6434 Set_Is_Static_Expression (N, False);
6444 -- Not that special case, so normal expansion of array aggregate
6446 Expand_Array_Aggregate (N);
6450 when RE_Not_Available =>
6452 end Expand_N_Aggregate;
6454 ------------------------------
6455 -- Expand_N_Delta_Aggregate --
6456 ------------------------------
6458 procedure Expand_N_Delta_Aggregate (N : Node_Id) is
6459 Loc : constant Source_Ptr := Sloc (N);
6460 Typ : constant Entity_Id := Etype (N);
6465 Make_Object_Declaration (Loc,
6466 Defining_Identifier => Make_Temporary (Loc, 'T'),
6467 Object_Definition => New_Occurrence_Of (Typ, Loc),
6468 Expression => New_Copy_Tree (Expression (N)));
6470 if Is_Array_Type (Etype (N)) then
6471 Expand_Delta_Array_Aggregate (N, New_List (Decl));
6473 Expand_Delta_Record_Aggregate (N, New_List (Decl));
6475 end Expand_N_Delta_Aggregate;
6477 ----------------------------------
6478 -- Expand_Delta_Array_Aggregate --
6479 ----------------------------------
6481 procedure Expand_Delta_Array_Aggregate (N : Node_Id; Deltas : List_Id) is
6482 Loc : constant Source_Ptr := Sloc (N);
6483 Temp : constant Entity_Id := Defining_Identifier (First (Deltas));
6486 function Generate_Loop (C : Node_Id) return Node_Id;
6487 -- Generate a loop containing individual component assignments for
6488 -- choices that are ranges, subtype indications, subtype names, and
6489 -- iterated component associations.
6495 function Generate_Loop (C : Node_Id) return Node_Id is
6496 Sl : constant Source_Ptr := Sloc (C);
6500 if Nkind (Parent (C)) = N_Iterated_Component_Association then
6502 Make_Defining_Identifier (Loc,
6503 Chars => (Chars (Defining_Identifier (Parent (C)))));
6505 Ix := Make_Temporary (Sl, 'I');
6509 Make_Loop_Statement (Loc,
6511 Make_Iteration_Scheme (Sl,
6512 Loop_Parameter_Specification =>
6513 Make_Loop_Parameter_Specification (Sl,
6514 Defining_Identifier => Ix,
6515 Discrete_Subtype_Definition => New_Copy_Tree (C))),
6517 Statements => New_List (
6518 Make_Assignment_Statement (Sl,
6520 Make_Indexed_Component (Sl,
6521 Prefix => New_Occurrence_Of (Temp, Sl),
6522 Expressions => New_List (New_Occurrence_Of (Ix, Sl))),
6523 Expression => New_Copy_Tree (Expression (Assoc)))),
6524 End_Label => Empty);
6531 -- Start of processing for Expand_Delta_Array_Aggregate
6534 Assoc := First (Component_Associations (N));
6535 while Present (Assoc) loop
6536 Choice := First (Choice_List (Assoc));
6537 if Nkind (Assoc) = N_Iterated_Component_Association then
6538 while Present (Choice) loop
6539 Append_To (Deltas, Generate_Loop (Choice));
6544 while Present (Choice) loop
6546 -- Choice can be given by a range, a subtype indication, a
6547 -- subtype name, a scalar value, or an entity.
6549 if Nkind (Choice) = N_Range
6550 or else (Is_Entity_Name (Choice)
6551 and then Is_Type (Entity (Choice)))
6553 Append_To (Deltas, Generate_Loop (Choice));
6555 elsif Nkind (Choice) = N_Subtype_Indication then
6557 Generate_Loop (Range_Expression (Constraint (Choice))));
6561 Make_Assignment_Statement (Sloc (Choice),
6563 Make_Indexed_Component (Sloc (Choice),
6564 Prefix => New_Occurrence_Of (Temp, Loc),
6565 Expressions => New_List (New_Copy_Tree (Choice))),
6566 Expression => New_Copy_Tree (Expression (Assoc))));
6576 Insert_Actions (N, Deltas);
6577 Rewrite (N, New_Occurrence_Of (Temp, Loc));
6578 end Expand_Delta_Array_Aggregate;
6580 -----------------------------------
6581 -- Expand_Delta_Record_Aggregate --
6582 -----------------------------------
6584 procedure Expand_Delta_Record_Aggregate (N : Node_Id; Deltas : List_Id) is
6585 Loc : constant Source_Ptr := Sloc (N);
6586 Temp : constant Entity_Id := Defining_Identifier (First (Deltas));
6591 Assoc := First (Component_Associations (N));
6593 while Present (Assoc) loop
6594 Choice := First (Choice_List (Assoc));
6595 while Present (Choice) loop
6597 Make_Assignment_Statement (Sloc (Choice),
6599 Make_Selected_Component (Sloc (Choice),
6600 Prefix => New_Occurrence_Of (Temp, Loc),
6601 Selector_Name => Make_Identifier (Loc, Chars (Choice))),
6602 Expression => New_Copy_Tree (Expression (Assoc))));
6609 Insert_Actions (N, Deltas);
6610 Rewrite (N, New_Occurrence_Of (Temp, Loc));
6611 end Expand_Delta_Record_Aggregate;
6613 ----------------------------------
6614 -- Expand_N_Extension_Aggregate --
6615 ----------------------------------
6617 -- If the ancestor part is an expression, add a component association for
6618 -- the parent field. If the type of the ancestor part is not the direct
6619 -- parent of the expected type, build recursively the needed ancestors.
6620 -- If the ancestor part is a subtype_mark, replace aggregate with a decla-
6621 -- ration for a temporary of the expected type, followed by individual
6622 -- assignments to the given components.
6624 procedure Expand_N_Extension_Aggregate (N : Node_Id) is
6625 Loc : constant Source_Ptr := Sloc (N);
6626 A : constant Node_Id := Ancestor_Part (N);
6627 Typ : constant Entity_Id := Etype (N);
6630 -- If the ancestor is a subtype mark, an init proc must be called
6631 -- on the resulting object which thus has to be materialized in
6634 if Is_Entity_Name (A) and then Is_Type (Entity (A)) then
6635 Convert_To_Assignments (N, Typ);
6637 -- The extension aggregate is transformed into a record aggregate
6638 -- of the following form (c1 and c2 are inherited components)
6640 -- (Exp with c3 => a, c4 => b)
6641 -- ==> (c1 => Exp.c1, c2 => Exp.c2, c3 => a, c4 => b)
6646 if Tagged_Type_Expansion then
6647 Expand_Record_Aggregate (N,
6650 (Node (First_Elmt (Access_Disp_Table (Typ))), Loc),
6653 -- No tag is needed in the case of a VM
6656 Expand_Record_Aggregate (N, Parent_Expr => A);
6661 when RE_Not_Available =>
6663 end Expand_N_Extension_Aggregate;
6665 -----------------------------
6666 -- Expand_Record_Aggregate --
6667 -----------------------------
6669 procedure Expand_Record_Aggregate
6671 Orig_Tag : Node_Id := Empty;
6672 Parent_Expr : Node_Id := Empty)
6674 Loc : constant Source_Ptr := Sloc (N);
6675 Comps : constant List_Id := Component_Associations (N);
6676 Typ : constant Entity_Id := Etype (N);
6677 Base_Typ : constant Entity_Id := Base_Type (Typ);
6679 Static_Components : Boolean := True;
6680 -- Flag to indicate whether all components are compile-time known,
6681 -- and the aggregate can be constructed statically and handled by
6684 procedure Build_Back_End_Aggregate;
6685 -- Build a proper aggregate to be handled by the back-end
6687 function Compile_Time_Known_Composite_Value (N : Node_Id) return Boolean;
6688 -- Returns true if N is an expression of composite type which can be
6689 -- fully evaluated at compile time without raising constraint error.
6690 -- Such expressions can be passed as is to Gigi without any expansion.
6692 -- This returns true for N_Aggregate with Compile_Time_Known_Aggregate
6693 -- set and constants whose expression is such an aggregate, recursively.
6695 function Component_Not_OK_For_Backend return Boolean;
6696 -- Check for presence of a component which makes it impossible for the
6697 -- backend to process the aggregate, thus requiring the use of a series
6698 -- of assignment statements. Cases checked for are a nested aggregate
6699 -- needing Late_Expansion, the presence of a tagged component which may
6700 -- need tag adjustment, and a bit unaligned component reference.
6702 -- We also force expansion into assignments if a component is of a
6703 -- mutable type (including a private type with discriminants) because
6704 -- in that case the size of the component to be copied may be smaller
6705 -- than the side of the target, and there is no simple way for gigi
6706 -- to compute the size of the object to be copied.
6708 -- NOTE: This is part of the ongoing work to define precisely the
6709 -- interface between front-end and back-end handling of aggregates.
6710 -- In general it is desirable to pass aggregates as they are to gigi,
6711 -- in order to minimize elaboration code. This is one case where the
6712 -- semantics of Ada complicate the analysis and lead to anomalies in
6713 -- the gcc back-end if the aggregate is not expanded into assignments.
6715 function Has_Per_Object_Constraint (L : List_Id) return Boolean;
6716 -- Return True if any element of L has Has_Per_Object_Constraint set.
6717 -- L should be the Choices component of an N_Component_Association.
6719 function Has_Visible_Private_Ancestor (Id : E) return Boolean;
6720 -- If any ancestor of the current type is private, the aggregate
6721 -- cannot be built in place. We cannot rely on Has_Private_Ancestor,
6722 -- because it will not be set when type and its parent are in the
6723 -- same scope, and the parent component needs expansion.
6725 function Top_Level_Aggregate (N : Node_Id) return Node_Id;
6726 -- For nested aggregates return the ultimate enclosing aggregate; for
6727 -- non-nested aggregates return N.
6729 ------------------------------
6730 -- Build_Back_End_Aggregate --
6731 ------------------------------
6733 procedure Build_Back_End_Aggregate is
6736 Tag_Value : Node_Id;
6739 if Nkind (N) = N_Aggregate then
6741 -- If the aggregate is static and can be handled by the back-end,
6742 -- nothing left to do.
6744 if Static_Components then
6745 Set_Compile_Time_Known_Aggregate (N);
6746 Set_Expansion_Delayed (N, False);
6750 -- If no discriminants, nothing special to do
6752 if not Has_Discriminants (Typ) then
6755 -- Case of discriminants present
6757 elsif Is_Derived_Type (Typ) then
6759 -- For untagged types, non-stored discriminants are replaced with
6760 -- stored discriminants, which are the ones that gigi uses to
6761 -- describe the type and its components.
6763 Generate_Aggregate_For_Derived_Type : declare
6764 procedure Prepend_Stored_Values (T : Entity_Id);
6765 -- Scan the list of stored discriminants of the type, and add
6766 -- their values to the aggregate being built.
6768 ---------------------------
6769 -- Prepend_Stored_Values --
6770 ---------------------------
6772 procedure Prepend_Stored_Values (T : Entity_Id) is
6774 First_Comp : Node_Id := Empty;
6777 Discr := First_Stored_Discriminant (T);
6778 while Present (Discr) loop
6780 Make_Component_Association (Loc,
6781 Choices => New_List (
6782 New_Occurrence_Of (Discr, Loc)),
6785 (Get_Discriminant_Value
6788 Discriminant_Constraint (Typ))));
6790 if No (First_Comp) then
6791 Prepend_To (Component_Associations (N), New_Comp);
6793 Insert_After (First_Comp, New_Comp);
6796 First_Comp := New_Comp;
6797 Next_Stored_Discriminant (Discr);
6799 end Prepend_Stored_Values;
6803 Constraints : constant List_Id := New_List;
6807 Num_Disc : Nat := 0;
6808 Num_Gird : Nat := 0;
6810 -- Start of processing for Generate_Aggregate_For_Derived_Type
6813 -- Remove the associations for the discriminant of derived type
6816 First_Comp : Node_Id;
6819 First_Comp := First (Component_Associations (N));
6820 while Present (First_Comp) loop
6824 if Ekind (Entity (First (Choices (Comp)))) =
6828 Num_Disc := Num_Disc + 1;
6833 -- Insert stored discriminant associations in the correct
6834 -- order. If there are more stored discriminants than new
6835 -- discriminants, there is at least one new discriminant that
6836 -- constrains more than one of the stored discriminants. In
6837 -- this case we need to construct a proper subtype of the
6838 -- parent type, in order to supply values to all the
6839 -- components. Otherwise there is one-one correspondence
6840 -- between the constraints and the stored discriminants.
6842 Discr := First_Stored_Discriminant (Base_Type (Typ));
6843 while Present (Discr) loop
6844 Num_Gird := Num_Gird + 1;
6845 Next_Stored_Discriminant (Discr);
6848 -- Case of more stored discriminants than new discriminants
6850 if Num_Gird > Num_Disc then
6852 -- Create a proper subtype of the parent type, which is the
6853 -- proper implementation type for the aggregate, and convert
6854 -- it to the intended target type.
6856 Discr := First_Stored_Discriminant (Base_Type (Typ));
6857 while Present (Discr) loop
6860 (Get_Discriminant_Value
6863 Discriminant_Constraint (Typ)));
6865 Append (New_Comp, Constraints);
6866 Next_Stored_Discriminant (Discr);
6870 Make_Subtype_Declaration (Loc,
6871 Defining_Identifier => Make_Temporary (Loc, 'T'),
6872 Subtype_Indication =>
6873 Make_Subtype_Indication (Loc,
6875 New_Occurrence_Of (Etype (Base_Type (Typ)), Loc),
6877 Make_Index_Or_Discriminant_Constraint
6878 (Loc, Constraints)));
6880 Insert_Action (N, Decl);
6881 Prepend_Stored_Values (Base_Type (Typ));
6883 Set_Etype (N, Defining_Identifier (Decl));
6886 Rewrite (N, Unchecked_Convert_To (Typ, N));
6889 -- Case where we do not have fewer new discriminants than
6890 -- stored discriminants, so in this case we can simply use the
6891 -- stored discriminants of the subtype.
6894 Prepend_Stored_Values (Typ);
6896 end Generate_Aggregate_For_Derived_Type;
6899 if Is_Tagged_Type (Typ) then
6901 -- In the tagged case, _parent and _tag component must be created
6903 -- Reset Null_Present unconditionally. Tagged records always have
6904 -- at least one field (the tag or the parent).
6906 Set_Null_Record_Present (N, False);
6908 -- When the current aggregate comes from the expansion of an
6909 -- extension aggregate, the parent expr is replaced by an
6910 -- aggregate formed by selected components of this expr.
6912 if Present (Parent_Expr) and then Is_Empty_List (Comps) then
6913 Comp := First_Component_Or_Discriminant (Typ);
6914 while Present (Comp) loop
6916 -- Skip all expander-generated components
6918 if not Comes_From_Source (Original_Record_Component (Comp))
6924 Make_Selected_Component (Loc,
6926 Unchecked_Convert_To (Typ,
6927 Duplicate_Subexpr (Parent_Expr, True)),
6928 Selector_Name => New_Occurrence_Of (Comp, Loc));
6931 Make_Component_Association (Loc,
6932 Choices => New_List (
6933 New_Occurrence_Of (Comp, Loc)),
6934 Expression => New_Comp));
6936 Analyze_And_Resolve (New_Comp, Etype (Comp));
6939 Next_Component_Or_Discriminant (Comp);
6943 -- Compute the value for the Tag now, if the type is a root it
6944 -- will be included in the aggregate right away, otherwise it will
6945 -- be propagated to the parent aggregate.
6947 if Present (Orig_Tag) then
6948 Tag_Value := Orig_Tag;
6950 elsif not Tagged_Type_Expansion then
6956 (Node (First_Elmt (Access_Disp_Table (Typ))), Loc);
6959 -- For a derived type, an aggregate for the parent is formed with
6960 -- all the inherited components.
6962 if Is_Derived_Type (Typ) then
6964 First_Comp : Node_Id;
6965 Parent_Comps : List_Id;
6966 Parent_Aggr : Node_Id;
6967 Parent_Name : Node_Id;
6970 -- Remove the inherited component association from the
6971 -- aggregate and store them in the parent aggregate
6973 First_Comp := First (Component_Associations (N));
6974 Parent_Comps := New_List;
6975 while Present (First_Comp)
6977 Scope (Original_Record_Component
6978 (Entity (First (Choices (First_Comp))))) /=
6984 Append (Comp, Parent_Comps);
6988 Make_Aggregate (Loc,
6989 Component_Associations => Parent_Comps);
6990 Set_Etype (Parent_Aggr, Etype (Base_Type (Typ)));
6992 -- Find the _parent component
6994 Comp := First_Component (Typ);
6995 while Chars (Comp) /= Name_uParent loop
6996 Comp := Next_Component (Comp);
6999 Parent_Name := New_Occurrence_Of (Comp, Loc);
7001 -- Insert the parent aggregate
7003 Prepend_To (Component_Associations (N),
7004 Make_Component_Association (Loc,
7005 Choices => New_List (Parent_Name),
7006 Expression => Parent_Aggr));
7008 -- Expand recursively the parent propagating the right Tag
7010 Expand_Record_Aggregate
7011 (Parent_Aggr, Tag_Value, Parent_Expr);
7013 -- The ancestor part may be a nested aggregate that has
7014 -- delayed expansion: recheck now.
7016 if Component_Not_OK_For_Backend then
7017 Convert_To_Assignments (N, Typ);
7021 -- For a root type, the tag component is added (unless compiling
7022 -- for the VMs, where tags are implicit).
7024 elsif Tagged_Type_Expansion then
7026 Tag_Name : constant Node_Id :=
7028 (First_Tag_Component (Typ), Loc);
7029 Typ_Tag : constant Entity_Id := RTE (RE_Tag);
7030 Conv_Node : constant Node_Id :=
7031 Unchecked_Convert_To (Typ_Tag, Tag_Value);
7034 Set_Etype (Conv_Node, Typ_Tag);
7035 Prepend_To (Component_Associations (N),
7036 Make_Component_Association (Loc,
7037 Choices => New_List (Tag_Name),
7038 Expression => Conv_Node));
7042 end Build_Back_End_Aggregate;
7044 ----------------------------------------
7045 -- Compile_Time_Known_Composite_Value --
7046 ----------------------------------------
7048 function Compile_Time_Known_Composite_Value
7049 (N : Node_Id) return Boolean
7052 -- If we have an entity name, then see if it is the name of a
7053 -- constant and if so, test the corresponding constant value.
7055 if Is_Entity_Name (N) then
7057 E : constant Entity_Id := Entity (N);
7060 if Ekind (E) /= E_Constant then
7063 V := Constant_Value (E);
7065 and then Compile_Time_Known_Composite_Value (V);
7069 -- We have a value, see if it is compile time known
7072 if Nkind (N) = N_Aggregate then
7073 return Compile_Time_Known_Aggregate (N);
7076 -- All other types of values are not known at compile time
7081 end Compile_Time_Known_Composite_Value;
7083 ----------------------------------
7084 -- Component_Not_OK_For_Backend --
7085 ----------------------------------
7087 function Component_Not_OK_For_Backend return Boolean is
7097 while Present (C) loop
7099 -- If the component has box initialization, expansion is needed
7100 -- and component is not ready for backend.
7102 if Box_Present (C) then
7106 if Nkind (Expression (C)) = N_Qualified_Expression then
7107 Expr_Q := Expression (Expression (C));
7109 Expr_Q := Expression (C);
7112 -- Return true if the aggregate has any associations for tagged
7113 -- components that may require tag adjustment.
7115 -- These are cases where the source expression may have a tag that
7116 -- could differ from the component tag (e.g., can occur for type
7117 -- conversions and formal parameters). (Tag adjustment not needed
7118 -- if Tagged_Type_Expansion because object tags are implicit in
7121 if Is_Tagged_Type (Etype (Expr_Q))
7122 and then (Nkind (Expr_Q) = N_Type_Conversion
7123 or else (Is_Entity_Name (Expr_Q)
7125 Ekind (Entity (Expr_Q)) in Formal_Kind))
7126 and then Tagged_Type_Expansion
7128 Static_Components := False;
7131 elsif Is_Delayed_Aggregate (Expr_Q) then
7132 Static_Components := False;
7135 elsif Possible_Bit_Aligned_Component (Expr_Q) then
7136 Static_Components := False;
7139 elsif Modify_Tree_For_C
7140 and then Nkind (C) = N_Component_Association
7141 and then Has_Per_Object_Constraint (Choices (C))
7143 Static_Components := False;
7146 elsif Modify_Tree_For_C
7147 and then Nkind (Expr_Q) = N_Identifier
7148 and then Is_Array_Type (Etype (Expr_Q))
7150 Static_Components := False;
7154 if Is_Elementary_Type (Etype (Expr_Q)) then
7155 if not Compile_Time_Known_Value (Expr_Q) then
7156 Static_Components := False;
7159 elsif not Compile_Time_Known_Composite_Value (Expr_Q) then
7160 Static_Components := False;
7162 if Is_Private_Type (Etype (Expr_Q))
7163 and then Has_Discriminants (Etype (Expr_Q))
7173 end Component_Not_OK_For_Backend;
7175 -------------------------------
7176 -- Has_Per_Object_Constraint --
7177 -------------------------------
7179 function Has_Per_Object_Constraint (L : List_Id) return Boolean is
7180 N : Node_Id := First (L);
7182 while Present (N) loop
7183 if Is_Entity_Name (N)
7184 and then Present (Entity (N))
7185 and then Has_Per_Object_Constraint (Entity (N))
7194 end Has_Per_Object_Constraint;
7196 -----------------------------------
7197 -- Has_Visible_Private_Ancestor --
7198 -----------------------------------
7200 function Has_Visible_Private_Ancestor (Id : E) return Boolean is
7201 R : constant Entity_Id := Root_Type (Id);
7202 T1 : Entity_Id := Id;
7206 if Is_Private_Type (T1) then
7216 end Has_Visible_Private_Ancestor;
7218 -------------------------
7219 -- Top_Level_Aggregate --
7220 -------------------------
7222 function Top_Level_Aggregate (N : Node_Id) return Node_Id is
7227 while Present (Parent (Aggr))
7228 and then Nkind_In (Parent (Aggr), N_Aggregate,
7229 N_Component_Association)
7231 Aggr := Parent (Aggr);
7235 end Top_Level_Aggregate;
7239 Top_Level_Aggr : constant Node_Id := Top_Level_Aggregate (N);
7241 -- Start of processing for Expand_Record_Aggregate
7244 -- If the aggregate is to be assigned to an atomic/VFA variable, we have
7245 -- to prevent a piecemeal assignment even if the aggregate is to be
7246 -- expanded. We create a temporary for the aggregate, and assign the
7247 -- temporary instead, so that the back end can generate an atomic move
7250 if Is_Atomic_VFA_Aggregate (N) then
7253 -- No special management required for aggregates used to initialize
7254 -- statically allocated dispatch tables
7256 elsif Is_Static_Dispatch_Table_Aggregate (N) then
7260 -- Ada 2005 (AI-318-2): We need to convert to assignments if components
7261 -- are build-in-place function calls. The assignments will each turn
7262 -- into a build-in-place function call. If components are all static,
7263 -- we can pass the aggregate to the backend regardless of limitedness.
7265 -- Extension aggregates, aggregates in extended return statements, and
7266 -- aggregates for C++ imported types must be expanded.
7268 if Ada_Version >= Ada_2005 and then Is_Limited_View (Typ) then
7269 if not Nkind_In (Parent (N), N_Component_Association,
7270 N_Object_Declaration)
7272 Convert_To_Assignments (N, Typ);
7274 elsif Nkind (N) = N_Extension_Aggregate
7275 or else Convention (Typ) = Convention_CPP
7277 Convert_To_Assignments (N, Typ);
7279 elsif not Size_Known_At_Compile_Time (Typ)
7280 or else Component_Not_OK_For_Backend
7281 or else not Static_Components
7283 Convert_To_Assignments (N, Typ);
7285 -- In all other cases, build a proper aggregate to be handled by
7289 Build_Back_End_Aggregate;
7292 -- Gigi doesn't properly handle temporaries of variable size so we
7293 -- generate it in the front-end
7295 elsif not Size_Known_At_Compile_Time (Typ)
7296 and then Tagged_Type_Expansion
7298 Convert_To_Assignments (N, Typ);
7300 -- An aggregate used to initialize a controlled object must be turned
7301 -- into component assignments as the components themselves may require
7302 -- finalization actions such as adjustment.
7304 elsif Needs_Finalization (Typ) then
7305 Convert_To_Assignments (N, Typ);
7307 -- Ada 2005 (AI-287): In case of default initialized components we
7308 -- convert the aggregate into assignments.
7310 elsif Has_Default_Init_Comps (N) then
7311 Convert_To_Assignments (N, Typ);
7315 elsif Component_Not_OK_For_Backend then
7316 Convert_To_Assignments (N, Typ);
7318 -- If an ancestor is private, some components are not inherited and we
7319 -- cannot expand into a record aggregate.
7321 elsif Has_Visible_Private_Ancestor (Typ) then
7322 Convert_To_Assignments (N, Typ);
7324 -- ??? The following was done to compile fxacc00.ads in the ACVCs. Gigi
7325 -- is not able to handle the aggregate for Late_Request.
7327 elsif Is_Tagged_Type (Typ) and then Has_Discriminants (Typ) then
7328 Convert_To_Assignments (N, Typ);
7330 -- If the tagged types covers interface types we need to initialize all
7331 -- hidden components containing pointers to secondary dispatch tables.
7333 elsif Is_Tagged_Type (Typ) and then Has_Interfaces (Typ) then
7334 Convert_To_Assignments (N, Typ);
7336 -- If some components are mutable, the size of the aggregate component
7337 -- may be distinct from the default size of the type component, so
7338 -- we need to expand to insure that the back-end copies the proper
7339 -- size of the data. However, if the aggregate is the initial value of
7340 -- a constant, the target is immutable and might be built statically
7341 -- if components are appropriate.
7343 elsif Has_Mutable_Components (Typ)
7345 (Nkind (Parent (Top_Level_Aggr)) /= N_Object_Declaration
7346 or else not Constant_Present (Parent (Top_Level_Aggr))
7347 or else not Static_Components)
7349 Convert_To_Assignments (N, Typ);
7351 -- If the type involved has bit aligned components, then we are not sure
7352 -- that the back end can handle this case correctly.
7354 elsif Type_May_Have_Bit_Aligned_Components (Typ) then
7355 Convert_To_Assignments (N, Typ);
7357 -- When generating C, only generate an aggregate when declaring objects
7358 -- since C does not support aggregates in e.g. assignment statements.
7360 elsif Modify_Tree_For_C and then not In_Object_Declaration (N) then
7361 Convert_To_Assignments (N, Typ);
7363 -- In all other cases, build a proper aggregate to be handled by gigi
7366 Build_Back_End_Aggregate;
7368 end Expand_Record_Aggregate;
7370 ----------------------------
7371 -- Has_Default_Init_Comps --
7372 ----------------------------
7374 function Has_Default_Init_Comps (N : Node_Id) return Boolean is
7375 Comps : constant List_Id := Component_Associations (N);
7380 pragma Assert (Nkind_In (N, N_Aggregate, N_Extension_Aggregate));
7386 if Has_Self_Reference (N) then
7390 -- Check if any direct component has default initialized components
7393 while Present (C) loop
7394 if Box_Present (C) then
7401 -- Recursive call in case of aggregate expression
7404 while Present (C) loop
7405 Expr := Expression (C);
7408 and then Nkind_In (Expr, N_Aggregate, N_Extension_Aggregate)
7409 and then Has_Default_Init_Comps (Expr)
7418 end Has_Default_Init_Comps;
7420 --------------------------
7421 -- Is_Delayed_Aggregate --
7422 --------------------------
7424 function Is_Delayed_Aggregate (N : Node_Id) return Boolean is
7425 Node : Node_Id := N;
7426 Kind : Node_Kind := Nkind (Node);
7429 if Kind = N_Qualified_Expression then
7430 Node := Expression (Node);
7431 Kind := Nkind (Node);
7434 if not Nkind_In (Kind, N_Aggregate, N_Extension_Aggregate) then
7437 return Expansion_Delayed (Node);
7439 end Is_Delayed_Aggregate;
7441 ---------------------------
7442 -- In_Object_Declaration --
7443 ---------------------------
7445 function In_Object_Declaration (N : Node_Id) return Boolean is
7446 P : Node_Id := Parent (N);
7448 while Present (P) loop
7449 if Nkind (P) = N_Object_Declaration then
7457 end In_Object_Declaration;
7459 ----------------------------------------
7460 -- Is_Static_Dispatch_Table_Aggregate --
7461 ----------------------------------------
7463 function Is_Static_Dispatch_Table_Aggregate (N : Node_Id) return Boolean is
7464 Typ : constant Entity_Id := Base_Type (Etype (N));
7467 return Static_Dispatch_Tables
7468 and then Tagged_Type_Expansion
7469 and then RTU_Loaded (Ada_Tags)
7471 -- Avoid circularity when rebuilding the compiler
7473 and then Cunit_Entity (Get_Source_Unit (N)) /= RTU_Entity (Ada_Tags)
7474 and then (Typ = RTE (RE_Dispatch_Table_Wrapper)
7476 Typ = RTE (RE_Address_Array)
7478 Typ = RTE (RE_Type_Specific_Data)
7480 Typ = RTE (RE_Tag_Table)
7482 (RTE_Available (RE_Interface_Data)
7483 and then Typ = RTE (RE_Interface_Data))
7485 (RTE_Available (RE_Interfaces_Array)
7486 and then Typ = RTE (RE_Interfaces_Array))
7488 (RTE_Available (RE_Interface_Data_Element)
7489 and then Typ = RTE (RE_Interface_Data_Element)));
7490 end Is_Static_Dispatch_Table_Aggregate;
7492 -----------------------------
7493 -- Is_Two_Dim_Packed_Array --
7494 -----------------------------
7496 function Is_Two_Dim_Packed_Array (Typ : Entity_Id) return Boolean is
7497 C : constant Int := UI_To_Int (Component_Size (Typ));
7499 return Number_Dimensions (Typ) = 2
7500 and then Is_Bit_Packed_Array (Typ)
7501 and then (C = 1 or else C = 2 or else C = 4);
7502 end Is_Two_Dim_Packed_Array;
7504 --------------------
7505 -- Late_Expansion --
7506 --------------------
7508 function Late_Expansion
7511 Target : Node_Id) return List_Id
7513 Aggr_Code : List_Id;
7516 if Is_Array_Type (Etype (N)) then
7518 Build_Array_Aggr_Code
7520 Ctype => Component_Type (Etype (N)),
7521 Index => First_Index (Typ),
7523 Scalar_Comp => Is_Scalar_Type (Component_Type (Typ)),
7524 Indexes => No_List);
7526 -- Directly or indirectly (e.g. access protected procedure) a record
7529 Aggr_Code := Build_Record_Aggr_Code (N, Typ, Target);
7532 -- Save the last assignment statement associated with the aggregate
7533 -- when building a controlled object. This reference is utilized by
7534 -- the finalization machinery when marking an object as successfully
7537 if Needs_Finalization (Typ)
7538 and then Is_Entity_Name (Target)
7539 and then Present (Entity (Target))
7540 and then Ekind_In (Entity (Target), E_Constant, E_Variable)
7542 Set_Last_Aggregate_Assignment (Entity (Target), Last (Aggr_Code));
7548 ----------------------------------
7549 -- Make_OK_Assignment_Statement --
7550 ----------------------------------
7552 function Make_OK_Assignment_Statement
7555 Expression : Node_Id) return Node_Id
7558 Set_Assignment_OK (Name);
7559 return Make_Assignment_Statement (Sloc, Name, Expression);
7560 end Make_OK_Assignment_Statement;
7562 -----------------------
7563 -- Number_Of_Choices --
7564 -----------------------
7566 function Number_Of_Choices (N : Node_Id) return Nat is
7570 Nb_Choices : Nat := 0;
7573 if Present (Expressions (N)) then
7577 Assoc := First (Component_Associations (N));
7578 while Present (Assoc) loop
7579 Choice := First (Choice_List (Assoc));
7580 while Present (Choice) loop
7581 if Nkind (Choice) /= N_Others_Choice then
7582 Nb_Choices := Nb_Choices + 1;
7592 end Number_Of_Choices;
7594 ------------------------------------
7595 -- Packed_Array_Aggregate_Handled --
7596 ------------------------------------
7598 -- The current version of this procedure will handle at compile time
7599 -- any array aggregate that meets these conditions:
7601 -- One and two dimensional, bit packed
7602 -- Underlying packed type is modular type
7603 -- Bounds are within 32-bit Int range
7604 -- All bounds and values are static
7606 -- Note: for now, in the 2-D case, we only handle component sizes of
7607 -- 1, 2, 4 (cases where an integral number of elements occupies a byte).
7609 function Packed_Array_Aggregate_Handled (N : Node_Id) return Boolean is
7610 Loc : constant Source_Ptr := Sloc (N);
7611 Typ : constant Entity_Id := Etype (N);
7612 Ctyp : constant Entity_Id := Component_Type (Typ);
7614 Not_Handled : exception;
7615 -- Exception raised if this aggregate cannot be handled
7618 -- Handle one- or two dimensional bit packed array
7620 if not Is_Bit_Packed_Array (Typ)
7621 or else Number_Dimensions (Typ) > 2
7626 -- If two-dimensional, check whether it can be folded, and transformed
7627 -- into a one-dimensional aggregate for the Packed_Array_Impl_Type of
7628 -- the original type.
7630 if Number_Dimensions (Typ) = 2 then
7631 return Two_Dim_Packed_Array_Handled (N);
7634 if not Is_Modular_Integer_Type (Packed_Array_Impl_Type (Typ)) then
7638 if not Is_Scalar_Type (Component_Type (Typ))
7639 and then Has_Non_Standard_Rep (Component_Type (Typ))
7645 Csiz : constant Nat := UI_To_Int (Component_Size (Typ));
7649 -- Bounds of index type
7653 -- Values of bounds if compile time known
7655 function Get_Component_Val (N : Node_Id) return Uint;
7656 -- Given a expression value N of the component type Ctyp, returns a
7657 -- value of Csiz (component size) bits representing this value. If
7658 -- the value is non-static or any other reason exists why the value
7659 -- cannot be returned, then Not_Handled is raised.
7661 -----------------------
7662 -- Get_Component_Val --
7663 -----------------------
7665 function Get_Component_Val (N : Node_Id) return Uint is
7669 -- We have to analyze the expression here before doing any further
7670 -- processing here. The analysis of such expressions is deferred
7671 -- till expansion to prevent some problems of premature analysis.
7673 Analyze_And_Resolve (N, Ctyp);
7675 -- Must have a compile time value. String literals have to be
7676 -- converted into temporaries as well, because they cannot easily
7677 -- be converted into their bit representation.
7679 if not Compile_Time_Known_Value (N)
7680 or else Nkind (N) = N_String_Literal
7685 Val := Expr_Rep_Value (N);
7687 -- Adjust for bias, and strip proper number of bits
7689 if Has_Biased_Representation (Ctyp) then
7690 Val := Val - Expr_Value (Type_Low_Bound (Ctyp));
7693 return Val mod Uint_2 ** Csiz;
7694 end Get_Component_Val;
7696 -- Here we know we have a one dimensional bit packed array
7699 Get_Index_Bounds (First_Index (Typ), Lo, Hi);
7701 -- Cannot do anything if bounds are dynamic
7703 if not Compile_Time_Known_Value (Lo)
7705 not Compile_Time_Known_Value (Hi)
7710 -- Or are silly out of range of int bounds
7712 Lob := Expr_Value (Lo);
7713 Hib := Expr_Value (Hi);
7715 if not UI_Is_In_Int_Range (Lob)
7717 not UI_Is_In_Int_Range (Hib)
7722 -- At this stage we have a suitable aggregate for handling at compile
7723 -- time. The only remaining checks are that the values of expressions
7724 -- in the aggregate are compile-time known (checks are performed by
7725 -- Get_Component_Val), and that any subtypes or ranges are statically
7728 -- If the aggregate is not fully positional at this stage, then
7729 -- convert it to positional form. Either this will fail, in which
7730 -- case we can do nothing, or it will succeed, in which case we have
7731 -- succeeded in handling the aggregate and transforming it into a
7732 -- modular value, or it will stay an aggregate, in which case we
7733 -- have failed to create a packed value for it.
7735 if Present (Component_Associations (N)) then
7736 Convert_To_Positional
7737 (N, Max_Others_Replicate => 64, Handle_Bit_Packed => True);
7738 return Nkind (N) /= N_Aggregate;
7741 -- Otherwise we are all positional, so convert to proper value
7744 Lov : constant Int := UI_To_Int (Lob);
7745 Hiv : constant Int := UI_To_Int (Hib);
7747 Len : constant Nat := Int'Max (0, Hiv - Lov + 1);
7748 -- The length of the array (number of elements)
7750 Aggregate_Val : Uint;
7751 -- Value of aggregate. The value is set in the low order bits of
7752 -- this value. For the little-endian case, the values are stored
7753 -- from low-order to high-order and for the big-endian case the
7754 -- values are stored from high-order to low-order. Note that gigi
7755 -- will take care of the conversions to left justify the value in
7756 -- the big endian case (because of left justified modular type
7757 -- processing), so we do not have to worry about that here.
7760 -- Integer literal for resulting constructed value
7763 -- Shift count from low order for next value
7766 -- Shift increment for loop
7769 -- Next expression from positional parameters of aggregate
7771 Left_Justified : Boolean;
7772 -- Set True if we are filling the high order bits of the target
7773 -- value (i.e. the value is left justified).
7776 -- For little endian, we fill up the low order bits of the target
7777 -- value. For big endian we fill up the high order bits of the
7778 -- target value (which is a left justified modular value).
7780 Left_Justified := Bytes_Big_Endian;
7782 -- Switch justification if using -gnatd8
7784 if Debug_Flag_8 then
7785 Left_Justified := not Left_Justified;
7788 -- Switch justfification if reverse storage order
7790 if Reverse_Storage_Order (Base_Type (Typ)) then
7791 Left_Justified := not Left_Justified;
7794 if Left_Justified then
7795 Shift := Csiz * (Len - 1);
7802 -- Loop to set the values
7805 Aggregate_Val := Uint_0;
7807 Expr := First (Expressions (N));
7808 Aggregate_Val := Get_Component_Val (Expr) * Uint_2 ** Shift;
7810 for J in 2 .. Len loop
7811 Shift := Shift + Incr;
7814 Aggregate_Val + Get_Component_Val (Expr) * Uint_2 ** Shift;
7818 -- Now we can rewrite with the proper value
7820 Lit := Make_Integer_Literal (Loc, Intval => Aggregate_Val);
7821 Set_Print_In_Hex (Lit);
7823 -- Construct the expression using this literal. Note that it is
7824 -- important to qualify the literal with its proper modular type
7825 -- since universal integer does not have the required range and
7826 -- also this is a left justified modular type, which is important
7827 -- in the big-endian case.
7830 Unchecked_Convert_To (Typ,
7831 Make_Qualified_Expression (Loc,
7833 New_Occurrence_Of (Packed_Array_Impl_Type (Typ), Loc),
7834 Expression => Lit)));
7836 Analyze_And_Resolve (N, Typ);
7844 end Packed_Array_Aggregate_Handled;
7846 ----------------------------
7847 -- Has_Mutable_Components --
7848 ----------------------------
7850 function Has_Mutable_Components (Typ : Entity_Id) return Boolean is
7854 Comp := First_Component (Typ);
7855 while Present (Comp) loop
7856 if Is_Record_Type (Etype (Comp))
7857 and then Has_Discriminants (Etype (Comp))
7858 and then not Is_Constrained (Etype (Comp))
7863 Next_Component (Comp);
7867 end Has_Mutable_Components;
7869 ------------------------------
7870 -- Initialize_Discriminants --
7871 ------------------------------
7873 procedure Initialize_Discriminants (N : Node_Id; Typ : Entity_Id) is
7874 Loc : constant Source_Ptr := Sloc (N);
7875 Bas : constant Entity_Id := Base_Type (Typ);
7876 Par : constant Entity_Id := Etype (Bas);
7877 Decl : constant Node_Id := Parent (Par);
7881 if Is_Tagged_Type (Bas)
7882 and then Is_Derived_Type (Bas)
7883 and then Has_Discriminants (Par)
7884 and then Has_Discriminants (Bas)
7885 and then Number_Discriminants (Bas) /= Number_Discriminants (Par)
7886 and then Nkind (Decl) = N_Full_Type_Declaration
7887 and then Nkind (Type_Definition (Decl)) = N_Record_Definition
7889 Present (Variant_Part (Component_List (Type_Definition (Decl))))
7890 and then Nkind (N) /= N_Extension_Aggregate
7893 -- Call init proc to set discriminants.
7894 -- There should eventually be a special procedure for this ???
7896 Ref := New_Occurrence_Of (Defining_Identifier (N), Loc);
7897 Insert_Actions_After (N,
7898 Build_Initialization_Call (Sloc (N), Ref, Typ));
7900 end Initialize_Discriminants;
7907 (Obj_Type : Entity_Id;
7908 Typ : Entity_Id) return Boolean
7910 L1, L2, H1, H2 : Node_Id;
7913 -- No sliding if the type of the object is not established yet, if it is
7914 -- an unconstrained type whose actual subtype comes from the aggregate,
7915 -- or if the two types are identical.
7917 if not Is_Array_Type (Obj_Type) then
7920 elsif not Is_Constrained (Obj_Type) then
7923 elsif Typ = Obj_Type then
7927 -- Sliding can only occur along the first dimension
7929 Get_Index_Bounds (First_Index (Typ), L1, H1);
7930 Get_Index_Bounds (First_Index (Obj_Type), L2, H2);
7932 if not Is_OK_Static_Expression (L1) or else
7933 not Is_OK_Static_Expression (L2) or else
7934 not Is_OK_Static_Expression (H1) or else
7935 not Is_OK_Static_Expression (H2)
7939 return Expr_Value (L1) /= Expr_Value (L2)
7941 Expr_Value (H1) /= Expr_Value (H2);
7946 ---------------------------------
7947 -- Process_Transient_Component --
7948 ---------------------------------
7950 procedure Process_Transient_Component
7952 Comp_Typ : Entity_Id;
7953 Init_Expr : Node_Id;
7954 Fin_Call : out Node_Id;
7955 Hook_Clear : out Node_Id;
7956 Aggr : Node_Id := Empty;
7957 Stmts : List_Id := No_List)
7959 procedure Add_Item (Item : Node_Id);
7960 -- Insert arbitrary node Item into the tree depending on the values of
7967 procedure Add_Item (Item : Node_Id) is
7969 if Present (Aggr) then
7970 Insert_Action (Aggr, Item);
7972 pragma Assert (Present (Stmts));
7973 Append_To (Stmts, Item);
7979 Hook_Assign : Node_Id;
7980 Hook_Decl : Node_Id;
7984 Res_Typ : Entity_Id;
7986 -- Start of processing for Process_Transient_Component
7989 -- Add the access type, which provides a reference to the function
7990 -- result. Generate:
7992 -- type Res_Typ is access all Comp_Typ;
7994 Res_Typ := Make_Temporary (Loc, 'A');
7995 Set_Ekind (Res_Typ, E_General_Access_Type);
7996 Set_Directly_Designated_Type (Res_Typ, Comp_Typ);
7999 (Make_Full_Type_Declaration (Loc,
8000 Defining_Identifier => Res_Typ,
8002 Make_Access_To_Object_Definition (Loc,
8003 All_Present => True,
8004 Subtype_Indication => New_Occurrence_Of (Comp_Typ, Loc))));
8006 -- Add the temporary which captures the result of the function call.
8009 -- Res : constant Res_Typ := Init_Expr'Reference;
8011 -- Note that this temporary is effectively a transient object because
8012 -- its lifetime is bounded by the current array or record component.
8014 Res_Id := Make_Temporary (Loc, 'R');
8015 Set_Ekind (Res_Id, E_Constant);
8016 Set_Etype (Res_Id, Res_Typ);
8018 -- Mark the transient object as successfully processed to avoid double
8021 Set_Is_Finalized_Transient (Res_Id);
8023 -- Signal the general finalization machinery that this transient object
8024 -- should not be considered for finalization actions because its cleanup
8025 -- will be performed by Process_Transient_Component_Completion.
8027 Set_Is_Ignored_Transient (Res_Id);
8030 Make_Object_Declaration (Loc,
8031 Defining_Identifier => Res_Id,
8032 Constant_Present => True,
8033 Object_Definition => New_Occurrence_Of (Res_Typ, Loc),
8035 Make_Reference (Loc, New_Copy_Tree (Init_Expr)));
8037 Add_Item (Res_Decl);
8039 -- Construct all pieces necessary to hook and finalize the transient
8042 Build_Transient_Object_Statements
8043 (Obj_Decl => Res_Decl,
8044 Fin_Call => Fin_Call,
8045 Hook_Assign => Hook_Assign,
8046 Hook_Clear => Hook_Clear,
8047 Hook_Decl => Hook_Decl,
8048 Ptr_Decl => Ptr_Decl);
8050 -- Add the access type which provides a reference to the transient
8051 -- result. Generate:
8053 -- type Ptr_Typ is access all Comp_Typ;
8055 Add_Item (Ptr_Decl);
8057 -- Add the temporary which acts as a hook to the transient result.
8060 -- Hook : Ptr_Typ := null;
8062 Add_Item (Hook_Decl);
8064 -- Attach the transient result to the hook. Generate:
8066 -- Hook := Ptr_Typ (Res);
8068 Add_Item (Hook_Assign);
8070 -- The original initialization expression now references the value of
8071 -- the temporary function result. Generate:
8076 Make_Explicit_Dereference (Loc,
8077 Prefix => New_Occurrence_Of (Res_Id, Loc)));
8078 end Process_Transient_Component;
8080 --------------------------------------------
8081 -- Process_Transient_Component_Completion --
8082 --------------------------------------------
8084 procedure Process_Transient_Component_Completion
8088 Hook_Clear : Node_Id;
8091 Exceptions_OK : constant Boolean :=
8092 not Restriction_Active (No_Exception_Propagation);
8095 pragma Assert (Present (Hook_Clear));
8097 -- Generate the following code if exception propagation is allowed:
8100 -- Abort : constant Boolean := Triggered_By_Abort;
8102 -- Abort : constant Boolean := False; -- no abort
8104 -- E : Exception_Occurrence;
8105 -- Raised : Boolean := False;
8112 -- [Deep_]Finalize (Res.all);
8116 -- if not Raised then
8118 -- Save_Occurrence (E,
8119 -- Get_Curent_Excep.all.all);
8125 -- if Raised and then not Abort then
8126 -- Raise_From_Controlled_Operation (E);
8130 if Exceptions_OK then
8131 Abort_And_Exception : declare
8132 Blk_Decls : constant List_Id := New_List;
8133 Blk_Stmts : constant List_Id := New_List;
8134 Fin_Stmts : constant List_Id := New_List;
8136 Fin_Data : Finalization_Exception_Data;
8139 -- Create the declarations of the two flags and the exception
8142 Build_Object_Declarations (Fin_Data, Blk_Decls, Loc);
8147 if Abort_Allowed then
8148 Append_To (Blk_Stmts,
8149 Build_Runtime_Call (Loc, RE_Abort_Defer));
8152 -- Wrap the hook clear and the finalization call in order to trap
8153 -- a potential exception.
8155 Append_To (Fin_Stmts, Hook_Clear);
8157 if Present (Fin_Call) then
8158 Append_To (Fin_Stmts, Fin_Call);
8161 Append_To (Blk_Stmts,
8162 Make_Block_Statement (Loc,
8163 Handled_Statement_Sequence =>
8164 Make_Handled_Sequence_Of_Statements (Loc,
8165 Statements => Fin_Stmts,
8166 Exception_Handlers => New_List (
8167 Build_Exception_Handler (Fin_Data)))));
8172 if Abort_Allowed then
8173 Append_To (Blk_Stmts,
8174 Build_Runtime_Call (Loc, RE_Abort_Undefer));
8177 -- Reraise the potential exception with a proper "upgrade" to
8178 -- Program_Error if needed.
8180 Append_To (Blk_Stmts, Build_Raise_Statement (Fin_Data));
8182 -- Wrap everything in a block
8185 Make_Block_Statement (Loc,
8186 Declarations => Blk_Decls,
8187 Handled_Statement_Sequence =>
8188 Make_Handled_Sequence_Of_Statements (Loc,
8189 Statements => Blk_Stmts)));
8190 end Abort_And_Exception;
8192 -- Generate the following code if exception propagation is not allowed
8193 -- and aborts are allowed:
8198 -- [Deep_]Finalize (Res.all);
8200 -- Abort_Undefer_Direct;
8203 elsif Abort_Allowed then
8204 Abort_Only : declare
8205 Blk_Stmts : constant List_Id := New_List;
8208 Append_To (Blk_Stmts, Build_Runtime_Call (Loc, RE_Abort_Defer));
8209 Append_To (Blk_Stmts, Hook_Clear);
8211 if Present (Fin_Call) then
8212 Append_To (Blk_Stmts, Fin_Call);
8216 Build_Abort_Undefer_Block (Loc,
8221 -- Otherwise generate:
8224 -- [Deep_]Finalize (Res.all);
8227 Append_To (Stmts, Hook_Clear);
8229 if Present (Fin_Call) then
8230 Append_To (Stmts, Fin_Call);
8233 end Process_Transient_Component_Completion;
8235 ---------------------
8236 -- Sort_Case_Table --
8237 ---------------------
8239 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type) is
8240 L : constant Int := Case_Table'First;
8241 U : constant Int := Case_Table'Last;
8249 T := Case_Table (K + 1);
8253 and then Expr_Value (Case_Table (J - 1).Choice_Lo) >
8254 Expr_Value (T.Choice_Lo)
8256 Case_Table (J) := Case_Table (J - 1);
8260 Case_Table (J) := T;
8263 end Sort_Case_Table;
8265 ----------------------------
8266 -- Static_Array_Aggregate --
8267 ----------------------------
8269 function Static_Array_Aggregate (N : Node_Id) return Boolean is
8270 Bounds : constant Node_Id := Aggregate_Bounds (N);
8272 Typ : constant Entity_Id := Etype (N);
8273 Comp_Type : constant Entity_Id := Component_Type (Typ);
8280 if Is_Tagged_Type (Typ)
8281 or else Is_Controlled (Typ)
8282 or else Is_Packed (Typ)
8288 and then Nkind (Bounds) = N_Range
8289 and then Nkind (Low_Bound (Bounds)) = N_Integer_Literal
8290 and then Nkind (High_Bound (Bounds)) = N_Integer_Literal
8292 Lo := Low_Bound (Bounds);
8293 Hi := High_Bound (Bounds);
8295 if No (Component_Associations (N)) then
8297 -- Verify that all components are static integers
8299 Expr := First (Expressions (N));
8300 while Present (Expr) loop
8301 if Nkind (Expr) /= N_Integer_Literal then
8311 -- We allow only a single named association, either a static
8312 -- range or an others_clause, with a static expression.
8314 Expr := First (Component_Associations (N));
8316 if Present (Expressions (N)) then
8319 elsif Present (Next (Expr)) then
8322 elsif Present (Next (First (Choice_List (Expr)))) then
8326 -- The aggregate is static if all components are literals,
8327 -- or else all its components are static aggregates for the
8328 -- component type. We also limit the size of a static aggregate
8329 -- to prevent runaway static expressions.
8331 if Is_Array_Type (Comp_Type)
8332 or else Is_Record_Type (Comp_Type)
8334 if Nkind (Expression (Expr)) /= N_Aggregate
8336 not Compile_Time_Known_Aggregate (Expression (Expr))
8341 elsif Nkind (Expression (Expr)) /= N_Integer_Literal then
8345 if not Aggr_Size_OK (N, Typ) then
8349 -- Create a positional aggregate with the right number of
8350 -- copies of the expression.
8352 Agg := Make_Aggregate (Sloc (N), New_List, No_List);
8354 for I in UI_To_Int (Intval (Lo)) .. UI_To_Int (Intval (Hi))
8356 Append_To (Expressions (Agg), New_Copy (Expression (Expr)));
8358 -- The copied expression must be analyzed and resolved.
8359 -- Besides setting the type, this ensures that static
8360 -- expressions are appropriately marked as such.
8363 (Last (Expressions (Agg)), Component_Type (Typ));
8366 Set_Aggregate_Bounds (Agg, Bounds);
8367 Set_Etype (Agg, Typ);
8370 Set_Compile_Time_Known_Aggregate (N);
8379 end Static_Array_Aggregate;
8381 ----------------------------------
8382 -- Two_Dim_Packed_Array_Handled --
8383 ----------------------------------
8385 function Two_Dim_Packed_Array_Handled (N : Node_Id) return Boolean is
8386 Loc : constant Source_Ptr := Sloc (N);
8387 Typ : constant Entity_Id := Etype (N);
8388 Ctyp : constant Entity_Id := Component_Type (Typ);
8389 Comp_Size : constant Int := UI_To_Int (Component_Size (Typ));
8390 Packed_Array : constant Entity_Id :=
8391 Packed_Array_Impl_Type (Base_Type (Typ));
8394 -- Expression in original aggregate
8397 -- One-dimensional subaggregate
8401 -- For now, only deal with cases where an integral number of elements
8402 -- fit in a single byte. This includes the most common boolean case.
8404 if not (Comp_Size = 1 or else
8405 Comp_Size = 2 or else
8411 Convert_To_Positional
8412 (N, Max_Others_Replicate => 64, Handle_Bit_Packed => True);
8414 -- Verify that all components are static
8416 if Nkind (N) = N_Aggregate
8417 and then Compile_Time_Known_Aggregate (N)
8421 -- The aggregate may have been reanalyzed and converted already
8423 elsif Nkind (N) /= N_Aggregate then
8426 -- If component associations remain, the aggregate is not static
8428 elsif Present (Component_Associations (N)) then
8432 One_Dim := First (Expressions (N));
8433 while Present (One_Dim) loop
8434 if Present (Component_Associations (One_Dim)) then
8438 One_Comp := First (Expressions (One_Dim));
8439 while Present (One_Comp) loop
8440 if not Is_OK_Static_Expression (One_Comp) then
8451 -- Two-dimensional aggregate is now fully positional so pack one
8452 -- dimension to create a static one-dimensional array, and rewrite
8453 -- as an unchecked conversion to the original type.
8456 Byte_Size : constant Int := UI_To_Int (Component_Size (Packed_Array));
8457 -- The packed array type is a byte array
8460 -- Number of components accumulated in current byte
8463 -- Assembled list of packed values for equivalent aggregate
8466 -- Integer value of component
8469 -- Step size for packing
8472 -- Endian-dependent start position for packing
8475 -- Current insertion position
8478 -- Component of packed array being assembled
8485 -- Account for endianness. See corresponding comment in
8486 -- Packed_Array_Aggregate_Handled concerning the following.
8490 xor Reverse_Storage_Order (Base_Type (Typ))
8492 Init_Shift := Byte_Size - Comp_Size;
8499 -- Iterate over each subaggregate
8501 Shift := Init_Shift;
8502 One_Dim := First (Expressions (N));
8503 while Present (One_Dim) loop
8504 One_Comp := First (Expressions (One_Dim));
8505 while Present (One_Comp) loop
8506 if Packed_Num = Byte_Size / Comp_Size then
8508 -- Byte is complete, add to list of expressions
8510 Append (Make_Integer_Literal (Sloc (One_Dim), Val), Comps);
8512 Shift := Init_Shift;
8516 Comp_Val := Expr_Rep_Value (One_Comp);
8518 -- Adjust for bias, and strip proper number of bits
8520 if Has_Biased_Representation (Ctyp) then
8521 Comp_Val := Comp_Val - Expr_Value (Type_Low_Bound (Ctyp));
8524 Comp_Val := Comp_Val mod Uint_2 ** Comp_Size;
8525 Val := UI_To_Int (Val + Comp_Val * Uint_2 ** Shift);
8526 Shift := Shift + Incr;
8527 One_Comp := Next (One_Comp);
8528 Packed_Num := Packed_Num + 1;
8532 One_Dim := Next (One_Dim);
8535 if Packed_Num > 0 then
8537 -- Add final incomplete byte if present
8539 Append (Make_Integer_Literal (Sloc (One_Dim), Val), Comps);
8543 Unchecked_Convert_To (Typ,
8544 Make_Qualified_Expression (Loc,
8545 Subtype_Mark => New_Occurrence_Of (Packed_Array, Loc),
8546 Expression => Make_Aggregate (Loc, Expressions => Comps))));
8547 Analyze_And_Resolve (N);
8550 end Two_Dim_Packed_Array_Handled;