1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2018, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Atree; use Atree;
27 with Checks; use Checks;
28 with Debug; use Debug;
29 with Einfo; use Einfo;
30 with Elists; use Elists;
31 with Errout; use Errout;
32 with Expander; use Expander;
33 with Exp_Util; use Exp_Util;
34 with Exp_Ch3; use Exp_Ch3;
35 with Exp_Ch6; use Exp_Ch6;
36 with Exp_Ch7; use Exp_Ch7;
37 with Exp_Ch9; use Exp_Ch9;
38 with Exp_Disp; use Exp_Disp;
39 with Exp_Tss; use Exp_Tss;
40 with Freeze; use Freeze;
41 with Itypes; use Itypes;
43 with Namet; use Namet;
44 with Nmake; use Nmake;
45 with Nlists; use Nlists;
47 with Restrict; use Restrict;
48 with Rident; use Rident;
49 with Rtsfind; use Rtsfind;
50 with Ttypes; use Ttypes;
52 with Sem_Aggr; use Sem_Aggr;
53 with Sem_Aux; use Sem_Aux;
54 with Sem_Ch3; use Sem_Ch3;
55 with Sem_Eval; use Sem_Eval;
56 with Sem_Res; use Sem_Res;
57 with Sem_Util; use Sem_Util;
58 with Sinfo; use Sinfo;
59 with Snames; use Snames;
60 with Stand; use Stand;
61 with Stringt; use Stringt;
62 with Tbuild; use Tbuild;
63 with Uintp; use Uintp;
64 with Urealp; use Urealp;
66 package body Exp_Aggr is
68 type Case_Bounds is record
71 Choice_Node : Node_Id;
74 type Case_Table_Type is array (Nat range <>) of Case_Bounds;
75 -- Table type used by Check_Case_Choices procedure
77 procedure Collect_Initialization_Statements
80 Node_After : Node_Id);
81 -- If Obj is not frozen, collect actions inserted after N until, but not
82 -- including, Node_After, for initialization of Obj, and move them to an
83 -- expression with actions, which becomes the Initialization_Statements for
86 procedure Expand_Delta_Array_Aggregate (N : Node_Id; Deltas : List_Id);
87 procedure Expand_Delta_Record_Aggregate (N : Node_Id; Deltas : List_Id);
89 function Has_Default_Init_Comps (N : Node_Id) return Boolean;
90 -- N is an aggregate (record or array). Checks the presence of default
91 -- initialization (<>) in any component (Ada 2005: AI-287).
93 function 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 Is_Build_In_Place_Aggregate_Return (N : Node_Id) return Boolean;
179 -- True if N is an aggregate (possibly qualified or converted) that is
180 -- being returned from a build-in-place function.
182 function Build_Record_Aggr_Code
185 Lhs : Node_Id) return List_Id;
186 -- N is an N_Aggregate or an N_Extension_Aggregate. Typ is the type of the
187 -- aggregate. Target is an expression containing the location on which the
188 -- component by component assignments will take place. Returns the list of
189 -- assignments plus all other adjustments needed for tagged and controlled
192 procedure Convert_To_Assignments (N : Node_Id; Typ : Entity_Id);
193 -- Transform a record aggregate into a sequence of assignments performed
194 -- component by component. N is an N_Aggregate or N_Extension_Aggregate.
195 -- Typ is the type of the record aggregate.
197 procedure Expand_Record_Aggregate
199 Orig_Tag : Node_Id := Empty;
200 Parent_Expr : Node_Id := Empty);
201 -- This is the top level procedure for record aggregate expansion.
202 -- Expansion for record aggregates needs expand aggregates for tagged
203 -- record types. Specifically Expand_Record_Aggregate adds the Tag
204 -- field in front of the Component_Association list that was created
205 -- during resolution by Resolve_Record_Aggregate.
207 -- N is the record aggregate node.
208 -- Orig_Tag is the value of the Tag that has to be provided for this
209 -- specific aggregate. It carries the tag corresponding to the type
210 -- of the outermost aggregate during the recursive expansion
211 -- Parent_Expr is the ancestor part of the original extension
214 function Has_Mutable_Components (Typ : Entity_Id) return Boolean;
215 -- Return true if one of the components is of a discriminated type with
216 -- defaults. An aggregate for a type with mutable components must be
217 -- expanded into individual assignments.
219 procedure Initialize_Discriminants (N : Node_Id; Typ : Entity_Id);
220 -- If the type of the aggregate is a type extension with renamed discrimi-
221 -- nants, we must initialize the hidden discriminants of the parent.
222 -- Otherwise, the target object must not be initialized. The discriminants
223 -- are initialized by calling the initialization procedure for the type.
224 -- This is incorrect if the initialization of other components has any
225 -- side effects. We restrict this call to the case where the parent type
226 -- has a variant part, because this is the only case where the hidden
227 -- discriminants are accessed, namely when calling discriminant checking
228 -- functions of the parent type, and when applying a stream attribute to
229 -- an object of the derived type.
231 -----------------------------------------------------
232 -- Local Subprograms for Array Aggregate Expansion --
233 -----------------------------------------------------
235 function Aggr_Size_OK (N : Node_Id; Typ : Entity_Id) return Boolean;
236 -- Very large static aggregates present problems to the back-end, and are
237 -- transformed into assignments and loops. This function verifies that the
238 -- total number of components of an aggregate is acceptable for rewriting
239 -- into a purely positional static form. Aggr_Size_OK must be called before
242 -- This function also detects and warns about one-component aggregates that
243 -- appear in a nonstatic context. Even if the component value is static,
244 -- such an aggregate must be expanded into an assignment.
246 function Backend_Processing_Possible (N : Node_Id) return Boolean;
247 -- This function checks if array aggregate N can be processed directly
248 -- by the backend. If this is the case, True is returned.
250 function Build_Array_Aggr_Code
255 Scalar_Comp : Boolean;
256 Indexes : List_Id := No_List) return List_Id;
257 -- This recursive routine returns a list of statements containing the
258 -- loops and assignments that are needed for the expansion of the array
261 -- N is the (sub-)aggregate node to be expanded into code. This node has
262 -- been fully analyzed, and its Etype is properly set.
264 -- Index is the index node corresponding to the array subaggregate N
266 -- Into is the target expression into which we are copying the aggregate.
267 -- Note that this node may not have been analyzed yet, and so the Etype
268 -- field may not be set.
270 -- Scalar_Comp is True if the component type of the aggregate is scalar
272 -- Indexes is the current list of expressions used to index the object we
275 procedure Convert_Array_Aggr_In_Allocator
279 -- If the aggregate appears within an allocator and can be expanded in
280 -- place, this routine generates the individual assignments to components
281 -- of the designated object. This is an optimization over the general
282 -- case, where a temporary is first created on the stack and then used to
283 -- construct the allocated object on the heap.
285 procedure Convert_To_Positional
287 Max_Others_Replicate : Nat := 32;
288 Handle_Bit_Packed : Boolean := False);
289 -- If possible, convert named notation to positional notation. This
290 -- conversion is possible only in some static cases. If the conversion is
291 -- possible, then N is rewritten with the analyzed converted aggregate.
292 -- The parameter Max_Others_Replicate controls the maximum number of
293 -- values corresponding to an others choice that will be converted to
294 -- positional notation (the default of 32 is the normal limit, and reflects
295 -- the fact that normally the loop is better than a lot of separate
296 -- assignments). Note that this limit gets overridden in any case if
297 -- either of the restrictions No_Elaboration_Code or No_Implicit_Loops is
298 -- set. The parameter Handle_Bit_Packed is usually set False (since we do
299 -- not expect the back end to handle bit packed arrays, so the normal case
300 -- of conversion is pointless), but in the special case of a call from
301 -- Packed_Array_Aggregate_Handled, we set this parameter to True, since
302 -- these are cases we handle in there.
304 procedure Expand_Array_Aggregate (N : Node_Id);
305 -- This is the top-level routine to perform array aggregate expansion.
306 -- N is the N_Aggregate node to be expanded.
308 function Is_Two_Dim_Packed_Array (Typ : Entity_Id) return Boolean;
309 -- For two-dimensional packed aggregates with constant bounds and constant
310 -- components, it is preferable to pack the inner aggregates because the
311 -- whole matrix can then be presented to the back-end as a one-dimensional
312 -- list of literals. This is much more efficient than expanding into single
313 -- component assignments. This function determines if the type Typ is for
314 -- an array that is suitable for this optimization: it returns True if Typ
315 -- is a two dimensional bit packed array with component size 1, 2, or 4.
317 function Packed_Array_Aggregate_Handled (N : Node_Id) return Boolean;
318 -- Given an array aggregate, this function handles the case of a packed
319 -- array aggregate with all constant values, where the aggregate can be
320 -- evaluated at compile time. If this is possible, then N is rewritten
321 -- to be its proper compile time value with all the components properly
322 -- assembled. The expression is analyzed and resolved and True is returned.
323 -- If this transformation is not possible, N is unchanged and False is
326 function Two_Dim_Packed_Array_Handled (N : Node_Id) return Boolean;
327 -- If the type of the aggregate is a two-dimensional bit_packed array
328 -- it may be transformed into an array of bytes with constant values,
329 -- and presented to the back-end as a static value. The function returns
330 -- false if this transformation cannot be performed. THis is similar to,
331 -- and reuses part of the machinery in Packed_Array_Aggregate_Handled.
337 function Aggr_Size_OK (N : Node_Id; Typ : Entity_Id) return Boolean is
346 -- Determines the maximum size of an array aggregate produced by
347 -- converting named to positional notation (e.g. from others clauses).
348 -- This avoids running away with attempts to convert huge aggregates,
349 -- which hit memory limits in the backend.
351 function Component_Count (T : Entity_Id) return Nat;
352 -- The limit is applied to the total number of subcomponents that the
353 -- aggregate will have, which is the number of static expressions
354 -- that will appear in the flattened array. This requires a recursive
355 -- computation of the number of scalar components of the structure.
357 ---------------------
358 -- Component_Count --
359 ---------------------
361 function Component_Count (T : Entity_Id) return Nat is
366 if Is_Scalar_Type (T) then
369 elsif Is_Record_Type (T) then
370 Comp := First_Component (T);
371 while Present (Comp) loop
372 Res := Res + Component_Count (Etype (Comp));
373 Next_Component (Comp);
378 elsif Is_Array_Type (T) then
380 Lo : constant Node_Id :=
381 Type_Low_Bound (Etype (First_Index (T)));
382 Hi : constant Node_Id :=
383 Type_High_Bound (Etype (First_Index (T)));
385 Siz : constant Nat := Component_Count (Component_Type (T));
388 -- Check for superflat arrays, i.e. arrays with such bounds
389 -- as 4 .. 2, to insure that this function never returns a
390 -- meaningless negative value.
392 if not Compile_Time_Known_Value (Lo)
393 or else not Compile_Time_Known_Value (Hi)
394 or else Expr_Value (Hi) < Expr_Value (Lo)
399 -- If the number of components is greater than Int'Last,
400 -- then return Int'Last, so caller will return False (Aggr
401 -- size is not OK). Otherwise, UI_To_Int will crash.
404 UI : constant Uint :=
405 Expr_Value (Hi) - Expr_Value (Lo) + 1;
407 if UI_Is_In_Int_Range (UI) then
408 return Siz * UI_To_Int (UI);
417 -- Can only be a null for an access type
423 -- Start of processing for Aggr_Size_OK
426 -- The normal aggregate limit is 500000, but we increase this limit to
427 -- 2**24 (about 16 million) if Restrictions (No_Elaboration_Code) or
428 -- Restrictions (No_Implicit_Loops) is specified, since in either case
429 -- we are at risk of declaring the program illegal because of this
430 -- limit. We also increase the limit when Static_Elaboration_Desired,
431 -- given that this means that objects are intended to be placed in data
434 -- We also increase the limit if the aggregate is for a packed two-
435 -- dimensional array, because if components are static it is much more
436 -- efficient to construct a one-dimensional equivalent array with static
439 -- Conversely, we decrease the maximum size if none of the above
440 -- requirements apply, and if the aggregate has a single component
441 -- association, which will be more efficient if implemented with a loop.
443 -- Finally, we use a small limit in CodePeer mode where we favor loops
444 -- instead of thousands of single assignments (from large aggregates).
446 Max_Aggr_Size := 500000;
448 if CodePeer_Mode then
449 Max_Aggr_Size := 100;
451 elsif Restriction_Active (No_Elaboration_Code)
452 or else Restriction_Active (No_Implicit_Loops)
453 or else Is_Two_Dim_Packed_Array (Typ)
454 or else (Ekind (Current_Scope) = E_Package
455 and then Static_Elaboration_Desired (Current_Scope))
457 Max_Aggr_Size := 2 ** 24;
459 elsif No (Expressions (N))
460 and then No (Next (First (Component_Associations (N))))
462 Max_Aggr_Size := 5000;
465 Siz := Component_Count (Component_Type (Typ));
467 Indx := First_Index (Typ);
468 while Present (Indx) loop
469 Lo := Type_Low_Bound (Etype (Indx));
470 Hi := Type_High_Bound (Etype (Indx));
472 -- Bounds need to be known at compile time
474 if not Compile_Time_Known_Value (Lo)
475 or else not Compile_Time_Known_Value (Hi)
480 Lov := Expr_Value (Lo);
481 Hiv := Expr_Value (Hi);
483 -- A flat array is always safe
489 -- One-component aggregates are suspicious, and if the context type
490 -- is an object declaration with nonstatic bounds it will trip gcc;
491 -- such an aggregate must be expanded into a single assignment.
493 if Hiv = Lov and then Nkind (Parent (N)) = N_Object_Declaration then
495 Index_Type : constant Entity_Id :=
497 (First_Index (Etype (Defining_Identifier (Parent (N)))));
501 if not Compile_Time_Known_Value (Type_Low_Bound (Index_Type))
502 or else not Compile_Time_Known_Value
503 (Type_High_Bound (Index_Type))
505 if Present (Component_Associations (N)) then
508 (Choice_List (First (Component_Associations (N))));
510 if Is_Entity_Name (Indx)
511 and then not Is_Type (Entity (Indx))
514 ("single component aggregate in "
515 & "non-static context??", Indx);
516 Error_Msg_N ("\maybe subtype name was meant??", Indx);
526 Rng : constant Uint := Hiv - Lov + 1;
529 -- Check if size is too large
531 if not UI_Is_In_Int_Range (Rng) then
535 Siz := Siz * UI_To_Int (Rng);
539 or else Siz > Max_Aggr_Size
544 -- Bounds must be in integer range, for later array construction
546 if not UI_Is_In_Int_Range (Lov)
548 not UI_Is_In_Int_Range (Hiv)
559 ---------------------------------
560 -- Backend_Processing_Possible --
561 ---------------------------------
563 -- Backend processing by Gigi/gcc is possible only if all the following
564 -- conditions are met:
566 -- 1. N is fully positional
568 -- 2. N is not a bit-packed array aggregate;
570 -- 3. The size of N's array type must be known at compile time. Note
571 -- that this implies that the component size is also known
573 -- 4. The array type of N does not follow the Fortran layout convention
574 -- or if it does it must be 1 dimensional.
576 -- 5. The array component type may not be tagged (which could necessitate
577 -- reassignment of proper tags).
579 -- 6. The array component type must not have unaligned bit components
581 -- 7. None of the components of the aggregate may be bit unaligned
584 -- 8. There cannot be delayed components, since we do not know enough
585 -- at this stage to know if back end processing is possible.
587 -- 9. There cannot be any discriminated record components, since the
588 -- back end cannot handle this complex case.
590 -- 10. No controlled actions need to be generated for components
592 -- 11. When generating C code, N must be part of a N_Object_Declaration
594 -- 12. When generating C code, N must not include function calls
596 function Backend_Processing_Possible (N : Node_Id) return Boolean is
597 Typ : constant Entity_Id := Etype (N);
598 -- Typ is the correct constrained array subtype of the aggregate
600 function Component_Check (N : Node_Id; Index : Node_Id) return Boolean;
601 -- This routine checks components of aggregate N, enforcing checks
602 -- 1, 7, 8, 9, 11, and 12. In the multidimensional case, these checks
603 -- are performed on subaggregates. The Index value is the current index
604 -- being checked in the multidimensional case.
606 ---------------------
607 -- Component_Check --
608 ---------------------
610 function Component_Check (N : Node_Id; Index : Node_Id) return Boolean is
611 function Ultimate_Original_Expression (N : Node_Id) return Node_Id;
612 -- Given a type conversion or an unchecked type conversion N, return
613 -- its innermost original expression.
615 ----------------------------------
616 -- Ultimate_Original_Expression --
617 ----------------------------------
619 function Ultimate_Original_Expression (N : Node_Id) return Node_Id is
620 Expr : Node_Id := Original_Node (N);
623 while Nkind_In (Expr, N_Type_Conversion,
624 N_Unchecked_Type_Conversion)
626 Expr := Original_Node (Expression (Expr));
630 end Ultimate_Original_Expression;
636 -- Start of processing for Component_Check
639 -- Checks 1: (no component associations)
641 if Present (Component_Associations (N)) then
645 -- Checks 11: The C code generator cannot handle aggregates that are
646 -- not part of an object declaration.
648 if Modify_Tree_For_C then
650 Par : Node_Id := Parent (N);
653 -- Skip enclosing nested aggregates and their qualified
656 while Nkind (Par) = N_Aggregate
657 or else Nkind (Par) = N_Qualified_Expression
662 if Nkind (Par) /= N_Object_Declaration then
668 -- Checks on components
670 -- Recurse to check subaggregates, which may appear in qualified
671 -- expressions. If delayed, the front-end will have to expand.
672 -- If the component is a discriminated record, treat as nonstatic,
673 -- as the back-end cannot handle this properly.
675 Expr := First (Expressions (N));
676 while Present (Expr) loop
678 -- Checks 8: (no delayed components)
680 if Is_Delayed_Aggregate (Expr) then
684 -- Checks 9: (no discriminated records)
686 if Present (Etype (Expr))
687 and then Is_Record_Type (Etype (Expr))
688 and then Has_Discriminants (Etype (Expr))
693 -- Checks 7. Component must not be bit aligned component
695 if Possible_Bit_Aligned_Component (Expr) then
699 -- Checks 12: (no function call)
703 Nkind (Ultimate_Original_Expression (Expr)) = N_Function_Call
708 -- Recursion to following indexes for multiple dimension case
710 if Present (Next_Index (Index))
711 and then not Component_Check (Expr, Next_Index (Index))
716 -- All checks for that component finished, on to next
724 -- Start of processing for Backend_Processing_Possible
727 -- Checks 2 (array not bit packed) and 10 (no controlled actions)
729 if Is_Bit_Packed_Array (Typ) or else Needs_Finalization (Typ) then
733 -- If component is limited, aggregate must be expanded because each
734 -- component assignment must be built in place.
736 if Is_Limited_View (Component_Type (Typ)) then
740 -- Checks 4 (array must not be multidimensional Fortran case)
742 if Convention (Typ) = Convention_Fortran
743 and then Number_Dimensions (Typ) > 1
748 -- Checks 3 (size of array must be known at compile time)
750 if not Size_Known_At_Compile_Time (Typ) then
754 -- Checks on components
756 if not Component_Check (N, First_Index (Typ)) then
760 -- Checks 5 (if the component type is tagged, then we may need to do
761 -- tag adjustments. Perhaps this should be refined to check for any
762 -- component associations that actually need tag adjustment, similar
763 -- to the test in Component_OK_For_Backend for record aggregates with
764 -- tagged components, but not clear whether it's worthwhile ???; in the
765 -- case of virtual machines (no Tagged_Type_Expansion), object tags are
766 -- handled implicitly).
768 if Is_Tagged_Type (Component_Type (Typ))
769 and then Tagged_Type_Expansion
774 -- Checks 6 (component type must not have bit aligned components)
776 if Type_May_Have_Bit_Aligned_Components (Component_Type (Typ)) then
780 -- Backend processing is possible
782 Set_Size_Known_At_Compile_Time (Etype (N), True);
784 end Backend_Processing_Possible;
786 ---------------------------
787 -- Build_Array_Aggr_Code --
788 ---------------------------
790 -- The code that we generate from a one dimensional aggregate is
792 -- 1. If the subaggregate contains discrete choices we
794 -- (a) Sort the discrete choices
796 -- (b) Otherwise for each discrete choice that specifies a range we
797 -- emit a loop. If a range specifies a maximum of three values, or
798 -- we are dealing with an expression we emit a sequence of
799 -- assignments instead of a loop.
801 -- (c) Generate the remaining loops to cover the others choice if any
803 -- 2. If the aggregate contains positional elements we
805 -- (a) translate the positional elements in a series of assignments
807 -- (b) Generate a final loop to cover the others choice if any.
808 -- Note that this final loop has to be a while loop since the case
810 -- L : Integer := Integer'Last;
811 -- H : Integer := Integer'Last;
812 -- A : array (L .. H) := (1, others =>0);
814 -- cannot be handled by a for loop. Thus for the following
816 -- array (L .. H) := (.. positional elements.., others =>E);
818 -- we always generate something like:
820 -- J : Index_Type := Index_Of_Last_Positional_Element;
822 -- J := Index_Base'Succ (J)
826 function Build_Array_Aggr_Code
831 Scalar_Comp : Boolean;
832 Indexes : List_Id := No_List) return List_Id
834 Loc : constant Source_Ptr := Sloc (N);
835 Index_Base : constant Entity_Id := Base_Type (Etype (Index));
836 Index_Base_L : constant Node_Id := Type_Low_Bound (Index_Base);
837 Index_Base_H : constant Node_Id := Type_High_Bound (Index_Base);
839 function Add (Val : Int; To : Node_Id) return Node_Id;
840 -- Returns an expression where Val is added to expression To, unless
841 -- To+Val is provably out of To's base type range. To must be an
842 -- already analyzed expression.
844 function Empty_Range (L, H : Node_Id) return Boolean;
845 -- Returns True if the range defined by L .. H is certainly empty
847 function Equal (L, H : Node_Id) return Boolean;
848 -- Returns True if L = H for sure
850 function Index_Base_Name return Node_Id;
851 -- Returns a new reference to the index type name
856 In_Loop : Boolean := False) return List_Id;
857 -- Ind must be a side-effect-free expression. If the input aggregate N
858 -- to Build_Loop contains no subaggregates, then this function returns
859 -- the assignment statement:
861 -- Into (Indexes, Ind) := Expr;
863 -- Otherwise we call Build_Code recursively. Flag In_Loop should be set
864 -- when the assignment appears within a generated loop.
866 -- Ada 2005 (AI-287): In case of default initialized component, Expr
867 -- is empty and we generate a call to the corresponding IP subprogram.
869 function Gen_Loop (L, H : Node_Id; Expr : Node_Id) return List_Id;
870 -- Nodes L and H must be side-effect-free expressions. If the input
871 -- aggregate N to Build_Loop contains no subaggregates, this routine
872 -- returns the for loop statement:
874 -- for J in Index_Base'(L) .. Index_Base'(H) loop
875 -- Into (Indexes, J) := Expr;
878 -- Otherwise we call Build_Code recursively. As an optimization if the
879 -- loop covers 3 or fewer scalar elements we generate a sequence of
881 -- If the component association that generates the loop comes from an
882 -- Iterated_Component_Association, the loop parameter has the name of
883 -- the corresponding parameter in the original construct.
885 function Gen_While (L, H : Node_Id; Expr : Node_Id) return List_Id;
886 -- Nodes L and H must be side-effect-free expressions. If the input
887 -- aggregate N to Build_Loop contains no subaggregates, this routine
888 -- returns the while loop statement:
890 -- J : Index_Base := L;
892 -- J := Index_Base'Succ (J);
893 -- Into (Indexes, J) := Expr;
896 -- Otherwise we call Build_Code recursively
898 function Get_Assoc_Expr (Assoc : Node_Id) return Node_Id;
899 -- For an association with a box, use value given by aspect
900 -- Default_Component_Value of array type if specified, else use
901 -- value given by aspect Default_Value for component type itself
902 -- if specified, else return Empty.
904 function Local_Compile_Time_Known_Value (E : Node_Id) return Boolean;
905 function Local_Expr_Value (E : Node_Id) return Uint;
906 -- These two Local routines are used to replace the corresponding ones
907 -- in sem_eval because while processing the bounds of an aggregate with
908 -- discrete choices whose index type is an enumeration, we build static
909 -- expressions not recognized by Compile_Time_Known_Value as such since
910 -- they have not yet been analyzed and resolved. All the expressions in
911 -- question are things like Index_Base_Name'Val (Const) which we can
912 -- easily recognize as being constant.
918 function Add (Val : Int; To : Node_Id) return Node_Id is
923 U_Val : constant Uint := UI_From_Int (Val);
926 -- Note: do not try to optimize the case of Val = 0, because
927 -- we need to build a new node with the proper Sloc value anyway.
929 -- First test if we can do constant folding
931 if Local_Compile_Time_Known_Value (To) then
932 U_To := Local_Expr_Value (To) + Val;
934 -- Determine if our constant is outside the range of the index.
935 -- If so return an Empty node. This empty node will be caught
936 -- by Empty_Range below.
938 if Compile_Time_Known_Value (Index_Base_L)
939 and then U_To < Expr_Value (Index_Base_L)
943 elsif Compile_Time_Known_Value (Index_Base_H)
944 and then U_To > Expr_Value (Index_Base_H)
949 Expr_Pos := Make_Integer_Literal (Loc, U_To);
950 Set_Is_Static_Expression (Expr_Pos);
952 if not Is_Enumeration_Type (Index_Base) then
955 -- If we are dealing with enumeration return
956 -- Index_Base'Val (Expr_Pos)
960 Make_Attribute_Reference
962 Prefix => Index_Base_Name,
963 Attribute_Name => Name_Val,
964 Expressions => New_List (Expr_Pos));
970 -- If we are here no constant folding possible
972 if not Is_Enumeration_Type (Index_Base) then
975 Left_Opnd => Duplicate_Subexpr (To),
976 Right_Opnd => Make_Integer_Literal (Loc, U_Val));
978 -- If we are dealing with enumeration return
979 -- Index_Base'Val (Index_Base'Pos (To) + Val)
983 Make_Attribute_Reference
985 Prefix => Index_Base_Name,
986 Attribute_Name => Name_Pos,
987 Expressions => New_List (Duplicate_Subexpr (To)));
992 Right_Opnd => Make_Integer_Literal (Loc, U_Val));
995 Make_Attribute_Reference
997 Prefix => Index_Base_Name,
998 Attribute_Name => Name_Val,
999 Expressions => New_List (Expr_Pos));
1009 function Empty_Range (L, H : Node_Id) return Boolean is
1010 Is_Empty : Boolean := False;
1015 -- First check if L or H were already detected as overflowing the
1016 -- index base range type by function Add above. If this is so Add
1017 -- returns the empty node.
1019 if No (L) or else No (H) then
1023 for J in 1 .. 3 loop
1026 -- L > H range is empty
1032 -- B_L > H range must be empty
1035 Low := Index_Base_L;
1038 -- L > B_H range must be empty
1042 High := Index_Base_H;
1045 if Local_Compile_Time_Known_Value (Low)
1047 Local_Compile_Time_Known_Value (High)
1050 UI_Gt (Local_Expr_Value (Low), Local_Expr_Value (High));
1063 function Equal (L, H : Node_Id) return Boolean is
1068 elsif Local_Compile_Time_Known_Value (L)
1070 Local_Compile_Time_Known_Value (H)
1072 return UI_Eq (Local_Expr_Value (L), Local_Expr_Value (H));
1085 In_Loop : Boolean := False) return List_Id
1087 function Add_Loop_Actions (Lis : List_Id) return List_Id;
1088 -- Collect insert_actions generated in the construction of a loop,
1089 -- and prepend them to the sequence of assignments to complete the
1090 -- eventual body of the loop.
1092 procedure Initialize_Array_Component
1093 (Arr_Comp : Node_Id;
1095 Init_Expr : Node_Id;
1097 -- Perform the initialization of array component Arr_Comp with
1098 -- expected type Comp_Typ. Init_Expr denotes the initialization
1099 -- expression of the array component. All generated code is added
1102 procedure Initialize_Ctrl_Array_Component
1103 (Arr_Comp : Node_Id;
1104 Comp_Typ : Entity_Id;
1105 Init_Expr : Node_Id;
1107 -- Perform the initialization of array component Arr_Comp when its
1108 -- expected type Comp_Typ needs finalization actions. Init_Expr is
1109 -- the initialization expression of the array component. All hook-
1110 -- related declarations are inserted prior to aggregate N. Remaining
1111 -- code is added to list Stmts.
1113 ----------------------
1114 -- Add_Loop_Actions --
1115 ----------------------
1117 function Add_Loop_Actions (Lis : List_Id) return List_Id is
1121 -- Ada 2005 (AI-287): Do nothing else in case of default
1122 -- initialized component.
1127 elsif Nkind (Parent (Expr)) = N_Component_Association
1128 and then Present (Loop_Actions (Parent (Expr)))
1130 Append_List (Lis, Loop_Actions (Parent (Expr)));
1131 Res := Loop_Actions (Parent (Expr));
1132 Set_Loop_Actions (Parent (Expr), No_List);
1138 end Add_Loop_Actions;
1140 --------------------------------
1141 -- Initialize_Array_Component --
1142 --------------------------------
1144 procedure Initialize_Array_Component
1145 (Arr_Comp : Node_Id;
1147 Init_Expr : Node_Id;
1150 Exceptions_OK : constant Boolean :=
1151 not Restriction_Active
1152 (No_Exception_Propagation);
1154 Finalization_OK : constant Boolean :=
1156 and then Needs_Finalization (Comp_Typ);
1158 Full_Typ : constant Entity_Id := Underlying_Type (Comp_Typ);
1160 Blk_Stmts : List_Id;
1161 Init_Stmt : Node_Id;
1164 -- Protect the initialization statements from aborts. Generate:
1168 if Finalization_OK and Abort_Allowed then
1169 if Exceptions_OK then
1170 Blk_Stmts := New_List;
1175 Append_To (Blk_Stmts, Build_Runtime_Call (Loc, RE_Abort_Defer));
1177 -- Otherwise aborts are not allowed. All generated code is added
1178 -- directly to the input list.
1184 -- Initialize the array element. Generate:
1186 -- Arr_Comp := Init_Expr;
1188 -- Note that the initialization expression is replicated because
1189 -- it has to be reevaluated within a generated loop.
1192 Make_OK_Assignment_Statement (Loc,
1193 Name => New_Copy_Tree (Arr_Comp),
1194 Expression => New_Copy_Tree (Init_Expr));
1195 Set_No_Ctrl_Actions (Init_Stmt);
1197 -- If this is an aggregate for an array of arrays, each
1198 -- subaggregate will be expanded as well, and even with
1199 -- No_Ctrl_Actions the assignments of inner components will
1200 -- require attachment in their assignments to temporaries. These
1201 -- temporaries must be finalized for each subaggregate. Generate:
1204 -- Arr_Comp := Init_Expr;
1207 if Finalization_OK and then Is_Array_Type (Comp_Typ) then
1209 Make_Block_Statement (Loc,
1210 Handled_Statement_Sequence =>
1211 Make_Handled_Sequence_Of_Statements (Loc,
1212 Statements => New_List (Init_Stmt)));
1215 Append_To (Blk_Stmts, Init_Stmt);
1217 -- Adjust the tag due to a possible view conversion. Generate:
1219 -- Arr_Comp._tag := Full_TypP;
1221 if Tagged_Type_Expansion
1222 and then Present (Comp_Typ)
1223 and then Is_Tagged_Type (Comp_Typ)
1225 Append_To (Blk_Stmts,
1226 Make_OK_Assignment_Statement (Loc,
1228 Make_Selected_Component (Loc,
1229 Prefix => New_Copy_Tree (Arr_Comp),
1232 (First_Tag_Component (Full_Typ), Loc)),
1235 Unchecked_Convert_To (RTE (RE_Tag),
1237 (Node (First_Elmt (Access_Disp_Table (Full_Typ))),
1241 -- Adjust the array component. Controlled subaggregates are not
1242 -- considered because each of their individual elements will
1243 -- receive an adjustment of its own. Generate:
1245 -- [Deep_]Adjust (Arr_Comp);
1248 and then not Is_Limited_Type (Comp_Typ)
1249 and then not Is_Build_In_Place_Function_Call (Init_Expr)
1251 (Is_Array_Type (Comp_Typ)
1252 and then Is_Controlled (Component_Type (Comp_Typ))
1253 and then Nkind (Expr) = N_Aggregate)
1257 (Obj_Ref => New_Copy_Tree (Arr_Comp),
1260 -- Guard against a missing [Deep_]Adjust when the component
1261 -- type was not frozen properly.
1263 if Present (Adj_Call) then
1264 Append_To (Blk_Stmts, Adj_Call);
1268 -- Complete the protection of the initialization statements
1270 if Finalization_OK and Abort_Allowed then
1272 -- Wrap the initialization statements in a block to catch a
1273 -- potential exception. Generate:
1277 -- Arr_Comp := Init_Expr;
1278 -- Arr_Comp._tag := Full_TypP;
1279 -- [Deep_]Adjust (Arr_Comp);
1281 -- Abort_Undefer_Direct;
1284 if Exceptions_OK then
1286 Build_Abort_Undefer_Block (Loc,
1290 -- Otherwise exceptions are not propagated. Generate:
1293 -- Arr_Comp := Init_Expr;
1294 -- Arr_Comp._tag := Full_TypP;
1295 -- [Deep_]Adjust (Arr_Comp);
1299 Append_To (Blk_Stmts,
1300 Build_Runtime_Call (Loc, RE_Abort_Undefer));
1303 end Initialize_Array_Component;
1305 -------------------------------------
1306 -- Initialize_Ctrl_Array_Component --
1307 -------------------------------------
1309 procedure Initialize_Ctrl_Array_Component
1310 (Arr_Comp : Node_Id;
1311 Comp_Typ : Entity_Id;
1312 Init_Expr : Node_Id;
1316 Act_Stmts : List_Id;
1319 Hook_Clear : Node_Id;
1321 In_Place_Expansion : Boolean;
1322 -- Flag set when a nonlimited controlled function call requires
1323 -- in-place expansion.
1326 -- Duplicate the initialization expression in case the context is
1327 -- a multi choice list or an "others" choice which plugs various
1328 -- holes in the aggregate. As a result the expression is no longer
1329 -- shared between the various components and is reevaluated for
1330 -- each such component.
1332 Expr := New_Copy_Tree (Init_Expr);
1333 Set_Parent (Expr, Parent (Init_Expr));
1335 -- Perform a preliminary analysis and resolution to determine what
1336 -- the initialization expression denotes. An unanalyzed function
1337 -- call may appear as an identifier or an indexed component.
1339 if Nkind_In (Expr, N_Function_Call,
1341 N_Indexed_Component)
1342 and then not Analyzed (Expr)
1344 Preanalyze_And_Resolve (Expr, Comp_Typ);
1347 In_Place_Expansion :=
1348 Nkind (Expr) = N_Function_Call
1349 and then not Is_Build_In_Place_Result_Type (Comp_Typ);
1351 -- The initialization expression is a controlled function call.
1352 -- Perform in-place removal of side effects to avoid creating a
1353 -- transient scope, which leads to premature finalization.
1355 -- This in-place expansion is not performed for limited transient
1356 -- objects because the initialization is already done in-place.
1358 if In_Place_Expansion then
1360 -- Suppress the removal of side effects by general analysis
1361 -- because this behavior is emulated here. This avoids the
1362 -- generation of a transient scope, which leads to out-of-order
1363 -- adjustment and finalization.
1365 Set_No_Side_Effect_Removal (Expr);
1367 -- When the transient component initialization is related to a
1368 -- range or an "others", keep all generated statements within
1369 -- the enclosing loop. This way the controlled function call
1370 -- will be evaluated at each iteration, and its result will be
1371 -- finalized at the end of each iteration.
1377 -- Otherwise this is a single component initialization. Hook-
1378 -- related statements are inserted prior to the aggregate.
1382 Act_Stmts := No_List;
1385 -- Install all hook-related declarations and prepare the clean
1388 Process_Transient_Component
1390 Comp_Typ => Comp_Typ,
1392 Fin_Call => Fin_Call,
1393 Hook_Clear => Hook_Clear,
1395 Stmts => Act_Stmts);
1398 -- Use the noncontrolled component initialization circuitry to
1399 -- assign the result of the function call to the array element.
1400 -- This also performs subaggregate wrapping, tag adjustment, and
1401 -- [deep] adjustment of the array element.
1403 Initialize_Array_Component
1404 (Arr_Comp => Arr_Comp,
1405 Comp_Typ => Comp_Typ,
1409 -- At this point the array element is fully initialized. Complete
1410 -- the processing of the controlled array component by finalizing
1411 -- the transient function result.
1413 if In_Place_Expansion then
1414 Process_Transient_Component_Completion
1417 Fin_Call => Fin_Call,
1418 Hook_Clear => Hook_Clear,
1421 end Initialize_Ctrl_Array_Component;
1425 Stmts : constant List_Id := New_List;
1427 Comp_Typ : Entity_Id := Empty;
1429 Indexed_Comp : Node_Id;
1430 Init_Call : Node_Id;
1431 New_Indexes : List_Id;
1433 -- Start of processing for Gen_Assign
1436 if No (Indexes) then
1437 New_Indexes := New_List;
1439 New_Indexes := New_Copy_List_Tree (Indexes);
1442 Append_To (New_Indexes, Ind);
1444 if Present (Next_Index (Index)) then
1447 Build_Array_Aggr_Code
1450 Index => Next_Index (Index),
1452 Scalar_Comp => Scalar_Comp,
1453 Indexes => New_Indexes));
1456 -- If we get here then we are at a bottom-level (sub-)aggregate
1460 (Make_Indexed_Component (Loc,
1461 Prefix => New_Copy_Tree (Into),
1462 Expressions => New_Indexes));
1464 Set_Assignment_OK (Indexed_Comp);
1466 -- Ada 2005 (AI-287): In case of default initialized component, Expr
1467 -- is not present (and therefore we also initialize Expr_Q to empty).
1471 elsif Nkind (Expr) = N_Qualified_Expression then
1472 Expr_Q := Expression (Expr);
1477 if Present (Etype (N)) and then Etype (N) /= Any_Composite then
1478 Comp_Typ := Component_Type (Etype (N));
1479 pragma Assert (Comp_Typ = Ctype); -- AI-287
1481 elsif Present (Next (First (New_Indexes))) then
1483 -- Ada 2005 (AI-287): Do nothing in case of default initialized
1484 -- component because we have received the component type in
1485 -- the formal parameter Ctype.
1487 -- ??? Some assert pragmas have been added to check if this new
1488 -- formal can be used to replace this code in all cases.
1490 if Present (Expr) then
1492 -- This is a multidimensional array. Recover the component type
1493 -- from the outermost aggregate, because subaggregates do not
1494 -- have an assigned type.
1501 while Present (P) loop
1502 if Nkind (P) = N_Aggregate
1503 and then Present (Etype (P))
1505 Comp_Typ := Component_Type (Etype (P));
1513 pragma Assert (Comp_Typ = Ctype); -- AI-287
1518 -- Ada 2005 (AI-287): We only analyze the expression in case of non-
1519 -- default initialized components (otherwise Expr_Q is not present).
1522 and then Nkind_In (Expr_Q, N_Aggregate, N_Extension_Aggregate)
1524 -- At this stage the Expression may not have been analyzed yet
1525 -- because the array aggregate code has not been updated to use
1526 -- the Expansion_Delayed flag and avoid analysis altogether to
1527 -- solve the same problem (see Resolve_Aggr_Expr). So let us do
1528 -- the analysis of non-array aggregates now in order to get the
1529 -- value of Expansion_Delayed flag for the inner aggregate ???
1531 -- In the case of an iterated component association, the analysis
1532 -- of the generated loop will analyze the expression in the
1533 -- proper context, in which the loop parameter is visible.
1535 if Present (Comp_Typ) and then not Is_Array_Type (Comp_Typ) then
1536 if Nkind (Parent (Expr_Q)) = N_Iterated_Component_Association
1537 or else Nkind (Parent (Parent ((Expr_Q)))) =
1538 N_Iterated_Component_Association
1542 Analyze_And_Resolve (Expr_Q, Comp_Typ);
1546 if Is_Delayed_Aggregate (Expr_Q) then
1548 -- This is either a subaggregate of a multidimensional array,
1549 -- or a component of an array type whose component type is
1550 -- also an array. In the latter case, the expression may have
1551 -- component associations that provide different bounds from
1552 -- those of the component type, and sliding must occur. Instead
1553 -- of decomposing the current aggregate assignment, force the
1554 -- reanalysis of the assignment, so that a temporary will be
1555 -- generated in the usual fashion, and sliding will take place.
1557 if Nkind (Parent (N)) = N_Assignment_Statement
1558 and then Is_Array_Type (Comp_Typ)
1559 and then Present (Component_Associations (Expr_Q))
1560 and then Must_Slide (Comp_Typ, Etype (Expr_Q))
1562 Set_Expansion_Delayed (Expr_Q, False);
1563 Set_Analyzed (Expr_Q, False);
1568 Late_Expansion (Expr_Q, Etype (Expr_Q), Indexed_Comp));
1573 if Present (Expr) then
1575 -- Handle an initialization expression of a controlled type in
1576 -- case it denotes a function call. In general such a scenario
1577 -- will produce a transient scope, but this will lead to wrong
1578 -- order of initialization, adjustment, and finalization in the
1579 -- context of aggregates.
1581 -- Target (1) := Ctrl_Func_Call;
1584 -- Trans_Obj : ... := Ctrl_Func_Call; -- object
1585 -- Target (1) := Trans_Obj;
1586 -- Finalize (Trans_Obj);
1588 -- Target (1)._tag := ...;
1589 -- Adjust (Target (1));
1591 -- In the example above, the call to Finalize occurs too early
1592 -- and as a result it may leave the array component in a bad
1593 -- state. Finalization of the transient object should really
1594 -- happen after adjustment.
1596 -- To avoid this scenario, perform in-place side-effect removal
1597 -- of the function call. This eliminates the transient property
1598 -- of the function result and ensures correct order of actions.
1600 -- Res : ... := Ctrl_Func_Call;
1601 -- Target (1) := Res;
1602 -- Target (1)._tag := ...;
1603 -- Adjust (Target (1));
1606 if Present (Comp_Typ)
1607 and then Needs_Finalization (Comp_Typ)
1608 and then Nkind (Expr) /= N_Aggregate
1610 Initialize_Ctrl_Array_Component
1611 (Arr_Comp => Indexed_Comp,
1612 Comp_Typ => Comp_Typ,
1616 -- Otherwise perform simple component initialization
1619 Initialize_Array_Component
1620 (Arr_Comp => Indexed_Comp,
1621 Comp_Typ => Comp_Typ,
1626 -- Ada 2005 (AI-287): In case of default initialized component, call
1627 -- the initialization subprogram associated with the component type.
1628 -- If the component type is an access type, add an explicit null
1629 -- assignment, because for the back-end there is an initialization
1630 -- present for the whole aggregate, and no default initialization
1633 -- In addition, if the component type is controlled, we must call
1634 -- its Initialize procedure explicitly, because there is no explicit
1635 -- object creation that will invoke it otherwise.
1638 if Present (Base_Init_Proc (Base_Type (Ctype)))
1639 or else Has_Task (Base_Type (Ctype))
1641 Append_List_To (Stmts,
1642 Build_Initialization_Call (Loc,
1643 Id_Ref => Indexed_Comp,
1645 With_Default_Init => True));
1647 -- If the component type has invariants, add an invariant
1648 -- check after the component is default-initialized. It will
1649 -- be analyzed and resolved before the code for initialization
1650 -- of other components.
1652 if Has_Invariants (Ctype) then
1653 Set_Etype (Indexed_Comp, Ctype);
1654 Append_To (Stmts, Make_Invariant_Call (Indexed_Comp));
1657 elsif Is_Access_Type (Ctype) then
1659 Make_Assignment_Statement (Loc,
1660 Name => New_Copy_Tree (Indexed_Comp),
1661 Expression => Make_Null (Loc)));
1664 if Needs_Finalization (Ctype) then
1667 (Obj_Ref => New_Copy_Tree (Indexed_Comp),
1670 -- Guard against a missing [Deep_]Initialize when the component
1671 -- type was not properly frozen.
1673 if Present (Init_Call) then
1674 Append_To (Stmts, Init_Call);
1679 return Add_Loop_Actions (Stmts);
1686 function Gen_Loop (L, H : Node_Id; Expr : Node_Id) return List_Id is
1687 Is_Iterated_Component : constant Boolean :=
1688 Nkind (Parent (Expr)) = N_Iterated_Component_Association;
1699 -- Index_Base'(L) .. Index_Base'(H)
1701 L_Iteration_Scheme : Node_Id;
1702 -- L_J in Index_Base'(L) .. Index_Base'(H)
1705 -- The statements to execute in the loop
1707 S : constant List_Id := New_List;
1708 -- List of statements
1711 -- Copy of expression tree, used for checking purposes
1714 -- If loop bounds define an empty range return the null statement
1716 if Empty_Range (L, H) then
1717 Append_To (S, Make_Null_Statement (Loc));
1719 -- Ada 2005 (AI-287): Nothing else need to be done in case of
1720 -- default initialized component.
1726 -- The expression must be type-checked even though no component
1727 -- of the aggregate will have this value. This is done only for
1728 -- actual components of the array, not for subaggregates. Do
1729 -- the check on a copy, because the expression may be shared
1730 -- among several choices, some of which might be non-null.
1732 if Present (Etype (N))
1733 and then Is_Array_Type (Etype (N))
1734 and then No (Next_Index (Index))
1736 Expander_Mode_Save_And_Set (False);
1737 Tcopy := New_Copy_Tree (Expr);
1738 Set_Parent (Tcopy, N);
1739 Analyze_And_Resolve (Tcopy, Component_Type (Etype (N)));
1740 Expander_Mode_Restore;
1746 -- If loop bounds are the same then generate an assignment, unless
1747 -- the parent construct is an Iterated_Component_Association.
1749 elsif Equal (L, H) and then not Is_Iterated_Component then
1750 return Gen_Assign (New_Copy_Tree (L), Expr);
1752 -- If H - L <= 2 then generate a sequence of assignments when we are
1753 -- processing the bottom most aggregate and it contains scalar
1756 elsif No (Next_Index (Index))
1757 and then Scalar_Comp
1758 and then Local_Compile_Time_Known_Value (L)
1759 and then Local_Compile_Time_Known_Value (H)
1760 and then Local_Expr_Value (H) - Local_Expr_Value (L) <= 2
1761 and then not Is_Iterated_Component
1763 Append_List_To (S, Gen_Assign (New_Copy_Tree (L), Expr));
1764 Append_List_To (S, Gen_Assign (Add (1, To => L), Expr));
1766 if Local_Expr_Value (H) - Local_Expr_Value (L) = 2 then
1767 Append_List_To (S, Gen_Assign (Add (2, To => L), Expr));
1773 -- Otherwise construct the loop, starting with the loop index L_J
1775 if Is_Iterated_Component then
1777 Make_Defining_Identifier (Loc,
1778 Chars => (Chars (Defining_Identifier (Parent (Expr)))));
1781 L_J := Make_Temporary (Loc, 'J', L);
1784 -- Construct "L .. H" in Index_Base. We use a qualified expression
1785 -- for the bound to convert to the index base, but we don't need
1786 -- to do that if we already have the base type at hand.
1788 if Etype (L) = Index_Base then
1792 Make_Qualified_Expression (Loc,
1793 Subtype_Mark => Index_Base_Name,
1794 Expression => New_Copy_Tree (L));
1797 if Etype (H) = Index_Base then
1801 Make_Qualified_Expression (Loc,
1802 Subtype_Mark => Index_Base_Name,
1803 Expression => New_Copy_Tree (H));
1811 -- Construct "for L_J in Index_Base range L .. H"
1813 L_Iteration_Scheme :=
1814 Make_Iteration_Scheme
1816 Loop_Parameter_Specification =>
1817 Make_Loop_Parameter_Specification
1819 Defining_Identifier => L_J,
1820 Discrete_Subtype_Definition => L_Range));
1822 -- Construct the statements to execute in the loop body
1825 Gen_Assign (New_Occurrence_Of (L_J, Loc), Expr, In_Loop => True);
1827 -- Construct the final loop
1830 Make_Implicit_Loop_Statement
1832 Identifier => Empty,
1833 Iteration_Scheme => L_Iteration_Scheme,
1834 Statements => L_Body));
1836 -- A small optimization: if the aggregate is initialized with a box
1837 -- and the component type has no initialization procedure, remove the
1838 -- useless empty loop.
1840 if Nkind (First (S)) = N_Loop_Statement
1841 and then Is_Empty_List (Statements (First (S)))
1843 return New_List (Make_Null_Statement (Loc));
1853 -- The code built is
1855 -- W_J : Index_Base := L;
1856 -- while W_J < H loop
1857 -- W_J := Index_Base'Succ (W);
1861 function Gen_While (L, H : Node_Id; Expr : Node_Id) return List_Id is
1865 -- W_J : Base_Type := L;
1867 W_Iteration_Scheme : Node_Id;
1870 W_Index_Succ : Node_Id;
1871 -- Index_Base'Succ (J)
1873 W_Increment : Node_Id;
1874 -- W_J := Index_Base'Succ (W)
1876 W_Body : constant List_Id := New_List;
1877 -- The statements to execute in the loop
1879 S : constant List_Id := New_List;
1880 -- list of statement
1883 -- If loop bounds define an empty range or are equal return null
1885 if Empty_Range (L, H) or else Equal (L, H) then
1886 Append_To (S, Make_Null_Statement (Loc));
1890 -- Build the decl of W_J
1892 W_J := Make_Temporary (Loc, 'J', L);
1894 Make_Object_Declaration
1896 Defining_Identifier => W_J,
1897 Object_Definition => Index_Base_Name,
1900 -- Theoretically we should do a New_Copy_Tree (L) here, but we know
1901 -- that in this particular case L is a fresh Expr generated by
1902 -- Add which we are the only ones to use.
1904 Append_To (S, W_Decl);
1906 -- Construct " while W_J < H"
1908 W_Iteration_Scheme :=
1909 Make_Iteration_Scheme
1911 Condition => Make_Op_Lt
1913 Left_Opnd => New_Occurrence_Of (W_J, Loc),
1914 Right_Opnd => New_Copy_Tree (H)));
1916 -- Construct the statements to execute in the loop body
1919 Make_Attribute_Reference
1921 Prefix => Index_Base_Name,
1922 Attribute_Name => Name_Succ,
1923 Expressions => New_List (New_Occurrence_Of (W_J, Loc)));
1926 Make_OK_Assignment_Statement
1928 Name => New_Occurrence_Of (W_J, Loc),
1929 Expression => W_Index_Succ);
1931 Append_To (W_Body, W_Increment);
1933 Append_List_To (W_Body,
1934 Gen_Assign (New_Occurrence_Of (W_J, Loc), Expr, In_Loop => True));
1936 -- Construct the final loop
1939 Make_Implicit_Loop_Statement
1941 Identifier => Empty,
1942 Iteration_Scheme => W_Iteration_Scheme,
1943 Statements => W_Body));
1948 --------------------
1949 -- Get_Assoc_Expr --
1950 --------------------
1952 function Get_Assoc_Expr (Assoc : Node_Id) return Node_Id is
1953 Typ : constant Entity_Id := Base_Type (Etype (N));
1956 if Box_Present (Assoc) then
1957 if Is_Scalar_Type (Ctype) then
1958 if Present (Default_Aspect_Component_Value (Typ)) then
1959 return Default_Aspect_Component_Value (Typ);
1960 elsif Present (Default_Aspect_Value (Ctype)) then
1961 return Default_Aspect_Value (Ctype);
1971 return Expression (Assoc);
1975 ---------------------
1976 -- Index_Base_Name --
1977 ---------------------
1979 function Index_Base_Name return Node_Id is
1981 return New_Occurrence_Of (Index_Base, Sloc (N));
1982 end Index_Base_Name;
1984 ------------------------------------
1985 -- Local_Compile_Time_Known_Value --
1986 ------------------------------------
1988 function Local_Compile_Time_Known_Value (E : Node_Id) return Boolean is
1990 return Compile_Time_Known_Value (E)
1992 (Nkind (E) = N_Attribute_Reference
1993 and then Attribute_Name (E) = Name_Val
1994 and then Compile_Time_Known_Value (First (Expressions (E))));
1995 end Local_Compile_Time_Known_Value;
1997 ----------------------
1998 -- Local_Expr_Value --
1999 ----------------------
2001 function Local_Expr_Value (E : Node_Id) return Uint is
2003 if Compile_Time_Known_Value (E) then
2004 return Expr_Value (E);
2006 return Expr_Value (First (Expressions (E)));
2008 end Local_Expr_Value;
2012 New_Code : constant List_Id := New_List;
2014 Aggr_L : constant Node_Id := Low_Bound (Aggregate_Bounds (N));
2015 Aggr_H : constant Node_Id := High_Bound (Aggregate_Bounds (N));
2016 -- The aggregate bounds of this specific subaggregate. Note that if the
2017 -- code generated by Build_Array_Aggr_Code is executed then these bounds
2018 -- are OK. Otherwise a Constraint_Error would have been raised.
2020 Aggr_Low : constant Node_Id := Duplicate_Subexpr_No_Checks (Aggr_L);
2021 Aggr_High : constant Node_Id := Duplicate_Subexpr_No_Checks (Aggr_H);
2022 -- After Duplicate_Subexpr these are side-effect free
2031 Nb_Choices : Nat := 0;
2032 Table : Case_Table_Type (1 .. Number_Of_Choices (N));
2033 -- Used to sort all the different choice values
2036 -- Number of elements in the positional aggregate
2038 Others_Assoc : Node_Id := Empty;
2040 -- Start of processing for Build_Array_Aggr_Code
2043 -- First before we start, a special case. if we have a bit packed
2044 -- array represented as a modular type, then clear the value to
2045 -- zero first, to ensure that unused bits are properly cleared.
2050 and then Is_Bit_Packed_Array (Typ)
2051 and then Is_Modular_Integer_Type (Packed_Array_Impl_Type (Typ))
2053 Append_To (New_Code,
2054 Make_Assignment_Statement (Loc,
2055 Name => New_Copy_Tree (Into),
2057 Unchecked_Convert_To (Typ,
2058 Make_Integer_Literal (Loc, Uint_0))));
2061 -- If the component type contains tasks, we need to build a Master
2062 -- entity in the current scope, because it will be needed if build-
2063 -- in-place functions are called in the expanded code.
2065 if Nkind (Parent (N)) = N_Object_Declaration and then Has_Task (Typ) then
2066 Build_Master_Entity (Defining_Identifier (Parent (N)));
2069 -- STEP 1: Process component associations
2071 -- For those associations that may generate a loop, initialize
2072 -- Loop_Actions to collect inserted actions that may be crated.
2074 -- Skip this if no component associations
2076 if No (Expressions (N)) then
2078 -- STEP 1 (a): Sort the discrete choices
2080 Assoc := First (Component_Associations (N));
2081 while Present (Assoc) loop
2082 Choice := First (Choice_List (Assoc));
2083 while Present (Choice) loop
2084 if Nkind (Choice) = N_Others_Choice then
2085 Set_Loop_Actions (Assoc, New_List);
2086 Others_Assoc := Assoc;
2090 Get_Index_Bounds (Choice, Low, High);
2093 Set_Loop_Actions (Assoc, New_List);
2096 Nb_Choices := Nb_Choices + 1;
2098 Table (Nb_Choices) :=
2101 Choice_Node => Get_Assoc_Expr (Assoc));
2109 -- If there is more than one set of choices these must be static
2110 -- and we can therefore sort them. Remember that Nb_Choices does not
2111 -- account for an others choice.
2113 if Nb_Choices > 1 then
2114 Sort_Case_Table (Table);
2117 -- STEP 1 (b): take care of the whole set of discrete choices
2119 for J in 1 .. Nb_Choices loop
2120 Low := Table (J).Choice_Lo;
2121 High := Table (J).Choice_Hi;
2122 Expr := Table (J).Choice_Node;
2123 Append_List (Gen_Loop (Low, High, Expr), To => New_Code);
2126 -- STEP 1 (c): generate the remaining loops to cover others choice
2127 -- We don't need to generate loops over empty gaps, but if there is
2128 -- a single empty range we must analyze the expression for semantics
2130 if Present (Others_Assoc) then
2132 First : Boolean := True;
2135 for J in 0 .. Nb_Choices loop
2139 Low := Add (1, To => Table (J).Choice_Hi);
2142 if J = Nb_Choices then
2145 High := Add (-1, To => Table (J + 1).Choice_Lo);
2148 -- If this is an expansion within an init proc, make
2149 -- sure that discriminant references are replaced by
2150 -- the corresponding discriminal.
2152 if Inside_Init_Proc then
2153 if Is_Entity_Name (Low)
2154 and then Ekind (Entity (Low)) = E_Discriminant
2156 Set_Entity (Low, Discriminal (Entity (Low)));
2159 if Is_Entity_Name (High)
2160 and then Ekind (Entity (High)) = E_Discriminant
2162 Set_Entity (High, Discriminal (Entity (High)));
2167 or else not Empty_Range (Low, High)
2171 (Gen_Loop (Low, High,
2172 Get_Assoc_Expr (Others_Assoc)), To => New_Code);
2178 -- STEP 2: Process positional components
2181 -- STEP 2 (a): Generate the assignments for each positional element
2182 -- Note that here we have to use Aggr_L rather than Aggr_Low because
2183 -- Aggr_L is analyzed and Add wants an analyzed expression.
2185 Expr := First (Expressions (N));
2187 while Present (Expr) loop
2188 Nb_Elements := Nb_Elements + 1;
2189 Append_List (Gen_Assign (Add (Nb_Elements, To => Aggr_L), Expr),
2194 -- STEP 2 (b): Generate final loop if an others choice is present
2195 -- Here Nb_Elements gives the offset of the last positional element.
2197 if Present (Component_Associations (N)) then
2198 Assoc := Last (Component_Associations (N));
2200 -- Ada 2005 (AI-287)
2202 Append_List (Gen_While (Add (Nb_Elements, To => Aggr_L),
2204 Get_Assoc_Expr (Assoc)), -- AI-287
2210 end Build_Array_Aggr_Code;
2212 ----------------------------
2213 -- Build_Record_Aggr_Code --
2214 ----------------------------
2216 function Build_Record_Aggr_Code
2219 Lhs : Node_Id) return List_Id
2221 Loc : constant Source_Ptr := Sloc (N);
2222 L : constant List_Id := New_List;
2223 N_Typ : constant Entity_Id := Etype (N);
2229 Comp_Type : Entity_Id;
2230 Selector : Entity_Id;
2231 Comp_Expr : Node_Id;
2234 -- If this is an internal aggregate, the External_Final_List is an
2235 -- expression for the controller record of the enclosing type.
2237 -- If the current aggregate has several controlled components, this
2238 -- expression will appear in several calls to attach to the finali-
2239 -- zation list, and it must not be shared.
2241 Ancestor_Is_Expression : Boolean := False;
2242 Ancestor_Is_Subtype_Mark : Boolean := False;
2244 Init_Typ : Entity_Id := Empty;
2246 Finalization_Done : Boolean := False;
2247 -- True if Generate_Finalization_Actions has already been called; calls
2248 -- after the first do nothing.
2250 function Ancestor_Discriminant_Value (Disc : Entity_Id) return Node_Id;
2251 -- Returns the value that the given discriminant of an ancestor type
2252 -- should receive (in the absence of a conflict with the value provided
2253 -- by an ancestor part of an extension aggregate).
2255 procedure Check_Ancestor_Discriminants (Anc_Typ : Entity_Id);
2256 -- Check that each of the discriminant values defined by the ancestor
2257 -- part of an extension aggregate match the corresponding values
2258 -- provided by either an association of the aggregate or by the
2259 -- constraint imposed by a parent type (RM95-4.3.2(8)).
2261 function Compatible_Int_Bounds
2262 (Agg_Bounds : Node_Id;
2263 Typ_Bounds : Node_Id) return Boolean;
2264 -- Return true if Agg_Bounds are equal or within Typ_Bounds. It is
2265 -- assumed that both bounds are integer ranges.
2267 procedure Generate_Finalization_Actions;
2268 -- Deal with the various controlled type data structure initializations
2269 -- (but only if it hasn't been done already).
2271 function Get_Constraint_Association (T : Entity_Id) return Node_Id;
2272 -- Returns the first discriminant association in the constraint
2273 -- associated with T, if any, otherwise returns Empty.
2275 function Get_Explicit_Discriminant_Value (D : Entity_Id) return Node_Id;
2276 -- If the ancestor part is an unconstrained type and further ancestors
2277 -- do not provide discriminants for it, check aggregate components for
2278 -- values of the discriminants.
2280 procedure Init_Hidden_Discriminants (Typ : Entity_Id; List : List_Id);
2281 -- If Typ is derived, and constrains discriminants of the parent type,
2282 -- these discriminants are not components of the aggregate, and must be
2283 -- initialized. The assignments are appended to List. The same is done
2284 -- if Typ derives fron an already constrained subtype of a discriminated
2287 procedure Init_Stored_Discriminants;
2288 -- If the type is derived and has inherited discriminants, generate
2289 -- explicit assignments for each, using the store constraint of the
2290 -- type. Note that both visible and stored discriminants must be
2291 -- initialized in case the derived type has some renamed and some
2292 -- constrained discriminants.
2294 procedure Init_Visible_Discriminants;
2295 -- If type has discriminants, retrieve their values from aggregate,
2296 -- and generate explicit assignments for each. This does not include
2297 -- discriminants inherited from ancestor, which are handled above.
2298 -- The type of the aggregate is a subtype created ealier using the
2299 -- given values of the discriminant components of the aggregate.
2301 procedure Initialize_Ctrl_Record_Component
2302 (Rec_Comp : Node_Id;
2303 Comp_Typ : Entity_Id;
2304 Init_Expr : Node_Id;
2306 -- Perform the initialization of controlled record component Rec_Comp.
2307 -- Comp_Typ is the component type. Init_Expr is the initialization
2308 -- expression for the record component. Hook-related declarations are
2309 -- inserted prior to aggregate N using Insert_Action. All remaining
2310 -- generated code is added to list Stmts.
2312 procedure Initialize_Record_Component
2313 (Rec_Comp : Node_Id;
2314 Comp_Typ : Entity_Id;
2315 Init_Expr : Node_Id;
2317 -- Perform the initialization of record component Rec_Comp. Comp_Typ
2318 -- is the component type. Init_Expr is the initialization expression
2319 -- of the record component. All generated code is added to list Stmts.
2321 function Is_Int_Range_Bounds (Bounds : Node_Id) return Boolean;
2322 -- Check whether Bounds is a range node and its lower and higher bounds
2323 -- are integers literals.
2325 function Replace_Type (Expr : Node_Id) return Traverse_Result;
2326 -- If the aggregate contains a self-reference, traverse each expression
2327 -- to replace a possible self-reference with a reference to the proper
2328 -- component of the target of the assignment.
2330 function Rewrite_Discriminant (Expr : Node_Id) return Traverse_Result;
2331 -- If default expression of a component mentions a discriminant of the
2332 -- type, it must be rewritten as the discriminant of the target object.
2334 ---------------------------------
2335 -- Ancestor_Discriminant_Value --
2336 ---------------------------------
2338 function Ancestor_Discriminant_Value (Disc : Entity_Id) return Node_Id is
2340 Assoc_Elmt : Elmt_Id;
2341 Aggr_Comp : Entity_Id;
2342 Corresp_Disc : Entity_Id;
2343 Current_Typ : Entity_Id := Base_Type (Typ);
2344 Parent_Typ : Entity_Id;
2345 Parent_Disc : Entity_Id;
2346 Save_Assoc : Node_Id := Empty;
2349 -- First check any discriminant associations to see if any of them
2350 -- provide a value for the discriminant.
2352 if Present (Discriminant_Specifications (Parent (Current_Typ))) then
2353 Assoc := First (Component_Associations (N));
2354 while Present (Assoc) loop
2355 Aggr_Comp := Entity (First (Choices (Assoc)));
2357 if Ekind (Aggr_Comp) = E_Discriminant then
2358 Save_Assoc := Expression (Assoc);
2360 Corresp_Disc := Corresponding_Discriminant (Aggr_Comp);
2361 while Present (Corresp_Disc) loop
2363 -- If found a corresponding discriminant then return the
2364 -- value given in the aggregate. (Note: this is not
2365 -- correct in the presence of side effects. ???)
2367 if Disc = Corresp_Disc then
2368 return Duplicate_Subexpr (Expression (Assoc));
2371 Corresp_Disc := Corresponding_Discriminant (Corresp_Disc);
2379 -- No match found in aggregate, so chain up parent types to find
2380 -- a constraint that defines the value of the discriminant.
2382 Parent_Typ := Etype (Current_Typ);
2383 while Current_Typ /= Parent_Typ loop
2384 if Has_Discriminants (Parent_Typ)
2385 and then not Has_Unknown_Discriminants (Parent_Typ)
2387 Parent_Disc := First_Discriminant (Parent_Typ);
2389 -- We either get the association from the subtype indication
2390 -- of the type definition itself, or from the discriminant
2391 -- constraint associated with the type entity (which is
2392 -- preferable, but it's not always present ???)
2394 if Is_Empty_Elmt_List (Discriminant_Constraint (Current_Typ))
2396 Assoc := Get_Constraint_Association (Current_Typ);
2397 Assoc_Elmt := No_Elmt;
2400 First_Elmt (Discriminant_Constraint (Current_Typ));
2401 Assoc := Node (Assoc_Elmt);
2404 -- Traverse the discriminants of the parent type looking
2405 -- for one that corresponds.
2407 while Present (Parent_Disc) and then Present (Assoc) loop
2408 Corresp_Disc := Parent_Disc;
2409 while Present (Corresp_Disc)
2410 and then Disc /= Corresp_Disc
2412 Corresp_Disc := Corresponding_Discriminant (Corresp_Disc);
2415 if Disc = Corresp_Disc then
2416 if Nkind (Assoc) = N_Discriminant_Association then
2417 Assoc := Expression (Assoc);
2420 -- If the located association directly denotes
2421 -- a discriminant, then use the value of a saved
2422 -- association of the aggregate. This is an approach
2423 -- used to handle certain cases involving multiple
2424 -- discriminants mapped to a single discriminant of
2425 -- a descendant. It's not clear how to locate the
2426 -- appropriate discriminant value for such cases. ???
2428 if Is_Entity_Name (Assoc)
2429 and then Ekind (Entity (Assoc)) = E_Discriminant
2431 Assoc := Save_Assoc;
2434 return Duplicate_Subexpr (Assoc);
2437 Next_Discriminant (Parent_Disc);
2439 if No (Assoc_Elmt) then
2443 Next_Elmt (Assoc_Elmt);
2445 if Present (Assoc_Elmt) then
2446 Assoc := Node (Assoc_Elmt);
2454 Current_Typ := Parent_Typ;
2455 Parent_Typ := Etype (Current_Typ);
2458 -- In some cases there's no ancestor value to locate (such as
2459 -- when an ancestor part given by an expression defines the
2460 -- discriminant value).
2463 end Ancestor_Discriminant_Value;
2465 ----------------------------------
2466 -- Check_Ancestor_Discriminants --
2467 ----------------------------------
2469 procedure Check_Ancestor_Discriminants (Anc_Typ : Entity_Id) is
2471 Disc_Value : Node_Id;
2475 Discr := First_Discriminant (Base_Type (Anc_Typ));
2476 while Present (Discr) loop
2477 Disc_Value := Ancestor_Discriminant_Value (Discr);
2479 if Present (Disc_Value) then
2480 Cond := Make_Op_Ne (Loc,
2482 Make_Selected_Component (Loc,
2483 Prefix => New_Copy_Tree (Target),
2484 Selector_Name => New_Occurrence_Of (Discr, Loc)),
2485 Right_Opnd => Disc_Value);
2488 Make_Raise_Constraint_Error (Loc,
2490 Reason => CE_Discriminant_Check_Failed));
2493 Next_Discriminant (Discr);
2495 end Check_Ancestor_Discriminants;
2497 ---------------------------
2498 -- Compatible_Int_Bounds --
2499 ---------------------------
2501 function Compatible_Int_Bounds
2502 (Agg_Bounds : Node_Id;
2503 Typ_Bounds : Node_Id) return Boolean
2505 Agg_Lo : constant Uint := Intval (Low_Bound (Agg_Bounds));
2506 Agg_Hi : constant Uint := Intval (High_Bound (Agg_Bounds));
2507 Typ_Lo : constant Uint := Intval (Low_Bound (Typ_Bounds));
2508 Typ_Hi : constant Uint := Intval (High_Bound (Typ_Bounds));
2510 return Typ_Lo <= Agg_Lo and then Agg_Hi <= Typ_Hi;
2511 end Compatible_Int_Bounds;
2513 -----------------------------------
2514 -- Generate_Finalization_Actions --
2515 -----------------------------------
2517 procedure Generate_Finalization_Actions is
2519 -- Do the work only the first time this is called
2521 if Finalization_Done then
2525 Finalization_Done := True;
2527 -- Determine the external finalization list. It is either the
2528 -- finalization list of the outer scope or the one coming from an
2529 -- outer aggregate. When the target is not a temporary, the proper
2530 -- scope is the scope of the target rather than the potentially
2531 -- transient current scope.
2533 if Is_Controlled (Typ) and then Ancestor_Is_Subtype_Mark then
2534 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
2535 Set_Assignment_OK (Ref);
2538 Make_Procedure_Call_Statement (Loc,
2541 (Find_Prim_Op (Init_Typ, Name_Initialize), Loc),
2542 Parameter_Associations => New_List (New_Copy_Tree (Ref))));
2544 end Generate_Finalization_Actions;
2546 --------------------------------
2547 -- Get_Constraint_Association --
2548 --------------------------------
2550 function Get_Constraint_Association (T : Entity_Id) return Node_Id is
2557 -- If type is private, get constraint from full view. This was
2558 -- previously done in an instance context, but is needed whenever
2559 -- the ancestor part has a discriminant, possibly inherited through
2560 -- multiple derivations.
2562 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
2563 Typ := Full_View (Typ);
2566 Indic := Subtype_Indication (Type_Definition (Parent (Typ)));
2568 -- Verify that the subtype indication carries a constraint
2570 if Nkind (Indic) = N_Subtype_Indication
2571 and then Present (Constraint (Indic))
2573 return First (Constraints (Constraint (Indic)));
2577 end Get_Constraint_Association;
2579 -------------------------------------
2580 -- Get_Explicit_Discriminant_Value --
2581 -------------------------------------
2583 function Get_Explicit_Discriminant_Value
2584 (D : Entity_Id) return Node_Id
2591 -- The aggregate has been normalized and all associations have a
2594 Assoc := First (Component_Associations (N));
2595 while Present (Assoc) loop
2596 Choice := First (Choices (Assoc));
2598 if Chars (Choice) = Chars (D) then
2599 Val := Expression (Assoc);
2608 end Get_Explicit_Discriminant_Value;
2610 -------------------------------
2611 -- Init_Hidden_Discriminants --
2612 -------------------------------
2614 procedure Init_Hidden_Discriminants (Typ : Entity_Id; List : List_Id) is
2615 function Is_Completely_Hidden_Discriminant
2616 (Discr : Entity_Id) return Boolean;
2617 -- Determine whether Discr is a completely hidden discriminant of
2620 ---------------------------------------
2621 -- Is_Completely_Hidden_Discriminant --
2622 ---------------------------------------
2624 function Is_Completely_Hidden_Discriminant
2625 (Discr : Entity_Id) return Boolean
2630 -- Use First/Next_Entity as First/Next_Discriminant do not yield
2631 -- completely hidden discriminants.
2633 Item := First_Entity (Typ);
2634 while Present (Item) loop
2635 if Ekind (Item) = E_Discriminant
2636 and then Is_Completely_Hidden (Item)
2637 and then Chars (Original_Record_Component (Item)) =
2647 end Is_Completely_Hidden_Discriminant;
2651 Base_Typ : Entity_Id;
2653 Discr_Constr : Elmt_Id;
2654 Discr_Init : Node_Id;
2655 Discr_Val : Node_Id;
2656 In_Aggr_Type : Boolean;
2657 Par_Typ : Entity_Id;
2659 -- Start of processing for Init_Hidden_Discriminants
2662 -- The constraints on the hidden discriminants, if present, are kept
2663 -- in the Stored_Constraint list of the type itself, or in that of
2664 -- the base type. If not in the constraints of the aggregate itself,
2665 -- we examine ancestors to find discriminants that are not renamed
2666 -- by other discriminants but constrained explicitly.
2668 In_Aggr_Type := True;
2670 Base_Typ := Base_Type (Typ);
2671 while Is_Derived_Type (Base_Typ)
2673 (Present (Stored_Constraint (Base_Typ))
2675 (In_Aggr_Type and then Present (Stored_Constraint (Typ))))
2677 Par_Typ := Etype (Base_Typ);
2679 if not Has_Discriminants (Par_Typ) then
2683 Discr := First_Discriminant (Par_Typ);
2685 -- We know that one of the stored-constraint lists is present
2687 if Present (Stored_Constraint (Base_Typ)) then
2688 Discr_Constr := First_Elmt (Stored_Constraint (Base_Typ));
2690 -- For private extension, stored constraint may be on full view
2692 elsif Is_Private_Type (Base_Typ)
2693 and then Present (Full_View (Base_Typ))
2694 and then Present (Stored_Constraint (Full_View (Base_Typ)))
2697 First_Elmt (Stored_Constraint (Full_View (Base_Typ)));
2700 Discr_Constr := First_Elmt (Stored_Constraint (Typ));
2703 while Present (Discr) and then Present (Discr_Constr) loop
2704 Discr_Val := Node (Discr_Constr);
2706 -- The parent discriminant is renamed in the derived type,
2707 -- nothing to initialize.
2709 -- type Deriv_Typ (Discr : ...)
2710 -- is new Parent_Typ (Discr => Discr);
2712 if Is_Entity_Name (Discr_Val)
2713 and then Ekind (Entity (Discr_Val)) = E_Discriminant
2717 -- When the parent discriminant is constrained at the type
2718 -- extension level, it does not appear in the derived type.
2720 -- type Deriv_Typ (Discr : ...)
2721 -- is new Parent_Typ (Discr => Discr,
2722 -- Hidden_Discr => Expression);
2724 elsif Is_Completely_Hidden_Discriminant (Discr) then
2727 -- Otherwise initialize the discriminant
2731 Make_OK_Assignment_Statement (Loc,
2733 Make_Selected_Component (Loc,
2734 Prefix => New_Copy_Tree (Target),
2735 Selector_Name => New_Occurrence_Of (Discr, Loc)),
2736 Expression => New_Copy_Tree (Discr_Val));
2738 Append_To (List, Discr_Init);
2741 Next_Elmt (Discr_Constr);
2742 Next_Discriminant (Discr);
2745 In_Aggr_Type := False;
2746 Base_Typ := Base_Type (Par_Typ);
2748 end Init_Hidden_Discriminants;
2750 --------------------------------
2751 -- Init_Visible_Discriminants --
2752 --------------------------------
2754 procedure Init_Visible_Discriminants is
2755 Discriminant : Entity_Id;
2756 Discriminant_Value : Node_Id;
2759 Discriminant := First_Discriminant (Typ);
2760 while Present (Discriminant) loop
2762 Make_Selected_Component (Loc,
2763 Prefix => New_Copy_Tree (Target),
2764 Selector_Name => New_Occurrence_Of (Discriminant, Loc));
2766 Discriminant_Value :=
2767 Get_Discriminant_Value
2768 (Discriminant, Typ, Discriminant_Constraint (N_Typ));
2771 Make_OK_Assignment_Statement (Loc,
2773 Expression => New_Copy_Tree (Discriminant_Value));
2775 Append_To (L, Instr);
2777 Next_Discriminant (Discriminant);
2779 end Init_Visible_Discriminants;
2781 -------------------------------
2782 -- Init_Stored_Discriminants --
2783 -------------------------------
2785 procedure Init_Stored_Discriminants is
2786 Discriminant : Entity_Id;
2787 Discriminant_Value : Node_Id;
2790 Discriminant := First_Stored_Discriminant (Typ);
2791 while Present (Discriminant) loop
2793 Make_Selected_Component (Loc,
2794 Prefix => New_Copy_Tree (Target),
2795 Selector_Name => New_Occurrence_Of (Discriminant, Loc));
2797 Discriminant_Value :=
2798 Get_Discriminant_Value
2799 (Discriminant, N_Typ, Discriminant_Constraint (N_Typ));
2802 Make_OK_Assignment_Statement (Loc,
2804 Expression => New_Copy_Tree (Discriminant_Value));
2806 Append_To (L, Instr);
2808 Next_Stored_Discriminant (Discriminant);
2810 end Init_Stored_Discriminants;
2812 --------------------------------------
2813 -- Initialize_Ctrl_Record_Component --
2814 --------------------------------------
2816 procedure Initialize_Ctrl_Record_Component
2817 (Rec_Comp : Node_Id;
2818 Comp_Typ : Entity_Id;
2819 Init_Expr : Node_Id;
2823 Hook_Clear : Node_Id;
2825 In_Place_Expansion : Boolean;
2826 -- Flag set when a nonlimited controlled function call requires
2827 -- in-place expansion.
2830 -- Perform a preliminary analysis and resolution to determine what
2831 -- the initialization expression denotes. Unanalyzed function calls
2832 -- may appear as identifiers or indexed components.
2834 if Nkind_In (Init_Expr, N_Function_Call,
2836 N_Indexed_Component)
2837 and then not Analyzed (Init_Expr)
2839 Preanalyze_And_Resolve (Init_Expr, Comp_Typ);
2842 In_Place_Expansion :=
2843 Nkind (Init_Expr) = N_Function_Call
2844 and then not Is_Build_In_Place_Result_Type (Comp_Typ);
2846 -- The initialization expression is a controlled function call.
2847 -- Perform in-place removal of side effects to avoid creating a
2850 -- This in-place expansion is not performed for limited transient
2851 -- objects because the initialization is already done in place.
2853 if In_Place_Expansion then
2855 -- Suppress the removal of side effects by general analysis
2856 -- because this behavior is emulated here. This avoids the
2857 -- generation of a transient scope, which leads to out-of-order
2858 -- adjustment and finalization.
2860 Set_No_Side_Effect_Removal (Init_Expr);
2862 -- Install all hook-related declarations and prepare the clean up
2863 -- statements. The generated code follows the initialization order
2864 -- of individual components and discriminants, rather than being
2865 -- inserted prior to the aggregate. This ensures that a transient
2866 -- component which mentions a discriminant has proper visibility
2867 -- of the discriminant.
2869 Process_Transient_Component
2871 Comp_Typ => Comp_Typ,
2872 Init_Expr => Init_Expr,
2873 Fin_Call => Fin_Call,
2874 Hook_Clear => Hook_Clear,
2878 -- Use the noncontrolled component initialization circuitry to
2879 -- assign the result of the function call to the record component.
2880 -- This also performs tag adjustment and [deep] adjustment of the
2881 -- record component.
2883 Initialize_Record_Component
2884 (Rec_Comp => Rec_Comp,
2885 Comp_Typ => Comp_Typ,
2886 Init_Expr => Init_Expr,
2889 -- At this point the record component is fully initialized. Complete
2890 -- the processing of the controlled record component by finalizing
2891 -- the transient function result.
2893 if In_Place_Expansion then
2894 Process_Transient_Component_Completion
2897 Fin_Call => Fin_Call,
2898 Hook_Clear => Hook_Clear,
2901 end Initialize_Ctrl_Record_Component;
2903 ---------------------------------
2904 -- Initialize_Record_Component --
2905 ---------------------------------
2907 procedure Initialize_Record_Component
2908 (Rec_Comp : Node_Id;
2909 Comp_Typ : Entity_Id;
2910 Init_Expr : Node_Id;
2913 Exceptions_OK : constant Boolean :=
2914 not Restriction_Active (No_Exception_Propagation);
2916 Finalization_OK : constant Boolean := Needs_Finalization (Comp_Typ);
2918 Full_Typ : constant Entity_Id := Underlying_Type (Comp_Typ);
2920 Blk_Stmts : List_Id;
2921 Init_Stmt : Node_Id;
2924 -- Protect the initialization statements from aborts. Generate:
2928 if Finalization_OK and Abort_Allowed then
2929 if Exceptions_OK then
2930 Blk_Stmts := New_List;
2935 Append_To (Blk_Stmts, Build_Runtime_Call (Loc, RE_Abort_Defer));
2937 -- Otherwise aborts are not allowed. All generated code is added
2938 -- directly to the input list.
2944 -- Initialize the record component. Generate:
2946 -- Rec_Comp := Init_Expr;
2948 -- Note that the initialization expression is NOT replicated because
2949 -- only a single component may be initialized by it.
2952 Make_OK_Assignment_Statement (Loc,
2953 Name => New_Copy_Tree (Rec_Comp),
2954 Expression => Init_Expr);
2955 Set_No_Ctrl_Actions (Init_Stmt);
2957 Append_To (Blk_Stmts, Init_Stmt);
2959 -- Adjust the tag due to a possible view conversion. Generate:
2961 -- Rec_Comp._tag := Full_TypeP;
2963 if Tagged_Type_Expansion and then Is_Tagged_Type (Comp_Typ) then
2964 Append_To (Blk_Stmts,
2965 Make_OK_Assignment_Statement (Loc,
2967 Make_Selected_Component (Loc,
2968 Prefix => New_Copy_Tree (Rec_Comp),
2971 (First_Tag_Component (Full_Typ), Loc)),
2974 Unchecked_Convert_To (RTE (RE_Tag),
2976 (Node (First_Elmt (Access_Disp_Table (Full_Typ))),
2980 -- Adjust the component. Generate:
2982 -- [Deep_]Adjust (Rec_Comp);
2985 and then not Is_Limited_Type (Comp_Typ)
2986 and then not Is_Build_In_Place_Function_Call (Init_Expr)
2990 (Obj_Ref => New_Copy_Tree (Rec_Comp),
2993 -- Guard against a missing [Deep_]Adjust when the component type
2994 -- was not properly frozen.
2996 if Present (Adj_Call) then
2997 Append_To (Blk_Stmts, Adj_Call);
3001 -- Complete the protection of the initialization statements
3003 if Finalization_OK and Abort_Allowed then
3005 -- Wrap the initialization statements in a block to catch a
3006 -- potential exception. Generate:
3010 -- Rec_Comp := Init_Expr;
3011 -- Rec_Comp._tag := Full_TypP;
3012 -- [Deep_]Adjust (Rec_Comp);
3014 -- Abort_Undefer_Direct;
3017 if Exceptions_OK then
3019 Build_Abort_Undefer_Block (Loc,
3023 -- Otherwise exceptions are not propagated. Generate:
3026 -- Rec_Comp := Init_Expr;
3027 -- Rec_Comp._tag := Full_TypP;
3028 -- [Deep_]Adjust (Rec_Comp);
3032 Append_To (Blk_Stmts,
3033 Build_Runtime_Call (Loc, RE_Abort_Undefer));
3036 end Initialize_Record_Component;
3038 -------------------------
3039 -- Is_Int_Range_Bounds --
3040 -------------------------
3042 function Is_Int_Range_Bounds (Bounds : Node_Id) return Boolean is
3044 return Nkind (Bounds) = N_Range
3045 and then Nkind (Low_Bound (Bounds)) = N_Integer_Literal
3046 and then Nkind (High_Bound (Bounds)) = N_Integer_Literal;
3047 end Is_Int_Range_Bounds;
3053 function Replace_Type (Expr : Node_Id) return Traverse_Result is
3055 -- Note regarding the Root_Type test below: Aggregate components for
3056 -- self-referential types include attribute references to the current
3057 -- instance, of the form: Typ'access, etc.. These references are
3058 -- rewritten as references to the target of the aggregate: the
3059 -- left-hand side of an assignment, the entity in a declaration,
3060 -- or a temporary. Without this test, we would improperly extended
3061 -- this rewriting to attribute references whose prefix was not the
3062 -- type of the aggregate.
3064 if Nkind (Expr) = N_Attribute_Reference
3065 and then Is_Entity_Name (Prefix (Expr))
3066 and then Is_Type (Entity (Prefix (Expr)))
3067 and then Root_Type (Etype (N)) = Root_Type (Entity (Prefix (Expr)))
3069 if Is_Entity_Name (Lhs) then
3070 Rewrite (Prefix (Expr), New_Occurrence_Of (Entity (Lhs), Loc));
3074 Make_Attribute_Reference (Loc,
3075 Attribute_Name => Name_Unrestricted_Access,
3076 Prefix => New_Copy_Tree (Lhs)));
3077 Set_Analyzed (Parent (Expr), False);
3084 --------------------------
3085 -- Rewrite_Discriminant --
3086 --------------------------
3088 function Rewrite_Discriminant (Expr : Node_Id) return Traverse_Result is
3090 if Is_Entity_Name (Expr)
3091 and then Present (Entity (Expr))
3092 and then Ekind (Entity (Expr)) = E_In_Parameter
3093 and then Present (Discriminal_Link (Entity (Expr)))
3094 and then Scope (Discriminal_Link (Entity (Expr))) =
3095 Base_Type (Etype (N))
3098 Make_Selected_Component (Loc,
3099 Prefix => New_Copy_Tree (Lhs),
3100 Selector_Name => Make_Identifier (Loc, Chars (Expr))));
3104 end Rewrite_Discriminant;
3106 procedure Replace_Discriminants is
3107 new Traverse_Proc (Rewrite_Discriminant);
3109 procedure Replace_Self_Reference is
3110 new Traverse_Proc (Replace_Type);
3112 -- Start of processing for Build_Record_Aggr_Code
3115 if Has_Self_Reference (N) then
3116 Replace_Self_Reference (N);
3119 -- If the target of the aggregate is class-wide, we must convert it
3120 -- to the actual type of the aggregate, so that the proper components
3121 -- are visible. We know already that the types are compatible.
3123 if Present (Etype (Lhs))
3124 and then Is_Class_Wide_Type (Etype (Lhs))
3126 Target := Unchecked_Convert_To (Typ, Lhs);
3131 -- Deal with the ancestor part of extension aggregates or with the
3132 -- discriminants of the root type.
3134 if Nkind (N) = N_Extension_Aggregate then
3136 Ancestor : constant Node_Id := Ancestor_Part (N);
3141 -- If the ancestor part is a subtype mark "T", we generate
3143 -- init-proc (T (tmp)); if T is constrained and
3144 -- init-proc (S (tmp)); where S applies an appropriate
3145 -- constraint if T is unconstrained
3147 if Is_Entity_Name (Ancestor)
3148 and then Is_Type (Entity (Ancestor))
3150 Ancestor_Is_Subtype_Mark := True;
3152 if Is_Constrained (Entity (Ancestor)) then
3153 Init_Typ := Entity (Ancestor);
3155 -- For an ancestor part given by an unconstrained type mark,
3156 -- create a subtype constrained by appropriate corresponding
3157 -- discriminant values coming from either associations of the
3158 -- aggregate or a constraint on a parent type. The subtype will
3159 -- be used to generate the correct default value for the
3162 elsif Has_Discriminants (Entity (Ancestor)) then
3164 Anc_Typ : constant Entity_Id := Entity (Ancestor);
3165 Anc_Constr : constant List_Id := New_List;
3166 Discrim : Entity_Id;
3167 Disc_Value : Node_Id;
3168 New_Indic : Node_Id;
3169 Subt_Decl : Node_Id;
3172 Discrim := First_Discriminant (Anc_Typ);
3173 while Present (Discrim) loop
3174 Disc_Value := Ancestor_Discriminant_Value (Discrim);
3176 -- If no usable discriminant in ancestors, check
3177 -- whether aggregate has an explicit value for it.
3179 if No (Disc_Value) then
3181 Get_Explicit_Discriminant_Value (Discrim);
3184 Append_To (Anc_Constr, Disc_Value);
3185 Next_Discriminant (Discrim);
3189 Make_Subtype_Indication (Loc,
3190 Subtype_Mark => New_Occurrence_Of (Anc_Typ, Loc),
3192 Make_Index_Or_Discriminant_Constraint (Loc,
3193 Constraints => Anc_Constr));
3195 Init_Typ := Create_Itype (Ekind (Anc_Typ), N);
3198 Make_Subtype_Declaration (Loc,
3199 Defining_Identifier => Init_Typ,
3200 Subtype_Indication => New_Indic);
3202 -- Itypes must be analyzed with checks off Declaration
3203 -- must have a parent for proper handling of subsidiary
3206 Set_Parent (Subt_Decl, N);
3207 Analyze (Subt_Decl, Suppress => All_Checks);
3211 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
3212 Set_Assignment_OK (Ref);
3214 if not Is_Interface (Init_Typ) then
3216 Build_Initialization_Call (Loc,
3219 In_Init_Proc => Within_Init_Proc,
3220 With_Default_Init => Has_Default_Init_Comps (N)
3222 Has_Task (Base_Type (Init_Typ))));
3224 if Is_Constrained (Entity (Ancestor))
3225 and then Has_Discriminants (Entity (Ancestor))
3227 Check_Ancestor_Discriminants (Entity (Ancestor));
3231 -- Handle calls to C++ constructors
3233 elsif Is_CPP_Constructor_Call (Ancestor) then
3234 Init_Typ := Etype (Ancestor);
3235 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
3236 Set_Assignment_OK (Ref);
3239 Build_Initialization_Call (Loc,
3242 In_Init_Proc => Within_Init_Proc,
3243 With_Default_Init => Has_Default_Init_Comps (N),
3244 Constructor_Ref => Ancestor));
3246 -- Ada 2005 (AI-287): If the ancestor part is an aggregate of
3247 -- limited type, a recursive call expands the ancestor. Note that
3248 -- in the limited case, the ancestor part must be either a
3249 -- function call (possibly qualified) or aggregate (definitely
3252 elsif Is_Limited_Type (Etype (Ancestor))
3253 and then Nkind_In (Unqualify (Ancestor), N_Aggregate,
3254 N_Extension_Aggregate)
3256 Ancestor_Is_Expression := True;
3258 -- Set up finalization data for enclosing record, because
3259 -- controlled subcomponents of the ancestor part will be
3262 Generate_Finalization_Actions;
3265 Build_Record_Aggr_Code
3266 (N => Unqualify (Ancestor),
3267 Typ => Etype (Unqualify (Ancestor)),
3270 -- If the ancestor part is an expression "E", we generate
3274 -- In Ada 2005, this includes the case of a (possibly qualified)
3275 -- limited function call. The assignment will turn into a
3276 -- build-in-place function call (for further details, see
3277 -- Make_Build_In_Place_Call_In_Assignment).
3280 Ancestor_Is_Expression := True;
3281 Init_Typ := Etype (Ancestor);
3283 -- If the ancestor part is an aggregate, force its full
3284 -- expansion, which was delayed.
3286 if Nkind_In (Unqualify (Ancestor), N_Aggregate,
3287 N_Extension_Aggregate)
3289 Set_Analyzed (Ancestor, False);
3290 Set_Analyzed (Expression (Ancestor), False);
3293 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
3294 Set_Assignment_OK (Ref);
3296 -- Make the assignment without usual controlled actions, since
3297 -- we only want to Adjust afterwards, but not to Finalize
3298 -- beforehand. Add manual Adjust when necessary.
3300 Assign := New_List (
3301 Make_OK_Assignment_Statement (Loc,
3303 Expression => Ancestor));
3304 Set_No_Ctrl_Actions (First (Assign));
3306 -- Assign the tag now to make sure that the dispatching call in
3307 -- the subsequent deep_adjust works properly (unless
3308 -- Tagged_Type_Expansion where tags are implicit).
3310 if Tagged_Type_Expansion then
3312 Make_OK_Assignment_Statement (Loc,
3314 Make_Selected_Component (Loc,
3315 Prefix => New_Copy_Tree (Target),
3318 (First_Tag_Component (Base_Type (Typ)), Loc)),
3321 Unchecked_Convert_To (RTE (RE_Tag),
3324 (Access_Disp_Table (Base_Type (Typ)))),
3327 Set_Assignment_OK (Name (Instr));
3328 Append_To (Assign, Instr);
3330 -- Ada 2005 (AI-251): If tagged type has progenitors we must
3331 -- also initialize tags of the secondary dispatch tables.
3333 if Has_Interfaces (Base_Type (Typ)) then
3335 (Typ => Base_Type (Typ),
3337 Stmts_List => Assign,
3338 Init_Tags_List => Assign);
3342 -- Call Adjust manually
3344 if Needs_Finalization (Etype (Ancestor))
3345 and then not Is_Limited_Type (Etype (Ancestor))
3346 and then not Is_Build_In_Place_Function_Call (Ancestor)
3350 (Obj_Ref => New_Copy_Tree (Ref),
3351 Typ => Etype (Ancestor));
3353 -- Guard against a missing [Deep_]Adjust when the ancestor
3354 -- type was not properly frozen.
3356 if Present (Adj_Call) then
3357 Append_To (Assign, Adj_Call);
3362 Make_Unsuppress_Block (Loc, Name_Discriminant_Check, Assign));
3364 if Has_Discriminants (Init_Typ) then
3365 Check_Ancestor_Discriminants (Init_Typ);
3369 pragma Assert (Nkind (N) = N_Extension_Aggregate);
3371 (not (Ancestor_Is_Expression and Ancestor_Is_Subtype_Mark));
3374 -- Generate assignments of hidden discriminants. If the base type is
3375 -- an unchecked union, the discriminants are unknown to the back-end
3376 -- and absent from a value of the type, so assignments for them are
3379 if Has_Discriminants (Typ)
3380 and then not Is_Unchecked_Union (Base_Type (Typ))
3382 Init_Hidden_Discriminants (Typ, L);
3385 -- Normal case (not an extension aggregate)
3388 -- Generate the discriminant expressions, component by component.
3389 -- If the base type is an unchecked union, the discriminants are
3390 -- unknown to the back-end and absent from a value of the type, so
3391 -- assignments for them are not emitted.
3393 if Has_Discriminants (Typ)
3394 and then not Is_Unchecked_Union (Base_Type (Typ))
3396 Init_Hidden_Discriminants (Typ, L);
3398 -- Generate discriminant init values for the visible discriminants
3400 Init_Visible_Discriminants;
3402 if Is_Derived_Type (N_Typ) then
3403 Init_Stored_Discriminants;
3408 -- For CPP types we generate an implicit call to the C++ default
3409 -- constructor to ensure the proper initialization of the _Tag
3412 if Is_CPP_Class (Root_Type (Typ)) and then CPP_Num_Prims (Typ) > 0 then
3413 Invoke_Constructor : declare
3414 CPP_Parent : constant Entity_Id := Enclosing_CPP_Parent (Typ);
3416 procedure Invoke_IC_Proc (T : Entity_Id);
3417 -- Recursive routine used to climb to parents. Required because
3418 -- parents must be initialized before descendants to ensure
3419 -- propagation of inherited C++ slots.
3421 --------------------
3422 -- Invoke_IC_Proc --
3423 --------------------
3425 procedure Invoke_IC_Proc (T : Entity_Id) is
3427 -- Avoid generating extra calls. Initialization required
3428 -- only for types defined from the level of derivation of
3429 -- type of the constructor and the type of the aggregate.
3431 if T = CPP_Parent then
3435 Invoke_IC_Proc (Etype (T));
3437 -- Generate call to the IC routine
3439 if Present (CPP_Init_Proc (T)) then
3441 Make_Procedure_Call_Statement (Loc,
3442 Name => New_Occurrence_Of (CPP_Init_Proc (T), Loc)));
3446 -- Start of processing for Invoke_Constructor
3449 -- Implicit invocation of the C++ constructor
3451 if Nkind (N) = N_Aggregate then
3453 Make_Procedure_Call_Statement (Loc,
3455 New_Occurrence_Of (Base_Init_Proc (CPP_Parent), Loc),
3456 Parameter_Associations => New_List (
3457 Unchecked_Convert_To (CPP_Parent,
3458 New_Copy_Tree (Lhs)))));
3461 Invoke_IC_Proc (Typ);
3462 end Invoke_Constructor;
3465 -- Generate the assignments, component by component
3467 -- tmp.comp1 := Expr1_From_Aggr;
3468 -- tmp.comp2 := Expr2_From_Aggr;
3471 Comp := First (Component_Associations (N));
3472 while Present (Comp) loop
3473 Selector := Entity (First (Choices (Comp)));
3477 if Is_CPP_Constructor_Call (Expression (Comp)) then
3479 Build_Initialization_Call (Loc,
3481 Make_Selected_Component (Loc,
3482 Prefix => New_Copy_Tree (Target),
3483 Selector_Name => New_Occurrence_Of (Selector, Loc)),
3484 Typ => Etype (Selector),
3486 With_Default_Init => True,
3487 Constructor_Ref => Expression (Comp)));
3489 -- Ada 2005 (AI-287): For each default-initialized component generate
3490 -- a call to the corresponding IP subprogram if available.
3492 elsif Box_Present (Comp)
3493 and then Has_Non_Null_Base_Init_Proc (Etype (Selector))
3495 if Ekind (Selector) /= E_Discriminant then
3496 Generate_Finalization_Actions;
3499 -- Ada 2005 (AI-287): If the component type has tasks then
3500 -- generate the activation chain and master entities (except
3501 -- in case of an allocator because in that case these entities
3502 -- are generated by Build_Task_Allocate_Block_With_Init_Stmts).
3505 Ctype : constant Entity_Id := Etype (Selector);
3506 Inside_Allocator : Boolean := False;
3507 P : Node_Id := Parent (N);
3510 if Is_Task_Type (Ctype) or else Has_Task (Ctype) then
3511 while Present (P) loop
3512 if Nkind (P) = N_Allocator then
3513 Inside_Allocator := True;
3520 if not Inside_Init_Proc and not Inside_Allocator then
3521 Build_Activation_Chain_Entity (N);
3527 Build_Initialization_Call (Loc,
3528 Id_Ref => Make_Selected_Component (Loc,
3529 Prefix => New_Copy_Tree (Target),
3531 New_Occurrence_Of (Selector, Loc)),
3532 Typ => Etype (Selector),
3534 With_Default_Init => True));
3536 -- Prepare for component assignment
3538 elsif Ekind (Selector) /= E_Discriminant
3539 or else Nkind (N) = N_Extension_Aggregate
3541 -- All the discriminants have now been assigned
3543 -- This is now a good moment to initialize and attach all the
3544 -- controllers. Their position may depend on the discriminants.
3546 if Ekind (Selector) /= E_Discriminant then
3547 Generate_Finalization_Actions;
3550 Comp_Type := Underlying_Type (Etype (Selector));
3552 Make_Selected_Component (Loc,
3553 Prefix => New_Copy_Tree (Target),
3554 Selector_Name => New_Occurrence_Of (Selector, Loc));
3556 if Nkind (Expression (Comp)) = N_Qualified_Expression then
3557 Expr_Q := Expression (Expression (Comp));
3559 Expr_Q := Expression (Comp);
3562 -- Now either create the assignment or generate the code for the
3563 -- inner aggregate top-down.
3565 if Is_Delayed_Aggregate (Expr_Q) then
3567 -- We have the following case of aggregate nesting inside
3568 -- an object declaration:
3570 -- type Arr_Typ is array (Integer range <>) of ...;
3572 -- type Rec_Typ (...) is record
3573 -- Obj_Arr_Typ : Arr_Typ (A .. B);
3576 -- Obj_Rec_Typ : Rec_Typ := (...,
3577 -- Obj_Arr_Typ => (X => (...), Y => (...)));
3579 -- The length of the ranges of the aggregate and Obj_Add_Typ
3580 -- are equal (B - A = Y - X), but they do not coincide (X /=
3581 -- A and B /= Y). This case requires array sliding which is
3582 -- performed in the following manner:
3584 -- subtype Arr_Sub is Arr_Typ (X .. Y);
3586 -- Temp (X) := (...);
3588 -- Temp (Y) := (...);
3589 -- Obj_Rec_Typ.Obj_Arr_Typ := Temp;
3591 if Ekind (Comp_Type) = E_Array_Subtype
3592 and then Is_Int_Range_Bounds (Aggregate_Bounds (Expr_Q))
3593 and then Is_Int_Range_Bounds (First_Index (Comp_Type))
3595 Compatible_Int_Bounds
3596 (Agg_Bounds => Aggregate_Bounds (Expr_Q),
3597 Typ_Bounds => First_Index (Comp_Type))
3599 -- Create the array subtype with bounds equal to those of
3600 -- the corresponding aggregate.
3603 SubE : constant Entity_Id := Make_Temporary (Loc, 'T');
3605 SubD : constant Node_Id :=
3606 Make_Subtype_Declaration (Loc,
3607 Defining_Identifier => SubE,
3608 Subtype_Indication =>
3609 Make_Subtype_Indication (Loc,
3611 New_Occurrence_Of (Etype (Comp_Type), Loc),
3613 Make_Index_Or_Discriminant_Constraint
3615 Constraints => New_List (
3617 (Aggregate_Bounds (Expr_Q))))));
3619 -- Create a temporary array of the above subtype which
3620 -- will be used to capture the aggregate assignments.
3622 TmpE : constant Entity_Id := Make_Temporary (Loc, 'A', N);
3624 TmpD : constant Node_Id :=
3625 Make_Object_Declaration (Loc,
3626 Defining_Identifier => TmpE,
3627 Object_Definition => New_Occurrence_Of (SubE, Loc));
3630 Set_No_Initialization (TmpD);
3631 Append_To (L, SubD);
3632 Append_To (L, TmpD);
3634 -- Expand aggregate into assignments to the temp array
3637 Late_Expansion (Expr_Q, Comp_Type,
3638 New_Occurrence_Of (TmpE, Loc)));
3643 Make_Assignment_Statement (Loc,
3644 Name => New_Copy_Tree (Comp_Expr),
3645 Expression => New_Occurrence_Of (TmpE, Loc)));
3648 -- Normal case (sliding not required)
3652 Late_Expansion (Expr_Q, Comp_Type, Comp_Expr));
3655 -- Expr_Q is not delayed aggregate
3658 if Has_Discriminants (Typ) then
3659 Replace_Discriminants (Expr_Q);
3661 -- If the component is an array type that depends on
3662 -- discriminants, and the expression is a single Others
3663 -- clause, create an explicit subtype for it because the
3664 -- backend has troubles recovering the actual bounds.
3666 if Nkind (Expr_Q) = N_Aggregate
3667 and then Is_Array_Type (Comp_Type)
3668 and then Present (Component_Associations (Expr_Q))
3671 Assoc : constant Node_Id :=
3672 First (Component_Associations (Expr_Q));
3676 if Nkind (First (Choices (Assoc))) = N_Others_Choice
3679 Build_Actual_Subtype_Of_Component
3680 (Comp_Type, Comp_Expr);
3682 -- If the component type does not in fact depend on
3683 -- discriminants, the subtype declaration is empty.
3685 if Present (Decl) then
3686 Append_To (L, Decl);
3687 Set_Etype (Comp_Expr, Defining_Entity (Decl));
3694 if Modify_Tree_For_C
3695 and then Nkind (Expr_Q) = N_Aggregate
3696 and then Is_Array_Type (Etype (Expr_Q))
3697 and then Present (First_Index (Etype (Expr_Q)))
3700 Expr_Q_Type : constant Node_Id := Etype (Expr_Q);
3703 Build_Array_Aggr_Code
3705 Ctype => Component_Type (Expr_Q_Type),
3706 Index => First_Index (Expr_Q_Type),
3709 Is_Scalar_Type (Component_Type (Expr_Q_Type))));
3713 -- Handle an initialization expression of a controlled type
3714 -- in case it denotes a function call. In general such a
3715 -- scenario will produce a transient scope, but this will
3716 -- lead to wrong order of initialization, adjustment, and
3717 -- finalization in the context of aggregates.
3719 -- Target.Comp := Ctrl_Func_Call;
3722 -- Trans_Obj : ... := Ctrl_Func_Call; -- object
3723 -- Target.Comp := Trans_Obj;
3724 -- Finalize (Trans_Obj);
3726 -- Target.Comp._tag := ...;
3727 -- Adjust (Target.Comp);
3729 -- In the example above, the call to Finalize occurs too
3730 -- early and as a result it may leave the record component
3731 -- in a bad state. Finalization of the transient object
3732 -- should really happen after adjustment.
3734 -- To avoid this scenario, perform in-place side-effect
3735 -- removal of the function call. This eliminates the
3736 -- transient property of the function result and ensures
3737 -- correct order of actions.
3739 -- Res : ... := Ctrl_Func_Call;
3740 -- Target.Comp := Res;
3741 -- Target.Comp._tag := ...;
3742 -- Adjust (Target.Comp);
3745 if Needs_Finalization (Comp_Type)
3746 and then Nkind (Expr_Q) /= N_Aggregate
3748 Initialize_Ctrl_Record_Component
3749 (Rec_Comp => Comp_Expr,
3750 Comp_Typ => Etype (Selector),
3751 Init_Expr => Expr_Q,
3754 -- Otherwise perform single component initialization
3757 Initialize_Record_Component
3758 (Rec_Comp => Comp_Expr,
3759 Comp_Typ => Etype (Selector),
3760 Init_Expr => Expr_Q,
3766 -- comment would be good here ???
3768 elsif Ekind (Selector) = E_Discriminant
3769 and then Nkind (N) /= N_Extension_Aggregate
3770 and then Nkind (Parent (N)) = N_Component_Association
3771 and then Is_Constrained (Typ)
3773 -- We must check that the discriminant value imposed by the
3774 -- context is the same as the value given in the subaggregate,
3775 -- because after the expansion into assignments there is no
3776 -- record on which to perform a regular discriminant check.
3783 D_Val := First_Elmt (Discriminant_Constraint (Typ));
3784 Disc := First_Discriminant (Typ);
3785 while Chars (Disc) /= Chars (Selector) loop
3786 Next_Discriminant (Disc);
3790 pragma Assert (Present (D_Val));
3792 -- This check cannot performed for components that are
3793 -- constrained by a current instance, because this is not a
3794 -- value that can be compared with the actual constraint.
3796 if Nkind (Node (D_Val)) /= N_Attribute_Reference
3797 or else not Is_Entity_Name (Prefix (Node (D_Val)))
3798 or else not Is_Type (Entity (Prefix (Node (D_Val))))
3801 Make_Raise_Constraint_Error (Loc,
3804 Left_Opnd => New_Copy_Tree (Node (D_Val)),
3805 Right_Opnd => Expression (Comp)),
3806 Reason => CE_Discriminant_Check_Failed));
3809 -- Find self-reference in previous discriminant assignment,
3810 -- and replace with proper expression.
3817 while Present (Ass) loop
3818 if Nkind (Ass) = N_Assignment_Statement
3819 and then Nkind (Name (Ass)) = N_Selected_Component
3820 and then Chars (Selector_Name (Name (Ass))) =
3824 (Ass, New_Copy_Tree (Expression (Comp)));
3837 -- If the type is tagged, the tag needs to be initialized (unless we
3838 -- are in VM-mode where tags are implicit). It is done late in the
3839 -- initialization process because in some cases, we call the init
3840 -- proc of an ancestor which will not leave out the right tag.
3842 if Ancestor_Is_Expression then
3845 -- For CPP types we generated a call to the C++ default constructor
3846 -- before the components have been initialized to ensure the proper
3847 -- initialization of the _Tag component (see above).
3849 elsif Is_CPP_Class (Typ) then
3852 elsif Is_Tagged_Type (Typ) and then Tagged_Type_Expansion then
3854 Make_OK_Assignment_Statement (Loc,
3856 Make_Selected_Component (Loc,
3857 Prefix => New_Copy_Tree (Target),
3860 (First_Tag_Component (Base_Type (Typ)), Loc)),
3863 Unchecked_Convert_To (RTE (RE_Tag),
3865 (Node (First_Elmt (Access_Disp_Table (Base_Type (Typ)))),
3868 Append_To (L, Instr);
3870 -- Ada 2005 (AI-251): If the tagged type has been derived from an
3871 -- abstract interfaces we must also initialize the tags of the
3872 -- secondary dispatch tables.
3874 if Has_Interfaces (Base_Type (Typ)) then
3876 (Typ => Base_Type (Typ),
3879 Init_Tags_List => L);
3883 -- If the controllers have not been initialized yet (by lack of non-
3884 -- discriminant components), let's do it now.
3886 Generate_Finalization_Actions;
3889 end Build_Record_Aggr_Code;
3891 ---------------------------------------
3892 -- Collect_Initialization_Statements --
3893 ---------------------------------------
3895 procedure Collect_Initialization_Statements
3898 Node_After : Node_Id)
3900 Loc : constant Source_Ptr := Sloc (N);
3901 Init_Actions : constant List_Id := New_List;
3902 Init_Node : Node_Id;
3903 Comp_Stmt : Node_Id;
3906 -- Nothing to do if Obj is already frozen, as in this case we known we
3907 -- won't need to move the initialization statements about later on.
3909 if Is_Frozen (Obj) then
3914 while Next (Init_Node) /= Node_After loop
3915 Append_To (Init_Actions, Remove_Next (Init_Node));
3918 if not Is_Empty_List (Init_Actions) then
3919 Comp_Stmt := Make_Compound_Statement (Loc, Actions => Init_Actions);
3920 Insert_Action_After (Init_Node, Comp_Stmt);
3921 Set_Initialization_Statements (Obj, Comp_Stmt);
3923 end Collect_Initialization_Statements;
3925 -------------------------------
3926 -- Convert_Aggr_In_Allocator --
3927 -------------------------------
3929 procedure Convert_Aggr_In_Allocator
3934 Loc : constant Source_Ptr := Sloc (Aggr);
3935 Typ : constant Entity_Id := Etype (Aggr);
3936 Temp : constant Entity_Id := Defining_Identifier (Decl);
3938 Occ : constant Node_Id :=
3939 Unchecked_Convert_To (Typ,
3940 Make_Explicit_Dereference (Loc, New_Occurrence_Of (Temp, Loc)));
3943 if Is_Array_Type (Typ) then
3944 Convert_Array_Aggr_In_Allocator (Decl, Aggr, Occ);
3946 elsif Has_Default_Init_Comps (Aggr) then
3948 L : constant List_Id := New_List;
3949 Init_Stmts : List_Id;
3952 Init_Stmts := Late_Expansion (Aggr, Typ, Occ);
3954 if Has_Task (Typ) then
3955 Build_Task_Allocate_Block_With_Init_Stmts (L, Aggr, Init_Stmts);
3956 Insert_Actions (Alloc, L);
3958 Insert_Actions (Alloc, Init_Stmts);
3963 Insert_Actions (Alloc, Late_Expansion (Aggr, Typ, Occ));
3965 end Convert_Aggr_In_Allocator;
3967 --------------------------------
3968 -- Convert_Aggr_In_Assignment --
3969 --------------------------------
3971 procedure Convert_Aggr_In_Assignment (N : Node_Id) is
3972 Aggr : Node_Id := Expression (N);
3973 Typ : constant Entity_Id := Etype (Aggr);
3974 Occ : constant Node_Id := New_Copy_Tree (Name (N));
3977 if Nkind (Aggr) = N_Qualified_Expression then
3978 Aggr := Expression (Aggr);
3981 Insert_Actions_After (N, Late_Expansion (Aggr, Typ, Occ));
3982 end Convert_Aggr_In_Assignment;
3984 ---------------------------------
3985 -- Convert_Aggr_In_Object_Decl --
3986 ---------------------------------
3988 procedure Convert_Aggr_In_Object_Decl (N : Node_Id) is
3989 Obj : constant Entity_Id := Defining_Identifier (N);
3990 Aggr : Node_Id := Expression (N);
3991 Loc : constant Source_Ptr := Sloc (Aggr);
3992 Typ : constant Entity_Id := Etype (Aggr);
3993 Occ : constant Node_Id := New_Occurrence_Of (Obj, Loc);
3995 function Discriminants_Ok return Boolean;
3996 -- If the object type is constrained, the discriminants in the
3997 -- aggregate must be checked against the discriminants of the subtype.
3998 -- This cannot be done using Apply_Discriminant_Checks because after
3999 -- expansion there is no aggregate left to check.
4001 ----------------------
4002 -- Discriminants_Ok --
4003 ----------------------
4005 function Discriminants_Ok return Boolean is
4006 Cond : Node_Id := Empty;
4015 D := First_Discriminant (Typ);
4016 Disc1 := First_Elmt (Discriminant_Constraint (Typ));
4017 Disc2 := First_Elmt (Discriminant_Constraint (Etype (Obj)));
4018 while Present (Disc1) and then Present (Disc2) loop
4019 Val1 := Node (Disc1);
4020 Val2 := Node (Disc2);
4022 if not Is_OK_Static_Expression (Val1)
4023 or else not Is_OK_Static_Expression (Val2)
4025 Check := Make_Op_Ne (Loc,
4026 Left_Opnd => Duplicate_Subexpr (Val1),
4027 Right_Opnd => Duplicate_Subexpr (Val2));
4033 Cond := Make_Or_Else (Loc,
4035 Right_Opnd => Check);
4038 elsif Expr_Value (Val1) /= Expr_Value (Val2) then
4039 Apply_Compile_Time_Constraint_Error (Aggr,
4040 Msg => "incorrect value for discriminant&??",
4041 Reason => CE_Discriminant_Check_Failed,
4046 Next_Discriminant (D);
4051 -- If any discriminant constraint is nonstatic, emit a check
4053 if Present (Cond) then
4055 Make_Raise_Constraint_Error (Loc,
4057 Reason => CE_Discriminant_Check_Failed));
4061 end Discriminants_Ok;
4063 -- Start of processing for Convert_Aggr_In_Object_Decl
4066 Set_Assignment_OK (Occ);
4068 if Nkind (Aggr) = N_Qualified_Expression then
4069 Aggr := Expression (Aggr);
4072 if Has_Discriminants (Typ)
4073 and then Typ /= Etype (Obj)
4074 and then Is_Constrained (Etype (Obj))
4075 and then not Discriminants_Ok
4080 -- If the context is an extended return statement, it has its own
4081 -- finalization machinery (i.e. works like a transient scope) and
4082 -- we do not want to create an additional one, because objects on
4083 -- the finalization list of the return must be moved to the caller's
4084 -- finalization list to complete the return.
4086 -- However, if the aggregate is limited, it is built in place, and the
4087 -- controlled components are not assigned to intermediate temporaries
4088 -- so there is no need for a transient scope in this case either.
4090 if Requires_Transient_Scope (Typ)
4091 and then Ekind (Current_Scope) /= E_Return_Statement
4092 and then not Is_Limited_Type (Typ)
4094 Establish_Transient_Scope (Aggr, Manage_Sec_Stack => False);
4098 Node_After : constant Node_Id := Next (N);
4100 Insert_Actions_After (N, Late_Expansion (Aggr, Typ, Occ));
4101 Collect_Initialization_Statements (Obj, N, Node_After);
4104 Set_No_Initialization (N);
4105 Initialize_Discriminants (N, Typ);
4106 end Convert_Aggr_In_Object_Decl;
4108 -------------------------------------
4109 -- Convert_Array_Aggr_In_Allocator --
4110 -------------------------------------
4112 procedure Convert_Array_Aggr_In_Allocator
4117 Aggr_Code : List_Id;
4118 Typ : constant Entity_Id := Etype (Aggr);
4119 Ctyp : constant Entity_Id := Component_Type (Typ);
4122 -- The target is an explicit dereference of the allocated object.
4123 -- Generate component assignments to it, as for an aggregate that
4124 -- appears on the right-hand side of an assignment statement.
4127 Build_Array_Aggr_Code (Aggr,
4129 Index => First_Index (Typ),
4131 Scalar_Comp => Is_Scalar_Type (Ctyp));
4133 Insert_Actions_After (Decl, Aggr_Code);
4134 end Convert_Array_Aggr_In_Allocator;
4136 ----------------------------
4137 -- Convert_To_Assignments --
4138 ----------------------------
4140 procedure Convert_To_Assignments (N : Node_Id; Typ : Entity_Id) is
4141 Loc : constant Source_Ptr := Sloc (N);
4145 Aggr_Code : List_Id;
4147 Target_Expr : Node_Id;
4148 Parent_Kind : Node_Kind;
4149 Unc_Decl : Boolean := False;
4150 Parent_Node : Node_Id;
4153 pragma Assert (Nkind_In (N, N_Aggregate, N_Extension_Aggregate));
4154 pragma Assert (not Is_Static_Dispatch_Table_Aggregate (N));
4155 pragma Assert (Is_Record_Type (Typ));
4157 Parent_Node := Parent (N);
4158 Parent_Kind := Nkind (Parent_Node);
4160 if Parent_Kind = N_Qualified_Expression then
4161 -- Check if we are in an unconstrained declaration because in this
4162 -- case the current delayed expansion mechanism doesn't work when
4163 -- the declared object size depends on the initializing expr.
4165 Parent_Node := Parent (Parent_Node);
4166 Parent_Kind := Nkind (Parent_Node);
4168 if Parent_Kind = N_Object_Declaration then
4170 not Is_Entity_Name (Object_Definition (Parent_Node))
4171 or else (Nkind (N) = N_Aggregate
4174 (Entity (Object_Definition (Parent_Node))))
4175 or else Is_Class_Wide_Type
4176 (Entity (Object_Definition (Parent_Node)));
4180 -- Just set the Delay flag in the cases where the transformation will be
4181 -- done top down from above.
4185 -- Internal aggregate (transformed when expanding the parent)
4187 or else Parent_Kind = N_Aggregate
4188 or else Parent_Kind = N_Extension_Aggregate
4189 or else Parent_Kind = N_Component_Association
4191 -- Allocator (see Convert_Aggr_In_Allocator)
4193 or else Parent_Kind = N_Allocator
4195 -- Object declaration (see Convert_Aggr_In_Object_Decl)
4197 or else (Parent_Kind = N_Object_Declaration and then not Unc_Decl)
4199 -- Safe assignment (see Convert_Aggr_Assignments). So far only the
4200 -- assignments in init procs are taken into account.
4202 or else (Parent_Kind = N_Assignment_Statement
4203 and then Inside_Init_Proc)
4205 -- (Ada 2005) An inherently limited type in a return statement, which
4206 -- will be handled in a build-in-place fashion, and may be rewritten
4207 -- as an extended return and have its own finalization machinery.
4208 -- In the case of a simple return, the aggregate needs to be delayed
4209 -- until the scope for the return statement has been created, so
4210 -- that any finalization chain will be associated with that scope.
4211 -- For extended returns, we delay expansion to avoid the creation
4212 -- of an unwanted transient scope that could result in premature
4213 -- finalization of the return object (which is built in place
4214 -- within the caller's scope).
4216 or else Is_Build_In_Place_Aggregate_Return (N)
4218 Set_Expansion_Delayed (N);
4222 -- Otherwise, if a transient scope is required, create it now. If we
4223 -- are within an initialization procedure do not create such, because
4224 -- the target of the assignment must not be declared within a local
4225 -- block, and because cleanup will take place on return from the
4226 -- initialization procedure.
4228 -- Should the condition be more restrictive ???
4230 if Requires_Transient_Scope (Typ) and then not Inside_Init_Proc then
4231 Establish_Transient_Scope (N, Manage_Sec_Stack => False);
4234 -- If the aggregate is nonlimited, create a temporary. If it is limited
4235 -- and context is an assignment, this is a subaggregate for an enclosing
4236 -- aggregate being expanded. It must be built in place, so use target of
4237 -- the current assignment.
4239 if Is_Limited_Type (Typ)
4240 and then Nkind (Parent (N)) = N_Assignment_Statement
4242 Target_Expr := New_Copy_Tree (Name (Parent (N)));
4243 Insert_Actions (Parent (N),
4244 Build_Record_Aggr_Code (N, Typ, Target_Expr));
4245 Rewrite (Parent (N), Make_Null_Statement (Loc));
4248 Temp := Make_Temporary (Loc, 'A', N);
4250 -- If the type inherits unknown discriminants, use the view with
4251 -- known discriminants if available.
4253 if Has_Unknown_Discriminants (Typ)
4254 and then Present (Underlying_Record_View (Typ))
4256 T := Underlying_Record_View (Typ);
4262 Make_Object_Declaration (Loc,
4263 Defining_Identifier => Temp,
4264 Object_Definition => New_Occurrence_Of (T, Loc));
4266 Set_No_Initialization (Instr);
4267 Insert_Action (N, Instr);
4268 Initialize_Discriminants (Instr, T);
4270 Target_Expr := New_Occurrence_Of (Temp, Loc);
4271 Aggr_Code := Build_Record_Aggr_Code (N, T, Target_Expr);
4273 -- Save the last assignment statement associated with the aggregate
4274 -- when building a controlled object. This reference is utilized by
4275 -- the finalization machinery when marking an object as successfully
4278 if Needs_Finalization (T) then
4279 Set_Last_Aggregate_Assignment (Temp, Last (Aggr_Code));
4282 Insert_Actions (N, Aggr_Code);
4283 Rewrite (N, New_Occurrence_Of (Temp, Loc));
4284 Analyze_And_Resolve (N, T);
4286 end Convert_To_Assignments;
4288 ---------------------------
4289 -- Convert_To_Positional --
4290 ---------------------------
4292 procedure Convert_To_Positional
4294 Max_Others_Replicate : Nat := 32;
4295 Handle_Bit_Packed : Boolean := False)
4297 Typ : constant Entity_Id := Etype (N);
4299 Static_Components : Boolean := True;
4301 procedure Check_Static_Components;
4302 -- Check whether all components of the aggregate are compile-time known
4303 -- values, and can be passed as is to the back-end without further
4309 Ixb : Node_Id) return Boolean;
4310 -- Convert the aggregate into a purely positional form if possible. On
4311 -- entry the bounds of all dimensions are known to be static, and the
4312 -- total number of components is safe enough to expand.
4314 function Is_Flat (N : Node_Id; Dims : Int) return Boolean;
4315 -- Return True iff the array N is flat (which is not trivial in the case
4316 -- of multidimensional aggregates).
4318 function Is_Static_Element (N : Node_Id) return Boolean;
4319 -- Return True if N, an element of a component association list, i.e.
4320 -- N_Component_Association or N_Iterated_Component_Association, has a
4321 -- compile-time known value and can be passed as is to the back-end
4322 -- without further expansion.
4323 -- An Iterated_Component_Association is treated as nonstatic in most
4324 -- cases for now, so there are possibilities for optimization.
4326 -----------------------------
4327 -- Check_Static_Components --
4328 -----------------------------
4330 -- Could use some comments in this body ???
4332 procedure Check_Static_Components is
4337 Static_Components := True;
4339 if Nkind (N) = N_String_Literal then
4342 elsif Present (Expressions (N)) then
4343 Expr := First (Expressions (N));
4344 while Present (Expr) loop
4345 if Nkind (Expr) /= N_Aggregate
4346 or else not Compile_Time_Known_Aggregate (Expr)
4347 or else Expansion_Delayed (Expr)
4349 Static_Components := False;
4357 if Nkind (N) = N_Aggregate
4358 and then Present (Component_Associations (N))
4360 Assoc := First (Component_Associations (N));
4361 while Present (Assoc) loop
4362 if not Is_Static_Element (Assoc) then
4363 Static_Components := False;
4370 end Check_Static_Components;
4379 Ixb : Node_Id) return Boolean
4381 Loc : constant Source_Ptr := Sloc (N);
4382 Blo : constant Node_Id := Type_Low_Bound (Etype (Ixb));
4383 Lo : constant Node_Id := Type_Low_Bound (Etype (Ix));
4384 Hi : constant Node_Id := Type_High_Bound (Etype (Ix));
4388 Others_Present : Boolean := False;
4391 if Nkind (Original_Node (N)) = N_String_Literal then
4395 if not Compile_Time_Known_Value (Lo)
4396 or else not Compile_Time_Known_Value (Hi)
4401 Lov := Expr_Value (Lo);
4402 Hiv := Expr_Value (Hi);
4404 -- Check if there is an others choice
4406 if Present (Component_Associations (N)) then
4412 Assoc := First (Component_Associations (N));
4413 while Present (Assoc) loop
4415 -- If this is a box association, flattening is in general
4416 -- not possible because at this point we cannot tell if the
4417 -- default is static or even exists.
4419 if Box_Present (Assoc) then
4422 elsif Nkind (Assoc) = N_Iterated_Component_Association then
4426 Choice := First (Choice_List (Assoc));
4428 while Present (Choice) loop
4429 if Nkind (Choice) = N_Others_Choice then
4430 Others_Present := True;
4441 -- If the low bound is not known at compile time and others is not
4442 -- present we can proceed since the bounds can be obtained from the
4446 or else (not Compile_Time_Known_Value (Blo) and then Others_Present)
4451 -- Determine if set of alternatives is suitable for conversion and
4452 -- build an array containing the values in sequence.
4455 Vals : array (UI_To_Int (Lov) .. UI_To_Int (Hiv))
4456 of Node_Id := (others => Empty);
4457 -- The values in the aggregate sorted appropriately
4460 -- Same data as Vals in list form
4463 -- Used to validate Max_Others_Replicate limit
4466 Num : Int := UI_To_Int (Lov);
4472 if Present (Expressions (N)) then
4473 Elmt := First (Expressions (N));
4474 while Present (Elmt) loop
4475 if Nkind (Elmt) = N_Aggregate
4476 and then Present (Next_Index (Ix))
4478 not Flatten (Elmt, Next_Index (Ix), Next_Index (Ixb))
4483 -- Duplicate expression for each index it covers
4485 Vals (Num) := New_Copy_Tree (Elmt);
4492 if No (Component_Associations (N)) then
4496 Elmt := First (Component_Associations (N));
4498 if Nkind (Expression (Elmt)) = N_Aggregate then
4499 if Present (Next_Index (Ix))
4502 (Expression (Elmt), Next_Index (Ix), Next_Index (Ixb))
4508 Component_Loop : while Present (Elmt) loop
4509 Choice := First (Choice_List (Elmt));
4510 Choice_Loop : while Present (Choice) loop
4512 -- If we have an others choice, fill in the missing elements
4513 -- subject to the limit established by Max_Others_Replicate.
4515 if Nkind (Choice) = N_Others_Choice then
4518 -- If the expression involves a construct that generates
4519 -- a loop, we must generate individual assignments and
4520 -- no flattening is possible.
4522 if Nkind (Expression (Elmt)) = N_Quantified_Expression
4527 for J in Vals'Range loop
4528 if No (Vals (J)) then
4529 Vals (J) := New_Copy_Tree (Expression (Elmt));
4530 Rep_Count := Rep_Count + 1;
4532 -- Check for maximum others replication. Note that
4533 -- we skip this test if either of the restrictions
4534 -- No_Elaboration_Code or No_Implicit_Loops is
4535 -- active, if this is a preelaborable unit or
4536 -- a predefined unit, or if the unit must be
4537 -- placed in data memory. This also ensures that
4538 -- predefined units get the same level of constant
4539 -- folding in Ada 95 and Ada 2005, where their
4540 -- categorization has changed.
4543 P : constant Entity_Id :=
4544 Cunit_Entity (Current_Sem_Unit);
4547 -- Check if duplication is always OK and, if so,
4548 -- continue processing.
4550 if Restriction_Active (No_Elaboration_Code)
4551 or else Restriction_Active (No_Implicit_Loops)
4553 (Ekind (Current_Scope) = E_Package
4554 and then Static_Elaboration_Desired
4556 or else Is_Preelaborated (P)
4557 or else (Ekind (P) = E_Package_Body
4559 Is_Preelaborated (Spec_Entity (P)))
4561 Is_Predefined_Unit (Get_Source_Unit (P))
4565 -- If duplication is not always OK, continue
4566 -- only if either the element is static or is
4567 -- an aggregate which can itself be flattened,
4568 -- and the replication count is not too high.
4570 elsif (Is_Static_Element (Elmt)
4572 (Nkind (Expression (Elmt)) = N_Aggregate
4573 and then Present (Next_Index (Ix))))
4574 and then Rep_Count <= Max_Others_Replicate
4578 -- Return False in all the other cases
4588 and then Warn_On_Redundant_Constructs
4590 Error_Msg_N ("there are no others?r?", Elmt);
4593 exit Component_Loop;
4595 -- Case of a subtype mark, identifier or expanded name
4597 elsif Is_Entity_Name (Choice)
4598 and then Is_Type (Entity (Choice))
4600 Lo := Type_Low_Bound (Etype (Choice));
4601 Hi := Type_High_Bound (Etype (Choice));
4603 -- Case of subtype indication
4605 elsif Nkind (Choice) = N_Subtype_Indication then
4606 Lo := Low_Bound (Range_Expression (Constraint (Choice)));
4607 Hi := High_Bound (Range_Expression (Constraint (Choice)));
4611 elsif Nkind (Choice) = N_Range then
4612 Lo := Low_Bound (Choice);
4613 Hi := High_Bound (Choice);
4615 -- Normal subexpression case
4617 else pragma Assert (Nkind (Choice) in N_Subexpr);
4618 if not Compile_Time_Known_Value (Choice) then
4622 Choice_Index := UI_To_Int (Expr_Value (Choice));
4624 if Choice_Index in Vals'Range then
4625 Vals (Choice_Index) :=
4626 New_Copy_Tree (Expression (Elmt));
4629 -- Choice is statically out-of-range, will be
4630 -- rewritten to raise Constraint_Error.
4638 -- Range cases merge with Lo,Hi set
4640 if not Compile_Time_Known_Value (Lo)
4642 not Compile_Time_Known_Value (Hi)
4647 for J in UI_To_Int (Expr_Value (Lo)) ..
4648 UI_To_Int (Expr_Value (Hi))
4650 Vals (J) := New_Copy_Tree (Expression (Elmt));
4656 end loop Choice_Loop;
4659 end loop Component_Loop;
4661 -- If we get here the conversion is possible
4664 for J in Vals'Range loop
4665 Append (Vals (J), Vlist);
4668 Rewrite (N, Make_Aggregate (Loc, Expressions => Vlist));
4669 Set_Aggregate_Bounds (N, Aggregate_Bounds (Original_Node (N)));
4678 function Is_Flat (N : Node_Id; Dims : Int) return Boolean is
4685 elsif Nkind (N) = N_Aggregate then
4686 if Present (Component_Associations (N)) then
4690 Elmt := First (Expressions (N));
4691 while Present (Elmt) loop
4692 if not Is_Flat (Elmt, Dims - 1) then
4706 -------------------------
4707 -- Is_Static_Element --
4708 -------------------------
4710 function Is_Static_Element (N : Node_Id) return Boolean is
4711 Expr : constant Node_Id := Expression (N);
4714 if Nkind_In (Expr, N_Integer_Literal, N_Real_Literal) then
4717 elsif Is_Entity_Name (Expr)
4718 and then Present (Entity (Expr))
4719 and then Ekind (Entity (Expr)) = E_Enumeration_Literal
4723 elsif Nkind (N) = N_Iterated_Component_Association then
4726 elsif Nkind (Expr) = N_Aggregate
4727 and then Compile_Time_Known_Aggregate (Expr)
4728 and then not Expansion_Delayed (Expr)
4735 end Is_Static_Element;
4737 -- Start of processing for Convert_To_Positional
4740 -- Only convert to positional when generating C in case of an
4741 -- object declaration, this is the only case where aggregates are
4744 if Modify_Tree_For_C and then not In_Object_Declaration (N) then
4748 -- Ada 2005 (AI-287): Do not convert in case of default initialized
4749 -- components because in this case will need to call the corresponding
4752 if Has_Default_Init_Comps (N) then
4756 -- A subaggregate may have been flattened but is not known to be
4757 -- Compile_Time_Known. Set that flag in cases that cannot require
4758 -- elaboration code, so that the aggregate can be used as the
4759 -- initial value of a thread-local variable.
4761 if Is_Flat (N, Number_Dimensions (Typ)) then
4762 if Static_Array_Aggregate (N) then
4763 Set_Compile_Time_Known_Aggregate (N);
4769 if Is_Bit_Packed_Array (Typ) and then not Handle_Bit_Packed then
4773 -- Do not convert to positional if controlled components are involved
4774 -- since these require special processing
4776 if Has_Controlled_Component (Typ) then
4780 Check_Static_Components;
4782 -- If the size is known, or all the components are static, try to
4783 -- build a fully positional aggregate.
4785 -- The size of the type may not be known for an aggregate with
4786 -- discriminated array components, but if the components are static
4787 -- it is still possible to verify statically that the length is
4788 -- compatible with the upper bound of the type, and therefore it is
4789 -- worth flattening such aggregates as well.
4791 -- For now the back-end expands these aggregates into individual
4792 -- assignments to the target anyway, but it is conceivable that
4793 -- it will eventually be able to treat such aggregates statically???
4795 if Aggr_Size_OK (N, Typ)
4796 and then Flatten (N, First_Index (Typ), First_Index (Base_Type (Typ)))
4798 if Static_Components then
4799 Set_Compile_Time_Known_Aggregate (N);
4800 Set_Expansion_Delayed (N, False);
4803 Analyze_And_Resolve (N, Typ);
4806 -- If Static_Elaboration_Desired has been specified, diagnose aggregates
4807 -- that will still require initialization code.
4809 if (Ekind (Current_Scope) = E_Package
4810 and then Static_Elaboration_Desired (Current_Scope))
4811 and then Nkind (Parent (N)) = N_Object_Declaration
4817 if Nkind (N) = N_Aggregate and then Present (Expressions (N)) then
4818 Expr := First (Expressions (N));
4819 while Present (Expr) loop
4820 if Nkind_In (Expr, N_Integer_Literal, N_Real_Literal)
4822 (Is_Entity_Name (Expr)
4823 and then Ekind (Entity (Expr)) = E_Enumeration_Literal)
4829 ("non-static object requires elaboration code??", N);
4836 if Present (Component_Associations (N)) then
4837 Error_Msg_N ("object requires elaboration code??", N);
4842 end Convert_To_Positional;
4844 ----------------------------
4845 -- Expand_Array_Aggregate --
4846 ----------------------------
4848 -- Array aggregate expansion proceeds as follows:
4850 -- 1. If requested we generate code to perform all the array aggregate
4851 -- bound checks, specifically
4853 -- (a) Check that the index range defined by aggregate bounds is
4854 -- compatible with corresponding index subtype.
4856 -- (b) If an others choice is present check that no aggregate
4857 -- index is outside the bounds of the index constraint.
4859 -- (c) For multidimensional arrays make sure that all subaggregates
4860 -- corresponding to the same dimension have the same bounds.
4862 -- 2. Check for packed array aggregate which can be converted to a
4863 -- constant so that the aggregate disappears completely.
4865 -- 3. Check case of nested aggregate. Generally nested aggregates are
4866 -- handled during the processing of the parent aggregate.
4868 -- 4. Check if the aggregate can be statically processed. If this is the
4869 -- case pass it as is to Gigi. Note that a necessary condition for
4870 -- static processing is that the aggregate be fully positional.
4872 -- 5. If in place aggregate expansion is possible (i.e. no need to create
4873 -- a temporary) then mark the aggregate as such and return. Otherwise
4874 -- create a new temporary and generate the appropriate initialization
4877 procedure Expand_Array_Aggregate (N : Node_Id) is
4878 Loc : constant Source_Ptr := Sloc (N);
4880 Typ : constant Entity_Id := Etype (N);
4881 Ctyp : constant Entity_Id := Component_Type (Typ);
4882 -- Typ is the correct constrained array subtype of the aggregate
4883 -- Ctyp is the corresponding component type.
4885 Aggr_Dimension : constant Pos := Number_Dimensions (Typ);
4886 -- Number of aggregate index dimensions
4888 Aggr_Low : array (1 .. Aggr_Dimension) of Node_Id;
4889 Aggr_High : array (1 .. Aggr_Dimension) of Node_Id;
4890 -- Low and High bounds of the constraint for each aggregate index
4892 Aggr_Index_Typ : array (1 .. Aggr_Dimension) of Entity_Id;
4893 -- The type of each index
4895 In_Place_Assign_OK_For_Declaration : Boolean := False;
4896 -- True if we are to generate an in place assignment for a declaration
4898 Maybe_In_Place_OK : Boolean;
4899 -- If the type is neither controlled nor packed and the aggregate
4900 -- is the expression in an assignment, assignment in place may be
4901 -- possible, provided other conditions are met on the LHS.
4903 Others_Present : array (1 .. Aggr_Dimension) of Boolean :=
4905 -- If Others_Present (J) is True, then there is an others choice in one
4906 -- of the subaggregates of N at dimension J.
4908 function Aggr_Assignment_OK_For_Backend (N : Node_Id) return Boolean;
4909 -- Returns true if an aggregate assignment can be done by the back end
4911 procedure Build_Constrained_Type (Positional : Boolean);
4912 -- If the subtype is not static or unconstrained, build a constrained
4913 -- type using the computable sizes of the aggregate and its sub-
4916 procedure Check_Bounds (Aggr_Bounds : Node_Id; Index_Bounds : Node_Id);
4917 -- Checks that the bounds of Aggr_Bounds are within the bounds defined
4920 procedure Check_Same_Aggr_Bounds (Sub_Aggr : Node_Id; Dim : Pos);
4921 -- Checks that in a multidimensional array aggregate all subaggregates
4922 -- corresponding to the same dimension have the same bounds. Sub_Aggr is
4923 -- an array subaggregate. Dim is the dimension corresponding to the
4926 procedure Compute_Others_Present (Sub_Aggr : Node_Id; Dim : Pos);
4927 -- Computes the values of array Others_Present. Sub_Aggr is the array
4928 -- subaggregate we start the computation from. Dim is the dimension
4929 -- corresponding to the subaggregate.
4931 function In_Place_Assign_OK return Boolean;
4932 -- Simple predicate to determine whether an aggregate assignment can
4933 -- be done in place, because none of the new values can depend on the
4934 -- components of the target of the assignment.
4936 procedure Others_Check (Sub_Aggr : Node_Id; Dim : Pos);
4937 -- Checks that if an others choice is present in any subaggregate, no
4938 -- aggregate index is outside the bounds of the index constraint.
4939 -- Sub_Aggr is an array subaggregate. Dim is the dimension corresponding
4940 -- to the subaggregate.
4942 function Safe_Left_Hand_Side (N : Node_Id) return Boolean;
4943 -- In addition to Maybe_In_Place_OK, in order for an aggregate to be
4944 -- built directly into the target of the assignment it must be free
4947 ------------------------------------
4948 -- Aggr_Assignment_OK_For_Backend --
4949 ------------------------------------
4951 -- Backend processing by Gigi/gcc is possible only if all the following
4952 -- conditions are met:
4954 -- 1. N consists of a single OTHERS choice, possibly recursively
4956 -- 2. The array type has no null ranges (the purpose of this is to
4957 -- avoid a bogus warning for an out-of-range value).
4959 -- 3. The array type has no atomic components
4961 -- 4. The component type is elementary
4963 -- 5. The component size is a multiple of Storage_Unit
4965 -- 6. The component size is Storage_Unit or the value is of the form
4966 -- M * (1 + A**1 + A**2 + .. A**(K-1)) where A = 2**(Storage_Unit)
4967 -- and M in 1 .. A-1. This can also be viewed as K occurrences of
4968 -- the 8-bit value M, concatenated together.
4970 -- The ultimate goal is to generate a call to a fast memset routine
4971 -- specifically optimized for the target.
4973 function Aggr_Assignment_OK_For_Backend (N : Node_Id) return Boolean is
4985 -- Recurse as far as possible to find the innermost component type
4989 while Is_Array_Type (Ctyp) loop
4990 if Nkind (Expr) /= N_Aggregate
4991 or else not Is_Others_Aggregate (Expr)
4996 Index := First_Index (Ctyp);
4997 while Present (Index) loop
4998 Get_Index_Bounds (Index, Low, High);
5000 if Is_Null_Range (Low, High) then
5007 Expr := Expression (First (Component_Associations (Expr)));
5009 for J in 1 .. Number_Dimensions (Ctyp) - 1 loop
5010 if Nkind (Expr) /= N_Aggregate
5011 or else not Is_Others_Aggregate (Expr)
5016 Expr := Expression (First (Component_Associations (Expr)));
5019 if Has_Atomic_Components (Ctyp) then
5023 Csiz := Component_Size (Ctyp);
5024 Ctyp := Component_Type (Ctyp);
5026 if Is_Atomic_Or_VFA (Ctyp) then
5031 -- An Iterated_Component_Association involves a loop (in most cases)
5032 -- and is never static.
5034 if Nkind (Parent (Expr)) = N_Iterated_Component_Association then
5038 -- Access types need to be dealt with specially
5040 if Is_Access_Type (Ctyp) then
5042 -- Component_Size is not set by Layout_Type if the component
5043 -- type is an access type ???
5045 Csiz := Esize (Ctyp);
5047 -- Fat pointers are rejected as they are not really elementary
5050 if Csiz /= System_Address_Size then
5054 -- The supported expressions are NULL and constants, others are
5055 -- rejected upfront to avoid being analyzed below, which can be
5056 -- problematic for some of them, for example allocators.
5058 if Nkind (Expr) /= N_Null and then not Is_Entity_Name (Expr) then
5062 -- Scalar types are OK if their size is a multiple of Storage_Unit
5064 elsif Is_Scalar_Type (Ctyp) then
5065 if Csiz mod System_Storage_Unit /= 0 then
5069 -- Composite types are rejected
5075 -- The expression needs to be analyzed if True is returned
5077 Analyze_And_Resolve (Expr, Ctyp);
5079 -- Strip away any conversions from the expression as they simply
5080 -- qualify the real expression.
5082 while Nkind_In (Expr, N_Unchecked_Type_Conversion,
5085 Expr := Expression (Expr);
5088 Nunits := UI_To_Int (Csiz) / System_Storage_Unit;
5094 if not Compile_Time_Known_Value (Expr) then
5098 -- The only supported value for floating point is 0.0
5100 if Is_Floating_Point_Type (Ctyp) then
5101 return Expr_Value_R (Expr) = Ureal_0;
5104 -- For other types, we can look into the value as an integer
5106 Value := Expr_Value (Expr);
5108 if Has_Biased_Representation (Ctyp) then
5109 Value := Value - Expr_Value (Type_Low_Bound (Ctyp));
5112 -- Values 0 and -1 immediately satisfy the last check
5114 if Value = Uint_0 or else Value = Uint_Minus_1 then
5118 -- We need to work with an unsigned value
5121 Value := Value + 2**(System_Storage_Unit * Nunits);
5124 Remainder := Value rem 2**System_Storage_Unit;
5126 for J in 1 .. Nunits - 1 loop
5127 Value := Value / 2**System_Storage_Unit;
5129 if Value rem 2**System_Storage_Unit /= Remainder then
5135 end Aggr_Assignment_OK_For_Backend;
5137 ----------------------------
5138 -- Build_Constrained_Type --
5139 ----------------------------
5141 procedure Build_Constrained_Type (Positional : Boolean) is
5142 Loc : constant Source_Ptr := Sloc (N);
5143 Agg_Type : constant Entity_Id := Make_Temporary (Loc, 'A');
5146 Typ : constant Entity_Id := Etype (N);
5147 Indexes : constant List_Id := New_List;
5152 -- If the aggregate is purely positional, all its subaggregates
5153 -- have the same size. We collect the dimensions from the first
5154 -- subaggregate at each level.
5159 for D in 1 .. Number_Dimensions (Typ) loop
5160 Sub_Agg := First (Expressions (Sub_Agg));
5164 while Present (Comp) loop
5171 Low_Bound => Make_Integer_Literal (Loc, 1),
5172 High_Bound => Make_Integer_Literal (Loc, Num)));
5176 -- We know the aggregate type is unconstrained and the aggregate
5177 -- is not processable by the back end, therefore not necessarily
5178 -- positional. Retrieve each dimension bounds (computed earlier).
5180 for D in 1 .. Number_Dimensions (Typ) loop
5183 Low_Bound => Aggr_Low (D),
5184 High_Bound => Aggr_High (D)));
5189 Make_Full_Type_Declaration (Loc,
5190 Defining_Identifier => Agg_Type,
5192 Make_Constrained_Array_Definition (Loc,
5193 Discrete_Subtype_Definitions => Indexes,
5194 Component_Definition =>
5195 Make_Component_Definition (Loc,
5196 Aliased_Present => False,
5197 Subtype_Indication =>
5198 New_Occurrence_Of (Component_Type (Typ), Loc))));
5200 Insert_Action (N, Decl);
5202 Set_Etype (N, Agg_Type);
5203 Set_Is_Itype (Agg_Type);
5204 Freeze_Itype (Agg_Type, N);
5205 end Build_Constrained_Type;
5211 procedure Check_Bounds (Aggr_Bounds : Node_Id; Index_Bounds : Node_Id) is
5218 Cond : Node_Id := Empty;
5221 Get_Index_Bounds (Aggr_Bounds, Aggr_Lo, Aggr_Hi);
5222 Get_Index_Bounds (Index_Bounds, Ind_Lo, Ind_Hi);
5224 -- Generate the following test:
5226 -- [constraint_error when
5227 -- Aggr_Lo <= Aggr_Hi and then
5228 -- (Aggr_Lo < Ind_Lo or else Aggr_Hi > Ind_Hi)]
5230 -- As an optimization try to see if some tests are trivially vacuous
5231 -- because we are comparing an expression against itself.
5233 if Aggr_Lo = Ind_Lo and then Aggr_Hi = Ind_Hi then
5236 elsif Aggr_Hi = Ind_Hi then
5239 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
5240 Right_Opnd => Duplicate_Subexpr_Move_Checks (Ind_Lo));
5242 elsif Aggr_Lo = Ind_Lo then
5245 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi),
5246 Right_Opnd => Duplicate_Subexpr_Move_Checks (Ind_Hi));
5253 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
5254 Right_Opnd => Duplicate_Subexpr_Move_Checks (Ind_Lo)),
5258 Left_Opnd => Duplicate_Subexpr (Aggr_Hi),
5259 Right_Opnd => Duplicate_Subexpr (Ind_Hi)));
5262 if Present (Cond) then
5267 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
5268 Right_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi)),
5270 Right_Opnd => Cond);
5272 Set_Analyzed (Left_Opnd (Left_Opnd (Cond)), False);
5273 Set_Analyzed (Right_Opnd (Left_Opnd (Cond)), False);
5275 Make_Raise_Constraint_Error (Loc,
5277 Reason => CE_Range_Check_Failed));
5281 ----------------------------
5282 -- Check_Same_Aggr_Bounds --
5283 ----------------------------
5285 procedure Check_Same_Aggr_Bounds (Sub_Aggr : Node_Id; Dim : Pos) is
5286 Sub_Lo : constant Node_Id := Low_Bound (Aggregate_Bounds (Sub_Aggr));
5287 Sub_Hi : constant Node_Id := High_Bound (Aggregate_Bounds (Sub_Aggr));
5288 -- The bounds of this specific subaggregate
5290 Aggr_Lo : constant Node_Id := Aggr_Low (Dim);
5291 Aggr_Hi : constant Node_Id := Aggr_High (Dim);
5292 -- The bounds of the aggregate for this dimension
5294 Ind_Typ : constant Entity_Id := Aggr_Index_Typ (Dim);
5295 -- The index type for this dimension.xxx
5297 Cond : Node_Id := Empty;
5302 -- If index checks are on generate the test
5304 -- [constraint_error when
5305 -- Aggr_Lo /= Sub_Lo or else Aggr_Hi /= Sub_Hi]
5307 -- As an optimization try to see if some tests are trivially vacuos
5308 -- because we are comparing an expression against itself. Also for
5309 -- the first dimension the test is trivially vacuous because there
5310 -- is just one aggregate for dimension 1.
5312 if Index_Checks_Suppressed (Ind_Typ) then
5315 elsif Dim = 1 or else (Aggr_Lo = Sub_Lo and then Aggr_Hi = Sub_Hi)
5319 elsif Aggr_Hi = Sub_Hi then
5322 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
5323 Right_Opnd => Duplicate_Subexpr_Move_Checks (Sub_Lo));
5325 elsif Aggr_Lo = Sub_Lo then
5328 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi),
5329 Right_Opnd => Duplicate_Subexpr_Move_Checks (Sub_Hi));
5336 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
5337 Right_Opnd => Duplicate_Subexpr_Move_Checks (Sub_Lo)),
5341 Left_Opnd => Duplicate_Subexpr (Aggr_Hi),
5342 Right_Opnd => Duplicate_Subexpr (Sub_Hi)));
5345 if Present (Cond) then
5347 Make_Raise_Constraint_Error (Loc,
5349 Reason => CE_Length_Check_Failed));
5352 -- Now look inside the subaggregate to see if there is more work
5354 if Dim < Aggr_Dimension then
5356 -- Process positional components
5358 if Present (Expressions (Sub_Aggr)) then
5359 Expr := First (Expressions (Sub_Aggr));
5360 while Present (Expr) loop
5361 Check_Same_Aggr_Bounds (Expr, Dim + 1);
5366 -- Process component associations
5368 if Present (Component_Associations (Sub_Aggr)) then
5369 Assoc := First (Component_Associations (Sub_Aggr));
5370 while Present (Assoc) loop
5371 Expr := Expression (Assoc);
5372 Check_Same_Aggr_Bounds (Expr, Dim + 1);
5377 end Check_Same_Aggr_Bounds;
5379 ----------------------------
5380 -- Compute_Others_Present --
5381 ----------------------------
5383 procedure Compute_Others_Present (Sub_Aggr : Node_Id; Dim : Pos) is
5388 if Present (Component_Associations (Sub_Aggr)) then
5389 Assoc := Last (Component_Associations (Sub_Aggr));
5391 if Nkind (First (Choice_List (Assoc))) = N_Others_Choice then
5392 Others_Present (Dim) := True;
5396 -- Now look inside the subaggregate to see if there is more work
5398 if Dim < Aggr_Dimension then
5400 -- Process positional components
5402 if Present (Expressions (Sub_Aggr)) then
5403 Expr := First (Expressions (Sub_Aggr));
5404 while Present (Expr) loop
5405 Compute_Others_Present (Expr, Dim + 1);
5410 -- Process component associations
5412 if Present (Component_Associations (Sub_Aggr)) then
5413 Assoc := First (Component_Associations (Sub_Aggr));
5414 while Present (Assoc) loop
5415 Expr := Expression (Assoc);
5416 Compute_Others_Present (Expr, Dim + 1);
5421 end Compute_Others_Present;
5423 ------------------------
5424 -- In_Place_Assign_OK --
5425 ------------------------
5427 function In_Place_Assign_OK return Boolean is
5435 function Safe_Aggregate (Aggr : Node_Id) return Boolean;
5436 -- Check recursively that each component of a (sub)aggregate does not
5437 -- depend on the variable being assigned to.
5439 function Safe_Component (Expr : Node_Id) return Boolean;
5440 -- Verify that an expression cannot depend on the variable being
5441 -- assigned to. Room for improvement here (but less than before).
5443 --------------------
5444 -- Safe_Aggregate --
5445 --------------------
5447 function Safe_Aggregate (Aggr : Node_Id) return Boolean is
5451 if Nkind (Parent (Aggr)) = N_Iterated_Component_Association then
5455 if Present (Expressions (Aggr)) then
5456 Expr := First (Expressions (Aggr));
5457 while Present (Expr) loop
5458 if Nkind (Expr) = N_Aggregate then
5459 if not Safe_Aggregate (Expr) then
5463 elsif not Safe_Component (Expr) then
5471 if Present (Component_Associations (Aggr)) then
5472 Expr := First (Component_Associations (Aggr));
5473 while Present (Expr) loop
5474 if Nkind (Expression (Expr)) = N_Aggregate then
5475 if not Safe_Aggregate (Expression (Expr)) then
5479 -- If association has a box, no way to determine yet
5480 -- whether default can be assigned in place.
5482 elsif Box_Present (Expr) then
5485 elsif not Safe_Component (Expression (Expr)) then
5496 --------------------
5497 -- Safe_Component --
5498 --------------------
5500 function Safe_Component (Expr : Node_Id) return Boolean is
5501 Comp : Node_Id := Expr;
5503 function Check_Component (Comp : Node_Id) return Boolean;
5504 -- Do the recursive traversal, after copy
5506 ---------------------
5507 -- Check_Component --
5508 ---------------------
5510 function Check_Component (Comp : Node_Id) return Boolean is
5512 if Is_Overloaded (Comp) then
5516 return Compile_Time_Known_Value (Comp)
5518 or else (Is_Entity_Name (Comp)
5519 and then Present (Entity (Comp))
5520 and then No (Renamed_Object (Entity (Comp))))
5522 or else (Nkind (Comp) = N_Attribute_Reference
5523 and then Check_Component (Prefix (Comp)))
5525 or else (Nkind (Comp) in N_Binary_Op
5526 and then Check_Component (Left_Opnd (Comp))
5527 and then Check_Component (Right_Opnd (Comp)))
5529 or else (Nkind (Comp) in N_Unary_Op
5530 and then Check_Component (Right_Opnd (Comp)))
5532 or else (Nkind (Comp) = N_Selected_Component
5533 and then Check_Component (Prefix (Comp)))
5535 or else (Nkind (Comp) = N_Unchecked_Type_Conversion
5536 and then Check_Component (Expression (Comp)));
5537 end Check_Component;
5539 -- Start of processing for Safe_Component
5542 -- If the component appears in an association that may correspond
5543 -- to more than one element, it is not analyzed before expansion
5544 -- into assignments, to avoid side effects. We analyze, but do not
5545 -- resolve the copy, to obtain sufficient entity information for
5546 -- the checks that follow. If component is overloaded we assume
5547 -- an unsafe function call.
5549 if not Analyzed (Comp) then
5550 if Is_Overloaded (Expr) then
5553 elsif Nkind (Expr) = N_Aggregate
5554 and then not Is_Others_Aggregate (Expr)
5558 elsif Nkind (Expr) = N_Allocator then
5560 -- For now, too complex to analyze
5564 elsif Nkind (Parent (Expr)) =
5565 N_Iterated_Component_Association
5567 -- Ditto for iterated component associations, which in
5568 -- general require an enclosing loop and involve nonstatic
5574 Comp := New_Copy_Tree (Expr);
5575 Set_Parent (Comp, Parent (Expr));
5579 if Nkind (Comp) = N_Aggregate then
5580 return Safe_Aggregate (Comp);
5582 return Check_Component (Comp);
5586 -- Start of processing for In_Place_Assign_OK
5589 if Present (Component_Associations (N)) then
5591 -- On assignment, sliding can take place, so we cannot do the
5592 -- assignment in place unless the bounds of the aggregate are
5593 -- statically equal to those of the target.
5595 -- If the aggregate is given by an others choice, the bounds are
5596 -- derived from the left-hand side, and the assignment is safe if
5597 -- the expression is.
5599 if Is_Others_Aggregate (N) then
5602 (Expression (First (Component_Associations (N))));
5605 Aggr_In := First_Index (Etype (N));
5607 if Nkind (Parent (N)) = N_Assignment_Statement then
5608 Obj_In := First_Index (Etype (Name (Parent (N))));
5611 -- Context is an allocator. Check bounds of aggregate against
5612 -- given type in qualified expression.
5614 pragma Assert (Nkind (Parent (Parent (N))) = N_Allocator);
5616 First_Index (Etype (Entity (Subtype_Mark (Parent (N)))));
5619 while Present (Aggr_In) loop
5620 Get_Index_Bounds (Aggr_In, Aggr_Lo, Aggr_Hi);
5621 Get_Index_Bounds (Obj_In, Obj_Lo, Obj_Hi);
5623 if not Compile_Time_Known_Value (Aggr_Lo)
5624 or else not Compile_Time_Known_Value (Obj_Lo)
5625 or else not Compile_Time_Known_Value (Obj_Hi)
5626 or else Expr_Value (Aggr_Lo) /= Expr_Value (Obj_Lo)
5630 -- For an assignment statement we require static matching of
5631 -- bounds. Ditto for an allocator whose qualified expression
5632 -- is a constrained type. If the expression in the allocator
5633 -- is an unconstrained array, we accept an upper bound that
5634 -- is not static, to allow for nonstatic expressions of the
5635 -- base type. Clearly there are further possibilities (with
5636 -- diminishing returns) for safely building arrays in place
5639 elsif Nkind (Parent (N)) = N_Assignment_Statement
5640 or else Is_Constrained (Etype (Parent (N)))
5642 if not Compile_Time_Known_Value (Aggr_Hi)
5643 or else Expr_Value (Aggr_Hi) /= Expr_Value (Obj_Hi)
5649 Next_Index (Aggr_In);
5650 Next_Index (Obj_In);
5654 -- Now check the component values themselves
5656 return Safe_Aggregate (N);
5657 end In_Place_Assign_OK;
5663 procedure Others_Check (Sub_Aggr : Node_Id; Dim : Pos) is
5664 Aggr_Lo : constant Node_Id := Aggr_Low (Dim);
5665 Aggr_Hi : constant Node_Id := Aggr_High (Dim);
5666 -- The bounds of the aggregate for this dimension
5668 Ind_Typ : constant Entity_Id := Aggr_Index_Typ (Dim);
5669 -- The index type for this dimension
5671 Need_To_Check : Boolean := False;
5673 Choices_Lo : Node_Id := Empty;
5674 Choices_Hi : Node_Id := Empty;
5675 -- The lowest and highest discrete choices for a named subaggregate
5677 Nb_Choices : Int := -1;
5678 -- The number of discrete non-others choices in this subaggregate
5680 Nb_Elements : Uint := Uint_0;
5681 -- The number of elements in a positional aggregate
5683 Cond : Node_Id := Empty;
5690 -- Check if we have an others choice. If we do make sure that this
5691 -- subaggregate contains at least one element in addition to the
5694 if Range_Checks_Suppressed (Ind_Typ) then
5695 Need_To_Check := False;
5697 elsif Present (Expressions (Sub_Aggr))
5698 and then Present (Component_Associations (Sub_Aggr))
5700 Need_To_Check := True;
5702 elsif Present (Component_Associations (Sub_Aggr)) then
5703 Assoc := Last (Component_Associations (Sub_Aggr));
5705 if Nkind (First (Choice_List (Assoc))) /= N_Others_Choice then
5706 Need_To_Check := False;
5709 -- Count the number of discrete choices. Start with -1 because
5710 -- the others choice does not count.
5712 -- Is there some reason we do not use List_Length here ???
5715 Assoc := First (Component_Associations (Sub_Aggr));
5716 while Present (Assoc) loop
5717 Choice := First (Choice_List (Assoc));
5718 while Present (Choice) loop
5719 Nb_Choices := Nb_Choices + 1;
5726 -- If there is only an others choice nothing to do
5728 Need_To_Check := (Nb_Choices > 0);
5732 Need_To_Check := False;
5735 -- If we are dealing with a positional subaggregate with an others
5736 -- choice then compute the number or positional elements.
5738 if Need_To_Check and then Present (Expressions (Sub_Aggr)) then
5739 Expr := First (Expressions (Sub_Aggr));
5740 Nb_Elements := Uint_0;
5741 while Present (Expr) loop
5742 Nb_Elements := Nb_Elements + 1;
5746 -- If the aggregate contains discrete choices and an others choice
5747 -- compute the smallest and largest discrete choice values.
5749 elsif Need_To_Check then
5750 Compute_Choices_Lo_And_Choices_Hi : declare
5752 Table : Case_Table_Type (1 .. Nb_Choices);
5753 -- Used to sort all the different choice values
5760 Assoc := First (Component_Associations (Sub_Aggr));
5761 while Present (Assoc) loop
5762 Choice := First (Choice_List (Assoc));
5763 while Present (Choice) loop
5764 if Nkind (Choice) = N_Others_Choice then
5768 Get_Index_Bounds (Choice, Low, High);
5769 Table (J).Choice_Lo := Low;
5770 Table (J).Choice_Hi := High;
5779 -- Sort the discrete choices
5781 Sort_Case_Table (Table);
5783 Choices_Lo := Table (1).Choice_Lo;
5784 Choices_Hi := Table (Nb_Choices).Choice_Hi;
5785 end Compute_Choices_Lo_And_Choices_Hi;
5788 -- If no others choice in this subaggregate, or the aggregate
5789 -- comprises only an others choice, nothing to do.
5791 if not Need_To_Check then
5794 -- If we are dealing with an aggregate containing an others choice
5795 -- and positional components, we generate the following test:
5797 -- if Ind_Typ'Pos (Aggr_Lo) + (Nb_Elements - 1) >
5798 -- Ind_Typ'Pos (Aggr_Hi)
5800 -- raise Constraint_Error;
5803 elsif Nb_Elements > Uint_0 then
5809 Make_Attribute_Reference (Loc,
5810 Prefix => New_Occurrence_Of (Ind_Typ, Loc),
5811 Attribute_Name => Name_Pos,
5814 (Duplicate_Subexpr_Move_Checks (Aggr_Lo))),
5815 Right_Opnd => Make_Integer_Literal (Loc, Nb_Elements - 1)),
5818 Make_Attribute_Reference (Loc,
5819 Prefix => New_Occurrence_Of (Ind_Typ, Loc),
5820 Attribute_Name => Name_Pos,
5821 Expressions => New_List (
5822 Duplicate_Subexpr_Move_Checks (Aggr_Hi))));
5824 -- If we are dealing with an aggregate containing an others choice
5825 -- and discrete choices we generate the following test:
5827 -- [constraint_error when
5828 -- Choices_Lo < Aggr_Lo or else Choices_Hi > Aggr_Hi];
5835 Left_Opnd => Duplicate_Subexpr_Move_Checks (Choices_Lo),
5836 Right_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo)),
5840 Left_Opnd => Duplicate_Subexpr (Choices_Hi),
5841 Right_Opnd => Duplicate_Subexpr (Aggr_Hi)));
5844 if Present (Cond) then
5846 Make_Raise_Constraint_Error (Loc,
5848 Reason => CE_Length_Check_Failed));
5849 -- Questionable reason code, shouldn't that be a
5850 -- CE_Range_Check_Failed ???
5853 -- Now look inside the subaggregate to see if there is more work
5855 if Dim < Aggr_Dimension then
5857 -- Process positional components
5859 if Present (Expressions (Sub_Aggr)) then
5860 Expr := First (Expressions (Sub_Aggr));
5861 while Present (Expr) loop
5862 Others_Check (Expr, Dim + 1);
5867 -- Process component associations
5869 if Present (Component_Associations (Sub_Aggr)) then
5870 Assoc := First (Component_Associations (Sub_Aggr));
5871 while Present (Assoc) loop
5872 Expr := Expression (Assoc);
5873 Others_Check (Expr, Dim + 1);
5880 -------------------------
5881 -- Safe_Left_Hand_Side --
5882 -------------------------
5884 function Safe_Left_Hand_Side (N : Node_Id) return Boolean is
5885 function Is_Safe_Index (Indx : Node_Id) return Boolean;
5886 -- If the left-hand side includes an indexed component, check that
5887 -- the indexes are free of side effects.
5893 function Is_Safe_Index (Indx : Node_Id) return Boolean is
5895 if Is_Entity_Name (Indx) then
5898 elsif Nkind (Indx) = N_Integer_Literal then
5901 elsif Nkind (Indx) = N_Function_Call
5902 and then Is_Entity_Name (Name (Indx))
5903 and then Has_Pragma_Pure_Function (Entity (Name (Indx)))
5907 elsif Nkind (Indx) = N_Type_Conversion
5908 and then Is_Safe_Index (Expression (Indx))
5917 -- Start of processing for Safe_Left_Hand_Side
5920 if Is_Entity_Name (N) then
5923 elsif Nkind_In (N, N_Explicit_Dereference, N_Selected_Component)
5924 and then Safe_Left_Hand_Side (Prefix (N))
5928 elsif Nkind (N) = N_Indexed_Component
5929 and then Safe_Left_Hand_Side (Prefix (N))
5930 and then Is_Safe_Index (First (Expressions (N)))
5934 elsif Nkind (N) = N_Unchecked_Type_Conversion then
5935 return Safe_Left_Hand_Side (Expression (N));
5940 end Safe_Left_Hand_Side;
5945 -- Holds the temporary aggregate value
5948 -- Holds the declaration of Tmp
5950 Aggr_Code : List_Id;
5951 Parent_Node : Node_Id;
5952 Parent_Kind : Node_Kind;
5954 -- Start of processing for Expand_Array_Aggregate
5957 -- Do not touch the special aggregates of attributes used for Asm calls
5959 if Is_RTE (Ctyp, RE_Asm_Input_Operand)
5960 or else Is_RTE (Ctyp, RE_Asm_Output_Operand)
5964 -- Do not expand an aggregate for an array type which contains tasks if
5965 -- the aggregate is associated with an unexpanded return statement of a
5966 -- build-in-place function. The aggregate is expanded when the related
5967 -- return statement (rewritten into an extended return) is processed.
5968 -- This delay ensures that any temporaries and initialization code
5969 -- generated for the aggregate appear in the proper return block and
5970 -- use the correct _chain and _master.
5972 elsif Has_Task (Base_Type (Etype (N)))
5973 and then Nkind (Parent (N)) = N_Simple_Return_Statement
5974 and then Is_Build_In_Place_Function
5975 (Return_Applies_To (Return_Statement_Entity (Parent (N))))
5979 -- Do not attempt expansion if error already detected. We may reach this
5980 -- point in spite of previous errors when compiling with -gnatq, to
5981 -- force all possible errors (this is the usual ACATS mode).
5983 elsif Error_Posted (N) then
5987 -- If the semantic analyzer has determined that aggregate N will raise
5988 -- Constraint_Error at run time, then the aggregate node has been
5989 -- replaced with an N_Raise_Constraint_Error node and we should
5992 pragma Assert (not Raises_Constraint_Error (N));
5996 -- Check that the index range defined by aggregate bounds is
5997 -- compatible with corresponding index subtype.
5999 Index_Compatibility_Check : declare
6000 Aggr_Index_Range : Node_Id := First_Index (Typ);
6001 -- The current aggregate index range
6003 Index_Constraint : Node_Id := First_Index (Etype (Typ));
6004 -- The corresponding index constraint against which we have to
6005 -- check the above aggregate index range.
6008 Compute_Others_Present (N, 1);
6010 for J in 1 .. Aggr_Dimension loop
6011 -- There is no need to emit a check if an others choice is present
6012 -- for this array aggregate dimension since in this case one of
6013 -- N's subaggregates has taken its bounds from the context and
6014 -- these bounds must have been checked already. In addition all
6015 -- subaggregates corresponding to the same dimension must all have
6016 -- the same bounds (checked in (c) below).
6018 if not Range_Checks_Suppressed (Etype (Index_Constraint))
6019 and then not Others_Present (J)
6021 -- We don't use Checks.Apply_Range_Check here because it emits
6022 -- a spurious check. Namely it checks that the range defined by
6023 -- the aggregate bounds is nonempty. But we know this already
6026 Check_Bounds (Aggr_Index_Range, Index_Constraint);
6029 -- Save the low and high bounds of the aggregate index as well as
6030 -- the index type for later use in checks (b) and (c) below.
6032 Aggr_Low (J) := Low_Bound (Aggr_Index_Range);
6033 Aggr_High (J) := High_Bound (Aggr_Index_Range);
6035 Aggr_Index_Typ (J) := Etype (Index_Constraint);
6037 Next_Index (Aggr_Index_Range);
6038 Next_Index (Index_Constraint);
6040 end Index_Compatibility_Check;
6044 -- If an others choice is present check that no aggregate index is
6045 -- outside the bounds of the index constraint.
6047 Others_Check (N, 1);
6051 -- For multidimensional arrays make sure that all subaggregates
6052 -- corresponding to the same dimension have the same bounds.
6054 if Aggr_Dimension > 1 then
6055 Check_Same_Aggr_Bounds (N, 1);
6060 -- If we have a default component value, or simple initialization is
6061 -- required for the component type, then we replace <> in component
6062 -- associations by the required default value.
6065 Default_Val : Node_Id;
6069 if (Present (Default_Aspect_Component_Value (Typ))
6070 or else Needs_Simple_Initialization (Ctyp))
6071 and then Present (Component_Associations (N))
6073 Assoc := First (Component_Associations (N));
6074 while Present (Assoc) loop
6075 if Nkind (Assoc) = N_Component_Association
6076 and then Box_Present (Assoc)
6078 Set_Box_Present (Assoc, False);
6080 if Present (Default_Aspect_Component_Value (Typ)) then
6081 Default_Val := Default_Aspect_Component_Value (Typ);
6083 Default_Val := Get_Simple_Init_Val (Ctyp, N);
6086 Set_Expression (Assoc, New_Copy_Tree (Default_Val));
6087 Analyze_And_Resolve (Expression (Assoc), Ctyp);
6097 -- Here we test for is packed array aggregate that we can handle at
6098 -- compile time. If so, return with transformation done. Note that we do
6099 -- this even if the aggregate is nested, because once we have done this
6100 -- processing, there is no more nested aggregate.
6102 if Packed_Array_Aggregate_Handled (N) then
6106 -- At this point we try to convert to positional form
6108 if Ekind (Current_Scope) = E_Package
6109 and then Static_Elaboration_Desired (Current_Scope)
6111 Convert_To_Positional (N, Max_Others_Replicate => 100);
6113 Convert_To_Positional (N);
6116 -- if the result is no longer an aggregate (e.g. it may be a string
6117 -- literal, or a temporary which has the needed value), then we are
6118 -- done, since there is no longer a nested aggregate.
6120 if Nkind (N) /= N_Aggregate then
6123 -- We are also done if the result is an analyzed aggregate, indicating
6124 -- that Convert_To_Positional succeeded and reanalyzed the rewritten
6127 elsif Analyzed (N) and then Is_Rewrite_Substitution (N) then
6131 -- If all aggregate components are compile-time known and the aggregate
6132 -- has been flattened, nothing left to do. The same occurs if the
6133 -- aggregate is used to initialize the components of a statically
6134 -- allocated dispatch table.
6136 if Compile_Time_Known_Aggregate (N)
6137 or else Is_Static_Dispatch_Table_Aggregate (N)
6139 Set_Expansion_Delayed (N, False);
6143 -- Now see if back end processing is possible
6145 if Backend_Processing_Possible (N) then
6147 -- If the aggregate is static but the constraints are not, build
6148 -- a static subtype for the aggregate, so that Gigi can place it
6149 -- in static memory. Perform an unchecked_conversion to the non-
6150 -- static type imposed by the context.
6153 Itype : constant Entity_Id := Etype (N);
6155 Needs_Type : Boolean := False;
6158 Index := First_Index (Itype);
6159 while Present (Index) loop
6160 if not Is_OK_Static_Subtype (Etype (Index)) then
6169 Build_Constrained_Type (Positional => True);
6170 Rewrite (N, Unchecked_Convert_To (Itype, N));
6180 -- Delay expansion for nested aggregates: it will be taken care of when
6181 -- the parent aggregate is expanded.
6183 Parent_Node := Parent (N);
6184 Parent_Kind := Nkind (Parent_Node);
6186 if Parent_Kind = N_Qualified_Expression then
6187 Parent_Node := Parent (Parent_Node);
6188 Parent_Kind := Nkind (Parent_Node);
6191 if Parent_Kind = N_Aggregate
6192 or else Parent_Kind = N_Extension_Aggregate
6193 or else Parent_Kind = N_Component_Association
6194 or else (Parent_Kind = N_Object_Declaration
6195 and then Needs_Finalization (Typ))
6196 or else (Parent_Kind = N_Assignment_Statement
6197 and then Inside_Init_Proc)
6199 Set_Expansion_Delayed (N, not Static_Array_Aggregate (N));
6205 -- Look if in place aggregate expansion is possible
6207 -- For object declarations we build the aggregate in place, unless
6208 -- the array is bit-packed.
6210 -- For assignments we do the assignment in place if all the component
6211 -- associations have compile-time known values, or are default-
6212 -- initialized limited components, e.g. tasks. For other cases we
6213 -- create a temporary. The analysis for safety of on-line assignment
6214 -- is delicate, i.e. we don't know how to do it fully yet ???
6216 -- For allocators we assign to the designated object in place if the
6217 -- aggregate meets the same conditions as other in-place assignments.
6218 -- In this case the aggregate may not come from source but was created
6219 -- for default initialization, e.g. with Initialize_Scalars.
6221 if Requires_Transient_Scope (Typ) then
6222 Establish_Transient_Scope (N, Manage_Sec_Stack => False);
6225 -- An array of limited components is built in place
6227 if Is_Limited_Type (Typ) then
6228 Maybe_In_Place_OK := True;
6230 elsif Has_Default_Init_Comps (N) then
6231 Maybe_In_Place_OK := False;
6233 elsif Is_Bit_Packed_Array (Typ)
6234 or else Has_Controlled_Component (Typ)
6236 Maybe_In_Place_OK := False;
6239 Maybe_In_Place_OK :=
6240 (Nkind (Parent (N)) = N_Assignment_Statement
6241 and then In_Place_Assign_OK)
6244 (Nkind (Parent (Parent (N))) = N_Allocator
6245 and then In_Place_Assign_OK);
6248 -- If this is an array of tasks, it will be expanded into build-in-place
6249 -- assignments. Build an activation chain for the tasks now.
6251 if Has_Task (Etype (N)) then
6252 Build_Activation_Chain_Entity (N);
6255 -- Perform in-place expansion of aggregate in an object declaration.
6256 -- Note: actions generated for the aggregate will be captured in an
6257 -- expression-with-actions statement so that they can be transferred
6258 -- to freeze actions later if there is an address clause for the
6259 -- object. (Note: we don't use a block statement because this would
6260 -- cause generated freeze nodes to be elaborated in the wrong scope).
6262 -- Do not perform in-place expansion for SPARK 05 because aggregates are
6263 -- expected to appear in qualified form. In-place expansion eliminates
6264 -- the qualification and eventually violates this SPARK 05 restiction.
6266 -- Arrays of limited components must be built in place. The code
6267 -- previously excluded controlled components but this is an old
6268 -- oversight: the rules in 7.6 (17) are clear.
6270 if (not Has_Default_Init_Comps (N)
6271 or else Is_Limited_Type (Etype (N)))
6272 and then Comes_From_Source (Parent_Node)
6273 and then Parent_Kind = N_Object_Declaration
6274 and then Present (Expression (Parent_Node))
6276 Must_Slide (Etype (Defining_Identifier (Parent_Node)), Typ)
6277 and then not Is_Bit_Packed_Array (Typ)
6278 and then not Restriction_Check_Required (SPARK_05)
6280 In_Place_Assign_OK_For_Declaration := True;
6281 Tmp := Defining_Identifier (Parent_Node);
6282 Set_No_Initialization (Parent_Node);
6283 Set_Expression (Parent_Node, Empty);
6285 -- Set kind and type of the entity, for use in the analysis
6286 -- of the subsequent assignments. If the nominal type is not
6287 -- constrained, build a subtype from the known bounds of the
6288 -- aggregate. If the declaration has a subtype mark, use it,
6289 -- otherwise use the itype of the aggregate.
6291 Set_Ekind (Tmp, E_Variable);
6293 if not Is_Constrained (Typ) then
6294 Build_Constrained_Type (Positional => False);
6296 elsif Is_Entity_Name (Object_Definition (Parent_Node))
6297 and then Is_Constrained (Entity (Object_Definition (Parent_Node)))
6299 Set_Etype (Tmp, Entity (Object_Definition (Parent_Node)));
6302 Set_Size_Known_At_Compile_Time (Typ, False);
6303 Set_Etype (Tmp, Typ);
6306 elsif Maybe_In_Place_OK
6307 and then Nkind (Parent (N)) = N_Qualified_Expression
6308 and then Nkind (Parent (Parent (N))) = N_Allocator
6310 Set_Expansion_Delayed (N);
6313 -- Limited arrays in return statements are expanded when
6314 -- enclosing construct is expanded.
6316 elsif Maybe_In_Place_OK
6317 and then Nkind (Parent (N)) = N_Simple_Return_Statement
6319 Set_Expansion_Delayed (N);
6322 -- In the remaining cases the aggregate is the RHS of an assignment
6324 elsif Maybe_In_Place_OK
6325 and then Safe_Left_Hand_Side (Name (Parent (N)))
6327 Tmp := Name (Parent (N));
6329 if Etype (Tmp) /= Etype (N) then
6330 Apply_Length_Check (N, Etype (Tmp));
6332 if Nkind (N) = N_Raise_Constraint_Error then
6334 -- Static error, nothing further to expand
6340 -- If a slice assignment has an aggregate with a single others_choice,
6341 -- the assignment can be done in place even if bounds are not static,
6342 -- by converting it into a loop over the discrete range of the slice.
6344 elsif Maybe_In_Place_OK
6345 and then Nkind (Name (Parent (N))) = N_Slice
6346 and then Is_Others_Aggregate (N)
6348 Tmp := Name (Parent (N));
6350 -- Set type of aggregate to be type of lhs in assignment, in order
6351 -- to suppress redundant length checks.
6353 Set_Etype (N, Etype (Tmp));
6357 -- In place aggregate expansion is not possible
6360 Maybe_In_Place_OK := False;
6361 Tmp := Make_Temporary (Loc, 'A', N);
6363 Make_Object_Declaration (Loc,
6364 Defining_Identifier => Tmp,
6365 Object_Definition => New_Occurrence_Of (Typ, Loc));
6366 Set_No_Initialization (Tmp_Decl, True);
6367 Set_Warnings_Off (Tmp);
6369 -- If we are within a loop, the temporary will be pushed on the
6370 -- stack at each iteration. If the aggregate is the expression
6371 -- for an allocator, it will be immediately copied to the heap
6372 -- and can be reclaimed at once. We create a transient scope
6373 -- around the aggregate for this purpose.
6375 if Ekind (Current_Scope) = E_Loop
6376 and then Nkind (Parent (Parent (N))) = N_Allocator
6378 Establish_Transient_Scope (N, Manage_Sec_Stack => False);
6381 Insert_Action (N, Tmp_Decl);
6384 -- Construct and insert the aggregate code. We can safely suppress index
6385 -- checks because this code is guaranteed not to raise CE on index
6386 -- checks. However we should *not* suppress all checks.
6392 if Nkind (Tmp) = N_Defining_Identifier then
6393 Target := New_Occurrence_Of (Tmp, Loc);
6396 if Has_Default_Init_Comps (N)
6397 and then not Maybe_In_Place_OK
6399 -- Ada 2005 (AI-287): This case has not been analyzed???
6401 raise Program_Error;
6404 -- Name in assignment is explicit dereference
6406 Target := New_Copy (Tmp);
6409 -- If we are to generate an in place assignment for a declaration or
6410 -- an assignment statement, and the assignment can be done directly
6411 -- by the back end, then do not expand further.
6413 -- ??? We can also do that if in place expansion is not possible but
6414 -- then we could go into an infinite recursion.
6416 if (In_Place_Assign_OK_For_Declaration or else Maybe_In_Place_OK)
6417 and then not CodePeer_Mode
6418 and then not Modify_Tree_For_C
6419 and then not Possible_Bit_Aligned_Component (Target)
6420 and then not Is_Possibly_Unaligned_Slice (Target)
6421 and then Aggr_Assignment_OK_For_Backend (N)
6423 if Maybe_In_Place_OK then
6429 Make_Assignment_Statement (Loc,
6431 Expression => New_Copy_Tree (N)));
6435 Build_Array_Aggr_Code (N,
6437 Index => First_Index (Typ),
6439 Scalar_Comp => Is_Scalar_Type (Ctyp));
6442 -- Save the last assignment statement associated with the aggregate
6443 -- when building a controlled object. This reference is utilized by
6444 -- the finalization machinery when marking an object as successfully
6447 if Needs_Finalization (Typ)
6448 and then Is_Entity_Name (Target)
6449 and then Present (Entity (Target))
6450 and then Ekind_In (Entity (Target), E_Constant, E_Variable)
6452 Set_Last_Aggregate_Assignment (Entity (Target), Last (Aggr_Code));
6456 -- If the aggregate is the expression in a declaration, the expanded
6457 -- code must be inserted after it. The defining entity might not come
6458 -- from source if this is part of an inlined body, but the declaration
6461 if Comes_From_Source (Tmp)
6463 (Nkind (Parent (N)) = N_Object_Declaration
6464 and then Comes_From_Source (Parent (N))
6465 and then Tmp = Defining_Entity (Parent (N)))
6468 Node_After : constant Node_Id := Next (Parent_Node);
6471 Insert_Actions_After (Parent_Node, Aggr_Code);
6473 if Parent_Kind = N_Object_Declaration then
6474 Collect_Initialization_Statements
6475 (Obj => Tmp, N => Parent_Node, Node_After => Node_After);
6480 Insert_Actions (N, Aggr_Code);
6483 -- If the aggregate has been assigned in place, remove the original
6486 if Nkind (Parent (N)) = N_Assignment_Statement
6487 and then Maybe_In_Place_OK
6489 Rewrite (Parent (N), Make_Null_Statement (Loc));
6491 elsif Nkind (Parent (N)) /= N_Object_Declaration
6492 or else Tmp /= Defining_Identifier (Parent (N))
6494 Rewrite (N, New_Occurrence_Of (Tmp, Loc));
6495 Analyze_And_Resolve (N, Typ);
6497 end Expand_Array_Aggregate;
6499 ------------------------
6500 -- Expand_N_Aggregate --
6501 ------------------------
6503 procedure Expand_N_Aggregate (N : Node_Id) is
6505 -- Record aggregate case
6507 if Is_Record_Type (Etype (N)) then
6508 Expand_Record_Aggregate (N);
6510 -- Array aggregate case
6513 -- A special case, if we have a string subtype with bounds 1 .. N,
6514 -- where N is known at compile time, and the aggregate is of the
6515 -- form (others => 'x'), with a single choice and no expressions,
6516 -- and N is less than 80 (an arbitrary limit for now), then replace
6517 -- the aggregate by the equivalent string literal (but do not mark
6518 -- it as static since it is not).
6520 -- Note: this entire circuit is redundant with respect to code in
6521 -- Expand_Array_Aggregate that collapses others choices to positional
6522 -- form, but there are two problems with that circuit:
6524 -- a) It is limited to very small cases due to ill-understood
6525 -- interactions with bootstrapping. That limit is removed by
6526 -- use of the No_Implicit_Loops restriction.
6528 -- b) It incorrectly ends up with the resulting expressions being
6529 -- considered static when they are not. For example, the
6530 -- following test should fail:
6532 -- pragma Restrictions (No_Implicit_Loops);
6533 -- package NonSOthers4 is
6534 -- B : constant String (1 .. 6) := (others => 'A');
6535 -- DH : constant String (1 .. 8) := B & "BB";
6537 -- pragma Export (C, X, Link_Name => DH);
6540 -- But it succeeds (DH looks static to pragma Export)
6542 -- To be sorted out ???
6544 if Present (Component_Associations (N)) then
6546 CA : constant Node_Id := First (Component_Associations (N));
6547 MX : constant := 80;
6550 if Nkind (First (Choice_List (CA))) = N_Others_Choice
6551 and then Nkind (Expression (CA)) = N_Character_Literal
6552 and then No (Expressions (N))
6555 T : constant Entity_Id := Etype (N);
6556 X : constant Node_Id := First_Index (T);
6557 EC : constant Node_Id := Expression (CA);
6558 CV : constant Uint := Char_Literal_Value (EC);
6559 CC : constant Int := UI_To_Int (CV);
6562 if Nkind (X) = N_Range
6563 and then Compile_Time_Known_Value (Low_Bound (X))
6564 and then Expr_Value (Low_Bound (X)) = 1
6565 and then Compile_Time_Known_Value (High_Bound (X))
6568 Hi : constant Uint := Expr_Value (High_Bound (X));
6574 for J in 1 .. UI_To_Int (Hi) loop
6575 Store_String_Char (Char_Code (CC));
6579 Make_String_Literal (Sloc (N),
6580 Strval => End_String));
6582 if CC >= Int (2 ** 16) then
6583 Set_Has_Wide_Wide_Character (N);
6584 elsif CC >= Int (2 ** 8) then
6585 Set_Has_Wide_Character (N);
6588 Analyze_And_Resolve (N, T);
6589 Set_Is_Static_Expression (N, False);
6599 -- Not that special case, so normal expansion of array aggregate
6601 Expand_Array_Aggregate (N);
6605 when RE_Not_Available =>
6607 end Expand_N_Aggregate;
6609 ------------------------------
6610 -- Expand_N_Delta_Aggregate --
6611 ------------------------------
6613 procedure Expand_N_Delta_Aggregate (N : Node_Id) is
6614 Loc : constant Source_Ptr := Sloc (N);
6615 Typ : constant Entity_Id := Etype (N);
6620 Make_Object_Declaration (Loc,
6621 Defining_Identifier => Make_Temporary (Loc, 'T'),
6622 Object_Definition => New_Occurrence_Of (Typ, Loc),
6623 Expression => New_Copy_Tree (Expression (N)));
6625 if Is_Array_Type (Etype (N)) then
6626 Expand_Delta_Array_Aggregate (N, New_List (Decl));
6628 Expand_Delta_Record_Aggregate (N, New_List (Decl));
6630 end Expand_N_Delta_Aggregate;
6632 ----------------------------------
6633 -- Expand_Delta_Array_Aggregate --
6634 ----------------------------------
6636 procedure Expand_Delta_Array_Aggregate (N : Node_Id; Deltas : List_Id) is
6637 Loc : constant Source_Ptr := Sloc (N);
6638 Temp : constant Entity_Id := Defining_Identifier (First (Deltas));
6641 function Generate_Loop (C : Node_Id) return Node_Id;
6642 -- Generate a loop containing individual component assignments for
6643 -- choices that are ranges, subtype indications, subtype names, and
6644 -- iterated component associations.
6650 function Generate_Loop (C : Node_Id) return Node_Id is
6651 Sl : constant Source_Ptr := Sloc (C);
6655 if Nkind (Parent (C)) = N_Iterated_Component_Association then
6657 Make_Defining_Identifier (Loc,
6658 Chars => (Chars (Defining_Identifier (Parent (C)))));
6660 Ix := Make_Temporary (Sl, 'I');
6664 Make_Loop_Statement (Loc,
6666 Make_Iteration_Scheme (Sl,
6667 Loop_Parameter_Specification =>
6668 Make_Loop_Parameter_Specification (Sl,
6669 Defining_Identifier => Ix,
6670 Discrete_Subtype_Definition => New_Copy_Tree (C))),
6672 Statements => New_List (
6673 Make_Assignment_Statement (Sl,
6675 Make_Indexed_Component (Sl,
6676 Prefix => New_Occurrence_Of (Temp, Sl),
6677 Expressions => New_List (New_Occurrence_Of (Ix, Sl))),
6678 Expression => New_Copy_Tree (Expression (Assoc)))),
6679 End_Label => Empty);
6686 -- Start of processing for Expand_Delta_Array_Aggregate
6689 Assoc := First (Component_Associations (N));
6690 while Present (Assoc) loop
6691 Choice := First (Choice_List (Assoc));
6692 if Nkind (Assoc) = N_Iterated_Component_Association then
6693 while Present (Choice) loop
6694 Append_To (Deltas, Generate_Loop (Choice));
6699 while Present (Choice) loop
6701 -- Choice can be given by a range, a subtype indication, a
6702 -- subtype name, a scalar value, or an entity.
6704 if Nkind (Choice) = N_Range
6705 or else (Is_Entity_Name (Choice)
6706 and then Is_Type (Entity (Choice)))
6708 Append_To (Deltas, Generate_Loop (Choice));
6710 elsif Nkind (Choice) = N_Subtype_Indication then
6712 Generate_Loop (Range_Expression (Constraint (Choice))));
6716 Make_Assignment_Statement (Sloc (Choice),
6718 Make_Indexed_Component (Sloc (Choice),
6719 Prefix => New_Occurrence_Of (Temp, Loc),
6720 Expressions => New_List (New_Copy_Tree (Choice))),
6721 Expression => New_Copy_Tree (Expression (Assoc))));
6731 Insert_Actions (N, Deltas);
6732 Rewrite (N, New_Occurrence_Of (Temp, Loc));
6733 end Expand_Delta_Array_Aggregate;
6735 -----------------------------------
6736 -- Expand_Delta_Record_Aggregate --
6737 -----------------------------------
6739 procedure Expand_Delta_Record_Aggregate (N : Node_Id; Deltas : List_Id) is
6740 Loc : constant Source_Ptr := Sloc (N);
6741 Temp : constant Entity_Id := Defining_Identifier (First (Deltas));
6746 Assoc := First (Component_Associations (N));
6748 while Present (Assoc) loop
6749 Choice := First (Choice_List (Assoc));
6750 while Present (Choice) loop
6752 Make_Assignment_Statement (Sloc (Choice),
6754 Make_Selected_Component (Sloc (Choice),
6755 Prefix => New_Occurrence_Of (Temp, Loc),
6756 Selector_Name => Make_Identifier (Loc, Chars (Choice))),
6757 Expression => New_Copy_Tree (Expression (Assoc))));
6764 Insert_Actions (N, Deltas);
6765 Rewrite (N, New_Occurrence_Of (Temp, Loc));
6766 end Expand_Delta_Record_Aggregate;
6768 ----------------------------------
6769 -- Expand_N_Extension_Aggregate --
6770 ----------------------------------
6772 -- If the ancestor part is an expression, add a component association for
6773 -- the parent field. If the type of the ancestor part is not the direct
6774 -- parent of the expected type, build recursively the needed ancestors.
6775 -- If the ancestor part is a subtype_mark, replace aggregate with a
6776 -- declaration for a temporary of the expected type, followed by
6777 -- individual assignments to the given components.
6779 procedure Expand_N_Extension_Aggregate (N : Node_Id) is
6780 A : constant Node_Id := Ancestor_Part (N);
6781 Loc : constant Source_Ptr := Sloc (N);
6782 Typ : constant Entity_Id := Etype (N);
6785 -- If the ancestor is a subtype mark, an init proc must be called
6786 -- on the resulting object which thus has to be materialized in
6789 if Is_Entity_Name (A) and then Is_Type (Entity (A)) then
6790 Convert_To_Assignments (N, Typ);
6792 -- The extension aggregate is transformed into a record aggregate
6793 -- of the following form (c1 and c2 are inherited components)
6795 -- (Exp with c3 => a, c4 => b)
6796 -- ==> (c1 => Exp.c1, c2 => Exp.c2, c3 => a, c4 => b)
6801 if Tagged_Type_Expansion then
6802 Expand_Record_Aggregate (N,
6805 (Node (First_Elmt (Access_Disp_Table (Typ))), Loc),
6808 -- No tag is needed in the case of a VM
6811 Expand_Record_Aggregate (N, Parent_Expr => A);
6816 when RE_Not_Available =>
6818 end Expand_N_Extension_Aggregate;
6820 -----------------------------
6821 -- Expand_Record_Aggregate --
6822 -----------------------------
6824 procedure Expand_Record_Aggregate
6826 Orig_Tag : Node_Id := Empty;
6827 Parent_Expr : Node_Id := Empty)
6829 Loc : constant Source_Ptr := Sloc (N);
6830 Comps : constant List_Id := Component_Associations (N);
6831 Typ : constant Entity_Id := Etype (N);
6832 Base_Typ : constant Entity_Id := Base_Type (Typ);
6834 Static_Components : Boolean := True;
6835 -- Flag to indicate whether all components are compile-time known,
6836 -- and the aggregate can be constructed statically and handled by
6837 -- the back-end. Set to False by Component_OK_For_Backend.
6839 procedure Build_Back_End_Aggregate;
6840 -- Build a proper aggregate to be handled by the back-end
6842 function Compile_Time_Known_Composite_Value (N : Node_Id) return Boolean;
6843 -- Returns true if N is an expression of composite type which can be
6844 -- fully evaluated at compile time without raising constraint error.
6845 -- Such expressions can be passed as is to Gigi without any expansion.
6847 -- This returns true for N_Aggregate with Compile_Time_Known_Aggregate
6848 -- set and constants whose expression is such an aggregate, recursively.
6850 function Component_OK_For_Backend return Boolean;
6851 -- Check for presence of a component which makes it impossible for the
6852 -- backend to process the aggregate, thus requiring the use of a series
6853 -- of assignment statements. Cases checked for are a nested aggregate
6854 -- needing Late_Expansion, the presence of a tagged component which may
6855 -- need tag adjustment, and a bit unaligned component reference.
6857 -- We also force expansion into assignments if a component is of a
6858 -- mutable type (including a private type with discriminants) because
6859 -- in that case the size of the component to be copied may be smaller
6860 -- than the side of the target, and there is no simple way for gigi
6861 -- to compute the size of the object to be copied.
6863 -- NOTE: This is part of the ongoing work to define precisely the
6864 -- interface between front-end and back-end handling of aggregates.
6865 -- In general it is desirable to pass aggregates as they are to gigi,
6866 -- in order to minimize elaboration code. This is one case where the
6867 -- semantics of Ada complicate the analysis and lead to anomalies in
6868 -- the gcc back-end if the aggregate is not expanded into assignments.
6870 -- NOTE: This sets the global Static_Components to False in most, but
6871 -- not all, cases when it returns False.
6873 function Has_Per_Object_Constraint (L : List_Id) return Boolean;
6874 -- Return True if any element of L has Has_Per_Object_Constraint set.
6875 -- L should be the Choices component of an N_Component_Association.
6877 function Has_Visible_Private_Ancestor (Id : E) return Boolean;
6878 -- If any ancestor of the current type is private, the aggregate
6879 -- cannot be built in place. We cannot rely on Has_Private_Ancestor,
6880 -- because it will not be set when type and its parent are in the
6881 -- same scope, and the parent component needs expansion.
6883 function Top_Level_Aggregate (N : Node_Id) return Node_Id;
6884 -- For nested aggregates return the ultimate enclosing aggregate; for
6885 -- non-nested aggregates return N.
6887 ------------------------------
6888 -- Build_Back_End_Aggregate --
6889 ------------------------------
6891 procedure Build_Back_End_Aggregate is
6894 Tag_Value : Node_Id;
6897 if Nkind (N) = N_Aggregate then
6899 -- If the aggregate is static and can be handled by the back-end,
6900 -- nothing left to do.
6902 if Static_Components then
6903 Set_Compile_Time_Known_Aggregate (N);
6904 Set_Expansion_Delayed (N, False);
6908 -- If no discriminants, nothing special to do
6910 if not Has_Discriminants (Typ) then
6913 -- Case of discriminants present
6915 elsif Is_Derived_Type (Typ) then
6917 -- For untagged types, non-stored discriminants are replaced with
6918 -- stored discriminants, which are the ones that gigi uses to
6919 -- describe the type and its components.
6921 Generate_Aggregate_For_Derived_Type : declare
6922 procedure Prepend_Stored_Values (T : Entity_Id);
6923 -- Scan the list of stored discriminants of the type, and add
6924 -- their values to the aggregate being built.
6926 ---------------------------
6927 -- Prepend_Stored_Values --
6928 ---------------------------
6930 procedure Prepend_Stored_Values (T : Entity_Id) is
6932 First_Comp : Node_Id := Empty;
6935 Discr := First_Stored_Discriminant (T);
6936 while Present (Discr) loop
6938 Make_Component_Association (Loc,
6939 Choices => New_List (
6940 New_Occurrence_Of (Discr, Loc)),
6943 (Get_Discriminant_Value
6946 Discriminant_Constraint (Typ))));
6948 if No (First_Comp) then
6949 Prepend_To (Component_Associations (N), New_Comp);
6951 Insert_After (First_Comp, New_Comp);
6954 First_Comp := New_Comp;
6955 Next_Stored_Discriminant (Discr);
6957 end Prepend_Stored_Values;
6961 Constraints : constant List_Id := New_List;
6965 Num_Disc : Nat := 0;
6966 Num_Gird : Nat := 0;
6968 -- Start of processing for Generate_Aggregate_For_Derived_Type
6971 -- Remove the associations for the discriminant of derived type
6974 First_Comp : Node_Id;
6977 First_Comp := First (Component_Associations (N));
6978 while Present (First_Comp) loop
6982 if Ekind (Entity (First (Choices (Comp)))) =
6986 Num_Disc := Num_Disc + 1;
6991 -- Insert stored discriminant associations in the correct
6992 -- order. If there are more stored discriminants than new
6993 -- discriminants, there is at least one new discriminant that
6994 -- constrains more than one of the stored discriminants. In
6995 -- this case we need to construct a proper subtype of the
6996 -- parent type, in order to supply values to all the
6997 -- components. Otherwise there is one-one correspondence
6998 -- between the constraints and the stored discriminants.
7000 Discr := First_Stored_Discriminant (Base_Type (Typ));
7001 while Present (Discr) loop
7002 Num_Gird := Num_Gird + 1;
7003 Next_Stored_Discriminant (Discr);
7006 -- Case of more stored discriminants than new discriminants
7008 if Num_Gird > Num_Disc then
7010 -- Create a proper subtype of the parent type, which is the
7011 -- proper implementation type for the aggregate, and convert
7012 -- it to the intended target type.
7014 Discr := First_Stored_Discriminant (Base_Type (Typ));
7015 while Present (Discr) loop
7018 (Get_Discriminant_Value
7021 Discriminant_Constraint (Typ)));
7023 Append (New_Comp, Constraints);
7024 Next_Stored_Discriminant (Discr);
7028 Make_Subtype_Declaration (Loc,
7029 Defining_Identifier => Make_Temporary (Loc, 'T'),
7030 Subtype_Indication =>
7031 Make_Subtype_Indication (Loc,
7033 New_Occurrence_Of (Etype (Base_Type (Typ)), Loc),
7035 Make_Index_Or_Discriminant_Constraint
7036 (Loc, Constraints)));
7038 Insert_Action (N, Decl);
7039 Prepend_Stored_Values (Base_Type (Typ));
7041 Set_Etype (N, Defining_Identifier (Decl));
7044 Rewrite (N, Unchecked_Convert_To (Typ, N));
7047 -- Case where we do not have fewer new discriminants than
7048 -- stored discriminants, so in this case we can simply use the
7049 -- stored discriminants of the subtype.
7052 Prepend_Stored_Values (Typ);
7054 end Generate_Aggregate_For_Derived_Type;
7057 if Is_Tagged_Type (Typ) then
7059 -- In the tagged case, _parent and _tag component must be created
7061 -- Reset Null_Present unconditionally. Tagged records always have
7062 -- at least one field (the tag or the parent).
7064 Set_Null_Record_Present (N, False);
7066 -- When the current aggregate comes from the expansion of an
7067 -- extension aggregate, the parent expr is replaced by an
7068 -- aggregate formed by selected components of this expr.
7070 if Present (Parent_Expr) and then Is_Empty_List (Comps) then
7071 Comp := First_Component_Or_Discriminant (Typ);
7072 while Present (Comp) loop
7074 -- Skip all expander-generated components
7076 if not Comes_From_Source (Original_Record_Component (Comp))
7082 Make_Selected_Component (Loc,
7084 Unchecked_Convert_To (Typ,
7085 Duplicate_Subexpr (Parent_Expr, True)),
7086 Selector_Name => New_Occurrence_Of (Comp, Loc));
7089 Make_Component_Association (Loc,
7090 Choices => New_List (
7091 New_Occurrence_Of (Comp, Loc)),
7092 Expression => New_Comp));
7094 Analyze_And_Resolve (New_Comp, Etype (Comp));
7097 Next_Component_Or_Discriminant (Comp);
7101 -- Compute the value for the Tag now, if the type is a root it
7102 -- will be included in the aggregate right away, otherwise it will
7103 -- be propagated to the parent aggregate.
7105 if Present (Orig_Tag) then
7106 Tag_Value := Orig_Tag;
7108 elsif not Tagged_Type_Expansion then
7114 (Node (First_Elmt (Access_Disp_Table (Typ))), Loc);
7117 -- For a derived type, an aggregate for the parent is formed with
7118 -- all the inherited components.
7120 if Is_Derived_Type (Typ) then
7122 First_Comp : Node_Id;
7123 Parent_Comps : List_Id;
7124 Parent_Aggr : Node_Id;
7125 Parent_Name : Node_Id;
7128 -- Remove the inherited component association from the
7129 -- aggregate and store them in the parent aggregate
7131 First_Comp := First (Component_Associations (N));
7132 Parent_Comps := New_List;
7133 while Present (First_Comp)
7135 Scope (Original_Record_Component
7136 (Entity (First (Choices (First_Comp))))) /=
7142 Append (Comp, Parent_Comps);
7146 Make_Aggregate (Loc,
7147 Component_Associations => Parent_Comps);
7148 Set_Etype (Parent_Aggr, Etype (Base_Type (Typ)));
7150 -- Find the _parent component
7152 Comp := First_Component (Typ);
7153 while Chars (Comp) /= Name_uParent loop
7154 Comp := Next_Component (Comp);
7157 Parent_Name := New_Occurrence_Of (Comp, Loc);
7159 -- Insert the parent aggregate
7161 Prepend_To (Component_Associations (N),
7162 Make_Component_Association (Loc,
7163 Choices => New_List (Parent_Name),
7164 Expression => Parent_Aggr));
7166 -- Expand recursively the parent propagating the right Tag
7168 Expand_Record_Aggregate
7169 (Parent_Aggr, Tag_Value, Parent_Expr);
7171 -- The ancestor part may be a nested aggregate that has
7172 -- delayed expansion: recheck now.
7174 if not Component_OK_For_Backend then
7175 Convert_To_Assignments (N, Typ);
7179 -- For a root type, the tag component is added (unless compiling
7180 -- for the VMs, where tags are implicit).
7182 elsif Tagged_Type_Expansion then
7184 Tag_Name : constant Node_Id :=
7186 (First_Tag_Component (Typ), Loc);
7187 Typ_Tag : constant Entity_Id := RTE (RE_Tag);
7188 Conv_Node : constant Node_Id :=
7189 Unchecked_Convert_To (Typ_Tag, Tag_Value);
7192 Set_Etype (Conv_Node, Typ_Tag);
7193 Prepend_To (Component_Associations (N),
7194 Make_Component_Association (Loc,
7195 Choices => New_List (Tag_Name),
7196 Expression => Conv_Node));
7200 end Build_Back_End_Aggregate;
7202 ----------------------------------------
7203 -- Compile_Time_Known_Composite_Value --
7204 ----------------------------------------
7206 function Compile_Time_Known_Composite_Value
7207 (N : Node_Id) return Boolean
7210 -- If we have an entity name, then see if it is the name of a
7211 -- constant and if so, test the corresponding constant value.
7213 if Is_Entity_Name (N) then
7215 E : constant Entity_Id := Entity (N);
7218 if Ekind (E) /= E_Constant then
7221 V := Constant_Value (E);
7223 and then Compile_Time_Known_Composite_Value (V);
7227 -- We have a value, see if it is compile time known
7230 if Nkind (N) = N_Aggregate then
7231 return Compile_Time_Known_Aggregate (N);
7234 -- All other types of values are not known at compile time
7239 end Compile_Time_Known_Composite_Value;
7241 ------------------------------
7242 -- Component_OK_For_Backend --
7243 ------------------------------
7245 function Component_OK_For_Backend return Boolean is
7255 while Present (C) loop
7257 -- If the component has box initialization, expansion is needed
7258 -- and component is not ready for backend.
7260 if Box_Present (C) then
7264 if Nkind (Expression (C)) = N_Qualified_Expression then
7265 Expr_Q := Expression (Expression (C));
7267 Expr_Q := Expression (C);
7270 -- Return False for array components whose bounds raise
7271 -- constraint error.
7274 Comp : constant Entity_Id := First (Choices (C));
7278 if Present (Etype (Comp))
7279 and then Is_Array_Type (Etype (Comp))
7281 Indx := First_Index (Etype (Comp));
7282 while Present (Indx) loop
7283 if Nkind (Type_Low_Bound (Etype (Indx))) =
7284 N_Raise_Constraint_Error
7285 or else Nkind (Type_High_Bound (Etype (Indx))) =
7286 N_Raise_Constraint_Error
7291 Indx := Next_Index (Indx);
7296 -- Return False if the aggregate has any associations for tagged
7297 -- components that may require tag adjustment.
7299 -- These are cases where the source expression may have a tag that
7300 -- could differ from the component tag (e.g., can occur for type
7301 -- conversions and formal parameters). (Tag adjustment not needed
7302 -- if Tagged_Type_Expansion because object tags are implicit in
7305 if Is_Tagged_Type (Etype (Expr_Q))
7307 (Nkind (Expr_Q) = N_Type_Conversion
7309 (Is_Entity_Name (Expr_Q)
7310 and then Is_Formal (Entity (Expr_Q))))
7311 and then Tagged_Type_Expansion
7313 Static_Components := False;
7316 elsif Is_Delayed_Aggregate (Expr_Q) then
7317 Static_Components := False;
7320 elsif Nkind (Expr_Q) = N_Quantified_Expression then
7321 Static_Components := False;
7324 elsif Possible_Bit_Aligned_Component (Expr_Q) then
7325 Static_Components := False;
7328 elsif Modify_Tree_For_C
7329 and then Nkind (C) = N_Component_Association
7330 and then Has_Per_Object_Constraint (Choices (C))
7332 Static_Components := False;
7335 elsif Modify_Tree_For_C
7336 and then Nkind (Expr_Q) = N_Identifier
7337 and then Is_Array_Type (Etype (Expr_Q))
7339 Static_Components := False;
7342 elsif Modify_Tree_For_C
7343 and then Nkind (Expr_Q) = N_Type_Conversion
7344 and then Is_Array_Type (Etype (Expr_Q))
7346 Static_Components := False;
7350 if Is_Elementary_Type (Etype (Expr_Q)) then
7351 if not Compile_Time_Known_Value (Expr_Q) then
7352 Static_Components := False;
7355 elsif not Compile_Time_Known_Composite_Value (Expr_Q) then
7356 Static_Components := False;
7358 if Is_Private_Type (Etype (Expr_Q))
7359 and then Has_Discriminants (Etype (Expr_Q))
7369 end Component_OK_For_Backend;
7371 -------------------------------
7372 -- Has_Per_Object_Constraint --
7373 -------------------------------
7375 function Has_Per_Object_Constraint (L : List_Id) return Boolean is
7376 N : Node_Id := First (L);
7378 while Present (N) loop
7379 if Is_Entity_Name (N)
7380 and then Present (Entity (N))
7381 and then Has_Per_Object_Constraint (Entity (N))
7390 end Has_Per_Object_Constraint;
7392 -----------------------------------
7393 -- Has_Visible_Private_Ancestor --
7394 -----------------------------------
7396 function Has_Visible_Private_Ancestor (Id : E) return Boolean is
7397 R : constant Entity_Id := Root_Type (Id);
7398 T1 : Entity_Id := Id;
7402 if Is_Private_Type (T1) then
7412 end Has_Visible_Private_Ancestor;
7414 -------------------------
7415 -- Top_Level_Aggregate --
7416 -------------------------
7418 function Top_Level_Aggregate (N : Node_Id) return Node_Id is
7423 while Present (Parent (Aggr))
7424 and then Nkind_In (Parent (Aggr), N_Aggregate,
7425 N_Component_Association)
7427 Aggr := Parent (Aggr);
7431 end Top_Level_Aggregate;
7435 Top_Level_Aggr : constant Node_Id := Top_Level_Aggregate (N);
7437 -- Start of processing for Expand_Record_Aggregate
7440 -- If the aggregate is to be assigned to an atomic/VFA variable, we have
7441 -- to prevent a piecemeal assignment even if the aggregate is to be
7442 -- expanded. We create a temporary for the aggregate, and assign the
7443 -- temporary instead, so that the back end can generate an atomic move
7446 if Is_Atomic_VFA_Aggregate (N) then
7449 -- No special management required for aggregates used to initialize
7450 -- statically allocated dispatch tables
7452 elsif Is_Static_Dispatch_Table_Aggregate (N) then
7456 -- Ada 2005 (AI-318-2): We need to convert to assignments if components
7457 -- are build-in-place function calls. The assignments will each turn
7458 -- into a build-in-place function call. If components are all static,
7459 -- we can pass the aggregate to the back end regardless of limitedness.
7461 -- Extension aggregates, aggregates in extended return statements, and
7462 -- aggregates for C++ imported types must be expanded.
7464 if Ada_Version >= Ada_2005 and then Is_Limited_View (Typ) then
7465 if not Nkind_In (Parent (N), N_Component_Association,
7466 N_Object_Declaration)
7468 Convert_To_Assignments (N, Typ);
7470 elsif Nkind (N) = N_Extension_Aggregate
7471 or else Convention (Typ) = Convention_CPP
7473 Convert_To_Assignments (N, Typ);
7475 elsif not Size_Known_At_Compile_Time (Typ)
7476 or else not Component_OK_For_Backend
7477 or else not Static_Components
7479 Convert_To_Assignments (N, Typ);
7481 -- In all other cases, build a proper aggregate to be handled by
7485 Build_Back_End_Aggregate;
7488 -- Gigi doesn't properly handle temporaries of variable size so we
7489 -- generate it in the front-end
7491 elsif not Size_Known_At_Compile_Time (Typ)
7492 and then Tagged_Type_Expansion
7494 Convert_To_Assignments (N, Typ);
7496 -- An aggregate used to initialize a controlled object must be turned
7497 -- into component assignments as the components themselves may require
7498 -- finalization actions such as adjustment.
7500 elsif Needs_Finalization (Typ) then
7501 Convert_To_Assignments (N, Typ);
7503 -- Ada 2005 (AI-287): In case of default initialized components we
7504 -- convert the aggregate into assignments.
7506 elsif Has_Default_Init_Comps (N) then
7507 Convert_To_Assignments (N, Typ);
7511 elsif not Component_OK_For_Backend then
7512 Convert_To_Assignments (N, Typ);
7514 -- If an ancestor is private, some components are not inherited and we
7515 -- cannot expand into a record aggregate.
7517 elsif Has_Visible_Private_Ancestor (Typ) then
7518 Convert_To_Assignments (N, Typ);
7520 -- ??? The following was done to compile fxacc00.ads in the ACVCs. Gigi
7521 -- is not able to handle the aggregate for Late_Request.
7523 elsif Is_Tagged_Type (Typ) and then Has_Discriminants (Typ) then
7524 Convert_To_Assignments (N, Typ);
7526 -- If the tagged types covers interface types we need to initialize all
7527 -- hidden components containing pointers to secondary dispatch tables.
7529 elsif Is_Tagged_Type (Typ) and then Has_Interfaces (Typ) then
7530 Convert_To_Assignments (N, Typ);
7532 -- If some components are mutable, the size of the aggregate component
7533 -- may be distinct from the default size of the type component, so
7534 -- we need to expand to insure that the back-end copies the proper
7535 -- size of the data. However, if the aggregate is the initial value of
7536 -- a constant, the target is immutable and might be built statically
7537 -- if components are appropriate.
7539 elsif Has_Mutable_Components (Typ)
7541 (Nkind (Parent (Top_Level_Aggr)) /= N_Object_Declaration
7542 or else not Constant_Present (Parent (Top_Level_Aggr))
7543 or else not Static_Components)
7545 Convert_To_Assignments (N, Typ);
7547 -- If the type involved has bit aligned components, then we are not sure
7548 -- that the back end can handle this case correctly.
7550 elsif Type_May_Have_Bit_Aligned_Components (Typ) then
7551 Convert_To_Assignments (N, Typ);
7553 -- When generating C, only generate an aggregate when declaring objects
7554 -- since C does not support aggregates in e.g. assignment statements.
7556 elsif Modify_Tree_For_C and then not In_Object_Declaration (N) then
7557 Convert_To_Assignments (N, Typ);
7559 -- In all other cases, build a proper aggregate to be handled by gigi
7562 Build_Back_End_Aggregate;
7564 end Expand_Record_Aggregate;
7566 ----------------------------
7567 -- Has_Default_Init_Comps --
7568 ----------------------------
7570 function Has_Default_Init_Comps (N : Node_Id) return Boolean is
7571 Comps : constant List_Id := Component_Associations (N);
7576 pragma Assert (Nkind_In (N, N_Aggregate, N_Extension_Aggregate));
7582 if Has_Self_Reference (N) then
7586 -- Check if any direct component has default initialized components
7589 while Present (C) loop
7590 if Box_Present (C) then
7597 -- Recursive call in case of aggregate expression
7600 while Present (C) loop
7601 Expr := Expression (C);
7604 and then Nkind_In (Expr, N_Aggregate, N_Extension_Aggregate)
7605 and then Has_Default_Init_Comps (Expr)
7614 end Has_Default_Init_Comps;
7616 ----------------------------------------
7617 -- Is_Build_In_Place_Aggregate_Return --
7618 ----------------------------------------
7620 function Is_Build_In_Place_Aggregate_Return (N : Node_Id) return Boolean is
7621 P : Node_Id := Parent (N);
7624 while Nkind (P) = N_Qualified_Expression loop
7628 if Nkind (P) = N_Simple_Return_Statement then
7631 elsif Nkind (Parent (P)) = N_Extended_Return_Statement then
7639 Is_Build_In_Place_Function
7640 (Return_Applies_To (Return_Statement_Entity (P)));
7641 end Is_Build_In_Place_Aggregate_Return;
7643 --------------------------
7644 -- Is_Delayed_Aggregate --
7645 --------------------------
7647 function Is_Delayed_Aggregate (N : Node_Id) return Boolean is
7648 Node : Node_Id := N;
7649 Kind : Node_Kind := Nkind (Node);
7652 if Kind = N_Qualified_Expression then
7653 Node := Expression (Node);
7654 Kind := Nkind (Node);
7657 if not Nkind_In (Kind, N_Aggregate, N_Extension_Aggregate) then
7660 return Expansion_Delayed (Node);
7662 end Is_Delayed_Aggregate;
7664 ---------------------------
7665 -- In_Object_Declaration --
7666 ---------------------------
7668 function In_Object_Declaration (N : Node_Id) return Boolean is
7669 P : Node_Id := Parent (N);
7671 while Present (P) loop
7672 if Nkind (P) = N_Object_Declaration then
7680 end In_Object_Declaration;
7682 ----------------------------------------
7683 -- Is_Static_Dispatch_Table_Aggregate --
7684 ----------------------------------------
7686 function Is_Static_Dispatch_Table_Aggregate (N : Node_Id) return Boolean is
7687 Typ : constant Entity_Id := Base_Type (Etype (N));
7690 return Building_Static_Dispatch_Tables
7691 and then Tagged_Type_Expansion
7692 and then RTU_Loaded (Ada_Tags)
7694 -- Avoid circularity when rebuilding the compiler
7696 and then Cunit_Entity (Get_Source_Unit (N)) /= RTU_Entity (Ada_Tags)
7697 and then (Typ = RTE (RE_Dispatch_Table_Wrapper)
7699 Typ = RTE (RE_Address_Array)
7701 Typ = RTE (RE_Type_Specific_Data)
7703 Typ = RTE (RE_Tag_Table)
7705 (RTE_Available (RE_Interface_Data)
7706 and then Typ = RTE (RE_Interface_Data))
7708 (RTE_Available (RE_Interfaces_Array)
7709 and then Typ = RTE (RE_Interfaces_Array))
7711 (RTE_Available (RE_Interface_Data_Element)
7712 and then Typ = RTE (RE_Interface_Data_Element)));
7713 end Is_Static_Dispatch_Table_Aggregate;
7715 -----------------------------
7716 -- Is_Two_Dim_Packed_Array --
7717 -----------------------------
7719 function Is_Two_Dim_Packed_Array (Typ : Entity_Id) return Boolean is
7720 C : constant Int := UI_To_Int (Component_Size (Typ));
7722 return Number_Dimensions (Typ) = 2
7723 and then Is_Bit_Packed_Array (Typ)
7724 and then (C = 1 or else C = 2 or else C = 4);
7725 end Is_Two_Dim_Packed_Array;
7727 --------------------
7728 -- Late_Expansion --
7729 --------------------
7731 function Late_Expansion
7734 Target : Node_Id) return List_Id
7736 Aggr_Code : List_Id;
7739 if Is_Array_Type (Etype (N)) then
7741 Build_Array_Aggr_Code
7743 Ctype => Component_Type (Etype (N)),
7744 Index => First_Index (Typ),
7746 Scalar_Comp => Is_Scalar_Type (Component_Type (Typ)),
7747 Indexes => No_List);
7749 -- Directly or indirectly (e.g. access protected procedure) a record
7752 Aggr_Code := Build_Record_Aggr_Code (N, Typ, Target);
7755 -- Save the last assignment statement associated with the aggregate
7756 -- when building a controlled object. This reference is utilized by
7757 -- the finalization machinery when marking an object as successfully
7760 if Needs_Finalization (Typ)
7761 and then Is_Entity_Name (Target)
7762 and then Present (Entity (Target))
7763 and then Ekind_In (Entity (Target), E_Constant, E_Variable)
7765 Set_Last_Aggregate_Assignment (Entity (Target), Last (Aggr_Code));
7771 ----------------------------------
7772 -- Make_OK_Assignment_Statement --
7773 ----------------------------------
7775 function Make_OK_Assignment_Statement
7778 Expression : Node_Id) return Node_Id
7781 Set_Assignment_OK (Name);
7782 return Make_Assignment_Statement (Sloc, Name, Expression);
7783 end Make_OK_Assignment_Statement;
7785 -----------------------
7786 -- Number_Of_Choices --
7787 -----------------------
7789 function Number_Of_Choices (N : Node_Id) return Nat is
7793 Nb_Choices : Nat := 0;
7796 if Present (Expressions (N)) then
7800 Assoc := First (Component_Associations (N));
7801 while Present (Assoc) loop
7802 Choice := First (Choice_List (Assoc));
7803 while Present (Choice) loop
7804 if Nkind (Choice) /= N_Others_Choice then
7805 Nb_Choices := Nb_Choices + 1;
7815 end Number_Of_Choices;
7817 ------------------------------------
7818 -- Packed_Array_Aggregate_Handled --
7819 ------------------------------------
7821 -- The current version of this procedure will handle at compile time
7822 -- any array aggregate that meets these conditions:
7824 -- One and two dimensional, bit packed
7825 -- Underlying packed type is modular type
7826 -- Bounds are within 32-bit Int range
7827 -- All bounds and values are static
7829 -- Note: for now, in the 2-D case, we only handle component sizes of
7830 -- 1, 2, 4 (cases where an integral number of elements occupies a byte).
7832 function Packed_Array_Aggregate_Handled (N : Node_Id) return Boolean is
7833 Loc : constant Source_Ptr := Sloc (N);
7834 Typ : constant Entity_Id := Etype (N);
7835 Ctyp : constant Entity_Id := Component_Type (Typ);
7837 Not_Handled : exception;
7838 -- Exception raised if this aggregate cannot be handled
7841 -- Handle one- or two dimensional bit packed array
7843 if not Is_Bit_Packed_Array (Typ)
7844 or else Number_Dimensions (Typ) > 2
7849 -- If two-dimensional, check whether it can be folded, and transformed
7850 -- into a one-dimensional aggregate for the Packed_Array_Impl_Type of
7851 -- the original type.
7853 if Number_Dimensions (Typ) = 2 then
7854 return Two_Dim_Packed_Array_Handled (N);
7857 if not Is_Modular_Integer_Type (Packed_Array_Impl_Type (Typ)) then
7861 if not Is_Scalar_Type (Component_Type (Typ))
7862 and then Has_Non_Standard_Rep (Component_Type (Typ))
7868 Csiz : constant Nat := UI_To_Int (Component_Size (Typ));
7872 -- Bounds of index type
7876 -- Values of bounds if compile time known
7878 function Get_Component_Val (N : Node_Id) return Uint;
7879 -- Given a expression value N of the component type Ctyp, returns a
7880 -- value of Csiz (component size) bits representing this value. If
7881 -- the value is nonstatic or any other reason exists why the value
7882 -- cannot be returned, then Not_Handled is raised.
7884 -----------------------
7885 -- Get_Component_Val --
7886 -----------------------
7888 function Get_Component_Val (N : Node_Id) return Uint is
7892 -- We have to analyze the expression here before doing any further
7893 -- processing here. The analysis of such expressions is deferred
7894 -- till expansion to prevent some problems of premature analysis.
7896 Analyze_And_Resolve (N, Ctyp);
7898 -- Must have a compile time value. String literals have to be
7899 -- converted into temporaries as well, because they cannot easily
7900 -- be converted into their bit representation.
7902 if not Compile_Time_Known_Value (N)
7903 or else Nkind (N) = N_String_Literal
7908 Val := Expr_Rep_Value (N);
7910 -- Adjust for bias, and strip proper number of bits
7912 if Has_Biased_Representation (Ctyp) then
7913 Val := Val - Expr_Value (Type_Low_Bound (Ctyp));
7916 return Val mod Uint_2 ** Csiz;
7917 end Get_Component_Val;
7919 -- Here we know we have a one dimensional bit packed array
7922 Get_Index_Bounds (First_Index (Typ), Lo, Hi);
7924 -- Cannot do anything if bounds are dynamic
7926 if not Compile_Time_Known_Value (Lo)
7928 not Compile_Time_Known_Value (Hi)
7933 -- Or are silly out of range of int bounds
7935 Lob := Expr_Value (Lo);
7936 Hib := Expr_Value (Hi);
7938 if not UI_Is_In_Int_Range (Lob)
7940 not UI_Is_In_Int_Range (Hib)
7945 -- At this stage we have a suitable aggregate for handling at compile
7946 -- time. The only remaining checks are that the values of expressions
7947 -- in the aggregate are compile-time known (checks are performed by
7948 -- Get_Component_Val), and that any subtypes or ranges are statically
7951 -- If the aggregate is not fully positional at this stage, then
7952 -- convert it to positional form. Either this will fail, in which
7953 -- case we can do nothing, or it will succeed, in which case we have
7954 -- succeeded in handling the aggregate and transforming it into a
7955 -- modular value, or it will stay an aggregate, in which case we
7956 -- have failed to create a packed value for it.
7958 if Present (Component_Associations (N)) then
7959 Convert_To_Positional
7960 (N, Max_Others_Replicate => 64, Handle_Bit_Packed => True);
7961 return Nkind (N) /= N_Aggregate;
7964 -- Otherwise we are all positional, so convert to proper value
7967 Lov : constant Int := UI_To_Int (Lob);
7968 Hiv : constant Int := UI_To_Int (Hib);
7970 Len : constant Nat := Int'Max (0, Hiv - Lov + 1);
7971 -- The length of the array (number of elements)
7973 Aggregate_Val : Uint;
7974 -- Value of aggregate. The value is set in the low order bits of
7975 -- this value. For the little-endian case, the values are stored
7976 -- from low-order to high-order and for the big-endian case the
7977 -- values are stored from high-order to low-order. Note that gigi
7978 -- will take care of the conversions to left justify the value in
7979 -- the big endian case (because of left justified modular type
7980 -- processing), so we do not have to worry about that here.
7983 -- Integer literal for resulting constructed value
7986 -- Shift count from low order for next value
7989 -- Shift increment for loop
7992 -- Next expression from positional parameters of aggregate
7994 Left_Justified : Boolean;
7995 -- Set True if we are filling the high order bits of the target
7996 -- value (i.e. the value is left justified).
7999 -- For little endian, we fill up the low order bits of the target
8000 -- value. For big endian we fill up the high order bits of the
8001 -- target value (which is a left justified modular value).
8003 Left_Justified := Bytes_Big_Endian;
8005 -- Switch justification if using -gnatd8
8007 if Debug_Flag_8 then
8008 Left_Justified := not Left_Justified;
8011 -- Switch justfification if reverse storage order
8013 if Reverse_Storage_Order (Base_Type (Typ)) then
8014 Left_Justified := not Left_Justified;
8017 if Left_Justified then
8018 Shift := Csiz * (Len - 1);
8025 -- Loop to set the values
8028 Aggregate_Val := Uint_0;
8030 Expr := First (Expressions (N));
8031 Aggregate_Val := Get_Component_Val (Expr) * Uint_2 ** Shift;
8033 for J in 2 .. Len loop
8034 Shift := Shift + Incr;
8037 Aggregate_Val + Get_Component_Val (Expr) * Uint_2 ** Shift;
8041 -- Now we can rewrite with the proper value
8043 Lit := Make_Integer_Literal (Loc, Intval => Aggregate_Val);
8044 Set_Print_In_Hex (Lit);
8046 -- Construct the expression using this literal. Note that it is
8047 -- important to qualify the literal with its proper modular type
8048 -- since universal integer does not have the required range and
8049 -- also this is a left justified modular type, which is important
8050 -- in the big-endian case.
8053 Unchecked_Convert_To (Typ,
8054 Make_Qualified_Expression (Loc,
8056 New_Occurrence_Of (Packed_Array_Impl_Type (Typ), Loc),
8057 Expression => Lit)));
8059 Analyze_And_Resolve (N, Typ);
8067 end Packed_Array_Aggregate_Handled;
8069 ----------------------------
8070 -- Has_Mutable_Components --
8071 ----------------------------
8073 function Has_Mutable_Components (Typ : Entity_Id) return Boolean is
8077 Comp := First_Component (Typ);
8078 while Present (Comp) loop
8079 if Is_Record_Type (Etype (Comp))
8080 and then Has_Discriminants (Etype (Comp))
8081 and then not Is_Constrained (Etype (Comp))
8086 Next_Component (Comp);
8090 end Has_Mutable_Components;
8092 ------------------------------
8093 -- Initialize_Discriminants --
8094 ------------------------------
8096 procedure Initialize_Discriminants (N : Node_Id; Typ : Entity_Id) is
8097 Loc : constant Source_Ptr := Sloc (N);
8098 Bas : constant Entity_Id := Base_Type (Typ);
8099 Par : constant Entity_Id := Etype (Bas);
8100 Decl : constant Node_Id := Parent (Par);
8104 if Is_Tagged_Type (Bas)
8105 and then Is_Derived_Type (Bas)
8106 and then Has_Discriminants (Par)
8107 and then Has_Discriminants (Bas)
8108 and then Number_Discriminants (Bas) /= Number_Discriminants (Par)
8109 and then Nkind (Decl) = N_Full_Type_Declaration
8110 and then Nkind (Type_Definition (Decl)) = N_Record_Definition
8112 Present (Variant_Part (Component_List (Type_Definition (Decl))))
8113 and then Nkind (N) /= N_Extension_Aggregate
8116 -- Call init proc to set discriminants.
8117 -- There should eventually be a special procedure for this ???
8119 Ref := New_Occurrence_Of (Defining_Identifier (N), Loc);
8120 Insert_Actions_After (N,
8121 Build_Initialization_Call (Sloc (N), Ref, Typ));
8123 end Initialize_Discriminants;
8130 (Obj_Type : Entity_Id;
8131 Typ : Entity_Id) return Boolean
8133 L1, L2, H1, H2 : Node_Id;
8136 -- No sliding if the type of the object is not established yet, if it is
8137 -- an unconstrained type whose actual subtype comes from the aggregate,
8138 -- or if the two types are identical.
8140 if not Is_Array_Type (Obj_Type) then
8143 elsif not Is_Constrained (Obj_Type) then
8146 elsif Typ = Obj_Type then
8150 -- Sliding can only occur along the first dimension
8152 Get_Index_Bounds (First_Index (Typ), L1, H1);
8153 Get_Index_Bounds (First_Index (Obj_Type), L2, H2);
8155 if not Is_OK_Static_Expression (L1) or else
8156 not Is_OK_Static_Expression (L2) or else
8157 not Is_OK_Static_Expression (H1) or else
8158 not Is_OK_Static_Expression (H2)
8162 return Expr_Value (L1) /= Expr_Value (L2)
8164 Expr_Value (H1) /= Expr_Value (H2);
8169 ---------------------------------
8170 -- Process_Transient_Component --
8171 ---------------------------------
8173 procedure Process_Transient_Component
8175 Comp_Typ : Entity_Id;
8176 Init_Expr : Node_Id;
8177 Fin_Call : out Node_Id;
8178 Hook_Clear : out Node_Id;
8179 Aggr : Node_Id := Empty;
8180 Stmts : List_Id := No_List)
8182 procedure Add_Item (Item : Node_Id);
8183 -- Insert arbitrary node Item into the tree depending on the values of
8190 procedure Add_Item (Item : Node_Id) is
8192 if Present (Aggr) then
8193 Insert_Action (Aggr, Item);
8195 pragma Assert (Present (Stmts));
8196 Append_To (Stmts, Item);
8202 Hook_Assign : Node_Id;
8203 Hook_Decl : Node_Id;
8207 Res_Typ : Entity_Id;
8209 -- Start of processing for Process_Transient_Component
8212 -- Add the access type, which provides a reference to the function
8213 -- result. Generate:
8215 -- type Res_Typ is access all Comp_Typ;
8217 Res_Typ := Make_Temporary (Loc, 'A');
8218 Set_Ekind (Res_Typ, E_General_Access_Type);
8219 Set_Directly_Designated_Type (Res_Typ, Comp_Typ);
8222 (Make_Full_Type_Declaration (Loc,
8223 Defining_Identifier => Res_Typ,
8225 Make_Access_To_Object_Definition (Loc,
8226 All_Present => True,
8227 Subtype_Indication => New_Occurrence_Of (Comp_Typ, Loc))));
8229 -- Add the temporary which captures the result of the function call.
8232 -- Res : constant Res_Typ := Init_Expr'Reference;
8234 -- Note that this temporary is effectively a transient object because
8235 -- its lifetime is bounded by the current array or record component.
8237 Res_Id := Make_Temporary (Loc, 'R');
8238 Set_Ekind (Res_Id, E_Constant);
8239 Set_Etype (Res_Id, Res_Typ);
8241 -- Mark the transient object as successfully processed to avoid double
8244 Set_Is_Finalized_Transient (Res_Id);
8246 -- Signal the general finalization machinery that this transient object
8247 -- should not be considered for finalization actions because its cleanup
8248 -- will be performed by Process_Transient_Component_Completion.
8250 Set_Is_Ignored_Transient (Res_Id);
8253 Make_Object_Declaration (Loc,
8254 Defining_Identifier => Res_Id,
8255 Constant_Present => True,
8256 Object_Definition => New_Occurrence_Of (Res_Typ, Loc),
8258 Make_Reference (Loc, New_Copy_Tree (Init_Expr)));
8260 Add_Item (Res_Decl);
8262 -- Construct all pieces necessary to hook and finalize the transient
8265 Build_Transient_Object_Statements
8266 (Obj_Decl => Res_Decl,
8267 Fin_Call => Fin_Call,
8268 Hook_Assign => Hook_Assign,
8269 Hook_Clear => Hook_Clear,
8270 Hook_Decl => Hook_Decl,
8271 Ptr_Decl => Ptr_Decl);
8273 -- Add the access type which provides a reference to the transient
8274 -- result. Generate:
8276 -- type Ptr_Typ is access all Comp_Typ;
8278 Add_Item (Ptr_Decl);
8280 -- Add the temporary which acts as a hook to the transient result.
8283 -- Hook : Ptr_Typ := null;
8285 Add_Item (Hook_Decl);
8287 -- Attach the transient result to the hook. Generate:
8289 -- Hook := Ptr_Typ (Res);
8291 Add_Item (Hook_Assign);
8293 -- The original initialization expression now references the value of
8294 -- the temporary function result. Generate:
8299 Make_Explicit_Dereference (Loc,
8300 Prefix => New_Occurrence_Of (Res_Id, Loc)));
8301 end Process_Transient_Component;
8303 --------------------------------------------
8304 -- Process_Transient_Component_Completion --
8305 --------------------------------------------
8307 procedure Process_Transient_Component_Completion
8311 Hook_Clear : Node_Id;
8314 Exceptions_OK : constant Boolean :=
8315 not Restriction_Active (No_Exception_Propagation);
8318 pragma Assert (Present (Hook_Clear));
8320 -- Generate the following code if exception propagation is allowed:
8323 -- Abort : constant Boolean := Triggered_By_Abort;
8325 -- Abort : constant Boolean := False; -- no abort
8327 -- E : Exception_Occurrence;
8328 -- Raised : Boolean := False;
8335 -- [Deep_]Finalize (Res.all);
8339 -- if not Raised then
8341 -- Save_Occurrence (E,
8342 -- Get_Curent_Excep.all.all);
8348 -- if Raised and then not Abort then
8349 -- Raise_From_Controlled_Operation (E);
8353 if Exceptions_OK then
8354 Abort_And_Exception : declare
8355 Blk_Decls : constant List_Id := New_List;
8356 Blk_Stmts : constant List_Id := New_List;
8357 Fin_Stmts : constant List_Id := New_List;
8359 Fin_Data : Finalization_Exception_Data;
8362 -- Create the declarations of the two flags and the exception
8365 Build_Object_Declarations (Fin_Data, Blk_Decls, Loc);
8370 if Abort_Allowed then
8371 Append_To (Blk_Stmts,
8372 Build_Runtime_Call (Loc, RE_Abort_Defer));
8375 -- Wrap the hook clear and the finalization call in order to trap
8376 -- a potential exception.
8378 Append_To (Fin_Stmts, Hook_Clear);
8380 if Present (Fin_Call) then
8381 Append_To (Fin_Stmts, Fin_Call);
8384 Append_To (Blk_Stmts,
8385 Make_Block_Statement (Loc,
8386 Handled_Statement_Sequence =>
8387 Make_Handled_Sequence_Of_Statements (Loc,
8388 Statements => Fin_Stmts,
8389 Exception_Handlers => New_List (
8390 Build_Exception_Handler (Fin_Data)))));
8395 if Abort_Allowed then
8396 Append_To (Blk_Stmts,
8397 Build_Runtime_Call (Loc, RE_Abort_Undefer));
8400 -- Reraise the potential exception with a proper "upgrade" to
8401 -- Program_Error if needed.
8403 Append_To (Blk_Stmts, Build_Raise_Statement (Fin_Data));
8405 -- Wrap everything in a block
8408 Make_Block_Statement (Loc,
8409 Declarations => Blk_Decls,
8410 Handled_Statement_Sequence =>
8411 Make_Handled_Sequence_Of_Statements (Loc,
8412 Statements => Blk_Stmts)));
8413 end Abort_And_Exception;
8415 -- Generate the following code if exception propagation is not allowed
8416 -- and aborts are allowed:
8421 -- [Deep_]Finalize (Res.all);
8423 -- Abort_Undefer_Direct;
8426 elsif Abort_Allowed then
8427 Abort_Only : declare
8428 Blk_Stmts : constant List_Id := New_List;
8431 Append_To (Blk_Stmts, Build_Runtime_Call (Loc, RE_Abort_Defer));
8432 Append_To (Blk_Stmts, Hook_Clear);
8434 if Present (Fin_Call) then
8435 Append_To (Blk_Stmts, Fin_Call);
8439 Build_Abort_Undefer_Block (Loc,
8444 -- Otherwise generate:
8447 -- [Deep_]Finalize (Res.all);
8450 Append_To (Stmts, Hook_Clear);
8452 if Present (Fin_Call) then
8453 Append_To (Stmts, Fin_Call);
8456 end Process_Transient_Component_Completion;
8458 ---------------------
8459 -- Sort_Case_Table --
8460 ---------------------
8462 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type) is
8463 L : constant Int := Case_Table'First;
8464 U : constant Int := Case_Table'Last;
8472 T := Case_Table (K + 1);
8476 and then Expr_Value (Case_Table (J - 1).Choice_Lo) >
8477 Expr_Value (T.Choice_Lo)
8479 Case_Table (J) := Case_Table (J - 1);
8483 Case_Table (J) := T;
8486 end Sort_Case_Table;
8488 ----------------------------
8489 -- Static_Array_Aggregate --
8490 ----------------------------
8492 function Static_Array_Aggregate (N : Node_Id) return Boolean is
8494 function Is_Static_Component (N : Node_Id) return Boolean;
8495 -- Return True if N has a compile-time known value and can be passed as
8496 -- is to the back-end without further expansion.
8498 ---------------------------
8499 -- Is_Static_Component --
8500 ---------------------------
8502 function Is_Static_Component (N : Node_Id) return Boolean is
8504 if Nkind_In (N, N_Integer_Literal, N_Real_Literal) then
8507 elsif Is_Entity_Name (N)
8508 and then Present (Entity (N))
8509 and then Ekind (Entity (N)) = E_Enumeration_Literal
8513 elsif Nkind (N) = N_Aggregate
8514 and then Compile_Time_Known_Aggregate (N)
8521 end Is_Static_Component;
8523 Bounds : constant Node_Id := Aggregate_Bounds (N);
8525 Typ : constant Entity_Id := Etype (N);
8531 -- Start of processing for Static_Array_Aggregate
8534 if Is_Packed (Typ) or else Has_Discriminants (Component_Type (Typ)) then
8539 and then Nkind (Bounds) = N_Range
8540 and then Nkind (Low_Bound (Bounds)) = N_Integer_Literal
8541 and then Nkind (High_Bound (Bounds)) = N_Integer_Literal
8543 Lo := Low_Bound (Bounds);
8544 Hi := High_Bound (Bounds);
8546 if No (Component_Associations (N)) then
8548 -- Verify that all components are static
8550 Expr := First (Expressions (N));
8551 while Present (Expr) loop
8552 if not Is_Static_Component (Expr) then
8562 -- We allow only a single named association, either a static
8563 -- range or an others_clause, with a static expression.
8565 Expr := First (Component_Associations (N));
8567 if Present (Expressions (N)) then
8570 elsif Present (Next (Expr)) then
8573 elsif Present (Next (First (Choice_List (Expr)))) then
8577 -- The aggregate is static if all components are literals,
8578 -- or else all its components are static aggregates for the
8579 -- component type. We also limit the size of a static aggregate
8580 -- to prevent runaway static expressions.
8582 if not Is_Static_Component (Expression (Expr)) then
8586 if not Aggr_Size_OK (N, Typ) then
8590 -- Create a positional aggregate with the right number of
8591 -- copies of the expression.
8593 Agg := Make_Aggregate (Sloc (N), New_List, No_List);
8595 for I in UI_To_Int (Intval (Lo)) .. UI_To_Int (Intval (Hi))
8597 Append_To (Expressions (Agg), New_Copy (Expression (Expr)));
8599 -- The copied expression must be analyzed and resolved.
8600 -- Besides setting the type, this ensures that static
8601 -- expressions are appropriately marked as such.
8604 (Last (Expressions (Agg)), Component_Type (Typ));
8607 Set_Aggregate_Bounds (Agg, Bounds);
8608 Set_Etype (Agg, Typ);
8611 Set_Compile_Time_Known_Aggregate (N);
8620 end Static_Array_Aggregate;
8622 ----------------------------------
8623 -- Two_Dim_Packed_Array_Handled --
8624 ----------------------------------
8626 function Two_Dim_Packed_Array_Handled (N : Node_Id) return Boolean is
8627 Loc : constant Source_Ptr := Sloc (N);
8628 Typ : constant Entity_Id := Etype (N);
8629 Ctyp : constant Entity_Id := Component_Type (Typ);
8630 Comp_Size : constant Int := UI_To_Int (Component_Size (Typ));
8631 Packed_Array : constant Entity_Id :=
8632 Packed_Array_Impl_Type (Base_Type (Typ));
8635 -- Expression in original aggregate
8638 -- One-dimensional subaggregate
8642 -- For now, only deal with cases where an integral number of elements
8643 -- fit in a single byte. This includes the most common boolean case.
8645 if not (Comp_Size = 1 or else
8646 Comp_Size = 2 or else
8652 Convert_To_Positional
8653 (N, Max_Others_Replicate => 64, Handle_Bit_Packed => True);
8655 -- Verify that all components are static
8657 if Nkind (N) = N_Aggregate
8658 and then Compile_Time_Known_Aggregate (N)
8662 -- The aggregate may have been reanalyzed and converted already
8664 elsif Nkind (N) /= N_Aggregate then
8667 -- If component associations remain, the aggregate is not static
8669 elsif Present (Component_Associations (N)) then
8673 One_Dim := First (Expressions (N));
8674 while Present (One_Dim) loop
8675 if Present (Component_Associations (One_Dim)) then
8679 One_Comp := First (Expressions (One_Dim));
8680 while Present (One_Comp) loop
8681 if not Is_OK_Static_Expression (One_Comp) then
8692 -- Two-dimensional aggregate is now fully positional so pack one
8693 -- dimension to create a static one-dimensional array, and rewrite
8694 -- as an unchecked conversion to the original type.
8697 Byte_Size : constant Int := UI_To_Int (Component_Size (Packed_Array));
8698 -- The packed array type is a byte array
8701 -- Number of components accumulated in current byte
8704 -- Assembled list of packed values for equivalent aggregate
8707 -- Integer value of component
8710 -- Step size for packing
8713 -- Endian-dependent start position for packing
8716 -- Current insertion position
8719 -- Component of packed array being assembled
8726 -- Account for endianness. See corresponding comment in
8727 -- Packed_Array_Aggregate_Handled concerning the following.
8731 xor Reverse_Storage_Order (Base_Type (Typ))
8733 Init_Shift := Byte_Size - Comp_Size;
8740 -- Iterate over each subaggregate
8742 Shift := Init_Shift;
8743 One_Dim := First (Expressions (N));
8744 while Present (One_Dim) loop
8745 One_Comp := First (Expressions (One_Dim));
8746 while Present (One_Comp) loop
8747 if Packed_Num = Byte_Size / Comp_Size then
8749 -- Byte is complete, add to list of expressions
8751 Append (Make_Integer_Literal (Sloc (One_Dim), Val), Comps);
8753 Shift := Init_Shift;
8757 Comp_Val := Expr_Rep_Value (One_Comp);
8759 -- Adjust for bias, and strip proper number of bits
8761 if Has_Biased_Representation (Ctyp) then
8762 Comp_Val := Comp_Val - Expr_Value (Type_Low_Bound (Ctyp));
8765 Comp_Val := Comp_Val mod Uint_2 ** Comp_Size;
8766 Val := UI_To_Int (Val + Comp_Val * Uint_2 ** Shift);
8767 Shift := Shift + Incr;
8768 One_Comp := Next (One_Comp);
8769 Packed_Num := Packed_Num + 1;
8773 One_Dim := Next (One_Dim);
8776 if Packed_Num > 0 then
8778 -- Add final incomplete byte if present
8780 Append (Make_Integer_Literal (Sloc (One_Dim), Val), Comps);
8784 Unchecked_Convert_To (Typ,
8785 Make_Qualified_Expression (Loc,
8786 Subtype_Mark => New_Occurrence_Of (Packed_Array, Loc),
8787 Expression => Make_Aggregate (Loc, Expressions => Comps))));
8788 Analyze_And_Resolve (N);
8791 end Two_Dim_Packed_Array_Handled;