1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2011, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Atree; use Atree;
27 with Checks; use Checks;
28 with Debug; use Debug;
29 with Einfo; use Einfo;
30 with Elists; use Elists;
31 with Errout; use Errout;
32 with Expander; use Expander;
33 with Exp_Util; use Exp_Util;
34 with Exp_Ch3; use Exp_Ch3;
35 with Exp_Ch7; use Exp_Ch7;
36 with Exp_Ch9; use Exp_Ch9;
37 with Exp_Disp; use Exp_Disp;
38 with Exp_Tss; use Exp_Tss;
39 with Fname; use Fname;
40 with Freeze; use Freeze;
41 with Itypes; use Itypes;
43 with Namet; use Namet;
44 with Nmake; use Nmake;
45 with Nlists; use Nlists;
47 with Restrict; use Restrict;
48 with Rident; use Rident;
49 with Rtsfind; use Rtsfind;
50 with Ttypes; use Ttypes;
52 with Sem_Aggr; use Sem_Aggr;
53 with Sem_Aux; use Sem_Aux;
54 with Sem_Ch3; use Sem_Ch3;
55 with Sem_Eval; use Sem_Eval;
56 with Sem_Res; use Sem_Res;
57 with Sem_Util; use Sem_Util;
58 with Sinfo; use Sinfo;
59 with Snames; use Snames;
60 with Stand; use Stand;
61 with Targparm; use Targparm;
62 with Tbuild; use Tbuild;
63 with Uintp; use Uintp;
65 package body Exp_Aggr is
67 type Case_Bounds is record
70 Choice_Node : Node_Id;
73 type Case_Table_Type is array (Nat range <>) of Case_Bounds;
74 -- Table type used by Check_Case_Choices procedure
76 function Has_Default_Init_Comps (N : Node_Id) return Boolean;
77 -- N is an aggregate (record or array). Checks the presence of default
78 -- initialization (<>) in any component (Ada 2005: AI-287).
80 function Is_Static_Dispatch_Table_Aggregate (N : Node_Id) return Boolean;
81 -- Returns true if N is an aggregate used to initialize the components
82 -- of an statically allocated dispatch table.
85 (Obj_Type : Entity_Id;
86 Typ : Entity_Id) return Boolean;
87 -- A static array aggregate in an object declaration can in most cases be
88 -- expanded in place. The one exception is when the aggregate is given
89 -- with component associations that specify different bounds from those of
90 -- the type definition in the object declaration. In this pathological
91 -- case the aggregate must slide, and we must introduce an intermediate
92 -- temporary to hold it.
94 -- The same holds in an assignment to one-dimensional array of arrays,
95 -- when a component may be given with bounds that differ from those of the
98 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type);
99 -- Sort the Case Table using the Lower Bound of each Choice as the key.
100 -- A simple insertion sort is used since the number of choices in a case
101 -- statement of variant part will usually be small and probably in near
104 ------------------------------------------------------
105 -- Local subprograms for Record Aggregate Expansion --
106 ------------------------------------------------------
108 function Build_Record_Aggr_Code
112 Is_Limited_Ancestor_Expansion : Boolean := False) return List_Id;
113 -- N is an N_Aggregate or an N_Extension_Aggregate. Typ is the type of the
114 -- aggregate. Target is an expression containing the location on which the
115 -- component by component assignments will take place. Returns the list of
116 -- assignments plus all other adjustments needed for tagged and controlled
117 -- types. Is_Limited_Ancestor_Expansion indicates that the function has
118 -- been called recursively to expand the limited ancestor to avoid copying
121 procedure Convert_To_Assignments (N : Node_Id; Typ : Entity_Id);
122 -- N is an N_Aggregate or an N_Extension_Aggregate. Typ is the type of the
123 -- aggregate (which can only be a record type, this procedure is only used
124 -- for record types). Transform the given aggregate into a sequence of
125 -- assignments performed component by component.
127 procedure Expand_Record_Aggregate
129 Orig_Tag : Node_Id := Empty;
130 Parent_Expr : Node_Id := Empty);
131 -- This is the top level procedure for record aggregate expansion.
132 -- Expansion for record aggregates needs expand aggregates for tagged
133 -- record types. Specifically Expand_Record_Aggregate adds the Tag
134 -- field in front of the Component_Association list that was created
135 -- during resolution by Resolve_Record_Aggregate.
137 -- N is the record aggregate node.
138 -- Orig_Tag is the value of the Tag that has to be provided for this
139 -- specific aggregate. It carries the tag corresponding to the type
140 -- of the outermost aggregate during the recursive expansion
141 -- Parent_Expr is the ancestor part of the original extension
144 function Has_Mutable_Components (Typ : Entity_Id) return Boolean;
145 -- Return true if one of the component is of a discriminated type with
146 -- defaults. An aggregate for a type with mutable components must be
147 -- expanded into individual assignments.
149 procedure Initialize_Discriminants (N : Node_Id; Typ : Entity_Id);
150 -- If the type of the aggregate is a type extension with renamed discrimi-
151 -- nants, we must initialize the hidden discriminants of the parent.
152 -- Otherwise, the target object must not be initialized. The discriminants
153 -- are initialized by calling the initialization procedure for the type.
154 -- This is incorrect if the initialization of other components has any
155 -- side effects. We restrict this call to the case where the parent type
156 -- has a variant part, because this is the only case where the hidden
157 -- discriminants are accessed, namely when calling discriminant checking
158 -- functions of the parent type, and when applying a stream attribute to
159 -- an object of the derived type.
161 -----------------------------------------------------
162 -- Local Subprograms for Array Aggregate Expansion --
163 -----------------------------------------------------
165 function Aggr_Size_OK (N : Node_Id; Typ : Entity_Id) return Boolean;
166 -- Very large static aggregates present problems to the back-end, and are
167 -- transformed into assignments and loops. This function verifies that the
168 -- total number of components of an aggregate is acceptable for rewriting
169 -- into a purely positional static form. Aggr_Size_OK must be called before
172 -- This function also detects and warns about one-component aggregates that
173 -- appear in a non-static context. Even if the component value is static,
174 -- such an aggregate must be expanded into an assignment.
176 function Backend_Processing_Possible (N : Node_Id) return Boolean;
177 -- This function checks if array aggregate N can be processed directly
178 -- by the backend. If this is the case True is returned.
180 function Build_Array_Aggr_Code
185 Scalar_Comp : Boolean;
186 Indexes : List_Id := No_List) return List_Id;
187 -- This recursive routine returns a list of statements containing the
188 -- loops and assignments that are needed for the expansion of the array
191 -- N is the (sub-)aggregate node to be expanded into code. This node has
192 -- been fully analyzed, and its Etype is properly set.
194 -- Index is the index node corresponding to the array sub-aggregate N
196 -- Into is the target expression into which we are copying the aggregate.
197 -- Note that this node may not have been analyzed yet, and so the Etype
198 -- field may not be set.
200 -- Scalar_Comp is True if the component type of the aggregate is scalar
202 -- Indexes is the current list of expressions used to index the object we
205 procedure Convert_Array_Aggr_In_Allocator
209 -- If the aggregate appears within an allocator and can be expanded in
210 -- place, this routine generates the individual assignments to components
211 -- of the designated object. This is an optimization over the general
212 -- case, where a temporary is first created on the stack and then used to
213 -- construct the allocated object on the heap.
215 procedure Convert_To_Positional
217 Max_Others_Replicate : Nat := 5;
218 Handle_Bit_Packed : Boolean := False);
219 -- If possible, convert named notation to positional notation. This
220 -- conversion is possible only in some static cases. If the conversion is
221 -- possible, then N is rewritten with the analyzed converted aggregate.
222 -- The parameter Max_Others_Replicate controls the maximum number of
223 -- values corresponding to an others choice that will be converted to
224 -- positional notation (the default of 5 is the normal limit, and reflects
225 -- the fact that normally the loop is better than a lot of separate
226 -- assignments). Note that this limit gets overridden in any case if
227 -- either of the restrictions No_Elaboration_Code or No_Implicit_Loops is
228 -- set. The parameter Handle_Bit_Packed is usually set False (since we do
229 -- not expect the back end to handle bit packed arrays, so the normal case
230 -- of conversion is pointless), but in the special case of a call from
231 -- Packed_Array_Aggregate_Handled, we set this parameter to True, since
232 -- these are cases we handle in there.
234 procedure Expand_Array_Aggregate (N : Node_Id);
235 -- This is the top-level routine to perform array aggregate expansion.
236 -- N is the N_Aggregate node to be expanded.
238 function Late_Expansion
241 Target : Node_Id) return List_Id;
242 -- This routine implements top-down expansion of nested aggregates. In
243 -- doing so, it avoids the generation of temporaries at each level. N is a
244 -- nested (record or array) aggregate that has been marked with 'Delay_
245 -- Expansion'. Typ is the expected type of the aggregate. Target is a
246 -- (duplicable) expression that will hold the result of the aggregate
249 function Make_OK_Assignment_Statement
252 Expression : Node_Id) return Node_Id;
253 -- This is like Make_Assignment_Statement, except that Assignment_OK
254 -- is set in the left operand. All assignments built by this unit
255 -- use this routine. This is needed to deal with assignments to
256 -- initialized constants that are done in place.
258 function Number_Of_Choices (N : Node_Id) return Nat;
259 -- Returns the number of discrete choices (not including the others choice
260 -- if present) contained in (sub-)aggregate N.
262 function Packed_Array_Aggregate_Handled (N : Node_Id) return Boolean;
263 -- Given an array aggregate, this function handles the case of a packed
264 -- array aggregate with all constant values, where the aggregate can be
265 -- evaluated at compile time. If this is possible, then N is rewritten
266 -- to be its proper compile time value with all the components properly
267 -- assembled. The expression is analyzed and resolved and True is
268 -- returned. If this transformation is not possible, N is unchanged
269 -- and False is returned
271 function Safe_Slice_Assignment (N : Node_Id) return Boolean;
272 -- If a slice assignment has an aggregate with a single others_choice,
273 -- the assignment can be done in place even if bounds are not static,
274 -- by converting it into a loop over the discrete range of the slice.
280 function Aggr_Size_OK (N : Node_Id; Typ : Entity_Id) return Boolean is
288 -- The following constant determines the maximum size of an array
289 -- aggregate produced by converting named to positional notation (e.g.
290 -- from others clauses). This avoids running away with attempts to
291 -- convert huge aggregates, which hit memory limits in the backend.
293 -- The normal limit is 5000, but we increase this limit to 2**24 (about
294 -- 16 million) if Restrictions (No_Elaboration_Code) or Restrictions
295 -- (No_Implicit_Loops) is specified, since in either case, we are at
296 -- risk of declaring the program illegal because of this limit.
298 Max_Aggr_Size : constant Nat :=
299 5000 + (2 ** 24 - 5000) *
301 (Restriction_Active (No_Elaboration_Code)
303 Restriction_Active (No_Implicit_Loops));
305 function Component_Count (T : Entity_Id) return Int;
306 -- The limit is applied to the total number of components that the
307 -- aggregate will have, which is the number of static expressions
308 -- that will appear in the flattened array. This requires a recursive
309 -- computation of the number of scalar components of the structure.
311 ---------------------
312 -- Component_Count --
313 ---------------------
315 function Component_Count (T : Entity_Id) return Int is
320 if Is_Scalar_Type (T) then
323 elsif Is_Record_Type (T) then
324 Comp := First_Component (T);
325 while Present (Comp) loop
326 Res := Res + Component_Count (Etype (Comp));
327 Next_Component (Comp);
332 elsif Is_Array_Type (T) then
334 Lo : constant Node_Id :=
335 Type_Low_Bound (Etype (First_Index (T)));
336 Hi : constant Node_Id :=
337 Type_High_Bound (Etype (First_Index (T)));
339 Siz : constant Int := Component_Count (Component_Type (T));
342 if not Compile_Time_Known_Value (Lo)
343 or else not Compile_Time_Known_Value (Hi)
348 Siz * UI_To_Int (Expr_Value (Hi) - Expr_Value (Lo) + 1);
353 -- Can only be a null for an access type
359 -- Start of processing for Aggr_Size_OK
362 Siz := Component_Count (Component_Type (Typ));
364 Indx := First_Index (Typ);
365 while Present (Indx) loop
366 Lo := Type_Low_Bound (Etype (Indx));
367 Hi := Type_High_Bound (Etype (Indx));
369 -- Bounds need to be known at compile time
371 if not Compile_Time_Known_Value (Lo)
372 or else not Compile_Time_Known_Value (Hi)
377 Lov := Expr_Value (Lo);
378 Hiv := Expr_Value (Hi);
380 -- A flat array is always safe
386 -- One-component aggregates are suspicious, and if the context type
387 -- is an object declaration with non-static bounds it will trip gcc;
388 -- such an aggregate must be expanded into a single assignment.
391 and then Nkind (Parent (N)) = N_Object_Declaration
394 Index_Type : constant Entity_Id :=
397 (Etype (Defining_Identifier (Parent (N)))));
401 if not Compile_Time_Known_Value (Type_Low_Bound (Index_Type))
402 or else not Compile_Time_Known_Value
403 (Type_High_Bound (Index_Type))
405 if Present (Component_Associations (N)) then
407 First (Choices (First (Component_Associations (N))));
408 if Is_Entity_Name (Indx)
409 and then not Is_Type (Entity (Indx))
412 ("single component aggregate in non-static context?",
414 Error_Msg_N ("\maybe subtype name was meant?", Indx);
424 Rng : constant Uint := Hiv - Lov + 1;
427 -- Check if size is too large
429 if not UI_Is_In_Int_Range (Rng) then
433 Siz := Siz * UI_To_Int (Rng);
437 or else Siz > Max_Aggr_Size
442 -- Bounds must be in integer range, for later array construction
444 if not UI_Is_In_Int_Range (Lov)
446 not UI_Is_In_Int_Range (Hiv)
457 ---------------------------------
458 -- Backend_Processing_Possible --
459 ---------------------------------
461 -- Backend processing by Gigi/gcc is possible only if all the following
462 -- conditions are met:
464 -- 1. N is fully positional
466 -- 2. N is not a bit-packed array aggregate;
468 -- 3. The size of N's array type must be known at compile time. Note
469 -- that this implies that the component size is also known
471 -- 4. The array type of N does not follow the Fortran layout convention
472 -- or if it does it must be 1 dimensional.
474 -- 5. The array component type may not be tagged (which could necessitate
475 -- reassignment of proper tags).
477 -- 6. The array component type must not have unaligned bit components
479 -- 7. None of the components of the aggregate may be bit unaligned
482 -- 8. There cannot be delayed components, since we do not know enough
483 -- at this stage to know if back end processing is possible.
485 -- 9. There cannot be any discriminated record components, since the
486 -- back end cannot handle this complex case.
488 -- 10. No controlled actions need to be generated for components
490 -- 11. For a VM back end, the array should have no aliased components
492 function Backend_Processing_Possible (N : Node_Id) return Boolean is
493 Typ : constant Entity_Id := Etype (N);
494 -- Typ is the correct constrained array subtype of the aggregate
496 function Component_Check (N : Node_Id; Index : Node_Id) return Boolean;
497 -- This routine checks components of aggregate N, enforcing checks
498 -- 1, 7, 8, and 9. In the multi-dimensional case, these checks are
499 -- performed on subaggregates. The Index value is the current index
500 -- being checked in the multi-dimensional case.
502 ---------------------
503 -- Component_Check --
504 ---------------------
506 function Component_Check (N : Node_Id; Index : Node_Id) return Boolean is
510 -- Checks 1: (no component associations)
512 if Present (Component_Associations (N)) then
516 -- Checks on components
518 -- Recurse to check subaggregates, which may appear in qualified
519 -- expressions. If delayed, the front-end will have to expand.
520 -- If the component is a discriminated record, treat as non-static,
521 -- as the back-end cannot handle this properly.
523 Expr := First (Expressions (N));
524 while Present (Expr) loop
526 -- Checks 8: (no delayed components)
528 if Is_Delayed_Aggregate (Expr) then
532 -- Checks 9: (no discriminated records)
534 if Present (Etype (Expr))
535 and then Is_Record_Type (Etype (Expr))
536 and then Has_Discriminants (Etype (Expr))
541 -- Checks 7. Component must not be bit aligned component
543 if Possible_Bit_Aligned_Component (Expr) then
547 -- Recursion to following indexes for multiple dimension case
549 if Present (Next_Index (Index))
550 and then not Component_Check (Expr, Next_Index (Index))
555 -- All checks for that component finished, on to next
563 -- Start of processing for Backend_Processing_Possible
566 -- Checks 2 (array not bit packed) and 10 (no controlled actions)
568 if Is_Bit_Packed_Array (Typ) or else Needs_Finalization (Typ) then
572 -- If component is limited, aggregate must be expanded because each
573 -- component assignment must be built in place.
575 if Is_Immutably_Limited_Type (Component_Type (Typ)) then
579 -- Checks 4 (array must not be multi-dimensional Fortran case)
581 if Convention (Typ) = Convention_Fortran
582 and then Number_Dimensions (Typ) > 1
587 -- Checks 3 (size of array must be known at compile time)
589 if not Size_Known_At_Compile_Time (Typ) then
593 -- Checks on components
595 if not Component_Check (N, First_Index (Typ)) then
599 -- Checks 5 (if the component type is tagged, then we may need to do
600 -- tag adjustments. Perhaps this should be refined to check for any
601 -- component associations that actually need tag adjustment, similar
602 -- to the test in Component_Not_OK_For_Backend for record aggregates
603 -- with tagged components, but not clear whether it's worthwhile ???;
604 -- in the case of the JVM, object tags are handled implicitly)
606 if Is_Tagged_Type (Component_Type (Typ))
607 and then Tagged_Type_Expansion
612 -- Checks 6 (component type must not have bit aligned components)
614 if Type_May_Have_Bit_Aligned_Components (Component_Type (Typ)) then
618 -- Checks 11: Array aggregates with aliased components are currently
619 -- not well supported by the VM backend; disable temporarily this
620 -- backend processing until it is definitely supported.
622 if VM_Target /= No_VM
623 and then Has_Aliased_Components (Base_Type (Typ))
628 -- Backend processing is possible
630 Set_Size_Known_At_Compile_Time (Etype (N), True);
632 end Backend_Processing_Possible;
634 ---------------------------
635 -- Build_Array_Aggr_Code --
636 ---------------------------
638 -- The code that we generate from a one dimensional aggregate is
640 -- 1. If the sub-aggregate contains discrete choices we
642 -- (a) Sort the discrete choices
644 -- (b) Otherwise for each discrete choice that specifies a range we
645 -- emit a loop. If a range specifies a maximum of three values, or
646 -- we are dealing with an expression we emit a sequence of
647 -- assignments instead of a loop.
649 -- (c) Generate the remaining loops to cover the others choice if any
651 -- 2. If the aggregate contains positional elements we
653 -- (a) translate the positional elements in a series of assignments
655 -- (b) Generate a final loop to cover the others choice if any.
656 -- Note that this final loop has to be a while loop since the case
658 -- L : Integer := Integer'Last;
659 -- H : Integer := Integer'Last;
660 -- A : array (L .. H) := (1, others =>0);
662 -- cannot be handled by a for loop. Thus for the following
664 -- array (L .. H) := (.. positional elements.., others =>E);
666 -- we always generate something like:
668 -- J : Index_Type := Index_Of_Last_Positional_Element;
670 -- J := Index_Base'Succ (J)
674 function Build_Array_Aggr_Code
679 Scalar_Comp : Boolean;
680 Indexes : List_Id := No_List) return List_Id
682 Loc : constant Source_Ptr := Sloc (N);
683 Index_Base : constant Entity_Id := Base_Type (Etype (Index));
684 Index_Base_L : constant Node_Id := Type_Low_Bound (Index_Base);
685 Index_Base_H : constant Node_Id := Type_High_Bound (Index_Base);
687 function Add (Val : Int; To : Node_Id) return Node_Id;
688 -- Returns an expression where Val is added to expression To, unless
689 -- To+Val is provably out of To's base type range. To must be an
690 -- already analyzed expression.
692 function Empty_Range (L, H : Node_Id) return Boolean;
693 -- Returns True if the range defined by L .. H is certainly empty
695 function Equal (L, H : Node_Id) return Boolean;
696 -- Returns True if L = H for sure
698 function Index_Base_Name return Node_Id;
699 -- Returns a new reference to the index type name
701 function Gen_Assign (Ind : Node_Id; Expr : Node_Id) return List_Id;
702 -- Ind must be a side-effect free expression. If the input aggregate
703 -- N to Build_Loop contains no sub-aggregates, then this function
704 -- returns the assignment statement:
706 -- Into (Indexes, Ind) := Expr;
708 -- Otherwise we call Build_Code recursively
710 -- Ada 2005 (AI-287): In case of default initialized component, Expr
711 -- is empty and we generate a call to the corresponding IP subprogram.
713 function Gen_Loop (L, H : Node_Id; Expr : Node_Id) return List_Id;
714 -- Nodes L and H must be side-effect free expressions.
715 -- If the input aggregate N to Build_Loop contains no sub-aggregates,
716 -- This routine returns the for loop statement
718 -- for J in Index_Base'(L) .. Index_Base'(H) loop
719 -- Into (Indexes, J) := Expr;
722 -- Otherwise we call Build_Code recursively.
723 -- As an optimization if the loop covers 3 or less scalar elements we
724 -- generate a sequence of assignments.
726 function Gen_While (L, H : Node_Id; Expr : Node_Id) return List_Id;
727 -- Nodes L and H must be side-effect free expressions.
728 -- If the input aggregate N to Build_Loop contains no sub-aggregates,
729 -- This routine returns the while loop statement
731 -- J : Index_Base := L;
733 -- J := Index_Base'Succ (J);
734 -- Into (Indexes, J) := Expr;
737 -- Otherwise we call Build_Code recursively
739 function Local_Compile_Time_Known_Value (E : Node_Id) return Boolean;
740 function Local_Expr_Value (E : Node_Id) return Uint;
741 -- These two Local routines are used to replace the corresponding ones
742 -- in sem_eval because while processing the bounds of an aggregate with
743 -- discrete choices whose index type is an enumeration, we build static
744 -- expressions not recognized by Compile_Time_Known_Value as such since
745 -- they have not yet been analyzed and resolved. All the expressions in
746 -- question are things like Index_Base_Name'Val (Const) which we can
747 -- easily recognize as being constant.
753 function Add (Val : Int; To : Node_Id) return Node_Id is
758 U_Val : constant Uint := UI_From_Int (Val);
761 -- Note: do not try to optimize the case of Val = 0, because
762 -- we need to build a new node with the proper Sloc value anyway.
764 -- First test if we can do constant folding
766 if Local_Compile_Time_Known_Value (To) then
767 U_To := Local_Expr_Value (To) + Val;
769 -- Determine if our constant is outside the range of the index.
770 -- If so return an Empty node. This empty node will be caught
771 -- by Empty_Range below.
773 if Compile_Time_Known_Value (Index_Base_L)
774 and then U_To < Expr_Value (Index_Base_L)
778 elsif Compile_Time_Known_Value (Index_Base_H)
779 and then U_To > Expr_Value (Index_Base_H)
784 Expr_Pos := Make_Integer_Literal (Loc, U_To);
785 Set_Is_Static_Expression (Expr_Pos);
787 if not Is_Enumeration_Type (Index_Base) then
790 -- If we are dealing with enumeration return
791 -- Index_Base'Val (Expr_Pos)
795 Make_Attribute_Reference
797 Prefix => Index_Base_Name,
798 Attribute_Name => Name_Val,
799 Expressions => New_List (Expr_Pos));
805 -- If we are here no constant folding possible
807 if not Is_Enumeration_Type (Index_Base) then
810 Left_Opnd => Duplicate_Subexpr (To),
811 Right_Opnd => Make_Integer_Literal (Loc, U_Val));
813 -- If we are dealing with enumeration return
814 -- Index_Base'Val (Index_Base'Pos (To) + Val)
818 Make_Attribute_Reference
820 Prefix => Index_Base_Name,
821 Attribute_Name => Name_Pos,
822 Expressions => New_List (Duplicate_Subexpr (To)));
827 Right_Opnd => Make_Integer_Literal (Loc, U_Val));
830 Make_Attribute_Reference
832 Prefix => Index_Base_Name,
833 Attribute_Name => Name_Val,
834 Expressions => New_List (Expr_Pos));
844 function Empty_Range (L, H : Node_Id) return Boolean is
845 Is_Empty : Boolean := False;
850 -- First check if L or H were already detected as overflowing the
851 -- index base range type by function Add above. If this is so Add
852 -- returns the empty node.
854 if No (L) or else No (H) then
861 -- L > H range is empty
867 -- B_L > H range must be empty
873 -- L > B_H range must be empty
877 High := Index_Base_H;
880 if Local_Compile_Time_Known_Value (Low)
881 and then Local_Compile_Time_Known_Value (High)
884 UI_Gt (Local_Expr_Value (Low), Local_Expr_Value (High));
897 function Equal (L, H : Node_Id) return Boolean is
902 elsif Local_Compile_Time_Known_Value (L)
903 and then Local_Compile_Time_Known_Value (H)
905 return UI_Eq (Local_Expr_Value (L), Local_Expr_Value (H));
915 function Gen_Assign (Ind : Node_Id; Expr : Node_Id) return List_Id is
916 L : constant List_Id := New_List;
919 New_Indexes : List_Id;
920 Indexed_Comp : Node_Id;
922 Comp_Type : Entity_Id := Empty;
924 function Add_Loop_Actions (Lis : List_Id) return List_Id;
925 -- Collect insert_actions generated in the construction of a
926 -- loop, and prepend them to the sequence of assignments to
927 -- complete the eventual body of the loop.
929 ----------------------
930 -- Add_Loop_Actions --
931 ----------------------
933 function Add_Loop_Actions (Lis : List_Id) return List_Id is
937 -- Ada 2005 (AI-287): Do nothing else in case of default
938 -- initialized component.
943 elsif Nkind (Parent (Expr)) = N_Component_Association
944 and then Present (Loop_Actions (Parent (Expr)))
946 Append_List (Lis, Loop_Actions (Parent (Expr)));
947 Res := Loop_Actions (Parent (Expr));
948 Set_Loop_Actions (Parent (Expr), No_List);
954 end Add_Loop_Actions;
956 -- Start of processing for Gen_Assign
960 New_Indexes := New_List;
962 New_Indexes := New_Copy_List_Tree (Indexes);
965 Append_To (New_Indexes, Ind);
967 if Present (Next_Index (Index)) then
970 Build_Array_Aggr_Code
973 Index => Next_Index (Index),
975 Scalar_Comp => Scalar_Comp,
976 Indexes => New_Indexes));
979 -- If we get here then we are at a bottom-level (sub-)aggregate
983 (Make_Indexed_Component (Loc,
984 Prefix => New_Copy_Tree (Into),
985 Expressions => New_Indexes));
987 Set_Assignment_OK (Indexed_Comp);
989 -- Ada 2005 (AI-287): In case of default initialized component, Expr
990 -- is not present (and therefore we also initialize Expr_Q to empty).
994 elsif Nkind (Expr) = N_Qualified_Expression then
995 Expr_Q := Expression (Expr);
1000 if Present (Etype (N))
1001 and then Etype (N) /= Any_Composite
1003 Comp_Type := Component_Type (Etype (N));
1004 pragma Assert (Comp_Type = Ctype); -- AI-287
1006 elsif Present (Next (First (New_Indexes))) then
1008 -- Ada 2005 (AI-287): Do nothing in case of default initialized
1009 -- component because we have received the component type in
1010 -- the formal parameter Ctype.
1012 -- ??? Some assert pragmas have been added to check if this new
1013 -- formal can be used to replace this code in all cases.
1015 if Present (Expr) then
1017 -- This is a multidimensional array. Recover the component
1018 -- type from the outermost aggregate, because subaggregates
1019 -- do not have an assigned type.
1026 while Present (P) loop
1027 if Nkind (P) = N_Aggregate
1028 and then Present (Etype (P))
1030 Comp_Type := Component_Type (Etype (P));
1038 pragma Assert (Comp_Type = Ctype); -- AI-287
1043 -- Ada 2005 (AI-287): We only analyze the expression in case of non-
1044 -- default initialized components (otherwise Expr_Q is not present).
1047 and then Nkind_In (Expr_Q, N_Aggregate, N_Extension_Aggregate)
1049 -- At this stage the Expression may not have been analyzed yet
1050 -- because the array aggregate code has not been updated to use
1051 -- the Expansion_Delayed flag and avoid analysis altogether to
1052 -- solve the same problem (see Resolve_Aggr_Expr). So let us do
1053 -- the analysis of non-array aggregates now in order to get the
1054 -- value of Expansion_Delayed flag for the inner aggregate ???
1056 if Present (Comp_Type) and then not Is_Array_Type (Comp_Type) then
1057 Analyze_And_Resolve (Expr_Q, Comp_Type);
1060 if Is_Delayed_Aggregate (Expr_Q) then
1062 -- This is either a subaggregate of a multidimensional array,
1063 -- or a component of an array type whose component type is
1064 -- also an array. In the latter case, the expression may have
1065 -- component associations that provide different bounds from
1066 -- those of the component type, and sliding must occur. Instead
1067 -- of decomposing the current aggregate assignment, force the
1068 -- re-analysis of the assignment, so that a temporary will be
1069 -- generated in the usual fashion, and sliding will take place.
1071 if Nkind (Parent (N)) = N_Assignment_Statement
1072 and then Is_Array_Type (Comp_Type)
1073 and then Present (Component_Associations (Expr_Q))
1074 and then Must_Slide (Comp_Type, Etype (Expr_Q))
1076 Set_Expansion_Delayed (Expr_Q, False);
1077 Set_Analyzed (Expr_Q, False);
1082 Late_Expansion (Expr_Q, Etype (Expr_Q), Indexed_Comp));
1087 -- Ada 2005 (AI-287): In case of default initialized component, call
1088 -- the initialization subprogram associated with the component type.
1089 -- If the component type is an access type, add an explicit null
1090 -- assignment, because for the back-end there is an initialization
1091 -- present for the whole aggregate, and no default initialization
1094 -- In addition, if the component type is controlled, we must call
1095 -- its Initialize procedure explicitly, because there is no explicit
1096 -- object creation that will invoke it otherwise.
1099 if Present (Base_Init_Proc (Base_Type (Ctype)))
1100 or else Has_Task (Base_Type (Ctype))
1103 Build_Initialization_Call (Loc,
1104 Id_Ref => Indexed_Comp,
1106 With_Default_Init => True));
1108 elsif Is_Access_Type (Ctype) then
1110 Make_Assignment_Statement (Loc,
1111 Name => Indexed_Comp,
1112 Expression => Make_Null (Loc)));
1115 if Needs_Finalization (Ctype) then
1118 Obj_Ref => New_Copy_Tree (Indexed_Comp),
1123 -- Now generate the assignment with no associated controlled
1124 -- actions since the target of the assignment may not have been
1125 -- initialized, it is not possible to Finalize it as expected by
1126 -- normal controlled assignment. The rest of the controlled
1127 -- actions are done manually with the proper finalization list
1128 -- coming from the context.
1131 Make_OK_Assignment_Statement (Loc,
1132 Name => Indexed_Comp,
1133 Expression => New_Copy_Tree (Expr));
1135 if Present (Comp_Type) and then Needs_Finalization (Comp_Type) then
1136 Set_No_Ctrl_Actions (A);
1138 -- If this is an aggregate for an array of arrays, each
1139 -- sub-aggregate will be expanded as well, and even with
1140 -- No_Ctrl_Actions the assignments of inner components will
1141 -- require attachment in their assignments to temporaries.
1142 -- These temporaries must be finalized for each subaggregate,
1143 -- to prevent multiple attachments of the same temporary
1144 -- location to same finalization chain (and consequently
1145 -- circular lists). To ensure that finalization takes place
1146 -- for each subaggregate we wrap the assignment in a block.
1148 if Is_Array_Type (Comp_Type)
1149 and then Nkind (Expr) = N_Aggregate
1152 Make_Block_Statement (Loc,
1153 Handled_Statement_Sequence =>
1154 Make_Handled_Sequence_Of_Statements (Loc,
1155 Statements => New_List (A)));
1161 -- Adjust the tag if tagged (because of possible view
1162 -- conversions), unless compiling for a VM where
1163 -- tags are implicit.
1165 if Present (Comp_Type)
1166 and then Is_Tagged_Type (Comp_Type)
1167 and then Tagged_Type_Expansion
1170 Full_Typ : constant Entity_Id := Underlying_Type (Comp_Type);
1174 Make_OK_Assignment_Statement (Loc,
1176 Make_Selected_Component (Loc,
1177 Prefix => New_Copy_Tree (Indexed_Comp),
1180 (First_Tag_Component (Full_Typ), Loc)),
1183 Unchecked_Convert_To (RTE (RE_Tag),
1185 (Node (First_Elmt (Access_Disp_Table (Full_Typ))),
1192 -- Adjust and attach the component to the proper final list, which
1193 -- can be the controller of the outer record object or the final
1194 -- list associated with the scope.
1196 -- If the component is itself an array of controlled types, whose
1197 -- value is given by a sub-aggregate, then the attach calls have
1198 -- been generated when individual subcomponent are assigned, and
1199 -- must not be done again to prevent malformed finalization chains
1200 -- (see comments above, concerning the creation of a block to hold
1201 -- inner finalization actions).
1203 if Present (Comp_Type)
1204 and then Needs_Finalization (Comp_Type)
1205 and then not Is_Limited_Type (Comp_Type)
1207 (Is_Array_Type (Comp_Type)
1208 and then Is_Controlled (Component_Type (Comp_Type))
1209 and then Nkind (Expr) = N_Aggregate)
1213 Obj_Ref => New_Copy_Tree (Indexed_Comp),
1218 return Add_Loop_Actions (L);
1225 function Gen_Loop (L, H : Node_Id; Expr : Node_Id) return List_Id is
1235 -- Index_Base'(L) .. Index_Base'(H)
1237 L_Iteration_Scheme : Node_Id;
1238 -- L_J in Index_Base'(L) .. Index_Base'(H)
1241 -- The statements to execute in the loop
1243 S : constant List_Id := New_List;
1244 -- List of statements
1247 -- Copy of expression tree, used for checking purposes
1250 -- If loop bounds define an empty range return the null statement
1252 if Empty_Range (L, H) then
1253 Append_To (S, Make_Null_Statement (Loc));
1255 -- Ada 2005 (AI-287): Nothing else need to be done in case of
1256 -- default initialized component.
1262 -- The expression must be type-checked even though no component
1263 -- of the aggregate will have this value. This is done only for
1264 -- actual components of the array, not for subaggregates. Do
1265 -- the check on a copy, because the expression may be shared
1266 -- among several choices, some of which might be non-null.
1268 if Present (Etype (N))
1269 and then Is_Array_Type (Etype (N))
1270 and then No (Next_Index (Index))
1272 Expander_Mode_Save_And_Set (False);
1273 Tcopy := New_Copy_Tree (Expr);
1274 Set_Parent (Tcopy, N);
1275 Analyze_And_Resolve (Tcopy, Component_Type (Etype (N)));
1276 Expander_Mode_Restore;
1282 -- If loop bounds are the same then generate an assignment
1284 elsif Equal (L, H) then
1285 return Gen_Assign (New_Copy_Tree (L), Expr);
1287 -- If H - L <= 2 then generate a sequence of assignments when we are
1288 -- processing the bottom most aggregate and it contains scalar
1291 elsif No (Next_Index (Index))
1292 and then Scalar_Comp
1293 and then Local_Compile_Time_Known_Value (L)
1294 and then Local_Compile_Time_Known_Value (H)
1295 and then Local_Expr_Value (H) - Local_Expr_Value (L) <= 2
1298 Append_List_To (S, Gen_Assign (New_Copy_Tree (L), Expr));
1299 Append_List_To (S, Gen_Assign (Add (1, To => L), Expr));
1301 if Local_Expr_Value (H) - Local_Expr_Value (L) = 2 then
1302 Append_List_To (S, Gen_Assign (Add (2, To => L), Expr));
1308 -- Otherwise construct the loop, starting with the loop index L_J
1310 L_J := Make_Temporary (Loc, 'J', L);
1312 -- Construct "L .. H" in Index_Base. We use a qualified expression
1313 -- for the bound to convert to the index base, but we don't need
1314 -- to do that if we already have the base type at hand.
1316 if Etype (L) = Index_Base then
1320 Make_Qualified_Expression (Loc,
1321 Subtype_Mark => Index_Base_Name,
1325 if Etype (H) = Index_Base then
1329 Make_Qualified_Expression (Loc,
1330 Subtype_Mark => Index_Base_Name,
1339 -- Construct "for L_J in Index_Base range L .. H"
1341 L_Iteration_Scheme :=
1342 Make_Iteration_Scheme
1344 Loop_Parameter_Specification =>
1345 Make_Loop_Parameter_Specification
1347 Defining_Identifier => L_J,
1348 Discrete_Subtype_Definition => L_Range));
1350 -- Construct the statements to execute in the loop body
1352 L_Body := Gen_Assign (New_Reference_To (L_J, Loc), Expr);
1354 -- Construct the final loop
1356 Append_To (S, Make_Implicit_Loop_Statement
1358 Identifier => Empty,
1359 Iteration_Scheme => L_Iteration_Scheme,
1360 Statements => L_Body));
1362 -- A small optimization: if the aggregate is initialized with a box
1363 -- and the component type has no initialization procedure, remove the
1364 -- useless empty loop.
1366 if Nkind (First (S)) = N_Loop_Statement
1367 and then Is_Empty_List (Statements (First (S)))
1369 return New_List (Make_Null_Statement (Loc));
1379 -- The code built is
1381 -- W_J : Index_Base := L;
1382 -- while W_J < H loop
1383 -- W_J := Index_Base'Succ (W);
1387 function Gen_While (L, H : Node_Id; Expr : Node_Id) return List_Id is
1391 -- W_J : Base_Type := L;
1393 W_Iteration_Scheme : Node_Id;
1396 W_Index_Succ : Node_Id;
1397 -- Index_Base'Succ (J)
1399 W_Increment : Node_Id;
1400 -- W_J := Index_Base'Succ (W)
1402 W_Body : constant List_Id := New_List;
1403 -- The statements to execute in the loop
1405 S : constant List_Id := New_List;
1406 -- list of statement
1409 -- If loop bounds define an empty range or are equal return null
1411 if Empty_Range (L, H) or else Equal (L, H) then
1412 Append_To (S, Make_Null_Statement (Loc));
1416 -- Build the decl of W_J
1418 W_J := Make_Temporary (Loc, 'J', L);
1420 Make_Object_Declaration
1422 Defining_Identifier => W_J,
1423 Object_Definition => Index_Base_Name,
1426 -- Theoretically we should do a New_Copy_Tree (L) here, but we know
1427 -- that in this particular case L is a fresh Expr generated by
1428 -- Add which we are the only ones to use.
1430 Append_To (S, W_Decl);
1432 -- Construct " while W_J < H"
1434 W_Iteration_Scheme :=
1435 Make_Iteration_Scheme
1437 Condition => Make_Op_Lt
1439 Left_Opnd => New_Reference_To (W_J, Loc),
1440 Right_Opnd => New_Copy_Tree (H)));
1442 -- Construct the statements to execute in the loop body
1445 Make_Attribute_Reference
1447 Prefix => Index_Base_Name,
1448 Attribute_Name => Name_Succ,
1449 Expressions => New_List (New_Reference_To (W_J, Loc)));
1452 Make_OK_Assignment_Statement
1454 Name => New_Reference_To (W_J, Loc),
1455 Expression => W_Index_Succ);
1457 Append_To (W_Body, W_Increment);
1458 Append_List_To (W_Body,
1459 Gen_Assign (New_Reference_To (W_J, Loc), Expr));
1461 -- Construct the final loop
1463 Append_To (S, Make_Implicit_Loop_Statement
1465 Identifier => Empty,
1466 Iteration_Scheme => W_Iteration_Scheme,
1467 Statements => W_Body));
1472 ---------------------
1473 -- Index_Base_Name --
1474 ---------------------
1476 function Index_Base_Name return Node_Id is
1478 return New_Reference_To (Index_Base, Sloc (N));
1479 end Index_Base_Name;
1481 ------------------------------------
1482 -- Local_Compile_Time_Known_Value --
1483 ------------------------------------
1485 function Local_Compile_Time_Known_Value (E : Node_Id) return Boolean is
1487 return Compile_Time_Known_Value (E)
1489 (Nkind (E) = N_Attribute_Reference
1490 and then Attribute_Name (E) = Name_Val
1491 and then Compile_Time_Known_Value (First (Expressions (E))));
1492 end Local_Compile_Time_Known_Value;
1494 ----------------------
1495 -- Local_Expr_Value --
1496 ----------------------
1498 function Local_Expr_Value (E : Node_Id) return Uint is
1500 if Compile_Time_Known_Value (E) then
1501 return Expr_Value (E);
1503 return Expr_Value (First (Expressions (E)));
1505 end Local_Expr_Value;
1507 -- Build_Array_Aggr_Code Variables
1514 Others_Expr : Node_Id := Empty;
1515 Others_Box_Present : Boolean := False;
1517 Aggr_L : constant Node_Id := Low_Bound (Aggregate_Bounds (N));
1518 Aggr_H : constant Node_Id := High_Bound (Aggregate_Bounds (N));
1519 -- The aggregate bounds of this specific sub-aggregate. Note that if
1520 -- the code generated by Build_Array_Aggr_Code is executed then these
1521 -- bounds are OK. Otherwise a Constraint_Error would have been raised.
1523 Aggr_Low : constant Node_Id := Duplicate_Subexpr_No_Checks (Aggr_L);
1524 Aggr_High : constant Node_Id := Duplicate_Subexpr_No_Checks (Aggr_H);
1525 -- After Duplicate_Subexpr these are side-effect free
1530 Nb_Choices : Nat := 0;
1531 Table : Case_Table_Type (1 .. Number_Of_Choices (N));
1532 -- Used to sort all the different choice values
1535 -- Number of elements in the positional aggregate
1537 New_Code : constant List_Id := New_List;
1539 -- Start of processing for Build_Array_Aggr_Code
1542 -- First before we start, a special case. if we have a bit packed
1543 -- array represented as a modular type, then clear the value to
1544 -- zero first, to ensure that unused bits are properly cleared.
1549 and then Is_Bit_Packed_Array (Typ)
1550 and then Is_Modular_Integer_Type (Packed_Array_Type (Typ))
1552 Append_To (New_Code,
1553 Make_Assignment_Statement (Loc,
1554 Name => New_Copy_Tree (Into),
1556 Unchecked_Convert_To (Typ,
1557 Make_Integer_Literal (Loc, Uint_0))));
1560 -- If the component type contains tasks, we need to build a Master
1561 -- entity in the current scope, because it will be needed if build-
1562 -- in-place functions are called in the expanded code.
1564 if Nkind (Parent (N)) = N_Object_Declaration
1565 and then Has_Task (Typ)
1567 Build_Master_Entity (Defining_Identifier (Parent (N)));
1570 -- STEP 1: Process component associations
1572 -- For those associations that may generate a loop, initialize
1573 -- Loop_Actions to collect inserted actions that may be crated.
1575 -- Skip this if no component associations
1577 if No (Expressions (N)) then
1579 -- STEP 1 (a): Sort the discrete choices
1581 Assoc := First (Component_Associations (N));
1582 while Present (Assoc) loop
1583 Choice := First (Choices (Assoc));
1584 while Present (Choice) loop
1585 if Nkind (Choice) = N_Others_Choice then
1586 Set_Loop_Actions (Assoc, New_List);
1588 if Box_Present (Assoc) then
1589 Others_Box_Present := True;
1591 Others_Expr := Expression (Assoc);
1596 Get_Index_Bounds (Choice, Low, High);
1599 Set_Loop_Actions (Assoc, New_List);
1602 Nb_Choices := Nb_Choices + 1;
1603 if Box_Present (Assoc) then
1604 Table (Nb_Choices) := (Choice_Lo => Low,
1606 Choice_Node => Empty);
1608 Table (Nb_Choices) := (Choice_Lo => Low,
1610 Choice_Node => Expression (Assoc));
1618 -- If there is more than one set of choices these must be static
1619 -- and we can therefore sort them. Remember that Nb_Choices does not
1620 -- account for an others choice.
1622 if Nb_Choices > 1 then
1623 Sort_Case_Table (Table);
1626 -- STEP 1 (b): take care of the whole set of discrete choices
1628 for J in 1 .. Nb_Choices loop
1629 Low := Table (J).Choice_Lo;
1630 High := Table (J).Choice_Hi;
1631 Expr := Table (J).Choice_Node;
1632 Append_List (Gen_Loop (Low, High, Expr), To => New_Code);
1635 -- STEP 1 (c): generate the remaining loops to cover others choice
1636 -- We don't need to generate loops over empty gaps, but if there is
1637 -- a single empty range we must analyze the expression for semantics
1639 if Present (Others_Expr) or else Others_Box_Present then
1641 First : Boolean := True;
1644 for J in 0 .. Nb_Choices loop
1648 Low := Add (1, To => Table (J).Choice_Hi);
1651 if J = Nb_Choices then
1654 High := Add (-1, To => Table (J + 1).Choice_Lo);
1657 -- If this is an expansion within an init proc, make
1658 -- sure that discriminant references are replaced by
1659 -- the corresponding discriminal.
1661 if Inside_Init_Proc then
1662 if Is_Entity_Name (Low)
1663 and then Ekind (Entity (Low)) = E_Discriminant
1665 Set_Entity (Low, Discriminal (Entity (Low)));
1668 if Is_Entity_Name (High)
1669 and then Ekind (Entity (High)) = E_Discriminant
1671 Set_Entity (High, Discriminal (Entity (High)));
1676 or else not Empty_Range (Low, High)
1680 (Gen_Loop (Low, High, Others_Expr), To => New_Code);
1686 -- STEP 2: Process positional components
1689 -- STEP 2 (a): Generate the assignments for each positional element
1690 -- Note that here we have to use Aggr_L rather than Aggr_Low because
1691 -- Aggr_L is analyzed and Add wants an analyzed expression.
1693 Expr := First (Expressions (N));
1695 while Present (Expr) loop
1696 Nb_Elements := Nb_Elements + 1;
1697 Append_List (Gen_Assign (Add (Nb_Elements, To => Aggr_L), Expr),
1702 -- STEP 2 (b): Generate final loop if an others choice is present
1703 -- Here Nb_Elements gives the offset of the last positional element.
1705 if Present (Component_Associations (N)) then
1706 Assoc := Last (Component_Associations (N));
1708 -- Ada 2005 (AI-287)
1710 if Box_Present (Assoc) then
1711 Append_List (Gen_While (Add (Nb_Elements, To => Aggr_L),
1716 Expr := Expression (Assoc);
1718 Append_List (Gen_While (Add (Nb_Elements, To => Aggr_L),
1727 end Build_Array_Aggr_Code;
1729 ----------------------------
1730 -- Build_Record_Aggr_Code --
1731 ----------------------------
1733 function Build_Record_Aggr_Code
1737 Is_Limited_Ancestor_Expansion : Boolean := False) return List_Id
1739 Loc : constant Source_Ptr := Sloc (N);
1740 L : constant List_Id := New_List;
1741 N_Typ : constant Entity_Id := Etype (N);
1747 Comp_Type : Entity_Id;
1748 Selector : Entity_Id;
1749 Comp_Expr : Node_Id;
1752 -- If this is an internal aggregate, the External_Final_List is an
1753 -- expression for the controller record of the enclosing type.
1755 -- If the current aggregate has several controlled components, this
1756 -- expression will appear in several calls to attach to the finali-
1757 -- zation list, and it must not be shared.
1759 Ancestor_Is_Expression : Boolean := False;
1760 Ancestor_Is_Subtype_Mark : Boolean := False;
1762 Init_Typ : Entity_Id := Empty;
1764 Finalization_Done : Boolean := False;
1765 -- True if Generate_Finalization_Actions has already been called; calls
1766 -- after the first do nothing.
1768 function Ancestor_Discriminant_Value (Disc : Entity_Id) return Node_Id;
1769 -- Returns the value that the given discriminant of an ancestor type
1770 -- should receive (in the absence of a conflict with the value provided
1771 -- by an ancestor part of an extension aggregate).
1773 procedure Check_Ancestor_Discriminants (Anc_Typ : Entity_Id);
1774 -- Check that each of the discriminant values defined by the ancestor
1775 -- part of an extension aggregate match the corresponding values
1776 -- provided by either an association of the aggregate or by the
1777 -- constraint imposed by a parent type (RM95-4.3.2(8)).
1779 function Compatible_Int_Bounds
1780 (Agg_Bounds : Node_Id;
1781 Typ_Bounds : Node_Id) return Boolean;
1782 -- Return true if Agg_Bounds are equal or within Typ_Bounds. It is
1783 -- assumed that both bounds are integer ranges.
1785 procedure Generate_Finalization_Actions;
1786 -- Deal with the various controlled type data structure initializations
1787 -- (but only if it hasn't been done already).
1789 function Get_Constraint_Association (T : Entity_Id) return Node_Id;
1790 -- Returns the first discriminant association in the constraint
1791 -- associated with T, if any, otherwise returns Empty.
1793 procedure Init_Hidden_Discriminants (Typ : Entity_Id; List : List_Id);
1794 -- If Typ is derived, and constrains discriminants of the parent type,
1795 -- these discriminants are not components of the aggregate, and must be
1796 -- initialized. The assignments are appended to List.
1798 function Is_Int_Range_Bounds (Bounds : Node_Id) return Boolean;
1799 -- Check whether Bounds is a range node and its lower and higher bounds
1800 -- are integers literals.
1802 ---------------------------------
1803 -- Ancestor_Discriminant_Value --
1804 ---------------------------------
1806 function Ancestor_Discriminant_Value (Disc : Entity_Id) return Node_Id is
1808 Assoc_Elmt : Elmt_Id;
1809 Aggr_Comp : Entity_Id;
1810 Corresp_Disc : Entity_Id;
1811 Current_Typ : Entity_Id := Base_Type (Typ);
1812 Parent_Typ : Entity_Id;
1813 Parent_Disc : Entity_Id;
1814 Save_Assoc : Node_Id := Empty;
1817 -- First check any discriminant associations to see if any of them
1818 -- provide a value for the discriminant.
1820 if Present (Discriminant_Specifications (Parent (Current_Typ))) then
1821 Assoc := First (Component_Associations (N));
1822 while Present (Assoc) loop
1823 Aggr_Comp := Entity (First (Choices (Assoc)));
1825 if Ekind (Aggr_Comp) = E_Discriminant then
1826 Save_Assoc := Expression (Assoc);
1828 Corresp_Disc := Corresponding_Discriminant (Aggr_Comp);
1829 while Present (Corresp_Disc) loop
1831 -- If found a corresponding discriminant then return the
1832 -- value given in the aggregate. (Note: this is not
1833 -- correct in the presence of side effects. ???)
1835 if Disc = Corresp_Disc then
1836 return Duplicate_Subexpr (Expression (Assoc));
1840 Corresponding_Discriminant (Corresp_Disc);
1848 -- No match found in aggregate, so chain up parent types to find
1849 -- a constraint that defines the value of the discriminant.
1851 Parent_Typ := Etype (Current_Typ);
1852 while Current_Typ /= Parent_Typ loop
1853 if Has_Discriminants (Parent_Typ)
1854 and then not Has_Unknown_Discriminants (Parent_Typ)
1856 Parent_Disc := First_Discriminant (Parent_Typ);
1858 -- We either get the association from the subtype indication
1859 -- of the type definition itself, or from the discriminant
1860 -- constraint associated with the type entity (which is
1861 -- preferable, but it's not always present ???)
1863 if Is_Empty_Elmt_List (
1864 Discriminant_Constraint (Current_Typ))
1866 Assoc := Get_Constraint_Association (Current_Typ);
1867 Assoc_Elmt := No_Elmt;
1870 First_Elmt (Discriminant_Constraint (Current_Typ));
1871 Assoc := Node (Assoc_Elmt);
1874 -- Traverse the discriminants of the parent type looking
1875 -- for one that corresponds.
1877 while Present (Parent_Disc) and then Present (Assoc) loop
1878 Corresp_Disc := Parent_Disc;
1879 while Present (Corresp_Disc)
1880 and then Disc /= Corresp_Disc
1883 Corresponding_Discriminant (Corresp_Disc);
1886 if Disc = Corresp_Disc then
1887 if Nkind (Assoc) = N_Discriminant_Association then
1888 Assoc := Expression (Assoc);
1891 -- If the located association directly denotes a
1892 -- discriminant, then use the value of a saved
1893 -- association of the aggregate. This is a kludge to
1894 -- handle certain cases involving multiple discriminants
1895 -- mapped to a single discriminant of a descendant. It's
1896 -- not clear how to locate the appropriate discriminant
1897 -- value for such cases. ???
1899 if Is_Entity_Name (Assoc)
1900 and then Ekind (Entity (Assoc)) = E_Discriminant
1902 Assoc := Save_Assoc;
1905 return Duplicate_Subexpr (Assoc);
1908 Next_Discriminant (Parent_Disc);
1910 if No (Assoc_Elmt) then
1913 Next_Elmt (Assoc_Elmt);
1914 if Present (Assoc_Elmt) then
1915 Assoc := Node (Assoc_Elmt);
1923 Current_Typ := Parent_Typ;
1924 Parent_Typ := Etype (Current_Typ);
1927 -- In some cases there's no ancestor value to locate (such as
1928 -- when an ancestor part given by an expression defines the
1929 -- discriminant value).
1932 end Ancestor_Discriminant_Value;
1934 ----------------------------------
1935 -- Check_Ancestor_Discriminants --
1936 ----------------------------------
1938 procedure Check_Ancestor_Discriminants (Anc_Typ : Entity_Id) is
1940 Disc_Value : Node_Id;
1944 Discr := First_Discriminant (Base_Type (Anc_Typ));
1945 while Present (Discr) loop
1946 Disc_Value := Ancestor_Discriminant_Value (Discr);
1948 if Present (Disc_Value) then
1949 Cond := Make_Op_Ne (Loc,
1951 Make_Selected_Component (Loc,
1952 Prefix => New_Copy_Tree (Target),
1953 Selector_Name => New_Occurrence_Of (Discr, Loc)),
1954 Right_Opnd => Disc_Value);
1957 Make_Raise_Constraint_Error (Loc,
1959 Reason => CE_Discriminant_Check_Failed));
1962 Next_Discriminant (Discr);
1964 end Check_Ancestor_Discriminants;
1966 ---------------------------
1967 -- Compatible_Int_Bounds --
1968 ---------------------------
1970 function Compatible_Int_Bounds
1971 (Agg_Bounds : Node_Id;
1972 Typ_Bounds : Node_Id) return Boolean
1974 Agg_Lo : constant Uint := Intval (Low_Bound (Agg_Bounds));
1975 Agg_Hi : constant Uint := Intval (High_Bound (Agg_Bounds));
1976 Typ_Lo : constant Uint := Intval (Low_Bound (Typ_Bounds));
1977 Typ_Hi : constant Uint := Intval (High_Bound (Typ_Bounds));
1979 return Typ_Lo <= Agg_Lo and then Agg_Hi <= Typ_Hi;
1980 end Compatible_Int_Bounds;
1982 --------------------------------
1983 -- Get_Constraint_Association --
1984 --------------------------------
1986 function Get_Constraint_Association (T : Entity_Id) return Node_Id is
1987 Typ_Def : constant Node_Id := Type_Definition (Parent (T));
1988 Indic : constant Node_Id := Subtype_Indication (Typ_Def);
1991 -- ??? Also need to cover case of a type mark denoting a subtype
1994 if Nkind (Indic) = N_Subtype_Indication
1995 and then Present (Constraint (Indic))
1997 return First (Constraints (Constraint (Indic)));
2001 end Get_Constraint_Association;
2003 -------------------------------
2004 -- Init_Hidden_Discriminants --
2005 -------------------------------
2007 procedure Init_Hidden_Discriminants (Typ : Entity_Id; List : List_Id) is
2009 Parent_Type : Entity_Id;
2011 Discr_Val : Elmt_Id;
2014 Btype := Base_Type (Typ);
2015 while Is_Derived_Type (Btype)
2016 and then Present (Stored_Constraint (Btype))
2018 Parent_Type := Etype (Btype);
2020 Disc := First_Discriminant (Parent_Type);
2021 Discr_Val := First_Elmt (Stored_Constraint (Base_Type (Typ)));
2022 while Present (Discr_Val) loop
2024 -- Only those discriminants of the parent that are not
2025 -- renamed by discriminants of the derived type need to
2026 -- be added explicitly.
2028 if not Is_Entity_Name (Node (Discr_Val))
2029 or else Ekind (Entity (Node (Discr_Val))) /= E_Discriminant
2032 Make_Selected_Component (Loc,
2033 Prefix => New_Copy_Tree (Target),
2034 Selector_Name => New_Occurrence_Of (Disc, Loc));
2037 Make_OK_Assignment_Statement (Loc,
2039 Expression => New_Copy_Tree (Node (Discr_Val)));
2041 Set_No_Ctrl_Actions (Instr);
2042 Append_To (List, Instr);
2045 Next_Discriminant (Disc);
2046 Next_Elmt (Discr_Val);
2049 Btype := Base_Type (Parent_Type);
2051 end Init_Hidden_Discriminants;
2053 -------------------------
2054 -- Is_Int_Range_Bounds --
2055 -------------------------
2057 function Is_Int_Range_Bounds (Bounds : Node_Id) return Boolean is
2059 return Nkind (Bounds) = N_Range
2060 and then Nkind (Low_Bound (Bounds)) = N_Integer_Literal
2061 and then Nkind (High_Bound (Bounds)) = N_Integer_Literal;
2062 end Is_Int_Range_Bounds;
2064 -----------------------------------
2065 -- Generate_Finalization_Actions --
2066 -----------------------------------
2068 procedure Generate_Finalization_Actions is
2070 -- Do the work only the first time this is called
2072 if Finalization_Done then
2076 Finalization_Done := True;
2078 -- Determine the external finalization list. It is either the
2079 -- finalization list of the outer-scope or the one coming from
2080 -- an outer aggregate. When the target is not a temporary, the
2081 -- proper scope is the scope of the target rather than the
2082 -- potentially transient current scope.
2084 if Is_Controlled (Typ)
2085 and then Ancestor_Is_Subtype_Mark
2087 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
2088 Set_Assignment_OK (Ref);
2091 Make_Procedure_Call_Statement (Loc,
2094 (Find_Prim_Op (Init_Typ, Name_Initialize), Loc),
2095 Parameter_Associations => New_List (New_Copy_Tree (Ref))));
2097 end Generate_Finalization_Actions;
2099 function Rewrite_Discriminant (Expr : Node_Id) return Traverse_Result;
2100 -- If default expression of a component mentions a discriminant of the
2101 -- type, it must be rewritten as the discriminant of the target object.
2103 function Replace_Type (Expr : Node_Id) return Traverse_Result;
2104 -- If the aggregate contains a self-reference, traverse each expression
2105 -- to replace a possible self-reference with a reference to the proper
2106 -- component of the target of the assignment.
2108 --------------------------
2109 -- Rewrite_Discriminant --
2110 --------------------------
2112 function Rewrite_Discriminant (Expr : Node_Id) return Traverse_Result is
2114 if Is_Entity_Name (Expr)
2115 and then Present (Entity (Expr))
2116 and then Ekind (Entity (Expr)) = E_In_Parameter
2117 and then Present (Discriminal_Link (Entity (Expr)))
2118 and then Scope (Discriminal_Link (Entity (Expr)))
2119 = Base_Type (Etype (N))
2122 Make_Selected_Component (Loc,
2123 Prefix => New_Copy_Tree (Lhs),
2124 Selector_Name => Make_Identifier (Loc, Chars (Expr))));
2127 end Rewrite_Discriminant;
2133 function Replace_Type (Expr : Node_Id) return Traverse_Result is
2135 -- Note regarding the Root_Type test below: Aggregate components for
2136 -- self-referential types include attribute references to the current
2137 -- instance, of the form: Typ'access, etc.. These references are
2138 -- rewritten as references to the target of the aggregate: the
2139 -- left-hand side of an assignment, the entity in a declaration,
2140 -- or a temporary. Without this test, we would improperly extended
2141 -- this rewriting to attribute references whose prefix was not the
2142 -- type of the aggregate.
2144 if Nkind (Expr) = N_Attribute_Reference
2145 and then Is_Entity_Name (Prefix (Expr))
2146 and then Is_Type (Entity (Prefix (Expr)))
2147 and then Root_Type (Etype (N)) = Root_Type (Entity (Prefix (Expr)))
2149 if Is_Entity_Name (Lhs) then
2150 Rewrite (Prefix (Expr),
2151 New_Occurrence_Of (Entity (Lhs), Loc));
2153 elsif Nkind (Lhs) = N_Selected_Component then
2155 Make_Attribute_Reference (Loc,
2156 Attribute_Name => Name_Unrestricted_Access,
2157 Prefix => New_Copy_Tree (Prefix (Lhs))));
2158 Set_Analyzed (Parent (Expr), False);
2162 Make_Attribute_Reference (Loc,
2163 Attribute_Name => Name_Unrestricted_Access,
2164 Prefix => New_Copy_Tree (Lhs)));
2165 Set_Analyzed (Parent (Expr), False);
2172 procedure Replace_Self_Reference is
2173 new Traverse_Proc (Replace_Type);
2175 procedure Replace_Discriminants is
2176 new Traverse_Proc (Rewrite_Discriminant);
2178 -- Start of processing for Build_Record_Aggr_Code
2181 if Has_Self_Reference (N) then
2182 Replace_Self_Reference (N);
2185 -- If the target of the aggregate is class-wide, we must convert it
2186 -- to the actual type of the aggregate, so that the proper components
2187 -- are visible. We know already that the types are compatible.
2189 if Present (Etype (Lhs))
2190 and then Is_Class_Wide_Type (Etype (Lhs))
2192 Target := Unchecked_Convert_To (Typ, Lhs);
2197 -- Deal with the ancestor part of extension aggregates or with the
2198 -- discriminants of the root type.
2200 if Nkind (N) = N_Extension_Aggregate then
2202 Ancestor : constant Node_Id := Ancestor_Part (N);
2206 -- If the ancestor part is a subtype mark "T", we generate
2208 -- init-proc (T (tmp)); if T is constrained and
2209 -- init-proc (S (tmp)); where S applies an appropriate
2210 -- constraint if T is unconstrained
2212 if Is_Entity_Name (Ancestor)
2213 and then Is_Type (Entity (Ancestor))
2215 Ancestor_Is_Subtype_Mark := True;
2217 if Is_Constrained (Entity (Ancestor)) then
2218 Init_Typ := Entity (Ancestor);
2220 -- For an ancestor part given by an unconstrained type mark,
2221 -- create a subtype constrained by appropriate corresponding
2222 -- discriminant values coming from either associations of the
2223 -- aggregate or a constraint on a parent type. The subtype will
2224 -- be used to generate the correct default value for the
2227 elsif Has_Discriminants (Entity (Ancestor)) then
2229 Anc_Typ : constant Entity_Id := Entity (Ancestor);
2230 Anc_Constr : constant List_Id := New_List;
2231 Discrim : Entity_Id;
2232 Disc_Value : Node_Id;
2233 New_Indic : Node_Id;
2234 Subt_Decl : Node_Id;
2237 Discrim := First_Discriminant (Anc_Typ);
2238 while Present (Discrim) loop
2239 Disc_Value := Ancestor_Discriminant_Value (Discrim);
2240 Append_To (Anc_Constr, Disc_Value);
2241 Next_Discriminant (Discrim);
2245 Make_Subtype_Indication (Loc,
2246 Subtype_Mark => New_Occurrence_Of (Anc_Typ, Loc),
2248 Make_Index_Or_Discriminant_Constraint (Loc,
2249 Constraints => Anc_Constr));
2251 Init_Typ := Create_Itype (Ekind (Anc_Typ), N);
2254 Make_Subtype_Declaration (Loc,
2255 Defining_Identifier => Init_Typ,
2256 Subtype_Indication => New_Indic);
2258 -- Itypes must be analyzed with checks off Declaration
2259 -- must have a parent for proper handling of subsidiary
2262 Set_Parent (Subt_Decl, N);
2263 Analyze (Subt_Decl, Suppress => All_Checks);
2267 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
2268 Set_Assignment_OK (Ref);
2270 if not Is_Interface (Init_Typ) then
2272 Build_Initialization_Call (Loc,
2275 In_Init_Proc => Within_Init_Proc,
2276 With_Default_Init => Has_Default_Init_Comps (N)
2278 Has_Task (Base_Type (Init_Typ))));
2280 if Is_Constrained (Entity (Ancestor))
2281 and then Has_Discriminants (Entity (Ancestor))
2283 Check_Ancestor_Discriminants (Entity (Ancestor));
2287 -- Handle calls to C++ constructors
2289 elsif Is_CPP_Constructor_Call (Ancestor) then
2290 Init_Typ := Etype (Ancestor);
2291 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
2292 Set_Assignment_OK (Ref);
2295 Build_Initialization_Call (Loc,
2298 In_Init_Proc => Within_Init_Proc,
2299 With_Default_Init => Has_Default_Init_Comps (N),
2300 Constructor_Ref => Ancestor));
2302 -- Ada 2005 (AI-287): If the ancestor part is an aggregate of
2303 -- limited type, a recursive call expands the ancestor. Note that
2304 -- in the limited case, the ancestor part must be either a
2305 -- function call (possibly qualified, or wrapped in an unchecked
2306 -- conversion) or aggregate (definitely qualified).
2307 -- The ancestor part can also be a function call (that may be
2308 -- transformed into an explicit dereference) or a qualification
2311 elsif Is_Limited_Type (Etype (Ancestor))
2312 and then Nkind_In (Unqualify (Ancestor), N_Aggregate,
2313 N_Extension_Aggregate)
2315 Ancestor_Is_Expression := True;
2317 -- Set up finalization data for enclosing record, because
2318 -- controlled subcomponents of the ancestor part will be
2321 Generate_Finalization_Actions;
2324 Build_Record_Aggr_Code (
2325 N => Unqualify (Ancestor),
2326 Typ => Etype (Unqualify (Ancestor)),
2328 Is_Limited_Ancestor_Expansion => True));
2330 -- If the ancestor part is an expression "E", we generate
2334 -- In Ada 2005, this includes the case of a (possibly qualified)
2335 -- limited function call. The assignment will turn into a
2336 -- build-in-place function call (for further details, see
2337 -- Make_Build_In_Place_Call_In_Assignment).
2340 Ancestor_Is_Expression := True;
2341 Init_Typ := Etype (Ancestor);
2343 -- If the ancestor part is an aggregate, force its full
2344 -- expansion, which was delayed.
2346 if Nkind_In (Unqualify (Ancestor), N_Aggregate,
2347 N_Extension_Aggregate)
2349 Set_Analyzed (Ancestor, False);
2350 Set_Analyzed (Expression (Ancestor), False);
2353 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
2354 Set_Assignment_OK (Ref);
2356 -- Make the assignment without usual controlled actions since
2357 -- we only want the post adjust but not the pre finalize here
2358 -- Add manual adjust when necessary.
2360 Assign := New_List (
2361 Make_OK_Assignment_Statement (Loc,
2363 Expression => Ancestor));
2364 Set_No_Ctrl_Actions (First (Assign));
2366 -- Assign the tag now to make sure that the dispatching call in
2367 -- the subsequent deep_adjust works properly (unless VM_Target,
2368 -- where tags are implicit).
2370 if Tagged_Type_Expansion then
2372 Make_OK_Assignment_Statement (Loc,
2374 Make_Selected_Component (Loc,
2375 Prefix => New_Copy_Tree (Target),
2378 (First_Tag_Component (Base_Type (Typ)), Loc)),
2381 Unchecked_Convert_To (RTE (RE_Tag),
2384 (Access_Disp_Table (Base_Type (Typ)))),
2387 Set_Assignment_OK (Name (Instr));
2388 Append_To (Assign, Instr);
2390 -- Ada 2005 (AI-251): If tagged type has progenitors we must
2391 -- also initialize tags of the secondary dispatch tables.
2393 if Has_Interfaces (Base_Type (Typ)) then
2395 (Typ => Base_Type (Typ),
2397 Stmts_List => Assign);
2401 -- Call Adjust manually
2403 if Needs_Finalization (Etype (Ancestor))
2404 and then not Is_Limited_Type (Etype (Ancestor))
2408 Obj_Ref => New_Copy_Tree (Ref),
2409 Typ => Etype (Ancestor)));
2413 Make_Unsuppress_Block (Loc, Name_Discriminant_Check, Assign));
2415 if Has_Discriminants (Init_Typ) then
2416 Check_Ancestor_Discriminants (Init_Typ);
2421 -- Generate assignments of hidden assignments. If the base type is an
2422 -- unchecked union, the discriminants are unknown to the back-end and
2423 -- absent from a value of the type, so assignments for them are not
2426 if Has_Discriminants (Typ)
2427 and then not Is_Unchecked_Union (Base_Type (Typ))
2429 Init_Hidden_Discriminants (Typ, L);
2432 -- Normal case (not an extension aggregate)
2435 -- Generate the discriminant expressions, component by component.
2436 -- If the base type is an unchecked union, the discriminants are
2437 -- unknown to the back-end and absent from a value of the type, so
2438 -- assignments for them are not emitted.
2440 if Has_Discriminants (Typ)
2441 and then not Is_Unchecked_Union (Base_Type (Typ))
2443 Init_Hidden_Discriminants (Typ, L);
2445 -- Generate discriminant init values for the visible discriminants
2448 Discriminant : Entity_Id;
2449 Discriminant_Value : Node_Id;
2452 Discriminant := First_Stored_Discriminant (Typ);
2453 while Present (Discriminant) loop
2455 Make_Selected_Component (Loc,
2456 Prefix => New_Copy_Tree (Target),
2457 Selector_Name => New_Occurrence_Of (Discriminant, Loc));
2459 Discriminant_Value :=
2460 Get_Discriminant_Value (
2463 Discriminant_Constraint (N_Typ));
2466 Make_OK_Assignment_Statement (Loc,
2468 Expression => New_Copy_Tree (Discriminant_Value));
2470 Set_No_Ctrl_Actions (Instr);
2471 Append_To (L, Instr);
2473 Next_Stored_Discriminant (Discriminant);
2479 -- For CPP types we generate an implicit call to the C++ default
2480 -- constructor to ensure the proper initialization of the _Tag
2483 if Is_CPP_Class (Root_Type (Typ))
2484 and then CPP_Num_Prims (Typ) > 0
2486 Invoke_Constructor : declare
2487 CPP_Parent : constant Entity_Id :=
2488 Enclosing_CPP_Parent (Typ);
2490 procedure Invoke_IC_Proc (T : Entity_Id);
2491 -- Recursive routine used to climb to parents. Required because
2492 -- parents must be initialized before descendants to ensure
2493 -- propagation of inherited C++ slots.
2495 --------------------
2496 -- Invoke_IC_Proc --
2497 --------------------
2499 procedure Invoke_IC_Proc (T : Entity_Id) is
2501 -- Avoid generating extra calls. Initialization required
2502 -- only for types defined from the level of derivation of
2503 -- type of the constructor and the type of the aggregate.
2505 if T = CPP_Parent then
2509 Invoke_IC_Proc (Etype (T));
2511 -- Generate call to the IC routine
2513 if Present (CPP_Init_Proc (T)) then
2515 Make_Procedure_Call_Statement (Loc,
2516 New_Reference_To (CPP_Init_Proc (T), Loc)));
2520 -- Start of processing for Invoke_Constructor
2523 -- Implicit invocation of the C++ constructor
2525 if Nkind (N) = N_Aggregate then
2527 Make_Procedure_Call_Statement (Loc,
2530 (Base_Init_Proc (CPP_Parent), Loc),
2531 Parameter_Associations => New_List (
2532 Unchecked_Convert_To (CPP_Parent,
2533 New_Copy_Tree (Lhs)))));
2536 Invoke_IC_Proc (Typ);
2537 end Invoke_Constructor;
2540 -- Generate the assignments, component by component
2542 -- tmp.comp1 := Expr1_From_Aggr;
2543 -- tmp.comp2 := Expr2_From_Aggr;
2546 Comp := First (Component_Associations (N));
2547 while Present (Comp) loop
2548 Selector := Entity (First (Choices (Comp)));
2552 if Is_CPP_Constructor_Call (Expression (Comp)) then
2554 Build_Initialization_Call (Loc,
2555 Id_Ref => Make_Selected_Component (Loc,
2556 Prefix => New_Copy_Tree (Target),
2558 New_Occurrence_Of (Selector, Loc)),
2559 Typ => Etype (Selector),
2561 With_Default_Init => True,
2562 Constructor_Ref => Expression (Comp)));
2564 -- Ada 2005 (AI-287): For each default-initialized component generate
2565 -- a call to the corresponding IP subprogram if available.
2567 elsif Box_Present (Comp)
2568 and then Has_Non_Null_Base_Init_Proc (Etype (Selector))
2570 if Ekind (Selector) /= E_Discriminant then
2571 Generate_Finalization_Actions;
2574 -- Ada 2005 (AI-287): If the component type has tasks then
2575 -- generate the activation chain and master entities (except
2576 -- in case of an allocator because in that case these entities
2577 -- are generated by Build_Task_Allocate_Block_With_Init_Stmts).
2580 Ctype : constant Entity_Id := Etype (Selector);
2581 Inside_Allocator : Boolean := False;
2582 P : Node_Id := Parent (N);
2585 if Is_Task_Type (Ctype) or else Has_Task (Ctype) then
2586 while Present (P) loop
2587 if Nkind (P) = N_Allocator then
2588 Inside_Allocator := True;
2595 if not Inside_Init_Proc and not Inside_Allocator then
2596 Build_Activation_Chain_Entity (N);
2602 Build_Initialization_Call (Loc,
2603 Id_Ref => Make_Selected_Component (Loc,
2604 Prefix => New_Copy_Tree (Target),
2606 New_Occurrence_Of (Selector, Loc)),
2607 Typ => Etype (Selector),
2609 With_Default_Init => True));
2611 -- Prepare for component assignment
2613 elsif Ekind (Selector) /= E_Discriminant
2614 or else Nkind (N) = N_Extension_Aggregate
2616 -- All the discriminants have now been assigned
2618 -- This is now a good moment to initialize and attach all the
2619 -- controllers. Their position may depend on the discriminants.
2621 if Ekind (Selector) /= E_Discriminant then
2622 Generate_Finalization_Actions;
2625 Comp_Type := Underlying_Type (Etype (Selector));
2627 Make_Selected_Component (Loc,
2628 Prefix => New_Copy_Tree (Target),
2629 Selector_Name => New_Occurrence_Of (Selector, Loc));
2631 if Nkind (Expression (Comp)) = N_Qualified_Expression then
2632 Expr_Q := Expression (Expression (Comp));
2634 Expr_Q := Expression (Comp);
2637 -- Now either create the assignment or generate the code for the
2638 -- inner aggregate top-down.
2640 if Is_Delayed_Aggregate (Expr_Q) then
2642 -- We have the following case of aggregate nesting inside
2643 -- an object declaration:
2645 -- type Arr_Typ is array (Integer range <>) of ...;
2647 -- type Rec_Typ (...) is record
2648 -- Obj_Arr_Typ : Arr_Typ (A .. B);
2651 -- Obj_Rec_Typ : Rec_Typ := (...,
2652 -- Obj_Arr_Typ => (X => (...), Y => (...)));
2654 -- The length of the ranges of the aggregate and Obj_Add_Typ
2655 -- are equal (B - A = Y - X), but they do not coincide (X /=
2656 -- A and B /= Y). This case requires array sliding which is
2657 -- performed in the following manner:
2659 -- subtype Arr_Sub is Arr_Typ (X .. Y);
2661 -- Temp (X) := (...);
2663 -- Temp (Y) := (...);
2664 -- Obj_Rec_Typ.Obj_Arr_Typ := Temp;
2666 if Ekind (Comp_Type) = E_Array_Subtype
2667 and then Is_Int_Range_Bounds (Aggregate_Bounds (Expr_Q))
2668 and then Is_Int_Range_Bounds (First_Index (Comp_Type))
2670 Compatible_Int_Bounds
2671 (Agg_Bounds => Aggregate_Bounds (Expr_Q),
2672 Typ_Bounds => First_Index (Comp_Type))
2674 -- Create the array subtype with bounds equal to those of
2675 -- the corresponding aggregate.
2678 SubE : constant Entity_Id := Make_Temporary (Loc, 'T');
2680 SubD : constant Node_Id :=
2681 Make_Subtype_Declaration (Loc,
2682 Defining_Identifier => SubE,
2683 Subtype_Indication =>
2684 Make_Subtype_Indication (Loc,
2687 (Etype (Comp_Type), Loc),
2689 Make_Index_Or_Discriminant_Constraint
2691 Constraints => New_List (
2693 (Aggregate_Bounds (Expr_Q))))));
2695 -- Create a temporary array of the above subtype which
2696 -- will be used to capture the aggregate assignments.
2698 TmpE : constant Entity_Id := Make_Temporary (Loc, 'A', N);
2700 TmpD : constant Node_Id :=
2701 Make_Object_Declaration (Loc,
2702 Defining_Identifier => TmpE,
2703 Object_Definition =>
2704 New_Reference_To (SubE, Loc));
2707 Set_No_Initialization (TmpD);
2708 Append_To (L, SubD);
2709 Append_To (L, TmpD);
2711 -- Expand aggregate into assignments to the temp array
2714 Late_Expansion (Expr_Q, Comp_Type,
2715 New_Reference_To (TmpE, Loc)));
2720 Make_Assignment_Statement (Loc,
2721 Name => New_Copy_Tree (Comp_Expr),
2722 Expression => New_Reference_To (TmpE, Loc)));
2725 -- Normal case (sliding not required)
2729 Late_Expansion (Expr_Q, Comp_Type, Comp_Expr));
2732 -- Expr_Q is not delayed aggregate
2735 if Has_Discriminants (Typ) then
2736 Replace_Discriminants (Expr_Q);
2740 Make_OK_Assignment_Statement (Loc,
2742 Expression => Expr_Q);
2744 Set_No_Ctrl_Actions (Instr);
2745 Append_To (L, Instr);
2747 -- Adjust the tag if tagged (because of possible view
2748 -- conversions), unless compiling for a VM where tags are
2751 -- tmp.comp._tag := comp_typ'tag;
2753 if Is_Tagged_Type (Comp_Type)
2754 and then Tagged_Type_Expansion
2757 Make_OK_Assignment_Statement (Loc,
2759 Make_Selected_Component (Loc,
2760 Prefix => New_Copy_Tree (Comp_Expr),
2763 (First_Tag_Component (Comp_Type), Loc)),
2766 Unchecked_Convert_To (RTE (RE_Tag),
2768 (Node (First_Elmt (Access_Disp_Table (Comp_Type))),
2771 Append_To (L, Instr);
2775 -- Adjust (tmp.comp);
2777 if Needs_Finalization (Comp_Type)
2778 and then not Is_Limited_Type (Comp_Type)
2782 Obj_Ref => New_Copy_Tree (Comp_Expr),
2789 elsif Ekind (Selector) = E_Discriminant
2790 and then Nkind (N) /= N_Extension_Aggregate
2791 and then Nkind (Parent (N)) = N_Component_Association
2792 and then Is_Constrained (Typ)
2794 -- We must check that the discriminant value imposed by the
2795 -- context is the same as the value given in the subaggregate,
2796 -- because after the expansion into assignments there is no
2797 -- record on which to perform a regular discriminant check.
2804 D_Val := First_Elmt (Discriminant_Constraint (Typ));
2805 Disc := First_Discriminant (Typ);
2806 while Chars (Disc) /= Chars (Selector) loop
2807 Next_Discriminant (Disc);
2811 pragma Assert (Present (D_Val));
2813 -- This check cannot performed for components that are
2814 -- constrained by a current instance, because this is not a
2815 -- value that can be compared with the actual constraint.
2817 if Nkind (Node (D_Val)) /= N_Attribute_Reference
2818 or else not Is_Entity_Name (Prefix (Node (D_Val)))
2819 or else not Is_Type (Entity (Prefix (Node (D_Val))))
2822 Make_Raise_Constraint_Error (Loc,
2825 Left_Opnd => New_Copy_Tree (Node (D_Val)),
2826 Right_Opnd => Expression (Comp)),
2827 Reason => CE_Discriminant_Check_Failed));
2830 -- Find self-reference in previous discriminant assignment,
2831 -- and replace with proper expression.
2838 while Present (Ass) loop
2839 if Nkind (Ass) = N_Assignment_Statement
2840 and then Nkind (Name (Ass)) = N_Selected_Component
2841 and then Chars (Selector_Name (Name (Ass))) =
2845 (Ass, New_Copy_Tree (Expression (Comp)));
2858 -- If the type is tagged, the tag needs to be initialized (unless
2859 -- compiling for the Java VM where tags are implicit). It is done
2860 -- late in the initialization process because in some cases, we call
2861 -- the init proc of an ancestor which will not leave out the right tag
2863 if Ancestor_Is_Expression then
2866 -- For CPP types we generated a call to the C++ default constructor
2867 -- before the components have been initialized to ensure the proper
2868 -- initialization of the _Tag component (see above).
2870 elsif Is_CPP_Class (Typ) then
2873 elsif Is_Tagged_Type (Typ) and then Tagged_Type_Expansion then
2875 Make_OK_Assignment_Statement (Loc,
2877 Make_Selected_Component (Loc,
2878 Prefix => New_Copy_Tree (Target),
2881 (First_Tag_Component (Base_Type (Typ)), Loc)),
2884 Unchecked_Convert_To (RTE (RE_Tag),
2886 (Node (First_Elmt (Access_Disp_Table (Base_Type (Typ)))),
2889 Append_To (L, Instr);
2891 -- Ada 2005 (AI-251): If the tagged type has been derived from
2892 -- abstract interfaces we must also initialize the tags of the
2893 -- secondary dispatch tables.
2895 if Has_Interfaces (Base_Type (Typ)) then
2897 (Typ => Base_Type (Typ),
2903 -- If the controllers have not been initialized yet (by lack of non-
2904 -- discriminant components), let's do it now.
2906 Generate_Finalization_Actions;
2909 end Build_Record_Aggr_Code;
2911 -------------------------------
2912 -- Convert_Aggr_In_Allocator --
2913 -------------------------------
2915 procedure Convert_Aggr_In_Allocator
2920 Loc : constant Source_Ptr := Sloc (Aggr);
2921 Typ : constant Entity_Id := Etype (Aggr);
2922 Temp : constant Entity_Id := Defining_Identifier (Decl);
2924 Occ : constant Node_Id :=
2925 Unchecked_Convert_To (Typ,
2926 Make_Explicit_Dereference (Loc,
2927 New_Reference_To (Temp, Loc)));
2930 if Is_Array_Type (Typ) then
2931 Convert_Array_Aggr_In_Allocator (Decl, Aggr, Occ);
2933 elsif Has_Default_Init_Comps (Aggr) then
2935 L : constant List_Id := New_List;
2936 Init_Stmts : List_Id;
2939 Init_Stmts := Late_Expansion (Aggr, Typ, Occ);
2941 if Has_Task (Typ) then
2942 Build_Task_Allocate_Block_With_Init_Stmts (L, Aggr, Init_Stmts);
2943 Insert_Actions (Alloc, L);
2945 Insert_Actions (Alloc, Init_Stmts);
2950 Insert_Actions (Alloc, Late_Expansion (Aggr, Typ, Occ));
2952 end Convert_Aggr_In_Allocator;
2954 --------------------------------
2955 -- Convert_Aggr_In_Assignment --
2956 --------------------------------
2958 procedure Convert_Aggr_In_Assignment (N : Node_Id) is
2959 Aggr : Node_Id := Expression (N);
2960 Typ : constant Entity_Id := Etype (Aggr);
2961 Occ : constant Node_Id := New_Copy_Tree (Name (N));
2964 if Nkind (Aggr) = N_Qualified_Expression then
2965 Aggr := Expression (Aggr);
2968 Insert_Actions_After (N, Late_Expansion (Aggr, Typ, Occ));
2969 end Convert_Aggr_In_Assignment;
2971 ---------------------------------
2972 -- Convert_Aggr_In_Object_Decl --
2973 ---------------------------------
2975 procedure Convert_Aggr_In_Object_Decl (N : Node_Id) is
2976 Obj : constant Entity_Id := Defining_Identifier (N);
2977 Aggr : Node_Id := Expression (N);
2978 Loc : constant Source_Ptr := Sloc (Aggr);
2979 Typ : constant Entity_Id := Etype (Aggr);
2980 Occ : constant Node_Id := New_Occurrence_Of (Obj, Loc);
2982 function Discriminants_Ok return Boolean;
2983 -- If the object type is constrained, the discriminants in the
2984 -- aggregate must be checked against the discriminants of the subtype.
2985 -- This cannot be done using Apply_Discriminant_Checks because after
2986 -- expansion there is no aggregate left to check.
2988 ----------------------
2989 -- Discriminants_Ok --
2990 ----------------------
2992 function Discriminants_Ok return Boolean is
2993 Cond : Node_Id := Empty;
3002 D := First_Discriminant (Typ);
3003 Disc1 := First_Elmt (Discriminant_Constraint (Typ));
3004 Disc2 := First_Elmt (Discriminant_Constraint (Etype (Obj)));
3005 while Present (Disc1) and then Present (Disc2) loop
3006 Val1 := Node (Disc1);
3007 Val2 := Node (Disc2);
3009 if not Is_OK_Static_Expression (Val1)
3010 or else not Is_OK_Static_Expression (Val2)
3012 Check := Make_Op_Ne (Loc,
3013 Left_Opnd => Duplicate_Subexpr (Val1),
3014 Right_Opnd => Duplicate_Subexpr (Val2));
3020 Cond := Make_Or_Else (Loc,
3022 Right_Opnd => Check);
3025 elsif Expr_Value (Val1) /= Expr_Value (Val2) then
3026 Apply_Compile_Time_Constraint_Error (Aggr,
3027 Msg => "incorrect value for discriminant&?",
3028 Reason => CE_Discriminant_Check_Failed,
3033 Next_Discriminant (D);
3038 -- If any discriminant constraint is non-static, emit a check
3040 if Present (Cond) then
3042 Make_Raise_Constraint_Error (Loc,
3044 Reason => CE_Discriminant_Check_Failed));
3048 end Discriminants_Ok;
3050 -- Start of processing for Convert_Aggr_In_Object_Decl
3053 Set_Assignment_OK (Occ);
3055 if Nkind (Aggr) = N_Qualified_Expression then
3056 Aggr := Expression (Aggr);
3059 if Has_Discriminants (Typ)
3060 and then Typ /= Etype (Obj)
3061 and then Is_Constrained (Etype (Obj))
3062 and then not Discriminants_Ok
3067 -- If the context is an extended return statement, it has its own
3068 -- finalization machinery (i.e. works like a transient scope) and
3069 -- we do not want to create an additional one, because objects on
3070 -- the finalization list of the return must be moved to the caller's
3071 -- finalization list to complete the return.
3073 -- However, if the aggregate is limited, it is built in place, and the
3074 -- controlled components are not assigned to intermediate temporaries
3075 -- so there is no need for a transient scope in this case either.
3077 if Requires_Transient_Scope (Typ)
3078 and then Ekind (Current_Scope) /= E_Return_Statement
3079 and then not Is_Limited_Type (Typ)
3081 Establish_Transient_Scope
3084 Is_Controlled (Typ) or else Has_Controlled_Component (Typ));
3087 Insert_Actions_After (N, Late_Expansion (Aggr, Typ, Occ));
3088 Set_No_Initialization (N);
3089 Initialize_Discriminants (N, Typ);
3090 end Convert_Aggr_In_Object_Decl;
3092 -------------------------------------
3093 -- Convert_Array_Aggr_In_Allocator --
3094 -------------------------------------
3096 procedure Convert_Array_Aggr_In_Allocator
3101 Aggr_Code : List_Id;
3102 Typ : constant Entity_Id := Etype (Aggr);
3103 Ctyp : constant Entity_Id := Component_Type (Typ);
3106 -- The target is an explicit dereference of the allocated object.
3107 -- Generate component assignments to it, as for an aggregate that
3108 -- appears on the right-hand side of an assignment statement.
3111 Build_Array_Aggr_Code (Aggr,
3113 Index => First_Index (Typ),
3115 Scalar_Comp => Is_Scalar_Type (Ctyp));
3117 Insert_Actions_After (Decl, Aggr_Code);
3118 end Convert_Array_Aggr_In_Allocator;
3120 ----------------------------
3121 -- Convert_To_Assignments --
3122 ----------------------------
3124 procedure Convert_To_Assignments (N : Node_Id; Typ : Entity_Id) is
3125 Loc : constant Source_Ptr := Sloc (N);
3130 Target_Expr : Node_Id;
3131 Parent_Kind : Node_Kind;
3132 Unc_Decl : Boolean := False;
3133 Parent_Node : Node_Id;
3136 pragma Assert (not Is_Static_Dispatch_Table_Aggregate (N));
3137 pragma Assert (Is_Record_Type (Typ));
3139 Parent_Node := Parent (N);
3140 Parent_Kind := Nkind (Parent_Node);
3142 if Parent_Kind = N_Qualified_Expression then
3144 -- Check if we are in a unconstrained declaration because in this
3145 -- case the current delayed expansion mechanism doesn't work when
3146 -- the declared object size depend on the initializing expr.
3149 Parent_Node := Parent (Parent_Node);
3150 Parent_Kind := Nkind (Parent_Node);
3152 if Parent_Kind = N_Object_Declaration then
3154 not Is_Entity_Name (Object_Definition (Parent_Node))
3155 or else Has_Discriminants
3156 (Entity (Object_Definition (Parent_Node)))
3157 or else Is_Class_Wide_Type
3158 (Entity (Object_Definition (Parent_Node)));
3163 -- Just set the Delay flag in the cases where the transformation will be
3164 -- done top down from above.
3168 -- Internal aggregate (transformed when expanding the parent)
3170 or else Parent_Kind = N_Aggregate
3171 or else Parent_Kind = N_Extension_Aggregate
3172 or else Parent_Kind = N_Component_Association
3174 -- Allocator (see Convert_Aggr_In_Allocator)
3176 or else Parent_Kind = N_Allocator
3178 -- Object declaration (see Convert_Aggr_In_Object_Decl)
3180 or else (Parent_Kind = N_Object_Declaration and then not Unc_Decl)
3182 -- Safe assignment (see Convert_Aggr_Assignments). So far only the
3183 -- assignments in init procs are taken into account.
3185 or else (Parent_Kind = N_Assignment_Statement
3186 and then Inside_Init_Proc)
3188 -- (Ada 2005) An inherently limited type in a return statement,
3189 -- which will be handled in a build-in-place fashion, and may be
3190 -- rewritten as an extended return and have its own finalization
3191 -- machinery. In the case of a simple return, the aggregate needs
3192 -- to be delayed until the scope for the return statement has been
3193 -- created, so that any finalization chain will be associated with
3194 -- that scope. For extended returns, we delay expansion to avoid the
3195 -- creation of an unwanted transient scope that could result in
3196 -- premature finalization of the return object (which is built in
3197 -- in place within the caller's scope).
3200 (Is_Immutably_Limited_Type (Typ)
3202 (Nkind (Parent (Parent_Node)) = N_Extended_Return_Statement
3203 or else Nkind (Parent_Node) = N_Simple_Return_Statement))
3205 Set_Expansion_Delayed (N);
3209 if Requires_Transient_Scope (Typ) then
3210 Establish_Transient_Scope
3212 Is_Controlled (Typ) or else Has_Controlled_Component (Typ));
3215 -- If the aggregate is non-limited, create a temporary. If it is limited
3216 -- and the context is an assignment, this is a subaggregate for an
3217 -- enclosing aggregate being expanded. It must be built in place, so use
3218 -- the target of the current assignment.
3220 if Is_Limited_Type (Typ)
3221 and then Nkind (Parent (N)) = N_Assignment_Statement
3223 Target_Expr := New_Copy_Tree (Name (Parent (N)));
3224 Insert_Actions (Parent (N),
3225 Build_Record_Aggr_Code (N, Typ, Target_Expr));
3226 Rewrite (Parent (N), Make_Null_Statement (Loc));
3229 Temp := Make_Temporary (Loc, 'A', N);
3231 -- If the type inherits unknown discriminants, use the view with
3232 -- known discriminants if available.
3234 if Has_Unknown_Discriminants (Typ)
3235 and then Present (Underlying_Record_View (Typ))
3237 T := Underlying_Record_View (Typ);
3243 Make_Object_Declaration (Loc,
3244 Defining_Identifier => Temp,
3245 Object_Definition => New_Occurrence_Of (T, Loc));
3247 Set_No_Initialization (Instr);
3248 Insert_Action (N, Instr);
3249 Initialize_Discriminants (Instr, T);
3250 Target_Expr := New_Occurrence_Of (Temp, Loc);
3251 Insert_Actions (N, Build_Record_Aggr_Code (N, T, Target_Expr));
3252 Rewrite (N, New_Occurrence_Of (Temp, Loc));
3253 Analyze_And_Resolve (N, T);
3255 end Convert_To_Assignments;
3257 ---------------------------
3258 -- Convert_To_Positional --
3259 ---------------------------
3261 procedure Convert_To_Positional
3263 Max_Others_Replicate : Nat := 5;
3264 Handle_Bit_Packed : Boolean := False)
3266 Typ : constant Entity_Id := Etype (N);
3268 Static_Components : Boolean := True;
3270 procedure Check_Static_Components;
3271 -- Check whether all components of the aggregate are compile-time known
3272 -- values, and can be passed as is to the back-end without further
3278 Ixb : Node_Id) return Boolean;
3279 -- Convert the aggregate into a purely positional form if possible. On
3280 -- entry the bounds of all dimensions are known to be static, and the
3281 -- total number of components is safe enough to expand.
3283 function Is_Flat (N : Node_Id; Dims : Int) return Boolean;
3284 -- Return True iff the array N is flat (which is not trivial in the case
3285 -- of multidimensional aggregates).
3287 -----------------------------
3288 -- Check_Static_Components --
3289 -----------------------------
3291 procedure Check_Static_Components is
3295 Static_Components := True;
3297 if Nkind (N) = N_String_Literal then
3300 elsif Present (Expressions (N)) then
3301 Expr := First (Expressions (N));
3302 while Present (Expr) loop
3303 if Nkind (Expr) /= N_Aggregate
3304 or else not Compile_Time_Known_Aggregate (Expr)
3305 or else Expansion_Delayed (Expr)
3307 Static_Components := False;
3315 if Nkind (N) = N_Aggregate
3316 and then Present (Component_Associations (N))
3318 Expr := First (Component_Associations (N));
3319 while Present (Expr) loop
3320 if Nkind_In (Expression (Expr), N_Integer_Literal,
3325 elsif Is_Entity_Name (Expression (Expr))
3326 and then Present (Entity (Expression (Expr)))
3327 and then Ekind (Entity (Expression (Expr))) =
3328 E_Enumeration_Literal
3332 elsif Nkind (Expression (Expr)) /= N_Aggregate
3333 or else not Compile_Time_Known_Aggregate (Expression (Expr))
3334 or else Expansion_Delayed (Expression (Expr))
3336 Static_Components := False;
3343 end Check_Static_Components;
3352 Ixb : Node_Id) return Boolean
3354 Loc : constant Source_Ptr := Sloc (N);
3355 Blo : constant Node_Id := Type_Low_Bound (Etype (Ixb));
3356 Lo : constant Node_Id := Type_Low_Bound (Etype (Ix));
3357 Hi : constant Node_Id := Type_High_Bound (Etype (Ix));
3361 Others_Present : Boolean := False;
3364 if Nkind (Original_Node (N)) = N_String_Literal then
3368 if not Compile_Time_Known_Value (Lo)
3369 or else not Compile_Time_Known_Value (Hi)
3374 Lov := Expr_Value (Lo);
3375 Hiv := Expr_Value (Hi);
3377 -- Check if there is an others choice
3379 if Present (Component_Associations (N)) then
3385 Assoc := First (Component_Associations (N));
3386 while Present (Assoc) loop
3387 Choice := First (Choices (Assoc));
3389 while Present (Choice) loop
3390 if Nkind (Choice) = N_Others_Choice then
3391 Others_Present := True;
3402 -- If the low bound is not known at compile time and others is not
3403 -- present we can proceed since the bounds can be obtained from the
3406 -- Note: This case is required in VM platforms since their backends
3407 -- normalize array indexes in the range 0 .. N-1. Hence, if we do
3408 -- not flat an array whose bounds cannot be obtained from the type
3409 -- of the index the backend has no way to properly generate the code.
3410 -- See ACATS c460010 for an example.
3413 or else (not Compile_Time_Known_Value (Blo)
3414 and then Others_Present)
3419 -- Determine if set of alternatives is suitable for conversion and
3420 -- build an array containing the values in sequence.
3423 Vals : array (UI_To_Int (Lov) .. UI_To_Int (Hiv))
3424 of Node_Id := (others => Empty);
3425 -- The values in the aggregate sorted appropriately
3428 -- Same data as Vals in list form
3431 -- Used to validate Max_Others_Replicate limit
3434 Num : Int := UI_To_Int (Lov);
3440 if Present (Expressions (N)) then
3441 Elmt := First (Expressions (N));
3442 while Present (Elmt) loop
3443 if Nkind (Elmt) = N_Aggregate
3444 and then Present (Next_Index (Ix))
3446 not Flatten (Elmt, Next_Index (Ix), Next_Index (Ixb))
3451 Vals (Num) := Relocate_Node (Elmt);
3458 if No (Component_Associations (N)) then
3462 Elmt := First (Component_Associations (N));
3464 if Nkind (Expression (Elmt)) = N_Aggregate then
3465 if Present (Next_Index (Ix))
3468 (Expression (Elmt), Next_Index (Ix), Next_Index (Ixb))
3474 Component_Loop : while Present (Elmt) loop
3475 Choice := First (Choices (Elmt));
3476 Choice_Loop : while Present (Choice) loop
3478 -- If we have an others choice, fill in the missing elements
3479 -- subject to the limit established by Max_Others_Replicate.
3481 if Nkind (Choice) = N_Others_Choice then
3484 for J in Vals'Range loop
3485 if No (Vals (J)) then
3486 Vals (J) := New_Copy_Tree (Expression (Elmt));
3487 Rep_Count := Rep_Count + 1;
3489 -- Check for maximum others replication. Note that
3490 -- we skip this test if either of the restrictions
3491 -- No_Elaboration_Code or No_Implicit_Loops is
3492 -- active, if this is a preelaborable unit or a
3493 -- predefined unit. This ensures that predefined
3494 -- units get the same level of constant folding in
3495 -- Ada 95 and Ada 05, where their categorization
3499 P : constant Entity_Id :=
3500 Cunit_Entity (Current_Sem_Unit);
3503 -- Check if duplication OK and if so continue
3506 if Restriction_Active (No_Elaboration_Code)
3507 or else Restriction_Active (No_Implicit_Loops)
3508 or else Is_Preelaborated (P)
3509 or else (Ekind (P) = E_Package_Body
3511 Is_Preelaborated (Spec_Entity (P)))
3513 Is_Predefined_File_Name
3514 (Unit_File_Name (Get_Source_Unit (P)))
3518 -- If duplication not OK, then we return False
3519 -- if the replication count is too high
3521 elsif Rep_Count > Max_Others_Replicate then
3524 -- Continue on if duplication not OK, but the
3525 -- replication count is not excessive.
3534 exit Component_Loop;
3536 -- Case of a subtype mark, identifier or expanded name
3538 elsif Is_Entity_Name (Choice)
3539 and then Is_Type (Entity (Choice))
3541 Lo := Type_Low_Bound (Etype (Choice));
3542 Hi := Type_High_Bound (Etype (Choice));
3544 -- Case of subtype indication
3546 elsif Nkind (Choice) = N_Subtype_Indication then
3547 Lo := Low_Bound (Range_Expression (Constraint (Choice)));
3548 Hi := High_Bound (Range_Expression (Constraint (Choice)));
3552 elsif Nkind (Choice) = N_Range then
3553 Lo := Low_Bound (Choice);
3554 Hi := High_Bound (Choice);
3556 -- Normal subexpression case
3558 else pragma Assert (Nkind (Choice) in N_Subexpr);
3559 if not Compile_Time_Known_Value (Choice) then
3563 Choice_Index := UI_To_Int (Expr_Value (Choice));
3564 if Choice_Index in Vals'Range then
3565 Vals (Choice_Index) :=
3566 New_Copy_Tree (Expression (Elmt));
3570 -- Choice is statically out-of-range, will be
3571 -- rewritten to raise Constraint_Error.
3578 -- Range cases merge with Lo,Hi set
3580 if not Compile_Time_Known_Value (Lo)
3582 not Compile_Time_Known_Value (Hi)
3586 for J in UI_To_Int (Expr_Value (Lo)) ..
3587 UI_To_Int (Expr_Value (Hi))
3589 Vals (J) := New_Copy_Tree (Expression (Elmt));
3595 end loop Choice_Loop;
3598 end loop Component_Loop;
3600 -- If we get here the conversion is possible
3603 for J in Vals'Range loop
3604 Append (Vals (J), Vlist);
3607 Rewrite (N, Make_Aggregate (Loc, Expressions => Vlist));
3608 Set_Aggregate_Bounds (N, Aggregate_Bounds (Original_Node (N)));
3617 function Is_Flat (N : Node_Id; Dims : Int) return Boolean is
3624 elsif Nkind (N) = N_Aggregate then
3625 if Present (Component_Associations (N)) then
3629 Elmt := First (Expressions (N));
3630 while Present (Elmt) loop
3631 if not Is_Flat (Elmt, Dims - 1) then
3645 -- Start of processing for Convert_To_Positional
3648 -- Ada 2005 (AI-287): Do not convert in case of default initialized
3649 -- components because in this case will need to call the corresponding
3652 if Has_Default_Init_Comps (N) then
3656 if Is_Flat (N, Number_Dimensions (Typ)) then
3660 if Is_Bit_Packed_Array (Typ)
3661 and then not Handle_Bit_Packed
3666 -- Do not convert to positional if controlled components are involved
3667 -- since these require special processing
3669 if Has_Controlled_Component (Typ) then
3673 Check_Static_Components;
3675 -- If the size is known, or all the components are static, try to
3676 -- build a fully positional aggregate.
3678 -- The size of the type may not be known for an aggregate with
3679 -- discriminated array components, but if the components are static
3680 -- it is still possible to verify statically that the length is
3681 -- compatible with the upper bound of the type, and therefore it is
3682 -- worth flattening such aggregates as well.
3684 -- For now the back-end expands these aggregates into individual
3685 -- assignments to the target anyway, but it is conceivable that
3686 -- it will eventually be able to treat such aggregates statically???
3688 if Aggr_Size_OK (N, Typ)
3689 and then Flatten (N, First_Index (Typ), First_Index (Base_Type (Typ)))
3691 if Static_Components then
3692 Set_Compile_Time_Known_Aggregate (N);
3693 Set_Expansion_Delayed (N, False);
3696 Analyze_And_Resolve (N, Typ);
3698 end Convert_To_Positional;
3700 ----------------------------
3701 -- Expand_Array_Aggregate --
3702 ----------------------------
3704 -- Array aggregate expansion proceeds as follows:
3706 -- 1. If requested we generate code to perform all the array aggregate
3707 -- bound checks, specifically
3709 -- (a) Check that the index range defined by aggregate bounds is
3710 -- compatible with corresponding index subtype.
3712 -- (b) If an others choice is present check that no aggregate
3713 -- index is outside the bounds of the index constraint.
3715 -- (c) For multidimensional arrays make sure that all subaggregates
3716 -- corresponding to the same dimension have the same bounds.
3718 -- 2. Check for packed array aggregate which can be converted to a
3719 -- constant so that the aggregate disappeares completely.
3721 -- 3. Check case of nested aggregate. Generally nested aggregates are
3722 -- handled during the processing of the parent aggregate.
3724 -- 4. Check if the aggregate can be statically processed. If this is the
3725 -- case pass it as is to Gigi. Note that a necessary condition for
3726 -- static processing is that the aggregate be fully positional.
3728 -- 5. If in place aggregate expansion is possible (i.e. no need to create
3729 -- a temporary) then mark the aggregate as such and return. Otherwise
3730 -- create a new temporary and generate the appropriate initialization
3733 procedure Expand_Array_Aggregate (N : Node_Id) is
3734 Loc : constant Source_Ptr := Sloc (N);
3736 Typ : constant Entity_Id := Etype (N);
3737 Ctyp : constant Entity_Id := Component_Type (Typ);
3738 -- Typ is the correct constrained array subtype of the aggregate
3739 -- Ctyp is the corresponding component type.
3741 Aggr_Dimension : constant Pos := Number_Dimensions (Typ);
3742 -- Number of aggregate index dimensions
3744 Aggr_Low : array (1 .. Aggr_Dimension) of Node_Id;
3745 Aggr_High : array (1 .. Aggr_Dimension) of Node_Id;
3746 -- Low and High bounds of the constraint for each aggregate index
3748 Aggr_Index_Typ : array (1 .. Aggr_Dimension) of Entity_Id;
3749 -- The type of each index
3751 Maybe_In_Place_OK : Boolean;
3752 -- If the type is neither controlled nor packed and the aggregate
3753 -- is the expression in an assignment, assignment in place may be
3754 -- possible, provided other conditions are met on the LHS.
3756 Others_Present : array (1 .. Aggr_Dimension) of Boolean :=
3758 -- If Others_Present (J) is True, then there is an others choice
3759 -- in one of the sub-aggregates of N at dimension J.
3761 procedure Build_Constrained_Type (Positional : Boolean);
3762 -- If the subtype is not static or unconstrained, build a constrained
3763 -- type using the computable sizes of the aggregate and its sub-
3766 procedure Check_Bounds (Aggr_Bounds : Node_Id; Index_Bounds : Node_Id);
3767 -- Checks that the bounds of Aggr_Bounds are within the bounds defined
3770 procedure Check_Same_Aggr_Bounds (Sub_Aggr : Node_Id; Dim : Pos);
3771 -- Checks that in a multi-dimensional array aggregate all subaggregates
3772 -- corresponding to the same dimension have the same bounds.
3773 -- Sub_Aggr is an array sub-aggregate. Dim is the dimension
3774 -- corresponding to the sub-aggregate.
3776 procedure Compute_Others_Present (Sub_Aggr : Node_Id; Dim : Pos);
3777 -- Computes the values of array Others_Present. Sub_Aggr is the
3778 -- array sub-aggregate we start the computation from. Dim is the
3779 -- dimension corresponding to the sub-aggregate.
3781 function In_Place_Assign_OK return Boolean;
3782 -- Simple predicate to determine whether an aggregate assignment can
3783 -- be done in place, because none of the new values can depend on the
3784 -- components of the target of the assignment.
3786 procedure Others_Check (Sub_Aggr : Node_Id; Dim : Pos);
3787 -- Checks that if an others choice is present in any sub-aggregate no
3788 -- aggregate index is outside the bounds of the index constraint.
3789 -- Sub_Aggr is an array sub-aggregate. Dim is the dimension
3790 -- corresponding to the sub-aggregate.
3792 function Safe_Left_Hand_Side (N : Node_Id) return Boolean;
3793 -- In addition to Maybe_In_Place_OK, in order for an aggregate to be
3794 -- built directly into the target of the assignment it must be free
3797 ----------------------------
3798 -- Build_Constrained_Type --
3799 ----------------------------
3801 procedure Build_Constrained_Type (Positional : Boolean) is
3802 Loc : constant Source_Ptr := Sloc (N);
3803 Agg_Type : constant Entity_Id := Make_Temporary (Loc, 'A');
3806 Typ : constant Entity_Id := Etype (N);
3807 Indexes : constant List_Id := New_List;
3812 -- If the aggregate is purely positional, all its subaggregates
3813 -- have the same size. We collect the dimensions from the first
3814 -- subaggregate at each level.
3819 for D in 1 .. Number_Dimensions (Typ) loop
3820 Sub_Agg := First (Expressions (Sub_Agg));
3824 while Present (Comp) loop
3831 Low_Bound => Make_Integer_Literal (Loc, 1),
3832 High_Bound => Make_Integer_Literal (Loc, Num)));
3836 -- We know the aggregate type is unconstrained and the aggregate
3837 -- is not processable by the back end, therefore not necessarily
3838 -- positional. Retrieve each dimension bounds (computed earlier).
3840 for D in 1 .. Number_Dimensions (Typ) loop
3843 Low_Bound => Aggr_Low (D),
3844 High_Bound => Aggr_High (D)),
3850 Make_Full_Type_Declaration (Loc,
3851 Defining_Identifier => Agg_Type,
3853 Make_Constrained_Array_Definition (Loc,
3854 Discrete_Subtype_Definitions => Indexes,
3855 Component_Definition =>
3856 Make_Component_Definition (Loc,
3857 Aliased_Present => False,
3858 Subtype_Indication =>
3859 New_Occurrence_Of (Component_Type (Typ), Loc))));
3861 Insert_Action (N, Decl);
3863 Set_Etype (N, Agg_Type);
3864 Set_Is_Itype (Agg_Type);
3865 Freeze_Itype (Agg_Type, N);
3866 end Build_Constrained_Type;
3872 procedure Check_Bounds (Aggr_Bounds : Node_Id; Index_Bounds : Node_Id) is
3879 Cond : Node_Id := Empty;
3882 Get_Index_Bounds (Aggr_Bounds, Aggr_Lo, Aggr_Hi);
3883 Get_Index_Bounds (Index_Bounds, Ind_Lo, Ind_Hi);
3885 -- Generate the following test:
3887 -- [constraint_error when
3888 -- Aggr_Lo <= Aggr_Hi and then
3889 -- (Aggr_Lo < Ind_Lo or else Aggr_Hi > Ind_Hi)]
3891 -- As an optimization try to see if some tests are trivially vacuous
3892 -- because we are comparing an expression against itself.
3894 if Aggr_Lo = Ind_Lo and then Aggr_Hi = Ind_Hi then
3897 elsif Aggr_Hi = Ind_Hi then
3900 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
3901 Right_Opnd => Duplicate_Subexpr_Move_Checks (Ind_Lo));
3903 elsif Aggr_Lo = Ind_Lo then
3906 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi),
3907 Right_Opnd => Duplicate_Subexpr_Move_Checks (Ind_Hi));
3914 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
3915 Right_Opnd => Duplicate_Subexpr_Move_Checks (Ind_Lo)),
3919 Left_Opnd => Duplicate_Subexpr (Aggr_Hi),
3920 Right_Opnd => Duplicate_Subexpr (Ind_Hi)));
3923 if Present (Cond) then
3928 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
3929 Right_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi)),
3931 Right_Opnd => Cond);
3933 Set_Analyzed (Left_Opnd (Left_Opnd (Cond)), False);
3934 Set_Analyzed (Right_Opnd (Left_Opnd (Cond)), False);
3936 Make_Raise_Constraint_Error (Loc,
3938 Reason => CE_Length_Check_Failed));
3942 ----------------------------
3943 -- Check_Same_Aggr_Bounds --
3944 ----------------------------
3946 procedure Check_Same_Aggr_Bounds (Sub_Aggr : Node_Id; Dim : Pos) is
3947 Sub_Lo : constant Node_Id := Low_Bound (Aggregate_Bounds (Sub_Aggr));
3948 Sub_Hi : constant Node_Id := High_Bound (Aggregate_Bounds (Sub_Aggr));
3949 -- The bounds of this specific sub-aggregate
3951 Aggr_Lo : constant Node_Id := Aggr_Low (Dim);
3952 Aggr_Hi : constant Node_Id := Aggr_High (Dim);
3953 -- The bounds of the aggregate for this dimension
3955 Ind_Typ : constant Entity_Id := Aggr_Index_Typ (Dim);
3956 -- The index type for this dimension.xxx
3958 Cond : Node_Id := Empty;
3963 -- If index checks are on generate the test
3965 -- [constraint_error when
3966 -- Aggr_Lo /= Sub_Lo or else Aggr_Hi /= Sub_Hi]
3968 -- As an optimization try to see if some tests are trivially vacuos
3969 -- because we are comparing an expression against itself. Also for
3970 -- the first dimension the test is trivially vacuous because there
3971 -- is just one aggregate for dimension 1.
3973 if Index_Checks_Suppressed (Ind_Typ) then
3977 or else (Aggr_Lo = Sub_Lo and then Aggr_Hi = Sub_Hi)
3981 elsif Aggr_Hi = Sub_Hi then
3984 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
3985 Right_Opnd => Duplicate_Subexpr_Move_Checks (Sub_Lo));
3987 elsif Aggr_Lo = Sub_Lo then
3990 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi),
3991 Right_Opnd => Duplicate_Subexpr_Move_Checks (Sub_Hi));
3998 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
3999 Right_Opnd => Duplicate_Subexpr_Move_Checks (Sub_Lo)),
4003 Left_Opnd => Duplicate_Subexpr (Aggr_Hi),
4004 Right_Opnd => Duplicate_Subexpr (Sub_Hi)));
4007 if Present (Cond) then
4009 Make_Raise_Constraint_Error (Loc,
4011 Reason => CE_Length_Check_Failed));
4014 -- Now look inside the sub-aggregate to see if there is more work
4016 if Dim < Aggr_Dimension then
4018 -- Process positional components
4020 if Present (Expressions (Sub_Aggr)) then
4021 Expr := First (Expressions (Sub_Aggr));
4022 while Present (Expr) loop
4023 Check_Same_Aggr_Bounds (Expr, Dim + 1);
4028 -- Process component associations
4030 if Present (Component_Associations (Sub_Aggr)) then
4031 Assoc := First (Component_Associations (Sub_Aggr));
4032 while Present (Assoc) loop
4033 Expr := Expression (Assoc);
4034 Check_Same_Aggr_Bounds (Expr, Dim + 1);
4039 end Check_Same_Aggr_Bounds;
4041 ----------------------------
4042 -- Compute_Others_Present --
4043 ----------------------------
4045 procedure Compute_Others_Present (Sub_Aggr : Node_Id; Dim : Pos) is
4050 if Present (Component_Associations (Sub_Aggr)) then
4051 Assoc := Last (Component_Associations (Sub_Aggr));
4053 if Nkind (First (Choices (Assoc))) = N_Others_Choice then
4054 Others_Present (Dim) := True;
4058 -- Now look inside the sub-aggregate to see if there is more work
4060 if Dim < Aggr_Dimension then
4062 -- Process positional components
4064 if Present (Expressions (Sub_Aggr)) then
4065 Expr := First (Expressions (Sub_Aggr));
4066 while Present (Expr) loop
4067 Compute_Others_Present (Expr, Dim + 1);
4072 -- Process component associations
4074 if Present (Component_Associations (Sub_Aggr)) then
4075 Assoc := First (Component_Associations (Sub_Aggr));
4076 while Present (Assoc) loop
4077 Expr := Expression (Assoc);
4078 Compute_Others_Present (Expr, Dim + 1);
4083 end Compute_Others_Present;
4085 ------------------------
4086 -- In_Place_Assign_OK --
4087 ------------------------
4089 function In_Place_Assign_OK return Boolean is
4097 function Safe_Aggregate (Aggr : Node_Id) return Boolean;
4098 -- Check recursively that each component of a (sub)aggregate does
4099 -- not depend on the variable being assigned to.
4101 function Safe_Component (Expr : Node_Id) return Boolean;
4102 -- Verify that an expression cannot depend on the variable being
4103 -- assigned to. Room for improvement here (but less than before).
4105 --------------------
4106 -- Safe_Aggregate --
4107 --------------------
4109 function Safe_Aggregate (Aggr : Node_Id) return Boolean is
4113 if Present (Expressions (Aggr)) then
4114 Expr := First (Expressions (Aggr));
4115 while Present (Expr) loop
4116 if Nkind (Expr) = N_Aggregate then
4117 if not Safe_Aggregate (Expr) then
4121 elsif not Safe_Component (Expr) then
4129 if Present (Component_Associations (Aggr)) then
4130 Expr := First (Component_Associations (Aggr));
4131 while Present (Expr) loop
4132 if Nkind (Expression (Expr)) = N_Aggregate then
4133 if not Safe_Aggregate (Expression (Expr)) then
4137 elsif not Safe_Component (Expression (Expr)) then
4148 --------------------
4149 -- Safe_Component --
4150 --------------------
4152 function Safe_Component (Expr : Node_Id) return Boolean is
4153 Comp : Node_Id := Expr;
4155 function Check_Component (Comp : Node_Id) return Boolean;
4156 -- Do the recursive traversal, after copy
4158 ---------------------
4159 -- Check_Component --
4160 ---------------------
4162 function Check_Component (Comp : Node_Id) return Boolean is
4164 if Is_Overloaded (Comp) then
4168 return Compile_Time_Known_Value (Comp)
4170 or else (Is_Entity_Name (Comp)
4171 and then Present (Entity (Comp))
4172 and then No (Renamed_Object (Entity (Comp))))
4174 or else (Nkind (Comp) = N_Attribute_Reference
4175 and then Check_Component (Prefix (Comp)))
4177 or else (Nkind (Comp) in N_Binary_Op
4178 and then Check_Component (Left_Opnd (Comp))
4179 and then Check_Component (Right_Opnd (Comp)))
4181 or else (Nkind (Comp) in N_Unary_Op
4182 and then Check_Component (Right_Opnd (Comp)))
4184 or else (Nkind (Comp) = N_Selected_Component
4185 and then Check_Component (Prefix (Comp)))
4187 or else (Nkind (Comp) = N_Unchecked_Type_Conversion
4188 and then Check_Component (Expression (Comp)));
4189 end Check_Component;
4191 -- Start of processing for Safe_Component
4194 -- If the component appears in an association that may
4195 -- correspond to more than one element, it is not analyzed
4196 -- before the expansion into assignments, to avoid side effects.
4197 -- We analyze, but do not resolve the copy, to obtain sufficient
4198 -- entity information for the checks that follow. If component is
4199 -- overloaded we assume an unsafe function call.
4201 if not Analyzed (Comp) then
4202 if Is_Overloaded (Expr) then
4205 elsif Nkind (Expr) = N_Aggregate
4206 and then not Is_Others_Aggregate (Expr)
4210 elsif Nkind (Expr) = N_Allocator then
4212 -- For now, too complex to analyze
4217 Comp := New_Copy_Tree (Expr);
4218 Set_Parent (Comp, Parent (Expr));
4222 if Nkind (Comp) = N_Aggregate then
4223 return Safe_Aggregate (Comp);
4225 return Check_Component (Comp);
4229 -- Start of processing for In_Place_Assign_OK
4232 if Present (Component_Associations (N)) then
4234 -- On assignment, sliding can take place, so we cannot do the
4235 -- assignment in place unless the bounds of the aggregate are
4236 -- statically equal to those of the target.
4238 -- If the aggregate is given by an others choice, the bounds
4239 -- are derived from the left-hand side, and the assignment is
4240 -- safe if the expression is.
4242 if Is_Others_Aggregate (N) then
4245 (Expression (First (Component_Associations (N))));
4248 Aggr_In := First_Index (Etype (N));
4250 if Nkind (Parent (N)) = N_Assignment_Statement then
4251 Obj_In := First_Index (Etype (Name (Parent (N))));
4254 -- Context is an allocator. Check bounds of aggregate
4255 -- against given type in qualified expression.
4257 pragma Assert (Nkind (Parent (Parent (N))) = N_Allocator);
4259 First_Index (Etype (Entity (Subtype_Mark (Parent (N)))));
4262 while Present (Aggr_In) loop
4263 Get_Index_Bounds (Aggr_In, Aggr_Lo, Aggr_Hi);
4264 Get_Index_Bounds (Obj_In, Obj_Lo, Obj_Hi);
4266 if not Compile_Time_Known_Value (Aggr_Lo)
4267 or else not Compile_Time_Known_Value (Aggr_Hi)
4268 or else not Compile_Time_Known_Value (Obj_Lo)
4269 or else not Compile_Time_Known_Value (Obj_Hi)
4270 or else Expr_Value (Aggr_Lo) /= Expr_Value (Obj_Lo)
4271 or else Expr_Value (Aggr_Hi) /= Expr_Value (Obj_Hi)
4276 Next_Index (Aggr_In);
4277 Next_Index (Obj_In);
4281 -- Now check the component values themselves
4283 return Safe_Aggregate (N);
4284 end In_Place_Assign_OK;
4290 procedure Others_Check (Sub_Aggr : Node_Id; Dim : Pos) is
4291 Aggr_Lo : constant Node_Id := Aggr_Low (Dim);
4292 Aggr_Hi : constant Node_Id := Aggr_High (Dim);
4293 -- The bounds of the aggregate for this dimension
4295 Ind_Typ : constant Entity_Id := Aggr_Index_Typ (Dim);
4296 -- The index type for this dimension
4298 Need_To_Check : Boolean := False;
4300 Choices_Lo : Node_Id := Empty;
4301 Choices_Hi : Node_Id := Empty;
4302 -- The lowest and highest discrete choices for a named sub-aggregate
4304 Nb_Choices : Int := -1;
4305 -- The number of discrete non-others choices in this sub-aggregate
4307 Nb_Elements : Uint := Uint_0;
4308 -- The number of elements in a positional aggregate
4310 Cond : Node_Id := Empty;
4317 -- Check if we have an others choice. If we do make sure that this
4318 -- sub-aggregate contains at least one element in addition to the
4321 if Range_Checks_Suppressed (Ind_Typ) then
4322 Need_To_Check := False;
4324 elsif Present (Expressions (Sub_Aggr))
4325 and then Present (Component_Associations (Sub_Aggr))
4327 Need_To_Check := True;
4329 elsif Present (Component_Associations (Sub_Aggr)) then
4330 Assoc := Last (Component_Associations (Sub_Aggr));
4332 if Nkind (First (Choices (Assoc))) /= N_Others_Choice then
4333 Need_To_Check := False;
4336 -- Count the number of discrete choices. Start with -1 because
4337 -- the others choice does not count.
4340 Assoc := First (Component_Associations (Sub_Aggr));
4341 while Present (Assoc) loop
4342 Choice := First (Choices (Assoc));
4343 while Present (Choice) loop
4344 Nb_Choices := Nb_Choices + 1;
4351 -- If there is only an others choice nothing to do
4353 Need_To_Check := (Nb_Choices > 0);
4357 Need_To_Check := False;
4360 -- If we are dealing with a positional sub-aggregate with an others
4361 -- choice then compute the number or positional elements.
4363 if Need_To_Check and then Present (Expressions (Sub_Aggr)) then
4364 Expr := First (Expressions (Sub_Aggr));
4365 Nb_Elements := Uint_0;
4366 while Present (Expr) loop
4367 Nb_Elements := Nb_Elements + 1;
4371 -- If the aggregate contains discrete choices and an others choice
4372 -- compute the smallest and largest discrete choice values.
4374 elsif Need_To_Check then
4375 Compute_Choices_Lo_And_Choices_Hi : declare
4377 Table : Case_Table_Type (1 .. Nb_Choices);
4378 -- Used to sort all the different choice values
4385 Assoc := First (Component_Associations (Sub_Aggr));
4386 while Present (Assoc) loop
4387 Choice := First (Choices (Assoc));
4388 while Present (Choice) loop
4389 if Nkind (Choice) = N_Others_Choice then
4393 Get_Index_Bounds (Choice, Low, High);
4394 Table (J).Choice_Lo := Low;
4395 Table (J).Choice_Hi := High;
4404 -- Sort the discrete choices
4406 Sort_Case_Table (Table);
4408 Choices_Lo := Table (1).Choice_Lo;
4409 Choices_Hi := Table (Nb_Choices).Choice_Hi;
4410 end Compute_Choices_Lo_And_Choices_Hi;
4413 -- If no others choice in this sub-aggregate, or the aggregate
4414 -- comprises only an others choice, nothing to do.
4416 if not Need_To_Check then
4419 -- If we are dealing with an aggregate containing an others choice
4420 -- and positional components, we generate the following test:
4422 -- if Ind_Typ'Pos (Aggr_Lo) + (Nb_Elements - 1) >
4423 -- Ind_Typ'Pos (Aggr_Hi)
4425 -- raise Constraint_Error;
4428 elsif Nb_Elements > Uint_0 then
4434 Make_Attribute_Reference (Loc,
4435 Prefix => New_Reference_To (Ind_Typ, Loc),
4436 Attribute_Name => Name_Pos,
4439 (Duplicate_Subexpr_Move_Checks (Aggr_Lo))),
4440 Right_Opnd => Make_Integer_Literal (Loc, Nb_Elements - 1)),
4443 Make_Attribute_Reference (Loc,
4444 Prefix => New_Reference_To (Ind_Typ, Loc),
4445 Attribute_Name => Name_Pos,
4446 Expressions => New_List (
4447 Duplicate_Subexpr_Move_Checks (Aggr_Hi))));
4449 -- If we are dealing with an aggregate containing an others choice
4450 -- and discrete choices we generate the following test:
4452 -- [constraint_error when
4453 -- Choices_Lo < Aggr_Lo or else Choices_Hi > Aggr_Hi];
4461 Duplicate_Subexpr_Move_Checks (Choices_Lo),
4463 Duplicate_Subexpr_Move_Checks (Aggr_Lo)),
4468 Duplicate_Subexpr (Choices_Hi),
4470 Duplicate_Subexpr (Aggr_Hi)));
4473 if Present (Cond) then
4475 Make_Raise_Constraint_Error (Loc,
4477 Reason => CE_Length_Check_Failed));
4478 -- Questionable reason code, shouldn't that be a
4479 -- CE_Range_Check_Failed ???
4482 -- Now look inside the sub-aggregate to see if there is more work
4484 if Dim < Aggr_Dimension then
4486 -- Process positional components
4488 if Present (Expressions (Sub_Aggr)) then
4489 Expr := First (Expressions (Sub_Aggr));
4490 while Present (Expr) loop
4491 Others_Check (Expr, Dim + 1);
4496 -- Process component associations
4498 if Present (Component_Associations (Sub_Aggr)) then
4499 Assoc := First (Component_Associations (Sub_Aggr));
4500 while Present (Assoc) loop
4501 Expr := Expression (Assoc);
4502 Others_Check (Expr, Dim + 1);
4509 -------------------------
4510 -- Safe_Left_Hand_Side --
4511 -------------------------
4513 function Safe_Left_Hand_Side (N : Node_Id) return Boolean is
4514 function Is_Safe_Index (Indx : Node_Id) return Boolean;
4515 -- If the left-hand side includes an indexed component, check that
4516 -- the indexes are free of side-effect.
4522 function Is_Safe_Index (Indx : Node_Id) return Boolean is
4524 if Is_Entity_Name (Indx) then
4527 elsif Nkind (Indx) = N_Integer_Literal then
4530 elsif Nkind (Indx) = N_Function_Call
4531 and then Is_Entity_Name (Name (Indx))
4533 Has_Pragma_Pure_Function (Entity (Name (Indx)))
4537 elsif Nkind (Indx) = N_Type_Conversion
4538 and then Is_Safe_Index (Expression (Indx))
4547 -- Start of processing for Safe_Left_Hand_Side
4550 if Is_Entity_Name (N) then
4553 elsif Nkind_In (N, N_Explicit_Dereference, N_Selected_Component)
4554 and then Safe_Left_Hand_Side (Prefix (N))
4558 elsif Nkind (N) = N_Indexed_Component
4559 and then Safe_Left_Hand_Side (Prefix (N))
4561 Is_Safe_Index (First (Expressions (N)))
4565 elsif Nkind (N) = N_Unchecked_Type_Conversion then
4566 return Safe_Left_Hand_Side (Expression (N));
4571 end Safe_Left_Hand_Side;
4576 -- Holds the temporary aggregate value
4579 -- Holds the declaration of Tmp
4581 Aggr_Code : List_Id;
4582 Parent_Node : Node_Id;
4583 Parent_Kind : Node_Kind;
4585 -- Start of processing for Expand_Array_Aggregate
4588 -- Do not touch the special aggregates of attributes used for Asm calls
4590 if Is_RTE (Ctyp, RE_Asm_Input_Operand)
4591 or else Is_RTE (Ctyp, RE_Asm_Output_Operand)
4596 -- If the semantic analyzer has determined that aggregate N will raise
4597 -- Constraint_Error at run time, then the aggregate node has been
4598 -- replaced with an N_Raise_Constraint_Error node and we should
4601 pragma Assert (not Raises_Constraint_Error (N));
4605 -- Check that the index range defined by aggregate bounds is
4606 -- compatible with corresponding index subtype.
4608 Index_Compatibility_Check : declare
4609 Aggr_Index_Range : Node_Id := First_Index (Typ);
4610 -- The current aggregate index range
4612 Index_Constraint : Node_Id := First_Index (Etype (Typ));
4613 -- The corresponding index constraint against which we have to
4614 -- check the above aggregate index range.
4617 Compute_Others_Present (N, 1);
4619 for J in 1 .. Aggr_Dimension loop
4620 -- There is no need to emit a check if an others choice is
4621 -- present for this array aggregate dimension since in this
4622 -- case one of N's sub-aggregates has taken its bounds from the
4623 -- context and these bounds must have been checked already. In
4624 -- addition all sub-aggregates corresponding to the same
4625 -- dimension must all have the same bounds (checked in (c) below).
4627 if not Range_Checks_Suppressed (Etype (Index_Constraint))
4628 and then not Others_Present (J)
4630 -- We don't use Checks.Apply_Range_Check here because it emits
4631 -- a spurious check. Namely it checks that the range defined by
4632 -- the aggregate bounds is non empty. But we know this already
4635 Check_Bounds (Aggr_Index_Range, Index_Constraint);
4638 -- Save the low and high bounds of the aggregate index as well as
4639 -- the index type for later use in checks (b) and (c) below.
4641 Aggr_Low (J) := Low_Bound (Aggr_Index_Range);
4642 Aggr_High (J) := High_Bound (Aggr_Index_Range);
4644 Aggr_Index_Typ (J) := Etype (Index_Constraint);
4646 Next_Index (Aggr_Index_Range);
4647 Next_Index (Index_Constraint);
4649 end Index_Compatibility_Check;
4653 -- If an others choice is present check that no aggregate index is
4654 -- outside the bounds of the index constraint.
4656 Others_Check (N, 1);
4660 -- For multidimensional arrays make sure that all subaggregates
4661 -- corresponding to the same dimension have the same bounds.
4663 if Aggr_Dimension > 1 then
4664 Check_Same_Aggr_Bounds (N, 1);
4669 -- Here we test for is packed array aggregate that we can handle at
4670 -- compile time. If so, return with transformation done. Note that we do
4671 -- this even if the aggregate is nested, because once we have done this
4672 -- processing, there is no more nested aggregate!
4674 if Packed_Array_Aggregate_Handled (N) then
4678 -- At this point we try to convert to positional form
4680 if Ekind (Current_Scope) = E_Package
4681 and then Static_Elaboration_Desired (Current_Scope)
4683 Convert_To_Positional (N, Max_Others_Replicate => 100);
4686 Convert_To_Positional (N);
4689 -- if the result is no longer an aggregate (e.g. it may be a string
4690 -- literal, or a temporary which has the needed value), then we are
4691 -- done, since there is no longer a nested aggregate.
4693 if Nkind (N) /= N_Aggregate then
4696 -- We are also done if the result is an analyzed aggregate
4697 -- This case could use more comments ???
4700 and then N /= Original_Node (N)
4705 -- If all aggregate components are compile-time known and the aggregate
4706 -- has been flattened, nothing left to do. The same occurs if the
4707 -- aggregate is used to initialize the components of an statically
4708 -- allocated dispatch table.
4710 if Compile_Time_Known_Aggregate (N)
4711 or else Is_Static_Dispatch_Table_Aggregate (N)
4713 Set_Expansion_Delayed (N, False);
4717 -- Now see if back end processing is possible
4719 if Backend_Processing_Possible (N) then
4721 -- If the aggregate is static but the constraints are not, build
4722 -- a static subtype for the aggregate, so that Gigi can place it
4723 -- in static memory. Perform an unchecked_conversion to the non-
4724 -- static type imposed by the context.
4727 Itype : constant Entity_Id := Etype (N);
4729 Needs_Type : Boolean := False;
4732 Index := First_Index (Itype);
4733 while Present (Index) loop
4734 if not Is_Static_Subtype (Etype (Index)) then
4743 Build_Constrained_Type (Positional => True);
4744 Rewrite (N, Unchecked_Convert_To (Itype, N));
4754 -- Delay expansion for nested aggregates: it will be taken care of
4755 -- when the parent aggregate is expanded.
4757 Parent_Node := Parent (N);
4758 Parent_Kind := Nkind (Parent_Node);
4760 if Parent_Kind = N_Qualified_Expression then
4761 Parent_Node := Parent (Parent_Node);
4762 Parent_Kind := Nkind (Parent_Node);
4765 if Parent_Kind = N_Aggregate
4766 or else Parent_Kind = N_Extension_Aggregate
4767 or else Parent_Kind = N_Component_Association
4768 or else (Parent_Kind = N_Object_Declaration
4769 and then Needs_Finalization (Typ))
4770 or else (Parent_Kind = N_Assignment_Statement
4771 and then Inside_Init_Proc)
4773 if Static_Array_Aggregate (N)
4774 or else Compile_Time_Known_Aggregate (N)
4776 Set_Expansion_Delayed (N, False);
4779 Set_Expansion_Delayed (N);
4786 -- Look if in place aggregate expansion is possible
4788 -- For object declarations we build the aggregate in place, unless
4789 -- the array is bit-packed or the component is controlled.
4791 -- For assignments we do the assignment in place if all the component
4792 -- associations have compile-time known values. For other cases we
4793 -- create a temporary. The analysis for safety of on-line assignment
4794 -- is delicate, i.e. we don't know how to do it fully yet ???
4796 -- For allocators we assign to the designated object in place if the
4797 -- aggregate meets the same conditions as other in-place assignments.
4798 -- In this case the aggregate may not come from source but was created
4799 -- for default initialization, e.g. with Initialize_Scalars.
4801 if Requires_Transient_Scope (Typ) then
4802 Establish_Transient_Scope
4803 (N, Sec_Stack => Has_Controlled_Component (Typ));
4806 if Has_Default_Init_Comps (N) then
4807 Maybe_In_Place_OK := False;
4809 elsif Is_Bit_Packed_Array (Typ)
4810 or else Has_Controlled_Component (Typ)
4812 Maybe_In_Place_OK := False;
4815 Maybe_In_Place_OK :=
4816 (Nkind (Parent (N)) = N_Assignment_Statement
4817 and then Comes_From_Source (N)
4818 and then In_Place_Assign_OK)
4821 (Nkind (Parent (Parent (N))) = N_Allocator
4822 and then In_Place_Assign_OK);
4825 -- If this is an array of tasks, it will be expanded into build-in-place
4826 -- assignments. Build an activation chain for the tasks now.
4828 if Has_Task (Etype (N)) then
4829 Build_Activation_Chain_Entity (N);
4832 -- Should document these individual tests ???
4834 if not Has_Default_Init_Comps (N)
4835 and then Comes_From_Source (Parent (N))
4836 and then Nkind (Parent (N)) = N_Object_Declaration
4838 Must_Slide (Etype (Defining_Identifier (Parent (N))), Typ)
4839 and then N = Expression (Parent (N))
4840 and then not Is_Bit_Packed_Array (Typ)
4841 and then not Has_Controlled_Component (Typ)
4843 -- If the aggregate is the expression in an object declaration, it
4844 -- cannot be expanded in place. Lookahead in the current declarative
4845 -- part to find an address clause for the object being declared. If
4846 -- one is present, we cannot build in place. Unclear comment???
4848 and then not Has_Following_Address_Clause (Parent (N))
4850 Tmp := Defining_Identifier (Parent (N));
4851 Set_No_Initialization (Parent (N));
4852 Set_Expression (Parent (N), Empty);
4854 -- Set the type of the entity, for use in the analysis of the
4855 -- subsequent indexed assignments. If the nominal type is not
4856 -- constrained, build a subtype from the known bounds of the
4857 -- aggregate. If the declaration has a subtype mark, use it,
4858 -- otherwise use the itype of the aggregate.
4860 if not Is_Constrained (Typ) then
4861 Build_Constrained_Type (Positional => False);
4862 elsif Is_Entity_Name (Object_Definition (Parent (N)))
4863 and then Is_Constrained (Entity (Object_Definition (Parent (N))))
4865 Set_Etype (Tmp, Entity (Object_Definition (Parent (N))));
4867 Set_Size_Known_At_Compile_Time (Typ, False);
4868 Set_Etype (Tmp, Typ);
4871 elsif Maybe_In_Place_OK
4872 and then Nkind (Parent (N)) = N_Qualified_Expression
4873 and then Nkind (Parent (Parent (N))) = N_Allocator
4875 Set_Expansion_Delayed (N);
4878 -- In the remaining cases the aggregate is the RHS of an assignment
4880 elsif Maybe_In_Place_OK
4881 and then Safe_Left_Hand_Side (Name (Parent (N)))
4883 Tmp := Name (Parent (N));
4885 if Etype (Tmp) /= Etype (N) then
4886 Apply_Length_Check (N, Etype (Tmp));
4888 if Nkind (N) = N_Raise_Constraint_Error then
4890 -- Static error, nothing further to expand
4896 elsif Maybe_In_Place_OK
4897 and then Nkind (Name (Parent (N))) = N_Slice
4898 and then Safe_Slice_Assignment (N)
4900 -- Safe_Slice_Assignment rewrites assignment as a loop
4906 -- In place aggregate expansion is not possible
4909 Maybe_In_Place_OK := False;
4910 Tmp := Make_Temporary (Loc, 'A', N);
4912 Make_Object_Declaration
4914 Defining_Identifier => Tmp,
4915 Object_Definition => New_Occurrence_Of (Typ, Loc));
4916 Set_No_Initialization (Tmp_Decl, True);
4918 -- If we are within a loop, the temporary will be pushed on the
4919 -- stack at each iteration. If the aggregate is the expression for an
4920 -- allocator, it will be immediately copied to the heap and can
4921 -- be reclaimed at once. We create a transient scope around the
4922 -- aggregate for this purpose.
4924 if Ekind (Current_Scope) = E_Loop
4925 and then Nkind (Parent (Parent (N))) = N_Allocator
4927 Establish_Transient_Scope (N, False);
4930 Insert_Action (N, Tmp_Decl);
4933 -- Construct and insert the aggregate code. We can safely suppress index
4934 -- checks because this code is guaranteed not to raise CE on index
4935 -- checks. However we should *not* suppress all checks.
4941 if Nkind (Tmp) = N_Defining_Identifier then
4942 Target := New_Reference_To (Tmp, Loc);
4946 if Has_Default_Init_Comps (N) then
4948 -- Ada 2005 (AI-287): This case has not been analyzed???
4950 raise Program_Error;
4953 -- Name in assignment is explicit dereference
4955 Target := New_Copy (Tmp);
4959 Build_Array_Aggr_Code (N,
4961 Index => First_Index (Typ),
4963 Scalar_Comp => Is_Scalar_Type (Ctyp));
4966 if Comes_From_Source (Tmp) then
4967 Insert_Actions_After (Parent (N), Aggr_Code);
4970 Insert_Actions (N, Aggr_Code);
4973 -- If the aggregate has been assigned in place, remove the original
4976 if Nkind (Parent (N)) = N_Assignment_Statement
4977 and then Maybe_In_Place_OK
4979 Rewrite (Parent (N), Make_Null_Statement (Loc));
4981 elsif Nkind (Parent (N)) /= N_Object_Declaration
4982 or else Tmp /= Defining_Identifier (Parent (N))
4984 Rewrite (N, New_Occurrence_Of (Tmp, Loc));
4985 Analyze_And_Resolve (N, Typ);
4987 end Expand_Array_Aggregate;
4989 ------------------------
4990 -- Expand_N_Aggregate --
4991 ------------------------
4993 procedure Expand_N_Aggregate (N : Node_Id) is
4995 if Is_Record_Type (Etype (N)) then
4996 Expand_Record_Aggregate (N);
4998 Expand_Array_Aggregate (N);
5001 when RE_Not_Available =>
5003 end Expand_N_Aggregate;
5005 ----------------------------------
5006 -- Expand_N_Extension_Aggregate --
5007 ----------------------------------
5009 -- If the ancestor part is an expression, add a component association for
5010 -- the parent field. If the type of the ancestor part is not the direct
5011 -- parent of the expected type, build recursively the needed ancestors.
5012 -- If the ancestor part is a subtype_mark, replace aggregate with a decla-
5013 -- ration for a temporary of the expected type, followed by individual
5014 -- assignments to the given components.
5016 procedure Expand_N_Extension_Aggregate (N : Node_Id) is
5017 Loc : constant Source_Ptr := Sloc (N);
5018 A : constant Node_Id := Ancestor_Part (N);
5019 Typ : constant Entity_Id := Etype (N);
5022 -- If the ancestor is a subtype mark, an init proc must be called
5023 -- on the resulting object which thus has to be materialized in
5026 if Is_Entity_Name (A) and then Is_Type (Entity (A)) then
5027 Convert_To_Assignments (N, Typ);
5029 -- The extension aggregate is transformed into a record aggregate
5030 -- of the following form (c1 and c2 are inherited components)
5032 -- (Exp with c3 => a, c4 => b)
5033 -- ==> (c1 => Exp.c1, c2 => Exp.c2, c3 => a, c4 => b)
5038 if Tagged_Type_Expansion then
5039 Expand_Record_Aggregate (N,
5042 (Node (First_Elmt (Access_Disp_Table (Typ))), Loc),
5045 -- No tag is needed in the case of a VM
5048 Expand_Record_Aggregate (N, Parent_Expr => A);
5053 when RE_Not_Available =>
5055 end Expand_N_Extension_Aggregate;
5057 -----------------------------
5058 -- Expand_Record_Aggregate --
5059 -----------------------------
5061 procedure Expand_Record_Aggregate
5063 Orig_Tag : Node_Id := Empty;
5064 Parent_Expr : Node_Id := Empty)
5066 Loc : constant Source_Ptr := Sloc (N);
5067 Comps : constant List_Id := Component_Associations (N);
5068 Typ : constant Entity_Id := Etype (N);
5069 Base_Typ : constant Entity_Id := Base_Type (Typ);
5071 Static_Components : Boolean := True;
5072 -- Flag to indicate whether all components are compile-time known,
5073 -- and the aggregate can be constructed statically and handled by
5076 function Component_Not_OK_For_Backend return Boolean;
5077 -- Check for presence of component which makes it impossible for the
5078 -- backend to process the aggregate, thus requiring the use of a series
5079 -- of assignment statements. Cases checked for are a nested aggregate
5080 -- needing Late_Expansion, the presence of a tagged component which may
5081 -- need tag adjustment, and a bit unaligned component reference.
5083 -- We also force expansion into assignments if a component is of a
5084 -- mutable type (including a private type with discriminants) because
5085 -- in that case the size of the component to be copied may be smaller
5086 -- than the side of the target, and there is no simple way for gigi
5087 -- to compute the size of the object to be copied.
5089 -- NOTE: This is part of the ongoing work to define precisely the
5090 -- interface between front-end and back-end handling of aggregates.
5091 -- In general it is desirable to pass aggregates as they are to gigi,
5092 -- in order to minimize elaboration code. This is one case where the
5093 -- semantics of Ada complicate the analysis and lead to anomalies in
5094 -- the gcc back-end if the aggregate is not expanded into assignments.
5096 ----------------------------------
5097 -- Component_Not_OK_For_Backend --
5098 ----------------------------------
5100 function Component_Not_OK_For_Backend return Boolean is
5110 while Present (C) loop
5112 -- If the component has box initialization, expansion is needed
5113 -- and component is not ready for backend.
5115 if Box_Present (C) then
5119 if Nkind (Expression (C)) = N_Qualified_Expression then
5120 Expr_Q := Expression (Expression (C));
5122 Expr_Q := Expression (C);
5125 -- Return true if the aggregate has any associations for tagged
5126 -- components that may require tag adjustment.
5128 -- These are cases where the source expression may have a tag that
5129 -- could differ from the component tag (e.g., can occur for type
5130 -- conversions and formal parameters). (Tag adjustment not needed
5131 -- if VM_Target because object tags are implicit in the machine.)
5133 if Is_Tagged_Type (Etype (Expr_Q))
5134 and then (Nkind (Expr_Q) = N_Type_Conversion
5135 or else (Is_Entity_Name (Expr_Q)
5137 Ekind (Entity (Expr_Q)) in Formal_Kind))
5138 and then Tagged_Type_Expansion
5140 Static_Components := False;
5143 elsif Is_Delayed_Aggregate (Expr_Q) then
5144 Static_Components := False;
5147 elsif Possible_Bit_Aligned_Component (Expr_Q) then
5148 Static_Components := False;
5152 if Is_Scalar_Type (Etype (Expr_Q)) then
5153 if not Compile_Time_Known_Value (Expr_Q) then
5154 Static_Components := False;
5157 elsif Nkind (Expr_Q) /= N_Aggregate
5158 or else not Compile_Time_Known_Aggregate (Expr_Q)
5160 Static_Components := False;
5162 if Is_Private_Type (Etype (Expr_Q))
5163 and then Has_Discriminants (Etype (Expr_Q))
5173 end Component_Not_OK_For_Backend;
5175 -- Remaining Expand_Record_Aggregate variables
5177 Tag_Value : Node_Id;
5181 function Has_Visible_Private_Ancestor (Id : E) return Boolean;
5182 -- If any ancestor of the current type is private, the aggregate
5183 -- cannot be built in place. We canot rely on Has_Private_Ancestor,
5184 -- because it will not be set when type and its parent are in the
5185 -- same scope, and the parent component needs expansion.
5187 -----------------------------------
5188 -- Has_Visible_Private_Ancestor --
5189 -----------------------------------
5191 function Has_Visible_Private_Ancestor (Id : E) return Boolean is
5192 R : constant Entity_Id := Root_Type (Id);
5193 T1 : Entity_Id := Id;
5196 if Is_Private_Type (T1) then
5206 end Has_Visible_Private_Ancestor;
5208 -- Start of processing for Expand_Record_Aggregate
5211 -- If the aggregate is to be assigned to an atomic variable, we
5212 -- have to prevent a piecemeal assignment even if the aggregate
5213 -- is to be expanded. We create a temporary for the aggregate, and
5214 -- assign the temporary instead, so that the back end can generate
5215 -- an atomic move for it.
5218 and then Comes_From_Source (Parent (N))
5219 and then Is_Atomic_Aggregate (N, Typ)
5223 -- No special management required for aggregates used to initialize
5224 -- statically allocated dispatch tables
5226 elsif Is_Static_Dispatch_Table_Aggregate (N) then
5230 -- Ada 2005 (AI-318-2): We need to convert to assignments if components
5231 -- are build-in-place function calls. The assignments will each turn
5232 -- into a build-in-place function call. If components are all static,
5233 -- we can pass the aggregate to the backend regardless of limitedness.
5235 -- Extension aggregates, aggregates in extended return statements, and
5236 -- aggregates for C++ imported types must be expanded.
5238 if Ada_Version >= Ada_2005 and then Is_Immutably_Limited_Type (Typ) then
5239 if not Nkind_In (Parent (N), N_Object_Declaration,
5240 N_Component_Association)
5242 Convert_To_Assignments (N, Typ);
5244 elsif Nkind (N) = N_Extension_Aggregate
5245 or else Convention (Typ) = Convention_CPP
5247 Convert_To_Assignments (N, Typ);
5249 elsif not Size_Known_At_Compile_Time (Typ)
5250 or else Component_Not_OK_For_Backend
5251 or else not Static_Components
5253 Convert_To_Assignments (N, Typ);
5256 Set_Compile_Time_Known_Aggregate (N);
5257 Set_Expansion_Delayed (N, False);
5260 -- Gigi doesn't properly handle temporaries of variable size so we
5261 -- generate it in the front-end
5263 elsif not Size_Known_At_Compile_Time (Typ)
5264 and then Tagged_Type_Expansion
5266 Convert_To_Assignments (N, Typ);
5268 -- Temporaries for controlled aggregates need to be attached to a final
5269 -- chain in order to be properly finalized, so it has to be created in
5272 elsif Is_Controlled (Typ)
5273 or else Has_Controlled_Component (Base_Type (Typ))
5275 Convert_To_Assignments (N, Typ);
5277 -- Ada 2005 (AI-287): In case of default initialized components we
5278 -- convert the aggregate into assignments.
5280 elsif Has_Default_Init_Comps (N) then
5281 Convert_To_Assignments (N, Typ);
5285 elsif Component_Not_OK_For_Backend then
5286 Convert_To_Assignments (N, Typ);
5288 -- If an ancestor is private, some components are not inherited and
5289 -- we cannot expand into a record aggregate
5291 elsif Has_Visible_Private_Ancestor (Typ) then
5292 Convert_To_Assignments (N, Typ);
5294 -- ??? The following was done to compile fxacc00.ads in the ACVCs. Gigi
5295 -- is not able to handle the aggregate for Late_Request.
5297 elsif Is_Tagged_Type (Typ) and then Has_Discriminants (Typ) then
5298 Convert_To_Assignments (N, Typ);
5300 -- If the tagged types covers interface types we need to initialize all
5301 -- hidden components containing pointers to secondary dispatch tables.
5303 elsif Is_Tagged_Type (Typ) and then Has_Interfaces (Typ) then
5304 Convert_To_Assignments (N, Typ);
5306 -- If some components are mutable, the size of the aggregate component
5307 -- may be distinct from the default size of the type component, so
5308 -- we need to expand to insure that the back-end copies the proper
5309 -- size of the data. However, if the aggregate is the initial value of
5310 -- a constant, the target is immutable and may be built statically.
5312 elsif Has_Mutable_Components (Typ)
5314 (Nkind (Parent (N)) /= N_Object_Declaration
5315 or else not Constant_Present (Parent (N)))
5317 Convert_To_Assignments (N, Typ);
5319 -- If the type involved has any non-bit aligned components, then we are
5320 -- not sure that the back end can handle this case correctly.
5322 elsif Type_May_Have_Bit_Aligned_Components (Typ) then
5323 Convert_To_Assignments (N, Typ);
5325 -- In all other cases, build a proper aggregate handlable by gigi
5328 if Nkind (N) = N_Aggregate then
5330 -- If the aggregate is static and can be handled by the back-end,
5331 -- nothing left to do.
5333 if Static_Components then
5334 Set_Compile_Time_Known_Aggregate (N);
5335 Set_Expansion_Delayed (N, False);
5339 -- If no discriminants, nothing special to do
5341 if not Has_Discriminants (Typ) then
5344 -- Case of discriminants present
5346 elsif Is_Derived_Type (Typ) then
5348 -- For untagged types, non-stored discriminants are replaced
5349 -- with stored discriminants, which are the ones that gigi uses
5350 -- to describe the type and its components.
5352 Generate_Aggregate_For_Derived_Type : declare
5353 Constraints : constant List_Id := New_List;
5354 First_Comp : Node_Id;
5355 Discriminant : Entity_Id;
5357 Num_Disc : Int := 0;
5358 Num_Gird : Int := 0;
5360 procedure Prepend_Stored_Values (T : Entity_Id);
5361 -- Scan the list of stored discriminants of the type, and add
5362 -- their values to the aggregate being built.
5364 ---------------------------
5365 -- Prepend_Stored_Values --
5366 ---------------------------
5368 procedure Prepend_Stored_Values (T : Entity_Id) is
5370 Discriminant := First_Stored_Discriminant (T);
5371 while Present (Discriminant) loop
5373 Make_Component_Association (Loc,
5375 New_List (New_Occurrence_Of (Discriminant, Loc)),
5379 Get_Discriminant_Value (
5382 Discriminant_Constraint (Typ))));
5384 if No (First_Comp) then
5385 Prepend_To (Component_Associations (N), New_Comp);
5387 Insert_After (First_Comp, New_Comp);
5390 First_Comp := New_Comp;
5391 Next_Stored_Discriminant (Discriminant);
5393 end Prepend_Stored_Values;
5395 -- Start of processing for Generate_Aggregate_For_Derived_Type
5398 -- Remove the associations for the discriminant of derived type
5400 First_Comp := First (Component_Associations (N));
5401 while Present (First_Comp) loop
5406 (First (Choices (Comp)))) = E_Discriminant
5409 Num_Disc := Num_Disc + 1;
5413 -- Insert stored discriminant associations in the correct
5414 -- order. If there are more stored discriminants than new
5415 -- discriminants, there is at least one new discriminant that
5416 -- constrains more than one of the stored discriminants. In
5417 -- this case we need to construct a proper subtype of the
5418 -- parent type, in order to supply values to all the
5419 -- components. Otherwise there is one-one correspondence
5420 -- between the constraints and the stored discriminants.
5422 First_Comp := Empty;
5424 Discriminant := First_Stored_Discriminant (Base_Type (Typ));
5425 while Present (Discriminant) loop
5426 Num_Gird := Num_Gird + 1;
5427 Next_Stored_Discriminant (Discriminant);
5430 -- Case of more stored discriminants than new discriminants
5432 if Num_Gird > Num_Disc then
5434 -- Create a proper subtype of the parent type, which is the
5435 -- proper implementation type for the aggregate, and convert
5436 -- it to the intended target type.
5438 Discriminant := First_Stored_Discriminant (Base_Type (Typ));
5439 while Present (Discriminant) loop
5442 Get_Discriminant_Value (
5445 Discriminant_Constraint (Typ)));
5446 Append (New_Comp, Constraints);
5447 Next_Stored_Discriminant (Discriminant);
5451 Make_Subtype_Declaration (Loc,
5452 Defining_Identifier => Make_Temporary (Loc, 'T'),
5453 Subtype_Indication =>
5454 Make_Subtype_Indication (Loc,
5456 New_Occurrence_Of (Etype (Base_Type (Typ)), Loc),
5458 Make_Index_Or_Discriminant_Constraint
5459 (Loc, Constraints)));
5461 Insert_Action (N, Decl);
5462 Prepend_Stored_Values (Base_Type (Typ));
5464 Set_Etype (N, Defining_Identifier (Decl));
5467 Rewrite (N, Unchecked_Convert_To (Typ, N));
5470 -- Case where we do not have fewer new discriminants than
5471 -- stored discriminants, so in this case we can simply use the
5472 -- stored discriminants of the subtype.
5475 Prepend_Stored_Values (Typ);
5477 end Generate_Aggregate_For_Derived_Type;
5480 if Is_Tagged_Type (Typ) then
5482 -- The tagged case, _parent and _tag component must be created
5484 -- Reset null_present unconditionally. tagged records always have
5485 -- at least one field (the tag or the parent)
5487 Set_Null_Record_Present (N, False);
5489 -- When the current aggregate comes from the expansion of an
5490 -- extension aggregate, the parent expr is replaced by an
5491 -- aggregate formed by selected components of this expr
5493 if Present (Parent_Expr)
5494 and then Is_Empty_List (Comps)
5496 Comp := First_Component_Or_Discriminant (Typ);
5497 while Present (Comp) loop
5499 -- Skip all expander-generated components
5502 not Comes_From_Source (Original_Record_Component (Comp))
5508 Make_Selected_Component (Loc,
5510 Unchecked_Convert_To (Typ,
5511 Duplicate_Subexpr (Parent_Expr, True)),
5513 Selector_Name => New_Occurrence_Of (Comp, Loc));
5516 Make_Component_Association (Loc,
5518 New_List (New_Occurrence_Of (Comp, Loc)),
5522 Analyze_And_Resolve (New_Comp, Etype (Comp));
5525 Next_Component_Or_Discriminant (Comp);
5529 -- Compute the value for the Tag now, if the type is a root it
5530 -- will be included in the aggregate right away, otherwise it will
5531 -- be propagated to the parent aggregate
5533 if Present (Orig_Tag) then
5534 Tag_Value := Orig_Tag;
5535 elsif not Tagged_Type_Expansion then
5540 (Node (First_Elmt (Access_Disp_Table (Typ))), Loc);
5543 -- For a derived type, an aggregate for the parent is formed with
5544 -- all the inherited components.
5546 if Is_Derived_Type (Typ) then
5549 First_Comp : Node_Id;
5550 Parent_Comps : List_Id;
5551 Parent_Aggr : Node_Id;
5552 Parent_Name : Node_Id;
5555 -- Remove the inherited component association from the
5556 -- aggregate and store them in the parent aggregate
5558 First_Comp := First (Component_Associations (N));
5559 Parent_Comps := New_List;
5560 while Present (First_Comp)
5561 and then Scope (Original_Record_Component (
5562 Entity (First (Choices (First_Comp))))) /= Base_Typ
5567 Append (Comp, Parent_Comps);
5570 Parent_Aggr := Make_Aggregate (Loc,
5571 Component_Associations => Parent_Comps);
5572 Set_Etype (Parent_Aggr, Etype (Base_Type (Typ)));
5574 -- Find the _parent component
5576 Comp := First_Component (Typ);
5577 while Chars (Comp) /= Name_uParent loop
5578 Comp := Next_Component (Comp);
5581 Parent_Name := New_Occurrence_Of (Comp, Loc);
5583 -- Insert the parent aggregate
5585 Prepend_To (Component_Associations (N),
5586 Make_Component_Association (Loc,
5587 Choices => New_List (Parent_Name),
5588 Expression => Parent_Aggr));
5590 -- Expand recursively the parent propagating the right Tag
5592 Expand_Record_Aggregate (
5593 Parent_Aggr, Tag_Value, Parent_Expr);
5596 -- For a root type, the tag component is added (unless compiling
5597 -- for the VMs, where tags are implicit).
5599 elsif Tagged_Type_Expansion then
5601 Tag_Name : constant Node_Id :=
5603 (First_Tag_Component (Typ), Loc);
5604 Typ_Tag : constant Entity_Id := RTE (RE_Tag);
5605 Conv_Node : constant Node_Id :=
5606 Unchecked_Convert_To (Typ_Tag, Tag_Value);
5609 Set_Etype (Conv_Node, Typ_Tag);
5610 Prepend_To (Component_Associations (N),
5611 Make_Component_Association (Loc,
5612 Choices => New_List (Tag_Name),
5613 Expression => Conv_Node));
5619 end Expand_Record_Aggregate;
5621 ----------------------------
5622 -- Has_Default_Init_Comps --
5623 ----------------------------
5625 function Has_Default_Init_Comps (N : Node_Id) return Boolean is
5626 Comps : constant List_Id := Component_Associations (N);
5630 pragma Assert (Nkind_In (N, N_Aggregate, N_Extension_Aggregate));
5636 if Has_Self_Reference (N) then
5640 -- Check if any direct component has default initialized components
5643 while Present (C) loop
5644 if Box_Present (C) then
5651 -- Recursive call in case of aggregate expression
5654 while Present (C) loop
5655 Expr := Expression (C);
5659 Nkind_In (Expr, N_Aggregate, N_Extension_Aggregate)
5660 and then Has_Default_Init_Comps (Expr)
5669 end Has_Default_Init_Comps;
5671 --------------------------
5672 -- Is_Delayed_Aggregate --
5673 --------------------------
5675 function Is_Delayed_Aggregate (N : Node_Id) return Boolean is
5676 Node : Node_Id := N;
5677 Kind : Node_Kind := Nkind (Node);
5680 if Kind = N_Qualified_Expression then
5681 Node := Expression (Node);
5682 Kind := Nkind (Node);
5685 if Kind /= N_Aggregate and then Kind /= N_Extension_Aggregate then
5688 return Expansion_Delayed (Node);
5690 end Is_Delayed_Aggregate;
5692 ----------------------------------------
5693 -- Is_Static_Dispatch_Table_Aggregate --
5694 ----------------------------------------
5696 function Is_Static_Dispatch_Table_Aggregate (N : Node_Id) return Boolean is
5697 Typ : constant Entity_Id := Base_Type (Etype (N));
5700 return Static_Dispatch_Tables
5701 and then Tagged_Type_Expansion
5702 and then RTU_Loaded (Ada_Tags)
5704 -- Avoid circularity when rebuilding the compiler
5706 and then Cunit_Entity (Get_Source_Unit (N)) /= RTU_Entity (Ada_Tags)
5707 and then (Typ = RTE (RE_Dispatch_Table_Wrapper)
5709 Typ = RTE (RE_Address_Array)
5711 Typ = RTE (RE_Type_Specific_Data)
5713 Typ = RTE (RE_Tag_Table)
5715 (RTE_Available (RE_Interface_Data)
5716 and then Typ = RTE (RE_Interface_Data))
5718 (RTE_Available (RE_Interfaces_Array)
5719 and then Typ = RTE (RE_Interfaces_Array))
5721 (RTE_Available (RE_Interface_Data_Element)
5722 and then Typ = RTE (RE_Interface_Data_Element)));
5723 end Is_Static_Dispatch_Table_Aggregate;
5725 --------------------
5726 -- Late_Expansion --
5727 --------------------
5729 function Late_Expansion
5732 Target : Node_Id) return List_Id
5735 if Is_Record_Type (Etype (N)) then
5736 return Build_Record_Aggr_Code (N, Typ, Target);
5738 else pragma Assert (Is_Array_Type (Etype (N)));
5740 Build_Array_Aggr_Code
5742 Ctype => Component_Type (Etype (N)),
5743 Index => First_Index (Typ),
5745 Scalar_Comp => Is_Scalar_Type (Component_Type (Typ)),
5746 Indexes => No_List);
5750 ----------------------------------
5751 -- Make_OK_Assignment_Statement --
5752 ----------------------------------
5754 function Make_OK_Assignment_Statement
5757 Expression : Node_Id) return Node_Id
5760 Set_Assignment_OK (Name);
5762 return Make_Assignment_Statement (Sloc, Name, Expression);
5763 end Make_OK_Assignment_Statement;
5765 -----------------------
5766 -- Number_Of_Choices --
5767 -----------------------
5769 function Number_Of_Choices (N : Node_Id) return Nat is
5773 Nb_Choices : Nat := 0;
5776 if Present (Expressions (N)) then
5780 Assoc := First (Component_Associations (N));
5781 while Present (Assoc) loop
5782 Choice := First (Choices (Assoc));
5783 while Present (Choice) loop
5784 if Nkind (Choice) /= N_Others_Choice then
5785 Nb_Choices := Nb_Choices + 1;
5795 end Number_Of_Choices;
5797 ------------------------------------
5798 -- Packed_Array_Aggregate_Handled --
5799 ------------------------------------
5801 -- The current version of this procedure will handle at compile time
5802 -- any array aggregate that meets these conditions:
5804 -- One dimensional, bit packed
5805 -- Underlying packed type is modular type
5806 -- Bounds are within 32-bit Int range
5807 -- All bounds and values are static
5809 function Packed_Array_Aggregate_Handled (N : Node_Id) return Boolean is
5810 Loc : constant Source_Ptr := Sloc (N);
5811 Typ : constant Entity_Id := Etype (N);
5812 Ctyp : constant Entity_Id := Component_Type (Typ);
5814 Not_Handled : exception;
5815 -- Exception raised if this aggregate cannot be handled
5818 -- For now, handle only one dimensional bit packed arrays
5820 if not Is_Bit_Packed_Array (Typ)
5821 or else Number_Dimensions (Typ) > 1
5822 or else not Is_Modular_Integer_Type (Packed_Array_Type (Typ))
5827 if not Is_Scalar_Type (Component_Type (Typ))
5828 and then Has_Non_Standard_Rep (Component_Type (Typ))
5834 Csiz : constant Nat := UI_To_Int (Component_Size (Typ));
5838 -- Bounds of index type
5842 -- Values of bounds if compile time known
5844 function Get_Component_Val (N : Node_Id) return Uint;
5845 -- Given a expression value N of the component type Ctyp, returns a
5846 -- value of Csiz (component size) bits representing this value. If
5847 -- the value is non-static or any other reason exists why the value
5848 -- cannot be returned, then Not_Handled is raised.
5850 -----------------------
5851 -- Get_Component_Val --
5852 -----------------------
5854 function Get_Component_Val (N : Node_Id) return Uint is
5858 -- We have to analyze the expression here before doing any further
5859 -- processing here. The analysis of such expressions is deferred
5860 -- till expansion to prevent some problems of premature analysis.
5862 Analyze_And_Resolve (N, Ctyp);
5864 -- Must have a compile time value. String literals have to be
5865 -- converted into temporaries as well, because they cannot easily
5866 -- be converted into their bit representation.
5868 if not Compile_Time_Known_Value (N)
5869 or else Nkind (N) = N_String_Literal
5874 Val := Expr_Rep_Value (N);
5876 -- Adjust for bias, and strip proper number of bits
5878 if Has_Biased_Representation (Ctyp) then
5879 Val := Val - Expr_Value (Type_Low_Bound (Ctyp));
5882 return Val mod Uint_2 ** Csiz;
5883 end Get_Component_Val;
5885 -- Here we know we have a one dimensional bit packed array
5888 Get_Index_Bounds (First_Index (Typ), Lo, Hi);
5890 -- Cannot do anything if bounds are dynamic
5892 if not Compile_Time_Known_Value (Lo)
5894 not Compile_Time_Known_Value (Hi)
5899 -- Or are silly out of range of int bounds
5901 Lob := Expr_Value (Lo);
5902 Hib := Expr_Value (Hi);
5904 if not UI_Is_In_Int_Range (Lob)
5906 not UI_Is_In_Int_Range (Hib)
5911 -- At this stage we have a suitable aggregate for handling at compile
5912 -- time (the only remaining checks are that the values of expressions
5913 -- in the aggregate are compile time known (check is performed by
5914 -- Get_Component_Val), and that any subtypes or ranges are statically
5917 -- If the aggregate is not fully positional at this stage, then
5918 -- convert it to positional form. Either this will fail, in which
5919 -- case we can do nothing, or it will succeed, in which case we have
5920 -- succeeded in handling the aggregate, or it will stay an aggregate,
5921 -- in which case we have failed to handle this case.
5923 if Present (Component_Associations (N)) then
5924 Convert_To_Positional
5925 (N, Max_Others_Replicate => 64, Handle_Bit_Packed => True);
5926 return Nkind (N) /= N_Aggregate;
5929 -- Otherwise we are all positional, so convert to proper value
5932 Lov : constant Int := UI_To_Int (Lob);
5933 Hiv : constant Int := UI_To_Int (Hib);
5935 Len : constant Nat := Int'Max (0, Hiv - Lov + 1);
5936 -- The length of the array (number of elements)
5938 Aggregate_Val : Uint;
5939 -- Value of aggregate. The value is set in the low order bits of
5940 -- this value. For the little-endian case, the values are stored
5941 -- from low-order to high-order and for the big-endian case the
5942 -- values are stored from high-order to low-order. Note that gigi
5943 -- will take care of the conversions to left justify the value in
5944 -- the big endian case (because of left justified modular type
5945 -- processing), so we do not have to worry about that here.
5948 -- Integer literal for resulting constructed value
5951 -- Shift count from low order for next value
5954 -- Shift increment for loop
5957 -- Next expression from positional parameters of aggregate
5960 -- For little endian, we fill up the low order bits of the target
5961 -- value. For big endian we fill up the high order bits of the
5962 -- target value (which is a left justified modular value).
5964 if Bytes_Big_Endian xor Debug_Flag_8 then
5965 Shift := Csiz * (Len - 1);
5972 -- Loop to set the values
5975 Aggregate_Val := Uint_0;
5977 Expr := First (Expressions (N));
5978 Aggregate_Val := Get_Component_Val (Expr) * Uint_2 ** Shift;
5980 for J in 2 .. Len loop
5981 Shift := Shift + Incr;
5984 Aggregate_Val + Get_Component_Val (Expr) * Uint_2 ** Shift;
5988 -- Now we can rewrite with the proper value
5991 Make_Integer_Literal (Loc,
5992 Intval => Aggregate_Val);
5993 Set_Print_In_Hex (Lit);
5995 -- Construct the expression using this literal. Note that it is
5996 -- important to qualify the literal with its proper modular type
5997 -- since universal integer does not have the required range and
5998 -- also this is a left justified modular type, which is important
5999 -- in the big-endian case.
6002 Unchecked_Convert_To (Typ,
6003 Make_Qualified_Expression (Loc,
6005 New_Occurrence_Of (Packed_Array_Type (Typ), Loc),
6006 Expression => Lit)));
6008 Analyze_And_Resolve (N, Typ);
6016 end Packed_Array_Aggregate_Handled;
6018 ----------------------------
6019 -- Has_Mutable_Components --
6020 ----------------------------
6022 function Has_Mutable_Components (Typ : Entity_Id) return Boolean is
6026 Comp := First_Component (Typ);
6027 while Present (Comp) loop
6028 if Is_Record_Type (Etype (Comp))
6029 and then Has_Discriminants (Etype (Comp))
6030 and then not Is_Constrained (Etype (Comp))
6035 Next_Component (Comp);
6039 end Has_Mutable_Components;
6041 ------------------------------
6042 -- Initialize_Discriminants --
6043 ------------------------------
6045 procedure Initialize_Discriminants (N : Node_Id; Typ : Entity_Id) is
6046 Loc : constant Source_Ptr := Sloc (N);
6047 Bas : constant Entity_Id := Base_Type (Typ);
6048 Par : constant Entity_Id := Etype (Bas);
6049 Decl : constant Node_Id := Parent (Par);
6053 if Is_Tagged_Type (Bas)
6054 and then Is_Derived_Type (Bas)
6055 and then Has_Discriminants (Par)
6056 and then Has_Discriminants (Bas)
6057 and then Number_Discriminants (Bas) /= Number_Discriminants (Par)
6058 and then Nkind (Decl) = N_Full_Type_Declaration
6059 and then Nkind (Type_Definition (Decl)) = N_Record_Definition
6061 (Variant_Part (Component_List (Type_Definition (Decl))))
6062 and then Nkind (N) /= N_Extension_Aggregate
6065 -- Call init proc to set discriminants.
6066 -- There should eventually be a special procedure for this ???
6068 Ref := New_Reference_To (Defining_Identifier (N), Loc);
6069 Insert_Actions_After (N,
6070 Build_Initialization_Call (Sloc (N), Ref, Typ));
6072 end Initialize_Discriminants;
6079 (Obj_Type : Entity_Id;
6080 Typ : Entity_Id) return Boolean
6082 L1, L2, H1, H2 : Node_Id;
6084 -- No sliding if the type of the object is not established yet, if it is
6085 -- an unconstrained type whose actual subtype comes from the aggregate,
6086 -- or if the two types are identical.
6088 if not Is_Array_Type (Obj_Type) then
6091 elsif not Is_Constrained (Obj_Type) then
6094 elsif Typ = Obj_Type then
6098 -- Sliding can only occur along the first dimension
6100 Get_Index_Bounds (First_Index (Typ), L1, H1);
6101 Get_Index_Bounds (First_Index (Obj_Type), L2, H2);
6103 if not Is_Static_Expression (L1)
6104 or else not Is_Static_Expression (L2)
6105 or else not Is_Static_Expression (H1)
6106 or else not Is_Static_Expression (H2)
6110 return Expr_Value (L1) /= Expr_Value (L2)
6111 or else Expr_Value (H1) /= Expr_Value (H2);
6116 ---------------------------
6117 -- Safe_Slice_Assignment --
6118 ---------------------------
6120 function Safe_Slice_Assignment (N : Node_Id) return Boolean is
6121 Loc : constant Source_Ptr := Sloc (Parent (N));
6122 Pref : constant Node_Id := Prefix (Name (Parent (N)));
6123 Range_Node : constant Node_Id := Discrete_Range (Name (Parent (N)));
6131 -- Generate: for J in Range loop Pref (J) := Expr; end loop;
6133 if Comes_From_Source (N)
6134 and then No (Expressions (N))
6135 and then Nkind (First (Choices (First (Component_Associations (N)))))
6138 Expr := Expression (First (Component_Associations (N)));
6139 L_J := Make_Temporary (Loc, 'J');
6142 Make_Iteration_Scheme (Loc,
6143 Loop_Parameter_Specification =>
6144 Make_Loop_Parameter_Specification
6146 Defining_Identifier => L_J,
6147 Discrete_Subtype_Definition => Relocate_Node (Range_Node)));
6150 Make_Assignment_Statement (Loc,
6152 Make_Indexed_Component (Loc,
6153 Prefix => Relocate_Node (Pref),
6154 Expressions => New_List (New_Occurrence_Of (L_J, Loc))),
6155 Expression => Relocate_Node (Expr));
6157 -- Construct the final loop
6160 Make_Implicit_Loop_Statement
6161 (Node => Parent (N),
6162 Identifier => Empty,
6163 Iteration_Scheme => L_Iter,
6164 Statements => New_List (L_Body));
6166 -- Set type of aggregate to be type of lhs in assignment,
6167 -- to suppress redundant length checks.
6169 Set_Etype (N, Etype (Name (Parent (N))));
6171 Rewrite (Parent (N), Stat);
6172 Analyze (Parent (N));
6178 end Safe_Slice_Assignment;
6180 ---------------------
6181 -- Sort_Case_Table --
6182 ---------------------
6184 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type) is
6185 L : constant Int := Case_Table'First;
6186 U : constant Int := Case_Table'Last;
6194 T := Case_Table (K + 1);
6198 and then Expr_Value (Case_Table (J - 1).Choice_Lo) >
6199 Expr_Value (T.Choice_Lo)
6201 Case_Table (J) := Case_Table (J - 1);
6205 Case_Table (J) := T;
6208 end Sort_Case_Table;
6210 ----------------------------
6211 -- Static_Array_Aggregate --
6212 ----------------------------
6214 function Static_Array_Aggregate (N : Node_Id) return Boolean is
6215 Bounds : constant Node_Id := Aggregate_Bounds (N);
6217 Typ : constant Entity_Id := Etype (N);
6218 Comp_Type : constant Entity_Id := Component_Type (Typ);
6225 if Is_Tagged_Type (Typ)
6226 or else Is_Controlled (Typ)
6227 or else Is_Packed (Typ)
6233 and then Nkind (Bounds) = N_Range
6234 and then Nkind (Low_Bound (Bounds)) = N_Integer_Literal
6235 and then Nkind (High_Bound (Bounds)) = N_Integer_Literal
6237 Lo := Low_Bound (Bounds);
6238 Hi := High_Bound (Bounds);
6240 if No (Component_Associations (N)) then
6242 -- Verify that all components are static integers
6244 Expr := First (Expressions (N));
6245 while Present (Expr) loop
6246 if Nkind (Expr) /= N_Integer_Literal then
6256 -- We allow only a single named association, either a static
6257 -- range or an others_clause, with a static expression.
6259 Expr := First (Component_Associations (N));
6261 if Present (Expressions (N)) then
6264 elsif Present (Next (Expr)) then
6267 elsif Present (Next (First (Choices (Expr)))) then
6271 -- The aggregate is static if all components are literals,
6272 -- or else all its components are static aggregates for the
6273 -- component type. We also limit the size of a static aggregate
6274 -- to prevent runaway static expressions.
6276 if Is_Array_Type (Comp_Type)
6277 or else Is_Record_Type (Comp_Type)
6279 if Nkind (Expression (Expr)) /= N_Aggregate
6281 not Compile_Time_Known_Aggregate (Expression (Expr))
6286 elsif Nkind (Expression (Expr)) /= N_Integer_Literal then
6290 if not Aggr_Size_OK (N, Typ) then
6294 -- Create a positional aggregate with the right number of
6295 -- copies of the expression.
6297 Agg := Make_Aggregate (Sloc (N), New_List, No_List);
6299 for I in UI_To_Int (Intval (Lo)) .. UI_To_Int (Intval (Hi))
6302 (Expressions (Agg), New_Copy (Expression (Expr)));
6304 -- The copied expression must be analyzed and resolved.
6305 -- Besides setting the type, this ensures that static
6306 -- expressions are appropriately marked as such.
6309 (Last (Expressions (Agg)), Component_Type (Typ));
6312 Set_Aggregate_Bounds (Agg, Bounds);
6313 Set_Etype (Agg, Typ);
6316 Set_Compile_Time_Known_Aggregate (N);
6325 end Static_Array_Aggregate;