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 Tbuild; use Tbuild;
60 with Uintp; use Uintp;
62 package body Exp_Aggr is
64 type Case_Bounds is record
67 Choice_Node : Node_Id;
70 type Case_Table_Type is array (Nat range <>) of Case_Bounds;
71 -- Table type used by Check_Case_Choices procedure
74 (Obj_Type : Entity_Id;
75 Typ : Entity_Id) return Boolean;
76 -- A static array aggregate in an object declaration can in most cases be
77 -- expanded in place. The one exception is when the aggregate is given
78 -- with component associations that specify different bounds from those of
79 -- the type definition in the object declaration. In this pathological
80 -- case the aggregate must slide, and we must introduce an intermediate
81 -- temporary to hold it.
83 -- The same holds in an assignment to one-dimensional array of arrays,
84 -- when a component may be given with bounds that differ from those of the
87 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type);
88 -- Sort the Case Table using the Lower Bound of each Choice as the key.
89 -- A simple insertion sort is used since the number of choices in a case
90 -- statement of variant part will usually be small and probably in near
93 function Has_Default_Init_Comps (N : Node_Id) return Boolean;
94 -- N is an aggregate (record or array). Checks the presence of default
95 -- initialization (<>) in any component (Ada 2005: AI-287)
97 function Is_Static_Dispatch_Table_Aggregate (N : Node_Id) return Boolean;
98 -- Returns true if N is an aggregate used to initialize the components
99 -- of an statically allocated dispatch table.
101 ------------------------------------------------------
102 -- Local subprograms for Record Aggregate Expansion --
103 ------------------------------------------------------
105 procedure Expand_Record_Aggregate
107 Orig_Tag : Node_Id := Empty;
108 Parent_Expr : Node_Id := Empty);
109 -- This is the top level procedure for record aggregate expansion.
110 -- Expansion for record aggregates needs expand aggregates for tagged
111 -- record types. Specifically Expand_Record_Aggregate adds the Tag
112 -- field in front of the Component_Association list that was created
113 -- during resolution by Resolve_Record_Aggregate.
115 -- N is the record aggregate node.
116 -- Orig_Tag is the value of the Tag that has to be provided for this
117 -- specific aggregate. It carries the tag corresponding to the type
118 -- of the outermost aggregate during the recursive expansion
119 -- Parent_Expr is the ancestor part of the original extension
122 procedure Convert_To_Assignments (N : Node_Id; Typ : Entity_Id);
123 -- N is an N_Aggregate or an N_Extension_Aggregate. Typ is the type of the
124 -- aggregate (which can only be a record type, this procedure is only used
125 -- for record types). Transform the given aggregate into a sequence of
126 -- assignments performed component by component.
128 function Build_Record_Aggr_Code
132 Flist : Node_Id := Empty;
133 Obj : Entity_Id := Empty;
134 Is_Limited_Ancestor_Expansion : Boolean := False) return List_Id;
135 -- N is an N_Aggregate or an N_Extension_Aggregate. Typ is the type of the
136 -- aggregate. Target is an expression containing the location on which the
137 -- component by component assignments will take place. Returns the list of
138 -- assignments plus all other adjustments needed for tagged and controlled
139 -- types. Flist is an expression representing the finalization list on
140 -- which to attach the controlled components if any. Obj is present in the
141 -- object declaration and dynamic allocation cases, it contains an entity
142 -- that allows to know if the value being created needs to be attached to
143 -- the final list in case of pragma Finalize_Storage_Only.
146 -- The meaning of the Obj formal is extremely unclear. *What* entity
147 -- should be passed? For the object declaration case we may guess that
148 -- this is the object being declared, but what about the allocator case?
150 -- Is_Limited_Ancestor_Expansion indicates that the function has been
151 -- called recursively to expand the limited ancestor to avoid copying it.
153 function Has_Mutable_Components (Typ : Entity_Id) return Boolean;
154 -- Return true if one of the component is of a discriminated type with
155 -- defaults. An aggregate for a type with mutable components must be
156 -- expanded into individual assignments.
158 procedure Initialize_Discriminants (N : Node_Id; Typ : Entity_Id);
159 -- If the type of the aggregate is a type extension with renamed discrimi-
160 -- nants, we must initialize the hidden discriminants of the parent.
161 -- Otherwise, the target object must not be initialized. The discriminants
162 -- are initialized by calling the initialization procedure for the type.
163 -- This is incorrect if the initialization of other components has any
164 -- side effects. We restrict this call to the case where the parent type
165 -- has a variant part, because this is the only case where the hidden
166 -- discriminants are accessed, namely when calling discriminant checking
167 -- functions of the parent type, and when applying a stream attribute to
168 -- an object of the derived type.
170 -----------------------------------------------------
171 -- Local Subprograms for Array Aggregate Expansion --
172 -----------------------------------------------------
174 function Aggr_Size_OK (N : Node_Id; Typ : Entity_Id) return Boolean;
175 -- Very large static aggregates present problems to the back-end, and
176 -- are transformed into assignments and loops. This function verifies
177 -- that the total number of components of an aggregate is acceptable
178 -- for transformation into a purely positional static form. It is called
179 -- prior to calling Flatten.
180 -- This function also detects and warns about one-component aggregates
181 -- that appear in a non-static context. Even if the component value is
182 -- static, such an aggregate must be expanded into an assignment.
184 procedure Convert_Array_Aggr_In_Allocator
188 -- If the aggregate appears within an allocator and can be expanded in
189 -- place, this routine generates the individual assignments to components
190 -- of the designated object. This is an optimization over the general
191 -- case, where a temporary is first created on the stack and then used to
192 -- construct the allocated object on the heap.
194 procedure Convert_To_Positional
196 Max_Others_Replicate : Nat := 5;
197 Handle_Bit_Packed : Boolean := False);
198 -- If possible, convert named notation to positional notation. This
199 -- conversion is possible only in some static cases. If the conversion is
200 -- possible, then N is rewritten with the analyzed converted aggregate.
201 -- The parameter Max_Others_Replicate controls the maximum number of
202 -- values corresponding to an others choice that will be converted to
203 -- positional notation (the default of 5 is the normal limit, and reflects
204 -- the fact that normally the loop is better than a lot of separate
205 -- assignments). Note that this limit gets overridden in any case if
206 -- either of the restrictions No_Elaboration_Code or No_Implicit_Loops is
207 -- set. The parameter Handle_Bit_Packed is usually set False (since we do
208 -- not expect the back end to handle bit packed arrays, so the normal case
209 -- of conversion is pointless), but in the special case of a call from
210 -- Packed_Array_Aggregate_Handled, we set this parameter to True, since
211 -- these are cases we handle in there.
213 procedure Expand_Array_Aggregate (N : Node_Id);
214 -- This is the top-level routine to perform array aggregate expansion.
215 -- N is the N_Aggregate node to be expanded.
217 function Backend_Processing_Possible (N : Node_Id) return Boolean;
218 -- This function checks if array aggregate N can be processed directly
219 -- by Gigi. If this is the case True is returned.
221 function Build_Array_Aggr_Code
226 Scalar_Comp : Boolean;
227 Indices : List_Id := No_List;
228 Flist : Node_Id := Empty) return List_Id;
229 -- This recursive routine returns a list of statements containing the
230 -- loops and assignments that are needed for the expansion of the array
233 -- N is the (sub-)aggregate node to be expanded into code. This node
234 -- has been fully analyzed, and its Etype is properly set.
236 -- Index is the index node corresponding to the array sub-aggregate N.
238 -- Into is the target expression into which we are copying the aggregate.
239 -- Note that this node may not have been analyzed yet, and so the Etype
240 -- field may not be set.
242 -- Scalar_Comp is True if the component type of the aggregate is scalar.
244 -- Indices is the current list of expressions used to index the
245 -- object we are writing into.
247 -- Flist is an expression representing the finalization list on which
248 -- to attach the controlled components if any.
250 function Number_Of_Choices (N : Node_Id) return Nat;
251 -- Returns the number of discrete choices (not including the others choice
252 -- if present) contained in (sub-)aggregate N.
254 function Late_Expansion
258 Flist : Node_Id := Empty;
259 Obj : Entity_Id := Empty) return List_Id;
260 -- N is a nested (record or array) aggregate that has been marked with
261 -- 'Delay_Expansion'. Typ is the expected type of the aggregate and Target
262 -- is a (duplicable) expression that will hold the result of the aggregate
263 -- expansion. Flist is the finalization list to be used to attach
264 -- controlled components. 'Obj' when non empty, carries the original
265 -- object being initialized in order to know if it needs to be attached to
266 -- the previous parameter which may not be the case in the case where
267 -- Finalize_Storage_Only is set. Basically this procedure is used to
268 -- implement top-down expansions of nested aggregates. This is necessary
269 -- for avoiding temporaries at each level as well as for propagating the
270 -- right internal finalization list.
272 function Make_OK_Assignment_Statement
275 Expression : Node_Id) return Node_Id;
276 -- This is like Make_Assignment_Statement, except that Assignment_OK
277 -- is set in the left operand. All assignments built by this unit
278 -- use this routine. This is needed to deal with assignments to
279 -- initialized constants that are done in place.
281 function Packed_Array_Aggregate_Handled (N : Node_Id) return Boolean;
282 -- Given an array aggregate, this function handles the case of a packed
283 -- array aggregate with all constant values, where the aggregate can be
284 -- evaluated at compile time. If this is possible, then N is rewritten
285 -- to be its proper compile time value with all the components properly
286 -- assembled. The expression is analyzed and resolved and True is
287 -- returned. If this transformation is not possible, N is unchanged
288 -- and False is returned
290 function Safe_Slice_Assignment (N : Node_Id) return Boolean;
291 -- If a slice assignment has an aggregate with a single others_choice,
292 -- the assignment can be done in place even if bounds are not static,
293 -- by converting it into a loop over the discrete range of the slice.
299 function Aggr_Size_OK (N : Node_Id; Typ : Entity_Id) return Boolean is
307 -- The following constant determines the maximum size of an
308 -- array aggregate produced by converting named to positional
309 -- notation (e.g. from others clauses). This avoids running
310 -- away with attempts to convert huge aggregates, which hit
311 -- memory limits in the backend.
313 -- The normal limit is 5000, but we increase this limit to
314 -- 2**24 (about 16 million) if Restrictions (No_Elaboration_Code)
315 -- or Restrictions (No_Implicit_Loops) is specified, since in
316 -- either case, we are at risk of declaring the program illegal
317 -- because of this limit.
319 Max_Aggr_Size : constant Nat :=
320 5000 + (2 ** 24 - 5000) *
322 (Restriction_Active (No_Elaboration_Code)
324 Restriction_Active (No_Implicit_Loops));
326 function Component_Count (T : Entity_Id) return Int;
327 -- The limit is applied to the total number of components that the
328 -- aggregate will have, which is the number of static expressions
329 -- that will appear in the flattened array. This requires a recursive
330 -- computation of the number of scalar components of the structure.
332 ---------------------
333 -- Component_Count --
334 ---------------------
336 function Component_Count (T : Entity_Id) return Int is
341 if Is_Scalar_Type (T) then
344 elsif Is_Record_Type (T) then
345 Comp := First_Component (T);
346 while Present (Comp) loop
347 Res := Res + Component_Count (Etype (Comp));
348 Next_Component (Comp);
353 elsif Is_Array_Type (T) then
355 Lo : constant Node_Id :=
356 Type_Low_Bound (Etype (First_Index (T)));
357 Hi : constant Node_Id :=
358 Type_High_Bound (Etype (First_Index (T)));
360 Siz : constant Int := Component_Count (Component_Type (T));
363 if not Compile_Time_Known_Value (Lo)
364 or else not Compile_Time_Known_Value (Hi)
369 Siz * UI_To_Int (Expr_Value (Hi) - Expr_Value (Lo) + 1);
374 -- Can only be a null for an access type
380 -- Start of processing for Aggr_Size_OK
383 Siz := Component_Count (Component_Type (Typ));
385 Indx := First_Index (Typ);
386 while Present (Indx) loop
387 Lo := Type_Low_Bound (Etype (Indx));
388 Hi := Type_High_Bound (Etype (Indx));
390 -- Bounds need to be known at compile time
392 if not Compile_Time_Known_Value (Lo)
393 or else not Compile_Time_Known_Value (Hi)
398 Lov := Expr_Value (Lo);
399 Hiv := Expr_Value (Hi);
401 -- A flat array is always safe
407 -- One-component aggregates are suspicious, and if the context type
408 -- is an object declaration with non-static bounds it will trip gcc;
409 -- such an aggregate must be expanded into a single assignment.
412 and then Nkind (Parent (N)) = N_Object_Declaration
415 Index_Type : constant Entity_Id :=
418 (Etype (Defining_Identifier (Parent (N)))));
422 if not Compile_Time_Known_Value (Type_Low_Bound (Index_Type))
423 or else not Compile_Time_Known_Value
424 (Type_High_Bound (Index_Type))
426 if Present (Component_Associations (N)) then
428 First (Choices (First (Component_Associations (N))));
429 if Is_Entity_Name (Indx)
430 and then not Is_Type (Entity (Indx))
433 ("single component aggregate in non-static context?",
435 Error_Msg_N ("\maybe subtype name was meant?", Indx);
445 Rng : constant Uint := Hiv - Lov + 1;
448 -- Check if size is too large
450 if not UI_Is_In_Int_Range (Rng) then
454 Siz := Siz * UI_To_Int (Rng);
458 or else Siz > Max_Aggr_Size
463 -- Bounds must be in integer range, for later array construction
465 if not UI_Is_In_Int_Range (Lov)
467 not UI_Is_In_Int_Range (Hiv)
478 ---------------------------------
479 -- Backend_Processing_Possible --
480 ---------------------------------
482 -- Backend processing by Gigi/gcc is possible only if all the following
483 -- conditions are met:
485 -- 1. N is fully positional
487 -- 2. N is not a bit-packed array aggregate;
489 -- 3. The size of N's array type must be known at compile time. Note
490 -- that this implies that the component size is also known
492 -- 4. The array type of N does not follow the Fortran layout convention
493 -- or if it does it must be 1 dimensional.
495 -- 5. The array component type may not be tagged (which could necessitate
496 -- reassignment of proper tags).
498 -- 6. The array component type must not have unaligned bit components
500 -- 7. None of the components of the aggregate may be bit unaligned
503 -- 8. There cannot be delayed components, since we do not know enough
504 -- at this stage to know if back end processing is possible.
506 -- 9. There cannot be any discriminated record components, since the
507 -- back end cannot handle this complex case.
509 -- 10. No controlled actions need to be generated for components
511 function Backend_Processing_Possible (N : Node_Id) return Boolean is
512 Typ : constant Entity_Id := Etype (N);
513 -- Typ is the correct constrained array subtype of the aggregate
515 function Component_Check (N : Node_Id; Index : Node_Id) return Boolean;
516 -- This routine checks components of aggregate N, enforcing checks
517 -- 1, 7, 8, and 9. In the multi-dimensional case, these checks are
518 -- performed on subaggregates. The Index value is the current index
519 -- being checked in the multi-dimensional case.
521 ---------------------
522 -- Component_Check --
523 ---------------------
525 function Component_Check (N : Node_Id; Index : Node_Id) return Boolean is
529 -- Checks 1: (no component associations)
531 if Present (Component_Associations (N)) then
535 -- Checks on components
537 -- Recurse to check subaggregates, which may appear in qualified
538 -- expressions. If delayed, the front-end will have to expand.
539 -- If the component is a discriminated record, treat as non-static,
540 -- as the back-end cannot handle this properly.
542 Expr := First (Expressions (N));
543 while Present (Expr) loop
545 -- Checks 8: (no delayed components)
547 if Is_Delayed_Aggregate (Expr) then
551 -- Checks 9: (no discriminated records)
553 if Present (Etype (Expr))
554 and then Is_Record_Type (Etype (Expr))
555 and then Has_Discriminants (Etype (Expr))
560 -- Checks 7. Component must not be bit aligned component
562 if Possible_Bit_Aligned_Component (Expr) then
566 -- Recursion to following indexes for multiple dimension case
568 if Present (Next_Index (Index))
569 and then not Component_Check (Expr, Next_Index (Index))
574 -- All checks for that component finished, on to next
582 -- Start of processing for Backend_Processing_Possible
585 -- Checks 2 (array not bit packed) and 10 (no controlled actions)
587 if Is_Bit_Packed_Array (Typ) or else Needs_Finalization (Typ) then
591 -- If component is limited, aggregate must be expanded because each
592 -- component assignment must be built in place.
594 if Is_Inherently_Limited_Type (Component_Type (Typ)) then
598 -- Checks 4 (array must not be multi-dimensional Fortran case)
600 if Convention (Typ) = Convention_Fortran
601 and then Number_Dimensions (Typ) > 1
606 -- Checks 3 (size of array must be known at compile time)
608 if not Size_Known_At_Compile_Time (Typ) then
612 -- Checks on components
614 if not Component_Check (N, First_Index (Typ)) then
618 -- Checks 5 (if the component type is tagged, then we may need to do
619 -- tag adjustments. Perhaps this should be refined to check for any
620 -- component associations that actually need tag adjustment, similar
621 -- to the test in Component_Not_OK_For_Backend for record aggregates
622 -- with tagged components, but not clear whether it's worthwhile ???;
623 -- in the case of the JVM, object tags are handled implicitly)
625 if Is_Tagged_Type (Component_Type (Typ))
626 and then Tagged_Type_Expansion
631 -- Checks 6 (component type must not have bit aligned components)
633 if Type_May_Have_Bit_Aligned_Components (Component_Type (Typ)) then
637 -- Backend processing is possible
639 Set_Size_Known_At_Compile_Time (Etype (N), True);
641 end Backend_Processing_Possible;
643 ---------------------------
644 -- Build_Array_Aggr_Code --
645 ---------------------------
647 -- The code that we generate from a one dimensional aggregate is
649 -- 1. If the sub-aggregate contains discrete choices we
651 -- (a) Sort the discrete choices
653 -- (b) Otherwise for each discrete choice that specifies a range we
654 -- emit a loop. If a range specifies a maximum of three values, or
655 -- we are dealing with an expression we emit a sequence of
656 -- assignments instead of a loop.
658 -- (c) Generate the remaining loops to cover the others choice if any
660 -- 2. If the aggregate contains positional elements we
662 -- (a) translate the positional elements in a series of assignments
664 -- (b) Generate a final loop to cover the others choice if any.
665 -- Note that this final loop has to be a while loop since the case
667 -- L : Integer := Integer'Last;
668 -- H : Integer := Integer'Last;
669 -- A : array (L .. H) := (1, others =>0);
671 -- cannot be handled by a for loop. Thus for the following
673 -- array (L .. H) := (.. positional elements.., others =>E);
675 -- we always generate something like:
677 -- J : Index_Type := Index_Of_Last_Positional_Element;
679 -- J := Index_Base'Succ (J)
683 function Build_Array_Aggr_Code
688 Scalar_Comp : Boolean;
689 Indices : List_Id := No_List;
690 Flist : Node_Id := Empty) return List_Id
692 Loc : constant Source_Ptr := Sloc (N);
693 Index_Base : constant Entity_Id := Base_Type (Etype (Index));
694 Index_Base_L : constant Node_Id := Type_Low_Bound (Index_Base);
695 Index_Base_H : constant Node_Id := Type_High_Bound (Index_Base);
697 function Add (Val : Int; To : Node_Id) return Node_Id;
698 -- Returns an expression where Val is added to expression To, unless
699 -- To+Val is provably out of To's base type range. To must be an
700 -- already analyzed expression.
702 function Empty_Range (L, H : Node_Id) return Boolean;
703 -- Returns True if the range defined by L .. H is certainly empty
705 function Equal (L, H : Node_Id) return Boolean;
706 -- Returns True if L = H for sure
708 function Index_Base_Name return Node_Id;
709 -- Returns a new reference to the index type name
711 function Gen_Assign (Ind : Node_Id; Expr : Node_Id) return List_Id;
712 -- Ind must be a side-effect free expression. If the input aggregate
713 -- N to Build_Loop contains no sub-aggregates, then this function
714 -- returns the assignment statement:
716 -- Into (Indices, Ind) := Expr;
718 -- Otherwise we call Build_Code recursively
720 -- Ada 2005 (AI-287): In case of default initialized component, Expr
721 -- is empty and we generate a call to the corresponding IP subprogram.
723 function Gen_Loop (L, H : Node_Id; Expr : Node_Id) return List_Id;
724 -- Nodes L and H must be side-effect free expressions.
725 -- If the input aggregate N to Build_Loop contains no sub-aggregates,
726 -- This routine returns the for loop statement
728 -- for J in Index_Base'(L) .. Index_Base'(H) loop
729 -- Into (Indices, J) := Expr;
732 -- Otherwise we call Build_Code recursively.
733 -- As an optimization if the loop covers 3 or less scalar elements we
734 -- generate a sequence of assignments.
736 function Gen_While (L, H : Node_Id; Expr : Node_Id) return List_Id;
737 -- Nodes L and H must be side-effect free expressions.
738 -- If the input aggregate N to Build_Loop contains no sub-aggregates,
739 -- This routine returns the while loop statement
741 -- J : Index_Base := L;
743 -- J := Index_Base'Succ (J);
744 -- Into (Indices, J) := Expr;
747 -- Otherwise we call Build_Code recursively
749 function Local_Compile_Time_Known_Value (E : Node_Id) return Boolean;
750 function Local_Expr_Value (E : Node_Id) return Uint;
751 -- These two Local routines are used to replace the corresponding ones
752 -- in sem_eval because while processing the bounds of an aggregate with
753 -- discrete choices whose index type is an enumeration, we build static
754 -- expressions not recognized by Compile_Time_Known_Value as such since
755 -- they have not yet been analyzed and resolved. All the expressions in
756 -- question are things like Index_Base_Name'Val (Const) which we can
757 -- easily recognize as being constant.
763 function Add (Val : Int; To : Node_Id) return Node_Id is
768 U_Val : constant Uint := UI_From_Int (Val);
771 -- Note: do not try to optimize the case of Val = 0, because
772 -- we need to build a new node with the proper Sloc value anyway.
774 -- First test if we can do constant folding
776 if Local_Compile_Time_Known_Value (To) then
777 U_To := Local_Expr_Value (To) + Val;
779 -- Determine if our constant is outside the range of the index.
780 -- If so return an Empty node. This empty node will be caught
781 -- by Empty_Range below.
783 if Compile_Time_Known_Value (Index_Base_L)
784 and then U_To < Expr_Value (Index_Base_L)
788 elsif Compile_Time_Known_Value (Index_Base_H)
789 and then U_To > Expr_Value (Index_Base_H)
794 Expr_Pos := Make_Integer_Literal (Loc, U_To);
795 Set_Is_Static_Expression (Expr_Pos);
797 if not Is_Enumeration_Type (Index_Base) then
800 -- If we are dealing with enumeration return
801 -- Index_Base'Val (Expr_Pos)
805 Make_Attribute_Reference
807 Prefix => Index_Base_Name,
808 Attribute_Name => Name_Val,
809 Expressions => New_List (Expr_Pos));
815 -- If we are here no constant folding possible
817 if not Is_Enumeration_Type (Index_Base) then
820 Left_Opnd => Duplicate_Subexpr (To),
821 Right_Opnd => Make_Integer_Literal (Loc, U_Val));
823 -- If we are dealing with enumeration return
824 -- Index_Base'Val (Index_Base'Pos (To) + Val)
828 Make_Attribute_Reference
830 Prefix => Index_Base_Name,
831 Attribute_Name => Name_Pos,
832 Expressions => New_List (Duplicate_Subexpr (To)));
837 Right_Opnd => Make_Integer_Literal (Loc, U_Val));
840 Make_Attribute_Reference
842 Prefix => Index_Base_Name,
843 Attribute_Name => Name_Val,
844 Expressions => New_List (Expr_Pos));
854 function Empty_Range (L, H : Node_Id) return Boolean is
855 Is_Empty : Boolean := False;
860 -- First check if L or H were already detected as overflowing the
861 -- index base range type by function Add above. If this is so Add
862 -- returns the empty node.
864 if No (L) or else No (H) then
871 -- L > H range is empty
877 -- B_L > H range must be empty
883 -- L > B_H range must be empty
887 High := Index_Base_H;
890 if Local_Compile_Time_Known_Value (Low)
891 and then Local_Compile_Time_Known_Value (High)
894 UI_Gt (Local_Expr_Value (Low), Local_Expr_Value (High));
907 function Equal (L, H : Node_Id) return Boolean is
912 elsif Local_Compile_Time_Known_Value (L)
913 and then Local_Compile_Time_Known_Value (H)
915 return UI_Eq (Local_Expr_Value (L), Local_Expr_Value (H));
925 function Gen_Assign (Ind : Node_Id; Expr : Node_Id) return List_Id is
926 L : constant List_Id := New_List;
930 New_Indices : List_Id;
931 Indexed_Comp : Node_Id;
933 Comp_Type : Entity_Id := Empty;
935 function Add_Loop_Actions (Lis : List_Id) return List_Id;
936 -- Collect insert_actions generated in the construction of a
937 -- loop, and prepend them to the sequence of assignments to
938 -- complete the eventual body of the loop.
940 ----------------------
941 -- Add_Loop_Actions --
942 ----------------------
944 function Add_Loop_Actions (Lis : List_Id) return List_Id is
948 -- Ada 2005 (AI-287): Do nothing else in case of default
949 -- initialized component.
954 elsif Nkind (Parent (Expr)) = N_Component_Association
955 and then Present (Loop_Actions (Parent (Expr)))
957 Append_List (Lis, Loop_Actions (Parent (Expr)));
958 Res := Loop_Actions (Parent (Expr));
959 Set_Loop_Actions (Parent (Expr), No_List);
965 end Add_Loop_Actions;
967 -- Start of processing for Gen_Assign
971 New_Indices := New_List;
973 New_Indices := New_Copy_List_Tree (Indices);
976 Append_To (New_Indices, Ind);
978 if Present (Flist) then
979 F := New_Copy_Tree (Flist);
981 elsif Present (Etype (N)) and then Needs_Finalization (Etype (N)) then
982 if Is_Entity_Name (Into)
983 and then Present (Scope (Entity (Into)))
985 F := Find_Final_List (Scope (Entity (Into)));
987 F := Find_Final_List (Current_Scope);
993 if Present (Next_Index (Index)) then
996 Build_Array_Aggr_Code
999 Index => Next_Index (Index),
1001 Scalar_Comp => Scalar_Comp,
1002 Indices => New_Indices,
1006 -- If we get here then we are at a bottom-level (sub-)aggregate
1010 (Make_Indexed_Component (Loc,
1011 Prefix => New_Copy_Tree (Into),
1012 Expressions => New_Indices));
1014 Set_Assignment_OK (Indexed_Comp);
1016 -- Ada 2005 (AI-287): In case of default initialized component, Expr
1017 -- is not present (and therefore we also initialize Expr_Q to empty).
1021 elsif Nkind (Expr) = N_Qualified_Expression then
1022 Expr_Q := Expression (Expr);
1027 if Present (Etype (N))
1028 and then Etype (N) /= Any_Composite
1030 Comp_Type := Component_Type (Etype (N));
1031 pragma Assert (Comp_Type = Ctype); -- AI-287
1033 elsif Present (Next (First (New_Indices))) then
1035 -- Ada 2005 (AI-287): Do nothing in case of default initialized
1036 -- component because we have received the component type in
1037 -- the formal parameter Ctype.
1039 -- ??? Some assert pragmas have been added to check if this new
1040 -- formal can be used to replace this code in all cases.
1042 if Present (Expr) then
1044 -- This is a multidimensional array. Recover the component
1045 -- type from the outermost aggregate, because subaggregates
1046 -- do not have an assigned type.
1053 while Present (P) loop
1054 if Nkind (P) = N_Aggregate
1055 and then Present (Etype (P))
1057 Comp_Type := Component_Type (Etype (P));
1065 pragma Assert (Comp_Type = Ctype); -- AI-287
1070 -- Ada 2005 (AI-287): We only analyze the expression in case of non-
1071 -- default initialized components (otherwise Expr_Q is not present).
1074 and then Nkind_In (Expr_Q, N_Aggregate, N_Extension_Aggregate)
1076 -- At this stage the Expression may not have been analyzed yet
1077 -- because the array aggregate code has not been updated to use
1078 -- the Expansion_Delayed flag and avoid analysis altogether to
1079 -- solve the same problem (see Resolve_Aggr_Expr). So let us do
1080 -- the analysis of non-array aggregates now in order to get the
1081 -- value of Expansion_Delayed flag for the inner aggregate ???
1083 if Present (Comp_Type) and then not Is_Array_Type (Comp_Type) then
1084 Analyze_And_Resolve (Expr_Q, Comp_Type);
1087 if Is_Delayed_Aggregate (Expr_Q) then
1089 -- This is either a subaggregate of a multidimentional array,
1090 -- or a component of an array type whose component type is
1091 -- also an array. In the latter case, the expression may have
1092 -- component associations that provide different bounds from
1093 -- those of the component type, and sliding must occur. Instead
1094 -- of decomposing the current aggregate assignment, force the
1095 -- re-analysis of the assignment, so that a temporary will be
1096 -- generated in the usual fashion, and sliding will take place.
1098 if Nkind (Parent (N)) = N_Assignment_Statement
1099 and then Is_Array_Type (Comp_Type)
1100 and then Present (Component_Associations (Expr_Q))
1101 and then Must_Slide (Comp_Type, Etype (Expr_Q))
1103 Set_Expansion_Delayed (Expr_Q, False);
1104 Set_Analyzed (Expr_Q, False);
1110 Expr_Q, Etype (Expr_Q), Indexed_Comp, F));
1115 -- Ada 2005 (AI-287): In case of default initialized component, call
1116 -- the initialization subprogram associated with the component type.
1117 -- If the component type is an access type, add an explicit null
1118 -- assignment, because for the back-end there is an initialization
1119 -- present for the whole aggregate, and no default initialization
1122 -- In addition, if the component type is controlled, we must call
1123 -- its Initialize procedure explicitly, because there is no explicit
1124 -- object creation that will invoke it otherwise.
1127 if Present (Base_Init_Proc (Base_Type (Ctype)))
1128 or else Has_Task (Base_Type (Ctype))
1131 Build_Initialization_Call (Loc,
1132 Id_Ref => Indexed_Comp,
1134 With_Default_Init => True));
1136 elsif Is_Access_Type (Ctype) then
1138 Make_Assignment_Statement (Loc,
1139 Name => Indexed_Comp,
1140 Expression => Make_Null (Loc)));
1143 if Needs_Finalization (Ctype) then
1146 Ref => New_Copy_Tree (Indexed_Comp),
1148 Flist_Ref => Find_Final_List (Current_Scope),
1149 With_Attach => Make_Integer_Literal (Loc, 1)));
1153 -- Now generate the assignment with no associated controlled
1154 -- actions since the target of the assignment may not have been
1155 -- initialized, it is not possible to Finalize it as expected by
1156 -- normal controlled assignment. The rest of the controlled
1157 -- actions are done manually with the proper finalization list
1158 -- coming from the context.
1161 Make_OK_Assignment_Statement (Loc,
1162 Name => Indexed_Comp,
1163 Expression => New_Copy_Tree (Expr));
1165 if Present (Comp_Type) and then Needs_Finalization (Comp_Type) then
1166 Set_No_Ctrl_Actions (A);
1168 -- If this is an aggregate for an array of arrays, each
1169 -- sub-aggregate will be expanded as well, and even with
1170 -- No_Ctrl_Actions the assignments of inner components will
1171 -- require attachment in their assignments to temporaries.
1172 -- These temporaries must be finalized for each subaggregate,
1173 -- to prevent multiple attachments of the same temporary
1174 -- location to same finalization chain (and consequently
1175 -- circular lists). To ensure that finalization takes place
1176 -- for each subaggregate we wrap the assignment in a block.
1178 if Is_Array_Type (Comp_Type)
1179 and then Nkind (Expr) = N_Aggregate
1182 Make_Block_Statement (Loc,
1183 Handled_Statement_Sequence =>
1184 Make_Handled_Sequence_Of_Statements (Loc,
1185 Statements => New_List (A)));
1191 -- Adjust the tag if tagged (because of possible view
1192 -- conversions), unless compiling for a VM where
1193 -- tags are implicit.
1195 if Present (Comp_Type)
1196 and then Is_Tagged_Type (Comp_Type)
1197 and then Tagged_Type_Expansion
1200 Make_OK_Assignment_Statement (Loc,
1202 Make_Selected_Component (Loc,
1203 Prefix => New_Copy_Tree (Indexed_Comp),
1206 (First_Tag_Component (Comp_Type), Loc)),
1209 Unchecked_Convert_To (RTE (RE_Tag),
1211 (Node (First_Elmt (Access_Disp_Table (Comp_Type))),
1217 -- Adjust and attach the component to the proper final list, which
1218 -- can be the controller of the outer record object or the final
1219 -- list associated with the scope.
1221 -- If the component is itself an array of controlled types, whose
1222 -- value is given by a sub-aggregate, then the attach calls have
1223 -- been generated when individual subcomponent are assigned, and
1224 -- must not be done again to prevent malformed finalization chains
1225 -- (see comments above, concerning the creation of a block to hold
1226 -- inner finalization actions).
1228 if Present (Comp_Type)
1229 and then Needs_Finalization (Comp_Type)
1230 and then not Is_Limited_Type (Comp_Type)
1232 (Is_Array_Type (Comp_Type)
1233 and then Is_Controlled (Component_Type (Comp_Type))
1234 and then Nkind (Expr) = N_Aggregate)
1238 Ref => New_Copy_Tree (Indexed_Comp),
1241 With_Attach => Make_Integer_Literal (Loc, 1)));
1245 return Add_Loop_Actions (L);
1252 function Gen_Loop (L, H : Node_Id; Expr : Node_Id) return List_Id is
1256 -- Index_Base'(L) .. Index_Base'(H)
1258 L_Iteration_Scheme : Node_Id;
1259 -- L_J in Index_Base'(L) .. Index_Base'(H)
1262 -- The statements to execute in the loop
1264 S : constant List_Id := New_List;
1265 -- List of statements
1268 -- Copy of expression tree, used for checking purposes
1271 -- If loop bounds define an empty range return the null statement
1273 if Empty_Range (L, H) then
1274 Append_To (S, Make_Null_Statement (Loc));
1276 -- Ada 2005 (AI-287): Nothing else need to be done in case of
1277 -- default initialized component.
1283 -- The expression must be type-checked even though no component
1284 -- of the aggregate will have this value. This is done only for
1285 -- actual components of the array, not for subaggregates. Do
1286 -- the check on a copy, because the expression may be shared
1287 -- among several choices, some of which might be non-null.
1289 if Present (Etype (N))
1290 and then Is_Array_Type (Etype (N))
1291 and then No (Next_Index (Index))
1293 Expander_Mode_Save_And_Set (False);
1294 Tcopy := New_Copy_Tree (Expr);
1295 Set_Parent (Tcopy, N);
1296 Analyze_And_Resolve (Tcopy, Component_Type (Etype (N)));
1297 Expander_Mode_Restore;
1303 -- If loop bounds are the same then generate an assignment
1305 elsif Equal (L, H) then
1306 return Gen_Assign (New_Copy_Tree (L), Expr);
1308 -- If H - L <= 2 then generate a sequence of assignments when we are
1309 -- processing the bottom most aggregate and it contains scalar
1312 elsif No (Next_Index (Index))
1313 and then Scalar_Comp
1314 and then Local_Compile_Time_Known_Value (L)
1315 and then Local_Compile_Time_Known_Value (H)
1316 and then Local_Expr_Value (H) - Local_Expr_Value (L) <= 2
1319 Append_List_To (S, Gen_Assign (New_Copy_Tree (L), Expr));
1320 Append_List_To (S, Gen_Assign (Add (1, To => L), Expr));
1322 if Local_Expr_Value (H) - Local_Expr_Value (L) = 2 then
1323 Append_List_To (S, Gen_Assign (Add (2, To => L), Expr));
1329 -- Otherwise construct the loop, starting with the loop index L_J
1331 L_J := Make_Defining_Identifier (Loc, New_Internal_Name ('J'));
1333 -- Construct "L .. H"
1338 Low_Bound => Make_Qualified_Expression
1340 Subtype_Mark => Index_Base_Name,
1342 High_Bound => Make_Qualified_Expression
1344 Subtype_Mark => Index_Base_Name,
1347 -- Construct "for L_J in Index_Base range L .. H"
1349 L_Iteration_Scheme :=
1350 Make_Iteration_Scheme
1352 Loop_Parameter_Specification =>
1353 Make_Loop_Parameter_Specification
1355 Defining_Identifier => L_J,
1356 Discrete_Subtype_Definition => L_Range));
1358 -- Construct the statements to execute in the loop body
1360 L_Body := Gen_Assign (New_Reference_To (L_J, Loc), Expr);
1362 -- Construct the final loop
1364 Append_To (S, Make_Implicit_Loop_Statement
1366 Identifier => Empty,
1367 Iteration_Scheme => L_Iteration_Scheme,
1368 Statements => L_Body));
1370 -- A small optimization: if the aggregate is initialized with a box
1371 -- and the component type has no initialization procedure, remove the
1372 -- useless empty loop.
1374 if Nkind (First (S)) = N_Loop_Statement
1375 and then Is_Empty_List (Statements (First (S)))
1377 return New_List (Make_Null_Statement (Loc));
1387 -- The code built is
1389 -- W_J : Index_Base := L;
1390 -- while W_J < H loop
1391 -- W_J := Index_Base'Succ (W);
1395 function Gen_While (L, H : Node_Id; Expr : Node_Id) return List_Id is
1399 -- W_J : Base_Type := L;
1401 W_Iteration_Scheme : Node_Id;
1404 W_Index_Succ : Node_Id;
1405 -- Index_Base'Succ (J)
1407 W_Increment : Node_Id;
1408 -- W_J := Index_Base'Succ (W)
1410 W_Body : constant List_Id := New_List;
1411 -- The statements to execute in the loop
1413 S : constant List_Id := New_List;
1414 -- list of statement
1417 -- If loop bounds define an empty range or are equal return null
1419 if Empty_Range (L, H) or else Equal (L, H) then
1420 Append_To (S, Make_Null_Statement (Loc));
1424 -- Build the decl of W_J
1426 W_J := Make_Defining_Identifier (Loc, New_Internal_Name ('J'));
1428 Make_Object_Declaration
1430 Defining_Identifier => W_J,
1431 Object_Definition => Index_Base_Name,
1434 -- Theoretically we should do a New_Copy_Tree (L) here, but we know
1435 -- that in this particular case L is a fresh Expr generated by
1436 -- Add which we are the only ones to use.
1438 Append_To (S, W_Decl);
1440 -- Construct " while W_J < H"
1442 W_Iteration_Scheme :=
1443 Make_Iteration_Scheme
1445 Condition => Make_Op_Lt
1447 Left_Opnd => New_Reference_To (W_J, Loc),
1448 Right_Opnd => New_Copy_Tree (H)));
1450 -- Construct the statements to execute in the loop body
1453 Make_Attribute_Reference
1455 Prefix => Index_Base_Name,
1456 Attribute_Name => Name_Succ,
1457 Expressions => New_List (New_Reference_To (W_J, Loc)));
1460 Make_OK_Assignment_Statement
1462 Name => New_Reference_To (W_J, Loc),
1463 Expression => W_Index_Succ);
1465 Append_To (W_Body, W_Increment);
1466 Append_List_To (W_Body,
1467 Gen_Assign (New_Reference_To (W_J, Loc), Expr));
1469 -- Construct the final loop
1471 Append_To (S, Make_Implicit_Loop_Statement
1473 Identifier => Empty,
1474 Iteration_Scheme => W_Iteration_Scheme,
1475 Statements => W_Body));
1480 ---------------------
1481 -- Index_Base_Name --
1482 ---------------------
1484 function Index_Base_Name return Node_Id is
1486 return New_Reference_To (Index_Base, Sloc (N));
1487 end Index_Base_Name;
1489 ------------------------------------
1490 -- Local_Compile_Time_Known_Value --
1491 ------------------------------------
1493 function Local_Compile_Time_Known_Value (E : Node_Id) return Boolean is
1495 return Compile_Time_Known_Value (E)
1497 (Nkind (E) = N_Attribute_Reference
1498 and then Attribute_Name (E) = Name_Val
1499 and then Compile_Time_Known_Value (First (Expressions (E))));
1500 end Local_Compile_Time_Known_Value;
1502 ----------------------
1503 -- Local_Expr_Value --
1504 ----------------------
1506 function Local_Expr_Value (E : Node_Id) return Uint is
1508 if Compile_Time_Known_Value (E) then
1509 return Expr_Value (E);
1511 return Expr_Value (First (Expressions (E)));
1513 end Local_Expr_Value;
1515 -- Build_Array_Aggr_Code Variables
1522 Others_Expr : Node_Id := Empty;
1523 Others_Box_Present : Boolean := False;
1525 Aggr_L : constant Node_Id := Low_Bound (Aggregate_Bounds (N));
1526 Aggr_H : constant Node_Id := High_Bound (Aggregate_Bounds (N));
1527 -- The aggregate bounds of this specific sub-aggregate. Note that if
1528 -- the code generated by Build_Array_Aggr_Code is executed then these
1529 -- bounds are OK. Otherwise a Constraint_Error would have been raised.
1531 Aggr_Low : constant Node_Id := Duplicate_Subexpr_No_Checks (Aggr_L);
1532 Aggr_High : constant Node_Id := Duplicate_Subexpr_No_Checks (Aggr_H);
1533 -- After Duplicate_Subexpr these are side-effect free
1538 Nb_Choices : Nat := 0;
1539 Table : Case_Table_Type (1 .. Number_Of_Choices (N));
1540 -- Used to sort all the different choice values
1543 -- Number of elements in the positional aggregate
1545 New_Code : constant List_Id := New_List;
1547 -- Start of processing for Build_Array_Aggr_Code
1550 -- First before we start, a special case. if we have a bit packed
1551 -- array represented as a modular type, then clear the value to
1552 -- zero first, to ensure that unused bits are properly cleared.
1557 and then Is_Bit_Packed_Array (Typ)
1558 and then Is_Modular_Integer_Type (Packed_Array_Type (Typ))
1560 Append_To (New_Code,
1561 Make_Assignment_Statement (Loc,
1562 Name => New_Copy_Tree (Into),
1564 Unchecked_Convert_To (Typ,
1565 Make_Integer_Literal (Loc, Uint_0))));
1568 -- If the component type contains tasks, we need to build a Master
1569 -- entity in the current scope, because it will be needed if build-
1570 -- in-place functions are called in the expanded code.
1572 if Nkind (Parent (N)) = N_Object_Declaration
1573 and then Has_Task (Typ)
1575 Build_Master_Entity (Defining_Identifier (Parent (N)));
1578 -- STEP 1: Process component associations
1580 -- For those associations that may generate a loop, initialize
1581 -- Loop_Actions to collect inserted actions that may be crated.
1583 -- Skip this if no component associations
1585 if No (Expressions (N)) then
1587 -- STEP 1 (a): Sort the discrete choices
1589 Assoc := First (Component_Associations (N));
1590 while Present (Assoc) loop
1591 Choice := First (Choices (Assoc));
1592 while Present (Choice) loop
1593 if Nkind (Choice) = N_Others_Choice then
1594 Set_Loop_Actions (Assoc, New_List);
1596 if Box_Present (Assoc) then
1597 Others_Box_Present := True;
1599 Others_Expr := Expression (Assoc);
1604 Get_Index_Bounds (Choice, Low, High);
1607 Set_Loop_Actions (Assoc, New_List);
1610 Nb_Choices := Nb_Choices + 1;
1611 if Box_Present (Assoc) then
1612 Table (Nb_Choices) := (Choice_Lo => Low,
1614 Choice_Node => Empty);
1616 Table (Nb_Choices) := (Choice_Lo => Low,
1618 Choice_Node => Expression (Assoc));
1626 -- If there is more than one set of choices these must be static
1627 -- and we can therefore sort them. Remember that Nb_Choices does not
1628 -- account for an others choice.
1630 if Nb_Choices > 1 then
1631 Sort_Case_Table (Table);
1634 -- STEP 1 (b): take care of the whole set of discrete choices
1636 for J in 1 .. Nb_Choices loop
1637 Low := Table (J).Choice_Lo;
1638 High := Table (J).Choice_Hi;
1639 Expr := Table (J).Choice_Node;
1640 Append_List (Gen_Loop (Low, High, Expr), To => New_Code);
1643 -- STEP 1 (c): generate the remaining loops to cover others choice
1644 -- We don't need to generate loops over empty gaps, but if there is
1645 -- a single empty range we must analyze the expression for semantics
1647 if Present (Others_Expr) or else Others_Box_Present then
1649 First : Boolean := True;
1652 for J in 0 .. Nb_Choices loop
1656 Low := Add (1, To => Table (J).Choice_Hi);
1659 if J = Nb_Choices then
1662 High := Add (-1, To => Table (J + 1).Choice_Lo);
1665 -- If this is an expansion within an init proc, make
1666 -- sure that discriminant references are replaced by
1667 -- the corresponding discriminal.
1669 if Inside_Init_Proc then
1670 if Is_Entity_Name (Low)
1671 and then Ekind (Entity (Low)) = E_Discriminant
1673 Set_Entity (Low, Discriminal (Entity (Low)));
1676 if Is_Entity_Name (High)
1677 and then Ekind (Entity (High)) = E_Discriminant
1679 Set_Entity (High, Discriminal (Entity (High)));
1684 or else not Empty_Range (Low, High)
1688 (Gen_Loop (Low, High, Others_Expr), To => New_Code);
1694 -- STEP 2: Process positional components
1697 -- STEP 2 (a): Generate the assignments for each positional element
1698 -- Note that here we have to use Aggr_L rather than Aggr_Low because
1699 -- Aggr_L is analyzed and Add wants an analyzed expression.
1701 Expr := First (Expressions (N));
1703 while Present (Expr) loop
1704 Nb_Elements := Nb_Elements + 1;
1705 Append_List (Gen_Assign (Add (Nb_Elements, To => Aggr_L), Expr),
1710 -- STEP 2 (b): Generate final loop if an others choice is present
1711 -- Here Nb_Elements gives the offset of the last positional element.
1713 if Present (Component_Associations (N)) then
1714 Assoc := Last (Component_Associations (N));
1716 -- Ada 2005 (AI-287)
1718 if Box_Present (Assoc) then
1719 Append_List (Gen_While (Add (Nb_Elements, To => Aggr_L),
1724 Expr := Expression (Assoc);
1726 Append_List (Gen_While (Add (Nb_Elements, To => Aggr_L),
1735 end Build_Array_Aggr_Code;
1737 ----------------------------
1738 -- Build_Record_Aggr_Code --
1739 ----------------------------
1741 function Build_Record_Aggr_Code
1745 Flist : Node_Id := Empty;
1746 Obj : Entity_Id := Empty;
1747 Is_Limited_Ancestor_Expansion : Boolean := False) return List_Id
1749 Loc : constant Source_Ptr := Sloc (N);
1750 L : constant List_Id := New_List;
1751 N_Typ : constant Entity_Id := Etype (N);
1758 Comp_Type : Entity_Id;
1759 Selector : Entity_Id;
1760 Comp_Expr : Node_Id;
1763 Internal_Final_List : Node_Id := Empty;
1765 -- If this is an internal aggregate, the External_Final_List is an
1766 -- expression for the controller record of the enclosing type.
1768 -- If the current aggregate has several controlled components, this
1769 -- expression will appear in several calls to attach to the finali-
1770 -- zation list, and it must not be shared.
1772 External_Final_List : Node_Id;
1773 Ancestor_Is_Expression : Boolean := False;
1774 Ancestor_Is_Subtype_Mark : Boolean := False;
1776 Init_Typ : Entity_Id := Empty;
1779 Ctrl_Stuff_Done : Boolean := False;
1780 -- True if Gen_Ctrl_Actions_For_Aggr has already been called; calls
1781 -- after the first do nothing.
1783 function Ancestor_Discriminant_Value (Disc : Entity_Id) return Node_Id;
1784 -- Returns the value that the given discriminant of an ancestor type
1785 -- should receive (in the absence of a conflict with the value provided
1786 -- by an ancestor part of an extension aggregate).
1788 procedure Check_Ancestor_Discriminants (Anc_Typ : Entity_Id);
1789 -- Check that each of the discriminant values defined by the ancestor
1790 -- part of an extension aggregate match the corresponding values
1791 -- provided by either an association of the aggregate or by the
1792 -- constraint imposed by a parent type (RM95-4.3.2(8)).
1794 function Compatible_Int_Bounds
1795 (Agg_Bounds : Node_Id;
1796 Typ_Bounds : Node_Id) return Boolean;
1797 -- Return true if Agg_Bounds are equal or within Typ_Bounds. It is
1798 -- assumed that both bounds are integer ranges.
1800 procedure Gen_Ctrl_Actions_For_Aggr;
1801 -- Deal with the various controlled type data structure initializations
1802 -- (but only if it hasn't been done already).
1804 function Get_Constraint_Association (T : Entity_Id) return Node_Id;
1805 -- Returns the first discriminant association in the constraint
1806 -- associated with T, if any, otherwise returns Empty.
1808 function Init_Controller
1813 Init_Pr : Boolean) return List_Id;
1814 -- Returns the list of statements necessary to initialize the internal
1815 -- controller of the (possible) ancestor typ into target and attach it
1816 -- to finalization list F. Init_Pr conditions the call to the init proc
1817 -- since it may already be done due to ancestor initialization.
1819 function Is_Int_Range_Bounds (Bounds : Node_Id) return Boolean;
1820 -- Check whether Bounds is a range node and its lower and higher bounds
1821 -- are integers literals.
1823 ---------------------------------
1824 -- Ancestor_Discriminant_Value --
1825 ---------------------------------
1827 function Ancestor_Discriminant_Value (Disc : Entity_Id) return Node_Id is
1829 Assoc_Elmt : Elmt_Id;
1830 Aggr_Comp : Entity_Id;
1831 Corresp_Disc : Entity_Id;
1832 Current_Typ : Entity_Id := Base_Type (Typ);
1833 Parent_Typ : Entity_Id;
1834 Parent_Disc : Entity_Id;
1835 Save_Assoc : Node_Id := Empty;
1838 -- First check any discriminant associations to see if any of them
1839 -- provide a value for the discriminant.
1841 if Present (Discriminant_Specifications (Parent (Current_Typ))) then
1842 Assoc := First (Component_Associations (N));
1843 while Present (Assoc) loop
1844 Aggr_Comp := Entity (First (Choices (Assoc)));
1846 if Ekind (Aggr_Comp) = E_Discriminant then
1847 Save_Assoc := Expression (Assoc);
1849 Corresp_Disc := Corresponding_Discriminant (Aggr_Comp);
1850 while Present (Corresp_Disc) loop
1852 -- If found a corresponding discriminant then return the
1853 -- value given in the aggregate. (Note: this is not
1854 -- correct in the presence of side effects. ???)
1856 if Disc = Corresp_Disc then
1857 return Duplicate_Subexpr (Expression (Assoc));
1861 Corresponding_Discriminant (Corresp_Disc);
1869 -- No match found in aggregate, so chain up parent types to find
1870 -- a constraint that defines the value of the discriminant.
1872 Parent_Typ := Etype (Current_Typ);
1873 while Current_Typ /= Parent_Typ loop
1874 if Has_Discriminants (Parent_Typ)
1875 and then not Has_Unknown_Discriminants (Parent_Typ)
1877 Parent_Disc := First_Discriminant (Parent_Typ);
1879 -- We either get the association from the subtype indication
1880 -- of the type definition itself, or from the discriminant
1881 -- constraint associated with the type entity (which is
1882 -- preferable, but it's not always present ???)
1884 if Is_Empty_Elmt_List (
1885 Discriminant_Constraint (Current_Typ))
1887 Assoc := Get_Constraint_Association (Current_Typ);
1888 Assoc_Elmt := No_Elmt;
1891 First_Elmt (Discriminant_Constraint (Current_Typ));
1892 Assoc := Node (Assoc_Elmt);
1895 -- Traverse the discriminants of the parent type looking
1896 -- for one that corresponds.
1898 while Present (Parent_Disc) and then Present (Assoc) loop
1899 Corresp_Disc := Parent_Disc;
1900 while Present (Corresp_Disc)
1901 and then Disc /= Corresp_Disc
1904 Corresponding_Discriminant (Corresp_Disc);
1907 if Disc = Corresp_Disc then
1908 if Nkind (Assoc) = N_Discriminant_Association then
1909 Assoc := Expression (Assoc);
1912 -- If the located association directly denotes a
1913 -- discriminant, then use the value of a saved
1914 -- association of the aggregate. This is a kludge to
1915 -- handle certain cases involving multiple discriminants
1916 -- mapped to a single discriminant of a descendant. It's
1917 -- not clear how to locate the appropriate discriminant
1918 -- value for such cases. ???
1920 if Is_Entity_Name (Assoc)
1921 and then Ekind (Entity (Assoc)) = E_Discriminant
1923 Assoc := Save_Assoc;
1926 return Duplicate_Subexpr (Assoc);
1929 Next_Discriminant (Parent_Disc);
1931 if No (Assoc_Elmt) then
1934 Next_Elmt (Assoc_Elmt);
1935 if Present (Assoc_Elmt) then
1936 Assoc := Node (Assoc_Elmt);
1944 Current_Typ := Parent_Typ;
1945 Parent_Typ := Etype (Current_Typ);
1948 -- In some cases there's no ancestor value to locate (such as
1949 -- when an ancestor part given by an expression defines the
1950 -- discriminant value).
1953 end Ancestor_Discriminant_Value;
1955 ----------------------------------
1956 -- Check_Ancestor_Discriminants --
1957 ----------------------------------
1959 procedure Check_Ancestor_Discriminants (Anc_Typ : Entity_Id) is
1961 Disc_Value : Node_Id;
1965 Discr := First_Discriminant (Base_Type (Anc_Typ));
1966 while Present (Discr) loop
1967 Disc_Value := Ancestor_Discriminant_Value (Discr);
1969 if Present (Disc_Value) then
1970 Cond := Make_Op_Ne (Loc,
1972 Make_Selected_Component (Loc,
1973 Prefix => New_Copy_Tree (Target),
1974 Selector_Name => New_Occurrence_Of (Discr, Loc)),
1975 Right_Opnd => Disc_Value);
1978 Make_Raise_Constraint_Error (Loc,
1980 Reason => CE_Discriminant_Check_Failed));
1983 Next_Discriminant (Discr);
1985 end Check_Ancestor_Discriminants;
1987 ---------------------------
1988 -- Compatible_Int_Bounds --
1989 ---------------------------
1991 function Compatible_Int_Bounds
1992 (Agg_Bounds : Node_Id;
1993 Typ_Bounds : Node_Id) return Boolean
1995 Agg_Lo : constant Uint := Intval (Low_Bound (Agg_Bounds));
1996 Agg_Hi : constant Uint := Intval (High_Bound (Agg_Bounds));
1997 Typ_Lo : constant Uint := Intval (Low_Bound (Typ_Bounds));
1998 Typ_Hi : constant Uint := Intval (High_Bound (Typ_Bounds));
2000 return Typ_Lo <= Agg_Lo and then Agg_Hi <= Typ_Hi;
2001 end Compatible_Int_Bounds;
2003 --------------------------------
2004 -- Get_Constraint_Association --
2005 --------------------------------
2007 function Get_Constraint_Association (T : Entity_Id) return Node_Id is
2008 Typ_Def : constant Node_Id := Type_Definition (Parent (T));
2009 Indic : constant Node_Id := Subtype_Indication (Typ_Def);
2012 -- ??? Also need to cover case of a type mark denoting a subtype
2015 if Nkind (Indic) = N_Subtype_Indication
2016 and then Present (Constraint (Indic))
2018 return First (Constraints (Constraint (Indic)));
2022 end Get_Constraint_Association;
2024 ---------------------
2025 -- Init_Controller --
2026 ---------------------
2028 function Init_Controller
2033 Init_Pr : Boolean) return List_Id
2035 L : constant List_Id := New_List;
2038 Target_Type : Entity_Id;
2042 -- init-proc (target._controller);
2043 -- initialize (target._controller);
2044 -- Attach_to_Final_List (target._controller, F);
2047 Make_Selected_Component (Loc,
2048 Prefix => Convert_To (Typ, New_Copy_Tree (Target)),
2049 Selector_Name => Make_Identifier (Loc, Name_uController));
2050 Set_Assignment_OK (Ref);
2052 -- Ada 2005 (AI-287): Give support to aggregates of limited types.
2053 -- If the type is intrinsically limited the controller is limited as
2054 -- well. If it is tagged and limited then so is the controller.
2055 -- Otherwise an untagged type may have limited components without its
2056 -- full view being limited, so the controller is not limited.
2058 if Nkind (Target) = N_Identifier then
2059 Target_Type := Etype (Target);
2061 elsif Nkind (Target) = N_Selected_Component then
2062 Target_Type := Etype (Selector_Name (Target));
2064 elsif Nkind (Target) = N_Unchecked_Type_Conversion then
2065 Target_Type := Etype (Target);
2067 elsif Nkind (Target) = N_Unchecked_Expression
2068 and then Nkind (Expression (Target)) = N_Indexed_Component
2070 Target_Type := Etype (Prefix (Expression (Target)));
2073 Target_Type := Etype (Target);
2076 -- If the target has not been analyzed yet, as will happen with
2077 -- delayed expansion, use the given type (either the aggregate type
2078 -- or an ancestor) to determine limitedness.
2080 if No (Target_Type) then
2084 if (Is_Tagged_Type (Target_Type))
2085 and then Is_Limited_Type (Target_Type)
2087 RC := RE_Limited_Record_Controller;
2089 elsif Is_Inherently_Limited_Type (Target_Type) then
2090 RC := RE_Limited_Record_Controller;
2093 RC := RE_Record_Controller;
2098 Build_Initialization_Call (Loc,
2101 In_Init_Proc => Within_Init_Proc));
2105 Make_Procedure_Call_Statement (Loc,
2108 Find_Prim_Op (RTE (RC), Name_Initialize), Loc),
2109 Parameter_Associations =>
2110 New_List (New_Copy_Tree (Ref))));
2114 Obj_Ref => New_Copy_Tree (Ref),
2116 With_Attach => Attach));
2119 end Init_Controller;
2121 -------------------------
2122 -- Is_Int_Range_Bounds --
2123 -------------------------
2125 function Is_Int_Range_Bounds (Bounds : Node_Id) return Boolean is
2127 return Nkind (Bounds) = N_Range
2128 and then Nkind (Low_Bound (Bounds)) = N_Integer_Literal
2129 and then Nkind (High_Bound (Bounds)) = N_Integer_Literal;
2130 end Is_Int_Range_Bounds;
2132 -------------------------------
2133 -- Gen_Ctrl_Actions_For_Aggr --
2134 -------------------------------
2136 procedure Gen_Ctrl_Actions_For_Aggr is
2137 Alloc : Node_Id := Empty;
2140 -- Do the work only the first time this is called
2142 if Ctrl_Stuff_Done then
2146 Ctrl_Stuff_Done := True;
2149 and then Finalize_Storage_Only (Typ)
2151 (Is_Library_Level_Entity (Obj)
2152 or else Entity (Constant_Value (RTE (RE_Garbage_Collected))) =
2155 -- why not Is_True (Expr_Value (RTE (RE_Garbaage_Collected) ???
2157 Attach := Make_Integer_Literal (Loc, 0);
2159 elsif Nkind (Parent (N)) = N_Qualified_Expression
2160 and then Nkind (Parent (Parent (N))) = N_Allocator
2162 Alloc := Parent (Parent (N));
2163 Attach := Make_Integer_Literal (Loc, 2);
2166 Attach := Make_Integer_Literal (Loc, 1);
2169 -- Determine the external finalization list. It is either the
2170 -- finalization list of the outer-scope or the one coming from
2171 -- an outer aggregate. When the target is not a temporary, the
2172 -- proper scope is the scope of the target rather than the
2173 -- potentially transient current scope.
2175 if Needs_Finalization (Typ) then
2177 -- The current aggregate belongs to an allocator which creates
2178 -- an object through an anonymous access type or acts as the root
2179 -- of a coextension chain.
2183 (Is_Coextension_Root (Alloc)
2184 or else Ekind (Etype (Alloc)) = E_Anonymous_Access_Type)
2186 if No (Associated_Final_Chain (Etype (Alloc))) then
2187 Build_Final_List (Alloc, Etype (Alloc));
2190 External_Final_List :=
2191 Make_Selected_Component (Loc,
2194 Associated_Final_Chain (Etype (Alloc)), Loc),
2196 Make_Identifier (Loc, Name_F));
2198 elsif Present (Flist) then
2199 External_Final_List := New_Copy_Tree (Flist);
2201 elsif Is_Entity_Name (Target)
2202 and then Present (Scope (Entity (Target)))
2204 External_Final_List :=
2205 Find_Final_List (Scope (Entity (Target)));
2208 External_Final_List := Find_Final_List (Current_Scope);
2211 External_Final_List := Empty;
2214 -- Initialize and attach the outer object in the is_controlled case
2216 if Is_Controlled (Typ) then
2217 if Ancestor_Is_Subtype_Mark then
2218 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
2219 Set_Assignment_OK (Ref);
2221 Make_Procedure_Call_Statement (Loc,
2224 (Find_Prim_Op (Init_Typ, Name_Initialize), Loc),
2225 Parameter_Associations => New_List (New_Copy_Tree (Ref))));
2228 if not Has_Controlled_Component (Typ) then
2229 Ref := New_Copy_Tree (Target);
2230 Set_Assignment_OK (Ref);
2232 -- This is an aggregate of a coextension. Do not produce a
2233 -- finalization call, but rather attach the reference of the
2234 -- aggregate to its coextension chain.
2237 and then Is_Dynamic_Coextension (Alloc)
2239 if No (Coextensions (Alloc)) then
2240 Set_Coextensions (Alloc, New_Elmt_List);
2243 Append_Elmt (Ref, Coextensions (Alloc));
2248 Flist_Ref => New_Copy_Tree (External_Final_List),
2249 With_Attach => Attach));
2254 -- In the Has_Controlled component case, all the intermediate
2255 -- controllers must be initialized.
2257 if Has_Controlled_Component (Typ)
2258 and not Is_Limited_Ancestor_Expansion
2261 Inner_Typ : Entity_Id;
2262 Outer_Typ : Entity_Id;
2266 -- Find outer type with a controller
2268 Outer_Typ := Base_Type (Typ);
2269 while Outer_Typ /= Init_Typ
2270 and then not Has_New_Controlled_Component (Outer_Typ)
2272 Outer_Typ := Etype (Outer_Typ);
2275 -- Attach it to the outer record controller to the external
2278 if Outer_Typ = Init_Typ then
2283 F => External_Final_List,
2288 Inner_Typ := Init_Typ;
2295 F => External_Final_List,
2299 Inner_Typ := Etype (Outer_Typ);
2301 not Is_Tagged_Type (Typ) or else Inner_Typ = Outer_Typ;
2304 -- The outer object has to be attached as well
2306 if Is_Controlled (Typ) then
2307 Ref := New_Copy_Tree (Target);
2308 Set_Assignment_OK (Ref);
2312 Flist_Ref => New_Copy_Tree (External_Final_List),
2313 With_Attach => New_Copy_Tree (Attach)));
2316 -- Initialize the internal controllers for tagged types with
2317 -- more than one controller.
2319 while not At_Root and then Inner_Typ /= Init_Typ loop
2320 if Has_New_Controlled_Component (Inner_Typ) then
2322 Make_Selected_Component (Loc,
2324 Convert_To (Outer_Typ, New_Copy_Tree (Target)),
2326 Make_Identifier (Loc, Name_uController));
2328 Make_Selected_Component (Loc,
2330 Selector_Name => Make_Identifier (Loc, Name_F));
2337 Attach => Make_Integer_Literal (Loc, 1),
2339 Outer_Typ := Inner_Typ;
2344 At_Root := Inner_Typ = Etype (Inner_Typ);
2345 Inner_Typ := Etype (Inner_Typ);
2348 -- If not done yet attach the controller of the ancestor part
2350 if Outer_Typ /= Init_Typ
2351 and then Inner_Typ = Init_Typ
2352 and then Has_Controlled_Component (Init_Typ)
2355 Make_Selected_Component (Loc,
2356 Prefix => Convert_To (Outer_Typ, New_Copy_Tree (Target)),
2358 Make_Identifier (Loc, Name_uController));
2360 Make_Selected_Component (Loc,
2362 Selector_Name => Make_Identifier (Loc, Name_F));
2364 Attach := Make_Integer_Literal (Loc, 1);
2373 -- Note: Init_Pr is False because the ancestor part has
2374 -- already been initialized either way (by default, if
2375 -- given by a type name, otherwise from the expression).
2380 end Gen_Ctrl_Actions_For_Aggr;
2382 function Replace_Type (Expr : Node_Id) return Traverse_Result;
2383 -- If the aggregate contains a self-reference, traverse each expression
2384 -- to replace a possible self-reference with a reference to the proper
2385 -- component of the target of the assignment.
2391 function Replace_Type (Expr : Node_Id) return Traverse_Result is
2393 -- Note regarding the Root_Type test below: Aggregate components for
2394 -- self-referential types include attribute references to the current
2395 -- instance, of the form: Typ'access, etc.. These references are
2396 -- rewritten as references to the target of the aggregate: the
2397 -- left-hand side of an assignment, the entity in a declaration,
2398 -- or a temporary. Without this test, we would improperly extended
2399 -- this rewriting to attribute references whose prefix was not the
2400 -- type of the aggregate.
2402 if Nkind (Expr) = N_Attribute_Reference
2403 and then Is_Entity_Name (Prefix (Expr))
2404 and then Is_Type (Entity (Prefix (Expr)))
2405 and then Root_Type (Etype (N)) = Root_Type (Entity (Prefix (Expr)))
2407 if Is_Entity_Name (Lhs) then
2408 Rewrite (Prefix (Expr),
2409 New_Occurrence_Of (Entity (Lhs), Loc));
2411 elsif Nkind (Lhs) = N_Selected_Component then
2413 Make_Attribute_Reference (Loc,
2414 Attribute_Name => Name_Unrestricted_Access,
2415 Prefix => New_Copy_Tree (Prefix (Lhs))));
2416 Set_Analyzed (Parent (Expr), False);
2420 Make_Attribute_Reference (Loc,
2421 Attribute_Name => Name_Unrestricted_Access,
2422 Prefix => New_Copy_Tree (Lhs)));
2423 Set_Analyzed (Parent (Expr), False);
2430 procedure Replace_Self_Reference is
2431 new Traverse_Proc (Replace_Type);
2433 -- Start of processing for Build_Record_Aggr_Code
2436 if Has_Self_Reference (N) then
2437 Replace_Self_Reference (N);
2440 -- If the target of the aggregate is class-wide, we must convert it
2441 -- to the actual type of the aggregate, so that the proper components
2442 -- are visible. We know already that the types are compatible.
2444 if Present (Etype (Lhs))
2445 and then Is_Class_Wide_Type (Etype (Lhs))
2447 Target := Unchecked_Convert_To (Typ, Lhs);
2452 -- Deal with the ancestor part of extension aggregates or with the
2453 -- discriminants of the root type.
2455 if Nkind (N) = N_Extension_Aggregate then
2457 A : constant Node_Id := Ancestor_Part (N);
2461 -- If the ancestor part is a subtype mark "T", we generate
2463 -- init-proc (T(tmp)); if T is constrained and
2464 -- init-proc (S(tmp)); where S applies an appropriate
2465 -- constraint if T is unconstrained
2467 if Is_Entity_Name (A) and then Is_Type (Entity (A)) then
2468 Ancestor_Is_Subtype_Mark := True;
2470 if Is_Constrained (Entity (A)) then
2471 Init_Typ := Entity (A);
2473 -- For an ancestor part given by an unconstrained type mark,
2474 -- create a subtype constrained by appropriate corresponding
2475 -- discriminant values coming from either associations of the
2476 -- aggregate or a constraint on a parent type. The subtype will
2477 -- be used to generate the correct default value for the
2480 elsif Has_Discriminants (Entity (A)) then
2482 Anc_Typ : constant Entity_Id := Entity (A);
2483 Anc_Constr : constant List_Id := New_List;
2484 Discrim : Entity_Id;
2485 Disc_Value : Node_Id;
2486 New_Indic : Node_Id;
2487 Subt_Decl : Node_Id;
2490 Discrim := First_Discriminant (Anc_Typ);
2491 while Present (Discrim) loop
2492 Disc_Value := Ancestor_Discriminant_Value (Discrim);
2493 Append_To (Anc_Constr, Disc_Value);
2494 Next_Discriminant (Discrim);
2498 Make_Subtype_Indication (Loc,
2499 Subtype_Mark => New_Occurrence_Of (Anc_Typ, Loc),
2501 Make_Index_Or_Discriminant_Constraint (Loc,
2502 Constraints => Anc_Constr));
2504 Init_Typ := Create_Itype (Ekind (Anc_Typ), N);
2507 Make_Subtype_Declaration (Loc,
2508 Defining_Identifier => Init_Typ,
2509 Subtype_Indication => New_Indic);
2511 -- Itypes must be analyzed with checks off Declaration
2512 -- must have a parent for proper handling of subsidiary
2515 Set_Parent (Subt_Decl, N);
2516 Analyze (Subt_Decl, Suppress => All_Checks);
2520 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
2521 Set_Assignment_OK (Ref);
2524 Build_Initialization_Call (Loc,
2527 In_Init_Proc => Within_Init_Proc,
2528 With_Default_Init => Has_Default_Init_Comps (N)
2530 Has_Task (Base_Type (Init_Typ))));
2532 if Is_Constrained (Entity (A))
2533 and then Has_Discriminants (Entity (A))
2535 Check_Ancestor_Discriminants (Entity (A));
2538 -- Handle calls to C++ constructors
2540 elsif Is_CPP_Constructor_Call (A) then
2541 Init_Typ := Etype (Etype (A));
2542 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
2543 Set_Assignment_OK (Ref);
2546 Build_Initialization_Call (Loc,
2549 In_Init_Proc => Within_Init_Proc,
2550 With_Default_Init => Has_Default_Init_Comps (N),
2551 Constructor_Ref => A));
2553 -- Ada 2005 (AI-287): If the ancestor part is an aggregate of
2554 -- limited type, a recursive call expands the ancestor. Note that
2555 -- in the limited case, the ancestor part must be either a
2556 -- function call (possibly qualified, or wrapped in an unchecked
2557 -- conversion) or aggregate (definitely qualified).
2558 -- The ancestor part can also be a function call (that may be
2559 -- transformed into an explicit dereference) or a qualification
2562 elsif Is_Limited_Type (Etype (A))
2563 and then Nkind_In (Unqualify (A), N_Aggregate,
2564 N_Extension_Aggregate)
2566 Ancestor_Is_Expression := True;
2568 -- Set up finalization data for enclosing record, because
2569 -- controlled subcomponents of the ancestor part will be
2572 Gen_Ctrl_Actions_For_Aggr;
2575 Build_Record_Aggr_Code (
2577 Typ => Etype (Unqualify (A)),
2581 Is_Limited_Ancestor_Expansion => True));
2583 -- If the ancestor part is an expression "E", we generate
2587 -- In Ada 2005, this includes the case of a (possibly qualified)
2588 -- limited function call. The assignment will turn into a
2589 -- build-in-place function call (for further details, see
2590 -- Make_Build_In_Place_Call_In_Assignment).
2593 Ancestor_Is_Expression := True;
2594 Init_Typ := Etype (A);
2596 -- If the ancestor part is an aggregate, force its full
2597 -- expansion, which was delayed.
2599 if Nkind_In (Unqualify (A), N_Aggregate,
2600 N_Extension_Aggregate)
2602 Set_Analyzed (A, False);
2603 Set_Analyzed (Expression (A), False);
2606 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
2607 Set_Assignment_OK (Ref);
2609 -- Make the assignment without usual controlled actions since
2610 -- we only want the post adjust but not the pre finalize here
2611 -- Add manual adjust when necessary.
2613 Assign := New_List (
2614 Make_OK_Assignment_Statement (Loc,
2617 Set_No_Ctrl_Actions (First (Assign));
2619 -- Assign the tag now to make sure that the dispatching call in
2620 -- the subsequent deep_adjust works properly (unless VM_Target,
2621 -- where tags are implicit).
2623 if Tagged_Type_Expansion then
2625 Make_OK_Assignment_Statement (Loc,
2627 Make_Selected_Component (Loc,
2628 Prefix => New_Copy_Tree (Target),
2631 (First_Tag_Component (Base_Type (Typ)), Loc)),
2634 Unchecked_Convert_To (RTE (RE_Tag),
2637 (Access_Disp_Table (Base_Type (Typ)))),
2640 Set_Assignment_OK (Name (Instr));
2641 Append_To (Assign, Instr);
2643 -- Ada 2005 (AI-251): If tagged type has progenitors we must
2644 -- also initialize tags of the secondary dispatch tables.
2646 if Has_Interfaces (Base_Type (Typ)) then
2648 (Typ => Base_Type (Typ),
2650 Stmts_List => Assign);
2654 -- Call Adjust manually
2656 if Needs_Finalization (Etype (A))
2657 and then not Is_Limited_Type (Etype (A))
2659 Append_List_To (Assign,
2661 Ref => New_Copy_Tree (Ref),
2663 Flist_Ref => New_Reference_To (
2664 RTE (RE_Global_Final_List), Loc),
2665 With_Attach => Make_Integer_Literal (Loc, 0)));
2669 Make_Unsuppress_Block (Loc, Name_Discriminant_Check, Assign));
2671 if Has_Discriminants (Init_Typ) then
2672 Check_Ancestor_Discriminants (Init_Typ);
2677 -- Normal case (not an extension aggregate)
2680 -- Generate the discriminant expressions, component by component.
2681 -- If the base type is an unchecked union, the discriminants are
2682 -- unknown to the back-end and absent from a value of the type, so
2683 -- assignments for them are not emitted.
2685 if Has_Discriminants (Typ)
2686 and then not Is_Unchecked_Union (Base_Type (Typ))
2688 -- If the type is derived, and constrains discriminants of the
2689 -- parent type, these discriminants are not components of the
2690 -- aggregate, and must be initialized explicitly. They are not
2691 -- visible components of the object, but can become visible with
2692 -- a view conversion to the ancestor.
2696 Parent_Type : Entity_Id;
2698 Discr_Val : Elmt_Id;
2701 Btype := Base_Type (Typ);
2702 while Is_Derived_Type (Btype)
2703 and then Present (Stored_Constraint (Btype))
2705 Parent_Type := Etype (Btype);
2707 Disc := First_Discriminant (Parent_Type);
2709 First_Elmt (Stored_Constraint (Base_Type (Typ)));
2710 while Present (Discr_Val) loop
2712 -- Only those discriminants of the parent that are not
2713 -- renamed by discriminants of the derived type need to
2714 -- be added explicitly.
2716 if not Is_Entity_Name (Node (Discr_Val))
2718 Ekind (Entity (Node (Discr_Val))) /= E_Discriminant
2721 Make_Selected_Component (Loc,
2722 Prefix => New_Copy_Tree (Target),
2723 Selector_Name => New_Occurrence_Of (Disc, Loc));
2726 Make_OK_Assignment_Statement (Loc,
2728 Expression => New_Copy_Tree (Node (Discr_Val)));
2730 Set_No_Ctrl_Actions (Instr);
2731 Append_To (L, Instr);
2734 Next_Discriminant (Disc);
2735 Next_Elmt (Discr_Val);
2738 Btype := Base_Type (Parent_Type);
2742 -- Generate discriminant init values for the visible discriminants
2745 Discriminant : Entity_Id;
2746 Discriminant_Value : Node_Id;
2749 Discriminant := First_Stored_Discriminant (Typ);
2750 while Present (Discriminant) loop
2752 Make_Selected_Component (Loc,
2753 Prefix => New_Copy_Tree (Target),
2754 Selector_Name => New_Occurrence_Of (Discriminant, Loc));
2756 Discriminant_Value :=
2757 Get_Discriminant_Value (
2760 Discriminant_Constraint (N_Typ));
2763 Make_OK_Assignment_Statement (Loc,
2765 Expression => New_Copy_Tree (Discriminant_Value));
2767 Set_No_Ctrl_Actions (Instr);
2768 Append_To (L, Instr);
2770 Next_Stored_Discriminant (Discriminant);
2776 -- For CPP types we generate an implicit call to the C++ default
2777 -- constructor to ensure the proper initialization of the _Tag
2780 if Is_CPP_Class (Typ) then
2781 pragma Assert (Present (Base_Init_Proc (Typ)));
2783 Build_Initialization_Call (Loc,
2788 -- Generate the assignments, component by component
2790 -- tmp.comp1 := Expr1_From_Aggr;
2791 -- tmp.comp2 := Expr2_From_Aggr;
2794 Comp := First (Component_Associations (N));
2795 while Present (Comp) loop
2796 Selector := Entity (First (Choices (Comp)));
2800 if Is_CPP_Constructor_Call (Expression (Comp)) then
2802 Build_Initialization_Call (Loc,
2803 Id_Ref => Make_Selected_Component (Loc,
2804 Prefix => New_Copy_Tree (Target),
2805 Selector_Name => New_Occurrence_Of (Selector,
2807 Typ => Etype (Selector),
2809 With_Default_Init => True,
2810 Constructor_Ref => Expression (Comp)));
2812 -- Ada 2005 (AI-287): For each default-initialized component generate
2813 -- a call to the corresponding IP subprogram if available.
2815 elsif Box_Present (Comp)
2816 and then Has_Non_Null_Base_Init_Proc (Etype (Selector))
2818 if Ekind (Selector) /= E_Discriminant then
2819 Gen_Ctrl_Actions_For_Aggr;
2822 -- Ada 2005 (AI-287): If the component type has tasks then
2823 -- generate the activation chain and master entities (except
2824 -- in case of an allocator because in that case these entities
2825 -- are generated by Build_Task_Allocate_Block_With_Init_Stmts).
2828 Ctype : constant Entity_Id := Etype (Selector);
2829 Inside_Allocator : Boolean := False;
2830 P : Node_Id := Parent (N);
2833 if Is_Task_Type (Ctype) or else Has_Task (Ctype) then
2834 while Present (P) loop
2835 if Nkind (P) = N_Allocator then
2836 Inside_Allocator := True;
2843 if not Inside_Init_Proc and not Inside_Allocator then
2844 Build_Activation_Chain_Entity (N);
2850 Build_Initialization_Call (Loc,
2851 Id_Ref => Make_Selected_Component (Loc,
2852 Prefix => New_Copy_Tree (Target),
2853 Selector_Name => New_Occurrence_Of (Selector,
2855 Typ => Etype (Selector),
2857 With_Default_Init => True));
2859 -- Prepare for component assignment
2861 elsif Ekind (Selector) /= E_Discriminant
2862 or else Nkind (N) = N_Extension_Aggregate
2864 -- All the discriminants have now been assigned
2866 -- This is now a good moment to initialize and attach all the
2867 -- controllers. Their position may depend on the discriminants.
2869 if Ekind (Selector) /= E_Discriminant then
2870 Gen_Ctrl_Actions_For_Aggr;
2873 Comp_Type := Etype (Selector);
2875 Make_Selected_Component (Loc,
2876 Prefix => New_Copy_Tree (Target),
2877 Selector_Name => New_Occurrence_Of (Selector, Loc));
2879 if Nkind (Expression (Comp)) = N_Qualified_Expression then
2880 Expr_Q := Expression (Expression (Comp));
2882 Expr_Q := Expression (Comp);
2885 -- The controller is the one of the parent type defining the
2886 -- component (in case of inherited components).
2888 if Needs_Finalization (Comp_Type) then
2889 Internal_Final_List :=
2890 Make_Selected_Component (Loc,
2891 Prefix => Convert_To (
2892 Scope (Original_Record_Component (Selector)),
2893 New_Copy_Tree (Target)),
2895 Make_Identifier (Loc, Name_uController));
2897 Internal_Final_List :=
2898 Make_Selected_Component (Loc,
2899 Prefix => Internal_Final_List,
2900 Selector_Name => Make_Identifier (Loc, Name_F));
2902 -- The internal final list can be part of a constant object
2904 Set_Assignment_OK (Internal_Final_List);
2907 Internal_Final_List := Empty;
2910 -- Now either create the assignment or generate the code for the
2911 -- inner aggregate top-down.
2913 if Is_Delayed_Aggregate (Expr_Q) then
2915 -- We have the following case of aggregate nesting inside
2916 -- an object declaration:
2918 -- type Arr_Typ is array (Integer range <>) of ...;
2920 -- type Rec_Typ (...) is record
2921 -- Obj_Arr_Typ : Arr_Typ (A .. B);
2924 -- Obj_Rec_Typ : Rec_Typ := (...,
2925 -- Obj_Arr_Typ => (X => (...), Y => (...)));
2927 -- The length of the ranges of the aggregate and Obj_Add_Typ
2928 -- are equal (B - A = Y - X), but they do not coincide (X /=
2929 -- A and B /= Y). This case requires array sliding which is
2930 -- performed in the following manner:
2932 -- subtype Arr_Sub is Arr_Typ (X .. Y);
2934 -- Temp (X) := (...);
2936 -- Temp (Y) := (...);
2937 -- Obj_Rec_Typ.Obj_Arr_Typ := Temp;
2939 if Ekind (Comp_Type) = E_Array_Subtype
2940 and then Is_Int_Range_Bounds (Aggregate_Bounds (Expr_Q))
2941 and then Is_Int_Range_Bounds (First_Index (Comp_Type))
2943 Compatible_Int_Bounds
2944 (Agg_Bounds => Aggregate_Bounds (Expr_Q),
2945 Typ_Bounds => First_Index (Comp_Type))
2947 -- Create the array subtype with bounds equal to those of
2948 -- the corresponding aggregate.
2951 SubE : constant Entity_Id :=
2952 Make_Defining_Identifier (Loc,
2953 New_Internal_Name ('T'));
2955 SubD : constant Node_Id :=
2956 Make_Subtype_Declaration (Loc,
2957 Defining_Identifier =>
2959 Subtype_Indication =>
2960 Make_Subtype_Indication (Loc,
2961 Subtype_Mark => New_Reference_To (
2962 Etype (Comp_Type), Loc),
2964 Make_Index_Or_Discriminant_Constraint (
2965 Loc, Constraints => New_List (
2966 New_Copy_Tree (Aggregate_Bounds (
2969 -- Create a temporary array of the above subtype which
2970 -- will be used to capture the aggregate assignments.
2972 TmpE : constant Entity_Id :=
2973 Make_Defining_Identifier (Loc,
2974 New_Internal_Name ('A'));
2976 TmpD : constant Node_Id :=
2977 Make_Object_Declaration (Loc,
2978 Defining_Identifier =>
2980 Object_Definition =>
2981 New_Reference_To (SubE, Loc));
2984 Set_No_Initialization (TmpD);
2985 Append_To (L, SubD);
2986 Append_To (L, TmpD);
2988 -- Expand aggregate into assignments to the temp array
2991 Late_Expansion (Expr_Q, Comp_Type,
2992 New_Reference_To (TmpE, Loc), Internal_Final_List));
2997 Make_Assignment_Statement (Loc,
2998 Name => New_Copy_Tree (Comp_Expr),
2999 Expression => New_Reference_To (TmpE, Loc)));
3001 -- Do not pass the original aggregate to Gigi as is,
3002 -- since it will potentially clobber the front or the end
3003 -- of the array. Setting the expression to empty is safe
3004 -- since all aggregates are expanded into assignments.
3006 if Present (Obj) then
3007 Set_Expression (Parent (Obj), Empty);
3011 -- Normal case (sliding not required)
3015 Late_Expansion (Expr_Q, Comp_Type, Comp_Expr,
3016 Internal_Final_List));
3019 -- Expr_Q is not delayed aggregate
3023 Make_OK_Assignment_Statement (Loc,
3025 Expression => Expression (Comp));
3027 Set_No_Ctrl_Actions (Instr);
3028 Append_To (L, Instr);
3030 -- Adjust the tag if tagged (because of possible view
3031 -- conversions), unless compiling for a VM where tags are
3034 -- tmp.comp._tag := comp_typ'tag;
3036 if Is_Tagged_Type (Comp_Type)
3037 and then Tagged_Type_Expansion
3040 Make_OK_Assignment_Statement (Loc,
3042 Make_Selected_Component (Loc,
3043 Prefix => New_Copy_Tree (Comp_Expr),
3046 (First_Tag_Component (Comp_Type), Loc)),
3049 Unchecked_Convert_To (RTE (RE_Tag),
3051 (Node (First_Elmt (Access_Disp_Table (Comp_Type))),
3054 Append_To (L, Instr);
3057 -- Adjust and Attach the component to the proper controller
3059 -- Adjust (tmp.comp);
3060 -- Attach_To_Final_List (tmp.comp,
3061 -- comp_typ (tmp)._record_controller.f)
3063 if Needs_Finalization (Comp_Type)
3064 and then not Is_Limited_Type (Comp_Type)
3068 Ref => New_Copy_Tree (Comp_Expr),
3070 Flist_Ref => Internal_Final_List,
3071 With_Attach => Make_Integer_Literal (Loc, 1)));
3077 elsif Ekind (Selector) = E_Discriminant
3078 and then Nkind (N) /= N_Extension_Aggregate
3079 and then Nkind (Parent (N)) = N_Component_Association
3080 and then Is_Constrained (Typ)
3082 -- We must check that the discriminant value imposed by the
3083 -- context is the same as the value given in the subaggregate,
3084 -- because after the expansion into assignments there is no
3085 -- record on which to perform a regular discriminant check.
3092 D_Val := First_Elmt (Discriminant_Constraint (Typ));
3093 Disc := First_Discriminant (Typ);
3094 while Chars (Disc) /= Chars (Selector) loop
3095 Next_Discriminant (Disc);
3099 pragma Assert (Present (D_Val));
3101 -- This check cannot performed for components that are
3102 -- constrained by a current instance, because this is not a
3103 -- value that can be compared with the actual constraint.
3105 if Nkind (Node (D_Val)) /= N_Attribute_Reference
3106 or else not Is_Entity_Name (Prefix (Node (D_Val)))
3107 or else not Is_Type (Entity (Prefix (Node (D_Val))))
3110 Make_Raise_Constraint_Error (Loc,
3113 Left_Opnd => New_Copy_Tree (Node (D_Val)),
3114 Right_Opnd => Expression (Comp)),
3115 Reason => CE_Discriminant_Check_Failed));
3118 -- Find self-reference in previous discriminant assignment,
3119 -- and replace with proper expression.
3126 while Present (Ass) loop
3127 if Nkind (Ass) = N_Assignment_Statement
3128 and then Nkind (Name (Ass)) = N_Selected_Component
3129 and then Chars (Selector_Name (Name (Ass))) =
3133 (Ass, New_Copy_Tree (Expression (Comp)));
3146 -- If the type is tagged, the tag needs to be initialized (unless
3147 -- compiling for the Java VM where tags are implicit). It is done
3148 -- late in the initialization process because in some cases, we call
3149 -- the init proc of an ancestor which will not leave out the right tag
3151 if Ancestor_Is_Expression then
3154 -- For CPP types we generated a call to the C++ default constructor
3155 -- before the components have been initialized to ensure the proper
3156 -- initialization of the _Tag component (see above).
3158 elsif Is_CPP_Class (Typ) then
3161 elsif Is_Tagged_Type (Typ) and then Tagged_Type_Expansion then
3163 Make_OK_Assignment_Statement (Loc,
3165 Make_Selected_Component (Loc,
3166 Prefix => New_Copy_Tree (Target),
3169 (First_Tag_Component (Base_Type (Typ)), Loc)),
3172 Unchecked_Convert_To (RTE (RE_Tag),
3174 (Node (First_Elmt (Access_Disp_Table (Base_Type (Typ)))),
3177 Append_To (L, Instr);
3179 -- Ada 2005 (AI-251): If the tagged type has been derived from
3180 -- abstract interfaces we must also initialize the tags of the
3181 -- secondary dispatch tables.
3183 if Has_Interfaces (Base_Type (Typ)) then
3185 (Typ => Base_Type (Typ),
3191 -- If the controllers have not been initialized yet (by lack of non-
3192 -- discriminant components), let's do it now.
3194 Gen_Ctrl_Actions_For_Aggr;
3197 end Build_Record_Aggr_Code;
3199 -------------------------------
3200 -- Convert_Aggr_In_Allocator --
3201 -------------------------------
3203 procedure Convert_Aggr_In_Allocator
3208 Loc : constant Source_Ptr := Sloc (Aggr);
3209 Typ : constant Entity_Id := Etype (Aggr);
3210 Temp : constant Entity_Id := Defining_Identifier (Decl);
3212 Occ : constant Node_Id :=
3213 Unchecked_Convert_To (Typ,
3214 Make_Explicit_Dereference (Loc,
3215 New_Reference_To (Temp, Loc)));
3217 Access_Type : constant Entity_Id := Etype (Temp);
3221 -- If the allocator is for an access discriminant, there is no
3222 -- finalization list for the anonymous access type, and the eventual
3223 -- finalization of the object is handled through the coextension
3224 -- mechanism. If the enclosing object is not dynamically allocated,
3225 -- the access discriminant is itself placed on the stack. Otherwise,
3226 -- some other finalization list is used (see exp_ch4.adb).
3228 -- Decl has been inserted in the code ahead of the allocator, using
3229 -- Insert_Actions. We use Insert_Actions below as well, to ensure that
3230 -- subsequent insertions are done in the proper order. Using (for
3231 -- example) Insert_Actions_After to place the expanded aggregate
3232 -- immediately after Decl may lead to out-of-order references if the
3233 -- allocator has generated a finalization list, as when the designated
3234 -- object is controlled and there is an open transient scope.
3236 if Ekind (Access_Type) = E_Anonymous_Access_Type
3237 and then Nkind (Associated_Node_For_Itype (Access_Type)) =
3238 N_Discriminant_Specification
3242 Flist := Find_Final_List (Access_Type);
3245 if Is_Array_Type (Typ) then
3246 Convert_Array_Aggr_In_Allocator (Decl, Aggr, Occ);
3248 elsif Has_Default_Init_Comps (Aggr) then
3250 L : constant List_Id := New_List;
3251 Init_Stmts : List_Id;
3258 Associated_Final_Chain (Base_Type (Access_Type)));
3260 -- ??? Dubious actual for Obj: expect 'the original object being
3263 if Has_Task (Typ) then
3264 Build_Task_Allocate_Block_With_Init_Stmts (L, Aggr, Init_Stmts);
3265 Insert_Actions (Alloc, L);
3267 Insert_Actions (Alloc, Init_Stmts);
3272 Insert_Actions (Alloc,
3274 (Aggr, Typ, Occ, Flist,
3275 Associated_Final_Chain (Base_Type (Access_Type))));
3277 -- ??? Dubious actual for Obj: expect 'the original object being
3281 end Convert_Aggr_In_Allocator;
3283 --------------------------------
3284 -- Convert_Aggr_In_Assignment --
3285 --------------------------------
3287 procedure Convert_Aggr_In_Assignment (N : Node_Id) is
3288 Aggr : Node_Id := Expression (N);
3289 Typ : constant Entity_Id := Etype (Aggr);
3290 Occ : constant Node_Id := New_Copy_Tree (Name (N));
3293 if Nkind (Aggr) = N_Qualified_Expression then
3294 Aggr := Expression (Aggr);
3297 Insert_Actions_After (N,
3300 Find_Final_List (Typ, New_Copy_Tree (Occ))));
3301 end Convert_Aggr_In_Assignment;
3303 ---------------------------------
3304 -- Convert_Aggr_In_Object_Decl --
3305 ---------------------------------
3307 procedure Convert_Aggr_In_Object_Decl (N : Node_Id) is
3308 Obj : constant Entity_Id := Defining_Identifier (N);
3309 Aggr : Node_Id := Expression (N);
3310 Loc : constant Source_Ptr := Sloc (Aggr);
3311 Typ : constant Entity_Id := Etype (Aggr);
3312 Occ : constant Node_Id := New_Occurrence_Of (Obj, Loc);
3314 function Discriminants_Ok return Boolean;
3315 -- If the object type is constrained, the discriminants in the
3316 -- aggregate must be checked against the discriminants of the subtype.
3317 -- This cannot be done using Apply_Discriminant_Checks because after
3318 -- expansion there is no aggregate left to check.
3320 ----------------------
3321 -- Discriminants_Ok --
3322 ----------------------
3324 function Discriminants_Ok return Boolean is
3325 Cond : Node_Id := Empty;
3334 D := First_Discriminant (Typ);
3335 Disc1 := First_Elmt (Discriminant_Constraint (Typ));
3336 Disc2 := First_Elmt (Discriminant_Constraint (Etype (Obj)));
3337 while Present (Disc1) and then Present (Disc2) loop
3338 Val1 := Node (Disc1);
3339 Val2 := Node (Disc2);
3341 if not Is_OK_Static_Expression (Val1)
3342 or else not Is_OK_Static_Expression (Val2)
3344 Check := Make_Op_Ne (Loc,
3345 Left_Opnd => Duplicate_Subexpr (Val1),
3346 Right_Opnd => Duplicate_Subexpr (Val2));
3352 Cond := Make_Or_Else (Loc,
3354 Right_Opnd => Check);
3357 elsif Expr_Value (Val1) /= Expr_Value (Val2) then
3358 Apply_Compile_Time_Constraint_Error (Aggr,
3359 Msg => "incorrect value for discriminant&?",
3360 Reason => CE_Discriminant_Check_Failed,
3365 Next_Discriminant (D);
3370 -- If any discriminant constraint is non-static, emit a check
3372 if Present (Cond) then
3374 Make_Raise_Constraint_Error (Loc,
3376 Reason => CE_Discriminant_Check_Failed));
3380 end Discriminants_Ok;
3382 -- Start of processing for Convert_Aggr_In_Object_Decl
3385 Set_Assignment_OK (Occ);
3387 if Nkind (Aggr) = N_Qualified_Expression then
3388 Aggr := Expression (Aggr);
3391 if Has_Discriminants (Typ)
3392 and then Typ /= Etype (Obj)
3393 and then Is_Constrained (Etype (Obj))
3394 and then not Discriminants_Ok
3399 -- If the context is an extended return statement, it has its own
3400 -- finalization machinery (i.e. works like a transient scope) and
3401 -- we do not want to create an additional one, because objects on
3402 -- the finalization list of the return must be moved to the caller's
3403 -- finalization list to complete the return.
3405 -- However, if the aggregate is limited, it is built in place, and the
3406 -- controlled components are not assigned to intermediate temporaries
3407 -- so there is no need for a transient scope in this case either.
3409 if Requires_Transient_Scope (Typ)
3410 and then Ekind (Current_Scope) /= E_Return_Statement
3411 and then not Is_Limited_Type (Typ)
3413 Establish_Transient_Scope
3416 Is_Controlled (Typ) or else Has_Controlled_Component (Typ));
3419 Insert_Actions_After (N, Late_Expansion (Aggr, Typ, Occ, Obj => Obj));
3420 Set_No_Initialization (N);
3421 Initialize_Discriminants (N, Typ);
3422 end Convert_Aggr_In_Object_Decl;
3424 -------------------------------------
3425 -- Convert_Array_Aggr_In_Allocator --
3426 -------------------------------------
3428 procedure Convert_Array_Aggr_In_Allocator
3433 Aggr_Code : List_Id;
3434 Typ : constant Entity_Id := Etype (Aggr);
3435 Ctyp : constant Entity_Id := Component_Type (Typ);
3438 -- The target is an explicit dereference of the allocated object.
3439 -- Generate component assignments to it, as for an aggregate that
3440 -- appears on the right-hand side of an assignment statement.
3443 Build_Array_Aggr_Code (Aggr,
3445 Index => First_Index (Typ),
3447 Scalar_Comp => Is_Scalar_Type (Ctyp));
3449 Insert_Actions_After (Decl, Aggr_Code);
3450 end Convert_Array_Aggr_In_Allocator;
3452 ----------------------------
3453 -- Convert_To_Assignments --
3454 ----------------------------
3456 procedure Convert_To_Assignments (N : Node_Id; Typ : Entity_Id) is
3457 Loc : constant Source_Ptr := Sloc (N);
3462 Target_Expr : Node_Id;
3463 Parent_Kind : Node_Kind;
3464 Unc_Decl : Boolean := False;
3465 Parent_Node : Node_Id;
3468 pragma Assert (not Is_Static_Dispatch_Table_Aggregate (N));
3469 pragma Assert (Is_Record_Type (Typ));
3471 Parent_Node := Parent (N);
3472 Parent_Kind := Nkind (Parent_Node);
3474 if Parent_Kind = N_Qualified_Expression then
3476 -- Check if we are in a unconstrained declaration because in this
3477 -- case the current delayed expansion mechanism doesn't work when
3478 -- the declared object size depend on the initializing expr.
3481 Parent_Node := Parent (Parent_Node);
3482 Parent_Kind := Nkind (Parent_Node);
3484 if Parent_Kind = N_Object_Declaration then
3486 not Is_Entity_Name (Object_Definition (Parent_Node))
3487 or else Has_Discriminants
3488 (Entity (Object_Definition (Parent_Node)))
3489 or else Is_Class_Wide_Type
3490 (Entity (Object_Definition (Parent_Node)));
3495 -- Just set the Delay flag in the cases where the transformation will be
3496 -- done top down from above.
3500 -- Internal aggregate (transformed when expanding the parent)
3502 or else Parent_Kind = N_Aggregate
3503 or else Parent_Kind = N_Extension_Aggregate
3504 or else Parent_Kind = N_Component_Association
3506 -- Allocator (see Convert_Aggr_In_Allocator)
3508 or else Parent_Kind = N_Allocator
3510 -- Object declaration (see Convert_Aggr_In_Object_Decl)
3512 or else (Parent_Kind = N_Object_Declaration and then not Unc_Decl)
3514 -- Safe assignment (see Convert_Aggr_Assignments). So far only the
3515 -- assignments in init procs are taken into account.
3517 or else (Parent_Kind = N_Assignment_Statement
3518 and then Inside_Init_Proc)
3520 -- (Ada 2005) An inherently limited type in a return statement,
3521 -- which will be handled in a build-in-place fashion, and may be
3522 -- rewritten as an extended return and have its own finalization
3523 -- machinery. In the case of a simple return, the aggregate needs
3524 -- to be delayed until the scope for the return statement has been
3525 -- created, so that any finalization chain will be associated with
3526 -- that scope. For extended returns, we delay expansion to avoid the
3527 -- creation of an unwanted transient scope that could result in
3528 -- premature finalization of the return object (which is built in
3529 -- in place within the caller's scope).
3532 (Is_Inherently_Limited_Type (Typ)
3534 (Nkind (Parent (Parent_Node)) = N_Extended_Return_Statement
3535 or else Nkind (Parent_Node) = N_Simple_Return_Statement))
3537 Set_Expansion_Delayed (N);
3541 if Requires_Transient_Scope (Typ) then
3542 Establish_Transient_Scope
3544 Is_Controlled (Typ) or else Has_Controlled_Component (Typ));
3547 -- If the aggregate is non-limited, create a temporary. If it is limited
3548 -- and the context is an assignment, this is a subaggregate for an
3549 -- enclosing aggregate being expanded. It must be built in place, so use
3550 -- the target of the current assignment.
3552 if Is_Limited_Type (Typ)
3553 and then Nkind (Parent (N)) = N_Assignment_Statement
3555 Target_Expr := New_Copy_Tree (Name (Parent (N)));
3557 (Parent (N), Build_Record_Aggr_Code (N, Typ, Target_Expr));
3558 Rewrite (Parent (N), Make_Null_Statement (Loc));
3561 Temp := Make_Defining_Identifier (Loc, New_Internal_Name ('A'));
3563 -- If the type inherits unknown discriminants, use the view with
3564 -- known discriminants if available.
3566 if Has_Unknown_Discriminants (Typ)
3567 and then Present (Underlying_Record_View (Typ))
3569 T := Underlying_Record_View (Typ);
3575 Make_Object_Declaration (Loc,
3576 Defining_Identifier => Temp,
3577 Object_Definition => New_Occurrence_Of (T, Loc));
3579 Set_No_Initialization (Instr);
3580 Insert_Action (N, Instr);
3581 Initialize_Discriminants (Instr, T);
3582 Target_Expr := New_Occurrence_Of (Temp, Loc);
3583 Insert_Actions (N, Build_Record_Aggr_Code (N, T, Target_Expr));
3584 Rewrite (N, New_Occurrence_Of (Temp, Loc));
3585 Analyze_And_Resolve (N, T);
3587 end Convert_To_Assignments;
3589 ---------------------------
3590 -- Convert_To_Positional --
3591 ---------------------------
3593 procedure Convert_To_Positional
3595 Max_Others_Replicate : Nat := 5;
3596 Handle_Bit_Packed : Boolean := False)
3598 Typ : constant Entity_Id := Etype (N);
3600 Static_Components : Boolean := True;
3602 procedure Check_Static_Components;
3603 -- Check whether all components of the aggregate are compile-time known
3604 -- values, and can be passed as is to the back-end without further
3610 Ixb : Node_Id) return Boolean;
3611 -- Convert the aggregate into a purely positional form if possible. On
3612 -- entry the bounds of all dimensions are known to be static, and the
3613 -- total number of components is safe enough to expand.
3615 function Is_Flat (N : Node_Id; Dims : Int) return Boolean;
3616 -- Return True iff the array N is flat (which is not rivial in the case
3617 -- of multidimensionsl aggregates).
3619 -----------------------------
3620 -- Check_Static_Components --
3621 -----------------------------
3623 procedure Check_Static_Components is
3627 Static_Components := True;
3629 if Nkind (N) = N_String_Literal then
3632 elsif Present (Expressions (N)) then
3633 Expr := First (Expressions (N));
3634 while Present (Expr) loop
3635 if Nkind (Expr) /= N_Aggregate
3636 or else not Compile_Time_Known_Aggregate (Expr)
3637 or else Expansion_Delayed (Expr)
3639 Static_Components := False;
3647 if Nkind (N) = N_Aggregate
3648 and then Present (Component_Associations (N))
3650 Expr := First (Component_Associations (N));
3651 while Present (Expr) loop
3652 if Nkind (Expression (Expr)) = N_Integer_Literal then
3655 elsif Nkind (Expression (Expr)) /= N_Aggregate
3657 not Compile_Time_Known_Aggregate (Expression (Expr))
3658 or else Expansion_Delayed (Expression (Expr))
3660 Static_Components := False;
3667 end Check_Static_Components;
3676 Ixb : Node_Id) return Boolean
3678 Loc : constant Source_Ptr := Sloc (N);
3679 Blo : constant Node_Id := Type_Low_Bound (Etype (Ixb));
3680 Lo : constant Node_Id := Type_Low_Bound (Etype (Ix));
3681 Hi : constant Node_Id := Type_High_Bound (Etype (Ix));
3686 if Nkind (Original_Node (N)) = N_String_Literal then
3690 if not Compile_Time_Known_Value (Lo)
3691 or else not Compile_Time_Known_Value (Hi)
3696 Lov := Expr_Value (Lo);
3697 Hiv := Expr_Value (Hi);
3700 or else not Compile_Time_Known_Value (Blo)
3705 -- Determine if set of alternatives is suitable for conversion and
3706 -- build an array containing the values in sequence.
3709 Vals : array (UI_To_Int (Lov) .. UI_To_Int (Hiv))
3710 of Node_Id := (others => Empty);
3711 -- The values in the aggregate sorted appropriately
3714 -- Same data as Vals in list form
3717 -- Used to validate Max_Others_Replicate limit
3720 Num : Int := UI_To_Int (Lov);
3725 if Present (Expressions (N)) then
3726 Elmt := First (Expressions (N));
3727 while Present (Elmt) loop
3728 if Nkind (Elmt) = N_Aggregate
3729 and then Present (Next_Index (Ix))
3731 not Flatten (Elmt, Next_Index (Ix), Next_Index (Ixb))
3736 Vals (Num) := Relocate_Node (Elmt);
3743 if No (Component_Associations (N)) then
3747 Elmt := First (Component_Associations (N));
3749 if Nkind (Expression (Elmt)) = N_Aggregate then
3750 if Present (Next_Index (Ix))
3753 (Expression (Elmt), Next_Index (Ix), Next_Index (Ixb))
3759 Component_Loop : while Present (Elmt) loop
3760 Choice := First (Choices (Elmt));
3761 Choice_Loop : while Present (Choice) loop
3763 -- If we have an others choice, fill in the missing elements
3764 -- subject to the limit established by Max_Others_Replicate.
3766 if Nkind (Choice) = N_Others_Choice then
3769 for J in Vals'Range loop
3770 if No (Vals (J)) then
3771 Vals (J) := New_Copy_Tree (Expression (Elmt));
3772 Rep_Count := Rep_Count + 1;
3774 -- Check for maximum others replication. Note that
3775 -- we skip this test if either of the restrictions
3776 -- No_Elaboration_Code or No_Implicit_Loops is
3777 -- active, if this is a preelaborable unit or a
3778 -- predefined unit. This ensures that predefined
3779 -- units get the same level of constant folding in
3780 -- Ada 95 and Ada 05, where their categorization
3784 P : constant Entity_Id :=
3785 Cunit_Entity (Current_Sem_Unit);
3788 -- Check if duplication OK and if so continue
3791 if Restriction_Active (No_Elaboration_Code)
3792 or else Restriction_Active (No_Implicit_Loops)
3793 or else Is_Preelaborated (P)
3794 or else (Ekind (P) = E_Package_Body
3796 Is_Preelaborated (Spec_Entity (P)))
3798 Is_Predefined_File_Name
3799 (Unit_File_Name (Get_Source_Unit (P)))
3803 -- If duplication not OK, then we return False
3804 -- if the replication count is too high
3806 elsif Rep_Count > Max_Others_Replicate then
3809 -- Continue on if duplication not OK, but the
3810 -- replication count is not excessive.
3819 exit Component_Loop;
3821 -- Case of a subtype mark
3823 elsif Nkind (Choice) = N_Identifier
3824 and then Is_Type (Entity (Choice))
3826 Lo := Type_Low_Bound (Etype (Choice));
3827 Hi := Type_High_Bound (Etype (Choice));
3829 -- Case of subtype indication
3831 elsif Nkind (Choice) = N_Subtype_Indication then
3832 Lo := Low_Bound (Range_Expression (Constraint (Choice)));
3833 Hi := High_Bound (Range_Expression (Constraint (Choice)));
3837 elsif Nkind (Choice) = N_Range then
3838 Lo := Low_Bound (Choice);
3839 Hi := High_Bound (Choice);
3841 -- Normal subexpression case
3843 else pragma Assert (Nkind (Choice) in N_Subexpr);
3844 if not Compile_Time_Known_Value (Choice) then
3848 Vals (UI_To_Int (Expr_Value (Choice))) :=
3849 New_Copy_Tree (Expression (Elmt));
3854 -- Range cases merge with Lo,Hi said
3856 if not Compile_Time_Known_Value (Lo)
3858 not Compile_Time_Known_Value (Hi)
3862 for J in UI_To_Int (Expr_Value (Lo)) ..
3863 UI_To_Int (Expr_Value (Hi))
3865 Vals (J) := New_Copy_Tree (Expression (Elmt));
3871 end loop Choice_Loop;
3874 end loop Component_Loop;
3876 -- If we get here the conversion is possible
3879 for J in Vals'Range loop
3880 Append (Vals (J), Vlist);
3883 Rewrite (N, Make_Aggregate (Loc, Expressions => Vlist));
3884 Set_Aggregate_Bounds (N, Aggregate_Bounds (Original_Node (N)));
3893 function Is_Flat (N : Node_Id; Dims : Int) return Boolean is
3900 elsif Nkind (N) = N_Aggregate then
3901 if Present (Component_Associations (N)) then
3905 Elmt := First (Expressions (N));
3906 while Present (Elmt) loop
3907 if not Is_Flat (Elmt, Dims - 1) then
3921 -- Start of processing for Convert_To_Positional
3924 -- Ada 2005 (AI-287): Do not convert in case of default initialized
3925 -- components because in this case will need to call the corresponding
3928 if Has_Default_Init_Comps (N) then
3932 if Is_Flat (N, Number_Dimensions (Typ)) then
3936 if Is_Bit_Packed_Array (Typ)
3937 and then not Handle_Bit_Packed
3942 -- Do not convert to positional if controlled components are involved
3943 -- since these require special processing
3945 if Has_Controlled_Component (Typ) then
3949 Check_Static_Components;
3951 -- If the size is known, or all the components are static, try to
3952 -- build a fully positional aggregate.
3954 -- The size of the type may not be known for an aggregate with
3955 -- discriminated array components, but if the components are static
3956 -- it is still possible to verify statically that the length is
3957 -- compatible with the upper bound of the type, and therefore it is
3958 -- worth flattening such aggregates as well.
3960 -- For now the back-end expands these aggregates into individual
3961 -- assignments to the target anyway, but it is conceivable that
3962 -- it will eventually be able to treat such aggregates statically???
3964 if Aggr_Size_OK (N, Typ)
3965 and then Flatten (N, First_Index (Typ), First_Index (Base_Type (Typ)))
3967 if Static_Components then
3968 Set_Compile_Time_Known_Aggregate (N);
3969 Set_Expansion_Delayed (N, False);
3972 Analyze_And_Resolve (N, Typ);
3974 end Convert_To_Positional;
3976 ----------------------------
3977 -- Expand_Array_Aggregate --
3978 ----------------------------
3980 -- Array aggregate expansion proceeds as follows:
3982 -- 1. If requested we generate code to perform all the array aggregate
3983 -- bound checks, specifically
3985 -- (a) Check that the index range defined by aggregate bounds is
3986 -- compatible with corresponding index subtype.
3988 -- (b) If an others choice is present check that no aggregate
3989 -- index is outside the bounds of the index constraint.
3991 -- (c) For multidimensional arrays make sure that all subaggregates
3992 -- corresponding to the same dimension have the same bounds.
3994 -- 2. Check for packed array aggregate which can be converted to a
3995 -- constant so that the aggregate disappeares completely.
3997 -- 3. Check case of nested aggregate. Generally nested aggregates are
3998 -- handled during the processing of the parent aggregate.
4000 -- 4. Check if the aggregate can be statically processed. If this is the
4001 -- case pass it as is to Gigi. Note that a necessary condition for
4002 -- static processing is that the aggregate be fully positional.
4004 -- 5. If in place aggregate expansion is possible (i.e. no need to create
4005 -- a temporary) then mark the aggregate as such and return. Otherwise
4006 -- create a new temporary and generate the appropriate initialization
4009 procedure Expand_Array_Aggregate (N : Node_Id) is
4010 Loc : constant Source_Ptr := Sloc (N);
4012 Typ : constant Entity_Id := Etype (N);
4013 Ctyp : constant Entity_Id := Component_Type (Typ);
4014 -- Typ is the correct constrained array subtype of the aggregate
4015 -- Ctyp is the corresponding component type.
4017 Aggr_Dimension : constant Pos := Number_Dimensions (Typ);
4018 -- Number of aggregate index dimensions
4020 Aggr_Low : array (1 .. Aggr_Dimension) of Node_Id;
4021 Aggr_High : array (1 .. Aggr_Dimension) of Node_Id;
4022 -- Low and High bounds of the constraint for each aggregate index
4024 Aggr_Index_Typ : array (1 .. Aggr_Dimension) of Entity_Id;
4025 -- The type of each index
4027 Maybe_In_Place_OK : Boolean;
4028 -- If the type is neither controlled nor packed and the aggregate
4029 -- is the expression in an assignment, assignment in place may be
4030 -- possible, provided other conditions are met on the LHS.
4032 Others_Present : array (1 .. Aggr_Dimension) of Boolean :=
4034 -- If Others_Present (J) is True, then there is an others choice
4035 -- in one of the sub-aggregates of N at dimension J.
4037 procedure Build_Constrained_Type (Positional : Boolean);
4038 -- If the subtype is not static or unconstrained, build a constrained
4039 -- type using the computable sizes of the aggregate and its sub-
4042 procedure Check_Bounds (Aggr_Bounds : Node_Id; Index_Bounds : Node_Id);
4043 -- Checks that the bounds of Aggr_Bounds are within the bounds defined
4046 procedure Check_Same_Aggr_Bounds (Sub_Aggr : Node_Id; Dim : Pos);
4047 -- Checks that in a multi-dimensional array aggregate all subaggregates
4048 -- corresponding to the same dimension have the same bounds.
4049 -- Sub_Aggr is an array sub-aggregate. Dim is the dimension
4050 -- corresponding to the sub-aggregate.
4052 procedure Compute_Others_Present (Sub_Aggr : Node_Id; Dim : Pos);
4053 -- Computes the values of array Others_Present. Sub_Aggr is the
4054 -- array sub-aggregate we start the computation from. Dim is the
4055 -- dimension corresponding to the sub-aggregate.
4057 function Has_Address_Clause (D : Node_Id) return Boolean;
4058 -- If the aggregate is the expression in an object declaration, it
4059 -- cannot be expanded in place. This function does a lookahead in the
4060 -- current declarative part to find an address clause for the object
4063 function In_Place_Assign_OK return Boolean;
4064 -- Simple predicate to determine whether an aggregate assignment can
4065 -- be done in place, because none of the new values can depend on the
4066 -- components of the target of the assignment.
4068 procedure Others_Check (Sub_Aggr : Node_Id; Dim : Pos);
4069 -- Checks that if an others choice is present in any sub-aggregate no
4070 -- aggregate index is outside the bounds of the index constraint.
4071 -- Sub_Aggr is an array sub-aggregate. Dim is the dimension
4072 -- corresponding to the sub-aggregate.
4074 ----------------------------
4075 -- Build_Constrained_Type --
4076 ----------------------------
4078 procedure Build_Constrained_Type (Positional : Boolean) is
4079 Loc : constant Source_Ptr := Sloc (N);
4080 Agg_Type : Entity_Id;
4083 Typ : constant Entity_Id := Etype (N);
4084 Indices : constant List_Id := New_List;
4090 Make_Defining_Identifier (
4091 Loc, New_Internal_Name ('A'));
4093 -- If the aggregate is purely positional, all its subaggregates
4094 -- have the same size. We collect the dimensions from the first
4095 -- subaggregate at each level.
4100 for D in 1 .. Number_Dimensions (Typ) loop
4101 Sub_Agg := First (Expressions (Sub_Agg));
4105 while Present (Comp) loop
4112 Low_Bound => Make_Integer_Literal (Loc, 1),
4114 Make_Integer_Literal (Loc, Num)),
4119 -- We know the aggregate type is unconstrained and the aggregate
4120 -- is not processable by the back end, therefore not necessarily
4121 -- positional. Retrieve each dimension bounds (computed earlier).
4124 for D in 1 .. Number_Dimensions (Typ) loop
4127 Low_Bound => Aggr_Low (D),
4128 High_Bound => Aggr_High (D)),
4134 Make_Full_Type_Declaration (Loc,
4135 Defining_Identifier => Agg_Type,
4137 Make_Constrained_Array_Definition (Loc,
4138 Discrete_Subtype_Definitions => Indices,
4139 Component_Definition =>
4140 Make_Component_Definition (Loc,
4141 Aliased_Present => False,
4142 Subtype_Indication =>
4143 New_Occurrence_Of (Component_Type (Typ), Loc))));
4145 Insert_Action (N, Decl);
4147 Set_Etype (N, Agg_Type);
4148 Set_Is_Itype (Agg_Type);
4149 Freeze_Itype (Agg_Type, N);
4150 end Build_Constrained_Type;
4156 procedure Check_Bounds (Aggr_Bounds : Node_Id; Index_Bounds : Node_Id) is
4163 Cond : Node_Id := Empty;
4166 Get_Index_Bounds (Aggr_Bounds, Aggr_Lo, Aggr_Hi);
4167 Get_Index_Bounds (Index_Bounds, Ind_Lo, Ind_Hi);
4169 -- Generate the following test:
4171 -- [constraint_error when
4172 -- Aggr_Lo <= Aggr_Hi and then
4173 -- (Aggr_Lo < Ind_Lo or else Aggr_Hi > Ind_Hi)]
4175 -- As an optimization try to see if some tests are trivially vacuous
4176 -- because we are comparing an expression against itself.
4178 if Aggr_Lo = Ind_Lo and then Aggr_Hi = Ind_Hi then
4181 elsif Aggr_Hi = Ind_Hi then
4184 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
4185 Right_Opnd => Duplicate_Subexpr_Move_Checks (Ind_Lo));
4187 elsif Aggr_Lo = Ind_Lo then
4190 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi),
4191 Right_Opnd => Duplicate_Subexpr_Move_Checks (Ind_Hi));
4198 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
4199 Right_Opnd => Duplicate_Subexpr_Move_Checks (Ind_Lo)),
4203 Left_Opnd => Duplicate_Subexpr (Aggr_Hi),
4204 Right_Opnd => Duplicate_Subexpr (Ind_Hi)));
4207 if Present (Cond) then
4212 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
4213 Right_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi)),
4215 Right_Opnd => Cond);
4217 Set_Analyzed (Left_Opnd (Left_Opnd (Cond)), False);
4218 Set_Analyzed (Right_Opnd (Left_Opnd (Cond)), False);
4220 Make_Raise_Constraint_Error (Loc,
4222 Reason => CE_Length_Check_Failed));
4226 ----------------------------
4227 -- Check_Same_Aggr_Bounds --
4228 ----------------------------
4230 procedure Check_Same_Aggr_Bounds (Sub_Aggr : Node_Id; Dim : Pos) is
4231 Sub_Lo : constant Node_Id := Low_Bound (Aggregate_Bounds (Sub_Aggr));
4232 Sub_Hi : constant Node_Id := High_Bound (Aggregate_Bounds (Sub_Aggr));
4233 -- The bounds of this specific sub-aggregate
4235 Aggr_Lo : constant Node_Id := Aggr_Low (Dim);
4236 Aggr_Hi : constant Node_Id := Aggr_High (Dim);
4237 -- The bounds of the aggregate for this dimension
4239 Ind_Typ : constant Entity_Id := Aggr_Index_Typ (Dim);
4240 -- The index type for this dimension.xxx
4242 Cond : Node_Id := Empty;
4247 -- If index checks are on generate the test
4249 -- [constraint_error when
4250 -- Aggr_Lo /= Sub_Lo or else Aggr_Hi /= Sub_Hi]
4252 -- As an optimization try to see if some tests are trivially vacuos
4253 -- because we are comparing an expression against itself. Also for
4254 -- the first dimension the test is trivially vacuous because there
4255 -- is just one aggregate for dimension 1.
4257 if Index_Checks_Suppressed (Ind_Typ) then
4261 or else (Aggr_Lo = Sub_Lo and then Aggr_Hi = Sub_Hi)
4265 elsif Aggr_Hi = Sub_Hi then
4268 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
4269 Right_Opnd => Duplicate_Subexpr_Move_Checks (Sub_Lo));
4271 elsif Aggr_Lo = Sub_Lo then
4274 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi),
4275 Right_Opnd => Duplicate_Subexpr_Move_Checks (Sub_Hi));
4282 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
4283 Right_Opnd => Duplicate_Subexpr_Move_Checks (Sub_Lo)),
4287 Left_Opnd => Duplicate_Subexpr (Aggr_Hi),
4288 Right_Opnd => Duplicate_Subexpr (Sub_Hi)));
4291 if Present (Cond) then
4293 Make_Raise_Constraint_Error (Loc,
4295 Reason => CE_Length_Check_Failed));
4298 -- Now look inside the sub-aggregate to see if there is more work
4300 if Dim < Aggr_Dimension then
4302 -- Process positional components
4304 if Present (Expressions (Sub_Aggr)) then
4305 Expr := First (Expressions (Sub_Aggr));
4306 while Present (Expr) loop
4307 Check_Same_Aggr_Bounds (Expr, Dim + 1);
4312 -- Process component associations
4314 if Present (Component_Associations (Sub_Aggr)) then
4315 Assoc := First (Component_Associations (Sub_Aggr));
4316 while Present (Assoc) loop
4317 Expr := Expression (Assoc);
4318 Check_Same_Aggr_Bounds (Expr, Dim + 1);
4323 end Check_Same_Aggr_Bounds;
4325 ----------------------------
4326 -- Compute_Others_Present --
4327 ----------------------------
4329 procedure Compute_Others_Present (Sub_Aggr : Node_Id; Dim : Pos) is
4334 if Present (Component_Associations (Sub_Aggr)) then
4335 Assoc := Last (Component_Associations (Sub_Aggr));
4337 if Nkind (First (Choices (Assoc))) = N_Others_Choice then
4338 Others_Present (Dim) := True;
4342 -- Now look inside the sub-aggregate to see if there is more work
4344 if Dim < Aggr_Dimension then
4346 -- Process positional components
4348 if Present (Expressions (Sub_Aggr)) then
4349 Expr := First (Expressions (Sub_Aggr));
4350 while Present (Expr) loop
4351 Compute_Others_Present (Expr, Dim + 1);
4356 -- Process component associations
4358 if Present (Component_Associations (Sub_Aggr)) then
4359 Assoc := First (Component_Associations (Sub_Aggr));
4360 while Present (Assoc) loop
4361 Expr := Expression (Assoc);
4362 Compute_Others_Present (Expr, Dim + 1);
4367 end Compute_Others_Present;
4369 ------------------------
4370 -- Has_Address_Clause --
4371 ------------------------
4373 function Has_Address_Clause (D : Node_Id) return Boolean is
4374 Id : constant Entity_Id := Defining_Identifier (D);
4379 while Present (Decl) loop
4380 if Nkind (Decl) = N_At_Clause
4381 and then Chars (Identifier (Decl)) = Chars (Id)
4385 elsif Nkind (Decl) = N_Attribute_Definition_Clause
4386 and then Chars (Decl) = Name_Address
4387 and then Chars (Name (Decl)) = Chars (Id)
4396 end Has_Address_Clause;
4398 ------------------------
4399 -- In_Place_Assign_OK --
4400 ------------------------
4402 function In_Place_Assign_OK return Boolean is
4410 function Is_Others_Aggregate (Aggr : Node_Id) return Boolean;
4411 -- Aggregates that consist of a single Others choice are safe
4412 -- if the single expression is.
4414 function Safe_Aggregate (Aggr : Node_Id) return Boolean;
4415 -- Check recursively that each component of a (sub)aggregate does
4416 -- not depend on the variable being assigned to.
4418 function Safe_Component (Expr : Node_Id) return Boolean;
4419 -- Verify that an expression cannot depend on the variable being
4420 -- assigned to. Room for improvement here (but less than before).
4422 -------------------------
4423 -- Is_Others_Aggregate --
4424 -------------------------
4426 function Is_Others_Aggregate (Aggr : Node_Id) return Boolean is
4428 return No (Expressions (Aggr))
4430 (First (Choices (First (Component_Associations (Aggr)))))
4432 end Is_Others_Aggregate;
4434 --------------------
4435 -- Safe_Aggregate --
4436 --------------------
4438 function Safe_Aggregate (Aggr : Node_Id) return Boolean is
4442 if Present (Expressions (Aggr)) then
4443 Expr := First (Expressions (Aggr));
4444 while Present (Expr) loop
4445 if Nkind (Expr) = N_Aggregate then
4446 if not Safe_Aggregate (Expr) then
4450 elsif not Safe_Component (Expr) then
4458 if Present (Component_Associations (Aggr)) then
4459 Expr := First (Component_Associations (Aggr));
4460 while Present (Expr) loop
4461 if Nkind (Expression (Expr)) = N_Aggregate then
4462 if not Safe_Aggregate (Expression (Expr)) then
4466 elsif not Safe_Component (Expression (Expr)) then
4477 --------------------
4478 -- Safe_Component --
4479 --------------------
4481 function Safe_Component (Expr : Node_Id) return Boolean is
4482 Comp : Node_Id := Expr;
4484 function Check_Component (Comp : Node_Id) return Boolean;
4485 -- Do the recursive traversal, after copy
4487 ---------------------
4488 -- Check_Component --
4489 ---------------------
4491 function Check_Component (Comp : Node_Id) return Boolean is
4493 if Is_Overloaded (Comp) then
4497 return Compile_Time_Known_Value (Comp)
4499 or else (Is_Entity_Name (Comp)
4500 and then Present (Entity (Comp))
4501 and then No (Renamed_Object (Entity (Comp))))
4503 or else (Nkind (Comp) = N_Attribute_Reference
4504 and then Check_Component (Prefix (Comp)))
4506 or else (Nkind (Comp) in N_Binary_Op
4507 and then Check_Component (Left_Opnd (Comp))
4508 and then Check_Component (Right_Opnd (Comp)))
4510 or else (Nkind (Comp) in N_Unary_Op
4511 and then Check_Component (Right_Opnd (Comp)))
4513 or else (Nkind (Comp) = N_Selected_Component
4514 and then Check_Component (Prefix (Comp)))
4516 or else (Nkind (Comp) = N_Unchecked_Type_Conversion
4517 and then Check_Component (Expression (Comp)));
4518 end Check_Component;
4520 -- Start of processing for Safe_Component
4523 -- If the component appears in an association that may
4524 -- correspond to more than one element, it is not analyzed
4525 -- before the expansion into assignments, to avoid side effects.
4526 -- We analyze, but do not resolve the copy, to obtain sufficient
4527 -- entity information for the checks that follow. If component is
4528 -- overloaded we assume an unsafe function call.
4530 if not Analyzed (Comp) then
4531 if Is_Overloaded (Expr) then
4534 elsif Nkind (Expr) = N_Aggregate
4535 and then not Is_Others_Aggregate (Expr)
4539 elsif Nkind (Expr) = N_Allocator then
4541 -- For now, too complex to analyze
4546 Comp := New_Copy_Tree (Expr);
4547 Set_Parent (Comp, Parent (Expr));
4551 if Nkind (Comp) = N_Aggregate then
4552 return Safe_Aggregate (Comp);
4554 return Check_Component (Comp);
4558 -- Start of processing for In_Place_Assign_OK
4561 if Present (Component_Associations (N)) then
4563 -- On assignment, sliding can take place, so we cannot do the
4564 -- assignment in place unless the bounds of the aggregate are
4565 -- statically equal to those of the target.
4567 -- If the aggregate is given by an others choice, the bounds
4568 -- are derived from the left-hand side, and the assignment is
4569 -- safe if the expression is.
4571 if Is_Others_Aggregate (N) then
4574 (Expression (First (Component_Associations (N))));
4577 Aggr_In := First_Index (Etype (N));
4578 if Nkind (Parent (N)) = N_Assignment_Statement then
4579 Obj_In := First_Index (Etype (Name (Parent (N))));
4582 -- Context is an allocator. Check bounds of aggregate
4583 -- against given type in qualified expression.
4585 pragma Assert (Nkind (Parent (Parent (N))) = N_Allocator);
4587 First_Index (Etype (Entity (Subtype_Mark (Parent (N)))));
4590 while Present (Aggr_In) loop
4591 Get_Index_Bounds (Aggr_In, Aggr_Lo, Aggr_Hi);
4592 Get_Index_Bounds (Obj_In, Obj_Lo, Obj_Hi);
4594 if not Compile_Time_Known_Value (Aggr_Lo)
4595 or else not Compile_Time_Known_Value (Aggr_Hi)
4596 or else not Compile_Time_Known_Value (Obj_Lo)
4597 or else not Compile_Time_Known_Value (Obj_Hi)
4598 or else Expr_Value (Aggr_Lo) /= Expr_Value (Obj_Lo)
4599 or else Expr_Value (Aggr_Hi) /= Expr_Value (Obj_Hi)
4604 Next_Index (Aggr_In);
4605 Next_Index (Obj_In);
4609 -- Now check the component values themselves
4611 return Safe_Aggregate (N);
4612 end In_Place_Assign_OK;
4618 procedure Others_Check (Sub_Aggr : Node_Id; Dim : Pos) is
4619 Aggr_Lo : constant Node_Id := Aggr_Low (Dim);
4620 Aggr_Hi : constant Node_Id := Aggr_High (Dim);
4621 -- The bounds of the aggregate for this dimension
4623 Ind_Typ : constant Entity_Id := Aggr_Index_Typ (Dim);
4624 -- The index type for this dimension
4626 Need_To_Check : Boolean := False;
4628 Choices_Lo : Node_Id := Empty;
4629 Choices_Hi : Node_Id := Empty;
4630 -- The lowest and highest discrete choices for a named sub-aggregate
4632 Nb_Choices : Int := -1;
4633 -- The number of discrete non-others choices in this sub-aggregate
4635 Nb_Elements : Uint := Uint_0;
4636 -- The number of elements in a positional aggregate
4638 Cond : Node_Id := Empty;
4645 -- Check if we have an others choice. If we do make sure that this
4646 -- sub-aggregate contains at least one element in addition to the
4649 if Range_Checks_Suppressed (Ind_Typ) then
4650 Need_To_Check := False;
4652 elsif Present (Expressions (Sub_Aggr))
4653 and then Present (Component_Associations (Sub_Aggr))
4655 Need_To_Check := True;
4657 elsif Present (Component_Associations (Sub_Aggr)) then
4658 Assoc := Last (Component_Associations (Sub_Aggr));
4660 if Nkind (First (Choices (Assoc))) /= N_Others_Choice then
4661 Need_To_Check := False;
4664 -- Count the number of discrete choices. Start with -1 because
4665 -- the others choice does not count.
4668 Assoc := First (Component_Associations (Sub_Aggr));
4669 while Present (Assoc) loop
4670 Choice := First (Choices (Assoc));
4671 while Present (Choice) loop
4672 Nb_Choices := Nb_Choices + 1;
4679 -- If there is only an others choice nothing to do
4681 Need_To_Check := (Nb_Choices > 0);
4685 Need_To_Check := False;
4688 -- If we are dealing with a positional sub-aggregate with an others
4689 -- choice then compute the number or positional elements.
4691 if Need_To_Check and then Present (Expressions (Sub_Aggr)) then
4692 Expr := First (Expressions (Sub_Aggr));
4693 Nb_Elements := Uint_0;
4694 while Present (Expr) loop
4695 Nb_Elements := Nb_Elements + 1;
4699 -- If the aggregate contains discrete choices and an others choice
4700 -- compute the smallest and largest discrete choice values.
4702 elsif Need_To_Check then
4703 Compute_Choices_Lo_And_Choices_Hi : declare
4705 Table : Case_Table_Type (1 .. Nb_Choices);
4706 -- Used to sort all the different choice values
4713 Assoc := First (Component_Associations (Sub_Aggr));
4714 while Present (Assoc) loop
4715 Choice := First (Choices (Assoc));
4716 while Present (Choice) loop
4717 if Nkind (Choice) = N_Others_Choice then
4721 Get_Index_Bounds (Choice, Low, High);
4722 Table (J).Choice_Lo := Low;
4723 Table (J).Choice_Hi := High;
4732 -- Sort the discrete choices
4734 Sort_Case_Table (Table);
4736 Choices_Lo := Table (1).Choice_Lo;
4737 Choices_Hi := Table (Nb_Choices).Choice_Hi;
4738 end Compute_Choices_Lo_And_Choices_Hi;
4741 -- If no others choice in this sub-aggregate, or the aggregate
4742 -- comprises only an others choice, nothing to do.
4744 if not Need_To_Check then
4747 -- If we are dealing with an aggregate containing an others choice
4748 -- and positional components, we generate the following test:
4750 -- if Ind_Typ'Pos (Aggr_Lo) + (Nb_Elements - 1) >
4751 -- Ind_Typ'Pos (Aggr_Hi)
4753 -- raise Constraint_Error;
4756 elsif Nb_Elements > Uint_0 then
4762 Make_Attribute_Reference (Loc,
4763 Prefix => New_Reference_To (Ind_Typ, Loc),
4764 Attribute_Name => Name_Pos,
4767 (Duplicate_Subexpr_Move_Checks (Aggr_Lo))),
4768 Right_Opnd => Make_Integer_Literal (Loc, Nb_Elements - 1)),
4771 Make_Attribute_Reference (Loc,
4772 Prefix => New_Reference_To (Ind_Typ, Loc),
4773 Attribute_Name => Name_Pos,
4774 Expressions => New_List (
4775 Duplicate_Subexpr_Move_Checks (Aggr_Hi))));
4777 -- If we are dealing with an aggregate containing an others choice
4778 -- and discrete choices we generate the following test:
4780 -- [constraint_error when
4781 -- Choices_Lo < Aggr_Lo or else Choices_Hi > Aggr_Hi];
4789 Duplicate_Subexpr_Move_Checks (Choices_Lo),
4791 Duplicate_Subexpr_Move_Checks (Aggr_Lo)),
4796 Duplicate_Subexpr (Choices_Hi),
4798 Duplicate_Subexpr (Aggr_Hi)));
4801 if Present (Cond) then
4803 Make_Raise_Constraint_Error (Loc,
4805 Reason => CE_Length_Check_Failed));
4806 -- Questionable reason code, shouldn't that be a
4807 -- CE_Range_Check_Failed ???
4810 -- Now look inside the sub-aggregate to see if there is more work
4812 if Dim < Aggr_Dimension then
4814 -- Process positional components
4816 if Present (Expressions (Sub_Aggr)) then
4817 Expr := First (Expressions (Sub_Aggr));
4818 while Present (Expr) loop
4819 Others_Check (Expr, Dim + 1);
4824 -- Process component associations
4826 if Present (Component_Associations (Sub_Aggr)) then
4827 Assoc := First (Component_Associations (Sub_Aggr));
4828 while Present (Assoc) loop
4829 Expr := Expression (Assoc);
4830 Others_Check (Expr, Dim + 1);
4837 -- Remaining Expand_Array_Aggregate variables
4840 -- Holds the temporary aggregate value
4843 -- Holds the declaration of Tmp
4845 Aggr_Code : List_Id;
4846 Parent_Node : Node_Id;
4847 Parent_Kind : Node_Kind;
4849 -- Start of processing for Expand_Array_Aggregate
4852 -- Do not touch the special aggregates of attributes used for Asm calls
4854 if Is_RTE (Ctyp, RE_Asm_Input_Operand)
4855 or else Is_RTE (Ctyp, RE_Asm_Output_Operand)
4860 -- If the semantic analyzer has determined that aggregate N will raise
4861 -- Constraint_Error at run-time, then the aggregate node has been
4862 -- replaced with an N_Raise_Constraint_Error node and we should
4865 pragma Assert (not Raises_Constraint_Error (N));
4869 -- Check that the index range defined by aggregate bounds is
4870 -- compatible with corresponding index subtype.
4872 Index_Compatibility_Check : declare
4873 Aggr_Index_Range : Node_Id := First_Index (Typ);
4874 -- The current aggregate index range
4876 Index_Constraint : Node_Id := First_Index (Etype (Typ));
4877 -- The corresponding index constraint against which we have to
4878 -- check the above aggregate index range.
4881 Compute_Others_Present (N, 1);
4883 for J in 1 .. Aggr_Dimension loop
4884 -- There is no need to emit a check if an others choice is
4885 -- present for this array aggregate dimension since in this
4886 -- case one of N's sub-aggregates has taken its bounds from the
4887 -- context and these bounds must have been checked already. In
4888 -- addition all sub-aggregates corresponding to the same
4889 -- dimension must all have the same bounds (checked in (c) below).
4891 if not Range_Checks_Suppressed (Etype (Index_Constraint))
4892 and then not Others_Present (J)
4894 -- We don't use Checks.Apply_Range_Check here because it emits
4895 -- a spurious check. Namely it checks that the range defined by
4896 -- the aggregate bounds is non empty. But we know this already
4899 Check_Bounds (Aggr_Index_Range, Index_Constraint);
4902 -- Save the low and high bounds of the aggregate index as well as
4903 -- the index type for later use in checks (b) and (c) below.
4905 Aggr_Low (J) := Low_Bound (Aggr_Index_Range);
4906 Aggr_High (J) := High_Bound (Aggr_Index_Range);
4908 Aggr_Index_Typ (J) := Etype (Index_Constraint);
4910 Next_Index (Aggr_Index_Range);
4911 Next_Index (Index_Constraint);
4913 end Index_Compatibility_Check;
4917 -- If an others choice is present check that no aggregate index is
4918 -- outside the bounds of the index constraint.
4920 Others_Check (N, 1);
4924 -- For multidimensional arrays make sure that all subaggregates
4925 -- corresponding to the same dimension have the same bounds.
4927 if Aggr_Dimension > 1 then
4928 Check_Same_Aggr_Bounds (N, 1);
4933 -- Here we test for is packed array aggregate that we can handle at
4934 -- compile time. If so, return with transformation done. Note that we do
4935 -- this even if the aggregate is nested, because once we have done this
4936 -- processing, there is no more nested aggregate!
4938 if Packed_Array_Aggregate_Handled (N) then
4942 -- At this point we try to convert to positional form
4944 if Ekind (Current_Scope) = E_Package
4945 and then Static_Elaboration_Desired (Current_Scope)
4947 Convert_To_Positional (N, Max_Others_Replicate => 100);
4950 Convert_To_Positional (N);
4953 -- if the result is no longer an aggregate (e.g. it may be a string
4954 -- literal, or a temporary which has the needed value), then we are
4955 -- done, since there is no longer a nested aggregate.
4957 if Nkind (N) /= N_Aggregate then
4960 -- We are also done if the result is an analyzed aggregate
4961 -- This case could use more comments ???
4964 and then N /= Original_Node (N)
4969 -- If all aggregate components are compile-time known and the aggregate
4970 -- has been flattened, nothing left to do. The same occurs if the
4971 -- aggregate is used to initialize the components of an statically
4972 -- allocated dispatch table.
4974 if Compile_Time_Known_Aggregate (N)
4975 or else Is_Static_Dispatch_Table_Aggregate (N)
4977 Set_Expansion_Delayed (N, False);
4981 -- Now see if back end processing is possible
4983 if Backend_Processing_Possible (N) then
4985 -- If the aggregate is static but the constraints are not, build
4986 -- a static subtype for the aggregate, so that Gigi can place it
4987 -- in static memory. Perform an unchecked_conversion to the non-
4988 -- static type imposed by the context.
4991 Itype : constant Entity_Id := Etype (N);
4993 Needs_Type : Boolean := False;
4996 Index := First_Index (Itype);
4997 while Present (Index) loop
4998 if not Is_Static_Subtype (Etype (Index)) then
5007 Build_Constrained_Type (Positional => True);
5008 Rewrite (N, Unchecked_Convert_To (Itype, N));
5018 -- Delay expansion for nested aggregates: it will be taken care of
5019 -- when the parent aggregate is expanded.
5021 Parent_Node := Parent (N);
5022 Parent_Kind := Nkind (Parent_Node);
5024 if Parent_Kind = N_Qualified_Expression then
5025 Parent_Node := Parent (Parent_Node);
5026 Parent_Kind := Nkind (Parent_Node);
5029 if Parent_Kind = N_Aggregate
5030 or else Parent_Kind = N_Extension_Aggregate
5031 or else Parent_Kind = N_Component_Association
5032 or else (Parent_Kind = N_Object_Declaration
5033 and then Needs_Finalization (Typ))
5034 or else (Parent_Kind = N_Assignment_Statement
5035 and then Inside_Init_Proc)
5037 if Static_Array_Aggregate (N)
5038 or else Compile_Time_Known_Aggregate (N)
5040 Set_Expansion_Delayed (N, False);
5043 Set_Expansion_Delayed (N);
5050 -- Look if in place aggregate expansion is possible
5052 -- For object declarations we build the aggregate in place, unless
5053 -- the array is bit-packed or the component is controlled.
5055 -- For assignments we do the assignment in place if all the component
5056 -- associations have compile-time known values. For other cases we
5057 -- create a temporary. The analysis for safety of on-line assignment
5058 -- is delicate, i.e. we don't know how to do it fully yet ???
5060 -- For allocators we assign to the designated object in place if the
5061 -- aggregate meets the same conditions as other in-place assignments.
5062 -- In this case the aggregate may not come from source but was created
5063 -- for default initialization, e.g. with Initialize_Scalars.
5065 if Requires_Transient_Scope (Typ) then
5066 Establish_Transient_Scope
5067 (N, Sec_Stack => Has_Controlled_Component (Typ));
5070 if Has_Default_Init_Comps (N) then
5071 Maybe_In_Place_OK := False;
5073 elsif Is_Bit_Packed_Array (Typ)
5074 or else Has_Controlled_Component (Typ)
5076 Maybe_In_Place_OK := False;
5079 Maybe_In_Place_OK :=
5080 (Nkind (Parent (N)) = N_Assignment_Statement
5081 and then Comes_From_Source (N)
5082 and then In_Place_Assign_OK)
5085 (Nkind (Parent (Parent (N))) = N_Allocator
5086 and then In_Place_Assign_OK);
5089 -- If this is an array of tasks, it will be expanded into build-in-place
5090 -- assignments. Build an activation chain for the tasks now.
5092 if Has_Task (Etype (N)) then
5093 Build_Activation_Chain_Entity (N);
5096 if not Has_Default_Init_Comps (N)
5097 and then Comes_From_Source (Parent (N))
5098 and then Nkind (Parent (N)) = N_Object_Declaration
5100 Must_Slide (Etype (Defining_Identifier (Parent (N))), Typ)
5101 and then N = Expression (Parent (N))
5102 and then not Is_Bit_Packed_Array (Typ)
5103 and then not Has_Controlled_Component (Typ)
5104 and then not Has_Address_Clause (Parent (N))
5106 Tmp := Defining_Identifier (Parent (N));
5107 Set_No_Initialization (Parent (N));
5108 Set_Expression (Parent (N), Empty);
5110 -- Set the type of the entity, for use in the analysis of the
5111 -- subsequent indexed assignments. If the nominal type is not
5112 -- constrained, build a subtype from the known bounds of the
5113 -- aggregate. If the declaration has a subtype mark, use it,
5114 -- otherwise use the itype of the aggregate.
5116 if not Is_Constrained (Typ) then
5117 Build_Constrained_Type (Positional => False);
5118 elsif Is_Entity_Name (Object_Definition (Parent (N)))
5119 and then Is_Constrained (Entity (Object_Definition (Parent (N))))
5121 Set_Etype (Tmp, Entity (Object_Definition (Parent (N))));
5123 Set_Size_Known_At_Compile_Time (Typ, False);
5124 Set_Etype (Tmp, Typ);
5127 elsif Maybe_In_Place_OK
5128 and then Nkind (Parent (N)) = N_Qualified_Expression
5129 and then Nkind (Parent (Parent (N))) = N_Allocator
5131 Set_Expansion_Delayed (N);
5134 -- In the remaining cases the aggregate is the RHS of an assignment
5136 elsif Maybe_In_Place_OK
5137 and then Is_Entity_Name (Name (Parent (N)))
5139 Tmp := Entity (Name (Parent (N)));
5141 if Etype (Tmp) /= Etype (N) then
5142 Apply_Length_Check (N, Etype (Tmp));
5144 if Nkind (N) = N_Raise_Constraint_Error then
5146 -- Static error, nothing further to expand
5152 elsif Maybe_In_Place_OK
5153 and then Nkind (Name (Parent (N))) = N_Explicit_Dereference
5154 and then Is_Entity_Name (Prefix (Name (Parent (N))))
5156 Tmp := Name (Parent (N));
5158 if Etype (Tmp) /= Etype (N) then
5159 Apply_Length_Check (N, Etype (Tmp));
5162 elsif Maybe_In_Place_OK
5163 and then Nkind (Name (Parent (N))) = N_Slice
5164 and then Safe_Slice_Assignment (N)
5166 -- Safe_Slice_Assignment rewrites assignment as a loop
5172 -- In place aggregate expansion is not possible
5175 Maybe_In_Place_OK := False;
5176 Tmp := Make_Defining_Identifier (Loc, New_Internal_Name ('A'));
5178 Make_Object_Declaration
5180 Defining_Identifier => Tmp,
5181 Object_Definition => New_Occurrence_Of (Typ, Loc));
5182 Set_No_Initialization (Tmp_Decl, True);
5184 -- If we are within a loop, the temporary will be pushed on the
5185 -- stack at each iteration. If the aggregate is the expression for an
5186 -- allocator, it will be immediately copied to the heap and can
5187 -- be reclaimed at once. We create a transient scope around the
5188 -- aggregate for this purpose.
5190 if Ekind (Current_Scope) = E_Loop
5191 and then Nkind (Parent (Parent (N))) = N_Allocator
5193 Establish_Transient_Scope (N, False);
5196 Insert_Action (N, Tmp_Decl);
5199 -- Construct and insert the aggregate code. We can safely suppress index
5200 -- checks because this code is guaranteed not to raise CE on index
5201 -- checks. However we should *not* suppress all checks.
5207 if Nkind (Tmp) = N_Defining_Identifier then
5208 Target := New_Reference_To (Tmp, Loc);
5212 if Has_Default_Init_Comps (N) then
5214 -- Ada 2005 (AI-287): This case has not been analyzed???
5216 raise Program_Error;
5219 -- Name in assignment is explicit dereference
5221 Target := New_Copy (Tmp);
5225 Build_Array_Aggr_Code (N,
5227 Index => First_Index (Typ),
5229 Scalar_Comp => Is_Scalar_Type (Ctyp));
5232 if Comes_From_Source (Tmp) then
5233 Insert_Actions_After (Parent (N), Aggr_Code);
5236 Insert_Actions (N, Aggr_Code);
5239 -- If the aggregate has been assigned in place, remove the original
5242 if Nkind (Parent (N)) = N_Assignment_Statement
5243 and then Maybe_In_Place_OK
5245 Rewrite (Parent (N), Make_Null_Statement (Loc));
5247 elsif Nkind (Parent (N)) /= N_Object_Declaration
5248 or else Tmp /= Defining_Identifier (Parent (N))
5250 Rewrite (N, New_Occurrence_Of (Tmp, Loc));
5251 Analyze_And_Resolve (N, Typ);
5253 end Expand_Array_Aggregate;
5255 ------------------------
5256 -- Expand_N_Aggregate --
5257 ------------------------
5259 procedure Expand_N_Aggregate (N : Node_Id) is
5261 if Is_Record_Type (Etype (N)) then
5262 Expand_Record_Aggregate (N);
5264 Expand_Array_Aggregate (N);
5267 when RE_Not_Available =>
5269 end Expand_N_Aggregate;
5271 ----------------------------------
5272 -- Expand_N_Extension_Aggregate --
5273 ----------------------------------
5275 -- If the ancestor part is an expression, add a component association for
5276 -- the parent field. If the type of the ancestor part is not the direct
5277 -- parent of the expected type, build recursively the needed ancestors.
5278 -- If the ancestor part is a subtype_mark, replace aggregate with a decla-
5279 -- ration for a temporary of the expected type, followed by individual
5280 -- assignments to the given components.
5282 procedure Expand_N_Extension_Aggregate (N : Node_Id) is
5283 Loc : constant Source_Ptr := Sloc (N);
5284 A : constant Node_Id := Ancestor_Part (N);
5285 Typ : constant Entity_Id := Etype (N);
5288 -- If the ancestor is a subtype mark, an init proc must be called
5289 -- on the resulting object which thus has to be materialized in
5292 if Is_Entity_Name (A) and then Is_Type (Entity (A)) then
5293 Convert_To_Assignments (N, Typ);
5295 -- The extension aggregate is transformed into a record aggregate
5296 -- of the following form (c1 and c2 are inherited components)
5298 -- (Exp with c3 => a, c4 => b)
5299 -- ==> (c1 => Exp.c1, c2 => Exp.c2, c1 => a, c2 => b)
5304 if Tagged_Type_Expansion then
5305 Expand_Record_Aggregate (N,
5308 (Node (First_Elmt (Access_Disp_Table (Typ))), Loc),
5311 -- No tag is needed in the case of a VM
5312 Expand_Record_Aggregate (N,
5318 when RE_Not_Available =>
5320 end Expand_N_Extension_Aggregate;
5322 -----------------------------
5323 -- Expand_Record_Aggregate --
5324 -----------------------------
5326 procedure Expand_Record_Aggregate
5328 Orig_Tag : Node_Id := Empty;
5329 Parent_Expr : Node_Id := Empty)
5331 Loc : constant Source_Ptr := Sloc (N);
5332 Comps : constant List_Id := Component_Associations (N);
5333 Typ : constant Entity_Id := Etype (N);
5334 Base_Typ : constant Entity_Id := Base_Type (Typ);
5336 Static_Components : Boolean := True;
5337 -- Flag to indicate whether all components are compile-time known,
5338 -- and the aggregate can be constructed statically and handled by
5341 function Component_Not_OK_For_Backend return Boolean;
5342 -- Check for presence of component which makes it impossible for the
5343 -- backend to process the aggregate, thus requiring the use of a series
5344 -- of assignment statements. Cases checked for are a nested aggregate
5345 -- needing Late_Expansion, the presence of a tagged component which may
5346 -- need tag adjustment, and a bit unaligned component reference.
5348 -- We also force expansion into assignments if a component is of a
5349 -- mutable type (including a private type with discriminants) because
5350 -- in that case the size of the component to be copied may be smaller
5351 -- than the side of the target, and there is no simple way for gigi
5352 -- to compute the size of the object to be copied.
5354 -- NOTE: This is part of the ongoing work to define precisely the
5355 -- interface between front-end and back-end handling of aggregates.
5356 -- In general it is desirable to pass aggregates as they are to gigi,
5357 -- in order to minimize elaboration code. This is one case where the
5358 -- semantics of Ada complicate the analysis and lead to anomalies in
5359 -- the gcc back-end if the aggregate is not expanded into assignments.
5361 ----------------------------------
5362 -- Component_Not_OK_For_Backend --
5363 ----------------------------------
5365 function Component_Not_OK_For_Backend return Boolean is
5375 while Present (C) loop
5376 if Nkind (Expression (C)) = N_Qualified_Expression then
5377 Expr_Q := Expression (Expression (C));
5379 Expr_Q := Expression (C);
5382 -- Return true if the aggregate has any associations for tagged
5383 -- components that may require tag adjustment.
5385 -- These are cases where the source expression may have a tag that
5386 -- could differ from the component tag (e.g., can occur for type
5387 -- conversions and formal parameters). (Tag adjustment not needed
5388 -- if VM_Target because object tags are implicit in the machine.)
5390 if Is_Tagged_Type (Etype (Expr_Q))
5391 and then (Nkind (Expr_Q) = N_Type_Conversion
5392 or else (Is_Entity_Name (Expr_Q)
5394 Ekind (Entity (Expr_Q)) in Formal_Kind))
5395 and then Tagged_Type_Expansion
5397 Static_Components := False;
5400 elsif Is_Delayed_Aggregate (Expr_Q) then
5401 Static_Components := False;
5404 elsif Possible_Bit_Aligned_Component (Expr_Q) then
5405 Static_Components := False;
5409 if Is_Scalar_Type (Etype (Expr_Q)) then
5410 if not Compile_Time_Known_Value (Expr_Q) then
5411 Static_Components := False;
5414 elsif Nkind (Expr_Q) /= N_Aggregate
5415 or else not Compile_Time_Known_Aggregate (Expr_Q)
5417 Static_Components := False;
5419 if Is_Private_Type (Etype (Expr_Q))
5420 and then Has_Discriminants (Etype (Expr_Q))
5430 end Component_Not_OK_For_Backend;
5432 -- Remaining Expand_Record_Aggregate variables
5434 Tag_Value : Node_Id;
5438 -- Start of processing for Expand_Record_Aggregate
5441 -- If the aggregate is to be assigned to an atomic variable, we
5442 -- have to prevent a piecemeal assignment even if the aggregate
5443 -- is to be expanded. We create a temporary for the aggregate, and
5444 -- assign the temporary instead, so that the back end can generate
5445 -- an atomic move for it.
5448 and then Nkind_In (Parent (N), N_Object_Declaration,
5449 N_Assignment_Statement)
5450 and then Comes_From_Source (Parent (N))
5452 Expand_Atomic_Aggregate (N, Typ);
5455 -- No special management required for aggregates used to initialize
5456 -- statically allocated dispatch tables
5458 elsif Is_Static_Dispatch_Table_Aggregate (N) then
5462 -- Ada 2005 (AI-318-2): We need to convert to assignments if components
5463 -- are build-in-place function calls. This test could be more specific,
5464 -- but doing it for all inherently limited aggregates seems harmless.
5465 -- The assignments will turn into build-in-place function calls (see
5466 -- Make_Build_In_Place_Call_In_Assignment).
5468 if Ada_Version >= Ada_05 and then Is_Inherently_Limited_Type (Typ) then
5469 Convert_To_Assignments (N, Typ);
5471 -- Gigi doesn't handle properly temporaries of variable size
5472 -- so we generate it in the front-end
5474 elsif not Size_Known_At_Compile_Time (Typ) then
5475 Convert_To_Assignments (N, Typ);
5477 -- Temporaries for controlled aggregates need to be attached to a
5478 -- final chain in order to be properly finalized, so it has to
5479 -- be created in the front-end
5481 elsif Is_Controlled (Typ)
5482 or else Has_Controlled_Component (Base_Type (Typ))
5484 Convert_To_Assignments (N, Typ);
5486 -- Ada 2005 (AI-287): In case of default initialized components we
5487 -- convert the aggregate into assignments.
5489 elsif Has_Default_Init_Comps (N) then
5490 Convert_To_Assignments (N, Typ);
5494 elsif Component_Not_OK_For_Backend then
5495 Convert_To_Assignments (N, Typ);
5497 -- If an ancestor is private, some components are not inherited and
5498 -- we cannot expand into a record aggregate
5500 elsif Has_Private_Ancestor (Typ) then
5501 Convert_To_Assignments (N, Typ);
5503 -- ??? The following was done to compile fxacc00.ads in the ACVCs. Gigi
5504 -- is not able to handle the aggregate for Late_Request.
5506 elsif Is_Tagged_Type (Typ) and then Has_Discriminants (Typ) then
5507 Convert_To_Assignments (N, Typ);
5509 -- If the tagged types covers interface types we need to initialize all
5510 -- hidden components containing pointers to secondary dispatch tables.
5512 elsif Is_Tagged_Type (Typ) and then Has_Interfaces (Typ) then
5513 Convert_To_Assignments (N, Typ);
5515 -- If some components are mutable, the size of the aggregate component
5516 -- may be distinct from the default size of the type component, so
5517 -- we need to expand to insure that the back-end copies the proper
5518 -- size of the data.
5520 elsif Has_Mutable_Components (Typ) then
5521 Convert_To_Assignments (N, Typ);
5523 -- If the type involved has any non-bit aligned components, then we are
5524 -- not sure that the back end can handle this case correctly.
5526 elsif Type_May_Have_Bit_Aligned_Components (Typ) then
5527 Convert_To_Assignments (N, Typ);
5529 -- In all other cases, build a proper aggregate handlable by gigi
5532 if Nkind (N) = N_Aggregate then
5534 -- If the aggregate is static and can be handled by the back-end,
5535 -- nothing left to do.
5537 if Static_Components then
5538 Set_Compile_Time_Known_Aggregate (N);
5539 Set_Expansion_Delayed (N, False);
5543 -- If no discriminants, nothing special to do
5545 if not Has_Discriminants (Typ) then
5548 -- Case of discriminants present
5550 elsif Is_Derived_Type (Typ) then
5552 -- For untagged types, non-stored discriminants are replaced
5553 -- with stored discriminants, which are the ones that gigi uses
5554 -- to describe the type and its components.
5556 Generate_Aggregate_For_Derived_Type : declare
5557 Constraints : constant List_Id := New_List;
5558 First_Comp : Node_Id;
5559 Discriminant : Entity_Id;
5561 Num_Disc : Int := 0;
5562 Num_Gird : Int := 0;
5564 procedure Prepend_Stored_Values (T : Entity_Id);
5565 -- Scan the list of stored discriminants of the type, and add
5566 -- their values to the aggregate being built.
5568 ---------------------------
5569 -- Prepend_Stored_Values --
5570 ---------------------------
5572 procedure Prepend_Stored_Values (T : Entity_Id) is
5574 Discriminant := First_Stored_Discriminant (T);
5575 while Present (Discriminant) loop
5577 Make_Component_Association (Loc,
5579 New_List (New_Occurrence_Of (Discriminant, Loc)),
5583 Get_Discriminant_Value (
5586 Discriminant_Constraint (Typ))));
5588 if No (First_Comp) then
5589 Prepend_To (Component_Associations (N), New_Comp);
5591 Insert_After (First_Comp, New_Comp);
5594 First_Comp := New_Comp;
5595 Next_Stored_Discriminant (Discriminant);
5597 end Prepend_Stored_Values;
5599 -- Start of processing for Generate_Aggregate_For_Derived_Type
5602 -- Remove the associations for the discriminant of derived type
5604 First_Comp := First (Component_Associations (N));
5605 while Present (First_Comp) loop
5610 (First (Choices (Comp)))) = E_Discriminant
5613 Num_Disc := Num_Disc + 1;
5617 -- Insert stored discriminant associations in the correct
5618 -- order. If there are more stored discriminants than new
5619 -- discriminants, there is at least one new discriminant that
5620 -- constrains more than one of the stored discriminants. In
5621 -- this case we need to construct a proper subtype of the
5622 -- parent type, in order to supply values to all the
5623 -- components. Otherwise there is one-one correspondence
5624 -- between the constraints and the stored discriminants.
5626 First_Comp := Empty;
5628 Discriminant := First_Stored_Discriminant (Base_Type (Typ));
5629 while Present (Discriminant) loop
5630 Num_Gird := Num_Gird + 1;
5631 Next_Stored_Discriminant (Discriminant);
5634 -- Case of more stored discriminants than new discriminants
5636 if Num_Gird > Num_Disc then
5638 -- Create a proper subtype of the parent type, which is the
5639 -- proper implementation type for the aggregate, and convert
5640 -- it to the intended target type.
5642 Discriminant := First_Stored_Discriminant (Base_Type (Typ));
5643 while Present (Discriminant) loop
5646 Get_Discriminant_Value (
5649 Discriminant_Constraint (Typ)));
5650 Append (New_Comp, Constraints);
5651 Next_Stored_Discriminant (Discriminant);
5655 Make_Subtype_Declaration (Loc,
5656 Defining_Identifier =>
5657 Make_Defining_Identifier (Loc,
5658 New_Internal_Name ('T')),
5659 Subtype_Indication =>
5660 Make_Subtype_Indication (Loc,
5662 New_Occurrence_Of (Etype (Base_Type (Typ)), Loc),
5664 Make_Index_Or_Discriminant_Constraint
5665 (Loc, Constraints)));
5667 Insert_Action (N, Decl);
5668 Prepend_Stored_Values (Base_Type (Typ));
5670 Set_Etype (N, Defining_Identifier (Decl));
5673 Rewrite (N, Unchecked_Convert_To (Typ, N));
5676 -- Case where we do not have fewer new discriminants than
5677 -- stored discriminants, so in this case we can simply use the
5678 -- stored discriminants of the subtype.
5681 Prepend_Stored_Values (Typ);
5683 end Generate_Aggregate_For_Derived_Type;
5686 if Is_Tagged_Type (Typ) then
5688 -- The tagged case, _parent and _tag component must be created
5690 -- Reset null_present unconditionally. tagged records always have
5691 -- at least one field (the tag or the parent)
5693 Set_Null_Record_Present (N, False);
5695 -- When the current aggregate comes from the expansion of an
5696 -- extension aggregate, the parent expr is replaced by an
5697 -- aggregate formed by selected components of this expr
5699 if Present (Parent_Expr)
5700 and then Is_Empty_List (Comps)
5702 Comp := First_Component_Or_Discriminant (Typ);
5703 while Present (Comp) loop
5705 -- Skip all expander-generated components
5708 not Comes_From_Source (Original_Record_Component (Comp))
5714 Make_Selected_Component (Loc,
5716 Unchecked_Convert_To (Typ,
5717 Duplicate_Subexpr (Parent_Expr, True)),
5719 Selector_Name => New_Occurrence_Of (Comp, Loc));
5722 Make_Component_Association (Loc,
5724 New_List (New_Occurrence_Of (Comp, Loc)),
5728 Analyze_And_Resolve (New_Comp, Etype (Comp));
5731 Next_Component_Or_Discriminant (Comp);
5735 -- Compute the value for the Tag now, if the type is a root it
5736 -- will be included in the aggregate right away, otherwise it will
5737 -- be propagated to the parent aggregate
5739 if Present (Orig_Tag) then
5740 Tag_Value := Orig_Tag;
5741 elsif not Tagged_Type_Expansion then
5746 (Node (First_Elmt (Access_Disp_Table (Typ))), Loc);
5749 -- For a derived type, an aggregate for the parent is formed with
5750 -- all the inherited components.
5752 if Is_Derived_Type (Typ) then
5755 First_Comp : Node_Id;
5756 Parent_Comps : List_Id;
5757 Parent_Aggr : Node_Id;
5758 Parent_Name : Node_Id;
5761 -- Remove the inherited component association from the
5762 -- aggregate and store them in the parent aggregate
5764 First_Comp := First (Component_Associations (N));
5765 Parent_Comps := New_List;
5766 while Present (First_Comp)
5767 and then Scope (Original_Record_Component (
5768 Entity (First (Choices (First_Comp))))) /= Base_Typ
5773 Append (Comp, Parent_Comps);
5776 Parent_Aggr := Make_Aggregate (Loc,
5777 Component_Associations => Parent_Comps);
5778 Set_Etype (Parent_Aggr, Etype (Base_Type (Typ)));
5780 -- Find the _parent component
5782 Comp := First_Component (Typ);
5783 while Chars (Comp) /= Name_uParent loop
5784 Comp := Next_Component (Comp);
5787 Parent_Name := New_Occurrence_Of (Comp, Loc);
5789 -- Insert the parent aggregate
5791 Prepend_To (Component_Associations (N),
5792 Make_Component_Association (Loc,
5793 Choices => New_List (Parent_Name),
5794 Expression => Parent_Aggr));
5796 -- Expand recursively the parent propagating the right Tag
5798 Expand_Record_Aggregate (
5799 Parent_Aggr, Tag_Value, Parent_Expr);
5802 -- For a root type, the tag component is added (unless compiling
5803 -- for the VMs, where tags are implicit).
5805 elsif Tagged_Type_Expansion then
5807 Tag_Name : constant Node_Id :=
5809 (First_Tag_Component (Typ), Loc);
5810 Typ_Tag : constant Entity_Id := RTE (RE_Tag);
5811 Conv_Node : constant Node_Id :=
5812 Unchecked_Convert_To (Typ_Tag, Tag_Value);
5815 Set_Etype (Conv_Node, Typ_Tag);
5816 Prepend_To (Component_Associations (N),
5817 Make_Component_Association (Loc,
5818 Choices => New_List (Tag_Name),
5819 Expression => Conv_Node));
5825 end Expand_Record_Aggregate;
5827 ----------------------------
5828 -- Has_Default_Init_Comps --
5829 ----------------------------
5831 function Has_Default_Init_Comps (N : Node_Id) return Boolean is
5832 Comps : constant List_Id := Component_Associations (N);
5836 pragma Assert (Nkind_In (N, N_Aggregate, N_Extension_Aggregate));
5842 if Has_Self_Reference (N) then
5846 -- Check if any direct component has default initialized components
5849 while Present (C) loop
5850 if Box_Present (C) then
5857 -- Recursive call in case of aggregate expression
5860 while Present (C) loop
5861 Expr := Expression (C);
5865 Nkind_In (Expr, N_Aggregate, N_Extension_Aggregate)
5866 and then Has_Default_Init_Comps (Expr)
5875 end Has_Default_Init_Comps;
5877 --------------------------
5878 -- Is_Delayed_Aggregate --
5879 --------------------------
5881 function Is_Delayed_Aggregate (N : Node_Id) return Boolean is
5882 Node : Node_Id := N;
5883 Kind : Node_Kind := Nkind (Node);
5886 if Kind = N_Qualified_Expression then
5887 Node := Expression (Node);
5888 Kind := Nkind (Node);
5891 if Kind /= N_Aggregate and then Kind /= N_Extension_Aggregate then
5894 return Expansion_Delayed (Node);
5896 end Is_Delayed_Aggregate;
5898 ----------------------------------------
5899 -- Is_Static_Dispatch_Table_Aggregate --
5900 ----------------------------------------
5902 function Is_Static_Dispatch_Table_Aggregate (N : Node_Id) return Boolean is
5903 Typ : constant Entity_Id := Base_Type (Etype (N));
5906 return Static_Dispatch_Tables
5907 and then Tagged_Type_Expansion
5908 and then RTU_Loaded (Ada_Tags)
5910 -- Avoid circularity when rebuilding the compiler
5912 and then Cunit_Entity (Get_Source_Unit (N)) /= RTU_Entity (Ada_Tags)
5913 and then (Typ = RTE (RE_Dispatch_Table_Wrapper)
5915 Typ = RTE (RE_Address_Array)
5917 Typ = RTE (RE_Type_Specific_Data)
5919 Typ = RTE (RE_Tag_Table)
5921 (RTE_Available (RE_Interface_Data)
5922 and then Typ = RTE (RE_Interface_Data))
5924 (RTE_Available (RE_Interfaces_Array)
5925 and then Typ = RTE (RE_Interfaces_Array))
5927 (RTE_Available (RE_Interface_Data_Element)
5928 and then Typ = RTE (RE_Interface_Data_Element)));
5929 end Is_Static_Dispatch_Table_Aggregate;
5931 --------------------
5932 -- Late_Expansion --
5933 --------------------
5935 function Late_Expansion
5939 Flist : Node_Id := Empty;
5940 Obj : Entity_Id := Empty) return List_Id
5943 if Is_Record_Type (Etype (N)) then
5944 return Build_Record_Aggr_Code (N, Typ, Target, Flist, Obj);
5946 else pragma Assert (Is_Array_Type (Etype (N)));
5948 Build_Array_Aggr_Code
5950 Ctype => Component_Type (Etype (N)),
5951 Index => First_Index (Typ),
5953 Scalar_Comp => Is_Scalar_Type (Component_Type (Typ)),
5959 ----------------------------------
5960 -- Make_OK_Assignment_Statement --
5961 ----------------------------------
5963 function Make_OK_Assignment_Statement
5966 Expression : Node_Id) return Node_Id
5969 Set_Assignment_OK (Name);
5971 return Make_Assignment_Statement (Sloc, Name, Expression);
5972 end Make_OK_Assignment_Statement;
5974 -----------------------
5975 -- Number_Of_Choices --
5976 -----------------------
5978 function Number_Of_Choices (N : Node_Id) return Nat is
5982 Nb_Choices : Nat := 0;
5985 if Present (Expressions (N)) then
5989 Assoc := First (Component_Associations (N));
5990 while Present (Assoc) loop
5991 Choice := First (Choices (Assoc));
5992 while Present (Choice) loop
5993 if Nkind (Choice) /= N_Others_Choice then
5994 Nb_Choices := Nb_Choices + 1;
6004 end Number_Of_Choices;
6006 ------------------------------------
6007 -- Packed_Array_Aggregate_Handled --
6008 ------------------------------------
6010 -- The current version of this procedure will handle at compile time
6011 -- any array aggregate that meets these conditions:
6013 -- One dimensional, bit packed
6014 -- Underlying packed type is modular type
6015 -- Bounds are within 32-bit Int range
6016 -- All bounds and values are static
6018 function Packed_Array_Aggregate_Handled (N : Node_Id) return Boolean is
6019 Loc : constant Source_Ptr := Sloc (N);
6020 Typ : constant Entity_Id := Etype (N);
6021 Ctyp : constant Entity_Id := Component_Type (Typ);
6023 Not_Handled : exception;
6024 -- Exception raised if this aggregate cannot be handled
6027 -- For now, handle only one dimensional bit packed arrays
6029 if not Is_Bit_Packed_Array (Typ)
6030 or else Number_Dimensions (Typ) > 1
6031 or else not Is_Modular_Integer_Type (Packed_Array_Type (Typ))
6036 if not Is_Scalar_Type (Component_Type (Typ))
6037 and then Has_Non_Standard_Rep (Component_Type (Typ))
6043 Csiz : constant Nat := UI_To_Int (Component_Size (Typ));
6047 -- Bounds of index type
6051 -- Values of bounds if compile time known
6053 function Get_Component_Val (N : Node_Id) return Uint;
6054 -- Given a expression value N of the component type Ctyp, returns a
6055 -- value of Csiz (component size) bits representing this value. If
6056 -- the value is non-static or any other reason exists why the value
6057 -- cannot be returned, then Not_Handled is raised.
6059 -----------------------
6060 -- Get_Component_Val --
6061 -----------------------
6063 function Get_Component_Val (N : Node_Id) return Uint is
6067 -- We have to analyze the expression here before doing any further
6068 -- processing here. The analysis of such expressions is deferred
6069 -- till expansion to prevent some problems of premature analysis.
6071 Analyze_And_Resolve (N, Ctyp);
6073 -- Must have a compile time value. String literals have to be
6074 -- converted into temporaries as well, because they cannot easily
6075 -- be converted into their bit representation.
6077 if not Compile_Time_Known_Value (N)
6078 or else Nkind (N) = N_String_Literal
6083 Val := Expr_Rep_Value (N);
6085 -- Adjust for bias, and strip proper number of bits
6087 if Has_Biased_Representation (Ctyp) then
6088 Val := Val - Expr_Value (Type_Low_Bound (Ctyp));
6091 return Val mod Uint_2 ** Csiz;
6092 end Get_Component_Val;
6094 -- Here we know we have a one dimensional bit packed array
6097 Get_Index_Bounds (First_Index (Typ), Lo, Hi);
6099 -- Cannot do anything if bounds are dynamic
6101 if not Compile_Time_Known_Value (Lo)
6103 not Compile_Time_Known_Value (Hi)
6108 -- Or are silly out of range of int bounds
6110 Lob := Expr_Value (Lo);
6111 Hib := Expr_Value (Hi);
6113 if not UI_Is_In_Int_Range (Lob)
6115 not UI_Is_In_Int_Range (Hib)
6120 -- At this stage we have a suitable aggregate for handling at compile
6121 -- time (the only remaining checks are that the values of expressions
6122 -- in the aggregate are compile time known (check is performed by
6123 -- Get_Component_Val), and that any subtypes or ranges are statically
6126 -- If the aggregate is not fully positional at this stage, then
6127 -- convert it to positional form. Either this will fail, in which
6128 -- case we can do nothing, or it will succeed, in which case we have
6129 -- succeeded in handling the aggregate, or it will stay an aggregate,
6130 -- in which case we have failed to handle this case.
6132 if Present (Component_Associations (N)) then
6133 Convert_To_Positional
6134 (N, Max_Others_Replicate => 64, Handle_Bit_Packed => True);
6135 return Nkind (N) /= N_Aggregate;
6138 -- Otherwise we are all positional, so convert to proper value
6141 Lov : constant Int := UI_To_Int (Lob);
6142 Hiv : constant Int := UI_To_Int (Hib);
6144 Len : constant Nat := Int'Max (0, Hiv - Lov + 1);
6145 -- The length of the array (number of elements)
6147 Aggregate_Val : Uint;
6148 -- Value of aggregate. The value is set in the low order bits of
6149 -- this value. For the little-endian case, the values are stored
6150 -- from low-order to high-order and for the big-endian case the
6151 -- values are stored from high-order to low-order. Note that gigi
6152 -- will take care of the conversions to left justify the value in
6153 -- the big endian case (because of left justified modular type
6154 -- processing), so we do not have to worry about that here.
6157 -- Integer literal for resulting constructed value
6160 -- Shift count from low order for next value
6163 -- Shift increment for loop
6166 -- Next expression from positional parameters of aggregate
6169 -- For little endian, we fill up the low order bits of the target
6170 -- value. For big endian we fill up the high order bits of the
6171 -- target value (which is a left justified modular value).
6173 if Bytes_Big_Endian xor Debug_Flag_8 then
6174 Shift := Csiz * (Len - 1);
6181 -- Loop to set the values
6184 Aggregate_Val := Uint_0;
6186 Expr := First (Expressions (N));
6187 Aggregate_Val := Get_Component_Val (Expr) * Uint_2 ** Shift;
6189 for J in 2 .. Len loop
6190 Shift := Shift + Incr;
6193 Aggregate_Val + Get_Component_Val (Expr) * Uint_2 ** Shift;
6197 -- Now we can rewrite with the proper value
6200 Make_Integer_Literal (Loc,
6201 Intval => Aggregate_Val);
6202 Set_Print_In_Hex (Lit);
6204 -- Construct the expression using this literal. Note that it is
6205 -- important to qualify the literal with its proper modular type
6206 -- since universal integer does not have the required range and
6207 -- also this is a left justified modular type, which is important
6208 -- in the big-endian case.
6211 Unchecked_Convert_To (Typ,
6212 Make_Qualified_Expression (Loc,
6214 New_Occurrence_Of (Packed_Array_Type (Typ), Loc),
6215 Expression => Lit)));
6217 Analyze_And_Resolve (N, Typ);
6225 end Packed_Array_Aggregate_Handled;
6227 ----------------------------
6228 -- Has_Mutable_Components --
6229 ----------------------------
6231 function Has_Mutable_Components (Typ : Entity_Id) return Boolean is
6235 Comp := First_Component (Typ);
6236 while Present (Comp) loop
6237 if Is_Record_Type (Etype (Comp))
6238 and then Has_Discriminants (Etype (Comp))
6239 and then not Is_Constrained (Etype (Comp))
6244 Next_Component (Comp);
6248 end Has_Mutable_Components;
6250 ------------------------------
6251 -- Initialize_Discriminants --
6252 ------------------------------
6254 procedure Initialize_Discriminants (N : Node_Id; Typ : Entity_Id) is
6255 Loc : constant Source_Ptr := Sloc (N);
6256 Bas : constant Entity_Id := Base_Type (Typ);
6257 Par : constant Entity_Id := Etype (Bas);
6258 Decl : constant Node_Id := Parent (Par);
6262 if Is_Tagged_Type (Bas)
6263 and then Is_Derived_Type (Bas)
6264 and then Has_Discriminants (Par)
6265 and then Has_Discriminants (Bas)
6266 and then Number_Discriminants (Bas) /= Number_Discriminants (Par)
6267 and then Nkind (Decl) = N_Full_Type_Declaration
6268 and then Nkind (Type_Definition (Decl)) = N_Record_Definition
6270 (Variant_Part (Component_List (Type_Definition (Decl))))
6271 and then Nkind (N) /= N_Extension_Aggregate
6274 -- Call init proc to set discriminants.
6275 -- There should eventually be a special procedure for this ???
6277 Ref := New_Reference_To (Defining_Identifier (N), Loc);
6278 Insert_Actions_After (N,
6279 Build_Initialization_Call (Sloc (N), Ref, Typ));
6281 end Initialize_Discriminants;
6288 (Obj_Type : Entity_Id;
6289 Typ : Entity_Id) return Boolean
6291 L1, L2, H1, H2 : Node_Id;
6293 -- No sliding if the type of the object is not established yet, if it is
6294 -- an unconstrained type whose actual subtype comes from the aggregate,
6295 -- or if the two types are identical.
6297 if not Is_Array_Type (Obj_Type) then
6300 elsif not Is_Constrained (Obj_Type) then
6303 elsif Typ = Obj_Type then
6307 -- Sliding can only occur along the first dimension
6309 Get_Index_Bounds (First_Index (Typ), L1, H1);
6310 Get_Index_Bounds (First_Index (Obj_Type), L2, H2);
6312 if not Is_Static_Expression (L1)
6313 or else not Is_Static_Expression (L2)
6314 or else not Is_Static_Expression (H1)
6315 or else not Is_Static_Expression (H2)
6319 return Expr_Value (L1) /= Expr_Value (L2)
6320 or else Expr_Value (H1) /= Expr_Value (H2);
6325 ---------------------------
6326 -- Safe_Slice_Assignment --
6327 ---------------------------
6329 function Safe_Slice_Assignment (N : Node_Id) return Boolean is
6330 Loc : constant Source_Ptr := Sloc (Parent (N));
6331 Pref : constant Node_Id := Prefix (Name (Parent (N)));
6332 Range_Node : constant Node_Id := Discrete_Range (Name (Parent (N)));
6340 -- Generate: for J in Range loop Pref (J) := Expr; end loop;
6342 if Comes_From_Source (N)
6343 and then No (Expressions (N))
6344 and then Nkind (First (Choices (First (Component_Associations (N)))))
6348 Expression (First (Component_Associations (N)));
6349 L_J := Make_Defining_Identifier (Loc, New_Internal_Name ('J'));
6352 Make_Iteration_Scheme (Loc,
6353 Loop_Parameter_Specification =>
6354 Make_Loop_Parameter_Specification
6356 Defining_Identifier => L_J,
6357 Discrete_Subtype_Definition => Relocate_Node (Range_Node)));
6360 Make_Assignment_Statement (Loc,
6362 Make_Indexed_Component (Loc,
6363 Prefix => Relocate_Node (Pref),
6364 Expressions => New_List (New_Occurrence_Of (L_J, Loc))),
6365 Expression => Relocate_Node (Expr));
6367 -- Construct the final loop
6370 Make_Implicit_Loop_Statement
6371 (Node => Parent (N),
6372 Identifier => Empty,
6373 Iteration_Scheme => L_Iter,
6374 Statements => New_List (L_Body));
6376 -- Set type of aggregate to be type of lhs in assignment,
6377 -- to suppress redundant length checks.
6379 Set_Etype (N, Etype (Name (Parent (N))));
6381 Rewrite (Parent (N), Stat);
6382 Analyze (Parent (N));
6388 end Safe_Slice_Assignment;
6390 ---------------------
6391 -- Sort_Case_Table --
6392 ---------------------
6394 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type) is
6395 L : constant Int := Case_Table'First;
6396 U : constant Int := Case_Table'Last;
6404 T := Case_Table (K + 1);
6408 and then Expr_Value (Case_Table (J - 1).Choice_Lo) >
6409 Expr_Value (T.Choice_Lo)
6411 Case_Table (J) := Case_Table (J - 1);
6415 Case_Table (J) := T;
6418 end Sort_Case_Table;
6420 ----------------------------
6421 -- Static_Array_Aggregate --
6422 ----------------------------
6424 function Static_Array_Aggregate (N : Node_Id) return Boolean is
6425 Bounds : constant Node_Id := Aggregate_Bounds (N);
6427 Typ : constant Entity_Id := Etype (N);
6428 Comp_Type : constant Entity_Id := Component_Type (Typ);
6435 if Is_Tagged_Type (Typ)
6436 or else Is_Controlled (Typ)
6437 or else Is_Packed (Typ)
6443 and then Nkind (Bounds) = N_Range
6444 and then Nkind (Low_Bound (Bounds)) = N_Integer_Literal
6445 and then Nkind (High_Bound (Bounds)) = N_Integer_Literal
6447 Lo := Low_Bound (Bounds);
6448 Hi := High_Bound (Bounds);
6450 if No (Component_Associations (N)) then
6452 -- Verify that all components are static integers
6454 Expr := First (Expressions (N));
6455 while Present (Expr) loop
6456 if Nkind (Expr) /= N_Integer_Literal then
6466 -- We allow only a single named association, either a static
6467 -- range or an others_clause, with a static expression.
6469 Expr := First (Component_Associations (N));
6471 if Present (Expressions (N)) then
6474 elsif Present (Next (Expr)) then
6477 elsif Present (Next (First (Choices (Expr)))) then
6481 -- The aggregate is static if all components are literals,
6482 -- or else all its components are static aggregates for the
6483 -- component type. We also limit the size of a static aggregate
6484 -- to prevent runaway static expressions.
6486 if Is_Array_Type (Comp_Type)
6487 or else Is_Record_Type (Comp_Type)
6489 if Nkind (Expression (Expr)) /= N_Aggregate
6491 not Compile_Time_Known_Aggregate (Expression (Expr))
6496 elsif Nkind (Expression (Expr)) /= N_Integer_Literal then
6499 elsif not Aggr_Size_OK (N, Typ) then
6503 -- Create a positional aggregate with the right number of
6504 -- copies of the expression.
6506 Agg := Make_Aggregate (Sloc (N), New_List, No_List);
6508 for I in UI_To_Int (Intval (Lo)) .. UI_To_Int (Intval (Hi))
6511 (Expressions (Agg), New_Copy (Expression (Expr)));
6513 -- The copied expression must be analyzed and resolved.
6514 -- Besides setting the type, this ensures that static
6515 -- expressions are appropriately marked as such.
6518 (Last (Expressions (Agg)), Component_Type (Typ));
6521 Set_Aggregate_Bounds (Agg, Bounds);
6522 Set_Etype (Agg, Typ);
6525 Set_Compile_Time_Known_Aggregate (N);
6534 end Static_Array_Aggregate;