1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2009, 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_Ch7; use Exp_Ch7;
36 with Exp_Ch9; use Exp_Ch9;
37 with Exp_Tss; use Exp_Tss;
38 with Fname; use Fname;
39 with Freeze; use Freeze;
40 with Itypes; use Itypes;
42 with Namet; use Namet;
43 with Nmake; use Nmake;
44 with Nlists; use Nlists;
46 with Restrict; use Restrict;
47 with Rident; use Rident;
48 with Rtsfind; use Rtsfind;
49 with Ttypes; use Ttypes;
51 with Sem_Aux; use Sem_Aux;
52 with Sem_Ch3; use Sem_Ch3;
53 with Sem_Eval; use Sem_Eval;
54 with Sem_Res; use Sem_Res;
55 with Sem_Util; use Sem_Util;
56 with Sinfo; use Sinfo;
57 with Snames; use Snames;
58 with Stand; use Stand;
59 with Targparm; use Targparm;
60 with Tbuild; use Tbuild;
61 with Uintp; use Uintp;
63 package body Exp_Aggr is
65 type Case_Bounds is record
68 Choice_Node : Node_Id;
71 type Case_Table_Type is array (Nat range <>) of Case_Bounds;
72 -- Table type used by Check_Case_Choices procedure
75 (Obj_Type : Entity_Id;
76 Typ : Entity_Id) return Boolean;
77 -- A static array aggregate in an object declaration can in most cases be
78 -- expanded in place. The one exception is when the aggregate is given
79 -- with component associations that specify different bounds from those of
80 -- the type definition in the object declaration. In this pathological
81 -- case the aggregate must slide, and we must introduce an intermediate
82 -- temporary to hold it.
84 -- The same holds in an assignment to one-dimensional array of arrays,
85 -- when a component may be given with bounds that differ from those of the
88 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type);
89 -- Sort the Case Table using the Lower Bound of each Choice as the key.
90 -- A simple insertion sort is used since the number of choices in a case
91 -- statement of variant part will usually be small and probably in near
94 function Has_Default_Init_Comps (N : Node_Id) return Boolean;
95 -- N is an aggregate (record or array). Checks the presence of default
96 -- initialization (<>) in any component (Ada 2005: AI-287)
98 function Is_Static_Dispatch_Table_Aggregate (N : Node_Id) return Boolean;
99 -- Returns true if N is an aggregate used to initialize the components
100 -- of an statically allocated dispatch table.
102 ------------------------------------------------------
103 -- Local subprograms for Record Aggregate Expansion --
104 ------------------------------------------------------
106 procedure Expand_Record_Aggregate
108 Orig_Tag : Node_Id := Empty;
109 Parent_Expr : Node_Id := Empty);
110 -- This is the top level procedure for record aggregate expansion.
111 -- Expansion for record aggregates needs expand aggregates for tagged
112 -- record types. Specifically Expand_Record_Aggregate adds the Tag
113 -- field in front of the Component_Association list that was created
114 -- during resolution by Resolve_Record_Aggregate.
116 -- N is the record aggregate node.
117 -- Orig_Tag is the value of the Tag that has to be provided for this
118 -- specific aggregate. It carries the tag corresponding to the type
119 -- of the outermost aggregate during the recursive expansion
120 -- Parent_Expr is the ancestor part of the original extension
123 procedure Convert_To_Assignments (N : Node_Id; Typ : Entity_Id);
124 -- N is an N_Aggregate or an N_Extension_Aggregate. Typ is the type of the
125 -- aggregate (which can only be a record type, this procedure is only used
126 -- for record types). Transform the given aggregate into a sequence of
127 -- assignments performed component by component.
129 function Build_Record_Aggr_Code
133 Flist : Node_Id := Empty;
134 Obj : Entity_Id := Empty;
135 Is_Limited_Ancestor_Expansion : Boolean := False) return List_Id;
136 -- N is an N_Aggregate or an N_Extension_Aggregate. Typ is the type of the
137 -- aggregate. Target is an expression containing the location on which the
138 -- component by component assignments will take place. Returns the list of
139 -- assignments plus all other adjustments needed for tagged and controlled
140 -- types. Flist is an expression representing the finalization list on
141 -- which to attach the controlled components if any. Obj is present in the
142 -- object declaration and dynamic allocation cases, it contains an entity
143 -- that allows to know if the value being created needs to be attached to
144 -- the final list in case of pragma Finalize_Storage_Only.
147 -- The meaning of the Obj formal is extremely unclear. *What* entity
148 -- should be passed? For the object declaration case we may guess that
149 -- this is the object being declared, but what about the allocator case?
151 -- Is_Limited_Ancestor_Expansion indicates that the function has been
152 -- called recursively to expand the limited ancestor to avoid copying it.
154 function Has_Mutable_Components (Typ : Entity_Id) return Boolean;
155 -- Return true if one of the component is of a discriminated type with
156 -- defaults. An aggregate for a type with mutable components must be
157 -- expanded into individual assignments.
159 procedure Initialize_Discriminants (N : Node_Id; Typ : Entity_Id);
160 -- If the type of the aggregate is a type extension with renamed discrimi-
161 -- nants, we must initialize the hidden discriminants of the parent.
162 -- Otherwise, the target object must not be initialized. The discriminants
163 -- are initialized by calling the initialization procedure for the type.
164 -- This is incorrect if the initialization of other components has any
165 -- side effects. We restrict this call to the case where the parent type
166 -- has a variant part, because this is the only case where the hidden
167 -- discriminants are accessed, namely when calling discriminant checking
168 -- functions of the parent type, and when applying a stream attribute to
169 -- an object of the derived type.
171 -----------------------------------------------------
172 -- Local Subprograms for Array Aggregate Expansion --
173 -----------------------------------------------------
175 function Aggr_Size_OK (N : Node_Id; Typ : Entity_Id) return Boolean;
176 -- Very large static aggregates present problems to the back-end, and
177 -- are transformed into assignments and loops. This function verifies
178 -- that the total number of components of an aggregate is acceptable
179 -- for transformation into a purely positional static form. It is called
180 -- prior to calling Flatten.
181 -- This function also detects and warns about one-component aggregates
182 -- that appear in a non-static context. Even if the component value is
183 -- static, such an aggregate must be expanded into an assignment.
185 procedure Convert_Array_Aggr_In_Allocator
189 -- If the aggregate appears within an allocator and can be expanded in
190 -- place, this routine generates the individual assignments to components
191 -- of the designated object. This is an optimization over the general
192 -- case, where a temporary is first created on the stack and then used to
193 -- construct the allocated object on the heap.
195 procedure Convert_To_Positional
197 Max_Others_Replicate : Nat := 5;
198 Handle_Bit_Packed : Boolean := False);
199 -- If possible, convert named notation to positional notation. This
200 -- conversion is possible only in some static cases. If the conversion is
201 -- possible, then N is rewritten with the analyzed converted aggregate.
202 -- The parameter Max_Others_Replicate controls the maximum number of
203 -- values corresponding to an others choice that will be converted to
204 -- positional notation (the default of 5 is the normal limit, and reflects
205 -- the fact that normally the loop is better than a lot of separate
206 -- assignments). Note that this limit gets overridden in any case if
207 -- either of the restrictions No_Elaboration_Code or No_Implicit_Loops is
208 -- set. The parameter Handle_Bit_Packed is usually set False (since we do
209 -- not expect the back end to handle bit packed arrays, so the normal case
210 -- of conversion is pointless), but in the special case of a call from
211 -- Packed_Array_Aggregate_Handled, we set this parameter to True, since
212 -- these are cases we handle in there.
214 procedure Expand_Array_Aggregate (N : Node_Id);
215 -- This is the top-level routine to perform array aggregate expansion.
216 -- N is the N_Aggregate node to be expanded.
218 function Backend_Processing_Possible (N : Node_Id) return Boolean;
219 -- This function checks if array aggregate N can be processed directly
220 -- by Gigi. If this is the case True is returned.
222 function Build_Array_Aggr_Code
227 Scalar_Comp : Boolean;
228 Indices : List_Id := No_List;
229 Flist : Node_Id := Empty) return List_Id;
230 -- This recursive routine returns a list of statements containing the
231 -- loops and assignments that are needed for the expansion of the array
234 -- N is the (sub-)aggregate node to be expanded into code. This node
235 -- has been fully analyzed, and its Etype is properly set.
237 -- Index is the index node corresponding to the array sub-aggregate N.
239 -- Into is the target expression into which we are copying the aggregate.
240 -- Note that this node may not have been analyzed yet, and so the Etype
241 -- field may not be set.
243 -- Scalar_Comp is True if the component type of the aggregate is scalar.
245 -- Indices is the current list of expressions used to index the
246 -- object we are writing into.
248 -- Flist is an expression representing the finalization list on which
249 -- to attach the controlled components if any.
251 function Number_Of_Choices (N : Node_Id) return Nat;
252 -- Returns the number of discrete choices (not including the others choice
253 -- if present) contained in (sub-)aggregate N.
255 function Late_Expansion
259 Flist : Node_Id := Empty;
260 Obj : Entity_Id := Empty) return List_Id;
261 -- N is a nested (record or array) aggregate that has been marked with
262 -- 'Delay_Expansion'. Typ is the expected type of the aggregate and Target
263 -- is a (duplicable) expression that will hold the result of the aggregate
264 -- expansion. Flist is the finalization list to be used to attach
265 -- controlled components. 'Obj' when non empty, carries the original
266 -- object being initialized in order to know if it needs to be attached to
267 -- the previous parameter which may not be the case in the case where
268 -- Finalize_Storage_Only is set. Basically this procedure is used to
269 -- implement top-down expansions of nested aggregates. This is necessary
270 -- for avoiding temporaries at each level as well as for propagating the
271 -- right internal finalization list.
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
279 -- use this routine. This is needed to deal with assignments to
280 -- initialized constants that are done in place.
282 function Packed_Array_Aggregate_Handled (N : Node_Id) return Boolean;
283 -- Given an array aggregate, this function handles the case of a packed
284 -- array aggregate with all constant values, where the aggregate can be
285 -- evaluated at compile time. If this is possible, then N is rewritten
286 -- to be its proper compile time value with all the components properly
287 -- assembled. The expression is analyzed and resolved and True is
288 -- returned. If this transformation is not possible, N is unchanged
289 -- and False is returned
291 function Safe_Slice_Assignment (N : Node_Id) return Boolean;
292 -- If a slice assignment has an aggregate with a single others_choice,
293 -- the assignment can be done in place even if bounds are not static,
294 -- by converting it into a loop over the discrete range of the slice.
300 function Aggr_Size_OK (N : Node_Id; Typ : Entity_Id) return Boolean is
308 -- The following constant determines the maximum size of an
309 -- array aggregate produced by converting named to positional
310 -- notation (e.g. from others clauses). This avoids running
311 -- away with attempts to convert huge aggregates, which hit
312 -- memory limits in the backend.
314 -- The normal limit is 5000, but we increase this limit to
315 -- 2**24 (about 16 million) if Restrictions (No_Elaboration_Code)
316 -- or Restrictions (No_Implicit_Loops) is specified, since in
317 -- either case, we are at risk of declaring the program illegal
318 -- because of this limit.
320 Max_Aggr_Size : constant Nat :=
321 5000 + (2 ** 24 - 5000) *
323 (Restriction_Active (No_Elaboration_Code)
325 Restriction_Active (No_Implicit_Loops));
327 function Component_Count (T : Entity_Id) return Int;
328 -- The limit is applied to the total number of components that the
329 -- aggregate will have, which is the number of static expressions
330 -- that will appear in the flattened array. This requires a recursive
331 -- computation of the number of scalar components of the structure.
333 ---------------------
334 -- Component_Count --
335 ---------------------
337 function Component_Count (T : Entity_Id) return Int is
342 if Is_Scalar_Type (T) then
345 elsif Is_Record_Type (T) then
346 Comp := First_Component (T);
347 while Present (Comp) loop
348 Res := Res + Component_Count (Etype (Comp));
349 Next_Component (Comp);
354 elsif Is_Array_Type (T) then
356 Lo : constant Node_Id :=
357 Type_Low_Bound (Etype (First_Index (T)));
358 Hi : constant Node_Id :=
359 Type_High_Bound (Etype (First_Index (T)));
361 Siz : constant Int := Component_Count (Component_Type (T));
364 if not Compile_Time_Known_Value (Lo)
365 or else not Compile_Time_Known_Value (Hi)
370 Siz * UI_To_Int (Expr_Value (Hi) - Expr_Value (Lo) + 1);
375 -- Can only be a null for an access type
381 -- Start of processing for Aggr_Size_OK
384 Siz := Component_Count (Component_Type (Typ));
386 Indx := First_Index (Typ);
387 while Present (Indx) loop
388 Lo := Type_Low_Bound (Etype (Indx));
389 Hi := Type_High_Bound (Etype (Indx));
391 -- Bounds need to be known at compile time
393 if not Compile_Time_Known_Value (Lo)
394 or else not Compile_Time_Known_Value (Hi)
399 Lov := Expr_Value (Lo);
400 Hiv := Expr_Value (Hi);
402 -- A flat array is always safe
408 -- One-component aggregates are suspicious, and if the context type
409 -- is an object declaration with non-static bounds it will trip gcc;
410 -- such an aggregate must be expanded into a single assignment.
413 and then Nkind (Parent (N)) = N_Object_Declaration
416 Index_Type : constant Entity_Id :=
419 (Etype (Defining_Identifier (Parent (N)))));
423 if not Compile_Time_Known_Value (Type_Low_Bound (Index_Type))
424 or else not Compile_Time_Known_Value
425 (Type_High_Bound (Index_Type))
427 if Present (Component_Associations (N)) then
429 First (Choices (First (Component_Associations (N))));
430 if Is_Entity_Name (Indx)
431 and then not Is_Type (Entity (Indx))
434 ("single component aggregate in non-static context?",
436 Error_Msg_N ("\maybe subtype name was meant?", Indx);
446 Rng : constant Uint := Hiv - Lov + 1;
449 -- Check if size is too large
451 if not UI_Is_In_Int_Range (Rng) then
455 Siz := Siz * UI_To_Int (Rng);
459 or else Siz > Max_Aggr_Size
464 -- Bounds must be in integer range, for later array construction
466 if not UI_Is_In_Int_Range (Lov)
468 not UI_Is_In_Int_Range (Hiv)
479 ---------------------------------
480 -- Backend_Processing_Possible --
481 ---------------------------------
483 -- Backend processing by Gigi/gcc is possible only if all the following
484 -- conditions are met:
486 -- 1. N is fully positional
488 -- 2. N is not a bit-packed array aggregate;
490 -- 3. The size of N's array type must be known at compile time. Note
491 -- that this implies that the component size is also known
493 -- 4. The array type of N does not follow the Fortran layout convention
494 -- or if it does it must be 1 dimensional.
496 -- 5. The array component type may not be tagged (which could necessitate
497 -- reassignment of proper tags).
499 -- 6. The array component type must not have unaligned bit components
501 -- 7. None of the components of the aggregate may be bit unaligned
504 -- 8. There cannot be delayed components, since we do not know enough
505 -- at this stage to know if back end processing is possible.
507 -- 9. There cannot be any discriminated record components, since the
508 -- back end cannot handle this complex case.
510 -- 10. No controlled actions need to be generated for components
512 function Backend_Processing_Possible (N : Node_Id) return Boolean is
513 Typ : constant Entity_Id := Etype (N);
514 -- Typ is the correct constrained array subtype of the aggregate
516 function Component_Check (N : Node_Id; Index : Node_Id) return Boolean;
517 -- This routine checks components of aggregate N, enforcing checks
518 -- 1, 7, 8, and 9. In the multi-dimensional case, these checks are
519 -- performed on subaggregates. The Index value is the current index
520 -- being checked in the multi-dimensional case.
522 ---------------------
523 -- Component_Check --
524 ---------------------
526 function Component_Check (N : Node_Id; Index : Node_Id) return Boolean is
530 -- Checks 1: (no component associations)
532 if Present (Component_Associations (N)) then
536 -- Checks on components
538 -- Recurse to check subaggregates, which may appear in qualified
539 -- expressions. If delayed, the front-end will have to expand.
540 -- If the component is a discriminated record, treat as non-static,
541 -- as the back-end cannot handle this properly.
543 Expr := First (Expressions (N));
544 while Present (Expr) loop
546 -- Checks 8: (no delayed components)
548 if Is_Delayed_Aggregate (Expr) then
552 -- Checks 9: (no discriminated records)
554 if Present (Etype (Expr))
555 and then Is_Record_Type (Etype (Expr))
556 and then Has_Discriminants (Etype (Expr))
561 -- Checks 7. Component must not be bit aligned component
563 if Possible_Bit_Aligned_Component (Expr) then
567 -- Recursion to following indexes for multiple dimension case
569 if Present (Next_Index (Index))
570 and then not Component_Check (Expr, Next_Index (Index))
575 -- All checks for that component finished, on to next
583 -- Start of processing for Backend_Processing_Possible
586 -- Checks 2 (array not bit packed) and 10 (no controlled actions)
588 if Is_Bit_Packed_Array (Typ) or else Needs_Finalization (Typ) then
592 -- If component is limited, aggregate must be expanded because each
593 -- component assignment must be built in place.
595 if Is_Inherently_Limited_Type (Component_Type (Typ)) then
599 -- Checks 4 (array must not be multi-dimensional Fortran case)
601 if Convention (Typ) = Convention_Fortran
602 and then Number_Dimensions (Typ) > 1
607 -- Checks 3 (size of array must be known at compile time)
609 if not Size_Known_At_Compile_Time (Typ) then
613 -- Checks on components
615 if not Component_Check (N, First_Index (Typ)) then
619 -- Checks 5 (if the component type is tagged, then we may need to do
620 -- tag adjustments. Perhaps this should be refined to check for any
621 -- component associations that actually need tag adjustment, similar
622 -- to the test in Component_Not_OK_For_Backend for record aggregates
623 -- with tagged components, but not clear whether it's worthwhile ???;
624 -- in the case of the JVM, object tags are handled implicitly)
626 if Is_Tagged_Type (Component_Type (Typ)) and then VM_Target = No_VM then
630 -- Checks 6 (component type must not have bit aligned components)
632 if Type_May_Have_Bit_Aligned_Components (Component_Type (Typ)) then
636 -- Backend processing is possible
638 Set_Size_Known_At_Compile_Time (Etype (N), True);
640 end Backend_Processing_Possible;
642 ---------------------------
643 -- Build_Array_Aggr_Code --
644 ---------------------------
646 -- The code that we generate from a one dimensional aggregate is
648 -- 1. If the sub-aggregate contains discrete choices we
650 -- (a) Sort the discrete choices
652 -- (b) Otherwise for each discrete choice that specifies a range we
653 -- emit a loop. If a range specifies a maximum of three values, or
654 -- we are dealing with an expression we emit a sequence of
655 -- assignments instead of a loop.
657 -- (c) Generate the remaining loops to cover the others choice if any
659 -- 2. If the aggregate contains positional elements we
661 -- (a) translate the positional elements in a series of assignments
663 -- (b) Generate a final loop to cover the others choice if any.
664 -- Note that this final loop has to be a while loop since the case
666 -- L : Integer := Integer'Last;
667 -- H : Integer := Integer'Last;
668 -- A : array (L .. H) := (1, others =>0);
670 -- cannot be handled by a for loop. Thus for the following
672 -- array (L .. H) := (.. positional elements.., others =>E);
674 -- we always generate something like:
676 -- J : Index_Type := Index_Of_Last_Positional_Element;
678 -- J := Index_Base'Succ (J)
682 function Build_Array_Aggr_Code
687 Scalar_Comp : Boolean;
688 Indices : List_Id := No_List;
689 Flist : Node_Id := Empty) return List_Id
691 Loc : constant Source_Ptr := Sloc (N);
692 Index_Base : constant Entity_Id := Base_Type (Etype (Index));
693 Index_Base_L : constant Node_Id := Type_Low_Bound (Index_Base);
694 Index_Base_H : constant Node_Id := Type_High_Bound (Index_Base);
696 function Add (Val : Int; To : Node_Id) return Node_Id;
697 -- Returns an expression where Val is added to expression To, unless
698 -- To+Val is provably out of To's base type range. To must be an
699 -- already analyzed expression.
701 function Empty_Range (L, H : Node_Id) return Boolean;
702 -- Returns True if the range defined by L .. H is certainly empty
704 function Equal (L, H : Node_Id) return Boolean;
705 -- Returns True if L = H for sure
707 function Index_Base_Name return Node_Id;
708 -- Returns a new reference to the index type name
710 function Gen_Assign (Ind : Node_Id; Expr : Node_Id) return List_Id;
711 -- Ind must be a side-effect free expression. If the input aggregate
712 -- N to Build_Loop contains no sub-aggregates, then this function
713 -- returns the assignment statement:
715 -- Into (Indices, Ind) := Expr;
717 -- Otherwise we call Build_Code recursively
719 -- Ada 2005 (AI-287): In case of default initialized component, Expr
720 -- is empty and we generate a call to the corresponding IP subprogram.
722 function Gen_Loop (L, H : Node_Id; Expr : Node_Id) return List_Id;
723 -- Nodes L and H must be side-effect free expressions.
724 -- If the input aggregate N to Build_Loop contains no sub-aggregates,
725 -- This routine returns the for loop statement
727 -- for J in Index_Base'(L) .. Index_Base'(H) loop
728 -- Into (Indices, J) := Expr;
731 -- Otherwise we call Build_Code recursively.
732 -- As an optimization if the loop covers 3 or less scalar elements we
733 -- generate a sequence of assignments.
735 function Gen_While (L, H : Node_Id; Expr : Node_Id) return List_Id;
736 -- Nodes L and H must be side-effect free expressions.
737 -- If the input aggregate N to Build_Loop contains no sub-aggregates,
738 -- This routine returns the while loop statement
740 -- J : Index_Base := L;
742 -- J := Index_Base'Succ (J);
743 -- Into (Indices, J) := Expr;
746 -- Otherwise we call Build_Code recursively
748 function Local_Compile_Time_Known_Value (E : Node_Id) return Boolean;
749 function Local_Expr_Value (E : Node_Id) return Uint;
750 -- These two Local routines are used to replace the corresponding ones
751 -- in sem_eval because while processing the bounds of an aggregate with
752 -- discrete choices whose index type is an enumeration, we build static
753 -- expressions not recognized by Compile_Time_Known_Value as such since
754 -- they have not yet been analyzed and resolved. All the expressions in
755 -- question are things like Index_Base_Name'Val (Const) which we can
756 -- easily recognize as being constant.
762 function Add (Val : Int; To : Node_Id) return Node_Id is
767 U_Val : constant Uint := UI_From_Int (Val);
770 -- Note: do not try to optimize the case of Val = 0, because
771 -- we need to build a new node with the proper Sloc value anyway.
773 -- First test if we can do constant folding
775 if Local_Compile_Time_Known_Value (To) then
776 U_To := Local_Expr_Value (To) + Val;
778 -- Determine if our constant is outside the range of the index.
779 -- If so return an Empty node. This empty node will be caught
780 -- by Empty_Range below.
782 if Compile_Time_Known_Value (Index_Base_L)
783 and then U_To < Expr_Value (Index_Base_L)
787 elsif Compile_Time_Known_Value (Index_Base_H)
788 and then U_To > Expr_Value (Index_Base_H)
793 Expr_Pos := Make_Integer_Literal (Loc, U_To);
794 Set_Is_Static_Expression (Expr_Pos);
796 if not Is_Enumeration_Type (Index_Base) then
799 -- If we are dealing with enumeration return
800 -- Index_Base'Val (Expr_Pos)
804 Make_Attribute_Reference
806 Prefix => Index_Base_Name,
807 Attribute_Name => Name_Val,
808 Expressions => New_List (Expr_Pos));
814 -- If we are here no constant folding possible
816 if not Is_Enumeration_Type (Index_Base) then
819 Left_Opnd => Duplicate_Subexpr (To),
820 Right_Opnd => Make_Integer_Literal (Loc, U_Val));
822 -- If we are dealing with enumeration return
823 -- Index_Base'Val (Index_Base'Pos (To) + Val)
827 Make_Attribute_Reference
829 Prefix => Index_Base_Name,
830 Attribute_Name => Name_Pos,
831 Expressions => New_List (Duplicate_Subexpr (To)));
836 Right_Opnd => Make_Integer_Literal (Loc, U_Val));
839 Make_Attribute_Reference
841 Prefix => Index_Base_Name,
842 Attribute_Name => Name_Val,
843 Expressions => New_List (Expr_Pos));
853 function Empty_Range (L, H : Node_Id) return Boolean is
854 Is_Empty : Boolean := False;
859 -- First check if L or H were already detected as overflowing the
860 -- index base range type by function Add above. If this is so Add
861 -- returns the empty node.
863 if No (L) or else No (H) then
870 -- L > H range is empty
876 -- B_L > H range must be empty
882 -- L > B_H range must be empty
886 High := Index_Base_H;
889 if Local_Compile_Time_Known_Value (Low)
890 and then Local_Compile_Time_Known_Value (High)
893 UI_Gt (Local_Expr_Value (Low), Local_Expr_Value (High));
906 function Equal (L, H : Node_Id) return Boolean is
911 elsif Local_Compile_Time_Known_Value (L)
912 and then Local_Compile_Time_Known_Value (H)
914 return UI_Eq (Local_Expr_Value (L), Local_Expr_Value (H));
924 function Gen_Assign (Ind : Node_Id; Expr : Node_Id) return List_Id is
925 L : constant List_Id := New_List;
929 New_Indices : List_Id;
930 Indexed_Comp : Node_Id;
932 Comp_Type : Entity_Id := Empty;
934 function Add_Loop_Actions (Lis : List_Id) return List_Id;
935 -- Collect insert_actions generated in the construction of a
936 -- loop, and prepend them to the sequence of assignments to
937 -- complete the eventual body of the loop.
939 ----------------------
940 -- Add_Loop_Actions --
941 ----------------------
943 function Add_Loop_Actions (Lis : List_Id) return List_Id is
947 -- Ada 2005 (AI-287): Do nothing else in case of default
948 -- initialized component.
953 elsif Nkind (Parent (Expr)) = N_Component_Association
954 and then Present (Loop_Actions (Parent (Expr)))
956 Append_List (Lis, Loop_Actions (Parent (Expr)));
957 Res := Loop_Actions (Parent (Expr));
958 Set_Loop_Actions (Parent (Expr), No_List);
964 end Add_Loop_Actions;
966 -- Start of processing for Gen_Assign
970 New_Indices := New_List;
972 New_Indices := New_Copy_List_Tree (Indices);
975 Append_To (New_Indices, Ind);
977 if Present (Flist) then
978 F := New_Copy_Tree (Flist);
980 elsif Present (Etype (N)) and then Needs_Finalization (Etype (N)) then
981 if Is_Entity_Name (Into)
982 and then Present (Scope (Entity (Into)))
984 F := Find_Final_List (Scope (Entity (Into)));
986 F := Find_Final_List (Current_Scope);
992 if Present (Next_Index (Index)) then
995 Build_Array_Aggr_Code
998 Index => Next_Index (Index),
1000 Scalar_Comp => Scalar_Comp,
1001 Indices => New_Indices,
1005 -- If we get here then we are at a bottom-level (sub-)aggregate
1009 (Make_Indexed_Component (Loc,
1010 Prefix => New_Copy_Tree (Into),
1011 Expressions => New_Indices));
1013 Set_Assignment_OK (Indexed_Comp);
1015 -- Ada 2005 (AI-287): In case of default initialized component, Expr
1016 -- is not present (and therefore we also initialize Expr_Q to empty).
1020 elsif Nkind (Expr) = N_Qualified_Expression then
1021 Expr_Q := Expression (Expr);
1026 if Present (Etype (N))
1027 and then Etype (N) /= Any_Composite
1029 Comp_Type := Component_Type (Etype (N));
1030 pragma Assert (Comp_Type = Ctype); -- AI-287
1032 elsif Present (Next (First (New_Indices))) then
1034 -- Ada 2005 (AI-287): Do nothing in case of default initialized
1035 -- component because we have received the component type in
1036 -- the formal parameter Ctype.
1038 -- ??? Some assert pragmas have been added to check if this new
1039 -- formal can be used to replace this code in all cases.
1041 if Present (Expr) then
1043 -- This is a multidimensional array. Recover the component
1044 -- type from the outermost aggregate, because subaggregates
1045 -- do not have an assigned type.
1052 while Present (P) loop
1053 if Nkind (P) = N_Aggregate
1054 and then Present (Etype (P))
1056 Comp_Type := Component_Type (Etype (P));
1064 pragma Assert (Comp_Type = Ctype); -- AI-287
1069 -- Ada 2005 (AI-287): We only analyze the expression in case of non-
1070 -- default initialized components (otherwise Expr_Q is not present).
1073 and then Nkind_In (Expr_Q, N_Aggregate, N_Extension_Aggregate)
1075 -- At this stage the Expression may not have been analyzed yet
1076 -- because the array aggregate code has not been updated to use
1077 -- the Expansion_Delayed flag and avoid analysis altogether to
1078 -- solve the same problem (see Resolve_Aggr_Expr). So let us do
1079 -- the analysis of non-array aggregates now in order to get the
1080 -- value of Expansion_Delayed flag for the inner aggregate ???
1082 if Present (Comp_Type) and then not Is_Array_Type (Comp_Type) then
1083 Analyze_And_Resolve (Expr_Q, Comp_Type);
1086 if Is_Delayed_Aggregate (Expr_Q) then
1088 -- This is either a subaggregate of a multidimentional array,
1089 -- or a component of an array type whose component type is
1090 -- also an array. In the latter case, the expression may have
1091 -- component associations that provide different bounds from
1092 -- those of the component type, and sliding must occur. Instead
1093 -- of decomposing the current aggregate assignment, force the
1094 -- re-analysis of the assignment, so that a temporary will be
1095 -- generated in the usual fashion, and sliding will take place.
1097 if Nkind (Parent (N)) = N_Assignment_Statement
1098 and then Is_Array_Type (Comp_Type)
1099 and then Present (Component_Associations (Expr_Q))
1100 and then Must_Slide (Comp_Type, Etype (Expr_Q))
1102 Set_Expansion_Delayed (Expr_Q, False);
1103 Set_Analyzed (Expr_Q, False);
1109 Expr_Q, Etype (Expr_Q), Indexed_Comp, F));
1114 -- Ada 2005 (AI-287): In case of default initialized component, call
1115 -- the initialization subprogram associated with the component type.
1116 -- If the component type is an access type, add an explicit null
1117 -- assignment, because for the back-end there is an initialization
1118 -- present for the whole aggregate, and no default initialization
1121 -- In addition, if the component type is controlled, we must call
1122 -- its Initialize procedure explicitly, because there is no explicit
1123 -- object creation that will invoke it otherwise.
1126 if Present (Base_Init_Proc (Base_Type (Ctype)))
1127 or else Has_Task (Base_Type (Ctype))
1130 Build_Initialization_Call (Loc,
1131 Id_Ref => Indexed_Comp,
1133 With_Default_Init => True));
1135 elsif Is_Access_Type (Ctype) then
1137 Make_Assignment_Statement (Loc,
1138 Name => Indexed_Comp,
1139 Expression => Make_Null (Loc)));
1142 if Needs_Finalization (Ctype) then
1145 Ref => New_Copy_Tree (Indexed_Comp),
1147 Flist_Ref => Find_Final_List (Current_Scope),
1148 With_Attach => Make_Integer_Literal (Loc, 1)));
1152 -- Now generate the assignment with no associated controlled
1153 -- actions since the target of the assignment may not have been
1154 -- initialized, it is not possible to Finalize it as expected by
1155 -- normal controlled assignment. The rest of the controlled
1156 -- actions are done manually with the proper finalization list
1157 -- coming from the context.
1160 Make_OK_Assignment_Statement (Loc,
1161 Name => Indexed_Comp,
1162 Expression => New_Copy_Tree (Expr));
1164 if Present (Comp_Type) and then Needs_Finalization (Comp_Type) then
1165 Set_No_Ctrl_Actions (A);
1167 -- If this is an aggregate for an array of arrays, each
1168 -- sub-aggregate will be expanded as well, and even with
1169 -- No_Ctrl_Actions the assignments of inner components will
1170 -- require attachment in their assignments to temporaries.
1171 -- These temporaries must be finalized for each subaggregate,
1172 -- to prevent multiple attachments of the same temporary
1173 -- location to same finalization chain (and consequently
1174 -- circular lists). To ensure that finalization takes place
1175 -- for each subaggregate we wrap the assignment in a block.
1177 if Is_Array_Type (Comp_Type)
1178 and then Nkind (Expr) = N_Aggregate
1181 Make_Block_Statement (Loc,
1182 Handled_Statement_Sequence =>
1183 Make_Handled_Sequence_Of_Statements (Loc,
1184 Statements => New_List (A)));
1190 -- Adjust the tag if tagged (because of possible view
1191 -- conversions), unless compiling for the Java VM where
1192 -- tags are implicit.
1194 if Present (Comp_Type)
1195 and then Is_Tagged_Type (Comp_Type)
1196 and then VM_Target = No_VM
1199 Make_OK_Assignment_Statement (Loc,
1201 Make_Selected_Component (Loc,
1202 Prefix => New_Copy_Tree (Indexed_Comp),
1205 (First_Tag_Component (Comp_Type), Loc)),
1208 Unchecked_Convert_To (RTE (RE_Tag),
1210 (Node (First_Elmt (Access_Disp_Table (Comp_Type))),
1216 -- Adjust and attach the component to the proper final list, which
1217 -- can be the controller of the outer record object or the final
1218 -- list associated with the scope.
1220 -- If the component is itself an array of controlled types, whose
1221 -- value is given by a sub-aggregate, then the attach calls have
1222 -- been generated when individual subcomponent are assigned, and
1223 -- must not be done again to prevent malformed finalization chains
1224 -- (see comments above, concerning the creation of a block to hold
1225 -- inner finalization actions).
1227 if Present (Comp_Type)
1228 and then Needs_Finalization (Comp_Type)
1229 and then not Is_Limited_Type (Comp_Type)
1231 (Is_Array_Type (Comp_Type)
1232 and then Is_Controlled (Component_Type (Comp_Type))
1233 and then Nkind (Expr) = N_Aggregate)
1237 Ref => New_Copy_Tree (Indexed_Comp),
1240 With_Attach => Make_Integer_Literal (Loc, 1)));
1244 return Add_Loop_Actions (L);
1251 function Gen_Loop (L, H : Node_Id; Expr : Node_Id) return List_Id is
1255 -- Index_Base'(L) .. Index_Base'(H)
1257 L_Iteration_Scheme : Node_Id;
1258 -- L_J in Index_Base'(L) .. Index_Base'(H)
1261 -- The statements to execute in the loop
1263 S : constant List_Id := New_List;
1264 -- List of statements
1267 -- Copy of expression tree, used for checking purposes
1270 -- If loop bounds define an empty range return the null statement
1272 if Empty_Range (L, H) then
1273 Append_To (S, Make_Null_Statement (Loc));
1275 -- Ada 2005 (AI-287): Nothing else need to be done in case of
1276 -- default initialized component.
1282 -- The expression must be type-checked even though no component
1283 -- of the aggregate will have this value. This is done only for
1284 -- actual components of the array, not for subaggregates. Do
1285 -- the check on a copy, because the expression may be shared
1286 -- among several choices, some of which might be non-null.
1288 if Present (Etype (N))
1289 and then Is_Array_Type (Etype (N))
1290 and then No (Next_Index (Index))
1292 Expander_Mode_Save_And_Set (False);
1293 Tcopy := New_Copy_Tree (Expr);
1294 Set_Parent (Tcopy, N);
1295 Analyze_And_Resolve (Tcopy, Component_Type (Etype (N)));
1296 Expander_Mode_Restore;
1302 -- If loop bounds are the same then generate an assignment
1304 elsif Equal (L, H) then
1305 return Gen_Assign (New_Copy_Tree (L), Expr);
1307 -- If H - L <= 2 then generate a sequence of assignments when we are
1308 -- processing the bottom most aggregate and it contains scalar
1311 elsif No (Next_Index (Index))
1312 and then Scalar_Comp
1313 and then Local_Compile_Time_Known_Value (L)
1314 and then Local_Compile_Time_Known_Value (H)
1315 and then Local_Expr_Value (H) - Local_Expr_Value (L) <= 2
1318 Append_List_To (S, Gen_Assign (New_Copy_Tree (L), Expr));
1319 Append_List_To (S, Gen_Assign (Add (1, To => L), Expr));
1321 if Local_Expr_Value (H) - Local_Expr_Value (L) = 2 then
1322 Append_List_To (S, Gen_Assign (Add (2, To => L), Expr));
1328 -- Otherwise construct the loop, starting with the loop index L_J
1330 L_J := Make_Defining_Identifier (Loc, New_Internal_Name ('J'));
1332 -- Construct "L .. H"
1337 Low_Bound => Make_Qualified_Expression
1339 Subtype_Mark => Index_Base_Name,
1341 High_Bound => Make_Qualified_Expression
1343 Subtype_Mark => Index_Base_Name,
1346 -- Construct "for L_J in Index_Base range L .. H"
1348 L_Iteration_Scheme :=
1349 Make_Iteration_Scheme
1351 Loop_Parameter_Specification =>
1352 Make_Loop_Parameter_Specification
1354 Defining_Identifier => L_J,
1355 Discrete_Subtype_Definition => L_Range));
1357 -- Construct the statements to execute in the loop body
1359 L_Body := Gen_Assign (New_Reference_To (L_J, Loc), Expr);
1361 -- Construct the final loop
1363 Append_To (S, Make_Implicit_Loop_Statement
1365 Identifier => Empty,
1366 Iteration_Scheme => L_Iteration_Scheme,
1367 Statements => L_Body));
1369 -- A small optimization: if the aggregate is initialized with a box
1370 -- and the component type has no initialization procedure, remove the
1371 -- useless empty loop.
1373 if Nkind (First (S)) = N_Loop_Statement
1374 and then Is_Empty_List (Statements (First (S)))
1376 return New_List (Make_Null_Statement (Loc));
1386 -- The code built is
1388 -- W_J : Index_Base := L;
1389 -- while W_J < H loop
1390 -- W_J := Index_Base'Succ (W);
1394 function Gen_While (L, H : Node_Id; Expr : Node_Id) return List_Id is
1398 -- W_J : Base_Type := L;
1400 W_Iteration_Scheme : Node_Id;
1403 W_Index_Succ : Node_Id;
1404 -- Index_Base'Succ (J)
1406 W_Increment : Node_Id;
1407 -- W_J := Index_Base'Succ (W)
1409 W_Body : constant List_Id := New_List;
1410 -- The statements to execute in the loop
1412 S : constant List_Id := New_List;
1413 -- list of statement
1416 -- If loop bounds define an empty range or are equal return null
1418 if Empty_Range (L, H) or else Equal (L, H) then
1419 Append_To (S, Make_Null_Statement (Loc));
1423 -- Build the decl of W_J
1425 W_J := Make_Defining_Identifier (Loc, New_Internal_Name ('J'));
1427 Make_Object_Declaration
1429 Defining_Identifier => W_J,
1430 Object_Definition => Index_Base_Name,
1433 -- Theoretically we should do a New_Copy_Tree (L) here, but we know
1434 -- that in this particular case L is a fresh Expr generated by
1435 -- Add which we are the only ones to use.
1437 Append_To (S, W_Decl);
1439 -- Construct " while W_J < H"
1441 W_Iteration_Scheme :=
1442 Make_Iteration_Scheme
1444 Condition => Make_Op_Lt
1446 Left_Opnd => New_Reference_To (W_J, Loc),
1447 Right_Opnd => New_Copy_Tree (H)));
1449 -- Construct the statements to execute in the loop body
1452 Make_Attribute_Reference
1454 Prefix => Index_Base_Name,
1455 Attribute_Name => Name_Succ,
1456 Expressions => New_List (New_Reference_To (W_J, Loc)));
1459 Make_OK_Assignment_Statement
1461 Name => New_Reference_To (W_J, Loc),
1462 Expression => W_Index_Succ);
1464 Append_To (W_Body, W_Increment);
1465 Append_List_To (W_Body,
1466 Gen_Assign (New_Reference_To (W_J, Loc), Expr));
1468 -- Construct the final loop
1470 Append_To (S, Make_Implicit_Loop_Statement
1472 Identifier => Empty,
1473 Iteration_Scheme => W_Iteration_Scheme,
1474 Statements => W_Body));
1479 ---------------------
1480 -- Index_Base_Name --
1481 ---------------------
1483 function Index_Base_Name return Node_Id is
1485 return New_Reference_To (Index_Base, Sloc (N));
1486 end Index_Base_Name;
1488 ------------------------------------
1489 -- Local_Compile_Time_Known_Value --
1490 ------------------------------------
1492 function Local_Compile_Time_Known_Value (E : Node_Id) return Boolean is
1494 return Compile_Time_Known_Value (E)
1496 (Nkind (E) = N_Attribute_Reference
1497 and then Attribute_Name (E) = Name_Val
1498 and then Compile_Time_Known_Value (First (Expressions (E))));
1499 end Local_Compile_Time_Known_Value;
1501 ----------------------
1502 -- Local_Expr_Value --
1503 ----------------------
1505 function Local_Expr_Value (E : Node_Id) return Uint is
1507 if Compile_Time_Known_Value (E) then
1508 return Expr_Value (E);
1510 return Expr_Value (First (Expressions (E)));
1512 end Local_Expr_Value;
1514 -- Build_Array_Aggr_Code Variables
1521 Others_Expr : Node_Id := Empty;
1522 Others_Box_Present : Boolean := False;
1524 Aggr_L : constant Node_Id := Low_Bound (Aggregate_Bounds (N));
1525 Aggr_H : constant Node_Id := High_Bound (Aggregate_Bounds (N));
1526 -- The aggregate bounds of this specific sub-aggregate. Note that if
1527 -- the code generated by Build_Array_Aggr_Code is executed then these
1528 -- bounds are OK. Otherwise a Constraint_Error would have been raised.
1530 Aggr_Low : constant Node_Id := Duplicate_Subexpr_No_Checks (Aggr_L);
1531 Aggr_High : constant Node_Id := Duplicate_Subexpr_No_Checks (Aggr_H);
1532 -- After Duplicate_Subexpr these are side-effect free
1537 Nb_Choices : Nat := 0;
1538 Table : Case_Table_Type (1 .. Number_Of_Choices (N));
1539 -- Used to sort all the different choice values
1542 -- Number of elements in the positional aggregate
1544 New_Code : constant List_Id := New_List;
1546 -- Start of processing for Build_Array_Aggr_Code
1549 -- First before we start, a special case. if we have a bit packed
1550 -- array represented as a modular type, then clear the value to
1551 -- zero first, to ensure that unused bits are properly cleared.
1556 and then Is_Bit_Packed_Array (Typ)
1557 and then Is_Modular_Integer_Type (Packed_Array_Type (Typ))
1559 Append_To (New_Code,
1560 Make_Assignment_Statement (Loc,
1561 Name => New_Copy_Tree (Into),
1563 Unchecked_Convert_To (Typ,
1564 Make_Integer_Literal (Loc, Uint_0))));
1567 -- If the component type contains tasks, we need to build a Master
1568 -- entity in the current scope, because it will be needed if build-
1569 -- in-place functions are called in the expanded code.
1571 if Nkind (Parent (N)) = N_Object_Declaration
1572 and then Has_Task (Typ)
1574 Build_Master_Entity (Defining_Identifier (Parent (N)));
1577 -- STEP 1: Process component associations
1579 -- For those associations that may generate a loop, initialize
1580 -- Loop_Actions to collect inserted actions that may be crated.
1582 -- Skip this if no component associations
1584 if No (Expressions (N)) then
1586 -- STEP 1 (a): Sort the discrete choices
1588 Assoc := First (Component_Associations (N));
1589 while Present (Assoc) loop
1590 Choice := First (Choices (Assoc));
1591 while Present (Choice) loop
1592 if Nkind (Choice) = N_Others_Choice then
1593 Set_Loop_Actions (Assoc, New_List);
1595 if Box_Present (Assoc) then
1596 Others_Box_Present := True;
1598 Others_Expr := Expression (Assoc);
1603 Get_Index_Bounds (Choice, Low, High);
1606 Set_Loop_Actions (Assoc, New_List);
1609 Nb_Choices := Nb_Choices + 1;
1610 if Box_Present (Assoc) then
1611 Table (Nb_Choices) := (Choice_Lo => Low,
1613 Choice_Node => Empty);
1615 Table (Nb_Choices) := (Choice_Lo => Low,
1617 Choice_Node => Expression (Assoc));
1625 -- If there is more than one set of choices these must be static
1626 -- and we can therefore sort them. Remember that Nb_Choices does not
1627 -- account for an others choice.
1629 if Nb_Choices > 1 then
1630 Sort_Case_Table (Table);
1633 -- STEP 1 (b): take care of the whole set of discrete choices
1635 for J in 1 .. Nb_Choices loop
1636 Low := Table (J).Choice_Lo;
1637 High := Table (J).Choice_Hi;
1638 Expr := Table (J).Choice_Node;
1639 Append_List (Gen_Loop (Low, High, Expr), To => New_Code);
1642 -- STEP 1 (c): generate the remaining loops to cover others choice
1643 -- We don't need to generate loops over empty gaps, but if there is
1644 -- a single empty range we must analyze the expression for semantics
1646 if Present (Others_Expr) or else Others_Box_Present then
1648 First : Boolean := True;
1651 for J in 0 .. Nb_Choices loop
1655 Low := Add (1, To => Table (J).Choice_Hi);
1658 if J = Nb_Choices then
1661 High := Add (-1, To => Table (J + 1).Choice_Lo);
1664 -- If this is an expansion within an init proc, make
1665 -- sure that discriminant references are replaced by
1666 -- the corresponding discriminal.
1668 if Inside_Init_Proc then
1669 if Is_Entity_Name (Low)
1670 and then Ekind (Entity (Low)) = E_Discriminant
1672 Set_Entity (Low, Discriminal (Entity (Low)));
1675 if Is_Entity_Name (High)
1676 and then Ekind (Entity (High)) = E_Discriminant
1678 Set_Entity (High, Discriminal (Entity (High)));
1683 or else not Empty_Range (Low, High)
1687 (Gen_Loop (Low, High, Others_Expr), To => New_Code);
1693 -- STEP 2: Process positional components
1696 -- STEP 2 (a): Generate the assignments for each positional element
1697 -- Note that here we have to use Aggr_L rather than Aggr_Low because
1698 -- Aggr_L is analyzed and Add wants an analyzed expression.
1700 Expr := First (Expressions (N));
1702 while Present (Expr) loop
1703 Nb_Elements := Nb_Elements + 1;
1704 Append_List (Gen_Assign (Add (Nb_Elements, To => Aggr_L), Expr),
1709 -- STEP 2 (b): Generate final loop if an others choice is present
1710 -- Here Nb_Elements gives the offset of the last positional element.
1712 if Present (Component_Associations (N)) then
1713 Assoc := Last (Component_Associations (N));
1715 -- Ada 2005 (AI-287)
1717 if Box_Present (Assoc) then
1718 Append_List (Gen_While (Add (Nb_Elements, To => Aggr_L),
1723 Expr := Expression (Assoc);
1725 Append_List (Gen_While (Add (Nb_Elements, To => Aggr_L),
1734 end Build_Array_Aggr_Code;
1736 ----------------------------
1737 -- Build_Record_Aggr_Code --
1738 ----------------------------
1740 function Build_Record_Aggr_Code
1744 Flist : Node_Id := Empty;
1745 Obj : Entity_Id := Empty;
1746 Is_Limited_Ancestor_Expansion : Boolean := False) return List_Id
1748 Loc : constant Source_Ptr := Sloc (N);
1749 L : constant List_Id := New_List;
1750 N_Typ : constant Entity_Id := Etype (N);
1757 Comp_Type : Entity_Id;
1758 Selector : Entity_Id;
1759 Comp_Expr : Node_Id;
1762 Internal_Final_List : Node_Id := Empty;
1764 -- If this is an internal aggregate, the External_Final_List is an
1765 -- expression for the controller record of the enclosing type.
1767 -- If the current aggregate has several controlled components, this
1768 -- expression will appear in several calls to attach to the finali-
1769 -- zation list, and it must not be shared.
1771 External_Final_List : Node_Id;
1772 Ancestor_Is_Expression : Boolean := False;
1773 Ancestor_Is_Subtype_Mark : Boolean := False;
1775 Init_Typ : Entity_Id := Empty;
1778 Ctrl_Stuff_Done : Boolean := False;
1779 -- True if Gen_Ctrl_Actions_For_Aggr has already been called; calls
1780 -- after the first do nothing.
1782 function Ancestor_Discriminant_Value (Disc : Entity_Id) return Node_Id;
1783 -- Returns the value that the given discriminant of an ancestor type
1784 -- should receive (in the absence of a conflict with the value provided
1785 -- by an ancestor part of an extension aggregate).
1787 procedure Check_Ancestor_Discriminants (Anc_Typ : Entity_Id);
1788 -- Check that each of the discriminant values defined by the ancestor
1789 -- part of an extension aggregate match the corresponding values
1790 -- provided by either an association of the aggregate or by the
1791 -- constraint imposed by a parent type (RM95-4.3.2(8)).
1793 function Compatible_Int_Bounds
1794 (Agg_Bounds : Node_Id;
1795 Typ_Bounds : Node_Id) return Boolean;
1796 -- Return true if Agg_Bounds are equal or within Typ_Bounds. It is
1797 -- assumed that both bounds are integer ranges.
1799 procedure Gen_Ctrl_Actions_For_Aggr;
1800 -- Deal with the various controlled type data structure initializations
1801 -- (but only if it hasn't been done already).
1803 function Get_Constraint_Association (T : Entity_Id) return Node_Id;
1804 -- Returns the first discriminant association in the constraint
1805 -- associated with T, if any, otherwise returns Empty.
1807 function Init_Controller
1812 Init_Pr : Boolean) return List_Id;
1813 -- Returns the list of statements necessary to initialize the internal
1814 -- controller of the (possible) ancestor typ into target and attach it
1815 -- to finalization list F. Init_Pr conditions the call to the init proc
1816 -- since it may already be done due to ancestor initialization.
1818 function Is_Int_Range_Bounds (Bounds : Node_Id) return Boolean;
1819 -- Check whether Bounds is a range node and its lower and higher bounds
1820 -- are integers literals.
1822 ---------------------------------
1823 -- Ancestor_Discriminant_Value --
1824 ---------------------------------
1826 function Ancestor_Discriminant_Value (Disc : Entity_Id) return Node_Id is
1828 Assoc_Elmt : Elmt_Id;
1829 Aggr_Comp : Entity_Id;
1830 Corresp_Disc : Entity_Id;
1831 Current_Typ : Entity_Id := Base_Type (Typ);
1832 Parent_Typ : Entity_Id;
1833 Parent_Disc : Entity_Id;
1834 Save_Assoc : Node_Id := Empty;
1837 -- First check any discriminant associations to see if any of them
1838 -- provide a value for the discriminant.
1840 if Present (Discriminant_Specifications (Parent (Current_Typ))) then
1841 Assoc := First (Component_Associations (N));
1842 while Present (Assoc) loop
1843 Aggr_Comp := Entity (First (Choices (Assoc)));
1845 if Ekind (Aggr_Comp) = E_Discriminant then
1846 Save_Assoc := Expression (Assoc);
1848 Corresp_Disc := Corresponding_Discriminant (Aggr_Comp);
1849 while Present (Corresp_Disc) loop
1851 -- If found a corresponding discriminant then return the
1852 -- value given in the aggregate. (Note: this is not
1853 -- correct in the presence of side effects. ???)
1855 if Disc = Corresp_Disc then
1856 return Duplicate_Subexpr (Expression (Assoc));
1860 Corresponding_Discriminant (Corresp_Disc);
1868 -- No match found in aggregate, so chain up parent types to find
1869 -- a constraint that defines the value of the discriminant.
1871 Parent_Typ := Etype (Current_Typ);
1872 while Current_Typ /= Parent_Typ loop
1873 if Has_Discriminants (Parent_Typ)
1874 and then not Has_Unknown_Discriminants (Parent_Typ)
1876 Parent_Disc := First_Discriminant (Parent_Typ);
1878 -- We either get the association from the subtype indication
1879 -- of the type definition itself, or from the discriminant
1880 -- constraint associated with the type entity (which is
1881 -- preferable, but it's not always present ???)
1883 if Is_Empty_Elmt_List (
1884 Discriminant_Constraint (Current_Typ))
1886 Assoc := Get_Constraint_Association (Current_Typ);
1887 Assoc_Elmt := No_Elmt;
1890 First_Elmt (Discriminant_Constraint (Current_Typ));
1891 Assoc := Node (Assoc_Elmt);
1894 -- Traverse the discriminants of the parent type looking
1895 -- for one that corresponds.
1897 while Present (Parent_Disc) and then Present (Assoc) loop
1898 Corresp_Disc := Parent_Disc;
1899 while Present (Corresp_Disc)
1900 and then Disc /= Corresp_Disc
1903 Corresponding_Discriminant (Corresp_Disc);
1906 if Disc = Corresp_Disc then
1907 if Nkind (Assoc) = N_Discriminant_Association then
1908 Assoc := Expression (Assoc);
1911 -- If the located association directly denotes a
1912 -- discriminant, then use the value of a saved
1913 -- association of the aggregate. This is a kludge to
1914 -- handle certain cases involving multiple discriminants
1915 -- mapped to a single discriminant of a descendant. It's
1916 -- not clear how to locate the appropriate discriminant
1917 -- value for such cases. ???
1919 if Is_Entity_Name (Assoc)
1920 and then Ekind (Entity (Assoc)) = E_Discriminant
1922 Assoc := Save_Assoc;
1925 return Duplicate_Subexpr (Assoc);
1928 Next_Discriminant (Parent_Disc);
1930 if No (Assoc_Elmt) then
1933 Next_Elmt (Assoc_Elmt);
1934 if Present (Assoc_Elmt) then
1935 Assoc := Node (Assoc_Elmt);
1943 Current_Typ := Parent_Typ;
1944 Parent_Typ := Etype (Current_Typ);
1947 -- In some cases there's no ancestor value to locate (such as
1948 -- when an ancestor part given by an expression defines the
1949 -- discriminant value).
1952 end Ancestor_Discriminant_Value;
1954 ----------------------------------
1955 -- Check_Ancestor_Discriminants --
1956 ----------------------------------
1958 procedure Check_Ancestor_Discriminants (Anc_Typ : Entity_Id) is
1960 Disc_Value : Node_Id;
1964 Discr := First_Discriminant (Base_Type (Anc_Typ));
1965 while Present (Discr) loop
1966 Disc_Value := Ancestor_Discriminant_Value (Discr);
1968 if Present (Disc_Value) then
1969 Cond := Make_Op_Ne (Loc,
1971 Make_Selected_Component (Loc,
1972 Prefix => New_Copy_Tree (Target),
1973 Selector_Name => New_Occurrence_Of (Discr, Loc)),
1974 Right_Opnd => Disc_Value);
1977 Make_Raise_Constraint_Error (Loc,
1979 Reason => CE_Discriminant_Check_Failed));
1982 Next_Discriminant (Discr);
1984 end Check_Ancestor_Discriminants;
1986 ---------------------------
1987 -- Compatible_Int_Bounds --
1988 ---------------------------
1990 function Compatible_Int_Bounds
1991 (Agg_Bounds : Node_Id;
1992 Typ_Bounds : Node_Id) return Boolean
1994 Agg_Lo : constant Uint := Intval (Low_Bound (Agg_Bounds));
1995 Agg_Hi : constant Uint := Intval (High_Bound (Agg_Bounds));
1996 Typ_Lo : constant Uint := Intval (Low_Bound (Typ_Bounds));
1997 Typ_Hi : constant Uint := Intval (High_Bound (Typ_Bounds));
1999 return Typ_Lo <= Agg_Lo and then Agg_Hi <= Typ_Hi;
2000 end Compatible_Int_Bounds;
2002 --------------------------------
2003 -- Get_Constraint_Association --
2004 --------------------------------
2006 function Get_Constraint_Association (T : Entity_Id) return Node_Id is
2007 Typ_Def : constant Node_Id := Type_Definition (Parent (T));
2008 Indic : constant Node_Id := Subtype_Indication (Typ_Def);
2011 -- ??? Also need to cover case of a type mark denoting a subtype
2014 if Nkind (Indic) = N_Subtype_Indication
2015 and then Present (Constraint (Indic))
2017 return First (Constraints (Constraint (Indic)));
2021 end Get_Constraint_Association;
2023 ---------------------
2024 -- Init_Controller --
2025 ---------------------
2027 function Init_Controller
2032 Init_Pr : Boolean) return List_Id
2034 L : constant List_Id := New_List;
2037 Target_Type : Entity_Id;
2041 -- init-proc (target._controller);
2042 -- initialize (target._controller);
2043 -- Attach_to_Final_List (target._controller, F);
2046 Make_Selected_Component (Loc,
2047 Prefix => Convert_To (Typ, New_Copy_Tree (Target)),
2048 Selector_Name => Make_Identifier (Loc, Name_uController));
2049 Set_Assignment_OK (Ref);
2051 -- Ada 2005 (AI-287): Give support to aggregates of limited types.
2052 -- If the type is intrinsically limited the controller is limited as
2053 -- well. If it is tagged and limited then so is the controller.
2054 -- Otherwise an untagged type may have limited components without its
2055 -- full view being limited, so the controller is not limited.
2057 if Nkind (Target) = N_Identifier then
2058 Target_Type := Etype (Target);
2060 elsif Nkind (Target) = N_Selected_Component then
2061 Target_Type := Etype (Selector_Name (Target));
2063 elsif Nkind (Target) = N_Unchecked_Type_Conversion then
2064 Target_Type := Etype (Target);
2066 elsif Nkind (Target) = N_Unchecked_Expression
2067 and then Nkind (Expression (Target)) = N_Indexed_Component
2069 Target_Type := Etype (Prefix (Expression (Target)));
2072 Target_Type := Etype (Target);
2075 -- If the target has not been analyzed yet, as will happen with
2076 -- delayed expansion, use the given type (either the aggregate type
2077 -- or an ancestor) to determine limitedness.
2079 if No (Target_Type) then
2083 if (Is_Tagged_Type (Target_Type))
2084 and then Is_Limited_Type (Target_Type)
2086 RC := RE_Limited_Record_Controller;
2088 elsif Is_Inherently_Limited_Type (Target_Type) then
2089 RC := RE_Limited_Record_Controller;
2092 RC := RE_Record_Controller;
2097 Build_Initialization_Call (Loc,
2100 In_Init_Proc => Within_Init_Proc));
2104 Make_Procedure_Call_Statement (Loc,
2107 Find_Prim_Op (RTE (RC), Name_Initialize), Loc),
2108 Parameter_Associations =>
2109 New_List (New_Copy_Tree (Ref))));
2113 Obj_Ref => New_Copy_Tree (Ref),
2115 With_Attach => Attach));
2118 end Init_Controller;
2120 -------------------------
2121 -- Is_Int_Range_Bounds --
2122 -------------------------
2124 function Is_Int_Range_Bounds (Bounds : Node_Id) return Boolean is
2126 return Nkind (Bounds) = N_Range
2127 and then Nkind (Low_Bound (Bounds)) = N_Integer_Literal
2128 and then Nkind (High_Bound (Bounds)) = N_Integer_Literal;
2129 end Is_Int_Range_Bounds;
2131 -------------------------------
2132 -- Gen_Ctrl_Actions_For_Aggr --
2133 -------------------------------
2135 procedure Gen_Ctrl_Actions_For_Aggr is
2136 Alloc : Node_Id := Empty;
2139 -- Do the work only the first time this is called
2141 if Ctrl_Stuff_Done then
2145 Ctrl_Stuff_Done := True;
2148 and then Finalize_Storage_Only (Typ)
2150 (Is_Library_Level_Entity (Obj)
2151 or else Entity (Constant_Value (RTE (RE_Garbage_Collected))) =
2154 -- why not Is_True (Expr_Value (RTE (RE_Garbaage_Collected) ???
2156 Attach := Make_Integer_Literal (Loc, 0);
2158 elsif Nkind (Parent (N)) = N_Qualified_Expression
2159 and then Nkind (Parent (Parent (N))) = N_Allocator
2161 Alloc := Parent (Parent (N));
2162 Attach := Make_Integer_Literal (Loc, 2);
2165 Attach := Make_Integer_Literal (Loc, 1);
2168 -- Determine the external finalization list. It is either the
2169 -- finalization list of the outer-scope or the one coming from
2170 -- an outer aggregate. When the target is not a temporary, the
2171 -- proper scope is the scope of the target rather than the
2172 -- potentially transient current scope.
2174 if Needs_Finalization (Typ) then
2176 -- The current aggregate belongs to an allocator which creates
2177 -- an object through an anonymous access type or acts as the root
2178 -- of a coextension chain.
2182 (Is_Coextension_Root (Alloc)
2183 or else Ekind (Etype (Alloc)) = E_Anonymous_Access_Type)
2185 if No (Associated_Final_Chain (Etype (Alloc))) then
2186 Build_Final_List (Alloc, Etype (Alloc));
2189 External_Final_List :=
2190 Make_Selected_Component (Loc,
2193 Associated_Final_Chain (Etype (Alloc)), Loc),
2195 Make_Identifier (Loc, Name_F));
2197 elsif Present (Flist) then
2198 External_Final_List := New_Copy_Tree (Flist);
2200 elsif Is_Entity_Name (Target)
2201 and then Present (Scope (Entity (Target)))
2203 External_Final_List :=
2204 Find_Final_List (Scope (Entity (Target)));
2207 External_Final_List := Find_Final_List (Current_Scope);
2210 External_Final_List := Empty;
2213 -- Initialize and attach the outer object in the is_controlled case
2215 if Is_Controlled (Typ) then
2216 if Ancestor_Is_Subtype_Mark then
2217 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
2218 Set_Assignment_OK (Ref);
2220 Make_Procedure_Call_Statement (Loc,
2223 (Find_Prim_Op (Init_Typ, Name_Initialize), Loc),
2224 Parameter_Associations => New_List (New_Copy_Tree (Ref))));
2227 if not Has_Controlled_Component (Typ) then
2228 Ref := New_Copy_Tree (Target);
2229 Set_Assignment_OK (Ref);
2231 -- This is an aggregate of a coextension. Do not produce a
2232 -- finalization call, but rather attach the reference of the
2233 -- aggregate to its coextension chain.
2236 and then Is_Dynamic_Coextension (Alloc)
2238 if No (Coextensions (Alloc)) then
2239 Set_Coextensions (Alloc, New_Elmt_List);
2242 Append_Elmt (Ref, Coextensions (Alloc));
2247 Flist_Ref => New_Copy_Tree (External_Final_List),
2248 With_Attach => Attach));
2253 -- In the Has_Controlled component case, all the intermediate
2254 -- controllers must be initialized.
2256 if Has_Controlled_Component (Typ)
2257 and not Is_Limited_Ancestor_Expansion
2260 Inner_Typ : Entity_Id;
2261 Outer_Typ : Entity_Id;
2265 -- Find outer type with a controller
2267 Outer_Typ := Base_Type (Typ);
2268 while Outer_Typ /= Init_Typ
2269 and then not Has_New_Controlled_Component (Outer_Typ)
2271 Outer_Typ := Etype (Outer_Typ);
2274 -- Attach it to the outer record controller to the external
2277 if Outer_Typ = Init_Typ then
2282 F => External_Final_List,
2287 Inner_Typ := Init_Typ;
2294 F => External_Final_List,
2298 Inner_Typ := Etype (Outer_Typ);
2300 not Is_Tagged_Type (Typ) or else Inner_Typ = Outer_Typ;
2303 -- The outer object has to be attached as well
2305 if Is_Controlled (Typ) then
2306 Ref := New_Copy_Tree (Target);
2307 Set_Assignment_OK (Ref);
2311 Flist_Ref => New_Copy_Tree (External_Final_List),
2312 With_Attach => New_Copy_Tree (Attach)));
2315 -- Initialize the internal controllers for tagged types with
2316 -- more than one controller.
2318 while not At_Root and then Inner_Typ /= Init_Typ loop
2319 if Has_New_Controlled_Component (Inner_Typ) then
2321 Make_Selected_Component (Loc,
2323 Convert_To (Outer_Typ, New_Copy_Tree (Target)),
2325 Make_Identifier (Loc, Name_uController));
2327 Make_Selected_Component (Loc,
2329 Selector_Name => Make_Identifier (Loc, Name_F));
2336 Attach => Make_Integer_Literal (Loc, 1),
2338 Outer_Typ := Inner_Typ;
2343 At_Root := Inner_Typ = Etype (Inner_Typ);
2344 Inner_Typ := Etype (Inner_Typ);
2347 -- If not done yet attach the controller of the ancestor part
2349 if Outer_Typ /= Init_Typ
2350 and then Inner_Typ = Init_Typ
2351 and then Has_Controlled_Component (Init_Typ)
2354 Make_Selected_Component (Loc,
2355 Prefix => Convert_To (Outer_Typ, New_Copy_Tree (Target)),
2357 Make_Identifier (Loc, Name_uController));
2359 Make_Selected_Component (Loc,
2361 Selector_Name => Make_Identifier (Loc, Name_F));
2363 Attach := Make_Integer_Literal (Loc, 1);
2372 -- Note: Init_Pr is False because the ancestor part has
2373 -- already been initialized either way (by default, if
2374 -- given by a type name, otherwise from the expression).
2379 end Gen_Ctrl_Actions_For_Aggr;
2381 function Replace_Type (Expr : Node_Id) return Traverse_Result;
2382 -- If the aggregate contains a self-reference, traverse each expression
2383 -- to replace a possible self-reference with a reference to the proper
2384 -- component of the target of the assignment.
2390 function Replace_Type (Expr : Node_Id) return Traverse_Result is
2392 -- Note regarding the Root_Type test below: Aggregate components for
2393 -- self-referential types include attribute references to the current
2394 -- instance, of the form: Typ'access, etc.. These references are
2395 -- rewritten as references to the target of the aggregate: the
2396 -- left-hand side of an assignment, the entity in a declaration,
2397 -- or a temporary. Without this test, we would improperly extended
2398 -- this rewriting to attribute references whose prefix was not the
2399 -- type of the aggregate.
2401 if Nkind (Expr) = N_Attribute_Reference
2402 and then Is_Entity_Name (Prefix (Expr))
2403 and then Is_Type (Entity (Prefix (Expr)))
2404 and then Root_Type (Etype (N)) = Root_Type (Entity (Prefix (Expr)))
2406 if Is_Entity_Name (Lhs) then
2407 Rewrite (Prefix (Expr),
2408 New_Occurrence_Of (Entity (Lhs), Loc));
2410 elsif Nkind (Lhs) = N_Selected_Component then
2412 Make_Attribute_Reference (Loc,
2413 Attribute_Name => Name_Unrestricted_Access,
2414 Prefix => New_Copy_Tree (Prefix (Lhs))));
2415 Set_Analyzed (Parent (Expr), False);
2419 Make_Attribute_Reference (Loc,
2420 Attribute_Name => Name_Unrestricted_Access,
2421 Prefix => New_Copy_Tree (Lhs)));
2422 Set_Analyzed (Parent (Expr), False);
2429 procedure Replace_Self_Reference is
2430 new Traverse_Proc (Replace_Type);
2432 -- Start of processing for Build_Record_Aggr_Code
2435 if Has_Self_Reference (N) then
2436 Replace_Self_Reference (N);
2439 -- If the target of the aggregate is class-wide, we must convert it
2440 -- to the actual type of the aggregate, so that the proper components
2441 -- are visible. We know already that the types are compatible.
2443 if Present (Etype (Lhs))
2444 and then Is_Class_Wide_Type (Etype (Lhs))
2446 Target := Unchecked_Convert_To (Typ, Lhs);
2451 -- Deal with the ancestor part of extension aggregates or with the
2452 -- discriminants of the root type.
2454 if Nkind (N) = N_Extension_Aggregate then
2456 A : constant Node_Id := Ancestor_Part (N);
2460 -- If the ancestor part is a subtype mark "T", we generate
2462 -- init-proc (T(tmp)); if T is constrained and
2463 -- init-proc (S(tmp)); where S applies an appropriate
2464 -- constraint if T is unconstrained
2466 if Is_Entity_Name (A) and then Is_Type (Entity (A)) then
2467 Ancestor_Is_Subtype_Mark := True;
2469 if Is_Constrained (Entity (A)) then
2470 Init_Typ := Entity (A);
2472 -- For an ancestor part given by an unconstrained type mark,
2473 -- create a subtype constrained by appropriate corresponding
2474 -- discriminant values coming from either associations of the
2475 -- aggregate or a constraint on a parent type. The subtype will
2476 -- be used to generate the correct default value for the
2479 elsif Has_Discriminants (Entity (A)) then
2481 Anc_Typ : constant Entity_Id := Entity (A);
2482 Anc_Constr : constant List_Id := New_List;
2483 Discrim : Entity_Id;
2484 Disc_Value : Node_Id;
2485 New_Indic : Node_Id;
2486 Subt_Decl : Node_Id;
2489 Discrim := First_Discriminant (Anc_Typ);
2490 while Present (Discrim) loop
2491 Disc_Value := Ancestor_Discriminant_Value (Discrim);
2492 Append_To (Anc_Constr, Disc_Value);
2493 Next_Discriminant (Discrim);
2497 Make_Subtype_Indication (Loc,
2498 Subtype_Mark => New_Occurrence_Of (Anc_Typ, Loc),
2500 Make_Index_Or_Discriminant_Constraint (Loc,
2501 Constraints => Anc_Constr));
2503 Init_Typ := Create_Itype (Ekind (Anc_Typ), N);
2506 Make_Subtype_Declaration (Loc,
2507 Defining_Identifier => Init_Typ,
2508 Subtype_Indication => New_Indic);
2510 -- Itypes must be analyzed with checks off Declaration
2511 -- must have a parent for proper handling of subsidiary
2514 Set_Parent (Subt_Decl, N);
2515 Analyze (Subt_Decl, Suppress => All_Checks);
2519 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
2520 Set_Assignment_OK (Ref);
2522 if Has_Default_Init_Comps (N)
2523 or else Has_Task (Base_Type (Init_Typ))
2526 Build_Initialization_Call (Loc,
2529 In_Init_Proc => Within_Init_Proc,
2530 With_Default_Init => True));
2533 Build_Initialization_Call (Loc,
2536 In_Init_Proc => Within_Init_Proc));
2539 if Is_Constrained (Entity (A))
2540 and then Has_Discriminants (Entity (A))
2542 Check_Ancestor_Discriminants (Entity (A));
2545 -- Ada 2005 (AI-287): If the ancestor part is an aggregate of
2546 -- limited type, a recursive call expands the ancestor. Note that
2547 -- in the limited case, the ancestor part must be either a
2548 -- function call (possibly qualified, or wrapped in an unchecked
2549 -- conversion) or aggregate (definitely qualified).
2550 -- The ancestor part can also be a function call (that may be
2551 -- transformed into an explicit dereference) or a qualification
2554 elsif Is_Limited_Type (Etype (A))
2555 and then Nkind_In (Unqualify (A), N_Aggregate,
2556 N_Extension_Aggregate)
2558 Ancestor_Is_Expression := True;
2560 -- Set up finalization data for enclosing record, because
2561 -- controlled subcomponents of the ancestor part will be
2564 Gen_Ctrl_Actions_For_Aggr;
2567 Build_Record_Aggr_Code (
2569 Typ => Etype (Unqualify (A)),
2573 Is_Limited_Ancestor_Expansion => True));
2575 -- If the ancestor part is an expression "E", we generate
2579 -- In Ada 2005, this includes the case of a (possibly qualified)
2580 -- limited function call. The assignment will turn into a
2581 -- build-in-place function call (for further details, see
2582 -- Make_Build_In_Place_Call_In_Assignment).
2585 Ancestor_Is_Expression := True;
2586 Init_Typ := Etype (A);
2588 -- If the ancestor part is an aggregate, force its full
2589 -- expansion, which was delayed.
2591 if Nkind_In (Unqualify (A), N_Aggregate,
2592 N_Extension_Aggregate)
2594 Set_Analyzed (A, False);
2595 Set_Analyzed (Expression (A), False);
2598 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
2599 Set_Assignment_OK (Ref);
2601 -- Make the assignment without usual controlled actions since
2602 -- we only want the post adjust but not the pre finalize here
2603 -- Add manual adjust when necessary.
2605 Assign := New_List (
2606 Make_OK_Assignment_Statement (Loc,
2609 Set_No_Ctrl_Actions (First (Assign));
2611 -- Assign the tag now to make sure that the dispatching call in
2612 -- the subsequent deep_adjust works properly (unless VM_Target,
2613 -- where tags are implicit).
2615 if VM_Target = No_VM then
2617 Make_OK_Assignment_Statement (Loc,
2619 Make_Selected_Component (Loc,
2620 Prefix => New_Copy_Tree (Target),
2623 (First_Tag_Component (Base_Type (Typ)), Loc)),
2626 Unchecked_Convert_To (RTE (RE_Tag),
2629 (Access_Disp_Table (Base_Type (Typ)))),
2632 Set_Assignment_OK (Name (Instr));
2633 Append_To (Assign, Instr);
2635 -- Ada 2005 (AI-251): If tagged type has progenitors we must
2636 -- also initialize tags of the secondary dispatch tables.
2638 if Has_Interfaces (Base_Type (Typ)) then
2640 (Typ => Base_Type (Typ),
2642 Stmts_List => Assign);
2646 -- Call Adjust manually
2648 if Needs_Finalization (Etype (A))
2649 and then not Is_Limited_Type (Etype (A))
2651 Append_List_To (Assign,
2653 Ref => New_Copy_Tree (Ref),
2655 Flist_Ref => New_Reference_To (
2656 RTE (RE_Global_Final_List), Loc),
2657 With_Attach => Make_Integer_Literal (Loc, 0)));
2661 Make_Unsuppress_Block (Loc, Name_Discriminant_Check, Assign));
2663 if Has_Discriminants (Init_Typ) then
2664 Check_Ancestor_Discriminants (Init_Typ);
2669 -- Normal case (not an extension aggregate)
2672 -- Generate the discriminant expressions, component by component.
2673 -- If the base type is an unchecked union, the discriminants are
2674 -- unknown to the back-end and absent from a value of the type, so
2675 -- assignments for them are not emitted.
2677 if Has_Discriminants (Typ)
2678 and then not Is_Unchecked_Union (Base_Type (Typ))
2680 -- If the type is derived, and constrains discriminants of the
2681 -- parent type, these discriminants are not components of the
2682 -- aggregate, and must be initialized explicitly. They are not
2683 -- visible components of the object, but can become visible with
2684 -- a view conversion to the ancestor.
2688 Parent_Type : Entity_Id;
2690 Discr_Val : Elmt_Id;
2693 Btype := Base_Type (Typ);
2694 while Is_Derived_Type (Btype)
2695 and then Present (Stored_Constraint (Btype))
2697 Parent_Type := Etype (Btype);
2699 Disc := First_Discriminant (Parent_Type);
2701 First_Elmt (Stored_Constraint (Base_Type (Typ)));
2702 while Present (Discr_Val) loop
2704 -- Only those discriminants of the parent that are not
2705 -- renamed by discriminants of the derived type need to
2706 -- be added explicitly.
2708 if not Is_Entity_Name (Node (Discr_Val))
2710 Ekind (Entity (Node (Discr_Val))) /= E_Discriminant
2713 Make_Selected_Component (Loc,
2714 Prefix => New_Copy_Tree (Target),
2715 Selector_Name => New_Occurrence_Of (Disc, Loc));
2718 Make_OK_Assignment_Statement (Loc,
2720 Expression => New_Copy_Tree (Node (Discr_Val)));
2722 Set_No_Ctrl_Actions (Instr);
2723 Append_To (L, Instr);
2726 Next_Discriminant (Disc);
2727 Next_Elmt (Discr_Val);
2730 Btype := Base_Type (Parent_Type);
2734 -- Generate discriminant init values for the visible discriminants
2737 Discriminant : Entity_Id;
2738 Discriminant_Value : Node_Id;
2741 Discriminant := First_Stored_Discriminant (Typ);
2742 while Present (Discriminant) loop
2744 Make_Selected_Component (Loc,
2745 Prefix => New_Copy_Tree (Target),
2746 Selector_Name => New_Occurrence_Of (Discriminant, Loc));
2748 Discriminant_Value :=
2749 Get_Discriminant_Value (
2752 Discriminant_Constraint (N_Typ));
2755 Make_OK_Assignment_Statement (Loc,
2757 Expression => New_Copy_Tree (Discriminant_Value));
2759 Set_No_Ctrl_Actions (Instr);
2760 Append_To (L, Instr);
2762 Next_Stored_Discriminant (Discriminant);
2768 -- Generate the assignments, component by component
2770 -- tmp.comp1 := Expr1_From_Aggr;
2771 -- tmp.comp2 := Expr2_From_Aggr;
2774 Comp := First (Component_Associations (N));
2775 while Present (Comp) loop
2776 Selector := Entity (First (Choices (Comp)));
2778 -- Ada 2005 (AI-287): For each default-initialized component generate
2779 -- a call to the corresponding IP subprogram if available.
2781 if Box_Present (Comp)
2782 and then Has_Non_Null_Base_Init_Proc (Etype (Selector))
2784 if Ekind (Selector) /= E_Discriminant then
2785 Gen_Ctrl_Actions_For_Aggr;
2788 -- Ada 2005 (AI-287): If the component type has tasks then
2789 -- generate the activation chain and master entities (except
2790 -- in case of an allocator because in that case these entities
2791 -- are generated by Build_Task_Allocate_Block_With_Init_Stmts).
2794 Ctype : constant Entity_Id := Etype (Selector);
2795 Inside_Allocator : Boolean := False;
2796 P : Node_Id := Parent (N);
2799 if Is_Task_Type (Ctype) or else Has_Task (Ctype) then
2800 while Present (P) loop
2801 if Nkind (P) = N_Allocator then
2802 Inside_Allocator := True;
2809 if not Inside_Init_Proc and not Inside_Allocator then
2810 Build_Activation_Chain_Entity (N);
2816 Build_Initialization_Call (Loc,
2817 Id_Ref => Make_Selected_Component (Loc,
2818 Prefix => New_Copy_Tree (Target),
2819 Selector_Name => New_Occurrence_Of (Selector,
2821 Typ => Etype (Selector),
2823 With_Default_Init => True));
2828 -- Prepare for component assignment
2830 if Ekind (Selector) /= E_Discriminant
2831 or else Nkind (N) = N_Extension_Aggregate
2833 -- All the discriminants have now been assigned
2835 -- This is now a good moment to initialize and attach all the
2836 -- controllers. Their position may depend on the discriminants.
2838 if Ekind (Selector) /= E_Discriminant then
2839 Gen_Ctrl_Actions_For_Aggr;
2842 Comp_Type := Etype (Selector);
2844 Make_Selected_Component (Loc,
2845 Prefix => New_Copy_Tree (Target),
2846 Selector_Name => New_Occurrence_Of (Selector, Loc));
2848 if Nkind (Expression (Comp)) = N_Qualified_Expression then
2849 Expr_Q := Expression (Expression (Comp));
2851 Expr_Q := Expression (Comp);
2854 -- The controller is the one of the parent type defining the
2855 -- component (in case of inherited components).
2857 if Needs_Finalization (Comp_Type) then
2858 Internal_Final_List :=
2859 Make_Selected_Component (Loc,
2860 Prefix => Convert_To (
2861 Scope (Original_Record_Component (Selector)),
2862 New_Copy_Tree (Target)),
2864 Make_Identifier (Loc, Name_uController));
2866 Internal_Final_List :=
2867 Make_Selected_Component (Loc,
2868 Prefix => Internal_Final_List,
2869 Selector_Name => Make_Identifier (Loc, Name_F));
2871 -- The internal final list can be part of a constant object
2873 Set_Assignment_OK (Internal_Final_List);
2876 Internal_Final_List := Empty;
2879 -- Now either create the assignment or generate the code for the
2880 -- inner aggregate top-down.
2882 if Is_Delayed_Aggregate (Expr_Q) then
2884 -- We have the following case of aggregate nesting inside
2885 -- an object declaration:
2887 -- type Arr_Typ is array (Integer range <>) of ...;
2889 -- type Rec_Typ (...) is record
2890 -- Obj_Arr_Typ : Arr_Typ (A .. B);
2893 -- Obj_Rec_Typ : Rec_Typ := (...,
2894 -- Obj_Arr_Typ => (X => (...), Y => (...)));
2896 -- The length of the ranges of the aggregate and Obj_Add_Typ
2897 -- are equal (B - A = Y - X), but they do not coincide (X /=
2898 -- A and B /= Y). This case requires array sliding which is
2899 -- performed in the following manner:
2901 -- subtype Arr_Sub is Arr_Typ (X .. Y);
2903 -- Temp (X) := (...);
2905 -- Temp (Y) := (...);
2906 -- Obj_Rec_Typ.Obj_Arr_Typ := Temp;
2908 if Ekind (Comp_Type) = E_Array_Subtype
2909 and then Is_Int_Range_Bounds (Aggregate_Bounds (Expr_Q))
2910 and then Is_Int_Range_Bounds (First_Index (Comp_Type))
2912 Compatible_Int_Bounds
2913 (Agg_Bounds => Aggregate_Bounds (Expr_Q),
2914 Typ_Bounds => First_Index (Comp_Type))
2916 -- Create the array subtype with bounds equal to those of
2917 -- the corresponding aggregate.
2920 SubE : constant Entity_Id :=
2921 Make_Defining_Identifier (Loc,
2922 New_Internal_Name ('T'));
2924 SubD : constant Node_Id :=
2925 Make_Subtype_Declaration (Loc,
2926 Defining_Identifier =>
2928 Subtype_Indication =>
2929 Make_Subtype_Indication (Loc,
2930 Subtype_Mark => New_Reference_To (
2931 Etype (Comp_Type), Loc),
2933 Make_Index_Or_Discriminant_Constraint (
2934 Loc, Constraints => New_List (
2935 New_Copy_Tree (Aggregate_Bounds (
2938 -- Create a temporary array of the above subtype which
2939 -- will be used to capture the aggregate assignments.
2941 TmpE : constant Entity_Id :=
2942 Make_Defining_Identifier (Loc,
2943 New_Internal_Name ('A'));
2945 TmpD : constant Node_Id :=
2946 Make_Object_Declaration (Loc,
2947 Defining_Identifier =>
2949 Object_Definition =>
2950 New_Reference_To (SubE, Loc));
2953 Set_No_Initialization (TmpD);
2954 Append_To (L, SubD);
2955 Append_To (L, TmpD);
2957 -- Expand aggregate into assignments to the temp array
2960 Late_Expansion (Expr_Q, Comp_Type,
2961 New_Reference_To (TmpE, Loc), Internal_Final_List));
2966 Make_Assignment_Statement (Loc,
2967 Name => New_Copy_Tree (Comp_Expr),
2968 Expression => New_Reference_To (TmpE, Loc)));
2970 -- Do not pass the original aggregate to Gigi as is,
2971 -- since it will potentially clobber the front or the end
2972 -- of the array. Setting the expression to empty is safe
2973 -- since all aggregates are expanded into assignments.
2975 if Present (Obj) then
2976 Set_Expression (Parent (Obj), Empty);
2980 -- Normal case (sliding not required)
2984 Late_Expansion (Expr_Q, Comp_Type, Comp_Expr,
2985 Internal_Final_List));
2988 -- Expr_Q is not delayed aggregate
2992 Make_OK_Assignment_Statement (Loc,
2994 Expression => Expression (Comp));
2996 Set_No_Ctrl_Actions (Instr);
2997 Append_To (L, Instr);
2999 -- Adjust the tag if tagged (because of possible view
3000 -- conversions), unless compiling for a VM where tags are
3003 -- tmp.comp._tag := comp_typ'tag;
3005 if Is_Tagged_Type (Comp_Type) and then VM_Target = No_VM then
3007 Make_OK_Assignment_Statement (Loc,
3009 Make_Selected_Component (Loc,
3010 Prefix => New_Copy_Tree (Comp_Expr),
3013 (First_Tag_Component (Comp_Type), Loc)),
3016 Unchecked_Convert_To (RTE (RE_Tag),
3018 (Node (First_Elmt (Access_Disp_Table (Comp_Type))),
3021 Append_To (L, Instr);
3024 -- Adjust and Attach the component to the proper controller
3026 -- Adjust (tmp.comp);
3027 -- Attach_To_Final_List (tmp.comp,
3028 -- comp_typ (tmp)._record_controller.f)
3030 if Needs_Finalization (Comp_Type)
3031 and then not Is_Limited_Type (Comp_Type)
3035 Ref => New_Copy_Tree (Comp_Expr),
3037 Flist_Ref => Internal_Final_List,
3038 With_Attach => Make_Integer_Literal (Loc, 1)));
3044 elsif Ekind (Selector) = E_Discriminant
3045 and then Nkind (N) /= N_Extension_Aggregate
3046 and then Nkind (Parent (N)) = N_Component_Association
3047 and then Is_Constrained (Typ)
3049 -- We must check that the discriminant value imposed by the
3050 -- context is the same as the value given in the subaggregate,
3051 -- because after the expansion into assignments there is no
3052 -- record on which to perform a regular discriminant check.
3059 D_Val := First_Elmt (Discriminant_Constraint (Typ));
3060 Disc := First_Discriminant (Typ);
3061 while Chars (Disc) /= Chars (Selector) loop
3062 Next_Discriminant (Disc);
3066 pragma Assert (Present (D_Val));
3068 -- This check cannot performed for components that are
3069 -- constrained by a current instance, because this is not a
3070 -- value that can be compared with the actual constraint.
3072 if Nkind (Node (D_Val)) /= N_Attribute_Reference
3073 or else not Is_Entity_Name (Prefix (Node (D_Val)))
3074 or else not Is_Type (Entity (Prefix (Node (D_Val))))
3077 Make_Raise_Constraint_Error (Loc,
3080 Left_Opnd => New_Copy_Tree (Node (D_Val)),
3081 Right_Opnd => Expression (Comp)),
3082 Reason => CE_Discriminant_Check_Failed));
3085 -- Find self-reference in previous discriminant assignment,
3086 -- and replace with proper expression.
3093 while Present (Ass) loop
3094 if Nkind (Ass) = N_Assignment_Statement
3095 and then Nkind (Name (Ass)) = N_Selected_Component
3096 and then Chars (Selector_Name (Name (Ass))) =
3100 (Ass, New_Copy_Tree (Expression (Comp)));
3115 -- If the type is tagged, the tag needs to be initialized (unless
3116 -- compiling for the Java VM where tags are implicit). It is done
3117 -- late in the initialization process because in some cases, we call
3118 -- the init proc of an ancestor which will not leave out the right tag
3120 if Ancestor_Is_Expression then
3123 elsif Is_Tagged_Type (Typ) and then VM_Target = No_VM then
3125 Make_OK_Assignment_Statement (Loc,
3127 Make_Selected_Component (Loc,
3128 Prefix => New_Copy_Tree (Target),
3131 (First_Tag_Component (Base_Type (Typ)), Loc)),
3134 Unchecked_Convert_To (RTE (RE_Tag),
3136 (Node (First_Elmt (Access_Disp_Table (Base_Type (Typ)))),
3139 Append_To (L, Instr);
3141 -- Ada 2005 (AI-251): If the tagged type has been derived from
3142 -- abstract interfaces we must also initialize the tags of the
3143 -- secondary dispatch tables.
3145 if Has_Interfaces (Base_Type (Typ)) then
3147 (Typ => Base_Type (Typ),
3153 -- If the controllers have not been initialized yet (by lack of non-
3154 -- discriminant components), let's do it now.
3156 Gen_Ctrl_Actions_For_Aggr;
3159 end Build_Record_Aggr_Code;
3161 -------------------------------
3162 -- Convert_Aggr_In_Allocator --
3163 -------------------------------
3165 procedure Convert_Aggr_In_Allocator
3170 Loc : constant Source_Ptr := Sloc (Aggr);
3171 Typ : constant Entity_Id := Etype (Aggr);
3172 Temp : constant Entity_Id := Defining_Identifier (Decl);
3174 Occ : constant Node_Id :=
3175 Unchecked_Convert_To (Typ,
3176 Make_Explicit_Dereference (Loc,
3177 New_Reference_To (Temp, Loc)));
3179 Access_Type : constant Entity_Id := Etype (Temp);
3183 -- If the allocator is for an access discriminant, there is no
3184 -- finalization list for the anonymous access type, and the eventual
3185 -- finalization of the object is handled through the coextension
3186 -- mechanism. If the enclosing object is not dynamically allocated,
3187 -- the access discriminant is itself placed on the stack. Otherwise,
3188 -- some other finalization list is used (see exp_ch4.adb).
3190 -- Decl has been inserted in the code ahead of the allocator, using
3191 -- Insert_Actions. We use Insert_Actions below as well, to ensure that
3192 -- subsequent insertions are done in the proper order. Using (for
3193 -- example) Insert_Actions_After to place the expanded aggregate
3194 -- immediately after Decl may lead to out-of-order references if the
3195 -- allocator has generated a finalization list, as when the designated
3196 -- object is controlled and there is an open transient scope.
3198 if Ekind (Access_Type) = E_Anonymous_Access_Type
3199 and then Nkind (Associated_Node_For_Itype (Access_Type)) =
3200 N_Discriminant_Specification
3204 Flist := Find_Final_List (Access_Type);
3207 if Is_Array_Type (Typ) then
3208 Convert_Array_Aggr_In_Allocator (Decl, Aggr, Occ);
3210 elsif Has_Default_Init_Comps (Aggr) then
3212 L : constant List_Id := New_List;
3213 Init_Stmts : List_Id;
3220 Associated_Final_Chain (Base_Type (Access_Type)));
3222 -- ??? Dubious actual for Obj: expect 'the original object being
3225 if Has_Task (Typ) then
3226 Build_Task_Allocate_Block_With_Init_Stmts (L, Aggr, Init_Stmts);
3227 Insert_Actions (Alloc, L);
3229 Insert_Actions (Alloc, Init_Stmts);
3234 Insert_Actions (Alloc,
3236 (Aggr, Typ, Occ, Flist,
3237 Associated_Final_Chain (Base_Type (Access_Type))));
3239 -- ??? Dubious actual for Obj: expect 'the original object being
3243 end Convert_Aggr_In_Allocator;
3245 --------------------------------
3246 -- Convert_Aggr_In_Assignment --
3247 --------------------------------
3249 procedure Convert_Aggr_In_Assignment (N : Node_Id) is
3250 Aggr : Node_Id := Expression (N);
3251 Typ : constant Entity_Id := Etype (Aggr);
3252 Occ : constant Node_Id := New_Copy_Tree (Name (N));
3255 if Nkind (Aggr) = N_Qualified_Expression then
3256 Aggr := Expression (Aggr);
3259 Insert_Actions_After (N,
3262 Find_Final_List (Typ, New_Copy_Tree (Occ))));
3263 end Convert_Aggr_In_Assignment;
3265 ---------------------------------
3266 -- Convert_Aggr_In_Object_Decl --
3267 ---------------------------------
3269 procedure Convert_Aggr_In_Object_Decl (N : Node_Id) is
3270 Obj : constant Entity_Id := Defining_Identifier (N);
3271 Aggr : Node_Id := Expression (N);
3272 Loc : constant Source_Ptr := Sloc (Aggr);
3273 Typ : constant Entity_Id := Etype (Aggr);
3274 Occ : constant Node_Id := New_Occurrence_Of (Obj, Loc);
3276 function Discriminants_Ok return Boolean;
3277 -- If the object type is constrained, the discriminants in the
3278 -- aggregate must be checked against the discriminants of the subtype.
3279 -- This cannot be done using Apply_Discriminant_Checks because after
3280 -- expansion there is no aggregate left to check.
3282 ----------------------
3283 -- Discriminants_Ok --
3284 ----------------------
3286 function Discriminants_Ok return Boolean is
3287 Cond : Node_Id := Empty;
3296 D := First_Discriminant (Typ);
3297 Disc1 := First_Elmt (Discriminant_Constraint (Typ));
3298 Disc2 := First_Elmt (Discriminant_Constraint (Etype (Obj)));
3299 while Present (Disc1) and then Present (Disc2) loop
3300 Val1 := Node (Disc1);
3301 Val2 := Node (Disc2);
3303 if not Is_OK_Static_Expression (Val1)
3304 or else not Is_OK_Static_Expression (Val2)
3306 Check := Make_Op_Ne (Loc,
3307 Left_Opnd => Duplicate_Subexpr (Val1),
3308 Right_Opnd => Duplicate_Subexpr (Val2));
3314 Cond := Make_Or_Else (Loc,
3316 Right_Opnd => Check);
3319 elsif Expr_Value (Val1) /= Expr_Value (Val2) then
3320 Apply_Compile_Time_Constraint_Error (Aggr,
3321 Msg => "incorrect value for discriminant&?",
3322 Reason => CE_Discriminant_Check_Failed,
3327 Next_Discriminant (D);
3332 -- If any discriminant constraint is non-static, emit a check
3334 if Present (Cond) then
3336 Make_Raise_Constraint_Error (Loc,
3338 Reason => CE_Discriminant_Check_Failed));
3342 end Discriminants_Ok;
3344 -- Start of processing for Convert_Aggr_In_Object_Decl
3347 Set_Assignment_OK (Occ);
3349 if Nkind (Aggr) = N_Qualified_Expression then
3350 Aggr := Expression (Aggr);
3353 if Has_Discriminants (Typ)
3354 and then Typ /= Etype (Obj)
3355 and then Is_Constrained (Etype (Obj))
3356 and then not Discriminants_Ok
3361 -- If the context is an extended return statement, it has its own
3362 -- finalization machinery (i.e. works like a transient scope) and
3363 -- we do not want to create an additional one, because objects on
3364 -- the finalization list of the return must be moved to the caller's
3365 -- finalization list to complete the return.
3367 -- However, if the aggregate is limited, it is built in place, and the
3368 -- controlled components are not assigned to intermediate temporaries
3369 -- so there is no need for a transient scope in this case either.
3371 if Requires_Transient_Scope (Typ)
3372 and then Ekind (Current_Scope) /= E_Return_Statement
3373 and then not Is_Limited_Type (Typ)
3375 Establish_Transient_Scope
3378 Is_Controlled (Typ) or else Has_Controlled_Component (Typ));
3381 Insert_Actions_After (N, Late_Expansion (Aggr, Typ, Occ, Obj => Obj));
3382 Set_No_Initialization (N);
3383 Initialize_Discriminants (N, Typ);
3384 end Convert_Aggr_In_Object_Decl;
3386 -------------------------------------
3387 -- Convert_Array_Aggr_In_Allocator --
3388 -------------------------------------
3390 procedure Convert_Array_Aggr_In_Allocator
3395 Aggr_Code : List_Id;
3396 Typ : constant Entity_Id := Etype (Aggr);
3397 Ctyp : constant Entity_Id := Component_Type (Typ);
3400 -- The target is an explicit dereference of the allocated object.
3401 -- Generate component assignments to it, as for an aggregate that
3402 -- appears on the right-hand side of an assignment statement.
3405 Build_Array_Aggr_Code (Aggr,
3407 Index => First_Index (Typ),
3409 Scalar_Comp => Is_Scalar_Type (Ctyp));
3411 Insert_Actions_After (Decl, Aggr_Code);
3412 end Convert_Array_Aggr_In_Allocator;
3414 ----------------------------
3415 -- Convert_To_Assignments --
3416 ----------------------------
3418 procedure Convert_To_Assignments (N : Node_Id; Typ : Entity_Id) is
3419 Loc : constant Source_Ptr := Sloc (N);
3424 Target_Expr : Node_Id;
3425 Parent_Kind : Node_Kind;
3426 Unc_Decl : Boolean := False;
3427 Parent_Node : Node_Id;
3430 pragma Assert (not Is_Static_Dispatch_Table_Aggregate (N));
3431 pragma Assert (Is_Record_Type (Typ));
3433 Parent_Node := Parent (N);
3434 Parent_Kind := Nkind (Parent_Node);
3436 if Parent_Kind = N_Qualified_Expression then
3438 -- Check if we are in a unconstrained declaration because in this
3439 -- case the current delayed expansion mechanism doesn't work when
3440 -- the declared object size depend on the initializing expr.
3443 Parent_Node := Parent (Parent_Node);
3444 Parent_Kind := Nkind (Parent_Node);
3446 if Parent_Kind = N_Object_Declaration then
3448 not Is_Entity_Name (Object_Definition (Parent_Node))
3449 or else Has_Discriminants
3450 (Entity (Object_Definition (Parent_Node)))
3451 or else Is_Class_Wide_Type
3452 (Entity (Object_Definition (Parent_Node)));
3457 -- Just set the Delay flag in the cases where the transformation will be
3458 -- done top down from above.
3462 -- Internal aggregate (transformed when expanding the parent)
3464 or else Parent_Kind = N_Aggregate
3465 or else Parent_Kind = N_Extension_Aggregate
3466 or else Parent_Kind = N_Component_Association
3468 -- Allocator (see Convert_Aggr_In_Allocator)
3470 or else Parent_Kind = N_Allocator
3472 -- Object declaration (see Convert_Aggr_In_Object_Decl)
3474 or else (Parent_Kind = N_Object_Declaration and then not Unc_Decl)
3476 -- Safe assignment (see Convert_Aggr_Assignments). So far only the
3477 -- assignments in init procs are taken into account.
3479 or else (Parent_Kind = N_Assignment_Statement
3480 and then Inside_Init_Proc)
3482 -- (Ada 2005) An inherently limited type in a return statement,
3483 -- which will be handled in a build-in-place fashion, and may be
3484 -- rewritten as an extended return and have its own finalization
3485 -- machinery. In the case of a simple return, the aggregate needs
3486 -- to be delayed until the scope for the return statement has been
3487 -- created, so that any finalization chain will be associated with
3488 -- that scope. For extended returns, we delay expansion to avoid the
3489 -- creation of an unwanted transient scope that could result in
3490 -- premature finalization of the return object (which is built in
3491 -- in place within the caller's scope).
3494 (Is_Inherently_Limited_Type (Typ)
3496 (Nkind (Parent (Parent_Node)) = N_Extended_Return_Statement
3497 or else Nkind (Parent_Node) = N_Simple_Return_Statement))
3499 Set_Expansion_Delayed (N);
3503 if Requires_Transient_Scope (Typ) then
3504 Establish_Transient_Scope
3506 Is_Controlled (Typ) or else Has_Controlled_Component (Typ));
3509 -- If the aggregate is non-limited, create a temporary. If it is limited
3510 -- and the context is an assignment, this is a subaggregate for an
3511 -- enclosing aggregate being expanded. It must be built in place, so use
3512 -- the target of the current assignment.
3514 if Is_Limited_Type (Typ)
3515 and then Nkind (Parent (N)) = N_Assignment_Statement
3517 Target_Expr := New_Copy_Tree (Name (Parent (N)));
3519 (Parent (N), Build_Record_Aggr_Code (N, Typ, Target_Expr));
3520 Rewrite (Parent (N), Make_Null_Statement (Loc));
3523 Temp := Make_Defining_Identifier (Loc, New_Internal_Name ('A'));
3525 -- If the type inherits unknown discriminants, use the view with
3526 -- known discriminants if available.
3528 if Has_Unknown_Discriminants (Typ)
3529 and then Present (Underlying_Record_View (Typ))
3531 T := Underlying_Record_View (Typ);
3537 Make_Object_Declaration (Loc,
3538 Defining_Identifier => Temp,
3539 Object_Definition => New_Occurrence_Of (T, Loc));
3541 Set_No_Initialization (Instr);
3542 Insert_Action (N, Instr);
3543 Initialize_Discriminants (Instr, T);
3544 Target_Expr := New_Occurrence_Of (Temp, Loc);
3545 Insert_Actions (N, Build_Record_Aggr_Code (N, T, Target_Expr));
3546 Rewrite (N, New_Occurrence_Of (Temp, Loc));
3547 Analyze_And_Resolve (N, T);
3549 end Convert_To_Assignments;
3551 ---------------------------
3552 -- Convert_To_Positional --
3553 ---------------------------
3555 procedure Convert_To_Positional
3557 Max_Others_Replicate : Nat := 5;
3558 Handle_Bit_Packed : Boolean := False)
3560 Typ : constant Entity_Id := Etype (N);
3562 Static_Components : Boolean := True;
3564 procedure Check_Static_Components;
3565 -- Check whether all components of the aggregate are compile-time known
3566 -- values, and can be passed as is to the back-end without further
3572 Ixb : Node_Id) return Boolean;
3573 -- Convert the aggregate into a purely positional form if possible. On
3574 -- entry the bounds of all dimensions are known to be static, and the
3575 -- total number of components is safe enough to expand.
3577 function Is_Flat (N : Node_Id; Dims : Int) return Boolean;
3578 -- Return True iff the array N is flat (which is not rivial in the case
3579 -- of multidimensionsl aggregates).
3581 -----------------------------
3582 -- Check_Static_Components --
3583 -----------------------------
3585 procedure Check_Static_Components is
3589 Static_Components := True;
3591 if Nkind (N) = N_String_Literal then
3594 elsif Present (Expressions (N)) then
3595 Expr := First (Expressions (N));
3596 while Present (Expr) loop
3597 if Nkind (Expr) /= N_Aggregate
3598 or else not Compile_Time_Known_Aggregate (Expr)
3599 or else Expansion_Delayed (Expr)
3601 Static_Components := False;
3609 if Nkind (N) = N_Aggregate
3610 and then Present (Component_Associations (N))
3612 Expr := First (Component_Associations (N));
3613 while Present (Expr) loop
3614 if Nkind (Expression (Expr)) = N_Integer_Literal then
3617 elsif Nkind (Expression (Expr)) /= N_Aggregate
3619 not Compile_Time_Known_Aggregate (Expression (Expr))
3620 or else Expansion_Delayed (Expression (Expr))
3622 Static_Components := False;
3629 end Check_Static_Components;
3638 Ixb : Node_Id) return Boolean
3640 Loc : constant Source_Ptr := Sloc (N);
3641 Blo : constant Node_Id := Type_Low_Bound (Etype (Ixb));
3642 Lo : constant Node_Id := Type_Low_Bound (Etype (Ix));
3643 Hi : constant Node_Id := Type_High_Bound (Etype (Ix));
3648 if Nkind (Original_Node (N)) = N_String_Literal then
3652 if not Compile_Time_Known_Value (Lo)
3653 or else not Compile_Time_Known_Value (Hi)
3658 Lov := Expr_Value (Lo);
3659 Hiv := Expr_Value (Hi);
3662 or else not Compile_Time_Known_Value (Blo)
3667 -- Determine if set of alternatives is suitable for conversion and
3668 -- build an array containing the values in sequence.
3671 Vals : array (UI_To_Int (Lov) .. UI_To_Int (Hiv))
3672 of Node_Id := (others => Empty);
3673 -- The values in the aggregate sorted appropriately
3676 -- Same data as Vals in list form
3679 -- Used to validate Max_Others_Replicate limit
3682 Num : Int := UI_To_Int (Lov);
3687 if Present (Expressions (N)) then
3688 Elmt := First (Expressions (N));
3689 while Present (Elmt) loop
3690 if Nkind (Elmt) = N_Aggregate
3691 and then Present (Next_Index (Ix))
3693 not Flatten (Elmt, Next_Index (Ix), Next_Index (Ixb))
3698 Vals (Num) := Relocate_Node (Elmt);
3705 if No (Component_Associations (N)) then
3709 Elmt := First (Component_Associations (N));
3711 if Nkind (Expression (Elmt)) = N_Aggregate then
3712 if Present (Next_Index (Ix))
3715 (Expression (Elmt), Next_Index (Ix), Next_Index (Ixb))
3721 Component_Loop : while Present (Elmt) loop
3722 Choice := First (Choices (Elmt));
3723 Choice_Loop : while Present (Choice) loop
3725 -- If we have an others choice, fill in the missing elements
3726 -- subject to the limit established by Max_Others_Replicate.
3728 if Nkind (Choice) = N_Others_Choice then
3731 for J in Vals'Range loop
3732 if No (Vals (J)) then
3733 Vals (J) := New_Copy_Tree (Expression (Elmt));
3734 Rep_Count := Rep_Count + 1;
3736 -- Check for maximum others replication. Note that
3737 -- we skip this test if either of the restrictions
3738 -- No_Elaboration_Code or No_Implicit_Loops is
3739 -- active, if this is a preelaborable unit or a
3740 -- predefined unit. This ensures that predefined
3741 -- units get the same level of constant folding in
3742 -- Ada 95 and Ada 05, where their categorization
3746 P : constant Entity_Id :=
3747 Cunit_Entity (Current_Sem_Unit);
3750 -- Check if duplication OK and if so continue
3753 if Restriction_Active (No_Elaboration_Code)
3754 or else Restriction_Active (No_Implicit_Loops)
3755 or else Is_Preelaborated (P)
3756 or else (Ekind (P) = E_Package_Body
3758 Is_Preelaborated (Spec_Entity (P)))
3760 Is_Predefined_File_Name
3761 (Unit_File_Name (Get_Source_Unit (P)))
3765 -- If duplication not OK, then we return False
3766 -- if the replication count is too high
3768 elsif Rep_Count > Max_Others_Replicate then
3771 -- Continue on if duplication not OK, but the
3772 -- replication count is not excessive.
3781 exit Component_Loop;
3783 -- Case of a subtype mark
3785 elsif Nkind (Choice) = N_Identifier
3786 and then Is_Type (Entity (Choice))
3788 Lo := Type_Low_Bound (Etype (Choice));
3789 Hi := Type_High_Bound (Etype (Choice));
3791 -- Case of subtype indication
3793 elsif Nkind (Choice) = N_Subtype_Indication then
3794 Lo := Low_Bound (Range_Expression (Constraint (Choice)));
3795 Hi := High_Bound (Range_Expression (Constraint (Choice)));
3799 elsif Nkind (Choice) = N_Range then
3800 Lo := Low_Bound (Choice);
3801 Hi := High_Bound (Choice);
3803 -- Normal subexpression case
3805 else pragma Assert (Nkind (Choice) in N_Subexpr);
3806 if not Compile_Time_Known_Value (Choice) then
3810 Vals (UI_To_Int (Expr_Value (Choice))) :=
3811 New_Copy_Tree (Expression (Elmt));
3816 -- Range cases merge with Lo,Hi said
3818 if not Compile_Time_Known_Value (Lo)
3820 not Compile_Time_Known_Value (Hi)
3824 for J in UI_To_Int (Expr_Value (Lo)) ..
3825 UI_To_Int (Expr_Value (Hi))
3827 Vals (J) := New_Copy_Tree (Expression (Elmt));
3833 end loop Choice_Loop;
3836 end loop Component_Loop;
3838 -- If we get here the conversion is possible
3841 for J in Vals'Range loop
3842 Append (Vals (J), Vlist);
3845 Rewrite (N, Make_Aggregate (Loc, Expressions => Vlist));
3846 Set_Aggregate_Bounds (N, Aggregate_Bounds (Original_Node (N)));
3855 function Is_Flat (N : Node_Id; Dims : Int) return Boolean is
3862 elsif Nkind (N) = N_Aggregate then
3863 if Present (Component_Associations (N)) then
3867 Elmt := First (Expressions (N));
3868 while Present (Elmt) loop
3869 if not Is_Flat (Elmt, Dims - 1) then
3883 -- Start of processing for Convert_To_Positional
3886 -- Ada 2005 (AI-287): Do not convert in case of default initialized
3887 -- components because in this case will need to call the corresponding
3890 if Has_Default_Init_Comps (N) then
3894 if Is_Flat (N, Number_Dimensions (Typ)) then
3898 if Is_Bit_Packed_Array (Typ)
3899 and then not Handle_Bit_Packed
3904 -- Do not convert to positional if controlled components are involved
3905 -- since these require special processing
3907 if Has_Controlled_Component (Typ) then
3911 Check_Static_Components;
3913 -- If the size is known, or all the components are static, try to
3914 -- build a fully positional aggregate.
3916 -- The size of the type may not be known for an aggregate with
3917 -- discriminated array components, but if the components are static
3918 -- it is still possible to verify statically that the length is
3919 -- compatible with the upper bound of the type, and therefore it is
3920 -- worth flattening such aggregates as well.
3922 -- For now the back-end expands these aggregates into individual
3923 -- assignments to the target anyway, but it is conceivable that
3924 -- it will eventually be able to treat such aggregates statically???
3926 if Aggr_Size_OK (N, Typ)
3927 and then Flatten (N, First_Index (Typ), First_Index (Base_Type (Typ)))
3929 if Static_Components then
3930 Set_Compile_Time_Known_Aggregate (N);
3931 Set_Expansion_Delayed (N, False);
3934 Analyze_And_Resolve (N, Typ);
3936 end Convert_To_Positional;
3938 ----------------------------
3939 -- Expand_Array_Aggregate --
3940 ----------------------------
3942 -- Array aggregate expansion proceeds as follows:
3944 -- 1. If requested we generate code to perform all the array aggregate
3945 -- bound checks, specifically
3947 -- (a) Check that the index range defined by aggregate bounds is
3948 -- compatible with corresponding index subtype.
3950 -- (b) If an others choice is present check that no aggregate
3951 -- index is outside the bounds of the index constraint.
3953 -- (c) For multidimensional arrays make sure that all subaggregates
3954 -- corresponding to the same dimension have the same bounds.
3956 -- 2. Check for packed array aggregate which can be converted to a
3957 -- constant so that the aggregate disappeares completely.
3959 -- 3. Check case of nested aggregate. Generally nested aggregates are
3960 -- handled during the processing of the parent aggregate.
3962 -- 4. Check if the aggregate can be statically processed. If this is the
3963 -- case pass it as is to Gigi. Note that a necessary condition for
3964 -- static processing is that the aggregate be fully positional.
3966 -- 5. If in place aggregate expansion is possible (i.e. no need to create
3967 -- a temporary) then mark the aggregate as such and return. Otherwise
3968 -- create a new temporary and generate the appropriate initialization
3971 procedure Expand_Array_Aggregate (N : Node_Id) is
3972 Loc : constant Source_Ptr := Sloc (N);
3974 Typ : constant Entity_Id := Etype (N);
3975 Ctyp : constant Entity_Id := Component_Type (Typ);
3976 -- Typ is the correct constrained array subtype of the aggregate
3977 -- Ctyp is the corresponding component type.
3979 Aggr_Dimension : constant Pos := Number_Dimensions (Typ);
3980 -- Number of aggregate index dimensions
3982 Aggr_Low : array (1 .. Aggr_Dimension) of Node_Id;
3983 Aggr_High : array (1 .. Aggr_Dimension) of Node_Id;
3984 -- Low and High bounds of the constraint for each aggregate index
3986 Aggr_Index_Typ : array (1 .. Aggr_Dimension) of Entity_Id;
3987 -- The type of each index
3989 Maybe_In_Place_OK : Boolean;
3990 -- If the type is neither controlled nor packed and the aggregate
3991 -- is the expression in an assignment, assignment in place may be
3992 -- possible, provided other conditions are met on the LHS.
3994 Others_Present : array (1 .. Aggr_Dimension) of Boolean :=
3996 -- If Others_Present (J) is True, then there is an others choice
3997 -- in one of the sub-aggregates of N at dimension J.
3999 procedure Build_Constrained_Type (Positional : Boolean);
4000 -- If the subtype is not static or unconstrained, build a constrained
4001 -- type using the computable sizes of the aggregate and its sub-
4004 procedure Check_Bounds (Aggr_Bounds : Node_Id; Index_Bounds : Node_Id);
4005 -- Checks that the bounds of Aggr_Bounds are within the bounds defined
4008 procedure Check_Same_Aggr_Bounds (Sub_Aggr : Node_Id; Dim : Pos);
4009 -- Checks that in a multi-dimensional array aggregate all subaggregates
4010 -- corresponding to the same dimension have the same bounds.
4011 -- Sub_Aggr is an array sub-aggregate. Dim is the dimension
4012 -- corresponding to the sub-aggregate.
4014 procedure Compute_Others_Present (Sub_Aggr : Node_Id; Dim : Pos);
4015 -- Computes the values of array Others_Present. Sub_Aggr is the
4016 -- array sub-aggregate we start the computation from. Dim is the
4017 -- dimension corresponding to the sub-aggregate.
4019 function Has_Address_Clause (D : Node_Id) return Boolean;
4020 -- If the aggregate is the expression in an object declaration, it
4021 -- cannot be expanded in place. This function does a lookahead in the
4022 -- current declarative part to find an address clause for the object
4025 function In_Place_Assign_OK return Boolean;
4026 -- Simple predicate to determine whether an aggregate assignment can
4027 -- be done in place, because none of the new values can depend on the
4028 -- components of the target of the assignment.
4030 procedure Others_Check (Sub_Aggr : Node_Id; Dim : Pos);
4031 -- Checks that if an others choice is present in any sub-aggregate no
4032 -- aggregate index is outside the bounds of the index constraint.
4033 -- Sub_Aggr is an array sub-aggregate. Dim is the dimension
4034 -- corresponding to the sub-aggregate.
4036 ----------------------------
4037 -- Build_Constrained_Type --
4038 ----------------------------
4040 procedure Build_Constrained_Type (Positional : Boolean) is
4041 Loc : constant Source_Ptr := Sloc (N);
4042 Agg_Type : Entity_Id;
4045 Typ : constant Entity_Id := Etype (N);
4046 Indices : constant List_Id := New_List;
4052 Make_Defining_Identifier (
4053 Loc, New_Internal_Name ('A'));
4055 -- If the aggregate is purely positional, all its subaggregates
4056 -- have the same size. We collect the dimensions from the first
4057 -- subaggregate at each level.
4062 for D in 1 .. Number_Dimensions (Typ) loop
4063 Sub_Agg := First (Expressions (Sub_Agg));
4067 while Present (Comp) loop
4074 Low_Bound => Make_Integer_Literal (Loc, 1),
4076 Make_Integer_Literal (Loc, Num)),
4081 -- We know the aggregate type is unconstrained and the aggregate
4082 -- is not processable by the back end, therefore not necessarily
4083 -- positional. Retrieve each dimension bounds (computed earlier).
4086 for D in 1 .. Number_Dimensions (Typ) loop
4089 Low_Bound => Aggr_Low (D),
4090 High_Bound => Aggr_High (D)),
4096 Make_Full_Type_Declaration (Loc,
4097 Defining_Identifier => Agg_Type,
4099 Make_Constrained_Array_Definition (Loc,
4100 Discrete_Subtype_Definitions => Indices,
4101 Component_Definition =>
4102 Make_Component_Definition (Loc,
4103 Aliased_Present => False,
4104 Subtype_Indication =>
4105 New_Occurrence_Of (Component_Type (Typ), Loc))));
4107 Insert_Action (N, Decl);
4109 Set_Etype (N, Agg_Type);
4110 Set_Is_Itype (Agg_Type);
4111 Freeze_Itype (Agg_Type, N);
4112 end Build_Constrained_Type;
4118 procedure Check_Bounds (Aggr_Bounds : Node_Id; Index_Bounds : Node_Id) is
4125 Cond : Node_Id := Empty;
4128 Get_Index_Bounds (Aggr_Bounds, Aggr_Lo, Aggr_Hi);
4129 Get_Index_Bounds (Index_Bounds, Ind_Lo, Ind_Hi);
4131 -- Generate the following test:
4133 -- [constraint_error when
4134 -- Aggr_Lo <= Aggr_Hi and then
4135 -- (Aggr_Lo < Ind_Lo or else Aggr_Hi > Ind_Hi)]
4137 -- As an optimization try to see if some tests are trivially vacuous
4138 -- because we are comparing an expression against itself.
4140 if Aggr_Lo = Ind_Lo and then Aggr_Hi = Ind_Hi then
4143 elsif Aggr_Hi = Ind_Hi then
4146 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
4147 Right_Opnd => Duplicate_Subexpr_Move_Checks (Ind_Lo));
4149 elsif Aggr_Lo = Ind_Lo then
4152 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi),
4153 Right_Opnd => Duplicate_Subexpr_Move_Checks (Ind_Hi));
4160 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
4161 Right_Opnd => Duplicate_Subexpr_Move_Checks (Ind_Lo)),
4165 Left_Opnd => Duplicate_Subexpr (Aggr_Hi),
4166 Right_Opnd => Duplicate_Subexpr (Ind_Hi)));
4169 if Present (Cond) then
4174 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
4175 Right_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi)),
4177 Right_Opnd => Cond);
4179 Set_Analyzed (Left_Opnd (Left_Opnd (Cond)), False);
4180 Set_Analyzed (Right_Opnd (Left_Opnd (Cond)), False);
4182 Make_Raise_Constraint_Error (Loc,
4184 Reason => CE_Length_Check_Failed));
4188 ----------------------------
4189 -- Check_Same_Aggr_Bounds --
4190 ----------------------------
4192 procedure Check_Same_Aggr_Bounds (Sub_Aggr : Node_Id; Dim : Pos) is
4193 Sub_Lo : constant Node_Id := Low_Bound (Aggregate_Bounds (Sub_Aggr));
4194 Sub_Hi : constant Node_Id := High_Bound (Aggregate_Bounds (Sub_Aggr));
4195 -- The bounds of this specific sub-aggregate
4197 Aggr_Lo : constant Node_Id := Aggr_Low (Dim);
4198 Aggr_Hi : constant Node_Id := Aggr_High (Dim);
4199 -- The bounds of the aggregate for this dimension
4201 Ind_Typ : constant Entity_Id := Aggr_Index_Typ (Dim);
4202 -- The index type for this dimension.xxx
4204 Cond : Node_Id := Empty;
4209 -- If index checks are on generate the test
4211 -- [constraint_error when
4212 -- Aggr_Lo /= Sub_Lo or else Aggr_Hi /= Sub_Hi]
4214 -- As an optimization try to see if some tests are trivially vacuos
4215 -- because we are comparing an expression against itself. Also for
4216 -- the first dimension the test is trivially vacuous because there
4217 -- is just one aggregate for dimension 1.
4219 if Index_Checks_Suppressed (Ind_Typ) then
4223 or else (Aggr_Lo = Sub_Lo and then Aggr_Hi = Sub_Hi)
4227 elsif Aggr_Hi = Sub_Hi then
4230 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
4231 Right_Opnd => Duplicate_Subexpr_Move_Checks (Sub_Lo));
4233 elsif Aggr_Lo = Sub_Lo then
4236 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi),
4237 Right_Opnd => Duplicate_Subexpr_Move_Checks (Sub_Hi));
4244 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
4245 Right_Opnd => Duplicate_Subexpr_Move_Checks (Sub_Lo)),
4249 Left_Opnd => Duplicate_Subexpr (Aggr_Hi),
4250 Right_Opnd => Duplicate_Subexpr (Sub_Hi)));
4253 if Present (Cond) then
4255 Make_Raise_Constraint_Error (Loc,
4257 Reason => CE_Length_Check_Failed));
4260 -- Now look inside the sub-aggregate to see if there is more work
4262 if Dim < Aggr_Dimension then
4264 -- Process positional components
4266 if Present (Expressions (Sub_Aggr)) then
4267 Expr := First (Expressions (Sub_Aggr));
4268 while Present (Expr) loop
4269 Check_Same_Aggr_Bounds (Expr, Dim + 1);
4274 -- Process component associations
4276 if Present (Component_Associations (Sub_Aggr)) then
4277 Assoc := First (Component_Associations (Sub_Aggr));
4278 while Present (Assoc) loop
4279 Expr := Expression (Assoc);
4280 Check_Same_Aggr_Bounds (Expr, Dim + 1);
4285 end Check_Same_Aggr_Bounds;
4287 ----------------------------
4288 -- Compute_Others_Present --
4289 ----------------------------
4291 procedure Compute_Others_Present (Sub_Aggr : Node_Id; Dim : Pos) is
4296 if Present (Component_Associations (Sub_Aggr)) then
4297 Assoc := Last (Component_Associations (Sub_Aggr));
4299 if Nkind (First (Choices (Assoc))) = N_Others_Choice then
4300 Others_Present (Dim) := True;
4304 -- Now look inside the sub-aggregate to see if there is more work
4306 if Dim < Aggr_Dimension then
4308 -- Process positional components
4310 if Present (Expressions (Sub_Aggr)) then
4311 Expr := First (Expressions (Sub_Aggr));
4312 while Present (Expr) loop
4313 Compute_Others_Present (Expr, Dim + 1);
4318 -- Process component associations
4320 if Present (Component_Associations (Sub_Aggr)) then
4321 Assoc := First (Component_Associations (Sub_Aggr));
4322 while Present (Assoc) loop
4323 Expr := Expression (Assoc);
4324 Compute_Others_Present (Expr, Dim + 1);
4329 end Compute_Others_Present;
4331 ------------------------
4332 -- Has_Address_Clause --
4333 ------------------------
4335 function Has_Address_Clause (D : Node_Id) return Boolean is
4336 Id : constant Entity_Id := Defining_Identifier (D);
4341 while Present (Decl) loop
4342 if Nkind (Decl) = N_At_Clause
4343 and then Chars (Identifier (Decl)) = Chars (Id)
4347 elsif Nkind (Decl) = N_Attribute_Definition_Clause
4348 and then Chars (Decl) = Name_Address
4349 and then Chars (Name (Decl)) = Chars (Id)
4358 end Has_Address_Clause;
4360 ------------------------
4361 -- In_Place_Assign_OK --
4362 ------------------------
4364 function In_Place_Assign_OK return Boolean is
4372 function Is_Others_Aggregate (Aggr : Node_Id) return Boolean;
4373 -- Aggregates that consist of a single Others choice are safe
4374 -- if the single expression is.
4376 function Safe_Aggregate (Aggr : Node_Id) return Boolean;
4377 -- Check recursively that each component of a (sub)aggregate does
4378 -- not depend on the variable being assigned to.
4380 function Safe_Component (Expr : Node_Id) return Boolean;
4381 -- Verify that an expression cannot depend on the variable being
4382 -- assigned to. Room for improvement here (but less than before).
4384 -------------------------
4385 -- Is_Others_Aggregate --
4386 -------------------------
4388 function Is_Others_Aggregate (Aggr : Node_Id) return Boolean is
4390 return No (Expressions (Aggr))
4392 (First (Choices (First (Component_Associations (Aggr)))))
4394 end Is_Others_Aggregate;
4396 --------------------
4397 -- Safe_Aggregate --
4398 --------------------
4400 function Safe_Aggregate (Aggr : Node_Id) return Boolean is
4404 if Present (Expressions (Aggr)) then
4405 Expr := First (Expressions (Aggr));
4406 while Present (Expr) loop
4407 if Nkind (Expr) = N_Aggregate then
4408 if not Safe_Aggregate (Expr) then
4412 elsif not Safe_Component (Expr) then
4420 if Present (Component_Associations (Aggr)) then
4421 Expr := First (Component_Associations (Aggr));
4422 while Present (Expr) loop
4423 if Nkind (Expression (Expr)) = N_Aggregate then
4424 if not Safe_Aggregate (Expression (Expr)) then
4428 elsif not Safe_Component (Expression (Expr)) then
4439 --------------------
4440 -- Safe_Component --
4441 --------------------
4443 function Safe_Component (Expr : Node_Id) return Boolean is
4444 Comp : Node_Id := Expr;
4446 function Check_Component (Comp : Node_Id) return Boolean;
4447 -- Do the recursive traversal, after copy
4449 ---------------------
4450 -- Check_Component --
4451 ---------------------
4453 function Check_Component (Comp : Node_Id) return Boolean is
4455 if Is_Overloaded (Comp) then
4459 return Compile_Time_Known_Value (Comp)
4461 or else (Is_Entity_Name (Comp)
4462 and then Present (Entity (Comp))
4463 and then No (Renamed_Object (Entity (Comp))))
4465 or else (Nkind (Comp) = N_Attribute_Reference
4466 and then Check_Component (Prefix (Comp)))
4468 or else (Nkind (Comp) in N_Binary_Op
4469 and then Check_Component (Left_Opnd (Comp))
4470 and then Check_Component (Right_Opnd (Comp)))
4472 or else (Nkind (Comp) in N_Unary_Op
4473 and then Check_Component (Right_Opnd (Comp)))
4475 or else (Nkind (Comp) = N_Selected_Component
4476 and then Check_Component (Prefix (Comp)))
4478 or else (Nkind (Comp) = N_Unchecked_Type_Conversion
4479 and then Check_Component (Expression (Comp)));
4480 end Check_Component;
4482 -- Start of processing for Safe_Component
4485 -- If the component appears in an association that may
4486 -- correspond to more than one element, it is not analyzed
4487 -- before the expansion into assignments, to avoid side effects.
4488 -- We analyze, but do not resolve the copy, to obtain sufficient
4489 -- entity information for the checks that follow. If component is
4490 -- overloaded we assume an unsafe function call.
4492 if not Analyzed (Comp) then
4493 if Is_Overloaded (Expr) then
4496 elsif Nkind (Expr) = N_Aggregate
4497 and then not Is_Others_Aggregate (Expr)
4501 elsif Nkind (Expr) = N_Allocator then
4503 -- For now, too complex to analyze
4508 Comp := New_Copy_Tree (Expr);
4509 Set_Parent (Comp, Parent (Expr));
4513 if Nkind (Comp) = N_Aggregate then
4514 return Safe_Aggregate (Comp);
4516 return Check_Component (Comp);
4520 -- Start of processing for In_Place_Assign_OK
4523 if Present (Component_Associations (N)) then
4525 -- On assignment, sliding can take place, so we cannot do the
4526 -- assignment in place unless the bounds of the aggregate are
4527 -- statically equal to those of the target.
4529 -- If the aggregate is given by an others choice, the bounds
4530 -- are derived from the left-hand side, and the assignment is
4531 -- safe if the expression is.
4533 if Is_Others_Aggregate (N) then
4536 (Expression (First (Component_Associations (N))));
4539 Aggr_In := First_Index (Etype (N));
4540 if Nkind (Parent (N)) = N_Assignment_Statement then
4541 Obj_In := First_Index (Etype (Name (Parent (N))));
4544 -- Context is an allocator. Check bounds of aggregate
4545 -- against given type in qualified expression.
4547 pragma Assert (Nkind (Parent (Parent (N))) = N_Allocator);
4549 First_Index (Etype (Entity (Subtype_Mark (Parent (N)))));
4552 while Present (Aggr_In) loop
4553 Get_Index_Bounds (Aggr_In, Aggr_Lo, Aggr_Hi);
4554 Get_Index_Bounds (Obj_In, Obj_Lo, Obj_Hi);
4556 if not Compile_Time_Known_Value (Aggr_Lo)
4557 or else not Compile_Time_Known_Value (Aggr_Hi)
4558 or else not Compile_Time_Known_Value (Obj_Lo)
4559 or else not Compile_Time_Known_Value (Obj_Hi)
4560 or else Expr_Value (Aggr_Lo) /= Expr_Value (Obj_Lo)
4561 or else Expr_Value (Aggr_Hi) /= Expr_Value (Obj_Hi)
4566 Next_Index (Aggr_In);
4567 Next_Index (Obj_In);
4571 -- Now check the component values themselves
4573 return Safe_Aggregate (N);
4574 end In_Place_Assign_OK;
4580 procedure Others_Check (Sub_Aggr : Node_Id; Dim : Pos) is
4581 Aggr_Lo : constant Node_Id := Aggr_Low (Dim);
4582 Aggr_Hi : constant Node_Id := Aggr_High (Dim);
4583 -- The bounds of the aggregate for this dimension
4585 Ind_Typ : constant Entity_Id := Aggr_Index_Typ (Dim);
4586 -- The index type for this dimension
4588 Need_To_Check : Boolean := False;
4590 Choices_Lo : Node_Id := Empty;
4591 Choices_Hi : Node_Id := Empty;
4592 -- The lowest and highest discrete choices for a named sub-aggregate
4594 Nb_Choices : Int := -1;
4595 -- The number of discrete non-others choices in this sub-aggregate
4597 Nb_Elements : Uint := Uint_0;
4598 -- The number of elements in a positional aggregate
4600 Cond : Node_Id := Empty;
4607 -- Check if we have an others choice. If we do make sure that this
4608 -- sub-aggregate contains at least one element in addition to the
4611 if Range_Checks_Suppressed (Ind_Typ) then
4612 Need_To_Check := False;
4614 elsif Present (Expressions (Sub_Aggr))
4615 and then Present (Component_Associations (Sub_Aggr))
4617 Need_To_Check := True;
4619 elsif Present (Component_Associations (Sub_Aggr)) then
4620 Assoc := Last (Component_Associations (Sub_Aggr));
4622 if Nkind (First (Choices (Assoc))) /= N_Others_Choice then
4623 Need_To_Check := False;
4626 -- Count the number of discrete choices. Start with -1 because
4627 -- the others choice does not count.
4630 Assoc := First (Component_Associations (Sub_Aggr));
4631 while Present (Assoc) loop
4632 Choice := First (Choices (Assoc));
4633 while Present (Choice) loop
4634 Nb_Choices := Nb_Choices + 1;
4641 -- If there is only an others choice nothing to do
4643 Need_To_Check := (Nb_Choices > 0);
4647 Need_To_Check := False;
4650 -- If we are dealing with a positional sub-aggregate with an others
4651 -- choice then compute the number or positional elements.
4653 if Need_To_Check and then Present (Expressions (Sub_Aggr)) then
4654 Expr := First (Expressions (Sub_Aggr));
4655 Nb_Elements := Uint_0;
4656 while Present (Expr) loop
4657 Nb_Elements := Nb_Elements + 1;
4661 -- If the aggregate contains discrete choices and an others choice
4662 -- compute the smallest and largest discrete choice values.
4664 elsif Need_To_Check then
4665 Compute_Choices_Lo_And_Choices_Hi : declare
4667 Table : Case_Table_Type (1 .. Nb_Choices);
4668 -- Used to sort all the different choice values
4675 Assoc := First (Component_Associations (Sub_Aggr));
4676 while Present (Assoc) loop
4677 Choice := First (Choices (Assoc));
4678 while Present (Choice) loop
4679 if Nkind (Choice) = N_Others_Choice then
4683 Get_Index_Bounds (Choice, Low, High);
4684 Table (J).Choice_Lo := Low;
4685 Table (J).Choice_Hi := High;
4694 -- Sort the discrete choices
4696 Sort_Case_Table (Table);
4698 Choices_Lo := Table (1).Choice_Lo;
4699 Choices_Hi := Table (Nb_Choices).Choice_Hi;
4700 end Compute_Choices_Lo_And_Choices_Hi;
4703 -- If no others choice in this sub-aggregate, or the aggregate
4704 -- comprises only an others choice, nothing to do.
4706 if not Need_To_Check then
4709 -- If we are dealing with an aggregate containing an others choice
4710 -- and positional components, we generate the following test:
4712 -- if Ind_Typ'Pos (Aggr_Lo) + (Nb_Elements - 1) >
4713 -- Ind_Typ'Pos (Aggr_Hi)
4715 -- raise Constraint_Error;
4718 elsif Nb_Elements > Uint_0 then
4724 Make_Attribute_Reference (Loc,
4725 Prefix => New_Reference_To (Ind_Typ, Loc),
4726 Attribute_Name => Name_Pos,
4729 (Duplicate_Subexpr_Move_Checks (Aggr_Lo))),
4730 Right_Opnd => Make_Integer_Literal (Loc, Nb_Elements - 1)),
4733 Make_Attribute_Reference (Loc,
4734 Prefix => New_Reference_To (Ind_Typ, Loc),
4735 Attribute_Name => Name_Pos,
4736 Expressions => New_List (
4737 Duplicate_Subexpr_Move_Checks (Aggr_Hi))));
4739 -- If we are dealing with an aggregate containing an others choice
4740 -- and discrete choices we generate the following test:
4742 -- [constraint_error when
4743 -- Choices_Lo < Aggr_Lo or else Choices_Hi > Aggr_Hi];
4751 Duplicate_Subexpr_Move_Checks (Choices_Lo),
4753 Duplicate_Subexpr_Move_Checks (Aggr_Lo)),
4758 Duplicate_Subexpr (Choices_Hi),
4760 Duplicate_Subexpr (Aggr_Hi)));
4763 if Present (Cond) then
4765 Make_Raise_Constraint_Error (Loc,
4767 Reason => CE_Length_Check_Failed));
4768 -- Questionable reason code, shouldn't that be a
4769 -- CE_Range_Check_Failed ???
4772 -- Now look inside the sub-aggregate to see if there is more work
4774 if Dim < Aggr_Dimension then
4776 -- Process positional components
4778 if Present (Expressions (Sub_Aggr)) then
4779 Expr := First (Expressions (Sub_Aggr));
4780 while Present (Expr) loop
4781 Others_Check (Expr, Dim + 1);
4786 -- Process component associations
4788 if Present (Component_Associations (Sub_Aggr)) then
4789 Assoc := First (Component_Associations (Sub_Aggr));
4790 while Present (Assoc) loop
4791 Expr := Expression (Assoc);
4792 Others_Check (Expr, Dim + 1);
4799 -- Remaining Expand_Array_Aggregate variables
4802 -- Holds the temporary aggregate value
4805 -- Holds the declaration of Tmp
4807 Aggr_Code : List_Id;
4808 Parent_Node : Node_Id;
4809 Parent_Kind : Node_Kind;
4811 -- Start of processing for Expand_Array_Aggregate
4814 -- Do not touch the special aggregates of attributes used for Asm calls
4816 if Is_RTE (Ctyp, RE_Asm_Input_Operand)
4817 or else Is_RTE (Ctyp, RE_Asm_Output_Operand)
4822 -- If the semantic analyzer has determined that aggregate N will raise
4823 -- Constraint_Error at run-time, then the aggregate node has been
4824 -- replaced with an N_Raise_Constraint_Error node and we should
4827 pragma Assert (not Raises_Constraint_Error (N));
4831 -- Check that the index range defined by aggregate bounds is
4832 -- compatible with corresponding index subtype.
4834 Index_Compatibility_Check : declare
4835 Aggr_Index_Range : Node_Id := First_Index (Typ);
4836 -- The current aggregate index range
4838 Index_Constraint : Node_Id := First_Index (Etype (Typ));
4839 -- The corresponding index constraint against which we have to
4840 -- check the above aggregate index range.
4843 Compute_Others_Present (N, 1);
4845 for J in 1 .. Aggr_Dimension loop
4846 -- There is no need to emit a check if an others choice is
4847 -- present for this array aggregate dimension since in this
4848 -- case one of N's sub-aggregates has taken its bounds from the
4849 -- context and these bounds must have been checked already. In
4850 -- addition all sub-aggregates corresponding to the same
4851 -- dimension must all have the same bounds (checked in (c) below).
4853 if not Range_Checks_Suppressed (Etype (Index_Constraint))
4854 and then not Others_Present (J)
4856 -- We don't use Checks.Apply_Range_Check here because it emits
4857 -- a spurious check. Namely it checks that the range defined by
4858 -- the aggregate bounds is non empty. But we know this already
4861 Check_Bounds (Aggr_Index_Range, Index_Constraint);
4864 -- Save the low and high bounds of the aggregate index as well as
4865 -- the index type for later use in checks (b) and (c) below.
4867 Aggr_Low (J) := Low_Bound (Aggr_Index_Range);
4868 Aggr_High (J) := High_Bound (Aggr_Index_Range);
4870 Aggr_Index_Typ (J) := Etype (Index_Constraint);
4872 Next_Index (Aggr_Index_Range);
4873 Next_Index (Index_Constraint);
4875 end Index_Compatibility_Check;
4879 -- If an others choice is present check that no aggregate index is
4880 -- outside the bounds of the index constraint.
4882 Others_Check (N, 1);
4886 -- For multidimensional arrays make sure that all subaggregates
4887 -- corresponding to the same dimension have the same bounds.
4889 if Aggr_Dimension > 1 then
4890 Check_Same_Aggr_Bounds (N, 1);
4895 -- Here we test for is packed array aggregate that we can handle at
4896 -- compile time. If so, return with transformation done. Note that we do
4897 -- this even if the aggregate is nested, because once we have done this
4898 -- processing, there is no more nested aggregate!
4900 if Packed_Array_Aggregate_Handled (N) then
4904 -- At this point we try to convert to positional form
4906 if Ekind (Current_Scope) = E_Package
4907 and then Static_Elaboration_Desired (Current_Scope)
4909 Convert_To_Positional (N, Max_Others_Replicate => 100);
4912 Convert_To_Positional (N);
4915 -- if the result is no longer an aggregate (e.g. it may be a string
4916 -- literal, or a temporary which has the needed value), then we are
4917 -- done, since there is no longer a nested aggregate.
4919 if Nkind (N) /= N_Aggregate then
4922 -- We are also done if the result is an analyzed aggregate
4923 -- This case could use more comments ???
4926 and then N /= Original_Node (N)
4931 -- If all aggregate components are compile-time known and the aggregate
4932 -- has been flattened, nothing left to do. The same occurs if the
4933 -- aggregate is used to initialize the components of an statically
4934 -- allocated dispatch table.
4936 if Compile_Time_Known_Aggregate (N)
4937 or else Is_Static_Dispatch_Table_Aggregate (N)
4939 Set_Expansion_Delayed (N, False);
4943 -- Now see if back end processing is possible
4945 if Backend_Processing_Possible (N) then
4947 -- If the aggregate is static but the constraints are not, build
4948 -- a static subtype for the aggregate, so that Gigi can place it
4949 -- in static memory. Perform an unchecked_conversion to the non-
4950 -- static type imposed by the context.
4953 Itype : constant Entity_Id := Etype (N);
4955 Needs_Type : Boolean := False;
4958 Index := First_Index (Itype);
4959 while Present (Index) loop
4960 if not Is_Static_Subtype (Etype (Index)) then
4969 Build_Constrained_Type (Positional => True);
4970 Rewrite (N, Unchecked_Convert_To (Itype, N));
4980 -- Delay expansion for nested aggregates: it will be taken care of
4981 -- when the parent aggregate is expanded.
4983 Parent_Node := Parent (N);
4984 Parent_Kind := Nkind (Parent_Node);
4986 if Parent_Kind = N_Qualified_Expression then
4987 Parent_Node := Parent (Parent_Node);
4988 Parent_Kind := Nkind (Parent_Node);
4991 if Parent_Kind = N_Aggregate
4992 or else Parent_Kind = N_Extension_Aggregate
4993 or else Parent_Kind = N_Component_Association
4994 or else (Parent_Kind = N_Object_Declaration
4995 and then Needs_Finalization (Typ))
4996 or else (Parent_Kind = N_Assignment_Statement
4997 and then Inside_Init_Proc)
4999 if Static_Array_Aggregate (N)
5000 or else Compile_Time_Known_Aggregate (N)
5002 Set_Expansion_Delayed (N, False);
5005 Set_Expansion_Delayed (N);
5012 -- Look if in place aggregate expansion is possible
5014 -- For object declarations we build the aggregate in place, unless
5015 -- the array is bit-packed or the component is controlled.
5017 -- For assignments we do the assignment in place if all the component
5018 -- associations have compile-time known values. For other cases we
5019 -- create a temporary. The analysis for safety of on-line assignment
5020 -- is delicate, i.e. we don't know how to do it fully yet ???
5022 -- For allocators we assign to the designated object in place if the
5023 -- aggregate meets the same conditions as other in-place assignments.
5024 -- In this case the aggregate may not come from source but was created
5025 -- for default initialization, e.g. with Initialize_Scalars.
5027 if Requires_Transient_Scope (Typ) then
5028 Establish_Transient_Scope
5029 (N, Sec_Stack => Has_Controlled_Component (Typ));
5032 if Has_Default_Init_Comps (N) then
5033 Maybe_In_Place_OK := False;
5035 elsif Is_Bit_Packed_Array (Typ)
5036 or else Has_Controlled_Component (Typ)
5038 Maybe_In_Place_OK := False;
5041 Maybe_In_Place_OK :=
5042 (Nkind (Parent (N)) = N_Assignment_Statement
5043 and then Comes_From_Source (N)
5044 and then In_Place_Assign_OK)
5047 (Nkind (Parent (Parent (N))) = N_Allocator
5048 and then In_Place_Assign_OK);
5051 -- If this is an array of tasks, it will be expanded into build-in-place
5052 -- assignments. Build an activation chain for the tasks now.
5054 if Has_Task (Etype (N)) then
5055 Build_Activation_Chain_Entity (N);
5058 if not Has_Default_Init_Comps (N)
5059 and then Comes_From_Source (Parent (N))
5060 and then Nkind (Parent (N)) = N_Object_Declaration
5062 Must_Slide (Etype (Defining_Identifier (Parent (N))), Typ)
5063 and then N = Expression (Parent (N))
5064 and then not Is_Bit_Packed_Array (Typ)
5065 and then not Has_Controlled_Component (Typ)
5066 and then not Has_Address_Clause (Parent (N))
5068 Tmp := Defining_Identifier (Parent (N));
5069 Set_No_Initialization (Parent (N));
5070 Set_Expression (Parent (N), Empty);
5072 -- Set the type of the entity, for use in the analysis of the
5073 -- subsequent indexed assignments. If the nominal type is not
5074 -- constrained, build a subtype from the known bounds of the
5075 -- aggregate. If the declaration has a subtype mark, use it,
5076 -- otherwise use the itype of the aggregate.
5078 if not Is_Constrained (Typ) then
5079 Build_Constrained_Type (Positional => False);
5080 elsif Is_Entity_Name (Object_Definition (Parent (N)))
5081 and then Is_Constrained (Entity (Object_Definition (Parent (N))))
5083 Set_Etype (Tmp, Entity (Object_Definition (Parent (N))));
5085 Set_Size_Known_At_Compile_Time (Typ, False);
5086 Set_Etype (Tmp, Typ);
5089 elsif Maybe_In_Place_OK
5090 and then Nkind (Parent (N)) = N_Qualified_Expression
5091 and then Nkind (Parent (Parent (N))) = N_Allocator
5093 Set_Expansion_Delayed (N);
5096 -- In the remaining cases the aggregate is the RHS of an assignment
5098 elsif Maybe_In_Place_OK
5099 and then Is_Entity_Name (Name (Parent (N)))
5101 Tmp := Entity (Name (Parent (N)));
5103 if Etype (Tmp) /= Etype (N) then
5104 Apply_Length_Check (N, Etype (Tmp));
5106 if Nkind (N) = N_Raise_Constraint_Error then
5108 -- Static error, nothing further to expand
5114 elsif Maybe_In_Place_OK
5115 and then Nkind (Name (Parent (N))) = N_Explicit_Dereference
5116 and then Is_Entity_Name (Prefix (Name (Parent (N))))
5118 Tmp := Name (Parent (N));
5120 if Etype (Tmp) /= Etype (N) then
5121 Apply_Length_Check (N, Etype (Tmp));
5124 elsif Maybe_In_Place_OK
5125 and then Nkind (Name (Parent (N))) = N_Slice
5126 and then Safe_Slice_Assignment (N)
5128 -- Safe_Slice_Assignment rewrites assignment as a loop
5134 -- In place aggregate expansion is not possible
5137 Maybe_In_Place_OK := False;
5138 Tmp := Make_Defining_Identifier (Loc, New_Internal_Name ('A'));
5140 Make_Object_Declaration
5142 Defining_Identifier => Tmp,
5143 Object_Definition => New_Occurrence_Of (Typ, Loc));
5144 Set_No_Initialization (Tmp_Decl, True);
5146 -- If we are within a loop, the temporary will be pushed on the
5147 -- stack at each iteration. If the aggregate is the expression for an
5148 -- allocator, it will be immediately copied to the heap and can
5149 -- be reclaimed at once. We create a transient scope around the
5150 -- aggregate for this purpose.
5152 if Ekind (Current_Scope) = E_Loop
5153 and then Nkind (Parent (Parent (N))) = N_Allocator
5155 Establish_Transient_Scope (N, False);
5158 Insert_Action (N, Tmp_Decl);
5161 -- Construct and insert the aggregate code. We can safely suppress index
5162 -- checks because this code is guaranteed not to raise CE on index
5163 -- checks. However we should *not* suppress all checks.
5169 if Nkind (Tmp) = N_Defining_Identifier then
5170 Target := New_Reference_To (Tmp, Loc);
5174 if Has_Default_Init_Comps (N) then
5176 -- Ada 2005 (AI-287): This case has not been analyzed???
5178 raise Program_Error;
5181 -- Name in assignment is explicit dereference
5183 Target := New_Copy (Tmp);
5187 Build_Array_Aggr_Code (N,
5189 Index => First_Index (Typ),
5191 Scalar_Comp => Is_Scalar_Type (Ctyp));
5194 if Comes_From_Source (Tmp) then
5195 Insert_Actions_After (Parent (N), Aggr_Code);
5198 Insert_Actions (N, Aggr_Code);
5201 -- If the aggregate has been assigned in place, remove the original
5204 if Nkind (Parent (N)) = N_Assignment_Statement
5205 and then Maybe_In_Place_OK
5207 Rewrite (Parent (N), Make_Null_Statement (Loc));
5209 elsif Nkind (Parent (N)) /= N_Object_Declaration
5210 or else Tmp /= Defining_Identifier (Parent (N))
5212 Rewrite (N, New_Occurrence_Of (Tmp, Loc));
5213 Analyze_And_Resolve (N, Typ);
5215 end Expand_Array_Aggregate;
5217 ------------------------
5218 -- Expand_N_Aggregate --
5219 ------------------------
5221 procedure Expand_N_Aggregate (N : Node_Id) is
5223 if Is_Record_Type (Etype (N)) then
5224 Expand_Record_Aggregate (N);
5226 Expand_Array_Aggregate (N);
5229 when RE_Not_Available =>
5231 end Expand_N_Aggregate;
5233 ----------------------------------
5234 -- Expand_N_Extension_Aggregate --
5235 ----------------------------------
5237 -- If the ancestor part is an expression, add a component association for
5238 -- the parent field. If the type of the ancestor part is not the direct
5239 -- parent of the expected type, build recursively the needed ancestors.
5240 -- If the ancestor part is a subtype_mark, replace aggregate with a decla-
5241 -- ration for a temporary of the expected type, followed by individual
5242 -- assignments to the given components.
5244 procedure Expand_N_Extension_Aggregate (N : Node_Id) is
5245 Loc : constant Source_Ptr := Sloc (N);
5246 A : constant Node_Id := Ancestor_Part (N);
5247 Typ : constant Entity_Id := Etype (N);
5250 -- If the ancestor is a subtype mark, an init proc must be called
5251 -- on the resulting object which thus has to be materialized in
5254 if Is_Entity_Name (A) and then Is_Type (Entity (A)) then
5255 Convert_To_Assignments (N, Typ);
5257 -- The extension aggregate is transformed into a record aggregate
5258 -- of the following form (c1 and c2 are inherited components)
5260 -- (Exp with c3 => a, c4 => b)
5261 -- ==> (c1 => Exp.c1, c2 => Exp.c2, c1 => a, c2 => b)
5266 if VM_Target = No_VM then
5267 Expand_Record_Aggregate (N,
5270 (Node (First_Elmt (Access_Disp_Table (Typ))), Loc),
5273 -- No tag is needed in the case of a VM
5274 Expand_Record_Aggregate (N,
5280 when RE_Not_Available =>
5282 end Expand_N_Extension_Aggregate;
5284 -----------------------------
5285 -- Expand_Record_Aggregate --
5286 -----------------------------
5288 procedure Expand_Record_Aggregate
5290 Orig_Tag : Node_Id := Empty;
5291 Parent_Expr : Node_Id := Empty)
5293 Loc : constant Source_Ptr := Sloc (N);
5294 Comps : constant List_Id := Component_Associations (N);
5295 Typ : constant Entity_Id := Etype (N);
5296 Base_Typ : constant Entity_Id := Base_Type (Typ);
5298 Static_Components : Boolean := True;
5299 -- Flag to indicate whether all components are compile-time known,
5300 -- and the aggregate can be constructed statically and handled by
5303 function Component_Not_OK_For_Backend return Boolean;
5304 -- Check for presence of component which makes it impossible for the
5305 -- backend to process the aggregate, thus requiring the use of a series
5306 -- of assignment statements. Cases checked for are a nested aggregate
5307 -- needing Late_Expansion, the presence of a tagged component which may
5308 -- need tag adjustment, and a bit unaligned component reference.
5310 -- We also force expansion into assignments if a component is of a
5311 -- mutable type (including a private type with discriminants) because
5312 -- in that case the size of the component to be copied may be smaller
5313 -- than the side of the target, and there is no simple way for gigi
5314 -- to compute the size of the object to be copied.
5316 -- NOTE: This is part of the ongoing work to define precisely the
5317 -- interface between front-end and back-end handling of aggregates.
5318 -- In general it is desirable to pass aggregates as they are to gigi,
5319 -- in order to minimize elaboration code. This is one case where the
5320 -- semantics of Ada complicate the analysis and lead to anomalies in
5321 -- the gcc back-end if the aggregate is not expanded into assignments.
5323 ----------------------------------
5324 -- Component_Not_OK_For_Backend --
5325 ----------------------------------
5327 function Component_Not_OK_For_Backend return Boolean is
5337 while Present (C) loop
5338 if Nkind (Expression (C)) = N_Qualified_Expression then
5339 Expr_Q := Expression (Expression (C));
5341 Expr_Q := Expression (C);
5344 -- Return true if the aggregate has any associations for tagged
5345 -- components that may require tag adjustment.
5347 -- These are cases where the source expression may have a tag that
5348 -- could differ from the component tag (e.g., can occur for type
5349 -- conversions and formal parameters). (Tag adjustment not needed
5350 -- if VM_Target because object tags are implicit in the machine.)
5352 if Is_Tagged_Type (Etype (Expr_Q))
5353 and then (Nkind (Expr_Q) = N_Type_Conversion
5354 or else (Is_Entity_Name (Expr_Q)
5356 Ekind (Entity (Expr_Q)) in Formal_Kind))
5357 and then VM_Target = No_VM
5359 Static_Components := False;
5362 elsif Is_Delayed_Aggregate (Expr_Q) then
5363 Static_Components := False;
5366 elsif Possible_Bit_Aligned_Component (Expr_Q) then
5367 Static_Components := False;
5371 if Is_Scalar_Type (Etype (Expr_Q)) then
5372 if not Compile_Time_Known_Value (Expr_Q) then
5373 Static_Components := False;
5376 elsif Nkind (Expr_Q) /= N_Aggregate
5377 or else not Compile_Time_Known_Aggregate (Expr_Q)
5379 Static_Components := False;
5381 if Is_Private_Type (Etype (Expr_Q))
5382 and then Has_Discriminants (Etype (Expr_Q))
5392 end Component_Not_OK_For_Backend;
5394 -- Remaining Expand_Record_Aggregate variables
5396 Tag_Value : Node_Id;
5400 -- Start of processing for Expand_Record_Aggregate
5403 -- If the aggregate is to be assigned to an atomic variable, we
5404 -- have to prevent a piecemeal assignment even if the aggregate
5405 -- is to be expanded. We create a temporary for the aggregate, and
5406 -- assign the temporary instead, so that the back end can generate
5407 -- an atomic move for it.
5410 and then Nkind_In (Parent (N), N_Object_Declaration,
5411 N_Assignment_Statement)
5412 and then Comes_From_Source (Parent (N))
5414 Expand_Atomic_Aggregate (N, Typ);
5417 -- No special management required for aggregates used to initialize
5418 -- statically allocated dispatch tables
5420 elsif Is_Static_Dispatch_Table_Aggregate (N) then
5424 -- Ada 2005 (AI-318-2): We need to convert to assignments if components
5425 -- are build-in-place function calls. This test could be more specific,
5426 -- but doing it for all inherently limited aggregates seems harmless.
5427 -- The assignments will turn into build-in-place function calls (see
5428 -- Make_Build_In_Place_Call_In_Assignment).
5430 if Ada_Version >= Ada_05 and then Is_Inherently_Limited_Type (Typ) then
5431 Convert_To_Assignments (N, Typ);
5433 -- Gigi doesn't handle properly temporaries of variable size
5434 -- so we generate it in the front-end
5436 elsif not Size_Known_At_Compile_Time (Typ) then
5437 Convert_To_Assignments (N, Typ);
5439 -- Temporaries for controlled aggregates need to be attached to a
5440 -- final chain in order to be properly finalized, so it has to
5441 -- be created in the front-end
5443 elsif Is_Controlled (Typ)
5444 or else Has_Controlled_Component (Base_Type (Typ))
5446 Convert_To_Assignments (N, Typ);
5448 -- Ada 2005 (AI-287): In case of default initialized components we
5449 -- convert the aggregate into assignments.
5451 elsif Has_Default_Init_Comps (N) then
5452 Convert_To_Assignments (N, Typ);
5456 elsif Component_Not_OK_For_Backend then
5457 Convert_To_Assignments (N, Typ);
5459 -- If an ancestor is private, some components are not inherited and
5460 -- we cannot expand into a record aggregate
5462 elsif Has_Private_Ancestor (Typ) then
5463 Convert_To_Assignments (N, Typ);
5465 -- ??? The following was done to compile fxacc00.ads in the ACVCs. Gigi
5466 -- is not able to handle the aggregate for Late_Request.
5468 elsif Is_Tagged_Type (Typ) and then Has_Discriminants (Typ) then
5469 Convert_To_Assignments (N, Typ);
5471 -- If the tagged types covers interface types we need to initialize all
5472 -- hidden components containing pointers to secondary dispatch tables.
5474 elsif Is_Tagged_Type (Typ) and then Has_Interfaces (Typ) then
5475 Convert_To_Assignments (N, Typ);
5477 -- If some components are mutable, the size of the aggregate component
5478 -- may be distinct from the default size of the type component, so
5479 -- we need to expand to insure that the back-end copies the proper
5480 -- size of the data.
5482 elsif Has_Mutable_Components (Typ) then
5483 Convert_To_Assignments (N, Typ);
5485 -- If the type involved has any non-bit aligned components, then we are
5486 -- not sure that the back end can handle this case correctly.
5488 elsif Type_May_Have_Bit_Aligned_Components (Typ) then
5489 Convert_To_Assignments (N, Typ);
5491 -- In all other cases, build a proper aggregate handlable by gigi
5494 if Nkind (N) = N_Aggregate then
5496 -- If the aggregate is static and can be handled by the back-end,
5497 -- nothing left to do.
5499 if Static_Components then
5500 Set_Compile_Time_Known_Aggregate (N);
5501 Set_Expansion_Delayed (N, False);
5505 -- If no discriminants, nothing special to do
5507 if not Has_Discriminants (Typ) then
5510 -- Case of discriminants present
5512 elsif Is_Derived_Type (Typ) then
5514 -- For untagged types, non-stored discriminants are replaced
5515 -- with stored discriminants, which are the ones that gigi uses
5516 -- to describe the type and its components.
5518 Generate_Aggregate_For_Derived_Type : declare
5519 Constraints : constant List_Id := New_List;
5520 First_Comp : Node_Id;
5521 Discriminant : Entity_Id;
5523 Num_Disc : Int := 0;
5524 Num_Gird : Int := 0;
5526 procedure Prepend_Stored_Values (T : Entity_Id);
5527 -- Scan the list of stored discriminants of the type, and add
5528 -- their values to the aggregate being built.
5530 ---------------------------
5531 -- Prepend_Stored_Values --
5532 ---------------------------
5534 procedure Prepend_Stored_Values (T : Entity_Id) is
5536 Discriminant := First_Stored_Discriminant (T);
5537 while Present (Discriminant) loop
5539 Make_Component_Association (Loc,
5541 New_List (New_Occurrence_Of (Discriminant, Loc)),
5545 Get_Discriminant_Value (
5548 Discriminant_Constraint (Typ))));
5550 if No (First_Comp) then
5551 Prepend_To (Component_Associations (N), New_Comp);
5553 Insert_After (First_Comp, New_Comp);
5556 First_Comp := New_Comp;
5557 Next_Stored_Discriminant (Discriminant);
5559 end Prepend_Stored_Values;
5561 -- Start of processing for Generate_Aggregate_For_Derived_Type
5564 -- Remove the associations for the discriminant of derived type
5566 First_Comp := First (Component_Associations (N));
5567 while Present (First_Comp) loop
5572 (First (Choices (Comp)))) = E_Discriminant
5575 Num_Disc := Num_Disc + 1;
5579 -- Insert stored discriminant associations in the correct
5580 -- order. If there are more stored discriminants than new
5581 -- discriminants, there is at least one new discriminant that
5582 -- constrains more than one of the stored discriminants. In
5583 -- this case we need to construct a proper subtype of the
5584 -- parent type, in order to supply values to all the
5585 -- components. Otherwise there is one-one correspondence
5586 -- between the constraints and the stored discriminants.
5588 First_Comp := Empty;
5590 Discriminant := First_Stored_Discriminant (Base_Type (Typ));
5591 while Present (Discriminant) loop
5592 Num_Gird := Num_Gird + 1;
5593 Next_Stored_Discriminant (Discriminant);
5596 -- Case of more stored discriminants than new discriminants
5598 if Num_Gird > Num_Disc then
5600 -- Create a proper subtype of the parent type, which is the
5601 -- proper implementation type for the aggregate, and convert
5602 -- it to the intended target type.
5604 Discriminant := First_Stored_Discriminant (Base_Type (Typ));
5605 while Present (Discriminant) loop
5608 Get_Discriminant_Value (
5611 Discriminant_Constraint (Typ)));
5612 Append (New_Comp, Constraints);
5613 Next_Stored_Discriminant (Discriminant);
5617 Make_Subtype_Declaration (Loc,
5618 Defining_Identifier =>
5619 Make_Defining_Identifier (Loc,
5620 New_Internal_Name ('T')),
5621 Subtype_Indication =>
5622 Make_Subtype_Indication (Loc,
5624 New_Occurrence_Of (Etype (Base_Type (Typ)), Loc),
5626 Make_Index_Or_Discriminant_Constraint
5627 (Loc, Constraints)));
5629 Insert_Action (N, Decl);
5630 Prepend_Stored_Values (Base_Type (Typ));
5632 Set_Etype (N, Defining_Identifier (Decl));
5635 Rewrite (N, Unchecked_Convert_To (Typ, N));
5638 -- Case where we do not have fewer new discriminants than
5639 -- stored discriminants, so in this case we can simply use the
5640 -- stored discriminants of the subtype.
5643 Prepend_Stored_Values (Typ);
5645 end Generate_Aggregate_For_Derived_Type;
5648 if Is_Tagged_Type (Typ) then
5650 -- The tagged case, _parent and _tag component must be created
5652 -- Reset null_present unconditionally. tagged records always have
5653 -- at least one field (the tag or the parent)
5655 Set_Null_Record_Present (N, False);
5657 -- When the current aggregate comes from the expansion of an
5658 -- extension aggregate, the parent expr is replaced by an
5659 -- aggregate formed by selected components of this expr
5661 if Present (Parent_Expr)
5662 and then Is_Empty_List (Comps)
5664 Comp := First_Component_Or_Discriminant (Typ);
5665 while Present (Comp) loop
5667 -- Skip all expander-generated components
5670 not Comes_From_Source (Original_Record_Component (Comp))
5676 Make_Selected_Component (Loc,
5678 Unchecked_Convert_To (Typ,
5679 Duplicate_Subexpr (Parent_Expr, True)),
5681 Selector_Name => New_Occurrence_Of (Comp, Loc));
5684 Make_Component_Association (Loc,
5686 New_List (New_Occurrence_Of (Comp, Loc)),
5690 Analyze_And_Resolve (New_Comp, Etype (Comp));
5693 Next_Component_Or_Discriminant (Comp);
5697 -- Compute the value for the Tag now, if the type is a root it
5698 -- will be included in the aggregate right away, otherwise it will
5699 -- be propagated to the parent aggregate
5701 if Present (Orig_Tag) then
5702 Tag_Value := Orig_Tag;
5703 elsif VM_Target /= No_VM then
5708 (Node (First_Elmt (Access_Disp_Table (Typ))), Loc);
5711 -- For a derived type, an aggregate for the parent is formed with
5712 -- all the inherited components.
5714 if Is_Derived_Type (Typ) then
5717 First_Comp : Node_Id;
5718 Parent_Comps : List_Id;
5719 Parent_Aggr : Node_Id;
5720 Parent_Name : Node_Id;
5723 -- Remove the inherited component association from the
5724 -- aggregate and store them in the parent aggregate
5726 First_Comp := First (Component_Associations (N));
5727 Parent_Comps := New_List;
5728 while Present (First_Comp)
5729 and then Scope (Original_Record_Component (
5730 Entity (First (Choices (First_Comp))))) /= Base_Typ
5735 Append (Comp, Parent_Comps);
5738 Parent_Aggr := Make_Aggregate (Loc,
5739 Component_Associations => Parent_Comps);
5740 Set_Etype (Parent_Aggr, Etype (Base_Type (Typ)));
5742 -- Find the _parent component
5744 Comp := First_Component (Typ);
5745 while Chars (Comp) /= Name_uParent loop
5746 Comp := Next_Component (Comp);
5749 Parent_Name := New_Occurrence_Of (Comp, Loc);
5751 -- Insert the parent aggregate
5753 Prepend_To (Component_Associations (N),
5754 Make_Component_Association (Loc,
5755 Choices => New_List (Parent_Name),
5756 Expression => Parent_Aggr));
5758 -- Expand recursively the parent propagating the right Tag
5760 Expand_Record_Aggregate (
5761 Parent_Aggr, Tag_Value, Parent_Expr);
5764 -- For a root type, the tag component is added (unless compiling
5765 -- for the VMs, where tags are implicit).
5767 elsif VM_Target = No_VM then
5769 Tag_Name : constant Node_Id :=
5771 (First_Tag_Component (Typ), Loc);
5772 Typ_Tag : constant Entity_Id := RTE (RE_Tag);
5773 Conv_Node : constant Node_Id :=
5774 Unchecked_Convert_To (Typ_Tag, Tag_Value);
5777 Set_Etype (Conv_Node, Typ_Tag);
5778 Prepend_To (Component_Associations (N),
5779 Make_Component_Association (Loc,
5780 Choices => New_List (Tag_Name),
5781 Expression => Conv_Node));
5787 end Expand_Record_Aggregate;
5789 ----------------------------
5790 -- Has_Default_Init_Comps --
5791 ----------------------------
5793 function Has_Default_Init_Comps (N : Node_Id) return Boolean is
5794 Comps : constant List_Id := Component_Associations (N);
5798 pragma Assert (Nkind_In (N, N_Aggregate, N_Extension_Aggregate));
5804 if Has_Self_Reference (N) then
5808 -- Check if any direct component has default initialized components
5811 while Present (C) loop
5812 if Box_Present (C) then
5819 -- Recursive call in case of aggregate expression
5822 while Present (C) loop
5823 Expr := Expression (C);
5827 Nkind_In (Expr, N_Aggregate, N_Extension_Aggregate)
5828 and then Has_Default_Init_Comps (Expr)
5837 end Has_Default_Init_Comps;
5839 --------------------------
5840 -- Is_Delayed_Aggregate --
5841 --------------------------
5843 function Is_Delayed_Aggregate (N : Node_Id) return Boolean is
5844 Node : Node_Id := N;
5845 Kind : Node_Kind := Nkind (Node);
5848 if Kind = N_Qualified_Expression then
5849 Node := Expression (Node);
5850 Kind := Nkind (Node);
5853 if Kind /= N_Aggregate and then Kind /= N_Extension_Aggregate then
5856 return Expansion_Delayed (Node);
5858 end Is_Delayed_Aggregate;
5860 ----------------------------------------
5861 -- Is_Static_Dispatch_Table_Aggregate --
5862 ----------------------------------------
5864 function Is_Static_Dispatch_Table_Aggregate (N : Node_Id) return Boolean is
5865 Typ : constant Entity_Id := Base_Type (Etype (N));
5868 return Static_Dispatch_Tables
5869 and then VM_Target = No_VM
5870 and then RTU_Loaded (Ada_Tags)
5872 -- Avoid circularity when rebuilding the compiler
5874 and then Cunit_Entity (Get_Source_Unit (N)) /= RTU_Entity (Ada_Tags)
5875 and then (Typ = RTE (RE_Dispatch_Table_Wrapper)
5877 Typ = RTE (RE_Address_Array)
5879 Typ = RTE (RE_Type_Specific_Data)
5881 Typ = RTE (RE_Tag_Table)
5883 (RTE_Available (RE_Interface_Data)
5884 and then Typ = RTE (RE_Interface_Data))
5886 (RTE_Available (RE_Interfaces_Array)
5887 and then Typ = RTE (RE_Interfaces_Array))
5889 (RTE_Available (RE_Interface_Data_Element)
5890 and then Typ = RTE (RE_Interface_Data_Element)));
5891 end Is_Static_Dispatch_Table_Aggregate;
5893 --------------------
5894 -- Late_Expansion --
5895 --------------------
5897 function Late_Expansion
5901 Flist : Node_Id := Empty;
5902 Obj : Entity_Id := Empty) return List_Id
5905 if Is_Record_Type (Etype (N)) then
5906 return Build_Record_Aggr_Code (N, Typ, Target, Flist, Obj);
5908 else pragma Assert (Is_Array_Type (Etype (N)));
5910 Build_Array_Aggr_Code
5912 Ctype => Component_Type (Etype (N)),
5913 Index => First_Index (Typ),
5915 Scalar_Comp => Is_Scalar_Type (Component_Type (Typ)),
5921 ----------------------------------
5922 -- Make_OK_Assignment_Statement --
5923 ----------------------------------
5925 function Make_OK_Assignment_Statement
5928 Expression : Node_Id) return Node_Id
5931 Set_Assignment_OK (Name);
5933 return Make_Assignment_Statement (Sloc, Name, Expression);
5934 end Make_OK_Assignment_Statement;
5936 -----------------------
5937 -- Number_Of_Choices --
5938 -----------------------
5940 function Number_Of_Choices (N : Node_Id) return Nat is
5944 Nb_Choices : Nat := 0;
5947 if Present (Expressions (N)) then
5951 Assoc := First (Component_Associations (N));
5952 while Present (Assoc) loop
5953 Choice := First (Choices (Assoc));
5954 while Present (Choice) loop
5955 if Nkind (Choice) /= N_Others_Choice then
5956 Nb_Choices := Nb_Choices + 1;
5966 end Number_Of_Choices;
5968 ------------------------------------
5969 -- Packed_Array_Aggregate_Handled --
5970 ------------------------------------
5972 -- The current version of this procedure will handle at compile time
5973 -- any array aggregate that meets these conditions:
5975 -- One dimensional, bit packed
5976 -- Underlying packed type is modular type
5977 -- Bounds are within 32-bit Int range
5978 -- All bounds and values are static
5980 function Packed_Array_Aggregate_Handled (N : Node_Id) return Boolean is
5981 Loc : constant Source_Ptr := Sloc (N);
5982 Typ : constant Entity_Id := Etype (N);
5983 Ctyp : constant Entity_Id := Component_Type (Typ);
5985 Not_Handled : exception;
5986 -- Exception raised if this aggregate cannot be handled
5989 -- For now, handle only one dimensional bit packed arrays
5991 if not Is_Bit_Packed_Array (Typ)
5992 or else Number_Dimensions (Typ) > 1
5993 or else not Is_Modular_Integer_Type (Packed_Array_Type (Typ))
5998 if not Is_Scalar_Type (Component_Type (Typ))
5999 and then Has_Non_Standard_Rep (Component_Type (Typ))
6005 Csiz : constant Nat := UI_To_Int (Component_Size (Typ));
6009 -- Bounds of index type
6013 -- Values of bounds if compile time known
6015 function Get_Component_Val (N : Node_Id) return Uint;
6016 -- Given a expression value N of the component type Ctyp, returns a
6017 -- value of Csiz (component size) bits representing this value. If
6018 -- the value is non-static or any other reason exists why the value
6019 -- cannot be returned, then Not_Handled is raised.
6021 -----------------------
6022 -- Get_Component_Val --
6023 -----------------------
6025 function Get_Component_Val (N : Node_Id) return Uint is
6029 -- We have to analyze the expression here before doing any further
6030 -- processing here. The analysis of such expressions is deferred
6031 -- till expansion to prevent some problems of premature analysis.
6033 Analyze_And_Resolve (N, Ctyp);
6035 -- Must have a compile time value. String literals have to be
6036 -- converted into temporaries as well, because they cannot easily
6037 -- be converted into their bit representation.
6039 if not Compile_Time_Known_Value (N)
6040 or else Nkind (N) = N_String_Literal
6045 Val := Expr_Rep_Value (N);
6047 -- Adjust for bias, and strip proper number of bits
6049 if Has_Biased_Representation (Ctyp) then
6050 Val := Val - Expr_Value (Type_Low_Bound (Ctyp));
6053 return Val mod Uint_2 ** Csiz;
6054 end Get_Component_Val;
6056 -- Here we know we have a one dimensional bit packed array
6059 Get_Index_Bounds (First_Index (Typ), Lo, Hi);
6061 -- Cannot do anything if bounds are dynamic
6063 if not Compile_Time_Known_Value (Lo)
6065 not Compile_Time_Known_Value (Hi)
6070 -- Or are silly out of range of int bounds
6072 Lob := Expr_Value (Lo);
6073 Hib := Expr_Value (Hi);
6075 if not UI_Is_In_Int_Range (Lob)
6077 not UI_Is_In_Int_Range (Hib)
6082 -- At this stage we have a suitable aggregate for handling at compile
6083 -- time (the only remaining checks are that the values of expressions
6084 -- in the aggregate are compile time known (check is performed by
6085 -- Get_Component_Val), and that any subtypes or ranges are statically
6088 -- If the aggregate is not fully positional at this stage, then
6089 -- convert it to positional form. Either this will fail, in which
6090 -- case we can do nothing, or it will succeed, in which case we have
6091 -- succeeded in handling the aggregate, or it will stay an aggregate,
6092 -- in which case we have failed to handle this case.
6094 if Present (Component_Associations (N)) then
6095 Convert_To_Positional
6096 (N, Max_Others_Replicate => 64, Handle_Bit_Packed => True);
6097 return Nkind (N) /= N_Aggregate;
6100 -- Otherwise we are all positional, so convert to proper value
6103 Lov : constant Int := UI_To_Int (Lob);
6104 Hiv : constant Int := UI_To_Int (Hib);
6106 Len : constant Nat := Int'Max (0, Hiv - Lov + 1);
6107 -- The length of the array (number of elements)
6109 Aggregate_Val : Uint;
6110 -- Value of aggregate. The value is set in the low order bits of
6111 -- this value. For the little-endian case, the values are stored
6112 -- from low-order to high-order and for the big-endian case the
6113 -- values are stored from high-order to low-order. Note that gigi
6114 -- will take care of the conversions to left justify the value in
6115 -- the big endian case (because of left justified modular type
6116 -- processing), so we do not have to worry about that here.
6119 -- Integer literal for resulting constructed value
6122 -- Shift count from low order for next value
6125 -- Shift increment for loop
6128 -- Next expression from positional parameters of aggregate
6131 -- For little endian, we fill up the low order bits of the target
6132 -- value. For big endian we fill up the high order bits of the
6133 -- target value (which is a left justified modular value).
6135 if Bytes_Big_Endian xor Debug_Flag_8 then
6136 Shift := Csiz * (Len - 1);
6143 -- Loop to set the values
6146 Aggregate_Val := Uint_0;
6148 Expr := First (Expressions (N));
6149 Aggregate_Val := Get_Component_Val (Expr) * Uint_2 ** Shift;
6151 for J in 2 .. Len loop
6152 Shift := Shift + Incr;
6155 Aggregate_Val + Get_Component_Val (Expr) * Uint_2 ** Shift;
6159 -- Now we can rewrite with the proper value
6162 Make_Integer_Literal (Loc,
6163 Intval => Aggregate_Val);
6164 Set_Print_In_Hex (Lit);
6166 -- Construct the expression using this literal. Note that it is
6167 -- important to qualify the literal with its proper modular type
6168 -- since universal integer does not have the required range and
6169 -- also this is a left justified modular type, which is important
6170 -- in the big-endian case.
6173 Unchecked_Convert_To (Typ,
6174 Make_Qualified_Expression (Loc,
6176 New_Occurrence_Of (Packed_Array_Type (Typ), Loc),
6177 Expression => Lit)));
6179 Analyze_And_Resolve (N, Typ);
6187 end Packed_Array_Aggregate_Handled;
6189 ----------------------------
6190 -- Has_Mutable_Components --
6191 ----------------------------
6193 function Has_Mutable_Components (Typ : Entity_Id) return Boolean is
6197 Comp := First_Component (Typ);
6198 while Present (Comp) loop
6199 if Is_Record_Type (Etype (Comp))
6200 and then Has_Discriminants (Etype (Comp))
6201 and then not Is_Constrained (Etype (Comp))
6206 Next_Component (Comp);
6210 end Has_Mutable_Components;
6212 ------------------------------
6213 -- Initialize_Discriminants --
6214 ------------------------------
6216 procedure Initialize_Discriminants (N : Node_Id; Typ : Entity_Id) is
6217 Loc : constant Source_Ptr := Sloc (N);
6218 Bas : constant Entity_Id := Base_Type (Typ);
6219 Par : constant Entity_Id := Etype (Bas);
6220 Decl : constant Node_Id := Parent (Par);
6224 if Is_Tagged_Type (Bas)
6225 and then Is_Derived_Type (Bas)
6226 and then Has_Discriminants (Par)
6227 and then Has_Discriminants (Bas)
6228 and then Number_Discriminants (Bas) /= Number_Discriminants (Par)
6229 and then Nkind (Decl) = N_Full_Type_Declaration
6230 and then Nkind (Type_Definition (Decl)) = N_Record_Definition
6232 (Variant_Part (Component_List (Type_Definition (Decl))))
6233 and then Nkind (N) /= N_Extension_Aggregate
6236 -- Call init proc to set discriminants.
6237 -- There should eventually be a special procedure for this ???
6239 Ref := New_Reference_To (Defining_Identifier (N), Loc);
6240 Insert_Actions_After (N,
6241 Build_Initialization_Call (Sloc (N), Ref, Typ));
6243 end Initialize_Discriminants;
6250 (Obj_Type : Entity_Id;
6251 Typ : Entity_Id) return Boolean
6253 L1, L2, H1, H2 : Node_Id;
6255 -- No sliding if the type of the object is not established yet, if it is
6256 -- an unconstrained type whose actual subtype comes from the aggregate,
6257 -- or if the two types are identical.
6259 if not Is_Array_Type (Obj_Type) then
6262 elsif not Is_Constrained (Obj_Type) then
6265 elsif Typ = Obj_Type then
6269 -- Sliding can only occur along the first dimension
6271 Get_Index_Bounds (First_Index (Typ), L1, H1);
6272 Get_Index_Bounds (First_Index (Obj_Type), L2, H2);
6274 if not Is_Static_Expression (L1)
6275 or else not Is_Static_Expression (L2)
6276 or else not Is_Static_Expression (H1)
6277 or else not Is_Static_Expression (H2)
6281 return Expr_Value (L1) /= Expr_Value (L2)
6282 or else Expr_Value (H1) /= Expr_Value (H2);
6287 ---------------------------
6288 -- Safe_Slice_Assignment --
6289 ---------------------------
6291 function Safe_Slice_Assignment (N : Node_Id) return Boolean is
6292 Loc : constant Source_Ptr := Sloc (Parent (N));
6293 Pref : constant Node_Id := Prefix (Name (Parent (N)));
6294 Range_Node : constant Node_Id := Discrete_Range (Name (Parent (N)));
6302 -- Generate: for J in Range loop Pref (J) := Expr; end loop;
6304 if Comes_From_Source (N)
6305 and then No (Expressions (N))
6306 and then Nkind (First (Choices (First (Component_Associations (N)))))
6310 Expression (First (Component_Associations (N)));
6311 L_J := Make_Defining_Identifier (Loc, New_Internal_Name ('J'));
6314 Make_Iteration_Scheme (Loc,
6315 Loop_Parameter_Specification =>
6316 Make_Loop_Parameter_Specification
6318 Defining_Identifier => L_J,
6319 Discrete_Subtype_Definition => Relocate_Node (Range_Node)));
6322 Make_Assignment_Statement (Loc,
6324 Make_Indexed_Component (Loc,
6325 Prefix => Relocate_Node (Pref),
6326 Expressions => New_List (New_Occurrence_Of (L_J, Loc))),
6327 Expression => Relocate_Node (Expr));
6329 -- Construct the final loop
6332 Make_Implicit_Loop_Statement
6333 (Node => Parent (N),
6334 Identifier => Empty,
6335 Iteration_Scheme => L_Iter,
6336 Statements => New_List (L_Body));
6338 -- Set type of aggregate to be type of lhs in assignment,
6339 -- to suppress redundant length checks.
6341 Set_Etype (N, Etype (Name (Parent (N))));
6343 Rewrite (Parent (N), Stat);
6344 Analyze (Parent (N));
6350 end Safe_Slice_Assignment;
6352 ---------------------
6353 -- Sort_Case_Table --
6354 ---------------------
6356 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type) is
6357 L : constant Int := Case_Table'First;
6358 U : constant Int := Case_Table'Last;
6366 T := Case_Table (K + 1);
6370 and then Expr_Value (Case_Table (J - 1).Choice_Lo) >
6371 Expr_Value (T.Choice_Lo)
6373 Case_Table (J) := Case_Table (J - 1);
6377 Case_Table (J) := T;
6380 end Sort_Case_Table;
6382 ----------------------------
6383 -- Static_Array_Aggregate --
6384 ----------------------------
6386 function Static_Array_Aggregate (N : Node_Id) return Boolean is
6387 Bounds : constant Node_Id := Aggregate_Bounds (N);
6389 Typ : constant Entity_Id := Etype (N);
6390 Comp_Type : constant Entity_Id := Component_Type (Typ);
6397 if Is_Tagged_Type (Typ)
6398 or else Is_Controlled (Typ)
6399 or else Is_Packed (Typ)
6405 and then Nkind (Bounds) = N_Range
6406 and then Nkind (Low_Bound (Bounds)) = N_Integer_Literal
6407 and then Nkind (High_Bound (Bounds)) = N_Integer_Literal
6409 Lo := Low_Bound (Bounds);
6410 Hi := High_Bound (Bounds);
6412 if No (Component_Associations (N)) then
6414 -- Verify that all components are static integers
6416 Expr := First (Expressions (N));
6417 while Present (Expr) loop
6418 if Nkind (Expr) /= N_Integer_Literal then
6428 -- We allow only a single named association, either a static
6429 -- range or an others_clause, with a static expression.
6431 Expr := First (Component_Associations (N));
6433 if Present (Expressions (N)) then
6436 elsif Present (Next (Expr)) then
6439 elsif Present (Next (First (Choices (Expr)))) then
6443 -- The aggregate is static if all components are literals,
6444 -- or else all its components are static aggregates for the
6445 -- component type. We also limit the size of a static aggregate
6446 -- to prevent runaway static expressions.
6448 if Is_Array_Type (Comp_Type)
6449 or else Is_Record_Type (Comp_Type)
6451 if Nkind (Expression (Expr)) /= N_Aggregate
6453 not Compile_Time_Known_Aggregate (Expression (Expr))
6458 elsif Nkind (Expression (Expr)) /= N_Integer_Literal then
6461 elsif not Aggr_Size_OK (N, Typ) then
6465 -- Create a positional aggregate with the right number of
6466 -- copies of the expression.
6468 Agg := Make_Aggregate (Sloc (N), New_List, No_List);
6470 for I in UI_To_Int (Intval (Lo)) .. UI_To_Int (Intval (Hi))
6473 (Expressions (Agg), New_Copy (Expression (Expr)));
6475 -- The copied expression must be analyzed and resolved.
6476 -- Besides setting the type, this ensures that static
6477 -- expressions are appropriately marked as such.
6480 (Last (Expressions (Agg)), Component_Type (Typ));
6483 Set_Aggregate_Bounds (Agg, Bounds);
6484 Set_Etype (Agg, Typ);
6487 Set_Compile_Time_Known_Aggregate (N);
6496 end Static_Array_Aggregate;