1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2008, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Atree; use Atree;
27 with Checks; use Checks;
28 with Debug; use Debug;
29 with Einfo; use Einfo;
30 with Elists; use Elists;
31 with Expander; use Expander;
32 with Exp_Util; use Exp_Util;
33 with Exp_Ch3; use Exp_Ch3;
34 with Exp_Ch7; use Exp_Ch7;
35 with Exp_Ch9; use Exp_Ch9;
36 with Exp_Tss; use Exp_Tss;
37 with Freeze; use Freeze;
38 with Itypes; use Itypes;
40 with Namet; use Namet;
41 with Nmake; use Nmake;
42 with Nlists; use Nlists;
44 with Restrict; use Restrict;
45 with Rident; use Rident;
46 with Rtsfind; use Rtsfind;
47 with Ttypes; use Ttypes;
49 with Sem_Ch3; use Sem_Ch3;
50 with Sem_Eval; use Sem_Eval;
51 with Sem_Res; use Sem_Res;
52 with Sem_Util; use Sem_Util;
53 with Sinfo; use Sinfo;
54 with Snames; use Snames;
55 with Stand; use Stand;
56 with Targparm; use Targparm;
57 with Tbuild; use Tbuild;
58 with Uintp; use Uintp;
60 package body Exp_Aggr is
62 type Case_Bounds is record
65 Choice_Node : Node_Id;
68 type Case_Table_Type is array (Nat range <>) of Case_Bounds;
69 -- Table type used by Check_Case_Choices procedure
72 (Obj_Type : Entity_Id;
73 Typ : Entity_Id) return Boolean;
74 -- A static array aggregate in an object declaration can in most cases be
75 -- expanded in place. The one exception is when the aggregate is given
76 -- with component associations that specify different bounds from those of
77 -- the type definition in the object declaration. In this pathological
78 -- case the aggregate must slide, and we must introduce an intermediate
79 -- temporary to hold it.
81 -- The same holds in an assignment to one-dimensional array of arrays,
82 -- when a component may be given with bounds that differ from those of the
85 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type);
86 -- Sort the Case Table using the Lower Bound of each Choice as the key.
87 -- A simple insertion sort is used since the number of choices in a case
88 -- statement of variant part will usually be small and probably in near
91 function Has_Default_Init_Comps (N : Node_Id) return Boolean;
92 -- N is an aggregate (record or array). Checks the presence of default
93 -- initialization (<>) in any component (Ada 2005: AI-287)
95 function Is_Static_Dispatch_Table_Aggregate (N : Node_Id) return Boolean;
96 -- Returns true if N is an aggregate used to initialize the components
97 -- of an statically allocated dispatch table.
99 ------------------------------------------------------
100 -- Local subprograms for Record Aggregate Expansion --
101 ------------------------------------------------------
103 procedure Expand_Record_Aggregate
105 Orig_Tag : Node_Id := Empty;
106 Parent_Expr : Node_Id := Empty);
107 -- This is the top level procedure for record aggregate expansion.
108 -- Expansion for record aggregates needs expand aggregates for tagged
109 -- record types. Specifically Expand_Record_Aggregate adds the Tag
110 -- field in front of the Component_Association list that was created
111 -- during resolution by Resolve_Record_Aggregate.
113 -- N is the record aggregate node.
114 -- Orig_Tag is the value of the Tag that has to be provided for this
115 -- specific aggregate. It carries the tag corresponding to the type
116 -- of the outermost aggregate during the recursive expansion
117 -- Parent_Expr is the ancestor part of the original extension
120 procedure Convert_To_Assignments (N : Node_Id; Typ : Entity_Id);
121 -- N is an N_Aggregate or an N_Extension_Aggregate. Typ is the type of the
122 -- aggregate (which can only be a record type, this procedure is only used
123 -- for record types). Transform the given aggregate into a sequence of
124 -- assignments performed component by component.
126 function Build_Record_Aggr_Code
130 Flist : Node_Id := Empty;
131 Obj : Entity_Id := Empty;
132 Is_Limited_Ancestor_Expansion : Boolean := False) return List_Id;
133 -- N is an N_Aggregate or an N_Extension_Aggregate. Typ is the type of the
134 -- aggregate. Target is an expression containing the location on which the
135 -- component by component assignments will take place. Returns the list of
136 -- assignments plus all other adjustments needed for tagged and controlled
137 -- types. Flist is an expression representing the finalization list on
138 -- which to attach the controlled components if any. Obj is present in the
139 -- object declaration and dynamic allocation cases, it contains an entity
140 -- that allows to know if the value being created needs to be attached to
141 -- the final list in case of pragma Finalize_Storage_Only.
144 -- The meaning of the Obj formal is extremely unclear. *What* entity
145 -- should be passed? For the object declaration case we may guess that
146 -- this is the object being declared, but what about the allocator case?
148 -- Is_Limited_Ancestor_Expansion indicates that the function has been
149 -- called recursively to expand the limited ancestor to avoid copying it.
151 function Has_Mutable_Components (Typ : Entity_Id) return Boolean;
152 -- Return true if one of the component is of a discriminated type with
153 -- defaults. An aggregate for a type with mutable components must be
154 -- expanded into individual assignments.
156 procedure Initialize_Discriminants (N : Node_Id; Typ : Entity_Id);
157 -- If the type of the aggregate is a type extension with renamed discrimi-
158 -- nants, we must initialize the hidden discriminants of the parent.
159 -- Otherwise, the target object must not be initialized. The discriminants
160 -- are initialized by calling the initialization procedure for the type.
161 -- This is incorrect if the initialization of other components has any
162 -- side effects. We restrict this call to the case where the parent type
163 -- has a variant part, because this is the only case where the hidden
164 -- discriminants are accessed, namely when calling discriminant checking
165 -- functions of the parent type, and when applying a stream attribute to
166 -- an object of the derived type.
168 -----------------------------------------------------
169 -- Local Subprograms for Array Aggregate Expansion --
170 -----------------------------------------------------
172 function Aggr_Size_OK (Typ : Entity_Id) return Boolean;
173 -- Very large static aggregates present problems to the back-end, and
174 -- are transformed into assignments and loops. This function verifies
175 -- that the total number of components of an aggregate is acceptable
176 -- for transformation into a purely positional static form. It is called
177 -- prior to calling Flatten.
179 procedure Convert_Array_Aggr_In_Allocator
183 -- If the aggregate appears within an allocator and can be expanded in
184 -- place, this routine generates the individual assignments to components
185 -- of the designated object. This is an optimization over the general
186 -- case, where a temporary is first created on the stack and then used to
187 -- construct the allocated object on the heap.
189 procedure Convert_To_Positional
191 Max_Others_Replicate : Nat := 5;
192 Handle_Bit_Packed : Boolean := False);
193 -- If possible, convert named notation to positional notation. This
194 -- conversion is possible only in some static cases. If the conversion is
195 -- possible, then N is rewritten with the analyzed converted aggregate.
196 -- The parameter Max_Others_Replicate controls the maximum number of
197 -- values corresponding to an others choice that will be converted to
198 -- positional notation (the default of 5 is the normal limit, and reflects
199 -- the fact that normally the loop is better than a lot of separate
200 -- assignments). Note that this limit gets overridden in any case if
201 -- either of the restrictions No_Elaboration_Code or No_Implicit_Loops is
202 -- set. The parameter Handle_Bit_Packed is usually set False (since we do
203 -- not expect the back end to handle bit packed arrays, so the normal case
204 -- of conversion is pointless), but in the special case of a call from
205 -- Packed_Array_Aggregate_Handled, we set this parameter to True, since
206 -- these are cases we handle in there.
208 procedure Expand_Array_Aggregate (N : Node_Id);
209 -- This is the top-level routine to perform array aggregate expansion.
210 -- N is the N_Aggregate node to be expanded.
212 function Backend_Processing_Possible (N : Node_Id) return Boolean;
213 -- This function checks if array aggregate N can be processed directly
214 -- by Gigi. If this is the case True is returned.
216 function Build_Array_Aggr_Code
221 Scalar_Comp : Boolean;
222 Indices : List_Id := No_List;
223 Flist : Node_Id := Empty) return List_Id;
224 -- This recursive routine returns a list of statements containing the
225 -- loops and assignments that are needed for the expansion of the array
228 -- N is the (sub-)aggregate node to be expanded into code. This node
229 -- has been fully analyzed, and its Etype is properly set.
231 -- Index is the index node corresponding to the array sub-aggregate N.
233 -- Into is the target expression into which we are copying the aggregate.
234 -- Note that this node may not have been analyzed yet, and so the Etype
235 -- field may not be set.
237 -- Scalar_Comp is True if the component type of the aggregate is scalar.
239 -- Indices is the current list of expressions used to index the
240 -- object we are writing into.
242 -- Flist is an expression representing the finalization list on which
243 -- to attach the controlled components if any.
245 function Number_Of_Choices (N : Node_Id) return Nat;
246 -- Returns the number of discrete choices (not including the others choice
247 -- if present) contained in (sub-)aggregate N.
249 function Late_Expansion
253 Flist : Node_Id := Empty;
254 Obj : Entity_Id := Empty) return List_Id;
255 -- N is a nested (record or array) aggregate that has been marked with
256 -- 'Delay_Expansion'. Typ is the expected type of the aggregate and Target
257 -- is a (duplicable) expression that will hold the result of the aggregate
258 -- expansion. Flist is the finalization list to be used to attach
259 -- controlled components. 'Obj' when non empty, carries the original
260 -- object being initialized in order to know if it needs to be attached to
261 -- the previous parameter which may not be the case in the case where
262 -- Finalize_Storage_Only is set. Basically this procedure is used to
263 -- implement top-down expansions of nested aggregates. This is necessary
264 -- for avoiding temporaries at each level as well as for propagating the
265 -- right internal finalization list.
267 function Make_OK_Assignment_Statement
270 Expression : Node_Id) return Node_Id;
271 -- This is like Make_Assignment_Statement, except that Assignment_OK
272 -- is set in the left operand. All assignments built by this unit
273 -- use this routine. This is needed to deal with assignments to
274 -- initialized constants that are done in place.
276 function Packed_Array_Aggregate_Handled (N : Node_Id) return Boolean;
277 -- Given an array aggregate, this function handles the case of a packed
278 -- array aggregate with all constant values, where the aggregate can be
279 -- evaluated at compile time. If this is possible, then N is rewritten
280 -- to be its proper compile time value with all the components properly
281 -- assembled. The expression is analyzed and resolved and True is
282 -- returned. If this transformation is not possible, N is unchanged
283 -- and False is returned
285 function Safe_Slice_Assignment (N : Node_Id) return Boolean;
286 -- If a slice assignment has an aggregate with a single others_choice,
287 -- the assignment can be done in place even if bounds are not static,
288 -- by converting it into a loop over the discrete range of the slice.
294 function Aggr_Size_OK (Typ : Entity_Id) return Boolean is
302 -- The following constant determines the maximum size of an
303 -- aggregate produced by converting named to positional
304 -- notation (e.g. from others clauses). This avoids running
305 -- away with attempts to convert huge aggregates, which hit
306 -- memory limits in the backend.
308 -- The normal limit is 5000, but we increase this limit to
309 -- 2**24 (about 16 million) if Restrictions (No_Elaboration_Code)
310 -- or Restrictions (No_Implicit_Loops) is specified, since in
311 -- either case, we are at risk of declaring the program illegal
312 -- because of this limit.
314 Max_Aggr_Size : constant Nat :=
315 5000 + (2 ** 24 - 5000) *
317 (Restriction_Active (No_Elaboration_Code)
319 Restriction_Active (No_Implicit_Loops));
321 function Component_Count (T : Entity_Id) return Int;
322 -- The limit is applied to the total number of components that the
323 -- aggregate will have, which is the number of static expressions
324 -- that will appear in the flattened array. This requires a recursive
325 -- computation of the the number of scalar components of the structure.
327 ---------------------
328 -- Component_Count --
329 ---------------------
331 function Component_Count (T : Entity_Id) return Int is
336 if Is_Scalar_Type (T) then
339 elsif Is_Record_Type (T) then
340 Comp := First_Component (T);
341 while Present (Comp) loop
342 Res := Res + Component_Count (Etype (Comp));
343 Next_Component (Comp);
348 elsif Is_Array_Type (T) then
350 Lo : constant Node_Id :=
351 Type_Low_Bound (Etype (First_Index (T)));
352 Hi : constant Node_Id :=
353 Type_High_Bound (Etype (First_Index (T)));
355 Siz : constant Int := Component_Count (Component_Type (T));
358 if not Compile_Time_Known_Value (Lo)
359 or else not Compile_Time_Known_Value (Hi)
364 Siz * UI_To_Int (Expr_Value (Hi) - Expr_Value (Lo) + 1);
369 -- Can only be a null for an access type
375 -- Start of processing for Aggr_Size_OK
378 Siz := Component_Count (Component_Type (Typ));
380 Indx := First_Index (Typ);
381 while Present (Indx) loop
382 Lo := Type_Low_Bound (Etype (Indx));
383 Hi := Type_High_Bound (Etype (Indx));
385 -- Bounds need to be known at compile time
387 if not Compile_Time_Known_Value (Lo)
388 or else not Compile_Time_Known_Value (Hi)
393 Lov := Expr_Value (Lo);
394 Hiv := Expr_Value (Hi);
396 -- A flat array is always safe
403 Rng : constant Uint := Hiv - Lov + 1;
406 -- Check if size is too large
408 if not UI_Is_In_Int_Range (Rng) then
412 Siz := Siz * UI_To_Int (Rng);
416 or else Siz > Max_Aggr_Size
421 -- Bounds must be in integer range, for later array construction
423 if not UI_Is_In_Int_Range (Lov)
425 not UI_Is_In_Int_Range (Hiv)
436 ---------------------------------
437 -- Backend_Processing_Possible --
438 ---------------------------------
440 -- Backend processing by Gigi/gcc is possible only if all the following
441 -- conditions are met:
443 -- 1. N is fully positional
445 -- 2. N is not a bit-packed array aggregate;
447 -- 3. The size of N's array type must be known at compile time. Note
448 -- that this implies that the component size is also known
450 -- 4. The array type of N does not follow the Fortran layout convention
451 -- or if it does it must be 1 dimensional.
453 -- 5. The array component type may not be tagged (which could necessitate
454 -- reassignment of proper tags).
456 -- 6. The array component type must not have unaligned bit components
458 -- 7. None of the components of the aggregate may be bit unaligned
461 -- 8. There cannot be delayed components, since we do not know enough
462 -- at this stage to know if back end processing is possible.
464 -- 9. There cannot be any discriminated record components, since the
465 -- back end cannot handle this complex case.
467 function Backend_Processing_Possible (N : Node_Id) return Boolean is
468 Typ : constant Entity_Id := Etype (N);
469 -- Typ is the correct constrained array subtype of the aggregate
471 function Component_Check (N : Node_Id; Index : Node_Id) return Boolean;
472 -- This routine checks components of aggregate N, enforcing checks
473 -- 1, 7, 8, and 9. In the multi-dimensional case, these checks are
474 -- performed on subaggregates. The Index value is the current index
475 -- being checked in the multi-dimensional case.
477 ---------------------
478 -- Component_Check --
479 ---------------------
481 function Component_Check (N : Node_Id; Index : Node_Id) return Boolean is
485 -- Checks 1: (no component associations)
487 if Present (Component_Associations (N)) then
491 -- Checks on components
493 -- Recurse to check subaggregates, which may appear in qualified
494 -- expressions. If delayed, the front-end will have to expand.
495 -- If the component is a discriminated record, treat as non-static,
496 -- as the back-end cannot handle this properly.
498 Expr := First (Expressions (N));
499 while Present (Expr) loop
501 -- Checks 8: (no delayed components)
503 if Is_Delayed_Aggregate (Expr) then
507 -- Checks 9: (no discriminated records)
509 if Present (Etype (Expr))
510 and then Is_Record_Type (Etype (Expr))
511 and then Has_Discriminants (Etype (Expr))
516 -- Checks 7. Component must not be bit aligned component
518 if Possible_Bit_Aligned_Component (Expr) then
522 -- Recursion to following indexes for multiple dimension case
524 if Present (Next_Index (Index))
525 and then not Component_Check (Expr, Next_Index (Index))
530 -- All checks for that component finished, on to next
538 -- Start of processing for Backend_Processing_Possible
541 -- Checks 2 (array must not be bit packed)
543 if Is_Bit_Packed_Array (Typ) then
547 -- Checks 4 (array must not be multi-dimensional Fortran case)
549 if Convention (Typ) = Convention_Fortran
550 and then Number_Dimensions (Typ) > 1
555 -- Checks 3 (size of array must be known at compile time)
557 if not Size_Known_At_Compile_Time (Typ) then
561 -- Checks on components
563 if not Component_Check (N, First_Index (Typ)) then
567 -- Checks 5 (if the component type is tagged, then we may need to do
568 -- tag adjustments. Perhaps this should be refined to check for any
569 -- component associations that actually need tag adjustment, similar
570 -- to the test in Component_Not_OK_For_Backend for record aggregates
571 -- with tagged components, but not clear whether it's worthwhile ???;
572 -- in the case of the JVM, object tags are handled implicitly)
574 if Is_Tagged_Type (Component_Type (Typ)) and then VM_Target = No_VM then
578 -- Checks 6 (component type must not have bit aligned components)
580 if Type_May_Have_Bit_Aligned_Components (Component_Type (Typ)) then
584 -- Backend processing is possible
586 Set_Size_Known_At_Compile_Time (Etype (N), True);
588 end Backend_Processing_Possible;
590 ---------------------------
591 -- Build_Array_Aggr_Code --
592 ---------------------------
594 -- The code that we generate from a one dimensional aggregate is
596 -- 1. If the sub-aggregate contains discrete choices we
598 -- (a) Sort the discrete choices
600 -- (b) Otherwise for each discrete choice that specifies a range we
601 -- emit a loop. If a range specifies a maximum of three values, or
602 -- we are dealing with an expression we emit a sequence of
603 -- assignments instead of a loop.
605 -- (c) Generate the remaining loops to cover the others choice if any
607 -- 2. If the aggregate contains positional elements we
609 -- (a) translate the positional elements in a series of assignments
611 -- (b) Generate a final loop to cover the others choice if any.
612 -- Note that this final loop has to be a while loop since the case
614 -- L : Integer := Integer'Last;
615 -- H : Integer := Integer'Last;
616 -- A : array (L .. H) := (1, others =>0);
618 -- cannot be handled by a for loop. Thus for the following
620 -- array (L .. H) := (.. positional elements.., others =>E);
622 -- we always generate something like:
624 -- J : Index_Type := Index_Of_Last_Positional_Element;
626 -- J := Index_Base'Succ (J)
630 function Build_Array_Aggr_Code
635 Scalar_Comp : Boolean;
636 Indices : List_Id := No_List;
637 Flist : Node_Id := Empty) return List_Id
639 Loc : constant Source_Ptr := Sloc (N);
640 Index_Base : constant Entity_Id := Base_Type (Etype (Index));
641 Index_Base_L : constant Node_Id := Type_Low_Bound (Index_Base);
642 Index_Base_H : constant Node_Id := Type_High_Bound (Index_Base);
644 function Add (Val : Int; To : Node_Id) return Node_Id;
645 -- Returns an expression where Val is added to expression To, unless
646 -- To+Val is provably out of To's base type range. To must be an
647 -- already analyzed expression.
649 function Empty_Range (L, H : Node_Id) return Boolean;
650 -- Returns True if the range defined by L .. H is certainly empty
652 function Equal (L, H : Node_Id) return Boolean;
653 -- Returns True if L = H for sure
655 function Index_Base_Name return Node_Id;
656 -- Returns a new reference to the index type name
658 function Gen_Assign (Ind : Node_Id; Expr : Node_Id) return List_Id;
659 -- Ind must be a side-effect free expression. If the input aggregate
660 -- N to Build_Loop contains no sub-aggregates, then this function
661 -- returns the assignment statement:
663 -- Into (Indices, Ind) := Expr;
665 -- Otherwise we call Build_Code recursively
667 -- Ada 2005 (AI-287): In case of default initialized component, Expr
668 -- is empty and we generate a call to the corresponding IP subprogram.
670 function Gen_Loop (L, H : Node_Id; Expr : Node_Id) return List_Id;
671 -- Nodes L and H must be side-effect free expressions.
672 -- If the input aggregate N to Build_Loop contains no sub-aggregates,
673 -- This routine returns the for loop statement
675 -- for J in Index_Base'(L) .. Index_Base'(H) loop
676 -- Into (Indices, J) := Expr;
679 -- Otherwise we call Build_Code recursively.
680 -- As an optimization if the loop covers 3 or less scalar elements we
681 -- generate a sequence of assignments.
683 function Gen_While (L, H : Node_Id; Expr : Node_Id) return List_Id;
684 -- Nodes L and H must be side-effect free expressions.
685 -- If the input aggregate N to Build_Loop contains no sub-aggregates,
686 -- This routine returns the while loop statement
688 -- J : Index_Base := L;
690 -- J := Index_Base'Succ (J);
691 -- Into (Indices, J) := Expr;
694 -- Otherwise we call Build_Code recursively
696 function Local_Compile_Time_Known_Value (E : Node_Id) return Boolean;
697 function Local_Expr_Value (E : Node_Id) return Uint;
698 -- These two Local routines are used to replace the corresponding ones
699 -- in sem_eval because while processing the bounds of an aggregate with
700 -- discrete choices whose index type is an enumeration, we build static
701 -- expressions not recognized by Compile_Time_Known_Value as such since
702 -- they have not yet been analyzed and resolved. All the expressions in
703 -- question are things like Index_Base_Name'Val (Const) which we can
704 -- easily recognize as being constant.
710 function Add (Val : Int; To : Node_Id) return Node_Id is
715 U_Val : constant Uint := UI_From_Int (Val);
718 -- Note: do not try to optimize the case of Val = 0, because
719 -- we need to build a new node with the proper Sloc value anyway.
721 -- First test if we can do constant folding
723 if Local_Compile_Time_Known_Value (To) then
724 U_To := Local_Expr_Value (To) + Val;
726 -- Determine if our constant is outside the range of the index.
727 -- If so return an Empty node. This empty node will be caught
728 -- by Empty_Range below.
730 if Compile_Time_Known_Value (Index_Base_L)
731 and then U_To < Expr_Value (Index_Base_L)
735 elsif Compile_Time_Known_Value (Index_Base_H)
736 and then U_To > Expr_Value (Index_Base_H)
741 Expr_Pos := Make_Integer_Literal (Loc, U_To);
742 Set_Is_Static_Expression (Expr_Pos);
744 if not Is_Enumeration_Type (Index_Base) then
747 -- If we are dealing with enumeration return
748 -- Index_Base'Val (Expr_Pos)
752 Make_Attribute_Reference
754 Prefix => Index_Base_Name,
755 Attribute_Name => Name_Val,
756 Expressions => New_List (Expr_Pos));
762 -- If we are here no constant folding possible
764 if not Is_Enumeration_Type (Index_Base) then
767 Left_Opnd => Duplicate_Subexpr (To),
768 Right_Opnd => Make_Integer_Literal (Loc, U_Val));
770 -- If we are dealing with enumeration return
771 -- Index_Base'Val (Index_Base'Pos (To) + Val)
775 Make_Attribute_Reference
777 Prefix => Index_Base_Name,
778 Attribute_Name => Name_Pos,
779 Expressions => New_List (Duplicate_Subexpr (To)));
784 Right_Opnd => Make_Integer_Literal (Loc, U_Val));
787 Make_Attribute_Reference
789 Prefix => Index_Base_Name,
790 Attribute_Name => Name_Val,
791 Expressions => New_List (Expr_Pos));
801 function Empty_Range (L, H : Node_Id) return Boolean is
802 Is_Empty : Boolean := False;
807 -- First check if L or H were already detected as overflowing the
808 -- index base range type by function Add above. If this is so Add
809 -- returns the empty node.
811 if No (L) or else No (H) then
818 -- L > H range is empty
824 -- B_L > H range must be empty
830 -- L > B_H range must be empty
834 High := Index_Base_H;
837 if Local_Compile_Time_Known_Value (Low)
838 and then Local_Compile_Time_Known_Value (High)
841 UI_Gt (Local_Expr_Value (Low), Local_Expr_Value (High));
854 function Equal (L, H : Node_Id) return Boolean is
859 elsif Local_Compile_Time_Known_Value (L)
860 and then Local_Compile_Time_Known_Value (H)
862 return UI_Eq (Local_Expr_Value (L), Local_Expr_Value (H));
872 function Gen_Assign (Ind : Node_Id; Expr : Node_Id) return List_Id is
873 L : constant List_Id := New_List;
877 New_Indices : List_Id;
878 Indexed_Comp : Node_Id;
880 Comp_Type : Entity_Id := Empty;
882 function Add_Loop_Actions (Lis : List_Id) return List_Id;
883 -- Collect insert_actions generated in the construction of a
884 -- loop, and prepend them to the sequence of assignments to
885 -- complete the eventual body of the loop.
887 ----------------------
888 -- Add_Loop_Actions --
889 ----------------------
891 function Add_Loop_Actions (Lis : List_Id) return List_Id is
895 -- Ada 2005 (AI-287): Do nothing else in case of default
896 -- initialized component.
901 elsif Nkind (Parent (Expr)) = N_Component_Association
902 and then Present (Loop_Actions (Parent (Expr)))
904 Append_List (Lis, Loop_Actions (Parent (Expr)));
905 Res := Loop_Actions (Parent (Expr));
906 Set_Loop_Actions (Parent (Expr), No_List);
912 end Add_Loop_Actions;
914 -- Start of processing for Gen_Assign
918 New_Indices := New_List;
920 New_Indices := New_Copy_List_Tree (Indices);
923 Append_To (New_Indices, Ind);
925 if Present (Flist) then
926 F := New_Copy_Tree (Flist);
928 elsif Present (Etype (N)) and then Controlled_Type (Etype (N)) then
929 if Is_Entity_Name (Into)
930 and then Present (Scope (Entity (Into)))
932 F := Find_Final_List (Scope (Entity (Into)));
934 F := Find_Final_List (Current_Scope);
940 if Present (Next_Index (Index)) then
943 Build_Array_Aggr_Code
946 Index => Next_Index (Index),
948 Scalar_Comp => Scalar_Comp,
949 Indices => New_Indices,
953 -- If we get here then we are at a bottom-level (sub-)aggregate
957 (Make_Indexed_Component (Loc,
958 Prefix => New_Copy_Tree (Into),
959 Expressions => New_Indices));
961 Set_Assignment_OK (Indexed_Comp);
963 -- Ada 2005 (AI-287): In case of default initialized component, Expr
964 -- is not present (and therefore we also initialize Expr_Q to empty).
968 elsif Nkind (Expr) = N_Qualified_Expression then
969 Expr_Q := Expression (Expr);
974 if Present (Etype (N))
975 and then Etype (N) /= Any_Composite
977 Comp_Type := Component_Type (Etype (N));
978 pragma Assert (Comp_Type = Ctype); -- AI-287
980 elsif Present (Next (First (New_Indices))) then
982 -- Ada 2005 (AI-287): Do nothing in case of default initialized
983 -- component because we have received the component type in
984 -- the formal parameter Ctype.
986 -- ??? Some assert pragmas have been added to check if this new
987 -- formal can be used to replace this code in all cases.
989 if Present (Expr) then
991 -- This is a multidimensional array. Recover the component
992 -- type from the outermost aggregate, because subaggregates
993 -- do not have an assigned type.
1000 while Present (P) loop
1001 if Nkind (P) = N_Aggregate
1002 and then Present (Etype (P))
1004 Comp_Type := Component_Type (Etype (P));
1012 pragma Assert (Comp_Type = Ctype); -- AI-287
1017 -- Ada 2005 (AI-287): We only analyze the expression in case of non-
1018 -- default initialized components (otherwise Expr_Q is not present).
1021 and then (Nkind (Expr_Q) = N_Aggregate
1022 or else Nkind (Expr_Q) = N_Extension_Aggregate)
1024 -- At this stage the Expression may not have been
1025 -- analyzed yet because the array aggregate code has not
1026 -- been updated to use the Expansion_Delayed flag and
1027 -- avoid analysis altogether to solve the same problem
1028 -- (see Resolve_Aggr_Expr). So let us do the analysis of
1029 -- non-array aggregates now in order to get the value of
1030 -- Expansion_Delayed flag for the inner aggregate ???
1032 if Present (Comp_Type) and then not Is_Array_Type (Comp_Type) then
1033 Analyze_And_Resolve (Expr_Q, Comp_Type);
1036 if Is_Delayed_Aggregate (Expr_Q) then
1038 -- This is either a subaggregate of a multidimentional array,
1039 -- or a component of an array type whose component type is
1040 -- also an array. In the latter case, the expression may have
1041 -- component associations that provide different bounds from
1042 -- those of the component type, and sliding must occur. Instead
1043 -- of decomposing the current aggregate assignment, force the
1044 -- re-analysis of the assignment, so that a temporary will be
1045 -- generated in the usual fashion, and sliding will take place.
1047 if Nkind (Parent (N)) = N_Assignment_Statement
1048 and then Is_Array_Type (Comp_Type)
1049 and then Present (Component_Associations (Expr_Q))
1050 and then Must_Slide (Comp_Type, Etype (Expr_Q))
1052 Set_Expansion_Delayed (Expr_Q, False);
1053 Set_Analyzed (Expr_Q, False);
1059 Expr_Q, Etype (Expr_Q), Indexed_Comp, F));
1064 -- Ada 2005 (AI-287): In case of default initialized component, call
1065 -- the initialization subprogram associated with the component type.
1066 -- If the component type is an access type, add an explicit null
1067 -- assignment, because for the back-end there is an initialization
1068 -- present for the whole aggregate, and no default initialization
1071 -- In addition, if the component type is controlled, we must call
1072 -- its Initialize procedure explicitly, because there is no explicit
1073 -- object creation that will invoke it otherwise.
1076 if Present (Base_Init_Proc (Base_Type (Ctype)))
1077 or else Has_Task (Base_Type (Ctype))
1080 Build_Initialization_Call (Loc,
1081 Id_Ref => Indexed_Comp,
1083 With_Default_Init => True));
1085 elsif Is_Access_Type (Ctype) then
1087 Make_Assignment_Statement (Loc,
1088 Name => Indexed_Comp,
1089 Expression => Make_Null (Loc)));
1092 if Controlled_Type (Ctype) then
1095 Ref => New_Copy_Tree (Indexed_Comp),
1097 Flist_Ref => Find_Final_List (Current_Scope),
1098 With_Attach => Make_Integer_Literal (Loc, 1)));
1102 -- Now generate the assignment with no associated controlled
1103 -- actions since the target of the assignment may not have been
1104 -- initialized, it is not possible to Finalize it as expected by
1105 -- normal controlled assignment. The rest of the controlled
1106 -- actions are done manually with the proper finalization list
1107 -- coming from the context.
1110 Make_OK_Assignment_Statement (Loc,
1111 Name => Indexed_Comp,
1112 Expression => New_Copy_Tree (Expr));
1114 if Present (Comp_Type) and then Controlled_Type (Comp_Type) then
1115 Set_No_Ctrl_Actions (A);
1117 -- If this is an aggregate for an array of arrays, each
1118 -- sub-aggregate will be expanded as well, and even with
1119 -- No_Ctrl_Actions the assignments of inner components will
1120 -- require attachment in their assignments to temporaries.
1121 -- These temporaries must be finalized for each subaggregate,
1122 -- to prevent multiple attachments of the same temporary
1123 -- location to same finalization chain (and consequently
1124 -- circular lists). To ensure that finalization takes place
1125 -- for each subaggregate we wrap the assignment in a block.
1127 if Is_Array_Type (Comp_Type)
1128 and then Nkind (Expr) = N_Aggregate
1131 Make_Block_Statement (Loc,
1132 Handled_Statement_Sequence =>
1133 Make_Handled_Sequence_Of_Statements (Loc,
1134 Statements => New_List (A)));
1140 -- Adjust the tag if tagged (because of possible view
1141 -- conversions), unless compiling for the Java VM where
1142 -- tags are implicit.
1144 if Present (Comp_Type)
1145 and then Is_Tagged_Type (Comp_Type)
1146 and then VM_Target = No_VM
1149 Make_OK_Assignment_Statement (Loc,
1151 Make_Selected_Component (Loc,
1152 Prefix => New_Copy_Tree (Indexed_Comp),
1155 (First_Tag_Component (Comp_Type), Loc)),
1158 Unchecked_Convert_To (RTE (RE_Tag),
1160 (Node (First_Elmt (Access_Disp_Table (Comp_Type))),
1166 -- Adjust and attach the component to the proper final list, which
1167 -- can be the controller of the outer record object or the final
1168 -- list associated with the scope.
1170 -- If the component is itself an array of controlled types, whose
1171 -- value is given by a sub-aggregate, then the attach calls have
1172 -- been generated when individual subcomponent are assigned, and
1173 -- and must not be done again to prevent malformed finalization
1174 -- chains (see comments above, concerning the creation of a block
1175 -- to hold inner finalization actions).
1177 if Present (Comp_Type)
1178 and then Controlled_Type (Comp_Type)
1179 and then not Is_Limited_Type (Comp_Type)
1181 (not Is_Array_Type (Comp_Type)
1182 or else not Is_Controlled (Component_Type (Comp_Type))
1183 or else Nkind (Expr) /= N_Aggregate)
1187 Ref => New_Copy_Tree (Indexed_Comp),
1190 With_Attach => Make_Integer_Literal (Loc, 1)));
1194 return Add_Loop_Actions (L);
1201 function Gen_Loop (L, H : Node_Id; Expr : Node_Id) return List_Id is
1205 -- Index_Base'(L) .. Index_Base'(H)
1207 L_Iteration_Scheme : Node_Id;
1208 -- L_J in Index_Base'(L) .. Index_Base'(H)
1211 -- The statements to execute in the loop
1213 S : constant List_Id := New_List;
1214 -- List of statements
1217 -- Copy of expression tree, used for checking purposes
1220 -- If loop bounds define an empty range return the null statement
1222 if Empty_Range (L, H) then
1223 Append_To (S, Make_Null_Statement (Loc));
1225 -- Ada 2005 (AI-287): Nothing else need to be done in case of
1226 -- default initialized component.
1232 -- The expression must be type-checked even though no component
1233 -- of the aggregate will have this value. This is done only for
1234 -- actual components of the array, not for subaggregates. Do
1235 -- the check on a copy, because the expression may be shared
1236 -- among several choices, some of which might be non-null.
1238 if Present (Etype (N))
1239 and then Is_Array_Type (Etype (N))
1240 and then No (Next_Index (Index))
1242 Expander_Mode_Save_And_Set (False);
1243 Tcopy := New_Copy_Tree (Expr);
1244 Set_Parent (Tcopy, N);
1245 Analyze_And_Resolve (Tcopy, Component_Type (Etype (N)));
1246 Expander_Mode_Restore;
1252 -- If loop bounds are the same then generate an assignment
1254 elsif Equal (L, H) then
1255 return Gen_Assign (New_Copy_Tree (L), Expr);
1257 -- If H - L <= 2 then generate a sequence of assignments when we are
1258 -- processing the bottom most aggregate and it contains scalar
1261 elsif No (Next_Index (Index))
1262 and then Scalar_Comp
1263 and then Local_Compile_Time_Known_Value (L)
1264 and then Local_Compile_Time_Known_Value (H)
1265 and then Local_Expr_Value (H) - Local_Expr_Value (L) <= 2
1268 Append_List_To (S, Gen_Assign (New_Copy_Tree (L), Expr));
1269 Append_List_To (S, Gen_Assign (Add (1, To => L), Expr));
1271 if Local_Expr_Value (H) - Local_Expr_Value (L) = 2 then
1272 Append_List_To (S, Gen_Assign (Add (2, To => L), Expr));
1278 -- Otherwise construct the loop, starting with the loop index L_J
1280 L_J := Make_Defining_Identifier (Loc, New_Internal_Name ('J'));
1282 -- Construct "L .. H"
1287 Low_Bound => Make_Qualified_Expression
1289 Subtype_Mark => Index_Base_Name,
1291 High_Bound => Make_Qualified_Expression
1293 Subtype_Mark => Index_Base_Name,
1296 -- Construct "for L_J in Index_Base range L .. H"
1298 L_Iteration_Scheme :=
1299 Make_Iteration_Scheme
1301 Loop_Parameter_Specification =>
1302 Make_Loop_Parameter_Specification
1304 Defining_Identifier => L_J,
1305 Discrete_Subtype_Definition => L_Range));
1307 -- Construct the statements to execute in the loop body
1309 L_Body := Gen_Assign (New_Reference_To (L_J, Loc), Expr);
1311 -- Construct the final loop
1313 Append_To (S, Make_Implicit_Loop_Statement
1315 Identifier => Empty,
1316 Iteration_Scheme => L_Iteration_Scheme,
1317 Statements => L_Body));
1319 -- A small optimization: if the aggregate is initialized with a box
1320 -- and the component type has no initialization procedure, remove the
1321 -- useless empty loop.
1323 if Nkind (First (S)) = N_Loop_Statement
1324 and then Is_Empty_List (Statements (First (S)))
1326 return New_List (Make_Null_Statement (Loc));
1336 -- The code built is
1338 -- W_J : Index_Base := L;
1339 -- while W_J < H loop
1340 -- W_J := Index_Base'Succ (W);
1344 function Gen_While (L, H : Node_Id; Expr : Node_Id) return List_Id is
1348 -- W_J : Base_Type := L;
1350 W_Iteration_Scheme : Node_Id;
1353 W_Index_Succ : Node_Id;
1354 -- Index_Base'Succ (J)
1356 W_Increment : Node_Id;
1357 -- W_J := Index_Base'Succ (W)
1359 W_Body : constant List_Id := New_List;
1360 -- The statements to execute in the loop
1362 S : constant List_Id := New_List;
1363 -- list of statement
1366 -- If loop bounds define an empty range or are equal return null
1368 if Empty_Range (L, H) or else Equal (L, H) then
1369 Append_To (S, Make_Null_Statement (Loc));
1373 -- Build the decl of W_J
1375 W_J := Make_Defining_Identifier (Loc, New_Internal_Name ('J'));
1377 Make_Object_Declaration
1379 Defining_Identifier => W_J,
1380 Object_Definition => Index_Base_Name,
1383 -- Theoretically we should do a New_Copy_Tree (L) here, but we know
1384 -- that in this particular case L is a fresh Expr generated by
1385 -- Add which we are the only ones to use.
1387 Append_To (S, W_Decl);
1389 -- Construct " while W_J < H"
1391 W_Iteration_Scheme :=
1392 Make_Iteration_Scheme
1394 Condition => Make_Op_Lt
1396 Left_Opnd => New_Reference_To (W_J, Loc),
1397 Right_Opnd => New_Copy_Tree (H)));
1399 -- Construct the statements to execute in the loop body
1402 Make_Attribute_Reference
1404 Prefix => Index_Base_Name,
1405 Attribute_Name => Name_Succ,
1406 Expressions => New_List (New_Reference_To (W_J, Loc)));
1409 Make_OK_Assignment_Statement
1411 Name => New_Reference_To (W_J, Loc),
1412 Expression => W_Index_Succ);
1414 Append_To (W_Body, W_Increment);
1415 Append_List_To (W_Body,
1416 Gen_Assign (New_Reference_To (W_J, Loc), Expr));
1418 -- Construct the final loop
1420 Append_To (S, Make_Implicit_Loop_Statement
1422 Identifier => Empty,
1423 Iteration_Scheme => W_Iteration_Scheme,
1424 Statements => W_Body));
1429 ---------------------
1430 -- Index_Base_Name --
1431 ---------------------
1433 function Index_Base_Name return Node_Id is
1435 return New_Reference_To (Index_Base, Sloc (N));
1436 end Index_Base_Name;
1438 ------------------------------------
1439 -- Local_Compile_Time_Known_Value --
1440 ------------------------------------
1442 function Local_Compile_Time_Known_Value (E : Node_Id) return Boolean is
1444 return Compile_Time_Known_Value (E)
1446 (Nkind (E) = N_Attribute_Reference
1447 and then Attribute_Name (E) = Name_Val
1448 and then Compile_Time_Known_Value (First (Expressions (E))));
1449 end Local_Compile_Time_Known_Value;
1451 ----------------------
1452 -- Local_Expr_Value --
1453 ----------------------
1455 function Local_Expr_Value (E : Node_Id) return Uint is
1457 if Compile_Time_Known_Value (E) then
1458 return Expr_Value (E);
1460 return Expr_Value (First (Expressions (E)));
1462 end Local_Expr_Value;
1464 -- Build_Array_Aggr_Code Variables
1471 Others_Expr : Node_Id := Empty;
1472 Others_Box_Present : Boolean := False;
1474 Aggr_L : constant Node_Id := Low_Bound (Aggregate_Bounds (N));
1475 Aggr_H : constant Node_Id := High_Bound (Aggregate_Bounds (N));
1476 -- The aggregate bounds of this specific sub-aggregate. Note that if
1477 -- the code generated by Build_Array_Aggr_Code is executed then these
1478 -- bounds are OK. Otherwise a Constraint_Error would have been raised.
1480 Aggr_Low : constant Node_Id := Duplicate_Subexpr_No_Checks (Aggr_L);
1481 Aggr_High : constant Node_Id := Duplicate_Subexpr_No_Checks (Aggr_H);
1482 -- After Duplicate_Subexpr these are side-effect free
1487 Nb_Choices : Nat := 0;
1488 Table : Case_Table_Type (1 .. Number_Of_Choices (N));
1489 -- Used to sort all the different choice values
1492 -- Number of elements in the positional aggregate
1494 New_Code : constant List_Id := New_List;
1496 -- Start of processing for Build_Array_Aggr_Code
1499 -- First before we start, a special case. if we have a bit packed
1500 -- array represented as a modular type, then clear the value to
1501 -- zero first, to ensure that unused bits are properly cleared.
1506 and then Is_Bit_Packed_Array (Typ)
1507 and then Is_Modular_Integer_Type (Packed_Array_Type (Typ))
1509 Append_To (New_Code,
1510 Make_Assignment_Statement (Loc,
1511 Name => New_Copy_Tree (Into),
1513 Unchecked_Convert_To (Typ,
1514 Make_Integer_Literal (Loc, Uint_0))));
1517 -- STEP 1: Process component associations
1519 -- For those associations that may generate a loop, initialize
1520 -- Loop_Actions to collect inserted actions that may be crated.
1522 -- Skip this if no component associations
1524 if No (Expressions (N)) then
1526 -- STEP 1 (a): Sort the discrete choices
1528 Assoc := First (Component_Associations (N));
1529 while Present (Assoc) loop
1530 Choice := First (Choices (Assoc));
1531 while Present (Choice) loop
1532 if Nkind (Choice) = N_Others_Choice then
1533 Set_Loop_Actions (Assoc, New_List);
1535 if Box_Present (Assoc) then
1536 Others_Box_Present := True;
1538 Others_Expr := Expression (Assoc);
1543 Get_Index_Bounds (Choice, Low, High);
1546 Set_Loop_Actions (Assoc, New_List);
1549 Nb_Choices := Nb_Choices + 1;
1550 if Box_Present (Assoc) then
1551 Table (Nb_Choices) := (Choice_Lo => Low,
1553 Choice_Node => Empty);
1555 Table (Nb_Choices) := (Choice_Lo => Low,
1557 Choice_Node => Expression (Assoc));
1565 -- If there is more than one set of choices these must be static
1566 -- and we can therefore sort them. Remember that Nb_Choices does not
1567 -- account for an others choice.
1569 if Nb_Choices > 1 then
1570 Sort_Case_Table (Table);
1573 -- STEP 1 (b): take care of the whole set of discrete choices
1575 for J in 1 .. Nb_Choices loop
1576 Low := Table (J).Choice_Lo;
1577 High := Table (J).Choice_Hi;
1578 Expr := Table (J).Choice_Node;
1579 Append_List (Gen_Loop (Low, High, Expr), To => New_Code);
1582 -- STEP 1 (c): generate the remaining loops to cover others choice
1583 -- We don't need to generate loops over empty gaps, but if there is
1584 -- a single empty range we must analyze the expression for semantics
1586 if Present (Others_Expr) or else Others_Box_Present then
1588 First : Boolean := True;
1591 for J in 0 .. Nb_Choices loop
1595 Low := Add (1, To => Table (J).Choice_Hi);
1598 if J = Nb_Choices then
1601 High := Add (-1, To => Table (J + 1).Choice_Lo);
1604 -- If this is an expansion within an init proc, make
1605 -- sure that discriminant references are replaced by
1606 -- the corresponding discriminal.
1608 if Inside_Init_Proc then
1609 if Is_Entity_Name (Low)
1610 and then Ekind (Entity (Low)) = E_Discriminant
1612 Set_Entity (Low, Discriminal (Entity (Low)));
1615 if Is_Entity_Name (High)
1616 and then Ekind (Entity (High)) = E_Discriminant
1618 Set_Entity (High, Discriminal (Entity (High)));
1623 or else not Empty_Range (Low, High)
1627 (Gen_Loop (Low, High, Others_Expr), To => New_Code);
1633 -- STEP 2: Process positional components
1636 -- STEP 2 (a): Generate the assignments for each positional element
1637 -- Note that here we have to use Aggr_L rather than Aggr_Low because
1638 -- Aggr_L is analyzed and Add wants an analyzed expression.
1640 Expr := First (Expressions (N));
1642 while Present (Expr) loop
1643 Nb_Elements := Nb_Elements + 1;
1644 Append_List (Gen_Assign (Add (Nb_Elements, To => Aggr_L), Expr),
1649 -- STEP 2 (b): Generate final loop if an others choice is present
1650 -- Here Nb_Elements gives the offset of the last positional element.
1652 if Present (Component_Associations (N)) then
1653 Assoc := Last (Component_Associations (N));
1655 -- Ada 2005 (AI-287)
1657 if Box_Present (Assoc) then
1658 Append_List (Gen_While (Add (Nb_Elements, To => Aggr_L),
1663 Expr := Expression (Assoc);
1665 Append_List (Gen_While (Add (Nb_Elements, To => Aggr_L),
1674 end Build_Array_Aggr_Code;
1676 ----------------------------
1677 -- Build_Record_Aggr_Code --
1678 ----------------------------
1680 ----------------------------
1681 -- Build_Record_Aggr_Code --
1682 ----------------------------
1684 function Build_Record_Aggr_Code
1688 Flist : Node_Id := Empty;
1689 Obj : Entity_Id := Empty;
1690 Is_Limited_Ancestor_Expansion : Boolean := False) return List_Id
1692 Loc : constant Source_Ptr := Sloc (N);
1693 L : constant List_Id := New_List;
1694 N_Typ : constant Entity_Id := Etype (N);
1701 Comp_Type : Entity_Id;
1702 Selector : Entity_Id;
1703 Comp_Expr : Node_Id;
1706 Internal_Final_List : Node_Id := Empty;
1708 -- If this is an internal aggregate, the External_Final_List is an
1709 -- expression for the controller record of the enclosing type.
1711 -- If the current aggregate has several controlled components, this
1712 -- expression will appear in several calls to attach to the finali-
1713 -- zation list, and it must not be shared.
1715 External_Final_List : Node_Id;
1716 Ancestor_Is_Expression : Boolean := False;
1717 Ancestor_Is_Subtype_Mark : Boolean := False;
1719 Init_Typ : Entity_Id := Empty;
1722 Ctrl_Stuff_Done : Boolean := False;
1723 -- True if Gen_Ctrl_Actions_For_Aggr has already been called; calls
1724 -- after the first do nothing.
1726 function Ancestor_Discriminant_Value (Disc : Entity_Id) return Node_Id;
1727 -- Returns the value that the given discriminant of an ancestor type
1728 -- should receive (in the absence of a conflict with the value provided
1729 -- by an ancestor part of an extension aggregate).
1731 procedure Check_Ancestor_Discriminants (Anc_Typ : Entity_Id);
1732 -- Check that each of the discriminant values defined by the ancestor
1733 -- part of an extension aggregate match the corresponding values
1734 -- provided by either an association of the aggregate or by the
1735 -- constraint imposed by a parent type (RM95-4.3.2(8)).
1737 function Compatible_Int_Bounds
1738 (Agg_Bounds : Node_Id;
1739 Typ_Bounds : Node_Id) return Boolean;
1740 -- Return true if Agg_Bounds are equal or within Typ_Bounds. It is
1741 -- assumed that both bounds are integer ranges.
1743 procedure Gen_Ctrl_Actions_For_Aggr;
1744 -- Deal with the various controlled type data structure initializations
1745 -- (but only if it hasn't been done already).
1747 function Get_Constraint_Association (T : Entity_Id) return Node_Id;
1748 -- Returns the first discriminant association in the constraint
1749 -- associated with T, if any, otherwise returns Empty.
1751 function Init_Controller
1756 Init_Pr : Boolean) return List_Id;
1757 -- Returns the list of statements necessary to initialize the internal
1758 -- controller of the (possible) ancestor typ into target and attach it
1759 -- to finalization list F. Init_Pr conditions the call to the init proc
1760 -- since it may already be done due to ancestor initialization.
1762 function Is_Int_Range_Bounds (Bounds : Node_Id) return Boolean;
1763 -- Check whether Bounds is a range node and its lower and higher bounds
1764 -- are integers literals.
1766 ---------------------------------
1767 -- Ancestor_Discriminant_Value --
1768 ---------------------------------
1770 function Ancestor_Discriminant_Value (Disc : Entity_Id) return Node_Id is
1772 Assoc_Elmt : Elmt_Id;
1773 Aggr_Comp : Entity_Id;
1774 Corresp_Disc : Entity_Id;
1775 Current_Typ : Entity_Id := Base_Type (Typ);
1776 Parent_Typ : Entity_Id;
1777 Parent_Disc : Entity_Id;
1778 Save_Assoc : Node_Id := Empty;
1781 -- First check any discriminant associations to see if any of them
1782 -- provide a value for the discriminant.
1784 if Present (Discriminant_Specifications (Parent (Current_Typ))) then
1785 Assoc := First (Component_Associations (N));
1786 while Present (Assoc) loop
1787 Aggr_Comp := Entity (First (Choices (Assoc)));
1789 if Ekind (Aggr_Comp) = E_Discriminant then
1790 Save_Assoc := Expression (Assoc);
1792 Corresp_Disc := Corresponding_Discriminant (Aggr_Comp);
1793 while Present (Corresp_Disc) loop
1795 -- If found a corresponding discriminant then return the
1796 -- value given in the aggregate. (Note: this is not
1797 -- correct in the presence of side effects. ???)
1799 if Disc = Corresp_Disc then
1800 return Duplicate_Subexpr (Expression (Assoc));
1804 Corresponding_Discriminant (Corresp_Disc);
1812 -- No match found in aggregate, so chain up parent types to find
1813 -- a constraint that defines the value of the discriminant.
1815 Parent_Typ := Etype (Current_Typ);
1816 while Current_Typ /= Parent_Typ loop
1817 if Has_Discriminants (Parent_Typ) then
1818 Parent_Disc := First_Discriminant (Parent_Typ);
1820 -- We either get the association from the subtype indication
1821 -- of the type definition itself, or from the discriminant
1822 -- constraint associated with the type entity (which is
1823 -- preferable, but it's not always present ???)
1825 if Is_Empty_Elmt_List (
1826 Discriminant_Constraint (Current_Typ))
1828 Assoc := Get_Constraint_Association (Current_Typ);
1829 Assoc_Elmt := No_Elmt;
1832 First_Elmt (Discriminant_Constraint (Current_Typ));
1833 Assoc := Node (Assoc_Elmt);
1836 -- Traverse the discriminants of the parent type looking
1837 -- for one that corresponds.
1839 while Present (Parent_Disc) and then Present (Assoc) loop
1840 Corresp_Disc := Parent_Disc;
1841 while Present (Corresp_Disc)
1842 and then Disc /= Corresp_Disc
1845 Corresponding_Discriminant (Corresp_Disc);
1848 if Disc = Corresp_Disc then
1849 if Nkind (Assoc) = N_Discriminant_Association then
1850 Assoc := Expression (Assoc);
1853 -- If the located association directly denotes a
1854 -- discriminant, then use the value of a saved
1855 -- association of the aggregate. This is a kludge to
1856 -- handle certain cases involving multiple discriminants
1857 -- mapped to a single discriminant of a descendant. It's
1858 -- not clear how to locate the appropriate discriminant
1859 -- value for such cases. ???
1861 if Is_Entity_Name (Assoc)
1862 and then Ekind (Entity (Assoc)) = E_Discriminant
1864 Assoc := Save_Assoc;
1867 return Duplicate_Subexpr (Assoc);
1870 Next_Discriminant (Parent_Disc);
1872 if No (Assoc_Elmt) then
1875 Next_Elmt (Assoc_Elmt);
1876 if Present (Assoc_Elmt) then
1877 Assoc := Node (Assoc_Elmt);
1885 Current_Typ := Parent_Typ;
1886 Parent_Typ := Etype (Current_Typ);
1889 -- In some cases there's no ancestor value to locate (such as
1890 -- when an ancestor part given by an expression defines the
1891 -- discriminant value).
1894 end Ancestor_Discriminant_Value;
1896 ----------------------------------
1897 -- Check_Ancestor_Discriminants --
1898 ----------------------------------
1900 procedure Check_Ancestor_Discriminants (Anc_Typ : Entity_Id) is
1902 Disc_Value : Node_Id;
1906 Discr := First_Discriminant (Base_Type (Anc_Typ));
1907 while Present (Discr) loop
1908 Disc_Value := Ancestor_Discriminant_Value (Discr);
1910 if Present (Disc_Value) then
1911 Cond := Make_Op_Ne (Loc,
1913 Make_Selected_Component (Loc,
1914 Prefix => New_Copy_Tree (Target),
1915 Selector_Name => New_Occurrence_Of (Discr, Loc)),
1916 Right_Opnd => Disc_Value);
1919 Make_Raise_Constraint_Error (Loc,
1921 Reason => CE_Discriminant_Check_Failed));
1924 Next_Discriminant (Discr);
1926 end Check_Ancestor_Discriminants;
1928 ---------------------------
1929 -- Compatible_Int_Bounds --
1930 ---------------------------
1932 function Compatible_Int_Bounds
1933 (Agg_Bounds : Node_Id;
1934 Typ_Bounds : Node_Id) return Boolean
1936 Agg_Lo : constant Uint := Intval (Low_Bound (Agg_Bounds));
1937 Agg_Hi : constant Uint := Intval (High_Bound (Agg_Bounds));
1938 Typ_Lo : constant Uint := Intval (Low_Bound (Typ_Bounds));
1939 Typ_Hi : constant Uint := Intval (High_Bound (Typ_Bounds));
1941 return Typ_Lo <= Agg_Lo and then Agg_Hi <= Typ_Hi;
1942 end Compatible_Int_Bounds;
1944 --------------------------------
1945 -- Get_Constraint_Association --
1946 --------------------------------
1948 function Get_Constraint_Association (T : Entity_Id) return Node_Id is
1949 Typ_Def : constant Node_Id := Type_Definition (Parent (T));
1950 Indic : constant Node_Id := Subtype_Indication (Typ_Def);
1953 -- ??? Also need to cover case of a type mark denoting a subtype
1956 if Nkind (Indic) = N_Subtype_Indication
1957 and then Present (Constraint (Indic))
1959 return First (Constraints (Constraint (Indic)));
1963 end Get_Constraint_Association;
1965 ---------------------
1966 -- Init_Controller --
1967 ---------------------
1969 function Init_Controller
1974 Init_Pr : Boolean) return List_Id
1976 L : constant List_Id := New_List;
1979 Target_Type : Entity_Id;
1983 -- init-proc (target._controller);
1984 -- initialize (target._controller);
1985 -- Attach_to_Final_List (target._controller, F);
1988 Make_Selected_Component (Loc,
1989 Prefix => Convert_To (Typ, New_Copy_Tree (Target)),
1990 Selector_Name => Make_Identifier (Loc, Name_uController));
1991 Set_Assignment_OK (Ref);
1993 -- Ada 2005 (AI-287): Give support to aggregates of limited types.
1994 -- If the type is intrinsically limited the controller is limited as
1995 -- well. If it is tagged and limited then so is the controller.
1996 -- Otherwise an untagged type may have limited components without its
1997 -- full view being limited, so the controller is not limited.
1999 if Nkind (Target) = N_Identifier then
2000 Target_Type := Etype (Target);
2002 elsif Nkind (Target) = N_Selected_Component then
2003 Target_Type := Etype (Selector_Name (Target));
2005 elsif Nkind (Target) = N_Unchecked_Type_Conversion then
2006 Target_Type := Etype (Target);
2008 elsif Nkind (Target) = N_Unchecked_Expression
2009 and then Nkind (Expression (Target)) = N_Indexed_Component
2011 Target_Type := Etype (Prefix (Expression (Target)));
2014 Target_Type := Etype (Target);
2017 -- If the target has not been analyzed yet, as will happen with
2018 -- delayed expansion, use the given type (either the aggregate type
2019 -- or an ancestor) to determine limitedness.
2021 if No (Target_Type) then
2025 if (Is_Tagged_Type (Target_Type))
2026 and then Is_Limited_Type (Target_Type)
2028 RC := RE_Limited_Record_Controller;
2030 elsif Is_Inherently_Limited_Type (Target_Type) then
2031 RC := RE_Limited_Record_Controller;
2034 RC := RE_Record_Controller;
2039 Build_Initialization_Call (Loc,
2042 In_Init_Proc => Within_Init_Proc));
2046 Make_Procedure_Call_Statement (Loc,
2049 Find_Prim_Op (RTE (RC), Name_Initialize), Loc),
2050 Parameter_Associations =>
2051 New_List (New_Copy_Tree (Ref))));
2055 Obj_Ref => New_Copy_Tree (Ref),
2057 With_Attach => Attach));
2060 end Init_Controller;
2062 -------------------------
2063 -- Is_Int_Range_Bounds --
2064 -------------------------
2066 function Is_Int_Range_Bounds (Bounds : Node_Id) return Boolean is
2068 return Nkind (Bounds) = N_Range
2069 and then Nkind (Low_Bound (Bounds)) = N_Integer_Literal
2070 and then Nkind (High_Bound (Bounds)) = N_Integer_Literal;
2071 end Is_Int_Range_Bounds;
2073 -------------------------------
2074 -- Gen_Ctrl_Actions_For_Aggr --
2075 -------------------------------
2077 procedure Gen_Ctrl_Actions_For_Aggr is
2078 Alloc : Node_Id := Empty;
2081 -- Do the work only the first time this is called
2083 if Ctrl_Stuff_Done then
2087 Ctrl_Stuff_Done := True;
2090 and then Finalize_Storage_Only (Typ)
2092 (Is_Library_Level_Entity (Obj)
2093 or else Entity (Constant_Value (RTE (RE_Garbage_Collected))) =
2096 -- why not Is_True (Expr_Value (RTE (RE_Garbaage_Collected) ???
2098 Attach := Make_Integer_Literal (Loc, 0);
2100 elsif Nkind (Parent (N)) = N_Qualified_Expression
2101 and then Nkind (Parent (Parent (N))) = N_Allocator
2103 Alloc := Parent (Parent (N));
2104 Attach := Make_Integer_Literal (Loc, 2);
2107 Attach := Make_Integer_Literal (Loc, 1);
2110 -- Determine the external finalization list. It is either the
2111 -- finalization list of the outer-scope or the one coming from
2112 -- an outer aggregate. When the target is not a temporary, the
2113 -- proper scope is the scope of the target rather than the
2114 -- potentially transient current scope.
2116 if Controlled_Type (Typ) then
2118 -- The current aggregate belongs to an allocator which creates
2119 -- an object through an anonymous access type or acts as the root
2120 -- of a coextension chain.
2124 (Is_Coextension_Root (Alloc)
2125 or else Ekind (Etype (Alloc)) = E_Anonymous_Access_Type)
2127 if No (Associated_Final_Chain (Etype (Alloc))) then
2128 Build_Final_List (Alloc, Etype (Alloc));
2131 External_Final_List :=
2132 Make_Selected_Component (Loc,
2135 Associated_Final_Chain (Etype (Alloc)), Loc),
2137 Make_Identifier (Loc, Name_F));
2139 elsif Present (Flist) then
2140 External_Final_List := New_Copy_Tree (Flist);
2142 elsif Is_Entity_Name (Target)
2143 and then Present (Scope (Entity (Target)))
2145 External_Final_List :=
2146 Find_Final_List (Scope (Entity (Target)));
2149 External_Final_List := Find_Final_List (Current_Scope);
2152 External_Final_List := Empty;
2155 -- Initialize and attach the outer object in the is_controlled case
2157 if Is_Controlled (Typ) then
2158 if Ancestor_Is_Subtype_Mark then
2159 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
2160 Set_Assignment_OK (Ref);
2162 Make_Procedure_Call_Statement (Loc,
2165 (Find_Prim_Op (Init_Typ, Name_Initialize), Loc),
2166 Parameter_Associations => New_List (New_Copy_Tree (Ref))));
2169 if not Has_Controlled_Component (Typ) then
2170 Ref := New_Copy_Tree (Target);
2171 Set_Assignment_OK (Ref);
2173 -- This is an aggregate of a coextension. Do not produce a
2174 -- finalization call, but rather attach the reference of the
2175 -- aggregate to its coextension chain.
2178 and then Is_Dynamic_Coextension (Alloc)
2180 if No (Coextensions (Alloc)) then
2181 Set_Coextensions (Alloc, New_Elmt_List);
2184 Append_Elmt (Ref, Coextensions (Alloc));
2189 Flist_Ref => New_Copy_Tree (External_Final_List),
2190 With_Attach => Attach));
2195 -- In the Has_Controlled component case, all the intermediate
2196 -- controllers must be initialized.
2198 if Has_Controlled_Component (Typ)
2199 and not Is_Limited_Ancestor_Expansion
2202 Inner_Typ : Entity_Id;
2203 Outer_Typ : Entity_Id;
2207 -- Find outer type with a controller
2209 Outer_Typ := Base_Type (Typ);
2210 while Outer_Typ /= Init_Typ
2211 and then not Has_New_Controlled_Component (Outer_Typ)
2213 Outer_Typ := Etype (Outer_Typ);
2216 -- Attach it to the outer record controller to the external
2219 if Outer_Typ = Init_Typ then
2224 F => External_Final_List,
2229 Inner_Typ := Init_Typ;
2236 F => External_Final_List,
2240 Inner_Typ := Etype (Outer_Typ);
2242 not Is_Tagged_Type (Typ) or else Inner_Typ = Outer_Typ;
2245 -- The outer object has to be attached as well
2247 if Is_Controlled (Typ) then
2248 Ref := New_Copy_Tree (Target);
2249 Set_Assignment_OK (Ref);
2253 Flist_Ref => New_Copy_Tree (External_Final_List),
2254 With_Attach => New_Copy_Tree (Attach)));
2257 -- Initialize the internal controllers for tagged types with
2258 -- more than one controller.
2260 while not At_Root and then Inner_Typ /= Init_Typ loop
2261 if Has_New_Controlled_Component (Inner_Typ) then
2263 Make_Selected_Component (Loc,
2265 Convert_To (Outer_Typ, New_Copy_Tree (Target)),
2267 Make_Identifier (Loc, Name_uController));
2269 Make_Selected_Component (Loc,
2271 Selector_Name => Make_Identifier (Loc, Name_F));
2278 Attach => Make_Integer_Literal (Loc, 1),
2280 Outer_Typ := Inner_Typ;
2285 At_Root := Inner_Typ = Etype (Inner_Typ);
2286 Inner_Typ := Etype (Inner_Typ);
2289 -- If not done yet attach the controller of the ancestor part
2291 if Outer_Typ /= Init_Typ
2292 and then Inner_Typ = Init_Typ
2293 and then Has_Controlled_Component (Init_Typ)
2296 Make_Selected_Component (Loc,
2297 Prefix => Convert_To (Outer_Typ, New_Copy_Tree (Target)),
2299 Make_Identifier (Loc, Name_uController));
2301 Make_Selected_Component (Loc,
2303 Selector_Name => Make_Identifier (Loc, Name_F));
2305 Attach := Make_Integer_Literal (Loc, 1);
2314 -- Note: Init_Pr is False because the ancestor part has
2315 -- already been initialized either way (by default, if
2316 -- given by a type name, otherwise from the expression).
2321 end Gen_Ctrl_Actions_For_Aggr;
2323 function Replace_Type (Expr : Node_Id) return Traverse_Result;
2324 -- If the aggregate contains a self-reference, traverse each expression
2325 -- to replace a possible self-reference with a reference to the proper
2326 -- component of the target of the assignment.
2332 function Replace_Type (Expr : Node_Id) return Traverse_Result is
2334 -- Note regarding the Root_Type test below: Aggregate components for
2335 -- self-referential types include attribute references to the current
2336 -- instance, of the form: Typ'access, etc.. These references are
2337 -- rewritten as references to the target of the aggregate: the
2338 -- left-hand side of an assignment, the entity in a declaration,
2339 -- or a temporary. Without this test, we would improperly extended
2340 -- this rewriting to attribute references whose prefix was not the
2341 -- type of the aggregate.
2343 if Nkind (Expr) = N_Attribute_Reference
2344 and then Is_Entity_Name (Prefix (Expr))
2345 and then Is_Type (Entity (Prefix (Expr)))
2346 and then Root_Type (Etype (N)) = Root_Type (Entity (Prefix (Expr)))
2348 if Is_Entity_Name (Lhs) then
2349 Rewrite (Prefix (Expr),
2350 New_Occurrence_Of (Entity (Lhs), Loc));
2352 elsif Nkind (Lhs) = N_Selected_Component then
2354 Make_Attribute_Reference (Loc,
2355 Attribute_Name => Name_Unrestricted_Access,
2356 Prefix => New_Copy_Tree (Prefix (Lhs))));
2357 Set_Analyzed (Parent (Expr), False);
2361 Make_Attribute_Reference (Loc,
2362 Attribute_Name => Name_Unrestricted_Access,
2363 Prefix => New_Copy_Tree (Lhs)));
2364 Set_Analyzed (Parent (Expr), False);
2371 procedure Replace_Self_Reference is
2372 new Traverse_Proc (Replace_Type);
2374 -- Start of processing for Build_Record_Aggr_Code
2377 if Has_Self_Reference (N) then
2378 Replace_Self_Reference (N);
2381 -- If the target of the aggregate is class-wide, we must convert it
2382 -- to the actual type of the aggregate, so that the proper components
2383 -- are visible. We know already that the types are compatible.
2385 if Present (Etype (Lhs))
2386 and then Is_Interface (Etype (Lhs))
2388 Target := Unchecked_Convert_To (Typ, Lhs);
2393 -- Deal with the ancestor part of extension aggregates or with the
2394 -- discriminants of the root type.
2396 if Nkind (N) = N_Extension_Aggregate then
2398 A : constant Node_Id := Ancestor_Part (N);
2402 -- If the ancestor part is a subtype mark "T", we generate
2404 -- init-proc (T(tmp)); if T is constrained and
2405 -- init-proc (S(tmp)); where S applies an appropriate
2406 -- constraint if T is unconstrained
2408 if Is_Entity_Name (A) and then Is_Type (Entity (A)) then
2409 Ancestor_Is_Subtype_Mark := True;
2411 if Is_Constrained (Entity (A)) then
2412 Init_Typ := Entity (A);
2414 -- For an ancestor part given by an unconstrained type mark,
2415 -- create a subtype constrained by appropriate corresponding
2416 -- discriminant values coming from either associations of the
2417 -- aggregate or a constraint on a parent type. The subtype will
2418 -- be used to generate the correct default value for the
2421 elsif Has_Discriminants (Entity (A)) then
2423 Anc_Typ : constant Entity_Id := Entity (A);
2424 Anc_Constr : constant List_Id := New_List;
2425 Discrim : Entity_Id;
2426 Disc_Value : Node_Id;
2427 New_Indic : Node_Id;
2428 Subt_Decl : Node_Id;
2431 Discrim := First_Discriminant (Anc_Typ);
2432 while Present (Discrim) loop
2433 Disc_Value := Ancestor_Discriminant_Value (Discrim);
2434 Append_To (Anc_Constr, Disc_Value);
2435 Next_Discriminant (Discrim);
2439 Make_Subtype_Indication (Loc,
2440 Subtype_Mark => New_Occurrence_Of (Anc_Typ, Loc),
2442 Make_Index_Or_Discriminant_Constraint (Loc,
2443 Constraints => Anc_Constr));
2445 Init_Typ := Create_Itype (Ekind (Anc_Typ), N);
2448 Make_Subtype_Declaration (Loc,
2449 Defining_Identifier => Init_Typ,
2450 Subtype_Indication => New_Indic);
2452 -- Itypes must be analyzed with checks off Declaration
2453 -- must have a parent for proper handling of subsidiary
2456 Set_Parent (Subt_Decl, N);
2457 Analyze (Subt_Decl, Suppress => All_Checks);
2461 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
2462 Set_Assignment_OK (Ref);
2464 if Has_Default_Init_Comps (N)
2465 or else Has_Task (Base_Type (Init_Typ))
2468 Build_Initialization_Call (Loc,
2471 In_Init_Proc => Within_Init_Proc,
2472 With_Default_Init => True));
2475 Build_Initialization_Call (Loc,
2478 In_Init_Proc => Within_Init_Proc));
2481 if Is_Constrained (Entity (A))
2482 and then Has_Discriminants (Entity (A))
2484 Check_Ancestor_Discriminants (Entity (A));
2487 -- Ada 2005 (AI-287): If the ancestor part is an aggregate of
2488 -- limited type, a recursive call expands the ancestor. Note that
2489 -- in the limited case, the ancestor part must be either a
2490 -- function call (possibly qualified, or wrapped in an unchecked
2491 -- conversion) or aggregate (definitely qualified).
2493 elsif Is_Limited_Type (Etype (A))
2494 and then Nkind (Unqualify (A)) /= N_Function_Call -- aggregate?
2496 (Nkind (Unqualify (A)) /= N_Unchecked_Type_Conversion
2498 Nkind (Expression (Unqualify (A))) /= N_Function_Call)
2500 Ancestor_Is_Expression := True;
2502 -- Set up finalization data for enclosing record, because
2503 -- controlled subcomponents of the ancestor part will be
2506 Gen_Ctrl_Actions_For_Aggr;
2509 Build_Record_Aggr_Code (
2511 Typ => Etype (Unqualify (A)),
2515 Is_Limited_Ancestor_Expansion => True));
2517 -- If the ancestor part is an expression "E", we generate
2521 -- In Ada 2005, this includes the case of a (possibly qualified)
2522 -- limited function call. The assignment will turn into a
2523 -- build-in-place function call (for further details, see
2524 -- Make_Build_In_Place_Call_In_Assignment).
2527 Ancestor_Is_Expression := True;
2528 Init_Typ := Etype (A);
2530 -- If the ancestor part is an aggregate, force its full
2531 -- expansion, which was delayed.
2533 if Nkind (Unqualify (A)) = N_Aggregate
2534 or else Nkind (Unqualify (A)) = N_Extension_Aggregate
2536 Set_Analyzed (A, False);
2537 Set_Analyzed (Expression (A), False);
2540 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
2541 Set_Assignment_OK (Ref);
2543 -- Make the assignment without usual controlled actions since
2544 -- we only want the post adjust but not the pre finalize here
2545 -- Add manual adjust when necessary.
2547 Assign := New_List (
2548 Make_OK_Assignment_Statement (Loc,
2551 Set_No_Ctrl_Actions (First (Assign));
2553 -- Assign the tag now to make sure that the dispatching call in
2554 -- the subsequent deep_adjust works properly (unless VM_Target,
2555 -- where tags are implicit).
2557 if VM_Target = No_VM then
2559 Make_OK_Assignment_Statement (Loc,
2561 Make_Selected_Component (Loc,
2562 Prefix => New_Copy_Tree (Target),
2565 (First_Tag_Component (Base_Type (Typ)), Loc)),
2568 Unchecked_Convert_To (RTE (RE_Tag),
2571 (Access_Disp_Table (Base_Type (Typ)))),
2574 Set_Assignment_OK (Name (Instr));
2575 Append_To (Assign, Instr);
2577 -- Ada 2005 (AI-251): If tagged type has progenitors we must
2578 -- also initialize tags of the secondary dispatch tables.
2580 if Present (Abstract_Interfaces (Base_Type (Typ)))
2583 (Abstract_Interfaces (Base_Type (Typ)))
2586 (Typ => Base_Type (Typ),
2588 Stmts_List => Assign);
2592 -- Call Adjust manually
2594 if Controlled_Type (Etype (A))
2595 and then not Is_Limited_Type (Etype (A))
2597 Append_List_To (Assign,
2599 Ref => New_Copy_Tree (Ref),
2601 Flist_Ref => New_Reference_To (
2602 RTE (RE_Global_Final_List), Loc),
2603 With_Attach => Make_Integer_Literal (Loc, 0)));
2607 Make_Unsuppress_Block (Loc, Name_Discriminant_Check, Assign));
2609 if Has_Discriminants (Init_Typ) then
2610 Check_Ancestor_Discriminants (Init_Typ);
2615 -- Normal case (not an extension aggregate)
2618 -- Generate the discriminant expressions, component by component.
2619 -- If the base type is an unchecked union, the discriminants are
2620 -- unknown to the back-end and absent from a value of the type, so
2621 -- assignments for them are not emitted.
2623 if Has_Discriminants (Typ)
2624 and then not Is_Unchecked_Union (Base_Type (Typ))
2626 -- If the type is derived, and constrains discriminants of the
2627 -- parent type, these discriminants are not components of the
2628 -- aggregate, and must be initialized explicitly. They are not
2629 -- visible components of the object, but can become visible with
2630 -- a view conversion to the ancestor.
2634 Parent_Type : Entity_Id;
2636 Discr_Val : Elmt_Id;
2639 Btype := Base_Type (Typ);
2640 while Is_Derived_Type (Btype)
2641 and then Present (Stored_Constraint (Btype))
2643 Parent_Type := Etype (Btype);
2645 Disc := First_Discriminant (Parent_Type);
2647 First_Elmt (Stored_Constraint (Base_Type (Typ)));
2648 while Present (Discr_Val) loop
2650 -- Only those discriminants of the parent that are not
2651 -- renamed by discriminants of the derived type need to
2652 -- be added explicitly.
2654 if not Is_Entity_Name (Node (Discr_Val))
2656 Ekind (Entity (Node (Discr_Val))) /= E_Discriminant
2659 Make_Selected_Component (Loc,
2660 Prefix => New_Copy_Tree (Target),
2661 Selector_Name => New_Occurrence_Of (Disc, Loc));
2664 Make_OK_Assignment_Statement (Loc,
2666 Expression => New_Copy_Tree (Node (Discr_Val)));
2668 Set_No_Ctrl_Actions (Instr);
2669 Append_To (L, Instr);
2672 Next_Discriminant (Disc);
2673 Next_Elmt (Discr_Val);
2676 Btype := Base_Type (Parent_Type);
2680 -- Generate discriminant init values for the visible discriminants
2683 Discriminant : Entity_Id;
2684 Discriminant_Value : Node_Id;
2687 Discriminant := First_Stored_Discriminant (Typ);
2688 while Present (Discriminant) loop
2690 Make_Selected_Component (Loc,
2691 Prefix => New_Copy_Tree (Target),
2692 Selector_Name => New_Occurrence_Of (Discriminant, Loc));
2694 Discriminant_Value :=
2695 Get_Discriminant_Value (
2698 Discriminant_Constraint (N_Typ));
2701 Make_OK_Assignment_Statement (Loc,
2703 Expression => New_Copy_Tree (Discriminant_Value));
2705 Set_No_Ctrl_Actions (Instr);
2706 Append_To (L, Instr);
2708 Next_Stored_Discriminant (Discriminant);
2714 -- Generate the assignments, component by component
2716 -- tmp.comp1 := Expr1_From_Aggr;
2717 -- tmp.comp2 := Expr2_From_Aggr;
2720 Comp := First (Component_Associations (N));
2721 while Present (Comp) loop
2722 Selector := Entity (First (Choices (Comp)));
2724 -- Ada 2005 (AI-287): For each default-initialized component generate
2725 -- a call to the corresponding IP subprogram if available.
2727 if Box_Present (Comp)
2728 and then Has_Non_Null_Base_Init_Proc (Etype (Selector))
2730 if Ekind (Selector) /= E_Discriminant then
2731 Gen_Ctrl_Actions_For_Aggr;
2734 -- Ada 2005 (AI-287): If the component type has tasks then
2735 -- generate the activation chain and master entities (except
2736 -- in case of an allocator because in that case these entities
2737 -- are generated by Build_Task_Allocate_Block_With_Init_Stmts).
2740 Ctype : constant Entity_Id := Etype (Selector);
2741 Inside_Allocator : Boolean := False;
2742 P : Node_Id := Parent (N);
2745 if Is_Task_Type (Ctype) or else Has_Task (Ctype) then
2746 while Present (P) loop
2747 if Nkind (P) = N_Allocator then
2748 Inside_Allocator := True;
2755 if not Inside_Init_Proc and not Inside_Allocator then
2756 Build_Activation_Chain_Entity (N);
2762 Build_Initialization_Call (Loc,
2763 Id_Ref => Make_Selected_Component (Loc,
2764 Prefix => New_Copy_Tree (Target),
2765 Selector_Name => New_Occurrence_Of (Selector,
2767 Typ => Etype (Selector),
2769 With_Default_Init => True));
2774 -- Prepare for component assignment
2776 if Ekind (Selector) /= E_Discriminant
2777 or else Nkind (N) = N_Extension_Aggregate
2779 -- All the discriminants have now been assigned
2781 -- This is now a good moment to initialize and attach all the
2782 -- controllers. Their position may depend on the discriminants.
2784 if Ekind (Selector) /= E_Discriminant then
2785 Gen_Ctrl_Actions_For_Aggr;
2788 Comp_Type := Etype (Selector);
2790 Make_Selected_Component (Loc,
2791 Prefix => New_Copy_Tree (Target),
2792 Selector_Name => New_Occurrence_Of (Selector, Loc));
2794 if Nkind (Expression (Comp)) = N_Qualified_Expression then
2795 Expr_Q := Expression (Expression (Comp));
2797 Expr_Q := Expression (Comp);
2800 -- The controller is the one of the parent type defining the
2801 -- component (in case of inherited components).
2803 if Controlled_Type (Comp_Type) then
2804 Internal_Final_List :=
2805 Make_Selected_Component (Loc,
2806 Prefix => Convert_To (
2807 Scope (Original_Record_Component (Selector)),
2808 New_Copy_Tree (Target)),
2810 Make_Identifier (Loc, Name_uController));
2812 Internal_Final_List :=
2813 Make_Selected_Component (Loc,
2814 Prefix => Internal_Final_List,
2815 Selector_Name => Make_Identifier (Loc, Name_F));
2817 -- The internal final list can be part of a constant object
2819 Set_Assignment_OK (Internal_Final_List);
2822 Internal_Final_List := Empty;
2825 -- Now either create the assignment or generate the code for the
2826 -- inner aggregate top-down.
2828 if Is_Delayed_Aggregate (Expr_Q) then
2830 -- We have the following case of aggregate nesting inside
2831 -- an object declaration:
2833 -- type Arr_Typ is array (Integer range <>) of ...;
2835 -- type Rec_Typ (...) is record
2836 -- Obj_Arr_Typ : Arr_Typ (A .. B);
2839 -- Obj_Rec_Typ : Rec_Typ := (...,
2840 -- Obj_Arr_Typ => (X => (...), Y => (...)));
2842 -- The length of the ranges of the aggregate and Obj_Add_Typ
2843 -- are equal (B - A = Y - X), but they do not coincide (X /=
2844 -- A and B /= Y). This case requires array sliding which is
2845 -- performed in the following manner:
2847 -- subtype Arr_Sub is Arr_Typ (X .. Y);
2849 -- Temp (X) := (...);
2851 -- Temp (Y) := (...);
2852 -- Obj_Rec_Typ.Obj_Arr_Typ := Temp;
2854 if Ekind (Comp_Type) = E_Array_Subtype
2855 and then Is_Int_Range_Bounds (Aggregate_Bounds (Expr_Q))
2856 and then Is_Int_Range_Bounds (First_Index (Comp_Type))
2858 Compatible_Int_Bounds
2859 (Agg_Bounds => Aggregate_Bounds (Expr_Q),
2860 Typ_Bounds => First_Index (Comp_Type))
2862 -- Create the array subtype with bounds equal to those of
2863 -- the corresponding aggregate.
2866 SubE : constant Entity_Id :=
2867 Make_Defining_Identifier (Loc,
2868 New_Internal_Name ('T'));
2870 SubD : constant Node_Id :=
2871 Make_Subtype_Declaration (Loc,
2872 Defining_Identifier =>
2874 Subtype_Indication =>
2875 Make_Subtype_Indication (Loc,
2876 Subtype_Mark => New_Reference_To (
2877 Etype (Comp_Type), Loc),
2879 Make_Index_Or_Discriminant_Constraint (
2880 Loc, Constraints => New_List (
2881 New_Copy_Tree (Aggregate_Bounds (
2884 -- Create a temporary array of the above subtype which
2885 -- will be used to capture the aggregate assignments.
2887 TmpE : constant Entity_Id :=
2888 Make_Defining_Identifier (Loc,
2889 New_Internal_Name ('A'));
2891 TmpD : constant Node_Id :=
2892 Make_Object_Declaration (Loc,
2893 Defining_Identifier =>
2895 Object_Definition =>
2896 New_Reference_To (SubE, Loc));
2899 Set_No_Initialization (TmpD);
2900 Append_To (L, SubD);
2901 Append_To (L, TmpD);
2903 -- Expand aggregate into assignments to the temp array
2906 Late_Expansion (Expr_Q, Comp_Type,
2907 New_Reference_To (TmpE, Loc), Internal_Final_List));
2912 Make_Assignment_Statement (Loc,
2913 Name => New_Copy_Tree (Comp_Expr),
2914 Expression => New_Reference_To (TmpE, Loc)));
2916 -- Do not pass the original aggregate to Gigi as is,
2917 -- since it will potentially clobber the front or the end
2918 -- of the array. Setting the expression to empty is safe
2919 -- since all aggregates are expanded into assignments.
2921 if Present (Obj) then
2922 Set_Expression (Parent (Obj), Empty);
2926 -- Normal case (sliding not required)
2930 Late_Expansion (Expr_Q, Comp_Type, Comp_Expr,
2931 Internal_Final_List));
2934 -- Expr_Q is not delayed aggregate
2938 Make_OK_Assignment_Statement (Loc,
2940 Expression => Expression (Comp));
2942 Set_No_Ctrl_Actions (Instr);
2943 Append_To (L, Instr);
2945 -- Adjust the tag if tagged (because of possible view
2946 -- conversions), unless compiling for a VM where tags are
2949 -- tmp.comp._tag := comp_typ'tag;
2951 if Is_Tagged_Type (Comp_Type) and then VM_Target = No_VM then
2953 Make_OK_Assignment_Statement (Loc,
2955 Make_Selected_Component (Loc,
2956 Prefix => New_Copy_Tree (Comp_Expr),
2959 (First_Tag_Component (Comp_Type), Loc)),
2962 Unchecked_Convert_To (RTE (RE_Tag),
2964 (Node (First_Elmt (Access_Disp_Table (Comp_Type))),
2967 Append_To (L, Instr);
2970 -- Adjust and Attach the component to the proper controller
2972 -- Adjust (tmp.comp);
2973 -- Attach_To_Final_List (tmp.comp,
2974 -- comp_typ (tmp)._record_controller.f)
2976 if Controlled_Type (Comp_Type)
2977 and then not Is_Limited_Type (Comp_Type)
2981 Ref => New_Copy_Tree (Comp_Expr),
2983 Flist_Ref => Internal_Final_List,
2984 With_Attach => Make_Integer_Literal (Loc, 1)));
2990 elsif Ekind (Selector) = E_Discriminant
2991 and then Nkind (N) /= N_Extension_Aggregate
2992 and then Nkind (Parent (N)) = N_Component_Association
2993 and then Is_Constrained (Typ)
2995 -- We must check that the discriminant value imposed by the
2996 -- context is the same as the value given in the subaggregate,
2997 -- because after the expansion into assignments there is no
2998 -- record on which to perform a regular discriminant check.
3005 D_Val := First_Elmt (Discriminant_Constraint (Typ));
3006 Disc := First_Discriminant (Typ);
3007 while Chars (Disc) /= Chars (Selector) loop
3008 Next_Discriminant (Disc);
3012 pragma Assert (Present (D_Val));
3014 -- This check cannot performed for components that are
3015 -- constrained by a current instance, because this is not a
3016 -- value that can be compared with the actual constraint.
3018 if Nkind (Node (D_Val)) /= N_Attribute_Reference
3019 or else not Is_Entity_Name (Prefix (Node (D_Val)))
3020 or else not Is_Type (Entity (Prefix (Node (D_Val))))
3023 Make_Raise_Constraint_Error (Loc,
3026 Left_Opnd => New_Copy_Tree (Node (D_Val)),
3027 Right_Opnd => Expression (Comp)),
3028 Reason => CE_Discriminant_Check_Failed));
3031 -- Find self-reference in previous discriminant assignment,
3032 -- and replace with proper expression.
3039 while Present (Ass) loop
3040 if Nkind (Ass) = N_Assignment_Statement
3041 and then Nkind (Name (Ass)) = N_Selected_Component
3042 and then Chars (Selector_Name (Name (Ass))) =
3046 (Ass, New_Copy_Tree (Expression (Comp)));
3061 -- If the type is tagged, the tag needs to be initialized (unless
3062 -- compiling for the Java VM where tags are implicit). It is done
3063 -- late in the initialization process because in some cases, we call
3064 -- the init proc of an ancestor which will not leave out the right tag
3066 if Ancestor_Is_Expression then
3069 elsif Is_Tagged_Type (Typ) and then VM_Target = No_VM then
3071 Make_OK_Assignment_Statement (Loc,
3073 Make_Selected_Component (Loc,
3074 Prefix => New_Copy_Tree (Target),
3077 (First_Tag_Component (Base_Type (Typ)), Loc)),
3080 Unchecked_Convert_To (RTE (RE_Tag),
3082 (Node (First_Elmt (Access_Disp_Table (Base_Type (Typ)))),
3085 Append_To (L, Instr);
3087 -- Ada 2005 (AI-251): If the tagged type has been derived from
3088 -- abstract interfaces we must also initialize the tags of the
3089 -- secondary dispatch tables.
3091 if Present (Abstract_Interfaces (Base_Type (Typ)))
3093 Is_Empty_Elmt_List (Abstract_Interfaces (Base_Type (Typ)))
3096 (Typ => Base_Type (Typ),
3102 -- If the controllers have not been initialized yet (by lack of non-
3103 -- discriminant components), let's do it now.
3105 Gen_Ctrl_Actions_For_Aggr;
3108 end Build_Record_Aggr_Code;
3110 -------------------------------
3111 -- Convert_Aggr_In_Allocator --
3112 -------------------------------
3114 procedure Convert_Aggr_In_Allocator
3119 Loc : constant Source_Ptr := Sloc (Aggr);
3120 Typ : constant Entity_Id := Etype (Aggr);
3121 Temp : constant Entity_Id := Defining_Identifier (Decl);
3123 Occ : constant Node_Id :=
3124 Unchecked_Convert_To (Typ,
3125 Make_Explicit_Dereference (Loc,
3126 New_Reference_To (Temp, Loc)));
3128 Access_Type : constant Entity_Id := Etype (Temp);
3132 -- If the allocator is for an access discriminant, there is no
3133 -- finalization list for the anonymous access type, and the eventual
3134 -- finalization of the object is handled through the coextension
3135 -- mechanism. If the enclosing object is not dynamically allocated,
3136 -- the access discriminant is itself placed on the stack. Otherwise,
3137 -- some other finalization list is used (see exp_ch4.adb).
3139 -- Decl has been inserted in the code ahead of the allocator, using
3140 -- Insert_Actions. We use Insert_Actions below as well, to ensure that
3141 -- subsequent insertions are done in the proper order. Using (for
3142 -- example) Insert_Actions_After to place the expanded aggregate
3143 -- immediately after Decl may lead to out-of-order references if the
3144 -- allocator has generated a finalization list, as when the designated
3145 -- object is controlled and there is an open transient scope.
3147 if Ekind (Access_Type) = E_Anonymous_Access_Type
3148 and then Nkind (Associated_Node_For_Itype (Access_Type)) =
3149 N_Discriminant_Specification
3153 Flist := Find_Final_List (Access_Type);
3156 if Is_Array_Type (Typ) then
3157 Convert_Array_Aggr_In_Allocator (Decl, Aggr, Occ);
3159 elsif Has_Default_Init_Comps (Aggr) then
3161 L : constant List_Id := New_List;
3162 Init_Stmts : List_Id;
3169 Associated_Final_Chain (Base_Type (Access_Type)));
3171 -- ??? Dubious actual for Obj: expect 'the original object being
3174 if Has_Task (Typ) then
3175 Build_Task_Allocate_Block_With_Init_Stmts (L, Aggr, Init_Stmts);
3176 Insert_Actions (Alloc, L);
3178 Insert_Actions (Alloc, Init_Stmts);
3183 Insert_Actions (Alloc,
3185 (Aggr, Typ, Occ, Flist,
3186 Associated_Final_Chain (Base_Type (Access_Type))));
3188 -- ??? Dubious actual for Obj: expect 'the original object being
3192 end Convert_Aggr_In_Allocator;
3194 --------------------------------
3195 -- Convert_Aggr_In_Assignment --
3196 --------------------------------
3198 procedure Convert_Aggr_In_Assignment (N : Node_Id) is
3199 Aggr : Node_Id := Expression (N);
3200 Typ : constant Entity_Id := Etype (Aggr);
3201 Occ : constant Node_Id := New_Copy_Tree (Name (N));
3204 if Nkind (Aggr) = N_Qualified_Expression then
3205 Aggr := Expression (Aggr);
3208 Insert_Actions_After (N,
3211 Find_Final_List (Typ, New_Copy_Tree (Occ))));
3212 end Convert_Aggr_In_Assignment;
3214 ---------------------------------
3215 -- Convert_Aggr_In_Object_Decl --
3216 ---------------------------------
3218 procedure Convert_Aggr_In_Object_Decl (N : Node_Id) is
3219 Obj : constant Entity_Id := Defining_Identifier (N);
3220 Aggr : Node_Id := Expression (N);
3221 Loc : constant Source_Ptr := Sloc (Aggr);
3222 Typ : constant Entity_Id := Etype (Aggr);
3223 Occ : constant Node_Id := New_Occurrence_Of (Obj, Loc);
3225 function Discriminants_Ok return Boolean;
3226 -- If the object type is constrained, the discriminants in the
3227 -- aggregate must be checked against the discriminants of the subtype.
3228 -- This cannot be done using Apply_Discriminant_Checks because after
3229 -- expansion there is no aggregate left to check.
3231 ----------------------
3232 -- Discriminants_Ok --
3233 ----------------------
3235 function Discriminants_Ok return Boolean is
3236 Cond : Node_Id := Empty;
3245 D := First_Discriminant (Typ);
3246 Disc1 := First_Elmt (Discriminant_Constraint (Typ));
3247 Disc2 := First_Elmt (Discriminant_Constraint (Etype (Obj)));
3248 while Present (Disc1) and then Present (Disc2) loop
3249 Val1 := Node (Disc1);
3250 Val2 := Node (Disc2);
3252 if not Is_OK_Static_Expression (Val1)
3253 or else not Is_OK_Static_Expression (Val2)
3255 Check := Make_Op_Ne (Loc,
3256 Left_Opnd => Duplicate_Subexpr (Val1),
3257 Right_Opnd => Duplicate_Subexpr (Val2));
3263 Cond := Make_Or_Else (Loc,
3265 Right_Opnd => Check);
3268 elsif Expr_Value (Val1) /= Expr_Value (Val2) then
3269 Apply_Compile_Time_Constraint_Error (Aggr,
3270 Msg => "incorrect value for discriminant&?",
3271 Reason => CE_Discriminant_Check_Failed,
3276 Next_Discriminant (D);
3281 -- If any discriminant constraint is non-static, emit a check
3283 if Present (Cond) then
3285 Make_Raise_Constraint_Error (Loc,
3287 Reason => CE_Discriminant_Check_Failed));
3291 end Discriminants_Ok;
3293 -- Start of processing for Convert_Aggr_In_Object_Decl
3296 Set_Assignment_OK (Occ);
3298 if Nkind (Aggr) = N_Qualified_Expression then
3299 Aggr := Expression (Aggr);
3302 if Has_Discriminants (Typ)
3303 and then Typ /= Etype (Obj)
3304 and then Is_Constrained (Etype (Obj))
3305 and then not Discriminants_Ok
3310 -- If the context is an extended return statement, it has its own
3311 -- finalization machinery (i.e. works like a transient scope) and
3312 -- we do not want to create an additional one, because objects on
3313 -- the finalization list of the return must be moved to the caller's
3314 -- finalization list to complete the return.
3316 -- However, if the aggregate is limited, it is built in place, and the
3317 -- controlled components are not assigned to intermediate temporaries
3318 -- so there is no need for a transient scope in this case either.
3320 if Requires_Transient_Scope (Typ)
3321 and then Ekind (Current_Scope) /= E_Return_Statement
3322 and then not Is_Limited_Type (Typ)
3324 Establish_Transient_Scope (Aggr, Sec_Stack =>
3325 Is_Controlled (Typ) or else Has_Controlled_Component (Typ));
3328 Insert_Actions_After (N, Late_Expansion (Aggr, Typ, Occ, Obj => Obj));
3329 Set_No_Initialization (N);
3330 Initialize_Discriminants (N, Typ);
3331 end Convert_Aggr_In_Object_Decl;
3333 -------------------------------------
3334 -- Convert_Array_Aggr_In_Allocator --
3335 -------------------------------------
3337 procedure Convert_Array_Aggr_In_Allocator
3342 Aggr_Code : List_Id;
3343 Typ : constant Entity_Id := Etype (Aggr);
3344 Ctyp : constant Entity_Id := Component_Type (Typ);
3347 -- The target is an explicit dereference of the allocated object.
3348 -- Generate component assignments to it, as for an aggregate that
3349 -- appears on the right-hand side of an assignment statement.
3352 Build_Array_Aggr_Code (Aggr,
3354 Index => First_Index (Typ),
3356 Scalar_Comp => Is_Scalar_Type (Ctyp));
3358 Insert_Actions_After (Decl, Aggr_Code);
3359 end Convert_Array_Aggr_In_Allocator;
3361 ----------------------------
3362 -- Convert_To_Assignments --
3363 ----------------------------
3365 procedure Convert_To_Assignments (N : Node_Id; Typ : Entity_Id) is
3366 Loc : constant Source_Ptr := Sloc (N);
3370 Target_Expr : Node_Id;
3371 Parent_Kind : Node_Kind;
3372 Unc_Decl : Boolean := False;
3373 Parent_Node : Node_Id;
3376 pragma Assert (not Is_Static_Dispatch_Table_Aggregate (N));
3377 pragma Assert (Is_Record_Type (Typ));
3379 Parent_Node := Parent (N);
3380 Parent_Kind := Nkind (Parent_Node);
3382 if Parent_Kind = N_Qualified_Expression then
3384 -- Check if we are in a unconstrained declaration because in this
3385 -- case the current delayed expansion mechanism doesn't work when
3386 -- the declared object size depend on the initializing expr.
3389 Parent_Node := Parent (Parent_Node);
3390 Parent_Kind := Nkind (Parent_Node);
3392 if Parent_Kind = N_Object_Declaration then
3394 not Is_Entity_Name (Object_Definition (Parent_Node))
3395 or else Has_Discriminants
3396 (Entity (Object_Definition (Parent_Node)))
3397 or else Is_Class_Wide_Type
3398 (Entity (Object_Definition (Parent_Node)));
3403 -- Just set the Delay flag in the cases where the transformation will be
3404 -- done top down from above.
3408 -- Internal aggregate (transformed when expanding the parent)
3410 or else Parent_Kind = N_Aggregate
3411 or else Parent_Kind = N_Extension_Aggregate
3412 or else Parent_Kind = N_Component_Association
3414 -- Allocator (see Convert_Aggr_In_Allocator)
3416 or else Parent_Kind = N_Allocator
3418 -- Object declaration (see Convert_Aggr_In_Object_Decl)
3420 or else (Parent_Kind = N_Object_Declaration and then not Unc_Decl)
3422 -- Safe assignment (see Convert_Aggr_Assignments). So far only the
3423 -- assignments in init procs are taken into account.
3425 or else (Parent_Kind = N_Assignment_Statement
3426 and then Inside_Init_Proc)
3428 -- (Ada 2005) An inherently limited type in a return statement,
3429 -- which will be handled in a build-in-place fashion, and may be
3430 -- rewritten as an extended return and have its own finalization
3431 -- machinery. In the case of a simple return, the aggregate needs
3432 -- to be delayed until the scope for the return statement has been
3433 -- created, so that any finalization chain will be associated with
3434 -- that scope. For extended returns, we delay expansion to avoid the
3435 -- creation of an unwanted transient scope that could result in
3436 -- premature finalization of the return object (which is built in
3437 -- in place within the caller's scope).
3440 (Is_Inherently_Limited_Type (Typ)
3442 (Nkind (Parent (Parent_Node)) = N_Extended_Return_Statement
3443 or else Nkind (Parent_Node) = N_Simple_Return_Statement))
3445 Set_Expansion_Delayed (N);
3449 if Requires_Transient_Scope (Typ) then
3450 Establish_Transient_Scope
3452 Is_Controlled (Typ) or else Has_Controlled_Component (Typ));
3455 -- If the aggregate is non-limited, create a temporary. If it is
3456 -- limited and the context is an assignment, this is a subaggregate
3457 -- for an enclosing aggregate being expanded. It must be built in place,
3458 -- so use the target of the current assignment.
3460 if Is_Limited_Type (Typ)
3461 and then Nkind (Parent (N)) = N_Assignment_Statement
3463 Target_Expr := New_Copy_Tree (Name (Parent (N)));
3465 (Parent (N), Build_Record_Aggr_Code (N, Typ, Target_Expr));
3466 Rewrite (Parent (N), Make_Null_Statement (Loc));
3469 Temp := Make_Defining_Identifier (Loc, New_Internal_Name ('A'));
3472 Make_Object_Declaration (Loc,
3473 Defining_Identifier => Temp,
3474 Object_Definition => New_Occurrence_Of (Typ, Loc));
3476 Set_No_Initialization (Instr);
3477 Insert_Action (N, Instr);
3478 Initialize_Discriminants (Instr, Typ);
3479 Target_Expr := New_Occurrence_Of (Temp, Loc);
3480 Insert_Actions (N, Build_Record_Aggr_Code (N, Typ, Target_Expr));
3481 Rewrite (N, New_Occurrence_Of (Temp, Loc));
3482 Analyze_And_Resolve (N, Typ);
3484 end Convert_To_Assignments;
3486 ---------------------------
3487 -- Convert_To_Positional --
3488 ---------------------------
3490 procedure Convert_To_Positional
3492 Max_Others_Replicate : Nat := 5;
3493 Handle_Bit_Packed : Boolean := False)
3495 Typ : constant Entity_Id := Etype (N);
3497 Static_Components : Boolean := True;
3499 procedure Check_Static_Components;
3500 -- Check whether all components of the aggregate are compile-time known
3501 -- values, and can be passed as is to the back-end without further
3507 Ixb : Node_Id) return Boolean;
3508 -- Convert the aggregate into a purely positional form if possible. On
3509 -- entry the bounds of all dimensions are known to be static, and the
3510 -- total number of components is safe enough to expand.
3512 function Is_Flat (N : Node_Id; Dims : Int) return Boolean;
3513 -- Return True iff the array N is flat (which is not rivial in the case
3514 -- of multidimensionsl aggregates).
3516 -----------------------------
3517 -- Check_Static_Components --
3518 -----------------------------
3520 procedure Check_Static_Components is
3524 Static_Components := True;
3526 if Nkind (N) = N_String_Literal then
3529 elsif Present (Expressions (N)) then
3530 Expr := First (Expressions (N));
3531 while Present (Expr) loop
3532 if Nkind (Expr) /= N_Aggregate
3533 or else not Compile_Time_Known_Aggregate (Expr)
3534 or else Expansion_Delayed (Expr)
3536 Static_Components := False;
3544 if Nkind (N) = N_Aggregate
3545 and then Present (Component_Associations (N))
3547 Expr := First (Component_Associations (N));
3548 while Present (Expr) loop
3549 if Nkind (Expression (Expr)) = N_Integer_Literal then
3552 elsif Nkind (Expression (Expr)) /= N_Aggregate
3554 not Compile_Time_Known_Aggregate (Expression (Expr))
3555 or else Expansion_Delayed (Expression (Expr))
3557 Static_Components := False;
3564 end Check_Static_Components;
3573 Ixb : Node_Id) return Boolean
3575 Loc : constant Source_Ptr := Sloc (N);
3576 Blo : constant Node_Id := Type_Low_Bound (Etype (Ixb));
3577 Lo : constant Node_Id := Type_Low_Bound (Etype (Ix));
3578 Hi : constant Node_Id := Type_High_Bound (Etype (Ix));
3583 if Nkind (Original_Node (N)) = N_String_Literal then
3587 if not Compile_Time_Known_Value (Lo)
3588 or else not Compile_Time_Known_Value (Hi)
3593 Lov := Expr_Value (Lo);
3594 Hiv := Expr_Value (Hi);
3597 or else not Compile_Time_Known_Value (Blo)
3602 -- Determine if set of alternatives is suitable for conversion and
3603 -- build an array containing the values in sequence.
3606 Vals : array (UI_To_Int (Lov) .. UI_To_Int (Hiv))
3607 of Node_Id := (others => Empty);
3608 -- The values in the aggregate sorted appropriately
3611 -- Same data as Vals in list form
3614 -- Used to validate Max_Others_Replicate limit
3617 Num : Int := UI_To_Int (Lov);
3622 if Present (Expressions (N)) then
3623 Elmt := First (Expressions (N));
3624 while Present (Elmt) loop
3625 if Nkind (Elmt) = N_Aggregate
3626 and then Present (Next_Index (Ix))
3628 not Flatten (Elmt, Next_Index (Ix), Next_Index (Ixb))
3633 Vals (Num) := Relocate_Node (Elmt);
3640 if No (Component_Associations (N)) then
3644 Elmt := First (Component_Associations (N));
3646 if Nkind (Expression (Elmt)) = N_Aggregate then
3647 if Present (Next_Index (Ix))
3650 (Expression (Elmt), Next_Index (Ix), Next_Index (Ixb))
3656 Component_Loop : while Present (Elmt) loop
3657 Choice := First (Choices (Elmt));
3658 Choice_Loop : while Present (Choice) loop
3660 -- If we have an others choice, fill in the missing elements
3661 -- subject to the limit established by Max_Others_Replicate.
3663 if Nkind (Choice) = N_Others_Choice then
3666 for J in Vals'Range loop
3667 if No (Vals (J)) then
3668 Vals (J) := New_Copy_Tree (Expression (Elmt));
3669 Rep_Count := Rep_Count + 1;
3671 -- Check for maximum others replication. Note that
3672 -- we skip this test if either of the restrictions
3673 -- No_Elaboration_Code or No_Implicit_Loops is
3674 -- active, or if this is a preelaborable unit.
3677 P : constant Entity_Id :=
3678 Cunit_Entity (Current_Sem_Unit);
3681 if Restriction_Active (No_Elaboration_Code)
3682 or else Restriction_Active (No_Implicit_Loops)
3683 or else Is_Preelaborated (P)
3684 or else (Ekind (P) = E_Package_Body
3686 Is_Preelaborated (Spec_Entity (P)))
3690 elsif Rep_Count > Max_Others_Replicate then
3697 exit Component_Loop;
3699 -- Case of a subtype mark
3701 elsif Nkind (Choice) = N_Identifier
3702 and then Is_Type (Entity (Choice))
3704 Lo := Type_Low_Bound (Etype (Choice));
3705 Hi := Type_High_Bound (Etype (Choice));
3707 -- Case of subtype indication
3709 elsif Nkind (Choice) = N_Subtype_Indication then
3710 Lo := Low_Bound (Range_Expression (Constraint (Choice)));
3711 Hi := High_Bound (Range_Expression (Constraint (Choice)));
3715 elsif Nkind (Choice) = N_Range then
3716 Lo := Low_Bound (Choice);
3717 Hi := High_Bound (Choice);
3719 -- Normal subexpression case
3721 else pragma Assert (Nkind (Choice) in N_Subexpr);
3722 if not Compile_Time_Known_Value (Choice) then
3726 Vals (UI_To_Int (Expr_Value (Choice))) :=
3727 New_Copy_Tree (Expression (Elmt));
3732 -- Range cases merge with Lo,Hi said
3734 if not Compile_Time_Known_Value (Lo)
3736 not Compile_Time_Known_Value (Hi)
3740 for J in UI_To_Int (Expr_Value (Lo)) ..
3741 UI_To_Int (Expr_Value (Hi))
3743 Vals (J) := New_Copy_Tree (Expression (Elmt));
3749 end loop Choice_Loop;
3752 end loop Component_Loop;
3754 -- If we get here the conversion is possible
3757 for J in Vals'Range loop
3758 Append (Vals (J), Vlist);
3761 Rewrite (N, Make_Aggregate (Loc, Expressions => Vlist));
3762 Set_Aggregate_Bounds (N, Aggregate_Bounds (Original_Node (N)));
3771 function Is_Flat (N : Node_Id; Dims : Int) return Boolean is
3778 elsif Nkind (N) = N_Aggregate then
3779 if Present (Component_Associations (N)) then
3783 Elmt := First (Expressions (N));
3784 while Present (Elmt) loop
3785 if not Is_Flat (Elmt, Dims - 1) then
3799 -- Start of processing for Convert_To_Positional
3802 -- Ada 2005 (AI-287): Do not convert in case of default initialized
3803 -- components because in this case will need to call the corresponding
3806 if Has_Default_Init_Comps (N) then
3810 if Is_Flat (N, Number_Dimensions (Typ)) then
3814 if Is_Bit_Packed_Array (Typ)
3815 and then not Handle_Bit_Packed
3820 -- Do not convert to positional if controlled components are involved
3821 -- since these require special processing
3823 if Has_Controlled_Component (Typ) then
3827 Check_Static_Components;
3829 -- If the size is known, or all the components are static, try to
3830 -- build a fully positional aggregate.
3832 -- The size of the type may not be known for an aggregate with
3833 -- discriminated array components, but if the components are static
3834 -- it is still possible to verify statically that the length is
3835 -- compatible with the upper bound of the type, and therefore it is
3836 -- worth flattening such aggregates as well.
3838 -- For now the back-end expands these aggregates into individual
3839 -- assignments to the target anyway, but it is conceivable that
3840 -- it will eventually be able to treat such aggregates statically???
3842 if Aggr_Size_OK (Typ)
3843 and then Flatten (N, First_Index (Typ), First_Index (Base_Type (Typ)))
3845 if Static_Components then
3846 Set_Compile_Time_Known_Aggregate (N);
3847 Set_Expansion_Delayed (N, False);
3850 Analyze_And_Resolve (N, Typ);
3852 end Convert_To_Positional;
3854 ----------------------------
3855 -- Expand_Array_Aggregate --
3856 ----------------------------
3858 -- Array aggregate expansion proceeds as follows:
3860 -- 1. If requested we generate code to perform all the array aggregate
3861 -- bound checks, specifically
3863 -- (a) Check that the index range defined by aggregate bounds is
3864 -- compatible with corresponding index subtype.
3866 -- (b) If an others choice is present check that no aggregate
3867 -- index is outside the bounds of the index constraint.
3869 -- (c) For multidimensional arrays make sure that all subaggregates
3870 -- corresponding to the same dimension have the same bounds.
3872 -- 2. Check for packed array aggregate which can be converted to a
3873 -- constant so that the aggregate disappeares completely.
3875 -- 3. Check case of nested aggregate. Generally nested aggregates are
3876 -- handled during the processing of the parent aggregate.
3878 -- 4. Check if the aggregate can be statically processed. If this is the
3879 -- case pass it as is to Gigi. Note that a necessary condition for
3880 -- static processing is that the aggregate be fully positional.
3882 -- 5. If in place aggregate expansion is possible (i.e. no need to create
3883 -- a temporary) then mark the aggregate as such and return. Otherwise
3884 -- create a new temporary and generate the appropriate initialization
3887 procedure Expand_Array_Aggregate (N : Node_Id) is
3888 Loc : constant Source_Ptr := Sloc (N);
3890 Typ : constant Entity_Id := Etype (N);
3891 Ctyp : constant Entity_Id := Component_Type (Typ);
3892 -- Typ is the correct constrained array subtype of the aggregate
3893 -- Ctyp is the corresponding component type.
3895 Aggr_Dimension : constant Pos := Number_Dimensions (Typ);
3896 -- Number of aggregate index dimensions
3898 Aggr_Low : array (1 .. Aggr_Dimension) of Node_Id;
3899 Aggr_High : array (1 .. Aggr_Dimension) of Node_Id;
3900 -- Low and High bounds of the constraint for each aggregate index
3902 Aggr_Index_Typ : array (1 .. Aggr_Dimension) of Entity_Id;
3903 -- The type of each index
3905 Maybe_In_Place_OK : Boolean;
3906 -- If the type is neither controlled nor packed and the aggregate
3907 -- is the expression in an assignment, assignment in place may be
3908 -- possible, provided other conditions are met on the LHS.
3910 Others_Present : array (1 .. Aggr_Dimension) of Boolean :=
3912 -- If Others_Present (J) is True, then there is an others choice
3913 -- in one of the sub-aggregates of N at dimension J.
3915 procedure Build_Constrained_Type (Positional : Boolean);
3916 -- If the subtype is not static or unconstrained, build a constrained
3917 -- type using the computable sizes of the aggregate and its sub-
3920 procedure Check_Bounds (Aggr_Bounds : Node_Id; Index_Bounds : Node_Id);
3921 -- Checks that the bounds of Aggr_Bounds are within the bounds defined
3924 procedure Check_Same_Aggr_Bounds (Sub_Aggr : Node_Id; Dim : Pos);
3925 -- Checks that in a multi-dimensional array aggregate all subaggregates
3926 -- corresponding to the same dimension have the same bounds.
3927 -- Sub_Aggr is an array sub-aggregate. Dim is the dimension
3928 -- corresponding to the sub-aggregate.
3930 procedure Compute_Others_Present (Sub_Aggr : Node_Id; Dim : Pos);
3931 -- Computes the values of array Others_Present. Sub_Aggr is the
3932 -- array sub-aggregate we start the computation from. Dim is the
3933 -- dimension corresponding to the sub-aggregate.
3935 function Has_Address_Clause (D : Node_Id) return Boolean;
3936 -- If the aggregate is the expression in an object declaration, it
3937 -- cannot be expanded in place. This function does a lookahead in the
3938 -- current declarative part to find an address clause for the object
3941 function In_Place_Assign_OK return Boolean;
3942 -- Simple predicate to determine whether an aggregate assignment can
3943 -- be done in place, because none of the new values can depend on the
3944 -- components of the target of the assignment.
3946 procedure Others_Check (Sub_Aggr : Node_Id; Dim : Pos);
3947 -- Checks that if an others choice is present in any sub-aggregate no
3948 -- aggregate index is outside the bounds of the index constraint.
3949 -- Sub_Aggr is an array sub-aggregate. Dim is the dimension
3950 -- corresponding to the sub-aggregate.
3952 ----------------------------
3953 -- Build_Constrained_Type --
3954 ----------------------------
3956 procedure Build_Constrained_Type (Positional : Boolean) is
3957 Loc : constant Source_Ptr := Sloc (N);
3958 Agg_Type : Entity_Id;
3961 Typ : constant Entity_Id := Etype (N);
3962 Indices : constant List_Id := New_List;
3968 Make_Defining_Identifier (
3969 Loc, New_Internal_Name ('A'));
3971 -- If the aggregate is purely positional, all its subaggregates
3972 -- have the same size. We collect the dimensions from the first
3973 -- subaggregate at each level.
3978 for D in 1 .. Number_Dimensions (Typ) loop
3979 Sub_Agg := First (Expressions (Sub_Agg));
3983 while Present (Comp) loop
3990 Low_Bound => Make_Integer_Literal (Loc, 1),
3992 Make_Integer_Literal (Loc, Num)),
3997 -- We know the aggregate type is unconstrained and the aggregate
3998 -- is not processable by the back end, therefore not necessarily
3999 -- positional. Retrieve each dimension bounds (computed earlier).
4002 for D in 1 .. Number_Dimensions (Typ) loop
4005 Low_Bound => Aggr_Low (D),
4006 High_Bound => Aggr_High (D)),
4012 Make_Full_Type_Declaration (Loc,
4013 Defining_Identifier => Agg_Type,
4015 Make_Constrained_Array_Definition (Loc,
4016 Discrete_Subtype_Definitions => Indices,
4017 Component_Definition =>
4018 Make_Component_Definition (Loc,
4019 Aliased_Present => False,
4020 Subtype_Indication =>
4021 New_Occurrence_Of (Component_Type (Typ), Loc))));
4023 Insert_Action (N, Decl);
4025 Set_Etype (N, Agg_Type);
4026 Set_Is_Itype (Agg_Type);
4027 Freeze_Itype (Agg_Type, N);
4028 end Build_Constrained_Type;
4034 procedure Check_Bounds (Aggr_Bounds : Node_Id; Index_Bounds : Node_Id) is
4041 Cond : Node_Id := Empty;
4044 Get_Index_Bounds (Aggr_Bounds, Aggr_Lo, Aggr_Hi);
4045 Get_Index_Bounds (Index_Bounds, Ind_Lo, Ind_Hi);
4047 -- Generate the following test:
4049 -- [constraint_error when
4050 -- Aggr_Lo <= Aggr_Hi and then
4051 -- (Aggr_Lo < Ind_Lo or else Aggr_Hi > Ind_Hi)]
4053 -- As an optimization try to see if some tests are trivially vacuos
4054 -- because we are comparing an expression against itself.
4056 if Aggr_Lo = Ind_Lo and then Aggr_Hi = Ind_Hi then
4059 elsif Aggr_Hi = Ind_Hi then
4062 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
4063 Right_Opnd => Duplicate_Subexpr_Move_Checks (Ind_Lo));
4065 elsif Aggr_Lo = Ind_Lo then
4068 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi),
4069 Right_Opnd => Duplicate_Subexpr_Move_Checks (Ind_Hi));
4076 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
4077 Right_Opnd => Duplicate_Subexpr_Move_Checks (Ind_Lo)),
4081 Left_Opnd => Duplicate_Subexpr (Aggr_Hi),
4082 Right_Opnd => Duplicate_Subexpr (Ind_Hi)));
4085 if Present (Cond) then
4090 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
4091 Right_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi)),
4093 Right_Opnd => Cond);
4095 Set_Analyzed (Left_Opnd (Left_Opnd (Cond)), False);
4096 Set_Analyzed (Right_Opnd (Left_Opnd (Cond)), False);
4098 Make_Raise_Constraint_Error (Loc,
4100 Reason => CE_Length_Check_Failed));
4104 ----------------------------
4105 -- Check_Same_Aggr_Bounds --
4106 ----------------------------
4108 procedure Check_Same_Aggr_Bounds (Sub_Aggr : Node_Id; Dim : Pos) is
4109 Sub_Lo : constant Node_Id := Low_Bound (Aggregate_Bounds (Sub_Aggr));
4110 Sub_Hi : constant Node_Id := High_Bound (Aggregate_Bounds (Sub_Aggr));
4111 -- The bounds of this specific sub-aggregate
4113 Aggr_Lo : constant Node_Id := Aggr_Low (Dim);
4114 Aggr_Hi : constant Node_Id := Aggr_High (Dim);
4115 -- The bounds of the aggregate for this dimension
4117 Ind_Typ : constant Entity_Id := Aggr_Index_Typ (Dim);
4118 -- The index type for this dimension.xxx
4120 Cond : Node_Id := Empty;
4125 -- If index checks are on generate the test
4127 -- [constraint_error when
4128 -- Aggr_Lo /= Sub_Lo or else Aggr_Hi /= Sub_Hi]
4130 -- As an optimization try to see if some tests are trivially vacuos
4131 -- because we are comparing an expression against itself. Also for
4132 -- the first dimension the test is trivially vacuous because there
4133 -- is just one aggregate for dimension 1.
4135 if Index_Checks_Suppressed (Ind_Typ) then
4139 or else (Aggr_Lo = Sub_Lo and then Aggr_Hi = Sub_Hi)
4143 elsif Aggr_Hi = Sub_Hi then
4146 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
4147 Right_Opnd => Duplicate_Subexpr_Move_Checks (Sub_Lo));
4149 elsif Aggr_Lo = Sub_Lo then
4152 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi),
4153 Right_Opnd => Duplicate_Subexpr_Move_Checks (Sub_Hi));
4160 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
4161 Right_Opnd => Duplicate_Subexpr_Move_Checks (Sub_Lo)),
4165 Left_Opnd => Duplicate_Subexpr (Aggr_Hi),
4166 Right_Opnd => Duplicate_Subexpr (Sub_Hi)));
4169 if Present (Cond) then
4171 Make_Raise_Constraint_Error (Loc,
4173 Reason => CE_Length_Check_Failed));
4176 -- Now look inside the sub-aggregate to see if there is more work
4178 if Dim < Aggr_Dimension then
4180 -- Process positional components
4182 if Present (Expressions (Sub_Aggr)) then
4183 Expr := First (Expressions (Sub_Aggr));
4184 while Present (Expr) loop
4185 Check_Same_Aggr_Bounds (Expr, Dim + 1);
4190 -- Process component associations
4192 if Present (Component_Associations (Sub_Aggr)) then
4193 Assoc := First (Component_Associations (Sub_Aggr));
4194 while Present (Assoc) loop
4195 Expr := Expression (Assoc);
4196 Check_Same_Aggr_Bounds (Expr, Dim + 1);
4201 end Check_Same_Aggr_Bounds;
4203 ----------------------------
4204 -- Compute_Others_Present --
4205 ----------------------------
4207 procedure Compute_Others_Present (Sub_Aggr : Node_Id; Dim : Pos) is
4212 if Present (Component_Associations (Sub_Aggr)) then
4213 Assoc := Last (Component_Associations (Sub_Aggr));
4215 if Nkind (First (Choices (Assoc))) = N_Others_Choice then
4216 Others_Present (Dim) := True;
4220 -- Now look inside the sub-aggregate to see if there is more work
4222 if Dim < Aggr_Dimension then
4224 -- Process positional components
4226 if Present (Expressions (Sub_Aggr)) then
4227 Expr := First (Expressions (Sub_Aggr));
4228 while Present (Expr) loop
4229 Compute_Others_Present (Expr, Dim + 1);
4234 -- Process component associations
4236 if Present (Component_Associations (Sub_Aggr)) then
4237 Assoc := First (Component_Associations (Sub_Aggr));
4238 while Present (Assoc) loop
4239 Expr := Expression (Assoc);
4240 Compute_Others_Present (Expr, Dim + 1);
4245 end Compute_Others_Present;
4247 ------------------------
4248 -- Has_Address_Clause --
4249 ------------------------
4251 function Has_Address_Clause (D : Node_Id) return Boolean is
4252 Id : constant Entity_Id := Defining_Identifier (D);
4257 while Present (Decl) loop
4258 if Nkind (Decl) = N_At_Clause
4259 and then Chars (Identifier (Decl)) = Chars (Id)
4263 elsif Nkind (Decl) = N_Attribute_Definition_Clause
4264 and then Chars (Decl) = Name_Address
4265 and then Chars (Name (Decl)) = Chars (Id)
4274 end Has_Address_Clause;
4276 ------------------------
4277 -- In_Place_Assign_OK --
4278 ------------------------
4280 function In_Place_Assign_OK return Boolean is
4288 function Is_Others_Aggregate (Aggr : Node_Id) return Boolean;
4289 -- Aggregates that consist of a single Others choice are safe
4290 -- if the single expression is.
4292 function Safe_Aggregate (Aggr : Node_Id) return Boolean;
4293 -- Check recursively that each component of a (sub)aggregate does
4294 -- not depend on the variable being assigned to.
4296 function Safe_Component (Expr : Node_Id) return Boolean;
4297 -- Verify that an expression cannot depend on the variable being
4298 -- assigned to. Room for improvement here (but less than before).
4300 -------------------------
4301 -- Is_Others_Aggregate --
4302 -------------------------
4304 function Is_Others_Aggregate (Aggr : Node_Id) return Boolean is
4306 return No (Expressions (Aggr))
4308 (First (Choices (First (Component_Associations (Aggr)))))
4310 end Is_Others_Aggregate;
4312 --------------------
4313 -- Safe_Aggregate --
4314 --------------------
4316 function Safe_Aggregate (Aggr : Node_Id) return Boolean is
4320 if Present (Expressions (Aggr)) then
4321 Expr := First (Expressions (Aggr));
4322 while Present (Expr) loop
4323 if Nkind (Expr) = N_Aggregate then
4324 if not Safe_Aggregate (Expr) then
4328 elsif not Safe_Component (Expr) then
4336 if Present (Component_Associations (Aggr)) then
4337 Expr := First (Component_Associations (Aggr));
4338 while Present (Expr) loop
4339 if Nkind (Expression (Expr)) = N_Aggregate then
4340 if not Safe_Aggregate (Expression (Expr)) then
4344 elsif not Safe_Component (Expression (Expr)) then
4355 --------------------
4356 -- Safe_Component --
4357 --------------------
4359 function Safe_Component (Expr : Node_Id) return Boolean is
4360 Comp : Node_Id := Expr;
4362 function Check_Component (Comp : Node_Id) return Boolean;
4363 -- Do the recursive traversal, after copy
4365 ---------------------
4366 -- Check_Component --
4367 ---------------------
4369 function Check_Component (Comp : Node_Id) return Boolean is
4371 if Is_Overloaded (Comp) then
4375 return Compile_Time_Known_Value (Comp)
4377 or else (Is_Entity_Name (Comp)
4378 and then Present (Entity (Comp))
4379 and then No (Renamed_Object (Entity (Comp))))
4381 or else (Nkind (Comp) = N_Attribute_Reference
4382 and then Check_Component (Prefix (Comp)))
4384 or else (Nkind (Comp) in N_Binary_Op
4385 and then Check_Component (Left_Opnd (Comp))
4386 and then Check_Component (Right_Opnd (Comp)))
4388 or else (Nkind (Comp) in N_Unary_Op
4389 and then Check_Component (Right_Opnd (Comp)))
4391 or else (Nkind (Comp) = N_Selected_Component
4392 and then Check_Component (Prefix (Comp)))
4394 or else (Nkind (Comp) = N_Unchecked_Type_Conversion
4395 and then Check_Component (Expression (Comp)));
4396 end Check_Component;
4398 -- Start of processing for Safe_Component
4401 -- If the component appears in an association that may
4402 -- correspond to more than one element, it is not analyzed
4403 -- before the expansion into assignments, to avoid side effects.
4404 -- We analyze, but do not resolve the copy, to obtain sufficient
4405 -- entity information for the checks that follow. If component is
4406 -- overloaded we assume an unsafe function call.
4408 if not Analyzed (Comp) then
4409 if Is_Overloaded (Expr) then
4412 elsif Nkind (Expr) = N_Aggregate
4413 and then not Is_Others_Aggregate (Expr)
4417 elsif Nkind (Expr) = N_Allocator then
4419 -- For now, too complex to analyze
4424 Comp := New_Copy_Tree (Expr);
4425 Set_Parent (Comp, Parent (Expr));
4429 if Nkind (Comp) = N_Aggregate then
4430 return Safe_Aggregate (Comp);
4432 return Check_Component (Comp);
4436 -- Start of processing for In_Place_Assign_OK
4439 if Present (Component_Associations (N)) then
4441 -- On assignment, sliding can take place, so we cannot do the
4442 -- assignment in place unless the bounds of the aggregate are
4443 -- statically equal to those of the target.
4445 -- If the aggregate is given by an others choice, the bounds
4446 -- are derived from the left-hand side, and the assignment is
4447 -- safe if the expression is.
4449 if Is_Others_Aggregate (N) then
4452 (Expression (First (Component_Associations (N))));
4455 Aggr_In := First_Index (Etype (N));
4456 if Nkind (Parent (N)) = N_Assignment_Statement then
4457 Obj_In := First_Index (Etype (Name (Parent (N))));
4460 -- Context is an allocator. Check bounds of aggregate
4461 -- against given type in qualified expression.
4463 pragma Assert (Nkind (Parent (Parent (N))) = N_Allocator);
4465 First_Index (Etype (Entity (Subtype_Mark (Parent (N)))));
4468 while Present (Aggr_In) loop
4469 Get_Index_Bounds (Aggr_In, Aggr_Lo, Aggr_Hi);
4470 Get_Index_Bounds (Obj_In, Obj_Lo, Obj_Hi);
4472 if not Compile_Time_Known_Value (Aggr_Lo)
4473 or else not Compile_Time_Known_Value (Aggr_Hi)
4474 or else not Compile_Time_Known_Value (Obj_Lo)
4475 or else not Compile_Time_Known_Value (Obj_Hi)
4476 or else Expr_Value (Aggr_Lo) /= Expr_Value (Obj_Lo)
4477 or else Expr_Value (Aggr_Hi) /= Expr_Value (Obj_Hi)
4482 Next_Index (Aggr_In);
4483 Next_Index (Obj_In);
4487 -- Now check the component values themselves
4489 return Safe_Aggregate (N);
4490 end In_Place_Assign_OK;
4496 procedure Others_Check (Sub_Aggr : Node_Id; Dim : Pos) is
4497 Aggr_Lo : constant Node_Id := Aggr_Low (Dim);
4498 Aggr_Hi : constant Node_Id := Aggr_High (Dim);
4499 -- The bounds of the aggregate for this dimension
4501 Ind_Typ : constant Entity_Id := Aggr_Index_Typ (Dim);
4502 -- The index type for this dimension
4504 Need_To_Check : Boolean := False;
4506 Choices_Lo : Node_Id := Empty;
4507 Choices_Hi : Node_Id := Empty;
4508 -- The lowest and highest discrete choices for a named sub-aggregate
4510 Nb_Choices : Int := -1;
4511 -- The number of discrete non-others choices in this sub-aggregate
4513 Nb_Elements : Uint := Uint_0;
4514 -- The number of elements in a positional aggregate
4516 Cond : Node_Id := Empty;
4523 -- Check if we have an others choice. If we do make sure that this
4524 -- sub-aggregate contains at least one element in addition to the
4527 if Range_Checks_Suppressed (Ind_Typ) then
4528 Need_To_Check := False;
4530 elsif Present (Expressions (Sub_Aggr))
4531 and then Present (Component_Associations (Sub_Aggr))
4533 Need_To_Check := True;
4535 elsif Present (Component_Associations (Sub_Aggr)) then
4536 Assoc := Last (Component_Associations (Sub_Aggr));
4538 if Nkind (First (Choices (Assoc))) /= N_Others_Choice then
4539 Need_To_Check := False;
4542 -- Count the number of discrete choices. Start with -1 because
4543 -- the others choice does not count.
4546 Assoc := First (Component_Associations (Sub_Aggr));
4547 while Present (Assoc) loop
4548 Choice := First (Choices (Assoc));
4549 while Present (Choice) loop
4550 Nb_Choices := Nb_Choices + 1;
4557 -- If there is only an others choice nothing to do
4559 Need_To_Check := (Nb_Choices > 0);
4563 Need_To_Check := False;
4566 -- If we are dealing with a positional sub-aggregate with an others
4567 -- choice then compute the number or positional elements.
4569 if Need_To_Check and then Present (Expressions (Sub_Aggr)) then
4570 Expr := First (Expressions (Sub_Aggr));
4571 Nb_Elements := Uint_0;
4572 while Present (Expr) loop
4573 Nb_Elements := Nb_Elements + 1;
4577 -- If the aggregate contains discrete choices and an others choice
4578 -- compute the smallest and largest discrete choice values.
4580 elsif Need_To_Check then
4581 Compute_Choices_Lo_And_Choices_Hi : declare
4583 Table : Case_Table_Type (1 .. Nb_Choices);
4584 -- Used to sort all the different choice values
4591 Assoc := First (Component_Associations (Sub_Aggr));
4592 while Present (Assoc) loop
4593 Choice := First (Choices (Assoc));
4594 while Present (Choice) loop
4595 if Nkind (Choice) = N_Others_Choice then
4599 Get_Index_Bounds (Choice, Low, High);
4600 Table (J).Choice_Lo := Low;
4601 Table (J).Choice_Hi := High;
4610 -- Sort the discrete choices
4612 Sort_Case_Table (Table);
4614 Choices_Lo := Table (1).Choice_Lo;
4615 Choices_Hi := Table (Nb_Choices).Choice_Hi;
4616 end Compute_Choices_Lo_And_Choices_Hi;
4619 -- If no others choice in this sub-aggregate, or the aggregate
4620 -- comprises only an others choice, nothing to do.
4622 if not Need_To_Check then
4625 -- If we are dealing with an aggregate containing an others choice
4626 -- and positional components, we generate the following test:
4628 -- if Ind_Typ'Pos (Aggr_Lo) + (Nb_Elements - 1) >
4629 -- Ind_Typ'Pos (Aggr_Hi)
4631 -- raise Constraint_Error;
4634 elsif Nb_Elements > Uint_0 then
4640 Make_Attribute_Reference (Loc,
4641 Prefix => New_Reference_To (Ind_Typ, Loc),
4642 Attribute_Name => Name_Pos,
4645 (Duplicate_Subexpr_Move_Checks (Aggr_Lo))),
4646 Right_Opnd => Make_Integer_Literal (Loc, Nb_Elements - 1)),
4649 Make_Attribute_Reference (Loc,
4650 Prefix => New_Reference_To (Ind_Typ, Loc),
4651 Attribute_Name => Name_Pos,
4652 Expressions => New_List (
4653 Duplicate_Subexpr_Move_Checks (Aggr_Hi))));
4655 -- If we are dealing with an aggregate containing an others choice
4656 -- and discrete choices we generate the following test:
4658 -- [constraint_error when
4659 -- Choices_Lo < Aggr_Lo or else Choices_Hi > Aggr_Hi];
4667 Duplicate_Subexpr_Move_Checks (Choices_Lo),
4669 Duplicate_Subexpr_Move_Checks (Aggr_Lo)),
4674 Duplicate_Subexpr (Choices_Hi),
4676 Duplicate_Subexpr (Aggr_Hi)));
4679 if Present (Cond) then
4681 Make_Raise_Constraint_Error (Loc,
4683 Reason => CE_Length_Check_Failed));
4686 -- Now look inside the sub-aggregate to see if there is more work
4688 if Dim < Aggr_Dimension then
4690 -- Process positional components
4692 if Present (Expressions (Sub_Aggr)) then
4693 Expr := First (Expressions (Sub_Aggr));
4694 while Present (Expr) loop
4695 Others_Check (Expr, Dim + 1);
4700 -- Process component associations
4702 if Present (Component_Associations (Sub_Aggr)) then
4703 Assoc := First (Component_Associations (Sub_Aggr));
4704 while Present (Assoc) loop
4705 Expr := Expression (Assoc);
4706 Others_Check (Expr, Dim + 1);
4713 -- Remaining Expand_Array_Aggregate variables
4716 -- Holds the temporary aggregate value
4719 -- Holds the declaration of Tmp
4721 Aggr_Code : List_Id;
4722 Parent_Node : Node_Id;
4723 Parent_Kind : Node_Kind;
4725 -- Start of processing for Expand_Array_Aggregate
4728 -- Do not touch the special aggregates of attributes used for Asm calls
4730 if Is_RTE (Ctyp, RE_Asm_Input_Operand)
4731 or else Is_RTE (Ctyp, RE_Asm_Output_Operand)
4736 -- If the semantic analyzer has determined that aggregate N will raise
4737 -- Constraint_Error at run-time, then the aggregate node has been
4738 -- replaced with an N_Raise_Constraint_Error node and we should
4741 pragma Assert (not Raises_Constraint_Error (N));
4745 -- Check that the index range defined by aggregate bounds is
4746 -- compatible with corresponding index subtype.
4748 Index_Compatibility_Check : declare
4749 Aggr_Index_Range : Node_Id := First_Index (Typ);
4750 -- The current aggregate index range
4752 Index_Constraint : Node_Id := First_Index (Etype (Typ));
4753 -- The corresponding index constraint against which we have to
4754 -- check the above aggregate index range.
4757 Compute_Others_Present (N, 1);
4759 for J in 1 .. Aggr_Dimension loop
4760 -- There is no need to emit a check if an others choice is
4761 -- present for this array aggregate dimension since in this
4762 -- case one of N's sub-aggregates has taken its bounds from the
4763 -- context and these bounds must have been checked already. In
4764 -- addition all sub-aggregates corresponding to the same
4765 -- dimension must all have the same bounds (checked in (c) below).
4767 if not Range_Checks_Suppressed (Etype (Index_Constraint))
4768 and then not Others_Present (J)
4770 -- We don't use Checks.Apply_Range_Check here because it emits
4771 -- a spurious check. Namely it checks that the range defined by
4772 -- the aggregate bounds is non empty. But we know this already
4775 Check_Bounds (Aggr_Index_Range, Index_Constraint);
4778 -- Save the low and high bounds of the aggregate index as well as
4779 -- the index type for later use in checks (b) and (c) below.
4781 Aggr_Low (J) := Low_Bound (Aggr_Index_Range);
4782 Aggr_High (J) := High_Bound (Aggr_Index_Range);
4784 Aggr_Index_Typ (J) := Etype (Index_Constraint);
4786 Next_Index (Aggr_Index_Range);
4787 Next_Index (Index_Constraint);
4789 end Index_Compatibility_Check;
4793 -- If an others choice is present check that no aggregate index is
4794 -- outside the bounds of the index constraint.
4796 Others_Check (N, 1);
4800 -- For multidimensional arrays make sure that all subaggregates
4801 -- corresponding to the same dimension have the same bounds.
4803 if Aggr_Dimension > 1 then
4804 Check_Same_Aggr_Bounds (N, 1);
4809 -- Here we test for is packed array aggregate that we can handle at
4810 -- compile time. If so, return with transformation done. Note that we do
4811 -- this even if the aggregate is nested, because once we have done this
4812 -- processing, there is no more nested aggregate!
4814 if Packed_Array_Aggregate_Handled (N) then
4818 -- At this point we try to convert to positional form
4820 if Ekind (Current_Scope) = E_Package
4821 and then Static_Elaboration_Desired (Current_Scope)
4823 Convert_To_Positional (N, Max_Others_Replicate => 100);
4826 Convert_To_Positional (N);
4829 -- if the result is no longer an aggregate (e.g. it may be a string
4830 -- literal, or a temporary which has the needed value), then we are
4831 -- done, since there is no longer a nested aggregate.
4833 if Nkind (N) /= N_Aggregate then
4836 -- We are also done if the result is an analyzed aggregate
4837 -- This case could use more comments ???
4840 and then N /= Original_Node (N)
4845 -- If all aggregate components are compile-time known and the aggregate
4846 -- has been flattened, nothing left to do. The same occurs if the
4847 -- aggregate is used to initialize the components of an statically
4848 -- allocated dispatch table.
4850 if Compile_Time_Known_Aggregate (N)
4851 or else Is_Static_Dispatch_Table_Aggregate (N)
4853 Set_Expansion_Delayed (N, False);
4857 -- Now see if back end processing is possible
4859 if Backend_Processing_Possible (N) then
4861 -- If the aggregate is static but the constraints are not, build
4862 -- a static subtype for the aggregate, so that Gigi can place it
4863 -- in static memory. Perform an unchecked_conversion to the non-
4864 -- static type imposed by the context.
4867 Itype : constant Entity_Id := Etype (N);
4869 Needs_Type : Boolean := False;
4872 Index := First_Index (Itype);
4873 while Present (Index) loop
4874 if not Is_Static_Subtype (Etype (Index)) then
4883 Build_Constrained_Type (Positional => True);
4884 Rewrite (N, Unchecked_Convert_To (Itype, N));
4894 -- Delay expansion for nested aggregates it will be taken care of
4895 -- when the parent aggregate is expanded
4897 Parent_Node := Parent (N);
4898 Parent_Kind := Nkind (Parent_Node);
4900 if Parent_Kind = N_Qualified_Expression then
4901 Parent_Node := Parent (Parent_Node);
4902 Parent_Kind := Nkind (Parent_Node);
4905 if Parent_Kind = N_Aggregate
4906 or else Parent_Kind = N_Extension_Aggregate
4907 or else Parent_Kind = N_Component_Association
4908 or else (Parent_Kind = N_Object_Declaration
4909 and then Controlled_Type (Typ))
4910 or else (Parent_Kind = N_Assignment_Statement
4911 and then Inside_Init_Proc)
4913 if Static_Array_Aggregate (N)
4914 or else Compile_Time_Known_Aggregate (N)
4916 Set_Expansion_Delayed (N, False);
4919 Set_Expansion_Delayed (N);
4926 -- Look if in place aggregate expansion is possible
4928 -- For object declarations we build the aggregate in place, unless
4929 -- the array is bit-packed or the component is controlled.
4931 -- For assignments we do the assignment in place if all the component
4932 -- associations have compile-time known values. For other cases we
4933 -- create a temporary. The analysis for safety of on-line assignment
4934 -- is delicate, i.e. we don't know how to do it fully yet ???
4936 -- For allocators we assign to the designated object in place if the
4937 -- aggregate meets the same conditions as other in-place assignments.
4938 -- In this case the aggregate may not come from source but was created
4939 -- for default initialization, e.g. with Initialize_Scalars.
4941 if Requires_Transient_Scope (Typ) then
4942 Establish_Transient_Scope
4943 (N, Sec_Stack => Has_Controlled_Component (Typ));
4946 if Has_Default_Init_Comps (N) then
4947 Maybe_In_Place_OK := False;
4949 elsif Is_Bit_Packed_Array (Typ)
4950 or else Has_Controlled_Component (Typ)
4952 Maybe_In_Place_OK := False;
4955 Maybe_In_Place_OK :=
4956 (Nkind (Parent (N)) = N_Assignment_Statement
4957 and then Comes_From_Source (N)
4958 and then In_Place_Assign_OK)
4961 (Nkind (Parent (Parent (N))) = N_Allocator
4962 and then In_Place_Assign_OK);
4965 if not Has_Default_Init_Comps (N)
4966 and then Comes_From_Source (Parent (N))
4967 and then Nkind (Parent (N)) = N_Object_Declaration
4969 Must_Slide (Etype (Defining_Identifier (Parent (N))), Typ)
4970 and then N = Expression (Parent (N))
4971 and then not Is_Bit_Packed_Array (Typ)
4972 and then not Has_Controlled_Component (Typ)
4973 and then not Has_Address_Clause (Parent (N))
4975 Tmp := Defining_Identifier (Parent (N));
4976 Set_No_Initialization (Parent (N));
4977 Set_Expression (Parent (N), Empty);
4979 -- Set the type of the entity, for use in the analysis of the
4980 -- subsequent indexed assignments. If the nominal type is not
4981 -- constrained, build a subtype from the known bounds of the
4982 -- aggregate. If the declaration has a subtype mark, use it,
4983 -- otherwise use the itype of the aggregate.
4985 if not Is_Constrained (Typ) then
4986 Build_Constrained_Type (Positional => False);
4987 elsif Is_Entity_Name (Object_Definition (Parent (N)))
4988 and then Is_Constrained (Entity (Object_Definition (Parent (N))))
4990 Set_Etype (Tmp, Entity (Object_Definition (Parent (N))));
4992 Set_Size_Known_At_Compile_Time (Typ, False);
4993 Set_Etype (Tmp, Typ);
4996 elsif Maybe_In_Place_OK
4997 and then Nkind (Parent (N)) = N_Qualified_Expression
4998 and then Nkind (Parent (Parent (N))) = N_Allocator
5000 Set_Expansion_Delayed (N);
5003 -- In the remaining cases the aggregate is the RHS of an assignment
5005 elsif Maybe_In_Place_OK
5006 and then Is_Entity_Name (Name (Parent (N)))
5008 Tmp := Entity (Name (Parent (N)));
5010 if Etype (Tmp) /= Etype (N) then
5011 Apply_Length_Check (N, Etype (Tmp));
5013 if Nkind (N) = N_Raise_Constraint_Error then
5015 -- Static error, nothing further to expand
5021 elsif Maybe_In_Place_OK
5022 and then Nkind (Name (Parent (N))) = N_Explicit_Dereference
5023 and then Is_Entity_Name (Prefix (Name (Parent (N))))
5025 Tmp := Name (Parent (N));
5027 if Etype (Tmp) /= Etype (N) then
5028 Apply_Length_Check (N, Etype (Tmp));
5031 elsif Maybe_In_Place_OK
5032 and then Nkind (Name (Parent (N))) = N_Slice
5033 and then Safe_Slice_Assignment (N)
5035 -- Safe_Slice_Assignment rewrites assignment as a loop
5041 -- In place aggregate expansion is not possible
5044 Maybe_In_Place_OK := False;
5045 Tmp := Make_Defining_Identifier (Loc, New_Internal_Name ('A'));
5047 Make_Object_Declaration
5049 Defining_Identifier => Tmp,
5050 Object_Definition => New_Occurrence_Of (Typ, Loc));
5051 Set_No_Initialization (Tmp_Decl, True);
5053 -- If we are within a loop, the temporary will be pushed on the
5054 -- stack at each iteration. If the aggregate is the expression for
5055 -- an allocator, it will be immediately copied to the heap and can
5056 -- be reclaimed at once. We create a transient scope around the
5057 -- aggregate for this purpose.
5059 if Ekind (Current_Scope) = E_Loop
5060 and then Nkind (Parent (Parent (N))) = N_Allocator
5062 Establish_Transient_Scope (N, False);
5065 Insert_Action (N, Tmp_Decl);
5068 -- Construct and insert the aggregate code. We can safely suppress
5069 -- index checks because this code is guaranteed not to raise CE
5070 -- on index checks. However we should *not* suppress all checks.
5076 if Nkind (Tmp) = N_Defining_Identifier then
5077 Target := New_Reference_To (Tmp, Loc);
5081 if Has_Default_Init_Comps (N) then
5083 -- Ada 2005 (AI-287): This case has not been analyzed???
5085 raise Program_Error;
5088 -- Name in assignment is explicit dereference
5090 Target := New_Copy (Tmp);
5094 Build_Array_Aggr_Code (N,
5096 Index => First_Index (Typ),
5098 Scalar_Comp => Is_Scalar_Type (Ctyp));
5101 if Comes_From_Source (Tmp) then
5102 Insert_Actions_After (Parent (N), Aggr_Code);
5105 Insert_Actions (N, Aggr_Code);
5108 -- If the aggregate has been assigned in place, remove the original
5111 if Nkind (Parent (N)) = N_Assignment_Statement
5112 and then Maybe_In_Place_OK
5114 Rewrite (Parent (N), Make_Null_Statement (Loc));
5116 elsif Nkind (Parent (N)) /= N_Object_Declaration
5117 or else Tmp /= Defining_Identifier (Parent (N))
5119 Rewrite (N, New_Occurrence_Of (Tmp, Loc));
5120 Analyze_And_Resolve (N, Typ);
5122 end Expand_Array_Aggregate;
5124 ------------------------
5125 -- Expand_N_Aggregate --
5126 ------------------------
5128 procedure Expand_N_Aggregate (N : Node_Id) is
5130 if Is_Record_Type (Etype (N)) then
5131 Expand_Record_Aggregate (N);
5133 Expand_Array_Aggregate (N);
5136 when RE_Not_Available =>
5138 end Expand_N_Aggregate;
5140 ----------------------------------
5141 -- Expand_N_Extension_Aggregate --
5142 ----------------------------------
5144 -- If the ancestor part is an expression, add a component association for
5145 -- the parent field. If the type of the ancestor part is not the direct
5146 -- parent of the expected type, build recursively the needed ancestors.
5147 -- If the ancestor part is a subtype_mark, replace aggregate with a decla-
5148 -- ration for a temporary of the expected type, followed by individual
5149 -- assignments to the given components.
5151 procedure Expand_N_Extension_Aggregate (N : Node_Id) is
5152 Loc : constant Source_Ptr := Sloc (N);
5153 A : constant Node_Id := Ancestor_Part (N);
5154 Typ : constant Entity_Id := Etype (N);
5157 -- If the ancestor is a subtype mark, an init proc must be called
5158 -- on the resulting object which thus has to be materialized in
5161 if Is_Entity_Name (A) and then Is_Type (Entity (A)) then
5162 Convert_To_Assignments (N, Typ);
5164 -- The extension aggregate is transformed into a record aggregate
5165 -- of the following form (c1 and c2 are inherited components)
5167 -- (Exp with c3 => a, c4 => b)
5168 -- ==> (c1 => Exp.c1, c2 => Exp.c2, c1 => a, c2 => b)
5173 if VM_Target = No_VM then
5174 Expand_Record_Aggregate (N,
5177 (Node (First_Elmt (Access_Disp_Table (Typ))), Loc),
5180 -- No tag is needed in the case of a VM
5181 Expand_Record_Aggregate (N,
5187 when RE_Not_Available =>
5189 end Expand_N_Extension_Aggregate;
5191 -----------------------------
5192 -- Expand_Record_Aggregate --
5193 -----------------------------
5195 procedure Expand_Record_Aggregate
5197 Orig_Tag : Node_Id := Empty;
5198 Parent_Expr : Node_Id := Empty)
5200 Loc : constant Source_Ptr := Sloc (N);
5201 Comps : constant List_Id := Component_Associations (N);
5202 Typ : constant Entity_Id := Etype (N);
5203 Base_Typ : constant Entity_Id := Base_Type (Typ);
5205 Static_Components : Boolean := True;
5206 -- Flag to indicate whether all components are compile-time known,
5207 -- and the aggregate can be constructed statically and handled by
5210 function Component_Not_OK_For_Backend return Boolean;
5211 -- Check for presence of component which makes it impossible for the
5212 -- backend to process the aggregate, thus requiring the use of a series
5213 -- of assignment statements. Cases checked for are a nested aggregate
5214 -- needing Late_Expansion, the presence of a tagged component which may
5215 -- need tag adjustment, and a bit unaligned component reference.
5217 -- We also force expansion into assignments if a component is of a
5218 -- mutable type (including a private type with discriminants) because
5219 -- in that case the size of the component to be copied may be smaller
5220 -- than the side of the target, and there is no simple way for gigi
5221 -- to compute the size of the object to be copied.
5223 -- NOTE: This is part of the ongoing work to define precisely the
5224 -- interface between front-end and back-end handling of aggregates.
5225 -- In general it is desirable to pass aggregates as they are to gigi,
5226 -- in order to minimize elaboration code. This is one case where the
5227 -- semantics of Ada complicate the analysis and lead to anomalies in
5228 -- the gcc back-end if the aggregate is not expanded into assignments.
5230 ----------------------------------
5231 -- Component_Not_OK_For_Backend --
5232 ----------------------------------
5234 function Component_Not_OK_For_Backend return Boolean is
5244 while Present (C) loop
5245 if Nkind (Expression (C)) = N_Qualified_Expression then
5246 Expr_Q := Expression (Expression (C));
5248 Expr_Q := Expression (C);
5251 -- Return true if the aggregate has any associations for tagged
5252 -- components that may require tag adjustment.
5254 -- These are cases where the source expression may have a tag that
5255 -- could differ from the component tag (e.g., can occur for type
5256 -- conversions and formal parameters). (Tag adjustment not needed
5257 -- if VM_Target because object tags are implicit in the machine.)
5259 if Is_Tagged_Type (Etype (Expr_Q))
5260 and then (Nkind (Expr_Q) = N_Type_Conversion
5261 or else (Is_Entity_Name (Expr_Q)
5263 Ekind (Entity (Expr_Q)) in Formal_Kind))
5264 and then VM_Target = No_VM
5266 Static_Components := False;
5269 elsif Is_Delayed_Aggregate (Expr_Q) then
5270 Static_Components := False;
5273 elsif Possible_Bit_Aligned_Component (Expr_Q) then
5274 Static_Components := False;
5278 if Is_Scalar_Type (Etype (Expr_Q)) then
5279 if not Compile_Time_Known_Value (Expr_Q) then
5280 Static_Components := False;
5283 elsif Nkind (Expr_Q) /= N_Aggregate
5284 or else not Compile_Time_Known_Aggregate (Expr_Q)
5286 Static_Components := False;
5288 if Is_Private_Type (Etype (Expr_Q))
5289 and then Has_Discriminants (Etype (Expr_Q))
5299 end Component_Not_OK_For_Backend;
5301 -- Remaining Expand_Record_Aggregate variables
5303 Tag_Value : Node_Id;
5307 -- Start of processing for Expand_Record_Aggregate
5310 -- If the aggregate is to be assigned to an atomic variable, we
5311 -- have to prevent a piecemeal assignment even if the aggregate
5312 -- is to be expanded. We create a temporary for the aggregate, and
5313 -- assign the temporary instead, so that the back end can generate
5314 -- an atomic move for it.
5317 and then (Nkind (Parent (N)) = N_Object_Declaration
5318 or else Nkind (Parent (N)) = N_Assignment_Statement)
5319 and then Comes_From_Source (Parent (N))
5321 Expand_Atomic_Aggregate (N, Typ);
5324 -- No special management required for aggregates used to initialize
5325 -- statically allocated dispatch tables
5327 elsif Is_Static_Dispatch_Table_Aggregate (N) then
5331 -- Ada 2005 (AI-318-2): We need to convert to assignments if components
5332 -- are build-in-place function calls. This test could be more specific,
5333 -- but doing it for all inherently limited aggregates seems harmless.
5334 -- The assignments will turn into build-in-place function calls (see
5335 -- Make_Build_In_Place_Call_In_Assignment).
5337 if Ada_Version >= Ada_05 and then Is_Inherently_Limited_Type (Typ) then
5338 Convert_To_Assignments (N, Typ);
5340 -- Gigi doesn't handle properly temporaries of variable size
5341 -- so we generate it in the front-end
5343 elsif not Size_Known_At_Compile_Time (Typ) then
5344 Convert_To_Assignments (N, Typ);
5346 -- Temporaries for controlled aggregates need to be attached to a
5347 -- final chain in order to be properly finalized, so it has to
5348 -- be created in the front-end
5350 elsif Is_Controlled (Typ)
5351 or else Has_Controlled_Component (Base_Type (Typ))
5353 Convert_To_Assignments (N, Typ);
5355 -- Ada 2005 (AI-287): In case of default initialized components we
5356 -- convert the aggregate into assignments.
5358 elsif Has_Default_Init_Comps (N) then
5359 Convert_To_Assignments (N, Typ);
5363 elsif Component_Not_OK_For_Backend then
5364 Convert_To_Assignments (N, Typ);
5366 -- If an ancestor is private, some components are not inherited and
5367 -- we cannot expand into a record aggregate
5369 elsif Has_Private_Ancestor (Typ) then
5370 Convert_To_Assignments (N, Typ);
5372 -- ??? The following was done to compile fxacc00.ads in the ACVCs. Gigi
5373 -- is not able to handle the aggregate for Late_Request.
5375 elsif Is_Tagged_Type (Typ) and then Has_Discriminants (Typ) then
5376 Convert_To_Assignments (N, Typ);
5378 -- If the tagged types covers interface types we need to initialize all
5379 -- hidden components containing pointers to secondary dispatch tables.
5381 elsif Is_Tagged_Type (Typ) and then Has_Abstract_Interfaces (Typ) then
5382 Convert_To_Assignments (N, Typ);
5384 -- If some components are mutable, the size of the aggregate component
5385 -- may be distinct from the default size of the type component, so
5386 -- we need to expand to insure that the back-end copies the proper
5387 -- size of the data.
5389 elsif Has_Mutable_Components (Typ) then
5390 Convert_To_Assignments (N, Typ);
5392 -- If the type involved has any non-bit aligned components, then we are
5393 -- not sure that the back end can handle this case correctly.
5395 elsif Type_May_Have_Bit_Aligned_Components (Typ) then
5396 Convert_To_Assignments (N, Typ);
5398 -- In all other cases, build a proper aggregate handlable by gigi
5401 if Nkind (N) = N_Aggregate then
5403 -- If the aggregate is static and can be handled by the back-end,
5404 -- nothing left to do.
5406 if Static_Components then
5407 Set_Compile_Time_Known_Aggregate (N);
5408 Set_Expansion_Delayed (N, False);
5412 -- If no discriminants, nothing special to do
5414 if not Has_Discriminants (Typ) then
5417 -- Case of discriminants present
5419 elsif Is_Derived_Type (Typ) then
5421 -- For untagged types, non-stored discriminants are replaced
5422 -- with stored discriminants, which are the ones that gigi uses
5423 -- to describe the type and its components.
5425 Generate_Aggregate_For_Derived_Type : declare
5426 Constraints : constant List_Id := New_List;
5427 First_Comp : Node_Id;
5428 Discriminant : Entity_Id;
5430 Num_Disc : Int := 0;
5431 Num_Gird : Int := 0;
5433 procedure Prepend_Stored_Values (T : Entity_Id);
5434 -- Scan the list of stored discriminants of the type, and add
5435 -- their values to the aggregate being built.
5437 ---------------------------
5438 -- Prepend_Stored_Values --
5439 ---------------------------
5441 procedure Prepend_Stored_Values (T : Entity_Id) is
5443 Discriminant := First_Stored_Discriminant (T);
5444 while Present (Discriminant) loop
5446 Make_Component_Association (Loc,
5448 New_List (New_Occurrence_Of (Discriminant, Loc)),
5452 Get_Discriminant_Value (
5455 Discriminant_Constraint (Typ))));
5457 if No (First_Comp) then
5458 Prepend_To (Component_Associations (N), New_Comp);
5460 Insert_After (First_Comp, New_Comp);
5463 First_Comp := New_Comp;
5464 Next_Stored_Discriminant (Discriminant);
5466 end Prepend_Stored_Values;
5468 -- Start of processing for Generate_Aggregate_For_Derived_Type
5471 -- Remove the associations for the discriminant of derived type
5473 First_Comp := First (Component_Associations (N));
5474 while Present (First_Comp) loop
5479 (First (Choices (Comp)))) = E_Discriminant
5482 Num_Disc := Num_Disc + 1;
5486 -- Insert stored discriminant associations in the correct
5487 -- order. If there are more stored discriminants than new
5488 -- discriminants, there is at least one new discriminant that
5489 -- constrains more than one of the stored discriminants. In
5490 -- this case we need to construct a proper subtype of the
5491 -- parent type, in order to supply values to all the
5492 -- components. Otherwise there is one-one correspondence
5493 -- between the constraints and the stored discriminants.
5495 First_Comp := Empty;
5497 Discriminant := First_Stored_Discriminant (Base_Type (Typ));
5498 while Present (Discriminant) loop
5499 Num_Gird := Num_Gird + 1;
5500 Next_Stored_Discriminant (Discriminant);
5503 -- Case of more stored discriminants than new discriminants
5505 if Num_Gird > Num_Disc then
5507 -- Create a proper subtype of the parent type, which is the
5508 -- proper implementation type for the aggregate, and convert
5509 -- it to the intended target type.
5511 Discriminant := First_Stored_Discriminant (Base_Type (Typ));
5512 while Present (Discriminant) loop
5515 Get_Discriminant_Value (
5518 Discriminant_Constraint (Typ)));
5519 Append (New_Comp, Constraints);
5520 Next_Stored_Discriminant (Discriminant);
5524 Make_Subtype_Declaration (Loc,
5525 Defining_Identifier =>
5526 Make_Defining_Identifier (Loc,
5527 New_Internal_Name ('T')),
5528 Subtype_Indication =>
5529 Make_Subtype_Indication (Loc,
5531 New_Occurrence_Of (Etype (Base_Type (Typ)), Loc),
5533 Make_Index_Or_Discriminant_Constraint
5534 (Loc, Constraints)));
5536 Insert_Action (N, Decl);
5537 Prepend_Stored_Values (Base_Type (Typ));
5539 Set_Etype (N, Defining_Identifier (Decl));
5542 Rewrite (N, Unchecked_Convert_To (Typ, N));
5545 -- Case where we do not have fewer new discriminants than
5546 -- stored discriminants, so in this case we can simply use the
5547 -- stored discriminants of the subtype.
5550 Prepend_Stored_Values (Typ);
5552 end Generate_Aggregate_For_Derived_Type;
5555 if Is_Tagged_Type (Typ) then
5557 -- The tagged case, _parent and _tag component must be created
5559 -- Reset null_present unconditionally. tagged records always have
5560 -- at least one field (the tag or the parent)
5562 Set_Null_Record_Present (N, False);
5564 -- When the current aggregate comes from the expansion of an
5565 -- extension aggregate, the parent expr is replaced by an
5566 -- aggregate formed by selected components of this expr
5568 if Present (Parent_Expr)
5569 and then Is_Empty_List (Comps)
5571 Comp := First_Component_Or_Discriminant (Typ);
5572 while Present (Comp) loop
5574 -- Skip all expander-generated components
5577 not Comes_From_Source (Original_Record_Component (Comp))
5583 Make_Selected_Component (Loc,
5585 Unchecked_Convert_To (Typ,
5586 Duplicate_Subexpr (Parent_Expr, True)),
5588 Selector_Name => New_Occurrence_Of (Comp, Loc));
5591 Make_Component_Association (Loc,
5593 New_List (New_Occurrence_Of (Comp, Loc)),
5597 Analyze_And_Resolve (New_Comp, Etype (Comp));
5600 Next_Component_Or_Discriminant (Comp);
5604 -- Compute the value for the Tag now, if the type is a root it
5605 -- will be included in the aggregate right away, otherwise it will
5606 -- be propagated to the parent aggregate
5608 if Present (Orig_Tag) then
5609 Tag_Value := Orig_Tag;
5610 elsif VM_Target /= No_VM then
5615 (Node (First_Elmt (Access_Disp_Table (Typ))), Loc);
5618 -- For a derived type, an aggregate for the parent is formed with
5619 -- all the inherited components.
5621 if Is_Derived_Type (Typ) then
5624 First_Comp : Node_Id;
5625 Parent_Comps : List_Id;
5626 Parent_Aggr : Node_Id;
5627 Parent_Name : Node_Id;
5630 -- Remove the inherited component association from the
5631 -- aggregate and store them in the parent aggregate
5633 First_Comp := First (Component_Associations (N));
5634 Parent_Comps := New_List;
5635 while Present (First_Comp)
5636 and then Scope (Original_Record_Component (
5637 Entity (First (Choices (First_Comp))))) /= Base_Typ
5642 Append (Comp, Parent_Comps);
5645 Parent_Aggr := Make_Aggregate (Loc,
5646 Component_Associations => Parent_Comps);
5647 Set_Etype (Parent_Aggr, Etype (Base_Type (Typ)));
5649 -- Find the _parent component
5651 Comp := First_Component (Typ);
5652 while Chars (Comp) /= Name_uParent loop
5653 Comp := Next_Component (Comp);
5656 Parent_Name := New_Occurrence_Of (Comp, Loc);
5658 -- Insert the parent aggregate
5660 Prepend_To (Component_Associations (N),
5661 Make_Component_Association (Loc,
5662 Choices => New_List (Parent_Name),
5663 Expression => Parent_Aggr));
5665 -- Expand recursively the parent propagating the right Tag
5667 Expand_Record_Aggregate (
5668 Parent_Aggr, Tag_Value, Parent_Expr);
5671 -- For a root type, the tag component is added (unless compiling
5672 -- for the VMs, where tags are implicit).
5674 elsif VM_Target = No_VM then
5676 Tag_Name : constant Node_Id :=
5678 (First_Tag_Component (Typ), Loc);
5679 Typ_Tag : constant Entity_Id := RTE (RE_Tag);
5680 Conv_Node : constant Node_Id :=
5681 Unchecked_Convert_To (Typ_Tag, Tag_Value);
5684 Set_Etype (Conv_Node, Typ_Tag);
5685 Prepend_To (Component_Associations (N),
5686 Make_Component_Association (Loc,
5687 Choices => New_List (Tag_Name),
5688 Expression => Conv_Node));
5694 end Expand_Record_Aggregate;
5696 ----------------------------
5697 -- Has_Default_Init_Comps --
5698 ----------------------------
5700 function Has_Default_Init_Comps (N : Node_Id) return Boolean is
5701 Comps : constant List_Id := Component_Associations (N);
5705 pragma Assert (Nkind (N) = N_Aggregate
5706 or else Nkind (N) = N_Extension_Aggregate);
5712 if Has_Self_Reference (N) then
5716 -- Check if any direct component has default initialized components
5719 while Present (C) loop
5720 if Box_Present (C) then
5727 -- Recursive call in case of aggregate expression
5730 while Present (C) loop
5731 Expr := Expression (C);
5734 and then (Nkind (Expr) = N_Aggregate
5735 or else Nkind (Expr) = N_Extension_Aggregate)
5736 and then Has_Default_Init_Comps (Expr)
5745 end Has_Default_Init_Comps;
5747 --------------------------
5748 -- Is_Delayed_Aggregate --
5749 --------------------------
5751 function Is_Delayed_Aggregate (N : Node_Id) return Boolean is
5752 Node : Node_Id := N;
5753 Kind : Node_Kind := Nkind (Node);
5756 if Kind = N_Qualified_Expression then
5757 Node := Expression (Node);
5758 Kind := Nkind (Node);
5761 if Kind /= N_Aggregate and then Kind /= N_Extension_Aggregate then
5764 return Expansion_Delayed (Node);
5766 end Is_Delayed_Aggregate;
5768 ----------------------------------------
5769 -- Is_Static_Dispatch_Table_Aggregate --
5770 ----------------------------------------
5772 function Is_Static_Dispatch_Table_Aggregate (N : Node_Id) return Boolean is
5773 Typ : constant Entity_Id := Base_Type (Etype (N));
5776 return Static_Dispatch_Tables
5777 and then VM_Target = No_VM
5778 and then RTU_Loaded (Ada_Tags)
5780 -- Avoid circularity when rebuilding the compiler
5782 and then Cunit_Entity (Get_Source_Unit (N)) /= RTU_Entity (Ada_Tags)
5783 and then (Typ = RTE (RE_Dispatch_Table_Wrapper)
5785 Typ = RTE (RE_Address_Array)
5787 Typ = RTE (RE_Type_Specific_Data)
5789 Typ = RTE (RE_Tag_Table)
5791 (RTE_Available (RE_Interface_Data)
5792 and then Typ = RTE (RE_Interface_Data))
5794 (RTE_Available (RE_Interfaces_Array)
5795 and then Typ = RTE (RE_Interfaces_Array))
5797 (RTE_Available (RE_Interface_Data_Element)
5798 and then Typ = RTE (RE_Interface_Data_Element)));
5799 end Is_Static_Dispatch_Table_Aggregate;
5801 --------------------
5802 -- Late_Expansion --
5803 --------------------
5805 function Late_Expansion
5809 Flist : Node_Id := Empty;
5810 Obj : Entity_Id := Empty) return List_Id
5813 if Is_Record_Type (Etype (N)) then
5814 return Build_Record_Aggr_Code (N, Typ, Target, Flist, Obj);
5816 else pragma Assert (Is_Array_Type (Etype (N)));
5818 Build_Array_Aggr_Code
5820 Ctype => Component_Type (Etype (N)),
5821 Index => First_Index (Typ),
5823 Scalar_Comp => Is_Scalar_Type (Component_Type (Typ)),
5829 ----------------------------------
5830 -- Make_OK_Assignment_Statement --
5831 ----------------------------------
5833 function Make_OK_Assignment_Statement
5836 Expression : Node_Id) return Node_Id
5839 Set_Assignment_OK (Name);
5841 return Make_Assignment_Statement (Sloc, Name, Expression);
5842 end Make_OK_Assignment_Statement;
5844 -----------------------
5845 -- Number_Of_Choices --
5846 -----------------------
5848 function Number_Of_Choices (N : Node_Id) return Nat is
5852 Nb_Choices : Nat := 0;
5855 if Present (Expressions (N)) then
5859 Assoc := First (Component_Associations (N));
5860 while Present (Assoc) loop
5861 Choice := First (Choices (Assoc));
5862 while Present (Choice) loop
5863 if Nkind (Choice) /= N_Others_Choice then
5864 Nb_Choices := Nb_Choices + 1;
5874 end Number_Of_Choices;
5876 ------------------------------------
5877 -- Packed_Array_Aggregate_Handled --
5878 ------------------------------------
5880 -- The current version of this procedure will handle at compile time
5881 -- any array aggregate that meets these conditions:
5883 -- One dimensional, bit packed
5884 -- Underlying packed type is modular type
5885 -- Bounds are within 32-bit Int range
5886 -- All bounds and values are static
5888 function Packed_Array_Aggregate_Handled (N : Node_Id) return Boolean is
5889 Loc : constant Source_Ptr := Sloc (N);
5890 Typ : constant Entity_Id := Etype (N);
5891 Ctyp : constant Entity_Id := Component_Type (Typ);
5893 Not_Handled : exception;
5894 -- Exception raised if this aggregate cannot be handled
5897 -- For now, handle only one dimensional bit packed arrays
5899 if not Is_Bit_Packed_Array (Typ)
5900 or else Number_Dimensions (Typ) > 1
5901 or else not Is_Modular_Integer_Type (Packed_Array_Type (Typ))
5906 if not Is_Scalar_Type (Component_Type (Typ))
5907 and then Has_Non_Standard_Rep (Component_Type (Typ))
5913 Csiz : constant Nat := UI_To_Int (Component_Size (Typ));
5917 -- Bounds of index type
5921 -- Values of bounds if compile time known
5923 function Get_Component_Val (N : Node_Id) return Uint;
5924 -- Given a expression value N of the component type Ctyp, returns a
5925 -- value of Csiz (component size) bits representing this value. If
5926 -- the value is non-static or any other reason exists why the value
5927 -- cannot be returned, then Not_Handled is raised.
5929 -----------------------
5930 -- Get_Component_Val --
5931 -----------------------
5933 function Get_Component_Val (N : Node_Id) return Uint is
5937 -- We have to analyze the expression here before doing any further
5938 -- processing here. The analysis of such expressions is deferred
5939 -- till expansion to prevent some problems of premature analysis.
5941 Analyze_And_Resolve (N, Ctyp);
5943 -- Must have a compile time value. String literals have to be
5944 -- converted into temporaries as well, because they cannot easily
5945 -- be converted into their bit representation.
5947 if not Compile_Time_Known_Value (N)
5948 or else Nkind (N) = N_String_Literal
5953 Val := Expr_Rep_Value (N);
5955 -- Adjust for bias, and strip proper number of bits
5957 if Has_Biased_Representation (Ctyp) then
5958 Val := Val - Expr_Value (Type_Low_Bound (Ctyp));
5961 return Val mod Uint_2 ** Csiz;
5962 end Get_Component_Val;
5964 -- Here we know we have a one dimensional bit packed array
5967 Get_Index_Bounds (First_Index (Typ), Lo, Hi);
5969 -- Cannot do anything if bounds are dynamic
5971 if not Compile_Time_Known_Value (Lo)
5973 not Compile_Time_Known_Value (Hi)
5978 -- Or are silly out of range of int bounds
5980 Lob := Expr_Value (Lo);
5981 Hib := Expr_Value (Hi);
5983 if not UI_Is_In_Int_Range (Lob)
5985 not UI_Is_In_Int_Range (Hib)
5990 -- At this stage we have a suitable aggregate for handling at compile
5991 -- time (the only remaining checks are that the values of expressions
5992 -- in the aggregate are compile time known (check is performed by
5993 -- Get_Component_Val), and that any subtypes or ranges are statically
5996 -- If the aggregate is not fully positional at this stage, then
5997 -- convert it to positional form. Either this will fail, in which
5998 -- case we can do nothing, or it will succeed, in which case we have
5999 -- succeeded in handling the aggregate, or it will stay an aggregate,
6000 -- in which case we have failed to handle this case.
6002 if Present (Component_Associations (N)) then
6003 Convert_To_Positional
6004 (N, Max_Others_Replicate => 64, Handle_Bit_Packed => True);
6005 return Nkind (N) /= N_Aggregate;
6008 -- Otherwise we are all positional, so convert to proper value
6011 Lov : constant Int := UI_To_Int (Lob);
6012 Hiv : constant Int := UI_To_Int (Hib);
6014 Len : constant Nat := Int'Max (0, Hiv - Lov + 1);
6015 -- The length of the array (number of elements)
6017 Aggregate_Val : Uint;
6018 -- Value of aggregate. The value is set in the low order bits of
6019 -- this value. For the little-endian case, the values are stored
6020 -- from low-order to high-order and for the big-endian case the
6021 -- values are stored from high-order to low-order. Note that gigi
6022 -- will take care of the conversions to left justify the value in
6023 -- the big endian case (because of left justified modular type
6024 -- processing), so we do not have to worry about that here.
6027 -- Integer literal for resulting constructed value
6030 -- Shift count from low order for next value
6033 -- Shift increment for loop
6036 -- Next expression from positional parameters of aggregate
6039 -- For little endian, we fill up the low order bits of the target
6040 -- value. For big endian we fill up the high order bits of the
6041 -- target value (which is a left justified modular value).
6043 if Bytes_Big_Endian xor Debug_Flag_8 then
6044 Shift := Csiz * (Len - 1);
6051 -- Loop to set the values
6054 Aggregate_Val := Uint_0;
6056 Expr := First (Expressions (N));
6057 Aggregate_Val := Get_Component_Val (Expr) * Uint_2 ** Shift;
6059 for J in 2 .. Len loop
6060 Shift := Shift + Incr;
6063 Aggregate_Val + Get_Component_Val (Expr) * Uint_2 ** Shift;
6067 -- Now we can rewrite with the proper value
6070 Make_Integer_Literal (Loc,
6071 Intval => Aggregate_Val);
6072 Set_Print_In_Hex (Lit);
6074 -- Construct the expression using this literal. Note that it is
6075 -- important to qualify the literal with its proper modular type
6076 -- since universal integer does not have the required range and
6077 -- also this is a left justified modular type, which is important
6078 -- in the big-endian case.
6081 Unchecked_Convert_To (Typ,
6082 Make_Qualified_Expression (Loc,
6084 New_Occurrence_Of (Packed_Array_Type (Typ), Loc),
6085 Expression => Lit)));
6087 Analyze_And_Resolve (N, Typ);
6095 end Packed_Array_Aggregate_Handled;
6097 ----------------------------
6098 -- Has_Mutable_Components --
6099 ----------------------------
6101 function Has_Mutable_Components (Typ : Entity_Id) return Boolean is
6105 Comp := First_Component (Typ);
6106 while Present (Comp) loop
6107 if Is_Record_Type (Etype (Comp))
6108 and then Has_Discriminants (Etype (Comp))
6109 and then not Is_Constrained (Etype (Comp))
6114 Next_Component (Comp);
6118 end Has_Mutable_Components;
6120 ------------------------------
6121 -- Initialize_Discriminants --
6122 ------------------------------
6124 procedure Initialize_Discriminants (N : Node_Id; Typ : Entity_Id) is
6125 Loc : constant Source_Ptr := Sloc (N);
6126 Bas : constant Entity_Id := Base_Type (Typ);
6127 Par : constant Entity_Id := Etype (Bas);
6128 Decl : constant Node_Id := Parent (Par);
6132 if Is_Tagged_Type (Bas)
6133 and then Is_Derived_Type (Bas)
6134 and then Has_Discriminants (Par)
6135 and then Has_Discriminants (Bas)
6136 and then Number_Discriminants (Bas) /= Number_Discriminants (Par)
6137 and then Nkind (Decl) = N_Full_Type_Declaration
6138 and then Nkind (Type_Definition (Decl)) = N_Record_Definition
6140 (Variant_Part (Component_List (Type_Definition (Decl))))
6141 and then Nkind (N) /= N_Extension_Aggregate
6144 -- Call init proc to set discriminants.
6145 -- There should eventually be a special procedure for this ???
6147 Ref := New_Reference_To (Defining_Identifier (N), Loc);
6148 Insert_Actions_After (N,
6149 Build_Initialization_Call (Sloc (N), Ref, Typ));
6151 end Initialize_Discriminants;
6158 (Obj_Type : Entity_Id;
6159 Typ : Entity_Id) return Boolean
6161 L1, L2, H1, H2 : Node_Id;
6163 -- No sliding if the type of the object is not established yet, if it is
6164 -- an unconstrained type whose actual subtype comes from the aggregate,
6165 -- or if the two types are identical.
6167 if not Is_Array_Type (Obj_Type) then
6170 elsif not Is_Constrained (Obj_Type) then
6173 elsif Typ = Obj_Type then
6177 -- Sliding can only occur along the first dimension
6179 Get_Index_Bounds (First_Index (Typ), L1, H1);
6180 Get_Index_Bounds (First_Index (Obj_Type), L2, H2);
6182 if not Is_Static_Expression (L1)
6183 or else not Is_Static_Expression (L2)
6184 or else not Is_Static_Expression (H1)
6185 or else not Is_Static_Expression (H2)
6189 return Expr_Value (L1) /= Expr_Value (L2)
6190 or else Expr_Value (H1) /= Expr_Value (H2);
6195 ---------------------------
6196 -- Safe_Slice_Assignment --
6197 ---------------------------
6199 function Safe_Slice_Assignment (N : Node_Id) return Boolean is
6200 Loc : constant Source_Ptr := Sloc (Parent (N));
6201 Pref : constant Node_Id := Prefix (Name (Parent (N)));
6202 Range_Node : constant Node_Id := Discrete_Range (Name (Parent (N)));
6210 -- Generate: for J in Range loop Pref (J) := Expr; end loop;
6212 if Comes_From_Source (N)
6213 and then No (Expressions (N))
6214 and then Nkind (First (Choices (First (Component_Associations (N)))))
6218 Expression (First (Component_Associations (N)));
6219 L_J := Make_Defining_Identifier (Loc, New_Internal_Name ('J'));
6222 Make_Iteration_Scheme (Loc,
6223 Loop_Parameter_Specification =>
6224 Make_Loop_Parameter_Specification
6226 Defining_Identifier => L_J,
6227 Discrete_Subtype_Definition => Relocate_Node (Range_Node)));
6230 Make_Assignment_Statement (Loc,
6232 Make_Indexed_Component (Loc,
6233 Prefix => Relocate_Node (Pref),
6234 Expressions => New_List (New_Occurrence_Of (L_J, Loc))),
6235 Expression => Relocate_Node (Expr));
6237 -- Construct the final loop
6240 Make_Implicit_Loop_Statement
6241 (Node => Parent (N),
6242 Identifier => Empty,
6243 Iteration_Scheme => L_Iter,
6244 Statements => New_List (L_Body));
6246 -- Set type of aggregate to be type of lhs in assignment,
6247 -- to suppress redundant length checks.
6249 Set_Etype (N, Etype (Name (Parent (N))));
6251 Rewrite (Parent (N), Stat);
6252 Analyze (Parent (N));
6258 end Safe_Slice_Assignment;
6260 ---------------------
6261 -- Sort_Case_Table --
6262 ---------------------
6264 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type) is
6265 L : constant Int := Case_Table'First;
6266 U : constant Int := Case_Table'Last;
6274 T := Case_Table (K + 1);
6278 and then Expr_Value (Case_Table (J - 1).Choice_Lo) >
6279 Expr_Value (T.Choice_Lo)
6281 Case_Table (J) := Case_Table (J - 1);
6285 Case_Table (J) := T;
6288 end Sort_Case_Table;
6290 ----------------------------
6291 -- Static_Array_Aggregate --
6292 ----------------------------
6294 function Static_Array_Aggregate (N : Node_Id) return Boolean is
6295 Bounds : constant Node_Id := Aggregate_Bounds (N);
6297 Typ : constant Entity_Id := Etype (N);
6298 Comp_Type : constant Entity_Id := Component_Type (Typ);
6305 if Is_Tagged_Type (Typ)
6306 or else Is_Controlled (Typ)
6307 or else Is_Packed (Typ)
6313 and then Nkind (Bounds) = N_Range
6314 and then Nkind (Low_Bound (Bounds)) = N_Integer_Literal
6315 and then Nkind (High_Bound (Bounds)) = N_Integer_Literal
6317 Lo := Low_Bound (Bounds);
6318 Hi := High_Bound (Bounds);
6320 if No (Component_Associations (N)) then
6322 -- Verify that all components are static integers
6324 Expr := First (Expressions (N));
6325 while Present (Expr) loop
6326 if Nkind (Expr) /= N_Integer_Literal then
6336 -- We allow only a single named association, either a static
6337 -- range or an others_clause, with a static expression.
6339 Expr := First (Component_Associations (N));
6341 if Present (Expressions (N)) then
6344 elsif Present (Next (Expr)) then
6347 elsif Present (Next (First (Choices (Expr)))) then
6351 -- The aggregate is static if all components are literals, or
6352 -- else all its components are static aggregates for the
6355 if Is_Array_Type (Comp_Type)
6356 or else Is_Record_Type (Comp_Type)
6358 if Nkind (Expression (Expr)) /= N_Aggregate
6360 not Compile_Time_Known_Aggregate (Expression (Expr))
6365 elsif Nkind (Expression (Expr)) /= N_Integer_Literal then
6369 -- Create a positional aggregate with the right number of
6370 -- copies of the expression.
6372 Agg := Make_Aggregate (Sloc (N), New_List, No_List);
6374 for I in UI_To_Int (Intval (Lo)) .. UI_To_Int (Intval (Hi))
6377 (Expressions (Agg), New_Copy (Expression (Expr)));
6378 Set_Etype (Last (Expressions (Agg)), Component_Type (Typ));
6381 Set_Aggregate_Bounds (Agg, Bounds);
6382 Set_Etype (Agg, Typ);
6385 Set_Compile_Time_Known_Aggregate (N);
6394 end Static_Array_Aggregate;