1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2005, 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 2, 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 COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
25 ------------------------------------------------------------------------------
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Debug; use Debug;
30 with Einfo; use Einfo;
31 with Elists; use Elists;
32 with Expander; use Expander;
33 with Exp_Util; use Exp_Util;
34 with Exp_Ch3; use Exp_Ch3;
35 with Exp_Ch7; use Exp_Ch7;
36 with Exp_Ch9; use Exp_Ch9;
37 with Exp_Tss; use Exp_Tss;
38 with Freeze; use Freeze;
39 with Hostparm; use Hostparm;
40 with Itypes; use Itypes;
42 with Nmake; use Nmake;
43 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 Tbuild; use Tbuild;
57 with Uintp; use Uintp;
59 package body Exp_Aggr is
61 type Case_Bounds is record
64 Choice_Node : Node_Id;
67 type Case_Table_Type is array (Nat range <>) of Case_Bounds;
68 -- Table type used by Check_Case_Choices procedure
71 (Obj_Type : Entity_Id;
72 Typ : Entity_Id) return Boolean;
73 -- A static array aggregate in an object declaration can in most cases be
74 -- expanded in place. The one exception is when the aggregate is given
75 -- with component associations that specify different bounds from those of
76 -- the type definition in the object declaration. In this pathological
77 -- case the aggregate must slide, and we must introduce an intermediate
78 -- temporary to hold it.
80 -- The same holds in an assignment to one-dimensional array of arrays,
81 -- when a component may be given with bounds that differ from those of the
84 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type);
85 -- Sort the Case Table using the Lower Bound of each Choice as the key.
86 -- A simple insertion sort is used since the number of choices in a case
87 -- statement of variant part will usually be small and probably in near
90 function Has_Default_Init_Comps (N : Node_Id) return Boolean;
91 -- N is an aggregate (record or array). Checks the presence of default
92 -- initialization (<>) in any component (Ada 2005: AI-287)
94 ------------------------------------------------------
95 -- Local subprograms for Record Aggregate Expansion --
96 ------------------------------------------------------
98 procedure Expand_Record_Aggregate
100 Orig_Tag : Node_Id := Empty;
101 Parent_Expr : Node_Id := Empty);
102 -- This is the top level procedure for record aggregate expansion.
103 -- Expansion for record aggregates needs expand aggregates for tagged
104 -- record types. Specifically Expand_Record_Aggregate adds the Tag
105 -- field in front of the Component_Association list that was created
106 -- during resolution by Resolve_Record_Aggregate.
108 -- N is the record aggregate node.
109 -- Orig_Tag is the value of the Tag that has to be provided for this
110 -- specific aggregate. It carries the tag corresponding to the type
111 -- of the outermost aggregate during the recursive expansion
112 -- Parent_Expr is the ancestor part of the original extension
115 procedure Convert_To_Assignments (N : Node_Id; Typ : Entity_Id);
116 -- N is an N_Aggregate of a N_Extension_Aggregate. Typ is the type of
117 -- the aggregate. Transform the given aggregate into a sequence of
118 -- assignments component per component.
120 function Build_Record_Aggr_Code
124 Flist : Node_Id := Empty;
125 Obj : Entity_Id := Empty;
126 Is_Limited_Ancestor_Expansion : Boolean := False) return List_Id;
127 -- N is an N_Aggregate or a N_Extension_Aggregate. Typ is the type of the
128 -- aggregate. Target is an expression containing the location on which the
129 -- component by component assignments will take place. Returns the list of
130 -- assignments plus all other adjustments needed for tagged and controlled
131 -- types. Flist is an expression representing the finalization list on
132 -- which to attach the controlled components if any. Obj is present in the
133 -- object declaration and dynamic allocation cases, it contains an entity
134 -- that allows to know if the value being created needs to be attached to
135 -- the final list in case of pragma finalize_Storage_Only.
137 -- Is_Limited_Ancestor_Expansion indicates that the function has been
138 -- called recursively to expand the limited ancestor to avoid copying it.
140 function Has_Mutable_Components (Typ : Entity_Id) return Boolean;
141 -- Return true if one of the component is of a discriminated type with
142 -- defaults. An aggregate for a type with mutable components must be
143 -- expanded into individual assignments.
145 procedure Initialize_Discriminants (N : Node_Id; Typ : Entity_Id);
146 -- If the type of the aggregate is a type extension with renamed discrimi-
147 -- nants, we must initialize the hidden discriminants of the parent.
148 -- Otherwise, the target object must not be initialized. The discriminants
149 -- are initialized by calling the initialization procedure for the type.
150 -- This is incorrect if the initialization of other components has any
151 -- side effects. We restrict this call to the case where the parent type
152 -- has a variant part, because this is the only case where the hidden
153 -- discriminants are accessed, namely when calling discriminant checking
154 -- functions of the parent type, and when applying a stream attribute to
155 -- an object of the derived type.
157 -----------------------------------------------------
158 -- Local Subprograms for Array Aggregate Expansion --
159 -----------------------------------------------------
161 function Aggr_Size_OK (Typ : Entity_Id) return Boolean;
162 -- Very large static aggregates present problems to the back-end, and
163 -- are transformed into assignments and loops. This function verifies
164 -- that the total number of components of an aggregate is acceptable
165 -- for transformation into a purely positional static form. It is called
166 -- prior to calling Flatten.
168 procedure Convert_Array_Aggr_In_Allocator
172 -- If the aggregate appears within an allocator and can be expanded in
173 -- place, this routine generates the individual assignments to components
174 -- of the designated object. This is an optimization over the general
175 -- case, where a temporary is first created on the stack and then used to
176 -- construct the allocated object on the heap.
178 procedure Convert_To_Positional
180 Max_Others_Replicate : Nat := 5;
181 Handle_Bit_Packed : Boolean := False);
182 -- If possible, convert named notation to positional notation. This
183 -- conversion is possible only in some static cases. If the conversion is
184 -- possible, then N is rewritten with the analyzed converted aggregate.
185 -- The parameter Max_Others_Replicate controls the maximum number of
186 -- values corresponding to an others choice that will be converted to
187 -- positional notation (the default of 5 is the normal limit, and reflects
188 -- the fact that normally the loop is better than a lot of separate
189 -- assignments). Note that this limit gets overridden in any case if
190 -- either of the restrictions No_Elaboration_Code or No_Implicit_Loops is
191 -- set. The parameter Handle_Bit_Packed is usually set False (since we do
192 -- not expect the back end to handle bit packed arrays, so the normal case
193 -- of conversion is pointless), but in the special case of a call from
194 -- Packed_Array_Aggregate_Handled, we set this parameter to True, since
195 -- these are cases we handle in there.
197 procedure Expand_Array_Aggregate (N : Node_Id);
198 -- This is the top-level routine to perform array aggregate expansion.
199 -- N is the N_Aggregate node to be expanded.
201 function Backend_Processing_Possible (N : Node_Id) return Boolean;
202 -- This function checks if array aggregate N can be processed directly
203 -- by Gigi. If this is the case True is returned.
205 function Build_Array_Aggr_Code
210 Scalar_Comp : Boolean;
211 Indices : List_Id := No_List;
212 Flist : Node_Id := Empty) return List_Id;
213 -- This recursive routine returns a list of statements containing the
214 -- loops and assignments that are needed for the expansion of the array
217 -- N is the (sub-)aggregate node to be expanded into code. This node
218 -- has been fully analyzed, and its Etype is properly set.
220 -- Index is the index node corresponding to the array sub-aggregate N.
222 -- Into is the target expression into which we are copying the aggregate.
223 -- Note that this node may not have been analyzed yet, and so the Etype
224 -- field may not be set.
226 -- Scalar_Comp is True if the component type of the aggregate is scalar.
228 -- Indices is the current list of expressions used to index the
229 -- object we are writing into.
231 -- Flist is an expression representing the finalization list on which
232 -- to attach the controlled components if any.
234 function Number_Of_Choices (N : Node_Id) return Nat;
235 -- Returns the number of discrete choices (not including the others choice
236 -- if present) contained in (sub-)aggregate N.
238 function Late_Expansion
242 Flist : Node_Id := Empty;
243 Obj : Entity_Id := Empty) return List_Id;
244 -- N is a nested (record or array) aggregate that has been marked with
245 -- 'Delay_Expansion'. Typ is the expected type of the aggregate and Target
246 -- is a (duplicable) expression that will hold the result of the aggregate
247 -- expansion. Flist is the finalization list to be used to attach
248 -- controlled components. 'Obj' when non empty, carries the original
249 -- object being initialized in order to know if it needs to be attached to
250 -- the previous parameter which may not be the case in the case where
251 -- Finalize_Storage_Only is set. Basically this procedure is used to
252 -- implement top-down expansions of nested aggregates. This is necessary
253 -- for avoiding temporaries at each level as well as for propagating the
254 -- right internal finalization list.
256 function Make_OK_Assignment_Statement
259 Expression : Node_Id) return Node_Id;
260 -- This is like Make_Assignment_Statement, except that Assignment_OK
261 -- is set in the left operand. All assignments built by this unit
262 -- use this routine. This is needed to deal with assignments to
263 -- initialized constants that are done in place.
265 function Packed_Array_Aggregate_Handled (N : Node_Id) return Boolean;
266 -- Given an array aggregate, this function handles the case of a packed
267 -- array aggregate with all constant values, where the aggregate can be
268 -- evaluated at compile time. If this is possible, then N is rewritten
269 -- to be its proper compile time value with all the components properly
270 -- assembled. The expression is analyzed and resolved and True is
271 -- returned. If this transformation is not possible, N is unchanged
272 -- and False is returned
274 function Safe_Slice_Assignment (N : Node_Id) return Boolean;
275 -- If a slice assignment has an aggregate with a single others_choice,
276 -- the assignment can be done in place even if bounds are not static,
277 -- by converting it into a loop over the discrete range of the slice.
283 function Aggr_Size_OK (Typ : Entity_Id) return Boolean is
291 -- The following constant determines the maximum size of an
292 -- aggregate produced by converting named to positional
293 -- notation (e.g. from others clauses). This avoids running
294 -- away with attempts to convert huge aggregates, which hit
295 -- memory limits in the backend.
297 -- The normal limit is 5000, but we increase this limit to
298 -- 2**24 (about 16 million) if Restrictions (No_Elaboration_Code)
299 -- or Restrictions (No_Implicit_Loops) is specified, since in
300 -- either case, we are at risk of declaring the program illegal
301 -- because of this limit.
303 Max_Aggr_Size : constant Nat :=
304 5000 + (2 ** 24 - 5000) *
306 (Restriction_Active (No_Elaboration_Code)
308 Restriction_Active (No_Implicit_Loops));
310 function Component_Count (T : Entity_Id) return Int;
311 -- The limit is applied to the total number of components that the
312 -- aggregate will have, which is the number of static expressions
313 -- that will appear in the flattened array. This requires a recursive
314 -- computation of the the number of scalar components of the structure.
316 ---------------------
317 -- Component_Count --
318 ---------------------
320 function Component_Count (T : Entity_Id) return Int is
325 if Is_Scalar_Type (T) then
328 elsif Is_Record_Type (T) then
329 Comp := First_Component (T);
330 while Present (Comp) loop
331 Res := Res + Component_Count (Etype (Comp));
332 Next_Component (Comp);
337 elsif Is_Array_Type (T) then
339 Lo : constant Node_Id :=
340 Type_Low_Bound (Etype (First_Index (T)));
341 Hi : constant Node_Id :=
342 Type_High_Bound (Etype (First_Index (T)));
344 Siz : constant Int := Component_Count (Component_Type (T));
347 if not Compile_Time_Known_Value (Lo)
348 or else not Compile_Time_Known_Value (Hi)
353 Siz * UI_To_Int (Expr_Value (Hi) - Expr_Value (Lo) + 1);
358 -- Can only be a null for an access type
364 -- Start of processing for Aggr_Size_OK
367 Siz := Component_Count (Component_Type (Typ));
368 Indx := First_Index (Typ);
370 while Present (Indx) loop
371 Lo := Type_Low_Bound (Etype (Indx));
372 Hi := Type_High_Bound (Etype (Indx));
374 -- Bounds need to be known at compile time
376 if not Compile_Time_Known_Value (Lo)
377 or else not Compile_Time_Known_Value (Hi)
382 Lov := Expr_Value (Lo);
383 Hiv := Expr_Value (Hi);
385 -- A flat array is always safe
392 Rng : constant Uint := Hiv - Lov + 1;
395 -- Check if size is too large
397 if not UI_Is_In_Int_Range (Rng) then
401 Siz := Siz * UI_To_Int (Rng);
405 or else Siz > Max_Aggr_Size
410 -- Bounds must be in integer range, for later array construction
412 if not UI_Is_In_Int_Range (Lov)
414 not UI_Is_In_Int_Range (Hiv)
425 ---------------------------------
426 -- Backend_Processing_Possible --
427 ---------------------------------
429 -- Backend processing by Gigi/gcc is possible only if all the following
430 -- conditions are met:
432 -- 1. N is fully positional
434 -- 2. N is not a bit-packed array aggregate;
436 -- 3. The size of N's array type must be known at compile time. Note
437 -- that this implies that the component size is also known
439 -- 4. The array type of N does not follow the Fortran layout convention
440 -- or if it does it must be 1 dimensional.
442 -- 5. The array component type is tagged, which may necessitate
443 -- reassignment of proper tags.
445 -- 6. The array component type might have unaligned bit components
447 function Backend_Processing_Possible (N : Node_Id) return Boolean is
448 Typ : constant Entity_Id := Etype (N);
449 -- Typ is the correct constrained array subtype of the aggregate
451 function Static_Check (N : Node_Id; Index : Node_Id) return Boolean;
452 -- Recursively checks that N is fully positional, returns true if so
458 function Static_Check (N : Node_Id; Index : Node_Id) return Boolean is
462 -- Check for component associations
464 if Present (Component_Associations (N)) then
468 -- Recurse to check subaggregates, which may appear in qualified
469 -- expressions. If delayed, the front-end will have to expand.
471 Expr := First (Expressions (N));
473 while Present (Expr) loop
475 if Is_Delayed_Aggregate (Expr) then
479 if Present (Next_Index (Index))
480 and then not Static_Check (Expr, Next_Index (Index))
491 -- Start of processing for Backend_Processing_Possible
494 -- Checks 2 (array must not be bit packed)
496 if Is_Bit_Packed_Array (Typ) then
500 -- Checks 4 (array must not be multi-dimensional Fortran case)
502 if Convention (Typ) = Convention_Fortran
503 and then Number_Dimensions (Typ) > 1
508 -- Checks 3 (size of array must be known at compile time)
510 if not Size_Known_At_Compile_Time (Typ) then
514 -- Checks 1 (aggregate must be fully positional)
516 if not Static_Check (N, First_Index (Typ)) then
520 -- Checks 5 (if the component type is tagged, then we may need
521 -- to do tag adjustments; perhaps this should be refined to check for
522 -- any component associations that actually need tag adjustment,
523 -- along the lines of the test that is carried out in
524 -- Has_Delayed_Nested_Aggregate_Or_Tagged_Comps for record aggregates
525 -- with tagged components, but not clear whether it's worthwhile ???;
526 -- in the case of the JVM, object tags are handled implicitly)
528 if Is_Tagged_Type (Component_Type (Typ)) and then not Java_VM then
532 -- Checks 6 (component type must not have bit aligned components)
534 if Type_May_Have_Bit_Aligned_Components (Component_Type (Typ)) then
538 -- Backend processing is possible
540 Set_Compile_Time_Known_Aggregate (N, True);
541 Set_Size_Known_At_Compile_Time (Etype (N), True);
543 end Backend_Processing_Possible;
545 ---------------------------
546 -- Build_Array_Aggr_Code --
547 ---------------------------
549 -- The code that we generate from a one dimensional aggregate is
551 -- 1. If the sub-aggregate contains discrete choices we
553 -- (a) Sort the discrete choices
555 -- (b) Otherwise for each discrete choice that specifies a range we
556 -- emit a loop. If a range specifies a maximum of three values, or
557 -- we are dealing with an expression we emit a sequence of
558 -- assignments instead of a loop.
560 -- (c) Generate the remaining loops to cover the others choice if any
562 -- 2. If the aggregate contains positional elements we
564 -- (a) translate the positional elements in a series of assignments
566 -- (b) Generate a final loop to cover the others choice if any.
567 -- Note that this final loop has to be a while loop since the case
569 -- L : Integer := Integer'Last;
570 -- H : Integer := Integer'Last;
571 -- A : array (L .. H) := (1, others =>0);
573 -- cannot be handled by a for loop. Thus for the following
575 -- array (L .. H) := (.. positional elements.., others =>E);
577 -- we always generate something like:
579 -- J : Index_Type := Index_Of_Last_Positional_Element;
581 -- J := Index_Base'Succ (J)
585 function Build_Array_Aggr_Code
590 Scalar_Comp : Boolean;
591 Indices : List_Id := No_List;
592 Flist : Node_Id := Empty) return List_Id
594 Loc : constant Source_Ptr := Sloc (N);
595 Index_Base : constant Entity_Id := Base_Type (Etype (Index));
596 Index_Base_L : constant Node_Id := Type_Low_Bound (Index_Base);
597 Index_Base_H : constant Node_Id := Type_High_Bound (Index_Base);
599 function Add (Val : Int; To : Node_Id) return Node_Id;
600 -- Returns an expression where Val is added to expression To, unless
601 -- To+Val is provably out of To's base type range. To must be an
602 -- already analyzed expression.
604 function Empty_Range (L, H : Node_Id) return Boolean;
605 -- Returns True if the range defined by L .. H is certainly empty
607 function Equal (L, H : Node_Id) return Boolean;
608 -- Returns True if L = H for sure
610 function Index_Base_Name return Node_Id;
611 -- Returns a new reference to the index type name
613 function Gen_Assign (Ind : Node_Id; Expr : Node_Id) return List_Id;
614 -- Ind must be a side-effect free expression. If the input aggregate
615 -- N to Build_Loop contains no sub-aggregates, then this function
616 -- returns the assignment statement:
618 -- Into (Indices, Ind) := Expr;
620 -- Otherwise we call Build_Code recursively
622 -- Ada 2005 (AI-287): In case of default initialized component, Expr
623 -- is empty and we generate a call to the corresponding IP subprogram.
625 function Gen_Loop (L, H : Node_Id; Expr : Node_Id) return List_Id;
626 -- Nodes L and H must be side-effect free expressions.
627 -- If the input aggregate N to Build_Loop contains no sub-aggregates,
628 -- This routine returns the for loop statement
630 -- for J in Index_Base'(L) .. Index_Base'(H) loop
631 -- Into (Indices, J) := Expr;
634 -- Otherwise we call Build_Code recursively.
635 -- As an optimization if the loop covers 3 or less scalar elements we
636 -- generate a sequence of assignments.
638 function Gen_While (L, H : Node_Id; Expr : Node_Id) return List_Id;
639 -- Nodes L and H must be side-effect free expressions.
640 -- If the input aggregate N to Build_Loop contains no sub-aggregates,
641 -- This routine returns the while loop statement
643 -- J : Index_Base := L;
645 -- J := Index_Base'Succ (J);
646 -- Into (Indices, J) := Expr;
649 -- Otherwise we call Build_Code recursively
651 function Local_Compile_Time_Known_Value (E : Node_Id) return Boolean;
652 function Local_Expr_Value (E : Node_Id) return Uint;
653 -- These two Local routines are used to replace the corresponding ones
654 -- in sem_eval because while processing the bounds of an aggregate with
655 -- discrete choices whose index type is an enumeration, we build static
656 -- expressions not recognized by Compile_Time_Known_Value as such since
657 -- they have not yet been analyzed and resolved. All the expressions in
658 -- question are things like Index_Base_Name'Val (Const) which we can
659 -- easily recognize as being constant.
665 function Add (Val : Int; To : Node_Id) return Node_Id is
670 U_Val : constant Uint := UI_From_Int (Val);
673 -- Note: do not try to optimize the case of Val = 0, because
674 -- we need to build a new node with the proper Sloc value anyway.
676 -- First test if we can do constant folding
678 if Local_Compile_Time_Known_Value (To) then
679 U_To := Local_Expr_Value (To) + Val;
681 -- Determine if our constant is outside the range of the index.
682 -- If so return an Empty node. This empty node will be caught
683 -- by Empty_Range below.
685 if Compile_Time_Known_Value (Index_Base_L)
686 and then U_To < Expr_Value (Index_Base_L)
690 elsif Compile_Time_Known_Value (Index_Base_H)
691 and then U_To > Expr_Value (Index_Base_H)
696 Expr_Pos := Make_Integer_Literal (Loc, U_To);
697 Set_Is_Static_Expression (Expr_Pos);
699 if not Is_Enumeration_Type (Index_Base) then
702 -- If we are dealing with enumeration return
703 -- Index_Base'Val (Expr_Pos)
707 Make_Attribute_Reference
709 Prefix => Index_Base_Name,
710 Attribute_Name => Name_Val,
711 Expressions => New_List (Expr_Pos));
717 -- If we are here no constant folding possible
719 if not Is_Enumeration_Type (Index_Base) then
722 Left_Opnd => Duplicate_Subexpr (To),
723 Right_Opnd => Make_Integer_Literal (Loc, U_Val));
725 -- If we are dealing with enumeration return
726 -- Index_Base'Val (Index_Base'Pos (To) + Val)
730 Make_Attribute_Reference
732 Prefix => Index_Base_Name,
733 Attribute_Name => Name_Pos,
734 Expressions => New_List (Duplicate_Subexpr (To)));
739 Right_Opnd => Make_Integer_Literal (Loc, U_Val));
742 Make_Attribute_Reference
744 Prefix => Index_Base_Name,
745 Attribute_Name => Name_Val,
746 Expressions => New_List (Expr_Pos));
756 function Empty_Range (L, H : Node_Id) return Boolean is
757 Is_Empty : Boolean := False;
762 -- First check if L or H were already detected as overflowing the
763 -- index base range type by function Add above. If this is so Add
764 -- returns the empty node.
766 if No (L) or else No (H) then
773 -- L > H range is empty
779 -- B_L > H range must be empty
785 -- L > B_H range must be empty
789 High := Index_Base_H;
792 if Local_Compile_Time_Known_Value (Low)
793 and then Local_Compile_Time_Known_Value (High)
796 UI_Gt (Local_Expr_Value (Low), Local_Expr_Value (High));
809 function Equal (L, H : Node_Id) return Boolean is
814 elsif Local_Compile_Time_Known_Value (L)
815 and then Local_Compile_Time_Known_Value (H)
817 return UI_Eq (Local_Expr_Value (L), Local_Expr_Value (H));
827 function Gen_Assign (Ind : Node_Id; Expr : Node_Id) return List_Id is
828 L : constant List_Id := New_List;
832 New_Indices : List_Id;
833 Indexed_Comp : Node_Id;
835 Comp_Type : Entity_Id := Empty;
837 function Add_Loop_Actions (Lis : List_Id) return List_Id;
838 -- Collect insert_actions generated in the construction of a
839 -- loop, and prepend them to the sequence of assignments to
840 -- complete the eventual body of the loop.
842 ----------------------
843 -- Add_Loop_Actions --
844 ----------------------
846 function Add_Loop_Actions (Lis : List_Id) return List_Id is
850 -- Ada 2005 (AI-287): Do nothing else in case of default
851 -- initialized component.
853 if not Present (Expr) then
856 elsif Nkind (Parent (Expr)) = N_Component_Association
857 and then Present (Loop_Actions (Parent (Expr)))
859 Append_List (Lis, Loop_Actions (Parent (Expr)));
860 Res := Loop_Actions (Parent (Expr));
861 Set_Loop_Actions (Parent (Expr), No_List);
867 end Add_Loop_Actions;
869 -- Start of processing for Gen_Assign
873 New_Indices := New_List;
875 New_Indices := New_Copy_List_Tree (Indices);
878 Append_To (New_Indices, Ind);
880 if Present (Flist) then
881 F := New_Copy_Tree (Flist);
883 elsif Present (Etype (N)) and then Controlled_Type (Etype (N)) then
884 if Is_Entity_Name (Into)
885 and then Present (Scope (Entity (Into)))
887 F := Find_Final_List (Scope (Entity (Into)));
889 F := Find_Final_List (Current_Scope);
895 if Present (Next_Index (Index)) then
898 Build_Array_Aggr_Code
901 Index => Next_Index (Index),
903 Scalar_Comp => Scalar_Comp,
904 Indices => New_Indices,
908 -- If we get here then we are at a bottom-level (sub-)aggregate
912 (Make_Indexed_Component (Loc,
913 Prefix => New_Copy_Tree (Into),
914 Expressions => New_Indices));
916 Set_Assignment_OK (Indexed_Comp);
918 -- Ada 2005 (AI-287): In case of default initialized component, Expr
919 -- is not present (and therefore we also initialize Expr_Q to empty).
921 if not Present (Expr) then
923 elsif Nkind (Expr) = N_Qualified_Expression then
924 Expr_Q := Expression (Expr);
929 if Present (Etype (N))
930 and then Etype (N) /= Any_Composite
932 Comp_Type := Component_Type (Etype (N));
933 pragma Assert (Comp_Type = Ctype); -- AI-287
935 elsif Present (Next (First (New_Indices))) then
937 -- Ada 2005 (AI-287): Do nothing in case of default initialized
938 -- component because we have received the component type in
939 -- the formal parameter Ctype.
941 -- ??? Some assert pragmas have been added to check if this new
942 -- formal can be used to replace this code in all cases.
944 if Present (Expr) then
946 -- This is a multidimensional array. Recover the component
947 -- type from the outermost aggregate, because subaggregates
948 -- do not have an assigned type.
951 P : Node_Id := Parent (Expr);
954 while Present (P) loop
955 if Nkind (P) = N_Aggregate
956 and then Present (Etype (P))
958 Comp_Type := Component_Type (Etype (P));
966 pragma Assert (Comp_Type = Ctype); -- AI-287
971 -- Ada 2005 (AI-287): We only analyze the expression in case of non-
972 -- default initialized components (otherwise Expr_Q is not present).
975 and then (Nkind (Expr_Q) = N_Aggregate
976 or else Nkind (Expr_Q) = N_Extension_Aggregate)
978 -- At this stage the Expression may not have been
979 -- analyzed yet because the array aggregate code has not
980 -- been updated to use the Expansion_Delayed flag and
981 -- avoid analysis altogether to solve the same problem
982 -- (see Resolve_Aggr_Expr). So let us do the analysis of
983 -- non-array aggregates now in order to get the value of
984 -- Expansion_Delayed flag for the inner aggregate ???
986 if Present (Comp_Type) and then not Is_Array_Type (Comp_Type) then
987 Analyze_And_Resolve (Expr_Q, Comp_Type);
990 if Is_Delayed_Aggregate (Expr_Q) then
992 -- This is either a subaggregate of a multidimentional array,
993 -- or a component of an array type whose component type is
994 -- also an array. In the latter case, the expression may have
995 -- component associations that provide different bounds from
996 -- those of the component type, and sliding must occur. Instead
997 -- of decomposing the current aggregate assignment, force the
998 -- re-analysis of the assignment, so that a temporary will be
999 -- generated in the usual fashion, and sliding will take place.
1001 if Nkind (Parent (N)) = N_Assignment_Statement
1002 and then Is_Array_Type (Comp_Type)
1003 and then Present (Component_Associations (Expr_Q))
1004 and then Must_Slide (Comp_Type, Etype (Expr_Q))
1006 Set_Expansion_Delayed (Expr_Q, False);
1007 Set_Analyzed (Expr_Q, False);
1013 Expr_Q, Etype (Expr_Q), Indexed_Comp, F));
1018 -- Ada 2005 (AI-287): In case of default initialized component, call
1019 -- the initialization subprogram associated with the component type.
1021 if not Present (Expr) then
1023 if Present (Base_Init_Proc (Etype (Ctype)))
1024 or else Has_Task (Base_Type (Ctype))
1027 Build_Initialization_Call (Loc,
1028 Id_Ref => Indexed_Comp,
1030 With_Default_Init => True));
1034 -- Now generate the assignment with no associated controlled
1035 -- actions since the target of the assignment may not have
1036 -- been initialized, it is not possible to Finalize it as
1037 -- expected by normal controlled assignment. The rest of the
1038 -- controlled actions are done manually with the proper
1039 -- finalization list coming from the context.
1042 Make_OK_Assignment_Statement (Loc,
1043 Name => Indexed_Comp,
1044 Expression => New_Copy_Tree (Expr));
1046 if Present (Comp_Type) and then Controlled_Type (Comp_Type) then
1047 Set_No_Ctrl_Actions (A);
1049 -- If this is an aggregate for an array of arrays, each
1050 -- subaggregate will be expanded as well, and even with
1051 -- No_Ctrl_Actions the assignments of inner components will
1052 -- require attachment in their assignments to temporaries.
1053 -- These temporaries must be finalized for each subaggregate,
1054 -- to prevent multiple attachments of the same temporary
1055 -- location to same finalization chain (and consequently
1056 -- circular lists). To ensure that finalization takes place
1057 -- for each subaggregate we wrap the assignment in a block.
1059 if Is_Array_Type (Comp_Type)
1060 and then Nkind (Expr) = N_Aggregate
1063 Make_Block_Statement (Loc,
1064 Handled_Statement_Sequence =>
1065 Make_Handled_Sequence_Of_Statements (Loc,
1066 Statements => New_List (A)));
1072 -- Adjust the tag if tagged (because of possible view
1073 -- conversions), unless compiling for the Java VM
1074 -- where tags are implicit.
1076 if Present (Comp_Type)
1077 and then Is_Tagged_Type (Comp_Type)
1078 and then not Java_VM
1081 Make_OK_Assignment_Statement (Loc,
1083 Make_Selected_Component (Loc,
1084 Prefix => New_Copy_Tree (Indexed_Comp),
1087 (First_Tag_Component (Comp_Type), Loc)),
1090 Unchecked_Convert_To (RTE (RE_Tag),
1092 (Node (First_Elmt (Access_Disp_Table (Comp_Type))),
1098 -- Adjust and Attach the component to the proper final list
1099 -- which can be the controller of the outer record object or
1100 -- the final list associated with the scope
1102 if Present (Comp_Type) and then Controlled_Type (Comp_Type) then
1105 Ref => New_Copy_Tree (Indexed_Comp),
1108 With_Attach => Make_Integer_Literal (Loc, 1)));
1112 return Add_Loop_Actions (L);
1119 function Gen_Loop (L, H : Node_Id; Expr : Node_Id) return List_Id is
1123 -- Index_Base'(L) .. Index_Base'(H)
1125 L_Iteration_Scheme : Node_Id;
1126 -- L_J in Index_Base'(L) .. Index_Base'(H)
1129 -- The statements to execute in the loop
1131 S : constant List_Id := New_List;
1132 -- List of statements
1135 -- Copy of expression tree, used for checking purposes
1138 -- If loop bounds define an empty range return the null statement
1140 if Empty_Range (L, H) then
1141 Append_To (S, Make_Null_Statement (Loc));
1143 -- Ada 2005 (AI-287): Nothing else need to be done in case of
1144 -- default initialized component.
1146 if not Present (Expr) then
1150 -- The expression must be type-checked even though no component
1151 -- of the aggregate will have this value. This is done only for
1152 -- actual components of the array, not for subaggregates. Do
1153 -- the check on a copy, because the expression may be shared
1154 -- among several choices, some of which might be non-null.
1156 if Present (Etype (N))
1157 and then Is_Array_Type (Etype (N))
1158 and then No (Next_Index (Index))
1160 Expander_Mode_Save_And_Set (False);
1161 Tcopy := New_Copy_Tree (Expr);
1162 Set_Parent (Tcopy, N);
1163 Analyze_And_Resolve (Tcopy, Component_Type (Etype (N)));
1164 Expander_Mode_Restore;
1170 -- If loop bounds are the same then generate an assignment
1172 elsif Equal (L, H) then
1173 return Gen_Assign (New_Copy_Tree (L), Expr);
1175 -- If H - L <= 2 then generate a sequence of assignments
1176 -- when we are processing the bottom most aggregate and it contains
1177 -- scalar components.
1179 elsif No (Next_Index (Index))
1180 and then Scalar_Comp
1181 and then Local_Compile_Time_Known_Value (L)
1182 and then Local_Compile_Time_Known_Value (H)
1183 and then Local_Expr_Value (H) - Local_Expr_Value (L) <= 2
1186 Append_List_To (S, Gen_Assign (New_Copy_Tree (L), Expr));
1187 Append_List_To (S, Gen_Assign (Add (1, To => L), Expr));
1189 if Local_Expr_Value (H) - Local_Expr_Value (L) = 2 then
1190 Append_List_To (S, Gen_Assign (Add (2, To => L), Expr));
1196 -- Otherwise construct the loop, starting with the loop index L_J
1198 L_J := Make_Defining_Identifier (Loc, New_Internal_Name ('J'));
1200 -- Construct "L .. H"
1205 Low_Bound => Make_Qualified_Expression
1207 Subtype_Mark => Index_Base_Name,
1209 High_Bound => Make_Qualified_Expression
1211 Subtype_Mark => Index_Base_Name,
1214 -- Construct "for L_J in Index_Base range L .. H"
1216 L_Iteration_Scheme :=
1217 Make_Iteration_Scheme
1219 Loop_Parameter_Specification =>
1220 Make_Loop_Parameter_Specification
1222 Defining_Identifier => L_J,
1223 Discrete_Subtype_Definition => L_Range));
1225 -- Construct the statements to execute in the loop body
1227 L_Body := Gen_Assign (New_Reference_To (L_J, Loc), Expr);
1229 -- Construct the final loop
1231 Append_To (S, Make_Implicit_Loop_Statement
1233 Identifier => Empty,
1234 Iteration_Scheme => L_Iteration_Scheme,
1235 Statements => L_Body));
1244 -- The code built is
1246 -- W_J : Index_Base := L;
1247 -- while W_J < H loop
1248 -- W_J := Index_Base'Succ (W);
1252 function Gen_While (L, H : Node_Id; Expr : Node_Id) return List_Id is
1256 -- W_J : Base_Type := L;
1258 W_Iteration_Scheme : Node_Id;
1261 W_Index_Succ : Node_Id;
1262 -- Index_Base'Succ (J)
1264 W_Increment : Node_Id;
1265 -- W_J := Index_Base'Succ (W)
1267 W_Body : constant List_Id := New_List;
1268 -- The statements to execute in the loop
1270 S : constant List_Id := New_List;
1271 -- list of statement
1274 -- If loop bounds define an empty range or are equal return null
1276 if Empty_Range (L, H) or else Equal (L, H) then
1277 Append_To (S, Make_Null_Statement (Loc));
1281 -- Build the decl of W_J
1283 W_J := Make_Defining_Identifier (Loc, New_Internal_Name ('J'));
1285 Make_Object_Declaration
1287 Defining_Identifier => W_J,
1288 Object_Definition => Index_Base_Name,
1291 -- Theoretically we should do a New_Copy_Tree (L) here, but we know
1292 -- that in this particular case L is a fresh Expr generated by
1293 -- Add which we are the only ones to use.
1295 Append_To (S, W_Decl);
1297 -- Construct " while W_J < H"
1299 W_Iteration_Scheme :=
1300 Make_Iteration_Scheme
1302 Condition => Make_Op_Lt
1304 Left_Opnd => New_Reference_To (W_J, Loc),
1305 Right_Opnd => New_Copy_Tree (H)));
1307 -- Construct the statements to execute in the loop body
1310 Make_Attribute_Reference
1312 Prefix => Index_Base_Name,
1313 Attribute_Name => Name_Succ,
1314 Expressions => New_List (New_Reference_To (W_J, Loc)));
1317 Make_OK_Assignment_Statement
1319 Name => New_Reference_To (W_J, Loc),
1320 Expression => W_Index_Succ);
1322 Append_To (W_Body, W_Increment);
1323 Append_List_To (W_Body,
1324 Gen_Assign (New_Reference_To (W_J, Loc), Expr));
1326 -- Construct the final loop
1328 Append_To (S, Make_Implicit_Loop_Statement
1330 Identifier => Empty,
1331 Iteration_Scheme => W_Iteration_Scheme,
1332 Statements => W_Body));
1337 ---------------------
1338 -- Index_Base_Name --
1339 ---------------------
1341 function Index_Base_Name return Node_Id is
1343 return New_Reference_To (Index_Base, Sloc (N));
1344 end Index_Base_Name;
1346 ------------------------------------
1347 -- Local_Compile_Time_Known_Value --
1348 ------------------------------------
1350 function Local_Compile_Time_Known_Value (E : Node_Id) return Boolean is
1352 return Compile_Time_Known_Value (E)
1354 (Nkind (E) = N_Attribute_Reference
1355 and then Attribute_Name (E) = Name_Val
1356 and then Compile_Time_Known_Value (First (Expressions (E))));
1357 end Local_Compile_Time_Known_Value;
1359 ----------------------
1360 -- Local_Expr_Value --
1361 ----------------------
1363 function Local_Expr_Value (E : Node_Id) return Uint is
1365 if Compile_Time_Known_Value (E) then
1366 return Expr_Value (E);
1368 return Expr_Value (First (Expressions (E)));
1370 end Local_Expr_Value;
1372 -- Build_Array_Aggr_Code Variables
1379 Others_Expr : Node_Id := Empty;
1380 Others_Mbox_Present : Boolean := False;
1382 Aggr_L : constant Node_Id := Low_Bound (Aggregate_Bounds (N));
1383 Aggr_H : constant Node_Id := High_Bound (Aggregate_Bounds (N));
1384 -- The aggregate bounds of this specific sub-aggregate. Note that if
1385 -- the code generated by Build_Array_Aggr_Code is executed then these
1386 -- bounds are OK. Otherwise a Constraint_Error would have been raised.
1388 Aggr_Low : constant Node_Id := Duplicate_Subexpr_No_Checks (Aggr_L);
1389 Aggr_High : constant Node_Id := Duplicate_Subexpr_No_Checks (Aggr_H);
1390 -- After Duplicate_Subexpr these are side-effect free
1395 Nb_Choices : Nat := 0;
1396 Table : Case_Table_Type (1 .. Number_Of_Choices (N));
1397 -- Used to sort all the different choice values
1400 -- Number of elements in the positional aggregate
1402 New_Code : constant List_Id := New_List;
1404 -- Start of processing for Build_Array_Aggr_Code
1407 -- First before we start, a special case. if we have a bit packed
1408 -- array represented as a modular type, then clear the value to
1409 -- zero first, to ensure that unused bits are properly cleared.
1414 and then Is_Bit_Packed_Array (Typ)
1415 and then Is_Modular_Integer_Type (Packed_Array_Type (Typ))
1417 Append_To (New_Code,
1418 Make_Assignment_Statement (Loc,
1419 Name => New_Copy_Tree (Into),
1421 Unchecked_Convert_To (Typ,
1422 Make_Integer_Literal (Loc, Uint_0))));
1426 -- STEP 1: Process component associations
1427 -- For those associations that may generate a loop, initialize
1428 -- Loop_Actions to collect inserted actions that may be crated.
1430 if No (Expressions (N)) then
1432 -- STEP 1 (a): Sort the discrete choices
1434 Assoc := First (Component_Associations (N));
1435 while Present (Assoc) loop
1436 Choice := First (Choices (Assoc));
1437 while Present (Choice) loop
1438 if Nkind (Choice) = N_Others_Choice then
1439 Set_Loop_Actions (Assoc, New_List);
1441 if Box_Present (Assoc) then
1442 Others_Mbox_Present := True;
1444 Others_Expr := Expression (Assoc);
1449 Get_Index_Bounds (Choice, Low, High);
1452 Set_Loop_Actions (Assoc, New_List);
1455 Nb_Choices := Nb_Choices + 1;
1456 if Box_Present (Assoc) then
1457 Table (Nb_Choices) := (Choice_Lo => Low,
1459 Choice_Node => Empty);
1461 Table (Nb_Choices) := (Choice_Lo => Low,
1463 Choice_Node => Expression (Assoc));
1471 -- If there is more than one set of choices these must be static
1472 -- and we can therefore sort them. Remember that Nb_Choices does not
1473 -- account for an others choice.
1475 if Nb_Choices > 1 then
1476 Sort_Case_Table (Table);
1479 -- STEP 1 (b): take care of the whole set of discrete choices
1481 for J in 1 .. Nb_Choices loop
1482 Low := Table (J).Choice_Lo;
1483 High := Table (J).Choice_Hi;
1484 Expr := Table (J).Choice_Node;
1485 Append_List (Gen_Loop (Low, High, Expr), To => New_Code);
1488 -- STEP 1 (c): generate the remaining loops to cover others choice
1489 -- We don't need to generate loops over empty gaps, but if there is
1490 -- a single empty range we must analyze the expression for semantics
1492 if Present (Others_Expr) or else Others_Mbox_Present then
1494 First : Boolean := True;
1497 for J in 0 .. Nb_Choices loop
1501 Low := Add (1, To => Table (J).Choice_Hi);
1504 if J = Nb_Choices then
1507 High := Add (-1, To => Table (J + 1).Choice_Lo);
1510 -- If this is an expansion within an init proc, make
1511 -- sure that discriminant references are replaced by
1512 -- the corresponding discriminal.
1514 if Inside_Init_Proc then
1515 if Is_Entity_Name (Low)
1516 and then Ekind (Entity (Low)) = E_Discriminant
1518 Set_Entity (Low, Discriminal (Entity (Low)));
1521 if Is_Entity_Name (High)
1522 and then Ekind (Entity (High)) = E_Discriminant
1524 Set_Entity (High, Discriminal (Entity (High)));
1529 or else not Empty_Range (Low, High)
1533 (Gen_Loop (Low, High, Others_Expr), To => New_Code);
1539 -- STEP 2: Process positional components
1542 -- STEP 2 (a): Generate the assignments for each positional element
1543 -- Note that here we have to use Aggr_L rather than Aggr_Low because
1544 -- Aggr_L is analyzed and Add wants an analyzed expression.
1546 Expr := First (Expressions (N));
1549 while Present (Expr) loop
1550 Nb_Elements := Nb_Elements + 1;
1551 Append_List (Gen_Assign (Add (Nb_Elements, To => Aggr_L), Expr),
1556 -- STEP 2 (b): Generate final loop if an others choice is present
1557 -- Here Nb_Elements gives the offset of the last positional element.
1559 if Present (Component_Associations (N)) then
1560 Assoc := Last (Component_Associations (N));
1562 -- Ada 2005 (AI-287)
1564 if Box_Present (Assoc) then
1565 Append_List (Gen_While (Add (Nb_Elements, To => Aggr_L),
1570 Expr := Expression (Assoc);
1572 Append_List (Gen_While (Add (Nb_Elements, To => Aggr_L),
1581 end Build_Array_Aggr_Code;
1583 ----------------------------
1584 -- Build_Record_Aggr_Code --
1585 ----------------------------
1587 function Build_Record_Aggr_Code
1591 Flist : Node_Id := Empty;
1592 Obj : Entity_Id := Empty;
1593 Is_Limited_Ancestor_Expansion : Boolean := False) return List_Id
1595 Loc : constant Source_Ptr := Sloc (N);
1596 L : constant List_Id := New_List;
1597 N_Typ : constant Entity_Id := Etype (N);
1603 Comp_Type : Entity_Id;
1604 Selector : Entity_Id;
1605 Comp_Expr : Node_Id;
1608 Internal_Final_List : Node_Id;
1610 -- If this is an internal aggregate, the External_Final_List is an
1611 -- expression for the controller record of the enclosing type.
1612 -- If the current aggregate has several controlled components, this
1613 -- expression will appear in several calls to attach to the finali-
1614 -- zation list, and it must not be shared.
1616 External_Final_List : Node_Id;
1617 Ancestor_Is_Expression : Boolean := False;
1618 Ancestor_Is_Subtype_Mark : Boolean := False;
1620 Init_Typ : Entity_Id := Empty;
1622 Ctrl_Stuff_Done : Boolean := False;
1624 function Get_Constraint_Association (T : Entity_Id) return Node_Id;
1625 -- Returns the first discriminant association in the constraint
1626 -- associated with T, if any, otherwise returns Empty.
1628 function Ancestor_Discriminant_Value (Disc : Entity_Id) return Node_Id;
1629 -- Returns the value that the given discriminant of an ancestor
1630 -- type should receive (in the absence of a conflict with the
1631 -- value provided by an ancestor part of an extension aggregate).
1633 procedure Check_Ancestor_Discriminants (Anc_Typ : Entity_Id);
1634 -- Check that each of the discriminant values defined by the
1635 -- ancestor part of an extension aggregate match the corresponding
1636 -- values provided by either an association of the aggregate or
1637 -- by the constraint imposed by a parent type (RM95-4.3.2(8)).
1639 function Init_Controller
1644 Init_Pr : Boolean) return List_Id;
1645 -- returns the list of statements necessary to initialize the internal
1646 -- controller of the (possible) ancestor typ into target and attach
1647 -- it to finalization list F. Init_Pr conditions the call to the
1648 -- init proc since it may already be done due to ancestor initialization
1650 procedure Gen_Ctrl_Actions_For_Aggr;
1651 -- Deal with the various controlled type data structure
1654 ---------------------------------
1655 -- Ancestor_Discriminant_Value --
1656 ---------------------------------
1658 function Ancestor_Discriminant_Value (Disc : Entity_Id) return Node_Id is
1660 Assoc_Elmt : Elmt_Id;
1661 Aggr_Comp : Entity_Id;
1662 Corresp_Disc : Entity_Id;
1663 Current_Typ : Entity_Id := Base_Type (Typ);
1664 Parent_Typ : Entity_Id;
1665 Parent_Disc : Entity_Id;
1666 Save_Assoc : Node_Id := Empty;
1669 -- First check any discriminant associations to see if
1670 -- any of them provide a value for the discriminant.
1672 if Present (Discriminant_Specifications (Parent (Current_Typ))) then
1673 Assoc := First (Component_Associations (N));
1674 while Present (Assoc) loop
1675 Aggr_Comp := Entity (First (Choices (Assoc)));
1677 if Ekind (Aggr_Comp) = E_Discriminant then
1678 Save_Assoc := Expression (Assoc);
1680 Corresp_Disc := Corresponding_Discriminant (Aggr_Comp);
1681 while Present (Corresp_Disc) loop
1682 -- If found a corresponding discriminant then return
1683 -- the value given in the aggregate. (Note: this is
1684 -- not correct in the presence of side effects. ???)
1686 if Disc = Corresp_Disc then
1687 return Duplicate_Subexpr (Expression (Assoc));
1691 Corresponding_Discriminant (Corresp_Disc);
1699 -- No match found in aggregate, so chain up parent types to find
1700 -- a constraint that defines the value of the discriminant.
1702 Parent_Typ := Etype (Current_Typ);
1703 while Current_Typ /= Parent_Typ loop
1704 if Has_Discriminants (Parent_Typ) then
1705 Parent_Disc := First_Discriminant (Parent_Typ);
1707 -- We either get the association from the subtype indication
1708 -- of the type definition itself, or from the discriminant
1709 -- constraint associated with the type entity (which is
1710 -- preferable, but it's not always present ???)
1712 if Is_Empty_Elmt_List (
1713 Discriminant_Constraint (Current_Typ))
1715 Assoc := Get_Constraint_Association (Current_Typ);
1716 Assoc_Elmt := No_Elmt;
1719 First_Elmt (Discriminant_Constraint (Current_Typ));
1720 Assoc := Node (Assoc_Elmt);
1723 -- Traverse the discriminants of the parent type looking
1724 -- for one that corresponds.
1726 while Present (Parent_Disc) and then Present (Assoc) loop
1727 Corresp_Disc := Parent_Disc;
1728 while Present (Corresp_Disc)
1729 and then Disc /= Corresp_Disc
1732 Corresponding_Discriminant (Corresp_Disc);
1735 if Disc = Corresp_Disc then
1736 if Nkind (Assoc) = N_Discriminant_Association then
1737 Assoc := Expression (Assoc);
1740 -- If the located association directly denotes
1741 -- a discriminant, then use the value of a saved
1742 -- association of the aggregate. This is a kludge
1743 -- to handle certain cases involving multiple
1744 -- discriminants mapped to a single discriminant
1745 -- of a descendant. It's not clear how to locate the
1746 -- appropriate discriminant value for such cases. ???
1748 if Is_Entity_Name (Assoc)
1749 and then Ekind (Entity (Assoc)) = E_Discriminant
1751 Assoc := Save_Assoc;
1754 return Duplicate_Subexpr (Assoc);
1757 Next_Discriminant (Parent_Disc);
1759 if No (Assoc_Elmt) then
1762 Next_Elmt (Assoc_Elmt);
1763 if Present (Assoc_Elmt) then
1764 Assoc := Node (Assoc_Elmt);
1772 Current_Typ := Parent_Typ;
1773 Parent_Typ := Etype (Current_Typ);
1776 -- In some cases there's no ancestor value to locate (such as
1777 -- when an ancestor part given by an expression defines the
1778 -- discriminant value).
1781 end Ancestor_Discriminant_Value;
1783 ----------------------------------
1784 -- Check_Ancestor_Discriminants --
1785 ----------------------------------
1787 procedure Check_Ancestor_Discriminants (Anc_Typ : Entity_Id) is
1788 Discr : Entity_Id := First_Discriminant (Base_Type (Anc_Typ));
1789 Disc_Value : Node_Id;
1793 while Present (Discr) loop
1794 Disc_Value := Ancestor_Discriminant_Value (Discr);
1796 if Present (Disc_Value) then
1797 Cond := Make_Op_Ne (Loc,
1799 Make_Selected_Component (Loc,
1800 Prefix => New_Copy_Tree (Target),
1801 Selector_Name => New_Occurrence_Of (Discr, Loc)),
1802 Right_Opnd => Disc_Value);
1805 Make_Raise_Constraint_Error (Loc,
1807 Reason => CE_Discriminant_Check_Failed));
1810 Next_Discriminant (Discr);
1812 end Check_Ancestor_Discriminants;
1814 --------------------------------
1815 -- Get_Constraint_Association --
1816 --------------------------------
1818 function Get_Constraint_Association (T : Entity_Id) return Node_Id is
1819 Typ_Def : constant Node_Id := Type_Definition (Parent (T));
1820 Indic : constant Node_Id := Subtype_Indication (Typ_Def);
1823 -- ??? Also need to cover case of a type mark denoting a subtype
1826 if Nkind (Indic) = N_Subtype_Indication
1827 and then Present (Constraint (Indic))
1829 return First (Constraints (Constraint (Indic)));
1833 end Get_Constraint_Association;
1835 ---------------------
1836 -- Init_controller --
1837 ---------------------
1839 function Init_Controller
1844 Init_Pr : Boolean) return List_Id
1846 L : constant List_Id := New_List;
1852 -- init-proc (target._controller);
1853 -- initialize (target._controller);
1854 -- Attach_to_Final_List (target._controller, F);
1857 Make_Selected_Component (Loc,
1858 Prefix => Convert_To (Typ, New_Copy_Tree (Target)),
1859 Selector_Name => Make_Identifier (Loc, Name_uController));
1860 Set_Assignment_OK (Ref);
1862 -- Ada 2005 (AI-287): Give support to default initialization of
1863 -- limited types and components.
1865 if (Nkind (Target) = N_Identifier
1866 and then Present (Etype (Target))
1867 and then Is_Limited_Type (Etype (Target)))
1869 (Nkind (Target) = N_Selected_Component
1870 and then Present (Etype (Selector_Name (Target)))
1871 and then Is_Limited_Type (Etype (Selector_Name (Target))))
1873 (Nkind (Target) = N_Unchecked_Type_Conversion
1874 and then Present (Etype (Target))
1875 and then Is_Limited_Type (Etype (Target)))
1877 (Nkind (Target) = N_Unchecked_Expression
1878 and then Nkind (Expression (Target)) = N_Indexed_Component
1879 and then Present (Etype (Prefix (Expression (Target))))
1880 and then Is_Limited_Type (Etype (Prefix (Expression (Target)))))
1882 RC := RE_Limited_Record_Controller;
1884 RC := RE_Record_Controller;
1889 Build_Initialization_Call (Loc,
1892 In_Init_Proc => Within_Init_Proc));
1896 Make_Procedure_Call_Statement (Loc,
1899 Find_Prim_Op (RTE (RC), Name_Initialize), Loc),
1900 Parameter_Associations =>
1901 New_List (New_Copy_Tree (Ref))));
1905 Obj_Ref => New_Copy_Tree (Ref),
1907 With_Attach => Attach));
1910 end Init_Controller;
1912 -------------------------------
1913 -- Gen_Ctrl_Actions_For_Aggr --
1914 -------------------------------
1916 procedure Gen_Ctrl_Actions_For_Aggr is
1919 and then Finalize_Storage_Only (Typ)
1920 and then (Is_Library_Level_Entity (Obj)
1921 or else Entity (Constant_Value (RTE (RE_Garbage_Collected))) =
1924 Attach := Make_Integer_Literal (Loc, 0);
1926 elsif Nkind (Parent (N)) = N_Qualified_Expression
1927 and then Nkind (Parent (Parent (N))) = N_Allocator
1929 Attach := Make_Integer_Literal (Loc, 2);
1932 Attach := Make_Integer_Literal (Loc, 1);
1935 -- Determine the external finalization list. It is either the
1936 -- finalization list of the outer-scope or the one coming from
1937 -- an outer aggregate. When the target is not a temporary, the
1938 -- proper scope is the scope of the target rather than the
1939 -- potentially transient current scope.
1941 if Controlled_Type (Typ) then
1942 if Present (Flist) then
1943 External_Final_List := New_Copy_Tree (Flist);
1945 elsif Is_Entity_Name (Target)
1946 and then Present (Scope (Entity (Target)))
1949 := Find_Final_List (Scope (Entity (Target)));
1952 External_Final_List := Find_Final_List (Current_Scope);
1956 External_Final_List := Empty;
1959 -- Initialize and attach the outer object in the is_controlled case
1961 if Is_Controlled (Typ) then
1962 if Ancestor_Is_Subtype_Mark then
1963 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
1964 Set_Assignment_OK (Ref);
1966 Make_Procedure_Call_Statement (Loc,
1969 (Find_Prim_Op (Init_Typ, Name_Initialize), Loc),
1970 Parameter_Associations => New_List (New_Copy_Tree (Ref))));
1973 if not Has_Controlled_Component (Typ) then
1974 Ref := New_Copy_Tree (Target);
1975 Set_Assignment_OK (Ref);
1979 Flist_Ref => New_Copy_Tree (External_Final_List),
1980 With_Attach => Attach));
1984 -- In the Has_Controlled component case, all the intermediate
1985 -- controllers must be initialized
1987 if Has_Controlled_Component (Typ)
1988 and not Is_Limited_Ancestor_Expansion
1991 Inner_Typ : Entity_Id;
1992 Outer_Typ : Entity_Id;
1997 Outer_Typ := Base_Type (Typ);
1999 -- Find outer type with a controller
2001 while Outer_Typ /= Init_Typ
2002 and then not Has_New_Controlled_Component (Outer_Typ)
2004 Outer_Typ := Etype (Outer_Typ);
2007 -- Attach it to the outer record controller to the
2008 -- external final list
2010 if Outer_Typ = Init_Typ then
2015 F => External_Final_List,
2020 Inner_Typ := Init_Typ;
2027 F => External_Final_List,
2031 Inner_Typ := Etype (Outer_Typ);
2033 not Is_Tagged_Type (Typ) or else Inner_Typ = Outer_Typ;
2036 -- The outer object has to be attached as well
2038 if Is_Controlled (Typ) then
2039 Ref := New_Copy_Tree (Target);
2040 Set_Assignment_OK (Ref);
2044 Flist_Ref => New_Copy_Tree (External_Final_List),
2045 With_Attach => New_Copy_Tree (Attach)));
2048 -- Initialize the internal controllers for tagged types with
2049 -- more than one controller.
2051 while not At_Root and then Inner_Typ /= Init_Typ loop
2052 if Has_New_Controlled_Component (Inner_Typ) then
2054 Make_Selected_Component (Loc,
2056 Convert_To (Outer_Typ, New_Copy_Tree (Target)),
2058 Make_Identifier (Loc, Name_uController));
2060 Make_Selected_Component (Loc,
2062 Selector_Name => Make_Identifier (Loc, Name_F));
2069 Attach => Make_Integer_Literal (Loc, 1),
2071 Outer_Typ := Inner_Typ;
2076 At_Root := Inner_Typ = Etype (Inner_Typ);
2077 Inner_Typ := Etype (Inner_Typ);
2080 -- If not done yet attach the controller of the ancestor part
2082 if Outer_Typ /= Init_Typ
2083 and then Inner_Typ = Init_Typ
2084 and then Has_Controlled_Component (Init_Typ)
2087 Make_Selected_Component (Loc,
2088 Prefix => Convert_To (Outer_Typ, New_Copy_Tree (Target)),
2090 Make_Identifier (Loc, Name_uController));
2092 Make_Selected_Component (Loc,
2094 Selector_Name => Make_Identifier (Loc, Name_F));
2096 Attach := Make_Integer_Literal (Loc, 1);
2103 Init_Pr => Ancestor_Is_Expression));
2107 end Gen_Ctrl_Actions_For_Aggr;
2109 -- Start of processing for Build_Record_Aggr_Code
2112 -- Deal with the ancestor part of extension aggregates
2113 -- or with the discriminants of the root type
2115 if Nkind (N) = N_Extension_Aggregate then
2117 A : constant Node_Id := Ancestor_Part (N);
2121 -- If the ancestor part is a subtype mark "T", we generate
2123 -- init-proc (T(tmp)); if T is constrained and
2124 -- init-proc (S(tmp)); where S applies an appropriate
2125 -- constraint if T is unconstrained
2127 if Is_Entity_Name (A) and then Is_Type (Entity (A)) then
2128 Ancestor_Is_Subtype_Mark := True;
2130 if Is_Constrained (Entity (A)) then
2131 Init_Typ := Entity (A);
2133 -- For an ancestor part given by an unconstrained type
2134 -- mark, create a subtype constrained by appropriate
2135 -- corresponding discriminant values coming from either
2136 -- associations of the aggregate or a constraint on
2137 -- a parent type. The subtype will be used to generate
2138 -- the correct default value for the ancestor part.
2140 elsif Has_Discriminants (Entity (A)) then
2142 Anc_Typ : constant Entity_Id := Entity (A);
2143 Anc_Constr : constant List_Id := New_List;
2144 Discrim : Entity_Id;
2145 Disc_Value : Node_Id;
2146 New_Indic : Node_Id;
2147 Subt_Decl : Node_Id;
2150 Discrim := First_Discriminant (Anc_Typ);
2151 while Present (Discrim) loop
2152 Disc_Value := Ancestor_Discriminant_Value (Discrim);
2153 Append_To (Anc_Constr, Disc_Value);
2154 Next_Discriminant (Discrim);
2158 Make_Subtype_Indication (Loc,
2159 Subtype_Mark => New_Occurrence_Of (Anc_Typ, Loc),
2161 Make_Index_Or_Discriminant_Constraint (Loc,
2162 Constraints => Anc_Constr));
2164 Init_Typ := Create_Itype (Ekind (Anc_Typ), N);
2167 Make_Subtype_Declaration (Loc,
2168 Defining_Identifier => Init_Typ,
2169 Subtype_Indication => New_Indic);
2171 -- Itypes must be analyzed with checks off
2172 -- Declaration must have a parent for proper
2173 -- handling of subsidiary actions.
2175 Set_Parent (Subt_Decl, N);
2176 Analyze (Subt_Decl, Suppress => All_Checks);
2180 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
2181 Set_Assignment_OK (Ref);
2183 if Has_Default_Init_Comps (N)
2184 or else Has_Task (Base_Type (Init_Typ))
2187 Build_Initialization_Call (Loc,
2190 In_Init_Proc => Within_Init_Proc,
2191 With_Default_Init => True));
2194 Build_Initialization_Call (Loc,
2197 In_Init_Proc => Within_Init_Proc));
2200 if Is_Constrained (Entity (A))
2201 and then Has_Discriminants (Entity (A))
2203 Check_Ancestor_Discriminants (Entity (A));
2206 -- Ada 2005 (AI-287): If the ancestor part is a limited type,
2207 -- a recursive call expands the ancestor.
2209 elsif Is_Limited_Type (Etype (A)) then
2210 Ancestor_Is_Expression := True;
2213 Build_Record_Aggr_Code (
2214 N => Expression (A),
2215 Typ => Etype (Expression (A)),
2219 Is_Limited_Ancestor_Expansion => True));
2221 -- If the ancestor part is an expression "E", we generate
2225 Ancestor_Is_Expression := True;
2226 Init_Typ := Etype (A);
2228 -- If the ancestor part is an aggregate, force its full
2229 -- expansion, which was delayed.
2231 if Nkind (A) = N_Qualified_Expression
2232 and then (Nkind (Expression (A)) = N_Aggregate
2234 Nkind (Expression (A)) = N_Extension_Aggregate)
2236 Set_Analyzed (A, False);
2237 Set_Analyzed (Expression (A), False);
2240 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
2241 Set_Assignment_OK (Ref);
2243 -- Make the assignment without usual controlled actions since
2244 -- we only want the post adjust but not the pre finalize here
2245 -- Add manual adjust when necessary
2247 Assign := New_List (
2248 Make_OK_Assignment_Statement (Loc,
2251 Set_No_Ctrl_Actions (First (Assign));
2253 -- Assign the tag now to make sure that the dispatching call in
2254 -- the subsequent deep_adjust works properly (unless Java_VM,
2255 -- where tags are implicit).
2259 Make_OK_Assignment_Statement (Loc,
2261 Make_Selected_Component (Loc,
2262 Prefix => New_Copy_Tree (Target),
2265 (First_Tag_Component (Base_Type (Typ)), Loc)),
2268 Unchecked_Convert_To (RTE (RE_Tag),
2271 (Access_Disp_Table (Base_Type (Typ)))),
2274 Set_Assignment_OK (Name (Instr));
2275 Append_To (Assign, Instr);
2278 -- Call Adjust manually
2280 if Controlled_Type (Etype (A)) then
2281 Append_List_To (Assign,
2283 Ref => New_Copy_Tree (Ref),
2285 Flist_Ref => New_Reference_To (
2286 RTE (RE_Global_Final_List), Loc),
2287 With_Attach => Make_Integer_Literal (Loc, 0)));
2291 Make_Unsuppress_Block (Loc, Name_Discriminant_Check, Assign));
2293 if Has_Discriminants (Init_Typ) then
2294 Check_Ancestor_Discriminants (Init_Typ);
2299 -- Normal case (not an extension aggregate)
2302 -- Generate the discriminant expressions, component by component.
2303 -- If the base type is an unchecked union, the discriminants are
2304 -- unknown to the back-end and absent from a value of the type, so
2305 -- assignments for them are not emitted.
2307 if Has_Discriminants (Typ)
2308 and then not Is_Unchecked_Union (Base_Type (Typ))
2310 -- ??? The discriminants of the object not inherited in the type
2311 -- of the object should be initialized here
2315 -- Generate discriminant init values
2318 Discriminant : Entity_Id;
2319 Discriminant_Value : Node_Id;
2322 Discriminant := First_Stored_Discriminant (Typ);
2324 while Present (Discriminant) loop
2327 Make_Selected_Component (Loc,
2328 Prefix => New_Copy_Tree (Target),
2329 Selector_Name => New_Occurrence_Of (Discriminant, Loc));
2331 Discriminant_Value :=
2332 Get_Discriminant_Value (
2335 Discriminant_Constraint (N_Typ));
2338 Make_OK_Assignment_Statement (Loc,
2340 Expression => New_Copy_Tree (Discriminant_Value));
2342 Set_No_Ctrl_Actions (Instr);
2343 Append_To (L, Instr);
2345 Next_Stored_Discriminant (Discriminant);
2351 -- Generate the assignments, component by component
2353 -- tmp.comp1 := Expr1_From_Aggr;
2354 -- tmp.comp2 := Expr2_From_Aggr;
2357 Comp := First (Component_Associations (N));
2358 while Present (Comp) loop
2359 Selector := Entity (First (Choices (Comp)));
2361 -- Ada 2005 (AI-287): Default initialization of a limited component
2363 if Box_Present (Comp)
2364 and then Is_Limited_Type (Etype (Selector))
2366 -- Ada 2005 (AI-287): If the component type has tasks then
2367 -- generate the activation chain and master entities (except
2368 -- in case of an allocator because in that case these entities
2369 -- are generated by Build_Task_Allocate_Block_With_Init_Stmts).
2372 Ctype : constant Entity_Id := Etype (Selector);
2373 Inside_Allocator : Boolean := False;
2374 P : Node_Id := Parent (N);
2377 if Is_Task_Type (Ctype) or else Has_Task (Ctype) then
2378 while Present (P) loop
2379 if Nkind (P) = N_Allocator then
2380 Inside_Allocator := True;
2387 if not Inside_Init_Proc and not Inside_Allocator then
2388 Build_Activation_Chain_Entity (N);
2394 Build_Initialization_Call (Loc,
2395 Id_Ref => Make_Selected_Component (Loc,
2396 Prefix => New_Copy_Tree (Target),
2397 Selector_Name => New_Occurrence_Of (Selector,
2399 Typ => Etype (Selector),
2400 With_Default_Init => True));
2405 -- Prepare for component assignment
2407 if Ekind (Selector) /= E_Discriminant
2408 or else Nkind (N) = N_Extension_Aggregate
2411 -- All the discriminants have now been assigned
2412 -- This is now a good moment to initialize and attach all the
2413 -- controllers. Their position may depend on the discriminants.
2415 if Ekind (Selector) /= E_Discriminant
2416 and then not Ctrl_Stuff_Done
2418 Gen_Ctrl_Actions_For_Aggr;
2419 Ctrl_Stuff_Done := True;
2422 Comp_Type := Etype (Selector);
2424 Make_Selected_Component (Loc,
2425 Prefix => New_Copy_Tree (Target),
2426 Selector_Name => New_Occurrence_Of (Selector, Loc));
2428 if Nkind (Expression (Comp)) = N_Qualified_Expression then
2429 Expr_Q := Expression (Expression (Comp));
2431 Expr_Q := Expression (Comp);
2434 -- The controller is the one of the parent type defining
2435 -- the component (in case of inherited components).
2437 if Controlled_Type (Comp_Type) then
2438 Internal_Final_List :=
2439 Make_Selected_Component (Loc,
2440 Prefix => Convert_To (
2441 Scope (Original_Record_Component (Selector)),
2442 New_Copy_Tree (Target)),
2444 Make_Identifier (Loc, Name_uController));
2446 Internal_Final_List :=
2447 Make_Selected_Component (Loc,
2448 Prefix => Internal_Final_List,
2449 Selector_Name => Make_Identifier (Loc, Name_F));
2451 -- The internal final list can be part of a constant object
2453 Set_Assignment_OK (Internal_Final_List);
2456 Internal_Final_List := Empty;
2459 -- Now either create the assignment or generate the code for the
2460 -- inner aggregate top-down.
2462 if Is_Delayed_Aggregate (Expr_Q) then
2464 Late_Expansion (Expr_Q, Comp_Type, Comp_Expr,
2465 Internal_Final_List));
2469 Make_OK_Assignment_Statement (Loc,
2471 Expression => Expression (Comp));
2473 Set_No_Ctrl_Actions (Instr);
2474 Append_To (L, Instr);
2476 -- Adjust the tag if tagged (because of possible view
2477 -- conversions), unless compiling for the Java VM
2478 -- where tags are implicit.
2480 -- tmp.comp._tag := comp_typ'tag;
2482 if Is_Tagged_Type (Comp_Type) and then not Java_VM then
2484 Make_OK_Assignment_Statement (Loc,
2486 Make_Selected_Component (Loc,
2487 Prefix => New_Copy_Tree (Comp_Expr),
2490 (First_Tag_Component (Comp_Type), Loc)),
2493 Unchecked_Convert_To (RTE (RE_Tag),
2495 (Node (First_Elmt (Access_Disp_Table (Comp_Type))),
2498 Append_To (L, Instr);
2501 -- Adjust and Attach the component to the proper controller
2502 -- Adjust (tmp.comp);
2503 -- Attach_To_Final_List (tmp.comp,
2504 -- comp_typ (tmp)._record_controller.f)
2506 if Controlled_Type (Comp_Type) then
2509 Ref => New_Copy_Tree (Comp_Expr),
2511 Flist_Ref => Internal_Final_List,
2512 With_Attach => Make_Integer_Literal (Loc, 1)));
2518 elsif Ekind (Selector) = E_Discriminant
2519 and then Nkind (N) /= N_Extension_Aggregate
2520 and then Nkind (Parent (N)) = N_Component_Association
2521 and then Is_Constrained (Typ)
2523 -- We must check that the discriminant value imposed by the
2524 -- context is the same as the value given in the subaggregate,
2525 -- because after the expansion into assignments there is no
2526 -- record on which to perform a regular discriminant check.
2533 D_Val := First_Elmt (Discriminant_Constraint (Typ));
2534 Disc := First_Discriminant (Typ);
2536 while Chars (Disc) /= Chars (Selector) loop
2537 Next_Discriminant (Disc);
2541 pragma Assert (Present (D_Val));
2544 Make_Raise_Constraint_Error (Loc,
2547 Left_Opnd => New_Copy_Tree (Node (D_Val)),
2548 Right_Opnd => Expression (Comp)),
2549 Reason => CE_Discriminant_Check_Failed));
2558 -- If the type is tagged, the tag needs to be initialized (unless
2559 -- compiling for the Java VM where tags are implicit). It is done
2560 -- late in the initialization process because in some cases, we call
2561 -- the init proc of an ancestor which will not leave out the right tag
2563 if Ancestor_Is_Expression then
2566 elsif Is_Tagged_Type (Typ) and then not Java_VM then
2568 Make_OK_Assignment_Statement (Loc,
2570 Make_Selected_Component (Loc,
2571 Prefix => New_Copy_Tree (Target),
2574 (First_Tag_Component (Base_Type (Typ)), Loc)),
2577 Unchecked_Convert_To (RTE (RE_Tag),
2579 (Node (First_Elmt (Access_Disp_Table (Base_Type (Typ)))),
2582 Append_To (L, Instr);
2585 -- If the controllers have not been initialized yet (by lack of non-
2586 -- discriminant components), let's do it now.
2588 if not Ctrl_Stuff_Done then
2589 Gen_Ctrl_Actions_For_Aggr;
2590 Ctrl_Stuff_Done := True;
2594 end Build_Record_Aggr_Code;
2596 -------------------------------
2597 -- Convert_Aggr_In_Allocator --
2598 -------------------------------
2600 procedure Convert_Aggr_In_Allocator (Decl, Aggr : Node_Id) is
2601 Loc : constant Source_Ptr := Sloc (Aggr);
2602 Typ : constant Entity_Id := Etype (Aggr);
2603 Temp : constant Entity_Id := Defining_Identifier (Decl);
2605 Occ : constant Node_Id :=
2606 Unchecked_Convert_To (Typ,
2607 Make_Explicit_Dereference (Loc,
2608 New_Reference_To (Temp, Loc)));
2610 Access_Type : constant Entity_Id := Etype (Temp);
2613 if Is_Array_Type (Typ) then
2614 Convert_Array_Aggr_In_Allocator (Decl, Aggr, Occ);
2616 elsif Has_Default_Init_Comps (Aggr) then
2618 L : constant List_Id := New_List;
2619 Init_Stmts : List_Id;
2622 Init_Stmts := Late_Expansion (Aggr, Typ, Occ,
2623 Find_Final_List (Access_Type),
2624 Associated_Final_Chain (Base_Type (Access_Type)));
2626 Build_Task_Allocate_Block_With_Init_Stmts (L, Aggr, Init_Stmts);
2627 Insert_Actions_After (Decl, L);
2631 Insert_Actions_After (Decl,
2632 Late_Expansion (Aggr, Typ, Occ,
2633 Find_Final_List (Access_Type),
2634 Associated_Final_Chain (Base_Type (Access_Type))));
2636 end Convert_Aggr_In_Allocator;
2638 --------------------------------
2639 -- Convert_Aggr_In_Assignment --
2640 --------------------------------
2642 procedure Convert_Aggr_In_Assignment (N : Node_Id) is
2643 Aggr : Node_Id := Expression (N);
2644 Typ : constant Entity_Id := Etype (Aggr);
2645 Occ : constant Node_Id := New_Copy_Tree (Name (N));
2648 if Nkind (Aggr) = N_Qualified_Expression then
2649 Aggr := Expression (Aggr);
2652 Insert_Actions_After (N,
2653 Late_Expansion (Aggr, Typ, Occ,
2654 Find_Final_List (Typ, New_Copy_Tree (Occ))));
2655 end Convert_Aggr_In_Assignment;
2657 ---------------------------------
2658 -- Convert_Aggr_In_Object_Decl --
2659 ---------------------------------
2661 procedure Convert_Aggr_In_Object_Decl (N : Node_Id) is
2662 Obj : constant Entity_Id := Defining_Identifier (N);
2663 Aggr : Node_Id := Expression (N);
2664 Loc : constant Source_Ptr := Sloc (Aggr);
2665 Typ : constant Entity_Id := Etype (Aggr);
2666 Occ : constant Node_Id := New_Occurrence_Of (Obj, Loc);
2668 function Discriminants_Ok return Boolean;
2669 -- If the object type is constrained, the discriminants in the
2670 -- aggregate must be checked against the discriminants of the subtype.
2671 -- This cannot be done using Apply_Discriminant_Checks because after
2672 -- expansion there is no aggregate left to check.
2674 ----------------------
2675 -- Discriminants_Ok --
2676 ----------------------
2678 function Discriminants_Ok return Boolean is
2679 Cond : Node_Id := Empty;
2688 D := First_Discriminant (Typ);
2689 Disc1 := First_Elmt (Discriminant_Constraint (Typ));
2690 Disc2 := First_Elmt (Discriminant_Constraint (Etype (Obj)));
2692 while Present (Disc1) and then Present (Disc2) loop
2693 Val1 := Node (Disc1);
2694 Val2 := Node (Disc2);
2696 if not Is_OK_Static_Expression (Val1)
2697 or else not Is_OK_Static_Expression (Val2)
2699 Check := Make_Op_Ne (Loc,
2700 Left_Opnd => Duplicate_Subexpr (Val1),
2701 Right_Opnd => Duplicate_Subexpr (Val2));
2707 Cond := Make_Or_Else (Loc,
2709 Right_Opnd => Check);
2712 elsif Expr_Value (Val1) /= Expr_Value (Val2) then
2713 Apply_Compile_Time_Constraint_Error (Aggr,
2714 Msg => "incorrect value for discriminant&?",
2715 Reason => CE_Discriminant_Check_Failed,
2720 Next_Discriminant (D);
2725 -- If any discriminant constraint is non-static, emit a check
2727 if Present (Cond) then
2729 Make_Raise_Constraint_Error (Loc,
2731 Reason => CE_Discriminant_Check_Failed));
2735 end Discriminants_Ok;
2737 -- Start of processing for Convert_Aggr_In_Object_Decl
2740 Set_Assignment_OK (Occ);
2742 if Nkind (Aggr) = N_Qualified_Expression then
2743 Aggr := Expression (Aggr);
2746 if Has_Discriminants (Typ)
2747 and then Typ /= Etype (Obj)
2748 and then Is_Constrained (Etype (Obj))
2749 and then not Discriminants_Ok
2754 if Requires_Transient_Scope (Typ) then
2755 Establish_Transient_Scope (Aggr, Sec_Stack =>
2756 Is_Controlled (Typ) or else Has_Controlled_Component (Typ));
2759 Insert_Actions_After (N, Late_Expansion (Aggr, Typ, Occ, Obj => Obj));
2760 Set_No_Initialization (N);
2761 Initialize_Discriminants (N, Typ);
2762 end Convert_Aggr_In_Object_Decl;
2764 -------------------------------------
2765 -- Convert_array_Aggr_In_Allocator --
2766 -------------------------------------
2768 procedure Convert_Array_Aggr_In_Allocator
2773 Aggr_Code : List_Id;
2774 Typ : constant Entity_Id := Etype (Aggr);
2775 Ctyp : constant Entity_Id := Component_Type (Typ);
2778 -- The target is an explicit dereference of the allocated object.
2779 -- Generate component assignments to it, as for an aggregate that
2780 -- appears on the right-hand side of an assignment statement.
2783 Build_Array_Aggr_Code (Aggr,
2785 Index => First_Index (Typ),
2787 Scalar_Comp => Is_Scalar_Type (Ctyp));
2789 Insert_Actions_After (Decl, Aggr_Code);
2790 end Convert_Array_Aggr_In_Allocator;
2792 ----------------------------
2793 -- Convert_To_Assignments --
2794 ----------------------------
2796 procedure Convert_To_Assignments (N : Node_Id; Typ : Entity_Id) is
2797 Loc : constant Source_Ptr := Sloc (N);
2801 Target_Expr : Node_Id;
2802 Parent_Kind : Node_Kind;
2803 Unc_Decl : Boolean := False;
2804 Parent_Node : Node_Id;
2807 Parent_Node := Parent (N);
2808 Parent_Kind := Nkind (Parent_Node);
2810 if Parent_Kind = N_Qualified_Expression then
2812 -- Check if we are in a unconstrained declaration because in this
2813 -- case the current delayed expansion mechanism doesn't work when
2814 -- the declared object size depend on the initializing expr.
2817 Parent_Node := Parent (Parent_Node);
2818 Parent_Kind := Nkind (Parent_Node);
2820 if Parent_Kind = N_Object_Declaration then
2822 not Is_Entity_Name (Object_Definition (Parent_Node))
2823 or else Has_Discriminants
2824 (Entity (Object_Definition (Parent_Node)))
2825 or else Is_Class_Wide_Type
2826 (Entity (Object_Definition (Parent_Node)));
2831 -- Just set the Delay flag in the following cases where the
2832 -- transformation will be done top down from above
2834 -- - internal aggregate (transformed when expanding the parent)
2835 -- - allocators (see Convert_Aggr_In_Allocator)
2836 -- - object decl (see Convert_Aggr_In_Object_Decl)
2837 -- - safe assignments (see Convert_Aggr_Assignments)
2838 -- so far only the assignments in the init procs are taken
2841 if Parent_Kind = N_Aggregate
2842 or else Parent_Kind = N_Extension_Aggregate
2843 or else Parent_Kind = N_Component_Association
2844 or else Parent_Kind = N_Allocator
2845 or else (Parent_Kind = N_Object_Declaration and then not Unc_Decl)
2846 or else (Parent_Kind = N_Assignment_Statement
2847 and then Inside_Init_Proc)
2849 Set_Expansion_Delayed (N);
2853 if Requires_Transient_Scope (Typ) then
2854 Establish_Transient_Scope (N, Sec_Stack =>
2855 Is_Controlled (Typ) or else Has_Controlled_Component (Typ));
2858 -- Create the temporary
2860 Temp := Make_Defining_Identifier (Loc, New_Internal_Name ('A'));
2863 Make_Object_Declaration (Loc,
2864 Defining_Identifier => Temp,
2865 Object_Definition => New_Occurrence_Of (Typ, Loc));
2867 Set_No_Initialization (Instr);
2868 Insert_Action (N, Instr);
2869 Initialize_Discriminants (Instr, Typ);
2870 Target_Expr := New_Occurrence_Of (Temp, Loc);
2872 Insert_Actions (N, Build_Record_Aggr_Code (N, Typ, Target_Expr));
2873 Rewrite (N, New_Occurrence_Of (Temp, Loc));
2874 Analyze_And_Resolve (N, Typ);
2875 end Convert_To_Assignments;
2877 ---------------------------
2878 -- Convert_To_Positional --
2879 ---------------------------
2881 procedure Convert_To_Positional
2883 Max_Others_Replicate : Nat := 5;
2884 Handle_Bit_Packed : Boolean := False)
2886 Typ : constant Entity_Id := Etype (N);
2891 Ixb : Node_Id) return Boolean;
2892 -- Convert the aggregate into a purely positional form if possible.
2893 -- On entry the bounds of all dimensions are known to be static,
2894 -- and the total number of components is safe enough to expand.
2896 function Is_Flat (N : Node_Id; Dims : Int) return Boolean;
2897 -- Return True iff the array N is flat (which is not rivial
2898 -- in the case of multidimensionsl aggregates).
2907 Ixb : Node_Id) return Boolean
2909 Loc : constant Source_Ptr := Sloc (N);
2910 Blo : constant Node_Id := Type_Low_Bound (Etype (Ixb));
2911 Lo : constant Node_Id := Type_Low_Bound (Etype (Ix));
2912 Hi : constant Node_Id := Type_High_Bound (Etype (Ix));
2917 if Nkind (Original_Node (N)) = N_String_Literal then
2921 -- Only handle bounds starting at the base type low bound
2922 -- for now since the compiler isn't able to handle different low
2923 -- bounds yet. Case such as new String'(3..5 => ' ') will get
2924 -- the wrong bounds, though it seems that the aggregate should
2925 -- retain the bounds set on its Etype (see C64103E and CC1311B).
2927 Lov := Expr_Value (Lo);
2928 Hiv := Expr_Value (Hi);
2931 or else not Compile_Time_Known_Value (Blo)
2932 or else (Lov /= Expr_Value (Blo))
2937 -- Determine if set of alternatives is suitable for conversion
2938 -- and build an array containing the values in sequence.
2941 Vals : array (UI_To_Int (Lov) .. UI_To_Int (Hiv))
2942 of Node_Id := (others => Empty);
2943 -- The values in the aggregate sorted appropriately
2946 -- Same data as Vals in list form
2949 -- Used to validate Max_Others_Replicate limit
2952 Num : Int := UI_To_Int (Lov);
2957 if Present (Expressions (N)) then
2958 Elmt := First (Expressions (N));
2960 while Present (Elmt) loop
2961 if Nkind (Elmt) = N_Aggregate
2962 and then Present (Next_Index (Ix))
2964 not Flatten (Elmt, Next_Index (Ix), Next_Index (Ixb))
2969 Vals (Num) := Relocate_Node (Elmt);
2976 if No (Component_Associations (N)) then
2980 Elmt := First (Component_Associations (N));
2982 if Nkind (Expression (Elmt)) = N_Aggregate then
2983 if Present (Next_Index (Ix))
2986 (Expression (Elmt), Next_Index (Ix), Next_Index (Ixb))
2992 Component_Loop : while Present (Elmt) loop
2993 Choice := First (Choices (Elmt));
2994 Choice_Loop : while Present (Choice) loop
2996 -- If we have an others choice, fill in the missing elements
2997 -- subject to the limit established by Max_Others_Replicate.
2999 if Nkind (Choice) = N_Others_Choice then
3002 for J in Vals'Range loop
3003 if No (Vals (J)) then
3004 Vals (J) := New_Copy_Tree (Expression (Elmt));
3005 Rep_Count := Rep_Count + 1;
3007 -- Check for maximum others replication. Note that
3008 -- we skip this test if either of the restrictions
3009 -- No_Elaboration_Code or No_Implicit_Loops is
3010 -- active, or if this is a preelaborable unit.
3013 P : constant Entity_Id :=
3014 Cunit_Entity (Current_Sem_Unit);
3017 if Restriction_Active (No_Elaboration_Code)
3018 or else Restriction_Active (No_Implicit_Loops)
3019 or else Is_Preelaborated (P)
3020 or else (Ekind (P) = E_Package_Body
3022 Is_Preelaborated (Spec_Entity (P)))
3026 elsif Rep_Count > Max_Others_Replicate then
3033 exit Component_Loop;
3035 -- Case of a subtype mark
3037 elsif Nkind (Choice) = N_Identifier
3038 and then Is_Type (Entity (Choice))
3040 Lo := Type_Low_Bound (Etype (Choice));
3041 Hi := Type_High_Bound (Etype (Choice));
3043 -- Case of subtype indication
3045 elsif Nkind (Choice) = N_Subtype_Indication then
3046 Lo := Low_Bound (Range_Expression (Constraint (Choice)));
3047 Hi := High_Bound (Range_Expression (Constraint (Choice)));
3051 elsif Nkind (Choice) = N_Range then
3052 Lo := Low_Bound (Choice);
3053 Hi := High_Bound (Choice);
3055 -- Normal subexpression case
3057 else pragma Assert (Nkind (Choice) in N_Subexpr);
3058 if not Compile_Time_Known_Value (Choice) then
3062 Vals (UI_To_Int (Expr_Value (Choice))) :=
3063 New_Copy_Tree (Expression (Elmt));
3068 -- Range cases merge with Lo,Hi said
3070 if not Compile_Time_Known_Value (Lo)
3072 not Compile_Time_Known_Value (Hi)
3076 for J in UI_To_Int (Expr_Value (Lo)) ..
3077 UI_To_Int (Expr_Value (Hi))
3079 Vals (J) := New_Copy_Tree (Expression (Elmt));
3085 end loop Choice_Loop;
3088 end loop Component_Loop;
3090 -- If we get here the conversion is possible
3093 for J in Vals'Range loop
3094 Append (Vals (J), Vlist);
3097 Rewrite (N, Make_Aggregate (Loc, Expressions => Vlist));
3098 Set_Aggregate_Bounds (N, Aggregate_Bounds (Original_Node (N)));
3107 function Is_Flat (N : Node_Id; Dims : Int) return Boolean is
3114 elsif Nkind (N) = N_Aggregate then
3115 if Present (Component_Associations (N)) then
3119 Elmt := First (Expressions (N));
3121 while Present (Elmt) loop
3122 if not Is_Flat (Elmt, Dims - 1) then
3136 -- Start of processing for Convert_To_Positional
3139 -- Ada 2005 (AI-287): Do not convert in case of default initialized
3140 -- components because in this case will need to call the corresponding
3143 if Has_Default_Init_Comps (N) then
3147 if Is_Flat (N, Number_Dimensions (Typ)) then
3151 if Is_Bit_Packed_Array (Typ)
3152 and then not Handle_Bit_Packed
3157 -- Do not convert to positional if controlled components are
3158 -- involved since these require special processing
3160 if Has_Controlled_Component (Typ) then
3164 if Aggr_Size_OK (Typ)
3166 Flatten (N, First_Index (Typ), First_Index (Base_Type (Typ)))
3168 Analyze_And_Resolve (N, Typ);
3170 end Convert_To_Positional;
3172 ----------------------------
3173 -- Expand_Array_Aggregate --
3174 ----------------------------
3176 -- Array aggregate expansion proceeds as follows:
3178 -- 1. If requested we generate code to perform all the array aggregate
3179 -- bound checks, specifically
3181 -- (a) Check that the index range defined by aggregate bounds is
3182 -- compatible with corresponding index subtype.
3184 -- (b) If an others choice is present check that no aggregate
3185 -- index is outside the bounds of the index constraint.
3187 -- (c) For multidimensional arrays make sure that all subaggregates
3188 -- corresponding to the same dimension have the same bounds.
3190 -- 2. Check for packed array aggregate which can be converted to a
3191 -- constant so that the aggregate disappeares completely.
3193 -- 3. Check case of nested aggregate. Generally nested aggregates are
3194 -- handled during the processing of the parent aggregate.
3196 -- 4. Check if the aggregate can be statically processed. If this is the
3197 -- case pass it as is to Gigi. Note that a necessary condition for
3198 -- static processing is that the aggregate be fully positional.
3200 -- 5. If in place aggregate expansion is possible (i.e. no need to create
3201 -- a temporary) then mark the aggregate as such and return. Otherwise
3202 -- create a new temporary and generate the appropriate initialization
3205 procedure Expand_Array_Aggregate (N : Node_Id) is
3206 Loc : constant Source_Ptr := Sloc (N);
3208 Typ : constant Entity_Id := Etype (N);
3209 Ctyp : constant Entity_Id := Component_Type (Typ);
3210 -- Typ is the correct constrained array subtype of the aggregate
3211 -- Ctyp is the corresponding component type.
3213 Aggr_Dimension : constant Pos := Number_Dimensions (Typ);
3214 -- Number of aggregate index dimensions
3216 Aggr_Low : array (1 .. Aggr_Dimension) of Node_Id;
3217 Aggr_High : array (1 .. Aggr_Dimension) of Node_Id;
3218 -- Low and High bounds of the constraint for each aggregate index
3220 Aggr_Index_Typ : array (1 .. Aggr_Dimension) of Entity_Id;
3221 -- The type of each index
3223 Maybe_In_Place_OK : Boolean;
3224 -- If the type is neither controlled nor packed and the aggregate
3225 -- is the expression in an assignment, assignment in place may be
3226 -- possible, provided other conditions are met on the LHS.
3228 Others_Present : array (1 .. Aggr_Dimension) of Boolean :=
3230 -- If Others_Present (J) is True, then there is an others choice
3231 -- in one of the sub-aggregates of N at dimension J.
3233 procedure Build_Constrained_Type (Positional : Boolean);
3234 -- If the subtype is not static or unconstrained, build a constrained
3235 -- type using the computable sizes of the aggregate and its sub-
3238 procedure Check_Bounds (Aggr_Bounds : Node_Id; Index_Bounds : Node_Id);
3239 -- Checks that the bounds of Aggr_Bounds are within the bounds defined
3242 procedure Check_Same_Aggr_Bounds (Sub_Aggr : Node_Id; Dim : Pos);
3243 -- Checks that in a multi-dimensional array aggregate all subaggregates
3244 -- corresponding to the same dimension have the same bounds.
3245 -- Sub_Aggr is an array sub-aggregate. Dim is the dimension
3246 -- corresponding to the sub-aggregate.
3248 procedure Compute_Others_Present (Sub_Aggr : Node_Id; Dim : Pos);
3249 -- Computes the values of array Others_Present. Sub_Aggr is the
3250 -- array sub-aggregate we start the computation from. Dim is the
3251 -- dimension corresponding to the sub-aggregate.
3253 function Has_Address_Clause (D : Node_Id) return Boolean;
3254 -- If the aggregate is the expression in an object declaration, it
3255 -- cannot be expanded in place. This function does a lookahead in the
3256 -- current declarative part to find an address clause for the object
3259 function In_Place_Assign_OK return Boolean;
3260 -- Simple predicate to determine whether an aggregate assignment can
3261 -- be done in place, because none of the new values can depend on the
3262 -- components of the target of the assignment.
3264 procedure Others_Check (Sub_Aggr : Node_Id; Dim : Pos);
3265 -- Checks that if an others choice is present in any sub-aggregate no
3266 -- aggregate index is outside the bounds of the index constraint.
3267 -- Sub_Aggr is an array sub-aggregate. Dim is the dimension
3268 -- corresponding to the sub-aggregate.
3270 ----------------------------
3271 -- Build_Constrained_Type --
3272 ----------------------------
3274 procedure Build_Constrained_Type (Positional : Boolean) is
3275 Loc : constant Source_Ptr := Sloc (N);
3276 Agg_Type : Entity_Id;
3279 Typ : constant Entity_Id := Etype (N);
3280 Indices : constant List_Id := New_List;
3286 Make_Defining_Identifier (
3287 Loc, New_Internal_Name ('A'));
3289 -- If the aggregate is purely positional, all its subaggregates
3290 -- have the same size. We collect the dimensions from the first
3291 -- subaggregate at each level.
3296 for D in 1 .. Number_Dimensions (Typ) loop
3297 Comp := First (Expressions (Sub_Agg));
3302 while Present (Comp) loop
3309 Low_Bound => Make_Integer_Literal (Loc, 1),
3311 Make_Integer_Literal (Loc, Num)),
3316 -- We know the aggregate type is unconstrained and the
3317 -- aggregate is not processable by the back end, therefore
3318 -- not necessarily positional. Retrieve the bounds of each
3319 -- dimension as computed earlier.
3321 for D in 1 .. Number_Dimensions (Typ) loop
3324 Low_Bound => Aggr_Low (D),
3325 High_Bound => Aggr_High (D)),
3331 Make_Full_Type_Declaration (Loc,
3332 Defining_Identifier => Agg_Type,
3334 Make_Constrained_Array_Definition (Loc,
3335 Discrete_Subtype_Definitions => Indices,
3336 Component_Definition =>
3337 Make_Component_Definition (Loc,
3338 Aliased_Present => False,
3339 Subtype_Indication =>
3340 New_Occurrence_Of (Component_Type (Typ), Loc))));
3342 Insert_Action (N, Decl);
3344 Set_Etype (N, Agg_Type);
3345 Set_Is_Itype (Agg_Type);
3346 Freeze_Itype (Agg_Type, N);
3347 end Build_Constrained_Type;
3353 procedure Check_Bounds (Aggr_Bounds : Node_Id; Index_Bounds : Node_Id) is
3360 Cond : Node_Id := Empty;
3363 Get_Index_Bounds (Aggr_Bounds, Aggr_Lo, Aggr_Hi);
3364 Get_Index_Bounds (Index_Bounds, Ind_Lo, Ind_Hi);
3366 -- Generate the following test:
3368 -- [constraint_error when
3369 -- Aggr_Lo <= Aggr_Hi and then
3370 -- (Aggr_Lo < Ind_Lo or else Aggr_Hi > Ind_Hi)]
3372 -- As an optimization try to see if some tests are trivially vacuos
3373 -- because we are comparing an expression against itself.
3375 if Aggr_Lo = Ind_Lo and then Aggr_Hi = Ind_Hi then
3378 elsif Aggr_Hi = Ind_Hi then
3381 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
3382 Right_Opnd => Duplicate_Subexpr_Move_Checks (Ind_Lo));
3384 elsif Aggr_Lo = Ind_Lo then
3387 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi),
3388 Right_Opnd => Duplicate_Subexpr_Move_Checks (Ind_Hi));
3395 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
3396 Right_Opnd => Duplicate_Subexpr_Move_Checks (Ind_Lo)),
3400 Left_Opnd => Duplicate_Subexpr (Aggr_Hi),
3401 Right_Opnd => Duplicate_Subexpr (Ind_Hi)));
3404 if Present (Cond) then
3409 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
3410 Right_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi)),
3412 Right_Opnd => Cond);
3414 Set_Analyzed (Left_Opnd (Left_Opnd (Cond)), False);
3415 Set_Analyzed (Right_Opnd (Left_Opnd (Cond)), False);
3417 Make_Raise_Constraint_Error (Loc,
3419 Reason => CE_Length_Check_Failed));
3423 ----------------------------
3424 -- Check_Same_Aggr_Bounds --
3425 ----------------------------
3427 procedure Check_Same_Aggr_Bounds (Sub_Aggr : Node_Id; Dim : Pos) is
3428 Sub_Lo : constant Node_Id := Low_Bound (Aggregate_Bounds (Sub_Aggr));
3429 Sub_Hi : constant Node_Id := High_Bound (Aggregate_Bounds (Sub_Aggr));
3430 -- The bounds of this specific sub-aggregate
3432 Aggr_Lo : constant Node_Id := Aggr_Low (Dim);
3433 Aggr_Hi : constant Node_Id := Aggr_High (Dim);
3434 -- The bounds of the aggregate for this dimension
3436 Ind_Typ : constant Entity_Id := Aggr_Index_Typ (Dim);
3437 -- The index type for this dimension.xxx
3439 Cond : Node_Id := Empty;
3445 -- If index checks are on generate the test
3447 -- [constraint_error when
3448 -- Aggr_Lo /= Sub_Lo or else Aggr_Hi /= Sub_Hi]
3450 -- As an optimization try to see if some tests are trivially vacuos
3451 -- because we are comparing an expression against itself. Also for
3452 -- the first dimension the test is trivially vacuous because there
3453 -- is just one aggregate for dimension 1.
3455 if Index_Checks_Suppressed (Ind_Typ) then
3459 or else (Aggr_Lo = Sub_Lo and then Aggr_Hi = Sub_Hi)
3463 elsif Aggr_Hi = Sub_Hi then
3466 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
3467 Right_Opnd => Duplicate_Subexpr_Move_Checks (Sub_Lo));
3469 elsif Aggr_Lo = Sub_Lo then
3472 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi),
3473 Right_Opnd => Duplicate_Subexpr_Move_Checks (Sub_Hi));
3480 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
3481 Right_Opnd => Duplicate_Subexpr_Move_Checks (Sub_Lo)),
3485 Left_Opnd => Duplicate_Subexpr (Aggr_Hi),
3486 Right_Opnd => Duplicate_Subexpr (Sub_Hi)));
3489 if Present (Cond) then
3491 Make_Raise_Constraint_Error (Loc,
3493 Reason => CE_Length_Check_Failed));
3496 -- Now look inside the sub-aggregate to see if there is more work
3498 if Dim < Aggr_Dimension then
3500 -- Process positional components
3502 if Present (Expressions (Sub_Aggr)) then
3503 Expr := First (Expressions (Sub_Aggr));
3504 while Present (Expr) loop
3505 Check_Same_Aggr_Bounds (Expr, Dim + 1);
3510 -- Process component associations
3512 if Present (Component_Associations (Sub_Aggr)) then
3513 Assoc := First (Component_Associations (Sub_Aggr));
3514 while Present (Assoc) loop
3515 Expr := Expression (Assoc);
3516 Check_Same_Aggr_Bounds (Expr, Dim + 1);
3521 end Check_Same_Aggr_Bounds;
3523 ----------------------------
3524 -- Compute_Others_Present --
3525 ----------------------------
3527 procedure Compute_Others_Present (Sub_Aggr : Node_Id; Dim : Pos) is
3532 if Present (Component_Associations (Sub_Aggr)) then
3533 Assoc := Last (Component_Associations (Sub_Aggr));
3535 if Nkind (First (Choices (Assoc))) = N_Others_Choice then
3536 Others_Present (Dim) := True;
3540 -- Now look inside the sub-aggregate to see if there is more work
3542 if Dim < Aggr_Dimension then
3544 -- Process positional components
3546 if Present (Expressions (Sub_Aggr)) then
3547 Expr := First (Expressions (Sub_Aggr));
3548 while Present (Expr) loop
3549 Compute_Others_Present (Expr, Dim + 1);
3554 -- Process component associations
3556 if Present (Component_Associations (Sub_Aggr)) then
3557 Assoc := First (Component_Associations (Sub_Aggr));
3558 while Present (Assoc) loop
3559 Expr := Expression (Assoc);
3560 Compute_Others_Present (Expr, Dim + 1);
3565 end Compute_Others_Present;
3567 ------------------------
3568 -- Has_Address_Clause --
3569 ------------------------
3571 function Has_Address_Clause (D : Node_Id) return Boolean is
3572 Id : constant Entity_Id := Defining_Identifier (D);
3573 Decl : Node_Id := Next (D);
3576 while Present (Decl) loop
3577 if Nkind (Decl) = N_At_Clause
3578 and then Chars (Identifier (Decl)) = Chars (Id)
3582 elsif Nkind (Decl) = N_Attribute_Definition_Clause
3583 and then Chars (Decl) = Name_Address
3584 and then Chars (Name (Decl)) = Chars (Id)
3593 end Has_Address_Clause;
3595 ------------------------
3596 -- In_Place_Assign_OK --
3597 ------------------------
3599 function In_Place_Assign_OK return Boolean is
3607 function Is_Others_Aggregate (Aggr : Node_Id) return Boolean;
3608 -- Aggregates that consist of a single Others choice are safe
3609 -- if the single expression is.
3611 function Safe_Aggregate (Aggr : Node_Id) return Boolean;
3612 -- Check recursively that each component of a (sub)aggregate does
3613 -- not depend on the variable being assigned to.
3615 function Safe_Component (Expr : Node_Id) return Boolean;
3616 -- Verify that an expression cannot depend on the variable being
3617 -- assigned to. Room for improvement here (but less than before).
3619 -------------------------
3620 -- Is_Others_Aggregate --
3621 -------------------------
3623 function Is_Others_Aggregate (Aggr : Node_Id) return Boolean is
3625 return No (Expressions (Aggr))
3627 (First (Choices (First (Component_Associations (Aggr)))))
3629 end Is_Others_Aggregate;
3631 --------------------
3632 -- Safe_Aggregate --
3633 --------------------
3635 function Safe_Aggregate (Aggr : Node_Id) return Boolean is
3639 if Present (Expressions (Aggr)) then
3640 Expr := First (Expressions (Aggr));
3642 while Present (Expr) loop
3643 if Nkind (Expr) = N_Aggregate then
3644 if not Safe_Aggregate (Expr) then
3648 elsif not Safe_Component (Expr) then
3656 if Present (Component_Associations (Aggr)) then
3657 Expr := First (Component_Associations (Aggr));
3659 while Present (Expr) loop
3660 if Nkind (Expression (Expr)) = N_Aggregate then
3661 if not Safe_Aggregate (Expression (Expr)) then
3665 elsif not Safe_Component (Expression (Expr)) then
3676 --------------------
3677 -- Safe_Component --
3678 --------------------
3680 function Safe_Component (Expr : Node_Id) return Boolean is
3681 Comp : Node_Id := Expr;
3683 function Check_Component (Comp : Node_Id) return Boolean;
3684 -- Do the recursive traversal, after copy
3686 ---------------------
3687 -- Check_Component --
3688 ---------------------
3690 function Check_Component (Comp : Node_Id) return Boolean is
3692 if Is_Overloaded (Comp) then
3696 return Compile_Time_Known_Value (Comp)
3698 or else (Is_Entity_Name (Comp)
3699 and then Present (Entity (Comp))
3700 and then No (Renamed_Object (Entity (Comp))))
3702 or else (Nkind (Comp) = N_Attribute_Reference
3703 and then Check_Component (Prefix (Comp)))
3705 or else (Nkind (Comp) in N_Binary_Op
3706 and then Check_Component (Left_Opnd (Comp))
3707 and then Check_Component (Right_Opnd (Comp)))
3709 or else (Nkind (Comp) in N_Unary_Op
3710 and then Check_Component (Right_Opnd (Comp)))
3712 or else (Nkind (Comp) = N_Selected_Component
3713 and then Check_Component (Prefix (Comp)))
3715 or else (Nkind (Comp) = N_Unchecked_Type_Conversion
3716 and then Check_Component (Expression (Comp)));
3717 end Check_Component;
3719 -- Start of processing for Safe_Component
3722 -- If the component appears in an association that may
3723 -- correspond to more than one element, it is not analyzed
3724 -- before the expansion into assignments, to avoid side effects.
3725 -- We analyze, but do not resolve the copy, to obtain sufficient
3726 -- entity information for the checks that follow. If component is
3727 -- overloaded we assume an unsafe function call.
3729 if not Analyzed (Comp) then
3730 if Is_Overloaded (Expr) then
3733 elsif Nkind (Expr) = N_Aggregate
3734 and then not Is_Others_Aggregate (Expr)
3738 elsif Nkind (Expr) = N_Allocator then
3740 -- For now, too complex to analyze
3745 Comp := New_Copy_Tree (Expr);
3746 Set_Parent (Comp, Parent (Expr));
3750 if Nkind (Comp) = N_Aggregate then
3751 return Safe_Aggregate (Comp);
3753 return Check_Component (Comp);
3757 -- Start of processing for In_Place_Assign_OK
3760 if Present (Component_Associations (N)) then
3762 -- On assignment, sliding can take place, so we cannot do the
3763 -- assignment in place unless the bounds of the aggregate are
3764 -- statically equal to those of the target.
3766 -- If the aggregate is given by an others choice, the bounds
3767 -- are derived from the left-hand side, and the assignment is
3768 -- safe if the expression is.
3770 if Is_Others_Aggregate (N) then
3773 (Expression (First (Component_Associations (N))));
3776 Aggr_In := First_Index (Etype (N));
3777 if Nkind (Parent (N)) = N_Assignment_Statement then
3778 Obj_In := First_Index (Etype (Name (Parent (N))));
3781 -- Context is an allocator. Check bounds of aggregate
3782 -- against given type in qualified expression.
3784 pragma Assert (Nkind (Parent (Parent (N))) = N_Allocator);
3786 First_Index (Etype (Entity (Subtype_Mark (Parent (N)))));
3789 while Present (Aggr_In) loop
3790 Get_Index_Bounds (Aggr_In, Aggr_Lo, Aggr_Hi);
3791 Get_Index_Bounds (Obj_In, Obj_Lo, Obj_Hi);
3793 if not Compile_Time_Known_Value (Aggr_Lo)
3794 or else not Compile_Time_Known_Value (Aggr_Hi)
3795 or else not Compile_Time_Known_Value (Obj_Lo)
3796 or else not Compile_Time_Known_Value (Obj_Hi)
3797 or else Expr_Value (Aggr_Lo) /= Expr_Value (Obj_Lo)
3798 or else Expr_Value (Aggr_Hi) /= Expr_Value (Obj_Hi)
3803 Next_Index (Aggr_In);
3804 Next_Index (Obj_In);
3808 -- Now check the component values themselves
3810 return Safe_Aggregate (N);
3811 end In_Place_Assign_OK;
3817 procedure Others_Check (Sub_Aggr : Node_Id; Dim : Pos) is
3818 Aggr_Lo : constant Node_Id := Aggr_Low (Dim);
3819 Aggr_Hi : constant Node_Id := Aggr_High (Dim);
3820 -- The bounds of the aggregate for this dimension
3822 Ind_Typ : constant Entity_Id := Aggr_Index_Typ (Dim);
3823 -- The index type for this dimension
3825 Need_To_Check : Boolean := False;
3827 Choices_Lo : Node_Id := Empty;
3828 Choices_Hi : Node_Id := Empty;
3829 -- The lowest and highest discrete choices for a named sub-aggregate
3831 Nb_Choices : Int := -1;
3832 -- The number of discrete non-others choices in this sub-aggregate
3834 Nb_Elements : Uint := Uint_0;
3835 -- The number of elements in a positional aggregate
3837 Cond : Node_Id := Empty;
3844 -- Check if we have an others choice. If we do make sure that this
3845 -- sub-aggregate contains at least one element in addition to the
3848 if Range_Checks_Suppressed (Ind_Typ) then
3849 Need_To_Check := False;
3851 elsif Present (Expressions (Sub_Aggr))
3852 and then Present (Component_Associations (Sub_Aggr))
3854 Need_To_Check := True;
3856 elsif Present (Component_Associations (Sub_Aggr)) then
3857 Assoc := Last (Component_Associations (Sub_Aggr));
3859 if Nkind (First (Choices (Assoc))) /= N_Others_Choice then
3860 Need_To_Check := False;
3863 -- Count the number of discrete choices. Start with -1
3864 -- because the others choice does not count.
3867 Assoc := First (Component_Associations (Sub_Aggr));
3868 while Present (Assoc) loop
3869 Choice := First (Choices (Assoc));
3870 while Present (Choice) loop
3871 Nb_Choices := Nb_Choices + 1;
3878 -- If there is only an others choice nothing to do
3880 Need_To_Check := (Nb_Choices > 0);
3884 Need_To_Check := False;
3887 -- If we are dealing with a positional sub-aggregate with an
3888 -- others choice then compute the number or positional elements.
3890 if Need_To_Check and then Present (Expressions (Sub_Aggr)) then
3891 Expr := First (Expressions (Sub_Aggr));
3892 Nb_Elements := Uint_0;
3893 while Present (Expr) loop
3894 Nb_Elements := Nb_Elements + 1;
3898 -- If the aggregate contains discrete choices and an others choice
3899 -- compute the smallest and largest discrete choice values.
3901 elsif Need_To_Check then
3902 Compute_Choices_Lo_And_Choices_Hi : declare
3904 Table : Case_Table_Type (1 .. Nb_Choices);
3905 -- Used to sort all the different choice values
3912 Assoc := First (Component_Associations (Sub_Aggr));
3913 while Present (Assoc) loop
3914 Choice := First (Choices (Assoc));
3915 while Present (Choice) loop
3916 if Nkind (Choice) = N_Others_Choice then
3920 Get_Index_Bounds (Choice, Low, High);
3921 Table (J).Choice_Lo := Low;
3922 Table (J).Choice_Hi := High;
3931 -- Sort the discrete choices
3933 Sort_Case_Table (Table);
3935 Choices_Lo := Table (1).Choice_Lo;
3936 Choices_Hi := Table (Nb_Choices).Choice_Hi;
3937 end Compute_Choices_Lo_And_Choices_Hi;
3940 -- If no others choice in this sub-aggregate, or the aggregate
3941 -- comprises only an others choice, nothing to do.
3943 if not Need_To_Check then
3946 -- If we are dealing with an aggregate containing an others
3947 -- choice and positional components, we generate the following test:
3949 -- if Ind_Typ'Pos (Aggr_Lo) + (Nb_Elements - 1) >
3950 -- Ind_Typ'Pos (Aggr_Hi)
3952 -- raise Constraint_Error;
3955 elsif Nb_Elements > Uint_0 then
3961 Make_Attribute_Reference (Loc,
3962 Prefix => New_Reference_To (Ind_Typ, Loc),
3963 Attribute_Name => Name_Pos,
3966 (Duplicate_Subexpr_Move_Checks (Aggr_Lo))),
3967 Right_Opnd => Make_Integer_Literal (Loc, Nb_Elements - 1)),
3970 Make_Attribute_Reference (Loc,
3971 Prefix => New_Reference_To (Ind_Typ, Loc),
3972 Attribute_Name => Name_Pos,
3973 Expressions => New_List (
3974 Duplicate_Subexpr_Move_Checks (Aggr_Hi))));
3976 -- If we are dealing with an aggregate containing an others
3977 -- choice and discrete choices we generate the following test:
3979 -- [constraint_error when
3980 -- Choices_Lo < Aggr_Lo or else Choices_Hi > Aggr_Hi];
3988 Duplicate_Subexpr_Move_Checks (Choices_Lo),
3990 Duplicate_Subexpr_Move_Checks (Aggr_Lo)),
3995 Duplicate_Subexpr (Choices_Hi),
3997 Duplicate_Subexpr (Aggr_Hi)));
4000 if Present (Cond) then
4002 Make_Raise_Constraint_Error (Loc,
4004 Reason => CE_Length_Check_Failed));
4007 -- Now look inside the sub-aggregate to see if there is more work
4009 if Dim < Aggr_Dimension then
4011 -- Process positional components
4013 if Present (Expressions (Sub_Aggr)) then
4014 Expr := First (Expressions (Sub_Aggr));
4015 while Present (Expr) loop
4016 Others_Check (Expr, Dim + 1);
4021 -- Process component associations
4023 if Present (Component_Associations (Sub_Aggr)) then
4024 Assoc := First (Component_Associations (Sub_Aggr));
4025 while Present (Assoc) loop
4026 Expr := Expression (Assoc);
4027 Others_Check (Expr, Dim + 1);
4034 -- Remaining Expand_Array_Aggregate variables
4037 -- Holds the temporary aggregate value
4040 -- Holds the declaration of Tmp
4042 Aggr_Code : List_Id;
4043 Parent_Node : Node_Id;
4044 Parent_Kind : Node_Kind;
4046 -- Start of processing for Expand_Array_Aggregate
4049 -- Do not touch the special aggregates of attributes used for Asm calls
4051 if Is_RTE (Ctyp, RE_Asm_Input_Operand)
4052 or else Is_RTE (Ctyp, RE_Asm_Output_Operand)
4057 -- If the semantic analyzer has determined that aggregate N will raise
4058 -- Constraint_Error at run-time, then the aggregate node has been
4059 -- replaced with an N_Raise_Constraint_Error node and we should
4062 pragma Assert (not Raises_Constraint_Error (N));
4066 -- Check that the index range defined by aggregate bounds is
4067 -- compatible with corresponding index subtype.
4069 Index_Compatibility_Check : declare
4070 Aggr_Index_Range : Node_Id := First_Index (Typ);
4071 -- The current aggregate index range
4073 Index_Constraint : Node_Id := First_Index (Etype (Typ));
4074 -- The corresponding index constraint against which we have to
4075 -- check the above aggregate index range.
4078 Compute_Others_Present (N, 1);
4080 for J in 1 .. Aggr_Dimension loop
4081 -- There is no need to emit a check if an others choice is
4082 -- present for this array aggregate dimension since in this
4083 -- case one of N's sub-aggregates has taken its bounds from the
4084 -- context and these bounds must have been checked already. In
4085 -- addition all sub-aggregates corresponding to the same
4086 -- dimension must all have the same bounds (checked in (c) below).
4088 if not Range_Checks_Suppressed (Etype (Index_Constraint))
4089 and then not Others_Present (J)
4091 -- We don't use Checks.Apply_Range_Check here because it
4092 -- emits a spurious check. Namely it checks that the range
4093 -- defined by the aggregate bounds is non empty. But we know
4094 -- this already if we get here.
4096 Check_Bounds (Aggr_Index_Range, Index_Constraint);
4099 -- Save the low and high bounds of the aggregate index as well
4100 -- as the index type for later use in checks (b) and (c) below.
4102 Aggr_Low (J) := Low_Bound (Aggr_Index_Range);
4103 Aggr_High (J) := High_Bound (Aggr_Index_Range);
4105 Aggr_Index_Typ (J) := Etype (Index_Constraint);
4107 Next_Index (Aggr_Index_Range);
4108 Next_Index (Index_Constraint);
4110 end Index_Compatibility_Check;
4114 -- If an others choice is present check that no aggregate
4115 -- index is outside the bounds of the index constraint.
4117 Others_Check (N, 1);
4121 -- For multidimensional arrays make sure that all subaggregates
4122 -- corresponding to the same dimension have the same bounds.
4124 if Aggr_Dimension > 1 then
4125 Check_Same_Aggr_Bounds (N, 1);
4130 -- Here we test for is packed array aggregate that we can handle
4131 -- at compile time. If so, return with transformation done. Note
4132 -- that we do this even if the aggregate is nested, because once
4133 -- we have done this processing, there is no more nested aggregate!
4135 if Packed_Array_Aggregate_Handled (N) then
4139 -- At this point we try to convert to positional form
4141 Convert_To_Positional (N);
4143 -- if the result is no longer an aggregate (e.g. it may be a string
4144 -- literal, or a temporary which has the needed value), then we are
4145 -- done, since there is no longer a nested aggregate.
4147 if Nkind (N) /= N_Aggregate then
4150 -- We are also done if the result is an analyzed aggregate
4151 -- This case could use more comments ???
4154 and then N /= Original_Node (N)
4159 -- Now see if back end processing is possible
4161 if Backend_Processing_Possible (N) then
4163 -- If the aggregate is static but the constraints are not, build
4164 -- a static subtype for the aggregate, so that Gigi can place it
4165 -- in static memory. Perform an unchecked_conversion to the non-
4166 -- static type imposed by the context.
4169 Itype : constant Entity_Id := Etype (N);
4171 Needs_Type : Boolean := False;
4174 Index := First_Index (Itype);
4176 while Present (Index) loop
4177 if not Is_Static_Subtype (Etype (Index)) then
4186 Build_Constrained_Type (Positional => True);
4187 Rewrite (N, Unchecked_Convert_To (Itype, N));
4197 -- Delay expansion for nested aggregates it will be taken care of
4198 -- when the parent aggregate is expanded
4200 Parent_Node := Parent (N);
4201 Parent_Kind := Nkind (Parent_Node);
4203 if Parent_Kind = N_Qualified_Expression then
4204 Parent_Node := Parent (Parent_Node);
4205 Parent_Kind := Nkind (Parent_Node);
4208 if Parent_Kind = N_Aggregate
4209 or else Parent_Kind = N_Extension_Aggregate
4210 or else Parent_Kind = N_Component_Association
4211 or else (Parent_Kind = N_Object_Declaration
4212 and then Controlled_Type (Typ))
4213 or else (Parent_Kind = N_Assignment_Statement
4214 and then Inside_Init_Proc)
4216 Set_Expansion_Delayed (N);
4222 -- Look if in place aggregate expansion is possible
4224 -- For object declarations we build the aggregate in place, unless
4225 -- the array is bit-packed or the component is controlled.
4227 -- For assignments we do the assignment in place if all the component
4228 -- associations have compile-time known values. For other cases we
4229 -- create a temporary. The analysis for safety of on-line assignment
4230 -- is delicate, i.e. we don't know how to do it fully yet ???
4232 -- For allocators we assign to the designated object in place if the
4233 -- aggregate meets the same conditions as other in-place assignments.
4234 -- In this case the aggregate may not come from source but was created
4235 -- for default initialization, e.g. with Initialize_Scalars.
4237 if Requires_Transient_Scope (Typ) then
4238 Establish_Transient_Scope
4239 (N, Sec_Stack => Has_Controlled_Component (Typ));
4242 if Has_Default_Init_Comps (N) then
4243 Maybe_In_Place_OK := False;
4245 elsif Is_Bit_Packed_Array (Typ)
4246 or else Has_Controlled_Component (Typ)
4248 Maybe_In_Place_OK := False;
4251 Maybe_In_Place_OK :=
4252 (Nkind (Parent (N)) = N_Assignment_Statement
4253 and then Comes_From_Source (N)
4254 and then In_Place_Assign_OK)
4257 (Nkind (Parent (Parent (N))) = N_Allocator
4258 and then In_Place_Assign_OK);
4261 if not Has_Default_Init_Comps (N)
4262 and then Comes_From_Source (Parent (N))
4263 and then Nkind (Parent (N)) = N_Object_Declaration
4265 Must_Slide (Etype (Defining_Identifier (Parent (N))), Typ)
4266 and then N = Expression (Parent (N))
4267 and then not Is_Bit_Packed_Array (Typ)
4268 and then not Has_Controlled_Component (Typ)
4269 and then not Has_Address_Clause (Parent (N))
4271 Tmp := Defining_Identifier (Parent (N));
4272 Set_No_Initialization (Parent (N));
4273 Set_Expression (Parent (N), Empty);
4275 -- Set the type of the entity, for use in the analysis of the
4276 -- subsequent indexed assignments. If the nominal type is not
4277 -- constrained, build a subtype from the known bounds of the
4278 -- aggregate. If the declaration has a subtype mark, use it,
4279 -- otherwise use the itype of the aggregate.
4281 if not Is_Constrained (Typ) then
4282 Build_Constrained_Type (Positional => False);
4283 elsif Is_Entity_Name (Object_Definition (Parent (N)))
4284 and then Is_Constrained (Entity (Object_Definition (Parent (N))))
4286 Set_Etype (Tmp, Entity (Object_Definition (Parent (N))));
4288 Set_Size_Known_At_Compile_Time (Typ, False);
4289 Set_Etype (Tmp, Typ);
4292 elsif Maybe_In_Place_OK
4293 and then Nkind (Parent (N)) = N_Qualified_Expression
4294 and then Nkind (Parent (Parent (N))) = N_Allocator
4296 Set_Expansion_Delayed (N);
4299 -- In the remaining cases the aggregate is the RHS of an assignment
4301 elsif Maybe_In_Place_OK
4302 and then Is_Entity_Name (Name (Parent (N)))
4304 Tmp := Entity (Name (Parent (N)));
4306 if Etype (Tmp) /= Etype (N) then
4307 Apply_Length_Check (N, Etype (Tmp));
4309 if Nkind (N) = N_Raise_Constraint_Error then
4311 -- Static error, nothing further to expand
4317 elsif Maybe_In_Place_OK
4318 and then Nkind (Name (Parent (N))) = N_Explicit_Dereference
4319 and then Is_Entity_Name (Prefix (Name (Parent (N))))
4321 Tmp := Name (Parent (N));
4323 if Etype (Tmp) /= Etype (N) then
4324 Apply_Length_Check (N, Etype (Tmp));
4327 elsif Maybe_In_Place_OK
4328 and then Nkind (Name (Parent (N))) = N_Slice
4329 and then Safe_Slice_Assignment (N)
4331 -- Safe_Slice_Assignment rewrites assignment as a loop
4337 -- In place aggregate expansion is not possible
4340 Maybe_In_Place_OK := False;
4341 Tmp := Make_Defining_Identifier (Loc, New_Internal_Name ('A'));
4343 Make_Object_Declaration
4345 Defining_Identifier => Tmp,
4346 Object_Definition => New_Occurrence_Of (Typ, Loc));
4347 Set_No_Initialization (Tmp_Decl, True);
4349 -- If we are within a loop, the temporary will be pushed on the
4350 -- stack at each iteration. If the aggregate is the expression for
4351 -- an allocator, it will be immediately copied to the heap and can
4352 -- be reclaimed at once. We create a transient scope around the
4353 -- aggregate for this purpose.
4355 if Ekind (Current_Scope) = E_Loop
4356 and then Nkind (Parent (Parent (N))) = N_Allocator
4358 Establish_Transient_Scope (N, False);
4361 Insert_Action (N, Tmp_Decl);
4364 -- Construct and insert the aggregate code. We can safely suppress
4365 -- index checks because this code is guaranteed not to raise CE
4366 -- on index checks. However we should *not* suppress all checks.
4372 if Nkind (Tmp) = N_Defining_Identifier then
4373 Target := New_Reference_To (Tmp, Loc);
4377 if Has_Default_Init_Comps (N) then
4379 -- Ada 2005 (AI-287): This case has not been analyzed???
4381 raise Program_Error;
4384 -- Name in assignment is explicit dereference
4386 Target := New_Copy (Tmp);
4390 Build_Array_Aggr_Code (N,
4392 Index => First_Index (Typ),
4394 Scalar_Comp => Is_Scalar_Type (Ctyp));
4397 if Comes_From_Source (Tmp) then
4398 Insert_Actions_After (Parent (N), Aggr_Code);
4401 Insert_Actions (N, Aggr_Code);
4404 -- If the aggregate has been assigned in place, remove the original
4407 if Nkind (Parent (N)) = N_Assignment_Statement
4408 and then Maybe_In_Place_OK
4410 Rewrite (Parent (N), Make_Null_Statement (Loc));
4412 elsif Nkind (Parent (N)) /= N_Object_Declaration
4413 or else Tmp /= Defining_Identifier (Parent (N))
4415 Rewrite (N, New_Occurrence_Of (Tmp, Loc));
4416 Analyze_And_Resolve (N, Typ);
4418 end Expand_Array_Aggregate;
4420 ------------------------
4421 -- Expand_N_Aggregate --
4422 ------------------------
4424 procedure Expand_N_Aggregate (N : Node_Id) is
4426 if Is_Record_Type (Etype (N)) then
4427 Expand_Record_Aggregate (N);
4429 Expand_Array_Aggregate (N);
4433 when RE_Not_Available =>
4435 end Expand_N_Aggregate;
4437 ----------------------------------
4438 -- Expand_N_Extension_Aggregate --
4439 ----------------------------------
4441 -- If the ancestor part is an expression, add a component association for
4442 -- the parent field. If the type of the ancestor part is not the direct
4443 -- parent of the expected type, build recursively the needed ancestors.
4444 -- If the ancestor part is a subtype_mark, replace aggregate with a decla-
4445 -- ration for a temporary of the expected type, followed by individual
4446 -- assignments to the given components.
4448 procedure Expand_N_Extension_Aggregate (N : Node_Id) is
4449 Loc : constant Source_Ptr := Sloc (N);
4450 A : constant Node_Id := Ancestor_Part (N);
4451 Typ : constant Entity_Id := Etype (N);
4454 -- If the ancestor is a subtype mark, an init proc must be called
4455 -- on the resulting object which thus has to be materialized in
4458 if Is_Entity_Name (A) and then Is_Type (Entity (A)) then
4459 Convert_To_Assignments (N, Typ);
4461 -- The extension aggregate is transformed into a record aggregate
4462 -- of the following form (c1 and c2 are inherited components)
4464 -- (Exp with c3 => a, c4 => b)
4465 -- ==> (c1 => Exp.c1, c2 => Exp.c2, c1 => a, c2 => b)
4470 -- No tag is needed in the case of Java_VM
4473 Expand_Record_Aggregate (N,
4476 Expand_Record_Aggregate (N,
4479 (Node (First_Elmt (Access_Disp_Table (Typ))), Loc),
4485 when RE_Not_Available =>
4487 end Expand_N_Extension_Aggregate;
4489 -----------------------------
4490 -- Expand_Record_Aggregate --
4491 -----------------------------
4493 procedure Expand_Record_Aggregate
4495 Orig_Tag : Node_Id := Empty;
4496 Parent_Expr : Node_Id := Empty)
4498 Loc : constant Source_Ptr := Sloc (N);
4499 Comps : constant List_Id := Component_Associations (N);
4500 Typ : constant Entity_Id := Etype (N);
4501 Base_Typ : constant Entity_Id := Base_Type (Typ);
4503 function Has_Delayed_Nested_Aggregate_Or_Tagged_Comps return Boolean;
4504 -- Checks the presence of a nested aggregate which needs Late_Expansion
4505 -- or the presence of tagged components which may need tag adjustment.
4507 --------------------------------------------------
4508 -- Has_Delayed_Nested_Aggregate_Or_Tagged_Comps --
4509 --------------------------------------------------
4511 function Has_Delayed_Nested_Aggregate_Or_Tagged_Comps return Boolean is
4521 while Present (C) loop
4522 if Nkind (Expression (C)) = N_Qualified_Expression then
4523 Expr_Q := Expression (Expression (C));
4525 Expr_Q := Expression (C);
4528 -- Return true if the aggregate has any associations for
4529 -- tagged components that may require tag adjustment.
4530 -- These are cases where the source expression may have
4531 -- a tag that could differ from the component tag (e.g.,
4532 -- can occur for type conversions and formal parameters).
4533 -- (Tag adjustment is not needed if Java_VM because object
4534 -- tags are implicit in the JVM.)
4536 if Is_Tagged_Type (Etype (Expr_Q))
4537 and then (Nkind (Expr_Q) = N_Type_Conversion
4538 or else (Is_Entity_Name (Expr_Q)
4539 and then Ekind (Entity (Expr_Q)) in Formal_Kind))
4540 and then not Java_VM
4545 if Is_Delayed_Aggregate (Expr_Q) then
4553 end Has_Delayed_Nested_Aggregate_Or_Tagged_Comps;
4555 -- Remaining Expand_Record_Aggregate variables
4557 Tag_Value : Node_Id;
4561 -- Start of processing for Expand_Record_Aggregate
4564 -- If the aggregate is to be assigned to an atomic variable, we
4565 -- have to prevent a piecemeal assignment even if the aggregate
4566 -- is to be expanded. We create a temporary for the aggregate, and
4567 -- assign the temporary instead, so that the back end can generate
4568 -- an atomic move for it.
4571 and then (Nkind (Parent (N)) = N_Object_Declaration
4572 or else Nkind (Parent (N)) = N_Assignment_Statement)
4573 and then Comes_From_Source (Parent (N))
4575 Expand_Atomic_Aggregate (N, Typ);
4579 -- Gigi doesn't handle properly temporaries of variable size
4580 -- so we generate it in the front-end
4582 if not Size_Known_At_Compile_Time (Typ) then
4583 Convert_To_Assignments (N, Typ);
4585 -- Temporaries for controlled aggregates need to be attached to a
4586 -- final chain in order to be properly finalized, so it has to
4587 -- be created in the front-end
4589 elsif Is_Controlled (Typ)
4590 or else Has_Controlled_Component (Base_Type (Typ))
4592 Convert_To_Assignments (N, Typ);
4594 -- Ada 2005 (AI-287): In case of default initialized components we
4595 -- convert the aggregate into assignments.
4597 elsif Has_Default_Init_Comps (N) then
4598 Convert_To_Assignments (N, Typ);
4600 elsif Has_Delayed_Nested_Aggregate_Or_Tagged_Comps then
4601 Convert_To_Assignments (N, Typ);
4603 -- If an ancestor is private, some components are not inherited and
4604 -- we cannot expand into a record aggregate
4606 elsif Has_Private_Ancestor (Typ) then
4607 Convert_To_Assignments (N, Typ);
4609 -- ??? The following was done to compile fxacc00.ads in the ACVCs. Gigi
4610 -- is not able to handle the aggregate for Late_Request.
4612 elsif Is_Tagged_Type (Typ) and then Has_Discriminants (Typ) then
4613 Convert_To_Assignments (N, Typ);
4615 -- If some components are mutable, the size of the aggregate component
4616 -- may be disctinct from the default size of the type component, so
4617 -- we need to expand to insure that the back-end copies the proper
4618 -- size of the data.
4620 elsif Has_Mutable_Components (Typ) then
4621 Convert_To_Assignments (N, Typ);
4623 -- If the type involved has any non-bit aligned components, then
4624 -- we are not sure that the back end can handle this case correctly.
4626 elsif Type_May_Have_Bit_Aligned_Components (Typ) then
4627 Convert_To_Assignments (N, Typ);
4629 -- In all other cases we generate a proper aggregate that
4630 -- can be handled by gigi.
4633 -- If no discriminants, nothing special to do
4635 if not Has_Discriminants (Typ) then
4638 -- Case of discriminants present
4640 elsif Is_Derived_Type (Typ) then
4642 -- For untagged types, non-stored discriminants are replaced
4643 -- with stored discriminants, which are the ones that gigi uses
4644 -- to describe the type and its components.
4646 Generate_Aggregate_For_Derived_Type : declare
4647 Constraints : constant List_Id := New_List;
4648 First_Comp : Node_Id;
4649 Discriminant : Entity_Id;
4651 Num_Disc : Int := 0;
4652 Num_Gird : Int := 0;
4654 procedure Prepend_Stored_Values (T : Entity_Id);
4655 -- Scan the list of stored discriminants of the type, and
4656 -- add their values to the aggregate being built.
4658 ---------------------------
4659 -- Prepend_Stored_Values --
4660 ---------------------------
4662 procedure Prepend_Stored_Values (T : Entity_Id) is
4664 Discriminant := First_Stored_Discriminant (T);
4666 while Present (Discriminant) loop
4668 Make_Component_Association (Loc,
4670 New_List (New_Occurrence_Of (Discriminant, Loc)),
4674 Get_Discriminant_Value (
4677 Discriminant_Constraint (Typ))));
4679 if No (First_Comp) then
4680 Prepend_To (Component_Associations (N), New_Comp);
4682 Insert_After (First_Comp, New_Comp);
4685 First_Comp := New_Comp;
4686 Next_Stored_Discriminant (Discriminant);
4688 end Prepend_Stored_Values;
4690 -- Start of processing for Generate_Aggregate_For_Derived_Type
4693 -- Remove the associations for the discriminant of
4694 -- the derived type.
4696 First_Comp := First (Component_Associations (N));
4698 while Present (First_Comp) loop
4702 if Ekind (Entity (First (Choices (Comp)))) =
4706 Num_Disc := Num_Disc + 1;
4710 -- Insert stored discriminant associations in the correct
4711 -- order. If there are more stored discriminants than new
4712 -- discriminants, there is at least one new discriminant
4713 -- that constrains more than one of the stored discriminants.
4714 -- In this case we need to construct a proper subtype of
4715 -- the parent type, in order to supply values to all the
4716 -- components. Otherwise there is one-one correspondence
4717 -- between the constraints and the stored discriminants.
4719 First_Comp := Empty;
4721 Discriminant := First_Stored_Discriminant (Base_Type (Typ));
4723 while Present (Discriminant) loop
4724 Num_Gird := Num_Gird + 1;
4725 Next_Stored_Discriminant (Discriminant);
4728 -- Case of more stored discriminants than new discriminants
4730 if Num_Gird > Num_Disc then
4732 -- Create a proper subtype of the parent type, which is
4733 -- the proper implementation type for the aggregate, and
4734 -- convert it to the intended target type.
4736 Discriminant := First_Stored_Discriminant (Base_Type (Typ));
4738 while Present (Discriminant) loop
4741 Get_Discriminant_Value (
4744 Discriminant_Constraint (Typ)));
4745 Append (New_Comp, Constraints);
4746 Next_Stored_Discriminant (Discriminant);
4750 Make_Subtype_Declaration (Loc,
4751 Defining_Identifier =>
4752 Make_Defining_Identifier (Loc,
4753 New_Internal_Name ('T')),
4754 Subtype_Indication =>
4755 Make_Subtype_Indication (Loc,
4757 New_Occurrence_Of (Etype (Base_Type (Typ)), Loc),
4759 Make_Index_Or_Discriminant_Constraint
4760 (Loc, Constraints)));
4762 Insert_Action (N, Decl);
4763 Prepend_Stored_Values (Base_Type (Typ));
4765 Set_Etype (N, Defining_Identifier (Decl));
4768 Rewrite (N, Unchecked_Convert_To (Typ, N));
4771 -- Case where we do not have fewer new discriminants than
4772 -- stored discriminants, so in this case we can simply
4773 -- use the stored discriminants of the subtype.
4776 Prepend_Stored_Values (Typ);
4778 end Generate_Aggregate_For_Derived_Type;
4781 if Is_Tagged_Type (Typ) then
4783 -- The tagged case, _parent and _tag component must be created
4785 -- Reset null_present unconditionally. tagged records always have
4786 -- at least one field (the tag or the parent)
4788 Set_Null_Record_Present (N, False);
4790 -- When the current aggregate comes from the expansion of an
4791 -- extension aggregate, the parent expr is replaced by an
4792 -- aggregate formed by selected components of this expr
4794 if Present (Parent_Expr)
4795 and then Is_Empty_List (Comps)
4797 Comp := First_Entity (Typ);
4798 while Present (Comp) loop
4800 -- Skip all entities that aren't discriminants or components
4802 if Ekind (Comp) /= E_Discriminant
4803 and then Ekind (Comp) /= E_Component
4807 -- Skip all expander-generated components
4810 not Comes_From_Source (Original_Record_Component (Comp))
4816 Make_Selected_Component (Loc,
4818 Unchecked_Convert_To (Typ,
4819 Duplicate_Subexpr (Parent_Expr, True)),
4821 Selector_Name => New_Occurrence_Of (Comp, Loc));
4824 Make_Component_Association (Loc,
4826 New_List (New_Occurrence_Of (Comp, Loc)),
4830 Analyze_And_Resolve (New_Comp, Etype (Comp));
4837 -- Compute the value for the Tag now, if the type is a root it
4838 -- will be included in the aggregate right away, otherwise it will
4839 -- be propagated to the parent aggregate
4841 if Present (Orig_Tag) then
4842 Tag_Value := Orig_Tag;
4848 (Node (First_Elmt (Access_Disp_Table (Typ))), Loc);
4851 -- For a derived type, an aggregate for the parent is formed with
4852 -- all the inherited components.
4854 if Is_Derived_Type (Typ) then
4857 First_Comp : Node_Id;
4858 Parent_Comps : List_Id;
4859 Parent_Aggr : Node_Id;
4860 Parent_Name : Node_Id;
4863 -- Remove the inherited component association from the
4864 -- aggregate and store them in the parent aggregate
4866 First_Comp := First (Component_Associations (N));
4867 Parent_Comps := New_List;
4869 while Present (First_Comp)
4870 and then Scope (Original_Record_Component (
4871 Entity (First (Choices (First_Comp))))) /= Base_Typ
4876 Append (Comp, Parent_Comps);
4879 Parent_Aggr := Make_Aggregate (Loc,
4880 Component_Associations => Parent_Comps);
4881 Set_Etype (Parent_Aggr, Etype (Base_Type (Typ)));
4883 -- Find the _parent component
4885 Comp := First_Component (Typ);
4886 while Chars (Comp) /= Name_uParent loop
4887 Comp := Next_Component (Comp);
4890 Parent_Name := New_Occurrence_Of (Comp, Loc);
4892 -- Insert the parent aggregate
4894 Prepend_To (Component_Associations (N),
4895 Make_Component_Association (Loc,
4896 Choices => New_List (Parent_Name),
4897 Expression => Parent_Aggr));
4899 -- Expand recursively the parent propagating the right Tag
4901 Expand_Record_Aggregate (
4902 Parent_Aggr, Tag_Value, Parent_Expr);
4905 -- For a root type, the tag component is added (unless compiling
4906 -- for the Java VM, where tags are implicit).
4908 elsif not Java_VM then
4910 Tag_Name : constant Node_Id :=
4912 (First_Tag_Component (Typ), Loc);
4913 Typ_Tag : constant Entity_Id := RTE (RE_Tag);
4914 Conv_Node : constant Node_Id :=
4915 Unchecked_Convert_To (Typ_Tag, Tag_Value);
4918 Set_Etype (Conv_Node, Typ_Tag);
4919 Prepend_To (Component_Associations (N),
4920 Make_Component_Association (Loc,
4921 Choices => New_List (Tag_Name),
4922 Expression => Conv_Node));
4927 end Expand_Record_Aggregate;
4929 ----------------------------
4930 -- Has_Default_Init_Comps --
4931 ----------------------------
4933 function Has_Default_Init_Comps (N : Node_Id) return Boolean is
4934 Comps : constant List_Id := Component_Associations (N);
4938 pragma Assert (Nkind (N) = N_Aggregate
4939 or else Nkind (N) = N_Extension_Aggregate);
4945 -- Check if any direct component has default initialized components
4948 while Present (C) loop
4949 if Box_Present (C) then
4956 -- Recursive call in case of aggregate expression
4959 while Present (C) loop
4960 Expr := Expression (C);
4963 and then (Nkind (Expr) = N_Aggregate
4964 or else Nkind (Expr) = N_Extension_Aggregate)
4965 and then Has_Default_Init_Comps (Expr)
4974 end Has_Default_Init_Comps;
4976 --------------------------
4977 -- Is_Delayed_Aggregate --
4978 --------------------------
4980 function Is_Delayed_Aggregate (N : Node_Id) return Boolean is
4981 Node : Node_Id := N;
4982 Kind : Node_Kind := Nkind (Node);
4985 if Kind = N_Qualified_Expression then
4986 Node := Expression (Node);
4987 Kind := Nkind (Node);
4990 if Kind /= N_Aggregate and then Kind /= N_Extension_Aggregate then
4993 return Expansion_Delayed (Node);
4995 end Is_Delayed_Aggregate;
4997 --------------------
4998 -- Late_Expansion --
4999 --------------------
5001 function Late_Expansion
5005 Flist : Node_Id := Empty;
5006 Obj : Entity_Id := Empty) return List_Id
5009 if Is_Record_Type (Etype (N)) then
5010 return Build_Record_Aggr_Code (N, Typ, Target, Flist, Obj);
5012 else pragma Assert (Is_Array_Type (Etype (N)));
5014 Build_Array_Aggr_Code
5016 Ctype => Component_Type (Etype (N)),
5017 Index => First_Index (Typ),
5019 Scalar_Comp => Is_Scalar_Type (Component_Type (Typ)),
5025 ----------------------------------
5026 -- Make_OK_Assignment_Statement --
5027 ----------------------------------
5029 function Make_OK_Assignment_Statement
5032 Expression : Node_Id) return Node_Id
5035 Set_Assignment_OK (Name);
5036 return Make_Assignment_Statement (Sloc, Name, Expression);
5037 end Make_OK_Assignment_Statement;
5039 -----------------------
5040 -- Number_Of_Choices --
5041 -----------------------
5043 function Number_Of_Choices (N : Node_Id) return Nat is
5047 Nb_Choices : Nat := 0;
5050 if Present (Expressions (N)) then
5054 Assoc := First (Component_Associations (N));
5055 while Present (Assoc) loop
5057 Choice := First (Choices (Assoc));
5058 while Present (Choice) loop
5060 if Nkind (Choice) /= N_Others_Choice then
5061 Nb_Choices := Nb_Choices + 1;
5071 end Number_Of_Choices;
5073 ------------------------------------
5074 -- Packed_Array_Aggregate_Handled --
5075 ------------------------------------
5077 -- The current version of this procedure will handle at compile time
5078 -- any array aggregate that meets these conditions:
5080 -- One dimensional, bit packed
5081 -- Underlying packed type is modular type
5082 -- Bounds are within 32-bit Int range
5083 -- All bounds and values are static
5085 function Packed_Array_Aggregate_Handled (N : Node_Id) return Boolean is
5086 Loc : constant Source_Ptr := Sloc (N);
5087 Typ : constant Entity_Id := Etype (N);
5088 Ctyp : constant Entity_Id := Component_Type (Typ);
5090 Not_Handled : exception;
5091 -- Exception raised if this aggregate cannot be handled
5094 -- For now, handle only one dimensional bit packed arrays
5096 if not Is_Bit_Packed_Array (Typ)
5097 or else Number_Dimensions (Typ) > 1
5098 or else not Is_Modular_Integer_Type (Packed_Array_Type (Typ))
5104 Csiz : constant Nat := UI_To_Int (Component_Size (Typ));
5108 -- Bounds of index type
5112 -- Values of bounds if compile time known
5114 function Get_Component_Val (N : Node_Id) return Uint;
5115 -- Given a expression value N of the component type Ctyp, returns
5116 -- A value of Csiz (component size) bits representing this value.
5117 -- If the value is non-static or any other reason exists why the
5118 -- value cannot be returned, then Not_Handled is raised.
5120 -----------------------
5121 -- Get_Component_Val --
5122 -----------------------
5124 function Get_Component_Val (N : Node_Id) return Uint is
5128 -- We have to analyze the expression here before doing any further
5129 -- processing here. The analysis of such expressions is deferred
5130 -- till expansion to prevent some problems of premature analysis.
5132 Analyze_And_Resolve (N, Ctyp);
5134 -- Must have a compile time value. String literals have to
5135 -- be converted into temporaries as well, because they cannot
5136 -- easily be converted into their bit representation.
5138 if not Compile_Time_Known_Value (N)
5139 or else Nkind (N) = N_String_Literal
5144 Val := Expr_Rep_Value (N);
5146 -- Adjust for bias, and strip proper number of bits
5148 if Has_Biased_Representation (Ctyp) then
5149 Val := Val - Expr_Value (Type_Low_Bound (Ctyp));
5152 return Val mod Uint_2 ** Csiz;
5153 end Get_Component_Val;
5155 -- Here we know we have a one dimensional bit packed array
5158 Get_Index_Bounds (First_Index (Typ), Lo, Hi);
5160 -- Cannot do anything if bounds are dynamic
5162 if not Compile_Time_Known_Value (Lo)
5164 not Compile_Time_Known_Value (Hi)
5169 -- Or are silly out of range of int bounds
5171 Lob := Expr_Value (Lo);
5172 Hib := Expr_Value (Hi);
5174 if not UI_Is_In_Int_Range (Lob)
5176 not UI_Is_In_Int_Range (Hib)
5181 -- At this stage we have a suitable aggregate for handling
5182 -- at compile time (the only remaining checks, are that the
5183 -- values of expressions in the aggregate are compile time
5184 -- known (check performed by Get_Component_Val), and that
5185 -- any subtypes or ranges are statically known.
5187 -- If the aggregate is not fully positional at this stage,
5188 -- then convert it to positional form. Either this will fail,
5189 -- in which case we can do nothing, or it will succeed, in
5190 -- which case we have succeeded in handling the aggregate,
5191 -- or it will stay an aggregate, in which case we have failed
5192 -- to handle this case.
5194 if Present (Component_Associations (N)) then
5195 Convert_To_Positional
5196 (N, Max_Others_Replicate => 64, Handle_Bit_Packed => True);
5197 return Nkind (N) /= N_Aggregate;
5200 -- Otherwise we are all positional, so convert to proper value
5203 Lov : constant Int := UI_To_Int (Lob);
5204 Hiv : constant Int := UI_To_Int (Hib);
5206 Len : constant Nat := Int'Max (0, Hiv - Lov + 1);
5207 -- The length of the array (number of elements)
5209 Aggregate_Val : Uint;
5210 -- Value of aggregate. The value is set in the low order
5211 -- bits of this value. For the little-endian case, the
5212 -- values are stored from low-order to high-order and
5213 -- for the big-endian case the values are stored from
5214 -- high-order to low-order. Note that gigi will take care
5215 -- of the conversions to left justify the value in the big
5216 -- endian case (because of left justified modular type
5217 -- processing), so we do not have to worry about that here.
5220 -- Integer literal for resulting constructed value
5223 -- Shift count from low order for next value
5226 -- Shift increment for loop
5229 -- Next expression from positional parameters of aggregate
5232 -- For little endian, we fill up the low order bits of the
5233 -- target value. For big endian we fill up the high order
5234 -- bits of the target value (which is a left justified
5237 if Bytes_Big_Endian xor Debug_Flag_8 then
5238 Shift := Csiz * (Len - 1);
5245 -- Loop to set the values
5248 Aggregate_Val := Uint_0;
5250 Expr := First (Expressions (N));
5251 Aggregate_Val := Get_Component_Val (Expr) * Uint_2 ** Shift;
5253 for J in 2 .. Len loop
5254 Shift := Shift + Incr;
5257 Aggregate_Val + Get_Component_Val (Expr) * Uint_2 ** Shift;
5261 -- Now we can rewrite with the proper value
5264 Make_Integer_Literal (Loc,
5265 Intval => Aggregate_Val);
5266 Set_Print_In_Hex (Lit);
5268 -- Construct the expression using this literal. Note that it is
5269 -- important to qualify the literal with its proper modular type
5270 -- since universal integer does not have the required range and
5271 -- also this is a left justified modular type, which is important
5272 -- in the big-endian case.
5275 Unchecked_Convert_To (Typ,
5276 Make_Qualified_Expression (Loc,
5278 New_Occurrence_Of (Packed_Array_Type (Typ), Loc),
5279 Expression => Lit)));
5281 Analyze_And_Resolve (N, Typ);
5289 end Packed_Array_Aggregate_Handled;
5291 ----------------------------
5292 -- Has_Mutable_Components --
5293 ----------------------------
5295 function Has_Mutable_Components (Typ : Entity_Id) return Boolean is
5299 Comp := First_Component (Typ);
5301 while Present (Comp) loop
5302 if Is_Record_Type (Etype (Comp))
5303 and then Has_Discriminants (Etype (Comp))
5304 and then not Is_Constrained (Etype (Comp))
5309 Next_Component (Comp);
5313 end Has_Mutable_Components;
5315 ------------------------------
5316 -- Initialize_Discriminants --
5317 ------------------------------
5319 procedure Initialize_Discriminants (N : Node_Id; Typ : Entity_Id) is
5320 Loc : constant Source_Ptr := Sloc (N);
5321 Bas : constant Entity_Id := Base_Type (Typ);
5322 Par : constant Entity_Id := Etype (Bas);
5323 Decl : constant Node_Id := Parent (Par);
5327 if Is_Tagged_Type (Bas)
5328 and then Is_Derived_Type (Bas)
5329 and then Has_Discriminants (Par)
5330 and then Has_Discriminants (Bas)
5331 and then Number_Discriminants (Bas) /= Number_Discriminants (Par)
5332 and then Nkind (Decl) = N_Full_Type_Declaration
5333 and then Nkind (Type_Definition (Decl)) = N_Record_Definition
5335 (Variant_Part (Component_List (Type_Definition (Decl))))
5336 and then Nkind (N) /= N_Extension_Aggregate
5339 -- Call init proc to set discriminants.
5340 -- There should eventually be a special procedure for this ???
5342 Ref := New_Reference_To (Defining_Identifier (N), Loc);
5343 Insert_Actions_After (N,
5344 Build_Initialization_Call (Sloc (N), Ref, Typ));
5346 end Initialize_Discriminants;
5353 (Obj_Type : Entity_Id;
5354 Typ : Entity_Id) return Boolean
5356 L1, L2, H1, H2 : Node_Id;
5358 -- No sliding if the type of the object is not established yet, if
5359 -- it is an unconstrained type whose actual subtype comes from the
5360 -- aggregate, or if the two types are identical.
5362 if not Is_Array_Type (Obj_Type) then
5365 elsif not Is_Constrained (Obj_Type) then
5368 elsif Typ = Obj_Type then
5372 -- Sliding can only occur along the first dimension
5374 Get_Index_Bounds (First_Index (Typ), L1, H1);
5375 Get_Index_Bounds (First_Index (Obj_Type), L2, H2);
5377 if not Is_Static_Expression (L1)
5378 or else not Is_Static_Expression (L2)
5379 or else not Is_Static_Expression (H1)
5380 or else not Is_Static_Expression (H2)
5384 return Expr_Value (L1) /= Expr_Value (L2)
5385 or else Expr_Value (H1) /= Expr_Value (H2);
5390 ---------------------------
5391 -- Safe_Slice_Assignment --
5392 ---------------------------
5394 function Safe_Slice_Assignment (N : Node_Id) return Boolean is
5395 Loc : constant Source_Ptr := Sloc (Parent (N));
5396 Pref : constant Node_Id := Prefix (Name (Parent (N)));
5397 Range_Node : constant Node_Id := Discrete_Range (Name (Parent (N)));
5405 -- Generate: for J in Range loop Pref (J) := Expr; end loop;
5407 if Comes_From_Source (N)
5408 and then No (Expressions (N))
5409 and then Nkind (First (Choices (First (Component_Associations (N)))))
5413 Expression (First (Component_Associations (N)));
5414 L_J := Make_Defining_Identifier (Loc, New_Internal_Name ('J'));
5417 Make_Iteration_Scheme (Loc,
5418 Loop_Parameter_Specification =>
5419 Make_Loop_Parameter_Specification
5421 Defining_Identifier => L_J,
5422 Discrete_Subtype_Definition => Relocate_Node (Range_Node)));
5425 Make_Assignment_Statement (Loc,
5427 Make_Indexed_Component (Loc,
5428 Prefix => Relocate_Node (Pref),
5429 Expressions => New_List (New_Occurrence_Of (L_J, Loc))),
5430 Expression => Relocate_Node (Expr));
5432 -- Construct the final loop
5435 Make_Implicit_Loop_Statement
5436 (Node => Parent (N),
5437 Identifier => Empty,
5438 Iteration_Scheme => L_Iter,
5439 Statements => New_List (L_Body));
5441 -- Set type of aggregate to be type of lhs in assignment,
5442 -- to suppress redundant length checks.
5444 Set_Etype (N, Etype (Name (Parent (N))));
5446 Rewrite (Parent (N), Stat);
5447 Analyze (Parent (N));
5453 end Safe_Slice_Assignment;
5455 ---------------------
5456 -- Sort_Case_Table --
5457 ---------------------
5459 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type) is
5460 L : constant Int := Case_Table'First;
5461 U : constant Int := Case_Table'Last;
5470 T := Case_Table (K + 1);
5474 and then Expr_Value (Case_Table (J - 1).Choice_Lo) >
5475 Expr_Value (T.Choice_Lo)
5477 Case_Table (J) := Case_Table (J - 1);
5481 Case_Table (J) := T;
5484 end Sort_Case_Table;