1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2008, 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 Freeze; use Freeze;
39 with Itypes; use Itypes;
41 with Namet; use Namet;
42 with Nmake; use Nmake;
43 with Nlists; use Nlists;
45 with Restrict; use Restrict;
46 with Rident; use Rident;
47 with Rtsfind; use Rtsfind;
48 with Ttypes; use Ttypes;
50 with Sem_Aux; use Sem_Aux;
51 with Sem_Ch3; use Sem_Ch3;
52 with Sem_Eval; use Sem_Eval;
53 with Sem_Res; use Sem_Res;
54 with Sem_Util; use Sem_Util;
55 with Sinfo; use Sinfo;
56 with Snames; use Snames;
57 with Stand; use Stand;
58 with Targparm; use Targparm;
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 function Backend_Processing_Possible (N : Node_Id) return Boolean is
510 Typ : constant Entity_Id := Etype (N);
511 -- Typ is the correct constrained array subtype of the aggregate
513 function Component_Check (N : Node_Id; Index : Node_Id) return Boolean;
514 -- This routine checks components of aggregate N, enforcing checks
515 -- 1, 7, 8, and 9. In the multi-dimensional case, these checks are
516 -- performed on subaggregates. The Index value is the current index
517 -- being checked in the multi-dimensional case.
519 ---------------------
520 -- Component_Check --
521 ---------------------
523 function Component_Check (N : Node_Id; Index : Node_Id) return Boolean is
527 -- Checks 1: (no component associations)
529 if Present (Component_Associations (N)) then
533 -- Checks on components
535 -- Recurse to check subaggregates, which may appear in qualified
536 -- expressions. If delayed, the front-end will have to expand.
537 -- If the component is a discriminated record, treat as non-static,
538 -- as the back-end cannot handle this properly.
540 Expr := First (Expressions (N));
541 while Present (Expr) loop
543 -- Checks 8: (no delayed components)
545 if Is_Delayed_Aggregate (Expr) then
549 -- Checks 9: (no discriminated records)
551 if Present (Etype (Expr))
552 and then Is_Record_Type (Etype (Expr))
553 and then Has_Discriminants (Etype (Expr))
558 -- Checks 7. Component must not be bit aligned component
560 if Possible_Bit_Aligned_Component (Expr) then
564 -- Recursion to following indexes for multiple dimension case
566 if Present (Next_Index (Index))
567 and then not Component_Check (Expr, Next_Index (Index))
572 -- All checks for that component finished, on to next
580 -- Start of processing for Backend_Processing_Possible
583 -- Checks 2 (array must not be bit packed)
585 if Is_Bit_Packed_Array (Typ) then
589 -- If component is limited, aggregate must be expanded because each
590 -- component assignment must be built in place.
592 if Is_Inherently_Limited_Type (Component_Type (Typ)) then
596 -- Checks 4 (array must not be multi-dimensional Fortran case)
598 if Convention (Typ) = Convention_Fortran
599 and then Number_Dimensions (Typ) > 1
604 -- Checks 3 (size of array must be known at compile time)
606 if not Size_Known_At_Compile_Time (Typ) then
610 -- Checks on components
612 if not Component_Check (N, First_Index (Typ)) then
616 -- Checks 5 (if the component type is tagged, then we may need to do
617 -- tag adjustments. Perhaps this should be refined to check for any
618 -- component associations that actually need tag adjustment, similar
619 -- to the test in Component_Not_OK_For_Backend for record aggregates
620 -- with tagged components, but not clear whether it's worthwhile ???;
621 -- in the case of the JVM, object tags are handled implicitly)
623 if Is_Tagged_Type (Component_Type (Typ)) and then VM_Target = No_VM then
627 -- Checks 6 (component type must not have bit aligned components)
629 if Type_May_Have_Bit_Aligned_Components (Component_Type (Typ)) then
633 -- Backend processing is possible
635 Set_Size_Known_At_Compile_Time (Etype (N), True);
637 end Backend_Processing_Possible;
639 ---------------------------
640 -- Build_Array_Aggr_Code --
641 ---------------------------
643 -- The code that we generate from a one dimensional aggregate is
645 -- 1. If the sub-aggregate contains discrete choices we
647 -- (a) Sort the discrete choices
649 -- (b) Otherwise for each discrete choice that specifies a range we
650 -- emit a loop. If a range specifies a maximum of three values, or
651 -- we are dealing with an expression we emit a sequence of
652 -- assignments instead of a loop.
654 -- (c) Generate the remaining loops to cover the others choice if any
656 -- 2. If the aggregate contains positional elements we
658 -- (a) translate the positional elements in a series of assignments
660 -- (b) Generate a final loop to cover the others choice if any.
661 -- Note that this final loop has to be a while loop since the case
663 -- L : Integer := Integer'Last;
664 -- H : Integer := Integer'Last;
665 -- A : array (L .. H) := (1, others =>0);
667 -- cannot be handled by a for loop. Thus for the following
669 -- array (L .. H) := (.. positional elements.., others =>E);
671 -- we always generate something like:
673 -- J : Index_Type := Index_Of_Last_Positional_Element;
675 -- J := Index_Base'Succ (J)
679 function Build_Array_Aggr_Code
684 Scalar_Comp : Boolean;
685 Indices : List_Id := No_List;
686 Flist : Node_Id := Empty) return List_Id
688 Loc : constant Source_Ptr := Sloc (N);
689 Index_Base : constant Entity_Id := Base_Type (Etype (Index));
690 Index_Base_L : constant Node_Id := Type_Low_Bound (Index_Base);
691 Index_Base_H : constant Node_Id := Type_High_Bound (Index_Base);
693 function Add (Val : Int; To : Node_Id) return Node_Id;
694 -- Returns an expression where Val is added to expression To, unless
695 -- To+Val is provably out of To's base type range. To must be an
696 -- already analyzed expression.
698 function Empty_Range (L, H : Node_Id) return Boolean;
699 -- Returns True if the range defined by L .. H is certainly empty
701 function Equal (L, H : Node_Id) return Boolean;
702 -- Returns True if L = H for sure
704 function Index_Base_Name return Node_Id;
705 -- Returns a new reference to the index type name
707 function Gen_Assign (Ind : Node_Id; Expr : Node_Id) return List_Id;
708 -- Ind must be a side-effect free expression. If the input aggregate
709 -- N to Build_Loop contains no sub-aggregates, then this function
710 -- returns the assignment statement:
712 -- Into (Indices, Ind) := Expr;
714 -- Otherwise we call Build_Code recursively
716 -- Ada 2005 (AI-287): In case of default initialized component, Expr
717 -- is empty and we generate a call to the corresponding IP subprogram.
719 function Gen_Loop (L, H : Node_Id; Expr : Node_Id) return List_Id;
720 -- Nodes L and H must be side-effect free expressions.
721 -- If the input aggregate N to Build_Loop contains no sub-aggregates,
722 -- This routine returns the for loop statement
724 -- for J in Index_Base'(L) .. Index_Base'(H) loop
725 -- Into (Indices, J) := Expr;
728 -- Otherwise we call Build_Code recursively.
729 -- As an optimization if the loop covers 3 or less scalar elements we
730 -- generate a sequence of assignments.
732 function Gen_While (L, H : Node_Id; Expr : Node_Id) return List_Id;
733 -- Nodes L and H must be side-effect free expressions.
734 -- If the input aggregate N to Build_Loop contains no sub-aggregates,
735 -- This routine returns the while loop statement
737 -- J : Index_Base := L;
739 -- J := Index_Base'Succ (J);
740 -- Into (Indices, J) := Expr;
743 -- Otherwise we call Build_Code recursively
745 function Local_Compile_Time_Known_Value (E : Node_Id) return Boolean;
746 function Local_Expr_Value (E : Node_Id) return Uint;
747 -- These two Local routines are used to replace the corresponding ones
748 -- in sem_eval because while processing the bounds of an aggregate with
749 -- discrete choices whose index type is an enumeration, we build static
750 -- expressions not recognized by Compile_Time_Known_Value as such since
751 -- they have not yet been analyzed and resolved. All the expressions in
752 -- question are things like Index_Base_Name'Val (Const) which we can
753 -- easily recognize as being constant.
759 function Add (Val : Int; To : Node_Id) return Node_Id is
764 U_Val : constant Uint := UI_From_Int (Val);
767 -- Note: do not try to optimize the case of Val = 0, because
768 -- we need to build a new node with the proper Sloc value anyway.
770 -- First test if we can do constant folding
772 if Local_Compile_Time_Known_Value (To) then
773 U_To := Local_Expr_Value (To) + Val;
775 -- Determine if our constant is outside the range of the index.
776 -- If so return an Empty node. This empty node will be caught
777 -- by Empty_Range below.
779 if Compile_Time_Known_Value (Index_Base_L)
780 and then U_To < Expr_Value (Index_Base_L)
784 elsif Compile_Time_Known_Value (Index_Base_H)
785 and then U_To > Expr_Value (Index_Base_H)
790 Expr_Pos := Make_Integer_Literal (Loc, U_To);
791 Set_Is_Static_Expression (Expr_Pos);
793 if not Is_Enumeration_Type (Index_Base) then
796 -- If we are dealing with enumeration return
797 -- Index_Base'Val (Expr_Pos)
801 Make_Attribute_Reference
803 Prefix => Index_Base_Name,
804 Attribute_Name => Name_Val,
805 Expressions => New_List (Expr_Pos));
811 -- If we are here no constant folding possible
813 if not Is_Enumeration_Type (Index_Base) then
816 Left_Opnd => Duplicate_Subexpr (To),
817 Right_Opnd => Make_Integer_Literal (Loc, U_Val));
819 -- If we are dealing with enumeration return
820 -- Index_Base'Val (Index_Base'Pos (To) + Val)
824 Make_Attribute_Reference
826 Prefix => Index_Base_Name,
827 Attribute_Name => Name_Pos,
828 Expressions => New_List (Duplicate_Subexpr (To)));
833 Right_Opnd => Make_Integer_Literal (Loc, U_Val));
836 Make_Attribute_Reference
838 Prefix => Index_Base_Name,
839 Attribute_Name => Name_Val,
840 Expressions => New_List (Expr_Pos));
850 function Empty_Range (L, H : Node_Id) return Boolean is
851 Is_Empty : Boolean := False;
856 -- First check if L or H were already detected as overflowing the
857 -- index base range type by function Add above. If this is so Add
858 -- returns the empty node.
860 if No (L) or else No (H) then
867 -- L > H range is empty
873 -- B_L > H range must be empty
879 -- L > B_H range must be empty
883 High := Index_Base_H;
886 if Local_Compile_Time_Known_Value (Low)
887 and then Local_Compile_Time_Known_Value (High)
890 UI_Gt (Local_Expr_Value (Low), Local_Expr_Value (High));
903 function Equal (L, H : Node_Id) return Boolean is
908 elsif Local_Compile_Time_Known_Value (L)
909 and then Local_Compile_Time_Known_Value (H)
911 return UI_Eq (Local_Expr_Value (L), Local_Expr_Value (H));
921 function Gen_Assign (Ind : Node_Id; Expr : Node_Id) return List_Id is
922 L : constant List_Id := New_List;
926 New_Indices : List_Id;
927 Indexed_Comp : Node_Id;
929 Comp_Type : Entity_Id := Empty;
931 function Add_Loop_Actions (Lis : List_Id) return List_Id;
932 -- Collect insert_actions generated in the construction of a
933 -- loop, and prepend them to the sequence of assignments to
934 -- complete the eventual body of the loop.
936 ----------------------
937 -- Add_Loop_Actions --
938 ----------------------
940 function Add_Loop_Actions (Lis : List_Id) return List_Id is
944 -- Ada 2005 (AI-287): Do nothing else in case of default
945 -- initialized component.
950 elsif Nkind (Parent (Expr)) = N_Component_Association
951 and then Present (Loop_Actions (Parent (Expr)))
953 Append_List (Lis, Loop_Actions (Parent (Expr)));
954 Res := Loop_Actions (Parent (Expr));
955 Set_Loop_Actions (Parent (Expr), No_List);
961 end Add_Loop_Actions;
963 -- Start of processing for Gen_Assign
967 New_Indices := New_List;
969 New_Indices := New_Copy_List_Tree (Indices);
972 Append_To (New_Indices, Ind);
974 if Present (Flist) then
975 F := New_Copy_Tree (Flist);
977 elsif Present (Etype (N)) and then Needs_Finalization (Etype (N)) then
978 if Is_Entity_Name (Into)
979 and then Present (Scope (Entity (Into)))
981 F := Find_Final_List (Scope (Entity (Into)));
983 F := Find_Final_List (Current_Scope);
989 if Present (Next_Index (Index)) then
992 Build_Array_Aggr_Code
995 Index => Next_Index (Index),
997 Scalar_Comp => Scalar_Comp,
998 Indices => New_Indices,
1002 -- If we get here then we are at a bottom-level (sub-)aggregate
1006 (Make_Indexed_Component (Loc,
1007 Prefix => New_Copy_Tree (Into),
1008 Expressions => New_Indices));
1010 Set_Assignment_OK (Indexed_Comp);
1012 -- Ada 2005 (AI-287): In case of default initialized component, Expr
1013 -- is not present (and therefore we also initialize Expr_Q to empty).
1017 elsif Nkind (Expr) = N_Qualified_Expression then
1018 Expr_Q := Expression (Expr);
1023 if Present (Etype (N))
1024 and then Etype (N) /= Any_Composite
1026 Comp_Type := Component_Type (Etype (N));
1027 pragma Assert (Comp_Type = Ctype); -- AI-287
1029 elsif Present (Next (First (New_Indices))) then
1031 -- Ada 2005 (AI-287): Do nothing in case of default initialized
1032 -- component because we have received the component type in
1033 -- the formal parameter Ctype.
1035 -- ??? Some assert pragmas have been added to check if this new
1036 -- formal can be used to replace this code in all cases.
1038 if Present (Expr) then
1040 -- This is a multidimensional array. Recover the component
1041 -- type from the outermost aggregate, because subaggregates
1042 -- do not have an assigned type.
1049 while Present (P) loop
1050 if Nkind (P) = N_Aggregate
1051 and then Present (Etype (P))
1053 Comp_Type := Component_Type (Etype (P));
1061 pragma Assert (Comp_Type = Ctype); -- AI-287
1066 -- Ada 2005 (AI-287): We only analyze the expression in case of non-
1067 -- default initialized components (otherwise Expr_Q is not present).
1070 and then (Nkind (Expr_Q) = N_Aggregate
1071 or else Nkind (Expr_Q) = N_Extension_Aggregate)
1073 -- At this stage the Expression may not have been
1074 -- analyzed yet because the array aggregate code has not
1075 -- been updated to use the Expansion_Delayed flag and
1076 -- avoid analysis altogether to solve the same problem
1077 -- (see Resolve_Aggr_Expr). So let us do the analysis of
1078 -- non-array aggregates now in order to get the value of
1079 -- Expansion_Delayed flag for the inner aggregate ???
1081 if Present (Comp_Type) and then not Is_Array_Type (Comp_Type) then
1082 Analyze_And_Resolve (Expr_Q, Comp_Type);
1085 if Is_Delayed_Aggregate (Expr_Q) then
1087 -- This is either a subaggregate of a multidimentional array,
1088 -- or a component of an array type whose component type is
1089 -- also an array. In the latter case, the expression may have
1090 -- component associations that provide different bounds from
1091 -- those of the component type, and sliding must occur. Instead
1092 -- of decomposing the current aggregate assignment, force the
1093 -- re-analysis of the assignment, so that a temporary will be
1094 -- generated in the usual fashion, and sliding will take place.
1096 if Nkind (Parent (N)) = N_Assignment_Statement
1097 and then Is_Array_Type (Comp_Type)
1098 and then Present (Component_Associations (Expr_Q))
1099 and then Must_Slide (Comp_Type, Etype (Expr_Q))
1101 Set_Expansion_Delayed (Expr_Q, False);
1102 Set_Analyzed (Expr_Q, False);
1108 Expr_Q, Etype (Expr_Q), Indexed_Comp, F));
1113 -- Ada 2005 (AI-287): In case of default initialized component, call
1114 -- the initialization subprogram associated with the component type.
1115 -- If the component type is an access type, add an explicit null
1116 -- assignment, because for the back-end there is an initialization
1117 -- present for the whole aggregate, and no default initialization
1120 -- In addition, if the component type is controlled, we must call
1121 -- its Initialize procedure explicitly, because there is no explicit
1122 -- object creation that will invoke it otherwise.
1125 if Present (Base_Init_Proc (Base_Type (Ctype)))
1126 or else Has_Task (Base_Type (Ctype))
1129 Build_Initialization_Call (Loc,
1130 Id_Ref => Indexed_Comp,
1132 With_Default_Init => True));
1134 elsif Is_Access_Type (Ctype) then
1136 Make_Assignment_Statement (Loc,
1137 Name => Indexed_Comp,
1138 Expression => Make_Null (Loc)));
1141 if Needs_Finalization (Ctype) then
1144 Ref => New_Copy_Tree (Indexed_Comp),
1146 Flist_Ref => Find_Final_List (Current_Scope),
1147 With_Attach => Make_Integer_Literal (Loc, 1)));
1151 -- Now generate the assignment with no associated controlled
1152 -- actions since the target of the assignment may not have been
1153 -- initialized, it is not possible to Finalize it as expected by
1154 -- normal controlled assignment. The rest of the controlled
1155 -- actions are done manually with the proper finalization list
1156 -- coming from the context.
1159 Make_OK_Assignment_Statement (Loc,
1160 Name => Indexed_Comp,
1161 Expression => New_Copy_Tree (Expr));
1163 if Present (Comp_Type) and then Needs_Finalization (Comp_Type) then
1164 Set_No_Ctrl_Actions (A);
1166 -- If this is an aggregate for an array of arrays, each
1167 -- sub-aggregate will be expanded as well, and even with
1168 -- No_Ctrl_Actions the assignments of inner components will
1169 -- require attachment in their assignments to temporaries.
1170 -- These temporaries must be finalized for each subaggregate,
1171 -- to prevent multiple attachments of the same temporary
1172 -- location to same finalization chain (and consequently
1173 -- circular lists). To ensure that finalization takes place
1174 -- for each subaggregate we wrap the assignment in a block.
1176 if Is_Array_Type (Comp_Type)
1177 and then Nkind (Expr) = N_Aggregate
1180 Make_Block_Statement (Loc,
1181 Handled_Statement_Sequence =>
1182 Make_Handled_Sequence_Of_Statements (Loc,
1183 Statements => New_List (A)));
1189 -- Adjust the tag if tagged (because of possible view
1190 -- conversions), unless compiling for the Java VM where
1191 -- tags are implicit.
1193 if Present (Comp_Type)
1194 and then Is_Tagged_Type (Comp_Type)
1195 and then VM_Target = No_VM
1198 Make_OK_Assignment_Statement (Loc,
1200 Make_Selected_Component (Loc,
1201 Prefix => New_Copy_Tree (Indexed_Comp),
1204 (First_Tag_Component (Comp_Type), Loc)),
1207 Unchecked_Convert_To (RTE (RE_Tag),
1209 (Node (First_Elmt (Access_Disp_Table (Comp_Type))),
1215 -- Adjust and attach the component to the proper final list, which
1216 -- can be the controller of the outer record object or the final
1217 -- list associated with the scope.
1219 -- If the component is itself an array of controlled types, whose
1220 -- value is given by a sub-aggregate, then the attach calls have
1221 -- been generated when individual subcomponent are assigned, and
1222 -- must not be done again to prevent malformed finalization chains
1223 -- (see comments above, concerning the creation of a block to hold
1224 -- inner finalization actions).
1226 if Present (Comp_Type)
1227 and then Needs_Finalization (Comp_Type)
1228 and then not Is_Limited_Type (Comp_Type)
1230 (not Is_Array_Type (Comp_Type)
1231 or else not Is_Controlled (Component_Type (Comp_Type))
1232 or else Nkind (Expr) /= N_Aggregate)
1236 Ref => New_Copy_Tree (Indexed_Comp),
1239 With_Attach => Make_Integer_Literal (Loc, 1)));
1243 return Add_Loop_Actions (L);
1250 function Gen_Loop (L, H : Node_Id; Expr : Node_Id) return List_Id is
1254 -- Index_Base'(L) .. Index_Base'(H)
1256 L_Iteration_Scheme : Node_Id;
1257 -- L_J in Index_Base'(L) .. Index_Base'(H)
1260 -- The statements to execute in the loop
1262 S : constant List_Id := New_List;
1263 -- List of statements
1266 -- Copy of expression tree, used for checking purposes
1269 -- If loop bounds define an empty range return the null statement
1271 if Empty_Range (L, H) then
1272 Append_To (S, Make_Null_Statement (Loc));
1274 -- Ada 2005 (AI-287): Nothing else need to be done in case of
1275 -- default initialized component.
1281 -- The expression must be type-checked even though no component
1282 -- of the aggregate will have this value. This is done only for
1283 -- actual components of the array, not for subaggregates. Do
1284 -- the check on a copy, because the expression may be shared
1285 -- among several choices, some of which might be non-null.
1287 if Present (Etype (N))
1288 and then Is_Array_Type (Etype (N))
1289 and then No (Next_Index (Index))
1291 Expander_Mode_Save_And_Set (False);
1292 Tcopy := New_Copy_Tree (Expr);
1293 Set_Parent (Tcopy, N);
1294 Analyze_And_Resolve (Tcopy, Component_Type (Etype (N)));
1295 Expander_Mode_Restore;
1301 -- If loop bounds are the same then generate an assignment
1303 elsif Equal (L, H) then
1304 return Gen_Assign (New_Copy_Tree (L), Expr);
1306 -- If H - L <= 2 then generate a sequence of assignments when we are
1307 -- processing the bottom most aggregate and it contains scalar
1310 elsif No (Next_Index (Index))
1311 and then Scalar_Comp
1312 and then Local_Compile_Time_Known_Value (L)
1313 and then Local_Compile_Time_Known_Value (H)
1314 and then Local_Expr_Value (H) - Local_Expr_Value (L) <= 2
1317 Append_List_To (S, Gen_Assign (New_Copy_Tree (L), Expr));
1318 Append_List_To (S, Gen_Assign (Add (1, To => L), Expr));
1320 if Local_Expr_Value (H) - Local_Expr_Value (L) = 2 then
1321 Append_List_To (S, Gen_Assign (Add (2, To => L), Expr));
1327 -- Otherwise construct the loop, starting with the loop index L_J
1329 L_J := Make_Defining_Identifier (Loc, New_Internal_Name ('J'));
1331 -- Construct "L .. H"
1336 Low_Bound => Make_Qualified_Expression
1338 Subtype_Mark => Index_Base_Name,
1340 High_Bound => Make_Qualified_Expression
1342 Subtype_Mark => Index_Base_Name,
1345 -- Construct "for L_J in Index_Base range L .. H"
1347 L_Iteration_Scheme :=
1348 Make_Iteration_Scheme
1350 Loop_Parameter_Specification =>
1351 Make_Loop_Parameter_Specification
1353 Defining_Identifier => L_J,
1354 Discrete_Subtype_Definition => L_Range));
1356 -- Construct the statements to execute in the loop body
1358 L_Body := Gen_Assign (New_Reference_To (L_J, Loc), Expr);
1360 -- Construct the final loop
1362 Append_To (S, Make_Implicit_Loop_Statement
1364 Identifier => Empty,
1365 Iteration_Scheme => L_Iteration_Scheme,
1366 Statements => L_Body));
1368 -- A small optimization: if the aggregate is initialized with a box
1369 -- and the component type has no initialization procedure, remove the
1370 -- useless empty loop.
1372 if Nkind (First (S)) = N_Loop_Statement
1373 and then Is_Empty_List (Statements (First (S)))
1375 return New_List (Make_Null_Statement (Loc));
1385 -- The code built is
1387 -- W_J : Index_Base := L;
1388 -- while W_J < H loop
1389 -- W_J := Index_Base'Succ (W);
1393 function Gen_While (L, H : Node_Id; Expr : Node_Id) return List_Id is
1397 -- W_J : Base_Type := L;
1399 W_Iteration_Scheme : Node_Id;
1402 W_Index_Succ : Node_Id;
1403 -- Index_Base'Succ (J)
1405 W_Increment : Node_Id;
1406 -- W_J := Index_Base'Succ (W)
1408 W_Body : constant List_Id := New_List;
1409 -- The statements to execute in the loop
1411 S : constant List_Id := New_List;
1412 -- list of statement
1415 -- If loop bounds define an empty range or are equal return null
1417 if Empty_Range (L, H) or else Equal (L, H) then
1418 Append_To (S, Make_Null_Statement (Loc));
1422 -- Build the decl of W_J
1424 W_J := Make_Defining_Identifier (Loc, New_Internal_Name ('J'));
1426 Make_Object_Declaration
1428 Defining_Identifier => W_J,
1429 Object_Definition => Index_Base_Name,
1432 -- Theoretically we should do a New_Copy_Tree (L) here, but we know
1433 -- that in this particular case L is a fresh Expr generated by
1434 -- Add which we are the only ones to use.
1436 Append_To (S, W_Decl);
1438 -- Construct " while W_J < H"
1440 W_Iteration_Scheme :=
1441 Make_Iteration_Scheme
1443 Condition => Make_Op_Lt
1445 Left_Opnd => New_Reference_To (W_J, Loc),
1446 Right_Opnd => New_Copy_Tree (H)));
1448 -- Construct the statements to execute in the loop body
1451 Make_Attribute_Reference
1453 Prefix => Index_Base_Name,
1454 Attribute_Name => Name_Succ,
1455 Expressions => New_List (New_Reference_To (W_J, Loc)));
1458 Make_OK_Assignment_Statement
1460 Name => New_Reference_To (W_J, Loc),
1461 Expression => W_Index_Succ);
1463 Append_To (W_Body, W_Increment);
1464 Append_List_To (W_Body,
1465 Gen_Assign (New_Reference_To (W_J, Loc), Expr));
1467 -- Construct the final loop
1469 Append_To (S, Make_Implicit_Loop_Statement
1471 Identifier => Empty,
1472 Iteration_Scheme => W_Iteration_Scheme,
1473 Statements => W_Body));
1478 ---------------------
1479 -- Index_Base_Name --
1480 ---------------------
1482 function Index_Base_Name return Node_Id is
1484 return New_Reference_To (Index_Base, Sloc (N));
1485 end Index_Base_Name;
1487 ------------------------------------
1488 -- Local_Compile_Time_Known_Value --
1489 ------------------------------------
1491 function Local_Compile_Time_Known_Value (E : Node_Id) return Boolean is
1493 return Compile_Time_Known_Value (E)
1495 (Nkind (E) = N_Attribute_Reference
1496 and then Attribute_Name (E) = Name_Val
1497 and then Compile_Time_Known_Value (First (Expressions (E))));
1498 end Local_Compile_Time_Known_Value;
1500 ----------------------
1501 -- Local_Expr_Value --
1502 ----------------------
1504 function Local_Expr_Value (E : Node_Id) return Uint is
1506 if Compile_Time_Known_Value (E) then
1507 return Expr_Value (E);
1509 return Expr_Value (First (Expressions (E)));
1511 end Local_Expr_Value;
1513 -- Build_Array_Aggr_Code Variables
1520 Others_Expr : Node_Id := Empty;
1521 Others_Box_Present : Boolean := False;
1523 Aggr_L : constant Node_Id := Low_Bound (Aggregate_Bounds (N));
1524 Aggr_H : constant Node_Id := High_Bound (Aggregate_Bounds (N));
1525 -- The aggregate bounds of this specific sub-aggregate. Note that if
1526 -- the code generated by Build_Array_Aggr_Code is executed then these
1527 -- bounds are OK. Otherwise a Constraint_Error would have been raised.
1529 Aggr_Low : constant Node_Id := Duplicate_Subexpr_No_Checks (Aggr_L);
1530 Aggr_High : constant Node_Id := Duplicate_Subexpr_No_Checks (Aggr_H);
1531 -- After Duplicate_Subexpr these are side-effect free
1536 Nb_Choices : Nat := 0;
1537 Table : Case_Table_Type (1 .. Number_Of_Choices (N));
1538 -- Used to sort all the different choice values
1541 -- Number of elements in the positional aggregate
1543 New_Code : constant List_Id := New_List;
1545 -- Start of processing for Build_Array_Aggr_Code
1548 -- First before we start, a special case. if we have a bit packed
1549 -- array represented as a modular type, then clear the value to
1550 -- zero first, to ensure that unused bits are properly cleared.
1555 and then Is_Bit_Packed_Array (Typ)
1556 and then Is_Modular_Integer_Type (Packed_Array_Type (Typ))
1558 Append_To (New_Code,
1559 Make_Assignment_Statement (Loc,
1560 Name => New_Copy_Tree (Into),
1562 Unchecked_Convert_To (Typ,
1563 Make_Integer_Literal (Loc, Uint_0))));
1566 -- If the component type contains tasks, we need to build a Master
1567 -- entity in the current scope, because it will be needed if build-
1568 -- in-place functions are called in the expanded code.
1570 if Nkind (Parent (N)) = N_Object_Declaration
1571 and then Has_Task (Typ)
1573 Build_Master_Entity (Defining_Identifier (Parent (N)));
1576 -- STEP 1: Process component associations
1578 -- For those associations that may generate a loop, initialize
1579 -- Loop_Actions to collect inserted actions that may be crated.
1581 -- Skip this if no component associations
1583 if No (Expressions (N)) then
1585 -- STEP 1 (a): Sort the discrete choices
1587 Assoc := First (Component_Associations (N));
1588 while Present (Assoc) loop
1589 Choice := First (Choices (Assoc));
1590 while Present (Choice) loop
1591 if Nkind (Choice) = N_Others_Choice then
1592 Set_Loop_Actions (Assoc, New_List);
1594 if Box_Present (Assoc) then
1595 Others_Box_Present := True;
1597 Others_Expr := Expression (Assoc);
1602 Get_Index_Bounds (Choice, Low, High);
1605 Set_Loop_Actions (Assoc, New_List);
1608 Nb_Choices := Nb_Choices + 1;
1609 if Box_Present (Assoc) then
1610 Table (Nb_Choices) := (Choice_Lo => Low,
1612 Choice_Node => Empty);
1614 Table (Nb_Choices) := (Choice_Lo => Low,
1616 Choice_Node => Expression (Assoc));
1624 -- If there is more than one set of choices these must be static
1625 -- and we can therefore sort them. Remember that Nb_Choices does not
1626 -- account for an others choice.
1628 if Nb_Choices > 1 then
1629 Sort_Case_Table (Table);
1632 -- STEP 1 (b): take care of the whole set of discrete choices
1634 for J in 1 .. Nb_Choices loop
1635 Low := Table (J).Choice_Lo;
1636 High := Table (J).Choice_Hi;
1637 Expr := Table (J).Choice_Node;
1638 Append_List (Gen_Loop (Low, High, Expr), To => New_Code);
1641 -- STEP 1 (c): generate the remaining loops to cover others choice
1642 -- We don't need to generate loops over empty gaps, but if there is
1643 -- a single empty range we must analyze the expression for semantics
1645 if Present (Others_Expr) or else Others_Box_Present then
1647 First : Boolean := True;
1650 for J in 0 .. Nb_Choices loop
1654 Low := Add (1, To => Table (J).Choice_Hi);
1657 if J = Nb_Choices then
1660 High := Add (-1, To => Table (J + 1).Choice_Lo);
1663 -- If this is an expansion within an init proc, make
1664 -- sure that discriminant references are replaced by
1665 -- the corresponding discriminal.
1667 if Inside_Init_Proc then
1668 if Is_Entity_Name (Low)
1669 and then Ekind (Entity (Low)) = E_Discriminant
1671 Set_Entity (Low, Discriminal (Entity (Low)));
1674 if Is_Entity_Name (High)
1675 and then Ekind (Entity (High)) = E_Discriminant
1677 Set_Entity (High, Discriminal (Entity (High)));
1682 or else not Empty_Range (Low, High)
1686 (Gen_Loop (Low, High, Others_Expr), To => New_Code);
1692 -- STEP 2: Process positional components
1695 -- STEP 2 (a): Generate the assignments for each positional element
1696 -- Note that here we have to use Aggr_L rather than Aggr_Low because
1697 -- Aggr_L is analyzed and Add wants an analyzed expression.
1699 Expr := First (Expressions (N));
1701 while Present (Expr) loop
1702 Nb_Elements := Nb_Elements + 1;
1703 Append_List (Gen_Assign (Add (Nb_Elements, To => Aggr_L), Expr),
1708 -- STEP 2 (b): Generate final loop if an others choice is present
1709 -- Here Nb_Elements gives the offset of the last positional element.
1711 if Present (Component_Associations (N)) then
1712 Assoc := Last (Component_Associations (N));
1714 -- Ada 2005 (AI-287)
1716 if Box_Present (Assoc) then
1717 Append_List (Gen_While (Add (Nb_Elements, To => Aggr_L),
1722 Expr := Expression (Assoc);
1724 Append_List (Gen_While (Add (Nb_Elements, To => Aggr_L),
1733 end Build_Array_Aggr_Code;
1735 ----------------------------
1736 -- Build_Record_Aggr_Code --
1737 ----------------------------
1739 function Build_Record_Aggr_Code
1743 Flist : Node_Id := Empty;
1744 Obj : Entity_Id := Empty;
1745 Is_Limited_Ancestor_Expansion : Boolean := False) return List_Id
1747 Loc : constant Source_Ptr := Sloc (N);
1748 L : constant List_Id := New_List;
1749 N_Typ : constant Entity_Id := Etype (N);
1756 Comp_Type : Entity_Id;
1757 Selector : Entity_Id;
1758 Comp_Expr : Node_Id;
1761 Internal_Final_List : Node_Id := Empty;
1763 -- If this is an internal aggregate, the External_Final_List is an
1764 -- expression for the controller record of the enclosing type.
1766 -- If the current aggregate has several controlled components, this
1767 -- expression will appear in several calls to attach to the finali-
1768 -- zation list, and it must not be shared.
1770 External_Final_List : Node_Id;
1771 Ancestor_Is_Expression : Boolean := False;
1772 Ancestor_Is_Subtype_Mark : Boolean := False;
1774 Init_Typ : Entity_Id := Empty;
1777 Ctrl_Stuff_Done : Boolean := False;
1778 -- True if Gen_Ctrl_Actions_For_Aggr has already been called; calls
1779 -- after the first do nothing.
1781 function Ancestor_Discriminant_Value (Disc : Entity_Id) return Node_Id;
1782 -- Returns the value that the given discriminant of an ancestor type
1783 -- should receive (in the absence of a conflict with the value provided
1784 -- by an ancestor part of an extension aggregate).
1786 procedure Check_Ancestor_Discriminants (Anc_Typ : Entity_Id);
1787 -- Check that each of the discriminant values defined by the ancestor
1788 -- part of an extension aggregate match the corresponding values
1789 -- provided by either an association of the aggregate or by the
1790 -- constraint imposed by a parent type (RM95-4.3.2(8)).
1792 function Compatible_Int_Bounds
1793 (Agg_Bounds : Node_Id;
1794 Typ_Bounds : Node_Id) return Boolean;
1795 -- Return true if Agg_Bounds are equal or within Typ_Bounds. It is
1796 -- assumed that both bounds are integer ranges.
1798 procedure Gen_Ctrl_Actions_For_Aggr;
1799 -- Deal with the various controlled type data structure initializations
1800 -- (but only if it hasn't been done already).
1802 function Get_Constraint_Association (T : Entity_Id) return Node_Id;
1803 -- Returns the first discriminant association in the constraint
1804 -- associated with T, if any, otherwise returns Empty.
1806 function Init_Controller
1811 Init_Pr : Boolean) return List_Id;
1812 -- Returns the list of statements necessary to initialize the internal
1813 -- controller of the (possible) ancestor typ into target and attach it
1814 -- to finalization list F. Init_Pr conditions the call to the init proc
1815 -- since it may already be done due to ancestor initialization.
1817 function Is_Int_Range_Bounds (Bounds : Node_Id) return Boolean;
1818 -- Check whether Bounds is a range node and its lower and higher bounds
1819 -- are integers literals.
1821 ---------------------------------
1822 -- Ancestor_Discriminant_Value --
1823 ---------------------------------
1825 function Ancestor_Discriminant_Value (Disc : Entity_Id) return Node_Id is
1827 Assoc_Elmt : Elmt_Id;
1828 Aggr_Comp : Entity_Id;
1829 Corresp_Disc : Entity_Id;
1830 Current_Typ : Entity_Id := Base_Type (Typ);
1831 Parent_Typ : Entity_Id;
1832 Parent_Disc : Entity_Id;
1833 Save_Assoc : Node_Id := Empty;
1836 -- First check any discriminant associations to see if any of them
1837 -- provide a value for the discriminant.
1839 if Present (Discriminant_Specifications (Parent (Current_Typ))) then
1840 Assoc := First (Component_Associations (N));
1841 while Present (Assoc) loop
1842 Aggr_Comp := Entity (First (Choices (Assoc)));
1844 if Ekind (Aggr_Comp) = E_Discriminant then
1845 Save_Assoc := Expression (Assoc);
1847 Corresp_Disc := Corresponding_Discriminant (Aggr_Comp);
1848 while Present (Corresp_Disc) loop
1850 -- If found a corresponding discriminant then return the
1851 -- value given in the aggregate. (Note: this is not
1852 -- correct in the presence of side effects. ???)
1854 if Disc = Corresp_Disc then
1855 return Duplicate_Subexpr (Expression (Assoc));
1859 Corresponding_Discriminant (Corresp_Disc);
1867 -- No match found in aggregate, so chain up parent types to find
1868 -- a constraint that defines the value of the discriminant.
1870 Parent_Typ := Etype (Current_Typ);
1871 while Current_Typ /= Parent_Typ loop
1872 if Has_Discriminants (Parent_Typ) then
1873 Parent_Disc := First_Discriminant (Parent_Typ);
1875 -- We either get the association from the subtype indication
1876 -- of the type definition itself, or from the discriminant
1877 -- constraint associated with the type entity (which is
1878 -- preferable, but it's not always present ???)
1880 if Is_Empty_Elmt_List (
1881 Discriminant_Constraint (Current_Typ))
1883 Assoc := Get_Constraint_Association (Current_Typ);
1884 Assoc_Elmt := No_Elmt;
1887 First_Elmt (Discriminant_Constraint (Current_Typ));
1888 Assoc := Node (Assoc_Elmt);
1891 -- Traverse the discriminants of the parent type looking
1892 -- for one that corresponds.
1894 while Present (Parent_Disc) and then Present (Assoc) loop
1895 Corresp_Disc := Parent_Disc;
1896 while Present (Corresp_Disc)
1897 and then Disc /= Corresp_Disc
1900 Corresponding_Discriminant (Corresp_Disc);
1903 if Disc = Corresp_Disc then
1904 if Nkind (Assoc) = N_Discriminant_Association then
1905 Assoc := Expression (Assoc);
1908 -- If the located association directly denotes a
1909 -- discriminant, then use the value of a saved
1910 -- association of the aggregate. This is a kludge to
1911 -- handle certain cases involving multiple discriminants
1912 -- mapped to a single discriminant of a descendant. It's
1913 -- not clear how to locate the appropriate discriminant
1914 -- value for such cases. ???
1916 if Is_Entity_Name (Assoc)
1917 and then Ekind (Entity (Assoc)) = E_Discriminant
1919 Assoc := Save_Assoc;
1922 return Duplicate_Subexpr (Assoc);
1925 Next_Discriminant (Parent_Disc);
1927 if No (Assoc_Elmt) then
1930 Next_Elmt (Assoc_Elmt);
1931 if Present (Assoc_Elmt) then
1932 Assoc := Node (Assoc_Elmt);
1940 Current_Typ := Parent_Typ;
1941 Parent_Typ := Etype (Current_Typ);
1944 -- In some cases there's no ancestor value to locate (such as
1945 -- when an ancestor part given by an expression defines the
1946 -- discriminant value).
1949 end Ancestor_Discriminant_Value;
1951 ----------------------------------
1952 -- Check_Ancestor_Discriminants --
1953 ----------------------------------
1955 procedure Check_Ancestor_Discriminants (Anc_Typ : Entity_Id) is
1957 Disc_Value : Node_Id;
1961 Discr := First_Discriminant (Base_Type (Anc_Typ));
1962 while Present (Discr) loop
1963 Disc_Value := Ancestor_Discriminant_Value (Discr);
1965 if Present (Disc_Value) then
1966 Cond := Make_Op_Ne (Loc,
1968 Make_Selected_Component (Loc,
1969 Prefix => New_Copy_Tree (Target),
1970 Selector_Name => New_Occurrence_Of (Discr, Loc)),
1971 Right_Opnd => Disc_Value);
1974 Make_Raise_Constraint_Error (Loc,
1976 Reason => CE_Discriminant_Check_Failed));
1979 Next_Discriminant (Discr);
1981 end Check_Ancestor_Discriminants;
1983 ---------------------------
1984 -- Compatible_Int_Bounds --
1985 ---------------------------
1987 function Compatible_Int_Bounds
1988 (Agg_Bounds : Node_Id;
1989 Typ_Bounds : Node_Id) return Boolean
1991 Agg_Lo : constant Uint := Intval (Low_Bound (Agg_Bounds));
1992 Agg_Hi : constant Uint := Intval (High_Bound (Agg_Bounds));
1993 Typ_Lo : constant Uint := Intval (Low_Bound (Typ_Bounds));
1994 Typ_Hi : constant Uint := Intval (High_Bound (Typ_Bounds));
1996 return Typ_Lo <= Agg_Lo and then Agg_Hi <= Typ_Hi;
1997 end Compatible_Int_Bounds;
1999 --------------------------------
2000 -- Get_Constraint_Association --
2001 --------------------------------
2003 function Get_Constraint_Association (T : Entity_Id) return Node_Id is
2004 Typ_Def : constant Node_Id := Type_Definition (Parent (T));
2005 Indic : constant Node_Id := Subtype_Indication (Typ_Def);
2008 -- ??? Also need to cover case of a type mark denoting a subtype
2011 if Nkind (Indic) = N_Subtype_Indication
2012 and then Present (Constraint (Indic))
2014 return First (Constraints (Constraint (Indic)));
2018 end Get_Constraint_Association;
2020 ---------------------
2021 -- Init_Controller --
2022 ---------------------
2024 function Init_Controller
2029 Init_Pr : Boolean) return List_Id
2031 L : constant List_Id := New_List;
2034 Target_Type : Entity_Id;
2038 -- init-proc (target._controller);
2039 -- initialize (target._controller);
2040 -- Attach_to_Final_List (target._controller, F);
2043 Make_Selected_Component (Loc,
2044 Prefix => Convert_To (Typ, New_Copy_Tree (Target)),
2045 Selector_Name => Make_Identifier (Loc, Name_uController));
2046 Set_Assignment_OK (Ref);
2048 -- Ada 2005 (AI-287): Give support to aggregates of limited types.
2049 -- If the type is intrinsically limited the controller is limited as
2050 -- well. If it is tagged and limited then so is the controller.
2051 -- Otherwise an untagged type may have limited components without its
2052 -- full view being limited, so the controller is not limited.
2054 if Nkind (Target) = N_Identifier then
2055 Target_Type := Etype (Target);
2057 elsif Nkind (Target) = N_Selected_Component then
2058 Target_Type := Etype (Selector_Name (Target));
2060 elsif Nkind (Target) = N_Unchecked_Type_Conversion then
2061 Target_Type := Etype (Target);
2063 elsif Nkind (Target) = N_Unchecked_Expression
2064 and then Nkind (Expression (Target)) = N_Indexed_Component
2066 Target_Type := Etype (Prefix (Expression (Target)));
2069 Target_Type := Etype (Target);
2072 -- If the target has not been analyzed yet, as will happen with
2073 -- delayed expansion, use the given type (either the aggregate type
2074 -- or an ancestor) to determine limitedness.
2076 if No (Target_Type) then
2080 if (Is_Tagged_Type (Target_Type))
2081 and then Is_Limited_Type (Target_Type)
2083 RC := RE_Limited_Record_Controller;
2085 elsif Is_Inherently_Limited_Type (Target_Type) then
2086 RC := RE_Limited_Record_Controller;
2089 RC := RE_Record_Controller;
2094 Build_Initialization_Call (Loc,
2097 In_Init_Proc => Within_Init_Proc));
2101 Make_Procedure_Call_Statement (Loc,
2104 Find_Prim_Op (RTE (RC), Name_Initialize), Loc),
2105 Parameter_Associations =>
2106 New_List (New_Copy_Tree (Ref))));
2110 Obj_Ref => New_Copy_Tree (Ref),
2112 With_Attach => Attach));
2115 end Init_Controller;
2117 -------------------------
2118 -- Is_Int_Range_Bounds --
2119 -------------------------
2121 function Is_Int_Range_Bounds (Bounds : Node_Id) return Boolean is
2123 return Nkind (Bounds) = N_Range
2124 and then Nkind (Low_Bound (Bounds)) = N_Integer_Literal
2125 and then Nkind (High_Bound (Bounds)) = N_Integer_Literal;
2126 end Is_Int_Range_Bounds;
2128 -------------------------------
2129 -- Gen_Ctrl_Actions_For_Aggr --
2130 -------------------------------
2132 procedure Gen_Ctrl_Actions_For_Aggr is
2133 Alloc : Node_Id := Empty;
2136 -- Do the work only the first time this is called
2138 if Ctrl_Stuff_Done then
2142 Ctrl_Stuff_Done := True;
2145 and then Finalize_Storage_Only (Typ)
2147 (Is_Library_Level_Entity (Obj)
2148 or else Entity (Constant_Value (RTE (RE_Garbage_Collected))) =
2151 -- why not Is_True (Expr_Value (RTE (RE_Garbaage_Collected) ???
2153 Attach := Make_Integer_Literal (Loc, 0);
2155 elsif Nkind (Parent (N)) = N_Qualified_Expression
2156 and then Nkind (Parent (Parent (N))) = N_Allocator
2158 Alloc := Parent (Parent (N));
2159 Attach := Make_Integer_Literal (Loc, 2);
2162 Attach := Make_Integer_Literal (Loc, 1);
2165 -- Determine the external finalization list. It is either the
2166 -- finalization list of the outer-scope or the one coming from
2167 -- an outer aggregate. When the target is not a temporary, the
2168 -- proper scope is the scope of the target rather than the
2169 -- potentially transient current scope.
2171 if Needs_Finalization (Typ) then
2173 -- The current aggregate belongs to an allocator which creates
2174 -- an object through an anonymous access type or acts as the root
2175 -- of a coextension chain.
2179 (Is_Coextension_Root (Alloc)
2180 or else Ekind (Etype (Alloc)) = E_Anonymous_Access_Type)
2182 if No (Associated_Final_Chain (Etype (Alloc))) then
2183 Build_Final_List (Alloc, Etype (Alloc));
2186 External_Final_List :=
2187 Make_Selected_Component (Loc,
2190 Associated_Final_Chain (Etype (Alloc)), Loc),
2192 Make_Identifier (Loc, Name_F));
2194 elsif Present (Flist) then
2195 External_Final_List := New_Copy_Tree (Flist);
2197 elsif Is_Entity_Name (Target)
2198 and then Present (Scope (Entity (Target)))
2200 External_Final_List :=
2201 Find_Final_List (Scope (Entity (Target)));
2204 External_Final_List := Find_Final_List (Current_Scope);
2207 External_Final_List := Empty;
2210 -- Initialize and attach the outer object in the is_controlled case
2212 if Is_Controlled (Typ) then
2213 if Ancestor_Is_Subtype_Mark then
2214 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
2215 Set_Assignment_OK (Ref);
2217 Make_Procedure_Call_Statement (Loc,
2220 (Find_Prim_Op (Init_Typ, Name_Initialize), Loc),
2221 Parameter_Associations => New_List (New_Copy_Tree (Ref))));
2224 if not Has_Controlled_Component (Typ) then
2225 Ref := New_Copy_Tree (Target);
2226 Set_Assignment_OK (Ref);
2228 -- This is an aggregate of a coextension. Do not produce a
2229 -- finalization call, but rather attach the reference of the
2230 -- aggregate to its coextension chain.
2233 and then Is_Dynamic_Coextension (Alloc)
2235 if No (Coextensions (Alloc)) then
2236 Set_Coextensions (Alloc, New_Elmt_List);
2239 Append_Elmt (Ref, Coextensions (Alloc));
2244 Flist_Ref => New_Copy_Tree (External_Final_List),
2245 With_Attach => Attach));
2250 -- In the Has_Controlled component case, all the intermediate
2251 -- controllers must be initialized.
2253 if Has_Controlled_Component (Typ)
2254 and not Is_Limited_Ancestor_Expansion
2257 Inner_Typ : Entity_Id;
2258 Outer_Typ : Entity_Id;
2262 -- Find outer type with a controller
2264 Outer_Typ := Base_Type (Typ);
2265 while Outer_Typ /= Init_Typ
2266 and then not Has_New_Controlled_Component (Outer_Typ)
2268 Outer_Typ := Etype (Outer_Typ);
2271 -- Attach it to the outer record controller to the external
2274 if Outer_Typ = Init_Typ then
2279 F => External_Final_List,
2284 Inner_Typ := Init_Typ;
2291 F => External_Final_List,
2295 Inner_Typ := Etype (Outer_Typ);
2297 not Is_Tagged_Type (Typ) or else Inner_Typ = Outer_Typ;
2300 -- The outer object has to be attached as well
2302 if Is_Controlled (Typ) then
2303 Ref := New_Copy_Tree (Target);
2304 Set_Assignment_OK (Ref);
2308 Flist_Ref => New_Copy_Tree (External_Final_List),
2309 With_Attach => New_Copy_Tree (Attach)));
2312 -- Initialize the internal controllers for tagged types with
2313 -- more than one controller.
2315 while not At_Root and then Inner_Typ /= Init_Typ loop
2316 if Has_New_Controlled_Component (Inner_Typ) then
2318 Make_Selected_Component (Loc,
2320 Convert_To (Outer_Typ, New_Copy_Tree (Target)),
2322 Make_Identifier (Loc, Name_uController));
2324 Make_Selected_Component (Loc,
2326 Selector_Name => Make_Identifier (Loc, Name_F));
2333 Attach => Make_Integer_Literal (Loc, 1),
2335 Outer_Typ := Inner_Typ;
2340 At_Root := Inner_Typ = Etype (Inner_Typ);
2341 Inner_Typ := Etype (Inner_Typ);
2344 -- If not done yet attach the controller of the ancestor part
2346 if Outer_Typ /= Init_Typ
2347 and then Inner_Typ = Init_Typ
2348 and then Has_Controlled_Component (Init_Typ)
2351 Make_Selected_Component (Loc,
2352 Prefix => Convert_To (Outer_Typ, New_Copy_Tree (Target)),
2354 Make_Identifier (Loc, Name_uController));
2356 Make_Selected_Component (Loc,
2358 Selector_Name => Make_Identifier (Loc, Name_F));
2360 Attach := Make_Integer_Literal (Loc, 1);
2369 -- Note: Init_Pr is False because the ancestor part has
2370 -- already been initialized either way (by default, if
2371 -- given by a type name, otherwise from the expression).
2376 end Gen_Ctrl_Actions_For_Aggr;
2378 function Replace_Type (Expr : Node_Id) return Traverse_Result;
2379 -- If the aggregate contains a self-reference, traverse each expression
2380 -- to replace a possible self-reference with a reference to the proper
2381 -- component of the target of the assignment.
2387 function Replace_Type (Expr : Node_Id) return Traverse_Result is
2389 -- Note regarding the Root_Type test below: Aggregate components for
2390 -- self-referential types include attribute references to the current
2391 -- instance, of the form: Typ'access, etc.. These references are
2392 -- rewritten as references to the target of the aggregate: the
2393 -- left-hand side of an assignment, the entity in a declaration,
2394 -- or a temporary. Without this test, we would improperly extended
2395 -- this rewriting to attribute references whose prefix was not the
2396 -- type of the aggregate.
2398 if Nkind (Expr) = N_Attribute_Reference
2399 and then Is_Entity_Name (Prefix (Expr))
2400 and then Is_Type (Entity (Prefix (Expr)))
2401 and then Root_Type (Etype (N)) = Root_Type (Entity (Prefix (Expr)))
2403 if Is_Entity_Name (Lhs) then
2404 Rewrite (Prefix (Expr),
2405 New_Occurrence_Of (Entity (Lhs), Loc));
2407 elsif Nkind (Lhs) = N_Selected_Component then
2409 Make_Attribute_Reference (Loc,
2410 Attribute_Name => Name_Unrestricted_Access,
2411 Prefix => New_Copy_Tree (Prefix (Lhs))));
2412 Set_Analyzed (Parent (Expr), False);
2416 Make_Attribute_Reference (Loc,
2417 Attribute_Name => Name_Unrestricted_Access,
2418 Prefix => New_Copy_Tree (Lhs)));
2419 Set_Analyzed (Parent (Expr), False);
2426 procedure Replace_Self_Reference is
2427 new Traverse_Proc (Replace_Type);
2429 -- Start of processing for Build_Record_Aggr_Code
2432 if Has_Self_Reference (N) then
2433 Replace_Self_Reference (N);
2436 -- If the target of the aggregate is class-wide, we must convert it
2437 -- to the actual type of the aggregate, so that the proper components
2438 -- are visible. We know already that the types are compatible.
2440 -- There should also be a comment here explaining why the conversion
2441 -- is needed in the case of interfaces.???
2443 if Present (Etype (Lhs))
2444 and then (Is_Interface (Etype (Lhs))
2445 or else 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);
2523 if Has_Default_Init_Comps (N)
2524 or else Has_Task (Base_Type (Init_Typ))
2527 Build_Initialization_Call (Loc,
2530 In_Init_Proc => Within_Init_Proc,
2531 With_Default_Init => True));
2534 Build_Initialization_Call (Loc,
2537 In_Init_Proc => Within_Init_Proc));
2540 if Is_Constrained (Entity (A))
2541 and then Has_Discriminants (Entity (A))
2543 Check_Ancestor_Discriminants (Entity (A));
2546 -- Ada 2005 (AI-287): If the ancestor part is an aggregate of
2547 -- limited type, a recursive call expands the ancestor. Note that
2548 -- in the limited case, the ancestor part must be either a
2549 -- function call (possibly qualified, or wrapped in an unchecked
2550 -- conversion) or aggregate (definitely qualified).
2552 elsif Is_Limited_Type (Etype (A))
2553 and then Nkind (Unqualify (A)) /= N_Function_Call -- aggregate?
2555 (Nkind (Unqualify (A)) /= N_Unchecked_Type_Conversion
2557 Nkind (Expression (Unqualify (A))) /= N_Function_Call)
2559 Ancestor_Is_Expression := True;
2561 -- Set up finalization data for enclosing record, because
2562 -- controlled subcomponents of the ancestor part will be
2565 Gen_Ctrl_Actions_For_Aggr;
2568 Build_Record_Aggr_Code (
2570 Typ => Etype (Unqualify (A)),
2574 Is_Limited_Ancestor_Expansion => True));
2576 -- If the ancestor part is an expression "E", we generate
2580 -- In Ada 2005, this includes the case of a (possibly qualified)
2581 -- limited function call. The assignment will turn into a
2582 -- build-in-place function call (for further details, see
2583 -- Make_Build_In_Place_Call_In_Assignment).
2586 Ancestor_Is_Expression := True;
2587 Init_Typ := Etype (A);
2589 -- If the ancestor part is an aggregate, force its full
2590 -- expansion, which was delayed.
2592 if Nkind (Unqualify (A)) = N_Aggregate
2593 or else Nkind (Unqualify (A)) = N_Extension_Aggregate
2595 Set_Analyzed (A, False);
2596 Set_Analyzed (Expression (A), False);
2599 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
2600 Set_Assignment_OK (Ref);
2602 -- Make the assignment without usual controlled actions since
2603 -- we only want the post adjust but not the pre finalize here
2604 -- Add manual adjust when necessary.
2606 Assign := New_List (
2607 Make_OK_Assignment_Statement (Loc,
2610 Set_No_Ctrl_Actions (First (Assign));
2612 -- Assign the tag now to make sure that the dispatching call in
2613 -- the subsequent deep_adjust works properly (unless VM_Target,
2614 -- where tags are implicit).
2616 if VM_Target = No_VM then
2618 Make_OK_Assignment_Statement (Loc,
2620 Make_Selected_Component (Loc,
2621 Prefix => New_Copy_Tree (Target),
2624 (First_Tag_Component (Base_Type (Typ)), Loc)),
2627 Unchecked_Convert_To (RTE (RE_Tag),
2630 (Access_Disp_Table (Base_Type (Typ)))),
2633 Set_Assignment_OK (Name (Instr));
2634 Append_To (Assign, Instr);
2636 -- Ada 2005 (AI-251): If tagged type has progenitors we must
2637 -- also initialize tags of the secondary dispatch tables.
2639 if Has_Interfaces (Base_Type (Typ)) then
2641 (Typ => Base_Type (Typ),
2643 Stmts_List => Assign);
2647 -- Call Adjust manually
2649 if Needs_Finalization (Etype (A))
2650 and then not Is_Limited_Type (Etype (A))
2652 Append_List_To (Assign,
2654 Ref => New_Copy_Tree (Ref),
2656 Flist_Ref => New_Reference_To (
2657 RTE (RE_Global_Final_List), Loc),
2658 With_Attach => Make_Integer_Literal (Loc, 0)));
2662 Make_Unsuppress_Block (Loc, Name_Discriminant_Check, Assign));
2664 if Has_Discriminants (Init_Typ) then
2665 Check_Ancestor_Discriminants (Init_Typ);
2670 -- Normal case (not an extension aggregate)
2673 -- Generate the discriminant expressions, component by component.
2674 -- If the base type is an unchecked union, the discriminants are
2675 -- unknown to the back-end and absent from a value of the type, so
2676 -- assignments for them are not emitted.
2678 if Has_Discriminants (Typ)
2679 and then not Is_Unchecked_Union (Base_Type (Typ))
2681 -- If the type is derived, and constrains discriminants of the
2682 -- parent type, these discriminants are not components of the
2683 -- aggregate, and must be initialized explicitly. They are not
2684 -- visible components of the object, but can become visible with
2685 -- a view conversion to the ancestor.
2689 Parent_Type : Entity_Id;
2691 Discr_Val : Elmt_Id;
2694 Btype := Base_Type (Typ);
2695 while Is_Derived_Type (Btype)
2696 and then Present (Stored_Constraint (Btype))
2698 Parent_Type := Etype (Btype);
2700 Disc := First_Discriminant (Parent_Type);
2702 First_Elmt (Stored_Constraint (Base_Type (Typ)));
2703 while Present (Discr_Val) loop
2705 -- Only those discriminants of the parent that are not
2706 -- renamed by discriminants of the derived type need to
2707 -- be added explicitly.
2709 if not Is_Entity_Name (Node (Discr_Val))
2711 Ekind (Entity (Node (Discr_Val))) /= E_Discriminant
2714 Make_Selected_Component (Loc,
2715 Prefix => New_Copy_Tree (Target),
2716 Selector_Name => New_Occurrence_Of (Disc, Loc));
2719 Make_OK_Assignment_Statement (Loc,
2721 Expression => New_Copy_Tree (Node (Discr_Val)));
2723 Set_No_Ctrl_Actions (Instr);
2724 Append_To (L, Instr);
2727 Next_Discriminant (Disc);
2728 Next_Elmt (Discr_Val);
2731 Btype := Base_Type (Parent_Type);
2735 -- Generate discriminant init values for the visible discriminants
2738 Discriminant : Entity_Id;
2739 Discriminant_Value : Node_Id;
2742 Discriminant := First_Stored_Discriminant (Typ);
2743 while Present (Discriminant) loop
2745 Make_Selected_Component (Loc,
2746 Prefix => New_Copy_Tree (Target),
2747 Selector_Name => New_Occurrence_Of (Discriminant, Loc));
2749 Discriminant_Value :=
2750 Get_Discriminant_Value (
2753 Discriminant_Constraint (N_Typ));
2756 Make_OK_Assignment_Statement (Loc,
2758 Expression => New_Copy_Tree (Discriminant_Value));
2760 Set_No_Ctrl_Actions (Instr);
2761 Append_To (L, Instr);
2763 Next_Stored_Discriminant (Discriminant);
2769 -- Generate the assignments, component by component
2771 -- tmp.comp1 := Expr1_From_Aggr;
2772 -- tmp.comp2 := Expr2_From_Aggr;
2775 Comp := First (Component_Associations (N));
2776 while Present (Comp) loop
2777 Selector := Entity (First (Choices (Comp)));
2779 -- Ada 2005 (AI-287): For each default-initialized component generate
2780 -- a call to the corresponding IP subprogram if available.
2782 if Box_Present (Comp)
2783 and then Has_Non_Null_Base_Init_Proc (Etype (Selector))
2785 if Ekind (Selector) /= E_Discriminant then
2786 Gen_Ctrl_Actions_For_Aggr;
2789 -- Ada 2005 (AI-287): If the component type has tasks then
2790 -- generate the activation chain and master entities (except
2791 -- in case of an allocator because in that case these entities
2792 -- are generated by Build_Task_Allocate_Block_With_Init_Stmts).
2795 Ctype : constant Entity_Id := Etype (Selector);
2796 Inside_Allocator : Boolean := False;
2797 P : Node_Id := Parent (N);
2800 if Is_Task_Type (Ctype) or else Has_Task (Ctype) then
2801 while Present (P) loop
2802 if Nkind (P) = N_Allocator then
2803 Inside_Allocator := True;
2810 if not Inside_Init_Proc and not Inside_Allocator then
2811 Build_Activation_Chain_Entity (N);
2817 Build_Initialization_Call (Loc,
2818 Id_Ref => Make_Selected_Component (Loc,
2819 Prefix => New_Copy_Tree (Target),
2820 Selector_Name => New_Occurrence_Of (Selector,
2822 Typ => Etype (Selector),
2824 With_Default_Init => True));
2829 -- Prepare for component assignment
2831 if Ekind (Selector) /= E_Discriminant
2832 or else Nkind (N) = N_Extension_Aggregate
2834 -- All the discriminants have now been assigned
2836 -- This is now a good moment to initialize and attach all the
2837 -- controllers. Their position may depend on the discriminants.
2839 if Ekind (Selector) /= E_Discriminant then
2840 Gen_Ctrl_Actions_For_Aggr;
2843 Comp_Type := Etype (Selector);
2845 Make_Selected_Component (Loc,
2846 Prefix => New_Copy_Tree (Target),
2847 Selector_Name => New_Occurrence_Of (Selector, Loc));
2849 if Nkind (Expression (Comp)) = N_Qualified_Expression then
2850 Expr_Q := Expression (Expression (Comp));
2852 Expr_Q := Expression (Comp);
2855 -- The controller is the one of the parent type defining the
2856 -- component (in case of inherited components).
2858 if Needs_Finalization (Comp_Type) then
2859 Internal_Final_List :=
2860 Make_Selected_Component (Loc,
2861 Prefix => Convert_To (
2862 Scope (Original_Record_Component (Selector)),
2863 New_Copy_Tree (Target)),
2865 Make_Identifier (Loc, Name_uController));
2867 Internal_Final_List :=
2868 Make_Selected_Component (Loc,
2869 Prefix => Internal_Final_List,
2870 Selector_Name => Make_Identifier (Loc, Name_F));
2872 -- The internal final list can be part of a constant object
2874 Set_Assignment_OK (Internal_Final_List);
2877 Internal_Final_List := Empty;
2880 -- Now either create the assignment or generate the code for the
2881 -- inner aggregate top-down.
2883 if Is_Delayed_Aggregate (Expr_Q) then
2885 -- We have the following case of aggregate nesting inside
2886 -- an object declaration:
2888 -- type Arr_Typ is array (Integer range <>) of ...;
2890 -- type Rec_Typ (...) is record
2891 -- Obj_Arr_Typ : Arr_Typ (A .. B);
2894 -- Obj_Rec_Typ : Rec_Typ := (...,
2895 -- Obj_Arr_Typ => (X => (...), Y => (...)));
2897 -- The length of the ranges of the aggregate and Obj_Add_Typ
2898 -- are equal (B - A = Y - X), but they do not coincide (X /=
2899 -- A and B /= Y). This case requires array sliding which is
2900 -- performed in the following manner:
2902 -- subtype Arr_Sub is Arr_Typ (X .. Y);
2904 -- Temp (X) := (...);
2906 -- Temp (Y) := (...);
2907 -- Obj_Rec_Typ.Obj_Arr_Typ := Temp;
2909 if Ekind (Comp_Type) = E_Array_Subtype
2910 and then Is_Int_Range_Bounds (Aggregate_Bounds (Expr_Q))
2911 and then Is_Int_Range_Bounds (First_Index (Comp_Type))
2913 Compatible_Int_Bounds
2914 (Agg_Bounds => Aggregate_Bounds (Expr_Q),
2915 Typ_Bounds => First_Index (Comp_Type))
2917 -- Create the array subtype with bounds equal to those of
2918 -- the corresponding aggregate.
2921 SubE : constant Entity_Id :=
2922 Make_Defining_Identifier (Loc,
2923 New_Internal_Name ('T'));
2925 SubD : constant Node_Id :=
2926 Make_Subtype_Declaration (Loc,
2927 Defining_Identifier =>
2929 Subtype_Indication =>
2930 Make_Subtype_Indication (Loc,
2931 Subtype_Mark => New_Reference_To (
2932 Etype (Comp_Type), Loc),
2934 Make_Index_Or_Discriminant_Constraint (
2935 Loc, Constraints => New_List (
2936 New_Copy_Tree (Aggregate_Bounds (
2939 -- Create a temporary array of the above subtype which
2940 -- will be used to capture the aggregate assignments.
2942 TmpE : constant Entity_Id :=
2943 Make_Defining_Identifier (Loc,
2944 New_Internal_Name ('A'));
2946 TmpD : constant Node_Id :=
2947 Make_Object_Declaration (Loc,
2948 Defining_Identifier =>
2950 Object_Definition =>
2951 New_Reference_To (SubE, Loc));
2954 Set_No_Initialization (TmpD);
2955 Append_To (L, SubD);
2956 Append_To (L, TmpD);
2958 -- Expand aggregate into assignments to the temp array
2961 Late_Expansion (Expr_Q, Comp_Type,
2962 New_Reference_To (TmpE, Loc), Internal_Final_List));
2967 Make_Assignment_Statement (Loc,
2968 Name => New_Copy_Tree (Comp_Expr),
2969 Expression => New_Reference_To (TmpE, Loc)));
2971 -- Do not pass the original aggregate to Gigi as is,
2972 -- since it will potentially clobber the front or the end
2973 -- of the array. Setting the expression to empty is safe
2974 -- since all aggregates are expanded into assignments.
2976 if Present (Obj) then
2977 Set_Expression (Parent (Obj), Empty);
2981 -- Normal case (sliding not required)
2985 Late_Expansion (Expr_Q, Comp_Type, Comp_Expr,
2986 Internal_Final_List));
2989 -- Expr_Q is not delayed aggregate
2993 Make_OK_Assignment_Statement (Loc,
2995 Expression => Expression (Comp));
2997 Set_No_Ctrl_Actions (Instr);
2998 Append_To (L, Instr);
3000 -- Adjust the tag if tagged (because of possible view
3001 -- conversions), unless compiling for a VM where tags are
3004 -- tmp.comp._tag := comp_typ'tag;
3006 if Is_Tagged_Type (Comp_Type) and then VM_Target = No_VM then
3008 Make_OK_Assignment_Statement (Loc,
3010 Make_Selected_Component (Loc,
3011 Prefix => New_Copy_Tree (Comp_Expr),
3014 (First_Tag_Component (Comp_Type), Loc)),
3017 Unchecked_Convert_To (RTE (RE_Tag),
3019 (Node (First_Elmt (Access_Disp_Table (Comp_Type))),
3022 Append_To (L, Instr);
3025 -- Adjust and Attach the component to the proper controller
3027 -- Adjust (tmp.comp);
3028 -- Attach_To_Final_List (tmp.comp,
3029 -- comp_typ (tmp)._record_controller.f)
3031 if Needs_Finalization (Comp_Type)
3032 and then not Is_Limited_Type (Comp_Type)
3036 Ref => New_Copy_Tree (Comp_Expr),
3038 Flist_Ref => Internal_Final_List,
3039 With_Attach => Make_Integer_Literal (Loc, 1)));
3045 elsif Ekind (Selector) = E_Discriminant
3046 and then Nkind (N) /= N_Extension_Aggregate
3047 and then Nkind (Parent (N)) = N_Component_Association
3048 and then Is_Constrained (Typ)
3050 -- We must check that the discriminant value imposed by the
3051 -- context is the same as the value given in the subaggregate,
3052 -- because after the expansion into assignments there is no
3053 -- record on which to perform a regular discriminant check.
3060 D_Val := First_Elmt (Discriminant_Constraint (Typ));
3061 Disc := First_Discriminant (Typ);
3062 while Chars (Disc) /= Chars (Selector) loop
3063 Next_Discriminant (Disc);
3067 pragma Assert (Present (D_Val));
3069 -- This check cannot performed for components that are
3070 -- constrained by a current instance, because this is not a
3071 -- value that can be compared with the actual constraint.
3073 if Nkind (Node (D_Val)) /= N_Attribute_Reference
3074 or else not Is_Entity_Name (Prefix (Node (D_Val)))
3075 or else not Is_Type (Entity (Prefix (Node (D_Val))))
3078 Make_Raise_Constraint_Error (Loc,
3081 Left_Opnd => New_Copy_Tree (Node (D_Val)),
3082 Right_Opnd => Expression (Comp)),
3083 Reason => CE_Discriminant_Check_Failed));
3086 -- Find self-reference in previous discriminant assignment,
3087 -- and replace with proper expression.
3094 while Present (Ass) loop
3095 if Nkind (Ass) = N_Assignment_Statement
3096 and then Nkind (Name (Ass)) = N_Selected_Component
3097 and then Chars (Selector_Name (Name (Ass))) =
3101 (Ass, New_Copy_Tree (Expression (Comp)));
3116 -- If the type is tagged, the tag needs to be initialized (unless
3117 -- compiling for the Java VM where tags are implicit). It is done
3118 -- late in the initialization process because in some cases, we call
3119 -- the init proc of an ancestor which will not leave out the right tag
3121 if Ancestor_Is_Expression then
3124 elsif Is_Tagged_Type (Typ) and then VM_Target = No_VM then
3126 Make_OK_Assignment_Statement (Loc,
3128 Make_Selected_Component (Loc,
3129 Prefix => New_Copy_Tree (Target),
3132 (First_Tag_Component (Base_Type (Typ)), Loc)),
3135 Unchecked_Convert_To (RTE (RE_Tag),
3137 (Node (First_Elmt (Access_Disp_Table (Base_Type (Typ)))),
3140 Append_To (L, Instr);
3142 -- Ada 2005 (AI-251): If the tagged type has been derived from
3143 -- abstract interfaces we must also initialize the tags of the
3144 -- secondary dispatch tables.
3146 if Has_Interfaces (Base_Type (Typ)) then
3148 (Typ => Base_Type (Typ),
3154 -- If the controllers have not been initialized yet (by lack of non-
3155 -- discriminant components), let's do it now.
3157 Gen_Ctrl_Actions_For_Aggr;
3160 end Build_Record_Aggr_Code;
3162 -------------------------------
3163 -- Convert_Aggr_In_Allocator --
3164 -------------------------------
3166 procedure Convert_Aggr_In_Allocator
3171 Loc : constant Source_Ptr := Sloc (Aggr);
3172 Typ : constant Entity_Id := Etype (Aggr);
3173 Temp : constant Entity_Id := Defining_Identifier (Decl);
3175 Occ : constant Node_Id :=
3176 Unchecked_Convert_To (Typ,
3177 Make_Explicit_Dereference (Loc,
3178 New_Reference_To (Temp, Loc)));
3180 Access_Type : constant Entity_Id := Etype (Temp);
3184 -- If the allocator is for an access discriminant, there is no
3185 -- finalization list for the anonymous access type, and the eventual
3186 -- finalization of the object is handled through the coextension
3187 -- mechanism. If the enclosing object is not dynamically allocated,
3188 -- the access discriminant is itself placed on the stack. Otherwise,
3189 -- some other finalization list is used (see exp_ch4.adb).
3191 -- Decl has been inserted in the code ahead of the allocator, using
3192 -- Insert_Actions. We use Insert_Actions below as well, to ensure that
3193 -- subsequent insertions are done in the proper order. Using (for
3194 -- example) Insert_Actions_After to place the expanded aggregate
3195 -- immediately after Decl may lead to out-of-order references if the
3196 -- allocator has generated a finalization list, as when the designated
3197 -- object is controlled and there is an open transient scope.
3199 if Ekind (Access_Type) = E_Anonymous_Access_Type
3200 and then Nkind (Associated_Node_For_Itype (Access_Type)) =
3201 N_Discriminant_Specification
3205 Flist := Find_Final_List (Access_Type);
3208 if Is_Array_Type (Typ) then
3209 Convert_Array_Aggr_In_Allocator (Decl, Aggr, Occ);
3211 elsif Has_Default_Init_Comps (Aggr) then
3213 L : constant List_Id := New_List;
3214 Init_Stmts : List_Id;
3221 Associated_Final_Chain (Base_Type (Access_Type)));
3223 -- ??? Dubious actual for Obj: expect 'the original object being
3226 if Has_Task (Typ) then
3227 Build_Task_Allocate_Block_With_Init_Stmts (L, Aggr, Init_Stmts);
3228 Insert_Actions (Alloc, L);
3230 Insert_Actions (Alloc, Init_Stmts);
3235 Insert_Actions (Alloc,
3237 (Aggr, Typ, Occ, Flist,
3238 Associated_Final_Chain (Base_Type (Access_Type))));
3240 -- ??? Dubious actual for Obj: expect 'the original object being
3244 end Convert_Aggr_In_Allocator;
3246 --------------------------------
3247 -- Convert_Aggr_In_Assignment --
3248 --------------------------------
3250 procedure Convert_Aggr_In_Assignment (N : Node_Id) is
3251 Aggr : Node_Id := Expression (N);
3252 Typ : constant Entity_Id := Etype (Aggr);
3253 Occ : constant Node_Id := New_Copy_Tree (Name (N));
3256 if Nkind (Aggr) = N_Qualified_Expression then
3257 Aggr := Expression (Aggr);
3260 Insert_Actions_After (N,
3263 Find_Final_List (Typ, New_Copy_Tree (Occ))));
3264 end Convert_Aggr_In_Assignment;
3266 ---------------------------------
3267 -- Convert_Aggr_In_Object_Decl --
3268 ---------------------------------
3270 procedure Convert_Aggr_In_Object_Decl (N : Node_Id) is
3271 Obj : constant Entity_Id := Defining_Identifier (N);
3272 Aggr : Node_Id := Expression (N);
3273 Loc : constant Source_Ptr := Sloc (Aggr);
3274 Typ : constant Entity_Id := Etype (Aggr);
3275 Occ : constant Node_Id := New_Occurrence_Of (Obj, Loc);
3277 function Discriminants_Ok return Boolean;
3278 -- If the object type is constrained, the discriminants in the
3279 -- aggregate must be checked against the discriminants of the subtype.
3280 -- This cannot be done using Apply_Discriminant_Checks because after
3281 -- expansion there is no aggregate left to check.
3283 ----------------------
3284 -- Discriminants_Ok --
3285 ----------------------
3287 function Discriminants_Ok return Boolean is
3288 Cond : Node_Id := Empty;
3297 D := First_Discriminant (Typ);
3298 Disc1 := First_Elmt (Discriminant_Constraint (Typ));
3299 Disc2 := First_Elmt (Discriminant_Constraint (Etype (Obj)));
3300 while Present (Disc1) and then Present (Disc2) loop
3301 Val1 := Node (Disc1);
3302 Val2 := Node (Disc2);
3304 if not Is_OK_Static_Expression (Val1)
3305 or else not Is_OK_Static_Expression (Val2)
3307 Check := Make_Op_Ne (Loc,
3308 Left_Opnd => Duplicate_Subexpr (Val1),
3309 Right_Opnd => Duplicate_Subexpr (Val2));
3315 Cond := Make_Or_Else (Loc,
3317 Right_Opnd => Check);
3320 elsif Expr_Value (Val1) /= Expr_Value (Val2) then
3321 Apply_Compile_Time_Constraint_Error (Aggr,
3322 Msg => "incorrect value for discriminant&?",
3323 Reason => CE_Discriminant_Check_Failed,
3328 Next_Discriminant (D);
3333 -- If any discriminant constraint is non-static, emit a check
3335 if Present (Cond) then
3337 Make_Raise_Constraint_Error (Loc,
3339 Reason => CE_Discriminant_Check_Failed));
3343 end Discriminants_Ok;
3345 -- Start of processing for Convert_Aggr_In_Object_Decl
3348 Set_Assignment_OK (Occ);
3350 if Nkind (Aggr) = N_Qualified_Expression then
3351 Aggr := Expression (Aggr);
3354 if Has_Discriminants (Typ)
3355 and then Typ /= Etype (Obj)
3356 and then Is_Constrained (Etype (Obj))
3357 and then not Discriminants_Ok
3362 -- If the context is an extended return statement, it has its own
3363 -- finalization machinery (i.e. works like a transient scope) and
3364 -- we do not want to create an additional one, because objects on
3365 -- the finalization list of the return must be moved to the caller's
3366 -- finalization list to complete the return.
3368 -- However, if the aggregate is limited, it is built in place, and the
3369 -- controlled components are not assigned to intermediate temporaries
3370 -- so there is no need for a transient scope in this case either.
3372 if Requires_Transient_Scope (Typ)
3373 and then Ekind (Current_Scope) /= E_Return_Statement
3374 and then not Is_Limited_Type (Typ)
3376 Establish_Transient_Scope
3379 Is_Controlled (Typ) or else Has_Controlled_Component (Typ));
3382 Insert_Actions_After (N, Late_Expansion (Aggr, Typ, Occ, Obj => Obj));
3383 Set_No_Initialization (N);
3384 Initialize_Discriminants (N, Typ);
3385 end Convert_Aggr_In_Object_Decl;
3387 -------------------------------------
3388 -- Convert_Array_Aggr_In_Allocator --
3389 -------------------------------------
3391 procedure Convert_Array_Aggr_In_Allocator
3396 Aggr_Code : List_Id;
3397 Typ : constant Entity_Id := Etype (Aggr);
3398 Ctyp : constant Entity_Id := Component_Type (Typ);
3401 -- The target is an explicit dereference of the allocated object.
3402 -- Generate component assignments to it, as for an aggregate that
3403 -- appears on the right-hand side of an assignment statement.
3406 Build_Array_Aggr_Code (Aggr,
3408 Index => First_Index (Typ),
3410 Scalar_Comp => Is_Scalar_Type (Ctyp));
3412 Insert_Actions_After (Decl, Aggr_Code);
3413 end Convert_Array_Aggr_In_Allocator;
3415 ----------------------------
3416 -- Convert_To_Assignments --
3417 ----------------------------
3419 procedure Convert_To_Assignments (N : Node_Id; Typ : Entity_Id) is
3420 Loc : constant Source_Ptr := Sloc (N);
3424 Target_Expr : Node_Id;
3425 Parent_Kind : Node_Kind;
3426 Unc_Decl : Boolean := False;
3427 Parent_Node : Node_Id;
3430 pragma Assert (not Is_Static_Dispatch_Table_Aggregate (N));
3431 pragma Assert (Is_Record_Type (Typ));
3433 Parent_Node := Parent (N);
3434 Parent_Kind := Nkind (Parent_Node);
3436 if Parent_Kind = N_Qualified_Expression then
3438 -- Check if we are in a unconstrained declaration because in this
3439 -- case the current delayed expansion mechanism doesn't work when
3440 -- the declared object size depend on the initializing expr.
3443 Parent_Node := Parent (Parent_Node);
3444 Parent_Kind := Nkind (Parent_Node);
3446 if Parent_Kind = N_Object_Declaration then
3448 not Is_Entity_Name (Object_Definition (Parent_Node))
3449 or else Has_Discriminants
3450 (Entity (Object_Definition (Parent_Node)))
3451 or else Is_Class_Wide_Type
3452 (Entity (Object_Definition (Parent_Node)));
3457 -- Just set the Delay flag in the cases where the transformation will be
3458 -- done top down from above.
3462 -- Internal aggregate (transformed when expanding the parent)
3464 or else Parent_Kind = N_Aggregate
3465 or else Parent_Kind = N_Extension_Aggregate
3466 or else Parent_Kind = N_Component_Association
3468 -- Allocator (see Convert_Aggr_In_Allocator)
3470 or else Parent_Kind = N_Allocator
3472 -- Object declaration (see Convert_Aggr_In_Object_Decl)
3474 or else (Parent_Kind = N_Object_Declaration and then not Unc_Decl)
3476 -- Safe assignment (see Convert_Aggr_Assignments). So far only the
3477 -- assignments in init procs are taken into account.
3479 or else (Parent_Kind = N_Assignment_Statement
3480 and then Inside_Init_Proc)
3482 -- (Ada 2005) An inherently limited type in a return statement,
3483 -- which will be handled in a build-in-place fashion, and may be
3484 -- rewritten as an extended return and have its own finalization
3485 -- machinery. In the case of a simple return, the aggregate needs
3486 -- to be delayed until the scope for the return statement has been
3487 -- created, so that any finalization chain will be associated with
3488 -- that scope. For extended returns, we delay expansion to avoid the
3489 -- creation of an unwanted transient scope that could result in
3490 -- premature finalization of the return object (which is built in
3491 -- in place within the caller's scope).
3494 (Is_Inherently_Limited_Type (Typ)
3496 (Nkind (Parent (Parent_Node)) = N_Extended_Return_Statement
3497 or else Nkind (Parent_Node) = N_Simple_Return_Statement))
3499 Set_Expansion_Delayed (N);
3503 if Requires_Transient_Scope (Typ) then
3504 Establish_Transient_Scope
3506 Is_Controlled (Typ) or else Has_Controlled_Component (Typ));
3509 -- If the aggregate is non-limited, create a temporary. If it is limited
3510 -- and the context is an assignment, this is a subaggregate for an
3511 -- enclosing aggregate being expanded. It must be built in place, so use
3512 -- the target of the current assignment.
3514 if Is_Limited_Type (Typ)
3515 and then Nkind (Parent (N)) = N_Assignment_Statement
3517 Target_Expr := New_Copy_Tree (Name (Parent (N)));
3519 (Parent (N), Build_Record_Aggr_Code (N, Typ, Target_Expr));
3520 Rewrite (Parent (N), Make_Null_Statement (Loc));
3523 Temp := Make_Defining_Identifier (Loc, New_Internal_Name ('A'));
3526 Make_Object_Declaration (Loc,
3527 Defining_Identifier => Temp,
3528 Object_Definition => New_Occurrence_Of (Typ, Loc));
3530 Set_No_Initialization (Instr);
3531 Insert_Action (N, Instr);
3532 Initialize_Discriminants (Instr, Typ);
3533 Target_Expr := New_Occurrence_Of (Temp, Loc);
3534 Insert_Actions (N, Build_Record_Aggr_Code (N, Typ, Target_Expr));
3535 Rewrite (N, New_Occurrence_Of (Temp, Loc));
3536 Analyze_And_Resolve (N, Typ);
3538 end Convert_To_Assignments;
3540 ---------------------------
3541 -- Convert_To_Positional --
3542 ---------------------------
3544 procedure Convert_To_Positional
3546 Max_Others_Replicate : Nat := 5;
3547 Handle_Bit_Packed : Boolean := False)
3549 Typ : constant Entity_Id := Etype (N);
3551 Static_Components : Boolean := True;
3553 procedure Check_Static_Components;
3554 -- Check whether all components of the aggregate are compile-time known
3555 -- values, and can be passed as is to the back-end without further
3561 Ixb : Node_Id) return Boolean;
3562 -- Convert the aggregate into a purely positional form if possible. On
3563 -- entry the bounds of all dimensions are known to be static, and the
3564 -- total number of components is safe enough to expand.
3566 function Is_Flat (N : Node_Id; Dims : Int) return Boolean;
3567 -- Return True iff the array N is flat (which is not rivial in the case
3568 -- of multidimensionsl aggregates).
3570 -----------------------------
3571 -- Check_Static_Components --
3572 -----------------------------
3574 procedure Check_Static_Components is
3578 Static_Components := True;
3580 if Nkind (N) = N_String_Literal then
3583 elsif Present (Expressions (N)) then
3584 Expr := First (Expressions (N));
3585 while Present (Expr) loop
3586 if Nkind (Expr) /= N_Aggregate
3587 or else not Compile_Time_Known_Aggregate (Expr)
3588 or else Expansion_Delayed (Expr)
3590 Static_Components := False;
3598 if Nkind (N) = N_Aggregate
3599 and then Present (Component_Associations (N))
3601 Expr := First (Component_Associations (N));
3602 while Present (Expr) loop
3603 if Nkind (Expression (Expr)) = N_Integer_Literal then
3606 elsif Nkind (Expression (Expr)) /= N_Aggregate
3608 not Compile_Time_Known_Aggregate (Expression (Expr))
3609 or else Expansion_Delayed (Expression (Expr))
3611 Static_Components := False;
3618 end Check_Static_Components;
3627 Ixb : Node_Id) return Boolean
3629 Loc : constant Source_Ptr := Sloc (N);
3630 Blo : constant Node_Id := Type_Low_Bound (Etype (Ixb));
3631 Lo : constant Node_Id := Type_Low_Bound (Etype (Ix));
3632 Hi : constant Node_Id := Type_High_Bound (Etype (Ix));
3637 if Nkind (Original_Node (N)) = N_String_Literal then
3641 if not Compile_Time_Known_Value (Lo)
3642 or else not Compile_Time_Known_Value (Hi)
3647 Lov := Expr_Value (Lo);
3648 Hiv := Expr_Value (Hi);
3651 or else not Compile_Time_Known_Value (Blo)
3656 -- Determine if set of alternatives is suitable for conversion and
3657 -- build an array containing the values in sequence.
3660 Vals : array (UI_To_Int (Lov) .. UI_To_Int (Hiv))
3661 of Node_Id := (others => Empty);
3662 -- The values in the aggregate sorted appropriately
3665 -- Same data as Vals in list form
3668 -- Used to validate Max_Others_Replicate limit
3671 Num : Int := UI_To_Int (Lov);
3676 if Present (Expressions (N)) then
3677 Elmt := First (Expressions (N));
3678 while Present (Elmt) loop
3679 if Nkind (Elmt) = N_Aggregate
3680 and then Present (Next_Index (Ix))
3682 not Flatten (Elmt, Next_Index (Ix), Next_Index (Ixb))
3687 Vals (Num) := Relocate_Node (Elmt);
3694 if No (Component_Associations (N)) then
3698 Elmt := First (Component_Associations (N));
3700 if Nkind (Expression (Elmt)) = N_Aggregate then
3701 if Present (Next_Index (Ix))
3704 (Expression (Elmt), Next_Index (Ix), Next_Index (Ixb))
3710 Component_Loop : while Present (Elmt) loop
3711 Choice := First (Choices (Elmt));
3712 Choice_Loop : while Present (Choice) loop
3714 -- If we have an others choice, fill in the missing elements
3715 -- subject to the limit established by Max_Others_Replicate.
3717 if Nkind (Choice) = N_Others_Choice then
3720 for J in Vals'Range loop
3721 if No (Vals (J)) then
3722 Vals (J) := New_Copy_Tree (Expression (Elmt));
3723 Rep_Count := Rep_Count + 1;
3725 -- Check for maximum others replication. Note that
3726 -- we skip this test if either of the restrictions
3727 -- No_Elaboration_Code or No_Implicit_Loops is
3728 -- active, or if this is a preelaborable unit.
3731 P : constant Entity_Id :=
3732 Cunit_Entity (Current_Sem_Unit);
3735 if Restriction_Active (No_Elaboration_Code)
3736 or else Restriction_Active (No_Implicit_Loops)
3737 or else Is_Preelaborated (P)
3738 or else (Ekind (P) = E_Package_Body
3740 Is_Preelaborated (Spec_Entity (P)))
3744 elsif Rep_Count > Max_Others_Replicate then
3751 exit Component_Loop;
3753 -- Case of a subtype mark
3755 elsif Nkind (Choice) = N_Identifier
3756 and then Is_Type (Entity (Choice))
3758 Lo := Type_Low_Bound (Etype (Choice));
3759 Hi := Type_High_Bound (Etype (Choice));
3761 -- Case of subtype indication
3763 elsif Nkind (Choice) = N_Subtype_Indication then
3764 Lo := Low_Bound (Range_Expression (Constraint (Choice)));
3765 Hi := High_Bound (Range_Expression (Constraint (Choice)));
3769 elsif Nkind (Choice) = N_Range then
3770 Lo := Low_Bound (Choice);
3771 Hi := High_Bound (Choice);
3773 -- Normal subexpression case
3775 else pragma Assert (Nkind (Choice) in N_Subexpr);
3776 if not Compile_Time_Known_Value (Choice) then
3780 Vals (UI_To_Int (Expr_Value (Choice))) :=
3781 New_Copy_Tree (Expression (Elmt));
3786 -- Range cases merge with Lo,Hi said
3788 if not Compile_Time_Known_Value (Lo)
3790 not Compile_Time_Known_Value (Hi)
3794 for J in UI_To_Int (Expr_Value (Lo)) ..
3795 UI_To_Int (Expr_Value (Hi))
3797 Vals (J) := New_Copy_Tree (Expression (Elmt));
3803 end loop Choice_Loop;
3806 end loop Component_Loop;
3808 -- If we get here the conversion is possible
3811 for J in Vals'Range loop
3812 Append (Vals (J), Vlist);
3815 Rewrite (N, Make_Aggregate (Loc, Expressions => Vlist));
3816 Set_Aggregate_Bounds (N, Aggregate_Bounds (Original_Node (N)));
3825 function Is_Flat (N : Node_Id; Dims : Int) return Boolean is
3832 elsif Nkind (N) = N_Aggregate then
3833 if Present (Component_Associations (N)) then
3837 Elmt := First (Expressions (N));
3838 while Present (Elmt) loop
3839 if not Is_Flat (Elmt, Dims - 1) then
3853 -- Start of processing for Convert_To_Positional
3856 -- Ada 2005 (AI-287): Do not convert in case of default initialized
3857 -- components because in this case will need to call the corresponding
3860 if Has_Default_Init_Comps (N) then
3864 if Is_Flat (N, Number_Dimensions (Typ)) then
3868 if Is_Bit_Packed_Array (Typ)
3869 and then not Handle_Bit_Packed
3874 -- Do not convert to positional if controlled components are involved
3875 -- since these require special processing
3877 if Has_Controlled_Component (Typ) then
3881 Check_Static_Components;
3883 -- If the size is known, or all the components are static, try to
3884 -- build a fully positional aggregate.
3886 -- The size of the type may not be known for an aggregate with
3887 -- discriminated array components, but if the components are static
3888 -- it is still possible to verify statically that the length is
3889 -- compatible with the upper bound of the type, and therefore it is
3890 -- worth flattening such aggregates as well.
3892 -- For now the back-end expands these aggregates into individual
3893 -- assignments to the target anyway, but it is conceivable that
3894 -- it will eventually be able to treat such aggregates statically???
3896 if Aggr_Size_OK (N, Typ)
3897 and then Flatten (N, First_Index (Typ), First_Index (Base_Type (Typ)))
3899 if Static_Components then
3900 Set_Compile_Time_Known_Aggregate (N);
3901 Set_Expansion_Delayed (N, False);
3904 Analyze_And_Resolve (N, Typ);
3906 end Convert_To_Positional;
3908 ----------------------------
3909 -- Expand_Array_Aggregate --
3910 ----------------------------
3912 -- Array aggregate expansion proceeds as follows:
3914 -- 1. If requested we generate code to perform all the array aggregate
3915 -- bound checks, specifically
3917 -- (a) Check that the index range defined by aggregate bounds is
3918 -- compatible with corresponding index subtype.
3920 -- (b) If an others choice is present check that no aggregate
3921 -- index is outside the bounds of the index constraint.
3923 -- (c) For multidimensional arrays make sure that all subaggregates
3924 -- corresponding to the same dimension have the same bounds.
3926 -- 2. Check for packed array aggregate which can be converted to a
3927 -- constant so that the aggregate disappeares completely.
3929 -- 3. Check case of nested aggregate. Generally nested aggregates are
3930 -- handled during the processing of the parent aggregate.
3932 -- 4. Check if the aggregate can be statically processed. If this is the
3933 -- case pass it as is to Gigi. Note that a necessary condition for
3934 -- static processing is that the aggregate be fully positional.
3936 -- 5. If in place aggregate expansion is possible (i.e. no need to create
3937 -- a temporary) then mark the aggregate as such and return. Otherwise
3938 -- create a new temporary and generate the appropriate initialization
3941 procedure Expand_Array_Aggregate (N : Node_Id) is
3942 Loc : constant Source_Ptr := Sloc (N);
3944 Typ : constant Entity_Id := Etype (N);
3945 Ctyp : constant Entity_Id := Component_Type (Typ);
3946 -- Typ is the correct constrained array subtype of the aggregate
3947 -- Ctyp is the corresponding component type.
3949 Aggr_Dimension : constant Pos := Number_Dimensions (Typ);
3950 -- Number of aggregate index dimensions
3952 Aggr_Low : array (1 .. Aggr_Dimension) of Node_Id;
3953 Aggr_High : array (1 .. Aggr_Dimension) of Node_Id;
3954 -- Low and High bounds of the constraint for each aggregate index
3956 Aggr_Index_Typ : array (1 .. Aggr_Dimension) of Entity_Id;
3957 -- The type of each index
3959 Maybe_In_Place_OK : Boolean;
3960 -- If the type is neither controlled nor packed and the aggregate
3961 -- is the expression in an assignment, assignment in place may be
3962 -- possible, provided other conditions are met on the LHS.
3964 Others_Present : array (1 .. Aggr_Dimension) of Boolean :=
3966 -- If Others_Present (J) is True, then there is an others choice
3967 -- in one of the sub-aggregates of N at dimension J.
3969 procedure Build_Constrained_Type (Positional : Boolean);
3970 -- If the subtype is not static or unconstrained, build a constrained
3971 -- type using the computable sizes of the aggregate and its sub-
3974 procedure Check_Bounds (Aggr_Bounds : Node_Id; Index_Bounds : Node_Id);
3975 -- Checks that the bounds of Aggr_Bounds are within the bounds defined
3978 procedure Check_Same_Aggr_Bounds (Sub_Aggr : Node_Id; Dim : Pos);
3979 -- Checks that in a multi-dimensional array aggregate all subaggregates
3980 -- corresponding to the same dimension have the same bounds.
3981 -- Sub_Aggr is an array sub-aggregate. Dim is the dimension
3982 -- corresponding to the sub-aggregate.
3984 procedure Compute_Others_Present (Sub_Aggr : Node_Id; Dim : Pos);
3985 -- Computes the values of array Others_Present. Sub_Aggr is the
3986 -- array sub-aggregate we start the computation from. Dim is the
3987 -- dimension corresponding to the sub-aggregate.
3989 function Has_Address_Clause (D : Node_Id) return Boolean;
3990 -- If the aggregate is the expression in an object declaration, it
3991 -- cannot be expanded in place. This function does a lookahead in the
3992 -- current declarative part to find an address clause for the object
3995 function In_Place_Assign_OK return Boolean;
3996 -- Simple predicate to determine whether an aggregate assignment can
3997 -- be done in place, because none of the new values can depend on the
3998 -- components of the target of the assignment.
4000 procedure Others_Check (Sub_Aggr : Node_Id; Dim : Pos);
4001 -- Checks that if an others choice is present in any sub-aggregate no
4002 -- aggregate index is outside the bounds of the index constraint.
4003 -- Sub_Aggr is an array sub-aggregate. Dim is the dimension
4004 -- corresponding to the sub-aggregate.
4006 ----------------------------
4007 -- Build_Constrained_Type --
4008 ----------------------------
4010 procedure Build_Constrained_Type (Positional : Boolean) is
4011 Loc : constant Source_Ptr := Sloc (N);
4012 Agg_Type : Entity_Id;
4015 Typ : constant Entity_Id := Etype (N);
4016 Indices : constant List_Id := New_List;
4022 Make_Defining_Identifier (
4023 Loc, New_Internal_Name ('A'));
4025 -- If the aggregate is purely positional, all its subaggregates
4026 -- have the same size. We collect the dimensions from the first
4027 -- subaggregate at each level.
4032 for D in 1 .. Number_Dimensions (Typ) loop
4033 Sub_Agg := First (Expressions (Sub_Agg));
4037 while Present (Comp) loop
4044 Low_Bound => Make_Integer_Literal (Loc, 1),
4046 Make_Integer_Literal (Loc, Num)),
4051 -- We know the aggregate type is unconstrained and the aggregate
4052 -- is not processable by the back end, therefore not necessarily
4053 -- positional. Retrieve each dimension bounds (computed earlier).
4056 for D in 1 .. Number_Dimensions (Typ) loop
4059 Low_Bound => Aggr_Low (D),
4060 High_Bound => Aggr_High (D)),
4066 Make_Full_Type_Declaration (Loc,
4067 Defining_Identifier => Agg_Type,
4069 Make_Constrained_Array_Definition (Loc,
4070 Discrete_Subtype_Definitions => Indices,
4071 Component_Definition =>
4072 Make_Component_Definition (Loc,
4073 Aliased_Present => False,
4074 Subtype_Indication =>
4075 New_Occurrence_Of (Component_Type (Typ), Loc))));
4077 Insert_Action (N, Decl);
4079 Set_Etype (N, Agg_Type);
4080 Set_Is_Itype (Agg_Type);
4081 Freeze_Itype (Agg_Type, N);
4082 end Build_Constrained_Type;
4088 procedure Check_Bounds (Aggr_Bounds : Node_Id; Index_Bounds : Node_Id) is
4095 Cond : Node_Id := Empty;
4098 Get_Index_Bounds (Aggr_Bounds, Aggr_Lo, Aggr_Hi);
4099 Get_Index_Bounds (Index_Bounds, Ind_Lo, Ind_Hi);
4101 -- Generate the following test:
4103 -- [constraint_error when
4104 -- Aggr_Lo <= Aggr_Hi and then
4105 -- (Aggr_Lo < Ind_Lo or else Aggr_Hi > Ind_Hi)]
4107 -- As an optimization try to see if some tests are trivially vacuous
4108 -- because we are comparing an expression against itself.
4110 if Aggr_Lo = Ind_Lo and then Aggr_Hi = Ind_Hi then
4113 elsif Aggr_Hi = Ind_Hi then
4116 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
4117 Right_Opnd => Duplicate_Subexpr_Move_Checks (Ind_Lo));
4119 elsif Aggr_Lo = Ind_Lo then
4122 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi),
4123 Right_Opnd => Duplicate_Subexpr_Move_Checks (Ind_Hi));
4130 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
4131 Right_Opnd => Duplicate_Subexpr_Move_Checks (Ind_Lo)),
4135 Left_Opnd => Duplicate_Subexpr (Aggr_Hi),
4136 Right_Opnd => Duplicate_Subexpr (Ind_Hi)));
4139 if Present (Cond) then
4144 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
4145 Right_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi)),
4147 Right_Opnd => Cond);
4149 Set_Analyzed (Left_Opnd (Left_Opnd (Cond)), False);
4150 Set_Analyzed (Right_Opnd (Left_Opnd (Cond)), False);
4152 Make_Raise_Constraint_Error (Loc,
4154 Reason => CE_Length_Check_Failed));
4158 ----------------------------
4159 -- Check_Same_Aggr_Bounds --
4160 ----------------------------
4162 procedure Check_Same_Aggr_Bounds (Sub_Aggr : Node_Id; Dim : Pos) is
4163 Sub_Lo : constant Node_Id := Low_Bound (Aggregate_Bounds (Sub_Aggr));
4164 Sub_Hi : constant Node_Id := High_Bound (Aggregate_Bounds (Sub_Aggr));
4165 -- The bounds of this specific sub-aggregate
4167 Aggr_Lo : constant Node_Id := Aggr_Low (Dim);
4168 Aggr_Hi : constant Node_Id := Aggr_High (Dim);
4169 -- The bounds of the aggregate for this dimension
4171 Ind_Typ : constant Entity_Id := Aggr_Index_Typ (Dim);
4172 -- The index type for this dimension.xxx
4174 Cond : Node_Id := Empty;
4179 -- If index checks are on generate the test
4181 -- [constraint_error when
4182 -- Aggr_Lo /= Sub_Lo or else Aggr_Hi /= Sub_Hi]
4184 -- As an optimization try to see if some tests are trivially vacuos
4185 -- because we are comparing an expression against itself. Also for
4186 -- the first dimension the test is trivially vacuous because there
4187 -- is just one aggregate for dimension 1.
4189 if Index_Checks_Suppressed (Ind_Typ) then
4193 or else (Aggr_Lo = Sub_Lo and then Aggr_Hi = Sub_Hi)
4197 elsif Aggr_Hi = Sub_Hi then
4200 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
4201 Right_Opnd => Duplicate_Subexpr_Move_Checks (Sub_Lo));
4203 elsif Aggr_Lo = Sub_Lo then
4206 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi),
4207 Right_Opnd => Duplicate_Subexpr_Move_Checks (Sub_Hi));
4214 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
4215 Right_Opnd => Duplicate_Subexpr_Move_Checks (Sub_Lo)),
4219 Left_Opnd => Duplicate_Subexpr (Aggr_Hi),
4220 Right_Opnd => Duplicate_Subexpr (Sub_Hi)));
4223 if Present (Cond) then
4225 Make_Raise_Constraint_Error (Loc,
4227 Reason => CE_Length_Check_Failed));
4230 -- Now look inside the sub-aggregate to see if there is more work
4232 if Dim < Aggr_Dimension then
4234 -- Process positional components
4236 if Present (Expressions (Sub_Aggr)) then
4237 Expr := First (Expressions (Sub_Aggr));
4238 while Present (Expr) loop
4239 Check_Same_Aggr_Bounds (Expr, Dim + 1);
4244 -- Process component associations
4246 if Present (Component_Associations (Sub_Aggr)) then
4247 Assoc := First (Component_Associations (Sub_Aggr));
4248 while Present (Assoc) loop
4249 Expr := Expression (Assoc);
4250 Check_Same_Aggr_Bounds (Expr, Dim + 1);
4255 end Check_Same_Aggr_Bounds;
4257 ----------------------------
4258 -- Compute_Others_Present --
4259 ----------------------------
4261 procedure Compute_Others_Present (Sub_Aggr : Node_Id; Dim : Pos) is
4266 if Present (Component_Associations (Sub_Aggr)) then
4267 Assoc := Last (Component_Associations (Sub_Aggr));
4269 if Nkind (First (Choices (Assoc))) = N_Others_Choice then
4270 Others_Present (Dim) := True;
4274 -- Now look inside the sub-aggregate to see if there is more work
4276 if Dim < Aggr_Dimension then
4278 -- Process positional components
4280 if Present (Expressions (Sub_Aggr)) then
4281 Expr := First (Expressions (Sub_Aggr));
4282 while Present (Expr) loop
4283 Compute_Others_Present (Expr, Dim + 1);
4288 -- Process component associations
4290 if Present (Component_Associations (Sub_Aggr)) then
4291 Assoc := First (Component_Associations (Sub_Aggr));
4292 while Present (Assoc) loop
4293 Expr := Expression (Assoc);
4294 Compute_Others_Present (Expr, Dim + 1);
4299 end Compute_Others_Present;
4301 ------------------------
4302 -- Has_Address_Clause --
4303 ------------------------
4305 function Has_Address_Clause (D : Node_Id) return Boolean is
4306 Id : constant Entity_Id := Defining_Identifier (D);
4311 while Present (Decl) loop
4312 if Nkind (Decl) = N_At_Clause
4313 and then Chars (Identifier (Decl)) = Chars (Id)
4317 elsif Nkind (Decl) = N_Attribute_Definition_Clause
4318 and then Chars (Decl) = Name_Address
4319 and then Chars (Name (Decl)) = Chars (Id)
4328 end Has_Address_Clause;
4330 ------------------------
4331 -- In_Place_Assign_OK --
4332 ------------------------
4334 function In_Place_Assign_OK return Boolean is
4342 function Is_Others_Aggregate (Aggr : Node_Id) return Boolean;
4343 -- Aggregates that consist of a single Others choice are safe
4344 -- if the single expression is.
4346 function Safe_Aggregate (Aggr : Node_Id) return Boolean;
4347 -- Check recursively that each component of a (sub)aggregate does
4348 -- not depend on the variable being assigned to.
4350 function Safe_Component (Expr : Node_Id) return Boolean;
4351 -- Verify that an expression cannot depend on the variable being
4352 -- assigned to. Room for improvement here (but less than before).
4354 -------------------------
4355 -- Is_Others_Aggregate --
4356 -------------------------
4358 function Is_Others_Aggregate (Aggr : Node_Id) return Boolean is
4360 return No (Expressions (Aggr))
4362 (First (Choices (First (Component_Associations (Aggr)))))
4364 end Is_Others_Aggregate;
4366 --------------------
4367 -- Safe_Aggregate --
4368 --------------------
4370 function Safe_Aggregate (Aggr : Node_Id) return Boolean is
4374 if Present (Expressions (Aggr)) then
4375 Expr := First (Expressions (Aggr));
4376 while Present (Expr) loop
4377 if Nkind (Expr) = N_Aggregate then
4378 if not Safe_Aggregate (Expr) then
4382 elsif not Safe_Component (Expr) then
4390 if Present (Component_Associations (Aggr)) then
4391 Expr := First (Component_Associations (Aggr));
4392 while Present (Expr) loop
4393 if Nkind (Expression (Expr)) = N_Aggregate then
4394 if not Safe_Aggregate (Expression (Expr)) then
4398 elsif not Safe_Component (Expression (Expr)) then
4409 --------------------
4410 -- Safe_Component --
4411 --------------------
4413 function Safe_Component (Expr : Node_Id) return Boolean is
4414 Comp : Node_Id := Expr;
4416 function Check_Component (Comp : Node_Id) return Boolean;
4417 -- Do the recursive traversal, after copy
4419 ---------------------
4420 -- Check_Component --
4421 ---------------------
4423 function Check_Component (Comp : Node_Id) return Boolean is
4425 if Is_Overloaded (Comp) then
4429 return Compile_Time_Known_Value (Comp)
4431 or else (Is_Entity_Name (Comp)
4432 and then Present (Entity (Comp))
4433 and then No (Renamed_Object (Entity (Comp))))
4435 or else (Nkind (Comp) = N_Attribute_Reference
4436 and then Check_Component (Prefix (Comp)))
4438 or else (Nkind (Comp) in N_Binary_Op
4439 and then Check_Component (Left_Opnd (Comp))
4440 and then Check_Component (Right_Opnd (Comp)))
4442 or else (Nkind (Comp) in N_Unary_Op
4443 and then Check_Component (Right_Opnd (Comp)))
4445 or else (Nkind (Comp) = N_Selected_Component
4446 and then Check_Component (Prefix (Comp)))
4448 or else (Nkind (Comp) = N_Unchecked_Type_Conversion
4449 and then Check_Component (Expression (Comp)));
4450 end Check_Component;
4452 -- Start of processing for Safe_Component
4455 -- If the component appears in an association that may
4456 -- correspond to more than one element, it is not analyzed
4457 -- before the expansion into assignments, to avoid side effects.
4458 -- We analyze, but do not resolve the copy, to obtain sufficient
4459 -- entity information for the checks that follow. If component is
4460 -- overloaded we assume an unsafe function call.
4462 if not Analyzed (Comp) then
4463 if Is_Overloaded (Expr) then
4466 elsif Nkind (Expr) = N_Aggregate
4467 and then not Is_Others_Aggregate (Expr)
4471 elsif Nkind (Expr) = N_Allocator then
4473 -- For now, too complex to analyze
4478 Comp := New_Copy_Tree (Expr);
4479 Set_Parent (Comp, Parent (Expr));
4483 if Nkind (Comp) = N_Aggregate then
4484 return Safe_Aggregate (Comp);
4486 return Check_Component (Comp);
4490 -- Start of processing for In_Place_Assign_OK
4493 if Present (Component_Associations (N)) then
4495 -- On assignment, sliding can take place, so we cannot do the
4496 -- assignment in place unless the bounds of the aggregate are
4497 -- statically equal to those of the target.
4499 -- If the aggregate is given by an others choice, the bounds
4500 -- are derived from the left-hand side, and the assignment is
4501 -- safe if the expression is.
4503 if Is_Others_Aggregate (N) then
4506 (Expression (First (Component_Associations (N))));
4509 Aggr_In := First_Index (Etype (N));
4510 if Nkind (Parent (N)) = N_Assignment_Statement then
4511 Obj_In := First_Index (Etype (Name (Parent (N))));
4514 -- Context is an allocator. Check bounds of aggregate
4515 -- against given type in qualified expression.
4517 pragma Assert (Nkind (Parent (Parent (N))) = N_Allocator);
4519 First_Index (Etype (Entity (Subtype_Mark (Parent (N)))));
4522 while Present (Aggr_In) loop
4523 Get_Index_Bounds (Aggr_In, Aggr_Lo, Aggr_Hi);
4524 Get_Index_Bounds (Obj_In, Obj_Lo, Obj_Hi);
4526 if not Compile_Time_Known_Value (Aggr_Lo)
4527 or else not Compile_Time_Known_Value (Aggr_Hi)
4528 or else not Compile_Time_Known_Value (Obj_Lo)
4529 or else not Compile_Time_Known_Value (Obj_Hi)
4530 or else Expr_Value (Aggr_Lo) /= Expr_Value (Obj_Lo)
4531 or else Expr_Value (Aggr_Hi) /= Expr_Value (Obj_Hi)
4536 Next_Index (Aggr_In);
4537 Next_Index (Obj_In);
4541 -- Now check the component values themselves
4543 return Safe_Aggregate (N);
4544 end In_Place_Assign_OK;
4550 procedure Others_Check (Sub_Aggr : Node_Id; Dim : Pos) is
4551 Aggr_Lo : constant Node_Id := Aggr_Low (Dim);
4552 Aggr_Hi : constant Node_Id := Aggr_High (Dim);
4553 -- The bounds of the aggregate for this dimension
4555 Ind_Typ : constant Entity_Id := Aggr_Index_Typ (Dim);
4556 -- The index type for this dimension
4558 Need_To_Check : Boolean := False;
4560 Choices_Lo : Node_Id := Empty;
4561 Choices_Hi : Node_Id := Empty;
4562 -- The lowest and highest discrete choices for a named sub-aggregate
4564 Nb_Choices : Int := -1;
4565 -- The number of discrete non-others choices in this sub-aggregate
4567 Nb_Elements : Uint := Uint_0;
4568 -- The number of elements in a positional aggregate
4570 Cond : Node_Id := Empty;
4577 -- Check if we have an others choice. If we do make sure that this
4578 -- sub-aggregate contains at least one element in addition to the
4581 if Range_Checks_Suppressed (Ind_Typ) then
4582 Need_To_Check := False;
4584 elsif Present (Expressions (Sub_Aggr))
4585 and then Present (Component_Associations (Sub_Aggr))
4587 Need_To_Check := True;
4589 elsif Present (Component_Associations (Sub_Aggr)) then
4590 Assoc := Last (Component_Associations (Sub_Aggr));
4592 if Nkind (First (Choices (Assoc))) /= N_Others_Choice then
4593 Need_To_Check := False;
4596 -- Count the number of discrete choices. Start with -1 because
4597 -- the others choice does not count.
4600 Assoc := First (Component_Associations (Sub_Aggr));
4601 while Present (Assoc) loop
4602 Choice := First (Choices (Assoc));
4603 while Present (Choice) loop
4604 Nb_Choices := Nb_Choices + 1;
4611 -- If there is only an others choice nothing to do
4613 Need_To_Check := (Nb_Choices > 0);
4617 Need_To_Check := False;
4620 -- If we are dealing with a positional sub-aggregate with an others
4621 -- choice then compute the number or positional elements.
4623 if Need_To_Check and then Present (Expressions (Sub_Aggr)) then
4624 Expr := First (Expressions (Sub_Aggr));
4625 Nb_Elements := Uint_0;
4626 while Present (Expr) loop
4627 Nb_Elements := Nb_Elements + 1;
4631 -- If the aggregate contains discrete choices and an others choice
4632 -- compute the smallest and largest discrete choice values.
4634 elsif Need_To_Check then
4635 Compute_Choices_Lo_And_Choices_Hi : declare
4637 Table : Case_Table_Type (1 .. Nb_Choices);
4638 -- Used to sort all the different choice values
4645 Assoc := First (Component_Associations (Sub_Aggr));
4646 while Present (Assoc) loop
4647 Choice := First (Choices (Assoc));
4648 while Present (Choice) loop
4649 if Nkind (Choice) = N_Others_Choice then
4653 Get_Index_Bounds (Choice, Low, High);
4654 Table (J).Choice_Lo := Low;
4655 Table (J).Choice_Hi := High;
4664 -- Sort the discrete choices
4666 Sort_Case_Table (Table);
4668 Choices_Lo := Table (1).Choice_Lo;
4669 Choices_Hi := Table (Nb_Choices).Choice_Hi;
4670 end Compute_Choices_Lo_And_Choices_Hi;
4673 -- If no others choice in this sub-aggregate, or the aggregate
4674 -- comprises only an others choice, nothing to do.
4676 if not Need_To_Check then
4679 -- If we are dealing with an aggregate containing an others choice
4680 -- and positional components, we generate the following test:
4682 -- if Ind_Typ'Pos (Aggr_Lo) + (Nb_Elements - 1) >
4683 -- Ind_Typ'Pos (Aggr_Hi)
4685 -- raise Constraint_Error;
4688 elsif Nb_Elements > Uint_0 then
4694 Make_Attribute_Reference (Loc,
4695 Prefix => New_Reference_To (Ind_Typ, Loc),
4696 Attribute_Name => Name_Pos,
4699 (Duplicate_Subexpr_Move_Checks (Aggr_Lo))),
4700 Right_Opnd => Make_Integer_Literal (Loc, Nb_Elements - 1)),
4703 Make_Attribute_Reference (Loc,
4704 Prefix => New_Reference_To (Ind_Typ, Loc),
4705 Attribute_Name => Name_Pos,
4706 Expressions => New_List (
4707 Duplicate_Subexpr_Move_Checks (Aggr_Hi))));
4709 -- If we are dealing with an aggregate containing an others choice
4710 -- and discrete choices we generate the following test:
4712 -- [constraint_error when
4713 -- Choices_Lo < Aggr_Lo or else Choices_Hi > Aggr_Hi];
4721 Duplicate_Subexpr_Move_Checks (Choices_Lo),
4723 Duplicate_Subexpr_Move_Checks (Aggr_Lo)),
4728 Duplicate_Subexpr (Choices_Hi),
4730 Duplicate_Subexpr (Aggr_Hi)));
4733 if Present (Cond) then
4735 Make_Raise_Constraint_Error (Loc,
4737 Reason => CE_Length_Check_Failed));
4738 -- Questionable reason code, shouldn't that be a
4739 -- CE_Range_Check_Failed ???
4742 -- Now look inside the sub-aggregate to see if there is more work
4744 if Dim < Aggr_Dimension then
4746 -- Process positional components
4748 if Present (Expressions (Sub_Aggr)) then
4749 Expr := First (Expressions (Sub_Aggr));
4750 while Present (Expr) loop
4751 Others_Check (Expr, Dim + 1);
4756 -- Process component associations
4758 if Present (Component_Associations (Sub_Aggr)) then
4759 Assoc := First (Component_Associations (Sub_Aggr));
4760 while Present (Assoc) loop
4761 Expr := Expression (Assoc);
4762 Others_Check (Expr, Dim + 1);
4769 -- Remaining Expand_Array_Aggregate variables
4772 -- Holds the temporary aggregate value
4775 -- Holds the declaration of Tmp
4777 Aggr_Code : List_Id;
4778 Parent_Node : Node_Id;
4779 Parent_Kind : Node_Kind;
4781 -- Start of processing for Expand_Array_Aggregate
4784 -- Do not touch the special aggregates of attributes used for Asm calls
4786 if Is_RTE (Ctyp, RE_Asm_Input_Operand)
4787 or else Is_RTE (Ctyp, RE_Asm_Output_Operand)
4792 -- If the semantic analyzer has determined that aggregate N will raise
4793 -- Constraint_Error at run-time, then the aggregate node has been
4794 -- replaced with an N_Raise_Constraint_Error node and we should
4797 pragma Assert (not Raises_Constraint_Error (N));
4801 -- Check that the index range defined by aggregate bounds is
4802 -- compatible with corresponding index subtype.
4804 Index_Compatibility_Check : declare
4805 Aggr_Index_Range : Node_Id := First_Index (Typ);
4806 -- The current aggregate index range
4808 Index_Constraint : Node_Id := First_Index (Etype (Typ));
4809 -- The corresponding index constraint against which we have to
4810 -- check the above aggregate index range.
4813 Compute_Others_Present (N, 1);
4815 for J in 1 .. Aggr_Dimension loop
4816 -- There is no need to emit a check if an others choice is
4817 -- present for this array aggregate dimension since in this
4818 -- case one of N's sub-aggregates has taken its bounds from the
4819 -- context and these bounds must have been checked already. In
4820 -- addition all sub-aggregates corresponding to the same
4821 -- dimension must all have the same bounds (checked in (c) below).
4823 if not Range_Checks_Suppressed (Etype (Index_Constraint))
4824 and then not Others_Present (J)
4826 -- We don't use Checks.Apply_Range_Check here because it emits
4827 -- a spurious check. Namely it checks that the range defined by
4828 -- the aggregate bounds is non empty. But we know this already
4831 Check_Bounds (Aggr_Index_Range, Index_Constraint);
4834 -- Save the low and high bounds of the aggregate index as well as
4835 -- the index type for later use in checks (b) and (c) below.
4837 Aggr_Low (J) := Low_Bound (Aggr_Index_Range);
4838 Aggr_High (J) := High_Bound (Aggr_Index_Range);
4840 Aggr_Index_Typ (J) := Etype (Index_Constraint);
4842 Next_Index (Aggr_Index_Range);
4843 Next_Index (Index_Constraint);
4845 end Index_Compatibility_Check;
4849 -- If an others choice is present check that no aggregate index is
4850 -- outside the bounds of the index constraint.
4852 Others_Check (N, 1);
4856 -- For multidimensional arrays make sure that all subaggregates
4857 -- corresponding to the same dimension have the same bounds.
4859 if Aggr_Dimension > 1 then
4860 Check_Same_Aggr_Bounds (N, 1);
4865 -- Here we test for is packed array aggregate that we can handle at
4866 -- compile time. If so, return with transformation done. Note that we do
4867 -- this even if the aggregate is nested, because once we have done this
4868 -- processing, there is no more nested aggregate!
4870 if Packed_Array_Aggregate_Handled (N) then
4874 -- At this point we try to convert to positional form
4876 if Ekind (Current_Scope) = E_Package
4877 and then Static_Elaboration_Desired (Current_Scope)
4879 Convert_To_Positional (N, Max_Others_Replicate => 100);
4882 Convert_To_Positional (N);
4885 -- if the result is no longer an aggregate (e.g. it may be a string
4886 -- literal, or a temporary which has the needed value), then we are
4887 -- done, since there is no longer a nested aggregate.
4889 if Nkind (N) /= N_Aggregate then
4892 -- We are also done if the result is an analyzed aggregate
4893 -- This case could use more comments ???
4896 and then N /= Original_Node (N)
4901 -- If all aggregate components are compile-time known and the aggregate
4902 -- has been flattened, nothing left to do. The same occurs if the
4903 -- aggregate is used to initialize the components of an statically
4904 -- allocated dispatch table.
4906 if Compile_Time_Known_Aggregate (N)
4907 or else Is_Static_Dispatch_Table_Aggregate (N)
4909 Set_Expansion_Delayed (N, False);
4913 -- Now see if back end processing is possible
4915 if Backend_Processing_Possible (N) then
4917 -- If the aggregate is static but the constraints are not, build
4918 -- a static subtype for the aggregate, so that Gigi can place it
4919 -- in static memory. Perform an unchecked_conversion to the non-
4920 -- static type imposed by the context.
4923 Itype : constant Entity_Id := Etype (N);
4925 Needs_Type : Boolean := False;
4928 Index := First_Index (Itype);
4929 while Present (Index) loop
4930 if not Is_Static_Subtype (Etype (Index)) then
4939 Build_Constrained_Type (Positional => True);
4940 Rewrite (N, Unchecked_Convert_To (Itype, N));
4950 -- Delay expansion for nested aggregates: it will be taken care of
4951 -- when the parent aggregate is expanded.
4953 Parent_Node := Parent (N);
4954 Parent_Kind := Nkind (Parent_Node);
4956 if Parent_Kind = N_Qualified_Expression then
4957 Parent_Node := Parent (Parent_Node);
4958 Parent_Kind := Nkind (Parent_Node);
4961 if Parent_Kind = N_Aggregate
4962 or else Parent_Kind = N_Extension_Aggregate
4963 or else Parent_Kind = N_Component_Association
4964 or else (Parent_Kind = N_Object_Declaration
4965 and then Needs_Finalization (Typ))
4966 or else (Parent_Kind = N_Assignment_Statement
4967 and then Inside_Init_Proc)
4969 if Static_Array_Aggregate (N)
4970 or else Compile_Time_Known_Aggregate (N)
4972 Set_Expansion_Delayed (N, False);
4975 Set_Expansion_Delayed (N);
4982 -- Look if in place aggregate expansion is possible.
4984 -- For object declarations we build the aggregate in place, unless
4985 -- the array is bit-packed or the component is controlled.
4987 -- For assignments we do the assignment in place if all the component
4988 -- associations have compile-time known values. For other cases we
4989 -- create a temporary. The analysis for safety of on-line assignment
4990 -- is delicate, i.e. we don't know how to do it fully yet ???
4992 -- For allocators we assign to the designated object in place if the
4993 -- aggregate meets the same conditions as other in-place assignments.
4994 -- In this case the aggregate may not come from source but was created
4995 -- for default initialization, e.g. with Initialize_Scalars.
4997 if Requires_Transient_Scope (Typ) then
4998 Establish_Transient_Scope
4999 (N, Sec_Stack => Has_Controlled_Component (Typ));
5002 if Has_Default_Init_Comps (N) then
5003 Maybe_In_Place_OK := False;
5005 elsif Is_Bit_Packed_Array (Typ)
5006 or else Has_Controlled_Component (Typ)
5008 Maybe_In_Place_OK := False;
5011 Maybe_In_Place_OK :=
5012 (Nkind (Parent (N)) = N_Assignment_Statement
5013 and then Comes_From_Source (N)
5014 and then In_Place_Assign_OK)
5017 (Nkind (Parent (Parent (N))) = N_Allocator
5018 and then In_Place_Assign_OK);
5021 -- If this is an array of tasks, it will be expanded into build-in-place
5022 -- assignments. Build an activation chain for the tasks now.
5024 if Has_Task (Etype (N)) then
5025 Build_Activation_Chain_Entity (N);
5028 if not Has_Default_Init_Comps (N)
5029 and then Comes_From_Source (Parent (N))
5030 and then Nkind (Parent (N)) = N_Object_Declaration
5032 Must_Slide (Etype (Defining_Identifier (Parent (N))), Typ)
5033 and then N = Expression (Parent (N))
5034 and then not Is_Bit_Packed_Array (Typ)
5035 and then not Has_Controlled_Component (Typ)
5036 and then not Has_Address_Clause (Parent (N))
5038 Tmp := Defining_Identifier (Parent (N));
5039 Set_No_Initialization (Parent (N));
5040 Set_Expression (Parent (N), Empty);
5042 -- Set the type of the entity, for use in the analysis of the
5043 -- subsequent indexed assignments. If the nominal type is not
5044 -- constrained, build a subtype from the known bounds of the
5045 -- aggregate. If the declaration has a subtype mark, use it,
5046 -- otherwise use the itype of the aggregate.
5048 if not Is_Constrained (Typ) then
5049 Build_Constrained_Type (Positional => False);
5050 elsif Is_Entity_Name (Object_Definition (Parent (N)))
5051 and then Is_Constrained (Entity (Object_Definition (Parent (N))))
5053 Set_Etype (Tmp, Entity (Object_Definition (Parent (N))));
5055 Set_Size_Known_At_Compile_Time (Typ, False);
5056 Set_Etype (Tmp, Typ);
5059 elsif Maybe_In_Place_OK
5060 and then Nkind (Parent (N)) = N_Qualified_Expression
5061 and then Nkind (Parent (Parent (N))) = N_Allocator
5063 Set_Expansion_Delayed (N);
5066 -- In the remaining cases the aggregate is the RHS of an assignment
5068 elsif Maybe_In_Place_OK
5069 and then Is_Entity_Name (Name (Parent (N)))
5071 Tmp := Entity (Name (Parent (N)));
5073 if Etype (Tmp) /= Etype (N) then
5074 Apply_Length_Check (N, Etype (Tmp));
5076 if Nkind (N) = N_Raise_Constraint_Error then
5078 -- Static error, nothing further to expand
5084 elsif Maybe_In_Place_OK
5085 and then Nkind (Name (Parent (N))) = N_Explicit_Dereference
5086 and then Is_Entity_Name (Prefix (Name (Parent (N))))
5088 Tmp := Name (Parent (N));
5090 if Etype (Tmp) /= Etype (N) then
5091 Apply_Length_Check (N, Etype (Tmp));
5094 elsif Maybe_In_Place_OK
5095 and then Nkind (Name (Parent (N))) = N_Slice
5096 and then Safe_Slice_Assignment (N)
5098 -- Safe_Slice_Assignment rewrites assignment as a loop
5104 -- In place aggregate expansion is not possible
5107 Maybe_In_Place_OK := False;
5108 Tmp := Make_Defining_Identifier (Loc, New_Internal_Name ('A'));
5110 Make_Object_Declaration
5112 Defining_Identifier => Tmp,
5113 Object_Definition => New_Occurrence_Of (Typ, Loc));
5114 Set_No_Initialization (Tmp_Decl, True);
5116 -- If we are within a loop, the temporary will be pushed on the
5117 -- stack at each iteration. If the aggregate is the expression for an
5118 -- allocator, it will be immediately copied to the heap and can
5119 -- be reclaimed at once. We create a transient scope around the
5120 -- aggregate for this purpose.
5122 if Ekind (Current_Scope) = E_Loop
5123 and then Nkind (Parent (Parent (N))) = N_Allocator
5125 Establish_Transient_Scope (N, False);
5128 Insert_Action (N, Tmp_Decl);
5131 -- Construct and insert the aggregate code. We can safely suppress index
5132 -- checks because this code is guaranteed not to raise CE on index
5133 -- checks. However we should *not* suppress all checks.
5139 if Nkind (Tmp) = N_Defining_Identifier then
5140 Target := New_Reference_To (Tmp, Loc);
5144 if Has_Default_Init_Comps (N) then
5146 -- Ada 2005 (AI-287): This case has not been analyzed???
5148 raise Program_Error;
5151 -- Name in assignment is explicit dereference
5153 Target := New_Copy (Tmp);
5157 Build_Array_Aggr_Code (N,
5159 Index => First_Index (Typ),
5161 Scalar_Comp => Is_Scalar_Type (Ctyp));
5164 if Comes_From_Source (Tmp) then
5165 Insert_Actions_After (Parent (N), Aggr_Code);
5168 Insert_Actions (N, Aggr_Code);
5171 -- If the aggregate has been assigned in place, remove the original
5174 if Nkind (Parent (N)) = N_Assignment_Statement
5175 and then Maybe_In_Place_OK
5177 Rewrite (Parent (N), Make_Null_Statement (Loc));
5179 elsif Nkind (Parent (N)) /= N_Object_Declaration
5180 or else Tmp /= Defining_Identifier (Parent (N))
5182 Rewrite (N, New_Occurrence_Of (Tmp, Loc));
5183 Analyze_And_Resolve (N, Typ);
5185 end Expand_Array_Aggregate;
5187 ------------------------
5188 -- Expand_N_Aggregate --
5189 ------------------------
5191 procedure Expand_N_Aggregate (N : Node_Id) is
5193 if Is_Record_Type (Etype (N)) then
5194 Expand_Record_Aggregate (N);
5196 Expand_Array_Aggregate (N);
5199 when RE_Not_Available =>
5201 end Expand_N_Aggregate;
5203 ----------------------------------
5204 -- Expand_N_Extension_Aggregate --
5205 ----------------------------------
5207 -- If the ancestor part is an expression, add a component association for
5208 -- the parent field. If the type of the ancestor part is not the direct
5209 -- parent of the expected type, build recursively the needed ancestors.
5210 -- If the ancestor part is a subtype_mark, replace aggregate with a decla-
5211 -- ration for a temporary of the expected type, followed by individual
5212 -- assignments to the given components.
5214 procedure Expand_N_Extension_Aggregate (N : Node_Id) is
5215 Loc : constant Source_Ptr := Sloc (N);
5216 A : constant Node_Id := Ancestor_Part (N);
5217 Typ : constant Entity_Id := Etype (N);
5220 -- If the ancestor is a subtype mark, an init proc must be called
5221 -- on the resulting object which thus has to be materialized in
5224 if Is_Entity_Name (A) and then Is_Type (Entity (A)) then
5225 Convert_To_Assignments (N, Typ);
5227 -- The extension aggregate is transformed into a record aggregate
5228 -- of the following form (c1 and c2 are inherited components)
5230 -- (Exp with c3 => a, c4 => b)
5231 -- ==> (c1 => Exp.c1, c2 => Exp.c2, c1 => a, c2 => b)
5236 if VM_Target = No_VM then
5237 Expand_Record_Aggregate (N,
5240 (Node (First_Elmt (Access_Disp_Table (Typ))), Loc),
5243 -- No tag is needed in the case of a VM
5244 Expand_Record_Aggregate (N,
5250 when RE_Not_Available =>
5252 end Expand_N_Extension_Aggregate;
5254 -----------------------------
5255 -- Expand_Record_Aggregate --
5256 -----------------------------
5258 procedure Expand_Record_Aggregate
5260 Orig_Tag : Node_Id := Empty;
5261 Parent_Expr : Node_Id := Empty)
5263 Loc : constant Source_Ptr := Sloc (N);
5264 Comps : constant List_Id := Component_Associations (N);
5265 Typ : constant Entity_Id := Etype (N);
5266 Base_Typ : constant Entity_Id := Base_Type (Typ);
5268 Static_Components : Boolean := True;
5269 -- Flag to indicate whether all components are compile-time known,
5270 -- and the aggregate can be constructed statically and handled by
5273 function Component_Not_OK_For_Backend return Boolean;
5274 -- Check for presence of component which makes it impossible for the
5275 -- backend to process the aggregate, thus requiring the use of a series
5276 -- of assignment statements. Cases checked for are a nested aggregate
5277 -- needing Late_Expansion, the presence of a tagged component which may
5278 -- need tag adjustment, and a bit unaligned component reference.
5280 -- We also force expansion into assignments if a component is of a
5281 -- mutable type (including a private type with discriminants) because
5282 -- in that case the size of the component to be copied may be smaller
5283 -- than the side of the target, and there is no simple way for gigi
5284 -- to compute the size of the object to be copied.
5286 -- NOTE: This is part of the ongoing work to define precisely the
5287 -- interface between front-end and back-end handling of aggregates.
5288 -- In general it is desirable to pass aggregates as they are to gigi,
5289 -- in order to minimize elaboration code. This is one case where the
5290 -- semantics of Ada complicate the analysis and lead to anomalies in
5291 -- the gcc back-end if the aggregate is not expanded into assignments.
5293 ----------------------------------
5294 -- Component_Not_OK_For_Backend --
5295 ----------------------------------
5297 function Component_Not_OK_For_Backend return Boolean is
5307 while Present (C) loop
5308 if Nkind (Expression (C)) = N_Qualified_Expression then
5309 Expr_Q := Expression (Expression (C));
5311 Expr_Q := Expression (C);
5314 -- Return true if the aggregate has any associations for tagged
5315 -- components that may require tag adjustment.
5317 -- These are cases where the source expression may have a tag that
5318 -- could differ from the component tag (e.g., can occur for type
5319 -- conversions and formal parameters). (Tag adjustment not needed
5320 -- if VM_Target because object tags are implicit in the machine.)
5322 if Is_Tagged_Type (Etype (Expr_Q))
5323 and then (Nkind (Expr_Q) = N_Type_Conversion
5324 or else (Is_Entity_Name (Expr_Q)
5326 Ekind (Entity (Expr_Q)) in Formal_Kind))
5327 and then VM_Target = No_VM
5329 Static_Components := False;
5332 elsif Is_Delayed_Aggregate (Expr_Q) then
5333 Static_Components := False;
5336 elsif Possible_Bit_Aligned_Component (Expr_Q) then
5337 Static_Components := False;
5341 if Is_Scalar_Type (Etype (Expr_Q)) then
5342 if not Compile_Time_Known_Value (Expr_Q) then
5343 Static_Components := False;
5346 elsif Nkind (Expr_Q) /= N_Aggregate
5347 or else not Compile_Time_Known_Aggregate (Expr_Q)
5349 Static_Components := False;
5351 if Is_Private_Type (Etype (Expr_Q))
5352 and then Has_Discriminants (Etype (Expr_Q))
5362 end Component_Not_OK_For_Backend;
5364 -- Remaining Expand_Record_Aggregate variables
5366 Tag_Value : Node_Id;
5370 -- Start of processing for Expand_Record_Aggregate
5373 -- If the aggregate is to be assigned to an atomic variable, we
5374 -- have to prevent a piecemeal assignment even if the aggregate
5375 -- is to be expanded. We create a temporary for the aggregate, and
5376 -- assign the temporary instead, so that the back end can generate
5377 -- an atomic move for it.
5380 and then (Nkind (Parent (N)) = N_Object_Declaration
5381 or else Nkind (Parent (N)) = N_Assignment_Statement)
5382 and then Comes_From_Source (Parent (N))
5384 Expand_Atomic_Aggregate (N, Typ);
5387 -- No special management required for aggregates used to initialize
5388 -- statically allocated dispatch tables
5390 elsif Is_Static_Dispatch_Table_Aggregate (N) then
5394 -- Ada 2005 (AI-318-2): We need to convert to assignments if components
5395 -- are build-in-place function calls. This test could be more specific,
5396 -- but doing it for all inherently limited aggregates seems harmless.
5397 -- The assignments will turn into build-in-place function calls (see
5398 -- Make_Build_In_Place_Call_In_Assignment).
5400 if Ada_Version >= Ada_05 and then Is_Inherently_Limited_Type (Typ) then
5401 Convert_To_Assignments (N, Typ);
5403 -- Gigi doesn't handle properly temporaries of variable size
5404 -- so we generate it in the front-end
5406 elsif not Size_Known_At_Compile_Time (Typ) then
5407 Convert_To_Assignments (N, Typ);
5409 -- Temporaries for controlled aggregates need to be attached to a
5410 -- final chain in order to be properly finalized, so it has to
5411 -- be created in the front-end
5413 elsif Is_Controlled (Typ)
5414 or else Has_Controlled_Component (Base_Type (Typ))
5416 Convert_To_Assignments (N, Typ);
5418 -- Ada 2005 (AI-287): In case of default initialized components we
5419 -- convert the aggregate into assignments.
5421 elsif Has_Default_Init_Comps (N) then
5422 Convert_To_Assignments (N, Typ);
5426 elsif Component_Not_OK_For_Backend then
5427 Convert_To_Assignments (N, Typ);
5429 -- If an ancestor is private, some components are not inherited and
5430 -- we cannot expand into a record aggregate
5432 elsif Has_Private_Ancestor (Typ) then
5433 Convert_To_Assignments (N, Typ);
5435 -- ??? The following was done to compile fxacc00.ads in the ACVCs. Gigi
5436 -- is not able to handle the aggregate for Late_Request.
5438 elsif Is_Tagged_Type (Typ) and then Has_Discriminants (Typ) then
5439 Convert_To_Assignments (N, Typ);
5441 -- If the tagged types covers interface types we need to initialize all
5442 -- hidden components containing pointers to secondary dispatch tables.
5444 elsif Is_Tagged_Type (Typ) and then Has_Interfaces (Typ) then
5445 Convert_To_Assignments (N, Typ);
5447 -- If some components are mutable, the size of the aggregate component
5448 -- may be distinct from the default size of the type component, so
5449 -- we need to expand to insure that the back-end copies the proper
5450 -- size of the data.
5452 elsif Has_Mutable_Components (Typ) then
5453 Convert_To_Assignments (N, Typ);
5455 -- If the type involved has any non-bit aligned components, then we are
5456 -- not sure that the back end can handle this case correctly.
5458 elsif Type_May_Have_Bit_Aligned_Components (Typ) then
5459 Convert_To_Assignments (N, Typ);
5461 -- In all other cases, build a proper aggregate handlable by gigi
5464 if Nkind (N) = N_Aggregate then
5466 -- If the aggregate is static and can be handled by the back-end,
5467 -- nothing left to do.
5469 if Static_Components then
5470 Set_Compile_Time_Known_Aggregate (N);
5471 Set_Expansion_Delayed (N, False);
5475 -- If no discriminants, nothing special to do
5477 if not Has_Discriminants (Typ) then
5480 -- Case of discriminants present
5482 elsif Is_Derived_Type (Typ) then
5484 -- For untagged types, non-stored discriminants are replaced
5485 -- with stored discriminants, which are the ones that gigi uses
5486 -- to describe the type and its components.
5488 Generate_Aggregate_For_Derived_Type : declare
5489 Constraints : constant List_Id := New_List;
5490 First_Comp : Node_Id;
5491 Discriminant : Entity_Id;
5493 Num_Disc : Int := 0;
5494 Num_Gird : Int := 0;
5496 procedure Prepend_Stored_Values (T : Entity_Id);
5497 -- Scan the list of stored discriminants of the type, and add
5498 -- their values to the aggregate being built.
5500 ---------------------------
5501 -- Prepend_Stored_Values --
5502 ---------------------------
5504 procedure Prepend_Stored_Values (T : Entity_Id) is
5506 Discriminant := First_Stored_Discriminant (T);
5507 while Present (Discriminant) loop
5509 Make_Component_Association (Loc,
5511 New_List (New_Occurrence_Of (Discriminant, Loc)),
5515 Get_Discriminant_Value (
5518 Discriminant_Constraint (Typ))));
5520 if No (First_Comp) then
5521 Prepend_To (Component_Associations (N), New_Comp);
5523 Insert_After (First_Comp, New_Comp);
5526 First_Comp := New_Comp;
5527 Next_Stored_Discriminant (Discriminant);
5529 end Prepend_Stored_Values;
5531 -- Start of processing for Generate_Aggregate_For_Derived_Type
5534 -- Remove the associations for the discriminant of derived type
5536 First_Comp := First (Component_Associations (N));
5537 while Present (First_Comp) loop
5542 (First (Choices (Comp)))) = E_Discriminant
5545 Num_Disc := Num_Disc + 1;
5549 -- Insert stored discriminant associations in the correct
5550 -- order. If there are more stored discriminants than new
5551 -- discriminants, there is at least one new discriminant that
5552 -- constrains more than one of the stored discriminants. In
5553 -- this case we need to construct a proper subtype of the
5554 -- parent type, in order to supply values to all the
5555 -- components. Otherwise there is one-one correspondence
5556 -- between the constraints and the stored discriminants.
5558 First_Comp := Empty;
5560 Discriminant := First_Stored_Discriminant (Base_Type (Typ));
5561 while Present (Discriminant) loop
5562 Num_Gird := Num_Gird + 1;
5563 Next_Stored_Discriminant (Discriminant);
5566 -- Case of more stored discriminants than new discriminants
5568 if Num_Gird > Num_Disc then
5570 -- Create a proper subtype of the parent type, which is the
5571 -- proper implementation type for the aggregate, and convert
5572 -- it to the intended target type.
5574 Discriminant := First_Stored_Discriminant (Base_Type (Typ));
5575 while Present (Discriminant) loop
5578 Get_Discriminant_Value (
5581 Discriminant_Constraint (Typ)));
5582 Append (New_Comp, Constraints);
5583 Next_Stored_Discriminant (Discriminant);
5587 Make_Subtype_Declaration (Loc,
5588 Defining_Identifier =>
5589 Make_Defining_Identifier (Loc,
5590 New_Internal_Name ('T')),
5591 Subtype_Indication =>
5592 Make_Subtype_Indication (Loc,
5594 New_Occurrence_Of (Etype (Base_Type (Typ)), Loc),
5596 Make_Index_Or_Discriminant_Constraint
5597 (Loc, Constraints)));
5599 Insert_Action (N, Decl);
5600 Prepend_Stored_Values (Base_Type (Typ));
5602 Set_Etype (N, Defining_Identifier (Decl));
5605 Rewrite (N, Unchecked_Convert_To (Typ, N));
5608 -- Case where we do not have fewer new discriminants than
5609 -- stored discriminants, so in this case we can simply use the
5610 -- stored discriminants of the subtype.
5613 Prepend_Stored_Values (Typ);
5615 end Generate_Aggregate_For_Derived_Type;
5618 if Is_Tagged_Type (Typ) then
5620 -- The tagged case, _parent and _tag component must be created
5622 -- Reset null_present unconditionally. tagged records always have
5623 -- at least one field (the tag or the parent)
5625 Set_Null_Record_Present (N, False);
5627 -- When the current aggregate comes from the expansion of an
5628 -- extension aggregate, the parent expr is replaced by an
5629 -- aggregate formed by selected components of this expr
5631 if Present (Parent_Expr)
5632 and then Is_Empty_List (Comps)
5634 Comp := First_Component_Or_Discriminant (Typ);
5635 while Present (Comp) loop
5637 -- Skip all expander-generated components
5640 not Comes_From_Source (Original_Record_Component (Comp))
5646 Make_Selected_Component (Loc,
5648 Unchecked_Convert_To (Typ,
5649 Duplicate_Subexpr (Parent_Expr, True)),
5651 Selector_Name => New_Occurrence_Of (Comp, Loc));
5654 Make_Component_Association (Loc,
5656 New_List (New_Occurrence_Of (Comp, Loc)),
5660 Analyze_And_Resolve (New_Comp, Etype (Comp));
5663 Next_Component_Or_Discriminant (Comp);
5667 -- Compute the value for the Tag now, if the type is a root it
5668 -- will be included in the aggregate right away, otherwise it will
5669 -- be propagated to the parent aggregate
5671 if Present (Orig_Tag) then
5672 Tag_Value := Orig_Tag;
5673 elsif VM_Target /= No_VM then
5678 (Node (First_Elmt (Access_Disp_Table (Typ))), Loc);
5681 -- For a derived type, an aggregate for the parent is formed with
5682 -- all the inherited components.
5684 if Is_Derived_Type (Typ) then
5687 First_Comp : Node_Id;
5688 Parent_Comps : List_Id;
5689 Parent_Aggr : Node_Id;
5690 Parent_Name : Node_Id;
5693 -- Remove the inherited component association from the
5694 -- aggregate and store them in the parent aggregate
5696 First_Comp := First (Component_Associations (N));
5697 Parent_Comps := New_List;
5698 while Present (First_Comp)
5699 and then Scope (Original_Record_Component (
5700 Entity (First (Choices (First_Comp))))) /= Base_Typ
5705 Append (Comp, Parent_Comps);
5708 Parent_Aggr := Make_Aggregate (Loc,
5709 Component_Associations => Parent_Comps);
5710 Set_Etype (Parent_Aggr, Etype (Base_Type (Typ)));
5712 -- Find the _parent component
5714 Comp := First_Component (Typ);
5715 while Chars (Comp) /= Name_uParent loop
5716 Comp := Next_Component (Comp);
5719 Parent_Name := New_Occurrence_Of (Comp, Loc);
5721 -- Insert the parent aggregate
5723 Prepend_To (Component_Associations (N),
5724 Make_Component_Association (Loc,
5725 Choices => New_List (Parent_Name),
5726 Expression => Parent_Aggr));
5728 -- Expand recursively the parent propagating the right Tag
5730 Expand_Record_Aggregate (
5731 Parent_Aggr, Tag_Value, Parent_Expr);
5734 -- For a root type, the tag component is added (unless compiling
5735 -- for the VMs, where tags are implicit).
5737 elsif VM_Target = No_VM then
5739 Tag_Name : constant Node_Id :=
5741 (First_Tag_Component (Typ), Loc);
5742 Typ_Tag : constant Entity_Id := RTE (RE_Tag);
5743 Conv_Node : constant Node_Id :=
5744 Unchecked_Convert_To (Typ_Tag, Tag_Value);
5747 Set_Etype (Conv_Node, Typ_Tag);
5748 Prepend_To (Component_Associations (N),
5749 Make_Component_Association (Loc,
5750 Choices => New_List (Tag_Name),
5751 Expression => Conv_Node));
5757 end Expand_Record_Aggregate;
5759 ----------------------------
5760 -- Has_Default_Init_Comps --
5761 ----------------------------
5763 function Has_Default_Init_Comps (N : Node_Id) return Boolean is
5764 Comps : constant List_Id := Component_Associations (N);
5768 pragma Assert (Nkind (N) = N_Aggregate
5769 or else Nkind (N) = N_Extension_Aggregate);
5775 if Has_Self_Reference (N) then
5779 -- Check if any direct component has default initialized components
5782 while Present (C) loop
5783 if Box_Present (C) then
5790 -- Recursive call in case of aggregate expression
5793 while Present (C) loop
5794 Expr := Expression (C);
5797 and then (Nkind (Expr) = N_Aggregate
5798 or else Nkind (Expr) = N_Extension_Aggregate)
5799 and then Has_Default_Init_Comps (Expr)
5808 end Has_Default_Init_Comps;
5810 --------------------------
5811 -- Is_Delayed_Aggregate --
5812 --------------------------
5814 function Is_Delayed_Aggregate (N : Node_Id) return Boolean is
5815 Node : Node_Id := N;
5816 Kind : Node_Kind := Nkind (Node);
5819 if Kind = N_Qualified_Expression then
5820 Node := Expression (Node);
5821 Kind := Nkind (Node);
5824 if Kind /= N_Aggregate and then Kind /= N_Extension_Aggregate then
5827 return Expansion_Delayed (Node);
5829 end Is_Delayed_Aggregate;
5831 ----------------------------------------
5832 -- Is_Static_Dispatch_Table_Aggregate --
5833 ----------------------------------------
5835 function Is_Static_Dispatch_Table_Aggregate (N : Node_Id) return Boolean is
5836 Typ : constant Entity_Id := Base_Type (Etype (N));
5839 return Static_Dispatch_Tables
5840 and then VM_Target = No_VM
5841 and then RTU_Loaded (Ada_Tags)
5843 -- Avoid circularity when rebuilding the compiler
5845 and then Cunit_Entity (Get_Source_Unit (N)) /= RTU_Entity (Ada_Tags)
5846 and then (Typ = RTE (RE_Dispatch_Table_Wrapper)
5848 Typ = RTE (RE_Address_Array)
5850 Typ = RTE (RE_Type_Specific_Data)
5852 Typ = RTE (RE_Tag_Table)
5854 (RTE_Available (RE_Interface_Data)
5855 and then Typ = RTE (RE_Interface_Data))
5857 (RTE_Available (RE_Interfaces_Array)
5858 and then Typ = RTE (RE_Interfaces_Array))
5860 (RTE_Available (RE_Interface_Data_Element)
5861 and then Typ = RTE (RE_Interface_Data_Element)));
5862 end Is_Static_Dispatch_Table_Aggregate;
5864 --------------------
5865 -- Late_Expansion --
5866 --------------------
5868 function Late_Expansion
5872 Flist : Node_Id := Empty;
5873 Obj : Entity_Id := Empty) return List_Id
5876 if Is_Record_Type (Etype (N)) then
5877 return Build_Record_Aggr_Code (N, Typ, Target, Flist, Obj);
5879 else pragma Assert (Is_Array_Type (Etype (N)));
5881 Build_Array_Aggr_Code
5883 Ctype => Component_Type (Etype (N)),
5884 Index => First_Index (Typ),
5886 Scalar_Comp => Is_Scalar_Type (Component_Type (Typ)),
5892 ----------------------------------
5893 -- Make_OK_Assignment_Statement --
5894 ----------------------------------
5896 function Make_OK_Assignment_Statement
5899 Expression : Node_Id) return Node_Id
5902 Set_Assignment_OK (Name);
5904 return Make_Assignment_Statement (Sloc, Name, Expression);
5905 end Make_OK_Assignment_Statement;
5907 -----------------------
5908 -- Number_Of_Choices --
5909 -----------------------
5911 function Number_Of_Choices (N : Node_Id) return Nat is
5915 Nb_Choices : Nat := 0;
5918 if Present (Expressions (N)) then
5922 Assoc := First (Component_Associations (N));
5923 while Present (Assoc) loop
5924 Choice := First (Choices (Assoc));
5925 while Present (Choice) loop
5926 if Nkind (Choice) /= N_Others_Choice then
5927 Nb_Choices := Nb_Choices + 1;
5937 end Number_Of_Choices;
5939 ------------------------------------
5940 -- Packed_Array_Aggregate_Handled --
5941 ------------------------------------
5943 -- The current version of this procedure will handle at compile time
5944 -- any array aggregate that meets these conditions:
5946 -- One dimensional, bit packed
5947 -- Underlying packed type is modular type
5948 -- Bounds are within 32-bit Int range
5949 -- All bounds and values are static
5951 function Packed_Array_Aggregate_Handled (N : Node_Id) return Boolean is
5952 Loc : constant Source_Ptr := Sloc (N);
5953 Typ : constant Entity_Id := Etype (N);
5954 Ctyp : constant Entity_Id := Component_Type (Typ);
5956 Not_Handled : exception;
5957 -- Exception raised if this aggregate cannot be handled
5960 -- For now, handle only one dimensional bit packed arrays
5962 if not Is_Bit_Packed_Array (Typ)
5963 or else Number_Dimensions (Typ) > 1
5964 or else not Is_Modular_Integer_Type (Packed_Array_Type (Typ))
5969 if not Is_Scalar_Type (Component_Type (Typ))
5970 and then Has_Non_Standard_Rep (Component_Type (Typ))
5976 Csiz : constant Nat := UI_To_Int (Component_Size (Typ));
5980 -- Bounds of index type
5984 -- Values of bounds if compile time known
5986 function Get_Component_Val (N : Node_Id) return Uint;
5987 -- Given a expression value N of the component type Ctyp, returns a
5988 -- value of Csiz (component size) bits representing this value. If
5989 -- the value is non-static or any other reason exists why the value
5990 -- cannot be returned, then Not_Handled is raised.
5992 -----------------------
5993 -- Get_Component_Val --
5994 -----------------------
5996 function Get_Component_Val (N : Node_Id) return Uint is
6000 -- We have to analyze the expression here before doing any further
6001 -- processing here. The analysis of such expressions is deferred
6002 -- till expansion to prevent some problems of premature analysis.
6004 Analyze_And_Resolve (N, Ctyp);
6006 -- Must have a compile time value. String literals have to be
6007 -- converted into temporaries as well, because they cannot easily
6008 -- be converted into their bit representation.
6010 if not Compile_Time_Known_Value (N)
6011 or else Nkind (N) = N_String_Literal
6016 Val := Expr_Rep_Value (N);
6018 -- Adjust for bias, and strip proper number of bits
6020 if Has_Biased_Representation (Ctyp) then
6021 Val := Val - Expr_Value (Type_Low_Bound (Ctyp));
6024 return Val mod Uint_2 ** Csiz;
6025 end Get_Component_Val;
6027 -- Here we know we have a one dimensional bit packed array
6030 Get_Index_Bounds (First_Index (Typ), Lo, Hi);
6032 -- Cannot do anything if bounds are dynamic
6034 if not Compile_Time_Known_Value (Lo)
6036 not Compile_Time_Known_Value (Hi)
6041 -- Or are silly out of range of int bounds
6043 Lob := Expr_Value (Lo);
6044 Hib := Expr_Value (Hi);
6046 if not UI_Is_In_Int_Range (Lob)
6048 not UI_Is_In_Int_Range (Hib)
6053 -- At this stage we have a suitable aggregate for handling at compile
6054 -- time (the only remaining checks are that the values of expressions
6055 -- in the aggregate are compile time known (check is performed by
6056 -- Get_Component_Val), and that any subtypes or ranges are statically
6059 -- If the aggregate is not fully positional at this stage, then
6060 -- convert it to positional form. Either this will fail, in which
6061 -- case we can do nothing, or it will succeed, in which case we have
6062 -- succeeded in handling the aggregate, or it will stay an aggregate,
6063 -- in which case we have failed to handle this case.
6065 if Present (Component_Associations (N)) then
6066 Convert_To_Positional
6067 (N, Max_Others_Replicate => 64, Handle_Bit_Packed => True);
6068 return Nkind (N) /= N_Aggregate;
6071 -- Otherwise we are all positional, so convert to proper value
6074 Lov : constant Int := UI_To_Int (Lob);
6075 Hiv : constant Int := UI_To_Int (Hib);
6077 Len : constant Nat := Int'Max (0, Hiv - Lov + 1);
6078 -- The length of the array (number of elements)
6080 Aggregate_Val : Uint;
6081 -- Value of aggregate. The value is set in the low order bits of
6082 -- this value. For the little-endian case, the values are stored
6083 -- from low-order to high-order and for the big-endian case the
6084 -- values are stored from high-order to low-order. Note that gigi
6085 -- will take care of the conversions to left justify the value in
6086 -- the big endian case (because of left justified modular type
6087 -- processing), so we do not have to worry about that here.
6090 -- Integer literal for resulting constructed value
6093 -- Shift count from low order for next value
6096 -- Shift increment for loop
6099 -- Next expression from positional parameters of aggregate
6102 -- For little endian, we fill up the low order bits of the target
6103 -- value. For big endian we fill up the high order bits of the
6104 -- target value (which is a left justified modular value).
6106 if Bytes_Big_Endian xor Debug_Flag_8 then
6107 Shift := Csiz * (Len - 1);
6114 -- Loop to set the values
6117 Aggregate_Val := Uint_0;
6119 Expr := First (Expressions (N));
6120 Aggregate_Val := Get_Component_Val (Expr) * Uint_2 ** Shift;
6122 for J in 2 .. Len loop
6123 Shift := Shift + Incr;
6126 Aggregate_Val + Get_Component_Val (Expr) * Uint_2 ** Shift;
6130 -- Now we can rewrite with the proper value
6133 Make_Integer_Literal (Loc,
6134 Intval => Aggregate_Val);
6135 Set_Print_In_Hex (Lit);
6137 -- Construct the expression using this literal. Note that it is
6138 -- important to qualify the literal with its proper modular type
6139 -- since universal integer does not have the required range and
6140 -- also this is a left justified modular type, which is important
6141 -- in the big-endian case.
6144 Unchecked_Convert_To (Typ,
6145 Make_Qualified_Expression (Loc,
6147 New_Occurrence_Of (Packed_Array_Type (Typ), Loc),
6148 Expression => Lit)));
6150 Analyze_And_Resolve (N, Typ);
6158 end Packed_Array_Aggregate_Handled;
6160 ----------------------------
6161 -- Has_Mutable_Components --
6162 ----------------------------
6164 function Has_Mutable_Components (Typ : Entity_Id) return Boolean is
6168 Comp := First_Component (Typ);
6169 while Present (Comp) loop
6170 if Is_Record_Type (Etype (Comp))
6171 and then Has_Discriminants (Etype (Comp))
6172 and then not Is_Constrained (Etype (Comp))
6177 Next_Component (Comp);
6181 end Has_Mutable_Components;
6183 ------------------------------
6184 -- Initialize_Discriminants --
6185 ------------------------------
6187 procedure Initialize_Discriminants (N : Node_Id; Typ : Entity_Id) is
6188 Loc : constant Source_Ptr := Sloc (N);
6189 Bas : constant Entity_Id := Base_Type (Typ);
6190 Par : constant Entity_Id := Etype (Bas);
6191 Decl : constant Node_Id := Parent (Par);
6195 if Is_Tagged_Type (Bas)
6196 and then Is_Derived_Type (Bas)
6197 and then Has_Discriminants (Par)
6198 and then Has_Discriminants (Bas)
6199 and then Number_Discriminants (Bas) /= Number_Discriminants (Par)
6200 and then Nkind (Decl) = N_Full_Type_Declaration
6201 and then Nkind (Type_Definition (Decl)) = N_Record_Definition
6203 (Variant_Part (Component_List (Type_Definition (Decl))))
6204 and then Nkind (N) /= N_Extension_Aggregate
6207 -- Call init proc to set discriminants.
6208 -- There should eventually be a special procedure for this ???
6210 Ref := New_Reference_To (Defining_Identifier (N), Loc);
6211 Insert_Actions_After (N,
6212 Build_Initialization_Call (Sloc (N), Ref, Typ));
6214 end Initialize_Discriminants;
6221 (Obj_Type : Entity_Id;
6222 Typ : Entity_Id) return Boolean
6224 L1, L2, H1, H2 : Node_Id;
6226 -- No sliding if the type of the object is not established yet, if it is
6227 -- an unconstrained type whose actual subtype comes from the aggregate,
6228 -- or if the two types are identical.
6230 if not Is_Array_Type (Obj_Type) then
6233 elsif not Is_Constrained (Obj_Type) then
6236 elsif Typ = Obj_Type then
6240 -- Sliding can only occur along the first dimension
6242 Get_Index_Bounds (First_Index (Typ), L1, H1);
6243 Get_Index_Bounds (First_Index (Obj_Type), L2, H2);
6245 if not Is_Static_Expression (L1)
6246 or else not Is_Static_Expression (L2)
6247 or else not Is_Static_Expression (H1)
6248 or else not Is_Static_Expression (H2)
6252 return Expr_Value (L1) /= Expr_Value (L2)
6253 or else Expr_Value (H1) /= Expr_Value (H2);
6258 ---------------------------
6259 -- Safe_Slice_Assignment --
6260 ---------------------------
6262 function Safe_Slice_Assignment (N : Node_Id) return Boolean is
6263 Loc : constant Source_Ptr := Sloc (Parent (N));
6264 Pref : constant Node_Id := Prefix (Name (Parent (N)));
6265 Range_Node : constant Node_Id := Discrete_Range (Name (Parent (N)));
6273 -- Generate: for J in Range loop Pref (J) := Expr; end loop;
6275 if Comes_From_Source (N)
6276 and then No (Expressions (N))
6277 and then Nkind (First (Choices (First (Component_Associations (N)))))
6281 Expression (First (Component_Associations (N)));
6282 L_J := Make_Defining_Identifier (Loc, New_Internal_Name ('J'));
6285 Make_Iteration_Scheme (Loc,
6286 Loop_Parameter_Specification =>
6287 Make_Loop_Parameter_Specification
6289 Defining_Identifier => L_J,
6290 Discrete_Subtype_Definition => Relocate_Node (Range_Node)));
6293 Make_Assignment_Statement (Loc,
6295 Make_Indexed_Component (Loc,
6296 Prefix => Relocate_Node (Pref),
6297 Expressions => New_List (New_Occurrence_Of (L_J, Loc))),
6298 Expression => Relocate_Node (Expr));
6300 -- Construct the final loop
6303 Make_Implicit_Loop_Statement
6304 (Node => Parent (N),
6305 Identifier => Empty,
6306 Iteration_Scheme => L_Iter,
6307 Statements => New_List (L_Body));
6309 -- Set type of aggregate to be type of lhs in assignment,
6310 -- to suppress redundant length checks.
6312 Set_Etype (N, Etype (Name (Parent (N))));
6314 Rewrite (Parent (N), Stat);
6315 Analyze (Parent (N));
6321 end Safe_Slice_Assignment;
6323 ---------------------
6324 -- Sort_Case_Table --
6325 ---------------------
6327 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type) is
6328 L : constant Int := Case_Table'First;
6329 U : constant Int := Case_Table'Last;
6337 T := Case_Table (K + 1);
6341 and then Expr_Value (Case_Table (J - 1).Choice_Lo) >
6342 Expr_Value (T.Choice_Lo)
6344 Case_Table (J) := Case_Table (J - 1);
6348 Case_Table (J) := T;
6351 end Sort_Case_Table;
6353 ----------------------------
6354 -- Static_Array_Aggregate --
6355 ----------------------------
6357 function Static_Array_Aggregate (N : Node_Id) return Boolean is
6358 Bounds : constant Node_Id := Aggregate_Bounds (N);
6360 Typ : constant Entity_Id := Etype (N);
6361 Comp_Type : constant Entity_Id := Component_Type (Typ);
6368 if Is_Tagged_Type (Typ)
6369 or else Is_Controlled (Typ)
6370 or else Is_Packed (Typ)
6376 and then Nkind (Bounds) = N_Range
6377 and then Nkind (Low_Bound (Bounds)) = N_Integer_Literal
6378 and then Nkind (High_Bound (Bounds)) = N_Integer_Literal
6380 Lo := Low_Bound (Bounds);
6381 Hi := High_Bound (Bounds);
6383 if No (Component_Associations (N)) then
6385 -- Verify that all components are static integers
6387 Expr := First (Expressions (N));
6388 while Present (Expr) loop
6389 if Nkind (Expr) /= N_Integer_Literal then
6399 -- We allow only a single named association, either a static
6400 -- range or an others_clause, with a static expression.
6402 Expr := First (Component_Associations (N));
6404 if Present (Expressions (N)) then
6407 elsif Present (Next (Expr)) then
6410 elsif Present (Next (First (Choices (Expr)))) then
6414 -- The aggregate is static if all components are literals, or
6415 -- else all its components are static aggregates for the
6416 -- component type. We also limit the size of a static aggregate
6417 -- to prevent runaway static expressions.
6419 if Is_Array_Type (Comp_Type)
6420 or else Is_Record_Type (Comp_Type)
6422 if Nkind (Expression (Expr)) /= N_Aggregate
6424 not Compile_Time_Known_Aggregate (Expression (Expr))
6429 elsif Nkind (Expression (Expr)) /= N_Integer_Literal then
6432 elsif not Aggr_Size_OK (N, Typ) then
6436 -- Create a positional aggregate with the right number of
6437 -- copies of the expression.
6439 Agg := Make_Aggregate (Sloc (N), New_List, No_List);
6441 for I in UI_To_Int (Intval (Lo)) .. UI_To_Int (Intval (Hi))
6444 (Expressions (Agg), New_Copy (Expression (Expr)));
6446 -- The copied expression must be analyzed and resolved.
6447 -- Besides setting the type, this ensures that static
6448 -- expressions are appropriately marked as such.
6451 (Last (Expressions (Agg)), Component_Type (Typ));
6454 Set_Aggregate_Bounds (Agg, Bounds);
6455 Set_Etype (Agg, Typ);
6458 Set_Compile_Time_Known_Aggregate (N);
6467 end Static_Array_Aggregate;