1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2017, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Atree; use Atree;
27 with Checks; use Checks;
28 with Debug; use Debug;
29 with Einfo; use Einfo;
30 with Elists; use Elists;
31 with Errout; use Errout;
32 with Expander; use Expander;
33 with Exp_Util; use Exp_Util;
34 with Exp_Ch3; use Exp_Ch3;
35 with Exp_Ch6; use Exp_Ch6;
36 with Exp_Ch7; use Exp_Ch7;
37 with Exp_Ch9; use Exp_Ch9;
38 with Exp_Disp; use Exp_Disp;
39 with Exp_Tss; use Exp_Tss;
40 with Freeze; use Freeze;
41 with Itypes; use Itypes;
43 with Namet; use Namet;
44 with Nmake; use Nmake;
45 with Nlists; use Nlists;
47 with Restrict; use Restrict;
48 with Rident; use Rident;
49 with Rtsfind; use Rtsfind;
50 with Ttypes; use Ttypes;
52 with Sem_Aggr; use Sem_Aggr;
53 with Sem_Aux; use Sem_Aux;
54 with Sem_Ch3; use Sem_Ch3;
55 with Sem_Eval; use Sem_Eval;
56 with Sem_Res; use Sem_Res;
57 with Sem_Util; use Sem_Util;
58 with Sinfo; use Sinfo;
59 with Snames; use Snames;
60 with Stand; use Stand;
61 with Stringt; use Stringt;
62 with Targparm; use Targparm;
63 with Tbuild; use Tbuild;
64 with Uintp; use Uintp;
66 package body Exp_Aggr is
68 type Case_Bounds is record
71 Choice_Node : Node_Id;
74 type Case_Table_Type is array (Nat range <>) of Case_Bounds;
75 -- Table type used by Check_Case_Choices procedure
77 procedure Collect_Initialization_Statements
80 Node_After : Node_Id);
81 -- If Obj is not frozen, collect actions inserted after N until, but not
82 -- including, Node_After, for initialization of Obj, and move them to an
83 -- expression with actions, which becomes the Initialization_Statements for
86 procedure Expand_Delta_Array_Aggregate (N : Node_Id; Deltas : List_Id);
87 procedure Expand_Delta_Record_Aggregate (N : Node_Id; Deltas : List_Id);
89 function Has_Default_Init_Comps (N : Node_Id) return Boolean;
90 -- N is an aggregate (record or array). Checks the presence of default
91 -- initialization (<>) in any component (Ada 2005: AI-287).
93 function In_Object_Declaration (N : Node_Id) return Boolean;
94 -- Return True if N is part of an object declaration, False otherwise
96 function Is_Static_Dispatch_Table_Aggregate (N : Node_Id) return Boolean;
97 -- Returns true if N is an aggregate used to initialize the components
98 -- of a statically allocated dispatch table.
100 function Late_Expansion
103 Target : Node_Id) return List_Id;
104 -- This routine implements top-down expansion of nested aggregates. In
105 -- doing so, it avoids the generation of temporaries at each level. N is
106 -- a nested record or array aggregate with the Expansion_Delayed flag.
107 -- Typ is the expected type of the aggregate. Target is a (duplicatable)
108 -- expression that will hold the result of the aggregate expansion.
110 function Make_OK_Assignment_Statement
113 Expression : Node_Id) return Node_Id;
114 -- This is like Make_Assignment_Statement, except that Assignment_OK
115 -- is set in the left operand. All assignments built by this unit use
116 -- this routine. This is needed to deal with assignments to initialized
117 -- constants that are done in place.
120 (Obj_Type : Entity_Id;
121 Typ : Entity_Id) return Boolean;
122 -- A static array aggregate in an object declaration can in most cases be
123 -- expanded in place. The one exception is when the aggregate is given
124 -- with component associations that specify different bounds from those of
125 -- the type definition in the object declaration. In this pathological
126 -- case the aggregate must slide, and we must introduce an intermediate
127 -- temporary to hold it.
129 -- The same holds in an assignment to one-dimensional array of arrays,
130 -- when a component may be given with bounds that differ from those of the
133 function Number_Of_Choices (N : Node_Id) return Nat;
134 -- Returns the number of discrete choices (not including the others choice
135 -- if present) contained in (sub-)aggregate N.
137 procedure Process_Transient_Component
139 Comp_Typ : Entity_Id;
141 Fin_Call : out Node_Id;
142 Hook_Clear : out Node_Id;
143 Aggr : Node_Id := Empty;
144 Stmts : List_Id := No_List);
145 -- Subsidiary to the expansion of array and record aggregates. Generate
146 -- part of the necessary code to finalize a transient component. Comp_Typ
147 -- is the component type. Init_Expr is the initialization expression of the
148 -- component which is always a function call. Fin_Call is the finalization
149 -- call used to clean up the transient function result. Hook_Clear is the
150 -- hook reset statement. Aggr and Stmts both control the placement of the
151 -- generated code. Aggr is the related aggregate. If present, all code is
152 -- inserted prior to Aggr using Insert_Action. Stmts is the initialization
153 -- statements of the component. If present, all code is added to Stmts.
155 procedure Process_Transient_Component_Completion
159 Hook_Clear : Node_Id;
161 -- Subsidiary to the expansion of array and record aggregates. Generate
162 -- part of the necessary code to finalize a transient component. Aggr is
163 -- the related aggregate. Fin_Clear is the finalization call used to clean
164 -- up the transient component. Hook_Clear is the hook reset statment. Stmts
165 -- is the initialization statement list for the component. All generated
166 -- code is added to Stmts.
168 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type);
169 -- Sort the Case Table using the Lower Bound of each Choice as the key.
170 -- A simple insertion sort is used since the number of choices in a case
171 -- statement of variant part will usually be small and probably in near
174 ------------------------------------------------------
175 -- Local subprograms for Record Aggregate Expansion --
176 ------------------------------------------------------
178 function Build_Record_Aggr_Code
181 Lhs : Node_Id) return List_Id;
182 -- N is an N_Aggregate or an N_Extension_Aggregate. Typ is the type of the
183 -- aggregate. Target is an expression containing the location on which the
184 -- component by component assignments will take place. Returns the list of
185 -- assignments plus all other adjustments needed for tagged and controlled
188 procedure Convert_To_Assignments (N : Node_Id; Typ : Entity_Id);
189 -- N is an N_Aggregate or an N_Extension_Aggregate. Typ is the type of the
190 -- aggregate (which can only be a record type, this procedure is only used
191 -- for record types). Transform the given aggregate into a sequence of
192 -- assignments performed component by component.
194 procedure Expand_Record_Aggregate
196 Orig_Tag : Node_Id := Empty;
197 Parent_Expr : Node_Id := Empty);
198 -- This is the top level procedure for record aggregate expansion.
199 -- Expansion for record aggregates needs expand aggregates for tagged
200 -- record types. Specifically Expand_Record_Aggregate adds the Tag
201 -- field in front of the Component_Association list that was created
202 -- during resolution by Resolve_Record_Aggregate.
204 -- N is the record aggregate node.
205 -- Orig_Tag is the value of the Tag that has to be provided for this
206 -- specific aggregate. It carries the tag corresponding to the type
207 -- of the outermost aggregate during the recursive expansion
208 -- Parent_Expr is the ancestor part of the original extension
211 function Has_Mutable_Components (Typ : Entity_Id) return Boolean;
212 -- Return true if one of the components is of a discriminated type with
213 -- defaults. An aggregate for a type with mutable components must be
214 -- expanded into individual assignments.
216 procedure Initialize_Discriminants (N : Node_Id; Typ : Entity_Id);
217 -- If the type of the aggregate is a type extension with renamed discrimi-
218 -- nants, we must initialize the hidden discriminants of the parent.
219 -- Otherwise, the target object must not be initialized. The discriminants
220 -- are initialized by calling the initialization procedure for the type.
221 -- This is incorrect if the initialization of other components has any
222 -- side effects. We restrict this call to the case where the parent type
223 -- has a variant part, because this is the only case where the hidden
224 -- discriminants are accessed, namely when calling discriminant checking
225 -- functions of the parent type, and when applying a stream attribute to
226 -- an object of the derived type.
228 -----------------------------------------------------
229 -- Local Subprograms for Array Aggregate Expansion --
230 -----------------------------------------------------
232 function Aggr_Size_OK (N : Node_Id; Typ : Entity_Id) return Boolean;
233 -- Very large static aggregates present problems to the back-end, and are
234 -- transformed into assignments and loops. This function verifies that the
235 -- total number of components of an aggregate is acceptable for rewriting
236 -- into a purely positional static form. Aggr_Size_OK must be called before
239 -- This function also detects and warns about one-component aggregates that
240 -- appear in a non-static context. Even if the component value is static,
241 -- such an aggregate must be expanded into an assignment.
243 function Backend_Processing_Possible (N : Node_Id) return Boolean;
244 -- This function checks if array aggregate N can be processed directly
245 -- by the backend. If this is the case, True is returned.
247 function Build_Array_Aggr_Code
252 Scalar_Comp : Boolean;
253 Indexes : List_Id := No_List) return List_Id;
254 -- This recursive routine returns a list of statements containing the
255 -- loops and assignments that are needed for the expansion of the array
258 -- N is the (sub-)aggregate node to be expanded into code. This node has
259 -- been fully analyzed, and its Etype is properly set.
261 -- Index is the index node corresponding to the array subaggregate N
263 -- Into is the target expression into which we are copying the aggregate.
264 -- Note that this node may not have been analyzed yet, and so the Etype
265 -- field may not be set.
267 -- Scalar_Comp is True if the component type of the aggregate is scalar
269 -- Indexes is the current list of expressions used to index the object we
272 procedure Convert_Array_Aggr_In_Allocator
276 -- If the aggregate appears within an allocator and can be expanded in
277 -- place, this routine generates the individual assignments to components
278 -- of the designated object. This is an optimization over the general
279 -- case, where a temporary is first created on the stack and then used to
280 -- construct the allocated object on the heap.
282 procedure Convert_To_Positional
284 Max_Others_Replicate : Nat := 5;
285 Handle_Bit_Packed : Boolean := False);
286 -- If possible, convert named notation to positional notation. This
287 -- conversion is possible only in some static cases. If the conversion is
288 -- possible, then N is rewritten with the analyzed converted aggregate.
289 -- The parameter Max_Others_Replicate controls the maximum number of
290 -- values corresponding to an others choice that will be converted to
291 -- positional notation (the default of 5 is the normal limit, and reflects
292 -- the fact that normally the loop is better than a lot of separate
293 -- assignments). Note that this limit gets overridden in any case if
294 -- either of the restrictions No_Elaboration_Code or No_Implicit_Loops is
295 -- set. The parameter Handle_Bit_Packed is usually set False (since we do
296 -- not expect the back end to handle bit packed arrays, so the normal case
297 -- of conversion is pointless), but in the special case of a call from
298 -- Packed_Array_Aggregate_Handled, we set this parameter to True, since
299 -- these are cases we handle in there.
301 -- It would seem useful to have a higher default for Max_Others_Replicate,
302 -- but aggregates in the compiler make this impossible: the compiler
303 -- bootstrap fails if Max_Others_Replicate is greater than 25. This
306 procedure Expand_Array_Aggregate (N : Node_Id);
307 -- This is the top-level routine to perform array aggregate expansion.
308 -- N is the N_Aggregate node to be expanded.
310 function Is_Two_Dim_Packed_Array (Typ : Entity_Id) return Boolean;
311 -- For two-dimensional packed aggregates with constant bounds and constant
312 -- components, it is preferable to pack the inner aggregates because the
313 -- whole matrix can then be presented to the back-end as a one-dimensional
314 -- list of literals. This is much more efficient than expanding into single
315 -- component assignments. This function determines if the type Typ is for
316 -- an array that is suitable for this optimization: it returns True if Typ
317 -- is a two dimensional bit packed array with component size 1, 2, or 4.
319 function Packed_Array_Aggregate_Handled (N : Node_Id) return Boolean;
320 -- Given an array aggregate, this function handles the case of a packed
321 -- array aggregate with all constant values, where the aggregate can be
322 -- evaluated at compile time. If this is possible, then N is rewritten
323 -- to be its proper compile time value with all the components properly
324 -- assembled. The expression is analyzed and resolved and True is returned.
325 -- If this transformation is not possible, N is unchanged and False is
328 function Two_Dim_Packed_Array_Handled (N : Node_Id) return Boolean;
329 -- If the type of the aggregate is a two-dimensional bit_packed array
330 -- it may be transformed into an array of bytes with constant values,
331 -- and presented to the back-end as a static value. The function returns
332 -- false if this transformation cannot be performed. THis is similar to,
333 -- and reuses part of the machinery in Packed_Array_Aggregate_Handled.
339 function Aggr_Size_OK (N : Node_Id; Typ : Entity_Id) return Boolean is
348 -- Determines the maximum size of an array aggregate produced by
349 -- converting named to positional notation (e.g. from others clauses).
350 -- This avoids running away with attempts to convert huge aggregates,
351 -- which hit memory limits in the backend.
353 function Component_Count (T : Entity_Id) return Nat;
354 -- The limit is applied to the total number of subcomponents that the
355 -- aggregate will have, which is the number of static expressions
356 -- that will appear in the flattened array. This requires a recursive
357 -- computation of the number of scalar components of the structure.
359 ---------------------
360 -- Component_Count --
361 ---------------------
363 function Component_Count (T : Entity_Id) return Nat is
368 if Is_Scalar_Type (T) then
371 elsif Is_Record_Type (T) then
372 Comp := First_Component (T);
373 while Present (Comp) loop
374 Res := Res + Component_Count (Etype (Comp));
375 Next_Component (Comp);
380 elsif Is_Array_Type (T) then
382 Lo : constant Node_Id :=
383 Type_Low_Bound (Etype (First_Index (T)));
384 Hi : constant Node_Id :=
385 Type_High_Bound (Etype (First_Index (T)));
387 Siz : constant Nat := Component_Count (Component_Type (T));
390 -- Check for superflat arrays, i.e. arrays with such bounds
391 -- as 4 .. 2, to insure that this function never returns a
392 -- meaningless negative value.
394 if not Compile_Time_Known_Value (Lo)
395 or else not Compile_Time_Known_Value (Hi)
396 or else Expr_Value (Hi) < Expr_Value (Lo)
401 -- If the number of components is greater than Int'Last,
402 -- then return Int'Last, so caller will return False (Aggr
403 -- size is not OK). Otherwise, UI_To_Int will crash.
406 UI : constant Uint :=
407 Expr_Value (Hi) - Expr_Value (Lo) + 1;
409 if UI_Is_In_Int_Range (UI) then
410 return Siz * UI_To_Int (UI);
419 -- Can only be a null for an access type
425 -- Start of processing for Aggr_Size_OK
428 -- The normal aggregate limit is 50000, but we increase this limit to
429 -- 2**24 (about 16 million) if Restrictions (No_Elaboration_Code) or
430 -- Restrictions (No_Implicit_Loops) is specified, since in either case
431 -- we are at risk of declaring the program illegal because of this
432 -- limit. We also increase the limit when Static_Elaboration_Desired,
433 -- given that this means that objects are intended to be placed in data
436 -- We also increase the limit if the aggregate is for a packed two-
437 -- dimensional array, because if components are static it is much more
438 -- efficient to construct a one-dimensional equivalent array with static
441 -- Conversely, we decrease the maximum size if none of the above
442 -- requirements apply, and if the aggregate has a single component
443 -- association, which will be more efficient if implemented with a loop.
445 -- Finally, we use a small limit in CodePeer mode where we favor loops
446 -- instead of thousands of single assignments (from large aggregates).
448 Max_Aggr_Size := 50000;
450 if CodePeer_Mode then
451 Max_Aggr_Size := 100;
453 elsif Restriction_Active (No_Elaboration_Code)
454 or else Restriction_Active (No_Implicit_Loops)
455 or else Is_Two_Dim_Packed_Array (Typ)
456 or else (Ekind (Current_Scope) = E_Package
457 and then Static_Elaboration_Desired (Current_Scope))
459 Max_Aggr_Size := 2 ** 24;
461 elsif No (Expressions (N))
462 and then No (Next (First (Component_Associations (N))))
464 Max_Aggr_Size := 5000;
467 Siz := Component_Count (Component_Type (Typ));
469 Indx := First_Index (Typ);
470 while Present (Indx) loop
471 Lo := Type_Low_Bound (Etype (Indx));
472 Hi := Type_High_Bound (Etype (Indx));
474 -- Bounds need to be known at compile time
476 if not Compile_Time_Known_Value (Lo)
477 or else not Compile_Time_Known_Value (Hi)
482 Lov := Expr_Value (Lo);
483 Hiv := Expr_Value (Hi);
485 -- A flat array is always safe
491 -- One-component aggregates are suspicious, and if the context type
492 -- is an object declaration with non-static bounds it will trip gcc;
493 -- such an aggregate must be expanded into a single assignment.
495 if Hiv = Lov and then Nkind (Parent (N)) = N_Object_Declaration then
497 Index_Type : constant Entity_Id :=
499 (First_Index (Etype (Defining_Identifier (Parent (N)))));
503 if not Compile_Time_Known_Value (Type_Low_Bound (Index_Type))
504 or else not Compile_Time_Known_Value
505 (Type_High_Bound (Index_Type))
507 if Present (Component_Associations (N)) then
510 (Choice_List (First (Component_Associations (N))));
512 if Is_Entity_Name (Indx)
513 and then not Is_Type (Entity (Indx))
516 ("single component aggregate in "
517 & "non-static context??", Indx);
518 Error_Msg_N ("\maybe subtype name was meant??", Indx);
528 Rng : constant Uint := Hiv - Lov + 1;
531 -- Check if size is too large
533 if not UI_Is_In_Int_Range (Rng) then
537 Siz := Siz * UI_To_Int (Rng);
541 or else Siz > Max_Aggr_Size
546 -- Bounds must be in integer range, for later array construction
548 if not UI_Is_In_Int_Range (Lov)
550 not UI_Is_In_Int_Range (Hiv)
561 ---------------------------------
562 -- Backend_Processing_Possible --
563 ---------------------------------
565 -- Backend processing by Gigi/gcc is possible only if all the following
566 -- conditions are met:
568 -- 1. N is fully positional
570 -- 2. N is not a bit-packed array aggregate;
572 -- 3. The size of N's array type must be known at compile time. Note
573 -- that this implies that the component size is also known
575 -- 4. The array type of N does not follow the Fortran layout convention
576 -- or if it does it must be 1 dimensional.
578 -- 5. The array component type may not be tagged (which could necessitate
579 -- reassignment of proper tags).
581 -- 6. The array component type must not have unaligned bit components
583 -- 7. None of the components of the aggregate may be bit unaligned
586 -- 8. There cannot be delayed components, since we do not know enough
587 -- at this stage to know if back end processing is possible.
589 -- 9. There cannot be any discriminated record components, since the
590 -- back end cannot handle this complex case.
592 -- 10. No controlled actions need to be generated for components
594 -- 11. When generating C code, N must be part of a N_Object_Declaration
596 -- 12. When generating C code, N must not include function calls
598 function Backend_Processing_Possible (N : Node_Id) return Boolean is
599 Typ : constant Entity_Id := Etype (N);
600 -- Typ is the correct constrained array subtype of the aggregate
602 function Component_Check (N : Node_Id; Index : Node_Id) return Boolean;
603 -- This routine checks components of aggregate N, enforcing checks
604 -- 1, 7, 8, 9, 11, and 12. In the multidimensional case, these checks
605 -- are performed on subaggregates. The Index value is the current index
606 -- being checked in the multidimensional case.
608 ---------------------
609 -- Component_Check --
610 ---------------------
612 function Component_Check (N : Node_Id; Index : Node_Id) return Boolean is
613 function Ultimate_Original_Expression (N : Node_Id) return Node_Id;
614 -- Given a type conversion or an unchecked type conversion N, return
615 -- its innermost original expression.
617 ----------------------------------
618 -- Ultimate_Original_Expression --
619 ----------------------------------
621 function Ultimate_Original_Expression (N : Node_Id) return Node_Id is
622 Expr : Node_Id := Original_Node (N);
625 while Nkind_In (Expr, N_Type_Conversion,
626 N_Unchecked_Type_Conversion)
628 Expr := Original_Node (Expression (Expr));
632 end Ultimate_Original_Expression;
638 -- Start of processing for Component_Check
641 -- Checks 1: (no component associations)
643 if Present (Component_Associations (N)) then
647 -- Checks 11: The C code generator cannot handle aggregates that are
648 -- not part of an object declaration.
650 if Modify_Tree_For_C then
652 Par : Node_Id := Parent (N);
655 -- Skip enclosing nested aggregates and their qualified
658 while Nkind (Par) = N_Aggregate
659 or else Nkind (Par) = N_Qualified_Expression
664 if Nkind (Par) /= N_Object_Declaration then
670 -- Checks on components
672 -- Recurse to check subaggregates, which may appear in qualified
673 -- expressions. If delayed, the front-end will have to expand.
674 -- If the component is a discriminated record, treat as non-static,
675 -- as the back-end cannot handle this properly.
677 Expr := First (Expressions (N));
678 while Present (Expr) loop
680 -- Checks 8: (no delayed components)
682 if Is_Delayed_Aggregate (Expr) then
686 -- Checks 9: (no discriminated records)
688 if Present (Etype (Expr))
689 and then Is_Record_Type (Etype (Expr))
690 and then Has_Discriminants (Etype (Expr))
695 -- Checks 7. Component must not be bit aligned component
697 if Possible_Bit_Aligned_Component (Expr) then
701 -- Checks 12: (no function call)
705 Nkind (Ultimate_Original_Expression (Expr)) = N_Function_Call
710 -- Recursion to following indexes for multiple dimension case
712 if Present (Next_Index (Index))
713 and then not Component_Check (Expr, Next_Index (Index))
718 -- All checks for that component finished, on to next
726 -- Start of processing for Backend_Processing_Possible
729 -- Checks 2 (array not bit packed) and 10 (no controlled actions)
731 if Is_Bit_Packed_Array (Typ) or else Needs_Finalization (Typ) then
735 -- If component is limited, aggregate must be expanded because each
736 -- component assignment must be built in place.
738 if Is_Limited_View (Component_Type (Typ)) then
742 -- Checks 4 (array must not be multidimensional Fortran case)
744 if Convention (Typ) = Convention_Fortran
745 and then Number_Dimensions (Typ) > 1
750 -- Checks 3 (size of array must be known at compile time)
752 if not Size_Known_At_Compile_Time (Typ) then
756 -- Checks on components
758 if not Component_Check (N, First_Index (Typ)) then
762 -- Checks 5 (if the component type is tagged, then we may need to do
763 -- tag adjustments. Perhaps this should be refined to check for any
764 -- component associations that actually need tag adjustment, similar
765 -- to the test in Component_Not_OK_For_Backend for record aggregates
766 -- with tagged components, but not clear whether it's worthwhile ???;
767 -- in the case of virtual machines (no Tagged_Type_Expansion), object
768 -- tags are handled implicitly).
770 if Is_Tagged_Type (Component_Type (Typ))
771 and then Tagged_Type_Expansion
776 -- Checks 6 (component type must not have bit aligned components)
778 if Type_May_Have_Bit_Aligned_Components (Component_Type (Typ)) then
782 -- Backend processing is possible
784 Set_Size_Known_At_Compile_Time (Etype (N), True);
786 end Backend_Processing_Possible;
788 ---------------------------
789 -- Build_Array_Aggr_Code --
790 ---------------------------
792 -- The code that we generate from a one dimensional aggregate is
794 -- 1. If the subaggregate contains discrete choices we
796 -- (a) Sort the discrete choices
798 -- (b) Otherwise for each discrete choice that specifies a range we
799 -- emit a loop. If a range specifies a maximum of three values, or
800 -- we are dealing with an expression we emit a sequence of
801 -- assignments instead of a loop.
803 -- (c) Generate the remaining loops to cover the others choice if any
805 -- 2. If the aggregate contains positional elements we
807 -- (a) translate the positional elements in a series of assignments
809 -- (b) Generate a final loop to cover the others choice if any.
810 -- Note that this final loop has to be a while loop since the case
812 -- L : Integer := Integer'Last;
813 -- H : Integer := Integer'Last;
814 -- A : array (L .. H) := (1, others =>0);
816 -- cannot be handled by a for loop. Thus for the following
818 -- array (L .. H) := (.. positional elements.., others =>E);
820 -- we always generate something like:
822 -- J : Index_Type := Index_Of_Last_Positional_Element;
824 -- J := Index_Base'Succ (J)
828 function Build_Array_Aggr_Code
833 Scalar_Comp : Boolean;
834 Indexes : List_Id := No_List) return List_Id
836 Loc : constant Source_Ptr := Sloc (N);
837 Index_Base : constant Entity_Id := Base_Type (Etype (Index));
838 Index_Base_L : constant Node_Id := Type_Low_Bound (Index_Base);
839 Index_Base_H : constant Node_Id := Type_High_Bound (Index_Base);
841 function Add (Val : Int; To : Node_Id) return Node_Id;
842 -- Returns an expression where Val is added to expression To, unless
843 -- To+Val is provably out of To's base type range. To must be an
844 -- already analyzed expression.
846 function Empty_Range (L, H : Node_Id) return Boolean;
847 -- Returns True if the range defined by L .. H is certainly empty
849 function Equal (L, H : Node_Id) return Boolean;
850 -- Returns True if L = H for sure
852 function Index_Base_Name return Node_Id;
853 -- Returns a new reference to the index type name
858 In_Loop : Boolean := False) return List_Id;
859 -- Ind must be a side-effect-free expression. If the input aggregate N
860 -- to Build_Loop contains no subaggregates, then this function returns
861 -- the assignment statement:
863 -- Into (Indexes, Ind) := Expr;
865 -- Otherwise we call Build_Code recursively. Flag In_Loop should be set
866 -- when the assignment appears within a generated loop.
868 -- Ada 2005 (AI-287): In case of default initialized component, Expr
869 -- is empty and we generate a call to the corresponding IP subprogram.
871 function Gen_Loop (L, H : Node_Id; Expr : Node_Id) return List_Id;
872 -- Nodes L and H must be side-effect-free expressions. If the input
873 -- aggregate N to Build_Loop contains no subaggregates, this routine
874 -- returns the for loop statement:
876 -- for J in Index_Base'(L) .. Index_Base'(H) loop
877 -- Into (Indexes, J) := Expr;
880 -- Otherwise we call Build_Code recursively. As an optimization if the
881 -- loop covers 3 or fewer scalar elements we generate a sequence of
883 -- If the component association that generates the loop comes from an
884 -- Iterated_Component_Association, the loop parameter has the name of
885 -- the corresponding parameter in the original construct.
887 function Gen_While (L, H : Node_Id; Expr : Node_Id) return List_Id;
888 -- Nodes L and H must be side-effect-free expressions. If the input
889 -- aggregate N to Build_Loop contains no subaggregates, this routine
890 -- returns the while loop statement:
892 -- J : Index_Base := L;
894 -- J := Index_Base'Succ (J);
895 -- Into (Indexes, J) := Expr;
898 -- Otherwise we call Build_Code recursively
900 function Get_Assoc_Expr (Assoc : Node_Id) return Node_Id;
901 -- For an association with a box, use value given by aspect
902 -- Default_Component_Value of array type if specified, else use
903 -- value given by aspect Default_Value for component type itself
904 -- if specified, else return Empty.
906 function Local_Compile_Time_Known_Value (E : Node_Id) return Boolean;
907 function Local_Expr_Value (E : Node_Id) return Uint;
908 -- These two Local routines are used to replace the corresponding ones
909 -- in sem_eval because while processing the bounds of an aggregate with
910 -- discrete choices whose index type is an enumeration, we build static
911 -- expressions not recognized by Compile_Time_Known_Value as such since
912 -- they have not yet been analyzed and resolved. All the expressions in
913 -- question are things like Index_Base_Name'Val (Const) which we can
914 -- easily recognize as being constant.
920 function Add (Val : Int; To : Node_Id) return Node_Id is
925 U_Val : constant Uint := UI_From_Int (Val);
928 -- Note: do not try to optimize the case of Val = 0, because
929 -- we need to build a new node with the proper Sloc value anyway.
931 -- First test if we can do constant folding
933 if Local_Compile_Time_Known_Value (To) then
934 U_To := Local_Expr_Value (To) + Val;
936 -- Determine if our constant is outside the range of the index.
937 -- If so return an Empty node. This empty node will be caught
938 -- by Empty_Range below.
940 if Compile_Time_Known_Value (Index_Base_L)
941 and then U_To < Expr_Value (Index_Base_L)
945 elsif Compile_Time_Known_Value (Index_Base_H)
946 and then U_To > Expr_Value (Index_Base_H)
951 Expr_Pos := Make_Integer_Literal (Loc, U_To);
952 Set_Is_Static_Expression (Expr_Pos);
954 if not Is_Enumeration_Type (Index_Base) then
957 -- If we are dealing with enumeration return
958 -- Index_Base'Val (Expr_Pos)
962 Make_Attribute_Reference
964 Prefix => Index_Base_Name,
965 Attribute_Name => Name_Val,
966 Expressions => New_List (Expr_Pos));
972 -- If we are here no constant folding possible
974 if not Is_Enumeration_Type (Index_Base) then
977 Left_Opnd => Duplicate_Subexpr (To),
978 Right_Opnd => Make_Integer_Literal (Loc, U_Val));
980 -- If we are dealing with enumeration return
981 -- Index_Base'Val (Index_Base'Pos (To) + Val)
985 Make_Attribute_Reference
987 Prefix => Index_Base_Name,
988 Attribute_Name => Name_Pos,
989 Expressions => New_List (Duplicate_Subexpr (To)));
994 Right_Opnd => Make_Integer_Literal (Loc, U_Val));
997 Make_Attribute_Reference
999 Prefix => Index_Base_Name,
1000 Attribute_Name => Name_Val,
1001 Expressions => New_List (Expr_Pos));
1011 function Empty_Range (L, H : Node_Id) return Boolean is
1012 Is_Empty : Boolean := False;
1017 -- First check if L or H were already detected as overflowing the
1018 -- index base range type by function Add above. If this is so Add
1019 -- returns the empty node.
1021 if No (L) or else No (H) then
1025 for J in 1 .. 3 loop
1028 -- L > H range is empty
1034 -- B_L > H range must be empty
1037 Low := Index_Base_L;
1040 -- L > B_H range must be empty
1044 High := Index_Base_H;
1047 if Local_Compile_Time_Known_Value (Low)
1049 Local_Compile_Time_Known_Value (High)
1052 UI_Gt (Local_Expr_Value (Low), Local_Expr_Value (High));
1065 function Equal (L, H : Node_Id) return Boolean is
1070 elsif Local_Compile_Time_Known_Value (L)
1072 Local_Compile_Time_Known_Value (H)
1074 return UI_Eq (Local_Expr_Value (L), Local_Expr_Value (H));
1087 In_Loop : Boolean := False) return List_Id
1089 function Add_Loop_Actions (Lis : List_Id) return List_Id;
1090 -- Collect insert_actions generated in the construction of a loop,
1091 -- and prepend them to the sequence of assignments to complete the
1092 -- eventual body of the loop.
1094 procedure Initialize_Array_Component
1095 (Arr_Comp : Node_Id;
1097 Init_Expr : Node_Id;
1099 -- Perform the initialization of array component Arr_Comp with
1100 -- expected type Comp_Typ. Init_Expr denotes the initialization
1101 -- expression of the array component. All generated code is added
1104 procedure Initialize_Ctrl_Array_Component
1105 (Arr_Comp : Node_Id;
1106 Comp_Typ : Entity_Id;
1107 Init_Expr : Node_Id;
1109 -- Perform the initialization of array component Arr_Comp when its
1110 -- expected type Comp_Typ needs finalization actions. Init_Expr is
1111 -- the initialization expression of the array component. All hook-
1112 -- related declarations are inserted prior to aggregate N. Remaining
1113 -- code is added to list Stmts.
1115 ----------------------
1116 -- Add_Loop_Actions --
1117 ----------------------
1119 function Add_Loop_Actions (Lis : List_Id) return List_Id is
1123 -- Ada 2005 (AI-287): Do nothing else in case of default
1124 -- initialized component.
1129 elsif Nkind (Parent (Expr)) = N_Component_Association
1130 and then Present (Loop_Actions (Parent (Expr)))
1132 Append_List (Lis, Loop_Actions (Parent (Expr)));
1133 Res := Loop_Actions (Parent (Expr));
1134 Set_Loop_Actions (Parent (Expr), No_List);
1140 end Add_Loop_Actions;
1142 --------------------------------
1143 -- Initialize_Array_Component --
1144 --------------------------------
1146 procedure Initialize_Array_Component
1147 (Arr_Comp : Node_Id;
1149 Init_Expr : Node_Id;
1152 Exceptions_OK : constant Boolean :=
1153 not Restriction_Active
1154 (No_Exception_Propagation);
1156 Finalization_OK : constant Boolean :=
1158 and then Needs_Finalization (Comp_Typ);
1160 Full_Typ : constant Entity_Id := Underlying_Type (Comp_Typ);
1162 Blk_Stmts : List_Id;
1163 Init_Stmt : Node_Id;
1166 -- Protect the initialization statements from aborts. Generate:
1170 if Finalization_OK and Abort_Allowed then
1171 if Exceptions_OK then
1172 Blk_Stmts := New_List;
1177 Append_To (Blk_Stmts, Build_Runtime_Call (Loc, RE_Abort_Defer));
1179 -- Otherwise aborts are not allowed. All generated code is added
1180 -- directly to the input list.
1186 -- Initialize the array element. Generate:
1188 -- Arr_Comp := Init_Expr;
1190 -- Note that the initialization expression is replicated because
1191 -- it has to be reevaluated within a generated loop.
1194 Make_OK_Assignment_Statement (Loc,
1195 Name => New_Copy_Tree (Arr_Comp),
1196 Expression => New_Copy_Tree (Init_Expr));
1197 Set_No_Ctrl_Actions (Init_Stmt);
1199 -- If this is an aggregate for an array of arrays, each
1200 -- subaggregate will be expanded as well, and even with
1201 -- No_Ctrl_Actions the assignments of inner components will
1202 -- require attachment in their assignments to temporaries. These
1203 -- temporaries must be finalized for each subaggregate. Generate:
1206 -- Arr_Comp := Init_Expr;
1209 if Finalization_OK and then Is_Array_Type (Comp_Typ) then
1211 Make_Block_Statement (Loc,
1212 Handled_Statement_Sequence =>
1213 Make_Handled_Sequence_Of_Statements (Loc,
1214 Statements => New_List (Init_Stmt)));
1217 Append_To (Blk_Stmts, Init_Stmt);
1219 -- Adjust the tag due to a possible view conversion. Generate:
1221 -- Arr_Comp._tag := Full_TypP;
1223 if Tagged_Type_Expansion
1224 and then Present (Comp_Typ)
1225 and then Is_Tagged_Type (Comp_Typ)
1227 Append_To (Blk_Stmts,
1228 Make_OK_Assignment_Statement (Loc,
1230 Make_Selected_Component (Loc,
1231 Prefix => New_Copy_Tree (Arr_Comp),
1234 (First_Tag_Component (Full_Typ), Loc)),
1237 Unchecked_Convert_To (RTE (RE_Tag),
1239 (Node (First_Elmt (Access_Disp_Table (Full_Typ))),
1243 -- Adjust the array component. Controlled subaggregates are not
1244 -- considered because each of their individual elements will
1245 -- receive an adjustment of its own. Generate:
1247 -- [Deep_]Adjust (Arr_Comp);
1250 and then not Is_Limited_Type (Comp_Typ)
1252 (Is_Array_Type (Comp_Typ)
1253 and then Is_Controlled (Component_Type (Comp_Typ))
1254 and then Nkind (Expr) = N_Aggregate)
1258 (Obj_Ref => New_Copy_Tree (Arr_Comp),
1261 -- Guard against a missing [Deep_]Adjust when the component
1262 -- type was not frozen properly.
1264 if Present (Adj_Call) then
1265 Append_To (Blk_Stmts, Adj_Call);
1269 -- Complete the protection of the initialization statements
1271 if Finalization_OK and Abort_Allowed then
1273 -- Wrap the initialization statements in a block to catch a
1274 -- potential exception. Generate:
1278 -- Arr_Comp := Init_Expr;
1279 -- Arr_Comp._tag := Full_TypP;
1280 -- [Deep_]Adjust (Arr_Comp);
1282 -- Abort_Undefer_Direct;
1285 if Exceptions_OK then
1287 Build_Abort_Undefer_Block (Loc,
1291 -- Otherwise exceptions are not propagated. Generate:
1294 -- Arr_Comp := Init_Expr;
1295 -- Arr_Comp._tag := Full_TypP;
1296 -- [Deep_]Adjust (Arr_Comp);
1300 Append_To (Blk_Stmts,
1301 Build_Runtime_Call (Loc, RE_Abort_Undefer));
1304 end Initialize_Array_Component;
1306 -------------------------------------
1307 -- Initialize_Ctrl_Array_Component --
1308 -------------------------------------
1310 procedure Initialize_Ctrl_Array_Component
1311 (Arr_Comp : Node_Id;
1312 Comp_Typ : Entity_Id;
1313 Init_Expr : Node_Id;
1317 Act_Stmts : List_Id;
1320 Hook_Clear : Node_Id;
1322 In_Place_Expansion : Boolean;
1323 -- Flag set when a nonlimited controlled function call requires
1324 -- in-place expansion.
1327 -- Duplicate the initialization expression in case the context is
1328 -- a multi choice list or an "others" choice which plugs various
1329 -- holes in the aggregate. As a result the expression is no longer
1330 -- shared between the various components and is reevaluated for
1331 -- each such component.
1333 Expr := New_Copy_Tree (Init_Expr);
1334 Set_Parent (Expr, Parent (Init_Expr));
1336 -- Perform a preliminary analysis and resolution to determine what
1337 -- the initialization expression denotes. An unanalyzed function
1338 -- call may appear as an identifier or an indexed component.
1340 if Nkind_In (Expr, N_Function_Call,
1342 N_Indexed_Component)
1343 and then not Analyzed (Expr)
1345 Preanalyze_And_Resolve (Expr, Comp_Typ);
1348 In_Place_Expansion :=
1349 Nkind (Expr) = N_Function_Call
1350 and then not Is_Limited_Type (Comp_Typ);
1352 -- The initialization expression is a controlled function call.
1353 -- Perform in-place removal of side effects to avoid creating a
1354 -- transient scope, which leads to premature finalization.
1356 -- This in-place expansion is not performed for limited transient
1357 -- objects because the initialization is already done in-place.
1359 if In_Place_Expansion then
1361 -- Suppress the removal of side effects by general analysis
1362 -- because this behavior is emulated here. This avoids the
1363 -- generation of a transient scope, which leads to out-of-order
1364 -- adjustment and finalization.
1366 Set_No_Side_Effect_Removal (Expr);
1368 -- When the transient component initialization is related to a
1369 -- range or an "others", keep all generated statements within
1370 -- the enclosing loop. This way the controlled function call
1371 -- will be evaluated at each iteration, and its result will be
1372 -- finalized at the end of each iteration.
1378 -- Otherwise this is a single component initialization. Hook-
1379 -- related statements are inserted prior to the aggregate.
1383 Act_Stmts := No_List;
1386 -- Install all hook-related declarations and prepare the clean
1389 Process_Transient_Component
1391 Comp_Typ => Comp_Typ,
1393 Fin_Call => Fin_Call,
1394 Hook_Clear => Hook_Clear,
1396 Stmts => Act_Stmts);
1399 -- Use the noncontrolled component initialization circuitry to
1400 -- assign the result of the function call to the array element.
1401 -- This also performs subaggregate wrapping, tag adjustment, and
1402 -- [deep] adjustment of the array element.
1404 Initialize_Array_Component
1405 (Arr_Comp => Arr_Comp,
1406 Comp_Typ => Comp_Typ,
1410 -- At this point the array element is fully initialized. Complete
1411 -- the processing of the controlled array component by finalizing
1412 -- the transient function result.
1414 if In_Place_Expansion then
1415 Process_Transient_Component_Completion
1418 Fin_Call => Fin_Call,
1419 Hook_Clear => Hook_Clear,
1422 end Initialize_Ctrl_Array_Component;
1426 Stmts : constant List_Id := New_List;
1428 Comp_Typ : Entity_Id := Empty;
1430 Indexed_Comp : Node_Id;
1431 Init_Call : Node_Id;
1432 New_Indexes : List_Id;
1434 -- Start of processing for Gen_Assign
1437 if No (Indexes) then
1438 New_Indexes := New_List;
1440 New_Indexes := New_Copy_List_Tree (Indexes);
1443 Append_To (New_Indexes, Ind);
1445 if Present (Next_Index (Index)) then
1448 Build_Array_Aggr_Code
1451 Index => Next_Index (Index),
1453 Scalar_Comp => Scalar_Comp,
1454 Indexes => New_Indexes));
1457 -- If we get here then we are at a bottom-level (sub-)aggregate
1461 (Make_Indexed_Component (Loc,
1462 Prefix => New_Copy_Tree (Into),
1463 Expressions => New_Indexes));
1465 Set_Assignment_OK (Indexed_Comp);
1467 -- Ada 2005 (AI-287): In case of default initialized component, Expr
1468 -- is not present (and therefore we also initialize Expr_Q to empty).
1472 elsif Nkind (Expr) = N_Qualified_Expression then
1473 Expr_Q := Expression (Expr);
1478 if Present (Etype (N)) and then Etype (N) /= Any_Composite then
1479 Comp_Typ := Component_Type (Etype (N));
1480 pragma Assert (Comp_Typ = Ctype); -- AI-287
1482 elsif Present (Next (First (New_Indexes))) then
1484 -- Ada 2005 (AI-287): Do nothing in case of default initialized
1485 -- component because we have received the component type in
1486 -- the formal parameter Ctype.
1488 -- ??? Some assert pragmas have been added to check if this new
1489 -- formal can be used to replace this code in all cases.
1491 if Present (Expr) then
1493 -- This is a multidimensional array. Recover the component type
1494 -- from the outermost aggregate, because subaggregates do not
1495 -- have an assigned type.
1502 while Present (P) loop
1503 if Nkind (P) = N_Aggregate
1504 and then Present (Etype (P))
1506 Comp_Typ := Component_Type (Etype (P));
1514 pragma Assert (Comp_Typ = Ctype); -- AI-287
1519 -- Ada 2005 (AI-287): We only analyze the expression in case of non-
1520 -- default initialized components (otherwise Expr_Q is not present).
1523 and then Nkind_In (Expr_Q, N_Aggregate, N_Extension_Aggregate)
1525 -- At this stage the Expression may not have been analyzed yet
1526 -- because the array aggregate code has not been updated to use
1527 -- the Expansion_Delayed flag and avoid analysis altogether to
1528 -- solve the same problem (see Resolve_Aggr_Expr). So let us do
1529 -- the analysis of non-array aggregates now in order to get the
1530 -- value of Expansion_Delayed flag for the inner aggregate ???
1532 if Present (Comp_Typ) and then not Is_Array_Type (Comp_Typ) then
1533 Analyze_And_Resolve (Expr_Q, Comp_Typ);
1536 if Is_Delayed_Aggregate (Expr_Q) then
1538 -- This is either a subaggregate of a multidimensional array,
1539 -- or a component of an array type whose component type is
1540 -- also an array. In the latter case, the expression may have
1541 -- component associations that provide different bounds from
1542 -- those of the component type, and sliding must occur. Instead
1543 -- of decomposing the current aggregate assignment, force the
1544 -- reanalysis of the assignment, so that a temporary will be
1545 -- generated in the usual fashion, and sliding will take place.
1547 if Nkind (Parent (N)) = N_Assignment_Statement
1548 and then Is_Array_Type (Comp_Typ)
1549 and then Present (Component_Associations (Expr_Q))
1550 and then Must_Slide (Comp_Typ, Etype (Expr_Q))
1552 Set_Expansion_Delayed (Expr_Q, False);
1553 Set_Analyzed (Expr_Q, False);
1558 Late_Expansion (Expr_Q, Etype (Expr_Q), Indexed_Comp));
1563 if Present (Expr) then
1565 -- Handle an initialization expression of a controlled type in
1566 -- case it denotes a function call. In general such a scenario
1567 -- will produce a transient scope, but this will lead to wrong
1568 -- order of initialization, adjustment, and finalization in the
1569 -- context of aggregates.
1571 -- Target (1) := Ctrl_Func_Call;
1574 -- Trans_Obj : ... := Ctrl_Func_Call; -- object
1575 -- Target (1) := Trans_Obj;
1576 -- Finalize (Trans_Obj);
1578 -- Target (1)._tag := ...;
1579 -- Adjust (Target (1));
1581 -- In the example above, the call to Finalize occurs too early
1582 -- and as a result it may leave the array component in a bad
1583 -- state. Finalization of the transient object should really
1584 -- happen after adjustment.
1586 -- To avoid this scenario, perform in-place side-effect removal
1587 -- of the function call. This eliminates the transient property
1588 -- of the function result and ensures correct order of actions.
1590 -- Res : ... := Ctrl_Func_Call;
1591 -- Target (1) := Res;
1592 -- Target (1)._tag := ...;
1593 -- Adjust (Target (1));
1596 if Present (Comp_Typ)
1597 and then Needs_Finalization (Comp_Typ)
1598 and then Nkind (Expr) /= N_Aggregate
1600 Initialize_Ctrl_Array_Component
1601 (Arr_Comp => Indexed_Comp,
1602 Comp_Typ => Comp_Typ,
1606 -- Otherwise perform simple component initialization
1609 Initialize_Array_Component
1610 (Arr_Comp => Indexed_Comp,
1611 Comp_Typ => Comp_Typ,
1616 -- Ada 2005 (AI-287): In case of default initialized component, call
1617 -- the initialization subprogram associated with the component type.
1618 -- If the component type is an access type, add an explicit null
1619 -- assignment, because for the back-end there is an initialization
1620 -- present for the whole aggregate, and no default initialization
1623 -- In addition, if the component type is controlled, we must call
1624 -- its Initialize procedure explicitly, because there is no explicit
1625 -- object creation that will invoke it otherwise.
1628 if Present (Base_Init_Proc (Base_Type (Ctype)))
1629 or else Has_Task (Base_Type (Ctype))
1631 Append_List_To (Stmts,
1632 Build_Initialization_Call (Loc,
1633 Id_Ref => Indexed_Comp,
1635 With_Default_Init => True));
1637 -- If the component type has invariants, add an invariant
1638 -- check after the component is default-initialized. It will
1639 -- be analyzed and resolved before the code for initialization
1640 -- of other components.
1642 if Has_Invariants (Ctype) then
1643 Set_Etype (Indexed_Comp, Ctype);
1644 Append_To (Stmts, Make_Invariant_Call (Indexed_Comp));
1647 elsif Is_Access_Type (Ctype) then
1649 Make_Assignment_Statement (Loc,
1650 Name => New_Copy_Tree (Indexed_Comp),
1651 Expression => Make_Null (Loc)));
1654 if Needs_Finalization (Ctype) then
1657 (Obj_Ref => New_Copy_Tree (Indexed_Comp),
1660 -- Guard against a missing [Deep_]Initialize when the component
1661 -- type was not properly frozen.
1663 if Present (Init_Call) then
1664 Append_To (Stmts, Init_Call);
1669 return Add_Loop_Actions (Stmts);
1676 function Gen_Loop (L, H : Node_Id; Expr : Node_Id) return List_Id is
1677 Is_Iterated_Component : constant Boolean :=
1678 Nkind (Parent (Expr)) = N_Iterated_Component_Association;
1689 -- Index_Base'(L) .. Index_Base'(H)
1691 L_Iteration_Scheme : Node_Id;
1692 -- L_J in Index_Base'(L) .. Index_Base'(H)
1695 -- The statements to execute in the loop
1697 S : constant List_Id := New_List;
1698 -- List of statements
1701 -- Copy of expression tree, used for checking purposes
1704 -- If loop bounds define an empty range return the null statement
1706 if Empty_Range (L, H) then
1707 Append_To (S, Make_Null_Statement (Loc));
1709 -- Ada 2005 (AI-287): Nothing else need to be done in case of
1710 -- default initialized component.
1716 -- The expression must be type-checked even though no component
1717 -- of the aggregate will have this value. This is done only for
1718 -- actual components of the array, not for subaggregates. Do
1719 -- the check on a copy, because the expression may be shared
1720 -- among several choices, some of which might be non-null.
1722 if Present (Etype (N))
1723 and then Is_Array_Type (Etype (N))
1724 and then No (Next_Index (Index))
1726 Expander_Mode_Save_And_Set (False);
1727 Tcopy := New_Copy_Tree (Expr);
1728 Set_Parent (Tcopy, N);
1729 Analyze_And_Resolve (Tcopy, Component_Type (Etype (N)));
1730 Expander_Mode_Restore;
1736 -- If loop bounds are the same then generate an assignment, unless
1737 -- the parent construct is an Iterated_Component_Association.
1739 elsif Equal (L, H) and then not Is_Iterated_Component then
1740 return Gen_Assign (New_Copy_Tree (L), Expr);
1742 -- If H - L <= 2 then generate a sequence of assignments when we are
1743 -- processing the bottom most aggregate and it contains scalar
1746 elsif No (Next_Index (Index))
1747 and then Scalar_Comp
1748 and then Local_Compile_Time_Known_Value (L)
1749 and then Local_Compile_Time_Known_Value (H)
1750 and then Local_Expr_Value (H) - Local_Expr_Value (L) <= 2
1751 and then not Is_Iterated_Component
1753 Append_List_To (S, Gen_Assign (New_Copy_Tree (L), Expr));
1754 Append_List_To (S, Gen_Assign (Add (1, To => L), Expr));
1756 if Local_Expr_Value (H) - Local_Expr_Value (L) = 2 then
1757 Append_List_To (S, Gen_Assign (Add (2, To => L), Expr));
1763 -- Otherwise construct the loop, starting with the loop index L_J
1765 if Is_Iterated_Component then
1767 Make_Defining_Identifier (Loc,
1768 Chars => (Chars (Defining_Identifier (Parent (Expr)))));
1771 L_J := Make_Temporary (Loc, 'J', L);
1774 -- Construct "L .. H" in Index_Base. We use a qualified expression
1775 -- for the bound to convert to the index base, but we don't need
1776 -- to do that if we already have the base type at hand.
1778 if Etype (L) = Index_Base then
1782 Make_Qualified_Expression (Loc,
1783 Subtype_Mark => Index_Base_Name,
1784 Expression => New_Copy_Tree (L));
1787 if Etype (H) = Index_Base then
1791 Make_Qualified_Expression (Loc,
1792 Subtype_Mark => Index_Base_Name,
1793 Expression => New_Copy_Tree (H));
1801 -- Construct "for L_J in Index_Base range L .. H"
1803 L_Iteration_Scheme :=
1804 Make_Iteration_Scheme
1806 Loop_Parameter_Specification =>
1807 Make_Loop_Parameter_Specification
1809 Defining_Identifier => L_J,
1810 Discrete_Subtype_Definition => L_Range));
1812 -- Construct the statements to execute in the loop body
1815 Gen_Assign (New_Occurrence_Of (L_J, Loc), Expr, In_Loop => True);
1817 -- Construct the final loop
1820 Make_Implicit_Loop_Statement
1822 Identifier => Empty,
1823 Iteration_Scheme => L_Iteration_Scheme,
1824 Statements => L_Body));
1826 -- A small optimization: if the aggregate is initialized with a box
1827 -- and the component type has no initialization procedure, remove the
1828 -- useless empty loop.
1830 if Nkind (First (S)) = N_Loop_Statement
1831 and then Is_Empty_List (Statements (First (S)))
1833 return New_List (Make_Null_Statement (Loc));
1843 -- The code built is
1845 -- W_J : Index_Base := L;
1846 -- while W_J < H loop
1847 -- W_J := Index_Base'Succ (W);
1851 function Gen_While (L, H : Node_Id; Expr : Node_Id) return List_Id is
1855 -- W_J : Base_Type := L;
1857 W_Iteration_Scheme : Node_Id;
1860 W_Index_Succ : Node_Id;
1861 -- Index_Base'Succ (J)
1863 W_Increment : Node_Id;
1864 -- W_J := Index_Base'Succ (W)
1866 W_Body : constant List_Id := New_List;
1867 -- The statements to execute in the loop
1869 S : constant List_Id := New_List;
1870 -- list of statement
1873 -- If loop bounds define an empty range or are equal return null
1875 if Empty_Range (L, H) or else Equal (L, H) then
1876 Append_To (S, Make_Null_Statement (Loc));
1880 -- Build the decl of W_J
1882 W_J := Make_Temporary (Loc, 'J', L);
1884 Make_Object_Declaration
1886 Defining_Identifier => W_J,
1887 Object_Definition => Index_Base_Name,
1890 -- Theoretically we should do a New_Copy_Tree (L) here, but we know
1891 -- that in this particular case L is a fresh Expr generated by
1892 -- Add which we are the only ones to use.
1894 Append_To (S, W_Decl);
1896 -- Construct " while W_J < H"
1898 W_Iteration_Scheme :=
1899 Make_Iteration_Scheme
1901 Condition => Make_Op_Lt
1903 Left_Opnd => New_Occurrence_Of (W_J, Loc),
1904 Right_Opnd => New_Copy_Tree (H)));
1906 -- Construct the statements to execute in the loop body
1909 Make_Attribute_Reference
1911 Prefix => Index_Base_Name,
1912 Attribute_Name => Name_Succ,
1913 Expressions => New_List (New_Occurrence_Of (W_J, Loc)));
1916 Make_OK_Assignment_Statement
1918 Name => New_Occurrence_Of (W_J, Loc),
1919 Expression => W_Index_Succ);
1921 Append_To (W_Body, W_Increment);
1923 Append_List_To (W_Body,
1924 Gen_Assign (New_Occurrence_Of (W_J, Loc), Expr, In_Loop => True));
1926 -- Construct the final loop
1929 Make_Implicit_Loop_Statement
1931 Identifier => Empty,
1932 Iteration_Scheme => W_Iteration_Scheme,
1933 Statements => W_Body));
1938 --------------------
1939 -- Get_Assoc_Expr --
1940 --------------------
1942 function Get_Assoc_Expr (Assoc : Node_Id) return Node_Id is
1943 Typ : constant Entity_Id := Base_Type (Etype (N));
1946 if Box_Present (Assoc) then
1947 if Is_Scalar_Type (Ctype) then
1948 if Present (Default_Aspect_Component_Value (Typ)) then
1949 return Default_Aspect_Component_Value (Typ);
1950 elsif Present (Default_Aspect_Value (Ctype)) then
1951 return Default_Aspect_Value (Ctype);
1961 return Expression (Assoc);
1965 ---------------------
1966 -- Index_Base_Name --
1967 ---------------------
1969 function Index_Base_Name return Node_Id is
1971 return New_Occurrence_Of (Index_Base, Sloc (N));
1972 end Index_Base_Name;
1974 ------------------------------------
1975 -- Local_Compile_Time_Known_Value --
1976 ------------------------------------
1978 function Local_Compile_Time_Known_Value (E : Node_Id) return Boolean is
1980 return Compile_Time_Known_Value (E)
1982 (Nkind (E) = N_Attribute_Reference
1983 and then Attribute_Name (E) = Name_Val
1984 and then Compile_Time_Known_Value (First (Expressions (E))));
1985 end Local_Compile_Time_Known_Value;
1987 ----------------------
1988 -- Local_Expr_Value --
1989 ----------------------
1991 function Local_Expr_Value (E : Node_Id) return Uint is
1993 if Compile_Time_Known_Value (E) then
1994 return Expr_Value (E);
1996 return Expr_Value (First (Expressions (E)));
1998 end Local_Expr_Value;
2002 New_Code : constant List_Id := New_List;
2004 Aggr_L : constant Node_Id := Low_Bound (Aggregate_Bounds (N));
2005 Aggr_H : constant Node_Id := High_Bound (Aggregate_Bounds (N));
2006 -- The aggregate bounds of this specific subaggregate. Note that if the
2007 -- code generated by Build_Array_Aggr_Code is executed then these bounds
2008 -- are OK. Otherwise a Constraint_Error would have been raised.
2010 Aggr_Low : constant Node_Id := Duplicate_Subexpr_No_Checks (Aggr_L);
2011 Aggr_High : constant Node_Id := Duplicate_Subexpr_No_Checks (Aggr_H);
2012 -- After Duplicate_Subexpr these are side-effect free
2021 Nb_Choices : Nat := 0;
2022 Table : Case_Table_Type (1 .. Number_Of_Choices (N));
2023 -- Used to sort all the different choice values
2026 -- Number of elements in the positional aggregate
2028 Others_Assoc : Node_Id := Empty;
2030 -- Start of processing for Build_Array_Aggr_Code
2033 -- First before we start, a special case. if we have a bit packed
2034 -- array represented as a modular type, then clear the value to
2035 -- zero first, to ensure that unused bits are properly cleared.
2040 and then Is_Bit_Packed_Array (Typ)
2041 and then Is_Modular_Integer_Type (Packed_Array_Impl_Type (Typ))
2043 Append_To (New_Code,
2044 Make_Assignment_Statement (Loc,
2045 Name => New_Copy_Tree (Into),
2047 Unchecked_Convert_To (Typ,
2048 Make_Integer_Literal (Loc, Uint_0))));
2051 -- If the component type contains tasks, we need to build a Master
2052 -- entity in the current scope, because it will be needed if build-
2053 -- in-place functions are called in the expanded code.
2055 if Nkind (Parent (N)) = N_Object_Declaration and then Has_Task (Typ) then
2056 Build_Master_Entity (Defining_Identifier (Parent (N)));
2059 -- STEP 1: Process component associations
2061 -- For those associations that may generate a loop, initialize
2062 -- Loop_Actions to collect inserted actions that may be crated.
2064 -- Skip this if no component associations
2066 if No (Expressions (N)) then
2068 -- STEP 1 (a): Sort the discrete choices
2070 Assoc := First (Component_Associations (N));
2071 while Present (Assoc) loop
2072 Choice := First (Choice_List (Assoc));
2073 while Present (Choice) loop
2074 if Nkind (Choice) = N_Others_Choice then
2075 Set_Loop_Actions (Assoc, New_List);
2076 Others_Assoc := Assoc;
2080 Get_Index_Bounds (Choice, Low, High);
2083 Set_Loop_Actions (Assoc, New_List);
2086 Nb_Choices := Nb_Choices + 1;
2088 Table (Nb_Choices) :=
2091 Choice_Node => Get_Assoc_Expr (Assoc));
2099 -- If there is more than one set of choices these must be static
2100 -- and we can therefore sort them. Remember that Nb_Choices does not
2101 -- account for an others choice.
2103 if Nb_Choices > 1 then
2104 Sort_Case_Table (Table);
2107 -- STEP 1 (b): take care of the whole set of discrete choices
2109 for J in 1 .. Nb_Choices loop
2110 Low := Table (J).Choice_Lo;
2111 High := Table (J).Choice_Hi;
2112 Expr := Table (J).Choice_Node;
2113 Append_List (Gen_Loop (Low, High, Expr), To => New_Code);
2116 -- STEP 1 (c): generate the remaining loops to cover others choice
2117 -- We don't need to generate loops over empty gaps, but if there is
2118 -- a single empty range we must analyze the expression for semantics
2120 if Present (Others_Assoc) then
2122 First : Boolean := True;
2125 for J in 0 .. Nb_Choices loop
2129 Low := Add (1, To => Table (J).Choice_Hi);
2132 if J = Nb_Choices then
2135 High := Add (-1, To => Table (J + 1).Choice_Lo);
2138 -- If this is an expansion within an init proc, make
2139 -- sure that discriminant references are replaced by
2140 -- the corresponding discriminal.
2142 if Inside_Init_Proc then
2143 if Is_Entity_Name (Low)
2144 and then Ekind (Entity (Low)) = E_Discriminant
2146 Set_Entity (Low, Discriminal (Entity (Low)));
2149 if Is_Entity_Name (High)
2150 and then Ekind (Entity (High)) = E_Discriminant
2152 Set_Entity (High, Discriminal (Entity (High)));
2157 or else not Empty_Range (Low, High)
2161 (Gen_Loop (Low, High,
2162 Get_Assoc_Expr (Others_Assoc)), To => New_Code);
2168 -- STEP 2: Process positional components
2171 -- STEP 2 (a): Generate the assignments for each positional element
2172 -- Note that here we have to use Aggr_L rather than Aggr_Low because
2173 -- Aggr_L is analyzed and Add wants an analyzed expression.
2175 Expr := First (Expressions (N));
2177 while Present (Expr) loop
2178 Nb_Elements := Nb_Elements + 1;
2179 Append_List (Gen_Assign (Add (Nb_Elements, To => Aggr_L), Expr),
2184 -- STEP 2 (b): Generate final loop if an others choice is present
2185 -- Here Nb_Elements gives the offset of the last positional element.
2187 if Present (Component_Associations (N)) then
2188 Assoc := Last (Component_Associations (N));
2190 -- Ada 2005 (AI-287)
2192 Append_List (Gen_While (Add (Nb_Elements, To => Aggr_L),
2194 Get_Assoc_Expr (Assoc)), -- AI-287
2200 end Build_Array_Aggr_Code;
2202 ----------------------------
2203 -- Build_Record_Aggr_Code --
2204 ----------------------------
2206 function Build_Record_Aggr_Code
2209 Lhs : Node_Id) return List_Id
2211 Loc : constant Source_Ptr := Sloc (N);
2212 L : constant List_Id := New_List;
2213 N_Typ : constant Entity_Id := Etype (N);
2219 Comp_Type : Entity_Id;
2220 Selector : Entity_Id;
2221 Comp_Expr : Node_Id;
2224 -- If this is an internal aggregate, the External_Final_List is an
2225 -- expression for the controller record of the enclosing type.
2227 -- If the current aggregate has several controlled components, this
2228 -- expression will appear in several calls to attach to the finali-
2229 -- zation list, and it must not be shared.
2231 Ancestor_Is_Expression : Boolean := False;
2232 Ancestor_Is_Subtype_Mark : Boolean := False;
2234 Init_Typ : Entity_Id := Empty;
2236 Finalization_Done : Boolean := False;
2237 -- True if Generate_Finalization_Actions has already been called; calls
2238 -- after the first do nothing.
2240 function Ancestor_Discriminant_Value (Disc : Entity_Id) return Node_Id;
2241 -- Returns the value that the given discriminant of an ancestor type
2242 -- should receive (in the absence of a conflict with the value provided
2243 -- by an ancestor part of an extension aggregate).
2245 procedure Check_Ancestor_Discriminants (Anc_Typ : Entity_Id);
2246 -- Check that each of the discriminant values defined by the ancestor
2247 -- part of an extension aggregate match the corresponding values
2248 -- provided by either an association of the aggregate or by the
2249 -- constraint imposed by a parent type (RM95-4.3.2(8)).
2251 function Compatible_Int_Bounds
2252 (Agg_Bounds : Node_Id;
2253 Typ_Bounds : Node_Id) return Boolean;
2254 -- Return true if Agg_Bounds are equal or within Typ_Bounds. It is
2255 -- assumed that both bounds are integer ranges.
2257 procedure Generate_Finalization_Actions;
2258 -- Deal with the various controlled type data structure initializations
2259 -- (but only if it hasn't been done already).
2261 function Get_Constraint_Association (T : Entity_Id) return Node_Id;
2262 -- Returns the first discriminant association in the constraint
2263 -- associated with T, if any, otherwise returns Empty.
2265 function Get_Explicit_Discriminant_Value (D : Entity_Id) return Node_Id;
2266 -- If the ancestor part is an unconstrained type and further ancestors
2267 -- do not provide discriminants for it, check aggregate components for
2268 -- values of the discriminants.
2270 procedure Init_Hidden_Discriminants (Typ : Entity_Id; List : List_Id);
2271 -- If Typ is derived, and constrains discriminants of the parent type,
2272 -- these discriminants are not components of the aggregate, and must be
2273 -- initialized. The assignments are appended to List. The same is done
2274 -- if Typ derives fron an already constrained subtype of a discriminated
2277 procedure Init_Stored_Discriminants;
2278 -- If the type is derived and has inherited discriminants, generate
2279 -- explicit assignments for each, using the store constraint of the
2280 -- type. Note that both visible and stored discriminants must be
2281 -- initialized in case the derived type has some renamed and some
2282 -- constrained discriminants.
2284 procedure Init_Visible_Discriminants;
2285 -- If type has discriminants, retrieve their values from aggregate,
2286 -- and generate explicit assignments for each. This does not include
2287 -- discriminants inherited from ancestor, which are handled above.
2288 -- The type of the aggregate is a subtype created ealier using the
2289 -- given values of the discriminant components of the aggregate.
2291 procedure Initialize_Ctrl_Record_Component
2292 (Rec_Comp : Node_Id;
2293 Comp_Typ : Entity_Id;
2294 Init_Expr : Node_Id;
2296 -- Perform the initialization of controlled record component Rec_Comp.
2297 -- Comp_Typ is the component type. Init_Expr is the initialization
2298 -- expression for the record component. Hook-related declarations are
2299 -- inserted prior to aggregate N using Insert_Action. All remaining
2300 -- generated code is added to list Stmts.
2302 procedure Initialize_Record_Component
2303 (Rec_Comp : Node_Id;
2304 Comp_Typ : Entity_Id;
2305 Init_Expr : Node_Id;
2307 -- Perform the initialization of record component Rec_Comp. Comp_Typ
2308 -- is the component type. Init_Expr is the initialization expression
2309 -- of the record component. All generated code is added to list Stmts.
2311 function Is_Int_Range_Bounds (Bounds : Node_Id) return Boolean;
2312 -- Check whether Bounds is a range node and its lower and higher bounds
2313 -- are integers literals.
2315 function Replace_Type (Expr : Node_Id) return Traverse_Result;
2316 -- If the aggregate contains a self-reference, traverse each expression
2317 -- to replace a possible self-reference with a reference to the proper
2318 -- component of the target of the assignment.
2320 function Rewrite_Discriminant (Expr : Node_Id) return Traverse_Result;
2321 -- If default expression of a component mentions a discriminant of the
2322 -- type, it must be rewritten as the discriminant of the target object.
2324 ---------------------------------
2325 -- Ancestor_Discriminant_Value --
2326 ---------------------------------
2328 function Ancestor_Discriminant_Value (Disc : Entity_Id) return Node_Id is
2330 Assoc_Elmt : Elmt_Id;
2331 Aggr_Comp : Entity_Id;
2332 Corresp_Disc : Entity_Id;
2333 Current_Typ : Entity_Id := Base_Type (Typ);
2334 Parent_Typ : Entity_Id;
2335 Parent_Disc : Entity_Id;
2336 Save_Assoc : Node_Id := Empty;
2339 -- First check any discriminant associations to see if any of them
2340 -- provide a value for the discriminant.
2342 if Present (Discriminant_Specifications (Parent (Current_Typ))) then
2343 Assoc := First (Component_Associations (N));
2344 while Present (Assoc) loop
2345 Aggr_Comp := Entity (First (Choices (Assoc)));
2347 if Ekind (Aggr_Comp) = E_Discriminant then
2348 Save_Assoc := Expression (Assoc);
2350 Corresp_Disc := Corresponding_Discriminant (Aggr_Comp);
2351 while Present (Corresp_Disc) loop
2353 -- If found a corresponding discriminant then return the
2354 -- value given in the aggregate. (Note: this is not
2355 -- correct in the presence of side effects. ???)
2357 if Disc = Corresp_Disc then
2358 return Duplicate_Subexpr (Expression (Assoc));
2361 Corresp_Disc := Corresponding_Discriminant (Corresp_Disc);
2369 -- No match found in aggregate, so chain up parent types to find
2370 -- a constraint that defines the value of the discriminant.
2372 Parent_Typ := Etype (Current_Typ);
2373 while Current_Typ /= Parent_Typ loop
2374 if Has_Discriminants (Parent_Typ)
2375 and then not Has_Unknown_Discriminants (Parent_Typ)
2377 Parent_Disc := First_Discriminant (Parent_Typ);
2379 -- We either get the association from the subtype indication
2380 -- of the type definition itself, or from the discriminant
2381 -- constraint associated with the type entity (which is
2382 -- preferable, but it's not always present ???)
2384 if Is_Empty_Elmt_List (Discriminant_Constraint (Current_Typ))
2386 Assoc := Get_Constraint_Association (Current_Typ);
2387 Assoc_Elmt := No_Elmt;
2390 First_Elmt (Discriminant_Constraint (Current_Typ));
2391 Assoc := Node (Assoc_Elmt);
2394 -- Traverse the discriminants of the parent type looking
2395 -- for one that corresponds.
2397 while Present (Parent_Disc) and then Present (Assoc) loop
2398 Corresp_Disc := Parent_Disc;
2399 while Present (Corresp_Disc)
2400 and then Disc /= Corresp_Disc
2402 Corresp_Disc := Corresponding_Discriminant (Corresp_Disc);
2405 if Disc = Corresp_Disc then
2406 if Nkind (Assoc) = N_Discriminant_Association then
2407 Assoc := Expression (Assoc);
2410 -- If the located association directly denotes
2411 -- a discriminant, then use the value of a saved
2412 -- association of the aggregate. This is an approach
2413 -- used to handle certain cases involving multiple
2414 -- discriminants mapped to a single discriminant of
2415 -- a descendant. It's not clear how to locate the
2416 -- appropriate discriminant value for such cases. ???
2418 if Is_Entity_Name (Assoc)
2419 and then Ekind (Entity (Assoc)) = E_Discriminant
2421 Assoc := Save_Assoc;
2424 return Duplicate_Subexpr (Assoc);
2427 Next_Discriminant (Parent_Disc);
2429 if No (Assoc_Elmt) then
2433 Next_Elmt (Assoc_Elmt);
2435 if Present (Assoc_Elmt) then
2436 Assoc := Node (Assoc_Elmt);
2444 Current_Typ := Parent_Typ;
2445 Parent_Typ := Etype (Current_Typ);
2448 -- In some cases there's no ancestor value to locate (such as
2449 -- when an ancestor part given by an expression defines the
2450 -- discriminant value).
2453 end Ancestor_Discriminant_Value;
2455 ----------------------------------
2456 -- Check_Ancestor_Discriminants --
2457 ----------------------------------
2459 procedure Check_Ancestor_Discriminants (Anc_Typ : Entity_Id) is
2461 Disc_Value : Node_Id;
2465 Discr := First_Discriminant (Base_Type (Anc_Typ));
2466 while Present (Discr) loop
2467 Disc_Value := Ancestor_Discriminant_Value (Discr);
2469 if Present (Disc_Value) then
2470 Cond := Make_Op_Ne (Loc,
2472 Make_Selected_Component (Loc,
2473 Prefix => New_Copy_Tree (Target),
2474 Selector_Name => New_Occurrence_Of (Discr, Loc)),
2475 Right_Opnd => Disc_Value);
2478 Make_Raise_Constraint_Error (Loc,
2480 Reason => CE_Discriminant_Check_Failed));
2483 Next_Discriminant (Discr);
2485 end Check_Ancestor_Discriminants;
2487 ---------------------------
2488 -- Compatible_Int_Bounds --
2489 ---------------------------
2491 function Compatible_Int_Bounds
2492 (Agg_Bounds : Node_Id;
2493 Typ_Bounds : Node_Id) return Boolean
2495 Agg_Lo : constant Uint := Intval (Low_Bound (Agg_Bounds));
2496 Agg_Hi : constant Uint := Intval (High_Bound (Agg_Bounds));
2497 Typ_Lo : constant Uint := Intval (Low_Bound (Typ_Bounds));
2498 Typ_Hi : constant Uint := Intval (High_Bound (Typ_Bounds));
2500 return Typ_Lo <= Agg_Lo and then Agg_Hi <= Typ_Hi;
2501 end Compatible_Int_Bounds;
2503 -----------------------------------
2504 -- Generate_Finalization_Actions --
2505 -----------------------------------
2507 procedure Generate_Finalization_Actions is
2509 -- Do the work only the first time this is called
2511 if Finalization_Done then
2515 Finalization_Done := True;
2517 -- Determine the external finalization list. It is either the
2518 -- finalization list of the outer scope or the one coming from an
2519 -- outer aggregate. When the target is not a temporary, the proper
2520 -- scope is the scope of the target rather than the potentially
2521 -- transient current scope.
2523 if Is_Controlled (Typ) and then Ancestor_Is_Subtype_Mark then
2524 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
2525 Set_Assignment_OK (Ref);
2528 Make_Procedure_Call_Statement (Loc,
2531 (Find_Prim_Op (Init_Typ, Name_Initialize), Loc),
2532 Parameter_Associations => New_List (New_Copy_Tree (Ref))));
2534 end Generate_Finalization_Actions;
2536 --------------------------------
2537 -- Get_Constraint_Association --
2538 --------------------------------
2540 function Get_Constraint_Association (T : Entity_Id) return Node_Id is
2547 -- If type is private, get constraint from full view. This was
2548 -- previously done in an instance context, but is needed whenever
2549 -- the ancestor part has a discriminant, possibly inherited through
2550 -- multiple derivations.
2552 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
2553 Typ := Full_View (Typ);
2556 Indic := Subtype_Indication (Type_Definition (Parent (Typ)));
2558 -- Verify that the subtype indication carries a constraint
2560 if Nkind (Indic) = N_Subtype_Indication
2561 and then Present (Constraint (Indic))
2563 return First (Constraints (Constraint (Indic)));
2567 end Get_Constraint_Association;
2569 -------------------------------------
2570 -- Get_Explicit_Discriminant_Value --
2571 -------------------------------------
2573 function Get_Explicit_Discriminant_Value
2574 (D : Entity_Id) return Node_Id
2581 -- The aggregate has been normalized and all associations have a
2584 Assoc := First (Component_Associations (N));
2585 while Present (Assoc) loop
2586 Choice := First (Choices (Assoc));
2588 if Chars (Choice) = Chars (D) then
2589 Val := Expression (Assoc);
2598 end Get_Explicit_Discriminant_Value;
2600 -------------------------------
2601 -- Init_Hidden_Discriminants --
2602 -------------------------------
2604 procedure Init_Hidden_Discriminants (Typ : Entity_Id; List : List_Id) is
2605 function Is_Completely_Hidden_Discriminant
2606 (Discr : Entity_Id) return Boolean;
2607 -- Determine whether Discr is a completely hidden discriminant of
2610 ---------------------------------------
2611 -- Is_Completely_Hidden_Discriminant --
2612 ---------------------------------------
2614 function Is_Completely_Hidden_Discriminant
2615 (Discr : Entity_Id) return Boolean
2620 -- Use First/Next_Entity as First/Next_Discriminant do not yield
2621 -- completely hidden discriminants.
2623 Item := First_Entity (Typ);
2624 while Present (Item) loop
2625 if Ekind (Item) = E_Discriminant
2626 and then Is_Completely_Hidden (Item)
2627 and then Chars (Original_Record_Component (Item)) =
2637 end Is_Completely_Hidden_Discriminant;
2641 Base_Typ : Entity_Id;
2643 Discr_Constr : Elmt_Id;
2644 Discr_Init : Node_Id;
2645 Discr_Val : Node_Id;
2646 In_Aggr_Type : Boolean;
2647 Par_Typ : Entity_Id;
2649 -- Start of processing for Init_Hidden_Discriminants
2652 -- The constraints on the hidden discriminants, if present, are kept
2653 -- in the Stored_Constraint list of the type itself, or in that of
2654 -- the base type. If not in the constraints of the aggregate itself,
2655 -- we examine ancestors to find discriminants that are not renamed
2656 -- by other discriminants but constrained explicitly.
2658 In_Aggr_Type := True;
2660 Base_Typ := Base_Type (Typ);
2661 while Is_Derived_Type (Base_Typ)
2663 (Present (Stored_Constraint (Base_Typ))
2665 (In_Aggr_Type and then Present (Stored_Constraint (Typ))))
2667 Par_Typ := Etype (Base_Typ);
2669 if not Has_Discriminants (Par_Typ) then
2673 Discr := First_Discriminant (Par_Typ);
2675 -- We know that one of the stored-constraint lists is present
2677 if Present (Stored_Constraint (Base_Typ)) then
2678 Discr_Constr := First_Elmt (Stored_Constraint (Base_Typ));
2680 -- For private extension, stored constraint may be on full view
2682 elsif Is_Private_Type (Base_Typ)
2683 and then Present (Full_View (Base_Typ))
2684 and then Present (Stored_Constraint (Full_View (Base_Typ)))
2687 First_Elmt (Stored_Constraint (Full_View (Base_Typ)));
2690 Discr_Constr := First_Elmt (Stored_Constraint (Typ));
2693 while Present (Discr) and then Present (Discr_Constr) loop
2694 Discr_Val := Node (Discr_Constr);
2696 -- The parent discriminant is renamed in the derived type,
2697 -- nothing to initialize.
2699 -- type Deriv_Typ (Discr : ...)
2700 -- is new Parent_Typ (Discr => Discr);
2702 if Is_Entity_Name (Discr_Val)
2703 and then Ekind (Entity (Discr_Val)) = E_Discriminant
2707 -- When the parent discriminant is constrained at the type
2708 -- extension level, it does not appear in the derived type.
2710 -- type Deriv_Typ (Discr : ...)
2711 -- is new Parent_Typ (Discr => Discr,
2712 -- Hidden_Discr => Expression);
2714 elsif Is_Completely_Hidden_Discriminant (Discr) then
2717 -- Otherwise initialize the discriminant
2721 Make_OK_Assignment_Statement (Loc,
2723 Make_Selected_Component (Loc,
2724 Prefix => New_Copy_Tree (Target),
2725 Selector_Name => New_Occurrence_Of (Discr, Loc)),
2726 Expression => New_Copy_Tree (Discr_Val));
2728 Set_No_Ctrl_Actions (Discr_Init);
2729 Append_To (List, Discr_Init);
2732 Next_Elmt (Discr_Constr);
2733 Next_Discriminant (Discr);
2736 In_Aggr_Type := False;
2737 Base_Typ := Base_Type (Par_Typ);
2739 end Init_Hidden_Discriminants;
2741 --------------------------------
2742 -- Init_Visible_Discriminants --
2743 --------------------------------
2745 procedure Init_Visible_Discriminants is
2746 Discriminant : Entity_Id;
2747 Discriminant_Value : Node_Id;
2750 Discriminant := First_Discriminant (Typ);
2751 while Present (Discriminant) loop
2753 Make_Selected_Component (Loc,
2754 Prefix => New_Copy_Tree (Target),
2755 Selector_Name => New_Occurrence_Of (Discriminant, Loc));
2757 Discriminant_Value :=
2758 Get_Discriminant_Value
2759 (Discriminant, Typ, Discriminant_Constraint (N_Typ));
2762 Make_OK_Assignment_Statement (Loc,
2764 Expression => New_Copy_Tree (Discriminant_Value));
2766 Set_No_Ctrl_Actions (Instr);
2767 Append_To (L, Instr);
2769 Next_Discriminant (Discriminant);
2771 end Init_Visible_Discriminants;
2773 -------------------------------
2774 -- Init_Stored_Discriminants --
2775 -------------------------------
2777 procedure Init_Stored_Discriminants is
2778 Discriminant : Entity_Id;
2779 Discriminant_Value : Node_Id;
2782 Discriminant := First_Stored_Discriminant (Typ);
2783 while Present (Discriminant) loop
2785 Make_Selected_Component (Loc,
2786 Prefix => New_Copy_Tree (Target),
2787 Selector_Name => New_Occurrence_Of (Discriminant, Loc));
2789 Discriminant_Value :=
2790 Get_Discriminant_Value
2791 (Discriminant, N_Typ, Discriminant_Constraint (N_Typ));
2794 Make_OK_Assignment_Statement (Loc,
2796 Expression => New_Copy_Tree (Discriminant_Value));
2798 Set_No_Ctrl_Actions (Instr);
2799 Append_To (L, Instr);
2801 Next_Stored_Discriminant (Discriminant);
2803 end Init_Stored_Discriminants;
2805 --------------------------------------
2806 -- Initialize_Ctrl_Record_Component --
2807 --------------------------------------
2809 procedure Initialize_Ctrl_Record_Component
2810 (Rec_Comp : Node_Id;
2811 Comp_Typ : Entity_Id;
2812 Init_Expr : Node_Id;
2816 Hook_Clear : Node_Id;
2818 In_Place_Expansion : Boolean;
2819 -- Flag set when a nonlimited controlled function call requires
2820 -- in-place expansion.
2823 -- Perform a preliminary analysis and resolution to determine what
2824 -- the initialization expression denotes. Unanalyzed function calls
2825 -- may appear as identifiers or indexed components.
2827 if Nkind_In (Init_Expr, N_Function_Call,
2829 N_Indexed_Component)
2830 and then not Analyzed (Init_Expr)
2832 Preanalyze_And_Resolve (Init_Expr, Comp_Typ);
2835 In_Place_Expansion :=
2836 Nkind (Init_Expr) = N_Function_Call
2837 and then not Is_Limited_Type (Comp_Typ);
2839 -- The initialization expression is a controlled function call.
2840 -- Perform in-place removal of side effects to avoid creating a
2843 -- This in-place expansion is not performed for limited transient
2844 -- objects because the initialization is already done in place.
2846 if In_Place_Expansion then
2848 -- Suppress the removal of side effects by general analysis
2849 -- because this behavior is emulated here. This avoids the
2850 -- generation of a transient scope, which leads to out-of-order
2851 -- adjustment and finalization.
2853 Set_No_Side_Effect_Removal (Init_Expr);
2855 -- Install all hook-related declarations and prepare the clean up
2858 Process_Transient_Component
2860 Comp_Typ => Comp_Typ,
2861 Init_Expr => Init_Expr,
2862 Fin_Call => Fin_Call,
2863 Hook_Clear => Hook_Clear,
2867 -- Use the noncontrolled component initialization circuitry to
2868 -- assign the result of the function call to the record component.
2869 -- This also performs tag adjustment and [deep] adjustment of the
2870 -- record component.
2872 Initialize_Record_Component
2873 (Rec_Comp => Rec_Comp,
2874 Comp_Typ => Comp_Typ,
2875 Init_Expr => Init_Expr,
2878 -- At this point the record component is fully initialized. Complete
2879 -- the processing of the controlled record component by finalizing
2880 -- the transient function result.
2882 if In_Place_Expansion then
2883 Process_Transient_Component_Completion
2886 Fin_Call => Fin_Call,
2887 Hook_Clear => Hook_Clear,
2890 end Initialize_Ctrl_Record_Component;
2892 ---------------------------------
2893 -- Initialize_Record_Component --
2894 ---------------------------------
2896 procedure Initialize_Record_Component
2897 (Rec_Comp : Node_Id;
2898 Comp_Typ : Entity_Id;
2899 Init_Expr : Node_Id;
2902 Exceptions_OK : constant Boolean :=
2903 not Restriction_Active (No_Exception_Propagation);
2905 Finalization_OK : constant Boolean := Needs_Finalization (Comp_Typ);
2907 Full_Typ : constant Entity_Id := Underlying_Type (Comp_Typ);
2909 Blk_Stmts : List_Id;
2910 Init_Stmt : Node_Id;
2913 -- Protect the initialization statements from aborts. Generate:
2917 if Finalization_OK and Abort_Allowed then
2918 if Exceptions_OK then
2919 Blk_Stmts := New_List;
2924 Append_To (Blk_Stmts, Build_Runtime_Call (Loc, RE_Abort_Defer));
2926 -- Otherwise aborts are not allowed. All generated code is added
2927 -- directly to the input list.
2933 -- Initialize the record component. Generate:
2935 -- Rec_Comp := Init_Expr;
2937 -- Note that the initialization expression is NOT replicated because
2938 -- only a single component may be initialized by it.
2941 Make_OK_Assignment_Statement (Loc,
2942 Name => New_Copy_Tree (Rec_Comp),
2943 Expression => Init_Expr);
2944 Set_No_Ctrl_Actions (Init_Stmt);
2946 Append_To (Blk_Stmts, Init_Stmt);
2948 -- Adjust the tag due to a possible view conversion. Generate:
2950 -- Rec_Comp._tag := Full_TypeP;
2952 if Tagged_Type_Expansion and then Is_Tagged_Type (Comp_Typ) then
2953 Append_To (Blk_Stmts,
2954 Make_OK_Assignment_Statement (Loc,
2956 Make_Selected_Component (Loc,
2957 Prefix => New_Copy_Tree (Rec_Comp),
2960 (First_Tag_Component (Full_Typ), Loc)),
2963 Unchecked_Convert_To (RTE (RE_Tag),
2965 (Node (First_Elmt (Access_Disp_Table (Full_Typ))),
2969 -- Adjust the component. Generate:
2971 -- [Deep_]Adjust (Rec_Comp);
2973 if Finalization_OK and then not Is_Limited_Type (Comp_Typ) then
2976 (Obj_Ref => New_Copy_Tree (Rec_Comp),
2979 -- Guard against a missing [Deep_]Adjust when the component type
2980 -- was not properly frozen.
2982 if Present (Adj_Call) then
2983 Append_To (Blk_Stmts, Adj_Call);
2987 -- Complete the protection of the initialization statements
2989 if Finalization_OK and Abort_Allowed then
2991 -- Wrap the initialization statements in a block to catch a
2992 -- potential exception. Generate:
2996 -- Rec_Comp := Init_Expr;
2997 -- Rec_Comp._tag := Full_TypP;
2998 -- [Deep_]Adjust (Rec_Comp);
3000 -- Abort_Undefer_Direct;
3003 if Exceptions_OK then
3005 Build_Abort_Undefer_Block (Loc,
3009 -- Otherwise exceptions are not propagated. Generate:
3012 -- Rec_Comp := Init_Expr;
3013 -- Rec_Comp._tag := Full_TypP;
3014 -- [Deep_]Adjust (Rec_Comp);
3018 Append_To (Blk_Stmts,
3019 Build_Runtime_Call (Loc, RE_Abort_Undefer));
3022 end Initialize_Record_Component;
3024 -------------------------
3025 -- Is_Int_Range_Bounds --
3026 -------------------------
3028 function Is_Int_Range_Bounds (Bounds : Node_Id) return Boolean is
3030 return Nkind (Bounds) = N_Range
3031 and then Nkind (Low_Bound (Bounds)) = N_Integer_Literal
3032 and then Nkind (High_Bound (Bounds)) = N_Integer_Literal;
3033 end Is_Int_Range_Bounds;
3039 function Replace_Type (Expr : Node_Id) return Traverse_Result is
3041 -- Note regarding the Root_Type test below: Aggregate components for
3042 -- self-referential types include attribute references to the current
3043 -- instance, of the form: Typ'access, etc.. These references are
3044 -- rewritten as references to the target of the aggregate: the
3045 -- left-hand side of an assignment, the entity in a declaration,
3046 -- or a temporary. Without this test, we would improperly extended
3047 -- this rewriting to attribute references whose prefix was not the
3048 -- type of the aggregate.
3050 if Nkind (Expr) = N_Attribute_Reference
3051 and then Is_Entity_Name (Prefix (Expr))
3052 and then Is_Type (Entity (Prefix (Expr)))
3053 and then Root_Type (Etype (N)) = Root_Type (Entity (Prefix (Expr)))
3055 if Is_Entity_Name (Lhs) then
3056 Rewrite (Prefix (Expr), New_Occurrence_Of (Entity (Lhs), Loc));
3060 Make_Attribute_Reference (Loc,
3061 Attribute_Name => Name_Unrestricted_Access,
3062 Prefix => New_Copy_Tree (Lhs)));
3063 Set_Analyzed (Parent (Expr), False);
3070 --------------------------
3071 -- Rewrite_Discriminant --
3072 --------------------------
3074 function Rewrite_Discriminant (Expr : Node_Id) return Traverse_Result is
3076 if Is_Entity_Name (Expr)
3077 and then Present (Entity (Expr))
3078 and then Ekind (Entity (Expr)) = E_In_Parameter
3079 and then Present (Discriminal_Link (Entity (Expr)))
3080 and then Scope (Discriminal_Link (Entity (Expr))) =
3081 Base_Type (Etype (N))
3084 Make_Selected_Component (Loc,
3085 Prefix => New_Copy_Tree (Lhs),
3086 Selector_Name => Make_Identifier (Loc, Chars (Expr))));
3090 end Rewrite_Discriminant;
3092 procedure Replace_Discriminants is
3093 new Traverse_Proc (Rewrite_Discriminant);
3095 procedure Replace_Self_Reference is
3096 new Traverse_Proc (Replace_Type);
3098 -- Start of processing for Build_Record_Aggr_Code
3101 if Has_Self_Reference (N) then
3102 Replace_Self_Reference (N);
3105 -- If the target of the aggregate is class-wide, we must convert it
3106 -- to the actual type of the aggregate, so that the proper components
3107 -- are visible. We know already that the types are compatible.
3109 if Present (Etype (Lhs))
3110 and then Is_Class_Wide_Type (Etype (Lhs))
3112 Target := Unchecked_Convert_To (Typ, Lhs);
3117 -- Deal with the ancestor part of extension aggregates or with the
3118 -- discriminants of the root type.
3120 if Nkind (N) = N_Extension_Aggregate then
3122 Ancestor : constant Node_Id := Ancestor_Part (N);
3127 -- If the ancestor part is a subtype mark "T", we generate
3129 -- init-proc (T (tmp)); if T is constrained and
3130 -- init-proc (S (tmp)); where S applies an appropriate
3131 -- constraint if T is unconstrained
3133 if Is_Entity_Name (Ancestor)
3134 and then Is_Type (Entity (Ancestor))
3136 Ancestor_Is_Subtype_Mark := True;
3138 if Is_Constrained (Entity (Ancestor)) then
3139 Init_Typ := Entity (Ancestor);
3141 -- For an ancestor part given by an unconstrained type mark,
3142 -- create a subtype constrained by appropriate corresponding
3143 -- discriminant values coming from either associations of the
3144 -- aggregate or a constraint on a parent type. The subtype will
3145 -- be used to generate the correct default value for the
3148 elsif Has_Discriminants (Entity (Ancestor)) then
3150 Anc_Typ : constant Entity_Id := Entity (Ancestor);
3151 Anc_Constr : constant List_Id := New_List;
3152 Discrim : Entity_Id;
3153 Disc_Value : Node_Id;
3154 New_Indic : Node_Id;
3155 Subt_Decl : Node_Id;
3158 Discrim := First_Discriminant (Anc_Typ);
3159 while Present (Discrim) loop
3160 Disc_Value := Ancestor_Discriminant_Value (Discrim);
3162 -- If no usable discriminant in ancestors, check
3163 -- whether aggregate has an explicit value for it.
3165 if No (Disc_Value) then
3167 Get_Explicit_Discriminant_Value (Discrim);
3170 Append_To (Anc_Constr, Disc_Value);
3171 Next_Discriminant (Discrim);
3175 Make_Subtype_Indication (Loc,
3176 Subtype_Mark => New_Occurrence_Of (Anc_Typ, Loc),
3178 Make_Index_Or_Discriminant_Constraint (Loc,
3179 Constraints => Anc_Constr));
3181 Init_Typ := Create_Itype (Ekind (Anc_Typ), N);
3184 Make_Subtype_Declaration (Loc,
3185 Defining_Identifier => Init_Typ,
3186 Subtype_Indication => New_Indic);
3188 -- Itypes must be analyzed with checks off Declaration
3189 -- must have a parent for proper handling of subsidiary
3192 Set_Parent (Subt_Decl, N);
3193 Analyze (Subt_Decl, Suppress => All_Checks);
3197 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
3198 Set_Assignment_OK (Ref);
3200 if not Is_Interface (Init_Typ) then
3202 Build_Initialization_Call (Loc,
3205 In_Init_Proc => Within_Init_Proc,
3206 With_Default_Init => Has_Default_Init_Comps (N)
3208 Has_Task (Base_Type (Init_Typ))));
3210 if Is_Constrained (Entity (Ancestor))
3211 and then Has_Discriminants (Entity (Ancestor))
3213 Check_Ancestor_Discriminants (Entity (Ancestor));
3217 -- Handle calls to C++ constructors
3219 elsif Is_CPP_Constructor_Call (Ancestor) then
3220 Init_Typ := Etype (Ancestor);
3221 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
3222 Set_Assignment_OK (Ref);
3225 Build_Initialization_Call (Loc,
3228 In_Init_Proc => Within_Init_Proc,
3229 With_Default_Init => Has_Default_Init_Comps (N),
3230 Constructor_Ref => Ancestor));
3232 -- Ada 2005 (AI-287): If the ancestor part is an aggregate of
3233 -- limited type, a recursive call expands the ancestor. Note that
3234 -- in the limited case, the ancestor part must be either a
3235 -- function call (possibly qualified, or wrapped in an unchecked
3236 -- conversion) or aggregate (definitely qualified).
3238 -- The ancestor part can also be a function call (that may be
3239 -- transformed into an explicit dereference) or a qualification
3242 elsif Is_Limited_Type (Etype (Ancestor))
3243 and then Nkind_In (Unqualify (Ancestor), N_Aggregate,
3244 N_Extension_Aggregate)
3246 Ancestor_Is_Expression := True;
3248 -- Set up finalization data for enclosing record, because
3249 -- controlled subcomponents of the ancestor part will be
3252 Generate_Finalization_Actions;
3255 Build_Record_Aggr_Code
3256 (N => Unqualify (Ancestor),
3257 Typ => Etype (Unqualify (Ancestor)),
3260 -- If the ancestor part is an expression "E", we generate
3264 -- In Ada 2005, this includes the case of a (possibly qualified)
3265 -- limited function call. The assignment will turn into a
3266 -- build-in-place function call (for further details, see
3267 -- Make_Build_In_Place_Call_In_Assignment).
3270 Ancestor_Is_Expression := True;
3271 Init_Typ := Etype (Ancestor);
3273 -- If the ancestor part is an aggregate, force its full
3274 -- expansion, which was delayed.
3276 if Nkind_In (Unqualify (Ancestor), N_Aggregate,
3277 N_Extension_Aggregate)
3279 Set_Analyzed (Ancestor, False);
3280 Set_Analyzed (Expression (Ancestor), False);
3283 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
3284 Set_Assignment_OK (Ref);
3286 -- Make the assignment without usual controlled actions, since
3287 -- we only want to Adjust afterwards, but not to Finalize
3288 -- beforehand. Add manual Adjust when necessary.
3290 Assign := New_List (
3291 Make_OK_Assignment_Statement (Loc,
3293 Expression => Ancestor));
3294 Set_No_Ctrl_Actions (First (Assign));
3296 -- Assign the tag now to make sure that the dispatching call in
3297 -- the subsequent deep_adjust works properly (unless
3298 -- Tagged_Type_Expansion where tags are implicit).
3300 if Tagged_Type_Expansion then
3302 Make_OK_Assignment_Statement (Loc,
3304 Make_Selected_Component (Loc,
3305 Prefix => New_Copy_Tree (Target),
3308 (First_Tag_Component (Base_Type (Typ)), Loc)),
3311 Unchecked_Convert_To (RTE (RE_Tag),
3314 (Access_Disp_Table (Base_Type (Typ)))),
3317 Set_Assignment_OK (Name (Instr));
3318 Append_To (Assign, Instr);
3320 -- Ada 2005 (AI-251): If tagged type has progenitors we must
3321 -- also initialize tags of the secondary dispatch tables.
3323 if Has_Interfaces (Base_Type (Typ)) then
3325 (Typ => Base_Type (Typ),
3327 Stmts_List => Assign,
3328 Init_Tags_List => Assign);
3332 -- Call Adjust manually
3334 if Needs_Finalization (Etype (Ancestor))
3335 and then not Is_Limited_Type (Etype (Ancestor))
3339 (Obj_Ref => New_Copy_Tree (Ref),
3340 Typ => Etype (Ancestor));
3342 -- Guard against a missing [Deep_]Adjust when the ancestor
3343 -- type was not properly frozen.
3345 if Present (Adj_Call) then
3346 Append_To (Assign, Adj_Call);
3351 Make_Unsuppress_Block (Loc, Name_Discriminant_Check, Assign));
3353 if Has_Discriminants (Init_Typ) then
3354 Check_Ancestor_Discriminants (Init_Typ);
3359 -- Generate assignments of hidden discriminants. If the base type is
3360 -- an unchecked union, the discriminants are unknown to the back-end
3361 -- and absent from a value of the type, so assignments for them are
3364 if Has_Discriminants (Typ)
3365 and then not Is_Unchecked_Union (Base_Type (Typ))
3367 Init_Hidden_Discriminants (Typ, L);
3370 -- Normal case (not an extension aggregate)
3373 -- Generate the discriminant expressions, component by component.
3374 -- If the base type is an unchecked union, the discriminants are
3375 -- unknown to the back-end and absent from a value of the type, so
3376 -- assignments for them are not emitted.
3378 if Has_Discriminants (Typ)
3379 and then not Is_Unchecked_Union (Base_Type (Typ))
3381 Init_Hidden_Discriminants (Typ, L);
3383 -- Generate discriminant init values for the visible discriminants
3385 Init_Visible_Discriminants;
3387 if Is_Derived_Type (N_Typ) then
3388 Init_Stored_Discriminants;
3393 -- For CPP types we generate an implicit call to the C++ default
3394 -- constructor to ensure the proper initialization of the _Tag
3397 if Is_CPP_Class (Root_Type (Typ)) and then CPP_Num_Prims (Typ) > 0 then
3398 Invoke_Constructor : declare
3399 CPP_Parent : constant Entity_Id := Enclosing_CPP_Parent (Typ);
3401 procedure Invoke_IC_Proc (T : Entity_Id);
3402 -- Recursive routine used to climb to parents. Required because
3403 -- parents must be initialized before descendants to ensure
3404 -- propagation of inherited C++ slots.
3406 --------------------
3407 -- Invoke_IC_Proc --
3408 --------------------
3410 procedure Invoke_IC_Proc (T : Entity_Id) is
3412 -- Avoid generating extra calls. Initialization required
3413 -- only for types defined from the level of derivation of
3414 -- type of the constructor and the type of the aggregate.
3416 if T = CPP_Parent then
3420 Invoke_IC_Proc (Etype (T));
3422 -- Generate call to the IC routine
3424 if Present (CPP_Init_Proc (T)) then
3426 Make_Procedure_Call_Statement (Loc,
3427 Name => New_Occurrence_Of (CPP_Init_Proc (T), Loc)));
3431 -- Start of processing for Invoke_Constructor
3434 -- Implicit invocation of the C++ constructor
3436 if Nkind (N) = N_Aggregate then
3438 Make_Procedure_Call_Statement (Loc,
3440 New_Occurrence_Of (Base_Init_Proc (CPP_Parent), Loc),
3441 Parameter_Associations => New_List (
3442 Unchecked_Convert_To (CPP_Parent,
3443 New_Copy_Tree (Lhs)))));
3446 Invoke_IC_Proc (Typ);
3447 end Invoke_Constructor;
3450 -- Generate the assignments, component by component
3452 -- tmp.comp1 := Expr1_From_Aggr;
3453 -- tmp.comp2 := Expr2_From_Aggr;
3456 Comp := First (Component_Associations (N));
3457 while Present (Comp) loop
3458 Selector := Entity (First (Choices (Comp)));
3462 if Is_CPP_Constructor_Call (Expression (Comp)) then
3464 Build_Initialization_Call (Loc,
3466 Make_Selected_Component (Loc,
3467 Prefix => New_Copy_Tree (Target),
3468 Selector_Name => New_Occurrence_Of (Selector, Loc)),
3469 Typ => Etype (Selector),
3471 With_Default_Init => True,
3472 Constructor_Ref => Expression (Comp)));
3474 -- Ada 2005 (AI-287): For each default-initialized component generate
3475 -- a call to the corresponding IP subprogram if available.
3477 elsif Box_Present (Comp)
3478 and then Has_Non_Null_Base_Init_Proc (Etype (Selector))
3480 if Ekind (Selector) /= E_Discriminant then
3481 Generate_Finalization_Actions;
3484 -- Ada 2005 (AI-287): If the component type has tasks then
3485 -- generate the activation chain and master entities (except
3486 -- in case of an allocator because in that case these entities
3487 -- are generated by Build_Task_Allocate_Block_With_Init_Stmts).
3490 Ctype : constant Entity_Id := Etype (Selector);
3491 Inside_Allocator : Boolean := False;
3492 P : Node_Id := Parent (N);
3495 if Is_Task_Type (Ctype) or else Has_Task (Ctype) then
3496 while Present (P) loop
3497 if Nkind (P) = N_Allocator then
3498 Inside_Allocator := True;
3505 if not Inside_Init_Proc and not Inside_Allocator then
3506 Build_Activation_Chain_Entity (N);
3512 Build_Initialization_Call (Loc,
3513 Id_Ref => Make_Selected_Component (Loc,
3514 Prefix => New_Copy_Tree (Target),
3516 New_Occurrence_Of (Selector, Loc)),
3517 Typ => Etype (Selector),
3519 With_Default_Init => True));
3521 -- Prepare for component assignment
3523 elsif Ekind (Selector) /= E_Discriminant
3524 or else Nkind (N) = N_Extension_Aggregate
3526 -- All the discriminants have now been assigned
3528 -- This is now a good moment to initialize and attach all the
3529 -- controllers. Their position may depend on the discriminants.
3531 if Ekind (Selector) /= E_Discriminant then
3532 Generate_Finalization_Actions;
3535 Comp_Type := Underlying_Type (Etype (Selector));
3537 Make_Selected_Component (Loc,
3538 Prefix => New_Copy_Tree (Target),
3539 Selector_Name => New_Occurrence_Of (Selector, Loc));
3541 if Nkind (Expression (Comp)) = N_Qualified_Expression then
3542 Expr_Q := Expression (Expression (Comp));
3544 Expr_Q := Expression (Comp);
3547 -- Now either create the assignment or generate the code for the
3548 -- inner aggregate top-down.
3550 if Is_Delayed_Aggregate (Expr_Q) then
3552 -- We have the following case of aggregate nesting inside
3553 -- an object declaration:
3555 -- type Arr_Typ is array (Integer range <>) of ...;
3557 -- type Rec_Typ (...) is record
3558 -- Obj_Arr_Typ : Arr_Typ (A .. B);
3561 -- Obj_Rec_Typ : Rec_Typ := (...,
3562 -- Obj_Arr_Typ => (X => (...), Y => (...)));
3564 -- The length of the ranges of the aggregate and Obj_Add_Typ
3565 -- are equal (B - A = Y - X), but they do not coincide (X /=
3566 -- A and B /= Y). This case requires array sliding which is
3567 -- performed in the following manner:
3569 -- subtype Arr_Sub is Arr_Typ (X .. Y);
3571 -- Temp (X) := (...);
3573 -- Temp (Y) := (...);
3574 -- Obj_Rec_Typ.Obj_Arr_Typ := Temp;
3576 if Ekind (Comp_Type) = E_Array_Subtype
3577 and then Is_Int_Range_Bounds (Aggregate_Bounds (Expr_Q))
3578 and then Is_Int_Range_Bounds (First_Index (Comp_Type))
3580 Compatible_Int_Bounds
3581 (Agg_Bounds => Aggregate_Bounds (Expr_Q),
3582 Typ_Bounds => First_Index (Comp_Type))
3584 -- Create the array subtype with bounds equal to those of
3585 -- the corresponding aggregate.
3588 SubE : constant Entity_Id := Make_Temporary (Loc, 'T');
3590 SubD : constant Node_Id :=
3591 Make_Subtype_Declaration (Loc,
3592 Defining_Identifier => SubE,
3593 Subtype_Indication =>
3594 Make_Subtype_Indication (Loc,
3596 New_Occurrence_Of (Etype (Comp_Type), Loc),
3598 Make_Index_Or_Discriminant_Constraint
3600 Constraints => New_List (
3602 (Aggregate_Bounds (Expr_Q))))));
3604 -- Create a temporary array of the above subtype which
3605 -- will be used to capture the aggregate assignments.
3607 TmpE : constant Entity_Id := Make_Temporary (Loc, 'A', N);
3609 TmpD : constant Node_Id :=
3610 Make_Object_Declaration (Loc,
3611 Defining_Identifier => TmpE,
3612 Object_Definition => New_Occurrence_Of (SubE, Loc));
3615 Set_No_Initialization (TmpD);
3616 Append_To (L, SubD);
3617 Append_To (L, TmpD);
3619 -- Expand aggregate into assignments to the temp array
3622 Late_Expansion (Expr_Q, Comp_Type,
3623 New_Occurrence_Of (TmpE, Loc)));
3628 Make_Assignment_Statement (Loc,
3629 Name => New_Copy_Tree (Comp_Expr),
3630 Expression => New_Occurrence_Of (TmpE, Loc)));
3633 -- Normal case (sliding not required)
3637 Late_Expansion (Expr_Q, Comp_Type, Comp_Expr));
3640 -- Expr_Q is not delayed aggregate
3643 if Has_Discriminants (Typ) then
3644 Replace_Discriminants (Expr_Q);
3646 -- If the component is an array type that depends on
3647 -- discriminants, and the expression is a single Others
3648 -- clause, create an explicit subtype for it because the
3649 -- backend has troubles recovering the actual bounds.
3651 if Nkind (Expr_Q) = N_Aggregate
3652 and then Is_Array_Type (Comp_Type)
3653 and then Present (Component_Associations (Expr_Q))
3656 Assoc : constant Node_Id :=
3657 First (Component_Associations (Expr_Q));
3661 if Nkind (First (Choices (Assoc))) = N_Others_Choice
3664 Build_Actual_Subtype_Of_Component
3665 (Comp_Type, Comp_Expr);
3667 -- If the component type does not in fact depend on
3668 -- discriminants, the subtype declaration is empty.
3670 if Present (Decl) then
3671 Append_To (L, Decl);
3672 Set_Etype (Comp_Expr, Defining_Entity (Decl));
3679 if Modify_Tree_For_C
3680 and then Nkind (Expr_Q) = N_Aggregate
3681 and then Is_Array_Type (Etype (Expr_Q))
3682 and then Present (First_Index (Etype (Expr_Q)))
3685 Expr_Q_Type : constant Node_Id := Etype (Expr_Q);
3688 Build_Array_Aggr_Code
3690 Ctype => Component_Type (Expr_Q_Type),
3691 Index => First_Index (Expr_Q_Type),
3694 Is_Scalar_Type (Component_Type (Expr_Q_Type))));
3698 -- Handle an initialization expression of a controlled type
3699 -- in case it denotes a function call. In general such a
3700 -- scenario will produce a transient scope, but this will
3701 -- lead to wrong order of initialization, adjustment, and
3702 -- finalization in the context of aggregates.
3704 -- Target.Comp := Ctrl_Func_Call;
3707 -- Trans_Obj : ... := Ctrl_Func_Call; -- object
3708 -- Target.Comp := Trans_Obj;
3709 -- Finalize (Trans_Obj);
3711 -- Target.Comp._tag := ...;
3712 -- Adjust (Target.Comp);
3714 -- In the example above, the call to Finalize occurs too
3715 -- early and as a result it may leave the record component
3716 -- in a bad state. Finalization of the transient object
3717 -- should really happen after adjustment.
3719 -- To avoid this scenario, perform in-place side-effect
3720 -- removal of the function call. This eliminates the
3721 -- transient property of the function result and ensures
3722 -- correct order of actions.
3724 -- Res : ... := Ctrl_Func_Call;
3725 -- Target.Comp := Res;
3726 -- Target.Comp._tag := ...;
3727 -- Adjust (Target.Comp);
3730 if Needs_Finalization (Comp_Type)
3731 and then Nkind (Expr_Q) /= N_Aggregate
3733 Initialize_Ctrl_Record_Component
3734 (Rec_Comp => Comp_Expr,
3735 Comp_Typ => Etype (Selector),
3736 Init_Expr => Expr_Q,
3739 -- Otherwise perform single component initialization
3742 Initialize_Record_Component
3743 (Rec_Comp => Comp_Expr,
3744 Comp_Typ => Etype (Selector),
3745 Init_Expr => Expr_Q,
3751 -- comment would be good here ???
3753 elsif Ekind (Selector) = E_Discriminant
3754 and then Nkind (N) /= N_Extension_Aggregate
3755 and then Nkind (Parent (N)) = N_Component_Association
3756 and then Is_Constrained (Typ)
3758 -- We must check that the discriminant value imposed by the
3759 -- context is the same as the value given in the subaggregate,
3760 -- because after the expansion into assignments there is no
3761 -- record on which to perform a regular discriminant check.
3768 D_Val := First_Elmt (Discriminant_Constraint (Typ));
3769 Disc := First_Discriminant (Typ);
3770 while Chars (Disc) /= Chars (Selector) loop
3771 Next_Discriminant (Disc);
3775 pragma Assert (Present (D_Val));
3777 -- This check cannot performed for components that are
3778 -- constrained by a current instance, because this is not a
3779 -- value that can be compared with the actual constraint.
3781 if Nkind (Node (D_Val)) /= N_Attribute_Reference
3782 or else not Is_Entity_Name (Prefix (Node (D_Val)))
3783 or else not Is_Type (Entity (Prefix (Node (D_Val))))
3786 Make_Raise_Constraint_Error (Loc,
3789 Left_Opnd => New_Copy_Tree (Node (D_Val)),
3790 Right_Opnd => Expression (Comp)),
3791 Reason => CE_Discriminant_Check_Failed));
3794 -- Find self-reference in previous discriminant assignment,
3795 -- and replace with proper expression.
3802 while Present (Ass) loop
3803 if Nkind (Ass) = N_Assignment_Statement
3804 and then Nkind (Name (Ass)) = N_Selected_Component
3805 and then Chars (Selector_Name (Name (Ass))) =
3809 (Ass, New_Copy_Tree (Expression (Comp)));
3822 -- If the type is tagged, the tag needs to be initialized (unless we
3823 -- are in VM-mode where tags are implicit). It is done late in the
3824 -- initialization process because in some cases, we call the init
3825 -- proc of an ancestor which will not leave out the right tag.
3827 if Ancestor_Is_Expression then
3830 -- For CPP types we generated a call to the C++ default constructor
3831 -- before the components have been initialized to ensure the proper
3832 -- initialization of the _Tag component (see above).
3834 elsif Is_CPP_Class (Typ) then
3837 elsif Is_Tagged_Type (Typ) and then Tagged_Type_Expansion then
3839 Make_OK_Assignment_Statement (Loc,
3841 Make_Selected_Component (Loc,
3842 Prefix => New_Copy_Tree (Target),
3845 (First_Tag_Component (Base_Type (Typ)), Loc)),
3848 Unchecked_Convert_To (RTE (RE_Tag),
3850 (Node (First_Elmt (Access_Disp_Table (Base_Type (Typ)))),
3853 Append_To (L, Instr);
3855 -- Ada 2005 (AI-251): If the tagged type has been derived from an
3856 -- abstract interfaces we must also initialize the tags of the
3857 -- secondary dispatch tables.
3859 if Has_Interfaces (Base_Type (Typ)) then
3861 (Typ => Base_Type (Typ),
3864 Init_Tags_List => L);
3868 -- If the controllers have not been initialized yet (by lack of non-
3869 -- discriminant components), let's do it now.
3871 Generate_Finalization_Actions;
3874 end Build_Record_Aggr_Code;
3876 ---------------------------------------
3877 -- Collect_Initialization_Statements --
3878 ---------------------------------------
3880 procedure Collect_Initialization_Statements
3883 Node_After : Node_Id)
3885 Loc : constant Source_Ptr := Sloc (N);
3886 Init_Actions : constant List_Id := New_List;
3887 Init_Node : Node_Id;
3888 Comp_Stmt : Node_Id;
3891 -- Nothing to do if Obj is already frozen, as in this case we known we
3892 -- won't need to move the initialization statements about later on.
3894 if Is_Frozen (Obj) then
3899 while Next (Init_Node) /= Node_After loop
3900 Append_To (Init_Actions, Remove_Next (Init_Node));
3903 if not Is_Empty_List (Init_Actions) then
3904 Comp_Stmt := Make_Compound_Statement (Loc, Actions => Init_Actions);
3905 Insert_Action_After (Init_Node, Comp_Stmt);
3906 Set_Initialization_Statements (Obj, Comp_Stmt);
3908 end Collect_Initialization_Statements;
3910 -------------------------------
3911 -- Convert_Aggr_In_Allocator --
3912 -------------------------------
3914 procedure Convert_Aggr_In_Allocator
3919 Loc : constant Source_Ptr := Sloc (Aggr);
3920 Typ : constant Entity_Id := Etype (Aggr);
3921 Temp : constant Entity_Id := Defining_Identifier (Decl);
3923 Occ : constant Node_Id :=
3924 Unchecked_Convert_To (Typ,
3925 Make_Explicit_Dereference (Loc, New_Occurrence_Of (Temp, Loc)));
3928 if Is_Array_Type (Typ) then
3929 Convert_Array_Aggr_In_Allocator (Decl, Aggr, Occ);
3931 elsif Has_Default_Init_Comps (Aggr) then
3933 L : constant List_Id := New_List;
3934 Init_Stmts : List_Id;
3937 Init_Stmts := Late_Expansion (Aggr, Typ, Occ);
3939 if Has_Task (Typ) then
3940 Build_Task_Allocate_Block_With_Init_Stmts (L, Aggr, Init_Stmts);
3941 Insert_Actions (Alloc, L);
3943 Insert_Actions (Alloc, Init_Stmts);
3948 Insert_Actions (Alloc, Late_Expansion (Aggr, Typ, Occ));
3950 end Convert_Aggr_In_Allocator;
3952 --------------------------------
3953 -- Convert_Aggr_In_Assignment --
3954 --------------------------------
3956 procedure Convert_Aggr_In_Assignment (N : Node_Id) is
3957 Aggr : Node_Id := Expression (N);
3958 Typ : constant Entity_Id := Etype (Aggr);
3959 Occ : constant Node_Id := New_Copy_Tree (Name (N));
3962 if Nkind (Aggr) = N_Qualified_Expression then
3963 Aggr := Expression (Aggr);
3966 Insert_Actions_After (N, Late_Expansion (Aggr, Typ, Occ));
3967 end Convert_Aggr_In_Assignment;
3969 ---------------------------------
3970 -- Convert_Aggr_In_Object_Decl --
3971 ---------------------------------
3973 procedure Convert_Aggr_In_Object_Decl (N : Node_Id) is
3974 Obj : constant Entity_Id := Defining_Identifier (N);
3975 Aggr : Node_Id := Expression (N);
3976 Loc : constant Source_Ptr := Sloc (Aggr);
3977 Typ : constant Entity_Id := Etype (Aggr);
3978 Occ : constant Node_Id := New_Occurrence_Of (Obj, Loc);
3980 function Discriminants_Ok return Boolean;
3981 -- If the object type is constrained, the discriminants in the
3982 -- aggregate must be checked against the discriminants of the subtype.
3983 -- This cannot be done using Apply_Discriminant_Checks because after
3984 -- expansion there is no aggregate left to check.
3986 ----------------------
3987 -- Discriminants_Ok --
3988 ----------------------
3990 function Discriminants_Ok return Boolean is
3991 Cond : Node_Id := Empty;
4000 D := First_Discriminant (Typ);
4001 Disc1 := First_Elmt (Discriminant_Constraint (Typ));
4002 Disc2 := First_Elmt (Discriminant_Constraint (Etype (Obj)));
4003 while Present (Disc1) and then Present (Disc2) loop
4004 Val1 := Node (Disc1);
4005 Val2 := Node (Disc2);
4007 if not Is_OK_Static_Expression (Val1)
4008 or else not Is_OK_Static_Expression (Val2)
4010 Check := Make_Op_Ne (Loc,
4011 Left_Opnd => Duplicate_Subexpr (Val1),
4012 Right_Opnd => Duplicate_Subexpr (Val2));
4018 Cond := Make_Or_Else (Loc,
4020 Right_Opnd => Check);
4023 elsif Expr_Value (Val1) /= Expr_Value (Val2) then
4024 Apply_Compile_Time_Constraint_Error (Aggr,
4025 Msg => "incorrect value for discriminant&??",
4026 Reason => CE_Discriminant_Check_Failed,
4031 Next_Discriminant (D);
4036 -- If any discriminant constraint is non-static, emit a check
4038 if Present (Cond) then
4040 Make_Raise_Constraint_Error (Loc,
4042 Reason => CE_Discriminant_Check_Failed));
4046 end Discriminants_Ok;
4048 -- Start of processing for Convert_Aggr_In_Object_Decl
4051 Set_Assignment_OK (Occ);
4053 if Nkind (Aggr) = N_Qualified_Expression then
4054 Aggr := Expression (Aggr);
4057 if Has_Discriminants (Typ)
4058 and then Typ /= Etype (Obj)
4059 and then Is_Constrained (Etype (Obj))
4060 and then not Discriminants_Ok
4065 -- If the context is an extended return statement, it has its own
4066 -- finalization machinery (i.e. works like a transient scope) and
4067 -- we do not want to create an additional one, because objects on
4068 -- the finalization list of the return must be moved to the caller's
4069 -- finalization list to complete the return.
4071 -- However, if the aggregate is limited, it is built in place, and the
4072 -- controlled components are not assigned to intermediate temporaries
4073 -- so there is no need for a transient scope in this case either.
4075 if Requires_Transient_Scope (Typ)
4076 and then Ekind (Current_Scope) /= E_Return_Statement
4077 and then not Is_Limited_Type (Typ)
4079 Establish_Transient_Scope
4082 Is_Controlled (Typ) or else Has_Controlled_Component (Typ));
4086 Node_After : constant Node_Id := Next (N);
4088 Insert_Actions_After (N, Late_Expansion (Aggr, Typ, Occ));
4089 Collect_Initialization_Statements (Obj, N, Node_After);
4091 Set_No_Initialization (N);
4092 Initialize_Discriminants (N, Typ);
4093 end Convert_Aggr_In_Object_Decl;
4095 -------------------------------------
4096 -- Convert_Array_Aggr_In_Allocator --
4097 -------------------------------------
4099 procedure Convert_Array_Aggr_In_Allocator
4104 Aggr_Code : List_Id;
4105 Typ : constant Entity_Id := Etype (Aggr);
4106 Ctyp : constant Entity_Id := Component_Type (Typ);
4109 -- The target is an explicit dereference of the allocated object.
4110 -- Generate component assignments to it, as for an aggregate that
4111 -- appears on the right-hand side of an assignment statement.
4114 Build_Array_Aggr_Code (Aggr,
4116 Index => First_Index (Typ),
4118 Scalar_Comp => Is_Scalar_Type (Ctyp));
4120 Insert_Actions_After (Decl, Aggr_Code);
4121 end Convert_Array_Aggr_In_Allocator;
4123 ----------------------------
4124 -- Convert_To_Assignments --
4125 ----------------------------
4127 procedure Convert_To_Assignments (N : Node_Id; Typ : Entity_Id) is
4128 Loc : constant Source_Ptr := Sloc (N);
4132 Aggr_Code : List_Id;
4134 Target_Expr : Node_Id;
4135 Parent_Kind : Node_Kind;
4136 Unc_Decl : Boolean := False;
4137 Parent_Node : Node_Id;
4140 pragma Assert (not Is_Static_Dispatch_Table_Aggregate (N));
4141 pragma Assert (Is_Record_Type (Typ));
4143 Parent_Node := Parent (N);
4144 Parent_Kind := Nkind (Parent_Node);
4146 if Parent_Kind = N_Qualified_Expression then
4148 -- Check if we are in a unconstrained declaration because in this
4149 -- case the current delayed expansion mechanism doesn't work when
4150 -- the declared object size depend on the initializing expr.
4152 Parent_Node := Parent (Parent_Node);
4153 Parent_Kind := Nkind (Parent_Node);
4155 if Parent_Kind = N_Object_Declaration then
4157 not Is_Entity_Name (Object_Definition (Parent_Node))
4158 or else Has_Discriminants
4159 (Entity (Object_Definition (Parent_Node)))
4160 or else Is_Class_Wide_Type
4161 (Entity (Object_Definition (Parent_Node)));
4165 -- Just set the Delay flag in the cases where the transformation will be
4166 -- done top down from above.
4170 -- Internal aggregate (transformed when expanding the parent)
4172 or else Parent_Kind = N_Aggregate
4173 or else Parent_Kind = N_Extension_Aggregate
4174 or else Parent_Kind = N_Component_Association
4176 -- Allocator (see Convert_Aggr_In_Allocator)
4178 or else Parent_Kind = N_Allocator
4180 -- Object declaration (see Convert_Aggr_In_Object_Decl)
4182 or else (Parent_Kind = N_Object_Declaration and then not Unc_Decl)
4184 -- Safe assignment (see Convert_Aggr_Assignments). So far only the
4185 -- assignments in init procs are taken into account.
4187 or else (Parent_Kind = N_Assignment_Statement
4188 and then Inside_Init_Proc)
4190 -- (Ada 2005) An inherently limited type in a return statement, which
4191 -- will be handled in a build-in-place fashion, and may be rewritten
4192 -- as an extended return and have its own finalization machinery.
4193 -- In the case of a simple return, the aggregate needs to be delayed
4194 -- until the scope for the return statement has been created, so
4195 -- that any finalization chain will be associated with that scope.
4196 -- For extended returns, we delay expansion to avoid the creation
4197 -- of an unwanted transient scope that could result in premature
4198 -- finalization of the return object (which is built in place
4199 -- within the caller's scope).
4202 (Is_Limited_View (Typ)
4204 (Nkind (Parent (Parent_Node)) = N_Extended_Return_Statement
4205 or else Nkind (Parent_Node) = N_Simple_Return_Statement))
4207 Set_Expansion_Delayed (N);
4211 -- Otherwise, if a transient scope is required, create it now. If we
4212 -- are within an initialization procedure do not create such, because
4213 -- the target of the assignment must not be declared within a local
4214 -- block, and because cleanup will take place on return from the
4215 -- initialization procedure.
4217 -- Should the condition be more restrictive ???
4219 if Requires_Transient_Scope (Typ) and then not Inside_Init_Proc then
4220 Establish_Transient_Scope (N, Sec_Stack => Needs_Finalization (Typ));
4223 -- If the aggregate is nonlimited, create a temporary. If it is limited
4224 -- and context is an assignment, this is a subaggregate for an enclosing
4225 -- aggregate being expanded. It must be built in place, so use target of
4226 -- the current assignment.
4228 if Is_Limited_Type (Typ)
4229 and then Nkind (Parent (N)) = N_Assignment_Statement
4231 Target_Expr := New_Copy_Tree (Name (Parent (N)));
4232 Insert_Actions (Parent (N),
4233 Build_Record_Aggr_Code (N, Typ, Target_Expr));
4234 Rewrite (Parent (N), Make_Null_Statement (Loc));
4237 Temp := Make_Temporary (Loc, 'A', N);
4239 -- If the type inherits unknown discriminants, use the view with
4240 -- known discriminants if available.
4242 if Has_Unknown_Discriminants (Typ)
4243 and then Present (Underlying_Record_View (Typ))
4245 T := Underlying_Record_View (Typ);
4251 Make_Object_Declaration (Loc,
4252 Defining_Identifier => Temp,
4253 Object_Definition => New_Occurrence_Of (T, Loc));
4255 Set_No_Initialization (Instr);
4256 Insert_Action (N, Instr);
4257 Initialize_Discriminants (Instr, T);
4259 Target_Expr := New_Occurrence_Of (Temp, Loc);
4260 Aggr_Code := Build_Record_Aggr_Code (N, T, Target_Expr);
4262 -- Save the last assignment statement associated with the aggregate
4263 -- when building a controlled object. This reference is utilized by
4264 -- the finalization machinery when marking an object as successfully
4267 if Needs_Finalization (T) then
4268 Set_Last_Aggregate_Assignment (Temp, Last (Aggr_Code));
4271 Insert_Actions (N, Aggr_Code);
4272 Rewrite (N, New_Occurrence_Of (Temp, Loc));
4273 Analyze_And_Resolve (N, T);
4275 end Convert_To_Assignments;
4277 ---------------------------
4278 -- Convert_To_Positional --
4279 ---------------------------
4281 procedure Convert_To_Positional
4283 Max_Others_Replicate : Nat := 5;
4284 Handle_Bit_Packed : Boolean := False)
4286 Typ : constant Entity_Id := Etype (N);
4288 Static_Components : Boolean := True;
4290 procedure Check_Static_Components;
4291 -- Check whether all components of the aggregate are compile-time known
4292 -- values, and can be passed as is to the back-end without further
4294 -- An Iterated_Component_Association is treated as non-static, but there
4295 -- are possibilities for optimization here.
4300 Ixb : Node_Id) return Boolean;
4301 -- Convert the aggregate into a purely positional form if possible. On
4302 -- entry the bounds of all dimensions are known to be static, and the
4303 -- total number of components is safe enough to expand.
4305 function Is_Flat (N : Node_Id; Dims : Int) return Boolean;
4306 -- Return True iff the array N is flat (which is not trivial in the case
4307 -- of multidimensional aggregates).
4309 -----------------------------
4310 -- Check_Static_Components --
4311 -----------------------------
4313 -- Could use some comments in this body ???
4315 procedure Check_Static_Components is
4319 Static_Components := True;
4321 if Nkind (N) = N_String_Literal then
4324 elsif Present (Expressions (N)) then
4325 Expr := First (Expressions (N));
4326 while Present (Expr) loop
4327 if Nkind (Expr) /= N_Aggregate
4328 or else not Compile_Time_Known_Aggregate (Expr)
4329 or else Expansion_Delayed (Expr)
4331 Static_Components := False;
4339 if Nkind (N) = N_Aggregate
4340 and then Present (Component_Associations (N))
4342 Expr := First (Component_Associations (N));
4343 while Present (Expr) loop
4344 if Nkind_In (Expression (Expr), N_Integer_Literal,
4349 elsif Is_Entity_Name (Expression (Expr))
4350 and then Present (Entity (Expression (Expr)))
4351 and then Ekind (Entity (Expression (Expr))) =
4352 E_Enumeration_Literal
4356 elsif Nkind (Expression (Expr)) /= N_Aggregate
4357 or else not Compile_Time_Known_Aggregate (Expression (Expr))
4358 or else Expansion_Delayed (Expression (Expr))
4359 or else Nkind (Expr) = N_Iterated_Component_Association
4361 Static_Components := False;
4368 end Check_Static_Components;
4377 Ixb : Node_Id) return Boolean
4379 Loc : constant Source_Ptr := Sloc (N);
4380 Blo : constant Node_Id := Type_Low_Bound (Etype (Ixb));
4381 Lo : constant Node_Id := Type_Low_Bound (Etype (Ix));
4382 Hi : constant Node_Id := Type_High_Bound (Etype (Ix));
4386 Others_Present : Boolean := False;
4389 if Nkind (Original_Node (N)) = N_String_Literal then
4393 if not Compile_Time_Known_Value (Lo)
4394 or else not Compile_Time_Known_Value (Hi)
4399 Lov := Expr_Value (Lo);
4400 Hiv := Expr_Value (Hi);
4402 -- Check if there is an others choice
4404 if Present (Component_Associations (N)) then
4410 Assoc := First (Component_Associations (N));
4411 while Present (Assoc) loop
4413 -- If this is a box association, flattening is in general
4414 -- not possible because at this point we cannot tell if the
4415 -- default is static or even exists.
4417 if Box_Present (Assoc) then
4420 elsif Nkind (Assoc) = N_Iterated_Component_Association then
4424 Choice := First (Choice_List (Assoc));
4426 while Present (Choice) loop
4427 if Nkind (Choice) = N_Others_Choice then
4428 Others_Present := True;
4439 -- If the low bound is not known at compile time and others is not
4440 -- present we can proceed since the bounds can be obtained from the
4444 or else (not Compile_Time_Known_Value (Blo) and then Others_Present)
4449 -- Determine if set of alternatives is suitable for conversion and
4450 -- build an array containing the values in sequence.
4453 Vals : array (UI_To_Int (Lov) .. UI_To_Int (Hiv))
4454 of Node_Id := (others => Empty);
4455 -- The values in the aggregate sorted appropriately
4458 -- Same data as Vals in list form
4461 -- Used to validate Max_Others_Replicate limit
4464 Num : Int := UI_To_Int (Lov);
4470 if Present (Expressions (N)) then
4471 Elmt := First (Expressions (N));
4472 while Present (Elmt) loop
4473 if Nkind (Elmt) = N_Aggregate
4474 and then Present (Next_Index (Ix))
4476 not Flatten (Elmt, Next_Index (Ix), Next_Index (Ixb))
4481 Vals (Num) := Relocate_Node (Elmt);
4488 if No (Component_Associations (N)) then
4492 Elmt := First (Component_Associations (N));
4494 if Nkind (Expression (Elmt)) = N_Aggregate then
4495 if Present (Next_Index (Ix))
4498 (Expression (Elmt), Next_Index (Ix), Next_Index (Ixb))
4504 Component_Loop : while Present (Elmt) loop
4505 Choice := First (Choice_List (Elmt));
4506 Choice_Loop : while Present (Choice) loop
4508 -- If we have an others choice, fill in the missing elements
4509 -- subject to the limit established by Max_Others_Replicate.
4511 if Nkind (Choice) = N_Others_Choice then
4514 for J in Vals'Range loop
4515 if No (Vals (J)) then
4516 Vals (J) := New_Copy_Tree (Expression (Elmt));
4517 Rep_Count := Rep_Count + 1;
4519 -- Check for maximum others replication. Note that
4520 -- we skip this test if either of the restrictions
4521 -- No_Elaboration_Code or No_Implicit_Loops is
4522 -- active, if this is a preelaborable unit or
4523 -- a predefined unit, or if the unit must be
4524 -- placed in data memory. This also ensures that
4525 -- predefined units get the same level of constant
4526 -- folding in Ada 95 and Ada 2005, where their
4527 -- categorization has changed.
4530 P : constant Entity_Id :=
4531 Cunit_Entity (Current_Sem_Unit);
4534 -- Check if duplication OK and if so continue
4537 if Restriction_Active (No_Elaboration_Code)
4538 or else Restriction_Active (No_Implicit_Loops)
4540 (Ekind (Current_Scope) = E_Package
4541 and then Static_Elaboration_Desired
4543 or else Is_Preelaborated (P)
4544 or else (Ekind (P) = E_Package_Body
4546 Is_Preelaborated (Spec_Entity (P)))
4548 Is_Predefined_Unit (Get_Source_Unit (P))
4552 -- If duplication not OK, then we return False
4553 -- if the replication count is too high
4555 elsif Rep_Count > Max_Others_Replicate then
4558 -- Continue on if duplication not OK, but the
4559 -- replication count is not excessive.
4568 exit Component_Loop;
4570 -- Case of a subtype mark, identifier or expanded name
4572 elsif Is_Entity_Name (Choice)
4573 and then Is_Type (Entity (Choice))
4575 Lo := Type_Low_Bound (Etype (Choice));
4576 Hi := Type_High_Bound (Etype (Choice));
4578 -- Case of subtype indication
4580 elsif Nkind (Choice) = N_Subtype_Indication then
4581 Lo := Low_Bound (Range_Expression (Constraint (Choice)));
4582 Hi := High_Bound (Range_Expression (Constraint (Choice)));
4586 elsif Nkind (Choice) = N_Range then
4587 Lo := Low_Bound (Choice);
4588 Hi := High_Bound (Choice);
4590 -- Normal subexpression case
4592 else pragma Assert (Nkind (Choice) in N_Subexpr);
4593 if not Compile_Time_Known_Value (Choice) then
4597 Choice_Index := UI_To_Int (Expr_Value (Choice));
4599 if Choice_Index in Vals'Range then
4600 Vals (Choice_Index) :=
4601 New_Copy_Tree (Expression (Elmt));
4604 -- Choice is statically out-of-range, will be
4605 -- rewritten to raise Constraint_Error.
4613 -- Range cases merge with Lo,Hi set
4615 if not Compile_Time_Known_Value (Lo)
4617 not Compile_Time_Known_Value (Hi)
4622 for J in UI_To_Int (Expr_Value (Lo)) ..
4623 UI_To_Int (Expr_Value (Hi))
4625 Vals (J) := New_Copy_Tree (Expression (Elmt));
4631 end loop Choice_Loop;
4634 end loop Component_Loop;
4636 -- If we get here the conversion is possible
4639 for J in Vals'Range loop
4640 Append (Vals (J), Vlist);
4643 Rewrite (N, Make_Aggregate (Loc, Expressions => Vlist));
4644 Set_Aggregate_Bounds (N, Aggregate_Bounds (Original_Node (N)));
4653 function Is_Flat (N : Node_Id; Dims : Int) return Boolean is
4660 elsif Nkind (N) = N_Aggregate then
4661 if Present (Component_Associations (N)) then
4665 Elmt := First (Expressions (N));
4666 while Present (Elmt) loop
4667 if not Is_Flat (Elmt, Dims - 1) then
4681 -- Start of processing for Convert_To_Positional
4684 -- Only convert to positional when generating C in case of an
4685 -- object declaration, this is the only case where aggregates are
4688 if Modify_Tree_For_C and then not In_Object_Declaration (N) then
4692 -- Ada 2005 (AI-287): Do not convert in case of default initialized
4693 -- components because in this case will need to call the corresponding
4696 if Has_Default_Init_Comps (N) then
4700 if Is_Flat (N, Number_Dimensions (Typ)) then
4704 if Is_Bit_Packed_Array (Typ) and then not Handle_Bit_Packed then
4708 -- Do not convert to positional if controlled components are involved
4709 -- since these require special processing
4711 if Has_Controlled_Component (Typ) then
4715 Check_Static_Components;
4717 -- If the size is known, or all the components are static, try to
4718 -- build a fully positional aggregate.
4720 -- The size of the type may not be known for an aggregate with
4721 -- discriminated array components, but if the components are static
4722 -- it is still possible to verify statically that the length is
4723 -- compatible with the upper bound of the type, and therefore it is
4724 -- worth flattening such aggregates as well.
4726 -- For now the back-end expands these aggregates into individual
4727 -- assignments to the target anyway, but it is conceivable that
4728 -- it will eventually be able to treat such aggregates statically???
4730 if Aggr_Size_OK (N, Typ)
4731 and then Flatten (N, First_Index (Typ), First_Index (Base_Type (Typ)))
4733 if Static_Components then
4734 Set_Compile_Time_Known_Aggregate (N);
4735 Set_Expansion_Delayed (N, False);
4738 Analyze_And_Resolve (N, Typ);
4741 -- If Static_Elaboration_Desired has been specified, diagnose aggregates
4742 -- that will still require initialization code.
4744 if (Ekind (Current_Scope) = E_Package
4745 and then Static_Elaboration_Desired (Current_Scope))
4746 and then Nkind (Parent (N)) = N_Object_Declaration
4752 if Nkind (N) = N_Aggregate and then Present (Expressions (N)) then
4753 Expr := First (Expressions (N));
4754 while Present (Expr) loop
4755 if Nkind_In (Expr, N_Integer_Literal, N_Real_Literal)
4757 (Is_Entity_Name (Expr)
4758 and then Ekind (Entity (Expr)) = E_Enumeration_Literal)
4764 ("non-static object requires elaboration code??", N);
4771 if Present (Component_Associations (N)) then
4772 Error_Msg_N ("object requires elaboration code??", N);
4777 end Convert_To_Positional;
4779 ----------------------------
4780 -- Expand_Array_Aggregate --
4781 ----------------------------
4783 -- Array aggregate expansion proceeds as follows:
4785 -- 1. If requested we generate code to perform all the array aggregate
4786 -- bound checks, specifically
4788 -- (a) Check that the index range defined by aggregate bounds is
4789 -- compatible with corresponding index subtype.
4791 -- (b) If an others choice is present check that no aggregate
4792 -- index is outside the bounds of the index constraint.
4794 -- (c) For multidimensional arrays make sure that all subaggregates
4795 -- corresponding to the same dimension have the same bounds.
4797 -- 2. Check for packed array aggregate which can be converted to a
4798 -- constant so that the aggregate disappears completely.
4800 -- 3. Check case of nested aggregate. Generally nested aggregates are
4801 -- handled during the processing of the parent aggregate.
4803 -- 4. Check if the aggregate can be statically processed. If this is the
4804 -- case pass it as is to Gigi. Note that a necessary condition for
4805 -- static processing is that the aggregate be fully positional.
4807 -- 5. If in place aggregate expansion is possible (i.e. no need to create
4808 -- a temporary) then mark the aggregate as such and return. Otherwise
4809 -- create a new temporary and generate the appropriate initialization
4812 procedure Expand_Array_Aggregate (N : Node_Id) is
4813 Loc : constant Source_Ptr := Sloc (N);
4815 Typ : constant Entity_Id := Etype (N);
4816 Ctyp : constant Entity_Id := Component_Type (Typ);
4817 -- Typ is the correct constrained array subtype of the aggregate
4818 -- Ctyp is the corresponding component type.
4820 Aggr_Dimension : constant Pos := Number_Dimensions (Typ);
4821 -- Number of aggregate index dimensions
4823 Aggr_Low : array (1 .. Aggr_Dimension) of Node_Id;
4824 Aggr_High : array (1 .. Aggr_Dimension) of Node_Id;
4825 -- Low and High bounds of the constraint for each aggregate index
4827 Aggr_Index_Typ : array (1 .. Aggr_Dimension) of Entity_Id;
4828 -- The type of each index
4830 In_Place_Assign_OK_For_Declaration : Boolean := False;
4831 -- True if we are to generate an in place assignment for a declaration
4833 Maybe_In_Place_OK : Boolean;
4834 -- If the type is neither controlled nor packed and the aggregate
4835 -- is the expression in an assignment, assignment in place may be
4836 -- possible, provided other conditions are met on the LHS.
4838 Others_Present : array (1 .. Aggr_Dimension) of Boolean :=
4840 -- If Others_Present (J) is True, then there is an others choice in one
4841 -- of the subaggregates of N at dimension J.
4843 function Aggr_Assignment_OK_For_Backend (N : Node_Id) return Boolean;
4844 -- Returns true if an aggregate assignment can be done by the back end
4846 procedure Build_Constrained_Type (Positional : Boolean);
4847 -- If the subtype is not static or unconstrained, build a constrained
4848 -- type using the computable sizes of the aggregate and its sub-
4851 procedure Check_Bounds (Aggr_Bounds : Node_Id; Index_Bounds : Node_Id);
4852 -- Checks that the bounds of Aggr_Bounds are within the bounds defined
4855 procedure Check_Same_Aggr_Bounds (Sub_Aggr : Node_Id; Dim : Pos);
4856 -- Checks that in a multidimensional array aggregate all subaggregates
4857 -- corresponding to the same dimension have the same bounds. Sub_Aggr is
4858 -- an array subaggregate. Dim is the dimension corresponding to the
4861 procedure Compute_Others_Present (Sub_Aggr : Node_Id; Dim : Pos);
4862 -- Computes the values of array Others_Present. Sub_Aggr is the array
4863 -- subaggregate we start the computation from. Dim is the dimension
4864 -- corresponding to the subaggregate.
4866 function In_Place_Assign_OK return Boolean;
4867 -- Simple predicate to determine whether an aggregate assignment can
4868 -- be done in place, because none of the new values can depend on the
4869 -- components of the target of the assignment.
4871 procedure Others_Check (Sub_Aggr : Node_Id; Dim : Pos);
4872 -- Checks that if an others choice is present in any subaggregate, no
4873 -- aggregate index is outside the bounds of the index constraint.
4874 -- Sub_Aggr is an array subaggregate. Dim is the dimension corresponding
4875 -- to the subaggregate.
4877 function Safe_Left_Hand_Side (N : Node_Id) return Boolean;
4878 -- In addition to Maybe_In_Place_OK, in order for an aggregate to be
4879 -- built directly into the target of the assignment it must be free
4882 ------------------------------------
4883 -- Aggr_Assignment_OK_For_Backend --
4884 ------------------------------------
4886 -- Backend processing by Gigi/gcc is possible only if all the following
4887 -- conditions are met:
4889 -- 1. N consists of a single OTHERS choice, possibly recursively
4891 -- 2. The array type is not packed
4893 -- 3. The array type has no atomic components
4895 -- 4. The array type has no null ranges (the purpose of this is to
4896 -- avoid a bogus warning for an out-of-range value).
4898 -- 5. The component type is discrete
4900 -- 6. The component size is Storage_Unit or the value is of the form
4901 -- M * (1 + A**1 + A**2 + .. A**(K-1)) where A = 2**(Storage_Unit)
4902 -- and M in 1 .. A-1. This can also be viewed as K occurrences of
4903 -- the 8-bit value M, concatenated together.
4905 -- The ultimate goal is to generate a call to a fast memset routine
4906 -- specifically optimized for the target.
4908 function Aggr_Assignment_OK_For_Backend (N : Node_Id) return Boolean is
4911 Expr : Node_Id := N;
4919 -- Recurse as far as possible to find the innermost component type
4922 while Is_Array_Type (Ctyp) loop
4923 if Nkind (Expr) /= N_Aggregate
4924 or else not Is_Others_Aggregate (Expr)
4929 if Present (Packed_Array_Impl_Type (Ctyp)) then
4933 if Has_Atomic_Components (Ctyp) then
4937 Index := First_Index (Ctyp);
4938 while Present (Index) loop
4939 Get_Index_Bounds (Index, Low, High);
4941 if Is_Null_Range (Low, High) then
4948 Expr := Expression (First (Component_Associations (Expr)));
4950 for J in 1 .. Number_Dimensions (Ctyp) - 1 loop
4951 if Nkind (Expr) /= N_Aggregate
4952 or else not Is_Others_Aggregate (Expr)
4957 Expr := Expression (First (Component_Associations (Expr)));
4960 Ctyp := Component_Type (Ctyp);
4962 if Is_Atomic_Or_VFA (Ctyp) then
4967 -- An Iterated_Component_Association involves a loop (in most cases)
4968 -- and is never static.
4970 if Nkind (Parent (Expr)) = N_Iterated_Component_Association then
4974 if not Is_Discrete_Type (Ctyp) then
4978 -- The expression needs to be analyzed if True is returned
4980 Analyze_And_Resolve (Expr, Ctyp);
4982 -- The back end uses the Esize as the precision of the type
4984 Nunits := UI_To_Int (Esize (Ctyp)) / System_Storage_Unit;
4990 if not Compile_Time_Known_Value (Expr) then
4994 Value := Expr_Value (Expr);
4996 if Has_Biased_Representation (Ctyp) then
4997 Value := Value - Expr_Value (Type_Low_Bound (Ctyp));
5000 -- Values 0 and -1 immediately satisfy the last check
5002 if Value = Uint_0 or else Value = Uint_Minus_1 then
5006 -- We need to work with an unsigned value
5009 Value := Value + 2**(System_Storage_Unit * Nunits);
5012 Remainder := Value rem 2**System_Storage_Unit;
5014 for J in 1 .. Nunits - 1 loop
5015 Value := Value / 2**System_Storage_Unit;
5017 if Value rem 2**System_Storage_Unit /= Remainder then
5023 end Aggr_Assignment_OK_For_Backend;
5025 ----------------------------
5026 -- Build_Constrained_Type --
5027 ----------------------------
5029 procedure Build_Constrained_Type (Positional : Boolean) is
5030 Loc : constant Source_Ptr := Sloc (N);
5031 Agg_Type : constant Entity_Id := Make_Temporary (Loc, 'A');
5034 Typ : constant Entity_Id := Etype (N);
5035 Indexes : constant List_Id := New_List;
5040 -- If the aggregate is purely positional, all its subaggregates
5041 -- have the same size. We collect the dimensions from the first
5042 -- subaggregate at each level.
5047 for D in 1 .. Number_Dimensions (Typ) loop
5048 Sub_Agg := First (Expressions (Sub_Agg));
5052 while Present (Comp) loop
5059 Low_Bound => Make_Integer_Literal (Loc, 1),
5060 High_Bound => Make_Integer_Literal (Loc, Num)));
5064 -- We know the aggregate type is unconstrained and the aggregate
5065 -- is not processable by the back end, therefore not necessarily
5066 -- positional. Retrieve each dimension bounds (computed earlier).
5068 for D in 1 .. Number_Dimensions (Typ) loop
5071 Low_Bound => Aggr_Low (D),
5072 High_Bound => Aggr_High (D)));
5077 Make_Full_Type_Declaration (Loc,
5078 Defining_Identifier => Agg_Type,
5080 Make_Constrained_Array_Definition (Loc,
5081 Discrete_Subtype_Definitions => Indexes,
5082 Component_Definition =>
5083 Make_Component_Definition (Loc,
5084 Aliased_Present => False,
5085 Subtype_Indication =>
5086 New_Occurrence_Of (Component_Type (Typ), Loc))));
5088 Insert_Action (N, Decl);
5090 Set_Etype (N, Agg_Type);
5091 Set_Is_Itype (Agg_Type);
5092 Freeze_Itype (Agg_Type, N);
5093 end Build_Constrained_Type;
5099 procedure Check_Bounds (Aggr_Bounds : Node_Id; Index_Bounds : Node_Id) is
5106 Cond : Node_Id := Empty;
5109 Get_Index_Bounds (Aggr_Bounds, Aggr_Lo, Aggr_Hi);
5110 Get_Index_Bounds (Index_Bounds, Ind_Lo, Ind_Hi);
5112 -- Generate the following test:
5114 -- [constraint_error when
5115 -- Aggr_Lo <= Aggr_Hi and then
5116 -- (Aggr_Lo < Ind_Lo or else Aggr_Hi > Ind_Hi)]
5118 -- As an optimization try to see if some tests are trivially vacuous
5119 -- because we are comparing an expression against itself.
5121 if Aggr_Lo = Ind_Lo and then Aggr_Hi = Ind_Hi then
5124 elsif Aggr_Hi = Ind_Hi then
5127 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
5128 Right_Opnd => Duplicate_Subexpr_Move_Checks (Ind_Lo));
5130 elsif Aggr_Lo = Ind_Lo then
5133 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi),
5134 Right_Opnd => Duplicate_Subexpr_Move_Checks (Ind_Hi));
5141 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
5142 Right_Opnd => Duplicate_Subexpr_Move_Checks (Ind_Lo)),
5146 Left_Opnd => Duplicate_Subexpr (Aggr_Hi),
5147 Right_Opnd => Duplicate_Subexpr (Ind_Hi)));
5150 if Present (Cond) then
5155 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
5156 Right_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi)),
5158 Right_Opnd => Cond);
5160 Set_Analyzed (Left_Opnd (Left_Opnd (Cond)), False);
5161 Set_Analyzed (Right_Opnd (Left_Opnd (Cond)), False);
5163 Make_Raise_Constraint_Error (Loc,
5165 Reason => CE_Range_Check_Failed));
5169 ----------------------------
5170 -- Check_Same_Aggr_Bounds --
5171 ----------------------------
5173 procedure Check_Same_Aggr_Bounds (Sub_Aggr : Node_Id; Dim : Pos) is
5174 Sub_Lo : constant Node_Id := Low_Bound (Aggregate_Bounds (Sub_Aggr));
5175 Sub_Hi : constant Node_Id := High_Bound (Aggregate_Bounds (Sub_Aggr));
5176 -- The bounds of this specific subaggregate
5178 Aggr_Lo : constant Node_Id := Aggr_Low (Dim);
5179 Aggr_Hi : constant Node_Id := Aggr_High (Dim);
5180 -- The bounds of the aggregate for this dimension
5182 Ind_Typ : constant Entity_Id := Aggr_Index_Typ (Dim);
5183 -- The index type for this dimension.xxx
5185 Cond : Node_Id := Empty;
5190 -- If index checks are on generate the test
5192 -- [constraint_error when
5193 -- Aggr_Lo /= Sub_Lo or else Aggr_Hi /= Sub_Hi]
5195 -- As an optimization try to see if some tests are trivially vacuos
5196 -- because we are comparing an expression against itself. Also for
5197 -- the first dimension the test is trivially vacuous because there
5198 -- is just one aggregate for dimension 1.
5200 if Index_Checks_Suppressed (Ind_Typ) then
5203 elsif Dim = 1 or else (Aggr_Lo = Sub_Lo and then Aggr_Hi = Sub_Hi)
5207 elsif Aggr_Hi = Sub_Hi then
5210 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
5211 Right_Opnd => Duplicate_Subexpr_Move_Checks (Sub_Lo));
5213 elsif Aggr_Lo = Sub_Lo then
5216 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi),
5217 Right_Opnd => Duplicate_Subexpr_Move_Checks (Sub_Hi));
5224 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
5225 Right_Opnd => Duplicate_Subexpr_Move_Checks (Sub_Lo)),
5229 Left_Opnd => Duplicate_Subexpr (Aggr_Hi),
5230 Right_Opnd => Duplicate_Subexpr (Sub_Hi)));
5233 if Present (Cond) then
5235 Make_Raise_Constraint_Error (Loc,
5237 Reason => CE_Length_Check_Failed));
5240 -- Now look inside the subaggregate to see if there is more work
5242 if Dim < Aggr_Dimension then
5244 -- Process positional components
5246 if Present (Expressions (Sub_Aggr)) then
5247 Expr := First (Expressions (Sub_Aggr));
5248 while Present (Expr) loop
5249 Check_Same_Aggr_Bounds (Expr, Dim + 1);
5254 -- Process component associations
5256 if Present (Component_Associations (Sub_Aggr)) then
5257 Assoc := First (Component_Associations (Sub_Aggr));
5258 while Present (Assoc) loop
5259 Expr := Expression (Assoc);
5260 Check_Same_Aggr_Bounds (Expr, Dim + 1);
5265 end Check_Same_Aggr_Bounds;
5267 ----------------------------
5268 -- Compute_Others_Present --
5269 ----------------------------
5271 procedure Compute_Others_Present (Sub_Aggr : Node_Id; Dim : Pos) is
5276 if Present (Component_Associations (Sub_Aggr)) then
5277 Assoc := Last (Component_Associations (Sub_Aggr));
5279 if Nkind (First (Choice_List (Assoc))) = N_Others_Choice then
5280 Others_Present (Dim) := True;
5284 -- Now look inside the subaggregate to see if there is more work
5286 if Dim < Aggr_Dimension then
5288 -- Process positional components
5290 if Present (Expressions (Sub_Aggr)) then
5291 Expr := First (Expressions (Sub_Aggr));
5292 while Present (Expr) loop
5293 Compute_Others_Present (Expr, Dim + 1);
5298 -- Process component associations
5300 if Present (Component_Associations (Sub_Aggr)) then
5301 Assoc := First (Component_Associations (Sub_Aggr));
5302 while Present (Assoc) loop
5303 Expr := Expression (Assoc);
5304 Compute_Others_Present (Expr, Dim + 1);
5309 end Compute_Others_Present;
5311 ------------------------
5312 -- In_Place_Assign_OK --
5313 ------------------------
5315 function In_Place_Assign_OK return Boolean is
5323 function Safe_Aggregate (Aggr : Node_Id) return Boolean;
5324 -- Check recursively that each component of a (sub)aggregate does not
5325 -- depend on the variable being assigned to.
5327 function Safe_Component (Expr : Node_Id) return Boolean;
5328 -- Verify that an expression cannot depend on the variable being
5329 -- assigned to. Room for improvement here (but less than before).
5331 --------------------
5332 -- Safe_Aggregate --
5333 --------------------
5335 function Safe_Aggregate (Aggr : Node_Id) return Boolean is
5339 if Present (Expressions (Aggr)) then
5340 Expr := First (Expressions (Aggr));
5341 while Present (Expr) loop
5342 if Nkind (Expr) = N_Aggregate then
5343 if not Safe_Aggregate (Expr) then
5347 elsif not Safe_Component (Expr) then
5355 if Present (Component_Associations (Aggr)) then
5356 Expr := First (Component_Associations (Aggr));
5357 while Present (Expr) loop
5358 if Nkind (Expression (Expr)) = N_Aggregate then
5359 if not Safe_Aggregate (Expression (Expr)) then
5363 -- If association has a box, no way to determine yet
5364 -- whether default can be assigned in place.
5366 elsif Box_Present (Expr) then
5369 elsif not Safe_Component (Expression (Expr)) then
5380 --------------------
5381 -- Safe_Component --
5382 --------------------
5384 function Safe_Component (Expr : Node_Id) return Boolean is
5385 Comp : Node_Id := Expr;
5387 function Check_Component (Comp : Node_Id) return Boolean;
5388 -- Do the recursive traversal, after copy
5390 ---------------------
5391 -- Check_Component --
5392 ---------------------
5394 function Check_Component (Comp : Node_Id) return Boolean is
5396 if Is_Overloaded (Comp) then
5400 return Compile_Time_Known_Value (Comp)
5402 or else (Is_Entity_Name (Comp)
5403 and then Present (Entity (Comp))
5404 and then No (Renamed_Object (Entity (Comp))))
5406 or else (Nkind (Comp) = N_Attribute_Reference
5407 and then Check_Component (Prefix (Comp)))
5409 or else (Nkind (Comp) in N_Binary_Op
5410 and then Check_Component (Left_Opnd (Comp))
5411 and then Check_Component (Right_Opnd (Comp)))
5413 or else (Nkind (Comp) in N_Unary_Op
5414 and then Check_Component (Right_Opnd (Comp)))
5416 or else (Nkind (Comp) = N_Selected_Component
5417 and then Check_Component (Prefix (Comp)))
5419 or else (Nkind (Comp) = N_Unchecked_Type_Conversion
5420 and then Check_Component (Expression (Comp)));
5421 end Check_Component;
5423 -- Start of processing for Safe_Component
5426 -- If the component appears in an association that may correspond
5427 -- to more than one element, it is not analyzed before expansion
5428 -- into assignments, to avoid side effects. We analyze, but do not
5429 -- resolve the copy, to obtain sufficient entity information for
5430 -- the checks that follow. If component is overloaded we assume
5431 -- an unsafe function call.
5433 if not Analyzed (Comp) then
5434 if Is_Overloaded (Expr) then
5437 elsif Nkind (Expr) = N_Aggregate
5438 and then not Is_Others_Aggregate (Expr)
5442 elsif Nkind (Expr) = N_Allocator then
5444 -- For now, too complex to analyze
5449 Comp := New_Copy_Tree (Expr);
5450 Set_Parent (Comp, Parent (Expr));
5454 if Nkind (Comp) = N_Aggregate then
5455 return Safe_Aggregate (Comp);
5457 return Check_Component (Comp);
5461 -- Start of processing for In_Place_Assign_OK
5464 if Present (Component_Associations (N)) then
5466 -- On assignment, sliding can take place, so we cannot do the
5467 -- assignment in place unless the bounds of the aggregate are
5468 -- statically equal to those of the target.
5470 -- If the aggregate is given by an others choice, the bounds are
5471 -- derived from the left-hand side, and the assignment is safe if
5472 -- the expression is.
5474 if Is_Others_Aggregate (N) then
5477 (Expression (First (Component_Associations (N))));
5480 Aggr_In := First_Index (Etype (N));
5482 if Nkind (Parent (N)) = N_Assignment_Statement then
5483 Obj_In := First_Index (Etype (Name (Parent (N))));
5486 -- Context is an allocator. Check bounds of aggregate against
5487 -- given type in qualified expression.
5489 pragma Assert (Nkind (Parent (Parent (N))) = N_Allocator);
5491 First_Index (Etype (Entity (Subtype_Mark (Parent (N)))));
5494 while Present (Aggr_In) loop
5495 Get_Index_Bounds (Aggr_In, Aggr_Lo, Aggr_Hi);
5496 Get_Index_Bounds (Obj_In, Obj_Lo, Obj_Hi);
5498 if not Compile_Time_Known_Value (Aggr_Lo)
5499 or else not Compile_Time_Known_Value (Aggr_Hi)
5500 or else not Compile_Time_Known_Value (Obj_Lo)
5501 or else not Compile_Time_Known_Value (Obj_Hi)
5502 or else Expr_Value (Aggr_Lo) /= Expr_Value (Obj_Lo)
5503 or else Expr_Value (Aggr_Hi) /= Expr_Value (Obj_Hi)
5508 Next_Index (Aggr_In);
5509 Next_Index (Obj_In);
5513 -- Now check the component values themselves
5515 return Safe_Aggregate (N);
5516 end In_Place_Assign_OK;
5522 procedure Others_Check (Sub_Aggr : Node_Id; Dim : Pos) is
5523 Aggr_Lo : constant Node_Id := Aggr_Low (Dim);
5524 Aggr_Hi : constant Node_Id := Aggr_High (Dim);
5525 -- The bounds of the aggregate for this dimension
5527 Ind_Typ : constant Entity_Id := Aggr_Index_Typ (Dim);
5528 -- The index type for this dimension
5530 Need_To_Check : Boolean := False;
5532 Choices_Lo : Node_Id := Empty;
5533 Choices_Hi : Node_Id := Empty;
5534 -- The lowest and highest discrete choices for a named subaggregate
5536 Nb_Choices : Int := -1;
5537 -- The number of discrete non-others choices in this subaggregate
5539 Nb_Elements : Uint := Uint_0;
5540 -- The number of elements in a positional aggregate
5542 Cond : Node_Id := Empty;
5549 -- Check if we have an others choice. If we do make sure that this
5550 -- subaggregate contains at least one element in addition to the
5553 if Range_Checks_Suppressed (Ind_Typ) then
5554 Need_To_Check := False;
5556 elsif Present (Expressions (Sub_Aggr))
5557 and then Present (Component_Associations (Sub_Aggr))
5559 Need_To_Check := True;
5561 elsif Present (Component_Associations (Sub_Aggr)) then
5562 Assoc := Last (Component_Associations (Sub_Aggr));
5564 if Nkind (First (Choice_List (Assoc))) /= N_Others_Choice then
5565 Need_To_Check := False;
5568 -- Count the number of discrete choices. Start with -1 because
5569 -- the others choice does not count.
5571 -- Is there some reason we do not use List_Length here ???
5574 Assoc := First (Component_Associations (Sub_Aggr));
5575 while Present (Assoc) loop
5576 Choice := First (Choice_List (Assoc));
5577 while Present (Choice) loop
5578 Nb_Choices := Nb_Choices + 1;
5585 -- If there is only an others choice nothing to do
5587 Need_To_Check := (Nb_Choices > 0);
5591 Need_To_Check := False;
5594 -- If we are dealing with a positional subaggregate with an others
5595 -- choice then compute the number or positional elements.
5597 if Need_To_Check and then Present (Expressions (Sub_Aggr)) then
5598 Expr := First (Expressions (Sub_Aggr));
5599 Nb_Elements := Uint_0;
5600 while Present (Expr) loop
5601 Nb_Elements := Nb_Elements + 1;
5605 -- If the aggregate contains discrete choices and an others choice
5606 -- compute the smallest and largest discrete choice values.
5608 elsif Need_To_Check then
5609 Compute_Choices_Lo_And_Choices_Hi : declare
5611 Table : Case_Table_Type (1 .. Nb_Choices);
5612 -- Used to sort all the different choice values
5619 Assoc := First (Component_Associations (Sub_Aggr));
5620 while Present (Assoc) loop
5621 Choice := First (Choice_List (Assoc));
5622 while Present (Choice) loop
5623 if Nkind (Choice) = N_Others_Choice then
5627 Get_Index_Bounds (Choice, Low, High);
5628 Table (J).Choice_Lo := Low;
5629 Table (J).Choice_Hi := High;
5638 -- Sort the discrete choices
5640 Sort_Case_Table (Table);
5642 Choices_Lo := Table (1).Choice_Lo;
5643 Choices_Hi := Table (Nb_Choices).Choice_Hi;
5644 end Compute_Choices_Lo_And_Choices_Hi;
5647 -- If no others choice in this subaggregate, or the aggregate
5648 -- comprises only an others choice, nothing to do.
5650 if not Need_To_Check then
5653 -- If we are dealing with an aggregate containing an others choice
5654 -- and positional components, we generate the following test:
5656 -- if Ind_Typ'Pos (Aggr_Lo) + (Nb_Elements - 1) >
5657 -- Ind_Typ'Pos (Aggr_Hi)
5659 -- raise Constraint_Error;
5662 elsif Nb_Elements > Uint_0 then
5668 Make_Attribute_Reference (Loc,
5669 Prefix => New_Occurrence_Of (Ind_Typ, Loc),
5670 Attribute_Name => Name_Pos,
5673 (Duplicate_Subexpr_Move_Checks (Aggr_Lo))),
5674 Right_Opnd => Make_Integer_Literal (Loc, Nb_Elements - 1)),
5677 Make_Attribute_Reference (Loc,
5678 Prefix => New_Occurrence_Of (Ind_Typ, Loc),
5679 Attribute_Name => Name_Pos,
5680 Expressions => New_List (
5681 Duplicate_Subexpr_Move_Checks (Aggr_Hi))));
5683 -- If we are dealing with an aggregate containing an others choice
5684 -- and discrete choices we generate the following test:
5686 -- [constraint_error when
5687 -- Choices_Lo < Aggr_Lo or else Choices_Hi > Aggr_Hi];
5694 Left_Opnd => Duplicate_Subexpr_Move_Checks (Choices_Lo),
5695 Right_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo)),
5699 Left_Opnd => Duplicate_Subexpr (Choices_Hi),
5700 Right_Opnd => Duplicate_Subexpr (Aggr_Hi)));
5703 if Present (Cond) then
5705 Make_Raise_Constraint_Error (Loc,
5707 Reason => CE_Length_Check_Failed));
5708 -- Questionable reason code, shouldn't that be a
5709 -- CE_Range_Check_Failed ???
5712 -- Now look inside the subaggregate to see if there is more work
5714 if Dim < Aggr_Dimension then
5716 -- Process positional components
5718 if Present (Expressions (Sub_Aggr)) then
5719 Expr := First (Expressions (Sub_Aggr));
5720 while Present (Expr) loop
5721 Others_Check (Expr, Dim + 1);
5726 -- Process component associations
5728 if Present (Component_Associations (Sub_Aggr)) then
5729 Assoc := First (Component_Associations (Sub_Aggr));
5730 while Present (Assoc) loop
5731 Expr := Expression (Assoc);
5732 Others_Check (Expr, Dim + 1);
5739 -------------------------
5740 -- Safe_Left_Hand_Side --
5741 -------------------------
5743 function Safe_Left_Hand_Side (N : Node_Id) return Boolean is
5744 function Is_Safe_Index (Indx : Node_Id) return Boolean;
5745 -- If the left-hand side includes an indexed component, check that
5746 -- the indexes are free of side effects.
5752 function Is_Safe_Index (Indx : Node_Id) return Boolean is
5754 if Is_Entity_Name (Indx) then
5757 elsif Nkind (Indx) = N_Integer_Literal then
5760 elsif Nkind (Indx) = N_Function_Call
5761 and then Is_Entity_Name (Name (Indx))
5762 and then Has_Pragma_Pure_Function (Entity (Name (Indx)))
5766 elsif Nkind (Indx) = N_Type_Conversion
5767 and then Is_Safe_Index (Expression (Indx))
5776 -- Start of processing for Safe_Left_Hand_Side
5779 if Is_Entity_Name (N) then
5782 elsif Nkind_In (N, N_Explicit_Dereference, N_Selected_Component)
5783 and then Safe_Left_Hand_Side (Prefix (N))
5787 elsif Nkind (N) = N_Indexed_Component
5788 and then Safe_Left_Hand_Side (Prefix (N))
5789 and then Is_Safe_Index (First (Expressions (N)))
5793 elsif Nkind (N) = N_Unchecked_Type_Conversion then
5794 return Safe_Left_Hand_Side (Expression (N));
5799 end Safe_Left_Hand_Side;
5804 -- Holds the temporary aggregate value
5807 -- Holds the declaration of Tmp
5809 Aggr_Code : List_Id;
5810 Parent_Node : Node_Id;
5811 Parent_Kind : Node_Kind;
5813 -- Start of processing for Expand_Array_Aggregate
5816 -- Do not touch the special aggregates of attributes used for Asm calls
5818 if Is_RTE (Ctyp, RE_Asm_Input_Operand)
5819 or else Is_RTE (Ctyp, RE_Asm_Output_Operand)
5823 -- Do not expand an aggregate for an array type which contains tasks if
5824 -- the aggregate is associated with an unexpanded return statement of a
5825 -- build-in-place function. The aggregate is expanded when the related
5826 -- return statement (rewritten into an extended return) is processed.
5827 -- This delay ensures that any temporaries and initialization code
5828 -- generated for the aggregate appear in the proper return block and
5829 -- use the correct _chain and _master.
5831 elsif Has_Task (Base_Type (Etype (N)))
5832 and then Nkind (Parent (N)) = N_Simple_Return_Statement
5833 and then Is_Build_In_Place_Function
5834 (Return_Applies_To (Return_Statement_Entity (Parent (N))))
5838 -- Do not attempt expansion if error already detected. We may reach this
5839 -- point in spite of previous errors when compiling with -gnatq, to
5840 -- force all possible errors (this is the usual ACATS mode).
5842 elsif Error_Posted (N) then
5846 -- If the semantic analyzer has determined that aggregate N will raise
5847 -- Constraint_Error at run time, then the aggregate node has been
5848 -- replaced with an N_Raise_Constraint_Error node and we should
5851 pragma Assert (not Raises_Constraint_Error (N));
5855 -- Check that the index range defined by aggregate bounds is
5856 -- compatible with corresponding index subtype.
5858 Index_Compatibility_Check : declare
5859 Aggr_Index_Range : Node_Id := First_Index (Typ);
5860 -- The current aggregate index range
5862 Index_Constraint : Node_Id := First_Index (Etype (Typ));
5863 -- The corresponding index constraint against which we have to
5864 -- check the above aggregate index range.
5867 Compute_Others_Present (N, 1);
5869 for J in 1 .. Aggr_Dimension loop
5870 -- There is no need to emit a check if an others choice is present
5871 -- for this array aggregate dimension since in this case one of
5872 -- N's subaggregates has taken its bounds from the context and
5873 -- these bounds must have been checked already. In addition all
5874 -- subaggregates corresponding to the same dimension must all have
5875 -- the same bounds (checked in (c) below).
5877 if not Range_Checks_Suppressed (Etype (Index_Constraint))
5878 and then not Others_Present (J)
5880 -- We don't use Checks.Apply_Range_Check here because it emits
5881 -- a spurious check. Namely it checks that the range defined by
5882 -- the aggregate bounds is nonempty. But we know this already
5885 Check_Bounds (Aggr_Index_Range, Index_Constraint);
5888 -- Save the low and high bounds of the aggregate index as well as
5889 -- the index type for later use in checks (b) and (c) below.
5891 Aggr_Low (J) := Low_Bound (Aggr_Index_Range);
5892 Aggr_High (J) := High_Bound (Aggr_Index_Range);
5894 Aggr_Index_Typ (J) := Etype (Index_Constraint);
5896 Next_Index (Aggr_Index_Range);
5897 Next_Index (Index_Constraint);
5899 end Index_Compatibility_Check;
5903 -- If an others choice is present check that no aggregate index is
5904 -- outside the bounds of the index constraint.
5906 Others_Check (N, 1);
5910 -- For multidimensional arrays make sure that all subaggregates
5911 -- corresponding to the same dimension have the same bounds.
5913 if Aggr_Dimension > 1 then
5914 Check_Same_Aggr_Bounds (N, 1);
5919 -- If we have a default component value, or simple initialization is
5920 -- required for the component type, then we replace <> in component
5921 -- associations by the required default value.
5924 Default_Val : Node_Id;
5928 if (Present (Default_Aspect_Component_Value (Typ))
5929 or else Needs_Simple_Initialization (Ctyp))
5930 and then Present (Component_Associations (N))
5932 Assoc := First (Component_Associations (N));
5933 while Present (Assoc) loop
5934 if Nkind (Assoc) = N_Component_Association
5935 and then Box_Present (Assoc)
5937 Set_Box_Present (Assoc, False);
5939 if Present (Default_Aspect_Component_Value (Typ)) then
5940 Default_Val := Default_Aspect_Component_Value (Typ);
5942 Default_Val := Get_Simple_Init_Val (Ctyp, N);
5945 Set_Expression (Assoc, New_Copy_Tree (Default_Val));
5946 Analyze_And_Resolve (Expression (Assoc), Ctyp);
5956 -- Here we test for is packed array aggregate that we can handle at
5957 -- compile time. If so, return with transformation done. Note that we do
5958 -- this even if the aggregate is nested, because once we have done this
5959 -- processing, there is no more nested aggregate.
5961 if Packed_Array_Aggregate_Handled (N) then
5965 -- At this point we try to convert to positional form
5967 if Ekind (Current_Scope) = E_Package
5968 and then Static_Elaboration_Desired (Current_Scope)
5970 Convert_To_Positional (N, Max_Others_Replicate => 100);
5972 Convert_To_Positional (N);
5975 -- if the result is no longer an aggregate (e.g. it may be a string
5976 -- literal, or a temporary which has the needed value), then we are
5977 -- done, since there is no longer a nested aggregate.
5979 if Nkind (N) /= N_Aggregate then
5982 -- We are also done if the result is an analyzed aggregate, indicating
5983 -- that Convert_To_Positional succeeded and reanalyzed the rewritten
5986 elsif Analyzed (N) and then N /= Original_Node (N) then
5990 -- If all aggregate components are compile-time known and the aggregate
5991 -- has been flattened, nothing left to do. The same occurs if the
5992 -- aggregate is used to initialize the components of a statically
5993 -- allocated dispatch table.
5995 if Compile_Time_Known_Aggregate (N)
5996 or else Is_Static_Dispatch_Table_Aggregate (N)
5998 Set_Expansion_Delayed (N, False);
6002 -- Now see if back end processing is possible
6004 if Backend_Processing_Possible (N) then
6006 -- If the aggregate is static but the constraints are not, build
6007 -- a static subtype for the aggregate, so that Gigi can place it
6008 -- in static memory. Perform an unchecked_conversion to the non-
6009 -- static type imposed by the context.
6012 Itype : constant Entity_Id := Etype (N);
6014 Needs_Type : Boolean := False;
6017 Index := First_Index (Itype);
6018 while Present (Index) loop
6019 if not Is_OK_Static_Subtype (Etype (Index)) then
6028 Build_Constrained_Type (Positional => True);
6029 Rewrite (N, Unchecked_Convert_To (Itype, N));
6039 -- Delay expansion for nested aggregates: it will be taken care of when
6040 -- the parent aggregate is expanded.
6042 Parent_Node := Parent (N);
6043 Parent_Kind := Nkind (Parent_Node);
6045 if Parent_Kind = N_Qualified_Expression then
6046 Parent_Node := Parent (Parent_Node);
6047 Parent_Kind := Nkind (Parent_Node);
6050 if Parent_Kind = N_Aggregate
6051 or else Parent_Kind = N_Extension_Aggregate
6052 or else Parent_Kind = N_Component_Association
6053 or else (Parent_Kind = N_Object_Declaration
6054 and then Needs_Finalization (Typ))
6055 or else (Parent_Kind = N_Assignment_Statement
6056 and then Inside_Init_Proc)
6058 if Static_Array_Aggregate (N)
6059 or else Compile_Time_Known_Aggregate (N)
6061 Set_Expansion_Delayed (N, False);
6064 Set_Expansion_Delayed (N);
6071 -- Look if in place aggregate expansion is possible
6073 -- For object declarations we build the aggregate in place, unless
6074 -- the array is bit-packed or the component is controlled.
6076 -- For assignments we do the assignment in place if all the component
6077 -- associations have compile-time known values. For other cases we
6078 -- create a temporary. The analysis for safety of on-line assignment
6079 -- is delicate, i.e. we don't know how to do it fully yet ???
6081 -- For allocators we assign to the designated object in place if the
6082 -- aggregate meets the same conditions as other in-place assignments.
6083 -- In this case the aggregate may not come from source but was created
6084 -- for default initialization, e.g. with Initialize_Scalars.
6086 if Requires_Transient_Scope (Typ) then
6087 Establish_Transient_Scope
6088 (N, Sec_Stack => Has_Controlled_Component (Typ));
6091 if Has_Default_Init_Comps (N) then
6092 Maybe_In_Place_OK := False;
6094 elsif Is_Bit_Packed_Array (Typ)
6095 or else Has_Controlled_Component (Typ)
6097 Maybe_In_Place_OK := False;
6100 Maybe_In_Place_OK :=
6101 (Nkind (Parent (N)) = N_Assignment_Statement
6102 and then In_Place_Assign_OK)
6105 (Nkind (Parent (Parent (N))) = N_Allocator
6106 and then In_Place_Assign_OK);
6109 -- If this is an array of tasks, it will be expanded into build-in-place
6110 -- assignments. Build an activation chain for the tasks now.
6112 if Has_Task (Etype (N)) then
6113 Build_Activation_Chain_Entity (N);
6116 -- Perform in-place expansion of aggregate in an object declaration.
6117 -- Note: actions generated for the aggregate will be captured in an
6118 -- expression-with-actions statement so that they can be transferred
6119 -- to freeze actions later if there is an address clause for the
6120 -- object. (Note: we don't use a block statement because this would
6121 -- cause generated freeze nodes to be elaborated in the wrong scope).
6123 -- Do not perform in-place expansion for SPARK 05 because aggregates are
6124 -- expected to appear in qualified form. In-place expansion eliminates
6125 -- the qualification and eventually violates this SPARK 05 restiction.
6127 -- Should document the rest of the guards ???
6129 if not Has_Default_Init_Comps (N)
6130 and then Comes_From_Source (Parent_Node)
6131 and then Parent_Kind = N_Object_Declaration
6132 and then Present (Expression (Parent_Node))
6134 Must_Slide (Etype (Defining_Identifier (Parent_Node)), Typ)
6135 and then not Has_Controlled_Component (Typ)
6136 and then not Is_Bit_Packed_Array (Typ)
6137 and then not Restriction_Check_Required (SPARK_05)
6139 In_Place_Assign_OK_For_Declaration := True;
6140 Tmp := Defining_Identifier (Parent_Node);
6141 Set_No_Initialization (Parent_Node);
6142 Set_Expression (Parent_Node, Empty);
6144 -- Set kind and type of the entity, for use in the analysis
6145 -- of the subsequent assignments. If the nominal type is not
6146 -- constrained, build a subtype from the known bounds of the
6147 -- aggregate. If the declaration has a subtype mark, use it,
6148 -- otherwise use the itype of the aggregate.
6150 Set_Ekind (Tmp, E_Variable);
6152 if not Is_Constrained (Typ) then
6153 Build_Constrained_Type (Positional => False);
6155 elsif Is_Entity_Name (Object_Definition (Parent_Node))
6156 and then Is_Constrained (Entity (Object_Definition (Parent_Node)))
6158 Set_Etype (Tmp, Entity (Object_Definition (Parent_Node)));
6161 Set_Size_Known_At_Compile_Time (Typ, False);
6162 Set_Etype (Tmp, Typ);
6165 elsif Maybe_In_Place_OK
6166 and then Nkind (Parent (N)) = N_Qualified_Expression
6167 and then Nkind (Parent (Parent (N))) = N_Allocator
6169 Set_Expansion_Delayed (N);
6172 -- In the remaining cases the aggregate is the RHS of an assignment
6174 elsif Maybe_In_Place_OK
6175 and then Safe_Left_Hand_Side (Name (Parent (N)))
6177 Tmp := Name (Parent (N));
6179 if Etype (Tmp) /= Etype (N) then
6180 Apply_Length_Check (N, Etype (Tmp));
6182 if Nkind (N) = N_Raise_Constraint_Error then
6184 -- Static error, nothing further to expand
6190 -- If a slice assignment has an aggregate with a single others_choice,
6191 -- the assignment can be done in place even if bounds are not static,
6192 -- by converting it into a loop over the discrete range of the slice.
6194 elsif Maybe_In_Place_OK
6195 and then Nkind (Name (Parent (N))) = N_Slice
6196 and then Is_Others_Aggregate (N)
6198 Tmp := Name (Parent (N));
6200 -- Set type of aggregate to be type of lhs in assignment, in order
6201 -- to suppress redundant length checks.
6203 Set_Etype (N, Etype (Tmp));
6207 -- In place aggregate expansion is not possible
6210 Maybe_In_Place_OK := False;
6211 Tmp := Make_Temporary (Loc, 'A', N);
6213 Make_Object_Declaration (Loc,
6214 Defining_Identifier => Tmp,
6215 Object_Definition => New_Occurrence_Of (Typ, Loc));
6216 Set_No_Initialization (Tmp_Decl, True);
6218 -- If we are within a loop, the temporary will be pushed on the
6219 -- stack at each iteration. If the aggregate is the expression for an
6220 -- allocator, it will be immediately copied to the heap and can
6221 -- be reclaimed at once. We create a transient scope around the
6222 -- aggregate for this purpose.
6224 if Ekind (Current_Scope) = E_Loop
6225 and then Nkind (Parent (Parent (N))) = N_Allocator
6227 Establish_Transient_Scope (N, False);
6230 Insert_Action (N, Tmp_Decl);
6233 -- Construct and insert the aggregate code. We can safely suppress index
6234 -- checks because this code is guaranteed not to raise CE on index
6235 -- checks. However we should *not* suppress all checks.
6241 if Nkind (Tmp) = N_Defining_Identifier then
6242 Target := New_Occurrence_Of (Tmp, Loc);
6245 if Has_Default_Init_Comps (N) then
6247 -- Ada 2005 (AI-287): This case has not been analyzed???
6249 raise Program_Error;
6252 -- Name in assignment is explicit dereference
6254 Target := New_Copy (Tmp);
6257 -- If we are to generate an in place assignment for a declaration or
6258 -- an assignment statement, and the assignment can be done directly
6259 -- by the back end, then do not expand further.
6261 -- ??? We can also do that if in place expansion is not possible but
6262 -- then we could go into an infinite recursion.
6264 if (In_Place_Assign_OK_For_Declaration or else Maybe_In_Place_OK)
6265 and then not AAMP_On_Target
6266 and then not CodePeer_Mode
6267 and then not Modify_Tree_For_C
6268 and then not Possible_Bit_Aligned_Component (Target)
6269 and then not Is_Possibly_Unaligned_Slice (Target)
6270 and then Aggr_Assignment_OK_For_Backend (N)
6272 if Maybe_In_Place_OK then
6278 Make_Assignment_Statement (Loc,
6280 Expression => New_Copy (N)));
6284 Build_Array_Aggr_Code (N,
6286 Index => First_Index (Typ),
6288 Scalar_Comp => Is_Scalar_Type (Ctyp));
6291 -- Save the last assignment statement associated with the aggregate
6292 -- when building a controlled object. This reference is utilized by
6293 -- the finalization machinery when marking an object as successfully
6296 if Needs_Finalization (Typ)
6297 and then Is_Entity_Name (Target)
6298 and then Present (Entity (Target))
6299 and then Ekind_In (Entity (Target), E_Constant, E_Variable)
6301 Set_Last_Aggregate_Assignment (Entity (Target), Last (Aggr_Code));
6305 -- If the aggregate is the expression in a declaration, the expanded
6306 -- code must be inserted after it. The defining entity might not come
6307 -- from source if this is part of an inlined body, but the declaration
6310 if Comes_From_Source (Tmp)
6312 (Nkind (Parent (N)) = N_Object_Declaration
6313 and then Comes_From_Source (Parent (N))
6314 and then Tmp = Defining_Entity (Parent (N)))
6317 Node_After : constant Node_Id := Next (Parent_Node);
6320 Insert_Actions_After (Parent_Node, Aggr_Code);
6322 if Parent_Kind = N_Object_Declaration then
6323 Collect_Initialization_Statements
6324 (Obj => Tmp, N => Parent_Node, Node_After => Node_After);
6329 Insert_Actions (N, Aggr_Code);
6332 -- If the aggregate has been assigned in place, remove the original
6335 if Nkind (Parent (N)) = N_Assignment_Statement
6336 and then Maybe_In_Place_OK
6338 Rewrite (Parent (N), Make_Null_Statement (Loc));
6340 elsif Nkind (Parent (N)) /= N_Object_Declaration
6341 or else Tmp /= Defining_Identifier (Parent (N))
6343 Rewrite (N, New_Occurrence_Of (Tmp, Loc));
6344 Analyze_And_Resolve (N, Typ);
6346 end Expand_Array_Aggregate;
6348 ------------------------
6349 -- Expand_N_Aggregate --
6350 ------------------------
6352 procedure Expand_N_Aggregate (N : Node_Id) is
6354 -- Record aggregate case
6356 if Is_Record_Type (Etype (N)) then
6357 Expand_Record_Aggregate (N);
6359 -- Array aggregate case
6362 -- A special case, if we have a string subtype with bounds 1 .. N,
6363 -- where N is known at compile time, and the aggregate is of the
6364 -- form (others => 'x'), with a single choice and no expressions,
6365 -- and N is less than 80 (an arbitrary limit for now), then replace
6366 -- the aggregate by the equivalent string literal (but do not mark
6367 -- it as static since it is not).
6369 -- Note: this entire circuit is redundant with respect to code in
6370 -- Expand_Array_Aggregate that collapses others choices to positional
6371 -- form, but there are two problems with that circuit:
6373 -- a) It is limited to very small cases due to ill-understood
6374 -- interactions with bootstrapping. That limit is removed by
6375 -- use of the No_Implicit_Loops restriction.
6377 -- b) It incorrectly ends up with the resulting expressions being
6378 -- considered static when they are not. For example, the
6379 -- following test should fail:
6381 -- pragma Restrictions (No_Implicit_Loops);
6382 -- package NonSOthers4 is
6383 -- B : constant String (1 .. 6) := (others => 'A');
6384 -- DH : constant String (1 .. 8) := B & "BB";
6386 -- pragma Export (C, X, Link_Name => DH);
6389 -- But it succeeds (DH looks static to pragma Export)
6391 -- To be sorted out ???
6393 if Present (Component_Associations (N)) then
6395 CA : constant Node_Id := First (Component_Associations (N));
6396 MX : constant := 80;
6399 if Nkind (First (Choice_List (CA))) = N_Others_Choice
6400 and then Nkind (Expression (CA)) = N_Character_Literal
6401 and then No (Expressions (N))
6404 T : constant Entity_Id := Etype (N);
6405 X : constant Node_Id := First_Index (T);
6406 EC : constant Node_Id := Expression (CA);
6407 CV : constant Uint := Char_Literal_Value (EC);
6408 CC : constant Int := UI_To_Int (CV);
6411 if Nkind (X) = N_Range
6412 and then Compile_Time_Known_Value (Low_Bound (X))
6413 and then Expr_Value (Low_Bound (X)) = 1
6414 and then Compile_Time_Known_Value (High_Bound (X))
6417 Hi : constant Uint := Expr_Value (High_Bound (X));
6423 for J in 1 .. UI_To_Int (Hi) loop
6424 Store_String_Char (Char_Code (CC));
6428 Make_String_Literal (Sloc (N),
6429 Strval => End_String));
6431 if CC >= Int (2 ** 16) then
6432 Set_Has_Wide_Wide_Character (N);
6433 elsif CC >= Int (2 ** 8) then
6434 Set_Has_Wide_Character (N);
6437 Analyze_And_Resolve (N, T);
6438 Set_Is_Static_Expression (N, False);
6448 -- Not that special case, so normal expansion of array aggregate
6450 Expand_Array_Aggregate (N);
6454 when RE_Not_Available =>
6456 end Expand_N_Aggregate;
6458 ------------------------------
6459 -- Expand_N_Delta_Aggregate --
6460 ------------------------------
6462 procedure Expand_N_Delta_Aggregate (N : Node_Id) is
6463 Loc : constant Source_Ptr := Sloc (N);
6464 Typ : constant Entity_Id := Etype (N);
6469 Make_Object_Declaration (Loc,
6470 Defining_Identifier => Make_Temporary (Loc, 'T'),
6471 Object_Definition => New_Occurrence_Of (Typ, Loc),
6472 Expression => New_Copy_Tree (Expression (N)));
6474 if Is_Array_Type (Etype (N)) then
6475 Expand_Delta_Array_Aggregate (N, New_List (Decl));
6477 Expand_Delta_Record_Aggregate (N, New_List (Decl));
6479 end Expand_N_Delta_Aggregate;
6481 ----------------------------------
6482 -- Expand_Delta_Array_Aggregate --
6483 ----------------------------------
6485 procedure Expand_Delta_Array_Aggregate (N : Node_Id; Deltas : List_Id) is
6486 Loc : constant Source_Ptr := Sloc (N);
6487 Temp : constant Entity_Id := Defining_Identifier (First (Deltas));
6490 function Generate_Loop (C : Node_Id) return Node_Id;
6491 -- Generate a loop containing individual component assignments for
6492 -- choices that are ranges, subtype indications, subtype names, and
6493 -- iterated component associations.
6499 function Generate_Loop (C : Node_Id) return Node_Id is
6500 Sl : constant Source_Ptr := Sloc (C);
6504 if Nkind (Parent (C)) = N_Iterated_Component_Association then
6506 Make_Defining_Identifier (Loc,
6507 Chars => (Chars (Defining_Identifier (Parent (C)))));
6509 Ix := Make_Temporary (Sl, 'I');
6513 Make_Loop_Statement (Loc,
6515 Make_Iteration_Scheme (Sl,
6516 Loop_Parameter_Specification =>
6517 Make_Loop_Parameter_Specification (Sl,
6518 Defining_Identifier => Ix,
6519 Discrete_Subtype_Definition => New_Copy_Tree (C))),
6521 Statements => New_List (
6522 Make_Assignment_Statement (Sl,
6524 Make_Indexed_Component (Sl,
6525 Prefix => New_Occurrence_Of (Temp, Sl),
6526 Expressions => New_List (New_Occurrence_Of (Ix, Sl))),
6527 Expression => New_Copy_Tree (Expression (Assoc)))),
6528 End_Label => Empty);
6535 -- Start of processing for Expand_Delta_Array_Aggregate
6538 Assoc := First (Component_Associations (N));
6539 while Present (Assoc) loop
6540 Choice := First (Choice_List (Assoc));
6541 if Nkind (Assoc) = N_Iterated_Component_Association then
6542 while Present (Choice) loop
6543 Append_To (Deltas, Generate_Loop (Choice));
6548 while Present (Choice) loop
6550 -- Choice can be given by a range, a subtype indication, a
6551 -- subtype name, a scalar value, or an entity.
6553 if Nkind (Choice) = N_Range
6554 or else (Is_Entity_Name (Choice)
6555 and then Is_Type (Entity (Choice)))
6557 Append_To (Deltas, Generate_Loop (Choice));
6559 elsif Nkind (Choice) = N_Subtype_Indication then
6561 Generate_Loop (Range_Expression (Constraint (Choice))));
6565 Make_Assignment_Statement (Sloc (Choice),
6567 Make_Indexed_Component (Sloc (Choice),
6568 Prefix => New_Occurrence_Of (Temp, Loc),
6569 Expressions => New_List (New_Copy_Tree (Choice))),
6570 Expression => New_Copy_Tree (Expression (Assoc))));
6580 Insert_Actions (N, Deltas);
6581 Rewrite (N, New_Occurrence_Of (Temp, Loc));
6582 end Expand_Delta_Array_Aggregate;
6584 -----------------------------------
6585 -- Expand_Delta_Record_Aggregate --
6586 -----------------------------------
6588 procedure Expand_Delta_Record_Aggregate (N : Node_Id; Deltas : List_Id) is
6589 Loc : constant Source_Ptr := Sloc (N);
6590 Temp : constant Entity_Id := Defining_Identifier (First (Deltas));
6595 Assoc := First (Component_Associations (N));
6597 while Present (Assoc) loop
6598 Choice := First (Choice_List (Assoc));
6599 while Present (Choice) loop
6601 Make_Assignment_Statement (Sloc (Choice),
6603 Make_Selected_Component (Sloc (Choice),
6604 Prefix => New_Occurrence_Of (Temp, Loc),
6605 Selector_Name => Make_Identifier (Loc, Chars (Choice))),
6606 Expression => New_Copy_Tree (Expression (Assoc))));
6613 Insert_Actions (N, Deltas);
6614 Rewrite (N, New_Occurrence_Of (Temp, Loc));
6615 end Expand_Delta_Record_Aggregate;
6617 ----------------------------------
6618 -- Expand_N_Extension_Aggregate --
6619 ----------------------------------
6621 -- If the ancestor part is an expression, add a component association for
6622 -- the parent field. If the type of the ancestor part is not the direct
6623 -- parent of the expected type, build recursively the needed ancestors.
6624 -- If the ancestor part is a subtype_mark, replace aggregate with a decla-
6625 -- ration for a temporary of the expected type, followed by individual
6626 -- assignments to the given components.
6628 procedure Expand_N_Extension_Aggregate (N : Node_Id) is
6629 Loc : constant Source_Ptr := Sloc (N);
6630 A : constant Node_Id := Ancestor_Part (N);
6631 Typ : constant Entity_Id := Etype (N);
6634 -- If the ancestor is a subtype mark, an init proc must be called
6635 -- on the resulting object which thus has to be materialized in
6638 if Is_Entity_Name (A) and then Is_Type (Entity (A)) then
6639 Convert_To_Assignments (N, Typ);
6641 -- The extension aggregate is transformed into a record aggregate
6642 -- of the following form (c1 and c2 are inherited components)
6644 -- (Exp with c3 => a, c4 => b)
6645 -- ==> (c1 => Exp.c1, c2 => Exp.c2, c3 => a, c4 => b)
6650 if Tagged_Type_Expansion then
6651 Expand_Record_Aggregate (N,
6654 (Node (First_Elmt (Access_Disp_Table (Typ))), Loc),
6657 -- No tag is needed in the case of a VM
6660 Expand_Record_Aggregate (N, Parent_Expr => A);
6665 when RE_Not_Available =>
6667 end Expand_N_Extension_Aggregate;
6669 -----------------------------
6670 -- Expand_Record_Aggregate --
6671 -----------------------------
6673 procedure Expand_Record_Aggregate
6675 Orig_Tag : Node_Id := Empty;
6676 Parent_Expr : Node_Id := Empty)
6678 Loc : constant Source_Ptr := Sloc (N);
6679 Comps : constant List_Id := Component_Associations (N);
6680 Typ : constant Entity_Id := Etype (N);
6681 Base_Typ : constant Entity_Id := Base_Type (Typ);
6683 Static_Components : Boolean := True;
6684 -- Flag to indicate whether all components are compile-time known,
6685 -- and the aggregate can be constructed statically and handled by
6688 procedure Build_Back_End_Aggregate;
6689 -- Build a proper aggregate to be handled by the back-end
6691 function Compile_Time_Known_Composite_Value (N : Node_Id) return Boolean;
6692 -- Returns true if N is an expression of composite type which can be
6693 -- fully evaluated at compile time without raising constraint error.
6694 -- Such expressions can be passed as is to Gigi without any expansion.
6696 -- This returns true for N_Aggregate with Compile_Time_Known_Aggregate
6697 -- set and constants whose expression is such an aggregate, recursively.
6699 function Component_Not_OK_For_Backend return Boolean;
6700 -- Check for presence of a component which makes it impossible for the
6701 -- backend to process the aggregate, thus requiring the use of a series
6702 -- of assignment statements. Cases checked for are a nested aggregate
6703 -- needing Late_Expansion, the presence of a tagged component which may
6704 -- need tag adjustment, and a bit unaligned component reference.
6706 -- We also force expansion into assignments if a component is of a
6707 -- mutable type (including a private type with discriminants) because
6708 -- in that case the size of the component to be copied may be smaller
6709 -- than the side of the target, and there is no simple way for gigi
6710 -- to compute the size of the object to be copied.
6712 -- NOTE: This is part of the ongoing work to define precisely the
6713 -- interface between front-end and back-end handling of aggregates.
6714 -- In general it is desirable to pass aggregates as they are to gigi,
6715 -- in order to minimize elaboration code. This is one case where the
6716 -- semantics of Ada complicate the analysis and lead to anomalies in
6717 -- the gcc back-end if the aggregate is not expanded into assignments.
6719 function Has_Per_Object_Constraint (L : List_Id) return Boolean;
6720 -- Return True if any element of L has Has_Per_Object_Constraint set.
6721 -- L should be the Choices component of an N_Component_Association.
6723 function Has_Visible_Private_Ancestor (Id : E) return Boolean;
6724 -- If any ancestor of the current type is private, the aggregate
6725 -- cannot be built in place. We cannot rely on Has_Private_Ancestor,
6726 -- because it will not be set when type and its parent are in the
6727 -- same scope, and the parent component needs expansion.
6729 function Top_Level_Aggregate (N : Node_Id) return Node_Id;
6730 -- For nested aggregates return the ultimate enclosing aggregate; for
6731 -- non-nested aggregates return N.
6733 ------------------------------
6734 -- Build_Back_End_Aggregate --
6735 ------------------------------
6737 procedure Build_Back_End_Aggregate is
6740 Tag_Value : Node_Id;
6743 if Nkind (N) = N_Aggregate then
6745 -- If the aggregate is static and can be handled by the back-end,
6746 -- nothing left to do.
6748 if Static_Components then
6749 Set_Compile_Time_Known_Aggregate (N);
6750 Set_Expansion_Delayed (N, False);
6754 -- If no discriminants, nothing special to do
6756 if not Has_Discriminants (Typ) then
6759 -- Case of discriminants present
6761 elsif Is_Derived_Type (Typ) then
6763 -- For untagged types, non-stored discriminants are replaced with
6764 -- stored discriminants, which are the ones that gigi uses to
6765 -- describe the type and its components.
6767 Generate_Aggregate_For_Derived_Type : declare
6768 procedure Prepend_Stored_Values (T : Entity_Id);
6769 -- Scan the list of stored discriminants of the type, and add
6770 -- their values to the aggregate being built.
6772 ---------------------------
6773 -- Prepend_Stored_Values --
6774 ---------------------------
6776 procedure Prepend_Stored_Values (T : Entity_Id) is
6778 First_Comp : Node_Id := Empty;
6781 Discr := First_Stored_Discriminant (T);
6782 while Present (Discr) loop
6784 Make_Component_Association (Loc,
6785 Choices => New_List (
6786 New_Occurrence_Of (Discr, Loc)),
6789 (Get_Discriminant_Value
6792 Discriminant_Constraint (Typ))));
6794 if No (First_Comp) then
6795 Prepend_To (Component_Associations (N), New_Comp);
6797 Insert_After (First_Comp, New_Comp);
6800 First_Comp := New_Comp;
6801 Next_Stored_Discriminant (Discr);
6803 end Prepend_Stored_Values;
6807 Constraints : constant List_Id := New_List;
6811 Num_Disc : Nat := 0;
6812 Num_Gird : Nat := 0;
6814 -- Start of processing for Generate_Aggregate_For_Derived_Type
6817 -- Remove the associations for the discriminant of derived type
6820 First_Comp : Node_Id;
6823 First_Comp := First (Component_Associations (N));
6824 while Present (First_Comp) loop
6828 if Ekind (Entity (First (Choices (Comp)))) =
6832 Num_Disc := Num_Disc + 1;
6837 -- Insert stored discriminant associations in the correct
6838 -- order. If there are more stored discriminants than new
6839 -- discriminants, there is at least one new discriminant that
6840 -- constrains more than one of the stored discriminants. In
6841 -- this case we need to construct a proper subtype of the
6842 -- parent type, in order to supply values to all the
6843 -- components. Otherwise there is one-one correspondence
6844 -- between the constraints and the stored discriminants.
6846 Discr := First_Stored_Discriminant (Base_Type (Typ));
6847 while Present (Discr) loop
6848 Num_Gird := Num_Gird + 1;
6849 Next_Stored_Discriminant (Discr);
6852 -- Case of more stored discriminants than new discriminants
6854 if Num_Gird > Num_Disc then
6856 -- Create a proper subtype of the parent type, which is the
6857 -- proper implementation type for the aggregate, and convert
6858 -- it to the intended target type.
6860 Discr := First_Stored_Discriminant (Base_Type (Typ));
6861 while Present (Discr) loop
6864 (Get_Discriminant_Value
6867 Discriminant_Constraint (Typ)));
6869 Append (New_Comp, Constraints);
6870 Next_Stored_Discriminant (Discr);
6874 Make_Subtype_Declaration (Loc,
6875 Defining_Identifier => Make_Temporary (Loc, 'T'),
6876 Subtype_Indication =>
6877 Make_Subtype_Indication (Loc,
6879 New_Occurrence_Of (Etype (Base_Type (Typ)), Loc),
6881 Make_Index_Or_Discriminant_Constraint
6882 (Loc, Constraints)));
6884 Insert_Action (N, Decl);
6885 Prepend_Stored_Values (Base_Type (Typ));
6887 Set_Etype (N, Defining_Identifier (Decl));
6890 Rewrite (N, Unchecked_Convert_To (Typ, N));
6893 -- Case where we do not have fewer new discriminants than
6894 -- stored discriminants, so in this case we can simply use the
6895 -- stored discriminants of the subtype.
6898 Prepend_Stored_Values (Typ);
6900 end Generate_Aggregate_For_Derived_Type;
6903 if Is_Tagged_Type (Typ) then
6905 -- In the tagged case, _parent and _tag component must be created
6907 -- Reset Null_Present unconditionally. Tagged records always have
6908 -- at least one field (the tag or the parent).
6910 Set_Null_Record_Present (N, False);
6912 -- When the current aggregate comes from the expansion of an
6913 -- extension aggregate, the parent expr is replaced by an
6914 -- aggregate formed by selected components of this expr.
6916 if Present (Parent_Expr) and then Is_Empty_List (Comps) then
6917 Comp := First_Component_Or_Discriminant (Typ);
6918 while Present (Comp) loop
6920 -- Skip all expander-generated components
6922 if not Comes_From_Source (Original_Record_Component (Comp))
6928 Make_Selected_Component (Loc,
6930 Unchecked_Convert_To (Typ,
6931 Duplicate_Subexpr (Parent_Expr, True)),
6932 Selector_Name => New_Occurrence_Of (Comp, Loc));
6935 Make_Component_Association (Loc,
6936 Choices => New_List (
6937 New_Occurrence_Of (Comp, Loc)),
6938 Expression => New_Comp));
6940 Analyze_And_Resolve (New_Comp, Etype (Comp));
6943 Next_Component_Or_Discriminant (Comp);
6947 -- Compute the value for the Tag now, if the type is a root it
6948 -- will be included in the aggregate right away, otherwise it will
6949 -- be propagated to the parent aggregate.
6951 if Present (Orig_Tag) then
6952 Tag_Value := Orig_Tag;
6954 elsif not Tagged_Type_Expansion then
6960 (Node (First_Elmt (Access_Disp_Table (Typ))), Loc);
6963 -- For a derived type, an aggregate for the parent is formed with
6964 -- all the inherited components.
6966 if Is_Derived_Type (Typ) then
6968 First_Comp : Node_Id;
6969 Parent_Comps : List_Id;
6970 Parent_Aggr : Node_Id;
6971 Parent_Name : Node_Id;
6974 -- Remove the inherited component association from the
6975 -- aggregate and store them in the parent aggregate
6977 First_Comp := First (Component_Associations (N));
6978 Parent_Comps := New_List;
6979 while Present (First_Comp)
6981 Scope (Original_Record_Component
6982 (Entity (First (Choices (First_Comp))))) /=
6988 Append (Comp, Parent_Comps);
6992 Make_Aggregate (Loc,
6993 Component_Associations => Parent_Comps);
6994 Set_Etype (Parent_Aggr, Etype (Base_Type (Typ)));
6996 -- Find the _parent component
6998 Comp := First_Component (Typ);
6999 while Chars (Comp) /= Name_uParent loop
7000 Comp := Next_Component (Comp);
7003 Parent_Name := New_Occurrence_Of (Comp, Loc);
7005 -- Insert the parent aggregate
7007 Prepend_To (Component_Associations (N),
7008 Make_Component_Association (Loc,
7009 Choices => New_List (Parent_Name),
7010 Expression => Parent_Aggr));
7012 -- Expand recursively the parent propagating the right Tag
7014 Expand_Record_Aggregate
7015 (Parent_Aggr, Tag_Value, Parent_Expr);
7017 -- The ancestor part may be a nested aggregate that has
7018 -- delayed expansion: recheck now.
7020 if Component_Not_OK_For_Backend then
7021 Convert_To_Assignments (N, Typ);
7025 -- For a root type, the tag component is added (unless compiling
7026 -- for the VMs, where tags are implicit).
7028 elsif Tagged_Type_Expansion then
7030 Tag_Name : constant Node_Id :=
7032 (First_Tag_Component (Typ), Loc);
7033 Typ_Tag : constant Entity_Id := RTE (RE_Tag);
7034 Conv_Node : constant Node_Id :=
7035 Unchecked_Convert_To (Typ_Tag, Tag_Value);
7038 Set_Etype (Conv_Node, Typ_Tag);
7039 Prepend_To (Component_Associations (N),
7040 Make_Component_Association (Loc,
7041 Choices => New_List (Tag_Name),
7042 Expression => Conv_Node));
7046 end Build_Back_End_Aggregate;
7048 ----------------------------------------
7049 -- Compile_Time_Known_Composite_Value --
7050 ----------------------------------------
7052 function Compile_Time_Known_Composite_Value
7053 (N : Node_Id) return Boolean
7056 -- If we have an entity name, then see if it is the name of a
7057 -- constant and if so, test the corresponding constant value.
7059 if Is_Entity_Name (N) then
7061 E : constant Entity_Id := Entity (N);
7064 if Ekind (E) /= E_Constant then
7067 V := Constant_Value (E);
7069 and then Compile_Time_Known_Composite_Value (V);
7073 -- We have a value, see if it is compile time known
7076 if Nkind (N) = N_Aggregate then
7077 return Compile_Time_Known_Aggregate (N);
7080 -- All other types of values are not known at compile time
7085 end Compile_Time_Known_Composite_Value;
7087 ----------------------------------
7088 -- Component_Not_OK_For_Backend --
7089 ----------------------------------
7091 function Component_Not_OK_For_Backend return Boolean is
7101 while Present (C) loop
7103 -- If the component has box initialization, expansion is needed
7104 -- and component is not ready for backend.
7106 if Box_Present (C) then
7110 if Nkind (Expression (C)) = N_Qualified_Expression then
7111 Expr_Q := Expression (Expression (C));
7113 Expr_Q := Expression (C);
7116 -- Return true if the aggregate has any associations for tagged
7117 -- components that may require tag adjustment.
7119 -- These are cases where the source expression may have a tag that
7120 -- could differ from the component tag (e.g., can occur for type
7121 -- conversions and formal parameters). (Tag adjustment not needed
7122 -- if Tagged_Type_Expansion because object tags are implicit in
7125 if Is_Tagged_Type (Etype (Expr_Q))
7126 and then (Nkind (Expr_Q) = N_Type_Conversion
7127 or else (Is_Entity_Name (Expr_Q)
7129 Ekind (Entity (Expr_Q)) in Formal_Kind))
7130 and then Tagged_Type_Expansion
7132 Static_Components := False;
7135 elsif Is_Delayed_Aggregate (Expr_Q) then
7136 Static_Components := False;
7139 elsif Possible_Bit_Aligned_Component (Expr_Q) then
7140 Static_Components := False;
7143 elsif Modify_Tree_For_C
7144 and then Nkind (C) = N_Component_Association
7145 and then Has_Per_Object_Constraint (Choices (C))
7147 Static_Components := False;
7150 elsif Modify_Tree_For_C
7151 and then Nkind (Expr_Q) = N_Identifier
7152 and then Is_Array_Type (Etype (Expr_Q))
7154 Static_Components := False;
7157 elsif Modify_Tree_For_C
7158 and then Nkind (Expr_Q) = N_Type_Conversion
7159 and then Is_Array_Type (Etype (Expr_Q))
7161 Static_Components := False;
7165 if Is_Elementary_Type (Etype (Expr_Q)) then
7166 if not Compile_Time_Known_Value (Expr_Q) then
7167 Static_Components := False;
7170 elsif not Compile_Time_Known_Composite_Value (Expr_Q) then
7171 Static_Components := False;
7173 if Is_Private_Type (Etype (Expr_Q))
7174 and then Has_Discriminants (Etype (Expr_Q))
7184 end Component_Not_OK_For_Backend;
7186 -------------------------------
7187 -- Has_Per_Object_Constraint --
7188 -------------------------------
7190 function Has_Per_Object_Constraint (L : List_Id) return Boolean is
7191 N : Node_Id := First (L);
7193 while Present (N) loop
7194 if Is_Entity_Name (N)
7195 and then Present (Entity (N))
7196 and then Has_Per_Object_Constraint (Entity (N))
7205 end Has_Per_Object_Constraint;
7207 -----------------------------------
7208 -- Has_Visible_Private_Ancestor --
7209 -----------------------------------
7211 function Has_Visible_Private_Ancestor (Id : E) return Boolean is
7212 R : constant Entity_Id := Root_Type (Id);
7213 T1 : Entity_Id := Id;
7217 if Is_Private_Type (T1) then
7227 end Has_Visible_Private_Ancestor;
7229 -------------------------
7230 -- Top_Level_Aggregate --
7231 -------------------------
7233 function Top_Level_Aggregate (N : Node_Id) return Node_Id is
7238 while Present (Parent (Aggr))
7239 and then Nkind_In (Parent (Aggr), N_Aggregate,
7240 N_Component_Association)
7242 Aggr := Parent (Aggr);
7246 end Top_Level_Aggregate;
7250 Top_Level_Aggr : constant Node_Id := Top_Level_Aggregate (N);
7252 -- Start of processing for Expand_Record_Aggregate
7255 -- If the aggregate is to be assigned to an atomic/VFA variable, we have
7256 -- to prevent a piecemeal assignment even if the aggregate is to be
7257 -- expanded. We create a temporary for the aggregate, and assign the
7258 -- temporary instead, so that the back end can generate an atomic move
7261 if Is_Atomic_VFA_Aggregate (N) then
7264 -- No special management required for aggregates used to initialize
7265 -- statically allocated dispatch tables
7267 elsif Is_Static_Dispatch_Table_Aggregate (N) then
7271 -- Ada 2005 (AI-318-2): We need to convert to assignments if components
7272 -- are build-in-place function calls. The assignments will each turn
7273 -- into a build-in-place function call. If components are all static,
7274 -- we can pass the aggregate to the backend regardless of limitedness.
7276 -- Extension aggregates, aggregates in extended return statements, and
7277 -- aggregates for C++ imported types must be expanded.
7279 if Ada_Version >= Ada_2005 and then Is_Limited_View (Typ) then
7280 if not Nkind_In (Parent (N), N_Component_Association,
7281 N_Object_Declaration)
7283 Convert_To_Assignments (N, Typ);
7285 elsif Nkind (N) = N_Extension_Aggregate
7286 or else Convention (Typ) = Convention_CPP
7288 Convert_To_Assignments (N, Typ);
7290 elsif not Size_Known_At_Compile_Time (Typ)
7291 or else Component_Not_OK_For_Backend
7292 or else not Static_Components
7294 Convert_To_Assignments (N, Typ);
7296 -- In all other cases, build a proper aggregate to be handled by
7300 Build_Back_End_Aggregate;
7303 -- Gigi doesn't properly handle temporaries of variable size so we
7304 -- generate it in the front-end
7306 elsif not Size_Known_At_Compile_Time (Typ)
7307 and then Tagged_Type_Expansion
7309 Convert_To_Assignments (N, Typ);
7311 -- An aggregate used to initialize a controlled object must be turned
7312 -- into component assignments as the components themselves may require
7313 -- finalization actions such as adjustment.
7315 elsif Needs_Finalization (Typ) then
7316 Convert_To_Assignments (N, Typ);
7318 -- Ada 2005 (AI-287): In case of default initialized components we
7319 -- convert the aggregate into assignments.
7321 elsif Has_Default_Init_Comps (N) then
7322 Convert_To_Assignments (N, Typ);
7326 elsif Component_Not_OK_For_Backend then
7327 Convert_To_Assignments (N, Typ);
7329 -- If an ancestor is private, some components are not inherited and we
7330 -- cannot expand into a record aggregate.
7332 elsif Has_Visible_Private_Ancestor (Typ) then
7333 Convert_To_Assignments (N, Typ);
7335 -- ??? The following was done to compile fxacc00.ads in the ACVCs. Gigi
7336 -- is not able to handle the aggregate for Late_Request.
7338 elsif Is_Tagged_Type (Typ) and then Has_Discriminants (Typ) then
7339 Convert_To_Assignments (N, Typ);
7341 -- If the tagged types covers interface types we need to initialize all
7342 -- hidden components containing pointers to secondary dispatch tables.
7344 elsif Is_Tagged_Type (Typ) and then Has_Interfaces (Typ) then
7345 Convert_To_Assignments (N, Typ);
7347 -- If some components are mutable, the size of the aggregate component
7348 -- may be distinct from the default size of the type component, so
7349 -- we need to expand to insure that the back-end copies the proper
7350 -- size of the data. However, if the aggregate is the initial value of
7351 -- a constant, the target is immutable and might be built statically
7352 -- if components are appropriate.
7354 elsif Has_Mutable_Components (Typ)
7356 (Nkind (Parent (Top_Level_Aggr)) /= N_Object_Declaration
7357 or else not Constant_Present (Parent (Top_Level_Aggr))
7358 or else not Static_Components)
7360 Convert_To_Assignments (N, Typ);
7362 -- If the type involved has bit aligned components, then we are not sure
7363 -- that the back end can handle this case correctly.
7365 elsif Type_May_Have_Bit_Aligned_Components (Typ) then
7366 Convert_To_Assignments (N, Typ);
7368 -- When generating C, only generate an aggregate when declaring objects
7369 -- since C does not support aggregates in e.g. assignment statements.
7371 elsif Modify_Tree_For_C and then not In_Object_Declaration (N) then
7372 Convert_To_Assignments (N, Typ);
7374 -- In all other cases, build a proper aggregate to be handled by gigi
7377 Build_Back_End_Aggregate;
7379 end Expand_Record_Aggregate;
7381 ----------------------------
7382 -- Has_Default_Init_Comps --
7383 ----------------------------
7385 function Has_Default_Init_Comps (N : Node_Id) return Boolean is
7386 Comps : constant List_Id := Component_Associations (N);
7391 pragma Assert (Nkind_In (N, N_Aggregate, N_Extension_Aggregate));
7397 if Has_Self_Reference (N) then
7401 -- Check if any direct component has default initialized components
7404 while Present (C) loop
7405 if Box_Present (C) then
7412 -- Recursive call in case of aggregate expression
7415 while Present (C) loop
7416 Expr := Expression (C);
7419 and then Nkind_In (Expr, N_Aggregate, N_Extension_Aggregate)
7420 and then Has_Default_Init_Comps (Expr)
7429 end Has_Default_Init_Comps;
7431 --------------------------
7432 -- Is_Delayed_Aggregate --
7433 --------------------------
7435 function Is_Delayed_Aggregate (N : Node_Id) return Boolean is
7436 Node : Node_Id := N;
7437 Kind : Node_Kind := Nkind (Node);
7440 if Kind = N_Qualified_Expression then
7441 Node := Expression (Node);
7442 Kind := Nkind (Node);
7445 if not Nkind_In (Kind, N_Aggregate, N_Extension_Aggregate) then
7448 return Expansion_Delayed (Node);
7450 end Is_Delayed_Aggregate;
7452 ---------------------------
7453 -- In_Object_Declaration --
7454 ---------------------------
7456 function In_Object_Declaration (N : Node_Id) return Boolean is
7457 P : Node_Id := Parent (N);
7459 while Present (P) loop
7460 if Nkind (P) = N_Object_Declaration then
7468 end In_Object_Declaration;
7470 ----------------------------------------
7471 -- Is_Static_Dispatch_Table_Aggregate --
7472 ----------------------------------------
7474 function Is_Static_Dispatch_Table_Aggregate (N : Node_Id) return Boolean is
7475 Typ : constant Entity_Id := Base_Type (Etype (N));
7478 return Static_Dispatch_Tables
7479 and then Tagged_Type_Expansion
7480 and then RTU_Loaded (Ada_Tags)
7482 -- Avoid circularity when rebuilding the compiler
7484 and then Cunit_Entity (Get_Source_Unit (N)) /= RTU_Entity (Ada_Tags)
7485 and then (Typ = RTE (RE_Dispatch_Table_Wrapper)
7487 Typ = RTE (RE_Address_Array)
7489 Typ = RTE (RE_Type_Specific_Data)
7491 Typ = RTE (RE_Tag_Table)
7493 (RTE_Available (RE_Interface_Data)
7494 and then Typ = RTE (RE_Interface_Data))
7496 (RTE_Available (RE_Interfaces_Array)
7497 and then Typ = RTE (RE_Interfaces_Array))
7499 (RTE_Available (RE_Interface_Data_Element)
7500 and then Typ = RTE (RE_Interface_Data_Element)));
7501 end Is_Static_Dispatch_Table_Aggregate;
7503 -----------------------------
7504 -- Is_Two_Dim_Packed_Array --
7505 -----------------------------
7507 function Is_Two_Dim_Packed_Array (Typ : Entity_Id) return Boolean is
7508 C : constant Int := UI_To_Int (Component_Size (Typ));
7510 return Number_Dimensions (Typ) = 2
7511 and then Is_Bit_Packed_Array (Typ)
7512 and then (C = 1 or else C = 2 or else C = 4);
7513 end Is_Two_Dim_Packed_Array;
7515 --------------------
7516 -- Late_Expansion --
7517 --------------------
7519 function Late_Expansion
7522 Target : Node_Id) return List_Id
7524 Aggr_Code : List_Id;
7527 if Is_Array_Type (Etype (N)) then
7529 Build_Array_Aggr_Code
7531 Ctype => Component_Type (Etype (N)),
7532 Index => First_Index (Typ),
7534 Scalar_Comp => Is_Scalar_Type (Component_Type (Typ)),
7535 Indexes => No_List);
7537 -- Directly or indirectly (e.g. access protected procedure) a record
7540 Aggr_Code := Build_Record_Aggr_Code (N, Typ, Target);
7543 -- Save the last assignment statement associated with the aggregate
7544 -- when building a controlled object. This reference is utilized by
7545 -- the finalization machinery when marking an object as successfully
7548 if Needs_Finalization (Typ)
7549 and then Is_Entity_Name (Target)
7550 and then Present (Entity (Target))
7551 and then Ekind_In (Entity (Target), E_Constant, E_Variable)
7553 Set_Last_Aggregate_Assignment (Entity (Target), Last (Aggr_Code));
7559 ----------------------------------
7560 -- Make_OK_Assignment_Statement --
7561 ----------------------------------
7563 function Make_OK_Assignment_Statement
7566 Expression : Node_Id) return Node_Id
7569 Set_Assignment_OK (Name);
7570 return Make_Assignment_Statement (Sloc, Name, Expression);
7571 end Make_OK_Assignment_Statement;
7573 -----------------------
7574 -- Number_Of_Choices --
7575 -----------------------
7577 function Number_Of_Choices (N : Node_Id) return Nat is
7581 Nb_Choices : Nat := 0;
7584 if Present (Expressions (N)) then
7588 Assoc := First (Component_Associations (N));
7589 while Present (Assoc) loop
7590 Choice := First (Choice_List (Assoc));
7591 while Present (Choice) loop
7592 if Nkind (Choice) /= N_Others_Choice then
7593 Nb_Choices := Nb_Choices + 1;
7603 end Number_Of_Choices;
7605 ------------------------------------
7606 -- Packed_Array_Aggregate_Handled --
7607 ------------------------------------
7609 -- The current version of this procedure will handle at compile time
7610 -- any array aggregate that meets these conditions:
7612 -- One and two dimensional, bit packed
7613 -- Underlying packed type is modular type
7614 -- Bounds are within 32-bit Int range
7615 -- All bounds and values are static
7617 -- Note: for now, in the 2-D case, we only handle component sizes of
7618 -- 1, 2, 4 (cases where an integral number of elements occupies a byte).
7620 function Packed_Array_Aggregate_Handled (N : Node_Id) return Boolean is
7621 Loc : constant Source_Ptr := Sloc (N);
7622 Typ : constant Entity_Id := Etype (N);
7623 Ctyp : constant Entity_Id := Component_Type (Typ);
7625 Not_Handled : exception;
7626 -- Exception raised if this aggregate cannot be handled
7629 -- Handle one- or two dimensional bit packed array
7631 if not Is_Bit_Packed_Array (Typ)
7632 or else Number_Dimensions (Typ) > 2
7637 -- If two-dimensional, check whether it can be folded, and transformed
7638 -- into a one-dimensional aggregate for the Packed_Array_Impl_Type of
7639 -- the original type.
7641 if Number_Dimensions (Typ) = 2 then
7642 return Two_Dim_Packed_Array_Handled (N);
7645 if not Is_Modular_Integer_Type (Packed_Array_Impl_Type (Typ)) then
7649 if not Is_Scalar_Type (Component_Type (Typ))
7650 and then Has_Non_Standard_Rep (Component_Type (Typ))
7656 Csiz : constant Nat := UI_To_Int (Component_Size (Typ));
7660 -- Bounds of index type
7664 -- Values of bounds if compile time known
7666 function Get_Component_Val (N : Node_Id) return Uint;
7667 -- Given a expression value N of the component type Ctyp, returns a
7668 -- value of Csiz (component size) bits representing this value. If
7669 -- the value is non-static or any other reason exists why the value
7670 -- cannot be returned, then Not_Handled is raised.
7672 -----------------------
7673 -- Get_Component_Val --
7674 -----------------------
7676 function Get_Component_Val (N : Node_Id) return Uint is
7680 -- We have to analyze the expression here before doing any further
7681 -- processing here. The analysis of such expressions is deferred
7682 -- till expansion to prevent some problems of premature analysis.
7684 Analyze_And_Resolve (N, Ctyp);
7686 -- Must have a compile time value. String literals have to be
7687 -- converted into temporaries as well, because they cannot easily
7688 -- be converted into their bit representation.
7690 if not Compile_Time_Known_Value (N)
7691 or else Nkind (N) = N_String_Literal
7696 Val := Expr_Rep_Value (N);
7698 -- Adjust for bias, and strip proper number of bits
7700 if Has_Biased_Representation (Ctyp) then
7701 Val := Val - Expr_Value (Type_Low_Bound (Ctyp));
7704 return Val mod Uint_2 ** Csiz;
7705 end Get_Component_Val;
7707 -- Here we know we have a one dimensional bit packed array
7710 Get_Index_Bounds (First_Index (Typ), Lo, Hi);
7712 -- Cannot do anything if bounds are dynamic
7714 if not Compile_Time_Known_Value (Lo)
7716 not Compile_Time_Known_Value (Hi)
7721 -- Or are silly out of range of int bounds
7723 Lob := Expr_Value (Lo);
7724 Hib := Expr_Value (Hi);
7726 if not UI_Is_In_Int_Range (Lob)
7728 not UI_Is_In_Int_Range (Hib)
7733 -- At this stage we have a suitable aggregate for handling at compile
7734 -- time. The only remaining checks are that the values of expressions
7735 -- in the aggregate are compile-time known (checks are performed by
7736 -- Get_Component_Val), and that any subtypes or ranges are statically
7739 -- If the aggregate is not fully positional at this stage, then
7740 -- convert it to positional form. Either this will fail, in which
7741 -- case we can do nothing, or it will succeed, in which case we have
7742 -- succeeded in handling the aggregate and transforming it into a
7743 -- modular value, or it will stay an aggregate, in which case we
7744 -- have failed to create a packed value for it.
7746 if Present (Component_Associations (N)) then
7747 Convert_To_Positional
7748 (N, Max_Others_Replicate => 64, Handle_Bit_Packed => True);
7749 return Nkind (N) /= N_Aggregate;
7752 -- Otherwise we are all positional, so convert to proper value
7755 Lov : constant Int := UI_To_Int (Lob);
7756 Hiv : constant Int := UI_To_Int (Hib);
7758 Len : constant Nat := Int'Max (0, Hiv - Lov + 1);
7759 -- The length of the array (number of elements)
7761 Aggregate_Val : Uint;
7762 -- Value of aggregate. The value is set in the low order bits of
7763 -- this value. For the little-endian case, the values are stored
7764 -- from low-order to high-order and for the big-endian case the
7765 -- values are stored from high-order to low-order. Note that gigi
7766 -- will take care of the conversions to left justify the value in
7767 -- the big endian case (because of left justified modular type
7768 -- processing), so we do not have to worry about that here.
7771 -- Integer literal for resulting constructed value
7774 -- Shift count from low order for next value
7777 -- Shift increment for loop
7780 -- Next expression from positional parameters of aggregate
7782 Left_Justified : Boolean;
7783 -- Set True if we are filling the high order bits of the target
7784 -- value (i.e. the value is left justified).
7787 -- For little endian, we fill up the low order bits of the target
7788 -- value. For big endian we fill up the high order bits of the
7789 -- target value (which is a left justified modular value).
7791 Left_Justified := Bytes_Big_Endian;
7793 -- Switch justification if using -gnatd8
7795 if Debug_Flag_8 then
7796 Left_Justified := not Left_Justified;
7799 -- Switch justfification if reverse storage order
7801 if Reverse_Storage_Order (Base_Type (Typ)) then
7802 Left_Justified := not Left_Justified;
7805 if Left_Justified then
7806 Shift := Csiz * (Len - 1);
7813 -- Loop to set the values
7816 Aggregate_Val := Uint_0;
7818 Expr := First (Expressions (N));
7819 Aggregate_Val := Get_Component_Val (Expr) * Uint_2 ** Shift;
7821 for J in 2 .. Len loop
7822 Shift := Shift + Incr;
7825 Aggregate_Val + Get_Component_Val (Expr) * Uint_2 ** Shift;
7829 -- Now we can rewrite with the proper value
7831 Lit := Make_Integer_Literal (Loc, Intval => Aggregate_Val);
7832 Set_Print_In_Hex (Lit);
7834 -- Construct the expression using this literal. Note that it is
7835 -- important to qualify the literal with its proper modular type
7836 -- since universal integer does not have the required range and
7837 -- also this is a left justified modular type, which is important
7838 -- in the big-endian case.
7841 Unchecked_Convert_To (Typ,
7842 Make_Qualified_Expression (Loc,
7844 New_Occurrence_Of (Packed_Array_Impl_Type (Typ), Loc),
7845 Expression => Lit)));
7847 Analyze_And_Resolve (N, Typ);
7855 end Packed_Array_Aggregate_Handled;
7857 ----------------------------
7858 -- Has_Mutable_Components --
7859 ----------------------------
7861 function Has_Mutable_Components (Typ : Entity_Id) return Boolean is
7865 Comp := First_Component (Typ);
7866 while Present (Comp) loop
7867 if Is_Record_Type (Etype (Comp))
7868 and then Has_Discriminants (Etype (Comp))
7869 and then not Is_Constrained (Etype (Comp))
7874 Next_Component (Comp);
7878 end Has_Mutable_Components;
7880 ------------------------------
7881 -- Initialize_Discriminants --
7882 ------------------------------
7884 procedure Initialize_Discriminants (N : Node_Id; Typ : Entity_Id) is
7885 Loc : constant Source_Ptr := Sloc (N);
7886 Bas : constant Entity_Id := Base_Type (Typ);
7887 Par : constant Entity_Id := Etype (Bas);
7888 Decl : constant Node_Id := Parent (Par);
7892 if Is_Tagged_Type (Bas)
7893 and then Is_Derived_Type (Bas)
7894 and then Has_Discriminants (Par)
7895 and then Has_Discriminants (Bas)
7896 and then Number_Discriminants (Bas) /= Number_Discriminants (Par)
7897 and then Nkind (Decl) = N_Full_Type_Declaration
7898 and then Nkind (Type_Definition (Decl)) = N_Record_Definition
7900 Present (Variant_Part (Component_List (Type_Definition (Decl))))
7901 and then Nkind (N) /= N_Extension_Aggregate
7904 -- Call init proc to set discriminants.
7905 -- There should eventually be a special procedure for this ???
7907 Ref := New_Occurrence_Of (Defining_Identifier (N), Loc);
7908 Insert_Actions_After (N,
7909 Build_Initialization_Call (Sloc (N), Ref, Typ));
7911 end Initialize_Discriminants;
7918 (Obj_Type : Entity_Id;
7919 Typ : Entity_Id) return Boolean
7921 L1, L2, H1, H2 : Node_Id;
7924 -- No sliding if the type of the object is not established yet, if it is
7925 -- an unconstrained type whose actual subtype comes from the aggregate,
7926 -- or if the two types are identical.
7928 if not Is_Array_Type (Obj_Type) then
7931 elsif not Is_Constrained (Obj_Type) then
7934 elsif Typ = Obj_Type then
7938 -- Sliding can only occur along the first dimension
7940 Get_Index_Bounds (First_Index (Typ), L1, H1);
7941 Get_Index_Bounds (First_Index (Obj_Type), L2, H2);
7943 if not Is_OK_Static_Expression (L1) or else
7944 not Is_OK_Static_Expression (L2) or else
7945 not Is_OK_Static_Expression (H1) or else
7946 not Is_OK_Static_Expression (H2)
7950 return Expr_Value (L1) /= Expr_Value (L2)
7952 Expr_Value (H1) /= Expr_Value (H2);
7957 ---------------------------------
7958 -- Process_Transient_Component --
7959 ---------------------------------
7961 procedure Process_Transient_Component
7963 Comp_Typ : Entity_Id;
7964 Init_Expr : Node_Id;
7965 Fin_Call : out Node_Id;
7966 Hook_Clear : out Node_Id;
7967 Aggr : Node_Id := Empty;
7968 Stmts : List_Id := No_List)
7970 procedure Add_Item (Item : Node_Id);
7971 -- Insert arbitrary node Item into the tree depending on the values of
7978 procedure Add_Item (Item : Node_Id) is
7980 if Present (Aggr) then
7981 Insert_Action (Aggr, Item);
7983 pragma Assert (Present (Stmts));
7984 Append_To (Stmts, Item);
7990 Hook_Assign : Node_Id;
7991 Hook_Decl : Node_Id;
7995 Res_Typ : Entity_Id;
7997 -- Start of processing for Process_Transient_Component
8000 -- Add the access type, which provides a reference to the function
8001 -- result. Generate:
8003 -- type Res_Typ is access all Comp_Typ;
8005 Res_Typ := Make_Temporary (Loc, 'A');
8006 Set_Ekind (Res_Typ, E_General_Access_Type);
8007 Set_Directly_Designated_Type (Res_Typ, Comp_Typ);
8010 (Make_Full_Type_Declaration (Loc,
8011 Defining_Identifier => Res_Typ,
8013 Make_Access_To_Object_Definition (Loc,
8014 All_Present => True,
8015 Subtype_Indication => New_Occurrence_Of (Comp_Typ, Loc))));
8017 -- Add the temporary which captures the result of the function call.
8020 -- Res : constant Res_Typ := Init_Expr'Reference;
8022 -- Note that this temporary is effectively a transient object because
8023 -- its lifetime is bounded by the current array or record component.
8025 Res_Id := Make_Temporary (Loc, 'R');
8026 Set_Ekind (Res_Id, E_Constant);
8027 Set_Etype (Res_Id, Res_Typ);
8029 -- Mark the transient object as successfully processed to avoid double
8032 Set_Is_Finalized_Transient (Res_Id);
8034 -- Signal the general finalization machinery that this transient object
8035 -- should not be considered for finalization actions because its cleanup
8036 -- will be performed by Process_Transient_Component_Completion.
8038 Set_Is_Ignored_Transient (Res_Id);
8041 Make_Object_Declaration (Loc,
8042 Defining_Identifier => Res_Id,
8043 Constant_Present => True,
8044 Object_Definition => New_Occurrence_Of (Res_Typ, Loc),
8046 Make_Reference (Loc, New_Copy_Tree (Init_Expr)));
8048 Add_Item (Res_Decl);
8050 -- Construct all pieces necessary to hook and finalize the transient
8053 Build_Transient_Object_Statements
8054 (Obj_Decl => Res_Decl,
8055 Fin_Call => Fin_Call,
8056 Hook_Assign => Hook_Assign,
8057 Hook_Clear => Hook_Clear,
8058 Hook_Decl => Hook_Decl,
8059 Ptr_Decl => Ptr_Decl);
8061 -- Add the access type which provides a reference to the transient
8062 -- result. Generate:
8064 -- type Ptr_Typ is access all Comp_Typ;
8066 Add_Item (Ptr_Decl);
8068 -- Add the temporary which acts as a hook to the transient result.
8071 -- Hook : Ptr_Typ := null;
8073 Add_Item (Hook_Decl);
8075 -- Attach the transient result to the hook. Generate:
8077 -- Hook := Ptr_Typ (Res);
8079 Add_Item (Hook_Assign);
8081 -- The original initialization expression now references the value of
8082 -- the temporary function result. Generate:
8087 Make_Explicit_Dereference (Loc,
8088 Prefix => New_Occurrence_Of (Res_Id, Loc)));
8089 end Process_Transient_Component;
8091 --------------------------------------------
8092 -- Process_Transient_Component_Completion --
8093 --------------------------------------------
8095 procedure Process_Transient_Component_Completion
8099 Hook_Clear : Node_Id;
8102 Exceptions_OK : constant Boolean :=
8103 not Restriction_Active (No_Exception_Propagation);
8106 pragma Assert (Present (Hook_Clear));
8108 -- Generate the following code if exception propagation is allowed:
8111 -- Abort : constant Boolean := Triggered_By_Abort;
8113 -- Abort : constant Boolean := False; -- no abort
8115 -- E : Exception_Occurrence;
8116 -- Raised : Boolean := False;
8123 -- [Deep_]Finalize (Res.all);
8127 -- if not Raised then
8129 -- Save_Occurrence (E,
8130 -- Get_Curent_Excep.all.all);
8136 -- if Raised and then not Abort then
8137 -- Raise_From_Controlled_Operation (E);
8141 if Exceptions_OK then
8142 Abort_And_Exception : declare
8143 Blk_Decls : constant List_Id := New_List;
8144 Blk_Stmts : constant List_Id := New_List;
8145 Fin_Stmts : constant List_Id := New_List;
8147 Fin_Data : Finalization_Exception_Data;
8150 -- Create the declarations of the two flags and the exception
8153 Build_Object_Declarations (Fin_Data, Blk_Decls, Loc);
8158 if Abort_Allowed then
8159 Append_To (Blk_Stmts,
8160 Build_Runtime_Call (Loc, RE_Abort_Defer));
8163 -- Wrap the hook clear and the finalization call in order to trap
8164 -- a potential exception.
8166 Append_To (Fin_Stmts, Hook_Clear);
8168 if Present (Fin_Call) then
8169 Append_To (Fin_Stmts, Fin_Call);
8172 Append_To (Blk_Stmts,
8173 Make_Block_Statement (Loc,
8174 Handled_Statement_Sequence =>
8175 Make_Handled_Sequence_Of_Statements (Loc,
8176 Statements => Fin_Stmts,
8177 Exception_Handlers => New_List (
8178 Build_Exception_Handler (Fin_Data)))));
8183 if Abort_Allowed then
8184 Append_To (Blk_Stmts,
8185 Build_Runtime_Call (Loc, RE_Abort_Undefer));
8188 -- Reraise the potential exception with a proper "upgrade" to
8189 -- Program_Error if needed.
8191 Append_To (Blk_Stmts, Build_Raise_Statement (Fin_Data));
8193 -- Wrap everything in a block
8196 Make_Block_Statement (Loc,
8197 Declarations => Blk_Decls,
8198 Handled_Statement_Sequence =>
8199 Make_Handled_Sequence_Of_Statements (Loc,
8200 Statements => Blk_Stmts)));
8201 end Abort_And_Exception;
8203 -- Generate the following code if exception propagation is not allowed
8204 -- and aborts are allowed:
8209 -- [Deep_]Finalize (Res.all);
8211 -- Abort_Undefer_Direct;
8214 elsif Abort_Allowed then
8215 Abort_Only : declare
8216 Blk_Stmts : constant List_Id := New_List;
8219 Append_To (Blk_Stmts, Build_Runtime_Call (Loc, RE_Abort_Defer));
8220 Append_To (Blk_Stmts, Hook_Clear);
8222 if Present (Fin_Call) then
8223 Append_To (Blk_Stmts, Fin_Call);
8227 Build_Abort_Undefer_Block (Loc,
8232 -- Otherwise generate:
8235 -- [Deep_]Finalize (Res.all);
8238 Append_To (Stmts, Hook_Clear);
8240 if Present (Fin_Call) then
8241 Append_To (Stmts, Fin_Call);
8244 end Process_Transient_Component_Completion;
8246 ---------------------
8247 -- Sort_Case_Table --
8248 ---------------------
8250 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type) is
8251 L : constant Int := Case_Table'First;
8252 U : constant Int := Case_Table'Last;
8260 T := Case_Table (K + 1);
8264 and then Expr_Value (Case_Table (J - 1).Choice_Lo) >
8265 Expr_Value (T.Choice_Lo)
8267 Case_Table (J) := Case_Table (J - 1);
8271 Case_Table (J) := T;
8274 end Sort_Case_Table;
8276 ----------------------------
8277 -- Static_Array_Aggregate --
8278 ----------------------------
8280 function Static_Array_Aggregate (N : Node_Id) return Boolean is
8281 Bounds : constant Node_Id := Aggregate_Bounds (N);
8283 Typ : constant Entity_Id := Etype (N);
8284 Comp_Type : constant Entity_Id := Component_Type (Typ);
8291 if Is_Tagged_Type (Typ)
8292 or else Is_Controlled (Typ)
8293 or else Is_Packed (Typ)
8299 and then Nkind (Bounds) = N_Range
8300 and then Nkind (Low_Bound (Bounds)) = N_Integer_Literal
8301 and then Nkind (High_Bound (Bounds)) = N_Integer_Literal
8303 Lo := Low_Bound (Bounds);
8304 Hi := High_Bound (Bounds);
8306 if No (Component_Associations (N)) then
8308 -- Verify that all components are static integers
8310 Expr := First (Expressions (N));
8311 while Present (Expr) loop
8312 if Nkind (Expr) /= N_Integer_Literal then
8322 -- We allow only a single named association, either a static
8323 -- range or an others_clause, with a static expression.
8325 Expr := First (Component_Associations (N));
8327 if Present (Expressions (N)) then
8330 elsif Present (Next (Expr)) then
8333 elsif Present (Next (First (Choice_List (Expr)))) then
8337 -- The aggregate is static if all components are literals,
8338 -- or else all its components are static aggregates for the
8339 -- component type. We also limit the size of a static aggregate
8340 -- to prevent runaway static expressions.
8342 if Is_Array_Type (Comp_Type)
8343 or else Is_Record_Type (Comp_Type)
8345 if Nkind (Expression (Expr)) /= N_Aggregate
8347 not Compile_Time_Known_Aggregate (Expression (Expr))
8352 elsif Nkind (Expression (Expr)) /= N_Integer_Literal then
8356 if not Aggr_Size_OK (N, Typ) then
8360 -- Create a positional aggregate with the right number of
8361 -- copies of the expression.
8363 Agg := Make_Aggregate (Sloc (N), New_List, No_List);
8365 for I in UI_To_Int (Intval (Lo)) .. UI_To_Int (Intval (Hi))
8367 Append_To (Expressions (Agg), New_Copy (Expression (Expr)));
8369 -- The copied expression must be analyzed and resolved.
8370 -- Besides setting the type, this ensures that static
8371 -- expressions are appropriately marked as such.
8374 (Last (Expressions (Agg)), Component_Type (Typ));
8377 Set_Aggregate_Bounds (Agg, Bounds);
8378 Set_Etype (Agg, Typ);
8381 Set_Compile_Time_Known_Aggregate (N);
8390 end Static_Array_Aggregate;
8392 ----------------------------------
8393 -- Two_Dim_Packed_Array_Handled --
8394 ----------------------------------
8396 function Two_Dim_Packed_Array_Handled (N : Node_Id) return Boolean is
8397 Loc : constant Source_Ptr := Sloc (N);
8398 Typ : constant Entity_Id := Etype (N);
8399 Ctyp : constant Entity_Id := Component_Type (Typ);
8400 Comp_Size : constant Int := UI_To_Int (Component_Size (Typ));
8401 Packed_Array : constant Entity_Id :=
8402 Packed_Array_Impl_Type (Base_Type (Typ));
8405 -- Expression in original aggregate
8408 -- One-dimensional subaggregate
8412 -- For now, only deal with cases where an integral number of elements
8413 -- fit in a single byte. This includes the most common boolean case.
8415 if not (Comp_Size = 1 or else
8416 Comp_Size = 2 or else
8422 Convert_To_Positional
8423 (N, Max_Others_Replicate => 64, Handle_Bit_Packed => True);
8425 -- Verify that all components are static
8427 if Nkind (N) = N_Aggregate
8428 and then Compile_Time_Known_Aggregate (N)
8432 -- The aggregate may have been reanalyzed and converted already
8434 elsif Nkind (N) /= N_Aggregate then
8437 -- If component associations remain, the aggregate is not static
8439 elsif Present (Component_Associations (N)) then
8443 One_Dim := First (Expressions (N));
8444 while Present (One_Dim) loop
8445 if Present (Component_Associations (One_Dim)) then
8449 One_Comp := First (Expressions (One_Dim));
8450 while Present (One_Comp) loop
8451 if not Is_OK_Static_Expression (One_Comp) then
8462 -- Two-dimensional aggregate is now fully positional so pack one
8463 -- dimension to create a static one-dimensional array, and rewrite
8464 -- as an unchecked conversion to the original type.
8467 Byte_Size : constant Int := UI_To_Int (Component_Size (Packed_Array));
8468 -- The packed array type is a byte array
8471 -- Number of components accumulated in current byte
8474 -- Assembled list of packed values for equivalent aggregate
8477 -- Integer value of component
8480 -- Step size for packing
8483 -- Endian-dependent start position for packing
8486 -- Current insertion position
8489 -- Component of packed array being assembled
8496 -- Account for endianness. See corresponding comment in
8497 -- Packed_Array_Aggregate_Handled concerning the following.
8501 xor Reverse_Storage_Order (Base_Type (Typ))
8503 Init_Shift := Byte_Size - Comp_Size;
8510 -- Iterate over each subaggregate
8512 Shift := Init_Shift;
8513 One_Dim := First (Expressions (N));
8514 while Present (One_Dim) loop
8515 One_Comp := First (Expressions (One_Dim));
8516 while Present (One_Comp) loop
8517 if Packed_Num = Byte_Size / Comp_Size then
8519 -- Byte is complete, add to list of expressions
8521 Append (Make_Integer_Literal (Sloc (One_Dim), Val), Comps);
8523 Shift := Init_Shift;
8527 Comp_Val := Expr_Rep_Value (One_Comp);
8529 -- Adjust for bias, and strip proper number of bits
8531 if Has_Biased_Representation (Ctyp) then
8532 Comp_Val := Comp_Val - Expr_Value (Type_Low_Bound (Ctyp));
8535 Comp_Val := Comp_Val mod Uint_2 ** Comp_Size;
8536 Val := UI_To_Int (Val + Comp_Val * Uint_2 ** Shift);
8537 Shift := Shift + Incr;
8538 One_Comp := Next (One_Comp);
8539 Packed_Num := Packed_Num + 1;
8543 One_Dim := Next (One_Dim);
8546 if Packed_Num > 0 then
8548 -- Add final incomplete byte if present
8550 Append (Make_Integer_Literal (Sloc (One_Dim), Val), Comps);
8554 Unchecked_Convert_To (Typ,
8555 Make_Qualified_Expression (Loc,
8556 Subtype_Mark => New_Occurrence_Of (Packed_Array, Loc),
8557 Expression => Make_Aggregate (Loc, Expressions => Comps))));
8558 Analyze_And_Resolve (N);
8561 end Two_Dim_Packed_Array_Handled;