1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2010, 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_Disp; use Exp_Disp;
38 with Exp_Tss; use Exp_Tss;
39 with Fname; use Fname;
40 with Freeze; use Freeze;
41 with Itypes; use Itypes;
43 with Namet; use Namet;
44 with Nmake; use Nmake;
45 with Nlists; use Nlists;
47 with Restrict; use Restrict;
48 with Rident; use Rident;
49 with Rtsfind; use Rtsfind;
50 with Ttypes; use Ttypes;
52 with Sem_Aux; use Sem_Aux;
53 with Sem_Ch3; use Sem_Ch3;
54 with Sem_Eval; use Sem_Eval;
55 with Sem_Res; use Sem_Res;
56 with Sem_Util; use Sem_Util;
57 with Sinfo; use Sinfo;
58 with Snames; use Snames;
59 with Stand; use Stand;
60 with Targparm; use Targparm;
61 with Tbuild; use Tbuild;
62 with Uintp; use Uintp;
64 package body Exp_Aggr is
66 type Case_Bounds is record
69 Choice_Node : Node_Id;
72 type Case_Table_Type is array (Nat range <>) of Case_Bounds;
73 -- Table type used by Check_Case_Choices procedure
76 (Obj_Type : Entity_Id;
77 Typ : Entity_Id) return Boolean;
78 -- A static array aggregate in an object declaration can in most cases be
79 -- expanded in place. The one exception is when the aggregate is given
80 -- with component associations that specify different bounds from those of
81 -- the type definition in the object declaration. In this pathological
82 -- case the aggregate must slide, and we must introduce an intermediate
83 -- temporary to hold it.
85 -- The same holds in an assignment to one-dimensional array of arrays,
86 -- when a component may be given with bounds that differ from those of the
89 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type);
90 -- Sort the Case Table using the Lower Bound of each Choice as the key.
91 -- A simple insertion sort is used since the number of choices in a case
92 -- statement of variant part will usually be small and probably in near
95 function Has_Default_Init_Comps (N : Node_Id) return Boolean;
96 -- N is an aggregate (record or array). Checks the presence of default
97 -- initialization (<>) in any component (Ada 2005: AI-287).
99 function Is_Static_Dispatch_Table_Aggregate (N : Node_Id) return Boolean;
100 -- Returns true if N is an aggregate used to initialize the components
101 -- of an statically allocated dispatch table.
103 ------------------------------------------------------
104 -- Local subprograms for Record Aggregate Expansion --
105 ------------------------------------------------------
107 procedure Expand_Record_Aggregate
109 Orig_Tag : Node_Id := Empty;
110 Parent_Expr : Node_Id := Empty);
111 -- This is the top level procedure for record aggregate expansion.
112 -- Expansion for record aggregates needs expand aggregates for tagged
113 -- record types. Specifically Expand_Record_Aggregate adds the Tag
114 -- field in front of the Component_Association list that was created
115 -- during resolution by Resolve_Record_Aggregate.
117 -- N is the record aggregate node.
118 -- Orig_Tag is the value of the Tag that has to be provided for this
119 -- specific aggregate. It carries the tag corresponding to the type
120 -- of the outermost aggregate during the recursive expansion
121 -- Parent_Expr is the ancestor part of the original extension
124 procedure Convert_To_Assignments (N : Node_Id; Typ : Entity_Id);
125 -- N is an N_Aggregate or an N_Extension_Aggregate. Typ is the type of the
126 -- aggregate (which can only be a record type, this procedure is only used
127 -- for record types). Transform the given aggregate into a sequence of
128 -- assignments performed component by component.
130 function Build_Record_Aggr_Code
134 Flist : Node_Id := Empty;
135 Obj : Entity_Id := Empty;
136 Is_Limited_Ancestor_Expansion : Boolean := False) return List_Id;
137 -- N is an N_Aggregate or an N_Extension_Aggregate. Typ is the type of the
138 -- aggregate. Target is an expression containing the location on which the
139 -- component by component assignments will take place. Returns the list of
140 -- assignments plus all other adjustments needed for tagged and controlled
141 -- types. Flist is an expression representing the finalization list on
142 -- which to attach the controlled components if any. Obj is present in the
143 -- object declaration and dynamic allocation cases, it contains an entity
144 -- that allows to know if the value being created needs to be attached to
145 -- the final list in case of pragma Finalize_Storage_Only.
148 -- The meaning of the Obj formal is extremely unclear. *What* entity
149 -- should be passed? For the object declaration case we may guess that
150 -- this is the object being declared, but what about the allocator case?
152 -- Is_Limited_Ancestor_Expansion indicates that the function has been
153 -- called recursively to expand the limited ancestor to avoid copying it.
155 function Has_Mutable_Components (Typ : Entity_Id) return Boolean;
156 -- Return true if one of the component is of a discriminated type with
157 -- defaults. An aggregate for a type with mutable components must be
158 -- expanded into individual assignments.
160 procedure Initialize_Discriminants (N : Node_Id; Typ : Entity_Id);
161 -- If the type of the aggregate is a type extension with renamed discrimi-
162 -- nants, we must initialize the hidden discriminants of the parent.
163 -- Otherwise, the target object must not be initialized. The discriminants
164 -- are initialized by calling the initialization procedure for the type.
165 -- This is incorrect if the initialization of other components has any
166 -- side effects. We restrict this call to the case where the parent type
167 -- has a variant part, because this is the only case where the hidden
168 -- discriminants are accessed, namely when calling discriminant checking
169 -- functions of the parent type, and when applying a stream attribute to
170 -- an object of the derived type.
172 -----------------------------------------------------
173 -- Local Subprograms for Array Aggregate Expansion --
174 -----------------------------------------------------
176 function Aggr_Size_OK (N : Node_Id; Typ : Entity_Id) return Boolean;
177 -- Very large static aggregates present problems to the back-end, and are
178 -- transformed into assignments and loops. This function verifies that the
179 -- total number of components of an aggregate is acceptable for rewriting
180 -- into a purely positional static form. Aggr_Size_OK must be called before
183 -- This function also detects and warns about one-component aggregates that
184 -- appear in a non-static context. Even if the component value is static,
185 -- such an aggregate must be expanded into an assignment.
187 procedure Convert_Array_Aggr_In_Allocator
191 -- If the aggregate appears within an allocator and can be expanded in
192 -- place, this routine generates the individual assignments to components
193 -- of the designated object. This is an optimization over the general
194 -- case, where a temporary is first created on the stack and then used to
195 -- construct the allocated object on the heap.
197 procedure Convert_To_Positional
199 Max_Others_Replicate : Nat := 5;
200 Handle_Bit_Packed : Boolean := False);
201 -- If possible, convert named notation to positional notation. This
202 -- conversion is possible only in some static cases. If the conversion is
203 -- possible, then N is rewritten with the analyzed converted aggregate.
204 -- The parameter Max_Others_Replicate controls the maximum number of
205 -- values corresponding to an others choice that will be converted to
206 -- positional notation (the default of 5 is the normal limit, and reflects
207 -- the fact that normally the loop is better than a lot of separate
208 -- assignments). Note that this limit gets overridden in any case if
209 -- either of the restrictions No_Elaboration_Code or No_Implicit_Loops is
210 -- set. The parameter Handle_Bit_Packed is usually set False (since we do
211 -- not expect the back end to handle bit packed arrays, so the normal case
212 -- of conversion is pointless), but in the special case of a call from
213 -- Packed_Array_Aggregate_Handled, we set this parameter to True, since
214 -- these are cases we handle in there.
216 procedure Expand_Array_Aggregate (N : Node_Id);
217 -- This is the top-level routine to perform array aggregate expansion.
218 -- N is the N_Aggregate node to be expanded.
220 function Backend_Processing_Possible (N : Node_Id) return Boolean;
221 -- This function checks if array aggregate N can be processed directly
222 -- by the backend. If this is the case True is returned.
224 function Build_Array_Aggr_Code
229 Scalar_Comp : Boolean;
230 Indices : List_Id := No_List;
231 Flist : Node_Id := Empty) return List_Id;
232 -- This recursive routine returns a list of statements containing the
233 -- loops and assignments that are needed for the expansion of the array
236 -- N is the (sub-)aggregate node to be expanded into code. This node
237 -- has been fully analyzed, and its Etype is properly set.
239 -- Index is the index node corresponding to the array sub-aggregate N.
241 -- Into is the target expression into which we are copying the aggregate.
242 -- Note that this node may not have been analyzed yet, and so the Etype
243 -- field may not be set.
245 -- Scalar_Comp is True if the component type of the aggregate is scalar.
247 -- Indices is the current list of expressions used to index the
248 -- object we are writing into.
250 -- Flist is an expression representing the finalization list on which
251 -- to attach the controlled components if any.
253 function Number_Of_Choices (N : Node_Id) return Nat;
254 -- Returns the number of discrete choices (not including the others choice
255 -- if present) contained in (sub-)aggregate N.
257 function Late_Expansion
261 Flist : Node_Id := Empty;
262 Obj : Entity_Id := Empty) return List_Id;
263 -- N is a nested (record or array) aggregate that has been marked with
264 -- 'Delay_Expansion'. Typ is the expected type of the aggregate and Target
265 -- is a (duplicable) expression that will hold the result of the aggregate
266 -- expansion. Flist is the finalization list to be used to attach
267 -- controlled components. 'Obj' when non empty, carries the original
268 -- object being initialized in order to know if it needs to be attached to
269 -- the previous parameter which may not be the case in the case where
270 -- Finalize_Storage_Only is set. Basically this procedure is used to
271 -- implement top-down expansions of nested aggregates. This is necessary
272 -- for avoiding temporaries at each level as well as for propagating the
273 -- right internal finalization list.
275 function Make_OK_Assignment_Statement
278 Expression : Node_Id) return Node_Id;
279 -- This is like Make_Assignment_Statement, except that Assignment_OK
280 -- is set in the left operand. All assignments built by this unit
281 -- use this routine. This is needed to deal with assignments to
282 -- initialized constants that are done in place.
284 function Packed_Array_Aggregate_Handled (N : Node_Id) return Boolean;
285 -- Given an array aggregate, this function handles the case of a packed
286 -- array aggregate with all constant values, where the aggregate can be
287 -- evaluated at compile time. If this is possible, then N is rewritten
288 -- to be its proper compile time value with all the components properly
289 -- assembled. The expression is analyzed and resolved and True is
290 -- returned. If this transformation is not possible, N is unchanged
291 -- and False is returned
293 function Safe_Slice_Assignment (N : Node_Id) return Boolean;
294 -- If a slice assignment has an aggregate with a single others_choice,
295 -- the assignment can be done in place even if bounds are not static,
296 -- by converting it into a loop over the discrete range of the slice.
302 function Aggr_Size_OK (N : Node_Id; Typ : Entity_Id) return Boolean is
310 -- The following constant determines the maximum size of an
311 -- array aggregate produced by converting named to positional
312 -- notation (e.g. from others clauses). This avoids running
313 -- away with attempts to convert huge aggregates, which hit
314 -- memory limits in the backend.
316 -- The normal limit is 5000, but we increase this limit to
317 -- 2**24 (about 16 million) if Restrictions (No_Elaboration_Code)
318 -- or Restrictions (No_Implicit_Loops) is specified, since in
319 -- either case, we are at risk of declaring the program illegal
320 -- because of this limit.
322 Max_Aggr_Size : constant Nat :=
323 5000 + (2 ** 24 - 5000) *
325 (Restriction_Active (No_Elaboration_Code)
327 Restriction_Active (No_Implicit_Loops));
329 function Component_Count (T : Entity_Id) return Int;
330 -- The limit is applied to the total number of components that the
331 -- aggregate will have, which is the number of static expressions
332 -- that will appear in the flattened array. This requires a recursive
333 -- computation of the number of scalar components of the structure.
335 ---------------------
336 -- Component_Count --
337 ---------------------
339 function Component_Count (T : Entity_Id) return Int is
344 if Is_Scalar_Type (T) then
347 elsif Is_Record_Type (T) then
348 Comp := First_Component (T);
349 while Present (Comp) loop
350 Res := Res + Component_Count (Etype (Comp));
351 Next_Component (Comp);
356 elsif Is_Array_Type (T) then
358 Lo : constant Node_Id :=
359 Type_Low_Bound (Etype (First_Index (T)));
360 Hi : constant Node_Id :=
361 Type_High_Bound (Etype (First_Index (T)));
363 Siz : constant Int := Component_Count (Component_Type (T));
366 if not Compile_Time_Known_Value (Lo)
367 or else not Compile_Time_Known_Value (Hi)
372 Siz * UI_To_Int (Expr_Value (Hi) - Expr_Value (Lo) + 1);
377 -- Can only be a null for an access type
383 -- Start of processing for Aggr_Size_OK
386 Siz := Component_Count (Component_Type (Typ));
388 Indx := First_Index (Typ);
389 while Present (Indx) loop
390 Lo := Type_Low_Bound (Etype (Indx));
391 Hi := Type_High_Bound (Etype (Indx));
393 -- Bounds need to be known at compile time
395 if not Compile_Time_Known_Value (Lo)
396 or else not Compile_Time_Known_Value (Hi)
401 Lov := Expr_Value (Lo);
402 Hiv := Expr_Value (Hi);
404 -- A flat array is always safe
410 -- One-component aggregates are suspicious, and if the context type
411 -- is an object declaration with non-static bounds it will trip gcc;
412 -- such an aggregate must be expanded into a single assignment.
415 and then Nkind (Parent (N)) = N_Object_Declaration
418 Index_Type : constant Entity_Id :=
421 (Etype (Defining_Identifier (Parent (N)))));
425 if not Compile_Time_Known_Value (Type_Low_Bound (Index_Type))
426 or else not Compile_Time_Known_Value
427 (Type_High_Bound (Index_Type))
429 if Present (Component_Associations (N)) then
431 First (Choices (First (Component_Associations (N))));
432 if Is_Entity_Name (Indx)
433 and then not Is_Type (Entity (Indx))
436 ("single component aggregate in non-static context?",
438 Error_Msg_N ("\maybe subtype name was meant?", Indx);
448 Rng : constant Uint := Hiv - Lov + 1;
451 -- Check if size is too large
453 if not UI_Is_In_Int_Range (Rng) then
457 Siz := Siz * UI_To_Int (Rng);
461 or else Siz > Max_Aggr_Size
466 -- Bounds must be in integer range, for later array construction
468 if not UI_Is_In_Int_Range (Lov)
470 not UI_Is_In_Int_Range (Hiv)
481 ---------------------------------
482 -- Backend_Processing_Possible --
483 ---------------------------------
485 -- Backend processing by Gigi/gcc is possible only if all the following
486 -- conditions are met:
488 -- 1. N is fully positional
490 -- 2. N is not a bit-packed array aggregate;
492 -- 3. The size of N's array type must be known at compile time. Note
493 -- that this implies that the component size is also known
495 -- 4. The array type of N does not follow the Fortran layout convention
496 -- or if it does it must be 1 dimensional.
498 -- 5. The array component type may not be tagged (which could necessitate
499 -- reassignment of proper tags).
501 -- 6. The array component type must not have unaligned bit components
503 -- 7. None of the components of the aggregate may be bit unaligned
506 -- 8. There cannot be delayed components, since we do not know enough
507 -- at this stage to know if back end processing is possible.
509 -- 9. There cannot be any discriminated record components, since the
510 -- back end cannot handle this complex case.
512 -- 10. No controlled actions need to be generated for components
514 -- 11. For a VM back end, the array should have no aliased components
516 function Backend_Processing_Possible (N : Node_Id) return Boolean is
517 Typ : constant Entity_Id := Etype (N);
518 -- Typ is the correct constrained array subtype of the aggregate
520 function Component_Check (N : Node_Id; Index : Node_Id) return Boolean;
521 -- This routine checks components of aggregate N, enforcing checks
522 -- 1, 7, 8, and 9. In the multi-dimensional case, these checks are
523 -- performed on subaggregates. The Index value is the current index
524 -- being checked in the multi-dimensional case.
526 ---------------------
527 -- Component_Check --
528 ---------------------
530 function Component_Check (N : Node_Id; Index : Node_Id) return Boolean is
534 -- Checks 1: (no component associations)
536 if Present (Component_Associations (N)) then
540 -- Checks on components
542 -- Recurse to check subaggregates, which may appear in qualified
543 -- expressions. If delayed, the front-end will have to expand.
544 -- If the component is a discriminated record, treat as non-static,
545 -- as the back-end cannot handle this properly.
547 Expr := First (Expressions (N));
548 while Present (Expr) loop
550 -- Checks 8: (no delayed components)
552 if Is_Delayed_Aggregate (Expr) then
556 -- Checks 9: (no discriminated records)
558 if Present (Etype (Expr))
559 and then Is_Record_Type (Etype (Expr))
560 and then Has_Discriminants (Etype (Expr))
565 -- Checks 7. Component must not be bit aligned component
567 if Possible_Bit_Aligned_Component (Expr) then
571 -- Recursion to following indexes for multiple dimension case
573 if Present (Next_Index (Index))
574 and then not Component_Check (Expr, Next_Index (Index))
579 -- All checks for that component finished, on to next
587 -- Start of processing for Backend_Processing_Possible
590 -- Checks 2 (array not bit packed) and 10 (no controlled actions)
592 if Is_Bit_Packed_Array (Typ) or else Needs_Finalization (Typ) then
596 -- If component is limited, aggregate must be expanded because each
597 -- component assignment must be built in place.
599 if Is_Inherently_Limited_Type (Component_Type (Typ)) then
603 -- Checks 4 (array must not be multi-dimensional Fortran case)
605 if Convention (Typ) = Convention_Fortran
606 and then Number_Dimensions (Typ) > 1
611 -- Checks 3 (size of array must be known at compile time)
613 if not Size_Known_At_Compile_Time (Typ) then
617 -- Checks on components
619 if not Component_Check (N, First_Index (Typ)) then
623 -- Checks 5 (if the component type is tagged, then we may need to do
624 -- tag adjustments. Perhaps this should be refined to check for any
625 -- component associations that actually need tag adjustment, similar
626 -- to the test in Component_Not_OK_For_Backend for record aggregates
627 -- with tagged components, but not clear whether it's worthwhile ???;
628 -- in the case of the JVM, object tags are handled implicitly)
630 if Is_Tagged_Type (Component_Type (Typ))
631 and then Tagged_Type_Expansion
636 -- Checks 6 (component type must not have bit aligned components)
638 if Type_May_Have_Bit_Aligned_Components (Component_Type (Typ)) then
642 -- Checks 11: Array aggregates with aliased components are currently
643 -- not well supported by the VM backend; disable temporarily this
644 -- backend processing until it is definitely supported.
646 if VM_Target /= No_VM
647 and then Has_Aliased_Components (Base_Type (Typ))
652 -- Backend processing is possible
654 Set_Size_Known_At_Compile_Time (Etype (N), True);
656 end Backend_Processing_Possible;
658 ---------------------------
659 -- Build_Array_Aggr_Code --
660 ---------------------------
662 -- The code that we generate from a one dimensional aggregate is
664 -- 1. If the sub-aggregate contains discrete choices we
666 -- (a) Sort the discrete choices
668 -- (b) Otherwise for each discrete choice that specifies a range we
669 -- emit a loop. If a range specifies a maximum of three values, or
670 -- we are dealing with an expression we emit a sequence of
671 -- assignments instead of a loop.
673 -- (c) Generate the remaining loops to cover the others choice if any
675 -- 2. If the aggregate contains positional elements we
677 -- (a) translate the positional elements in a series of assignments
679 -- (b) Generate a final loop to cover the others choice if any.
680 -- Note that this final loop has to be a while loop since the case
682 -- L : Integer := Integer'Last;
683 -- H : Integer := Integer'Last;
684 -- A : array (L .. H) := (1, others =>0);
686 -- cannot be handled by a for loop. Thus for the following
688 -- array (L .. H) := (.. positional elements.., others =>E);
690 -- we always generate something like:
692 -- J : Index_Type := Index_Of_Last_Positional_Element;
694 -- J := Index_Base'Succ (J)
698 function Build_Array_Aggr_Code
703 Scalar_Comp : Boolean;
704 Indices : List_Id := No_List;
705 Flist : Node_Id := Empty) return List_Id
707 Loc : constant Source_Ptr := Sloc (N);
708 Index_Base : constant Entity_Id := Base_Type (Etype (Index));
709 Index_Base_L : constant Node_Id := Type_Low_Bound (Index_Base);
710 Index_Base_H : constant Node_Id := Type_High_Bound (Index_Base);
712 function Add (Val : Int; To : Node_Id) return Node_Id;
713 -- Returns an expression where Val is added to expression To, unless
714 -- To+Val is provably out of To's base type range. To must be an
715 -- already analyzed expression.
717 function Empty_Range (L, H : Node_Id) return Boolean;
718 -- Returns True if the range defined by L .. H is certainly empty
720 function Equal (L, H : Node_Id) return Boolean;
721 -- Returns True if L = H for sure
723 function Index_Base_Name return Node_Id;
724 -- Returns a new reference to the index type name
726 function Gen_Assign (Ind : Node_Id; Expr : Node_Id) return List_Id;
727 -- Ind must be a side-effect free expression. If the input aggregate
728 -- N to Build_Loop contains no sub-aggregates, then this function
729 -- returns the assignment statement:
731 -- Into (Indices, Ind) := Expr;
733 -- Otherwise we call Build_Code recursively
735 -- Ada 2005 (AI-287): In case of default initialized component, Expr
736 -- is empty and we generate a call to the corresponding IP subprogram.
738 function Gen_Loop (L, H : Node_Id; Expr : Node_Id) return List_Id;
739 -- Nodes L and H must be side-effect free expressions.
740 -- If the input aggregate N to Build_Loop contains no sub-aggregates,
741 -- This routine returns the for loop statement
743 -- for J in Index_Base'(L) .. Index_Base'(H) loop
744 -- Into (Indices, J) := Expr;
747 -- Otherwise we call Build_Code recursively.
748 -- As an optimization if the loop covers 3 or less scalar elements we
749 -- generate a sequence of assignments.
751 function Gen_While (L, H : Node_Id; Expr : Node_Id) return List_Id;
752 -- Nodes L and H must be side-effect free expressions.
753 -- If the input aggregate N to Build_Loop contains no sub-aggregates,
754 -- This routine returns the while loop statement
756 -- J : Index_Base := L;
758 -- J := Index_Base'Succ (J);
759 -- Into (Indices, J) := Expr;
762 -- Otherwise we call Build_Code recursively
764 function Local_Compile_Time_Known_Value (E : Node_Id) return Boolean;
765 function Local_Expr_Value (E : Node_Id) return Uint;
766 -- These two Local routines are used to replace the corresponding ones
767 -- in sem_eval because while processing the bounds of an aggregate with
768 -- discrete choices whose index type is an enumeration, we build static
769 -- expressions not recognized by Compile_Time_Known_Value as such since
770 -- they have not yet been analyzed and resolved. All the expressions in
771 -- question are things like Index_Base_Name'Val (Const) which we can
772 -- easily recognize as being constant.
778 function Add (Val : Int; To : Node_Id) return Node_Id is
783 U_Val : constant Uint := UI_From_Int (Val);
786 -- Note: do not try to optimize the case of Val = 0, because
787 -- we need to build a new node with the proper Sloc value anyway.
789 -- First test if we can do constant folding
791 if Local_Compile_Time_Known_Value (To) then
792 U_To := Local_Expr_Value (To) + Val;
794 -- Determine if our constant is outside the range of the index.
795 -- If so return an Empty node. This empty node will be caught
796 -- by Empty_Range below.
798 if Compile_Time_Known_Value (Index_Base_L)
799 and then U_To < Expr_Value (Index_Base_L)
803 elsif Compile_Time_Known_Value (Index_Base_H)
804 and then U_To > Expr_Value (Index_Base_H)
809 Expr_Pos := Make_Integer_Literal (Loc, U_To);
810 Set_Is_Static_Expression (Expr_Pos);
812 if not Is_Enumeration_Type (Index_Base) then
815 -- If we are dealing with enumeration return
816 -- Index_Base'Val (Expr_Pos)
820 Make_Attribute_Reference
822 Prefix => Index_Base_Name,
823 Attribute_Name => Name_Val,
824 Expressions => New_List (Expr_Pos));
830 -- If we are here no constant folding possible
832 if not Is_Enumeration_Type (Index_Base) then
835 Left_Opnd => Duplicate_Subexpr (To),
836 Right_Opnd => Make_Integer_Literal (Loc, U_Val));
838 -- If we are dealing with enumeration return
839 -- Index_Base'Val (Index_Base'Pos (To) + Val)
843 Make_Attribute_Reference
845 Prefix => Index_Base_Name,
846 Attribute_Name => Name_Pos,
847 Expressions => New_List (Duplicate_Subexpr (To)));
852 Right_Opnd => Make_Integer_Literal (Loc, U_Val));
855 Make_Attribute_Reference
857 Prefix => Index_Base_Name,
858 Attribute_Name => Name_Val,
859 Expressions => New_List (Expr_Pos));
869 function Empty_Range (L, H : Node_Id) return Boolean is
870 Is_Empty : Boolean := False;
875 -- First check if L or H were already detected as overflowing the
876 -- index base range type by function Add above. If this is so Add
877 -- returns the empty node.
879 if No (L) or else No (H) then
886 -- L > H range is empty
892 -- B_L > H range must be empty
898 -- L > B_H range must be empty
902 High := Index_Base_H;
905 if Local_Compile_Time_Known_Value (Low)
906 and then Local_Compile_Time_Known_Value (High)
909 UI_Gt (Local_Expr_Value (Low), Local_Expr_Value (High));
922 function Equal (L, H : Node_Id) return Boolean is
927 elsif Local_Compile_Time_Known_Value (L)
928 and then Local_Compile_Time_Known_Value (H)
930 return UI_Eq (Local_Expr_Value (L), Local_Expr_Value (H));
940 function Gen_Assign (Ind : Node_Id; Expr : Node_Id) return List_Id is
941 L : constant List_Id := New_List;
945 New_Indices : List_Id;
946 Indexed_Comp : Node_Id;
948 Comp_Type : Entity_Id := Empty;
950 function Add_Loop_Actions (Lis : List_Id) return List_Id;
951 -- Collect insert_actions generated in the construction of a
952 -- loop, and prepend them to the sequence of assignments to
953 -- complete the eventual body of the loop.
955 ----------------------
956 -- Add_Loop_Actions --
957 ----------------------
959 function Add_Loop_Actions (Lis : List_Id) return List_Id is
963 -- Ada 2005 (AI-287): Do nothing else in case of default
964 -- initialized component.
969 elsif Nkind (Parent (Expr)) = N_Component_Association
970 and then Present (Loop_Actions (Parent (Expr)))
972 Append_List (Lis, Loop_Actions (Parent (Expr)));
973 Res := Loop_Actions (Parent (Expr));
974 Set_Loop_Actions (Parent (Expr), No_List);
980 end Add_Loop_Actions;
982 -- Start of processing for Gen_Assign
986 New_Indices := New_List;
988 New_Indices := New_Copy_List_Tree (Indices);
991 Append_To (New_Indices, Ind);
993 if Present (Flist) then
994 F := New_Copy_Tree (Flist);
996 elsif Present (Etype (N)) and then Needs_Finalization (Etype (N)) then
997 if Is_Entity_Name (Into)
998 and then Present (Scope (Entity (Into)))
1000 F := Find_Final_List (Scope (Entity (Into)));
1002 F := Find_Final_List (Current_Scope);
1008 if Present (Next_Index (Index)) then
1011 Build_Array_Aggr_Code
1014 Index => Next_Index (Index),
1016 Scalar_Comp => Scalar_Comp,
1017 Indices => New_Indices,
1021 -- If we get here then we are at a bottom-level (sub-)aggregate
1025 (Make_Indexed_Component (Loc,
1026 Prefix => New_Copy_Tree (Into),
1027 Expressions => New_Indices));
1029 Set_Assignment_OK (Indexed_Comp);
1031 -- Ada 2005 (AI-287): In case of default initialized component, Expr
1032 -- is not present (and therefore we also initialize Expr_Q to empty).
1036 elsif Nkind (Expr) = N_Qualified_Expression then
1037 Expr_Q := Expression (Expr);
1042 if Present (Etype (N))
1043 and then Etype (N) /= Any_Composite
1045 Comp_Type := Component_Type (Etype (N));
1046 pragma Assert (Comp_Type = Ctype); -- AI-287
1048 elsif Present (Next (First (New_Indices))) then
1050 -- Ada 2005 (AI-287): Do nothing in case of default initialized
1051 -- component because we have received the component type in
1052 -- the formal parameter Ctype.
1054 -- ??? Some assert pragmas have been added to check if this new
1055 -- formal can be used to replace this code in all cases.
1057 if Present (Expr) then
1059 -- This is a multidimensional array. Recover the component
1060 -- type from the outermost aggregate, because subaggregates
1061 -- do not have an assigned type.
1068 while Present (P) loop
1069 if Nkind (P) = N_Aggregate
1070 and then Present (Etype (P))
1072 Comp_Type := Component_Type (Etype (P));
1080 pragma Assert (Comp_Type = Ctype); -- AI-287
1085 -- Ada 2005 (AI-287): We only analyze the expression in case of non-
1086 -- default initialized components (otherwise Expr_Q is not present).
1089 and then Nkind_In (Expr_Q, N_Aggregate, N_Extension_Aggregate)
1091 -- At this stage the Expression may not have been analyzed yet
1092 -- because the array aggregate code has not been updated to use
1093 -- the Expansion_Delayed flag and avoid analysis altogether to
1094 -- solve the same problem (see Resolve_Aggr_Expr). So let us do
1095 -- the analysis of non-array aggregates now in order to get the
1096 -- value of Expansion_Delayed flag for the inner aggregate ???
1098 if Present (Comp_Type) and then not Is_Array_Type (Comp_Type) then
1099 Analyze_And_Resolve (Expr_Q, Comp_Type);
1102 if Is_Delayed_Aggregate (Expr_Q) then
1104 -- This is either a subaggregate of a multidimentional array,
1105 -- or a component of an array type whose component type is
1106 -- also an array. In the latter case, the expression may have
1107 -- component associations that provide different bounds from
1108 -- those of the component type, and sliding must occur. Instead
1109 -- of decomposing the current aggregate assignment, force the
1110 -- re-analysis of the assignment, so that a temporary will be
1111 -- generated in the usual fashion, and sliding will take place.
1113 if Nkind (Parent (N)) = N_Assignment_Statement
1114 and then Is_Array_Type (Comp_Type)
1115 and then Present (Component_Associations (Expr_Q))
1116 and then Must_Slide (Comp_Type, Etype (Expr_Q))
1118 Set_Expansion_Delayed (Expr_Q, False);
1119 Set_Analyzed (Expr_Q, False);
1125 Expr_Q, Etype (Expr_Q), Indexed_Comp, F));
1130 -- Ada 2005 (AI-287): In case of default initialized component, call
1131 -- the initialization subprogram associated with the component type.
1132 -- If the component type is an access type, add an explicit null
1133 -- assignment, because for the back-end there is an initialization
1134 -- present for the whole aggregate, and no default initialization
1137 -- In addition, if the component type is controlled, we must call
1138 -- its Initialize procedure explicitly, because there is no explicit
1139 -- object creation that will invoke it otherwise.
1142 if Present (Base_Init_Proc (Base_Type (Ctype)))
1143 or else Has_Task (Base_Type (Ctype))
1146 Build_Initialization_Call (Loc,
1147 Id_Ref => Indexed_Comp,
1149 With_Default_Init => True));
1151 elsif Is_Access_Type (Ctype) then
1153 Make_Assignment_Statement (Loc,
1154 Name => Indexed_Comp,
1155 Expression => Make_Null (Loc)));
1158 if Needs_Finalization (Ctype) then
1161 Ref => New_Copy_Tree (Indexed_Comp),
1163 Flist_Ref => Find_Final_List (Current_Scope),
1164 With_Attach => Make_Integer_Literal (Loc, 1)));
1168 -- Now generate the assignment with no associated controlled
1169 -- actions since the target of the assignment may not have been
1170 -- initialized, it is not possible to Finalize it as expected by
1171 -- normal controlled assignment. The rest of the controlled
1172 -- actions are done manually with the proper finalization list
1173 -- coming from the context.
1176 Make_OK_Assignment_Statement (Loc,
1177 Name => Indexed_Comp,
1178 Expression => New_Copy_Tree (Expr));
1180 if Present (Comp_Type) and then Needs_Finalization (Comp_Type) then
1181 Set_No_Ctrl_Actions (A);
1183 -- If this is an aggregate for an array of arrays, each
1184 -- sub-aggregate will be expanded as well, and even with
1185 -- No_Ctrl_Actions the assignments of inner components will
1186 -- require attachment in their assignments to temporaries.
1187 -- These temporaries must be finalized for each subaggregate,
1188 -- to prevent multiple attachments of the same temporary
1189 -- location to same finalization chain (and consequently
1190 -- circular lists). To ensure that finalization takes place
1191 -- for each subaggregate we wrap the assignment in a block.
1193 if Is_Array_Type (Comp_Type)
1194 and then Nkind (Expr) = N_Aggregate
1197 Make_Block_Statement (Loc,
1198 Handled_Statement_Sequence =>
1199 Make_Handled_Sequence_Of_Statements (Loc,
1200 Statements => New_List (A)));
1206 -- Adjust the tag if tagged (because of possible view
1207 -- conversions), unless compiling for a VM where
1208 -- tags are implicit.
1210 if Present (Comp_Type)
1211 and then Is_Tagged_Type (Comp_Type)
1212 and then Tagged_Type_Expansion
1215 Make_OK_Assignment_Statement (Loc,
1217 Make_Selected_Component (Loc,
1218 Prefix => New_Copy_Tree (Indexed_Comp),
1221 (First_Tag_Component (Comp_Type), Loc)),
1224 Unchecked_Convert_To (RTE (RE_Tag),
1226 (Node (First_Elmt (Access_Disp_Table (Comp_Type))),
1232 -- Adjust and attach the component to the proper final list, which
1233 -- can be the controller of the outer record object or the final
1234 -- list associated with the scope.
1236 -- If the component is itself an array of controlled types, whose
1237 -- value is given by a sub-aggregate, then the attach calls have
1238 -- been generated when individual subcomponent are assigned, and
1239 -- must not be done again to prevent malformed finalization chains
1240 -- (see comments above, concerning the creation of a block to hold
1241 -- inner finalization actions).
1243 if Present (Comp_Type)
1244 and then Needs_Finalization (Comp_Type)
1245 and then not Is_Limited_Type (Comp_Type)
1247 (Is_Array_Type (Comp_Type)
1248 and then Is_Controlled (Component_Type (Comp_Type))
1249 and then Nkind (Expr) = N_Aggregate)
1253 Ref => New_Copy_Tree (Indexed_Comp),
1256 With_Attach => Make_Integer_Literal (Loc, 1)));
1260 return Add_Loop_Actions (L);
1267 function Gen_Loop (L, H : Node_Id; Expr : Node_Id) return List_Id is
1277 -- Index_Base'(L) .. Index_Base'(H)
1279 L_Iteration_Scheme : Node_Id;
1280 -- L_J in Index_Base'(L) .. Index_Base'(H)
1283 -- The statements to execute in the loop
1285 S : constant List_Id := New_List;
1286 -- List of statements
1289 -- Copy of expression tree, used for checking purposes
1292 -- If loop bounds define an empty range return the null statement
1294 if Empty_Range (L, H) then
1295 Append_To (S, Make_Null_Statement (Loc));
1297 -- Ada 2005 (AI-287): Nothing else need to be done in case of
1298 -- default initialized component.
1304 -- The expression must be type-checked even though no component
1305 -- of the aggregate will have this value. This is done only for
1306 -- actual components of the array, not for subaggregates. Do
1307 -- the check on a copy, because the expression may be shared
1308 -- among several choices, some of which might be non-null.
1310 if Present (Etype (N))
1311 and then Is_Array_Type (Etype (N))
1312 and then No (Next_Index (Index))
1314 Expander_Mode_Save_And_Set (False);
1315 Tcopy := New_Copy_Tree (Expr);
1316 Set_Parent (Tcopy, N);
1317 Analyze_And_Resolve (Tcopy, Component_Type (Etype (N)));
1318 Expander_Mode_Restore;
1324 -- If loop bounds are the same then generate an assignment
1326 elsif Equal (L, H) then
1327 return Gen_Assign (New_Copy_Tree (L), Expr);
1329 -- If H - L <= 2 then generate a sequence of assignments when we are
1330 -- processing the bottom most aggregate and it contains scalar
1333 elsif No (Next_Index (Index))
1334 and then Scalar_Comp
1335 and then Local_Compile_Time_Known_Value (L)
1336 and then Local_Compile_Time_Known_Value (H)
1337 and then Local_Expr_Value (H) - Local_Expr_Value (L) <= 2
1340 Append_List_To (S, Gen_Assign (New_Copy_Tree (L), Expr));
1341 Append_List_To (S, Gen_Assign (Add (1, To => L), Expr));
1343 if Local_Expr_Value (H) - Local_Expr_Value (L) = 2 then
1344 Append_List_To (S, Gen_Assign (Add (2, To => L), Expr));
1350 -- Otherwise construct the loop, starting with the loop index L_J
1352 L_J := Make_Temporary (Loc, 'J', L);
1354 -- Construct "L .. H" in Index_Base. We use a qualified expression
1355 -- for the bound to convert to the index base, but we don't need
1356 -- to do that if we already have the base type at hand.
1358 if Etype (L) = Index_Base then
1362 Make_Qualified_Expression (Loc,
1363 Subtype_Mark => Index_Base_Name,
1367 if Etype (H) = Index_Base then
1371 Make_Qualified_Expression (Loc,
1372 Subtype_Mark => Index_Base_Name,
1381 -- Construct "for L_J in Index_Base range L .. H"
1383 L_Iteration_Scheme :=
1384 Make_Iteration_Scheme
1386 Loop_Parameter_Specification =>
1387 Make_Loop_Parameter_Specification
1389 Defining_Identifier => L_J,
1390 Discrete_Subtype_Definition => L_Range));
1392 -- Construct the statements to execute in the loop body
1394 L_Body := Gen_Assign (New_Reference_To (L_J, Loc), Expr);
1396 -- Construct the final loop
1398 Append_To (S, Make_Implicit_Loop_Statement
1400 Identifier => Empty,
1401 Iteration_Scheme => L_Iteration_Scheme,
1402 Statements => L_Body));
1404 -- A small optimization: if the aggregate is initialized with a box
1405 -- and the component type has no initialization procedure, remove the
1406 -- useless empty loop.
1408 if Nkind (First (S)) = N_Loop_Statement
1409 and then Is_Empty_List (Statements (First (S)))
1411 return New_List (Make_Null_Statement (Loc));
1421 -- The code built is
1423 -- W_J : Index_Base := L;
1424 -- while W_J < H loop
1425 -- W_J := Index_Base'Succ (W);
1429 function Gen_While (L, H : Node_Id; Expr : Node_Id) return List_Id is
1433 -- W_J : Base_Type := L;
1435 W_Iteration_Scheme : Node_Id;
1438 W_Index_Succ : Node_Id;
1439 -- Index_Base'Succ (J)
1441 W_Increment : Node_Id;
1442 -- W_J := Index_Base'Succ (W)
1444 W_Body : constant List_Id := New_List;
1445 -- The statements to execute in the loop
1447 S : constant List_Id := New_List;
1448 -- list of statement
1451 -- If loop bounds define an empty range or are equal return null
1453 if Empty_Range (L, H) or else Equal (L, H) then
1454 Append_To (S, Make_Null_Statement (Loc));
1458 -- Build the decl of W_J
1460 W_J := Make_Temporary (Loc, 'J', L);
1462 Make_Object_Declaration
1464 Defining_Identifier => W_J,
1465 Object_Definition => Index_Base_Name,
1468 -- Theoretically we should do a New_Copy_Tree (L) here, but we know
1469 -- that in this particular case L is a fresh Expr generated by
1470 -- Add which we are the only ones to use.
1472 Append_To (S, W_Decl);
1474 -- Construct " while W_J < H"
1476 W_Iteration_Scheme :=
1477 Make_Iteration_Scheme
1479 Condition => Make_Op_Lt
1481 Left_Opnd => New_Reference_To (W_J, Loc),
1482 Right_Opnd => New_Copy_Tree (H)));
1484 -- Construct the statements to execute in the loop body
1487 Make_Attribute_Reference
1489 Prefix => Index_Base_Name,
1490 Attribute_Name => Name_Succ,
1491 Expressions => New_List (New_Reference_To (W_J, Loc)));
1494 Make_OK_Assignment_Statement
1496 Name => New_Reference_To (W_J, Loc),
1497 Expression => W_Index_Succ);
1499 Append_To (W_Body, W_Increment);
1500 Append_List_To (W_Body,
1501 Gen_Assign (New_Reference_To (W_J, Loc), Expr));
1503 -- Construct the final loop
1505 Append_To (S, Make_Implicit_Loop_Statement
1507 Identifier => Empty,
1508 Iteration_Scheme => W_Iteration_Scheme,
1509 Statements => W_Body));
1514 ---------------------
1515 -- Index_Base_Name --
1516 ---------------------
1518 function Index_Base_Name return Node_Id is
1520 return New_Reference_To (Index_Base, Sloc (N));
1521 end Index_Base_Name;
1523 ------------------------------------
1524 -- Local_Compile_Time_Known_Value --
1525 ------------------------------------
1527 function Local_Compile_Time_Known_Value (E : Node_Id) return Boolean is
1529 return Compile_Time_Known_Value (E)
1531 (Nkind (E) = N_Attribute_Reference
1532 and then Attribute_Name (E) = Name_Val
1533 and then Compile_Time_Known_Value (First (Expressions (E))));
1534 end Local_Compile_Time_Known_Value;
1536 ----------------------
1537 -- Local_Expr_Value --
1538 ----------------------
1540 function Local_Expr_Value (E : Node_Id) return Uint is
1542 if Compile_Time_Known_Value (E) then
1543 return Expr_Value (E);
1545 return Expr_Value (First (Expressions (E)));
1547 end Local_Expr_Value;
1549 -- Build_Array_Aggr_Code Variables
1556 Others_Expr : Node_Id := Empty;
1557 Others_Box_Present : Boolean := False;
1559 Aggr_L : constant Node_Id := Low_Bound (Aggregate_Bounds (N));
1560 Aggr_H : constant Node_Id := High_Bound (Aggregate_Bounds (N));
1561 -- The aggregate bounds of this specific sub-aggregate. Note that if
1562 -- the code generated by Build_Array_Aggr_Code is executed then these
1563 -- bounds are OK. Otherwise a Constraint_Error would have been raised.
1565 Aggr_Low : constant Node_Id := Duplicate_Subexpr_No_Checks (Aggr_L);
1566 Aggr_High : constant Node_Id := Duplicate_Subexpr_No_Checks (Aggr_H);
1567 -- After Duplicate_Subexpr these are side-effect free
1572 Nb_Choices : Nat := 0;
1573 Table : Case_Table_Type (1 .. Number_Of_Choices (N));
1574 -- Used to sort all the different choice values
1577 -- Number of elements in the positional aggregate
1579 New_Code : constant List_Id := New_List;
1581 -- Start of processing for Build_Array_Aggr_Code
1584 -- First before we start, a special case. if we have a bit packed
1585 -- array represented as a modular type, then clear the value to
1586 -- zero first, to ensure that unused bits are properly cleared.
1591 and then Is_Bit_Packed_Array (Typ)
1592 and then Is_Modular_Integer_Type (Packed_Array_Type (Typ))
1594 Append_To (New_Code,
1595 Make_Assignment_Statement (Loc,
1596 Name => New_Copy_Tree (Into),
1598 Unchecked_Convert_To (Typ,
1599 Make_Integer_Literal (Loc, Uint_0))));
1602 -- If the component type contains tasks, we need to build a Master
1603 -- entity in the current scope, because it will be needed if build-
1604 -- in-place functions are called in the expanded code.
1606 if Nkind (Parent (N)) = N_Object_Declaration
1607 and then Has_Task (Typ)
1609 Build_Master_Entity (Defining_Identifier (Parent (N)));
1612 -- STEP 1: Process component associations
1614 -- For those associations that may generate a loop, initialize
1615 -- Loop_Actions to collect inserted actions that may be crated.
1617 -- Skip this if no component associations
1619 if No (Expressions (N)) then
1621 -- STEP 1 (a): Sort the discrete choices
1623 Assoc := First (Component_Associations (N));
1624 while Present (Assoc) loop
1625 Choice := First (Choices (Assoc));
1626 while Present (Choice) loop
1627 if Nkind (Choice) = N_Others_Choice then
1628 Set_Loop_Actions (Assoc, New_List);
1630 if Box_Present (Assoc) then
1631 Others_Box_Present := True;
1633 Others_Expr := Expression (Assoc);
1638 Get_Index_Bounds (Choice, Low, High);
1641 Set_Loop_Actions (Assoc, New_List);
1644 Nb_Choices := Nb_Choices + 1;
1645 if Box_Present (Assoc) then
1646 Table (Nb_Choices) := (Choice_Lo => Low,
1648 Choice_Node => Empty);
1650 Table (Nb_Choices) := (Choice_Lo => Low,
1652 Choice_Node => Expression (Assoc));
1660 -- If there is more than one set of choices these must be static
1661 -- and we can therefore sort them. Remember that Nb_Choices does not
1662 -- account for an others choice.
1664 if Nb_Choices > 1 then
1665 Sort_Case_Table (Table);
1668 -- STEP 1 (b): take care of the whole set of discrete choices
1670 for J in 1 .. Nb_Choices loop
1671 Low := Table (J).Choice_Lo;
1672 High := Table (J).Choice_Hi;
1673 Expr := Table (J).Choice_Node;
1674 Append_List (Gen_Loop (Low, High, Expr), To => New_Code);
1677 -- STEP 1 (c): generate the remaining loops to cover others choice
1678 -- We don't need to generate loops over empty gaps, but if there is
1679 -- a single empty range we must analyze the expression for semantics
1681 if Present (Others_Expr) or else Others_Box_Present then
1683 First : Boolean := True;
1686 for J in 0 .. Nb_Choices loop
1690 Low := Add (1, To => Table (J).Choice_Hi);
1693 if J = Nb_Choices then
1696 High := Add (-1, To => Table (J + 1).Choice_Lo);
1699 -- If this is an expansion within an init proc, make
1700 -- sure that discriminant references are replaced by
1701 -- the corresponding discriminal.
1703 if Inside_Init_Proc then
1704 if Is_Entity_Name (Low)
1705 and then Ekind (Entity (Low)) = E_Discriminant
1707 Set_Entity (Low, Discriminal (Entity (Low)));
1710 if Is_Entity_Name (High)
1711 and then Ekind (Entity (High)) = E_Discriminant
1713 Set_Entity (High, Discriminal (Entity (High)));
1718 or else not Empty_Range (Low, High)
1722 (Gen_Loop (Low, High, Others_Expr), To => New_Code);
1728 -- STEP 2: Process positional components
1731 -- STEP 2 (a): Generate the assignments for each positional element
1732 -- Note that here we have to use Aggr_L rather than Aggr_Low because
1733 -- Aggr_L is analyzed and Add wants an analyzed expression.
1735 Expr := First (Expressions (N));
1737 while Present (Expr) loop
1738 Nb_Elements := Nb_Elements + 1;
1739 Append_List (Gen_Assign (Add (Nb_Elements, To => Aggr_L), Expr),
1744 -- STEP 2 (b): Generate final loop if an others choice is present
1745 -- Here Nb_Elements gives the offset of the last positional element.
1747 if Present (Component_Associations (N)) then
1748 Assoc := Last (Component_Associations (N));
1750 -- Ada 2005 (AI-287)
1752 if Box_Present (Assoc) then
1753 Append_List (Gen_While (Add (Nb_Elements, To => Aggr_L),
1758 Expr := Expression (Assoc);
1760 Append_List (Gen_While (Add (Nb_Elements, To => Aggr_L),
1769 end Build_Array_Aggr_Code;
1771 ----------------------------
1772 -- Build_Record_Aggr_Code --
1773 ----------------------------
1775 function Build_Record_Aggr_Code
1779 Flist : Node_Id := Empty;
1780 Obj : Entity_Id := Empty;
1781 Is_Limited_Ancestor_Expansion : Boolean := False) return List_Id
1783 Loc : constant Source_Ptr := Sloc (N);
1784 L : constant List_Id := New_List;
1785 N_Typ : constant Entity_Id := Etype (N);
1792 Comp_Type : Entity_Id;
1793 Selector : Entity_Id;
1794 Comp_Expr : Node_Id;
1797 Internal_Final_List : Node_Id := Empty;
1799 -- If this is an internal aggregate, the External_Final_List is an
1800 -- expression for the controller record of the enclosing type.
1802 -- If the current aggregate has several controlled components, this
1803 -- expression will appear in several calls to attach to the finali-
1804 -- zation list, and it must not be shared.
1806 External_Final_List : Node_Id;
1807 Ancestor_Is_Expression : Boolean := False;
1808 Ancestor_Is_Subtype_Mark : Boolean := False;
1810 Init_Typ : Entity_Id := Empty;
1813 Ctrl_Stuff_Done : Boolean := False;
1814 -- True if Gen_Ctrl_Actions_For_Aggr has already been called; calls
1815 -- after the first do nothing.
1817 function Ancestor_Discriminant_Value (Disc : Entity_Id) return Node_Id;
1818 -- Returns the value that the given discriminant of an ancestor type
1819 -- should receive (in the absence of a conflict with the value provided
1820 -- by an ancestor part of an extension aggregate).
1822 procedure Check_Ancestor_Discriminants (Anc_Typ : Entity_Id);
1823 -- Check that each of the discriminant values defined by the ancestor
1824 -- part of an extension aggregate match the corresponding values
1825 -- provided by either an association of the aggregate or by the
1826 -- constraint imposed by a parent type (RM95-4.3.2(8)).
1828 function Compatible_Int_Bounds
1829 (Agg_Bounds : Node_Id;
1830 Typ_Bounds : Node_Id) return Boolean;
1831 -- Return true if Agg_Bounds are equal or within Typ_Bounds. It is
1832 -- assumed that both bounds are integer ranges.
1834 procedure Gen_Ctrl_Actions_For_Aggr;
1835 -- Deal with the various controlled type data structure initializations
1836 -- (but only if it hasn't been done already).
1838 function Get_Constraint_Association (T : Entity_Id) return Node_Id;
1839 -- Returns the first discriminant association in the constraint
1840 -- associated with T, if any, otherwise returns Empty.
1842 function Init_Controller
1847 Init_Pr : Boolean) return List_Id;
1848 -- Returns the list of statements necessary to initialize the internal
1849 -- controller of the (possible) ancestor typ into target and attach it
1850 -- to finalization list F. Init_Pr conditions the call to the init proc
1851 -- since it may already be done due to ancestor initialization.
1853 function Is_Int_Range_Bounds (Bounds : Node_Id) return Boolean;
1854 -- Check whether Bounds is a range node and its lower and higher bounds
1855 -- are integers literals.
1857 ---------------------------------
1858 -- Ancestor_Discriminant_Value --
1859 ---------------------------------
1861 function Ancestor_Discriminant_Value (Disc : Entity_Id) return Node_Id is
1863 Assoc_Elmt : Elmt_Id;
1864 Aggr_Comp : Entity_Id;
1865 Corresp_Disc : Entity_Id;
1866 Current_Typ : Entity_Id := Base_Type (Typ);
1867 Parent_Typ : Entity_Id;
1868 Parent_Disc : Entity_Id;
1869 Save_Assoc : Node_Id := Empty;
1872 -- First check any discriminant associations to see if any of them
1873 -- provide a value for the discriminant.
1875 if Present (Discriminant_Specifications (Parent (Current_Typ))) then
1876 Assoc := First (Component_Associations (N));
1877 while Present (Assoc) loop
1878 Aggr_Comp := Entity (First (Choices (Assoc)));
1880 if Ekind (Aggr_Comp) = E_Discriminant then
1881 Save_Assoc := Expression (Assoc);
1883 Corresp_Disc := Corresponding_Discriminant (Aggr_Comp);
1884 while Present (Corresp_Disc) loop
1886 -- If found a corresponding discriminant then return the
1887 -- value given in the aggregate. (Note: this is not
1888 -- correct in the presence of side effects. ???)
1890 if Disc = Corresp_Disc then
1891 return Duplicate_Subexpr (Expression (Assoc));
1895 Corresponding_Discriminant (Corresp_Disc);
1903 -- No match found in aggregate, so chain up parent types to find
1904 -- a constraint that defines the value of the discriminant.
1906 Parent_Typ := Etype (Current_Typ);
1907 while Current_Typ /= Parent_Typ loop
1908 if Has_Discriminants (Parent_Typ)
1909 and then not Has_Unknown_Discriminants (Parent_Typ)
1911 Parent_Disc := First_Discriminant (Parent_Typ);
1913 -- We either get the association from the subtype indication
1914 -- of the type definition itself, or from the discriminant
1915 -- constraint associated with the type entity (which is
1916 -- preferable, but it's not always present ???)
1918 if Is_Empty_Elmt_List (
1919 Discriminant_Constraint (Current_Typ))
1921 Assoc := Get_Constraint_Association (Current_Typ);
1922 Assoc_Elmt := No_Elmt;
1925 First_Elmt (Discriminant_Constraint (Current_Typ));
1926 Assoc := Node (Assoc_Elmt);
1929 -- Traverse the discriminants of the parent type looking
1930 -- for one that corresponds.
1932 while Present (Parent_Disc) and then Present (Assoc) loop
1933 Corresp_Disc := Parent_Disc;
1934 while Present (Corresp_Disc)
1935 and then Disc /= Corresp_Disc
1938 Corresponding_Discriminant (Corresp_Disc);
1941 if Disc = Corresp_Disc then
1942 if Nkind (Assoc) = N_Discriminant_Association then
1943 Assoc := Expression (Assoc);
1946 -- If the located association directly denotes a
1947 -- discriminant, then use the value of a saved
1948 -- association of the aggregate. This is a kludge to
1949 -- handle certain cases involving multiple discriminants
1950 -- mapped to a single discriminant of a descendant. It's
1951 -- not clear how to locate the appropriate discriminant
1952 -- value for such cases. ???
1954 if Is_Entity_Name (Assoc)
1955 and then Ekind (Entity (Assoc)) = E_Discriminant
1957 Assoc := Save_Assoc;
1960 return Duplicate_Subexpr (Assoc);
1963 Next_Discriminant (Parent_Disc);
1965 if No (Assoc_Elmt) then
1968 Next_Elmt (Assoc_Elmt);
1969 if Present (Assoc_Elmt) then
1970 Assoc := Node (Assoc_Elmt);
1978 Current_Typ := Parent_Typ;
1979 Parent_Typ := Etype (Current_Typ);
1982 -- In some cases there's no ancestor value to locate (such as
1983 -- when an ancestor part given by an expression defines the
1984 -- discriminant value).
1987 end Ancestor_Discriminant_Value;
1989 ----------------------------------
1990 -- Check_Ancestor_Discriminants --
1991 ----------------------------------
1993 procedure Check_Ancestor_Discriminants (Anc_Typ : Entity_Id) is
1995 Disc_Value : Node_Id;
1999 Discr := First_Discriminant (Base_Type (Anc_Typ));
2000 while Present (Discr) loop
2001 Disc_Value := Ancestor_Discriminant_Value (Discr);
2003 if Present (Disc_Value) then
2004 Cond := Make_Op_Ne (Loc,
2006 Make_Selected_Component (Loc,
2007 Prefix => New_Copy_Tree (Target),
2008 Selector_Name => New_Occurrence_Of (Discr, Loc)),
2009 Right_Opnd => Disc_Value);
2012 Make_Raise_Constraint_Error (Loc,
2014 Reason => CE_Discriminant_Check_Failed));
2017 Next_Discriminant (Discr);
2019 end Check_Ancestor_Discriminants;
2021 ---------------------------
2022 -- Compatible_Int_Bounds --
2023 ---------------------------
2025 function Compatible_Int_Bounds
2026 (Agg_Bounds : Node_Id;
2027 Typ_Bounds : Node_Id) return Boolean
2029 Agg_Lo : constant Uint := Intval (Low_Bound (Agg_Bounds));
2030 Agg_Hi : constant Uint := Intval (High_Bound (Agg_Bounds));
2031 Typ_Lo : constant Uint := Intval (Low_Bound (Typ_Bounds));
2032 Typ_Hi : constant Uint := Intval (High_Bound (Typ_Bounds));
2034 return Typ_Lo <= Agg_Lo and then Agg_Hi <= Typ_Hi;
2035 end Compatible_Int_Bounds;
2037 --------------------------------
2038 -- Get_Constraint_Association --
2039 --------------------------------
2041 function Get_Constraint_Association (T : Entity_Id) return Node_Id is
2042 Typ_Def : constant Node_Id := Type_Definition (Parent (T));
2043 Indic : constant Node_Id := Subtype_Indication (Typ_Def);
2046 -- ??? Also need to cover case of a type mark denoting a subtype
2049 if Nkind (Indic) = N_Subtype_Indication
2050 and then Present (Constraint (Indic))
2052 return First (Constraints (Constraint (Indic)));
2056 end Get_Constraint_Association;
2058 ---------------------
2059 -- Init_Controller --
2060 ---------------------
2062 function Init_Controller
2067 Init_Pr : Boolean) return List_Id
2069 L : constant List_Id := New_List;
2072 Target_Type : Entity_Id;
2076 -- init-proc (target._controller);
2077 -- initialize (target._controller);
2078 -- Attach_to_Final_List (target._controller, F);
2081 Make_Selected_Component (Loc,
2082 Prefix => Convert_To (Typ, New_Copy_Tree (Target)),
2083 Selector_Name => Make_Identifier (Loc, Name_uController));
2084 Set_Assignment_OK (Ref);
2086 -- Ada 2005 (AI-287): Give support to aggregates of limited types.
2087 -- If the type is intrinsically limited the controller is limited as
2088 -- well. If it is tagged and limited then so is the controller.
2089 -- Otherwise an untagged type may have limited components without its
2090 -- full view being limited, so the controller is not limited.
2092 if Nkind (Target) = N_Identifier then
2093 Target_Type := Etype (Target);
2095 elsif Nkind (Target) = N_Selected_Component then
2096 Target_Type := Etype (Selector_Name (Target));
2098 elsif Nkind (Target) = N_Unchecked_Type_Conversion then
2099 Target_Type := Etype (Target);
2101 elsif Nkind (Target) = N_Unchecked_Expression
2102 and then Nkind (Expression (Target)) = N_Indexed_Component
2104 Target_Type := Etype (Prefix (Expression (Target)));
2107 Target_Type := Etype (Target);
2110 -- If the target has not been analyzed yet, as will happen with
2111 -- delayed expansion, use the given type (either the aggregate type
2112 -- or an ancestor) to determine limitedness.
2114 if No (Target_Type) then
2118 if (Is_Tagged_Type (Target_Type))
2119 and then Is_Limited_Type (Target_Type)
2121 RC := RE_Limited_Record_Controller;
2123 elsif Is_Inherently_Limited_Type (Target_Type) then
2124 RC := RE_Limited_Record_Controller;
2127 RC := RE_Record_Controller;
2132 Build_Initialization_Call (Loc,
2135 In_Init_Proc => Within_Init_Proc));
2139 Make_Procedure_Call_Statement (Loc,
2142 Find_Prim_Op (RTE (RC), Name_Initialize), Loc),
2143 Parameter_Associations =>
2144 New_List (New_Copy_Tree (Ref))));
2148 Obj_Ref => New_Copy_Tree (Ref),
2150 With_Attach => Attach));
2153 end Init_Controller;
2155 -------------------------
2156 -- Is_Int_Range_Bounds --
2157 -------------------------
2159 function Is_Int_Range_Bounds (Bounds : Node_Id) return Boolean is
2161 return Nkind (Bounds) = N_Range
2162 and then Nkind (Low_Bound (Bounds)) = N_Integer_Literal
2163 and then Nkind (High_Bound (Bounds)) = N_Integer_Literal;
2164 end Is_Int_Range_Bounds;
2166 -------------------------------
2167 -- Gen_Ctrl_Actions_For_Aggr --
2168 -------------------------------
2170 procedure Gen_Ctrl_Actions_For_Aggr is
2171 Alloc : Node_Id := Empty;
2174 -- Do the work only the first time this is called
2176 if Ctrl_Stuff_Done then
2180 Ctrl_Stuff_Done := True;
2183 and then Finalize_Storage_Only (Typ)
2185 (Is_Library_Level_Entity (Obj)
2186 or else Entity (Constant_Value (RTE (RE_Garbage_Collected))) =
2189 -- why not Is_True (Expr_Value (RTE (RE_Garbaage_Collected) ???
2191 Attach := Make_Integer_Literal (Loc, 0);
2193 elsif Nkind (Parent (N)) = N_Qualified_Expression
2194 and then Nkind (Parent (Parent (N))) = N_Allocator
2196 Alloc := Parent (Parent (N));
2197 Attach := Make_Integer_Literal (Loc, 2);
2200 Attach := Make_Integer_Literal (Loc, 1);
2203 -- Determine the external finalization list. It is either the
2204 -- finalization list of the outer-scope or the one coming from
2205 -- an outer aggregate. When the target is not a temporary, the
2206 -- proper scope is the scope of the target rather than the
2207 -- potentially transient current scope.
2209 if Needs_Finalization (Typ) then
2211 -- The current aggregate belongs to an allocator which creates
2212 -- an object through an anonymous access type or acts as the root
2213 -- of a coextension chain.
2217 (Is_Coextension_Root (Alloc)
2218 or else Ekind (Etype (Alloc)) = E_Anonymous_Access_Type)
2220 if No (Associated_Final_Chain (Etype (Alloc))) then
2221 Build_Final_List (Alloc, Etype (Alloc));
2224 External_Final_List :=
2225 Make_Selected_Component (Loc,
2228 Associated_Final_Chain (Etype (Alloc)), Loc),
2230 Make_Identifier (Loc, Name_F));
2232 elsif Present (Flist) then
2233 External_Final_List := New_Copy_Tree (Flist);
2235 elsif Is_Entity_Name (Target)
2236 and then Present (Scope (Entity (Target)))
2238 External_Final_List :=
2239 Find_Final_List (Scope (Entity (Target)));
2242 External_Final_List := Find_Final_List (Current_Scope);
2245 External_Final_List := Empty;
2248 -- Initialize and attach the outer object in the is_controlled case
2250 if Is_Controlled (Typ) then
2251 if Ancestor_Is_Subtype_Mark then
2252 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
2253 Set_Assignment_OK (Ref);
2255 Make_Procedure_Call_Statement (Loc,
2258 (Find_Prim_Op (Init_Typ, Name_Initialize), Loc),
2259 Parameter_Associations => New_List (New_Copy_Tree (Ref))));
2262 if not Has_Controlled_Component (Typ) then
2263 Ref := New_Copy_Tree (Target);
2264 Set_Assignment_OK (Ref);
2266 -- This is an aggregate of a coextension. Do not produce a
2267 -- finalization call, but rather attach the reference of the
2268 -- aggregate to its coextension chain.
2271 and then Is_Dynamic_Coextension (Alloc)
2273 if No (Coextensions (Alloc)) then
2274 Set_Coextensions (Alloc, New_Elmt_List);
2277 Append_Elmt (Ref, Coextensions (Alloc));
2282 Flist_Ref => New_Copy_Tree (External_Final_List),
2283 With_Attach => Attach));
2288 -- In the Has_Controlled component case, all the intermediate
2289 -- controllers must be initialized.
2291 if Has_Controlled_Component (Typ)
2292 and not Is_Limited_Ancestor_Expansion
2295 Inner_Typ : Entity_Id;
2296 Outer_Typ : Entity_Id;
2300 -- Find outer type with a controller
2302 Outer_Typ := Base_Type (Typ);
2303 while Outer_Typ /= Init_Typ
2304 and then not Has_New_Controlled_Component (Outer_Typ)
2306 Outer_Typ := Etype (Outer_Typ);
2309 -- Attach it to the outer record controller to the external
2312 if Outer_Typ = Init_Typ then
2317 F => External_Final_List,
2322 Inner_Typ := Init_Typ;
2329 F => External_Final_List,
2333 Inner_Typ := Etype (Outer_Typ);
2335 not Is_Tagged_Type (Typ) or else Inner_Typ = Outer_Typ;
2338 -- The outer object has to be attached as well
2340 if Is_Controlled (Typ) then
2341 Ref := New_Copy_Tree (Target);
2342 Set_Assignment_OK (Ref);
2346 Flist_Ref => New_Copy_Tree (External_Final_List),
2347 With_Attach => New_Copy_Tree (Attach)));
2350 -- Initialize the internal controllers for tagged types with
2351 -- more than one controller.
2353 while not At_Root and then Inner_Typ /= Init_Typ loop
2354 if Has_New_Controlled_Component (Inner_Typ) then
2356 Make_Selected_Component (Loc,
2358 Convert_To (Outer_Typ, New_Copy_Tree (Target)),
2360 Make_Identifier (Loc, Name_uController));
2362 Make_Selected_Component (Loc,
2364 Selector_Name => Make_Identifier (Loc, Name_F));
2371 Attach => Make_Integer_Literal (Loc, 1),
2373 Outer_Typ := Inner_Typ;
2378 At_Root := Inner_Typ = Etype (Inner_Typ);
2379 Inner_Typ := Etype (Inner_Typ);
2382 -- If not done yet attach the controller of the ancestor part
2384 if Outer_Typ /= Init_Typ
2385 and then Inner_Typ = Init_Typ
2386 and then Has_Controlled_Component (Init_Typ)
2389 Make_Selected_Component (Loc,
2390 Prefix => Convert_To (Outer_Typ, New_Copy_Tree (Target)),
2392 Make_Identifier (Loc, Name_uController));
2394 Make_Selected_Component (Loc,
2396 Selector_Name => Make_Identifier (Loc, Name_F));
2398 Attach := Make_Integer_Literal (Loc, 1);
2407 -- Note: Init_Pr is False because the ancestor part has
2408 -- already been initialized either way (by default, if
2409 -- given by a type name, otherwise from the expression).
2414 end Gen_Ctrl_Actions_For_Aggr;
2416 function Rewrite_Discriminant (Expr : Node_Id) return Traverse_Result;
2417 -- If default expression of a component mentions a discriminant of the
2418 -- type, it must be rewritten as the discriminant of the target object.
2420 function Replace_Type (Expr : Node_Id) return Traverse_Result;
2421 -- If the aggregate contains a self-reference, traverse each expression
2422 -- to replace a possible self-reference with a reference to the proper
2423 -- component of the target of the assignment.
2425 --------------------------
2426 -- Rewrite_Discriminant --
2427 --------------------------
2429 function Rewrite_Discriminant (Expr : Node_Id) return Traverse_Result is
2431 if Is_Entity_Name (Expr)
2432 and then Present (Entity (Expr))
2433 and then Ekind (Entity (Expr)) = E_In_Parameter
2434 and then Present (Discriminal_Link (Entity (Expr)))
2435 and then Scope (Discriminal_Link (Entity (Expr)))
2436 = Base_Type (Etype (N))
2439 Make_Selected_Component (Loc,
2440 Prefix => New_Copy_Tree (Lhs),
2441 Selector_Name => Make_Identifier (Loc, Chars (Expr))));
2444 end Rewrite_Discriminant;
2450 function Replace_Type (Expr : Node_Id) return Traverse_Result is
2452 -- Note regarding the Root_Type test below: Aggregate components for
2453 -- self-referential types include attribute references to the current
2454 -- instance, of the form: Typ'access, etc.. These references are
2455 -- rewritten as references to the target of the aggregate: the
2456 -- left-hand side of an assignment, the entity in a declaration,
2457 -- or a temporary. Without this test, we would improperly extended
2458 -- this rewriting to attribute references whose prefix was not the
2459 -- type of the aggregate.
2461 if Nkind (Expr) = N_Attribute_Reference
2462 and then Is_Entity_Name (Prefix (Expr))
2463 and then Is_Type (Entity (Prefix (Expr)))
2464 and then Root_Type (Etype (N)) = Root_Type (Entity (Prefix (Expr)))
2466 if Is_Entity_Name (Lhs) then
2467 Rewrite (Prefix (Expr),
2468 New_Occurrence_Of (Entity (Lhs), Loc));
2470 elsif Nkind (Lhs) = N_Selected_Component then
2472 Make_Attribute_Reference (Loc,
2473 Attribute_Name => Name_Unrestricted_Access,
2474 Prefix => New_Copy_Tree (Prefix (Lhs))));
2475 Set_Analyzed (Parent (Expr), False);
2479 Make_Attribute_Reference (Loc,
2480 Attribute_Name => Name_Unrestricted_Access,
2481 Prefix => New_Copy_Tree (Lhs)));
2482 Set_Analyzed (Parent (Expr), False);
2489 procedure Replace_Self_Reference is
2490 new Traverse_Proc (Replace_Type);
2492 procedure Replace_Discriminants is
2493 new Traverse_Proc (Rewrite_Discriminant);
2495 -- Start of processing for Build_Record_Aggr_Code
2498 if Has_Self_Reference (N) then
2499 Replace_Self_Reference (N);
2502 -- If the target of the aggregate is class-wide, we must convert it
2503 -- to the actual type of the aggregate, so that the proper components
2504 -- are visible. We know already that the types are compatible.
2506 if Present (Etype (Lhs))
2507 and then Is_Class_Wide_Type (Etype (Lhs))
2509 Target := Unchecked_Convert_To (Typ, Lhs);
2514 -- Deal with the ancestor part of extension aggregates or with the
2515 -- discriminants of the root type.
2517 if Nkind (N) = N_Extension_Aggregate then
2519 A : constant Node_Id := Ancestor_Part (N);
2523 -- If the ancestor part is a subtype mark "T", we generate
2525 -- init-proc (T(tmp)); if T is constrained and
2526 -- init-proc (S(tmp)); where S applies an appropriate
2527 -- constraint if T is unconstrained
2529 if Is_Entity_Name (A) and then Is_Type (Entity (A)) then
2530 Ancestor_Is_Subtype_Mark := True;
2532 if Is_Constrained (Entity (A)) then
2533 Init_Typ := Entity (A);
2535 -- For an ancestor part given by an unconstrained type mark,
2536 -- create a subtype constrained by appropriate corresponding
2537 -- discriminant values coming from either associations of the
2538 -- aggregate or a constraint on a parent type. The subtype will
2539 -- be used to generate the correct default value for the
2542 elsif Has_Discriminants (Entity (A)) then
2544 Anc_Typ : constant Entity_Id := Entity (A);
2545 Anc_Constr : constant List_Id := New_List;
2546 Discrim : Entity_Id;
2547 Disc_Value : Node_Id;
2548 New_Indic : Node_Id;
2549 Subt_Decl : Node_Id;
2552 Discrim := First_Discriminant (Anc_Typ);
2553 while Present (Discrim) loop
2554 Disc_Value := Ancestor_Discriminant_Value (Discrim);
2555 Append_To (Anc_Constr, Disc_Value);
2556 Next_Discriminant (Discrim);
2560 Make_Subtype_Indication (Loc,
2561 Subtype_Mark => New_Occurrence_Of (Anc_Typ, Loc),
2563 Make_Index_Or_Discriminant_Constraint (Loc,
2564 Constraints => Anc_Constr));
2566 Init_Typ := Create_Itype (Ekind (Anc_Typ), N);
2569 Make_Subtype_Declaration (Loc,
2570 Defining_Identifier => Init_Typ,
2571 Subtype_Indication => New_Indic);
2573 -- Itypes must be analyzed with checks off Declaration
2574 -- must have a parent for proper handling of subsidiary
2577 Set_Parent (Subt_Decl, N);
2578 Analyze (Subt_Decl, Suppress => All_Checks);
2582 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
2583 Set_Assignment_OK (Ref);
2585 if not Is_Interface (Init_Typ) then
2587 Build_Initialization_Call (Loc,
2590 In_Init_Proc => Within_Init_Proc,
2591 With_Default_Init => Has_Default_Init_Comps (N)
2593 Has_Task (Base_Type (Init_Typ))));
2595 if Is_Constrained (Entity (A))
2596 and then Has_Discriminants (Entity (A))
2598 Check_Ancestor_Discriminants (Entity (A));
2602 -- Handle calls to C++ constructors
2604 elsif Is_CPP_Constructor_Call (A) then
2605 Init_Typ := Etype (A);
2606 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
2607 Set_Assignment_OK (Ref);
2610 Build_Initialization_Call (Loc,
2613 In_Init_Proc => Within_Init_Proc,
2614 With_Default_Init => Has_Default_Init_Comps (N),
2615 Constructor_Ref => A));
2617 -- Ada 2005 (AI-287): If the ancestor part is an aggregate of
2618 -- limited type, a recursive call expands the ancestor. Note that
2619 -- in the limited case, the ancestor part must be either a
2620 -- function call (possibly qualified, or wrapped in an unchecked
2621 -- conversion) or aggregate (definitely qualified).
2622 -- The ancestor part can also be a function call (that may be
2623 -- transformed into an explicit dereference) or a qualification
2626 elsif Is_Limited_Type (Etype (A))
2627 and then Nkind_In (Unqualify (A), N_Aggregate,
2628 N_Extension_Aggregate)
2630 Ancestor_Is_Expression := True;
2632 -- Set up finalization data for enclosing record, because
2633 -- controlled subcomponents of the ancestor part will be
2636 Gen_Ctrl_Actions_For_Aggr;
2639 Build_Record_Aggr_Code (
2641 Typ => Etype (Unqualify (A)),
2645 Is_Limited_Ancestor_Expansion => True));
2647 -- If the ancestor part is an expression "E", we generate
2651 -- In Ada 2005, this includes the case of a (possibly qualified)
2652 -- limited function call. The assignment will turn into a
2653 -- build-in-place function call (for further details, see
2654 -- Make_Build_In_Place_Call_In_Assignment).
2657 Ancestor_Is_Expression := True;
2658 Init_Typ := Etype (A);
2660 -- If the ancestor part is an aggregate, force its full
2661 -- expansion, which was delayed.
2663 if Nkind_In (Unqualify (A), N_Aggregate,
2664 N_Extension_Aggregate)
2666 Set_Analyzed (A, False);
2667 Set_Analyzed (Expression (A), False);
2670 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
2671 Set_Assignment_OK (Ref);
2673 -- Make the assignment without usual controlled actions since
2674 -- we only want the post adjust but not the pre finalize here
2675 -- Add manual adjust when necessary.
2677 Assign := New_List (
2678 Make_OK_Assignment_Statement (Loc,
2681 Set_No_Ctrl_Actions (First (Assign));
2683 -- Assign the tag now to make sure that the dispatching call in
2684 -- the subsequent deep_adjust works properly (unless VM_Target,
2685 -- where tags are implicit).
2687 if Tagged_Type_Expansion then
2689 Make_OK_Assignment_Statement (Loc,
2691 Make_Selected_Component (Loc,
2692 Prefix => New_Copy_Tree (Target),
2695 (First_Tag_Component (Base_Type (Typ)), Loc)),
2698 Unchecked_Convert_To (RTE (RE_Tag),
2701 (Access_Disp_Table (Base_Type (Typ)))),
2704 Set_Assignment_OK (Name (Instr));
2705 Append_To (Assign, Instr);
2707 -- Ada 2005 (AI-251): If tagged type has progenitors we must
2708 -- also initialize tags of the secondary dispatch tables.
2710 if Has_Interfaces (Base_Type (Typ)) then
2712 (Typ => Base_Type (Typ),
2714 Stmts_List => Assign);
2718 -- Call Adjust manually
2720 if Needs_Finalization (Etype (A))
2721 and then not Is_Limited_Type (Etype (A))
2723 Append_List_To (Assign,
2725 Ref => New_Copy_Tree (Ref),
2727 Flist_Ref => New_Reference_To (
2728 RTE (RE_Global_Final_List), Loc),
2729 With_Attach => Make_Integer_Literal (Loc, 0)));
2733 Make_Unsuppress_Block (Loc, Name_Discriminant_Check, Assign));
2735 if Has_Discriminants (Init_Typ) then
2736 Check_Ancestor_Discriminants (Init_Typ);
2741 -- Normal case (not an extension aggregate)
2744 -- Generate the discriminant expressions, component by component.
2745 -- If the base type is an unchecked union, the discriminants are
2746 -- unknown to the back-end and absent from a value of the type, so
2747 -- assignments for them are not emitted.
2749 if Has_Discriminants (Typ)
2750 and then not Is_Unchecked_Union (Base_Type (Typ))
2752 -- If the type is derived, and constrains discriminants of the
2753 -- parent type, these discriminants are not components of the
2754 -- aggregate, and must be initialized explicitly. They are not
2755 -- visible components of the object, but can become visible with
2756 -- a view conversion to the ancestor.
2760 Parent_Type : Entity_Id;
2762 Discr_Val : Elmt_Id;
2765 Btype := Base_Type (Typ);
2766 while Is_Derived_Type (Btype)
2767 and then Present (Stored_Constraint (Btype))
2769 Parent_Type := Etype (Btype);
2771 Disc := First_Discriminant (Parent_Type);
2773 First_Elmt (Stored_Constraint (Base_Type (Typ)));
2774 while Present (Discr_Val) loop
2776 -- Only those discriminants of the parent that are not
2777 -- renamed by discriminants of the derived type need to
2778 -- be added explicitly.
2780 if not Is_Entity_Name (Node (Discr_Val))
2782 Ekind (Entity (Node (Discr_Val))) /= E_Discriminant
2785 Make_Selected_Component (Loc,
2786 Prefix => New_Copy_Tree (Target),
2787 Selector_Name => New_Occurrence_Of (Disc, Loc));
2790 Make_OK_Assignment_Statement (Loc,
2792 Expression => New_Copy_Tree (Node (Discr_Val)));
2794 Set_No_Ctrl_Actions (Instr);
2795 Append_To (L, Instr);
2798 Next_Discriminant (Disc);
2799 Next_Elmt (Discr_Val);
2802 Btype := Base_Type (Parent_Type);
2806 -- Generate discriminant init values for the visible discriminants
2809 Discriminant : Entity_Id;
2810 Discriminant_Value : Node_Id;
2813 Discriminant := First_Stored_Discriminant (Typ);
2814 while Present (Discriminant) loop
2816 Make_Selected_Component (Loc,
2817 Prefix => New_Copy_Tree (Target),
2818 Selector_Name => New_Occurrence_Of (Discriminant, Loc));
2820 Discriminant_Value :=
2821 Get_Discriminant_Value (
2824 Discriminant_Constraint (N_Typ));
2827 Make_OK_Assignment_Statement (Loc,
2829 Expression => New_Copy_Tree (Discriminant_Value));
2831 Set_No_Ctrl_Actions (Instr);
2832 Append_To (L, Instr);
2834 Next_Stored_Discriminant (Discriminant);
2840 -- For CPP types we generate an implicit call to the C++ default
2841 -- constructor to ensure the proper initialization of the _Tag
2844 if Is_CPP_Class (Root_Type (Typ))
2845 and then CPP_Num_Prims (Typ) > 0
2847 Invoke_Constructor : declare
2848 CPP_Parent : constant Entity_Id :=
2849 Enclosing_CPP_Parent (Typ);
2851 procedure Invoke_IC_Proc (T : Entity_Id);
2852 -- Recursive routine used to climb to parents. Required because
2853 -- parents must be initialized before descendants to ensure
2854 -- propagation of inherited C++ slots.
2856 --------------------
2857 -- Invoke_IC_Proc --
2858 --------------------
2860 procedure Invoke_IC_Proc (T : Entity_Id) is
2862 -- Avoid generating extra calls. Initialization required
2863 -- only for types defined from the level of derivation of
2864 -- type of the constructor and the type of the aggregate.
2866 if T = CPP_Parent then
2870 Invoke_IC_Proc (Etype (T));
2872 -- Generate call to the IC routine
2874 if Present (CPP_Init_Proc (T)) then
2876 Make_Procedure_Call_Statement (Loc,
2877 New_Reference_To (CPP_Init_Proc (T), Loc)));
2881 -- Start of processing for Invoke_Constructor
2884 -- Implicit invocation of the C++ constructor
2886 if Nkind (N) = N_Aggregate then
2888 Make_Procedure_Call_Statement (Loc,
2891 (Base_Init_Proc (CPP_Parent), Loc),
2892 Parameter_Associations => New_List (
2893 Unchecked_Convert_To (CPP_Parent,
2894 New_Copy_Tree (Lhs)))));
2897 Invoke_IC_Proc (Typ);
2898 end Invoke_Constructor;
2901 -- Generate the assignments, component by component
2903 -- tmp.comp1 := Expr1_From_Aggr;
2904 -- tmp.comp2 := Expr2_From_Aggr;
2907 Comp := First (Component_Associations (N));
2908 while Present (Comp) loop
2909 Selector := Entity (First (Choices (Comp)));
2913 if Is_CPP_Constructor_Call (Expression (Comp)) then
2915 Build_Initialization_Call (Loc,
2916 Id_Ref => Make_Selected_Component (Loc,
2917 Prefix => New_Copy_Tree (Target),
2919 New_Occurrence_Of (Selector, Loc)),
2920 Typ => Etype (Selector),
2922 With_Default_Init => True,
2923 Constructor_Ref => Expression (Comp)));
2925 -- Ada 2005 (AI-287): For each default-initialized component generate
2926 -- a call to the corresponding IP subprogram if available.
2928 elsif Box_Present (Comp)
2929 and then Has_Non_Null_Base_Init_Proc (Etype (Selector))
2931 if Ekind (Selector) /= E_Discriminant then
2932 Gen_Ctrl_Actions_For_Aggr;
2935 -- Ada 2005 (AI-287): If the component type has tasks then
2936 -- generate the activation chain and master entities (except
2937 -- in case of an allocator because in that case these entities
2938 -- are generated by Build_Task_Allocate_Block_With_Init_Stmts).
2941 Ctype : constant Entity_Id := Etype (Selector);
2942 Inside_Allocator : Boolean := False;
2943 P : Node_Id := Parent (N);
2946 if Is_Task_Type (Ctype) or else Has_Task (Ctype) then
2947 while Present (P) loop
2948 if Nkind (P) = N_Allocator then
2949 Inside_Allocator := True;
2956 if not Inside_Init_Proc and not Inside_Allocator then
2957 Build_Activation_Chain_Entity (N);
2963 Build_Initialization_Call (Loc,
2964 Id_Ref => Make_Selected_Component (Loc,
2965 Prefix => New_Copy_Tree (Target),
2967 New_Occurrence_Of (Selector, Loc)),
2968 Typ => Etype (Selector),
2970 With_Default_Init => True));
2972 -- Prepare for component assignment
2974 elsif Ekind (Selector) /= E_Discriminant
2975 or else Nkind (N) = N_Extension_Aggregate
2977 -- All the discriminants have now been assigned
2979 -- This is now a good moment to initialize and attach all the
2980 -- controllers. Their position may depend on the discriminants.
2982 if Ekind (Selector) /= E_Discriminant then
2983 Gen_Ctrl_Actions_For_Aggr;
2986 Comp_Type := Etype (Selector);
2988 Make_Selected_Component (Loc,
2989 Prefix => New_Copy_Tree (Target),
2990 Selector_Name => New_Occurrence_Of (Selector, Loc));
2992 if Nkind (Expression (Comp)) = N_Qualified_Expression then
2993 Expr_Q := Expression (Expression (Comp));
2995 Expr_Q := Expression (Comp);
2998 -- The controller is the one of the parent type defining the
2999 -- component (in case of inherited components).
3001 if Needs_Finalization (Comp_Type) then
3002 Internal_Final_List :=
3003 Make_Selected_Component (Loc,
3004 Prefix => Convert_To (
3005 Scope (Original_Record_Component (Selector)),
3006 New_Copy_Tree (Target)),
3008 Make_Identifier (Loc, Name_uController));
3010 Internal_Final_List :=
3011 Make_Selected_Component (Loc,
3012 Prefix => Internal_Final_List,
3013 Selector_Name => Make_Identifier (Loc, Name_F));
3015 -- The internal final list can be part of a constant object
3017 Set_Assignment_OK (Internal_Final_List);
3020 Internal_Final_List := Empty;
3023 -- Now either create the assignment or generate the code for the
3024 -- inner aggregate top-down.
3026 if Is_Delayed_Aggregate (Expr_Q) then
3028 -- We have the following case of aggregate nesting inside
3029 -- an object declaration:
3031 -- type Arr_Typ is array (Integer range <>) of ...;
3033 -- type Rec_Typ (...) is record
3034 -- Obj_Arr_Typ : Arr_Typ (A .. B);
3037 -- Obj_Rec_Typ : Rec_Typ := (...,
3038 -- Obj_Arr_Typ => (X => (...), Y => (...)));
3040 -- The length of the ranges of the aggregate and Obj_Add_Typ
3041 -- are equal (B - A = Y - X), but they do not coincide (X /=
3042 -- A and B /= Y). This case requires array sliding which is
3043 -- performed in the following manner:
3045 -- subtype Arr_Sub is Arr_Typ (X .. Y);
3047 -- Temp (X) := (...);
3049 -- Temp (Y) := (...);
3050 -- Obj_Rec_Typ.Obj_Arr_Typ := Temp;
3052 if Ekind (Comp_Type) = E_Array_Subtype
3053 and then Is_Int_Range_Bounds (Aggregate_Bounds (Expr_Q))
3054 and then Is_Int_Range_Bounds (First_Index (Comp_Type))
3056 Compatible_Int_Bounds
3057 (Agg_Bounds => Aggregate_Bounds (Expr_Q),
3058 Typ_Bounds => First_Index (Comp_Type))
3060 -- Create the array subtype with bounds equal to those of
3061 -- the corresponding aggregate.
3064 SubE : constant Entity_Id := Make_Temporary (Loc, 'T');
3066 SubD : constant Node_Id :=
3067 Make_Subtype_Declaration (Loc,
3068 Defining_Identifier => SubE,
3069 Subtype_Indication =>
3070 Make_Subtype_Indication (Loc,
3073 (Etype (Comp_Type), Loc),
3075 Make_Index_Or_Discriminant_Constraint
3077 Constraints => New_List (
3079 (Aggregate_Bounds (Expr_Q))))));
3081 -- Create a temporary array of the above subtype which
3082 -- will be used to capture the aggregate assignments.
3084 TmpE : constant Entity_Id := Make_Temporary (Loc, 'A', N);
3086 TmpD : constant Node_Id :=
3087 Make_Object_Declaration (Loc,
3088 Defining_Identifier => TmpE,
3089 Object_Definition =>
3090 New_Reference_To (SubE, Loc));
3093 Set_No_Initialization (TmpD);
3094 Append_To (L, SubD);
3095 Append_To (L, TmpD);
3097 -- Expand aggregate into assignments to the temp array
3100 Late_Expansion (Expr_Q, Comp_Type,
3101 New_Reference_To (TmpE, Loc), Internal_Final_List));
3106 Make_Assignment_Statement (Loc,
3107 Name => New_Copy_Tree (Comp_Expr),
3108 Expression => New_Reference_To (TmpE, Loc)));
3110 -- Do not pass the original aggregate to Gigi as is,
3111 -- since it will potentially clobber the front or the end
3112 -- of the array. Setting the expression to empty is safe
3113 -- since all aggregates are expanded into assignments.
3115 if Present (Obj) then
3116 Set_Expression (Parent (Obj), Empty);
3120 -- Normal case (sliding not required)
3124 Late_Expansion (Expr_Q, Comp_Type, Comp_Expr,
3125 Internal_Final_List));
3128 -- Expr_Q is not delayed aggregate
3131 if Has_Discriminants (Typ) then
3132 Replace_Discriminants (Expr_Q);
3136 Make_OK_Assignment_Statement (Loc,
3138 Expression => Expr_Q);
3140 Set_No_Ctrl_Actions (Instr);
3141 Append_To (L, Instr);
3143 -- Adjust the tag if tagged (because of possible view
3144 -- conversions), unless compiling for a VM where tags are
3147 -- tmp.comp._tag := comp_typ'tag;
3149 if Is_Tagged_Type (Comp_Type)
3150 and then Tagged_Type_Expansion
3153 Make_OK_Assignment_Statement (Loc,
3155 Make_Selected_Component (Loc,
3156 Prefix => New_Copy_Tree (Comp_Expr),
3159 (First_Tag_Component (Comp_Type), Loc)),
3162 Unchecked_Convert_To (RTE (RE_Tag),
3164 (Node (First_Elmt (Access_Disp_Table (Comp_Type))),
3167 Append_To (L, Instr);
3170 -- Adjust and Attach the component to the proper controller
3172 -- Adjust (tmp.comp);
3173 -- Attach_To_Final_List (tmp.comp,
3174 -- comp_typ (tmp)._record_controller.f)
3176 if Needs_Finalization (Comp_Type)
3177 and then not Is_Limited_Type (Comp_Type)
3181 Ref => New_Copy_Tree (Comp_Expr),
3183 Flist_Ref => Internal_Final_List,
3184 With_Attach => Make_Integer_Literal (Loc, 1)));
3190 elsif Ekind (Selector) = E_Discriminant
3191 and then Nkind (N) /= N_Extension_Aggregate
3192 and then Nkind (Parent (N)) = N_Component_Association
3193 and then Is_Constrained (Typ)
3195 -- We must check that the discriminant value imposed by the
3196 -- context is the same as the value given in the subaggregate,
3197 -- because after the expansion into assignments there is no
3198 -- record on which to perform a regular discriminant check.
3205 D_Val := First_Elmt (Discriminant_Constraint (Typ));
3206 Disc := First_Discriminant (Typ);
3207 while Chars (Disc) /= Chars (Selector) loop
3208 Next_Discriminant (Disc);
3212 pragma Assert (Present (D_Val));
3214 -- This check cannot performed for components that are
3215 -- constrained by a current instance, because this is not a
3216 -- value that can be compared with the actual constraint.
3218 if Nkind (Node (D_Val)) /= N_Attribute_Reference
3219 or else not Is_Entity_Name (Prefix (Node (D_Val)))
3220 or else not Is_Type (Entity (Prefix (Node (D_Val))))
3223 Make_Raise_Constraint_Error (Loc,
3226 Left_Opnd => New_Copy_Tree (Node (D_Val)),
3227 Right_Opnd => Expression (Comp)),
3228 Reason => CE_Discriminant_Check_Failed));
3231 -- Find self-reference in previous discriminant assignment,
3232 -- and replace with proper expression.
3239 while Present (Ass) loop
3240 if Nkind (Ass) = N_Assignment_Statement
3241 and then Nkind (Name (Ass)) = N_Selected_Component
3242 and then Chars (Selector_Name (Name (Ass))) =
3246 (Ass, New_Copy_Tree (Expression (Comp)));
3259 -- If the type is tagged, the tag needs to be initialized (unless
3260 -- compiling for the Java VM where tags are implicit). It is done
3261 -- late in the initialization process because in some cases, we call
3262 -- the init proc of an ancestor which will not leave out the right tag
3264 if Ancestor_Is_Expression then
3267 -- For CPP types we generated a call to the C++ default constructor
3268 -- before the components have been initialized to ensure the proper
3269 -- initialization of the _Tag component (see above).
3271 elsif Is_CPP_Class (Typ) then
3274 elsif Is_Tagged_Type (Typ) and then Tagged_Type_Expansion then
3276 Make_OK_Assignment_Statement (Loc,
3278 Make_Selected_Component (Loc,
3279 Prefix => New_Copy_Tree (Target),
3282 (First_Tag_Component (Base_Type (Typ)), Loc)),
3285 Unchecked_Convert_To (RTE (RE_Tag),
3287 (Node (First_Elmt (Access_Disp_Table (Base_Type (Typ)))),
3290 Append_To (L, Instr);
3292 -- Ada 2005 (AI-251): If the tagged type has been derived from
3293 -- abstract interfaces we must also initialize the tags of the
3294 -- secondary dispatch tables.
3296 if Has_Interfaces (Base_Type (Typ)) then
3298 (Typ => Base_Type (Typ),
3304 -- If the controllers have not been initialized yet (by lack of non-
3305 -- discriminant components), let's do it now.
3307 Gen_Ctrl_Actions_For_Aggr;
3310 end Build_Record_Aggr_Code;
3312 -------------------------------
3313 -- Convert_Aggr_In_Allocator --
3314 -------------------------------
3316 procedure Convert_Aggr_In_Allocator
3321 Loc : constant Source_Ptr := Sloc (Aggr);
3322 Typ : constant Entity_Id := Etype (Aggr);
3323 Temp : constant Entity_Id := Defining_Identifier (Decl);
3325 Occ : constant Node_Id :=
3326 Unchecked_Convert_To (Typ,
3327 Make_Explicit_Dereference (Loc,
3328 New_Reference_To (Temp, Loc)));
3330 Access_Type : constant Entity_Id := Etype (Temp);
3334 -- If the allocator is for an access discriminant, there is no
3335 -- finalization list for the anonymous access type, and the eventual
3336 -- finalization of the object is handled through the coextension
3337 -- mechanism. If the enclosing object is not dynamically allocated,
3338 -- the access discriminant is itself placed on the stack. Otherwise,
3339 -- some other finalization list is used (see exp_ch4.adb).
3341 -- Decl has been inserted in the code ahead of the allocator, using
3342 -- Insert_Actions. We use Insert_Actions below as well, to ensure that
3343 -- subsequent insertions are done in the proper order. Using (for
3344 -- example) Insert_Actions_After to place the expanded aggregate
3345 -- immediately after Decl may lead to out-of-order references if the
3346 -- allocator has generated a finalization list, as when the designated
3347 -- object is controlled and there is an open transient scope.
3349 if Ekind (Access_Type) = E_Anonymous_Access_Type
3350 and then Nkind (Associated_Node_For_Itype (Access_Type)) =
3351 N_Discriminant_Specification
3355 elsif Needs_Finalization (Typ) then
3356 Flist := Find_Final_List (Access_Type);
3358 -- Otherwise there are no controlled actions to be performed.
3364 if Is_Array_Type (Typ) then
3365 Convert_Array_Aggr_In_Allocator (Decl, Aggr, Occ);
3367 elsif Has_Default_Init_Comps (Aggr) then
3369 L : constant List_Id := New_List;
3370 Init_Stmts : List_Id;
3377 Associated_Final_Chain (Base_Type (Access_Type)));
3379 -- ??? Dubious actual for Obj: expect 'the original object being
3382 if Has_Task (Typ) then
3383 Build_Task_Allocate_Block_With_Init_Stmts (L, Aggr, Init_Stmts);
3384 Insert_Actions (Alloc, L);
3386 Insert_Actions (Alloc, Init_Stmts);
3391 Insert_Actions (Alloc,
3393 (Aggr, Typ, Occ, Flist,
3394 Associated_Final_Chain (Base_Type (Access_Type))));
3396 -- ??? Dubious actual for Obj: expect 'the original object being
3400 end Convert_Aggr_In_Allocator;
3402 --------------------------------
3403 -- Convert_Aggr_In_Assignment --
3404 --------------------------------
3406 procedure Convert_Aggr_In_Assignment (N : Node_Id) is
3407 Aggr : Node_Id := Expression (N);
3408 Typ : constant Entity_Id := Etype (Aggr);
3409 Occ : constant Node_Id := New_Copy_Tree (Name (N));
3412 if Nkind (Aggr) = N_Qualified_Expression then
3413 Aggr := Expression (Aggr);
3416 Insert_Actions_After (N,
3419 Find_Final_List (Typ, New_Copy_Tree (Occ))));
3420 end Convert_Aggr_In_Assignment;
3422 ---------------------------------
3423 -- Convert_Aggr_In_Object_Decl --
3424 ---------------------------------
3426 procedure Convert_Aggr_In_Object_Decl (N : Node_Id) is
3427 Obj : constant Entity_Id := Defining_Identifier (N);
3428 Aggr : Node_Id := Expression (N);
3429 Loc : constant Source_Ptr := Sloc (Aggr);
3430 Typ : constant Entity_Id := Etype (Aggr);
3431 Occ : constant Node_Id := New_Occurrence_Of (Obj, Loc);
3433 function Discriminants_Ok return Boolean;
3434 -- If the object type is constrained, the discriminants in the
3435 -- aggregate must be checked against the discriminants of the subtype.
3436 -- This cannot be done using Apply_Discriminant_Checks because after
3437 -- expansion there is no aggregate left to check.
3439 ----------------------
3440 -- Discriminants_Ok --
3441 ----------------------
3443 function Discriminants_Ok return Boolean is
3444 Cond : Node_Id := Empty;
3453 D := First_Discriminant (Typ);
3454 Disc1 := First_Elmt (Discriminant_Constraint (Typ));
3455 Disc2 := First_Elmt (Discriminant_Constraint (Etype (Obj)));
3456 while Present (Disc1) and then Present (Disc2) loop
3457 Val1 := Node (Disc1);
3458 Val2 := Node (Disc2);
3460 if not Is_OK_Static_Expression (Val1)
3461 or else not Is_OK_Static_Expression (Val2)
3463 Check := Make_Op_Ne (Loc,
3464 Left_Opnd => Duplicate_Subexpr (Val1),
3465 Right_Opnd => Duplicate_Subexpr (Val2));
3471 Cond := Make_Or_Else (Loc,
3473 Right_Opnd => Check);
3476 elsif Expr_Value (Val1) /= Expr_Value (Val2) then
3477 Apply_Compile_Time_Constraint_Error (Aggr,
3478 Msg => "incorrect value for discriminant&?",
3479 Reason => CE_Discriminant_Check_Failed,
3484 Next_Discriminant (D);
3489 -- If any discriminant constraint is non-static, emit a check
3491 if Present (Cond) then
3493 Make_Raise_Constraint_Error (Loc,
3495 Reason => CE_Discriminant_Check_Failed));
3499 end Discriminants_Ok;
3501 -- Start of processing for Convert_Aggr_In_Object_Decl
3504 Set_Assignment_OK (Occ);
3506 if Nkind (Aggr) = N_Qualified_Expression then
3507 Aggr := Expression (Aggr);
3510 if Has_Discriminants (Typ)
3511 and then Typ /= Etype (Obj)
3512 and then Is_Constrained (Etype (Obj))
3513 and then not Discriminants_Ok
3518 -- If the context is an extended return statement, it has its own
3519 -- finalization machinery (i.e. works like a transient scope) and
3520 -- we do not want to create an additional one, because objects on
3521 -- the finalization list of the return must be moved to the caller's
3522 -- finalization list to complete the return.
3524 -- However, if the aggregate is limited, it is built in place, and the
3525 -- controlled components are not assigned to intermediate temporaries
3526 -- so there is no need for a transient scope in this case either.
3528 if Requires_Transient_Scope (Typ)
3529 and then Ekind (Current_Scope) /= E_Return_Statement
3530 and then not Is_Limited_Type (Typ)
3532 Establish_Transient_Scope
3535 Is_Controlled (Typ) or else Has_Controlled_Component (Typ));
3538 Insert_Actions_After (N, Late_Expansion (Aggr, Typ, Occ, Obj => Obj));
3539 Set_No_Initialization (N);
3540 Initialize_Discriminants (N, Typ);
3541 end Convert_Aggr_In_Object_Decl;
3543 -------------------------------------
3544 -- Convert_Array_Aggr_In_Allocator --
3545 -------------------------------------
3547 procedure Convert_Array_Aggr_In_Allocator
3552 Aggr_Code : List_Id;
3553 Typ : constant Entity_Id := Etype (Aggr);
3554 Ctyp : constant Entity_Id := Component_Type (Typ);
3557 -- The target is an explicit dereference of the allocated object.
3558 -- Generate component assignments to it, as for an aggregate that
3559 -- appears on the right-hand side of an assignment statement.
3562 Build_Array_Aggr_Code (Aggr,
3564 Index => First_Index (Typ),
3566 Scalar_Comp => Is_Scalar_Type (Ctyp));
3568 Insert_Actions_After (Decl, Aggr_Code);
3569 end Convert_Array_Aggr_In_Allocator;
3571 ----------------------------
3572 -- Convert_To_Assignments --
3573 ----------------------------
3575 procedure Convert_To_Assignments (N : Node_Id; Typ : Entity_Id) is
3576 Loc : constant Source_Ptr := Sloc (N);
3581 Target_Expr : Node_Id;
3582 Parent_Kind : Node_Kind;
3583 Unc_Decl : Boolean := False;
3584 Parent_Node : Node_Id;
3587 pragma Assert (not Is_Static_Dispatch_Table_Aggregate (N));
3588 pragma Assert (Is_Record_Type (Typ));
3590 Parent_Node := Parent (N);
3591 Parent_Kind := Nkind (Parent_Node);
3593 if Parent_Kind = N_Qualified_Expression then
3595 -- Check if we are in a unconstrained declaration because in this
3596 -- case the current delayed expansion mechanism doesn't work when
3597 -- the declared object size depend on the initializing expr.
3600 Parent_Node := Parent (Parent_Node);
3601 Parent_Kind := Nkind (Parent_Node);
3603 if Parent_Kind = N_Object_Declaration then
3605 not Is_Entity_Name (Object_Definition (Parent_Node))
3606 or else Has_Discriminants
3607 (Entity (Object_Definition (Parent_Node)))
3608 or else Is_Class_Wide_Type
3609 (Entity (Object_Definition (Parent_Node)));
3614 -- Just set the Delay flag in the cases where the transformation will be
3615 -- done top down from above.
3619 -- Internal aggregate (transformed when expanding the parent)
3621 or else Parent_Kind = N_Aggregate
3622 or else Parent_Kind = N_Extension_Aggregate
3623 or else Parent_Kind = N_Component_Association
3625 -- Allocator (see Convert_Aggr_In_Allocator)
3627 or else Parent_Kind = N_Allocator
3629 -- Object declaration (see Convert_Aggr_In_Object_Decl)
3631 or else (Parent_Kind = N_Object_Declaration and then not Unc_Decl)
3633 -- Safe assignment (see Convert_Aggr_Assignments). So far only the
3634 -- assignments in init procs are taken into account.
3636 or else (Parent_Kind = N_Assignment_Statement
3637 and then Inside_Init_Proc)
3639 -- (Ada 2005) An inherently limited type in a return statement,
3640 -- which will be handled in a build-in-place fashion, and may be
3641 -- rewritten as an extended return and have its own finalization
3642 -- machinery. In the case of a simple return, the aggregate needs
3643 -- to be delayed until the scope for the return statement has been
3644 -- created, so that any finalization chain will be associated with
3645 -- that scope. For extended returns, we delay expansion to avoid the
3646 -- creation of an unwanted transient scope that could result in
3647 -- premature finalization of the return object (which is built in
3648 -- in place within the caller's scope).
3651 (Is_Inherently_Limited_Type (Typ)
3653 (Nkind (Parent (Parent_Node)) = N_Extended_Return_Statement
3654 or else Nkind (Parent_Node) = N_Simple_Return_Statement))
3656 Set_Expansion_Delayed (N);
3660 if Requires_Transient_Scope (Typ) then
3661 Establish_Transient_Scope
3663 Is_Controlled (Typ) or else Has_Controlled_Component (Typ));
3666 -- If the aggregate is non-limited, create a temporary. If it is limited
3667 -- and the context is an assignment, this is a subaggregate for an
3668 -- enclosing aggregate being expanded. It must be built in place, so use
3669 -- the target of the current assignment.
3671 if Is_Limited_Type (Typ)
3672 and then Nkind (Parent (N)) = N_Assignment_Statement
3674 Target_Expr := New_Copy_Tree (Name (Parent (N)));
3676 (Parent (N), Build_Record_Aggr_Code (N, Typ, Target_Expr));
3677 Rewrite (Parent (N), Make_Null_Statement (Loc));
3680 Temp := Make_Temporary (Loc, 'A', N);
3682 -- If the type inherits unknown discriminants, use the view with
3683 -- known discriminants if available.
3685 if Has_Unknown_Discriminants (Typ)
3686 and then Present (Underlying_Record_View (Typ))
3688 T := Underlying_Record_View (Typ);
3694 Make_Object_Declaration (Loc,
3695 Defining_Identifier => Temp,
3696 Object_Definition => New_Occurrence_Of (T, Loc));
3698 Set_No_Initialization (Instr);
3699 Insert_Action (N, Instr);
3700 Initialize_Discriminants (Instr, T);
3701 Target_Expr := New_Occurrence_Of (Temp, Loc);
3702 Insert_Actions (N, Build_Record_Aggr_Code (N, T, Target_Expr));
3703 Rewrite (N, New_Occurrence_Of (Temp, Loc));
3704 Analyze_And_Resolve (N, T);
3706 end Convert_To_Assignments;
3708 ---------------------------
3709 -- Convert_To_Positional --
3710 ---------------------------
3712 procedure Convert_To_Positional
3714 Max_Others_Replicate : Nat := 5;
3715 Handle_Bit_Packed : Boolean := False)
3717 Typ : constant Entity_Id := Etype (N);
3719 Static_Components : Boolean := True;
3721 procedure Check_Static_Components;
3722 -- Check whether all components of the aggregate are compile-time known
3723 -- values, and can be passed as is to the back-end without further
3729 Ixb : Node_Id) return Boolean;
3730 -- Convert the aggregate into a purely positional form if possible. On
3731 -- entry the bounds of all dimensions are known to be static, and the
3732 -- total number of components is safe enough to expand.
3734 function Is_Flat (N : Node_Id; Dims : Int) return Boolean;
3735 -- Return True iff the array N is flat (which is not trivial in the case
3736 -- of multidimensionsl aggregates).
3738 -----------------------------
3739 -- Check_Static_Components --
3740 -----------------------------
3742 procedure Check_Static_Components is
3746 Static_Components := True;
3748 if Nkind (N) = N_String_Literal then
3751 elsif Present (Expressions (N)) then
3752 Expr := First (Expressions (N));
3753 while Present (Expr) loop
3754 if Nkind (Expr) /= N_Aggregate
3755 or else not Compile_Time_Known_Aggregate (Expr)
3756 or else Expansion_Delayed (Expr)
3758 Static_Components := False;
3766 if Nkind (N) = N_Aggregate
3767 and then Present (Component_Associations (N))
3769 Expr := First (Component_Associations (N));
3770 while Present (Expr) loop
3771 if Nkind_In (Expression (Expr), N_Integer_Literal,
3776 elsif Nkind (Expression (Expr)) /= N_Aggregate
3777 or else not Compile_Time_Known_Aggregate (Expression (Expr))
3778 or else Expansion_Delayed (Expression (Expr))
3780 Static_Components := False;
3787 end Check_Static_Components;
3796 Ixb : Node_Id) return Boolean
3798 Loc : constant Source_Ptr := Sloc (N);
3799 Blo : constant Node_Id := Type_Low_Bound (Etype (Ixb));
3800 Lo : constant Node_Id := Type_Low_Bound (Etype (Ix));
3801 Hi : constant Node_Id := Type_High_Bound (Etype (Ix));
3806 if Nkind (Original_Node (N)) = N_String_Literal then
3810 if not Compile_Time_Known_Value (Lo)
3811 or else not Compile_Time_Known_Value (Hi)
3816 Lov := Expr_Value (Lo);
3817 Hiv := Expr_Value (Hi);
3820 or else not Compile_Time_Known_Value (Blo)
3825 -- Determine if set of alternatives is suitable for conversion and
3826 -- build an array containing the values in sequence.
3829 Vals : array (UI_To_Int (Lov) .. UI_To_Int (Hiv))
3830 of Node_Id := (others => Empty);
3831 -- The values in the aggregate sorted appropriately
3834 -- Same data as Vals in list form
3837 -- Used to validate Max_Others_Replicate limit
3840 Num : Int := UI_To_Int (Lov);
3846 if Present (Expressions (N)) then
3847 Elmt := First (Expressions (N));
3848 while Present (Elmt) loop
3849 if Nkind (Elmt) = N_Aggregate
3850 and then Present (Next_Index (Ix))
3852 not Flatten (Elmt, Next_Index (Ix), Next_Index (Ixb))
3857 Vals (Num) := Relocate_Node (Elmt);
3864 if No (Component_Associations (N)) then
3868 Elmt := First (Component_Associations (N));
3870 if Nkind (Expression (Elmt)) = N_Aggregate then
3871 if Present (Next_Index (Ix))
3874 (Expression (Elmt), Next_Index (Ix), Next_Index (Ixb))
3880 Component_Loop : while Present (Elmt) loop
3881 Choice := First (Choices (Elmt));
3882 Choice_Loop : while Present (Choice) loop
3884 -- If we have an others choice, fill in the missing elements
3885 -- subject to the limit established by Max_Others_Replicate.
3887 if Nkind (Choice) = N_Others_Choice then
3890 for J in Vals'Range loop
3891 if No (Vals (J)) then
3892 Vals (J) := New_Copy_Tree (Expression (Elmt));
3893 Rep_Count := Rep_Count + 1;
3895 -- Check for maximum others replication. Note that
3896 -- we skip this test if either of the restrictions
3897 -- No_Elaboration_Code or No_Implicit_Loops is
3898 -- active, if this is a preelaborable unit or a
3899 -- predefined unit. This ensures that predefined
3900 -- units get the same level of constant folding in
3901 -- Ada 95 and Ada 05, where their categorization
3905 P : constant Entity_Id :=
3906 Cunit_Entity (Current_Sem_Unit);
3909 -- Check if duplication OK and if so continue
3912 if Restriction_Active (No_Elaboration_Code)
3913 or else Restriction_Active (No_Implicit_Loops)
3914 or else Is_Preelaborated (P)
3915 or else (Ekind (P) = E_Package_Body
3917 Is_Preelaborated (Spec_Entity (P)))
3919 Is_Predefined_File_Name
3920 (Unit_File_Name (Get_Source_Unit (P)))
3924 -- If duplication not OK, then we return False
3925 -- if the replication count is too high
3927 elsif Rep_Count > Max_Others_Replicate then
3930 -- Continue on if duplication not OK, but the
3931 -- replication count is not excessive.
3940 exit Component_Loop;
3942 -- Case of a subtype mark
3944 elsif Nkind (Choice) = N_Identifier
3945 and then Is_Type (Entity (Choice))
3947 Lo := Type_Low_Bound (Etype (Choice));
3948 Hi := Type_High_Bound (Etype (Choice));
3950 -- Case of subtype indication
3952 elsif Nkind (Choice) = N_Subtype_Indication then
3953 Lo := Low_Bound (Range_Expression (Constraint (Choice)));
3954 Hi := High_Bound (Range_Expression (Constraint (Choice)));
3958 elsif Nkind (Choice) = N_Range then
3959 Lo := Low_Bound (Choice);
3960 Hi := High_Bound (Choice);
3962 -- Normal subexpression case
3964 else pragma Assert (Nkind (Choice) in N_Subexpr);
3965 if not Compile_Time_Known_Value (Choice) then
3969 Choice_Index := UI_To_Int (Expr_Value (Choice));
3970 if Choice_Index in Vals'Range then
3971 Vals (Choice_Index) :=
3972 New_Copy_Tree (Expression (Elmt));
3976 -- Choice is statically out-of-range, will be
3977 -- rewritten to raise Constraint_Error.
3984 -- Range cases merge with Lo,Hi set
3986 if not Compile_Time_Known_Value (Lo)
3988 not Compile_Time_Known_Value (Hi)
3992 for J in UI_To_Int (Expr_Value (Lo)) ..
3993 UI_To_Int (Expr_Value (Hi))
3995 Vals (J) := New_Copy_Tree (Expression (Elmt));
4001 end loop Choice_Loop;
4004 end loop Component_Loop;
4006 -- If we get here the conversion is possible
4009 for J in Vals'Range loop
4010 Append (Vals (J), Vlist);
4013 Rewrite (N, Make_Aggregate (Loc, Expressions => Vlist));
4014 Set_Aggregate_Bounds (N, Aggregate_Bounds (Original_Node (N)));
4023 function Is_Flat (N : Node_Id; Dims : Int) return Boolean is
4030 elsif Nkind (N) = N_Aggregate then
4031 if Present (Component_Associations (N)) then
4035 Elmt := First (Expressions (N));
4036 while Present (Elmt) loop
4037 if not Is_Flat (Elmt, Dims - 1) then
4051 -- Start of processing for Convert_To_Positional
4054 -- Ada 2005 (AI-287): Do not convert in case of default initialized
4055 -- components because in this case will need to call the corresponding
4058 if Has_Default_Init_Comps (N) then
4062 if Is_Flat (N, Number_Dimensions (Typ)) then
4066 if Is_Bit_Packed_Array (Typ)
4067 and then not Handle_Bit_Packed
4072 -- Do not convert to positional if controlled components are involved
4073 -- since these require special processing
4075 if Has_Controlled_Component (Typ) then
4079 Check_Static_Components;
4081 -- If the size is known, or all the components are static, try to
4082 -- build a fully positional aggregate.
4084 -- The size of the type may not be known for an aggregate with
4085 -- discriminated array components, but if the components are static
4086 -- it is still possible to verify statically that the length is
4087 -- compatible with the upper bound of the type, and therefore it is
4088 -- worth flattening such aggregates as well.
4090 -- For now the back-end expands these aggregates into individual
4091 -- assignments to the target anyway, but it is conceivable that
4092 -- it will eventually be able to treat such aggregates statically???
4094 if Aggr_Size_OK (N, Typ)
4095 and then Flatten (N, First_Index (Typ), First_Index (Base_Type (Typ)))
4097 if Static_Components then
4098 Set_Compile_Time_Known_Aggregate (N);
4099 Set_Expansion_Delayed (N, False);
4102 Analyze_And_Resolve (N, Typ);
4104 end Convert_To_Positional;
4106 ----------------------------
4107 -- Expand_Array_Aggregate --
4108 ----------------------------
4110 -- Array aggregate expansion proceeds as follows:
4112 -- 1. If requested we generate code to perform all the array aggregate
4113 -- bound checks, specifically
4115 -- (a) Check that the index range defined by aggregate bounds is
4116 -- compatible with corresponding index subtype.
4118 -- (b) If an others choice is present check that no aggregate
4119 -- index is outside the bounds of the index constraint.
4121 -- (c) For multidimensional arrays make sure that all subaggregates
4122 -- corresponding to the same dimension have the same bounds.
4124 -- 2. Check for packed array aggregate which can be converted to a
4125 -- constant so that the aggregate disappeares completely.
4127 -- 3. Check case of nested aggregate. Generally nested aggregates are
4128 -- handled during the processing of the parent aggregate.
4130 -- 4. Check if the aggregate can be statically processed. If this is the
4131 -- case pass it as is to Gigi. Note that a necessary condition for
4132 -- static processing is that the aggregate be fully positional.
4134 -- 5. If in place aggregate expansion is possible (i.e. no need to create
4135 -- a temporary) then mark the aggregate as such and return. Otherwise
4136 -- create a new temporary and generate the appropriate initialization
4139 procedure Expand_Array_Aggregate (N : Node_Id) is
4140 Loc : constant Source_Ptr := Sloc (N);
4142 Typ : constant Entity_Id := Etype (N);
4143 Ctyp : constant Entity_Id := Component_Type (Typ);
4144 -- Typ is the correct constrained array subtype of the aggregate
4145 -- Ctyp is the corresponding component type.
4147 Aggr_Dimension : constant Pos := Number_Dimensions (Typ);
4148 -- Number of aggregate index dimensions
4150 Aggr_Low : array (1 .. Aggr_Dimension) of Node_Id;
4151 Aggr_High : array (1 .. Aggr_Dimension) of Node_Id;
4152 -- Low and High bounds of the constraint for each aggregate index
4154 Aggr_Index_Typ : array (1 .. Aggr_Dimension) of Entity_Id;
4155 -- The type of each index
4157 Maybe_In_Place_OK : Boolean;
4158 -- If the type is neither controlled nor packed and the aggregate
4159 -- is the expression in an assignment, assignment in place may be
4160 -- possible, provided other conditions are met on the LHS.
4162 Others_Present : array (1 .. Aggr_Dimension) of Boolean :=
4164 -- If Others_Present (J) is True, then there is an others choice
4165 -- in one of the sub-aggregates of N at dimension J.
4167 procedure Build_Constrained_Type (Positional : Boolean);
4168 -- If the subtype is not static or unconstrained, build a constrained
4169 -- type using the computable sizes of the aggregate and its sub-
4172 procedure Check_Bounds (Aggr_Bounds : Node_Id; Index_Bounds : Node_Id);
4173 -- Checks that the bounds of Aggr_Bounds are within the bounds defined
4176 procedure Check_Same_Aggr_Bounds (Sub_Aggr : Node_Id; Dim : Pos);
4177 -- Checks that in a multi-dimensional array aggregate all subaggregates
4178 -- corresponding to the same dimension have the same bounds.
4179 -- Sub_Aggr is an array sub-aggregate. Dim is the dimension
4180 -- corresponding to the sub-aggregate.
4182 procedure Compute_Others_Present (Sub_Aggr : Node_Id; Dim : Pos);
4183 -- Computes the values of array Others_Present. Sub_Aggr is the
4184 -- array sub-aggregate we start the computation from. Dim is the
4185 -- dimension corresponding to the sub-aggregate.
4187 function In_Place_Assign_OK return Boolean;
4188 -- Simple predicate to determine whether an aggregate assignment can
4189 -- be done in place, because none of the new values can depend on the
4190 -- components of the target of the assignment.
4192 procedure Others_Check (Sub_Aggr : Node_Id; Dim : Pos);
4193 -- Checks that if an others choice is present in any sub-aggregate no
4194 -- aggregate index is outside the bounds of the index constraint.
4195 -- Sub_Aggr is an array sub-aggregate. Dim is the dimension
4196 -- corresponding to the sub-aggregate.
4198 function Safe_Left_Hand_Side (N : Node_Id) return Boolean;
4199 -- In addition to Maybe_In_Place_OK, in order for an aggregate to be
4200 -- built directly into the target of the assignment it must be free
4203 ----------------------------
4204 -- Build_Constrained_Type --
4205 ----------------------------
4207 procedure Build_Constrained_Type (Positional : Boolean) is
4208 Loc : constant Source_Ptr := Sloc (N);
4209 Agg_Type : constant Entity_Id := Make_Temporary (Loc, 'A');
4212 Typ : constant Entity_Id := Etype (N);
4213 Indices : constant List_Id := New_List;
4218 -- If the aggregate is purely positional, all its subaggregates
4219 -- have the same size. We collect the dimensions from the first
4220 -- subaggregate at each level.
4225 for D in 1 .. Number_Dimensions (Typ) loop
4226 Sub_Agg := First (Expressions (Sub_Agg));
4230 while Present (Comp) loop
4237 Low_Bound => Make_Integer_Literal (Loc, 1),
4238 High_Bound => Make_Integer_Literal (Loc, Num)));
4242 -- We know the aggregate type is unconstrained and the aggregate
4243 -- is not processable by the back end, therefore not necessarily
4244 -- positional. Retrieve each dimension bounds (computed earlier).
4246 for D in 1 .. Number_Dimensions (Typ) loop
4249 Low_Bound => Aggr_Low (D),
4250 High_Bound => Aggr_High (D)),
4256 Make_Full_Type_Declaration (Loc,
4257 Defining_Identifier => Agg_Type,
4259 Make_Constrained_Array_Definition (Loc,
4260 Discrete_Subtype_Definitions => Indices,
4261 Component_Definition =>
4262 Make_Component_Definition (Loc,
4263 Aliased_Present => False,
4264 Subtype_Indication =>
4265 New_Occurrence_Of (Component_Type (Typ), Loc))));
4267 Insert_Action (N, Decl);
4269 Set_Etype (N, Agg_Type);
4270 Set_Is_Itype (Agg_Type);
4271 Freeze_Itype (Agg_Type, N);
4272 end Build_Constrained_Type;
4278 procedure Check_Bounds (Aggr_Bounds : Node_Id; Index_Bounds : Node_Id) is
4285 Cond : Node_Id := Empty;
4288 Get_Index_Bounds (Aggr_Bounds, Aggr_Lo, Aggr_Hi);
4289 Get_Index_Bounds (Index_Bounds, Ind_Lo, Ind_Hi);
4291 -- Generate the following test:
4293 -- [constraint_error when
4294 -- Aggr_Lo <= Aggr_Hi and then
4295 -- (Aggr_Lo < Ind_Lo or else Aggr_Hi > Ind_Hi)]
4297 -- As an optimization try to see if some tests are trivially vacuous
4298 -- because we are comparing an expression against itself.
4300 if Aggr_Lo = Ind_Lo and then Aggr_Hi = Ind_Hi then
4303 elsif Aggr_Hi = Ind_Hi then
4306 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
4307 Right_Opnd => Duplicate_Subexpr_Move_Checks (Ind_Lo));
4309 elsif Aggr_Lo = Ind_Lo then
4312 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi),
4313 Right_Opnd => Duplicate_Subexpr_Move_Checks (Ind_Hi));
4320 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
4321 Right_Opnd => Duplicate_Subexpr_Move_Checks (Ind_Lo)),
4325 Left_Opnd => Duplicate_Subexpr (Aggr_Hi),
4326 Right_Opnd => Duplicate_Subexpr (Ind_Hi)));
4329 if Present (Cond) then
4334 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
4335 Right_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi)),
4337 Right_Opnd => Cond);
4339 Set_Analyzed (Left_Opnd (Left_Opnd (Cond)), False);
4340 Set_Analyzed (Right_Opnd (Left_Opnd (Cond)), False);
4342 Make_Raise_Constraint_Error (Loc,
4344 Reason => CE_Length_Check_Failed));
4348 ----------------------------
4349 -- Check_Same_Aggr_Bounds --
4350 ----------------------------
4352 procedure Check_Same_Aggr_Bounds (Sub_Aggr : Node_Id; Dim : Pos) is
4353 Sub_Lo : constant Node_Id := Low_Bound (Aggregate_Bounds (Sub_Aggr));
4354 Sub_Hi : constant Node_Id := High_Bound (Aggregate_Bounds (Sub_Aggr));
4355 -- The bounds of this specific sub-aggregate
4357 Aggr_Lo : constant Node_Id := Aggr_Low (Dim);
4358 Aggr_Hi : constant Node_Id := Aggr_High (Dim);
4359 -- The bounds of the aggregate for this dimension
4361 Ind_Typ : constant Entity_Id := Aggr_Index_Typ (Dim);
4362 -- The index type for this dimension.xxx
4364 Cond : Node_Id := Empty;
4369 -- If index checks are on generate the test
4371 -- [constraint_error when
4372 -- Aggr_Lo /= Sub_Lo or else Aggr_Hi /= Sub_Hi]
4374 -- As an optimization try to see if some tests are trivially vacuos
4375 -- because we are comparing an expression against itself. Also for
4376 -- the first dimension the test is trivially vacuous because there
4377 -- is just one aggregate for dimension 1.
4379 if Index_Checks_Suppressed (Ind_Typ) then
4383 or else (Aggr_Lo = Sub_Lo and then Aggr_Hi = Sub_Hi)
4387 elsif Aggr_Hi = Sub_Hi then
4390 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
4391 Right_Opnd => Duplicate_Subexpr_Move_Checks (Sub_Lo));
4393 elsif Aggr_Lo = Sub_Lo then
4396 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi),
4397 Right_Opnd => Duplicate_Subexpr_Move_Checks (Sub_Hi));
4404 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
4405 Right_Opnd => Duplicate_Subexpr_Move_Checks (Sub_Lo)),
4409 Left_Opnd => Duplicate_Subexpr (Aggr_Hi),
4410 Right_Opnd => Duplicate_Subexpr (Sub_Hi)));
4413 if Present (Cond) then
4415 Make_Raise_Constraint_Error (Loc,
4417 Reason => CE_Length_Check_Failed));
4420 -- Now look inside the sub-aggregate to see if there is more work
4422 if Dim < Aggr_Dimension then
4424 -- Process positional components
4426 if Present (Expressions (Sub_Aggr)) then
4427 Expr := First (Expressions (Sub_Aggr));
4428 while Present (Expr) loop
4429 Check_Same_Aggr_Bounds (Expr, Dim + 1);
4434 -- Process component associations
4436 if Present (Component_Associations (Sub_Aggr)) then
4437 Assoc := First (Component_Associations (Sub_Aggr));
4438 while Present (Assoc) loop
4439 Expr := Expression (Assoc);
4440 Check_Same_Aggr_Bounds (Expr, Dim + 1);
4445 end Check_Same_Aggr_Bounds;
4447 ----------------------------
4448 -- Compute_Others_Present --
4449 ----------------------------
4451 procedure Compute_Others_Present (Sub_Aggr : Node_Id; Dim : Pos) is
4456 if Present (Component_Associations (Sub_Aggr)) then
4457 Assoc := Last (Component_Associations (Sub_Aggr));
4459 if Nkind (First (Choices (Assoc))) = N_Others_Choice then
4460 Others_Present (Dim) := True;
4464 -- Now look inside the sub-aggregate to see if there is more work
4466 if Dim < Aggr_Dimension then
4468 -- Process positional components
4470 if Present (Expressions (Sub_Aggr)) then
4471 Expr := First (Expressions (Sub_Aggr));
4472 while Present (Expr) loop
4473 Compute_Others_Present (Expr, Dim + 1);
4478 -- Process component associations
4480 if Present (Component_Associations (Sub_Aggr)) then
4481 Assoc := First (Component_Associations (Sub_Aggr));
4482 while Present (Assoc) loop
4483 Expr := Expression (Assoc);
4484 Compute_Others_Present (Expr, Dim + 1);
4489 end Compute_Others_Present;
4491 ------------------------
4492 -- In_Place_Assign_OK --
4493 ------------------------
4495 function In_Place_Assign_OK return Boolean is
4503 function Is_Others_Aggregate (Aggr : Node_Id) return Boolean;
4504 -- Aggregates that consist of a single Others choice are safe
4505 -- if the single expression is.
4507 function Safe_Aggregate (Aggr : Node_Id) return Boolean;
4508 -- Check recursively that each component of a (sub)aggregate does
4509 -- not depend on the variable being assigned to.
4511 function Safe_Component (Expr : Node_Id) return Boolean;
4512 -- Verify that an expression cannot depend on the variable being
4513 -- assigned to. Room for improvement here (but less than before).
4515 -------------------------
4516 -- Is_Others_Aggregate --
4517 -------------------------
4519 function Is_Others_Aggregate (Aggr : Node_Id) return Boolean is
4521 return No (Expressions (Aggr))
4523 (First (Choices (First (Component_Associations (Aggr)))))
4525 end Is_Others_Aggregate;
4527 --------------------
4528 -- Safe_Aggregate --
4529 --------------------
4531 function Safe_Aggregate (Aggr : Node_Id) return Boolean is
4535 if Present (Expressions (Aggr)) then
4536 Expr := First (Expressions (Aggr));
4537 while Present (Expr) loop
4538 if Nkind (Expr) = N_Aggregate then
4539 if not Safe_Aggregate (Expr) then
4543 elsif not Safe_Component (Expr) then
4551 if Present (Component_Associations (Aggr)) then
4552 Expr := First (Component_Associations (Aggr));
4553 while Present (Expr) loop
4554 if Nkind (Expression (Expr)) = N_Aggregate then
4555 if not Safe_Aggregate (Expression (Expr)) then
4559 elsif not Safe_Component (Expression (Expr)) then
4570 --------------------
4571 -- Safe_Component --
4572 --------------------
4574 function Safe_Component (Expr : Node_Id) return Boolean is
4575 Comp : Node_Id := Expr;
4577 function Check_Component (Comp : Node_Id) return Boolean;
4578 -- Do the recursive traversal, after copy
4580 ---------------------
4581 -- Check_Component --
4582 ---------------------
4584 function Check_Component (Comp : Node_Id) return Boolean is
4586 if Is_Overloaded (Comp) then
4590 return Compile_Time_Known_Value (Comp)
4592 or else (Is_Entity_Name (Comp)
4593 and then Present (Entity (Comp))
4594 and then No (Renamed_Object (Entity (Comp))))
4596 or else (Nkind (Comp) = N_Attribute_Reference
4597 and then Check_Component (Prefix (Comp)))
4599 or else (Nkind (Comp) in N_Binary_Op
4600 and then Check_Component (Left_Opnd (Comp))
4601 and then Check_Component (Right_Opnd (Comp)))
4603 or else (Nkind (Comp) in N_Unary_Op
4604 and then Check_Component (Right_Opnd (Comp)))
4606 or else (Nkind (Comp) = N_Selected_Component
4607 and then Check_Component (Prefix (Comp)))
4609 or else (Nkind (Comp) = N_Unchecked_Type_Conversion
4610 and then Check_Component (Expression (Comp)));
4611 end Check_Component;
4613 -- Start of processing for Safe_Component
4616 -- If the component appears in an association that may
4617 -- correspond to more than one element, it is not analyzed
4618 -- before the expansion into assignments, to avoid side effects.
4619 -- We analyze, but do not resolve the copy, to obtain sufficient
4620 -- entity information for the checks that follow. If component is
4621 -- overloaded we assume an unsafe function call.
4623 if not Analyzed (Comp) then
4624 if Is_Overloaded (Expr) then
4627 elsif Nkind (Expr) = N_Aggregate
4628 and then not Is_Others_Aggregate (Expr)
4632 elsif Nkind (Expr) = N_Allocator then
4634 -- For now, too complex to analyze
4639 Comp := New_Copy_Tree (Expr);
4640 Set_Parent (Comp, Parent (Expr));
4644 if Nkind (Comp) = N_Aggregate then
4645 return Safe_Aggregate (Comp);
4647 return Check_Component (Comp);
4651 -- Start of processing for In_Place_Assign_OK
4654 if Present (Component_Associations (N)) then
4656 -- On assignment, sliding can take place, so we cannot do the
4657 -- assignment in place unless the bounds of the aggregate are
4658 -- statically equal to those of the target.
4660 -- If the aggregate is given by an others choice, the bounds
4661 -- are derived from the left-hand side, and the assignment is
4662 -- safe if the expression is.
4664 if Is_Others_Aggregate (N) then
4667 (Expression (First (Component_Associations (N))));
4670 Aggr_In := First_Index (Etype (N));
4672 if Nkind (Parent (N)) = N_Assignment_Statement then
4673 Obj_In := First_Index (Etype (Name (Parent (N))));
4676 -- Context is an allocator. Check bounds of aggregate
4677 -- against given type in qualified expression.
4679 pragma Assert (Nkind (Parent (Parent (N))) = N_Allocator);
4681 First_Index (Etype (Entity (Subtype_Mark (Parent (N)))));
4684 while Present (Aggr_In) loop
4685 Get_Index_Bounds (Aggr_In, Aggr_Lo, Aggr_Hi);
4686 Get_Index_Bounds (Obj_In, Obj_Lo, Obj_Hi);
4688 if not Compile_Time_Known_Value (Aggr_Lo)
4689 or else not Compile_Time_Known_Value (Aggr_Hi)
4690 or else not Compile_Time_Known_Value (Obj_Lo)
4691 or else not Compile_Time_Known_Value (Obj_Hi)
4692 or else Expr_Value (Aggr_Lo) /= Expr_Value (Obj_Lo)
4693 or else Expr_Value (Aggr_Hi) /= Expr_Value (Obj_Hi)
4698 Next_Index (Aggr_In);
4699 Next_Index (Obj_In);
4703 -- Now check the component values themselves
4705 return Safe_Aggregate (N);
4706 end In_Place_Assign_OK;
4712 procedure Others_Check (Sub_Aggr : Node_Id; Dim : Pos) is
4713 Aggr_Lo : constant Node_Id := Aggr_Low (Dim);
4714 Aggr_Hi : constant Node_Id := Aggr_High (Dim);
4715 -- The bounds of the aggregate for this dimension
4717 Ind_Typ : constant Entity_Id := Aggr_Index_Typ (Dim);
4718 -- The index type for this dimension
4720 Need_To_Check : Boolean := False;
4722 Choices_Lo : Node_Id := Empty;
4723 Choices_Hi : Node_Id := Empty;
4724 -- The lowest and highest discrete choices for a named sub-aggregate
4726 Nb_Choices : Int := -1;
4727 -- The number of discrete non-others choices in this sub-aggregate
4729 Nb_Elements : Uint := Uint_0;
4730 -- The number of elements in a positional aggregate
4732 Cond : Node_Id := Empty;
4739 -- Check if we have an others choice. If we do make sure that this
4740 -- sub-aggregate contains at least one element in addition to the
4743 if Range_Checks_Suppressed (Ind_Typ) then
4744 Need_To_Check := False;
4746 elsif Present (Expressions (Sub_Aggr))
4747 and then Present (Component_Associations (Sub_Aggr))
4749 Need_To_Check := True;
4751 elsif Present (Component_Associations (Sub_Aggr)) then
4752 Assoc := Last (Component_Associations (Sub_Aggr));
4754 if Nkind (First (Choices (Assoc))) /= N_Others_Choice then
4755 Need_To_Check := False;
4758 -- Count the number of discrete choices. Start with -1 because
4759 -- the others choice does not count.
4762 Assoc := First (Component_Associations (Sub_Aggr));
4763 while Present (Assoc) loop
4764 Choice := First (Choices (Assoc));
4765 while Present (Choice) loop
4766 Nb_Choices := Nb_Choices + 1;
4773 -- If there is only an others choice nothing to do
4775 Need_To_Check := (Nb_Choices > 0);
4779 Need_To_Check := False;
4782 -- If we are dealing with a positional sub-aggregate with an others
4783 -- choice then compute the number or positional elements.
4785 if Need_To_Check and then Present (Expressions (Sub_Aggr)) then
4786 Expr := First (Expressions (Sub_Aggr));
4787 Nb_Elements := Uint_0;
4788 while Present (Expr) loop
4789 Nb_Elements := Nb_Elements + 1;
4793 -- If the aggregate contains discrete choices and an others choice
4794 -- compute the smallest and largest discrete choice values.
4796 elsif Need_To_Check then
4797 Compute_Choices_Lo_And_Choices_Hi : declare
4799 Table : Case_Table_Type (1 .. Nb_Choices);
4800 -- Used to sort all the different choice values
4807 Assoc := First (Component_Associations (Sub_Aggr));
4808 while Present (Assoc) loop
4809 Choice := First (Choices (Assoc));
4810 while Present (Choice) loop
4811 if Nkind (Choice) = N_Others_Choice then
4815 Get_Index_Bounds (Choice, Low, High);
4816 Table (J).Choice_Lo := Low;
4817 Table (J).Choice_Hi := High;
4826 -- Sort the discrete choices
4828 Sort_Case_Table (Table);
4830 Choices_Lo := Table (1).Choice_Lo;
4831 Choices_Hi := Table (Nb_Choices).Choice_Hi;
4832 end Compute_Choices_Lo_And_Choices_Hi;
4835 -- If no others choice in this sub-aggregate, or the aggregate
4836 -- comprises only an others choice, nothing to do.
4838 if not Need_To_Check then
4841 -- If we are dealing with an aggregate containing an others choice
4842 -- and positional components, we generate the following test:
4844 -- if Ind_Typ'Pos (Aggr_Lo) + (Nb_Elements - 1) >
4845 -- Ind_Typ'Pos (Aggr_Hi)
4847 -- raise Constraint_Error;
4850 elsif Nb_Elements > Uint_0 then
4856 Make_Attribute_Reference (Loc,
4857 Prefix => New_Reference_To (Ind_Typ, Loc),
4858 Attribute_Name => Name_Pos,
4861 (Duplicate_Subexpr_Move_Checks (Aggr_Lo))),
4862 Right_Opnd => Make_Integer_Literal (Loc, Nb_Elements - 1)),
4865 Make_Attribute_Reference (Loc,
4866 Prefix => New_Reference_To (Ind_Typ, Loc),
4867 Attribute_Name => Name_Pos,
4868 Expressions => New_List (
4869 Duplicate_Subexpr_Move_Checks (Aggr_Hi))));
4871 -- If we are dealing with an aggregate containing an others choice
4872 -- and discrete choices we generate the following test:
4874 -- [constraint_error when
4875 -- Choices_Lo < Aggr_Lo or else Choices_Hi > Aggr_Hi];
4883 Duplicate_Subexpr_Move_Checks (Choices_Lo),
4885 Duplicate_Subexpr_Move_Checks (Aggr_Lo)),
4890 Duplicate_Subexpr (Choices_Hi),
4892 Duplicate_Subexpr (Aggr_Hi)));
4895 if Present (Cond) then
4897 Make_Raise_Constraint_Error (Loc,
4899 Reason => CE_Length_Check_Failed));
4900 -- Questionable reason code, shouldn't that be a
4901 -- CE_Range_Check_Failed ???
4904 -- Now look inside the sub-aggregate to see if there is more work
4906 if Dim < Aggr_Dimension then
4908 -- Process positional components
4910 if Present (Expressions (Sub_Aggr)) then
4911 Expr := First (Expressions (Sub_Aggr));
4912 while Present (Expr) loop
4913 Others_Check (Expr, Dim + 1);
4918 -- Process component associations
4920 if Present (Component_Associations (Sub_Aggr)) then
4921 Assoc := First (Component_Associations (Sub_Aggr));
4922 while Present (Assoc) loop
4923 Expr := Expression (Assoc);
4924 Others_Check (Expr, Dim + 1);
4931 -------------------------
4932 -- Safe_Left_Hand_Side --
4933 -------------------------
4935 function Safe_Left_Hand_Side (N : Node_Id) return Boolean is
4937 if Is_Entity_Name (N) then
4940 elsif Nkind_In (N, N_Explicit_Dereference, N_Selected_Component)
4941 and then Safe_Left_Hand_Side (Prefix (N))
4945 elsif Nkind (N) = N_Indexed_Component
4946 and then Safe_Left_Hand_Side (Prefix (N))
4948 (Is_Entity_Name (First (Expressions (N)))
4949 or else Nkind (First (Expressions (N))) = N_Integer_Literal)
4955 end Safe_Left_Hand_Side;
4960 -- Holds the temporary aggregate value
4963 -- Holds the declaration of Tmp
4965 Aggr_Code : List_Id;
4966 Parent_Node : Node_Id;
4967 Parent_Kind : Node_Kind;
4969 -- Start of processing for Expand_Array_Aggregate
4972 -- Do not touch the special aggregates of attributes used for Asm calls
4974 if Is_RTE (Ctyp, RE_Asm_Input_Operand)
4975 or else Is_RTE (Ctyp, RE_Asm_Output_Operand)
4980 -- If the semantic analyzer has determined that aggregate N will raise
4981 -- Constraint_Error at run time, then the aggregate node has been
4982 -- replaced with an N_Raise_Constraint_Error node and we should
4985 pragma Assert (not Raises_Constraint_Error (N));
4989 -- Check that the index range defined by aggregate bounds is
4990 -- compatible with corresponding index subtype.
4992 Index_Compatibility_Check : declare
4993 Aggr_Index_Range : Node_Id := First_Index (Typ);
4994 -- The current aggregate index range
4996 Index_Constraint : Node_Id := First_Index (Etype (Typ));
4997 -- The corresponding index constraint against which we have to
4998 -- check the above aggregate index range.
5001 Compute_Others_Present (N, 1);
5003 for J in 1 .. Aggr_Dimension loop
5004 -- There is no need to emit a check if an others choice is
5005 -- present for this array aggregate dimension since in this
5006 -- case one of N's sub-aggregates has taken its bounds from the
5007 -- context and these bounds must have been checked already. In
5008 -- addition all sub-aggregates corresponding to the same
5009 -- dimension must all have the same bounds (checked in (c) below).
5011 if not Range_Checks_Suppressed (Etype (Index_Constraint))
5012 and then not Others_Present (J)
5014 -- We don't use Checks.Apply_Range_Check here because it emits
5015 -- a spurious check. Namely it checks that the range defined by
5016 -- the aggregate bounds is non empty. But we know this already
5019 Check_Bounds (Aggr_Index_Range, Index_Constraint);
5022 -- Save the low and high bounds of the aggregate index as well as
5023 -- the index type for later use in checks (b) and (c) below.
5025 Aggr_Low (J) := Low_Bound (Aggr_Index_Range);
5026 Aggr_High (J) := High_Bound (Aggr_Index_Range);
5028 Aggr_Index_Typ (J) := Etype (Index_Constraint);
5030 Next_Index (Aggr_Index_Range);
5031 Next_Index (Index_Constraint);
5033 end Index_Compatibility_Check;
5037 -- If an others choice is present check that no aggregate index is
5038 -- outside the bounds of the index constraint.
5040 Others_Check (N, 1);
5044 -- For multidimensional arrays make sure that all subaggregates
5045 -- corresponding to the same dimension have the same bounds.
5047 if Aggr_Dimension > 1 then
5048 Check_Same_Aggr_Bounds (N, 1);
5053 -- Here we test for is packed array aggregate that we can handle at
5054 -- compile time. If so, return with transformation done. Note that we do
5055 -- this even if the aggregate is nested, because once we have done this
5056 -- processing, there is no more nested aggregate!
5058 if Packed_Array_Aggregate_Handled (N) then
5062 -- At this point we try to convert to positional form
5064 if Ekind (Current_Scope) = E_Package
5065 and then Static_Elaboration_Desired (Current_Scope)
5067 Convert_To_Positional (N, Max_Others_Replicate => 100);
5070 Convert_To_Positional (N);
5073 -- if the result is no longer an aggregate (e.g. it may be a string
5074 -- literal, or a temporary which has the needed value), then we are
5075 -- done, since there is no longer a nested aggregate.
5077 if Nkind (N) /= N_Aggregate then
5080 -- We are also done if the result is an analyzed aggregate
5081 -- This case could use more comments ???
5084 and then N /= Original_Node (N)
5089 -- If all aggregate components are compile-time known and the aggregate
5090 -- has been flattened, nothing left to do. The same occurs if the
5091 -- aggregate is used to initialize the components of an statically
5092 -- allocated dispatch table.
5094 if Compile_Time_Known_Aggregate (N)
5095 or else Is_Static_Dispatch_Table_Aggregate (N)
5097 Set_Expansion_Delayed (N, False);
5101 -- Now see if back end processing is possible
5103 if Backend_Processing_Possible (N) then
5105 -- If the aggregate is static but the constraints are not, build
5106 -- a static subtype for the aggregate, so that Gigi can place it
5107 -- in static memory. Perform an unchecked_conversion to the non-
5108 -- static type imposed by the context.
5111 Itype : constant Entity_Id := Etype (N);
5113 Needs_Type : Boolean := False;
5116 Index := First_Index (Itype);
5117 while Present (Index) loop
5118 if not Is_Static_Subtype (Etype (Index)) then
5127 Build_Constrained_Type (Positional => True);
5128 Rewrite (N, Unchecked_Convert_To (Itype, N));
5138 -- Delay expansion for nested aggregates: it will be taken care of
5139 -- when the parent aggregate is expanded.
5141 Parent_Node := Parent (N);
5142 Parent_Kind := Nkind (Parent_Node);
5144 if Parent_Kind = N_Qualified_Expression then
5145 Parent_Node := Parent (Parent_Node);
5146 Parent_Kind := Nkind (Parent_Node);
5149 if Parent_Kind = N_Aggregate
5150 or else Parent_Kind = N_Extension_Aggregate
5151 or else Parent_Kind = N_Component_Association
5152 or else (Parent_Kind = N_Object_Declaration
5153 and then Needs_Finalization (Typ))
5154 or else (Parent_Kind = N_Assignment_Statement
5155 and then Inside_Init_Proc)
5157 if Static_Array_Aggregate (N)
5158 or else Compile_Time_Known_Aggregate (N)
5160 Set_Expansion_Delayed (N, False);
5163 Set_Expansion_Delayed (N);
5170 -- Look if in place aggregate expansion is possible
5172 -- For object declarations we build the aggregate in place, unless
5173 -- the array is bit-packed or the component is controlled.
5175 -- For assignments we do the assignment in place if all the component
5176 -- associations have compile-time known values. For other cases we
5177 -- create a temporary. The analysis for safety of on-line assignment
5178 -- is delicate, i.e. we don't know how to do it fully yet ???
5180 -- For allocators we assign to the designated object in place if the
5181 -- aggregate meets the same conditions as other in-place assignments.
5182 -- In this case the aggregate may not come from source but was created
5183 -- for default initialization, e.g. with Initialize_Scalars.
5185 if Requires_Transient_Scope (Typ) then
5186 Establish_Transient_Scope
5187 (N, Sec_Stack => Has_Controlled_Component (Typ));
5190 if Has_Default_Init_Comps (N) then
5191 Maybe_In_Place_OK := False;
5193 elsif Is_Bit_Packed_Array (Typ)
5194 or else Has_Controlled_Component (Typ)
5196 Maybe_In_Place_OK := False;
5199 Maybe_In_Place_OK :=
5200 (Nkind (Parent (N)) = N_Assignment_Statement
5201 and then Comes_From_Source (N)
5202 and then In_Place_Assign_OK)
5205 (Nkind (Parent (Parent (N))) = N_Allocator
5206 and then In_Place_Assign_OK);
5209 -- If this is an array of tasks, it will be expanded into build-in-place
5210 -- assignments. Build an activation chain for the tasks now.
5212 if Has_Task (Etype (N)) then
5213 Build_Activation_Chain_Entity (N);
5216 -- Should document these individual tests ???
5218 if not Has_Default_Init_Comps (N)
5219 and then Comes_From_Source (Parent (N))
5220 and then Nkind (Parent (N)) = N_Object_Declaration
5222 Must_Slide (Etype (Defining_Identifier (Parent (N))), Typ)
5223 and then N = Expression (Parent (N))
5224 and then not Is_Bit_Packed_Array (Typ)
5225 and then not Has_Controlled_Component (Typ)
5227 -- If the aggregate is the expression in an object declaration, it
5228 -- cannot be expanded in place. Lookahead in the current declarative
5229 -- part to find an address clause for the object being declared. If
5230 -- one is present, we cannot build in place. Unclear comment???
5232 and then not Has_Following_Address_Clause (Parent (N))
5234 Tmp := Defining_Identifier (Parent (N));
5235 Set_No_Initialization (Parent (N));
5236 Set_Expression (Parent (N), Empty);
5238 -- Set the type of the entity, for use in the analysis of the
5239 -- subsequent indexed assignments. If the nominal type is not
5240 -- constrained, build a subtype from the known bounds of the
5241 -- aggregate. If the declaration has a subtype mark, use it,
5242 -- otherwise use the itype of the aggregate.
5244 if not Is_Constrained (Typ) then
5245 Build_Constrained_Type (Positional => False);
5246 elsif Is_Entity_Name (Object_Definition (Parent (N)))
5247 and then Is_Constrained (Entity (Object_Definition (Parent (N))))
5249 Set_Etype (Tmp, Entity (Object_Definition (Parent (N))));
5251 Set_Size_Known_At_Compile_Time (Typ, False);
5252 Set_Etype (Tmp, Typ);
5255 elsif Maybe_In_Place_OK
5256 and then Nkind (Parent (N)) = N_Qualified_Expression
5257 and then Nkind (Parent (Parent (N))) = N_Allocator
5259 Set_Expansion_Delayed (N);
5262 -- In the remaining cases the aggregate is the RHS of an assignment
5264 elsif Maybe_In_Place_OK
5265 and then Safe_Left_Hand_Side (Name (Parent (N)))
5267 Tmp := Name (Parent (N));
5269 if Etype (Tmp) /= Etype (N) then
5270 Apply_Length_Check (N, Etype (Tmp));
5272 if Nkind (N) = N_Raise_Constraint_Error then
5274 -- Static error, nothing further to expand
5280 elsif Maybe_In_Place_OK
5281 and then Nkind (Name (Parent (N))) = N_Slice
5282 and then Safe_Slice_Assignment (N)
5284 -- Safe_Slice_Assignment rewrites assignment as a loop
5290 -- In place aggregate expansion is not possible
5293 Maybe_In_Place_OK := False;
5294 Tmp := Make_Temporary (Loc, 'A', N);
5296 Make_Object_Declaration
5298 Defining_Identifier => Tmp,
5299 Object_Definition => New_Occurrence_Of (Typ, Loc));
5300 Set_No_Initialization (Tmp_Decl, True);
5302 -- If we are within a loop, the temporary will be pushed on the
5303 -- stack at each iteration. If the aggregate is the expression for an
5304 -- allocator, it will be immediately copied to the heap and can
5305 -- be reclaimed at once. We create a transient scope around the
5306 -- aggregate for this purpose.
5308 if Ekind (Current_Scope) = E_Loop
5309 and then Nkind (Parent (Parent (N))) = N_Allocator
5311 Establish_Transient_Scope (N, False);
5314 Insert_Action (N, Tmp_Decl);
5317 -- Construct and insert the aggregate code. We can safely suppress index
5318 -- checks because this code is guaranteed not to raise CE on index
5319 -- checks. However we should *not* suppress all checks.
5325 if Nkind (Tmp) = N_Defining_Identifier then
5326 Target := New_Reference_To (Tmp, Loc);
5330 if Has_Default_Init_Comps (N) then
5332 -- Ada 2005 (AI-287): This case has not been analyzed???
5334 raise Program_Error;
5337 -- Name in assignment is explicit dereference
5339 Target := New_Copy (Tmp);
5343 Build_Array_Aggr_Code (N,
5345 Index => First_Index (Typ),
5347 Scalar_Comp => Is_Scalar_Type (Ctyp));
5350 if Comes_From_Source (Tmp) then
5351 Insert_Actions_After (Parent (N), Aggr_Code);
5354 Insert_Actions (N, Aggr_Code);
5357 -- If the aggregate has been assigned in place, remove the original
5360 if Nkind (Parent (N)) = N_Assignment_Statement
5361 and then Maybe_In_Place_OK
5363 Rewrite (Parent (N), Make_Null_Statement (Loc));
5365 elsif Nkind (Parent (N)) /= N_Object_Declaration
5366 or else Tmp /= Defining_Identifier (Parent (N))
5368 Rewrite (N, New_Occurrence_Of (Tmp, Loc));
5369 Analyze_And_Resolve (N, Typ);
5371 end Expand_Array_Aggregate;
5373 ------------------------
5374 -- Expand_N_Aggregate --
5375 ------------------------
5377 procedure Expand_N_Aggregate (N : Node_Id) is
5379 if Is_Record_Type (Etype (N)) then
5380 Expand_Record_Aggregate (N);
5382 Expand_Array_Aggregate (N);
5385 when RE_Not_Available =>
5387 end Expand_N_Aggregate;
5389 ----------------------------------
5390 -- Expand_N_Extension_Aggregate --
5391 ----------------------------------
5393 -- If the ancestor part is an expression, add a component association for
5394 -- the parent field. If the type of the ancestor part is not the direct
5395 -- parent of the expected type, build recursively the needed ancestors.
5396 -- If the ancestor part is a subtype_mark, replace aggregate with a decla-
5397 -- ration for a temporary of the expected type, followed by individual
5398 -- assignments to the given components.
5400 procedure Expand_N_Extension_Aggregate (N : Node_Id) is
5401 Loc : constant Source_Ptr := Sloc (N);
5402 A : constant Node_Id := Ancestor_Part (N);
5403 Typ : constant Entity_Id := Etype (N);
5406 -- If the ancestor is a subtype mark, an init proc must be called
5407 -- on the resulting object which thus has to be materialized in
5410 if Is_Entity_Name (A) and then Is_Type (Entity (A)) then
5411 Convert_To_Assignments (N, Typ);
5413 -- The extension aggregate is transformed into a record aggregate
5414 -- of the following form (c1 and c2 are inherited components)
5416 -- (Exp with c3 => a, c4 => b)
5417 -- ==> (c1 => Exp.c1, c2 => Exp.c2, c1 => a, c2 => b)
5422 if Tagged_Type_Expansion then
5423 Expand_Record_Aggregate (N,
5426 (Node (First_Elmt (Access_Disp_Table (Typ))), Loc),
5429 -- No tag is needed in the case of a VM
5430 Expand_Record_Aggregate (N,
5436 when RE_Not_Available =>
5438 end Expand_N_Extension_Aggregate;
5440 -----------------------------
5441 -- Expand_Record_Aggregate --
5442 -----------------------------
5444 procedure Expand_Record_Aggregate
5446 Orig_Tag : Node_Id := Empty;
5447 Parent_Expr : Node_Id := Empty)
5449 Loc : constant Source_Ptr := Sloc (N);
5450 Comps : constant List_Id := Component_Associations (N);
5451 Typ : constant Entity_Id := Etype (N);
5452 Base_Typ : constant Entity_Id := Base_Type (Typ);
5454 Static_Components : Boolean := True;
5455 -- Flag to indicate whether all components are compile-time known,
5456 -- and the aggregate can be constructed statically and handled by
5459 function Component_Not_OK_For_Backend return Boolean;
5460 -- Check for presence of component which makes it impossible for the
5461 -- backend to process the aggregate, thus requiring the use of a series
5462 -- of assignment statements. Cases checked for are a nested aggregate
5463 -- needing Late_Expansion, the presence of a tagged component which may
5464 -- need tag adjustment, and a bit unaligned component reference.
5466 -- We also force expansion into assignments if a component is of a
5467 -- mutable type (including a private type with discriminants) because
5468 -- in that case the size of the component to be copied may be smaller
5469 -- than the side of the target, and there is no simple way for gigi
5470 -- to compute the size of the object to be copied.
5472 -- NOTE: This is part of the ongoing work to define precisely the
5473 -- interface between front-end and back-end handling of aggregates.
5474 -- In general it is desirable to pass aggregates as they are to gigi,
5475 -- in order to minimize elaboration code. This is one case where the
5476 -- semantics of Ada complicate the analysis and lead to anomalies in
5477 -- the gcc back-end if the aggregate is not expanded into assignments.
5479 ----------------------------------
5480 -- Component_Not_OK_For_Backend --
5481 ----------------------------------
5483 function Component_Not_OK_For_Backend return Boolean is
5493 while Present (C) loop
5494 if Nkind (Expression (C)) = N_Qualified_Expression then
5495 Expr_Q := Expression (Expression (C));
5497 Expr_Q := Expression (C);
5500 -- Return true if the aggregate has any associations for tagged
5501 -- components that may require tag adjustment.
5503 -- These are cases where the source expression may have a tag that
5504 -- could differ from the component tag (e.g., can occur for type
5505 -- conversions and formal parameters). (Tag adjustment not needed
5506 -- if VM_Target because object tags are implicit in the machine.)
5508 if Is_Tagged_Type (Etype (Expr_Q))
5509 and then (Nkind (Expr_Q) = N_Type_Conversion
5510 or else (Is_Entity_Name (Expr_Q)
5512 Ekind (Entity (Expr_Q)) in Formal_Kind))
5513 and then Tagged_Type_Expansion
5515 Static_Components := False;
5518 elsif Is_Delayed_Aggregate (Expr_Q) then
5519 Static_Components := False;
5522 elsif Possible_Bit_Aligned_Component (Expr_Q) then
5523 Static_Components := False;
5527 if Is_Scalar_Type (Etype (Expr_Q)) then
5528 if not Compile_Time_Known_Value (Expr_Q) then
5529 Static_Components := False;
5532 elsif Nkind (Expr_Q) /= N_Aggregate
5533 or else not Compile_Time_Known_Aggregate (Expr_Q)
5535 Static_Components := False;
5537 if Is_Private_Type (Etype (Expr_Q))
5538 and then Has_Discriminants (Etype (Expr_Q))
5548 end Component_Not_OK_For_Backend;
5550 -- Remaining Expand_Record_Aggregate variables
5552 Tag_Value : Node_Id;
5556 -- Start of processing for Expand_Record_Aggregate
5559 -- If the aggregate is to be assigned to an atomic variable, we
5560 -- have to prevent a piecemeal assignment even if the aggregate
5561 -- is to be expanded. We create a temporary for the aggregate, and
5562 -- assign the temporary instead, so that the back end can generate
5563 -- an atomic move for it.
5566 and then Comes_From_Source (Parent (N))
5567 and then Is_Atomic_Aggregate (N, Typ)
5571 -- No special management required for aggregates used to initialize
5572 -- statically allocated dispatch tables
5574 elsif Is_Static_Dispatch_Table_Aggregate (N) then
5578 -- Ada 2005 (AI-318-2): We need to convert to assignments if components
5579 -- are build-in-place function calls. This test could be more specific,
5580 -- but doing it for all inherently limited aggregates seems harmless.
5581 -- The assignments will turn into build-in-place function calls (see
5582 -- Make_Build_In_Place_Call_In_Assignment).
5584 if Ada_Version >= Ada_05 and then Is_Inherently_Limited_Type (Typ) then
5585 Convert_To_Assignments (N, Typ);
5587 -- Gigi doesn't handle properly temporaries of variable size
5588 -- so we generate it in the front-end
5590 elsif not Size_Known_At_Compile_Time (Typ) then
5591 Convert_To_Assignments (N, Typ);
5593 -- Temporaries for controlled aggregates need to be attached to a
5594 -- final chain in order to be properly finalized, so it has to
5595 -- be created in the front-end
5597 elsif Is_Controlled (Typ)
5598 or else Has_Controlled_Component (Base_Type (Typ))
5600 Convert_To_Assignments (N, Typ);
5602 -- Ada 2005 (AI-287): In case of default initialized components we
5603 -- convert the aggregate into assignments.
5605 elsif Has_Default_Init_Comps (N) then
5606 Convert_To_Assignments (N, Typ);
5610 elsif Component_Not_OK_For_Backend then
5611 Convert_To_Assignments (N, Typ);
5613 -- If an ancestor is private, some components are not inherited and
5614 -- we cannot expand into a record aggregate
5616 elsif Has_Private_Ancestor (Typ) then
5617 Convert_To_Assignments (N, Typ);
5619 -- ??? The following was done to compile fxacc00.ads in the ACVCs. Gigi
5620 -- is not able to handle the aggregate for Late_Request.
5622 elsif Is_Tagged_Type (Typ) and then Has_Discriminants (Typ) then
5623 Convert_To_Assignments (N, Typ);
5625 -- If the tagged types covers interface types we need to initialize all
5626 -- hidden components containing pointers to secondary dispatch tables.
5628 elsif Is_Tagged_Type (Typ) and then Has_Interfaces (Typ) then
5629 Convert_To_Assignments (N, Typ);
5631 -- If some components are mutable, the size of the aggregate component
5632 -- may be distinct from the default size of the type component, so
5633 -- we need to expand to insure that the back-end copies the proper
5634 -- size of the data.
5636 elsif Has_Mutable_Components (Typ) then
5637 Convert_To_Assignments (N, Typ);
5639 -- If the type involved has any non-bit aligned components, then we are
5640 -- not sure that the back end can handle this case correctly.
5642 elsif Type_May_Have_Bit_Aligned_Components (Typ) then
5643 Convert_To_Assignments (N, Typ);
5645 -- In all other cases, build a proper aggregate handlable by gigi
5648 if Nkind (N) = N_Aggregate then
5650 -- If the aggregate is static and can be handled by the back-end,
5651 -- nothing left to do.
5653 if Static_Components then
5654 Set_Compile_Time_Known_Aggregate (N);
5655 Set_Expansion_Delayed (N, False);
5659 -- If no discriminants, nothing special to do
5661 if not Has_Discriminants (Typ) then
5664 -- Case of discriminants present
5666 elsif Is_Derived_Type (Typ) then
5668 -- For untagged types, non-stored discriminants are replaced
5669 -- with stored discriminants, which are the ones that gigi uses
5670 -- to describe the type and its components.
5672 Generate_Aggregate_For_Derived_Type : declare
5673 Constraints : constant List_Id := New_List;
5674 First_Comp : Node_Id;
5675 Discriminant : Entity_Id;
5677 Num_Disc : Int := 0;
5678 Num_Gird : Int := 0;
5680 procedure Prepend_Stored_Values (T : Entity_Id);
5681 -- Scan the list of stored discriminants of the type, and add
5682 -- their values to the aggregate being built.
5684 ---------------------------
5685 -- Prepend_Stored_Values --
5686 ---------------------------
5688 procedure Prepend_Stored_Values (T : Entity_Id) is
5690 Discriminant := First_Stored_Discriminant (T);
5691 while Present (Discriminant) loop
5693 Make_Component_Association (Loc,
5695 New_List (New_Occurrence_Of (Discriminant, Loc)),
5699 Get_Discriminant_Value (
5702 Discriminant_Constraint (Typ))));
5704 if No (First_Comp) then
5705 Prepend_To (Component_Associations (N), New_Comp);
5707 Insert_After (First_Comp, New_Comp);
5710 First_Comp := New_Comp;
5711 Next_Stored_Discriminant (Discriminant);
5713 end Prepend_Stored_Values;
5715 -- Start of processing for Generate_Aggregate_For_Derived_Type
5718 -- Remove the associations for the discriminant of derived type
5720 First_Comp := First (Component_Associations (N));
5721 while Present (First_Comp) loop
5726 (First (Choices (Comp)))) = E_Discriminant
5729 Num_Disc := Num_Disc + 1;
5733 -- Insert stored discriminant associations in the correct
5734 -- order. If there are more stored discriminants than new
5735 -- discriminants, there is at least one new discriminant that
5736 -- constrains more than one of the stored discriminants. In
5737 -- this case we need to construct a proper subtype of the
5738 -- parent type, in order to supply values to all the
5739 -- components. Otherwise there is one-one correspondence
5740 -- between the constraints and the stored discriminants.
5742 First_Comp := Empty;
5744 Discriminant := First_Stored_Discriminant (Base_Type (Typ));
5745 while Present (Discriminant) loop
5746 Num_Gird := Num_Gird + 1;
5747 Next_Stored_Discriminant (Discriminant);
5750 -- Case of more stored discriminants than new discriminants
5752 if Num_Gird > Num_Disc then
5754 -- Create a proper subtype of the parent type, which is the
5755 -- proper implementation type for the aggregate, and convert
5756 -- it to the intended target type.
5758 Discriminant := First_Stored_Discriminant (Base_Type (Typ));
5759 while Present (Discriminant) loop
5762 Get_Discriminant_Value (
5765 Discriminant_Constraint (Typ)));
5766 Append (New_Comp, Constraints);
5767 Next_Stored_Discriminant (Discriminant);
5771 Make_Subtype_Declaration (Loc,
5772 Defining_Identifier => Make_Temporary (Loc, 'T'),
5773 Subtype_Indication =>
5774 Make_Subtype_Indication (Loc,
5776 New_Occurrence_Of (Etype (Base_Type (Typ)), Loc),
5778 Make_Index_Or_Discriminant_Constraint
5779 (Loc, Constraints)));
5781 Insert_Action (N, Decl);
5782 Prepend_Stored_Values (Base_Type (Typ));
5784 Set_Etype (N, Defining_Identifier (Decl));
5787 Rewrite (N, Unchecked_Convert_To (Typ, N));
5790 -- Case where we do not have fewer new discriminants than
5791 -- stored discriminants, so in this case we can simply use the
5792 -- stored discriminants of the subtype.
5795 Prepend_Stored_Values (Typ);
5797 end Generate_Aggregate_For_Derived_Type;
5800 if Is_Tagged_Type (Typ) then
5802 -- The tagged case, _parent and _tag component must be created
5804 -- Reset null_present unconditionally. tagged records always have
5805 -- at least one field (the tag or the parent)
5807 Set_Null_Record_Present (N, False);
5809 -- When the current aggregate comes from the expansion of an
5810 -- extension aggregate, the parent expr is replaced by an
5811 -- aggregate formed by selected components of this expr
5813 if Present (Parent_Expr)
5814 and then Is_Empty_List (Comps)
5816 Comp := First_Component_Or_Discriminant (Typ);
5817 while Present (Comp) loop
5819 -- Skip all expander-generated components
5822 not Comes_From_Source (Original_Record_Component (Comp))
5828 Make_Selected_Component (Loc,
5830 Unchecked_Convert_To (Typ,
5831 Duplicate_Subexpr (Parent_Expr, True)),
5833 Selector_Name => New_Occurrence_Of (Comp, Loc));
5836 Make_Component_Association (Loc,
5838 New_List (New_Occurrence_Of (Comp, Loc)),
5842 Analyze_And_Resolve (New_Comp, Etype (Comp));
5845 Next_Component_Or_Discriminant (Comp);
5849 -- Compute the value for the Tag now, if the type is a root it
5850 -- will be included in the aggregate right away, otherwise it will
5851 -- be propagated to the parent aggregate
5853 if Present (Orig_Tag) then
5854 Tag_Value := Orig_Tag;
5855 elsif not Tagged_Type_Expansion then
5860 (Node (First_Elmt (Access_Disp_Table (Typ))), Loc);
5863 -- For a derived type, an aggregate for the parent is formed with
5864 -- all the inherited components.
5866 if Is_Derived_Type (Typ) then
5869 First_Comp : Node_Id;
5870 Parent_Comps : List_Id;
5871 Parent_Aggr : Node_Id;
5872 Parent_Name : Node_Id;
5875 -- Remove the inherited component association from the
5876 -- aggregate and store them in the parent aggregate
5878 First_Comp := First (Component_Associations (N));
5879 Parent_Comps := New_List;
5880 while Present (First_Comp)
5881 and then Scope (Original_Record_Component (
5882 Entity (First (Choices (First_Comp))))) /= Base_Typ
5887 Append (Comp, Parent_Comps);
5890 Parent_Aggr := Make_Aggregate (Loc,
5891 Component_Associations => Parent_Comps);
5892 Set_Etype (Parent_Aggr, Etype (Base_Type (Typ)));
5894 -- Find the _parent component
5896 Comp := First_Component (Typ);
5897 while Chars (Comp) /= Name_uParent loop
5898 Comp := Next_Component (Comp);
5901 Parent_Name := New_Occurrence_Of (Comp, Loc);
5903 -- Insert the parent aggregate
5905 Prepend_To (Component_Associations (N),
5906 Make_Component_Association (Loc,
5907 Choices => New_List (Parent_Name),
5908 Expression => Parent_Aggr));
5910 -- Expand recursively the parent propagating the right Tag
5912 Expand_Record_Aggregate (
5913 Parent_Aggr, Tag_Value, Parent_Expr);
5916 -- For a root type, the tag component is added (unless compiling
5917 -- for the VMs, where tags are implicit).
5919 elsif Tagged_Type_Expansion then
5921 Tag_Name : constant Node_Id :=
5923 (First_Tag_Component (Typ), Loc);
5924 Typ_Tag : constant Entity_Id := RTE (RE_Tag);
5925 Conv_Node : constant Node_Id :=
5926 Unchecked_Convert_To (Typ_Tag, Tag_Value);
5929 Set_Etype (Conv_Node, Typ_Tag);
5930 Prepend_To (Component_Associations (N),
5931 Make_Component_Association (Loc,
5932 Choices => New_List (Tag_Name),
5933 Expression => Conv_Node));
5939 end Expand_Record_Aggregate;
5941 ----------------------------
5942 -- Has_Default_Init_Comps --
5943 ----------------------------
5945 function Has_Default_Init_Comps (N : Node_Id) return Boolean is
5946 Comps : constant List_Id := Component_Associations (N);
5950 pragma Assert (Nkind_In (N, N_Aggregate, N_Extension_Aggregate));
5956 if Has_Self_Reference (N) then
5960 -- Check if any direct component has default initialized components
5963 while Present (C) loop
5964 if Box_Present (C) then
5971 -- Recursive call in case of aggregate expression
5974 while Present (C) loop
5975 Expr := Expression (C);
5979 Nkind_In (Expr, N_Aggregate, N_Extension_Aggregate)
5980 and then Has_Default_Init_Comps (Expr)
5989 end Has_Default_Init_Comps;
5991 --------------------------
5992 -- Is_Delayed_Aggregate --
5993 --------------------------
5995 function Is_Delayed_Aggregate (N : Node_Id) return Boolean is
5996 Node : Node_Id := N;
5997 Kind : Node_Kind := Nkind (Node);
6000 if Kind = N_Qualified_Expression then
6001 Node := Expression (Node);
6002 Kind := Nkind (Node);
6005 if Kind /= N_Aggregate and then Kind /= N_Extension_Aggregate then
6008 return Expansion_Delayed (Node);
6010 end Is_Delayed_Aggregate;
6012 ----------------------------------------
6013 -- Is_Static_Dispatch_Table_Aggregate --
6014 ----------------------------------------
6016 function Is_Static_Dispatch_Table_Aggregate (N : Node_Id) return Boolean is
6017 Typ : constant Entity_Id := Base_Type (Etype (N));
6020 return Static_Dispatch_Tables
6021 and then Tagged_Type_Expansion
6022 and then RTU_Loaded (Ada_Tags)
6024 -- Avoid circularity when rebuilding the compiler
6026 and then Cunit_Entity (Get_Source_Unit (N)) /= RTU_Entity (Ada_Tags)
6027 and then (Typ = RTE (RE_Dispatch_Table_Wrapper)
6029 Typ = RTE (RE_Address_Array)
6031 Typ = RTE (RE_Type_Specific_Data)
6033 Typ = RTE (RE_Tag_Table)
6035 (RTE_Available (RE_Interface_Data)
6036 and then Typ = RTE (RE_Interface_Data))
6038 (RTE_Available (RE_Interfaces_Array)
6039 and then Typ = RTE (RE_Interfaces_Array))
6041 (RTE_Available (RE_Interface_Data_Element)
6042 and then Typ = RTE (RE_Interface_Data_Element)));
6043 end Is_Static_Dispatch_Table_Aggregate;
6045 --------------------
6046 -- Late_Expansion --
6047 --------------------
6049 function Late_Expansion
6053 Flist : Node_Id := Empty;
6054 Obj : Entity_Id := Empty) return List_Id
6057 if Is_Record_Type (Etype (N)) then
6058 return Build_Record_Aggr_Code (N, Typ, Target, Flist, Obj);
6060 else pragma Assert (Is_Array_Type (Etype (N)));
6062 Build_Array_Aggr_Code
6064 Ctype => Component_Type (Etype (N)),
6065 Index => First_Index (Typ),
6067 Scalar_Comp => Is_Scalar_Type (Component_Type (Typ)),
6073 ----------------------------------
6074 -- Make_OK_Assignment_Statement --
6075 ----------------------------------
6077 function Make_OK_Assignment_Statement
6080 Expression : Node_Id) return Node_Id
6083 Set_Assignment_OK (Name);
6085 return Make_Assignment_Statement (Sloc, Name, Expression);
6086 end Make_OK_Assignment_Statement;
6088 -----------------------
6089 -- Number_Of_Choices --
6090 -----------------------
6092 function Number_Of_Choices (N : Node_Id) return Nat is
6096 Nb_Choices : Nat := 0;
6099 if Present (Expressions (N)) then
6103 Assoc := First (Component_Associations (N));
6104 while Present (Assoc) loop
6105 Choice := First (Choices (Assoc));
6106 while Present (Choice) loop
6107 if Nkind (Choice) /= N_Others_Choice then
6108 Nb_Choices := Nb_Choices + 1;
6118 end Number_Of_Choices;
6120 ------------------------------------
6121 -- Packed_Array_Aggregate_Handled --
6122 ------------------------------------
6124 -- The current version of this procedure will handle at compile time
6125 -- any array aggregate that meets these conditions:
6127 -- One dimensional, bit packed
6128 -- Underlying packed type is modular type
6129 -- Bounds are within 32-bit Int range
6130 -- All bounds and values are static
6132 function Packed_Array_Aggregate_Handled (N : Node_Id) return Boolean is
6133 Loc : constant Source_Ptr := Sloc (N);
6134 Typ : constant Entity_Id := Etype (N);
6135 Ctyp : constant Entity_Id := Component_Type (Typ);
6137 Not_Handled : exception;
6138 -- Exception raised if this aggregate cannot be handled
6141 -- For now, handle only one dimensional bit packed arrays
6143 if not Is_Bit_Packed_Array (Typ)
6144 or else Number_Dimensions (Typ) > 1
6145 or else not Is_Modular_Integer_Type (Packed_Array_Type (Typ))
6150 if not Is_Scalar_Type (Component_Type (Typ))
6151 and then Has_Non_Standard_Rep (Component_Type (Typ))
6157 Csiz : constant Nat := UI_To_Int (Component_Size (Typ));
6161 -- Bounds of index type
6165 -- Values of bounds if compile time known
6167 function Get_Component_Val (N : Node_Id) return Uint;
6168 -- Given a expression value N of the component type Ctyp, returns a
6169 -- value of Csiz (component size) bits representing this value. If
6170 -- the value is non-static or any other reason exists why the value
6171 -- cannot be returned, then Not_Handled is raised.
6173 -----------------------
6174 -- Get_Component_Val --
6175 -----------------------
6177 function Get_Component_Val (N : Node_Id) return Uint is
6181 -- We have to analyze the expression here before doing any further
6182 -- processing here. The analysis of such expressions is deferred
6183 -- till expansion to prevent some problems of premature analysis.
6185 Analyze_And_Resolve (N, Ctyp);
6187 -- Must have a compile time value. String literals have to be
6188 -- converted into temporaries as well, because they cannot easily
6189 -- be converted into their bit representation.
6191 if not Compile_Time_Known_Value (N)
6192 or else Nkind (N) = N_String_Literal
6197 Val := Expr_Rep_Value (N);
6199 -- Adjust for bias, and strip proper number of bits
6201 if Has_Biased_Representation (Ctyp) then
6202 Val := Val - Expr_Value (Type_Low_Bound (Ctyp));
6205 return Val mod Uint_2 ** Csiz;
6206 end Get_Component_Val;
6208 -- Here we know we have a one dimensional bit packed array
6211 Get_Index_Bounds (First_Index (Typ), Lo, Hi);
6213 -- Cannot do anything if bounds are dynamic
6215 if not Compile_Time_Known_Value (Lo)
6217 not Compile_Time_Known_Value (Hi)
6222 -- Or are silly out of range of int bounds
6224 Lob := Expr_Value (Lo);
6225 Hib := Expr_Value (Hi);
6227 if not UI_Is_In_Int_Range (Lob)
6229 not UI_Is_In_Int_Range (Hib)
6234 -- At this stage we have a suitable aggregate for handling at compile
6235 -- time (the only remaining checks are that the values of expressions
6236 -- in the aggregate are compile time known (check is performed by
6237 -- Get_Component_Val), and that any subtypes or ranges are statically
6240 -- If the aggregate is not fully positional at this stage, then
6241 -- convert it to positional form. Either this will fail, in which
6242 -- case we can do nothing, or it will succeed, in which case we have
6243 -- succeeded in handling the aggregate, or it will stay an aggregate,
6244 -- in which case we have failed to handle this case.
6246 if Present (Component_Associations (N)) then
6247 Convert_To_Positional
6248 (N, Max_Others_Replicate => 64, Handle_Bit_Packed => True);
6249 return Nkind (N) /= N_Aggregate;
6252 -- Otherwise we are all positional, so convert to proper value
6255 Lov : constant Int := UI_To_Int (Lob);
6256 Hiv : constant Int := UI_To_Int (Hib);
6258 Len : constant Nat := Int'Max (0, Hiv - Lov + 1);
6259 -- The length of the array (number of elements)
6261 Aggregate_Val : Uint;
6262 -- Value of aggregate. The value is set in the low order bits of
6263 -- this value. For the little-endian case, the values are stored
6264 -- from low-order to high-order and for the big-endian case the
6265 -- values are stored from high-order to low-order. Note that gigi
6266 -- will take care of the conversions to left justify the value in
6267 -- the big endian case (because of left justified modular type
6268 -- processing), so we do not have to worry about that here.
6271 -- Integer literal for resulting constructed value
6274 -- Shift count from low order for next value
6277 -- Shift increment for loop
6280 -- Next expression from positional parameters of aggregate
6283 -- For little endian, we fill up the low order bits of the target
6284 -- value. For big endian we fill up the high order bits of the
6285 -- target value (which is a left justified modular value).
6287 if Bytes_Big_Endian xor Debug_Flag_8 then
6288 Shift := Csiz * (Len - 1);
6295 -- Loop to set the values
6298 Aggregate_Val := Uint_0;
6300 Expr := First (Expressions (N));
6301 Aggregate_Val := Get_Component_Val (Expr) * Uint_2 ** Shift;
6303 for J in 2 .. Len loop
6304 Shift := Shift + Incr;
6307 Aggregate_Val + Get_Component_Val (Expr) * Uint_2 ** Shift;
6311 -- Now we can rewrite with the proper value
6314 Make_Integer_Literal (Loc,
6315 Intval => Aggregate_Val);
6316 Set_Print_In_Hex (Lit);
6318 -- Construct the expression using this literal. Note that it is
6319 -- important to qualify the literal with its proper modular type
6320 -- since universal integer does not have the required range and
6321 -- also this is a left justified modular type, which is important
6322 -- in the big-endian case.
6325 Unchecked_Convert_To (Typ,
6326 Make_Qualified_Expression (Loc,
6328 New_Occurrence_Of (Packed_Array_Type (Typ), Loc),
6329 Expression => Lit)));
6331 Analyze_And_Resolve (N, Typ);
6339 end Packed_Array_Aggregate_Handled;
6341 ----------------------------
6342 -- Has_Mutable_Components --
6343 ----------------------------
6345 function Has_Mutable_Components (Typ : Entity_Id) return Boolean is
6349 Comp := First_Component (Typ);
6350 while Present (Comp) loop
6351 if Is_Record_Type (Etype (Comp))
6352 and then Has_Discriminants (Etype (Comp))
6353 and then not Is_Constrained (Etype (Comp))
6358 Next_Component (Comp);
6362 end Has_Mutable_Components;
6364 ------------------------------
6365 -- Initialize_Discriminants --
6366 ------------------------------
6368 procedure Initialize_Discriminants (N : Node_Id; Typ : Entity_Id) is
6369 Loc : constant Source_Ptr := Sloc (N);
6370 Bas : constant Entity_Id := Base_Type (Typ);
6371 Par : constant Entity_Id := Etype (Bas);
6372 Decl : constant Node_Id := Parent (Par);
6376 if Is_Tagged_Type (Bas)
6377 and then Is_Derived_Type (Bas)
6378 and then Has_Discriminants (Par)
6379 and then Has_Discriminants (Bas)
6380 and then Number_Discriminants (Bas) /= Number_Discriminants (Par)
6381 and then Nkind (Decl) = N_Full_Type_Declaration
6382 and then Nkind (Type_Definition (Decl)) = N_Record_Definition
6384 (Variant_Part (Component_List (Type_Definition (Decl))))
6385 and then Nkind (N) /= N_Extension_Aggregate
6388 -- Call init proc to set discriminants.
6389 -- There should eventually be a special procedure for this ???
6391 Ref := New_Reference_To (Defining_Identifier (N), Loc);
6392 Insert_Actions_After (N,
6393 Build_Initialization_Call (Sloc (N), Ref, Typ));
6395 end Initialize_Discriminants;
6402 (Obj_Type : Entity_Id;
6403 Typ : Entity_Id) return Boolean
6405 L1, L2, H1, H2 : Node_Id;
6407 -- No sliding if the type of the object is not established yet, if it is
6408 -- an unconstrained type whose actual subtype comes from the aggregate,
6409 -- or if the two types are identical.
6411 if not Is_Array_Type (Obj_Type) then
6414 elsif not Is_Constrained (Obj_Type) then
6417 elsif Typ = Obj_Type then
6421 -- Sliding can only occur along the first dimension
6423 Get_Index_Bounds (First_Index (Typ), L1, H1);
6424 Get_Index_Bounds (First_Index (Obj_Type), L2, H2);
6426 if not Is_Static_Expression (L1)
6427 or else not Is_Static_Expression (L2)
6428 or else not Is_Static_Expression (H1)
6429 or else not Is_Static_Expression (H2)
6433 return Expr_Value (L1) /= Expr_Value (L2)
6434 or else Expr_Value (H1) /= Expr_Value (H2);
6439 ---------------------------
6440 -- Safe_Slice_Assignment --
6441 ---------------------------
6443 function Safe_Slice_Assignment (N : Node_Id) return Boolean is
6444 Loc : constant Source_Ptr := Sloc (Parent (N));
6445 Pref : constant Node_Id := Prefix (Name (Parent (N)));
6446 Range_Node : constant Node_Id := Discrete_Range (Name (Parent (N)));
6454 -- Generate: for J in Range loop Pref (J) := Expr; end loop;
6456 if Comes_From_Source (N)
6457 and then No (Expressions (N))
6458 and then Nkind (First (Choices (First (Component_Associations (N)))))
6461 Expr := Expression (First (Component_Associations (N)));
6462 L_J := Make_Temporary (Loc, 'J');
6465 Make_Iteration_Scheme (Loc,
6466 Loop_Parameter_Specification =>
6467 Make_Loop_Parameter_Specification
6469 Defining_Identifier => L_J,
6470 Discrete_Subtype_Definition => Relocate_Node (Range_Node)));
6473 Make_Assignment_Statement (Loc,
6475 Make_Indexed_Component (Loc,
6476 Prefix => Relocate_Node (Pref),
6477 Expressions => New_List (New_Occurrence_Of (L_J, Loc))),
6478 Expression => Relocate_Node (Expr));
6480 -- Construct the final loop
6483 Make_Implicit_Loop_Statement
6484 (Node => Parent (N),
6485 Identifier => Empty,
6486 Iteration_Scheme => L_Iter,
6487 Statements => New_List (L_Body));
6489 -- Set type of aggregate to be type of lhs in assignment,
6490 -- to suppress redundant length checks.
6492 Set_Etype (N, Etype (Name (Parent (N))));
6494 Rewrite (Parent (N), Stat);
6495 Analyze (Parent (N));
6501 end Safe_Slice_Assignment;
6503 ---------------------
6504 -- Sort_Case_Table --
6505 ---------------------
6507 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type) is
6508 L : constant Int := Case_Table'First;
6509 U : constant Int := Case_Table'Last;
6517 T := Case_Table (K + 1);
6521 and then Expr_Value (Case_Table (J - 1).Choice_Lo) >
6522 Expr_Value (T.Choice_Lo)
6524 Case_Table (J) := Case_Table (J - 1);
6528 Case_Table (J) := T;
6531 end Sort_Case_Table;
6533 ----------------------------
6534 -- Static_Array_Aggregate --
6535 ----------------------------
6537 function Static_Array_Aggregate (N : Node_Id) return Boolean is
6538 Bounds : constant Node_Id := Aggregate_Bounds (N);
6540 Typ : constant Entity_Id := Etype (N);
6541 Comp_Type : constant Entity_Id := Component_Type (Typ);
6548 if Is_Tagged_Type (Typ)
6549 or else Is_Controlled (Typ)
6550 or else Is_Packed (Typ)
6556 and then Nkind (Bounds) = N_Range
6557 and then Nkind (Low_Bound (Bounds)) = N_Integer_Literal
6558 and then Nkind (High_Bound (Bounds)) = N_Integer_Literal
6560 Lo := Low_Bound (Bounds);
6561 Hi := High_Bound (Bounds);
6563 if No (Component_Associations (N)) then
6565 -- Verify that all components are static integers
6567 Expr := First (Expressions (N));
6568 while Present (Expr) loop
6569 if Nkind (Expr) /= N_Integer_Literal then
6579 -- We allow only a single named association, either a static
6580 -- range or an others_clause, with a static expression.
6582 Expr := First (Component_Associations (N));
6584 if Present (Expressions (N)) then
6587 elsif Present (Next (Expr)) then
6590 elsif Present (Next (First (Choices (Expr)))) then
6594 -- The aggregate is static if all components are literals,
6595 -- or else all its components are static aggregates for the
6596 -- component type. We also limit the size of a static aggregate
6597 -- to prevent runaway static expressions.
6599 if Is_Array_Type (Comp_Type)
6600 or else Is_Record_Type (Comp_Type)
6602 if Nkind (Expression (Expr)) /= N_Aggregate
6604 not Compile_Time_Known_Aggregate (Expression (Expr))
6609 elsif Nkind (Expression (Expr)) /= N_Integer_Literal then
6612 elsif not Aggr_Size_OK (N, Typ) then
6616 -- Create a positional aggregate with the right number of
6617 -- copies of the expression.
6619 Agg := Make_Aggregate (Sloc (N), New_List, No_List);
6621 for I in UI_To_Int (Intval (Lo)) .. UI_To_Int (Intval (Hi))
6624 (Expressions (Agg), New_Copy (Expression (Expr)));
6626 -- The copied expression must be analyzed and resolved.
6627 -- Besides setting the type, this ensures that static
6628 -- expressions are appropriately marked as such.
6631 (Last (Expressions (Agg)), Component_Type (Typ));
6634 Set_Aggregate_Bounds (Agg, Bounds);
6635 Set_Etype (Agg, Typ);
6638 Set_Compile_Time_Known_Aggregate (N);
6647 end Static_Array_Aggregate;