1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2015, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Atree; use Atree;
27 with Checks; use Checks;
28 with Debug; use Debug;
29 with Einfo; use Einfo;
30 with Elists; use Elists;
31 with Errout; use Errout;
32 with Expander; use Expander;
33 with Exp_Util; use Exp_Util;
34 with Exp_Ch3; use Exp_Ch3;
35 with Exp_Ch6; use Exp_Ch6;
36 with Exp_Ch7; use Exp_Ch7;
37 with Exp_Ch9; use Exp_Ch9;
38 with Exp_Disp; use Exp_Disp;
39 with Exp_Tss; use Exp_Tss;
40 with Fname; use Fname;
41 with Freeze; use Freeze;
42 with Itypes; use Itypes;
44 with Namet; use Namet;
45 with Nmake; use Nmake;
46 with Nlists; use Nlists;
48 with Restrict; use Restrict;
49 with Rident; use Rident;
50 with Rtsfind; use Rtsfind;
51 with Ttypes; use Ttypes;
53 with Sem_Aggr; use Sem_Aggr;
54 with Sem_Aux; use Sem_Aux;
55 with Sem_Ch3; use Sem_Ch3;
56 with Sem_Eval; use Sem_Eval;
57 with Sem_Res; use Sem_Res;
58 with Sem_Util; use Sem_Util;
59 with Sinfo; use Sinfo;
60 with Snames; use Snames;
61 with Stand; use Stand;
62 with Stringt; use Stringt;
63 with Targparm; use Targparm;
64 with Tbuild; use Tbuild;
65 with Uintp; use Uintp;
67 package body Exp_Aggr is
69 type Case_Bounds is record
72 Choice_Node : Node_Id;
75 type Case_Table_Type is array (Nat range <>) of Case_Bounds;
76 -- Table type used by Check_Case_Choices procedure
78 procedure Collect_Initialization_Statements
81 Node_After : Node_Id);
82 -- If Obj is not frozen, collect actions inserted after N until, but not
83 -- including, Node_After, for initialization of Obj, and move them to an
84 -- expression with actions, which becomes the Initialization_Statements for
87 function Has_Default_Init_Comps (N : Node_Id) return Boolean;
88 -- N is an aggregate (record or array). Checks the presence of default
89 -- initialization (<>) in any component (Ada 2005: AI-287).
91 function In_Object_Declaration (N : Node_Id) return Boolean;
92 -- Return True if N is part of an object declaration, False otherwise
94 function Is_Static_Dispatch_Table_Aggregate (N : Node_Id) return Boolean;
95 -- Returns true if N is an aggregate used to initialize the components
96 -- of a statically allocated dispatch table.
99 (Obj_Type : Entity_Id;
100 Typ : Entity_Id) return Boolean;
101 -- A static array aggregate in an object declaration can in most cases be
102 -- expanded in place. The one exception is when the aggregate is given
103 -- with component associations that specify different bounds from those of
104 -- the type definition in the object declaration. In this pathological
105 -- case the aggregate must slide, and we must introduce an intermediate
106 -- temporary to hold it.
108 -- The same holds in an assignment to one-dimensional array of arrays,
109 -- when a component may be given with bounds that differ from those of the
112 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type);
113 -- Sort the Case Table using the Lower Bound of each Choice as the key.
114 -- A simple insertion sort is used since the number of choices in a case
115 -- statement of variant part will usually be small and probably in near
118 ------------------------------------------------------
119 -- Local subprograms for Record Aggregate Expansion --
120 ------------------------------------------------------
122 function Build_Record_Aggr_Code
125 Lhs : Node_Id) return List_Id;
126 -- N is an N_Aggregate or an N_Extension_Aggregate. Typ is the type of the
127 -- aggregate. Target is an expression containing the location on which the
128 -- component by component assignments will take place. Returns the list of
129 -- assignments plus all other adjustments needed for tagged and controlled
132 procedure Convert_To_Assignments (N : Node_Id; Typ : Entity_Id);
133 -- N is an N_Aggregate or an N_Extension_Aggregate. Typ is the type of the
134 -- aggregate (which can only be a record type, this procedure is only used
135 -- for record types). Transform the given aggregate into a sequence of
136 -- assignments performed component by component.
138 procedure Expand_Record_Aggregate
140 Orig_Tag : Node_Id := Empty;
141 Parent_Expr : Node_Id := Empty);
142 -- This is the top level procedure for record aggregate expansion.
143 -- Expansion for record aggregates needs expand aggregates for tagged
144 -- record types. Specifically Expand_Record_Aggregate adds the Tag
145 -- field in front of the Component_Association list that was created
146 -- during resolution by Resolve_Record_Aggregate.
148 -- N is the record aggregate node.
149 -- Orig_Tag is the value of the Tag that has to be provided for this
150 -- specific aggregate. It carries the tag corresponding to the type
151 -- of the outermost aggregate during the recursive expansion
152 -- Parent_Expr is the ancestor part of the original extension
155 function Has_Mutable_Components (Typ : Entity_Id) return Boolean;
156 -- Return true if one of the components is of a discriminated type with
157 -- defaults. An aggregate for a type with mutable components must be
158 -- expanded into individual assignments.
160 procedure Initialize_Discriminants (N : Node_Id; Typ : Entity_Id);
161 -- If the type of the aggregate is a type extension with renamed discrimi-
162 -- nants, we must initialize the hidden discriminants of the parent.
163 -- Otherwise, the target object must not be initialized. The discriminants
164 -- are initialized by calling the initialization procedure for the type.
165 -- This is incorrect if the initialization of other components has any
166 -- side effects. We restrict this call to the case where the parent type
167 -- has a variant part, because this is the only case where the hidden
168 -- discriminants are accessed, namely when calling discriminant checking
169 -- functions of the parent type, and when applying a stream attribute to
170 -- an object of the derived type.
172 -----------------------------------------------------
173 -- Local Subprograms for Array Aggregate Expansion --
174 -----------------------------------------------------
176 function Aggr_Size_OK (N : Node_Id; Typ : Entity_Id) return Boolean;
177 -- Very large static aggregates present problems to the back-end, and are
178 -- transformed into assignments and loops. This function verifies that the
179 -- total number of components of an aggregate is acceptable for rewriting
180 -- into a purely positional static form. Aggr_Size_OK must be called before
183 -- This function also detects and warns about one-component aggregates that
184 -- appear in a non-static context. Even if the component value is static,
185 -- such an aggregate must be expanded into an assignment.
187 function Backend_Processing_Possible (N : Node_Id) return Boolean;
188 -- This function checks if array aggregate N can be processed directly
189 -- by the backend. If this is the case, True is returned.
191 function Build_Array_Aggr_Code
196 Scalar_Comp : Boolean;
197 Indexes : List_Id := No_List) return List_Id;
198 -- This recursive routine returns a list of statements containing the
199 -- loops and assignments that are needed for the expansion of the array
202 -- N is the (sub-)aggregate node to be expanded into code. This node has
203 -- been fully analyzed, and its Etype is properly set.
205 -- Index is the index node corresponding to the array sub-aggregate N
207 -- Into is the target expression into which we are copying the aggregate.
208 -- Note that this node may not have been analyzed yet, and so the Etype
209 -- field may not be set.
211 -- Scalar_Comp is True if the component type of the aggregate is scalar
213 -- Indexes is the current list of expressions used to index the object we
216 procedure Convert_Array_Aggr_In_Allocator
220 -- If the aggregate appears within an allocator and can be expanded in
221 -- place, this routine generates the individual assignments to components
222 -- of the designated object. This is an optimization over the general
223 -- case, where a temporary is first created on the stack and then used to
224 -- construct the allocated object on the heap.
226 procedure Convert_To_Positional
228 Max_Others_Replicate : Nat := 5;
229 Handle_Bit_Packed : Boolean := False);
230 -- If possible, convert named notation to positional notation. This
231 -- conversion is possible only in some static cases. If the conversion is
232 -- possible, then N is rewritten with the analyzed converted aggregate.
233 -- The parameter Max_Others_Replicate controls the maximum number of
234 -- values corresponding to an others choice that will be converted to
235 -- positional notation (the default of 5 is the normal limit, and reflects
236 -- the fact that normally the loop is better than a lot of separate
237 -- assignments). Note that this limit gets overridden in any case if
238 -- either of the restrictions No_Elaboration_Code or No_Implicit_Loops is
239 -- set. The parameter Handle_Bit_Packed is usually set False (since we do
240 -- not expect the back end to handle bit packed arrays, so the normal case
241 -- of conversion is pointless), but in the special case of a call from
242 -- Packed_Array_Aggregate_Handled, we set this parameter to True, since
243 -- these are cases we handle in there.
245 -- It would seem useful to have a higher default for Max_Others_Replicate,
246 -- but aggregates in the compiler make this impossible: the compiler
247 -- bootstrap fails if Max_Others_Replicate is greater than 25. This
250 procedure Expand_Array_Aggregate (N : Node_Id);
251 -- This is the top-level routine to perform array aggregate expansion.
252 -- N is the N_Aggregate node to be expanded.
254 function Is_Two_Dim_Packed_Array (Typ : Entity_Id) return Boolean;
255 -- For two-dimensional packed aggregates with constant bounds and constant
256 -- components, it is preferable to pack the inner aggregates because the
257 -- whole matrix can then be presented to the back-end as a one-dimensional
258 -- list of literals. This is much more efficient than expanding into single
259 -- component assignments. This function determines if the type Typ is for
260 -- an array that is suitable for this optimization: it returns True if Typ
261 -- is a two dimensional bit packed array with component size 1, 2, or 4.
263 function Late_Expansion
266 Target : Node_Id) return List_Id;
267 -- This routine implements top-down expansion of nested aggregates. In
268 -- doing so, it avoids the generation of temporaries at each level. N is
269 -- a nested record or array aggregate with the Expansion_Delayed flag.
270 -- Typ is the expected type of the aggregate. Target is a (duplicatable)
271 -- expression that will hold the result of the aggregate expansion.
273 function Make_OK_Assignment_Statement
276 Expression : Node_Id) return Node_Id;
277 -- This is like Make_Assignment_Statement, except that Assignment_OK
278 -- is set in the left operand. All assignments built by this unit use
279 -- this routine. This is needed to deal with assignments to initialized
280 -- constants that are done in place.
282 function Number_Of_Choices (N : Node_Id) return Nat;
283 -- Returns the number of discrete choices (not including the others choice
284 -- if present) contained in (sub-)aggregate N.
286 function Packed_Array_Aggregate_Handled (N : Node_Id) return Boolean;
287 -- Given an array aggregate, this function handles the case of a packed
288 -- array aggregate with all constant values, where the aggregate can be
289 -- evaluated at compile time. If this is possible, then N is rewritten
290 -- to be its proper compile time value with all the components properly
291 -- assembled. The expression is analyzed and resolved and True is returned.
292 -- If this transformation is not possible, N is unchanged and False is
295 function Two_Dim_Packed_Array_Handled (N : Node_Id) return Boolean;
296 -- If the type of the aggregate is a two-dimensional bit_packed array
297 -- it may be transformed into an array of bytes with constant values,
298 -- and presented to the back-end as a static value. The function returns
299 -- false if this transformation cannot be performed. THis is similar to,
300 -- and reuses part of the machinery in Packed_Array_Aggregate_Handled.
306 function Aggr_Size_OK (N : Node_Id; Typ : Entity_Id) return Boolean is
315 -- Determines the maximum size of an array aggregate produced by
316 -- converting named to positional notation (e.g. from others clauses).
317 -- This avoids running away with attempts to convert huge aggregates,
318 -- which hit memory limits in the backend.
320 function Component_Count (T : Entity_Id) return Nat;
321 -- The limit is applied to the total number of components that the
322 -- aggregate will have, which is the number of static expressions
323 -- that will appear in the flattened array. This requires a recursive
324 -- computation of the number of scalar components of the structure.
326 ---------------------
327 -- Component_Count --
328 ---------------------
330 function Component_Count (T : Entity_Id) return Nat is
335 if Is_Scalar_Type (T) then
338 elsif Is_Record_Type (T) then
339 Comp := First_Component (T);
340 while Present (Comp) loop
341 Res := Res + Component_Count (Etype (Comp));
342 Next_Component (Comp);
347 elsif Is_Array_Type (T) then
349 Lo : constant Node_Id :=
350 Type_Low_Bound (Etype (First_Index (T)));
351 Hi : constant Node_Id :=
352 Type_High_Bound (Etype (First_Index (T)));
354 Siz : constant Nat := Component_Count (Component_Type (T));
357 -- Check for superflat arrays, i.e. arrays with such bounds
358 -- as 4 .. 2, to insure that this function never returns a
359 -- meaningless negative value.
361 if not Compile_Time_Known_Value (Lo)
362 or else not Compile_Time_Known_Value (Hi)
363 or else Expr_Value (Hi) < Expr_Value (Lo)
369 Siz * UI_To_Int (Expr_Value (Hi) - Expr_Value (Lo) + 1);
374 -- Can only be a null for an access type
380 -- Start of processing for Aggr_Size_OK
383 -- The normal aggregate limit is 50000, but we increase this limit to
384 -- 2**24 (about 16 million) if Restrictions (No_Elaboration_Code) or
385 -- Restrictions (No_Implicit_Loops) is specified, since in either case
386 -- we are at risk of declaring the program illegal because of this
387 -- limit. We also increase the limit when Static_Elaboration_Desired,
388 -- given that this means that objects are intended to be placed in data
391 -- We also increase the limit if the aggregate is for a packed two-
392 -- dimensional array, because if components are static it is much more
393 -- efficient to construct a one-dimensional equivalent array with static
396 -- Conversely, we decrease the maximum size if none of the above
397 -- requirements apply, and if the aggregate has a single component
398 -- association, which will be more efficient if implemented with a loop.
400 -- Finally, we use a small limit in CodePeer mode where we favor loops
401 -- instead of thousands of single assignments (from large aggregates).
403 Max_Aggr_Size := 50000;
405 if CodePeer_Mode then
406 Max_Aggr_Size := 100;
408 elsif Restriction_Active (No_Elaboration_Code)
409 or else Restriction_Active (No_Implicit_Loops)
410 or else Is_Two_Dim_Packed_Array (Typ)
411 or else (Ekind (Current_Scope) = E_Package
412 and then Static_Elaboration_Desired (Current_Scope))
414 Max_Aggr_Size := 2 ** 24;
416 elsif No (Expressions (N))
417 and then No (Next (First (Component_Associations (N))))
419 Max_Aggr_Size := 5000;
422 Siz := Component_Count (Component_Type (Typ));
424 Indx := First_Index (Typ);
425 while Present (Indx) loop
426 Lo := Type_Low_Bound (Etype (Indx));
427 Hi := Type_High_Bound (Etype (Indx));
429 -- Bounds need to be known at compile time
431 if not Compile_Time_Known_Value (Lo)
432 or else not Compile_Time_Known_Value (Hi)
437 Lov := Expr_Value (Lo);
438 Hiv := Expr_Value (Hi);
440 -- A flat array is always safe
446 -- One-component aggregates are suspicious, and if the context type
447 -- is an object declaration with non-static bounds it will trip gcc;
448 -- such an aggregate must be expanded into a single assignment.
450 if Hiv = Lov and then Nkind (Parent (N)) = N_Object_Declaration then
452 Index_Type : constant Entity_Id :=
454 (First_Index (Etype (Defining_Identifier (Parent (N)))));
458 if not Compile_Time_Known_Value (Type_Low_Bound (Index_Type))
459 or else not Compile_Time_Known_Value
460 (Type_High_Bound (Index_Type))
462 if Present (Component_Associations (N)) then
464 First (Choices (First (Component_Associations (N))));
466 if Is_Entity_Name (Indx)
467 and then not Is_Type (Entity (Indx))
470 ("single component aggregate in "
471 & "non-static context??", Indx);
472 Error_Msg_N ("\maybe subtype name was meant??", Indx);
482 Rng : constant Uint := Hiv - Lov + 1;
485 -- Check if size is too large
487 if not UI_Is_In_Int_Range (Rng) then
491 Siz := Siz * UI_To_Int (Rng);
495 or else Siz > Max_Aggr_Size
500 -- Bounds must be in integer range, for later array construction
502 if not UI_Is_In_Int_Range (Lov)
504 not UI_Is_In_Int_Range (Hiv)
515 ---------------------------------
516 -- Backend_Processing_Possible --
517 ---------------------------------
519 -- Backend processing by Gigi/gcc is possible only if all the following
520 -- conditions are met:
522 -- 1. N is fully positional
524 -- 2. N is not a bit-packed array aggregate;
526 -- 3. The size of N's array type must be known at compile time. Note
527 -- that this implies that the component size is also known
529 -- 4. The array type of N does not follow the Fortran layout convention
530 -- or if it does it must be 1 dimensional.
532 -- 5. The array component type may not be tagged (which could necessitate
533 -- reassignment of proper tags).
535 -- 6. The array component type must not have unaligned bit components
537 -- 7. None of the components of the aggregate may be bit unaligned
540 -- 8. There cannot be delayed components, since we do not know enough
541 -- at this stage to know if back end processing is possible.
543 -- 9. There cannot be any discriminated record components, since the
544 -- back end cannot handle this complex case.
546 -- 10. No controlled actions need to be generated for components
548 function Backend_Processing_Possible (N : Node_Id) return Boolean is
549 Typ : constant Entity_Id := Etype (N);
550 -- Typ is the correct constrained array subtype of the aggregate
552 function Component_Check (N : Node_Id; Index : Node_Id) return Boolean;
553 -- This routine checks components of aggregate N, enforcing checks
554 -- 1, 7, 8, and 9. In the multi-dimensional case, these checks are
555 -- performed on subaggregates. The Index value is the current index
556 -- being checked in the multi-dimensional case.
558 ---------------------
559 -- Component_Check --
560 ---------------------
562 function Component_Check (N : Node_Id; Index : Node_Id) return Boolean is
566 -- Checks 1: (no component associations)
568 if Present (Component_Associations (N)) then
572 -- Checks on components
574 -- Recurse to check subaggregates, which may appear in qualified
575 -- expressions. If delayed, the front-end will have to expand.
576 -- If the component is a discriminated record, treat as non-static,
577 -- as the back-end cannot handle this properly.
579 Expr := First (Expressions (N));
580 while Present (Expr) loop
582 -- Checks 8: (no delayed components)
584 if Is_Delayed_Aggregate (Expr) then
588 -- Checks 9: (no discriminated records)
590 if Present (Etype (Expr))
591 and then Is_Record_Type (Etype (Expr))
592 and then Has_Discriminants (Etype (Expr))
597 -- Checks 7. Component must not be bit aligned component
599 if Possible_Bit_Aligned_Component (Expr) then
603 -- Recursion to following indexes for multiple dimension case
605 if Present (Next_Index (Index))
606 and then not Component_Check (Expr, Next_Index (Index))
611 -- All checks for that component finished, on to next
619 -- Start of processing for Backend_Processing_Possible
622 -- Checks 2 (array not bit packed) and 10 (no controlled actions)
624 if Is_Bit_Packed_Array (Typ) or else Needs_Finalization (Typ) then
628 -- If component is limited, aggregate must be expanded because each
629 -- component assignment must be built in place.
631 if Is_Limited_View (Component_Type (Typ)) then
635 -- Checks 4 (array must not be multi-dimensional Fortran case)
637 if Convention (Typ) = Convention_Fortran
638 and then Number_Dimensions (Typ) > 1
643 -- Checks 3 (size of array must be known at compile time)
645 if not Size_Known_At_Compile_Time (Typ) then
649 -- Checks on components
651 if not Component_Check (N, First_Index (Typ)) then
655 -- Checks 5 (if the component type is tagged, then we may need to do
656 -- tag adjustments. Perhaps this should be refined to check for any
657 -- component associations that actually need tag adjustment, similar
658 -- to the test in Component_Not_OK_For_Backend for record aggregates
659 -- with tagged components, but not clear whether it's worthwhile ???;
660 -- in the case of virtual machines (no Tagged_Type_Expansion), object
661 -- tags are handled implicitly).
663 if Is_Tagged_Type (Component_Type (Typ))
664 and then Tagged_Type_Expansion
669 -- Checks 6 (component type must not have bit aligned components)
671 if Type_May_Have_Bit_Aligned_Components (Component_Type (Typ)) then
675 -- Backend processing is possible
677 Set_Size_Known_At_Compile_Time (Etype (N), True);
679 end Backend_Processing_Possible;
681 ---------------------------
682 -- Build_Array_Aggr_Code --
683 ---------------------------
685 -- The code that we generate from a one dimensional aggregate is
687 -- 1. If the sub-aggregate contains discrete choices we
689 -- (a) Sort the discrete choices
691 -- (b) Otherwise for each discrete choice that specifies a range we
692 -- emit a loop. If a range specifies a maximum of three values, or
693 -- we are dealing with an expression we emit a sequence of
694 -- assignments instead of a loop.
696 -- (c) Generate the remaining loops to cover the others choice if any
698 -- 2. If the aggregate contains positional elements we
700 -- (a) translate the positional elements in a series of assignments
702 -- (b) Generate a final loop to cover the others choice if any.
703 -- Note that this final loop has to be a while loop since the case
705 -- L : Integer := Integer'Last;
706 -- H : Integer := Integer'Last;
707 -- A : array (L .. H) := (1, others =>0);
709 -- cannot be handled by a for loop. Thus for the following
711 -- array (L .. H) := (.. positional elements.., others =>E);
713 -- we always generate something like:
715 -- J : Index_Type := Index_Of_Last_Positional_Element;
717 -- J := Index_Base'Succ (J)
721 function Build_Array_Aggr_Code
726 Scalar_Comp : Boolean;
727 Indexes : List_Id := No_List) return List_Id
729 Loc : constant Source_Ptr := Sloc (N);
730 Index_Base : constant Entity_Id := Base_Type (Etype (Index));
731 Index_Base_L : constant Node_Id := Type_Low_Bound (Index_Base);
732 Index_Base_H : constant Node_Id := Type_High_Bound (Index_Base);
734 function Add (Val : Int; To : Node_Id) return Node_Id;
735 -- Returns an expression where Val is added to expression To, unless
736 -- To+Val is provably out of To's base type range. To must be an
737 -- already analyzed expression.
739 function Empty_Range (L, H : Node_Id) return Boolean;
740 -- Returns True if the range defined by L .. H is certainly empty
742 function Equal (L, H : Node_Id) return Boolean;
743 -- Returns True if L = H for sure
745 function Index_Base_Name return Node_Id;
746 -- Returns a new reference to the index type name
748 function Gen_Assign (Ind : Node_Id; Expr : Node_Id) return List_Id;
749 -- Ind must be a side-effect free expression. If the input aggregate
750 -- N to Build_Loop contains no sub-aggregates, then this function
751 -- returns the assignment statement:
753 -- Into (Indexes, Ind) := Expr;
755 -- Otherwise we call Build_Code recursively
757 -- Ada 2005 (AI-287): In case of default initialized component, Expr
758 -- is empty and we generate a call to the corresponding IP subprogram.
760 function Gen_Loop (L, H : Node_Id; Expr : Node_Id) return List_Id;
761 -- Nodes L and H must be side-effect free expressions.
762 -- If the input aggregate N to Build_Loop contains no sub-aggregates,
763 -- This routine returns the for loop statement
765 -- for J in Index_Base'(L) .. Index_Base'(H) loop
766 -- Into (Indexes, J) := Expr;
769 -- Otherwise we call Build_Code recursively.
770 -- As an optimization if the loop covers 3 or less scalar elements we
771 -- generate a sequence of assignments.
773 function Gen_While (L, H : Node_Id; Expr : Node_Id) return List_Id;
774 -- Nodes L and H must be side-effect free expressions.
775 -- If the input aggregate N to Build_Loop contains no sub-aggregates,
776 -- This routine returns the while loop statement
778 -- J : Index_Base := L;
780 -- J := Index_Base'Succ (J);
781 -- Into (Indexes, J) := Expr;
784 -- Otherwise we call Build_Code recursively
786 function Get_Assoc_Expr (Assoc : Node_Id) return Node_Id;
787 -- For an association with a box, use value given by aspect
788 -- Default_Component_Value of array type if specified, else use
789 -- value given by aspect Default_Value for component type itself
790 -- if specified, else return Empty.
792 function Local_Compile_Time_Known_Value (E : Node_Id) return Boolean;
793 function Local_Expr_Value (E : Node_Id) return Uint;
794 -- These two Local routines are used to replace the corresponding ones
795 -- in sem_eval because while processing the bounds of an aggregate with
796 -- discrete choices whose index type is an enumeration, we build static
797 -- expressions not recognized by Compile_Time_Known_Value as such since
798 -- they have not yet been analyzed and resolved. All the expressions in
799 -- question are things like Index_Base_Name'Val (Const) which we can
800 -- easily recognize as being constant.
806 function Add (Val : Int; To : Node_Id) return Node_Id is
811 U_Val : constant Uint := UI_From_Int (Val);
814 -- Note: do not try to optimize the case of Val = 0, because
815 -- we need to build a new node with the proper Sloc value anyway.
817 -- First test if we can do constant folding
819 if Local_Compile_Time_Known_Value (To) then
820 U_To := Local_Expr_Value (To) + Val;
822 -- Determine if our constant is outside the range of the index.
823 -- If so return an Empty node. This empty node will be caught
824 -- by Empty_Range below.
826 if Compile_Time_Known_Value (Index_Base_L)
827 and then U_To < Expr_Value (Index_Base_L)
831 elsif Compile_Time_Known_Value (Index_Base_H)
832 and then U_To > Expr_Value (Index_Base_H)
837 Expr_Pos := Make_Integer_Literal (Loc, U_To);
838 Set_Is_Static_Expression (Expr_Pos);
840 if not Is_Enumeration_Type (Index_Base) then
843 -- If we are dealing with enumeration return
844 -- Index_Base'Val (Expr_Pos)
848 Make_Attribute_Reference
850 Prefix => Index_Base_Name,
851 Attribute_Name => Name_Val,
852 Expressions => New_List (Expr_Pos));
858 -- If we are here no constant folding possible
860 if not Is_Enumeration_Type (Index_Base) then
863 Left_Opnd => Duplicate_Subexpr (To),
864 Right_Opnd => Make_Integer_Literal (Loc, U_Val));
866 -- If we are dealing with enumeration return
867 -- Index_Base'Val (Index_Base'Pos (To) + Val)
871 Make_Attribute_Reference
873 Prefix => Index_Base_Name,
874 Attribute_Name => Name_Pos,
875 Expressions => New_List (Duplicate_Subexpr (To)));
880 Right_Opnd => Make_Integer_Literal (Loc, U_Val));
883 Make_Attribute_Reference
885 Prefix => Index_Base_Name,
886 Attribute_Name => Name_Val,
887 Expressions => New_List (Expr_Pos));
897 function Empty_Range (L, H : Node_Id) return Boolean is
898 Is_Empty : Boolean := False;
903 -- First check if L or H were already detected as overflowing the
904 -- index base range type by function Add above. If this is so Add
905 -- returns the empty node.
907 if No (L) or else No (H) then
914 -- L > H range is empty
920 -- B_L > H range must be empty
926 -- L > B_H range must be empty
930 High := Index_Base_H;
933 if Local_Compile_Time_Known_Value (Low)
935 Local_Compile_Time_Known_Value (High)
938 UI_Gt (Local_Expr_Value (Low), Local_Expr_Value (High));
951 function Equal (L, H : Node_Id) return Boolean is
956 elsif Local_Compile_Time_Known_Value (L)
958 Local_Compile_Time_Known_Value (H)
960 return UI_Eq (Local_Expr_Value (L), Local_Expr_Value (H));
970 function Gen_Assign (Ind : Node_Id; Expr : Node_Id) return List_Id is
971 L : constant List_Id := New_List;
974 New_Indexes : List_Id;
975 Indexed_Comp : Node_Id;
977 Comp_Type : Entity_Id := Empty;
979 function Add_Loop_Actions (Lis : List_Id) return List_Id;
980 -- Collect insert_actions generated in the construction of a
981 -- loop, and prepend them to the sequence of assignments to
982 -- complete the eventual body of the loop.
984 ----------------------
985 -- Add_Loop_Actions --
986 ----------------------
988 function Add_Loop_Actions (Lis : List_Id) return List_Id is
992 -- Ada 2005 (AI-287): Do nothing else in case of default
993 -- initialized component.
998 elsif Nkind (Parent (Expr)) = N_Component_Association
999 and then Present (Loop_Actions (Parent (Expr)))
1001 Append_List (Lis, Loop_Actions (Parent (Expr)));
1002 Res := Loop_Actions (Parent (Expr));
1003 Set_Loop_Actions (Parent (Expr), No_List);
1009 end Add_Loop_Actions;
1011 -- Start of processing for Gen_Assign
1014 if No (Indexes) then
1015 New_Indexes := New_List;
1017 New_Indexes := New_Copy_List_Tree (Indexes);
1020 Append_To (New_Indexes, Ind);
1022 if Present (Next_Index (Index)) then
1025 Build_Array_Aggr_Code
1028 Index => Next_Index (Index),
1030 Scalar_Comp => Scalar_Comp,
1031 Indexes => New_Indexes));
1034 -- If we get here then we are at a bottom-level (sub-)aggregate
1038 (Make_Indexed_Component (Loc,
1039 Prefix => New_Copy_Tree (Into),
1040 Expressions => New_Indexes));
1042 Set_Assignment_OK (Indexed_Comp);
1044 -- Ada 2005 (AI-287): In case of default initialized component, Expr
1045 -- is not present (and therefore we also initialize Expr_Q to empty).
1049 elsif Nkind (Expr) = N_Qualified_Expression then
1050 Expr_Q := Expression (Expr);
1055 if Present (Etype (N)) and then Etype (N) /= Any_Composite then
1056 Comp_Type := Component_Type (Etype (N));
1057 pragma Assert (Comp_Type = Ctype); -- AI-287
1059 elsif Present (Next (First (New_Indexes))) then
1061 -- Ada 2005 (AI-287): Do nothing in case of default initialized
1062 -- component because we have received the component type in
1063 -- the formal parameter Ctype.
1065 -- ??? Some assert pragmas have been added to check if this new
1066 -- formal can be used to replace this code in all cases.
1068 if Present (Expr) then
1070 -- This is a multidimensional array. Recover the component type
1071 -- from the outermost aggregate, because subaggregates do not
1072 -- have an assigned type.
1079 while Present (P) loop
1080 if Nkind (P) = N_Aggregate
1081 and then Present (Etype (P))
1083 Comp_Type := Component_Type (Etype (P));
1091 pragma Assert (Comp_Type = Ctype); -- AI-287
1096 -- Ada 2005 (AI-287): We only analyze the expression in case of non-
1097 -- default initialized components (otherwise Expr_Q is not present).
1100 and then Nkind_In (Expr_Q, N_Aggregate, N_Extension_Aggregate)
1102 -- At this stage the Expression may not have been analyzed yet
1103 -- because the array aggregate code has not been updated to use
1104 -- the Expansion_Delayed flag and avoid analysis altogether to
1105 -- solve the same problem (see Resolve_Aggr_Expr). So let us do
1106 -- the analysis of non-array aggregates now in order to get the
1107 -- value of Expansion_Delayed flag for the inner aggregate ???
1109 if Present (Comp_Type) and then not Is_Array_Type (Comp_Type) then
1110 Analyze_And_Resolve (Expr_Q, Comp_Type);
1113 if Is_Delayed_Aggregate (Expr_Q) then
1115 -- This is either a subaggregate of a multidimensional array,
1116 -- or a component of an array type whose component type is
1117 -- also an array. In the latter case, the expression may have
1118 -- component associations that provide different bounds from
1119 -- those of the component type, and sliding must occur. Instead
1120 -- of decomposing the current aggregate assignment, force the
1121 -- re-analysis of the assignment, so that a temporary will be
1122 -- generated in the usual fashion, and sliding will take place.
1124 if Nkind (Parent (N)) = N_Assignment_Statement
1125 and then Is_Array_Type (Comp_Type)
1126 and then Present (Component_Associations (Expr_Q))
1127 and then Must_Slide (Comp_Type, Etype (Expr_Q))
1129 Set_Expansion_Delayed (Expr_Q, False);
1130 Set_Analyzed (Expr_Q, False);
1135 Late_Expansion (Expr_Q, Etype (Expr_Q), Indexed_Comp));
1140 -- Ada 2005 (AI-287): In case of default initialized component, call
1141 -- the initialization subprogram associated with the component type.
1142 -- If the component type is an access type, add an explicit null
1143 -- assignment, because for the back-end there is an initialization
1144 -- present for the whole aggregate, and no default initialization
1147 -- In addition, if the component type is controlled, we must call
1148 -- its Initialize procedure explicitly, because there is no explicit
1149 -- object creation that will invoke it otherwise.
1152 if Present (Base_Init_Proc (Base_Type (Ctype)))
1153 or else Has_Task (Base_Type (Ctype))
1156 Build_Initialization_Call (Loc,
1157 Id_Ref => Indexed_Comp,
1159 With_Default_Init => True));
1161 -- If the component type has invariants, add an invariant
1162 -- check after the component is default-initialized. It will
1163 -- be analyzed and resolved before the code for initialization
1164 -- of other components.
1166 if Has_Invariants (Ctype) then
1167 Set_Etype (Indexed_Comp, Ctype);
1168 Append_To (L, Make_Invariant_Call (Indexed_Comp));
1171 elsif Is_Access_Type (Ctype) then
1173 Make_Assignment_Statement (Loc,
1174 Name => Indexed_Comp,
1175 Expression => Make_Null (Loc)));
1178 if Needs_Finalization (Ctype) then
1181 (Obj_Ref => New_Copy_Tree (Indexed_Comp),
1187 Make_OK_Assignment_Statement (Loc,
1188 Name => Indexed_Comp,
1189 Expression => New_Copy_Tree (Expr));
1191 -- The target of the assignment may not have been initialized,
1192 -- so it is not possible to call Finalize as expected in normal
1193 -- controlled assignments. We must also avoid using the primitive
1194 -- _assign (which depends on a valid target, and may for example
1195 -- perform discriminant checks on it).
1197 -- Both Finalize and usage of _assign are disabled by setting
1198 -- No_Ctrl_Actions on the assignment. The rest of the controlled
1199 -- actions are done manually with the proper finalization list
1200 -- coming from the context.
1202 Set_No_Ctrl_Actions (A);
1204 -- If this is an aggregate for an array of arrays, each
1205 -- sub-aggregate will be expanded as well, and even with
1206 -- No_Ctrl_Actions the assignments of inner components will
1207 -- require attachment in their assignments to temporaries. These
1208 -- temporaries must be finalized for each subaggregate, to prevent
1209 -- multiple attachments of the same temporary location to same
1210 -- finalization chain (and consequently circular lists). To ensure
1211 -- that finalization takes place for each subaggregate we wrap the
1212 -- assignment in a block.
1214 if Present (Comp_Type)
1215 and then Needs_Finalization (Comp_Type)
1216 and then Is_Array_Type (Comp_Type)
1217 and then Present (Expr)
1220 Make_Block_Statement (Loc,
1221 Handled_Statement_Sequence =>
1222 Make_Handled_Sequence_Of_Statements (Loc,
1223 Statements => New_List (A)));
1228 -- Adjust the tag if tagged (because of possible view
1229 -- conversions), unless compiling for a VM where tags
1232 if Present (Comp_Type)
1233 and then Is_Tagged_Type (Comp_Type)
1234 and then Tagged_Type_Expansion
1237 Full_Typ : constant Entity_Id := Underlying_Type (Comp_Type);
1241 Make_OK_Assignment_Statement (Loc,
1243 Make_Selected_Component (Loc,
1244 Prefix => New_Copy_Tree (Indexed_Comp),
1247 (First_Tag_Component (Full_Typ), Loc)),
1250 Unchecked_Convert_To (RTE (RE_Tag),
1252 (Node (First_Elmt (Access_Disp_Table (Full_Typ))),
1259 -- Adjust and attach the component to the proper final list, which
1260 -- can be the controller of the outer record object or the final
1261 -- list associated with the scope.
1263 -- If the component is itself an array of controlled types, whose
1264 -- value is given by a sub-aggregate, then the attach calls have
1265 -- been generated when individual subcomponent are assigned, and
1266 -- must not be done again to prevent malformed finalization chains
1267 -- (see comments above, concerning the creation of a block to hold
1268 -- inner finalization actions).
1270 if Present (Comp_Type)
1271 and then Needs_Finalization (Comp_Type)
1272 and then not Is_Limited_Type (Comp_Type)
1274 (Is_Array_Type (Comp_Type)
1275 and then Is_Controlled (Component_Type (Comp_Type))
1276 and then Nkind (Expr) = N_Aggregate)
1280 (Obj_Ref => New_Copy_Tree (Indexed_Comp),
1285 return Add_Loop_Actions (L);
1292 function Gen_Loop (L, H : Node_Id; Expr : Node_Id) return List_Id is
1302 -- Index_Base'(L) .. Index_Base'(H)
1304 L_Iteration_Scheme : Node_Id;
1305 -- L_J in Index_Base'(L) .. Index_Base'(H)
1308 -- The statements to execute in the loop
1310 S : constant List_Id := New_List;
1311 -- List of statements
1314 -- Copy of expression tree, used for checking purposes
1317 -- If loop bounds define an empty range return the null statement
1319 if Empty_Range (L, H) then
1320 Append_To (S, Make_Null_Statement (Loc));
1322 -- Ada 2005 (AI-287): Nothing else need to be done in case of
1323 -- default initialized component.
1329 -- The expression must be type-checked even though no component
1330 -- of the aggregate will have this value. This is done only for
1331 -- actual components of the array, not for subaggregates. Do
1332 -- the check on a copy, because the expression may be shared
1333 -- among several choices, some of which might be non-null.
1335 if Present (Etype (N))
1336 and then Is_Array_Type (Etype (N))
1337 and then No (Next_Index (Index))
1339 Expander_Mode_Save_And_Set (False);
1340 Tcopy := New_Copy_Tree (Expr);
1341 Set_Parent (Tcopy, N);
1342 Analyze_And_Resolve (Tcopy, Component_Type (Etype (N)));
1343 Expander_Mode_Restore;
1349 -- If loop bounds are the same then generate an assignment
1351 elsif Equal (L, H) then
1352 return Gen_Assign (New_Copy_Tree (L), Expr);
1354 -- If H - L <= 2 then generate a sequence of assignments when we are
1355 -- processing the bottom most aggregate and it contains scalar
1358 elsif No (Next_Index (Index))
1359 and then Scalar_Comp
1360 and then Local_Compile_Time_Known_Value (L)
1361 and then Local_Compile_Time_Known_Value (H)
1362 and then Local_Expr_Value (H) - Local_Expr_Value (L) <= 2
1365 Append_List_To (S, Gen_Assign (New_Copy_Tree (L), Expr));
1366 Append_List_To (S, Gen_Assign (Add (1, To => L), Expr));
1368 if Local_Expr_Value (H) - Local_Expr_Value (L) = 2 then
1369 Append_List_To (S, Gen_Assign (Add (2, To => L), Expr));
1375 -- Otherwise construct the loop, starting with the loop index L_J
1377 L_J := Make_Temporary (Loc, 'J', L);
1379 -- Construct "L .. H" in Index_Base. We use a qualified expression
1380 -- for the bound to convert to the index base, but we don't need
1381 -- to do that if we already have the base type at hand.
1383 if Etype (L) = Index_Base then
1387 Make_Qualified_Expression (Loc,
1388 Subtype_Mark => Index_Base_Name,
1392 if Etype (H) = Index_Base then
1396 Make_Qualified_Expression (Loc,
1397 Subtype_Mark => Index_Base_Name,
1406 -- Construct "for L_J in Index_Base range L .. H"
1408 L_Iteration_Scheme :=
1409 Make_Iteration_Scheme
1411 Loop_Parameter_Specification =>
1412 Make_Loop_Parameter_Specification
1414 Defining_Identifier => L_J,
1415 Discrete_Subtype_Definition => L_Range));
1417 -- Construct the statements to execute in the loop body
1419 L_Body := Gen_Assign (New_Occurrence_Of (L_J, Loc), Expr);
1421 -- Construct the final loop
1424 Make_Implicit_Loop_Statement
1426 Identifier => Empty,
1427 Iteration_Scheme => L_Iteration_Scheme,
1428 Statements => L_Body));
1430 -- A small optimization: if the aggregate is initialized with a box
1431 -- and the component type has no initialization procedure, remove the
1432 -- useless empty loop.
1434 if Nkind (First (S)) = N_Loop_Statement
1435 and then Is_Empty_List (Statements (First (S)))
1437 return New_List (Make_Null_Statement (Loc));
1447 -- The code built is
1449 -- W_J : Index_Base := L;
1450 -- while W_J < H loop
1451 -- W_J := Index_Base'Succ (W);
1455 function Gen_While (L, H : Node_Id; Expr : Node_Id) return List_Id is
1459 -- W_J : Base_Type := L;
1461 W_Iteration_Scheme : Node_Id;
1464 W_Index_Succ : Node_Id;
1465 -- Index_Base'Succ (J)
1467 W_Increment : Node_Id;
1468 -- W_J := Index_Base'Succ (W)
1470 W_Body : constant List_Id := New_List;
1471 -- The statements to execute in the loop
1473 S : constant List_Id := New_List;
1474 -- list of statement
1477 -- If loop bounds define an empty range or are equal return null
1479 if Empty_Range (L, H) or else Equal (L, H) then
1480 Append_To (S, Make_Null_Statement (Loc));
1484 -- Build the decl of W_J
1486 W_J := Make_Temporary (Loc, 'J', L);
1488 Make_Object_Declaration
1490 Defining_Identifier => W_J,
1491 Object_Definition => Index_Base_Name,
1494 -- Theoretically we should do a New_Copy_Tree (L) here, but we know
1495 -- that in this particular case L is a fresh Expr generated by
1496 -- Add which we are the only ones to use.
1498 Append_To (S, W_Decl);
1500 -- Construct " while W_J < H"
1502 W_Iteration_Scheme :=
1503 Make_Iteration_Scheme
1505 Condition => Make_Op_Lt
1507 Left_Opnd => New_Occurrence_Of (W_J, Loc),
1508 Right_Opnd => New_Copy_Tree (H)));
1510 -- Construct the statements to execute in the loop body
1513 Make_Attribute_Reference
1515 Prefix => Index_Base_Name,
1516 Attribute_Name => Name_Succ,
1517 Expressions => New_List (New_Occurrence_Of (W_J, Loc)));
1520 Make_OK_Assignment_Statement
1522 Name => New_Occurrence_Of (W_J, Loc),
1523 Expression => W_Index_Succ);
1525 Append_To (W_Body, W_Increment);
1526 Append_List_To (W_Body,
1527 Gen_Assign (New_Occurrence_Of (W_J, Loc), Expr));
1529 -- Construct the final loop
1532 Make_Implicit_Loop_Statement
1534 Identifier => Empty,
1535 Iteration_Scheme => W_Iteration_Scheme,
1536 Statements => W_Body));
1541 --------------------
1542 -- Get_Assoc_Expr --
1543 --------------------
1545 function Get_Assoc_Expr (Assoc : Node_Id) return Node_Id is
1546 Typ : constant Entity_Id := Base_Type (Etype (N));
1549 if Box_Present (Assoc) then
1550 if Is_Scalar_Type (Ctype) then
1551 if Present (Default_Aspect_Component_Value (Typ)) then
1552 return Default_Aspect_Component_Value (Typ);
1553 elsif Present (Default_Aspect_Value (Ctype)) then
1554 return Default_Aspect_Value (Ctype);
1564 return Expression (Assoc);
1568 ---------------------
1569 -- Index_Base_Name --
1570 ---------------------
1572 function Index_Base_Name return Node_Id is
1574 return New_Occurrence_Of (Index_Base, Sloc (N));
1575 end Index_Base_Name;
1577 ------------------------------------
1578 -- Local_Compile_Time_Known_Value --
1579 ------------------------------------
1581 function Local_Compile_Time_Known_Value (E : Node_Id) return Boolean is
1583 return Compile_Time_Known_Value (E)
1585 (Nkind (E) = N_Attribute_Reference
1586 and then Attribute_Name (E) = Name_Val
1587 and then Compile_Time_Known_Value (First (Expressions (E))));
1588 end Local_Compile_Time_Known_Value;
1590 ----------------------
1591 -- Local_Expr_Value --
1592 ----------------------
1594 function Local_Expr_Value (E : Node_Id) return Uint is
1596 if Compile_Time_Known_Value (E) then
1597 return Expr_Value (E);
1599 return Expr_Value (First (Expressions (E)));
1601 end Local_Expr_Value;
1603 -- Build_Array_Aggr_Code Variables
1610 Others_Assoc : Node_Id := Empty;
1612 Aggr_L : constant Node_Id := Low_Bound (Aggregate_Bounds (N));
1613 Aggr_H : constant Node_Id := High_Bound (Aggregate_Bounds (N));
1614 -- The aggregate bounds of this specific sub-aggregate. Note that if
1615 -- the code generated by Build_Array_Aggr_Code is executed then these
1616 -- bounds are OK. Otherwise a Constraint_Error would have been raised.
1618 Aggr_Low : constant Node_Id := Duplicate_Subexpr_No_Checks (Aggr_L);
1619 Aggr_High : constant Node_Id := Duplicate_Subexpr_No_Checks (Aggr_H);
1620 -- After Duplicate_Subexpr these are side-effect free
1625 Nb_Choices : Nat := 0;
1626 Table : Case_Table_Type (1 .. Number_Of_Choices (N));
1627 -- Used to sort all the different choice values
1630 -- Number of elements in the positional aggregate
1632 New_Code : constant List_Id := New_List;
1634 -- Start of processing for Build_Array_Aggr_Code
1637 -- First before we start, a special case. if we have a bit packed
1638 -- array represented as a modular type, then clear the value to
1639 -- zero first, to ensure that unused bits are properly cleared.
1644 and then Is_Bit_Packed_Array (Typ)
1645 and then Is_Modular_Integer_Type (Packed_Array_Impl_Type (Typ))
1647 Append_To (New_Code,
1648 Make_Assignment_Statement (Loc,
1649 Name => New_Copy_Tree (Into),
1651 Unchecked_Convert_To (Typ,
1652 Make_Integer_Literal (Loc, Uint_0))));
1655 -- If the component type contains tasks, we need to build a Master
1656 -- entity in the current scope, because it will be needed if build-
1657 -- in-place functions are called in the expanded code.
1659 if Nkind (Parent (N)) = N_Object_Declaration and then Has_Task (Typ) then
1660 Build_Master_Entity (Defining_Identifier (Parent (N)));
1663 -- STEP 1: Process component associations
1665 -- For those associations that may generate a loop, initialize
1666 -- Loop_Actions to collect inserted actions that may be crated.
1668 -- Skip this if no component associations
1670 if No (Expressions (N)) then
1672 -- STEP 1 (a): Sort the discrete choices
1674 Assoc := First (Component_Associations (N));
1675 while Present (Assoc) loop
1676 Choice := First (Choices (Assoc));
1677 while Present (Choice) loop
1678 if Nkind (Choice) = N_Others_Choice then
1679 Set_Loop_Actions (Assoc, New_List);
1680 Others_Assoc := Assoc;
1684 Get_Index_Bounds (Choice, Low, High);
1687 Set_Loop_Actions (Assoc, New_List);
1690 Nb_Choices := Nb_Choices + 1;
1692 Table (Nb_Choices) :=
1695 Choice_Node => Get_Assoc_Expr (Assoc));
1703 -- If there is more than one set of choices these must be static
1704 -- and we can therefore sort them. Remember that Nb_Choices does not
1705 -- account for an others choice.
1707 if Nb_Choices > 1 then
1708 Sort_Case_Table (Table);
1711 -- STEP 1 (b): take care of the whole set of discrete choices
1713 for J in 1 .. Nb_Choices loop
1714 Low := Table (J).Choice_Lo;
1715 High := Table (J).Choice_Hi;
1716 Expr := Table (J).Choice_Node;
1717 Append_List (Gen_Loop (Low, High, Expr), To => New_Code);
1720 -- STEP 1 (c): generate the remaining loops to cover others choice
1721 -- We don't need to generate loops over empty gaps, but if there is
1722 -- a single empty range we must analyze the expression for semantics
1724 if Present (Others_Assoc) then
1726 First : Boolean := True;
1729 for J in 0 .. Nb_Choices loop
1733 Low := Add (1, To => Table (J).Choice_Hi);
1736 if J = Nb_Choices then
1739 High := Add (-1, To => Table (J + 1).Choice_Lo);
1742 -- If this is an expansion within an init proc, make
1743 -- sure that discriminant references are replaced by
1744 -- the corresponding discriminal.
1746 if Inside_Init_Proc then
1747 if Is_Entity_Name (Low)
1748 and then Ekind (Entity (Low)) = E_Discriminant
1750 Set_Entity (Low, Discriminal (Entity (Low)));
1753 if Is_Entity_Name (High)
1754 and then Ekind (Entity (High)) = E_Discriminant
1756 Set_Entity (High, Discriminal (Entity (High)));
1761 or else not Empty_Range (Low, High)
1765 (Gen_Loop (Low, High,
1766 Get_Assoc_Expr (Others_Assoc)), To => New_Code);
1772 -- STEP 2: Process positional components
1775 -- STEP 2 (a): Generate the assignments for each positional element
1776 -- Note that here we have to use Aggr_L rather than Aggr_Low because
1777 -- Aggr_L is analyzed and Add wants an analyzed expression.
1779 Expr := First (Expressions (N));
1781 while Present (Expr) loop
1782 Nb_Elements := Nb_Elements + 1;
1783 Append_List (Gen_Assign (Add (Nb_Elements, To => Aggr_L), Expr),
1788 -- STEP 2 (b): Generate final loop if an others choice is present
1789 -- Here Nb_Elements gives the offset of the last positional element.
1791 if Present (Component_Associations (N)) then
1792 Assoc := Last (Component_Associations (N));
1794 -- Ada 2005 (AI-287)
1796 Append_List (Gen_While (Add (Nb_Elements, To => Aggr_L),
1798 Get_Assoc_Expr (Assoc)), -- AI-287
1804 end Build_Array_Aggr_Code;
1806 ----------------------------
1807 -- Build_Record_Aggr_Code --
1808 ----------------------------
1810 function Build_Record_Aggr_Code
1813 Lhs : Node_Id) return List_Id
1815 Loc : constant Source_Ptr := Sloc (N);
1816 L : constant List_Id := New_List;
1817 N_Typ : constant Entity_Id := Etype (N);
1823 Comp_Type : Entity_Id;
1824 Selector : Entity_Id;
1825 Comp_Expr : Node_Id;
1828 -- If this is an internal aggregate, the External_Final_List is an
1829 -- expression for the controller record of the enclosing type.
1831 -- If the current aggregate has several controlled components, this
1832 -- expression will appear in several calls to attach to the finali-
1833 -- zation list, and it must not be shared.
1835 Ancestor_Is_Expression : Boolean := False;
1836 Ancestor_Is_Subtype_Mark : Boolean := False;
1838 Init_Typ : Entity_Id := Empty;
1840 Finalization_Done : Boolean := False;
1841 -- True if Generate_Finalization_Actions has already been called; calls
1842 -- after the first do nothing.
1844 function Ancestor_Discriminant_Value (Disc : Entity_Id) return Node_Id;
1845 -- Returns the value that the given discriminant of an ancestor type
1846 -- should receive (in the absence of a conflict with the value provided
1847 -- by an ancestor part of an extension aggregate).
1849 procedure Check_Ancestor_Discriminants (Anc_Typ : Entity_Id);
1850 -- Check that each of the discriminant values defined by the ancestor
1851 -- part of an extension aggregate match the corresponding values
1852 -- provided by either an association of the aggregate or by the
1853 -- constraint imposed by a parent type (RM95-4.3.2(8)).
1855 function Compatible_Int_Bounds
1856 (Agg_Bounds : Node_Id;
1857 Typ_Bounds : Node_Id) return Boolean;
1858 -- Return true if Agg_Bounds are equal or within Typ_Bounds. It is
1859 -- assumed that both bounds are integer ranges.
1861 procedure Generate_Finalization_Actions;
1862 -- Deal with the various controlled type data structure initializations
1863 -- (but only if it hasn't been done already).
1865 function Get_Constraint_Association (T : Entity_Id) return Node_Id;
1866 -- Returns the first discriminant association in the constraint
1867 -- associated with T, if any, otherwise returns Empty.
1869 procedure Init_Hidden_Discriminants (Typ : Entity_Id; List : List_Id);
1870 -- If Typ is derived, and constrains discriminants of the parent type,
1871 -- these discriminants are not components of the aggregate, and must be
1872 -- initialized. The assignments are appended to List. The same is done
1873 -- if Typ derives fron an already constrained subtype of a discriminated
1876 function Get_Explicit_Discriminant_Value (D : Entity_Id) return Node_Id;
1877 -- If the ancestor part is an unconstrained type and further ancestors
1878 -- do not provide discriminants for it, check aggregate components for
1879 -- values of the discriminants.
1881 function Is_Int_Range_Bounds (Bounds : Node_Id) return Boolean;
1882 -- Check whether Bounds is a range node and its lower and higher bounds
1883 -- are integers literals.
1885 ---------------------------------
1886 -- Ancestor_Discriminant_Value --
1887 ---------------------------------
1889 function Ancestor_Discriminant_Value (Disc : Entity_Id) return Node_Id is
1891 Assoc_Elmt : Elmt_Id;
1892 Aggr_Comp : Entity_Id;
1893 Corresp_Disc : Entity_Id;
1894 Current_Typ : Entity_Id := Base_Type (Typ);
1895 Parent_Typ : Entity_Id;
1896 Parent_Disc : Entity_Id;
1897 Save_Assoc : Node_Id := Empty;
1900 -- First check any discriminant associations to see if any of them
1901 -- provide a value for the discriminant.
1903 if Present (Discriminant_Specifications (Parent (Current_Typ))) then
1904 Assoc := First (Component_Associations (N));
1905 while Present (Assoc) loop
1906 Aggr_Comp := Entity (First (Choices (Assoc)));
1908 if Ekind (Aggr_Comp) = E_Discriminant then
1909 Save_Assoc := Expression (Assoc);
1911 Corresp_Disc := Corresponding_Discriminant (Aggr_Comp);
1912 while Present (Corresp_Disc) loop
1914 -- If found a corresponding discriminant then return the
1915 -- value given in the aggregate. (Note: this is not
1916 -- correct in the presence of side effects. ???)
1918 if Disc = Corresp_Disc then
1919 return Duplicate_Subexpr (Expression (Assoc));
1922 Corresp_Disc := Corresponding_Discriminant (Corresp_Disc);
1930 -- No match found in aggregate, so chain up parent types to find
1931 -- a constraint that defines the value of the discriminant.
1933 Parent_Typ := Etype (Current_Typ);
1934 while Current_Typ /= Parent_Typ loop
1935 if Has_Discriminants (Parent_Typ)
1936 and then not Has_Unknown_Discriminants (Parent_Typ)
1938 Parent_Disc := First_Discriminant (Parent_Typ);
1940 -- We either get the association from the subtype indication
1941 -- of the type definition itself, or from the discriminant
1942 -- constraint associated with the type entity (which is
1943 -- preferable, but it's not always present ???)
1945 if Is_Empty_Elmt_List (Discriminant_Constraint (Current_Typ))
1947 Assoc := Get_Constraint_Association (Current_Typ);
1948 Assoc_Elmt := No_Elmt;
1951 First_Elmt (Discriminant_Constraint (Current_Typ));
1952 Assoc := Node (Assoc_Elmt);
1955 -- Traverse the discriminants of the parent type looking
1956 -- for one that corresponds.
1958 while Present (Parent_Disc) and then Present (Assoc) loop
1959 Corresp_Disc := Parent_Disc;
1960 while Present (Corresp_Disc)
1961 and then Disc /= Corresp_Disc
1963 Corresp_Disc := Corresponding_Discriminant (Corresp_Disc);
1966 if Disc = Corresp_Disc then
1967 if Nkind (Assoc) = N_Discriminant_Association then
1968 Assoc := Expression (Assoc);
1971 -- If the located association directly denotes
1972 -- a discriminant, then use the value of a saved
1973 -- association of the aggregate. This is an approach
1974 -- used to handle certain cases involving multiple
1975 -- discriminants mapped to a single discriminant of
1976 -- a descendant. It's not clear how to locate the
1977 -- appropriate discriminant value for such cases. ???
1979 if Is_Entity_Name (Assoc)
1980 and then Ekind (Entity (Assoc)) = E_Discriminant
1982 Assoc := Save_Assoc;
1985 return Duplicate_Subexpr (Assoc);
1988 Next_Discriminant (Parent_Disc);
1990 if No (Assoc_Elmt) then
1994 Next_Elmt (Assoc_Elmt);
1996 if Present (Assoc_Elmt) then
1997 Assoc := Node (Assoc_Elmt);
2005 Current_Typ := Parent_Typ;
2006 Parent_Typ := Etype (Current_Typ);
2009 -- In some cases there's no ancestor value to locate (such as
2010 -- when an ancestor part given by an expression defines the
2011 -- discriminant value).
2014 end Ancestor_Discriminant_Value;
2016 ----------------------------------
2017 -- Check_Ancestor_Discriminants --
2018 ----------------------------------
2020 procedure Check_Ancestor_Discriminants (Anc_Typ : Entity_Id) is
2022 Disc_Value : Node_Id;
2026 Discr := First_Discriminant (Base_Type (Anc_Typ));
2027 while Present (Discr) loop
2028 Disc_Value := Ancestor_Discriminant_Value (Discr);
2030 if Present (Disc_Value) then
2031 Cond := Make_Op_Ne (Loc,
2033 Make_Selected_Component (Loc,
2034 Prefix => New_Copy_Tree (Target),
2035 Selector_Name => New_Occurrence_Of (Discr, Loc)),
2036 Right_Opnd => Disc_Value);
2039 Make_Raise_Constraint_Error (Loc,
2041 Reason => CE_Discriminant_Check_Failed));
2044 Next_Discriminant (Discr);
2046 end Check_Ancestor_Discriminants;
2048 ---------------------------
2049 -- Compatible_Int_Bounds --
2050 ---------------------------
2052 function Compatible_Int_Bounds
2053 (Agg_Bounds : Node_Id;
2054 Typ_Bounds : Node_Id) return Boolean
2056 Agg_Lo : constant Uint := Intval (Low_Bound (Agg_Bounds));
2057 Agg_Hi : constant Uint := Intval (High_Bound (Agg_Bounds));
2058 Typ_Lo : constant Uint := Intval (Low_Bound (Typ_Bounds));
2059 Typ_Hi : constant Uint := Intval (High_Bound (Typ_Bounds));
2061 return Typ_Lo <= Agg_Lo and then Agg_Hi <= Typ_Hi;
2062 end Compatible_Int_Bounds;
2064 --------------------------------
2065 -- Get_Constraint_Association --
2066 --------------------------------
2068 function Get_Constraint_Association (T : Entity_Id) return Node_Id is
2075 -- If type is private, get constraint from full view. This was
2076 -- previously done in an instance context, but is needed whenever
2077 -- the ancestor part has a discriminant, possibly inherited through
2078 -- multiple derivations.
2080 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
2081 Typ := Full_View (Typ);
2084 Indic := Subtype_Indication (Type_Definition (Parent (Typ)));
2086 -- Verify that the subtype indication carries a constraint
2088 if Nkind (Indic) = N_Subtype_Indication
2089 and then Present (Constraint (Indic))
2091 return First (Constraints (Constraint (Indic)));
2095 end Get_Constraint_Association;
2097 -------------------------------------
2098 -- Get_Explicit_Discriminant_Value --
2099 -------------------------------------
2101 function Get_Explicit_Discriminant_Value
2102 (D : Entity_Id) return Node_Id
2109 -- The aggregate has been normalized and all associations have a
2112 Assoc := First (Component_Associations (N));
2113 while Present (Assoc) loop
2114 Choice := First (Choices (Assoc));
2116 if Chars (Choice) = Chars (D) then
2117 Val := Expression (Assoc);
2126 end Get_Explicit_Discriminant_Value;
2128 -------------------------------
2129 -- Init_Hidden_Discriminants --
2130 -------------------------------
2132 procedure Init_Hidden_Discriminants (Typ : Entity_Id; List : List_Id) is
2133 function Is_Completely_Hidden_Discriminant
2134 (Discr : Entity_Id) return Boolean;
2135 -- Determine whether Discr is a completely hidden discriminant of
2138 ---------------------------------------
2139 -- Is_Completely_Hidden_Discriminant --
2140 ---------------------------------------
2142 function Is_Completely_Hidden_Discriminant
2143 (Discr : Entity_Id) return Boolean
2148 -- Use First/Next_Entity as First/Next_Discriminant do not yield
2149 -- completely hidden discriminants.
2151 Item := First_Entity (Typ);
2152 while Present (Item) loop
2153 if Ekind (Item) = E_Discriminant
2154 and then Is_Completely_Hidden (Item)
2155 and then Chars (Original_Record_Component (Item)) =
2165 end Is_Completely_Hidden_Discriminant;
2169 Base_Typ : Entity_Id;
2171 Discr_Constr : Elmt_Id;
2172 Discr_Init : Node_Id;
2173 Discr_Val : Node_Id;
2174 In_Aggr_Type : Boolean;
2175 Par_Typ : Entity_Id;
2177 -- Start of processing for Init_Hidden_Discriminants
2180 -- The constraints on the hidden discriminants, if present, are kept
2181 -- in the Stored_Constraint list of the type itself, or in that of
2182 -- the base type. If not in the constraints of the aggregate itself,
2183 -- we examine ancestors to find discriminants that are not renamed
2184 -- by other discriminants but constrained explicitly.
2186 In_Aggr_Type := True;
2188 Base_Typ := Base_Type (Typ);
2189 while Is_Derived_Type (Base_Typ)
2191 (Present (Stored_Constraint (Base_Typ))
2193 (In_Aggr_Type and then Present (Stored_Constraint (Typ))))
2195 Par_Typ := Etype (Base_Typ);
2197 if not Has_Discriminants (Par_Typ) then
2201 Discr := First_Discriminant (Par_Typ);
2203 -- We know that one of the stored-constraint lists is present
2205 if Present (Stored_Constraint (Base_Typ)) then
2206 Discr_Constr := First_Elmt (Stored_Constraint (Base_Typ));
2208 -- For private extension, stored constraint may be on full view
2210 elsif Is_Private_Type (Base_Typ)
2211 and then Present (Full_View (Base_Typ))
2212 and then Present (Stored_Constraint (Full_View (Base_Typ)))
2215 First_Elmt (Stored_Constraint (Full_View (Base_Typ)));
2218 Discr_Constr := First_Elmt (Stored_Constraint (Typ));
2221 while Present (Discr) and then Present (Discr_Constr) loop
2222 Discr_Val := Node (Discr_Constr);
2224 -- The parent discriminant is renamed in the derived type,
2225 -- nothing to initialize.
2227 -- type Deriv_Typ (Discr : ...)
2228 -- is new Parent_Typ (Discr => Discr);
2230 if Is_Entity_Name (Discr_Val)
2231 and then Ekind (Entity (Discr_Val)) = E_Discriminant
2235 -- When the parent discriminant is constrained at the type
2236 -- extension level, it does not appear in the derived type.
2238 -- type Deriv_Typ (Discr : ...)
2239 -- is new Parent_Typ (Discr => Discr,
2240 -- Hidden_Discr => Expression);
2242 elsif Is_Completely_Hidden_Discriminant (Discr) then
2245 -- Otherwise initialize the discriminant
2249 Make_OK_Assignment_Statement (Loc,
2251 Make_Selected_Component (Loc,
2252 Prefix => New_Copy_Tree (Target),
2253 Selector_Name => New_Occurrence_Of (Discr, Loc)),
2254 Expression => New_Copy_Tree (Discr_Val));
2256 Set_No_Ctrl_Actions (Discr_Init);
2257 Append_To (List, Discr_Init);
2260 Next_Elmt (Discr_Constr);
2261 Next_Discriminant (Discr);
2264 In_Aggr_Type := False;
2265 Base_Typ := Base_Type (Par_Typ);
2267 end Init_Hidden_Discriminants;
2269 -------------------------
2270 -- Is_Int_Range_Bounds --
2271 -------------------------
2273 function Is_Int_Range_Bounds (Bounds : Node_Id) return Boolean is
2275 return Nkind (Bounds) = N_Range
2276 and then Nkind (Low_Bound (Bounds)) = N_Integer_Literal
2277 and then Nkind (High_Bound (Bounds)) = N_Integer_Literal;
2278 end Is_Int_Range_Bounds;
2280 -----------------------------------
2281 -- Generate_Finalization_Actions --
2282 -----------------------------------
2284 procedure Generate_Finalization_Actions is
2286 -- Do the work only the first time this is called
2288 if Finalization_Done then
2292 Finalization_Done := True;
2294 -- Determine the external finalization list. It is either the
2295 -- finalization list of the outer-scope or the one coming from an
2296 -- outer aggregate. When the target is not a temporary, the proper
2297 -- scope is the scope of the target rather than the potentially
2298 -- transient current scope.
2300 if Is_Controlled (Typ) and then Ancestor_Is_Subtype_Mark then
2301 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
2302 Set_Assignment_OK (Ref);
2305 Make_Procedure_Call_Statement (Loc,
2308 (Find_Prim_Op (Init_Typ, Name_Initialize), Loc),
2309 Parameter_Associations => New_List (New_Copy_Tree (Ref))));
2311 end Generate_Finalization_Actions;
2313 function Rewrite_Discriminant (Expr : Node_Id) return Traverse_Result;
2314 -- If default expression of a component mentions a discriminant of the
2315 -- type, it must be rewritten as the discriminant of the target object.
2317 function Replace_Type (Expr : Node_Id) return Traverse_Result;
2318 -- If the aggregate contains a self-reference, traverse each expression
2319 -- to replace a possible self-reference with a reference to the proper
2320 -- component of the target of the assignment.
2322 --------------------------
2323 -- Rewrite_Discriminant --
2324 --------------------------
2326 function Rewrite_Discriminant (Expr : Node_Id) return Traverse_Result is
2328 if Is_Entity_Name (Expr)
2329 and then Present (Entity (Expr))
2330 and then Ekind (Entity (Expr)) = E_In_Parameter
2331 and then Present (Discriminal_Link (Entity (Expr)))
2332 and then Scope (Discriminal_Link (Entity (Expr))) =
2333 Base_Type (Etype (N))
2336 Make_Selected_Component (Loc,
2337 Prefix => New_Copy_Tree (Lhs),
2338 Selector_Name => Make_Identifier (Loc, Chars (Expr))));
2342 end Rewrite_Discriminant;
2348 function Replace_Type (Expr : Node_Id) return Traverse_Result is
2350 -- Note regarding the Root_Type test below: Aggregate components for
2351 -- self-referential types include attribute references to the current
2352 -- instance, of the form: Typ'access, etc.. These references are
2353 -- rewritten as references to the target of the aggregate: the
2354 -- left-hand side of an assignment, the entity in a declaration,
2355 -- or a temporary. Without this test, we would improperly extended
2356 -- this rewriting to attribute references whose prefix was not the
2357 -- type of the aggregate.
2359 if Nkind (Expr) = N_Attribute_Reference
2360 and then Is_Entity_Name (Prefix (Expr))
2361 and then Is_Type (Entity (Prefix (Expr)))
2362 and then Root_Type (Etype (N)) = Root_Type (Entity (Prefix (Expr)))
2364 if Is_Entity_Name (Lhs) then
2365 Rewrite (Prefix (Expr),
2366 New_Occurrence_Of (Entity (Lhs), Loc));
2368 elsif Nkind (Lhs) = N_Selected_Component then
2370 Make_Attribute_Reference (Loc,
2371 Attribute_Name => Name_Unrestricted_Access,
2372 Prefix => New_Copy_Tree (Lhs)));
2373 Set_Analyzed (Parent (Expr), False);
2377 Make_Attribute_Reference (Loc,
2378 Attribute_Name => Name_Unrestricted_Access,
2379 Prefix => New_Copy_Tree (Lhs)));
2380 Set_Analyzed (Parent (Expr), False);
2387 procedure Replace_Self_Reference is
2388 new Traverse_Proc (Replace_Type);
2390 procedure Replace_Discriminants is
2391 new Traverse_Proc (Rewrite_Discriminant);
2393 -- Start of processing for Build_Record_Aggr_Code
2396 if Has_Self_Reference (N) then
2397 Replace_Self_Reference (N);
2400 -- If the target of the aggregate is class-wide, we must convert it
2401 -- to the actual type of the aggregate, so that the proper components
2402 -- are visible. We know already that the types are compatible.
2404 if Present (Etype (Lhs))
2405 and then Is_Class_Wide_Type (Etype (Lhs))
2407 Target := Unchecked_Convert_To (Typ, Lhs);
2412 -- Deal with the ancestor part of extension aggregates or with the
2413 -- discriminants of the root type.
2415 if Nkind (N) = N_Extension_Aggregate then
2417 Ancestor : constant Node_Id := Ancestor_Part (N);
2421 -- If the ancestor part is a subtype mark "T", we generate
2423 -- init-proc (T (tmp)); if T is constrained and
2424 -- init-proc (S (tmp)); where S applies an appropriate
2425 -- constraint if T is unconstrained
2427 if Is_Entity_Name (Ancestor)
2428 and then Is_Type (Entity (Ancestor))
2430 Ancestor_Is_Subtype_Mark := True;
2432 if Is_Constrained (Entity (Ancestor)) then
2433 Init_Typ := Entity (Ancestor);
2435 -- For an ancestor part given by an unconstrained type mark,
2436 -- create a subtype constrained by appropriate corresponding
2437 -- discriminant values coming from either associations of the
2438 -- aggregate or a constraint on a parent type. The subtype will
2439 -- be used to generate the correct default value for the
2442 elsif Has_Discriminants (Entity (Ancestor)) then
2444 Anc_Typ : constant Entity_Id := Entity (Ancestor);
2445 Anc_Constr : constant List_Id := New_List;
2446 Discrim : Entity_Id;
2447 Disc_Value : Node_Id;
2448 New_Indic : Node_Id;
2449 Subt_Decl : Node_Id;
2452 Discrim := First_Discriminant (Anc_Typ);
2453 while Present (Discrim) loop
2454 Disc_Value := Ancestor_Discriminant_Value (Discrim);
2456 -- If no usable discriminant in ancestors, check
2457 -- whether aggregate has an explicit value for it.
2459 if No (Disc_Value) then
2461 Get_Explicit_Discriminant_Value (Discrim);
2464 Append_To (Anc_Constr, Disc_Value);
2465 Next_Discriminant (Discrim);
2469 Make_Subtype_Indication (Loc,
2470 Subtype_Mark => New_Occurrence_Of (Anc_Typ, Loc),
2472 Make_Index_Or_Discriminant_Constraint (Loc,
2473 Constraints => Anc_Constr));
2475 Init_Typ := Create_Itype (Ekind (Anc_Typ), N);
2478 Make_Subtype_Declaration (Loc,
2479 Defining_Identifier => Init_Typ,
2480 Subtype_Indication => New_Indic);
2482 -- Itypes must be analyzed with checks off Declaration
2483 -- must have a parent for proper handling of subsidiary
2486 Set_Parent (Subt_Decl, N);
2487 Analyze (Subt_Decl, Suppress => All_Checks);
2491 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
2492 Set_Assignment_OK (Ref);
2494 if not Is_Interface (Init_Typ) then
2496 Build_Initialization_Call (Loc,
2499 In_Init_Proc => Within_Init_Proc,
2500 With_Default_Init => Has_Default_Init_Comps (N)
2502 Has_Task (Base_Type (Init_Typ))));
2504 if Is_Constrained (Entity (Ancestor))
2505 and then Has_Discriminants (Entity (Ancestor))
2507 Check_Ancestor_Discriminants (Entity (Ancestor));
2511 -- Handle calls to C++ constructors
2513 elsif Is_CPP_Constructor_Call (Ancestor) then
2514 Init_Typ := Etype (Ancestor);
2515 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
2516 Set_Assignment_OK (Ref);
2519 Build_Initialization_Call (Loc,
2522 In_Init_Proc => Within_Init_Proc,
2523 With_Default_Init => Has_Default_Init_Comps (N),
2524 Constructor_Ref => Ancestor));
2526 -- Ada 2005 (AI-287): If the ancestor part is an aggregate of
2527 -- limited type, a recursive call expands the ancestor. Note that
2528 -- in the limited case, the ancestor part must be either a
2529 -- function call (possibly qualified, or wrapped in an unchecked
2530 -- conversion) or aggregate (definitely qualified).
2532 -- The ancestor part can also be a function call (that may be
2533 -- transformed into an explicit dereference) or a qualification
2536 elsif Is_Limited_Type (Etype (Ancestor))
2537 and then Nkind_In (Unqualify (Ancestor), N_Aggregate,
2538 N_Extension_Aggregate)
2540 Ancestor_Is_Expression := True;
2542 -- Set up finalization data for enclosing record, because
2543 -- controlled subcomponents of the ancestor part will be
2546 Generate_Finalization_Actions;
2549 Build_Record_Aggr_Code
2550 (N => Unqualify (Ancestor),
2551 Typ => Etype (Unqualify (Ancestor)),
2554 -- If the ancestor part is an expression "E", we generate
2558 -- In Ada 2005, this includes the case of a (possibly qualified)
2559 -- limited function call. The assignment will turn into a
2560 -- build-in-place function call (for further details, see
2561 -- Make_Build_In_Place_Call_In_Assignment).
2564 Ancestor_Is_Expression := True;
2565 Init_Typ := Etype (Ancestor);
2567 -- If the ancestor part is an aggregate, force its full
2568 -- expansion, which was delayed.
2570 if Nkind_In (Unqualify (Ancestor), N_Aggregate,
2571 N_Extension_Aggregate)
2573 Set_Analyzed (Ancestor, False);
2574 Set_Analyzed (Expression (Ancestor), False);
2577 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
2578 Set_Assignment_OK (Ref);
2580 -- Make the assignment without usual controlled actions, since
2581 -- we only want to Adjust afterwards, but not to Finalize
2582 -- beforehand. Add manual Adjust when necessary.
2584 Assign := New_List (
2585 Make_OK_Assignment_Statement (Loc,
2587 Expression => Ancestor));
2588 Set_No_Ctrl_Actions (First (Assign));
2590 -- Assign the tag now to make sure that the dispatching call in
2591 -- the subsequent deep_adjust works properly (unless
2592 -- Tagged_Type_Expansion where tags are implicit).
2594 if Tagged_Type_Expansion then
2596 Make_OK_Assignment_Statement (Loc,
2598 Make_Selected_Component (Loc,
2599 Prefix => New_Copy_Tree (Target),
2602 (First_Tag_Component (Base_Type (Typ)), Loc)),
2605 Unchecked_Convert_To (RTE (RE_Tag),
2608 (Access_Disp_Table (Base_Type (Typ)))),
2611 Set_Assignment_OK (Name (Instr));
2612 Append_To (Assign, Instr);
2614 -- Ada 2005 (AI-251): If tagged type has progenitors we must
2615 -- also initialize tags of the secondary dispatch tables.
2617 if Has_Interfaces (Base_Type (Typ)) then
2619 (Typ => Base_Type (Typ),
2621 Stmts_List => Assign);
2625 -- Call Adjust manually
2627 if Needs_Finalization (Etype (Ancestor))
2628 and then not Is_Limited_Type (Etype (Ancestor))
2632 (Obj_Ref => New_Copy_Tree (Ref),
2633 Typ => Etype (Ancestor)));
2637 Make_Unsuppress_Block (Loc, Name_Discriminant_Check, Assign));
2639 if Has_Discriminants (Init_Typ) then
2640 Check_Ancestor_Discriminants (Init_Typ);
2645 -- Generate assignments of hidden discriminants. If the base type is
2646 -- an unchecked union, the discriminants are unknown to the back-end
2647 -- and absent from a value of the type, so assignments for them are
2650 if Has_Discriminants (Typ)
2651 and then not Is_Unchecked_Union (Base_Type (Typ))
2653 Init_Hidden_Discriminants (Typ, L);
2656 -- Normal case (not an extension aggregate)
2659 -- Generate the discriminant expressions, component by component.
2660 -- If the base type is an unchecked union, the discriminants are
2661 -- unknown to the back-end and absent from a value of the type, so
2662 -- assignments for them are not emitted.
2664 if Has_Discriminants (Typ)
2665 and then not Is_Unchecked_Union (Base_Type (Typ))
2667 Init_Hidden_Discriminants (Typ, L);
2669 -- Generate discriminant init values for the visible discriminants
2672 Discriminant : Entity_Id;
2673 Discriminant_Value : Node_Id;
2676 Discriminant := First_Stored_Discriminant (Typ);
2677 while Present (Discriminant) loop
2679 Make_Selected_Component (Loc,
2680 Prefix => New_Copy_Tree (Target),
2681 Selector_Name => New_Occurrence_Of (Discriminant, Loc));
2683 Discriminant_Value :=
2684 Get_Discriminant_Value
2687 Discriminant_Constraint (N_Typ));
2690 Make_OK_Assignment_Statement (Loc,
2692 Expression => New_Copy_Tree (Discriminant_Value));
2694 Set_No_Ctrl_Actions (Instr);
2695 Append_To (L, Instr);
2697 Next_Stored_Discriminant (Discriminant);
2703 -- For CPP types we generate an implicit call to the C++ default
2704 -- constructor to ensure the proper initialization of the _Tag
2707 if Is_CPP_Class (Root_Type (Typ)) and then CPP_Num_Prims (Typ) > 0 then
2708 Invoke_Constructor : declare
2709 CPP_Parent : constant Entity_Id := Enclosing_CPP_Parent (Typ);
2711 procedure Invoke_IC_Proc (T : Entity_Id);
2712 -- Recursive routine used to climb to parents. Required because
2713 -- parents must be initialized before descendants to ensure
2714 -- propagation of inherited C++ slots.
2716 --------------------
2717 -- Invoke_IC_Proc --
2718 --------------------
2720 procedure Invoke_IC_Proc (T : Entity_Id) is
2722 -- Avoid generating extra calls. Initialization required
2723 -- only for types defined from the level of derivation of
2724 -- type of the constructor and the type of the aggregate.
2726 if T = CPP_Parent then
2730 Invoke_IC_Proc (Etype (T));
2732 -- Generate call to the IC routine
2734 if Present (CPP_Init_Proc (T)) then
2736 Make_Procedure_Call_Statement (Loc,
2737 Name => New_Occurrence_Of (CPP_Init_Proc (T), Loc)));
2741 -- Start of processing for Invoke_Constructor
2744 -- Implicit invocation of the C++ constructor
2746 if Nkind (N) = N_Aggregate then
2748 Make_Procedure_Call_Statement (Loc,
2750 New_Occurrence_Of (Base_Init_Proc (CPP_Parent), Loc),
2751 Parameter_Associations => New_List (
2752 Unchecked_Convert_To (CPP_Parent,
2753 New_Copy_Tree (Lhs)))));
2756 Invoke_IC_Proc (Typ);
2757 end Invoke_Constructor;
2760 -- Generate the assignments, component by component
2762 -- tmp.comp1 := Expr1_From_Aggr;
2763 -- tmp.comp2 := Expr2_From_Aggr;
2766 Comp := First (Component_Associations (N));
2767 while Present (Comp) loop
2768 Selector := Entity (First (Choices (Comp)));
2772 if Is_CPP_Constructor_Call (Expression (Comp)) then
2774 Build_Initialization_Call (Loc,
2776 Make_Selected_Component (Loc,
2777 Prefix => New_Copy_Tree (Target),
2778 Selector_Name => New_Occurrence_Of (Selector, Loc)),
2779 Typ => Etype (Selector),
2781 With_Default_Init => True,
2782 Constructor_Ref => Expression (Comp)));
2784 -- Ada 2005 (AI-287): For each default-initialized component generate
2785 -- a call to the corresponding IP subprogram if available.
2787 elsif Box_Present (Comp)
2788 and then Has_Non_Null_Base_Init_Proc (Etype (Selector))
2790 if Ekind (Selector) /= E_Discriminant then
2791 Generate_Finalization_Actions;
2794 -- Ada 2005 (AI-287): If the component type has tasks then
2795 -- generate the activation chain and master entities (except
2796 -- in case of an allocator because in that case these entities
2797 -- are generated by Build_Task_Allocate_Block_With_Init_Stmts).
2800 Ctype : constant Entity_Id := Etype (Selector);
2801 Inside_Allocator : Boolean := False;
2802 P : Node_Id := Parent (N);
2805 if Is_Task_Type (Ctype) or else Has_Task (Ctype) then
2806 while Present (P) loop
2807 if Nkind (P) = N_Allocator then
2808 Inside_Allocator := True;
2815 if not Inside_Init_Proc and not Inside_Allocator then
2816 Build_Activation_Chain_Entity (N);
2822 Build_Initialization_Call (Loc,
2823 Id_Ref => Make_Selected_Component (Loc,
2824 Prefix => New_Copy_Tree (Target),
2826 New_Occurrence_Of (Selector, Loc)),
2827 Typ => Etype (Selector),
2829 With_Default_Init => True));
2831 -- Prepare for component assignment
2833 elsif Ekind (Selector) /= E_Discriminant
2834 or else Nkind (N) = N_Extension_Aggregate
2836 -- All the discriminants have now been assigned
2838 -- This is now a good moment to initialize and attach all the
2839 -- controllers. Their position may depend on the discriminants.
2841 if Ekind (Selector) /= E_Discriminant then
2842 Generate_Finalization_Actions;
2845 Comp_Type := Underlying_Type (Etype (Selector));
2847 Make_Selected_Component (Loc,
2848 Prefix => New_Copy_Tree (Target),
2849 Selector_Name => New_Occurrence_Of (Selector, Loc));
2851 if Nkind (Expression (Comp)) = N_Qualified_Expression then
2852 Expr_Q := Expression (Expression (Comp));
2854 Expr_Q := Expression (Comp);
2857 -- Now either create the assignment or generate the code for the
2858 -- inner aggregate top-down.
2860 if Is_Delayed_Aggregate (Expr_Q) then
2862 -- We have the following case of aggregate nesting inside
2863 -- an object declaration:
2865 -- type Arr_Typ is array (Integer range <>) of ...;
2867 -- type Rec_Typ (...) is record
2868 -- Obj_Arr_Typ : Arr_Typ (A .. B);
2871 -- Obj_Rec_Typ : Rec_Typ := (...,
2872 -- Obj_Arr_Typ => (X => (...), Y => (...)));
2874 -- The length of the ranges of the aggregate and Obj_Add_Typ
2875 -- are equal (B - A = Y - X), but they do not coincide (X /=
2876 -- A and B /= Y). This case requires array sliding which is
2877 -- performed in the following manner:
2879 -- subtype Arr_Sub is Arr_Typ (X .. Y);
2881 -- Temp (X) := (...);
2883 -- Temp (Y) := (...);
2884 -- Obj_Rec_Typ.Obj_Arr_Typ := Temp;
2886 if Ekind (Comp_Type) = E_Array_Subtype
2887 and then Is_Int_Range_Bounds (Aggregate_Bounds (Expr_Q))
2888 and then Is_Int_Range_Bounds (First_Index (Comp_Type))
2890 Compatible_Int_Bounds
2891 (Agg_Bounds => Aggregate_Bounds (Expr_Q),
2892 Typ_Bounds => First_Index (Comp_Type))
2894 -- Create the array subtype with bounds equal to those of
2895 -- the corresponding aggregate.
2898 SubE : constant Entity_Id := Make_Temporary (Loc, 'T');
2900 SubD : constant Node_Id :=
2901 Make_Subtype_Declaration (Loc,
2902 Defining_Identifier => SubE,
2903 Subtype_Indication =>
2904 Make_Subtype_Indication (Loc,
2906 New_Occurrence_Of (Etype (Comp_Type), Loc),
2908 Make_Index_Or_Discriminant_Constraint
2910 Constraints => New_List (
2912 (Aggregate_Bounds (Expr_Q))))));
2914 -- Create a temporary array of the above subtype which
2915 -- will be used to capture the aggregate assignments.
2917 TmpE : constant Entity_Id := Make_Temporary (Loc, 'A', N);
2919 TmpD : constant Node_Id :=
2920 Make_Object_Declaration (Loc,
2921 Defining_Identifier => TmpE,
2922 Object_Definition => New_Occurrence_Of (SubE, Loc));
2925 Set_No_Initialization (TmpD);
2926 Append_To (L, SubD);
2927 Append_To (L, TmpD);
2929 -- Expand aggregate into assignments to the temp array
2932 Late_Expansion (Expr_Q, Comp_Type,
2933 New_Occurrence_Of (TmpE, Loc)));
2938 Make_Assignment_Statement (Loc,
2939 Name => New_Copy_Tree (Comp_Expr),
2940 Expression => New_Occurrence_Of (TmpE, Loc)));
2943 -- Normal case (sliding not required)
2947 Late_Expansion (Expr_Q, Comp_Type, Comp_Expr));
2950 -- Expr_Q is not delayed aggregate
2953 if Has_Discriminants (Typ) then
2954 Replace_Discriminants (Expr_Q);
2956 -- If the component is an array type that depends on
2957 -- discriminants, and the expression is a single Others
2958 -- clause, create an explicit subtype for it because the
2959 -- backend has troubles recovering the actual bounds.
2961 if Nkind (Expr_Q) = N_Aggregate
2962 and then Is_Array_Type (Comp_Type)
2963 and then Present (Component_Associations (Expr_Q))
2966 Assoc : constant Node_Id :=
2967 First (Component_Associations (Expr_Q));
2971 if Nkind (First (Choices (Assoc))) = N_Others_Choice
2974 Build_Actual_Subtype_Of_Component
2975 (Comp_Type, Comp_Expr);
2977 -- If the component type does not in fact depend on
2978 -- discriminants, the subtype declaration is empty.
2980 if Present (Decl) then
2981 Append_To (L, Decl);
2982 Set_Etype (Comp_Expr, Defining_Entity (Decl));
2990 and then Nkind (Expr_Q) = N_Aggregate
2991 and then Is_Array_Type (Etype (Expr_Q))
2992 and then Present (First_Index (Etype (Expr_Q)))
2995 Expr_Q_Type : constant Node_Id := Etype (Expr_Q);
2998 Build_Array_Aggr_Code
3000 Ctype => Component_Type (Expr_Q_Type),
3001 Index => First_Index (Expr_Q_Type),
3003 Scalar_Comp => Is_Scalar_Type
3004 (Component_Type (Expr_Q_Type))));
3009 Make_OK_Assignment_Statement (Loc,
3011 Expression => Expr_Q);
3013 Set_No_Ctrl_Actions (Instr);
3014 Append_To (L, Instr);
3017 -- Adjust the tag if tagged (because of possible view
3018 -- conversions), unless compiling for a VM where tags are
3021 -- tmp.comp._tag := comp_typ'tag;
3023 if Is_Tagged_Type (Comp_Type)
3024 and then Tagged_Type_Expansion
3027 Make_OK_Assignment_Statement (Loc,
3029 Make_Selected_Component (Loc,
3030 Prefix => New_Copy_Tree (Comp_Expr),
3033 (First_Tag_Component (Comp_Type), Loc)),
3036 Unchecked_Convert_To (RTE (RE_Tag),
3038 (Node (First_Elmt (Access_Disp_Table (Comp_Type))),
3041 Append_To (L, Instr);
3045 -- Adjust (tmp.comp);
3047 if Needs_Finalization (Comp_Type)
3048 and then not Is_Limited_Type (Comp_Type)
3052 (Obj_Ref => New_Copy_Tree (Comp_Expr),
3057 -- comment would be good here ???
3059 elsif Ekind (Selector) = E_Discriminant
3060 and then Nkind (N) /= N_Extension_Aggregate
3061 and then Nkind (Parent (N)) = N_Component_Association
3062 and then Is_Constrained (Typ)
3064 -- We must check that the discriminant value imposed by the
3065 -- context is the same as the value given in the subaggregate,
3066 -- because after the expansion into assignments there is no
3067 -- record on which to perform a regular discriminant check.
3074 D_Val := First_Elmt (Discriminant_Constraint (Typ));
3075 Disc := First_Discriminant (Typ);
3076 while Chars (Disc) /= Chars (Selector) loop
3077 Next_Discriminant (Disc);
3081 pragma Assert (Present (D_Val));
3083 -- This check cannot performed for components that are
3084 -- constrained by a current instance, because this is not a
3085 -- value that can be compared with the actual constraint.
3087 if Nkind (Node (D_Val)) /= N_Attribute_Reference
3088 or else not Is_Entity_Name (Prefix (Node (D_Val)))
3089 or else not Is_Type (Entity (Prefix (Node (D_Val))))
3092 Make_Raise_Constraint_Error (Loc,
3095 Left_Opnd => New_Copy_Tree (Node (D_Val)),
3096 Right_Opnd => Expression (Comp)),
3097 Reason => CE_Discriminant_Check_Failed));
3100 -- Find self-reference in previous discriminant assignment,
3101 -- and replace with proper expression.
3108 while Present (Ass) loop
3109 if Nkind (Ass) = N_Assignment_Statement
3110 and then Nkind (Name (Ass)) = N_Selected_Component
3111 and then Chars (Selector_Name (Name (Ass))) =
3115 (Ass, New_Copy_Tree (Expression (Comp)));
3128 -- If the type is tagged, the tag needs to be initialized (unless we
3129 -- are in VM-mode where tags are implicit). It is done late in the
3130 -- initialization process because in some cases, we call the init
3131 -- proc of an ancestor which will not leave out the right tag.
3133 if Ancestor_Is_Expression then
3136 -- For CPP types we generated a call to the C++ default constructor
3137 -- before the components have been initialized to ensure the proper
3138 -- initialization of the _Tag component (see above).
3140 elsif Is_CPP_Class (Typ) then
3143 elsif Is_Tagged_Type (Typ) and then Tagged_Type_Expansion then
3145 Make_OK_Assignment_Statement (Loc,
3147 Make_Selected_Component (Loc,
3148 Prefix => New_Copy_Tree (Target),
3151 (First_Tag_Component (Base_Type (Typ)), Loc)),
3154 Unchecked_Convert_To (RTE (RE_Tag),
3156 (Node (First_Elmt (Access_Disp_Table (Base_Type (Typ)))),
3159 Append_To (L, Instr);
3161 -- Ada 2005 (AI-251): If the tagged type has been derived from an
3162 -- abstract interfaces we must also initialize the tags of the
3163 -- secondary dispatch tables.
3165 if Has_Interfaces (Base_Type (Typ)) then
3167 (Typ => Base_Type (Typ),
3173 -- If the controllers have not been initialized yet (by lack of non-
3174 -- discriminant components), let's do it now.
3176 Generate_Finalization_Actions;
3179 end Build_Record_Aggr_Code;
3181 ---------------------------------------
3182 -- Collect_Initialization_Statements --
3183 ---------------------------------------
3185 procedure Collect_Initialization_Statements
3188 Node_After : Node_Id)
3190 Loc : constant Source_Ptr := Sloc (N);
3191 Init_Actions : constant List_Id := New_List;
3192 Init_Node : Node_Id;
3193 Comp_Stmt : Node_Id;
3196 -- Nothing to do if Obj is already frozen, as in this case we known we
3197 -- won't need to move the initialization statements about later on.
3199 if Is_Frozen (Obj) then
3204 while Next (Init_Node) /= Node_After loop
3205 Append_To (Init_Actions, Remove_Next (Init_Node));
3208 if not Is_Empty_List (Init_Actions) then
3209 Comp_Stmt := Make_Compound_Statement (Loc, Actions => Init_Actions);
3210 Insert_Action_After (Init_Node, Comp_Stmt);
3211 Set_Initialization_Statements (Obj, Comp_Stmt);
3213 end Collect_Initialization_Statements;
3215 -------------------------------
3216 -- Convert_Aggr_In_Allocator --
3217 -------------------------------
3219 procedure Convert_Aggr_In_Allocator
3224 Loc : constant Source_Ptr := Sloc (Aggr);
3225 Typ : constant Entity_Id := Etype (Aggr);
3226 Temp : constant Entity_Id := Defining_Identifier (Decl);
3228 Occ : constant Node_Id :=
3229 Unchecked_Convert_To (Typ,
3230 Make_Explicit_Dereference (Loc, New_Occurrence_Of (Temp, Loc)));
3233 if Is_Array_Type (Typ) then
3234 Convert_Array_Aggr_In_Allocator (Decl, Aggr, Occ);
3236 elsif Has_Default_Init_Comps (Aggr) then
3238 L : constant List_Id := New_List;
3239 Init_Stmts : List_Id;
3242 Init_Stmts := Late_Expansion (Aggr, Typ, Occ);
3244 if Has_Task (Typ) then
3245 Build_Task_Allocate_Block_With_Init_Stmts (L, Aggr, Init_Stmts);
3246 Insert_Actions (Alloc, L);
3248 Insert_Actions (Alloc, Init_Stmts);
3253 Insert_Actions (Alloc, Late_Expansion (Aggr, Typ, Occ));
3255 end Convert_Aggr_In_Allocator;
3257 --------------------------------
3258 -- Convert_Aggr_In_Assignment --
3259 --------------------------------
3261 procedure Convert_Aggr_In_Assignment (N : Node_Id) is
3262 Aggr : Node_Id := Expression (N);
3263 Typ : constant Entity_Id := Etype (Aggr);
3264 Occ : constant Node_Id := New_Copy_Tree (Name (N));
3267 if Nkind (Aggr) = N_Qualified_Expression then
3268 Aggr := Expression (Aggr);
3271 Insert_Actions_After (N, Late_Expansion (Aggr, Typ, Occ));
3272 end Convert_Aggr_In_Assignment;
3274 ---------------------------------
3275 -- Convert_Aggr_In_Object_Decl --
3276 ---------------------------------
3278 procedure Convert_Aggr_In_Object_Decl (N : Node_Id) is
3279 Obj : constant Entity_Id := Defining_Identifier (N);
3280 Aggr : Node_Id := Expression (N);
3281 Loc : constant Source_Ptr := Sloc (Aggr);
3282 Typ : constant Entity_Id := Etype (Aggr);
3283 Occ : constant Node_Id := New_Occurrence_Of (Obj, Loc);
3285 function Discriminants_Ok return Boolean;
3286 -- If the object type is constrained, the discriminants in the
3287 -- aggregate must be checked against the discriminants of the subtype.
3288 -- This cannot be done using Apply_Discriminant_Checks because after
3289 -- expansion there is no aggregate left to check.
3291 ----------------------
3292 -- Discriminants_Ok --
3293 ----------------------
3295 function Discriminants_Ok return Boolean is
3296 Cond : Node_Id := Empty;
3305 D := First_Discriminant (Typ);
3306 Disc1 := First_Elmt (Discriminant_Constraint (Typ));
3307 Disc2 := First_Elmt (Discriminant_Constraint (Etype (Obj)));
3308 while Present (Disc1) and then Present (Disc2) loop
3309 Val1 := Node (Disc1);
3310 Val2 := Node (Disc2);
3312 if not Is_OK_Static_Expression (Val1)
3313 or else not Is_OK_Static_Expression (Val2)
3315 Check := Make_Op_Ne (Loc,
3316 Left_Opnd => Duplicate_Subexpr (Val1),
3317 Right_Opnd => Duplicate_Subexpr (Val2));
3323 Cond := Make_Or_Else (Loc,
3325 Right_Opnd => Check);
3328 elsif Expr_Value (Val1) /= Expr_Value (Val2) then
3329 Apply_Compile_Time_Constraint_Error (Aggr,
3330 Msg => "incorrect value for discriminant&??",
3331 Reason => CE_Discriminant_Check_Failed,
3336 Next_Discriminant (D);
3341 -- If any discriminant constraint is non-static, emit a check
3343 if Present (Cond) then
3345 Make_Raise_Constraint_Error (Loc,
3347 Reason => CE_Discriminant_Check_Failed));
3351 end Discriminants_Ok;
3353 -- Start of processing for Convert_Aggr_In_Object_Decl
3356 Set_Assignment_OK (Occ);
3358 if Nkind (Aggr) = N_Qualified_Expression then
3359 Aggr := Expression (Aggr);
3362 if Has_Discriminants (Typ)
3363 and then Typ /= Etype (Obj)
3364 and then Is_Constrained (Etype (Obj))
3365 and then not Discriminants_Ok
3370 -- If the context is an extended return statement, it has its own
3371 -- finalization machinery (i.e. works like a transient scope) and
3372 -- we do not want to create an additional one, because objects on
3373 -- the finalization list of the return must be moved to the caller's
3374 -- finalization list to complete the return.
3376 -- However, if the aggregate is limited, it is built in place, and the
3377 -- controlled components are not assigned to intermediate temporaries
3378 -- so there is no need for a transient scope in this case either.
3380 if Requires_Transient_Scope (Typ)
3381 and then Ekind (Current_Scope) /= E_Return_Statement
3382 and then not Is_Limited_Type (Typ)
3384 Establish_Transient_Scope
3387 Is_Controlled (Typ) or else Has_Controlled_Component (Typ));
3391 Node_After : constant Node_Id := Next (N);
3393 Insert_Actions_After (N, Late_Expansion (Aggr, Typ, Occ));
3394 Collect_Initialization_Statements (Obj, N, Node_After);
3396 Set_No_Initialization (N);
3397 Initialize_Discriminants (N, Typ);
3398 end Convert_Aggr_In_Object_Decl;
3400 -------------------------------------
3401 -- Convert_Array_Aggr_In_Allocator --
3402 -------------------------------------
3404 procedure Convert_Array_Aggr_In_Allocator
3409 Aggr_Code : List_Id;
3410 Typ : constant Entity_Id := Etype (Aggr);
3411 Ctyp : constant Entity_Id := Component_Type (Typ);
3414 -- The target is an explicit dereference of the allocated object.
3415 -- Generate component assignments to it, as for an aggregate that
3416 -- appears on the right-hand side of an assignment statement.
3419 Build_Array_Aggr_Code (Aggr,
3421 Index => First_Index (Typ),
3423 Scalar_Comp => Is_Scalar_Type (Ctyp));
3425 Insert_Actions_After (Decl, Aggr_Code);
3426 end Convert_Array_Aggr_In_Allocator;
3428 ----------------------------
3429 -- Convert_To_Assignments --
3430 ----------------------------
3432 procedure Convert_To_Assignments (N : Node_Id; Typ : Entity_Id) is
3433 Loc : constant Source_Ptr := Sloc (N);
3437 Aggr_Code : List_Id;
3439 Target_Expr : Node_Id;
3440 Parent_Kind : Node_Kind;
3441 Unc_Decl : Boolean := False;
3442 Parent_Node : Node_Id;
3445 pragma Assert (not Is_Static_Dispatch_Table_Aggregate (N));
3446 pragma Assert (Is_Record_Type (Typ));
3448 Parent_Node := Parent (N);
3449 Parent_Kind := Nkind (Parent_Node);
3451 if Parent_Kind = N_Qualified_Expression then
3453 -- Check if we are in a unconstrained declaration because in this
3454 -- case the current delayed expansion mechanism doesn't work when
3455 -- the declared object size depend on the initializing expr.
3458 Parent_Node := Parent (Parent_Node);
3459 Parent_Kind := Nkind (Parent_Node);
3461 if Parent_Kind = N_Object_Declaration then
3463 not Is_Entity_Name (Object_Definition (Parent_Node))
3464 or else Has_Discriminants
3465 (Entity (Object_Definition (Parent_Node)))
3466 or else Is_Class_Wide_Type
3467 (Entity (Object_Definition (Parent_Node)));
3472 -- Just set the Delay flag in the cases where the transformation will be
3473 -- done top down from above.
3477 -- Internal aggregate (transformed when expanding the parent)
3479 or else Parent_Kind = N_Aggregate
3480 or else Parent_Kind = N_Extension_Aggregate
3481 or else Parent_Kind = N_Component_Association
3483 -- Allocator (see Convert_Aggr_In_Allocator)
3485 or else Parent_Kind = N_Allocator
3487 -- Object declaration (see Convert_Aggr_In_Object_Decl)
3489 or else (Parent_Kind = N_Object_Declaration and then not Unc_Decl)
3491 -- Safe assignment (see Convert_Aggr_Assignments). So far only the
3492 -- assignments in init procs are taken into account.
3494 or else (Parent_Kind = N_Assignment_Statement
3495 and then Inside_Init_Proc)
3497 -- (Ada 2005) An inherently limited type in a return statement, which
3498 -- will be handled in a build-in-place fashion, and may be rewritten
3499 -- as an extended return and have its own finalization machinery.
3500 -- In the case of a simple return, the aggregate needs to be delayed
3501 -- until the scope for the return statement has been created, so
3502 -- that any finalization chain will be associated with that scope.
3503 -- For extended returns, we delay expansion to avoid the creation
3504 -- of an unwanted transient scope that could result in premature
3505 -- finalization of the return object (which is built in place
3506 -- within the caller's scope).
3509 (Is_Limited_View (Typ)
3511 (Nkind (Parent (Parent_Node)) = N_Extended_Return_Statement
3512 or else Nkind (Parent_Node) = N_Simple_Return_Statement))
3514 Set_Expansion_Delayed (N);
3518 -- Otherwise, if a transient scope is required, create it now. If we
3519 -- are within an initialization procedure do not create such, because
3520 -- the target of the assignment must not be declared within a local
3521 -- block, and because cleanup will take place on return from the
3522 -- initialization procedure.
3523 -- Should the condition be more restrictive ???
3525 if Requires_Transient_Scope (Typ) and then not Inside_Init_Proc then
3526 Establish_Transient_Scope (N, Sec_Stack => Needs_Finalization (Typ));
3529 -- If the aggregate is non-limited, create a temporary. If it is limited
3530 -- and context is an assignment, this is a subaggregate for an enclosing
3531 -- aggregate being expanded. It must be built in place, so use target of
3532 -- the current assignment.
3534 if Is_Limited_Type (Typ)
3535 and then Nkind (Parent (N)) = N_Assignment_Statement
3537 Target_Expr := New_Copy_Tree (Name (Parent (N)));
3538 Insert_Actions (Parent (N),
3539 Build_Record_Aggr_Code (N, Typ, Target_Expr));
3540 Rewrite (Parent (N), Make_Null_Statement (Loc));
3543 Temp := Make_Temporary (Loc, 'A', N);
3545 -- If the type inherits unknown discriminants, use the view with
3546 -- known discriminants if available.
3548 if Has_Unknown_Discriminants (Typ)
3549 and then Present (Underlying_Record_View (Typ))
3551 T := Underlying_Record_View (Typ);
3557 Make_Object_Declaration (Loc,
3558 Defining_Identifier => Temp,
3559 Object_Definition => New_Occurrence_Of (T, Loc));
3561 Set_No_Initialization (Instr);
3562 Insert_Action (N, Instr);
3563 Initialize_Discriminants (Instr, T);
3565 Target_Expr := New_Occurrence_Of (Temp, Loc);
3566 Aggr_Code := Build_Record_Aggr_Code (N, T, Target_Expr);
3568 -- Save the last assignment statement associated with the aggregate
3569 -- when building a controlled object. This reference is utilized by
3570 -- the finalization machinery when marking an object as successfully
3573 if Needs_Finalization (T) then
3574 Set_Last_Aggregate_Assignment (Temp, Last (Aggr_Code));
3577 Insert_Actions (N, Aggr_Code);
3578 Rewrite (N, New_Occurrence_Of (Temp, Loc));
3579 Analyze_And_Resolve (N, T);
3581 end Convert_To_Assignments;
3583 ---------------------------
3584 -- Convert_To_Positional --
3585 ---------------------------
3587 procedure Convert_To_Positional
3589 Max_Others_Replicate : Nat := 5;
3590 Handle_Bit_Packed : Boolean := False)
3592 Typ : constant Entity_Id := Etype (N);
3594 Static_Components : Boolean := True;
3596 procedure Check_Static_Components;
3597 -- Check whether all components of the aggregate are compile-time known
3598 -- values, and can be passed as is to the back-end without further
3604 Ixb : Node_Id) return Boolean;
3605 -- Convert the aggregate into a purely positional form if possible. On
3606 -- entry the bounds of all dimensions are known to be static, and the
3607 -- total number of components is safe enough to expand.
3609 function Is_Flat (N : Node_Id; Dims : Int) return Boolean;
3610 -- Return True iff the array N is flat (which is not trivial in the case
3611 -- of multidimensional aggregates).
3613 -----------------------------
3614 -- Check_Static_Components --
3615 -----------------------------
3617 -- Could use some comments in this body ???
3619 procedure Check_Static_Components is
3623 Static_Components := True;
3625 if Nkind (N) = N_String_Literal then
3628 elsif Present (Expressions (N)) then
3629 Expr := First (Expressions (N));
3630 while Present (Expr) loop
3631 if Nkind (Expr) /= N_Aggregate
3632 or else not Compile_Time_Known_Aggregate (Expr)
3633 or else Expansion_Delayed (Expr)
3635 Static_Components := False;
3643 if Nkind (N) = N_Aggregate
3644 and then Present (Component_Associations (N))
3646 Expr := First (Component_Associations (N));
3647 while Present (Expr) loop
3648 if Nkind_In (Expression (Expr), N_Integer_Literal,
3653 elsif Is_Entity_Name (Expression (Expr))
3654 and then Present (Entity (Expression (Expr)))
3655 and then Ekind (Entity (Expression (Expr))) =
3656 E_Enumeration_Literal
3660 elsif Nkind (Expression (Expr)) /= N_Aggregate
3661 or else not Compile_Time_Known_Aggregate (Expression (Expr))
3662 or else Expansion_Delayed (Expression (Expr))
3664 Static_Components := False;
3671 end Check_Static_Components;
3680 Ixb : Node_Id) return Boolean
3682 Loc : constant Source_Ptr := Sloc (N);
3683 Blo : constant Node_Id := Type_Low_Bound (Etype (Ixb));
3684 Lo : constant Node_Id := Type_Low_Bound (Etype (Ix));
3685 Hi : constant Node_Id := Type_High_Bound (Etype (Ix));
3689 Others_Present : Boolean := False;
3692 if Nkind (Original_Node (N)) = N_String_Literal then
3696 if not Compile_Time_Known_Value (Lo)
3697 or else not Compile_Time_Known_Value (Hi)
3702 Lov := Expr_Value (Lo);
3703 Hiv := Expr_Value (Hi);
3705 -- Check if there is an others choice
3707 if Present (Component_Associations (N)) then
3713 Assoc := First (Component_Associations (N));
3714 while Present (Assoc) loop
3716 -- If this is a box association, flattening is in general
3717 -- not possible because at this point we cannot tell if the
3718 -- default is static or even exists.
3720 if Box_Present (Assoc) then
3724 Choice := First (Choices (Assoc));
3726 while Present (Choice) loop
3727 if Nkind (Choice) = N_Others_Choice then
3728 Others_Present := True;
3739 -- If the low bound is not known at compile time and others is not
3740 -- present we can proceed since the bounds can be obtained from the
3744 or else (not Compile_Time_Known_Value (Blo) and then Others_Present)
3749 -- Determine if set of alternatives is suitable for conversion and
3750 -- build an array containing the values in sequence.
3753 Vals : array (UI_To_Int (Lov) .. UI_To_Int (Hiv))
3754 of Node_Id := (others => Empty);
3755 -- The values in the aggregate sorted appropriately
3758 -- Same data as Vals in list form
3761 -- Used to validate Max_Others_Replicate limit
3764 Num : Int := UI_To_Int (Lov);
3770 if Present (Expressions (N)) then
3771 Elmt := First (Expressions (N));
3772 while Present (Elmt) loop
3773 if Nkind (Elmt) = N_Aggregate
3774 and then Present (Next_Index (Ix))
3776 not Flatten (Elmt, Next_Index (Ix), Next_Index (Ixb))
3781 Vals (Num) := Relocate_Node (Elmt);
3788 if No (Component_Associations (N)) then
3792 Elmt := First (Component_Associations (N));
3794 if Nkind (Expression (Elmt)) = N_Aggregate then
3795 if Present (Next_Index (Ix))
3798 (Expression (Elmt), Next_Index (Ix), Next_Index (Ixb))
3804 Component_Loop : while Present (Elmt) loop
3805 Choice := First (Choices (Elmt));
3806 Choice_Loop : while Present (Choice) loop
3808 -- If we have an others choice, fill in the missing elements
3809 -- subject to the limit established by Max_Others_Replicate.
3811 if Nkind (Choice) = N_Others_Choice then
3814 for J in Vals'Range loop
3815 if No (Vals (J)) then
3816 Vals (J) := New_Copy_Tree (Expression (Elmt));
3817 Rep_Count := Rep_Count + 1;
3819 -- Check for maximum others replication. Note that
3820 -- we skip this test if either of the restrictions
3821 -- No_Elaboration_Code or No_Implicit_Loops is
3822 -- active, if this is a preelaborable unit or
3823 -- a predefined unit, or if the unit must be
3824 -- placed in data memory. This also ensures that
3825 -- predefined units get the same level of constant
3826 -- folding in Ada 95 and Ada 2005, where their
3827 -- categorization has changed.
3830 P : constant Entity_Id :=
3831 Cunit_Entity (Current_Sem_Unit);
3834 -- Check if duplication OK and if so continue
3837 if Restriction_Active (No_Elaboration_Code)
3838 or else Restriction_Active (No_Implicit_Loops)
3840 (Ekind (Current_Scope) = E_Package
3841 and then Static_Elaboration_Desired
3843 or else Is_Preelaborated (P)
3844 or else (Ekind (P) = E_Package_Body
3846 Is_Preelaborated (Spec_Entity (P)))
3848 Is_Predefined_File_Name
3849 (Unit_File_Name (Get_Source_Unit (P)))
3853 -- If duplication not OK, then we return False
3854 -- if the replication count is too high
3856 elsif Rep_Count > Max_Others_Replicate then
3859 -- Continue on if duplication not OK, but the
3860 -- replication count is not excessive.
3869 exit Component_Loop;
3871 -- Case of a subtype mark, identifier or expanded name
3873 elsif Is_Entity_Name (Choice)
3874 and then Is_Type (Entity (Choice))
3876 Lo := Type_Low_Bound (Etype (Choice));
3877 Hi := Type_High_Bound (Etype (Choice));
3879 -- Case of subtype indication
3881 elsif Nkind (Choice) = N_Subtype_Indication then
3882 Lo := Low_Bound (Range_Expression (Constraint (Choice)));
3883 Hi := High_Bound (Range_Expression (Constraint (Choice)));
3887 elsif Nkind (Choice) = N_Range then
3888 Lo := Low_Bound (Choice);
3889 Hi := High_Bound (Choice);
3891 -- Normal subexpression case
3893 else pragma Assert (Nkind (Choice) in N_Subexpr);
3894 if not Compile_Time_Known_Value (Choice) then
3898 Choice_Index := UI_To_Int (Expr_Value (Choice));
3900 if Choice_Index in Vals'Range then
3901 Vals (Choice_Index) :=
3902 New_Copy_Tree (Expression (Elmt));
3905 -- Choice is statically out-of-range, will be
3906 -- rewritten to raise Constraint_Error.
3914 -- Range cases merge with Lo,Hi set
3916 if not Compile_Time_Known_Value (Lo)
3918 not Compile_Time_Known_Value (Hi)
3923 for J in UI_To_Int (Expr_Value (Lo)) ..
3924 UI_To_Int (Expr_Value (Hi))
3926 Vals (J) := New_Copy_Tree (Expression (Elmt));
3932 end loop Choice_Loop;
3935 end loop Component_Loop;
3937 -- If we get here the conversion is possible
3940 for J in Vals'Range loop
3941 Append (Vals (J), Vlist);
3944 Rewrite (N, Make_Aggregate (Loc, Expressions => Vlist));
3945 Set_Aggregate_Bounds (N, Aggregate_Bounds (Original_Node (N)));
3954 function Is_Flat (N : Node_Id; Dims : Int) return Boolean is
3961 elsif Nkind (N) = N_Aggregate then
3962 if Present (Component_Associations (N)) then
3966 Elmt := First (Expressions (N));
3967 while Present (Elmt) loop
3968 if not Is_Flat (Elmt, Dims - 1) then
3982 -- Start of processing for Convert_To_Positional
3985 -- Only convert to positional when generating C in case of an
3986 -- object declaration, this is the only case where aggregates are
3989 if Modify_Tree_For_C and then not In_Object_Declaration (N) then
3993 -- Ada 2005 (AI-287): Do not convert in case of default initialized
3994 -- components because in this case will need to call the corresponding
3997 if Has_Default_Init_Comps (N) then
4001 if Is_Flat (N, Number_Dimensions (Typ)) then
4005 if Is_Bit_Packed_Array (Typ) and then not Handle_Bit_Packed then
4009 -- Do not convert to positional if controlled components are involved
4010 -- since these require special processing
4012 if Has_Controlled_Component (Typ) then
4016 Check_Static_Components;
4018 -- If the size is known, or all the components are static, try to
4019 -- build a fully positional aggregate.
4021 -- The size of the type may not be known for an aggregate with
4022 -- discriminated array components, but if the components are static
4023 -- it is still possible to verify statically that the length is
4024 -- compatible with the upper bound of the type, and therefore it is
4025 -- worth flattening such aggregates as well.
4027 -- For now the back-end expands these aggregates into individual
4028 -- assignments to the target anyway, but it is conceivable that
4029 -- it will eventually be able to treat such aggregates statically???
4031 if Aggr_Size_OK (N, Typ)
4032 and then Flatten (N, First_Index (Typ), First_Index (Base_Type (Typ)))
4034 if Static_Components then
4035 Set_Compile_Time_Known_Aggregate (N);
4036 Set_Expansion_Delayed (N, False);
4039 Analyze_And_Resolve (N, Typ);
4042 -- Is Static_Eaboration_Desired has been specified, diagnose aggregates
4043 -- that will still require initialization code.
4045 if (Ekind (Current_Scope) = E_Package
4046 and then Static_Elaboration_Desired (Current_Scope))
4047 and then Nkind (Parent (N)) = N_Object_Declaration
4053 if Nkind (N) = N_Aggregate and then Present (Expressions (N)) then
4054 Expr := First (Expressions (N));
4055 while Present (Expr) loop
4056 if Nkind_In (Expr, N_Integer_Literal, N_Real_Literal)
4058 (Is_Entity_Name (Expr)
4059 and then Ekind (Entity (Expr)) = E_Enumeration_Literal)
4065 ("non-static object requires elaboration code??", N);
4072 if Present (Component_Associations (N)) then
4073 Error_Msg_N ("object requires elaboration code??", N);
4078 end Convert_To_Positional;
4080 ----------------------------
4081 -- Expand_Array_Aggregate --
4082 ----------------------------
4084 -- Array aggregate expansion proceeds as follows:
4086 -- 1. If requested we generate code to perform all the array aggregate
4087 -- bound checks, specifically
4089 -- (a) Check that the index range defined by aggregate bounds is
4090 -- compatible with corresponding index subtype.
4092 -- (b) If an others choice is present check that no aggregate
4093 -- index is outside the bounds of the index constraint.
4095 -- (c) For multidimensional arrays make sure that all subaggregates
4096 -- corresponding to the same dimension have the same bounds.
4098 -- 2. Check for packed array aggregate which can be converted to a
4099 -- constant so that the aggregate disappears completely.
4101 -- 3. Check case of nested aggregate. Generally nested aggregates are
4102 -- handled during the processing of the parent aggregate.
4104 -- 4. Check if the aggregate can be statically processed. If this is the
4105 -- case pass it as is to Gigi. Note that a necessary condition for
4106 -- static processing is that the aggregate be fully positional.
4108 -- 5. If in place aggregate expansion is possible (i.e. no need to create
4109 -- a temporary) then mark the aggregate as such and return. Otherwise
4110 -- create a new temporary and generate the appropriate initialization
4113 procedure Expand_Array_Aggregate (N : Node_Id) is
4114 Loc : constant Source_Ptr := Sloc (N);
4116 Typ : constant Entity_Id := Etype (N);
4117 Ctyp : constant Entity_Id := Component_Type (Typ);
4118 -- Typ is the correct constrained array subtype of the aggregate
4119 -- Ctyp is the corresponding component type.
4121 Aggr_Dimension : constant Pos := Number_Dimensions (Typ);
4122 -- Number of aggregate index dimensions
4124 Aggr_Low : array (1 .. Aggr_Dimension) of Node_Id;
4125 Aggr_High : array (1 .. Aggr_Dimension) of Node_Id;
4126 -- Low and High bounds of the constraint for each aggregate index
4128 Aggr_Index_Typ : array (1 .. Aggr_Dimension) of Entity_Id;
4129 -- The type of each index
4131 In_Place_Assign_OK_For_Declaration : Boolean := False;
4132 -- True if we are to generate an in place assignment for a declaration
4134 Maybe_In_Place_OK : Boolean;
4135 -- If the type is neither controlled nor packed and the aggregate
4136 -- is the expression in an assignment, assignment in place may be
4137 -- possible, provided other conditions are met on the LHS.
4139 Others_Present : array (1 .. Aggr_Dimension) of Boolean :=
4141 -- If Others_Present (J) is True, then there is an others choice
4142 -- in one of the sub-aggregates of N at dimension J.
4144 function Aggr_Assignment_OK_For_Backend (N : Node_Id) return Boolean;
4145 -- Returns true if an aggregate assignment can be done by the back end
4147 procedure Build_Constrained_Type (Positional : Boolean);
4148 -- If the subtype is not static or unconstrained, build a constrained
4149 -- type using the computable sizes of the aggregate and its sub-
4152 procedure Check_Bounds (Aggr_Bounds : Node_Id; Index_Bounds : Node_Id);
4153 -- Checks that the bounds of Aggr_Bounds are within the bounds defined
4156 procedure Check_Same_Aggr_Bounds (Sub_Aggr : Node_Id; Dim : Pos);
4157 -- Checks that in a multi-dimensional array aggregate all subaggregates
4158 -- corresponding to the same dimension have the same bounds.
4159 -- Sub_Aggr is an array sub-aggregate. Dim is the dimension
4160 -- corresponding to the sub-aggregate.
4162 procedure Compute_Others_Present (Sub_Aggr : Node_Id; Dim : Pos);
4163 -- Computes the values of array Others_Present. Sub_Aggr is the
4164 -- array sub-aggregate we start the computation from. Dim is the
4165 -- dimension corresponding to the sub-aggregate.
4167 function In_Place_Assign_OK return Boolean;
4168 -- Simple predicate to determine whether an aggregate assignment can
4169 -- be done in place, because none of the new values can depend on the
4170 -- components of the target of the assignment.
4172 procedure Others_Check (Sub_Aggr : Node_Id; Dim : Pos);
4173 -- Checks that if an others choice is present in any sub-aggregate no
4174 -- aggregate index is outside the bounds of the index constraint.
4175 -- Sub_Aggr is an array sub-aggregate. Dim is the dimension
4176 -- corresponding to the sub-aggregate.
4178 function Safe_Left_Hand_Side (N : Node_Id) return Boolean;
4179 -- In addition to Maybe_In_Place_OK, in order for an aggregate to be
4180 -- built directly into the target of the assignment it must be free
4183 ------------------------------------
4184 -- Aggr_Assignment_OK_For_Backend --
4185 ------------------------------------
4187 -- Backend processing by Gigi/gcc is possible only if all the following
4188 -- conditions are met:
4190 -- 1. N consists of a single OTHERS choice, possibly recursively
4192 -- 2. The array type is not packed
4194 -- 3. The array type has no atomic components
4196 -- 4. The array type has no null ranges (the purpose of this is to
4197 -- avoid a bogus warning for an out-of-range value).
4199 -- 5. The component type is discrete
4201 -- 6. The component size is Storage_Unit or the value is of the form
4202 -- M * (1 + A**1 + A**2 + .. A**(K-1)) where A = 2**(Storage_Unit)
4203 -- and M in 1 .. A-1. This can also be viewed as K occurrences of
4204 -- the 8-bit value M, concatenated together.
4206 -- The ultimate goal is to generate a call to a fast memset routine
4207 -- specifically optimized for the target.
4209 function Aggr_Assignment_OK_For_Backend (N : Node_Id) return Boolean is
4212 Expr : Node_Id := N;
4220 -- Recurse as far as possible to find the innermost component type
4223 while Is_Array_Type (Ctyp) loop
4224 if Nkind (Expr) /= N_Aggregate
4225 or else not Is_Others_Aggregate (Expr)
4230 if Present (Packed_Array_Impl_Type (Ctyp)) then
4234 if Has_Atomic_Components (Ctyp) then
4238 Index := First_Index (Ctyp);
4239 while Present (Index) loop
4240 Get_Index_Bounds (Index, Low, High);
4242 if Is_Null_Range (Low, High) then
4249 Expr := Expression (First (Component_Associations (Expr)));
4251 for J in 1 .. Number_Dimensions (Ctyp) - 1 loop
4252 if Nkind (Expr) /= N_Aggregate
4253 or else not Is_Others_Aggregate (Expr)
4258 Expr := Expression (First (Component_Associations (Expr)));
4261 Ctyp := Component_Type (Ctyp);
4263 if Is_Atomic_Or_VFA (Ctyp) then
4268 if not Is_Discrete_Type (Ctyp) then
4272 -- The expression needs to be analyzed if True is returned
4274 Analyze_And_Resolve (Expr, Ctyp);
4276 -- The back end uses the Esize as the precision of the type
4278 Nunits := UI_To_Int (Esize (Ctyp)) / System_Storage_Unit;
4284 if not Compile_Time_Known_Value (Expr) then
4288 Value := Expr_Value (Expr);
4290 if Has_Biased_Representation (Ctyp) then
4291 Value := Value - Expr_Value (Type_Low_Bound (Ctyp));
4294 -- Values 0 and -1 immediately satisfy the last check
4296 if Value = Uint_0 or else Value = Uint_Minus_1 then
4300 -- We need to work with an unsigned value
4303 Value := Value + 2**(System_Storage_Unit * Nunits);
4306 Remainder := Value rem 2**System_Storage_Unit;
4308 for J in 1 .. Nunits - 1 loop
4309 Value := Value / 2**System_Storage_Unit;
4311 if Value rem 2**System_Storage_Unit /= Remainder then
4317 end Aggr_Assignment_OK_For_Backend;
4319 ----------------------------
4320 -- Build_Constrained_Type --
4321 ----------------------------
4323 procedure Build_Constrained_Type (Positional : Boolean) is
4324 Loc : constant Source_Ptr := Sloc (N);
4325 Agg_Type : constant Entity_Id := Make_Temporary (Loc, 'A');
4328 Typ : constant Entity_Id := Etype (N);
4329 Indexes : constant List_Id := New_List;
4334 -- If the aggregate is purely positional, all its subaggregates
4335 -- have the same size. We collect the dimensions from the first
4336 -- subaggregate at each level.
4341 for D in 1 .. Number_Dimensions (Typ) loop
4342 Sub_Agg := First (Expressions (Sub_Agg));
4346 while Present (Comp) loop
4353 Low_Bound => Make_Integer_Literal (Loc, 1),
4354 High_Bound => Make_Integer_Literal (Loc, Num)));
4358 -- We know the aggregate type is unconstrained and the aggregate
4359 -- is not processable by the back end, therefore not necessarily
4360 -- positional. Retrieve each dimension bounds (computed earlier).
4362 for D in 1 .. Number_Dimensions (Typ) loop
4365 Low_Bound => Aggr_Low (D),
4366 High_Bound => Aggr_High (D)));
4371 Make_Full_Type_Declaration (Loc,
4372 Defining_Identifier => Agg_Type,
4374 Make_Constrained_Array_Definition (Loc,
4375 Discrete_Subtype_Definitions => Indexes,
4376 Component_Definition =>
4377 Make_Component_Definition (Loc,
4378 Aliased_Present => False,
4379 Subtype_Indication =>
4380 New_Occurrence_Of (Component_Type (Typ), Loc))));
4382 Insert_Action (N, Decl);
4384 Set_Etype (N, Agg_Type);
4385 Set_Is_Itype (Agg_Type);
4386 Freeze_Itype (Agg_Type, N);
4387 end Build_Constrained_Type;
4393 procedure Check_Bounds (Aggr_Bounds : Node_Id; Index_Bounds : Node_Id) is
4400 Cond : Node_Id := Empty;
4403 Get_Index_Bounds (Aggr_Bounds, Aggr_Lo, Aggr_Hi);
4404 Get_Index_Bounds (Index_Bounds, Ind_Lo, Ind_Hi);
4406 -- Generate the following test:
4408 -- [constraint_error when
4409 -- Aggr_Lo <= Aggr_Hi and then
4410 -- (Aggr_Lo < Ind_Lo or else Aggr_Hi > Ind_Hi)]
4412 -- As an optimization try to see if some tests are trivially vacuous
4413 -- because we are comparing an expression against itself.
4415 if Aggr_Lo = Ind_Lo and then Aggr_Hi = Ind_Hi then
4418 elsif Aggr_Hi = Ind_Hi then
4421 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
4422 Right_Opnd => Duplicate_Subexpr_Move_Checks (Ind_Lo));
4424 elsif Aggr_Lo = Ind_Lo then
4427 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi),
4428 Right_Opnd => Duplicate_Subexpr_Move_Checks (Ind_Hi));
4435 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
4436 Right_Opnd => Duplicate_Subexpr_Move_Checks (Ind_Lo)),
4440 Left_Opnd => Duplicate_Subexpr (Aggr_Hi),
4441 Right_Opnd => Duplicate_Subexpr (Ind_Hi)));
4444 if Present (Cond) then
4449 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
4450 Right_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi)),
4452 Right_Opnd => Cond);
4454 Set_Analyzed (Left_Opnd (Left_Opnd (Cond)), False);
4455 Set_Analyzed (Right_Opnd (Left_Opnd (Cond)), False);
4457 Make_Raise_Constraint_Error (Loc,
4459 Reason => CE_Range_Check_Failed));
4463 ----------------------------
4464 -- Check_Same_Aggr_Bounds --
4465 ----------------------------
4467 procedure Check_Same_Aggr_Bounds (Sub_Aggr : Node_Id; Dim : Pos) is
4468 Sub_Lo : constant Node_Id := Low_Bound (Aggregate_Bounds (Sub_Aggr));
4469 Sub_Hi : constant Node_Id := High_Bound (Aggregate_Bounds (Sub_Aggr));
4470 -- The bounds of this specific sub-aggregate
4472 Aggr_Lo : constant Node_Id := Aggr_Low (Dim);
4473 Aggr_Hi : constant Node_Id := Aggr_High (Dim);
4474 -- The bounds of the aggregate for this dimension
4476 Ind_Typ : constant Entity_Id := Aggr_Index_Typ (Dim);
4477 -- The index type for this dimension.xxx
4479 Cond : Node_Id := Empty;
4484 -- If index checks are on generate the test
4486 -- [constraint_error when
4487 -- Aggr_Lo /= Sub_Lo or else Aggr_Hi /= Sub_Hi]
4489 -- As an optimization try to see if some tests are trivially vacuos
4490 -- because we are comparing an expression against itself. Also for
4491 -- the first dimension the test is trivially vacuous because there
4492 -- is just one aggregate for dimension 1.
4494 if Index_Checks_Suppressed (Ind_Typ) then
4497 elsif Dim = 1 or else (Aggr_Lo = Sub_Lo and then Aggr_Hi = Sub_Hi)
4501 elsif Aggr_Hi = Sub_Hi then
4504 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
4505 Right_Opnd => Duplicate_Subexpr_Move_Checks (Sub_Lo));
4507 elsif Aggr_Lo = Sub_Lo then
4510 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi),
4511 Right_Opnd => Duplicate_Subexpr_Move_Checks (Sub_Hi));
4518 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
4519 Right_Opnd => Duplicate_Subexpr_Move_Checks (Sub_Lo)),
4523 Left_Opnd => Duplicate_Subexpr (Aggr_Hi),
4524 Right_Opnd => Duplicate_Subexpr (Sub_Hi)));
4527 if Present (Cond) then
4529 Make_Raise_Constraint_Error (Loc,
4531 Reason => CE_Length_Check_Failed));
4534 -- Now look inside the sub-aggregate to see if there is more work
4536 if Dim < Aggr_Dimension then
4538 -- Process positional components
4540 if Present (Expressions (Sub_Aggr)) then
4541 Expr := First (Expressions (Sub_Aggr));
4542 while Present (Expr) loop
4543 Check_Same_Aggr_Bounds (Expr, Dim + 1);
4548 -- Process component associations
4550 if Present (Component_Associations (Sub_Aggr)) then
4551 Assoc := First (Component_Associations (Sub_Aggr));
4552 while Present (Assoc) loop
4553 Expr := Expression (Assoc);
4554 Check_Same_Aggr_Bounds (Expr, Dim + 1);
4559 end Check_Same_Aggr_Bounds;
4561 ----------------------------
4562 -- Compute_Others_Present --
4563 ----------------------------
4565 procedure Compute_Others_Present (Sub_Aggr : Node_Id; Dim : Pos) is
4570 if Present (Component_Associations (Sub_Aggr)) then
4571 Assoc := Last (Component_Associations (Sub_Aggr));
4573 if Nkind (First (Choices (Assoc))) = N_Others_Choice then
4574 Others_Present (Dim) := True;
4578 -- Now look inside the sub-aggregate to see if there is more work
4580 if Dim < Aggr_Dimension then
4582 -- Process positional components
4584 if Present (Expressions (Sub_Aggr)) then
4585 Expr := First (Expressions (Sub_Aggr));
4586 while Present (Expr) loop
4587 Compute_Others_Present (Expr, Dim + 1);
4592 -- Process component associations
4594 if Present (Component_Associations (Sub_Aggr)) then
4595 Assoc := First (Component_Associations (Sub_Aggr));
4596 while Present (Assoc) loop
4597 Expr := Expression (Assoc);
4598 Compute_Others_Present (Expr, Dim + 1);
4603 end Compute_Others_Present;
4605 ------------------------
4606 -- In_Place_Assign_OK --
4607 ------------------------
4609 function In_Place_Assign_OK return Boolean is
4617 function Safe_Aggregate (Aggr : Node_Id) return Boolean;
4618 -- Check recursively that each component of a (sub)aggregate does
4619 -- not depend on the variable being assigned to.
4621 function Safe_Component (Expr : Node_Id) return Boolean;
4622 -- Verify that an expression cannot depend on the variable being
4623 -- assigned to. Room for improvement here (but less than before).
4625 --------------------
4626 -- Safe_Aggregate --
4627 --------------------
4629 function Safe_Aggregate (Aggr : Node_Id) return Boolean is
4633 if Present (Expressions (Aggr)) then
4634 Expr := First (Expressions (Aggr));
4635 while Present (Expr) loop
4636 if Nkind (Expr) = N_Aggregate then
4637 if not Safe_Aggregate (Expr) then
4641 elsif not Safe_Component (Expr) then
4649 if Present (Component_Associations (Aggr)) then
4650 Expr := First (Component_Associations (Aggr));
4651 while Present (Expr) loop
4652 if Nkind (Expression (Expr)) = N_Aggregate then
4653 if not Safe_Aggregate (Expression (Expr)) then
4657 -- If association has a box, no way to determine yet
4658 -- whether default can be assigned in place.
4660 elsif Box_Present (Expr) then
4663 elsif not Safe_Component (Expression (Expr)) then
4674 --------------------
4675 -- Safe_Component --
4676 --------------------
4678 function Safe_Component (Expr : Node_Id) return Boolean is
4679 Comp : Node_Id := Expr;
4681 function Check_Component (Comp : Node_Id) return Boolean;
4682 -- Do the recursive traversal, after copy
4684 ---------------------
4685 -- Check_Component --
4686 ---------------------
4688 function Check_Component (Comp : Node_Id) return Boolean is
4690 if Is_Overloaded (Comp) then
4694 return Compile_Time_Known_Value (Comp)
4696 or else (Is_Entity_Name (Comp)
4697 and then Present (Entity (Comp))
4698 and then No (Renamed_Object (Entity (Comp))))
4700 or else (Nkind (Comp) = N_Attribute_Reference
4701 and then Check_Component (Prefix (Comp)))
4703 or else (Nkind (Comp) in N_Binary_Op
4704 and then Check_Component (Left_Opnd (Comp))
4705 and then Check_Component (Right_Opnd (Comp)))
4707 or else (Nkind (Comp) in N_Unary_Op
4708 and then Check_Component (Right_Opnd (Comp)))
4710 or else (Nkind (Comp) = N_Selected_Component
4711 and then Check_Component (Prefix (Comp)))
4713 or else (Nkind (Comp) = N_Unchecked_Type_Conversion
4714 and then Check_Component (Expression (Comp)));
4715 end Check_Component;
4717 -- Start of processing for Safe_Component
4720 -- If the component appears in an association that may correspond
4721 -- to more than one element, it is not analyzed before expansion
4722 -- into assignments, to avoid side effects. We analyze, but do not
4723 -- resolve the copy, to obtain sufficient entity information for
4724 -- the checks that follow. If component is overloaded we assume
4725 -- an unsafe function call.
4727 if not Analyzed (Comp) then
4728 if Is_Overloaded (Expr) then
4731 elsif Nkind (Expr) = N_Aggregate
4732 and then not Is_Others_Aggregate (Expr)
4736 elsif Nkind (Expr) = N_Allocator then
4738 -- For now, too complex to analyze
4743 Comp := New_Copy_Tree (Expr);
4744 Set_Parent (Comp, Parent (Expr));
4748 if Nkind (Comp) = N_Aggregate then
4749 return Safe_Aggregate (Comp);
4751 return Check_Component (Comp);
4755 -- Start of processing for In_Place_Assign_OK
4758 if Present (Component_Associations (N)) then
4760 -- On assignment, sliding can take place, so we cannot do the
4761 -- assignment in place unless the bounds of the aggregate are
4762 -- statically equal to those of the target.
4764 -- If the aggregate is given by an others choice, the bounds are
4765 -- derived from the left-hand side, and the assignment is safe if
4766 -- the expression is.
4768 if Is_Others_Aggregate (N) then
4771 (Expression (First (Component_Associations (N))));
4774 Aggr_In := First_Index (Etype (N));
4776 if Nkind (Parent (N)) = N_Assignment_Statement then
4777 Obj_In := First_Index (Etype (Name (Parent (N))));
4780 -- Context is an allocator. Check bounds of aggregate against
4781 -- given type in qualified expression.
4783 pragma Assert (Nkind (Parent (Parent (N))) = N_Allocator);
4785 First_Index (Etype (Entity (Subtype_Mark (Parent (N)))));
4788 while Present (Aggr_In) loop
4789 Get_Index_Bounds (Aggr_In, Aggr_Lo, Aggr_Hi);
4790 Get_Index_Bounds (Obj_In, Obj_Lo, Obj_Hi);
4792 if not Compile_Time_Known_Value (Aggr_Lo)
4793 or else not Compile_Time_Known_Value (Aggr_Hi)
4794 or else not Compile_Time_Known_Value (Obj_Lo)
4795 or else not Compile_Time_Known_Value (Obj_Hi)
4796 or else Expr_Value (Aggr_Lo) /= Expr_Value (Obj_Lo)
4797 or else Expr_Value (Aggr_Hi) /= Expr_Value (Obj_Hi)
4802 Next_Index (Aggr_In);
4803 Next_Index (Obj_In);
4807 -- Now check the component values themselves
4809 return Safe_Aggregate (N);
4810 end In_Place_Assign_OK;
4816 procedure Others_Check (Sub_Aggr : Node_Id; Dim : Pos) is
4817 Aggr_Lo : constant Node_Id := Aggr_Low (Dim);
4818 Aggr_Hi : constant Node_Id := Aggr_High (Dim);
4819 -- The bounds of the aggregate for this dimension
4821 Ind_Typ : constant Entity_Id := Aggr_Index_Typ (Dim);
4822 -- The index type for this dimension
4824 Need_To_Check : Boolean := False;
4826 Choices_Lo : Node_Id := Empty;
4827 Choices_Hi : Node_Id := Empty;
4828 -- The lowest and highest discrete choices for a named sub-aggregate
4830 Nb_Choices : Int := -1;
4831 -- The number of discrete non-others choices in this sub-aggregate
4833 Nb_Elements : Uint := Uint_0;
4834 -- The number of elements in a positional aggregate
4836 Cond : Node_Id := Empty;
4843 -- Check if we have an others choice. If we do make sure that this
4844 -- sub-aggregate contains at least one element in addition to the
4847 if Range_Checks_Suppressed (Ind_Typ) then
4848 Need_To_Check := False;
4850 elsif Present (Expressions (Sub_Aggr))
4851 and then Present (Component_Associations (Sub_Aggr))
4853 Need_To_Check := True;
4855 elsif Present (Component_Associations (Sub_Aggr)) then
4856 Assoc := Last (Component_Associations (Sub_Aggr));
4858 if Nkind (First (Choices (Assoc))) /= N_Others_Choice then
4859 Need_To_Check := False;
4862 -- Count the number of discrete choices. Start with -1 because
4863 -- the others choice does not count.
4865 -- Is there some reason we do not use List_Length here ???
4868 Assoc := First (Component_Associations (Sub_Aggr));
4869 while Present (Assoc) loop
4870 Choice := First (Choices (Assoc));
4871 while Present (Choice) loop
4872 Nb_Choices := Nb_Choices + 1;
4879 -- If there is only an others choice nothing to do
4881 Need_To_Check := (Nb_Choices > 0);
4885 Need_To_Check := False;
4888 -- If we are dealing with a positional sub-aggregate with an others
4889 -- choice then compute the number or positional elements.
4891 if Need_To_Check and then Present (Expressions (Sub_Aggr)) then
4892 Expr := First (Expressions (Sub_Aggr));
4893 Nb_Elements := Uint_0;
4894 while Present (Expr) loop
4895 Nb_Elements := Nb_Elements + 1;
4899 -- If the aggregate contains discrete choices and an others choice
4900 -- compute the smallest and largest discrete choice values.
4902 elsif Need_To_Check then
4903 Compute_Choices_Lo_And_Choices_Hi : declare
4905 Table : Case_Table_Type (1 .. Nb_Choices);
4906 -- Used to sort all the different choice values
4913 Assoc := First (Component_Associations (Sub_Aggr));
4914 while Present (Assoc) loop
4915 Choice := First (Choices (Assoc));
4916 while Present (Choice) loop
4917 if Nkind (Choice) = N_Others_Choice then
4921 Get_Index_Bounds (Choice, Low, High);
4922 Table (J).Choice_Lo := Low;
4923 Table (J).Choice_Hi := High;
4932 -- Sort the discrete choices
4934 Sort_Case_Table (Table);
4936 Choices_Lo := Table (1).Choice_Lo;
4937 Choices_Hi := Table (Nb_Choices).Choice_Hi;
4938 end Compute_Choices_Lo_And_Choices_Hi;
4941 -- If no others choice in this sub-aggregate, or the aggregate
4942 -- comprises only an others choice, nothing to do.
4944 if not Need_To_Check then
4947 -- If we are dealing with an aggregate containing an others choice
4948 -- and positional components, we generate the following test:
4950 -- if Ind_Typ'Pos (Aggr_Lo) + (Nb_Elements - 1) >
4951 -- Ind_Typ'Pos (Aggr_Hi)
4953 -- raise Constraint_Error;
4956 elsif Nb_Elements > Uint_0 then
4962 Make_Attribute_Reference (Loc,
4963 Prefix => New_Occurrence_Of (Ind_Typ, Loc),
4964 Attribute_Name => Name_Pos,
4967 (Duplicate_Subexpr_Move_Checks (Aggr_Lo))),
4968 Right_Opnd => Make_Integer_Literal (Loc, Nb_Elements - 1)),
4971 Make_Attribute_Reference (Loc,
4972 Prefix => New_Occurrence_Of (Ind_Typ, Loc),
4973 Attribute_Name => Name_Pos,
4974 Expressions => New_List (
4975 Duplicate_Subexpr_Move_Checks (Aggr_Hi))));
4977 -- If we are dealing with an aggregate containing an others choice
4978 -- and discrete choices we generate the following test:
4980 -- [constraint_error when
4981 -- Choices_Lo < Aggr_Lo or else Choices_Hi > Aggr_Hi];
4988 Left_Opnd => Duplicate_Subexpr_Move_Checks (Choices_Lo),
4989 Right_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo)),
4993 Left_Opnd => Duplicate_Subexpr (Choices_Hi),
4994 Right_Opnd => Duplicate_Subexpr (Aggr_Hi)));
4997 if Present (Cond) then
4999 Make_Raise_Constraint_Error (Loc,
5001 Reason => CE_Length_Check_Failed));
5002 -- Questionable reason code, shouldn't that be a
5003 -- CE_Range_Check_Failed ???
5006 -- Now look inside the sub-aggregate to see if there is more work
5008 if Dim < Aggr_Dimension then
5010 -- Process positional components
5012 if Present (Expressions (Sub_Aggr)) then
5013 Expr := First (Expressions (Sub_Aggr));
5014 while Present (Expr) loop
5015 Others_Check (Expr, Dim + 1);
5020 -- Process component associations
5022 if Present (Component_Associations (Sub_Aggr)) then
5023 Assoc := First (Component_Associations (Sub_Aggr));
5024 while Present (Assoc) loop
5025 Expr := Expression (Assoc);
5026 Others_Check (Expr, Dim + 1);
5033 -------------------------
5034 -- Safe_Left_Hand_Side --
5035 -------------------------
5037 function Safe_Left_Hand_Side (N : Node_Id) return Boolean is
5038 function Is_Safe_Index (Indx : Node_Id) return Boolean;
5039 -- If the left-hand side includes an indexed component, check that
5040 -- the indexes are free of side-effect.
5046 function Is_Safe_Index (Indx : Node_Id) return Boolean is
5048 if Is_Entity_Name (Indx) then
5051 elsif Nkind (Indx) = N_Integer_Literal then
5054 elsif Nkind (Indx) = N_Function_Call
5055 and then Is_Entity_Name (Name (Indx))
5056 and then Has_Pragma_Pure_Function (Entity (Name (Indx)))
5060 elsif Nkind (Indx) = N_Type_Conversion
5061 and then Is_Safe_Index (Expression (Indx))
5070 -- Start of processing for Safe_Left_Hand_Side
5073 if Is_Entity_Name (N) then
5076 elsif Nkind_In (N, N_Explicit_Dereference, N_Selected_Component)
5077 and then Safe_Left_Hand_Side (Prefix (N))
5081 elsif Nkind (N) = N_Indexed_Component
5082 and then Safe_Left_Hand_Side (Prefix (N))
5083 and then Is_Safe_Index (First (Expressions (N)))
5087 elsif Nkind (N) = N_Unchecked_Type_Conversion then
5088 return Safe_Left_Hand_Side (Expression (N));
5093 end Safe_Left_Hand_Side;
5098 -- Holds the temporary aggregate value
5101 -- Holds the declaration of Tmp
5103 Aggr_Code : List_Id;
5104 Parent_Node : Node_Id;
5105 Parent_Kind : Node_Kind;
5107 -- Start of processing for Expand_Array_Aggregate
5110 -- Do not touch the special aggregates of attributes used for Asm calls
5112 if Is_RTE (Ctyp, RE_Asm_Input_Operand)
5113 or else Is_RTE (Ctyp, RE_Asm_Output_Operand)
5117 -- Do not expand an aggregate for an array type which contains tasks if
5118 -- the aggregate is associated with an unexpanded return statement of a
5119 -- build-in-place function. The aggregate is expanded when the related
5120 -- return statement (rewritten into an extended return) is processed.
5121 -- This delay ensures that any temporaries and initialization code
5122 -- generated for the aggregate appear in the proper return block and
5123 -- use the correct _chain and _master.
5125 elsif Has_Task (Base_Type (Etype (N)))
5126 and then Nkind (Parent (N)) = N_Simple_Return_Statement
5127 and then Is_Build_In_Place_Function
5128 (Return_Applies_To (Return_Statement_Entity (Parent (N))))
5132 -- Do not attempt expansion if error already detected. We may reach this
5133 -- point in spite of previous errors when compiling with -gnatq, to
5134 -- force all possible errors (this is the usual ACATS mode).
5136 elsif Error_Posted (N) then
5140 -- If the semantic analyzer has determined that aggregate N will raise
5141 -- Constraint_Error at run time, then the aggregate node has been
5142 -- replaced with an N_Raise_Constraint_Error node and we should
5145 pragma Assert (not Raises_Constraint_Error (N));
5149 -- Check that the index range defined by aggregate bounds is
5150 -- compatible with corresponding index subtype.
5152 Index_Compatibility_Check : declare
5153 Aggr_Index_Range : Node_Id := First_Index (Typ);
5154 -- The current aggregate index range
5156 Index_Constraint : Node_Id := First_Index (Etype (Typ));
5157 -- The corresponding index constraint against which we have to
5158 -- check the above aggregate index range.
5161 Compute_Others_Present (N, 1);
5163 for J in 1 .. Aggr_Dimension loop
5164 -- There is no need to emit a check if an others choice is present
5165 -- for this array aggregate dimension since in this case one of
5166 -- N's sub-aggregates has taken its bounds from the context and
5167 -- these bounds must have been checked already. In addition all
5168 -- sub-aggregates corresponding to the same dimension must all
5169 -- have the same bounds (checked in (c) below).
5171 if not Range_Checks_Suppressed (Etype (Index_Constraint))
5172 and then not Others_Present (J)
5174 -- We don't use Checks.Apply_Range_Check here because it emits
5175 -- a spurious check. Namely it checks that the range defined by
5176 -- the aggregate bounds is non empty. But we know this already
5179 Check_Bounds (Aggr_Index_Range, Index_Constraint);
5182 -- Save the low and high bounds of the aggregate index as well as
5183 -- the index type for later use in checks (b) and (c) below.
5185 Aggr_Low (J) := Low_Bound (Aggr_Index_Range);
5186 Aggr_High (J) := High_Bound (Aggr_Index_Range);
5188 Aggr_Index_Typ (J) := Etype (Index_Constraint);
5190 Next_Index (Aggr_Index_Range);
5191 Next_Index (Index_Constraint);
5193 end Index_Compatibility_Check;
5197 -- If an others choice is present check that no aggregate index is
5198 -- outside the bounds of the index constraint.
5200 Others_Check (N, 1);
5204 -- For multidimensional arrays make sure that all subaggregates
5205 -- corresponding to the same dimension have the same bounds.
5207 if Aggr_Dimension > 1 then
5208 Check_Same_Aggr_Bounds (N, 1);
5213 -- If we have a default component value, or simple initialization is
5214 -- required for the component type, then we replace <> in component
5215 -- associations by the required default value.
5218 Default_Val : Node_Id;
5222 if (Present (Default_Aspect_Component_Value (Typ))
5223 or else Needs_Simple_Initialization (Ctyp))
5224 and then Present (Component_Associations (N))
5226 Assoc := First (Component_Associations (N));
5227 while Present (Assoc) loop
5228 if Nkind (Assoc) = N_Component_Association
5229 and then Box_Present (Assoc)
5231 Set_Box_Present (Assoc, False);
5233 if Present (Default_Aspect_Component_Value (Typ)) then
5234 Default_Val := Default_Aspect_Component_Value (Typ);
5236 Default_Val := Get_Simple_Init_Val (Ctyp, N);
5239 Set_Expression (Assoc, New_Copy_Tree (Default_Val));
5240 Analyze_And_Resolve (Expression (Assoc), Ctyp);
5250 -- Here we test for is packed array aggregate that we can handle at
5251 -- compile time. If so, return with transformation done. Note that we do
5252 -- this even if the aggregate is nested, because once we have done this
5253 -- processing, there is no more nested aggregate.
5255 if Packed_Array_Aggregate_Handled (N) then
5259 -- At this point we try to convert to positional form
5261 if Ekind (Current_Scope) = E_Package
5262 and then Static_Elaboration_Desired (Current_Scope)
5264 Convert_To_Positional (N, Max_Others_Replicate => 100);
5266 Convert_To_Positional (N);
5269 -- if the result is no longer an aggregate (e.g. it may be a string
5270 -- literal, or a temporary which has the needed value), then we are
5271 -- done, since there is no longer a nested aggregate.
5273 if Nkind (N) /= N_Aggregate then
5276 -- We are also done if the result is an analyzed aggregate, indicating
5277 -- that Convert_To_Positional succeeded and reanalyzed the rewritten
5280 elsif Analyzed (N) and then N /= Original_Node (N) then
5284 -- If all aggregate components are compile-time known and the aggregate
5285 -- has been flattened, nothing left to do. The same occurs if the
5286 -- aggregate is used to initialize the components of a statically
5287 -- allocated dispatch table.
5289 if Compile_Time_Known_Aggregate (N)
5290 or else Is_Static_Dispatch_Table_Aggregate (N)
5292 Set_Expansion_Delayed (N, False);
5296 -- Now see if back end processing is possible
5298 if Backend_Processing_Possible (N) then
5300 -- If the aggregate is static but the constraints are not, build
5301 -- a static subtype for the aggregate, so that Gigi can place it
5302 -- in static memory. Perform an unchecked_conversion to the non-
5303 -- static type imposed by the context.
5306 Itype : constant Entity_Id := Etype (N);
5308 Needs_Type : Boolean := False;
5311 Index := First_Index (Itype);
5312 while Present (Index) loop
5313 if not Is_OK_Static_Subtype (Etype (Index)) then
5322 Build_Constrained_Type (Positional => True);
5323 Rewrite (N, Unchecked_Convert_To (Itype, N));
5333 -- Delay expansion for nested aggregates: it will be taken care of
5334 -- when the parent aggregate is expanded.
5336 Parent_Node := Parent (N);
5337 Parent_Kind := Nkind (Parent_Node);
5339 if Parent_Kind = N_Qualified_Expression then
5340 Parent_Node := Parent (Parent_Node);
5341 Parent_Kind := Nkind (Parent_Node);
5344 if Parent_Kind = N_Aggregate
5345 or else Parent_Kind = N_Extension_Aggregate
5346 or else Parent_Kind = N_Component_Association
5347 or else (Parent_Kind = N_Object_Declaration
5348 and then Needs_Finalization (Typ))
5349 or else (Parent_Kind = N_Assignment_Statement
5350 and then Inside_Init_Proc)
5352 if Static_Array_Aggregate (N)
5353 or else Compile_Time_Known_Aggregate (N)
5355 Set_Expansion_Delayed (N, False);
5358 Set_Expansion_Delayed (N);
5365 -- Look if in place aggregate expansion is possible
5367 -- For object declarations we build the aggregate in place, unless
5368 -- the array is bit-packed or the component is controlled.
5370 -- For assignments we do the assignment in place if all the component
5371 -- associations have compile-time known values. For other cases we
5372 -- create a temporary. The analysis for safety of on-line assignment
5373 -- is delicate, i.e. we don't know how to do it fully yet ???
5375 -- For allocators we assign to the designated object in place if the
5376 -- aggregate meets the same conditions as other in-place assignments.
5377 -- In this case the aggregate may not come from source but was created
5378 -- for default initialization, e.g. with Initialize_Scalars.
5380 if Requires_Transient_Scope (Typ) then
5381 Establish_Transient_Scope
5382 (N, Sec_Stack => Has_Controlled_Component (Typ));
5385 if Has_Default_Init_Comps (N) then
5386 Maybe_In_Place_OK := False;
5388 elsif Is_Bit_Packed_Array (Typ)
5389 or else Has_Controlled_Component (Typ)
5391 Maybe_In_Place_OK := False;
5394 Maybe_In_Place_OK :=
5395 (Nkind (Parent (N)) = N_Assignment_Statement
5396 and then In_Place_Assign_OK)
5399 (Nkind (Parent (Parent (N))) = N_Allocator
5400 and then In_Place_Assign_OK);
5403 -- If this is an array of tasks, it will be expanded into build-in-place
5404 -- assignments. Build an activation chain for the tasks now.
5406 if Has_Task (Etype (N)) then
5407 Build_Activation_Chain_Entity (N);
5410 -- Perform in-place expansion of aggregate in an object declaration.
5411 -- Note: actions generated for the aggregate will be captured in an
5412 -- expression-with-actions statement so that they can be transferred
5413 -- to freeze actions later if there is an address clause for the
5414 -- object. (Note: we don't use a block statement because this would
5415 -- cause generated freeze nodes to be elaborated in the wrong scope).
5417 -- Should document these individual tests ???
5419 if not Has_Default_Init_Comps (N)
5420 and then Comes_From_Source (Parent_Node)
5421 and then Parent_Kind = N_Object_Declaration
5423 Must_Slide (Etype (Defining_Identifier (Parent_Node)), Typ)
5424 and then N = Expression (Parent_Node)
5425 and then not Is_Bit_Packed_Array (Typ)
5426 and then not Has_Controlled_Component (Typ)
5428 In_Place_Assign_OK_For_Declaration := True;
5429 Tmp := Defining_Identifier (Parent (N));
5430 Set_No_Initialization (Parent (N));
5431 Set_Expression (Parent (N), Empty);
5433 -- Set kind and type of the entity, for use in the analysis
5434 -- of the subsequent assignments. If the nominal type is not
5435 -- constrained, build a subtype from the known bounds of the
5436 -- aggregate. If the declaration has a subtype mark, use it,
5437 -- otherwise use the itype of the aggregate.
5439 Set_Ekind (Tmp, E_Variable);
5441 if not Is_Constrained (Typ) then
5442 Build_Constrained_Type (Positional => False);
5444 elsif Is_Entity_Name (Object_Definition (Parent (N)))
5445 and then Is_Constrained (Entity (Object_Definition (Parent (N))))
5447 Set_Etype (Tmp, Entity (Object_Definition (Parent (N))));
5450 Set_Size_Known_At_Compile_Time (Typ, False);
5451 Set_Etype (Tmp, Typ);
5454 elsif Maybe_In_Place_OK
5455 and then Nkind (Parent (N)) = N_Qualified_Expression
5456 and then Nkind (Parent (Parent (N))) = N_Allocator
5458 Set_Expansion_Delayed (N);
5461 -- In the remaining cases the aggregate is the RHS of an assignment
5463 elsif Maybe_In_Place_OK
5464 and then Safe_Left_Hand_Side (Name (Parent (N)))
5466 Tmp := Name (Parent (N));
5468 if Etype (Tmp) /= Etype (N) then
5469 Apply_Length_Check (N, Etype (Tmp));
5471 if Nkind (N) = N_Raise_Constraint_Error then
5473 -- Static error, nothing further to expand
5479 -- If a slice assignment has an aggregate with a single others_choice,
5480 -- the assignment can be done in place even if bounds are not static,
5481 -- by converting it into a loop over the discrete range of the slice.
5483 elsif Maybe_In_Place_OK
5484 and then Nkind (Name (Parent (N))) = N_Slice
5485 and then Is_Others_Aggregate (N)
5487 Tmp := Name (Parent (N));
5489 -- Set type of aggregate to be type of lhs in assignment, in order
5490 -- to suppress redundant length checks.
5492 Set_Etype (N, Etype (Tmp));
5496 -- In place aggregate expansion is not possible
5499 Maybe_In_Place_OK := False;
5500 Tmp := Make_Temporary (Loc, 'A', N);
5502 Make_Object_Declaration (Loc,
5503 Defining_Identifier => Tmp,
5504 Object_Definition => New_Occurrence_Of (Typ, Loc));
5505 Set_No_Initialization (Tmp_Decl, True);
5507 -- If we are within a loop, the temporary will be pushed on the
5508 -- stack at each iteration. If the aggregate is the expression for an
5509 -- allocator, it will be immediately copied to the heap and can
5510 -- be reclaimed at once. We create a transient scope around the
5511 -- aggregate for this purpose.
5513 if Ekind (Current_Scope) = E_Loop
5514 and then Nkind (Parent (Parent (N))) = N_Allocator
5516 Establish_Transient_Scope (N, False);
5519 Insert_Action (N, Tmp_Decl);
5522 -- Construct and insert the aggregate code. We can safely suppress index
5523 -- checks because this code is guaranteed not to raise CE on index
5524 -- checks. However we should *not* suppress all checks.
5530 if Nkind (Tmp) = N_Defining_Identifier then
5531 Target := New_Occurrence_Of (Tmp, Loc);
5534 if Has_Default_Init_Comps (N) then
5536 -- Ada 2005 (AI-287): This case has not been analyzed???
5538 raise Program_Error;
5541 -- Name in assignment is explicit dereference
5543 Target := New_Copy (Tmp);
5546 -- If we are to generate an in place assignment for a declaration or
5547 -- an assignment statement, and the assignment can be done directly
5548 -- by the back end, then do not expand further.
5550 -- ??? We can also do that if in place expansion is not possible but
5551 -- then we could go into an infinite recursion.
5553 if (In_Place_Assign_OK_For_Declaration or else Maybe_In_Place_OK)
5554 and then not AAMP_On_Target
5555 and then not CodePeer_Mode
5556 and then not Generate_C_Code
5557 and then not Possible_Bit_Aligned_Component (Target)
5558 and then not Is_Possibly_Unaligned_Slice (Target)
5559 and then Aggr_Assignment_OK_For_Backend (N)
5561 if Maybe_In_Place_OK then
5567 Make_Assignment_Statement (Loc,
5569 Expression => New_Copy (N)));
5573 Build_Array_Aggr_Code (N,
5575 Index => First_Index (Typ),
5577 Scalar_Comp => Is_Scalar_Type (Ctyp));
5580 -- Save the last assignment statement associated with the aggregate
5581 -- when building a controlled object. This reference is utilized by
5582 -- the finalization machinery when marking an object as successfully
5585 if Needs_Finalization (Typ)
5586 and then Is_Entity_Name (Target)
5587 and then Present (Entity (Target))
5588 and then Ekind_In (Entity (Target), E_Constant, E_Variable)
5590 Set_Last_Aggregate_Assignment (Entity (Target), Last (Aggr_Code));
5594 -- If the aggregate is the expression in a declaration, the expanded
5595 -- code must be inserted after it. The defining entity might not come
5596 -- from source if this is part of an inlined body, but the declaration
5599 if Comes_From_Source (Tmp)
5601 (Nkind (Parent (N)) = N_Object_Declaration
5602 and then Comes_From_Source (Parent (N))
5603 and then Tmp = Defining_Entity (Parent (N)))
5606 Node_After : constant Node_Id := Next (Parent_Node);
5609 Insert_Actions_After (Parent_Node, Aggr_Code);
5611 if Parent_Kind = N_Object_Declaration then
5612 Collect_Initialization_Statements
5613 (Obj => Tmp, N => Parent_Node, Node_After => Node_After);
5618 Insert_Actions (N, Aggr_Code);
5621 -- If the aggregate has been assigned in place, remove the original
5624 if Nkind (Parent (N)) = N_Assignment_Statement
5625 and then Maybe_In_Place_OK
5627 Rewrite (Parent (N), Make_Null_Statement (Loc));
5629 elsif Nkind (Parent (N)) /= N_Object_Declaration
5630 or else Tmp /= Defining_Identifier (Parent (N))
5632 Rewrite (N, New_Occurrence_Of (Tmp, Loc));
5633 Analyze_And_Resolve (N, Typ);
5635 end Expand_Array_Aggregate;
5637 ------------------------
5638 -- Expand_N_Aggregate --
5639 ------------------------
5641 procedure Expand_N_Aggregate (N : Node_Id) is
5643 -- Record aggregate case
5645 if Is_Record_Type (Etype (N)) then
5646 Expand_Record_Aggregate (N);
5648 -- Array aggregate case
5651 -- A special case, if we have a string subtype with bounds 1 .. N,
5652 -- where N is known at compile time, and the aggregate is of the
5653 -- form (others => 'x'), with a single choice and no expressions,
5654 -- and N is less than 80 (an arbitrary limit for now), then replace
5655 -- the aggregate by the equivalent string literal (but do not mark
5656 -- it as static since it is not).
5658 -- Note: this entire circuit is redundant with respect to code in
5659 -- Expand_Array_Aggregate that collapses others choices to positional
5660 -- form, but there are two problems with that circuit:
5662 -- a) It is limited to very small cases due to ill-understood
5663 -- interactions with bootstrapping. That limit is removed by
5664 -- use of the No_Implicit_Loops restriction.
5666 -- b) It incorrectly ends up with the resulting expressions being
5667 -- considered static when they are not. For example, the
5668 -- following test should fail:
5670 -- pragma Restrictions (No_Implicit_Loops);
5671 -- package NonSOthers4 is
5672 -- B : constant String (1 .. 6) := (others => 'A');
5673 -- DH : constant String (1 .. 8) := B & "BB";
5675 -- pragma Export (C, X, Link_Name => DH);
5678 -- But it succeeds (DH looks static to pragma Export)
5680 -- To be sorted out ???
5682 if Present (Component_Associations (N)) then
5684 CA : constant Node_Id := First (Component_Associations (N));
5685 MX : constant := 80;
5688 if Nkind (First (Choices (CA))) = N_Others_Choice
5689 and then Nkind (Expression (CA)) = N_Character_Literal
5690 and then No (Expressions (N))
5693 T : constant Entity_Id := Etype (N);
5694 X : constant Node_Id := First_Index (T);
5695 EC : constant Node_Id := Expression (CA);
5696 CV : constant Uint := Char_Literal_Value (EC);
5697 CC : constant Int := UI_To_Int (CV);
5700 if Nkind (X) = N_Range
5701 and then Compile_Time_Known_Value (Low_Bound (X))
5702 and then Expr_Value (Low_Bound (X)) = 1
5703 and then Compile_Time_Known_Value (High_Bound (X))
5706 Hi : constant Uint := Expr_Value (High_Bound (X));
5712 for J in 1 .. UI_To_Int (Hi) loop
5713 Store_String_Char (Char_Code (CC));
5717 Make_String_Literal (Sloc (N),
5718 Strval => End_String));
5720 if CC >= Int (2 ** 16) then
5721 Set_Has_Wide_Wide_Character (N);
5722 elsif CC >= Int (2 ** 8) then
5723 Set_Has_Wide_Character (N);
5726 Analyze_And_Resolve (N, T);
5727 Set_Is_Static_Expression (N, False);
5737 -- Not that special case, so normal expansion of array aggregate
5739 Expand_Array_Aggregate (N);
5743 when RE_Not_Available =>
5745 end Expand_N_Aggregate;
5747 ----------------------------------
5748 -- Expand_N_Extension_Aggregate --
5749 ----------------------------------
5751 -- If the ancestor part is an expression, add a component association for
5752 -- the parent field. If the type of the ancestor part is not the direct
5753 -- parent of the expected type, build recursively the needed ancestors.
5754 -- If the ancestor part is a subtype_mark, replace aggregate with a decla-
5755 -- ration for a temporary of the expected type, followed by individual
5756 -- assignments to the given components.
5758 procedure Expand_N_Extension_Aggregate (N : Node_Id) is
5759 Loc : constant Source_Ptr := Sloc (N);
5760 A : constant Node_Id := Ancestor_Part (N);
5761 Typ : constant Entity_Id := Etype (N);
5764 -- If the ancestor is a subtype mark, an init proc must be called
5765 -- on the resulting object which thus has to be materialized in
5768 if Is_Entity_Name (A) and then Is_Type (Entity (A)) then
5769 Convert_To_Assignments (N, Typ);
5771 -- The extension aggregate is transformed into a record aggregate
5772 -- of the following form (c1 and c2 are inherited components)
5774 -- (Exp with c3 => a, c4 => b)
5775 -- ==> (c1 => Exp.c1, c2 => Exp.c2, c3 => a, c4 => b)
5780 if Tagged_Type_Expansion then
5781 Expand_Record_Aggregate (N,
5784 (Node (First_Elmt (Access_Disp_Table (Typ))), Loc),
5787 -- No tag is needed in the case of a VM
5790 Expand_Record_Aggregate (N, Parent_Expr => A);
5795 when RE_Not_Available =>
5797 end Expand_N_Extension_Aggregate;
5799 -----------------------------
5800 -- Expand_Record_Aggregate --
5801 -----------------------------
5803 procedure Expand_Record_Aggregate
5805 Orig_Tag : Node_Id := Empty;
5806 Parent_Expr : Node_Id := Empty)
5808 Loc : constant Source_Ptr := Sloc (N);
5809 Comps : constant List_Id := Component_Associations (N);
5810 Typ : constant Entity_Id := Etype (N);
5811 Base_Typ : constant Entity_Id := Base_Type (Typ);
5813 Static_Components : Boolean := True;
5814 -- Flag to indicate whether all components are compile-time known,
5815 -- and the aggregate can be constructed statically and handled by
5818 function Compile_Time_Known_Composite_Value (N : Node_Id) return Boolean;
5819 -- Returns true if N is an expression of composite type which can be
5820 -- fully evaluated at compile time without raising constraint error.
5821 -- Such expressions can be passed as is to Gigi without any expansion.
5823 -- This returns true for N_Aggregate with Compile_Time_Known_Aggregate
5824 -- set and constants whose expression is such an aggregate, recursively.
5826 function Component_Not_OK_For_Backend return Boolean;
5827 -- Check for presence of a component which makes it impossible for the
5828 -- backend to process the aggregate, thus requiring the use of a series
5829 -- of assignment statements. Cases checked for are a nested aggregate
5830 -- needing Late_Expansion, the presence of a tagged component which may
5831 -- need tag adjustment, and a bit unaligned component reference.
5833 -- We also force expansion into assignments if a component is of a
5834 -- mutable type (including a private type with discriminants) because
5835 -- in that case the size of the component to be copied may be smaller
5836 -- than the side of the target, and there is no simple way for gigi
5837 -- to compute the size of the object to be copied.
5839 -- NOTE: This is part of the ongoing work to define precisely the
5840 -- interface between front-end and back-end handling of aggregates.
5841 -- In general it is desirable to pass aggregates as they are to gigi,
5842 -- in order to minimize elaboration code. This is one case where the
5843 -- semantics of Ada complicate the analysis and lead to anomalies in
5844 -- the gcc back-end if the aggregate is not expanded into assignments.
5846 function Has_Visible_Private_Ancestor (Id : E) return Boolean;
5847 -- If any ancestor of the current type is private, the aggregate
5848 -- cannot be built in place. We cannot rely on Has_Private_Ancestor,
5849 -- because it will not be set when type and its parent are in the
5850 -- same scope, and the parent component needs expansion.
5852 function Top_Level_Aggregate (N : Node_Id) return Node_Id;
5853 -- For nested aggregates return the ultimate enclosing aggregate; for
5854 -- non-nested aggregates return N.
5856 ----------------------------------------
5857 -- Compile_Time_Known_Composite_Value --
5858 ----------------------------------------
5860 function Compile_Time_Known_Composite_Value
5861 (N : Node_Id) return Boolean
5864 -- If we have an entity name, then see if it is the name of a
5865 -- constant and if so, test the corresponding constant value.
5867 if Is_Entity_Name (N) then
5869 E : constant Entity_Id := Entity (N);
5872 if Ekind (E) /= E_Constant then
5875 V := Constant_Value (E);
5877 and then Compile_Time_Known_Composite_Value (V);
5881 -- We have a value, see if it is compile time known
5884 if Nkind (N) = N_Aggregate then
5885 return Compile_Time_Known_Aggregate (N);
5888 -- All other types of values are not known at compile time
5893 end Compile_Time_Known_Composite_Value;
5895 ----------------------------------
5896 -- Component_Not_OK_For_Backend --
5897 ----------------------------------
5899 function Component_Not_OK_For_Backend return Boolean is
5909 while Present (C) loop
5911 -- If the component has box initialization, expansion is needed
5912 -- and component is not ready for backend.
5914 if Box_Present (C) then
5918 if Nkind (Expression (C)) = N_Qualified_Expression then
5919 Expr_Q := Expression (Expression (C));
5921 Expr_Q := Expression (C);
5924 -- Return true if the aggregate has any associations for tagged
5925 -- components that may require tag adjustment.
5927 -- These are cases where the source expression may have a tag that
5928 -- could differ from the component tag (e.g., can occur for type
5929 -- conversions and formal parameters). (Tag adjustment not needed
5930 -- if Tagged_Type_Expansion because object tags are implicit in
5933 if Is_Tagged_Type (Etype (Expr_Q))
5934 and then (Nkind (Expr_Q) = N_Type_Conversion
5935 or else (Is_Entity_Name (Expr_Q)
5937 Ekind (Entity (Expr_Q)) in Formal_Kind))
5938 and then Tagged_Type_Expansion
5940 Static_Components := False;
5943 elsif Is_Delayed_Aggregate (Expr_Q) then
5944 Static_Components := False;
5947 elsif Possible_Bit_Aligned_Component (Expr_Q) then
5948 Static_Components := False;
5952 if Is_Elementary_Type (Etype (Expr_Q)) then
5953 if not Compile_Time_Known_Value (Expr_Q) then
5954 Static_Components := False;
5957 elsif not Compile_Time_Known_Composite_Value (Expr_Q) then
5958 Static_Components := False;
5960 if Is_Private_Type (Etype (Expr_Q))
5961 and then Has_Discriminants (Etype (Expr_Q))
5971 end Component_Not_OK_For_Backend;
5973 -----------------------------------
5974 -- Has_Visible_Private_Ancestor --
5975 -----------------------------------
5977 function Has_Visible_Private_Ancestor (Id : E) return Boolean is
5978 R : constant Entity_Id := Root_Type (Id);
5979 T1 : Entity_Id := Id;
5983 if Is_Private_Type (T1) then
5993 end Has_Visible_Private_Ancestor;
5995 -------------------------
5996 -- Top_Level_Aggregate --
5997 -------------------------
5999 function Top_Level_Aggregate (N : Node_Id) return Node_Id is
6004 while Present (Parent (Aggr))
6005 and then Nkind_In (Parent (Aggr), N_Component_Association,
6008 Aggr := Parent (Aggr);
6012 end Top_Level_Aggregate;
6016 Top_Level_Aggr : constant Node_Id := Top_Level_Aggregate (N);
6017 Tag_Value : Node_Id;
6021 -- Start of processing for Expand_Record_Aggregate
6024 -- If the aggregate is to be assigned to an atomic/VFA variable, we have
6025 -- to prevent a piecemeal assignment even if the aggregate is to be
6026 -- expanded. We create a temporary for the aggregate, and assign the
6027 -- temporary instead, so that the back end can generate an atomic move
6030 if Is_Atomic_VFA_Aggregate (N) then
6033 -- No special management required for aggregates used to initialize
6034 -- statically allocated dispatch tables
6036 elsif Is_Static_Dispatch_Table_Aggregate (N) then
6040 -- Ada 2005 (AI-318-2): We need to convert to assignments if components
6041 -- are build-in-place function calls. The assignments will each turn
6042 -- into a build-in-place function call. If components are all static,
6043 -- we can pass the aggregate to the backend regardless of limitedness.
6045 -- Extension aggregates, aggregates in extended return statements, and
6046 -- aggregates for C++ imported types must be expanded.
6048 if Ada_Version >= Ada_2005 and then Is_Limited_View (Typ) then
6049 if not Nkind_In (Parent (N), N_Object_Declaration,
6050 N_Component_Association)
6052 Convert_To_Assignments (N, Typ);
6054 elsif Nkind (N) = N_Extension_Aggregate
6055 or else Convention (Typ) = Convention_CPP
6057 Convert_To_Assignments (N, Typ);
6059 elsif not Size_Known_At_Compile_Time (Typ)
6060 or else Component_Not_OK_For_Backend
6061 or else not Static_Components
6063 Convert_To_Assignments (N, Typ);
6066 Set_Compile_Time_Known_Aggregate (N);
6067 Set_Expansion_Delayed (N, False);
6070 -- Gigi doesn't properly handle temporaries of variable size so we
6071 -- generate it in the front-end
6073 elsif not Size_Known_At_Compile_Time (Typ)
6074 and then Tagged_Type_Expansion
6076 Convert_To_Assignments (N, Typ);
6078 -- An aggregate used to initialize a controlled object must be turned
6079 -- into component assignments as the components themselves may require
6080 -- finalization actions such as adjustment.
6082 elsif Needs_Finalization (Typ) then
6083 Convert_To_Assignments (N, Typ);
6085 -- Ada 2005 (AI-287): In case of default initialized components we
6086 -- convert the aggregate into assignments.
6088 elsif Has_Default_Init_Comps (N) then
6089 Convert_To_Assignments (N, Typ);
6093 elsif Component_Not_OK_For_Backend then
6094 Convert_To_Assignments (N, Typ);
6096 -- If an ancestor is private, some components are not inherited and we
6097 -- cannot expand into a record aggregate.
6099 elsif Has_Visible_Private_Ancestor (Typ) then
6100 Convert_To_Assignments (N, Typ);
6102 -- ??? The following was done to compile fxacc00.ads in the ACVCs. Gigi
6103 -- is not able to handle the aggregate for Late_Request.
6105 elsif Is_Tagged_Type (Typ) and then Has_Discriminants (Typ) then
6106 Convert_To_Assignments (N, Typ);
6108 -- If the tagged types covers interface types we need to initialize all
6109 -- hidden components containing pointers to secondary dispatch tables.
6111 elsif Is_Tagged_Type (Typ) and then Has_Interfaces (Typ) then
6112 Convert_To_Assignments (N, Typ);
6114 -- If some components are mutable, the size of the aggregate component
6115 -- may be distinct from the default size of the type component, so
6116 -- we need to expand to insure that the back-end copies the proper
6117 -- size of the data. However, if the aggregate is the initial value of
6118 -- a constant, the target is immutable and might be built statically
6119 -- if components are appropriate.
6121 elsif Has_Mutable_Components (Typ)
6123 (Nkind (Parent (Top_Level_Aggr)) /= N_Object_Declaration
6124 or else not Constant_Present (Parent (Top_Level_Aggr))
6125 or else not Static_Components)
6127 Convert_To_Assignments (N, Typ);
6129 -- If the type involved has bit aligned components, then we are not sure
6130 -- that the back end can handle this case correctly.
6132 elsif Type_May_Have_Bit_Aligned_Components (Typ) then
6133 Convert_To_Assignments (N, Typ);
6135 -- When generating C, only generate an aggregate when declaring objects
6136 -- since C does not support aggregates in e.g. assignment statements.
6138 elsif Modify_Tree_For_C and then not In_Object_Declaration (N) then
6139 Convert_To_Assignments (N, Typ);
6141 -- In all other cases, build a proper aggregate to be handled by gigi
6144 if Nkind (N) = N_Aggregate then
6146 -- If the aggregate is static and can be handled by the back-end,
6147 -- nothing left to do.
6149 if Static_Components then
6150 Set_Compile_Time_Known_Aggregate (N);
6151 Set_Expansion_Delayed (N, False);
6155 -- If no discriminants, nothing special to do
6157 if not Has_Discriminants (Typ) then
6160 -- Case of discriminants present
6162 elsif Is_Derived_Type (Typ) then
6164 -- For untagged types, non-stored discriminants are replaced
6165 -- with stored discriminants, which are the ones that gigi uses
6166 -- to describe the type and its components.
6168 Generate_Aggregate_For_Derived_Type : declare
6169 Constraints : constant List_Id := New_List;
6170 First_Comp : Node_Id;
6171 Discriminant : Entity_Id;
6173 Num_Disc : Nat := 0;
6174 Num_Gird : Nat := 0;
6176 procedure Prepend_Stored_Values (T : Entity_Id);
6177 -- Scan the list of stored discriminants of the type, and add
6178 -- their values to the aggregate being built.
6180 ---------------------------
6181 -- Prepend_Stored_Values --
6182 ---------------------------
6184 procedure Prepend_Stored_Values (T : Entity_Id) is
6186 Discriminant := First_Stored_Discriminant (T);
6187 while Present (Discriminant) loop
6189 Make_Component_Association (Loc,
6191 New_List (New_Occurrence_Of (Discriminant, Loc)),
6195 (Get_Discriminant_Value
6198 Discriminant_Constraint (Typ))));
6200 if No (First_Comp) then
6201 Prepend_To (Component_Associations (N), New_Comp);
6203 Insert_After (First_Comp, New_Comp);
6206 First_Comp := New_Comp;
6207 Next_Stored_Discriminant (Discriminant);
6209 end Prepend_Stored_Values;
6211 -- Start of processing for Generate_Aggregate_For_Derived_Type
6214 -- Remove the associations for the discriminant of derived type
6216 First_Comp := First (Component_Associations (N));
6217 while Present (First_Comp) loop
6221 if Ekind (Entity (First (Choices (Comp)))) = E_Discriminant
6224 Num_Disc := Num_Disc + 1;
6228 -- Insert stored discriminant associations in the correct
6229 -- order. If there are more stored discriminants than new
6230 -- discriminants, there is at least one new discriminant that
6231 -- constrains more than one of the stored discriminants. In
6232 -- this case we need to construct a proper subtype of the
6233 -- parent type, in order to supply values to all the
6234 -- components. Otherwise there is one-one correspondence
6235 -- between the constraints and the stored discriminants.
6237 First_Comp := Empty;
6239 Discriminant := First_Stored_Discriminant (Base_Type (Typ));
6240 while Present (Discriminant) loop
6241 Num_Gird := Num_Gird + 1;
6242 Next_Stored_Discriminant (Discriminant);
6245 -- Case of more stored discriminants than new discriminants
6247 if Num_Gird > Num_Disc then
6249 -- Create a proper subtype of the parent type, which is the
6250 -- proper implementation type for the aggregate, and convert
6251 -- it to the intended target type.
6253 Discriminant := First_Stored_Discriminant (Base_Type (Typ));
6254 while Present (Discriminant) loop
6257 (Get_Discriminant_Value
6260 Discriminant_Constraint (Typ)));
6261 Append (New_Comp, Constraints);
6262 Next_Stored_Discriminant (Discriminant);
6266 Make_Subtype_Declaration (Loc,
6267 Defining_Identifier => Make_Temporary (Loc, 'T'),
6268 Subtype_Indication =>
6269 Make_Subtype_Indication (Loc,
6271 New_Occurrence_Of (Etype (Base_Type (Typ)), Loc),
6273 Make_Index_Or_Discriminant_Constraint
6274 (Loc, Constraints)));
6276 Insert_Action (N, Decl);
6277 Prepend_Stored_Values (Base_Type (Typ));
6279 Set_Etype (N, Defining_Identifier (Decl));
6282 Rewrite (N, Unchecked_Convert_To (Typ, N));
6285 -- Case where we do not have fewer new discriminants than
6286 -- stored discriminants, so in this case we can simply use the
6287 -- stored discriminants of the subtype.
6290 Prepend_Stored_Values (Typ);
6292 end Generate_Aggregate_For_Derived_Type;
6295 if Is_Tagged_Type (Typ) then
6297 -- In the tagged case, _parent and _tag component must be created
6299 -- Reset Null_Present unconditionally. Tagged records always have
6300 -- at least one field (the tag or the parent).
6302 Set_Null_Record_Present (N, False);
6304 -- When the current aggregate comes from the expansion of an
6305 -- extension aggregate, the parent expr is replaced by an
6306 -- aggregate formed by selected components of this expr.
6308 if Present (Parent_Expr) and then Is_Empty_List (Comps) then
6309 Comp := First_Component_Or_Discriminant (Typ);
6310 while Present (Comp) loop
6312 -- Skip all expander-generated components
6314 if not Comes_From_Source (Original_Record_Component (Comp))
6320 Make_Selected_Component (Loc,
6322 Unchecked_Convert_To (Typ,
6323 Duplicate_Subexpr (Parent_Expr, True)),
6324 Selector_Name => New_Occurrence_Of (Comp, Loc));
6327 Make_Component_Association (Loc,
6329 New_List (New_Occurrence_Of (Comp, Loc)),
6330 Expression => New_Comp));
6332 Analyze_And_Resolve (New_Comp, Etype (Comp));
6335 Next_Component_Or_Discriminant (Comp);
6339 -- Compute the value for the Tag now, if the type is a root it
6340 -- will be included in the aggregate right away, otherwise it will
6341 -- be propagated to the parent aggregate.
6343 if Present (Orig_Tag) then
6344 Tag_Value := Orig_Tag;
6345 elsif not Tagged_Type_Expansion then
6350 (Node (First_Elmt (Access_Disp_Table (Typ))), Loc);
6353 -- For a derived type, an aggregate for the parent is formed with
6354 -- all the inherited components.
6356 if Is_Derived_Type (Typ) then
6359 First_Comp : Node_Id;
6360 Parent_Comps : List_Id;
6361 Parent_Aggr : Node_Id;
6362 Parent_Name : Node_Id;
6365 -- Remove the inherited component association from the
6366 -- aggregate and store them in the parent aggregate
6368 First_Comp := First (Component_Associations (N));
6369 Parent_Comps := New_List;
6370 while Present (First_Comp)
6372 Scope (Original_Record_Component
6373 (Entity (First (Choices (First_Comp))))) /=
6379 Append (Comp, Parent_Comps);
6383 Make_Aggregate (Loc,
6384 Component_Associations => Parent_Comps);
6385 Set_Etype (Parent_Aggr, Etype (Base_Type (Typ)));
6387 -- Find the _parent component
6389 Comp := First_Component (Typ);
6390 while Chars (Comp) /= Name_uParent loop
6391 Comp := Next_Component (Comp);
6394 Parent_Name := New_Occurrence_Of (Comp, Loc);
6396 -- Insert the parent aggregate
6398 Prepend_To (Component_Associations (N),
6399 Make_Component_Association (Loc,
6400 Choices => New_List (Parent_Name),
6401 Expression => Parent_Aggr));
6403 -- Expand recursively the parent propagating the right Tag
6405 Expand_Record_Aggregate
6406 (Parent_Aggr, Tag_Value, Parent_Expr);
6408 -- The ancestor part may be a nested aggregate that has
6409 -- delayed expansion: recheck now.
6411 if Component_Not_OK_For_Backend then
6412 Convert_To_Assignments (N, Typ);
6416 -- For a root type, the tag component is added (unless compiling
6417 -- for the VMs, where tags are implicit).
6419 elsif Tagged_Type_Expansion then
6421 Tag_Name : constant Node_Id :=
6422 New_Occurrence_Of (First_Tag_Component (Typ), Loc);
6423 Typ_Tag : constant Entity_Id := RTE (RE_Tag);
6424 Conv_Node : constant Node_Id :=
6425 Unchecked_Convert_To (Typ_Tag, Tag_Value);
6428 Set_Etype (Conv_Node, Typ_Tag);
6429 Prepend_To (Component_Associations (N),
6430 Make_Component_Association (Loc,
6431 Choices => New_List (Tag_Name),
6432 Expression => Conv_Node));
6438 end Expand_Record_Aggregate;
6440 ----------------------------
6441 -- Has_Default_Init_Comps --
6442 ----------------------------
6444 function Has_Default_Init_Comps (N : Node_Id) return Boolean is
6445 Comps : constant List_Id := Component_Associations (N);
6450 pragma Assert (Nkind_In (N, N_Aggregate, N_Extension_Aggregate));
6456 if Has_Self_Reference (N) then
6460 -- Check if any direct component has default initialized components
6463 while Present (C) loop
6464 if Box_Present (C) then
6471 -- Recursive call in case of aggregate expression
6474 while Present (C) loop
6475 Expr := Expression (C);
6478 and then Nkind_In (Expr, N_Aggregate, N_Extension_Aggregate)
6479 and then Has_Default_Init_Comps (Expr)
6488 end Has_Default_Init_Comps;
6490 --------------------------
6491 -- Is_Delayed_Aggregate --
6492 --------------------------
6494 function Is_Delayed_Aggregate (N : Node_Id) return Boolean is
6495 Node : Node_Id := N;
6496 Kind : Node_Kind := Nkind (Node);
6499 if Kind = N_Qualified_Expression then
6500 Node := Expression (Node);
6501 Kind := Nkind (Node);
6504 if not Nkind_In (Kind, N_Aggregate, N_Extension_Aggregate) then
6507 return Expansion_Delayed (Node);
6509 end Is_Delayed_Aggregate;
6511 ---------------------------
6512 -- In_Object_Declaration --
6513 ---------------------------
6515 function In_Object_Declaration (N : Node_Id) return Boolean is
6516 P : Node_Id := Parent (N);
6518 while Present (P) loop
6519 if Nkind (P) = N_Object_Declaration then
6527 end In_Object_Declaration;
6529 ----------------------------------------
6530 -- Is_Static_Dispatch_Table_Aggregate --
6531 ----------------------------------------
6533 function Is_Static_Dispatch_Table_Aggregate (N : Node_Id) return Boolean is
6534 Typ : constant Entity_Id := Base_Type (Etype (N));
6537 return Static_Dispatch_Tables
6538 and then Tagged_Type_Expansion
6539 and then RTU_Loaded (Ada_Tags)
6541 -- Avoid circularity when rebuilding the compiler
6543 and then Cunit_Entity (Get_Source_Unit (N)) /= RTU_Entity (Ada_Tags)
6544 and then (Typ = RTE (RE_Dispatch_Table_Wrapper)
6546 Typ = RTE (RE_Address_Array)
6548 Typ = RTE (RE_Type_Specific_Data)
6550 Typ = RTE (RE_Tag_Table)
6552 (RTE_Available (RE_Interface_Data)
6553 and then Typ = RTE (RE_Interface_Data))
6555 (RTE_Available (RE_Interfaces_Array)
6556 and then Typ = RTE (RE_Interfaces_Array))
6558 (RTE_Available (RE_Interface_Data_Element)
6559 and then Typ = RTE (RE_Interface_Data_Element)));
6560 end Is_Static_Dispatch_Table_Aggregate;
6562 -----------------------------
6563 -- Is_Two_Dim_Packed_Array --
6564 -----------------------------
6566 function Is_Two_Dim_Packed_Array (Typ : Entity_Id) return Boolean is
6567 C : constant Int := UI_To_Int (Component_Size (Typ));
6569 return Number_Dimensions (Typ) = 2
6570 and then Is_Bit_Packed_Array (Typ)
6571 and then (C = 1 or else C = 2 or else C = 4);
6572 end Is_Two_Dim_Packed_Array;
6574 --------------------
6575 -- Late_Expansion --
6576 --------------------
6578 function Late_Expansion
6581 Target : Node_Id) return List_Id
6583 Aggr_Code : List_Id;
6586 if Is_Array_Type (Etype (N)) then
6588 Build_Array_Aggr_Code
6590 Ctype => Component_Type (Etype (N)),
6591 Index => First_Index (Typ),
6593 Scalar_Comp => Is_Scalar_Type (Component_Type (Typ)),
6594 Indexes => No_List);
6596 -- Directly or indirectly (e.g. access protected procedure) a record
6599 Aggr_Code := Build_Record_Aggr_Code (N, Typ, Target);
6602 -- Save the last assignment statement associated with the aggregate
6603 -- when building a controlled object. This reference is utilized by
6604 -- the finalization machinery when marking an object as successfully
6607 if Needs_Finalization (Typ)
6608 and then Is_Entity_Name (Target)
6609 and then Present (Entity (Target))
6610 and then Ekind_In (Entity (Target), E_Constant, E_Variable)
6612 Set_Last_Aggregate_Assignment (Entity (Target), Last (Aggr_Code));
6618 ----------------------------------
6619 -- Make_OK_Assignment_Statement --
6620 ----------------------------------
6622 function Make_OK_Assignment_Statement
6625 Expression : Node_Id) return Node_Id
6628 Set_Assignment_OK (Name);
6629 return Make_Assignment_Statement (Sloc, Name, Expression);
6630 end Make_OK_Assignment_Statement;
6632 -----------------------
6633 -- Number_Of_Choices --
6634 -----------------------
6636 function Number_Of_Choices (N : Node_Id) return Nat is
6640 Nb_Choices : Nat := 0;
6643 if Present (Expressions (N)) then
6647 Assoc := First (Component_Associations (N));
6648 while Present (Assoc) loop
6649 Choice := First (Choices (Assoc));
6650 while Present (Choice) loop
6651 if Nkind (Choice) /= N_Others_Choice then
6652 Nb_Choices := Nb_Choices + 1;
6662 end Number_Of_Choices;
6664 ------------------------------------
6665 -- Packed_Array_Aggregate_Handled --
6666 ------------------------------------
6668 -- The current version of this procedure will handle at compile time
6669 -- any array aggregate that meets these conditions:
6671 -- One and two dimensional, bit packed
6672 -- Underlying packed type is modular type
6673 -- Bounds are within 32-bit Int range
6674 -- All bounds and values are static
6676 -- Note: for now, in the 2-D case, we only handle component sizes of
6677 -- 1, 2, 4 (cases where an integral number of elements occupies a byte).
6679 function Packed_Array_Aggregate_Handled (N : Node_Id) return Boolean is
6680 Loc : constant Source_Ptr := Sloc (N);
6681 Typ : constant Entity_Id := Etype (N);
6682 Ctyp : constant Entity_Id := Component_Type (Typ);
6684 Not_Handled : exception;
6685 -- Exception raised if this aggregate cannot be handled
6688 -- Handle one- or two dimensional bit packed array
6690 if not Is_Bit_Packed_Array (Typ)
6691 or else Number_Dimensions (Typ) > 2
6696 -- If two-dimensional, check whether it can be folded, and transformed
6697 -- into a one-dimensional aggregate for the Packed_Array_Impl_Type of
6698 -- the original type.
6700 if Number_Dimensions (Typ) = 2 then
6701 return Two_Dim_Packed_Array_Handled (N);
6704 if not Is_Modular_Integer_Type (Packed_Array_Impl_Type (Typ)) then
6708 if not Is_Scalar_Type (Component_Type (Typ))
6709 and then Has_Non_Standard_Rep (Component_Type (Typ))
6715 Csiz : constant Nat := UI_To_Int (Component_Size (Typ));
6719 -- Bounds of index type
6723 -- Values of bounds if compile time known
6725 function Get_Component_Val (N : Node_Id) return Uint;
6726 -- Given a expression value N of the component type Ctyp, returns a
6727 -- value of Csiz (component size) bits representing this value. If
6728 -- the value is non-static or any other reason exists why the value
6729 -- cannot be returned, then Not_Handled is raised.
6731 -----------------------
6732 -- Get_Component_Val --
6733 -----------------------
6735 function Get_Component_Val (N : Node_Id) return Uint is
6739 -- We have to analyze the expression here before doing any further
6740 -- processing here. The analysis of such expressions is deferred
6741 -- till expansion to prevent some problems of premature analysis.
6743 Analyze_And_Resolve (N, Ctyp);
6745 -- Must have a compile time value. String literals have to be
6746 -- converted into temporaries as well, because they cannot easily
6747 -- be converted into their bit representation.
6749 if not Compile_Time_Known_Value (N)
6750 or else Nkind (N) = N_String_Literal
6755 Val := Expr_Rep_Value (N);
6757 -- Adjust for bias, and strip proper number of bits
6759 if Has_Biased_Representation (Ctyp) then
6760 Val := Val - Expr_Value (Type_Low_Bound (Ctyp));
6763 return Val mod Uint_2 ** Csiz;
6764 end Get_Component_Val;
6766 -- Here we know we have a one dimensional bit packed array
6769 Get_Index_Bounds (First_Index (Typ), Lo, Hi);
6771 -- Cannot do anything if bounds are dynamic
6773 if not Compile_Time_Known_Value (Lo)
6775 not Compile_Time_Known_Value (Hi)
6780 -- Or are silly out of range of int bounds
6782 Lob := Expr_Value (Lo);
6783 Hib := Expr_Value (Hi);
6785 if not UI_Is_In_Int_Range (Lob)
6787 not UI_Is_In_Int_Range (Hib)
6792 -- At this stage we have a suitable aggregate for handling at compile
6793 -- time. The only remaining checks are that the values of expressions
6794 -- in the aggregate are compile-time known (checks are performed by
6795 -- Get_Component_Val), and that any subtypes or ranges are statically
6798 -- If the aggregate is not fully positional at this stage, then
6799 -- convert it to positional form. Either this will fail, in which
6800 -- case we can do nothing, or it will succeed, in which case we have
6801 -- succeeded in handling the aggregate and transforming it into a
6802 -- modular value, or it will stay an aggregate, in which case we
6803 -- have failed to create a packed value for it.
6805 if Present (Component_Associations (N)) then
6806 Convert_To_Positional
6807 (N, Max_Others_Replicate => 64, Handle_Bit_Packed => True);
6808 return Nkind (N) /= N_Aggregate;
6811 -- Otherwise we are all positional, so convert to proper value
6814 Lov : constant Int := UI_To_Int (Lob);
6815 Hiv : constant Int := UI_To_Int (Hib);
6817 Len : constant Nat := Int'Max (0, Hiv - Lov + 1);
6818 -- The length of the array (number of elements)
6820 Aggregate_Val : Uint;
6821 -- Value of aggregate. The value is set in the low order bits of
6822 -- this value. For the little-endian case, the values are stored
6823 -- from low-order to high-order and for the big-endian case the
6824 -- values are stored from high-order to low-order. Note that gigi
6825 -- will take care of the conversions to left justify the value in
6826 -- the big endian case (because of left justified modular type
6827 -- processing), so we do not have to worry about that here.
6830 -- Integer literal for resulting constructed value
6833 -- Shift count from low order for next value
6836 -- Shift increment for loop
6839 -- Next expression from positional parameters of aggregate
6841 Left_Justified : Boolean;
6842 -- Set True if we are filling the high order bits of the target
6843 -- value (i.e. the value is left justified).
6846 -- For little endian, we fill up the low order bits of the target
6847 -- value. For big endian we fill up the high order bits of the
6848 -- target value (which is a left justified modular value).
6850 Left_Justified := Bytes_Big_Endian;
6852 -- Switch justification if using -gnatd8
6854 if Debug_Flag_8 then
6855 Left_Justified := not Left_Justified;
6858 -- Switch justfification if reverse storage order
6860 if Reverse_Storage_Order (Base_Type (Typ)) then
6861 Left_Justified := not Left_Justified;
6864 if Left_Justified then
6865 Shift := Csiz * (Len - 1);
6872 -- Loop to set the values
6875 Aggregate_Val := Uint_0;
6877 Expr := First (Expressions (N));
6878 Aggregate_Val := Get_Component_Val (Expr) * Uint_2 ** Shift;
6880 for J in 2 .. Len loop
6881 Shift := Shift + Incr;
6884 Aggregate_Val + Get_Component_Val (Expr) * Uint_2 ** Shift;
6888 -- Now we can rewrite with the proper value
6890 Lit := Make_Integer_Literal (Loc, Intval => Aggregate_Val);
6891 Set_Print_In_Hex (Lit);
6893 -- Construct the expression using this literal. Note that it is
6894 -- important to qualify the literal with its proper modular type
6895 -- since universal integer does not have the required range and
6896 -- also this is a left justified modular type, which is important
6897 -- in the big-endian case.
6900 Unchecked_Convert_To (Typ,
6901 Make_Qualified_Expression (Loc,
6903 New_Occurrence_Of (Packed_Array_Impl_Type (Typ), Loc),
6904 Expression => Lit)));
6906 Analyze_And_Resolve (N, Typ);
6914 end Packed_Array_Aggregate_Handled;
6916 ----------------------------
6917 -- Has_Mutable_Components --
6918 ----------------------------
6920 function Has_Mutable_Components (Typ : Entity_Id) return Boolean is
6924 Comp := First_Component (Typ);
6925 while Present (Comp) loop
6926 if Is_Record_Type (Etype (Comp))
6927 and then Has_Discriminants (Etype (Comp))
6928 and then not Is_Constrained (Etype (Comp))
6933 Next_Component (Comp);
6937 end Has_Mutable_Components;
6939 ------------------------------
6940 -- Initialize_Discriminants --
6941 ------------------------------
6943 procedure Initialize_Discriminants (N : Node_Id; Typ : Entity_Id) is
6944 Loc : constant Source_Ptr := Sloc (N);
6945 Bas : constant Entity_Id := Base_Type (Typ);
6946 Par : constant Entity_Id := Etype (Bas);
6947 Decl : constant Node_Id := Parent (Par);
6951 if Is_Tagged_Type (Bas)
6952 and then Is_Derived_Type (Bas)
6953 and then Has_Discriminants (Par)
6954 and then Has_Discriminants (Bas)
6955 and then Number_Discriminants (Bas) /= Number_Discriminants (Par)
6956 and then Nkind (Decl) = N_Full_Type_Declaration
6957 and then Nkind (Type_Definition (Decl)) = N_Record_Definition
6959 Present (Variant_Part (Component_List (Type_Definition (Decl))))
6960 and then Nkind (N) /= N_Extension_Aggregate
6963 -- Call init proc to set discriminants.
6964 -- There should eventually be a special procedure for this ???
6966 Ref := New_Occurrence_Of (Defining_Identifier (N), Loc);
6967 Insert_Actions_After (N,
6968 Build_Initialization_Call (Sloc (N), Ref, Typ));
6970 end Initialize_Discriminants;
6977 (Obj_Type : Entity_Id;
6978 Typ : Entity_Id) return Boolean
6980 L1, L2, H1, H2 : Node_Id;
6983 -- No sliding if the type of the object is not established yet, if it is
6984 -- an unconstrained type whose actual subtype comes from the aggregate,
6985 -- or if the two types are identical.
6987 if not Is_Array_Type (Obj_Type) then
6990 elsif not Is_Constrained (Obj_Type) then
6993 elsif Typ = Obj_Type then
6997 -- Sliding can only occur along the first dimension
6999 Get_Index_Bounds (First_Index (Typ), L1, H1);
7000 Get_Index_Bounds (First_Index (Obj_Type), L2, H2);
7002 if not Is_OK_Static_Expression (L1) or else
7003 not Is_OK_Static_Expression (L2) or else
7004 not Is_OK_Static_Expression (H1) or else
7005 not Is_OK_Static_Expression (H2)
7009 return Expr_Value (L1) /= Expr_Value (L2)
7011 Expr_Value (H1) /= Expr_Value (H2);
7016 ----------------------------------
7017 -- Two_Dim_Packed_Array_Handled --
7018 ----------------------------------
7020 function Two_Dim_Packed_Array_Handled (N : Node_Id) return Boolean is
7021 Loc : constant Source_Ptr := Sloc (N);
7022 Typ : constant Entity_Id := Etype (N);
7023 Ctyp : constant Entity_Id := Component_Type (Typ);
7024 Comp_Size : constant Int := UI_To_Int (Component_Size (Typ));
7025 Packed_Array : constant Entity_Id :=
7026 Packed_Array_Impl_Type (Base_Type (Typ));
7029 -- Expression in original aggregate
7032 -- One-dimensional subaggregate
7036 -- For now, only deal with cases where an integral number of elements
7037 -- fit in a single byte. This includes the most common boolean case.
7039 if not (Comp_Size = 1 or else
7040 Comp_Size = 2 or else
7046 Convert_To_Positional
7047 (N, Max_Others_Replicate => 64, Handle_Bit_Packed => True);
7049 -- Verify that all components are static
7051 if Nkind (N) = N_Aggregate
7052 and then Compile_Time_Known_Aggregate (N)
7056 -- The aggregate may have been re-analyzed and converted already
7058 elsif Nkind (N) /= N_Aggregate then
7061 -- If component associations remain, the aggregate is not static
7063 elsif Present (Component_Associations (N)) then
7067 One_Dim := First (Expressions (N));
7068 while Present (One_Dim) loop
7069 if Present (Component_Associations (One_Dim)) then
7073 One_Comp := First (Expressions (One_Dim));
7074 while Present (One_Comp) loop
7075 if not Is_OK_Static_Expression (One_Comp) then
7086 -- Two-dimensional aggregate is now fully positional so pack one
7087 -- dimension to create a static one-dimensional array, and rewrite
7088 -- as an unchecked conversion to the original type.
7091 Byte_Size : constant Int := UI_To_Int (Component_Size (Packed_Array));
7092 -- The packed array type is a byte array
7095 -- Number of components accumulated in current byte
7098 -- Assembled list of packed values for equivalent aggregate
7101 -- integer value of component
7104 -- Step size for packing
7107 -- Endian-dependent start position for packing
7110 -- Current insertion position
7113 -- Component of packed array being assembled.
7120 -- Account for endianness. See corresponding comment in
7121 -- Packed_Array_Aggregate_Handled concerning the following.
7125 xor Reverse_Storage_Order (Base_Type (Typ))
7127 Init_Shift := Byte_Size - Comp_Size;
7134 -- Iterate over each subaggregate
7136 Shift := Init_Shift;
7137 One_Dim := First (Expressions (N));
7138 while Present (One_Dim) loop
7139 One_Comp := First (Expressions (One_Dim));
7140 while Present (One_Comp) loop
7141 if Packed_Num = Byte_Size / Comp_Size then
7143 -- Byte is complete, add to list of expressions
7145 Append (Make_Integer_Literal (Sloc (One_Dim), Val), Comps);
7147 Shift := Init_Shift;
7151 Comp_Val := Expr_Rep_Value (One_Comp);
7153 -- Adjust for bias, and strip proper number of bits
7155 if Has_Biased_Representation (Ctyp) then
7156 Comp_Val := Comp_Val - Expr_Value (Type_Low_Bound (Ctyp));
7159 Comp_Val := Comp_Val mod Uint_2 ** Comp_Size;
7160 Val := UI_To_Int (Val + Comp_Val * Uint_2 ** Shift);
7161 Shift := Shift + Incr;
7162 One_Comp := Next (One_Comp);
7163 Packed_Num := Packed_Num + 1;
7167 One_Dim := Next (One_Dim);
7170 if Packed_Num > 0 then
7172 -- Add final incomplete byte if present
7174 Append (Make_Integer_Literal (Sloc (One_Dim), Val), Comps);
7178 Unchecked_Convert_To (Typ,
7179 Make_Qualified_Expression (Loc,
7180 Subtype_Mark => New_Occurrence_Of (Packed_Array, Loc),
7181 Expression => Make_Aggregate (Loc, Expressions => Comps))));
7182 Analyze_And_Resolve (N);
7185 end Two_Dim_Packed_Array_Handled;
7187 ---------------------
7188 -- Sort_Case_Table --
7189 ---------------------
7191 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type) is
7192 L : constant Int := Case_Table'First;
7193 U : constant Int := Case_Table'Last;
7201 T := Case_Table (K + 1);
7205 and then Expr_Value (Case_Table (J - 1).Choice_Lo) >
7206 Expr_Value (T.Choice_Lo)
7208 Case_Table (J) := Case_Table (J - 1);
7212 Case_Table (J) := T;
7215 end Sort_Case_Table;
7217 ----------------------------
7218 -- Static_Array_Aggregate --
7219 ----------------------------
7221 function Static_Array_Aggregate (N : Node_Id) return Boolean is
7222 Bounds : constant Node_Id := Aggregate_Bounds (N);
7224 Typ : constant Entity_Id := Etype (N);
7225 Comp_Type : constant Entity_Id := Component_Type (Typ);
7232 if Is_Tagged_Type (Typ)
7233 or else Is_Controlled (Typ)
7234 or else Is_Packed (Typ)
7240 and then Nkind (Bounds) = N_Range
7241 and then Nkind (Low_Bound (Bounds)) = N_Integer_Literal
7242 and then Nkind (High_Bound (Bounds)) = N_Integer_Literal
7244 Lo := Low_Bound (Bounds);
7245 Hi := High_Bound (Bounds);
7247 if No (Component_Associations (N)) then
7249 -- Verify that all components are static integers
7251 Expr := First (Expressions (N));
7252 while Present (Expr) loop
7253 if Nkind (Expr) /= N_Integer_Literal then
7263 -- We allow only a single named association, either a static
7264 -- range or an others_clause, with a static expression.
7266 Expr := First (Component_Associations (N));
7268 if Present (Expressions (N)) then
7271 elsif Present (Next (Expr)) then
7274 elsif Present (Next (First (Choices (Expr)))) then
7278 -- The aggregate is static if all components are literals,
7279 -- or else all its components are static aggregates for the
7280 -- component type. We also limit the size of a static aggregate
7281 -- to prevent runaway static expressions.
7283 if Is_Array_Type (Comp_Type)
7284 or else Is_Record_Type (Comp_Type)
7286 if Nkind (Expression (Expr)) /= N_Aggregate
7288 not Compile_Time_Known_Aggregate (Expression (Expr))
7293 elsif Nkind (Expression (Expr)) /= N_Integer_Literal then
7297 if not Aggr_Size_OK (N, Typ) then
7301 -- Create a positional aggregate with the right number of
7302 -- copies of the expression.
7304 Agg := Make_Aggregate (Sloc (N), New_List, No_List);
7306 for I in UI_To_Int (Intval (Lo)) .. UI_To_Int (Intval (Hi))
7308 Append_To (Expressions (Agg), New_Copy (Expression (Expr)));
7310 -- The copied expression must be analyzed and resolved.
7311 -- Besides setting the type, this ensures that static
7312 -- expressions are appropriately marked as such.
7315 (Last (Expressions (Agg)), Component_Type (Typ));
7318 Set_Aggregate_Bounds (Agg, Bounds);
7319 Set_Etype (Agg, Typ);
7322 Set_Compile_Time_Known_Aggregate (N);
7331 end Static_Array_Aggregate;