1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2004 Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 2, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, USA. --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
25 ------------------------------------------------------------------------------
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Debug; use Debug;
30 with Einfo; use Einfo;
31 with Elists; use Elists;
32 with Expander; use Expander;
33 with Exp_Util; use Exp_Util;
34 with Exp_Ch3; use Exp_Ch3;
35 with Exp_Ch7; use Exp_Ch7;
36 with Exp_Ch9; use Exp_Ch9;
37 with Exp_Tss; use Exp_Tss;
38 with Freeze; use Freeze;
39 with Hostparm; use Hostparm;
40 with Itypes; use Itypes;
42 with Nmake; use Nmake;
43 with Nlists; use Nlists;
44 with Restrict; use Restrict;
45 with Rident; use Rident;
46 with Rtsfind; use Rtsfind;
47 with Ttypes; use Ttypes;
49 with Sem_Ch3; use Sem_Ch3;
50 with Sem_Eval; use Sem_Eval;
51 with Sem_Res; use Sem_Res;
52 with Sem_Util; use Sem_Util;
53 with Sinfo; use Sinfo;
54 with Snames; use Snames;
55 with Stand; use Stand;
56 with Tbuild; use Tbuild;
57 with Uintp; use Uintp;
59 package body Exp_Aggr is
61 type Case_Bounds is record
64 Choice_Node : Node_Id;
67 type Case_Table_Type is array (Nat range <>) of Case_Bounds;
68 -- Table type used by Check_Case_Choices procedure
70 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type);
71 -- Sort the Case Table using the Lower Bound of each Choice as the key.
72 -- A simple insertion sort is used since the number of choices in a case
73 -- statement of variant part will usually be small and probably in near
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 ------------------------------------------------------
81 -- Local subprograms for Record Aggregate Expansion --
82 ------------------------------------------------------
84 procedure Expand_Record_Aggregate
86 Orig_Tag : Node_Id := Empty;
87 Parent_Expr : Node_Id := Empty);
88 -- This is the top level procedure for record aggregate expansion.
89 -- Expansion for record aggregates needs expand aggregates for tagged
90 -- record types. Specifically Expand_Record_Aggregate adds the Tag
91 -- field in front of the Component_Association list that was created
92 -- during resolution by Resolve_Record_Aggregate.
94 -- N is the record aggregate node.
95 -- Orig_Tag is the value of the Tag that has to be provided for this
96 -- specific aggregate. It carries the tag corresponding to the type
97 -- of the outermost aggregate during the recursive expansion
98 -- Parent_Expr is the ancestor part of the original extension
101 procedure Convert_To_Assignments (N : Node_Id; Typ : Entity_Id);
102 -- N is an N_Aggregate of a N_Extension_Aggregate. Typ is the type of
103 -- the aggregate. Transform the given aggregate into a sequence of
104 -- assignments component per component.
106 function Build_Record_Aggr_Code
110 Flist : Node_Id := Empty;
111 Obj : Entity_Id := Empty;
112 Is_Limited_Ancestor_Expansion : Boolean := False) return List_Id;
113 -- N is an N_Aggregate or a N_Extension_Aggregate. Typ is the type
114 -- of the aggregate. Target is an expression containing the
115 -- location on which the component by component assignments will
116 -- take place. Returns the list of assignments plus all other
117 -- adjustments needed for tagged and controlled types. Flist is an
118 -- expression representing the finalization list on which to
119 -- attach the controlled components if any. Obj is present in the
120 -- object declaration and dynamic allocation cases, it contains
121 -- an entity that allows to know if the value being created needs to be
122 -- attached to the final list in case of pragma finalize_Storage_Only.
123 -- Is_Limited_Ancestor_Expansion indicates that the function has been
124 -- called recursively to expand the limited ancestor to avoid copying it.
126 function Has_Mutable_Components (Typ : Entity_Id) return Boolean;
127 -- Return true if one of the component is of a discriminated type with
128 -- defaults. An aggregate for a type with mutable components must be
129 -- expanded into individual assignments.
131 procedure Initialize_Discriminants (N : Node_Id; Typ : Entity_Id);
132 -- If the type of the aggregate is a type extension with renamed discrimi-
133 -- nants, we must initialize the hidden discriminants of the parent.
134 -- Otherwise, the target object must not be initialized. The discriminants
135 -- are initialized by calling the initialization procedure for the type.
136 -- This is incorrect if the initialization of other components has any
137 -- side effects. We restrict this call to the case where the parent type
138 -- has a variant part, because this is the only case where the hidden
139 -- discriminants are accessed, namely when calling discriminant checking
140 -- functions of the parent type, and when applying a stream attribute to
141 -- an object of the derived type.
143 -----------------------------------------------------
144 -- Local Subprograms for Array Aggregate Expansion --
145 -----------------------------------------------------
147 procedure Convert_Array_Aggr_In_Allocator
151 -- If the aggregate appears within an allocator and can be expanded in
152 -- place, this routine generates the individual assignments to components
153 -- of the designated object. This is an optimization over the general
154 -- case, where a temporary is first created on the stack and then used to
155 -- construct the allocated object on the heap.
157 procedure Convert_To_Positional
159 Max_Others_Replicate : Nat := 5;
160 Handle_Bit_Packed : Boolean := False);
161 -- If possible, convert named notation to positional notation. This
162 -- conversion is possible only in some static cases. If the conversion
163 -- is possible, then N is rewritten with the analyzed converted
164 -- aggregate. The parameter Max_Others_Replicate controls the maximum
165 -- number of values corresponding to an others choice that will be
166 -- converted to positional notation (the default of 5 is the normal
167 -- limit, and reflects the fact that normally the loop is better than
168 -- a lot of separate assignments). Note that this limit gets overridden
169 -- in any case if either of the restrictions No_Elaboration_Code or
170 -- No_Implicit_Loops is set. The parameter Handle_Bit_Packed is usually
171 -- set False (since we do not expect the back end to handle bit packed
172 -- arrays, so the normal case of conversion is pointless), but in the
173 -- special case of a call from Packed_Array_Aggregate_Handled, we set
174 -- this parameter to True, since these are cases we handle in there.
176 procedure Expand_Array_Aggregate (N : Node_Id);
177 -- This is the top-level routine to perform array aggregate expansion.
178 -- N is the N_Aggregate node to be expanded.
180 function Backend_Processing_Possible (N : Node_Id) return Boolean;
181 -- This function checks if array aggregate N can be processed directly
182 -- by Gigi. If this is the case True is returned.
184 function Build_Array_Aggr_Code
189 Scalar_Comp : Boolean;
190 Indices : List_Id := No_List;
191 Flist : Node_Id := Empty) return List_Id;
192 -- This recursive routine returns a list of statements containing the
193 -- loops and assignments that are needed for the expansion of the array
196 -- N is the (sub-)aggregate node to be expanded into code. This node
197 -- has been fully analyzed, and its Etype is properly set.
199 -- Index is the index node corresponding to the array sub-aggregate N.
201 -- Into is the target expression into which we are copying the aggregate.
202 -- Note that this node may not have been analyzed yet, and so the Etype
203 -- field may not be set.
205 -- Scalar_Comp is True if the component type of the aggregate is scalar.
207 -- Indices is the current list of expressions used to index the
208 -- object we are writing into.
210 -- Flist is an expression representing the finalization list on which
211 -- to attach the controlled components if any.
213 function Number_Of_Choices (N : Node_Id) return Nat;
214 -- Returns the number of discrete choices (not including the others choice
215 -- if present) contained in (sub-)aggregate N.
217 function Late_Expansion
221 Flist : Node_Id := Empty;
222 Obj : Entity_Id := Empty) return List_Id;
223 -- N is a nested (record or array) aggregate that has been marked
224 -- with 'Delay_Expansion'. Typ is the expected type of the
225 -- aggregate and Target is a (duplicable) expression that will
226 -- hold the result of the aggregate expansion. Flist is the
227 -- finalization list to be used to attach controlled
228 -- components. 'Obj' when non empty, carries the original object
229 -- being initialized in order to know if it needs to be attached
230 -- to the previous parameter which may not be the case when
231 -- Finalize_Storage_Only is set. Basically this procedure is used
232 -- to implement top-down expansions of nested aggregates. This is
233 -- necessary for avoiding temporaries at each level as well as for
234 -- propagating the right internal finalization list.
236 function Make_OK_Assignment_Statement
239 Expression : Node_Id) return Node_Id;
240 -- This is like Make_Assignment_Statement, except that Assignment_OK
241 -- is set in the left operand. All assignments built by this unit
242 -- use this routine. This is needed to deal with assignments to
243 -- initialized constants that are done in place.
245 function Packed_Array_Aggregate_Handled (N : Node_Id) return Boolean;
246 -- Given an array aggregate, this function handles the case of a packed
247 -- array aggregate with all constant values, where the aggregate can be
248 -- evaluated at compile time. If this is possible, then N is rewritten
249 -- to be its proper compile time value with all the components properly
250 -- assembled. The expression is analyzed and resolved and True is
251 -- returned. If this transformation is not possible, N is unchanged
252 -- and False is returned
254 function Safe_Slice_Assignment (N : Node_Id) return Boolean;
255 -- If a slice assignment has an aggregate with a single others_choice,
256 -- the assignment can be done in place even if bounds are not static,
257 -- by converting it into a loop over the discrete range of the slice.
259 ---------------------------------
260 -- Backend_Processing_Possible --
261 ---------------------------------
263 -- Backend processing by Gigi/gcc is possible only if all the following
264 -- conditions are met:
266 -- 1. N is fully positional
268 -- 2. N is not a bit-packed array aggregate;
270 -- 3. The size of N's array type must be known at compile time. Note
271 -- that this implies that the component size is also known
273 -- 4. The array type of N does not follow the Fortran layout convention
274 -- or if it does it must be 1 dimensional.
276 -- 5. The array component type is tagged, which may necessitate
277 -- reassignment of proper tags.
279 -- 6. The array component type might have unaligned bit components
281 function Backend_Processing_Possible (N : Node_Id) return Boolean is
282 Typ : constant Entity_Id := Etype (N);
283 -- Typ is the correct constrained array subtype of the aggregate.
285 function Static_Check (N : Node_Id; Index : Node_Id) return Boolean;
286 -- Recursively checks that N is fully positional, returns true if so.
292 function Static_Check (N : Node_Id; Index : Node_Id) return Boolean is
296 -- Check for component associations
298 if Present (Component_Associations (N)) then
302 -- Recurse to check subaggregates, which may appear in qualified
303 -- expressions. If delayed, the front-end will have to expand.
305 Expr := First (Expressions (N));
307 while Present (Expr) loop
309 if Is_Delayed_Aggregate (Expr) then
313 if Present (Next_Index (Index))
314 and then not Static_Check (Expr, Next_Index (Index))
325 -- Start of processing for Backend_Processing_Possible
328 -- Checks 2 (array must not be bit packed)
330 if Is_Bit_Packed_Array (Typ) then
334 -- Checks 4 (array must not be multi-dimensional Fortran case)
336 if Convention (Typ) = Convention_Fortran
337 and then Number_Dimensions (Typ) > 1
342 -- Checks 3 (size of array must be known at compile time)
344 if not Size_Known_At_Compile_Time (Typ) then
348 -- Checks 1 (aggregate must be fully positional)
350 if not Static_Check (N, First_Index (Typ)) then
354 -- Checks 5 (if the component type is tagged, then we may need
355 -- to do tag adjustments; perhaps this should be refined to
356 -- check for any component associations that actually
357 -- need tag adjustment, along the lines of the test that's
358 -- done in Has_Delayed_Nested_Aggregate_Or_Tagged_Comps
359 -- for record aggregates with tagged components, but not
360 -- clear whether it's worthwhile ???; in the case of the
361 -- JVM, object tags are handled implicitly)
363 if Is_Tagged_Type (Component_Type (Typ)) and then not Java_VM then
367 -- Checks 6 (component type must not have bit aligned components)
369 if Type_May_Have_Bit_Aligned_Components (Component_Type (Typ)) then
373 -- Backend processing is possible
375 Set_Compile_Time_Known_Aggregate (N, True);
376 Set_Size_Known_At_Compile_Time (Etype (N), True);
378 end Backend_Processing_Possible;
380 ---------------------------
381 -- Build_Array_Aggr_Code --
382 ---------------------------
384 -- The code that we generate from a one dimensional aggregate is
386 -- 1. If the sub-aggregate contains discrete choices we
388 -- (a) Sort the discrete choices
390 -- (b) Otherwise for each discrete choice that specifies a range we
391 -- emit a loop. If a range specifies a maximum of three values, or
392 -- we are dealing with an expression we emit a sequence of
393 -- assignments instead of a loop.
395 -- (c) Generate the remaining loops to cover the others choice if any.
397 -- 2. If the aggregate contains positional elements we
399 -- (a) translate the positional elements in a series of assignments.
401 -- (b) Generate a final loop to cover the others choice if any.
402 -- Note that this final loop has to be a while loop since the case
404 -- L : Integer := Integer'Last;
405 -- H : Integer := Integer'Last;
406 -- A : array (L .. H) := (1, others =>0);
408 -- cannot be handled by a for loop. Thus for the following
410 -- array (L .. H) := (.. positional elements.., others =>E);
412 -- we always generate something like:
414 -- J : Index_Type := Index_Of_Last_Positional_Element;
416 -- J := Index_Base'Succ (J)
420 function Build_Array_Aggr_Code
425 Scalar_Comp : Boolean;
426 Indices : List_Id := No_List;
427 Flist : Node_Id := Empty) return List_Id
429 Loc : constant Source_Ptr := Sloc (N);
430 Index_Base : constant Entity_Id := Base_Type (Etype (Index));
431 Index_Base_L : constant Node_Id := Type_Low_Bound (Index_Base);
432 Index_Base_H : constant Node_Id := Type_High_Bound (Index_Base);
434 function Add (Val : Int; To : Node_Id) return Node_Id;
435 -- Returns an expression where Val is added to expression To,
436 -- unless To+Val is provably out of To's base type range.
437 -- To must be an already analyzed expression.
439 function Empty_Range (L, H : Node_Id) return Boolean;
440 -- Returns True if the range defined by L .. H is certainly empty.
442 function Equal (L, H : Node_Id) return Boolean;
443 -- Returns True if L = H for sure.
445 function Index_Base_Name return Node_Id;
446 -- Returns a new reference to the index type name.
448 function Gen_Assign (Ind : Node_Id; Expr : Node_Id) return List_Id;
449 -- Ind must be a side-effect free expression. If the input aggregate
450 -- N to Build_Loop contains no sub-aggregates, then this function
451 -- returns the assignment statement:
453 -- Into (Indices, Ind) := Expr;
455 -- Otherwise we call Build_Code recursively.
457 -- Ada 2005 (AI-287): In case of default initialized component, Expr
458 -- is empty and we generate a call to the corresponding IP subprogram.
460 function Gen_Loop (L, H : Node_Id; Expr : Node_Id) return List_Id;
461 -- Nodes L and H must be side-effect free expressions.
462 -- If the input aggregate N to Build_Loop contains no sub-aggregates,
463 -- This routine returns the for loop statement
465 -- for J in Index_Base'(L) .. Index_Base'(H) loop
466 -- Into (Indices, J) := Expr;
469 -- Otherwise we call Build_Code recursively.
470 -- As an optimization if the loop covers 3 or less scalar elements we
471 -- generate a sequence of assignments.
473 function Gen_While (L, H : Node_Id; Expr : Node_Id) return List_Id;
474 -- Nodes L and H must be side-effect free expressions.
475 -- If the input aggregate N to Build_Loop contains no sub-aggregates,
476 -- This routine returns the while loop statement
478 -- J : Index_Base := L;
480 -- J := Index_Base'Succ (J);
481 -- Into (Indices, J) := Expr;
484 -- Otherwise we call Build_Code recursively
486 function Local_Compile_Time_Known_Value (E : Node_Id) return Boolean;
487 function Local_Expr_Value (E : Node_Id) return Uint;
488 -- These two Local routines are used to replace the corresponding ones
489 -- in sem_eval because while processing the bounds of an aggregate with
490 -- discrete choices whose index type is an enumeration, we build static
491 -- expressions not recognized by Compile_Time_Known_Value as such since
492 -- they have not yet been analyzed and resolved. All the expressions in
493 -- question are things like Index_Base_Name'Val (Const) which we can
494 -- easily recognize as being constant.
500 function Add (Val : Int; To : Node_Id) return Node_Id is
505 U_Val : constant Uint := UI_From_Int (Val);
508 -- Note: do not try to optimize the case of Val = 0, because
509 -- we need to build a new node with the proper Sloc value anyway.
511 -- First test if we can do constant folding
513 if Local_Compile_Time_Known_Value (To) then
514 U_To := Local_Expr_Value (To) + Val;
516 -- Determine if our constant is outside the range of the index.
517 -- If so return an Empty node. This empty node will be caught
518 -- by Empty_Range below.
520 if Compile_Time_Known_Value (Index_Base_L)
521 and then U_To < Expr_Value (Index_Base_L)
525 elsif Compile_Time_Known_Value (Index_Base_H)
526 and then U_To > Expr_Value (Index_Base_H)
531 Expr_Pos := Make_Integer_Literal (Loc, U_To);
532 Set_Is_Static_Expression (Expr_Pos);
534 if not Is_Enumeration_Type (Index_Base) then
537 -- If we are dealing with enumeration return
538 -- Index_Base'Val (Expr_Pos)
542 Make_Attribute_Reference
544 Prefix => Index_Base_Name,
545 Attribute_Name => Name_Val,
546 Expressions => New_List (Expr_Pos));
552 -- If we are here no constant folding possible
554 if not Is_Enumeration_Type (Index_Base) then
557 Left_Opnd => Duplicate_Subexpr (To),
558 Right_Opnd => Make_Integer_Literal (Loc, U_Val));
560 -- If we are dealing with enumeration return
561 -- Index_Base'Val (Index_Base'Pos (To) + Val)
565 Make_Attribute_Reference
567 Prefix => Index_Base_Name,
568 Attribute_Name => Name_Pos,
569 Expressions => New_List (Duplicate_Subexpr (To)));
574 Right_Opnd => Make_Integer_Literal (Loc, U_Val));
577 Make_Attribute_Reference
579 Prefix => Index_Base_Name,
580 Attribute_Name => Name_Val,
581 Expressions => New_List (Expr_Pos));
591 function Empty_Range (L, H : Node_Id) return Boolean is
592 Is_Empty : Boolean := False;
597 -- First check if L or H were already detected as overflowing the
598 -- index base range type by function Add above. If this is so Add
599 -- returns the empty node.
601 if No (L) or else No (H) then
608 -- L > H range is empty
614 -- B_L > H range must be empty
620 -- L > B_H range must be empty
624 High := Index_Base_H;
627 if Local_Compile_Time_Known_Value (Low)
628 and then Local_Compile_Time_Known_Value (High)
631 UI_Gt (Local_Expr_Value (Low), Local_Expr_Value (High));
644 function Equal (L, H : Node_Id) return Boolean is
649 elsif Local_Compile_Time_Known_Value (L)
650 and then Local_Compile_Time_Known_Value (H)
652 return UI_Eq (Local_Expr_Value (L), Local_Expr_Value (H));
662 function Gen_Assign (Ind : Node_Id; Expr : Node_Id) return List_Id is
663 L : constant List_Id := New_List;
667 New_Indices : List_Id;
668 Indexed_Comp : Node_Id;
670 Comp_Type : Entity_Id := Empty;
672 function Add_Loop_Actions (Lis : List_Id) return List_Id;
673 -- Collect insert_actions generated in the construction of a
674 -- loop, and prepend them to the sequence of assignments to
675 -- complete the eventual body of the loop.
677 ----------------------
678 -- Add_Loop_Actions --
679 ----------------------
681 function Add_Loop_Actions (Lis : List_Id) return List_Id is
685 -- Ada 2005 (AI-287): Do nothing else in case of default
686 -- initialized component.
688 if not Present (Expr) then
691 elsif Nkind (Parent (Expr)) = N_Component_Association
692 and then Present (Loop_Actions (Parent (Expr)))
694 Append_List (Lis, Loop_Actions (Parent (Expr)));
695 Res := Loop_Actions (Parent (Expr));
696 Set_Loop_Actions (Parent (Expr), No_List);
702 end Add_Loop_Actions;
704 -- Start of processing for Gen_Assign
708 New_Indices := New_List;
710 New_Indices := New_Copy_List_Tree (Indices);
713 Append_To (New_Indices, Ind);
715 if Present (Flist) then
716 F := New_Copy_Tree (Flist);
718 elsif Present (Etype (N)) and then Controlled_Type (Etype (N)) then
719 if Is_Entity_Name (Into)
720 and then Present (Scope (Entity (Into)))
722 F := Find_Final_List (Scope (Entity (Into)));
724 F := Find_Final_List (Current_Scope);
730 if Present (Next_Index (Index)) then
733 Build_Array_Aggr_Code
736 Index => Next_Index (Index),
738 Scalar_Comp => Scalar_Comp,
739 Indices => New_Indices,
743 -- If we get here then we are at a bottom-level (sub-)aggregate
747 (Make_Indexed_Component (Loc,
748 Prefix => New_Copy_Tree (Into),
749 Expressions => New_Indices));
751 Set_Assignment_OK (Indexed_Comp);
753 -- Ada 2005 (AI-287): In case of default initialized component, Expr
754 -- is not present (and therefore we also initialize Expr_Q to empty).
756 if not Present (Expr) then
758 elsif Nkind (Expr) = N_Qualified_Expression then
759 Expr_Q := Expression (Expr);
764 if Present (Etype (N))
765 and then Etype (N) /= Any_Composite
767 Comp_Type := Component_Type (Etype (N));
768 pragma Assert (Comp_Type = Ctype); -- AI-287
770 elsif Present (Next (First (New_Indices))) then
772 -- Ada 2005 (AI-287): Do nothing in case of default initialized
773 -- component because we have received the component type in
774 -- the formal parameter Ctype.
776 -- ??? Some assert pragmas have been added to check if this new
777 -- formal can be used to replace this code in all cases.
779 if Present (Expr) then
781 -- This is a multidimensional array. Recover the component
782 -- type from the outermost aggregate, because subaggregates
783 -- do not have an assigned type.
786 P : Node_Id := Parent (Expr);
789 while Present (P) loop
790 if Nkind (P) = N_Aggregate
791 and then Present (Etype (P))
793 Comp_Type := Component_Type (Etype (P));
801 pragma Assert (Comp_Type = Ctype); -- AI-287
806 -- Ada 2005 (AI-287): We only analyze the expression in case of non-
807 -- default initialized components (otherwise Expr_Q is not present).
810 and then (Nkind (Expr_Q) = N_Aggregate
811 or else Nkind (Expr_Q) = N_Extension_Aggregate)
813 -- At this stage the Expression may not have been
814 -- analyzed yet because the array aggregate code has not
815 -- been updated to use the Expansion_Delayed flag and
816 -- avoid analysis altogether to solve the same problem
817 -- (see Resolve_Aggr_Expr). So let us do the analysis of
818 -- non-array aggregates now in order to get the value of
819 -- Expansion_Delayed flag for the inner aggregate ???
821 if Present (Comp_Type) and then not Is_Array_Type (Comp_Type) then
822 Analyze_And_Resolve (Expr_Q, Comp_Type);
825 if Is_Delayed_Aggregate (Expr_Q) then
828 Late_Expansion (Expr_Q, Etype (Expr_Q), Indexed_Comp, F));
832 -- Ada 2005 (AI-287): In case of default initialized component, call
833 -- the initialization subprogram associated with the component type.
835 if not Present (Expr) then
837 if Present (Base_Init_Proc (Etype (Ctype)))
838 or else Has_Task (Base_Type (Ctype))
841 Build_Initialization_Call (Loc,
842 Id_Ref => Indexed_Comp,
844 With_Default_Init => True));
848 -- Now generate the assignment with no associated controlled
849 -- actions since the target of the assignment may not have
850 -- been initialized, it is not possible to Finalize it as
851 -- expected by normal controlled assignment. The rest of the
852 -- controlled actions are done manually with the proper
853 -- finalization list coming from the context.
856 Make_OK_Assignment_Statement (Loc,
857 Name => Indexed_Comp,
858 Expression => New_Copy_Tree (Expr));
860 if Present (Comp_Type) and then Controlled_Type (Comp_Type) then
861 Set_No_Ctrl_Actions (A);
866 -- Adjust the tag if tagged (because of possible view
867 -- conversions), unless compiling for the Java VM
868 -- where tags are implicit.
870 if Present (Comp_Type)
871 and then Is_Tagged_Type (Comp_Type)
875 Make_OK_Assignment_Statement (Loc,
877 Make_Selected_Component (Loc,
878 Prefix => New_Copy_Tree (Indexed_Comp),
880 New_Reference_To (Tag_Component (Comp_Type), Loc)),
883 Unchecked_Convert_To (RTE (RE_Tag),
885 Access_Disp_Table (Comp_Type), Loc)));
890 -- Adjust and Attach the component to the proper final list
891 -- which can be the controller of the outer record object or
892 -- the final list associated with the scope
894 if Present (Comp_Type) and then Controlled_Type (Comp_Type) then
897 Ref => New_Copy_Tree (Indexed_Comp),
900 With_Attach => Make_Integer_Literal (Loc, 1)));
904 return Add_Loop_Actions (L);
911 function Gen_Loop (L, H : Node_Id; Expr : Node_Id) return List_Id is
915 -- Index_Base'(L) .. Index_Base'(H)
917 L_Iteration_Scheme : Node_Id;
918 -- L_J in Index_Base'(L) .. Index_Base'(H)
921 -- The statements to execute in the loop
923 S : constant List_Id := New_List;
924 -- List of statements
927 -- Copy of expression tree, used for checking purposes
930 -- If loop bounds define an empty range return the null statement
932 if Empty_Range (L, H) then
933 Append_To (S, Make_Null_Statement (Loc));
935 -- Ada 2005 (AI-287): Nothing else need to be done in case of
936 -- default initialized component.
938 if not Present (Expr) then
942 -- The expression must be type-checked even though no component
943 -- of the aggregate will have this value. This is done only for
944 -- actual components of the array, not for subaggregates. Do
945 -- the check on a copy, because the expression may be shared
946 -- among several choices, some of which might be non-null.
948 if Present (Etype (N))
949 and then Is_Array_Type (Etype (N))
950 and then No (Next_Index (Index))
952 Expander_Mode_Save_And_Set (False);
953 Tcopy := New_Copy_Tree (Expr);
954 Set_Parent (Tcopy, N);
955 Analyze_And_Resolve (Tcopy, Component_Type (Etype (N)));
956 Expander_Mode_Restore;
962 -- If loop bounds are the same then generate an assignment
964 elsif Equal (L, H) then
965 return Gen_Assign (New_Copy_Tree (L), Expr);
967 -- If H - L <= 2 then generate a sequence of assignments
968 -- when we are processing the bottom most aggregate and it contains
969 -- scalar components.
971 elsif No (Next_Index (Index))
973 and then Local_Compile_Time_Known_Value (L)
974 and then Local_Compile_Time_Known_Value (H)
975 and then Local_Expr_Value (H) - Local_Expr_Value (L) <= 2
978 Append_List_To (S, Gen_Assign (New_Copy_Tree (L), Expr));
979 Append_List_To (S, Gen_Assign (Add (1, To => L), Expr));
981 if Local_Expr_Value (H) - Local_Expr_Value (L) = 2 then
982 Append_List_To (S, Gen_Assign (Add (2, To => L), Expr));
988 -- Otherwise construct the loop, starting with the loop index L_J
990 L_J := Make_Defining_Identifier (Loc, New_Internal_Name ('J'));
992 -- Construct "L .. H"
997 Low_Bound => Make_Qualified_Expression
999 Subtype_Mark => Index_Base_Name,
1001 High_Bound => Make_Qualified_Expression
1003 Subtype_Mark => Index_Base_Name,
1006 -- Construct "for L_J in Index_Base range L .. H"
1008 L_Iteration_Scheme :=
1009 Make_Iteration_Scheme
1011 Loop_Parameter_Specification =>
1012 Make_Loop_Parameter_Specification
1014 Defining_Identifier => L_J,
1015 Discrete_Subtype_Definition => L_Range));
1017 -- Construct the statements to execute in the loop body
1019 L_Body := Gen_Assign (New_Reference_To (L_J, Loc), Expr);
1021 -- Construct the final loop
1023 Append_To (S, Make_Implicit_Loop_Statement
1025 Identifier => Empty,
1026 Iteration_Scheme => L_Iteration_Scheme,
1027 Statements => L_Body));
1036 -- The code built is
1038 -- W_J : Index_Base := L;
1039 -- while W_J < H loop
1040 -- W_J := Index_Base'Succ (W);
1044 function Gen_While (L, H : Node_Id; Expr : Node_Id) return List_Id is
1048 -- W_J : Base_Type := L;
1050 W_Iteration_Scheme : Node_Id;
1053 W_Index_Succ : Node_Id;
1054 -- Index_Base'Succ (J)
1056 W_Increment : Node_Id;
1057 -- W_J := Index_Base'Succ (W)
1059 W_Body : constant List_Id := New_List;
1060 -- The statements to execute in the loop
1062 S : constant List_Id := New_List;
1063 -- list of statement
1066 -- If loop bounds define an empty range or are equal return null
1068 if Empty_Range (L, H) or else Equal (L, H) then
1069 Append_To (S, Make_Null_Statement (Loc));
1073 -- Build the decl of W_J
1075 W_J := Make_Defining_Identifier (Loc, New_Internal_Name ('J'));
1077 Make_Object_Declaration
1079 Defining_Identifier => W_J,
1080 Object_Definition => Index_Base_Name,
1083 -- Theoretically we should do a New_Copy_Tree (L) here, but we know
1084 -- that in this particular case L is a fresh Expr generated by
1085 -- Add which we are the only ones to use.
1087 Append_To (S, W_Decl);
1089 -- Construct " while W_J < H"
1091 W_Iteration_Scheme :=
1092 Make_Iteration_Scheme
1094 Condition => Make_Op_Lt
1096 Left_Opnd => New_Reference_To (W_J, Loc),
1097 Right_Opnd => New_Copy_Tree (H)));
1099 -- Construct the statements to execute in the loop body
1102 Make_Attribute_Reference
1104 Prefix => Index_Base_Name,
1105 Attribute_Name => Name_Succ,
1106 Expressions => New_List (New_Reference_To (W_J, Loc)));
1109 Make_OK_Assignment_Statement
1111 Name => New_Reference_To (W_J, Loc),
1112 Expression => W_Index_Succ);
1114 Append_To (W_Body, W_Increment);
1115 Append_List_To (W_Body,
1116 Gen_Assign (New_Reference_To (W_J, Loc), Expr));
1118 -- Construct the final loop
1120 Append_To (S, Make_Implicit_Loop_Statement
1122 Identifier => Empty,
1123 Iteration_Scheme => W_Iteration_Scheme,
1124 Statements => W_Body));
1129 ---------------------
1130 -- Index_Base_Name --
1131 ---------------------
1133 function Index_Base_Name return Node_Id is
1135 return New_Reference_To (Index_Base, Sloc (N));
1136 end Index_Base_Name;
1138 ------------------------------------
1139 -- Local_Compile_Time_Known_Value --
1140 ------------------------------------
1142 function Local_Compile_Time_Known_Value (E : Node_Id) return Boolean is
1144 return Compile_Time_Known_Value (E)
1146 (Nkind (E) = N_Attribute_Reference
1147 and then Attribute_Name (E) = Name_Val
1148 and then Compile_Time_Known_Value (First (Expressions (E))));
1149 end Local_Compile_Time_Known_Value;
1151 ----------------------
1152 -- Local_Expr_Value --
1153 ----------------------
1155 function Local_Expr_Value (E : Node_Id) return Uint is
1157 if Compile_Time_Known_Value (E) then
1158 return Expr_Value (E);
1160 return Expr_Value (First (Expressions (E)));
1162 end Local_Expr_Value;
1164 -- Build_Array_Aggr_Code Variables
1171 Others_Expr : Node_Id := Empty;
1172 Others_Mbox_Present : Boolean := False;
1174 Aggr_L : constant Node_Id := Low_Bound (Aggregate_Bounds (N));
1175 Aggr_H : constant Node_Id := High_Bound (Aggregate_Bounds (N));
1176 -- The aggregate bounds of this specific sub-aggregate. Note that if
1177 -- the code generated by Build_Array_Aggr_Code is executed then these
1178 -- bounds are OK. Otherwise a Constraint_Error would have been raised.
1180 Aggr_Low : constant Node_Id := Duplicate_Subexpr_No_Checks (Aggr_L);
1181 Aggr_High : constant Node_Id := Duplicate_Subexpr_No_Checks (Aggr_H);
1182 -- After Duplicate_Subexpr these are side-effect free
1187 Nb_Choices : Nat := 0;
1188 Table : Case_Table_Type (1 .. Number_Of_Choices (N));
1189 -- Used to sort all the different choice values
1192 -- Number of elements in the positional aggregate
1194 New_Code : constant List_Id := New_List;
1196 -- Start of processing for Build_Array_Aggr_Code
1199 -- First before we start, a special case. if we have a bit packed
1200 -- array represented as a modular type, then clear the value to
1201 -- zero first, to ensure that unused bits are properly cleared.
1206 and then Is_Bit_Packed_Array (Typ)
1207 and then Is_Modular_Integer_Type (Packed_Array_Type (Typ))
1209 Append_To (New_Code,
1210 Make_Assignment_Statement (Loc,
1211 Name => New_Copy_Tree (Into),
1213 Unchecked_Convert_To (Typ,
1214 Make_Integer_Literal (Loc, Uint_0))));
1218 -- STEP 1: Process component associations
1219 -- For those associations that may generate a loop, initialize
1220 -- Loop_Actions to collect inserted actions that may be crated.
1222 if No (Expressions (N)) then
1224 -- STEP 1 (a): Sort the discrete choices
1226 Assoc := First (Component_Associations (N));
1227 while Present (Assoc) loop
1228 Choice := First (Choices (Assoc));
1229 while Present (Choice) loop
1230 if Nkind (Choice) = N_Others_Choice then
1231 Set_Loop_Actions (Assoc, New_List);
1233 if Box_Present (Assoc) then
1234 Others_Mbox_Present := True;
1236 Others_Expr := Expression (Assoc);
1241 Get_Index_Bounds (Choice, Low, High);
1244 Set_Loop_Actions (Assoc, New_List);
1247 Nb_Choices := Nb_Choices + 1;
1248 if Box_Present (Assoc) then
1249 Table (Nb_Choices) := (Choice_Lo => Low,
1251 Choice_Node => Empty);
1253 Table (Nb_Choices) := (Choice_Lo => Low,
1255 Choice_Node => Expression (Assoc));
1263 -- If there is more than one set of choices these must be static
1264 -- and we can therefore sort them. Remember that Nb_Choices does not
1265 -- account for an others choice.
1267 if Nb_Choices > 1 then
1268 Sort_Case_Table (Table);
1271 -- STEP 1 (b): take care of the whole set of discrete choices.
1273 for J in 1 .. Nb_Choices loop
1274 Low := Table (J).Choice_Lo;
1275 High := Table (J).Choice_Hi;
1276 Expr := Table (J).Choice_Node;
1277 Append_List (Gen_Loop (Low, High, Expr), To => New_Code);
1280 -- STEP 1 (c): generate the remaining loops to cover others choice
1281 -- We don't need to generate loops over empty gaps, but if there is
1282 -- a single empty range we must analyze the expression for semantics
1284 if Present (Others_Expr) or else Others_Mbox_Present then
1286 First : Boolean := True;
1289 for J in 0 .. Nb_Choices loop
1293 Low := Add (1, To => Table (J).Choice_Hi);
1296 if J = Nb_Choices then
1299 High := Add (-1, To => Table (J + 1).Choice_Lo);
1302 -- If this is an expansion within an init proc, make
1303 -- sure that discriminant references are replaced by
1304 -- the corresponding discriminal.
1306 if Inside_Init_Proc then
1307 if Is_Entity_Name (Low)
1308 and then Ekind (Entity (Low)) = E_Discriminant
1310 Set_Entity (Low, Discriminal (Entity (Low)));
1313 if Is_Entity_Name (High)
1314 and then Ekind (Entity (High)) = E_Discriminant
1316 Set_Entity (High, Discriminal (Entity (High)));
1321 or else not Empty_Range (Low, High)
1325 (Gen_Loop (Low, High, Others_Expr), To => New_Code);
1331 -- STEP 2: Process positional components
1334 -- STEP 2 (a): Generate the assignments for each positional element
1335 -- Note that here we have to use Aggr_L rather than Aggr_Low because
1336 -- Aggr_L is analyzed and Add wants an analyzed expression.
1338 Expr := First (Expressions (N));
1341 while Present (Expr) loop
1342 Nb_Elements := Nb_Elements + 1;
1343 Append_List (Gen_Assign (Add (Nb_Elements, To => Aggr_L), Expr),
1348 -- STEP 2 (b): Generate final loop if an others choice is present
1349 -- Here Nb_Elements gives the offset of the last positional element.
1351 if Present (Component_Associations (N)) then
1352 Assoc := Last (Component_Associations (N));
1354 -- Ada 2005 (AI-287)
1356 if Box_Present (Assoc) then
1357 Append_List (Gen_While (Add (Nb_Elements, To => Aggr_L),
1362 Expr := Expression (Assoc);
1364 Append_List (Gen_While (Add (Nb_Elements, To => Aggr_L),
1373 end Build_Array_Aggr_Code;
1375 ----------------------------
1376 -- Build_Record_Aggr_Code --
1377 ----------------------------
1379 function Build_Record_Aggr_Code
1383 Flist : Node_Id := Empty;
1384 Obj : Entity_Id := Empty;
1385 Is_Limited_Ancestor_Expansion : Boolean := False) return List_Id
1387 Loc : constant Source_Ptr := Sloc (N);
1388 L : constant List_Id := New_List;
1389 Start_L : constant List_Id := New_List;
1390 N_Typ : constant Entity_Id := Etype (N);
1396 Comp_Type : Entity_Id;
1397 Selector : Entity_Id;
1398 Comp_Expr : Node_Id;
1401 Internal_Final_List : Node_Id;
1403 -- If this is an internal aggregate, the External_Final_List is an
1404 -- expression for the controller record of the enclosing type.
1405 -- If the current aggregate has several controlled components, this
1406 -- expression will appear in several calls to attach to the finali-
1407 -- zation list, and it must not be shared.
1409 External_Final_List : Node_Id;
1410 Ancestor_Is_Expression : Boolean := False;
1411 Ancestor_Is_Subtype_Mark : Boolean := False;
1413 Init_Typ : Entity_Id := Empty;
1416 function Get_Constraint_Association (T : Entity_Id) return Node_Id;
1417 -- Returns the first discriminant association in the constraint
1418 -- associated with T, if any, otherwise returns Empty.
1420 function Ancestor_Discriminant_Value (Disc : Entity_Id) return Node_Id;
1421 -- Returns the value that the given discriminant of an ancestor
1422 -- type should receive (in the absence of a conflict with the
1423 -- value provided by an ancestor part of an extension aggregate).
1425 procedure Check_Ancestor_Discriminants (Anc_Typ : Entity_Id);
1426 -- Check that each of the discriminant values defined by the
1427 -- ancestor part of an extension aggregate match the corresponding
1428 -- values provided by either an association of the aggregate or
1429 -- by the constraint imposed by a parent type (RM95-4.3.2(8)).
1431 function Init_Controller
1436 Init_Pr : Boolean) return List_Id;
1437 -- returns the list of statements necessary to initialize the internal
1438 -- controller of the (possible) ancestor typ into target and attach
1439 -- it to finalization list F. Init_Pr conditions the call to the
1440 -- init proc since it may already be done due to ancestor initialization
1442 ---------------------------------
1443 -- Ancestor_Discriminant_Value --
1444 ---------------------------------
1446 function Ancestor_Discriminant_Value (Disc : Entity_Id) return Node_Id is
1448 Assoc_Elmt : Elmt_Id;
1449 Aggr_Comp : Entity_Id;
1450 Corresp_Disc : Entity_Id;
1451 Current_Typ : Entity_Id := Base_Type (Typ);
1452 Parent_Typ : Entity_Id;
1453 Parent_Disc : Entity_Id;
1454 Save_Assoc : Node_Id := Empty;
1457 -- First check any discriminant associations to see if
1458 -- any of them provide a value for the discriminant.
1460 if Present (Discriminant_Specifications (Parent (Current_Typ))) then
1461 Assoc := First (Component_Associations (N));
1462 while Present (Assoc) loop
1463 Aggr_Comp := Entity (First (Choices (Assoc)));
1465 if Ekind (Aggr_Comp) = E_Discriminant then
1466 Save_Assoc := Expression (Assoc);
1468 Corresp_Disc := Corresponding_Discriminant (Aggr_Comp);
1469 while Present (Corresp_Disc) loop
1470 -- If found a corresponding discriminant then return
1471 -- the value given in the aggregate. (Note: this is
1472 -- not correct in the presence of side effects. ???)
1474 if Disc = Corresp_Disc then
1475 return Duplicate_Subexpr (Expression (Assoc));
1479 Corresponding_Discriminant (Corresp_Disc);
1487 -- No match found in aggregate, so chain up parent types to find
1488 -- a constraint that defines the value of the discriminant.
1490 Parent_Typ := Etype (Current_Typ);
1491 while Current_Typ /= Parent_Typ loop
1492 if Has_Discriminants (Parent_Typ) then
1493 Parent_Disc := First_Discriminant (Parent_Typ);
1495 -- We either get the association from the subtype indication
1496 -- of the type definition itself, or from the discriminant
1497 -- constraint associated with the type entity (which is
1498 -- preferable, but it's not always present ???)
1500 if Is_Empty_Elmt_List (
1501 Discriminant_Constraint (Current_Typ))
1503 Assoc := Get_Constraint_Association (Current_Typ);
1504 Assoc_Elmt := No_Elmt;
1507 First_Elmt (Discriminant_Constraint (Current_Typ));
1508 Assoc := Node (Assoc_Elmt);
1511 -- Traverse the discriminants of the parent type looking
1512 -- for one that corresponds.
1514 while Present (Parent_Disc) and then Present (Assoc) loop
1515 Corresp_Disc := Parent_Disc;
1516 while Present (Corresp_Disc)
1517 and then Disc /= Corresp_Disc
1520 Corresponding_Discriminant (Corresp_Disc);
1523 if Disc = Corresp_Disc then
1524 if Nkind (Assoc) = N_Discriminant_Association then
1525 Assoc := Expression (Assoc);
1528 -- If the located association directly denotes
1529 -- a discriminant, then use the value of a saved
1530 -- association of the aggregate. This is a kludge
1531 -- to handle certain cases involving multiple
1532 -- discriminants mapped to a single discriminant
1533 -- of a descendant. It's not clear how to locate the
1534 -- appropriate discriminant value for such cases. ???
1536 if Is_Entity_Name (Assoc)
1537 and then Ekind (Entity (Assoc)) = E_Discriminant
1539 Assoc := Save_Assoc;
1542 return Duplicate_Subexpr (Assoc);
1545 Next_Discriminant (Parent_Disc);
1547 if No (Assoc_Elmt) then
1550 Next_Elmt (Assoc_Elmt);
1551 if Present (Assoc_Elmt) then
1552 Assoc := Node (Assoc_Elmt);
1560 Current_Typ := Parent_Typ;
1561 Parent_Typ := Etype (Current_Typ);
1564 -- In some cases there's no ancestor value to locate (such as
1565 -- when an ancestor part given by an expression defines the
1566 -- discriminant value).
1569 end Ancestor_Discriminant_Value;
1571 ----------------------------------
1572 -- Check_Ancestor_Discriminants --
1573 ----------------------------------
1575 procedure Check_Ancestor_Discriminants (Anc_Typ : Entity_Id) is
1576 Discr : Entity_Id := First_Discriminant (Base_Type (Anc_Typ));
1577 Disc_Value : Node_Id;
1581 while Present (Discr) loop
1582 Disc_Value := Ancestor_Discriminant_Value (Discr);
1584 if Present (Disc_Value) then
1585 Cond := Make_Op_Ne (Loc,
1587 Make_Selected_Component (Loc,
1588 Prefix => New_Copy_Tree (Target),
1589 Selector_Name => New_Occurrence_Of (Discr, Loc)),
1590 Right_Opnd => Disc_Value);
1593 Make_Raise_Constraint_Error (Loc,
1595 Reason => CE_Discriminant_Check_Failed));
1598 Next_Discriminant (Discr);
1600 end Check_Ancestor_Discriminants;
1602 --------------------------------
1603 -- Get_Constraint_Association --
1604 --------------------------------
1606 function Get_Constraint_Association (T : Entity_Id) return Node_Id is
1607 Typ_Def : constant Node_Id := Type_Definition (Parent (T));
1608 Indic : constant Node_Id := Subtype_Indication (Typ_Def);
1611 -- ??? Also need to cover case of a type mark denoting a subtype
1614 if Nkind (Indic) = N_Subtype_Indication
1615 and then Present (Constraint (Indic))
1617 return First (Constraints (Constraint (Indic)));
1621 end Get_Constraint_Association;
1623 ---------------------
1624 -- Init_controller --
1625 ---------------------
1627 function Init_Controller
1632 Init_Pr : Boolean) return List_Id
1634 L : constant List_Id := New_List;
1639 -- init-proc (target._controller);
1640 -- initialize (target._controller);
1641 -- Attach_to_Final_List (target._controller, F);
1644 Make_Selected_Component (Loc,
1645 Prefix => Convert_To (Typ, New_Copy_Tree (Target)),
1646 Selector_Name => Make_Identifier (Loc, Name_uController));
1647 Set_Assignment_OK (Ref);
1649 -- Ada 2005 (AI-287): Give support to default initialization of
1650 -- limited types and components.
1652 if (Nkind (Target) = N_Identifier
1653 and then Present (Etype (Target))
1654 and then Is_Limited_Type (Etype (Target)))
1656 (Nkind (Target) = N_Selected_Component
1657 and then Present (Etype (Selector_Name (Target)))
1658 and then Is_Limited_Type (Etype (Selector_Name (Target))))
1660 (Nkind (Target) = N_Unchecked_Type_Conversion
1661 and then Present (Etype (Target))
1662 and then Is_Limited_Type (Etype (Target)))
1664 (Nkind (Target) = N_Unchecked_Expression
1665 and then Nkind (Expression (Target)) = N_Indexed_Component
1666 and then Present (Etype (Prefix (Expression (Target))))
1667 and then Is_Limited_Type (Etype (Prefix (Expression (Target)))))
1671 Build_Initialization_Call (Loc,
1673 Typ => RTE (RE_Limited_Record_Controller),
1674 In_Init_Proc => Within_Init_Proc));
1678 Make_Procedure_Call_Statement (Loc,
1681 (Find_Prim_Op (RTE (RE_Limited_Record_Controller),
1682 Name_Initialize), Loc),
1683 Parameter_Associations => New_List (New_Copy_Tree (Ref))));
1688 Build_Initialization_Call (Loc,
1690 Typ => RTE (RE_Record_Controller),
1691 In_Init_Proc => Within_Init_Proc));
1695 Make_Procedure_Call_Statement (Loc,
1697 New_Reference_To (Find_Prim_Op (RTE (RE_Record_Controller),
1698 Name_Initialize), Loc),
1699 Parameter_Associations => New_List (New_Copy_Tree (Ref))));
1705 Obj_Ref => New_Copy_Tree (Ref),
1707 With_Attach => Attach));
1709 end Init_Controller;
1711 -- Start of processing for Build_Record_Aggr_Code
1714 -- Deal with the ancestor part of extension aggregates
1715 -- or with the discriminants of the root type
1717 if Nkind (N) = N_Extension_Aggregate then
1719 A : constant Node_Id := Ancestor_Part (N);
1722 -- If the ancestor part is a subtype mark "T", we generate
1724 -- init-proc (T(tmp)); if T is constrained and
1725 -- init-proc (S(tmp)); where S applies an appropriate
1726 -- constraint if T is unconstrained
1728 if Is_Entity_Name (A) and then Is_Type (Entity (A)) then
1729 Ancestor_Is_Subtype_Mark := True;
1731 if Is_Constrained (Entity (A)) then
1732 Init_Typ := Entity (A);
1734 -- For an ancestor part given by an unconstrained type
1735 -- mark, create a subtype constrained by appropriate
1736 -- corresponding discriminant values coming from either
1737 -- associations of the aggregate or a constraint on
1738 -- a parent type. The subtype will be used to generate
1739 -- the correct default value for the ancestor part.
1741 elsif Has_Discriminants (Entity (A)) then
1743 Anc_Typ : constant Entity_Id := Entity (A);
1744 Anc_Constr : constant List_Id := New_List;
1745 Discrim : Entity_Id;
1746 Disc_Value : Node_Id;
1747 New_Indic : Node_Id;
1748 Subt_Decl : Node_Id;
1751 Discrim := First_Discriminant (Anc_Typ);
1752 while Present (Discrim) loop
1753 Disc_Value := Ancestor_Discriminant_Value (Discrim);
1754 Append_To (Anc_Constr, Disc_Value);
1755 Next_Discriminant (Discrim);
1759 Make_Subtype_Indication (Loc,
1760 Subtype_Mark => New_Occurrence_Of (Anc_Typ, Loc),
1762 Make_Index_Or_Discriminant_Constraint (Loc,
1763 Constraints => Anc_Constr));
1765 Init_Typ := Create_Itype (Ekind (Anc_Typ), N);
1768 Make_Subtype_Declaration (Loc,
1769 Defining_Identifier => Init_Typ,
1770 Subtype_Indication => New_Indic);
1772 -- Itypes must be analyzed with checks off
1773 -- Declaration must have a parent for proper
1774 -- handling of subsidiary actions.
1776 Set_Parent (Subt_Decl, N);
1777 Analyze (Subt_Decl, Suppress => All_Checks);
1781 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
1782 Set_Assignment_OK (Ref);
1784 if Has_Default_Init_Comps (N)
1785 or else Has_Task (Base_Type (Init_Typ))
1787 Append_List_To (Start_L,
1788 Build_Initialization_Call (Loc,
1791 In_Init_Proc => Within_Init_Proc,
1792 With_Default_Init => True));
1794 Append_List_To (Start_L,
1795 Build_Initialization_Call (Loc,
1798 In_Init_Proc => Within_Init_Proc));
1801 if Is_Constrained (Entity (A))
1802 and then Has_Discriminants (Entity (A))
1804 Check_Ancestor_Discriminants (Entity (A));
1807 -- Ada 2005 (AI-287): If the ancestor part is a limited type,
1808 -- a recursive call expands the ancestor.
1810 elsif Is_Limited_Type (Etype (A)) then
1811 Ancestor_Is_Expression := True;
1813 Append_List_To (Start_L,
1814 Build_Record_Aggr_Code (
1815 N => Expression (A),
1816 Typ => Etype (Expression (A)),
1820 Is_Limited_Ancestor_Expansion => True));
1822 -- If the ancestor part is an expression "E", we generate
1826 Ancestor_Is_Expression := True;
1827 Init_Typ := Etype (A);
1829 -- Assign the tag before doing the assignment to make sure
1830 -- that the dispatching call in the subsequent deep_adjust
1831 -- works properly (unless Java_VM, where tags are implicit).
1835 Make_OK_Assignment_Statement (Loc,
1837 Make_Selected_Component (Loc,
1838 Prefix => New_Copy_Tree (Target),
1839 Selector_Name => New_Reference_To (
1840 Tag_Component (Base_Type (Typ)), Loc)),
1843 Unchecked_Convert_To (RTE (RE_Tag),
1845 Access_Disp_Table (Base_Type (Typ)), Loc)));
1847 Set_Assignment_OK (Name (Instr));
1848 Append_To (L, Instr);
1851 -- If the ancestor part is an aggregate, force its full
1852 -- expansion, which was delayed.
1854 if Nkind (A) = N_Qualified_Expression
1855 and then (Nkind (Expression (A)) = N_Aggregate
1857 Nkind (Expression (A)) = N_Extension_Aggregate)
1859 Set_Analyzed (A, False);
1860 Set_Analyzed (Expression (A), False);
1863 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
1864 Set_Assignment_OK (Ref);
1866 Make_Unsuppress_Block (Loc,
1867 Name_Discriminant_Check,
1869 Make_OK_Assignment_Statement (Loc,
1871 Expression => A))));
1873 if Has_Discriminants (Init_Typ) then
1874 Check_Ancestor_Discriminants (Init_Typ);
1879 -- Normal case (not an extension aggregate)
1882 -- Generate the discriminant expressions, component by component.
1883 -- If the base type is an unchecked union, the discriminants are
1884 -- unknown to the back-end and absent from a value of the type, so
1885 -- assignments for them are not emitted.
1887 if Has_Discriminants (Typ)
1888 and then not Is_Unchecked_Union (Base_Type (Typ))
1890 -- ??? The discriminants of the object not inherited in the type
1891 -- of the object should be initialized here
1895 -- Generate discriminant init values
1898 Discriminant : Entity_Id;
1899 Discriminant_Value : Node_Id;
1902 Discriminant := First_Stored_Discriminant (Typ);
1904 while Present (Discriminant) loop
1907 Make_Selected_Component (Loc,
1908 Prefix => New_Copy_Tree (Target),
1909 Selector_Name => New_Occurrence_Of (Discriminant, Loc));
1911 Discriminant_Value :=
1912 Get_Discriminant_Value (
1915 Discriminant_Constraint (N_Typ));
1918 Make_OK_Assignment_Statement (Loc,
1920 Expression => New_Copy_Tree (Discriminant_Value));
1922 Set_No_Ctrl_Actions (Instr);
1923 Append_To (L, Instr);
1925 Next_Stored_Discriminant (Discriminant);
1931 -- Generate the assignments, component by component
1933 -- tmp.comp1 := Expr1_From_Aggr;
1934 -- tmp.comp2 := Expr2_From_Aggr;
1937 Comp := First (Component_Associations (N));
1938 while Present (Comp) loop
1939 Selector := Entity (First (Choices (Comp)));
1941 -- Ada 2005 (AI-287): Default initialization of a limited component
1943 if Box_Present (Comp)
1944 and then Is_Limited_Type (Etype (Selector))
1946 -- Ada 2005 (AI-287): If the component type has tasks then
1947 -- generate the activation chain and master entities (except
1948 -- in case of an allocator because in that case these entities
1949 -- are generated by Build_Task_Allocate_Block_With_Init_Stmts).
1952 Ctype : constant Entity_Id := Etype (Selector);
1953 Inside_Allocator : Boolean := False;
1954 P : Node_Id := Parent (N);
1957 if Is_Task_Type (Ctype) or else Has_Task (Ctype) then
1958 while Present (P) loop
1959 if Nkind (P) = N_Allocator then
1960 Inside_Allocator := True;
1967 if not Inside_Init_Proc and not Inside_Allocator then
1968 Build_Activation_Chain_Entity (N);
1970 if not Has_Master_Entity (Current_Scope) then
1971 Build_Master_Entity (Etype (N));
1978 Build_Initialization_Call (Loc,
1979 Id_Ref => Make_Selected_Component (Loc,
1980 Prefix => New_Copy_Tree (Target),
1981 Selector_Name => New_Occurrence_Of (Selector,
1983 Typ => Etype (Selector),
1984 With_Default_Init => True));
1991 if Ekind (Selector) /= E_Discriminant
1992 or else Nkind (N) = N_Extension_Aggregate
1994 Comp_Type := Etype (Selector);
1996 Make_Selected_Component (Loc,
1997 Prefix => New_Copy_Tree (Target),
1998 Selector_Name => New_Occurrence_Of (Selector, Loc));
2000 if Nkind (Expression (Comp)) = N_Qualified_Expression then
2001 Expr_Q := Expression (Expression (Comp));
2003 Expr_Q := Expression (Comp);
2006 -- The controller is the one of the parent type defining
2007 -- the component (in case of inherited components).
2009 if Controlled_Type (Comp_Type) then
2010 Internal_Final_List :=
2011 Make_Selected_Component (Loc,
2012 Prefix => Convert_To (
2013 Scope (Original_Record_Component (Selector)),
2014 New_Copy_Tree (Target)),
2016 Make_Identifier (Loc, Name_uController));
2018 Internal_Final_List :=
2019 Make_Selected_Component (Loc,
2020 Prefix => Internal_Final_List,
2021 Selector_Name => Make_Identifier (Loc, Name_F));
2023 -- The internal final list can be part of a constant object
2025 Set_Assignment_OK (Internal_Final_List);
2028 Internal_Final_List := Empty;
2033 if Is_Delayed_Aggregate (Expr_Q) then
2035 Late_Expansion (Expr_Q, Comp_Type, Comp_Expr,
2036 Internal_Final_List));
2040 Make_OK_Assignment_Statement (Loc,
2042 Expression => Expression (Comp));
2044 Set_No_Ctrl_Actions (Instr);
2045 Append_To (L, Instr);
2047 -- Adjust the tag if tagged (because of possible view
2048 -- conversions), unless compiling for the Java VM
2049 -- where tags are implicit.
2051 -- tmp.comp._tag := comp_typ'tag;
2053 if Is_Tagged_Type (Comp_Type) and then not Java_VM then
2055 Make_OK_Assignment_Statement (Loc,
2057 Make_Selected_Component (Loc,
2058 Prefix => New_Copy_Tree (Comp_Expr),
2060 New_Reference_To (Tag_Component (Comp_Type), Loc)),
2063 Unchecked_Convert_To (RTE (RE_Tag),
2065 Access_Disp_Table (Comp_Type), Loc)));
2067 Append_To (L, Instr);
2070 -- Adjust and Attach the component to the proper controller
2071 -- Adjust (tmp.comp);
2072 -- Attach_To_Final_List (tmp.comp,
2073 -- comp_typ (tmp)._record_controller.f)
2075 if Controlled_Type (Comp_Type) then
2078 Ref => New_Copy_Tree (Comp_Expr),
2080 Flist_Ref => Internal_Final_List,
2081 With_Attach => Make_Integer_Literal (Loc, 1)));
2087 elsif Ekind (Selector) = E_Discriminant
2088 and then Nkind (N) /= N_Extension_Aggregate
2089 and then Nkind (Parent (N)) = N_Component_Association
2090 and then Is_Constrained (Typ)
2092 -- We must check that the discriminant value imposed by the
2093 -- context is the same as the value given in the subaggregate,
2094 -- because after the expansion into assignments there is no
2095 -- record on which to perform a regular discriminant check.
2102 D_Val := First_Elmt (Discriminant_Constraint (Typ));
2103 Disc := First_Discriminant (Typ);
2105 while Chars (Disc) /= Chars (Selector) loop
2106 Next_Discriminant (Disc);
2110 pragma Assert (Present (D_Val));
2113 Make_Raise_Constraint_Error (Loc,
2116 Left_Opnd => New_Copy_Tree (Node (D_Val)),
2117 Right_Opnd => Expression (Comp)),
2118 Reason => CE_Discriminant_Check_Failed));
2127 -- If the type is tagged, the tag needs to be initialized (unless
2128 -- compiling for the Java VM where tags are implicit). It is done
2129 -- late in the initialization process because in some cases, we call
2130 -- the init proc of an ancestor which will not leave out the right tag
2132 if Ancestor_Is_Expression then
2135 elsif Is_Tagged_Type (Typ) and then not Java_VM then
2137 Make_OK_Assignment_Statement (Loc,
2139 Make_Selected_Component (Loc,
2140 Prefix => New_Copy_Tree (Target),
2142 New_Reference_To (Tag_Component (Base_Type (Typ)), Loc)),
2145 Unchecked_Convert_To (RTE (RE_Tag),
2146 New_Reference_To (Access_Disp_Table (Base_Type (Typ)), Loc)));
2148 Append_To (L, Instr);
2151 -- Now deal with the various controlled type data structure
2155 and then Finalize_Storage_Only (Typ)
2156 and then (Is_Library_Level_Entity (Obj)
2157 or else Entity (Constant_Value (RTE (RE_Garbage_Collected)))
2160 Attach := Make_Integer_Literal (Loc, 0);
2162 elsif Nkind (Parent (N)) = N_Qualified_Expression
2163 and then Nkind (Parent (Parent (N))) = N_Allocator
2165 Attach := Make_Integer_Literal (Loc, 2);
2168 Attach := Make_Integer_Literal (Loc, 1);
2171 -- Determine the external finalization list. It is either the
2172 -- finalization list of the outer-scope or the one coming from
2173 -- an outer aggregate. When the target is not a temporary, the
2174 -- proper scope is the scope of the target rather than the
2175 -- potentially transient current scope.
2177 if Controlled_Type (Typ) then
2178 if Present (Flist) then
2179 External_Final_List := New_Copy_Tree (Flist);
2181 elsif Is_Entity_Name (Target)
2182 and then Present (Scope (Entity (Target)))
2184 External_Final_List := Find_Final_List (Scope (Entity (Target)));
2187 External_Final_List := Find_Final_List (Current_Scope);
2191 External_Final_List := Empty;
2194 -- Initialize and attach the outer object in the is_controlled case
2196 if Is_Controlled (Typ) then
2197 if Ancestor_Is_Subtype_Mark then
2198 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
2199 Set_Assignment_OK (Ref);
2201 Make_Procedure_Call_Statement (Loc,
2202 Name => New_Reference_To (
2203 Find_Prim_Op (Init_Typ, Name_Initialize), Loc),
2204 Parameter_Associations => New_List (New_Copy_Tree (Ref))));
2207 if not Has_Controlled_Component (Typ) then
2208 Ref := New_Copy_Tree (Target);
2209 Set_Assignment_OK (Ref);
2213 Flist_Ref => New_Copy_Tree (External_Final_List),
2214 With_Attach => Attach));
2218 -- In the Has_Controlled component case, all the intermediate
2219 -- controllers must be initialized
2221 if Has_Controlled_Component (Typ)
2222 and not Is_Limited_Ancestor_Expansion
2225 Inner_Typ : Entity_Id;
2226 Outer_Typ : Entity_Id;
2231 Outer_Typ := Base_Type (Typ);
2233 -- Find outer type with a controller
2235 while Outer_Typ /= Init_Typ
2236 and then not Has_New_Controlled_Component (Outer_Typ)
2238 Outer_Typ := Etype (Outer_Typ);
2241 -- Attach it to the outer record controller to the
2242 -- external final list
2244 if Outer_Typ = Init_Typ then
2245 Append_List_To (Start_L,
2249 F => External_Final_List,
2251 Init_Pr => Ancestor_Is_Expression));
2254 Inner_Typ := Init_Typ;
2257 Append_List_To (Start_L,
2261 F => External_Final_List,
2265 Inner_Typ := Etype (Outer_Typ);
2267 not Is_Tagged_Type (Typ) or else Inner_Typ = Outer_Typ;
2270 -- The outer object has to be attached as well
2272 if Is_Controlled (Typ) then
2273 Ref := New_Copy_Tree (Target);
2274 Set_Assignment_OK (Ref);
2278 Flist_Ref => New_Copy_Tree (External_Final_List),
2279 With_Attach => New_Copy_Tree (Attach)));
2282 -- Initialize the internal controllers for tagged types with
2283 -- more than one controller.
2285 while not At_Root and then Inner_Typ /= Init_Typ loop
2286 if Has_New_Controlled_Component (Inner_Typ) then
2288 Make_Selected_Component (Loc,
2289 Prefix => Convert_To (Outer_Typ, New_Copy_Tree (Target)),
2291 Make_Identifier (Loc, Name_uController));
2293 Make_Selected_Component (Loc,
2295 Selector_Name => Make_Identifier (Loc, Name_F));
2297 Append_List_To (Start_L,
2302 Attach => Make_Integer_Literal (Loc, 1),
2304 Outer_Typ := Inner_Typ;
2309 At_Root := Inner_Typ = Etype (Inner_Typ);
2310 Inner_Typ := Etype (Inner_Typ);
2313 -- If not done yet attach the controller of the ancestor part
2315 if Outer_Typ /= Init_Typ
2316 and then Inner_Typ = Init_Typ
2317 and then Has_Controlled_Component (Init_Typ)
2320 Make_Selected_Component (Loc,
2321 Prefix => Convert_To (Outer_Typ, New_Copy_Tree (Target)),
2322 Selector_Name => Make_Identifier (Loc, Name_uController));
2324 Make_Selected_Component (Loc,
2326 Selector_Name => Make_Identifier (Loc, Name_F));
2328 Attach := Make_Integer_Literal (Loc, 1);
2329 Append_List_To (Start_L,
2335 Init_Pr => Ancestor_Is_Expression));
2340 Append_List_To (Start_L, L);
2342 end Build_Record_Aggr_Code;
2344 -------------------------------
2345 -- Convert_Aggr_In_Allocator --
2346 -------------------------------
2348 procedure Convert_Aggr_In_Allocator (Decl, Aggr : Node_Id) is
2349 Loc : constant Source_Ptr := Sloc (Aggr);
2350 Typ : constant Entity_Id := Etype (Aggr);
2351 Temp : constant Entity_Id := Defining_Identifier (Decl);
2353 Occ : constant Node_Id :=
2354 Unchecked_Convert_To (Typ,
2355 Make_Explicit_Dereference (Loc,
2356 New_Reference_To (Temp, Loc)));
2358 Access_Type : constant Entity_Id := Etype (Temp);
2361 if Is_Array_Type (Typ) then
2362 Convert_Array_Aggr_In_Allocator (Decl, Aggr, Occ);
2364 elsif Has_Default_Init_Comps (Aggr) then
2366 L : constant List_Id := New_List;
2367 Init_Stmts : List_Id;
2370 Init_Stmts := Late_Expansion (Aggr, Typ, Occ,
2371 Find_Final_List (Access_Type),
2372 Associated_Final_Chain (Base_Type (Access_Type)));
2374 Build_Task_Allocate_Block_With_Init_Stmts (L, Aggr, Init_Stmts);
2375 Insert_Actions_After (Decl, L);
2379 Insert_Actions_After (Decl,
2380 Late_Expansion (Aggr, Typ, Occ,
2381 Find_Final_List (Access_Type),
2382 Associated_Final_Chain (Base_Type (Access_Type))));
2384 end Convert_Aggr_In_Allocator;
2386 --------------------------------
2387 -- Convert_Aggr_In_Assignment --
2388 --------------------------------
2390 procedure Convert_Aggr_In_Assignment (N : Node_Id) is
2391 Aggr : Node_Id := Expression (N);
2392 Typ : constant Entity_Id := Etype (Aggr);
2393 Occ : constant Node_Id := New_Copy_Tree (Name (N));
2396 if Nkind (Aggr) = N_Qualified_Expression then
2397 Aggr := Expression (Aggr);
2400 Insert_Actions_After (N,
2401 Late_Expansion (Aggr, Typ, Occ,
2402 Find_Final_List (Typ, New_Copy_Tree (Occ))));
2403 end Convert_Aggr_In_Assignment;
2405 ---------------------------------
2406 -- Convert_Aggr_In_Object_Decl --
2407 ---------------------------------
2409 procedure Convert_Aggr_In_Object_Decl (N : Node_Id) is
2410 Obj : constant Entity_Id := Defining_Identifier (N);
2411 Aggr : Node_Id := Expression (N);
2412 Loc : constant Source_Ptr := Sloc (Aggr);
2413 Typ : constant Entity_Id := Etype (Aggr);
2414 Occ : constant Node_Id := New_Occurrence_Of (Obj, Loc);
2416 function Discriminants_Ok return Boolean;
2417 -- If the object type is constrained, the discriminants in the
2418 -- aggregate must be checked against the discriminants of the subtype.
2419 -- This cannot be done using Apply_Discriminant_Checks because after
2420 -- expansion there is no aggregate left to check.
2422 ----------------------
2423 -- Discriminants_Ok --
2424 ----------------------
2426 function Discriminants_Ok return Boolean is
2427 Cond : Node_Id := Empty;
2436 D := First_Discriminant (Typ);
2437 Disc1 := First_Elmt (Discriminant_Constraint (Typ));
2438 Disc2 := First_Elmt (Discriminant_Constraint (Etype (Obj)));
2440 while Present (Disc1) and then Present (Disc2) loop
2441 Val1 := Node (Disc1);
2442 Val2 := Node (Disc2);
2444 if not Is_OK_Static_Expression (Val1)
2445 or else not Is_OK_Static_Expression (Val2)
2447 Check := Make_Op_Ne (Loc,
2448 Left_Opnd => Duplicate_Subexpr (Val1),
2449 Right_Opnd => Duplicate_Subexpr (Val2));
2455 Cond := Make_Or_Else (Loc,
2457 Right_Opnd => Check);
2460 elsif Expr_Value (Val1) /= Expr_Value (Val2) then
2461 Apply_Compile_Time_Constraint_Error (Aggr,
2462 Msg => "incorrect value for discriminant&?",
2463 Reason => CE_Discriminant_Check_Failed,
2468 Next_Discriminant (D);
2473 -- If any discriminant constraint is non-static, emit a check.
2475 if Present (Cond) then
2477 Make_Raise_Constraint_Error (Loc,
2479 Reason => CE_Discriminant_Check_Failed));
2483 end Discriminants_Ok;
2485 -- Start of processing for Convert_Aggr_In_Object_Decl
2488 Set_Assignment_OK (Occ);
2490 if Nkind (Aggr) = N_Qualified_Expression then
2491 Aggr := Expression (Aggr);
2494 if Has_Discriminants (Typ)
2495 and then Typ /= Etype (Obj)
2496 and then Is_Constrained (Etype (Obj))
2497 and then not Discriminants_Ok
2502 Insert_Actions_After (N, Late_Expansion (Aggr, Typ, Occ, Obj => Obj));
2503 Set_No_Initialization (N);
2504 Initialize_Discriminants (N, Typ);
2505 end Convert_Aggr_In_Object_Decl;
2507 -------------------------------------
2508 -- Convert_array_Aggr_In_Allocator --
2509 -------------------------------------
2511 procedure Convert_Array_Aggr_In_Allocator
2516 Aggr_Code : List_Id;
2517 Typ : constant Entity_Id := Etype (Aggr);
2518 Ctyp : constant Entity_Id := Component_Type (Typ);
2521 -- The target is an explicit dereference of the allocated object.
2522 -- Generate component assignments to it, as for an aggregate that
2523 -- appears on the right-hand side of an assignment statement.
2526 Build_Array_Aggr_Code (Aggr,
2528 Index => First_Index (Typ),
2530 Scalar_Comp => Is_Scalar_Type (Ctyp));
2532 Insert_Actions_After (Decl, Aggr_Code);
2533 end Convert_Array_Aggr_In_Allocator;
2535 ----------------------------
2536 -- Convert_To_Assignments --
2537 ----------------------------
2539 procedure Convert_To_Assignments (N : Node_Id; Typ : Entity_Id) is
2540 Loc : constant Source_Ptr := Sloc (N);
2544 Target_Expr : Node_Id;
2545 Parent_Kind : Node_Kind;
2546 Unc_Decl : Boolean := False;
2547 Parent_Node : Node_Id;
2550 Parent_Node := Parent (N);
2551 Parent_Kind := Nkind (Parent_Node);
2553 if Parent_Kind = N_Qualified_Expression then
2555 -- Check if we are in a unconstrained declaration because in this
2556 -- case the current delayed expansion mechanism doesn't work when
2557 -- the declared object size depend on the initializing expr.
2560 Parent_Node := Parent (Parent_Node);
2561 Parent_Kind := Nkind (Parent_Node);
2563 if Parent_Kind = N_Object_Declaration then
2565 not Is_Entity_Name (Object_Definition (Parent_Node))
2566 or else Has_Discriminants
2567 (Entity (Object_Definition (Parent_Node)))
2568 or else Is_Class_Wide_Type
2569 (Entity (Object_Definition (Parent_Node)));
2574 -- Just set the Delay flag in the following cases where the
2575 -- transformation will be done top down from above
2577 -- - internal aggregate (transformed when expanding the parent)
2578 -- - allocators (see Convert_Aggr_In_Allocator)
2579 -- - object decl (see Convert_Aggr_In_Object_Decl)
2580 -- - safe assignments (see Convert_Aggr_Assignments)
2581 -- so far only the assignments in the init procs are taken
2584 if Parent_Kind = N_Aggregate
2585 or else Parent_Kind = N_Extension_Aggregate
2586 or else Parent_Kind = N_Component_Association
2587 or else Parent_Kind = N_Allocator
2588 or else (Parent_Kind = N_Object_Declaration and then not Unc_Decl)
2589 or else (Parent_Kind = N_Assignment_Statement
2590 and then Inside_Init_Proc)
2592 Set_Expansion_Delayed (N);
2596 if Requires_Transient_Scope (Typ) then
2597 Establish_Transient_Scope (N, Sec_Stack =>
2598 Is_Controlled (Typ) or else Has_Controlled_Component (Typ));
2601 -- Create the temporary
2603 Temp := Make_Defining_Identifier (Loc, New_Internal_Name ('A'));
2606 Make_Object_Declaration (Loc,
2607 Defining_Identifier => Temp,
2608 Object_Definition => New_Occurrence_Of (Typ, Loc));
2610 Set_No_Initialization (Instr);
2611 Insert_Action (N, Instr);
2612 Initialize_Discriminants (Instr, Typ);
2613 Target_Expr := New_Occurrence_Of (Temp, Loc);
2615 Insert_Actions (N, Build_Record_Aggr_Code (N, Typ, Target_Expr));
2616 Rewrite (N, New_Occurrence_Of (Temp, Loc));
2617 Analyze_And_Resolve (N, Typ);
2618 end Convert_To_Assignments;
2620 ---------------------------
2621 -- Convert_To_Positional --
2622 ---------------------------
2624 procedure Convert_To_Positional
2626 Max_Others_Replicate : Nat := 5;
2627 Handle_Bit_Packed : Boolean := False)
2629 Typ : constant Entity_Id := Etype (N);
2634 Ixb : Node_Id) return Boolean;
2635 -- Convert the aggregate into a purely positional form if possible.
2637 function Is_Flat (N : Node_Id; Dims : Int) return Boolean;
2638 -- Non trivial for multidimensional aggregate.
2647 Ixb : Node_Id) return Boolean
2649 Loc : constant Source_Ptr := Sloc (N);
2650 Blo : constant Node_Id := Type_Low_Bound (Etype (Ixb));
2651 Lo : constant Node_Id := Type_Low_Bound (Etype (Ix));
2652 Hi : constant Node_Id := Type_High_Bound (Etype (Ix));
2656 -- The following constant determines the maximum size of an
2657 -- aggregate produced by converting named to positional
2658 -- notation (e.g. from others clauses). This avoids running
2659 -- away with attempts to convert huge aggregates.
2661 -- The normal limit is 5000, but we increase this limit to
2662 -- 2**24 (about 16 million) if Restrictions (No_Elaboration_Code)
2663 -- or Restrictions (No_Implicit_Loops) is specified, since in
2664 -- either case, we are at risk of declaring the program illegal
2665 -- because of this limit.
2667 Max_Aggr_Size : constant Nat :=
2668 5000 + (2 ** 24 - 5000) *
2670 (Restriction_Active (No_Elaboration_Code)
2672 Restriction_Active (No_Implicit_Loops));
2675 if Nkind (Original_Node (N)) = N_String_Literal then
2679 -- Bounds need to be known at compile time
2681 if not Compile_Time_Known_Value (Lo)
2682 or else not Compile_Time_Known_Value (Hi)
2687 -- Get bounds and check reasonable size (positive, not too large)
2688 -- Also only handle bounds starting at the base type low bound
2689 -- for now since the compiler isn't able to handle different low
2690 -- bounds yet. Case such as new String'(3..5 => ' ') will get
2691 -- the wrong bounds, though it seems that the aggregate should
2692 -- retain the bounds set on its Etype (see C64103E and CC1311B).
2694 Lov := Expr_Value (Lo);
2695 Hiv := Expr_Value (Hi);
2698 or else (Hiv - Lov > Max_Aggr_Size)
2699 or else not Compile_Time_Known_Value (Blo)
2700 or else (Lov /= Expr_Value (Blo))
2705 -- Bounds must be in integer range (for array Vals below)
2707 if not UI_Is_In_Int_Range (Lov)
2709 not UI_Is_In_Int_Range (Hiv)
2714 -- Determine if set of alternatives is suitable for conversion
2715 -- and build an array containing the values in sequence.
2718 Vals : array (UI_To_Int (Lov) .. UI_To_Int (Hiv))
2719 of Node_Id := (others => Empty);
2720 -- The values in the aggregate sorted appropriately
2723 -- Same data as Vals in list form
2726 -- Used to validate Max_Others_Replicate limit
2729 Num : Int := UI_To_Int (Lov);
2734 if Present (Expressions (N)) then
2735 Elmt := First (Expressions (N));
2737 while Present (Elmt) loop
2738 if Nkind (Elmt) = N_Aggregate
2739 and then Present (Next_Index (Ix))
2741 not Flatten (Elmt, Next_Index (Ix), Next_Index (Ixb))
2746 Vals (Num) := Relocate_Node (Elmt);
2753 if No (Component_Associations (N)) then
2757 Elmt := First (Component_Associations (N));
2759 if Nkind (Expression (Elmt)) = N_Aggregate then
2760 if Present (Next_Index (Ix))
2763 (Expression (Elmt), Next_Index (Ix), Next_Index (Ixb))
2769 Component_Loop : while Present (Elmt) loop
2770 Choice := First (Choices (Elmt));
2771 Choice_Loop : while Present (Choice) loop
2773 -- If we have an others choice, fill in the missing elements
2774 -- subject to the limit established by Max_Others_Replicate.
2776 if Nkind (Choice) = N_Others_Choice then
2779 for J in Vals'Range loop
2780 if No (Vals (J)) then
2781 Vals (J) := New_Copy_Tree (Expression (Elmt));
2782 Rep_Count := Rep_Count + 1;
2784 -- Check for maximum others replication. Note that
2785 -- we skip this test if either of the restrictions
2786 -- No_Elaboration_Code or No_Implicit_Loops is
2787 -- active, or if this is a preelaborable unit.
2790 P : constant Entity_Id :=
2791 Cunit_Entity (Current_Sem_Unit);
2794 if Restriction_Active (No_Elaboration_Code)
2795 or else Restriction_Active (No_Implicit_Loops)
2796 or else Is_Preelaborated (P)
2797 or else (Ekind (P) = E_Package_Body
2799 Is_Preelaborated (Spec_Entity (P)))
2803 elsif Rep_Count > Max_Others_Replicate then
2810 exit Component_Loop;
2812 -- Case of a subtype mark
2814 elsif Nkind (Choice) = N_Identifier
2815 and then Is_Type (Entity (Choice))
2817 Lo := Type_Low_Bound (Etype (Choice));
2818 Hi := Type_High_Bound (Etype (Choice));
2820 -- Case of subtype indication
2822 elsif Nkind (Choice) = N_Subtype_Indication then
2823 Lo := Low_Bound (Range_Expression (Constraint (Choice)));
2824 Hi := High_Bound (Range_Expression (Constraint (Choice)));
2828 elsif Nkind (Choice) = N_Range then
2829 Lo := Low_Bound (Choice);
2830 Hi := High_Bound (Choice);
2832 -- Normal subexpression case
2834 else pragma Assert (Nkind (Choice) in N_Subexpr);
2835 if not Compile_Time_Known_Value (Choice) then
2839 Vals (UI_To_Int (Expr_Value (Choice))) :=
2840 New_Copy_Tree (Expression (Elmt));
2845 -- Range cases merge with Lo,Hi said
2847 if not Compile_Time_Known_Value (Lo)
2849 not Compile_Time_Known_Value (Hi)
2853 for J in UI_To_Int (Expr_Value (Lo)) ..
2854 UI_To_Int (Expr_Value (Hi))
2856 Vals (J) := New_Copy_Tree (Expression (Elmt));
2862 end loop Choice_Loop;
2865 end loop Component_Loop;
2867 -- If we get here the conversion is possible
2870 for J in Vals'Range loop
2871 Append (Vals (J), Vlist);
2874 Rewrite (N, Make_Aggregate (Loc, Expressions => Vlist));
2875 Set_Aggregate_Bounds (N, Aggregate_Bounds (Original_Node (N)));
2884 function Is_Flat (N : Node_Id; Dims : Int) return Boolean is
2891 elsif Nkind (N) = N_Aggregate then
2892 if Present (Component_Associations (N)) then
2896 Elmt := First (Expressions (N));
2898 while Present (Elmt) loop
2899 if not Is_Flat (Elmt, Dims - 1) then
2913 -- Start of processing for Convert_To_Positional
2916 -- Ada 2005 (AI-287): Do not convert in case of default initialized
2917 -- components because in this case will need to call the corresponding
2920 if Has_Default_Init_Comps (N) then
2924 if Is_Flat (N, Number_Dimensions (Typ)) then
2928 if Is_Bit_Packed_Array (Typ)
2929 and then not Handle_Bit_Packed
2934 -- Do not convert to positional if controlled components are
2935 -- involved since these require special processing
2937 if Has_Controlled_Component (Typ) then
2941 if Flatten (N, First_Index (Typ), First_Index (Base_Type (Typ))) then
2942 Analyze_And_Resolve (N, Typ);
2944 end Convert_To_Positional;
2946 ----------------------------
2947 -- Expand_Array_Aggregate --
2948 ----------------------------
2950 -- Array aggregate expansion proceeds as follows:
2952 -- 1. If requested we generate code to perform all the array aggregate
2953 -- bound checks, specifically
2955 -- (a) Check that the index range defined by aggregate bounds is
2956 -- compatible with corresponding index subtype.
2958 -- (b) If an others choice is present check that no aggregate
2959 -- index is outside the bounds of the index constraint.
2961 -- (c) For multidimensional arrays make sure that all subaggregates
2962 -- corresponding to the same dimension have the same bounds.
2964 -- 2. Check for packed array aggregate which can be converted to a
2965 -- constant so that the aggregate disappeares completely.
2967 -- 3. Check case of nested aggregate. Generally nested aggregates are
2968 -- handled during the processing of the parent aggregate.
2970 -- 4. Check if the aggregate can be statically processed. If this is the
2971 -- case pass it as is to Gigi. Note that a necessary condition for
2972 -- static processing is that the aggregate be fully positional.
2974 -- 5. If in place aggregate expansion is possible (i.e. no need to create
2975 -- a temporary) then mark the aggregate as such and return. Otherwise
2976 -- create a new temporary and generate the appropriate initialization
2979 procedure Expand_Array_Aggregate (N : Node_Id) is
2980 Loc : constant Source_Ptr := Sloc (N);
2982 Typ : constant Entity_Id := Etype (N);
2983 Ctyp : constant Entity_Id := Component_Type (Typ);
2984 -- Typ is the correct constrained array subtype of the aggregate
2985 -- Ctyp is the corresponding component type.
2987 Aggr_Dimension : constant Pos := Number_Dimensions (Typ);
2988 -- Number of aggregate index dimensions.
2990 Aggr_Low : array (1 .. Aggr_Dimension) of Node_Id;
2991 Aggr_High : array (1 .. Aggr_Dimension) of Node_Id;
2992 -- Low and High bounds of the constraint for each aggregate index.
2994 Aggr_Index_Typ : array (1 .. Aggr_Dimension) of Entity_Id;
2995 -- The type of each index.
2997 Maybe_In_Place_OK : Boolean;
2998 -- If the type is neither controlled nor packed and the aggregate
2999 -- is the expression in an assignment, assignment in place may be
3000 -- possible, provided other conditions are met on the LHS.
3002 Others_Present : array (1 .. Aggr_Dimension) of Boolean :=
3004 -- If Others_Present (J) is True, then there is an others choice
3005 -- in one of the sub-aggregates of N at dimension J.
3007 procedure Build_Constrained_Type (Positional : Boolean);
3008 -- If the subtype is not static or unconstrained, build a constrained
3009 -- type using the computable sizes of the aggregate and its sub-
3012 procedure Check_Bounds (Aggr_Bounds : Node_Id; Index_Bounds : Node_Id);
3013 -- Checks that the bounds of Aggr_Bounds are within the bounds defined
3016 procedure Check_Same_Aggr_Bounds (Sub_Aggr : Node_Id; Dim : Pos);
3017 -- Checks that in a multi-dimensional array aggregate all subaggregates
3018 -- corresponding to the same dimension have the same bounds.
3019 -- Sub_Aggr is an array sub-aggregate. Dim is the dimension
3020 -- corresponding to the sub-aggregate.
3022 procedure Compute_Others_Present (Sub_Aggr : Node_Id; Dim : Pos);
3023 -- Computes the values of array Others_Present. Sub_Aggr is the
3024 -- array sub-aggregate we start the computation from. Dim is the
3025 -- dimension corresponding to the sub-aggregate.
3027 function Has_Address_Clause (D : Node_Id) return Boolean;
3028 -- If the aggregate is the expression in an object declaration, it
3029 -- cannot be expanded in place. This function does a lookahead in the
3030 -- current declarative part to find an address clause for the object
3033 function In_Place_Assign_OK return Boolean;
3034 -- Simple predicate to determine whether an aggregate assignment can
3035 -- be done in place, because none of the new values can depend on the
3036 -- components of the target of the assignment.
3038 function Must_Slide (N : Node_Id; Typ : Entity_Id) return Boolean;
3039 -- A static aggregate in an object declaration can in most cases be
3040 -- expanded in place. The one exception is when the aggregate is given
3041 -- with component associations that specify different bounds from those
3042 -- of the type definition in the object declaration. In this rather
3043 -- pathological case the aggregate must slide, and we must introduce
3044 -- an intermediate temporary to hold it.
3046 procedure Others_Check (Sub_Aggr : Node_Id; Dim : Pos);
3047 -- Checks that if an others choice is present in any sub-aggregate no
3048 -- aggregate index is outside the bounds of the index constraint.
3049 -- Sub_Aggr is an array sub-aggregate. Dim is the dimension
3050 -- corresponding to the sub-aggregate.
3052 ----------------------------
3053 -- Build_Constrained_Type --
3054 ----------------------------
3056 procedure Build_Constrained_Type (Positional : Boolean) is
3057 Loc : constant Source_Ptr := Sloc (N);
3058 Agg_Type : Entity_Id;
3061 Typ : constant Entity_Id := Etype (N);
3062 Indices : constant List_Id := New_List;
3068 Make_Defining_Identifier (
3069 Loc, New_Internal_Name ('A'));
3071 -- If the aggregate is purely positional, all its subaggregates
3072 -- have the same size. We collect the dimensions from the first
3073 -- subaggregate at each level.
3078 for D in 1 .. Number_Dimensions (Typ) loop
3079 Comp := First (Expressions (Sub_Agg));
3084 while Present (Comp) loop
3091 Low_Bound => Make_Integer_Literal (Loc, 1),
3093 Make_Integer_Literal (Loc, Num)),
3098 -- We know the aggregate type is unconstrained and the
3099 -- aggregate is not processable by the back end, therefore
3100 -- not necessarily positional. Retrieve the bounds of each
3101 -- dimension as computed earlier.
3103 for D in 1 .. Number_Dimensions (Typ) loop
3106 Low_Bound => Aggr_Low (D),
3107 High_Bound => Aggr_High (D)),
3113 Make_Full_Type_Declaration (Loc,
3114 Defining_Identifier => Agg_Type,
3116 Make_Constrained_Array_Definition (Loc,
3117 Discrete_Subtype_Definitions => Indices,
3118 Component_Definition =>
3119 Make_Component_Definition (Loc,
3120 Aliased_Present => False,
3121 Subtype_Indication =>
3122 New_Occurrence_Of (Component_Type (Typ), Loc))));
3124 Insert_Action (N, Decl);
3126 Set_Etype (N, Agg_Type);
3127 Set_Is_Itype (Agg_Type);
3128 Freeze_Itype (Agg_Type, N);
3129 end Build_Constrained_Type;
3135 procedure Check_Bounds (Aggr_Bounds : Node_Id; Index_Bounds : Node_Id) is
3142 Cond : Node_Id := Empty;
3145 Get_Index_Bounds (Aggr_Bounds, Aggr_Lo, Aggr_Hi);
3146 Get_Index_Bounds (Index_Bounds, Ind_Lo, Ind_Hi);
3148 -- Generate the following test:
3150 -- [constraint_error when
3151 -- Aggr_Lo <= Aggr_Hi and then
3152 -- (Aggr_Lo < Ind_Lo or else Aggr_Hi > Ind_Hi)]
3154 -- As an optimization try to see if some tests are trivially vacuos
3155 -- because we are comparing an expression against itself.
3157 if Aggr_Lo = Ind_Lo and then Aggr_Hi = Ind_Hi then
3160 elsif Aggr_Hi = Ind_Hi then
3163 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
3164 Right_Opnd => Duplicate_Subexpr_Move_Checks (Ind_Lo));
3166 elsif Aggr_Lo = Ind_Lo then
3169 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi),
3170 Right_Opnd => Duplicate_Subexpr_Move_Checks (Ind_Hi));
3177 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
3178 Right_Opnd => Duplicate_Subexpr_Move_Checks (Ind_Lo)),
3182 Left_Opnd => Duplicate_Subexpr (Aggr_Hi),
3183 Right_Opnd => Duplicate_Subexpr (Ind_Hi)));
3186 if Present (Cond) then
3191 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
3192 Right_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi)),
3194 Right_Opnd => Cond);
3196 Set_Analyzed (Left_Opnd (Left_Opnd (Cond)), False);
3197 Set_Analyzed (Right_Opnd (Left_Opnd (Cond)), False);
3199 Make_Raise_Constraint_Error (Loc,
3201 Reason => CE_Length_Check_Failed));
3205 ----------------------------
3206 -- Check_Same_Aggr_Bounds --
3207 ----------------------------
3209 procedure Check_Same_Aggr_Bounds (Sub_Aggr : Node_Id; Dim : Pos) is
3210 Sub_Lo : constant Node_Id := Low_Bound (Aggregate_Bounds (Sub_Aggr));
3211 Sub_Hi : constant Node_Id := High_Bound (Aggregate_Bounds (Sub_Aggr));
3212 -- The bounds of this specific sub-aggregate.
3214 Aggr_Lo : constant Node_Id := Aggr_Low (Dim);
3215 Aggr_Hi : constant Node_Id := Aggr_High (Dim);
3216 -- The bounds of the aggregate for this dimension
3218 Ind_Typ : constant Entity_Id := Aggr_Index_Typ (Dim);
3219 -- The index type for this dimension.
3221 Cond : Node_Id := Empty;
3227 -- If index checks are on generate the test
3229 -- [constraint_error when
3230 -- Aggr_Lo /= Sub_Lo or else Aggr_Hi /= Sub_Hi]
3232 -- As an optimization try to see if some tests are trivially vacuos
3233 -- because we are comparing an expression against itself. Also for
3234 -- the first dimension the test is trivially vacuous because there
3235 -- is just one aggregate for dimension 1.
3237 if Index_Checks_Suppressed (Ind_Typ) then
3241 or else (Aggr_Lo = Sub_Lo and then Aggr_Hi = Sub_Hi)
3245 elsif Aggr_Hi = Sub_Hi then
3248 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
3249 Right_Opnd => Duplicate_Subexpr_Move_Checks (Sub_Lo));
3251 elsif Aggr_Lo = Sub_Lo then
3254 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi),
3255 Right_Opnd => Duplicate_Subexpr_Move_Checks (Sub_Hi));
3262 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
3263 Right_Opnd => Duplicate_Subexpr_Move_Checks (Sub_Lo)),
3267 Left_Opnd => Duplicate_Subexpr (Aggr_Hi),
3268 Right_Opnd => Duplicate_Subexpr (Sub_Hi)));
3271 if Present (Cond) then
3273 Make_Raise_Constraint_Error (Loc,
3275 Reason => CE_Length_Check_Failed));
3278 -- Now look inside the sub-aggregate to see if there is more work
3280 if Dim < Aggr_Dimension then
3282 -- Process positional components
3284 if Present (Expressions (Sub_Aggr)) then
3285 Expr := First (Expressions (Sub_Aggr));
3286 while Present (Expr) loop
3287 Check_Same_Aggr_Bounds (Expr, Dim + 1);
3292 -- Process component associations
3294 if Present (Component_Associations (Sub_Aggr)) then
3295 Assoc := First (Component_Associations (Sub_Aggr));
3296 while Present (Assoc) loop
3297 Expr := Expression (Assoc);
3298 Check_Same_Aggr_Bounds (Expr, Dim + 1);
3303 end Check_Same_Aggr_Bounds;
3305 ----------------------------
3306 -- Compute_Others_Present --
3307 ----------------------------
3309 procedure Compute_Others_Present (Sub_Aggr : Node_Id; Dim : Pos) is
3314 if Present (Component_Associations (Sub_Aggr)) then
3315 Assoc := Last (Component_Associations (Sub_Aggr));
3317 if Nkind (First (Choices (Assoc))) = N_Others_Choice then
3318 Others_Present (Dim) := True;
3322 -- Now look inside the sub-aggregate to see if there is more work
3324 if Dim < Aggr_Dimension then
3326 -- Process positional components
3328 if Present (Expressions (Sub_Aggr)) then
3329 Expr := First (Expressions (Sub_Aggr));
3330 while Present (Expr) loop
3331 Compute_Others_Present (Expr, Dim + 1);
3336 -- Process component associations
3338 if Present (Component_Associations (Sub_Aggr)) then
3339 Assoc := First (Component_Associations (Sub_Aggr));
3340 while Present (Assoc) loop
3341 Expr := Expression (Assoc);
3342 Compute_Others_Present (Expr, Dim + 1);
3347 end Compute_Others_Present;
3349 ------------------------
3350 -- Has_Address_Clause --
3351 ------------------------
3353 function Has_Address_Clause (D : Node_Id) return Boolean is
3354 Id : constant Entity_Id := Defining_Identifier (D);
3355 Decl : Node_Id := Next (D);
3358 while Present (Decl) loop
3359 if Nkind (Decl) = N_At_Clause
3360 and then Chars (Identifier (Decl)) = Chars (Id)
3364 elsif Nkind (Decl) = N_Attribute_Definition_Clause
3365 and then Chars (Decl) = Name_Address
3366 and then Chars (Name (Decl)) = Chars (Id)
3375 end Has_Address_Clause;
3377 ------------------------
3378 -- In_Place_Assign_OK --
3379 ------------------------
3381 function In_Place_Assign_OK return Boolean is
3389 function Is_Others_Aggregate (Aggr : Node_Id) return Boolean;
3390 -- Aggregates that consist of a single Others choice are safe
3391 -- if the single expression is.
3393 function Safe_Aggregate (Aggr : Node_Id) return Boolean;
3394 -- Check recursively that each component of a (sub)aggregate does
3395 -- not depend on the variable being assigned to.
3397 function Safe_Component (Expr : Node_Id) return Boolean;
3398 -- Verify that an expression cannot depend on the variable being
3399 -- assigned to. Room for improvement here (but less than before).
3401 -------------------------
3402 -- Is_Others_Aggregate --
3403 -------------------------
3405 function Is_Others_Aggregate (Aggr : Node_Id) return Boolean is
3407 return No (Expressions (Aggr))
3409 (First (Choices (First (Component_Associations (Aggr)))))
3411 end Is_Others_Aggregate;
3413 --------------------
3414 -- Safe_Aggregate --
3415 --------------------
3417 function Safe_Aggregate (Aggr : Node_Id) return Boolean is
3421 if Present (Expressions (Aggr)) then
3422 Expr := First (Expressions (Aggr));
3424 while Present (Expr) loop
3425 if Nkind (Expr) = N_Aggregate then
3426 if not Safe_Aggregate (Expr) then
3430 elsif not Safe_Component (Expr) then
3438 if Present (Component_Associations (Aggr)) then
3439 Expr := First (Component_Associations (Aggr));
3441 while Present (Expr) loop
3442 if Nkind (Expression (Expr)) = N_Aggregate then
3443 if not Safe_Aggregate (Expression (Expr)) then
3447 elsif not Safe_Component (Expression (Expr)) then
3458 --------------------
3459 -- Safe_Component --
3460 --------------------
3462 function Safe_Component (Expr : Node_Id) return Boolean is
3463 Comp : Node_Id := Expr;
3465 function Check_Component (Comp : Node_Id) return Boolean;
3466 -- Do the recursive traversal, after copy.
3468 ---------------------
3469 -- Check_Component --
3470 ---------------------
3472 function Check_Component (Comp : Node_Id) return Boolean is
3474 if Is_Overloaded (Comp) then
3478 return Compile_Time_Known_Value (Comp)
3480 or else (Is_Entity_Name (Comp)
3481 and then Present (Entity (Comp))
3482 and then No (Renamed_Object (Entity (Comp))))
3484 or else (Nkind (Comp) = N_Attribute_Reference
3485 and then Check_Component (Prefix (Comp)))
3487 or else (Nkind (Comp) in N_Binary_Op
3488 and then Check_Component (Left_Opnd (Comp))
3489 and then Check_Component (Right_Opnd (Comp)))
3491 or else (Nkind (Comp) in N_Unary_Op
3492 and then Check_Component (Right_Opnd (Comp)))
3494 or else (Nkind (Comp) = N_Selected_Component
3495 and then Check_Component (Prefix (Comp)))
3497 or else (Nkind (Comp) = N_Unchecked_Type_Conversion
3498 and then Check_Component (Expression (Comp)));
3499 end Check_Component;
3501 -- Start of processing for Safe_Component
3504 -- If the component appears in an association that may
3505 -- correspond to more than one element, it is not analyzed
3506 -- before the expansion into assignments, to avoid side effects.
3507 -- We analyze, but do not resolve the copy, to obtain sufficient
3508 -- entity information for the checks that follow. If component is
3509 -- overloaded we assume an unsafe function call.
3511 if not Analyzed (Comp) then
3512 if Is_Overloaded (Expr) then
3515 elsif Nkind (Expr) = N_Aggregate
3516 and then not Is_Others_Aggregate (Expr)
3520 elsif Nkind (Expr) = N_Allocator then
3521 -- For now, too complex to analyze.
3526 Comp := New_Copy_Tree (Expr);
3527 Set_Parent (Comp, Parent (Expr));
3531 if Nkind (Comp) = N_Aggregate then
3532 return Safe_Aggregate (Comp);
3534 return Check_Component (Comp);
3538 -- Start of processing for In_Place_Assign_OK
3541 if Present (Component_Associations (N)) then
3543 -- On assignment, sliding can take place, so we cannot do the
3544 -- assignment in place unless the bounds of the aggregate are
3545 -- statically equal to those of the target.
3547 -- If the aggregate is given by an others choice, the bounds
3548 -- are derived from the left-hand side, and the assignment is
3549 -- safe if the expression is.
3551 if Is_Others_Aggregate (N) then
3554 (Expression (First (Component_Associations (N))));
3557 Aggr_In := First_Index (Etype (N));
3558 if Nkind (Parent (N)) = N_Assignment_Statement then
3559 Obj_In := First_Index (Etype (Name (Parent (N))));
3562 -- Context is an allocator. Check bounds of aggregate
3563 -- against given type in qualified expression.
3565 pragma Assert (Nkind (Parent (Parent (N))) = N_Allocator);
3567 First_Index (Etype (Entity (Subtype_Mark (Parent (N)))));
3570 while Present (Aggr_In) loop
3571 Get_Index_Bounds (Aggr_In, Aggr_Lo, Aggr_Hi);
3572 Get_Index_Bounds (Obj_In, Obj_Lo, Obj_Hi);
3574 if not Compile_Time_Known_Value (Aggr_Lo)
3575 or else not Compile_Time_Known_Value (Aggr_Hi)
3576 or else not Compile_Time_Known_Value (Obj_Lo)
3577 or else not Compile_Time_Known_Value (Obj_Hi)
3578 or else Expr_Value (Aggr_Lo) /= Expr_Value (Obj_Lo)
3579 or else Expr_Value (Aggr_Hi) /= Expr_Value (Obj_Hi)
3584 Next_Index (Aggr_In);
3585 Next_Index (Obj_In);
3589 -- Now check the component values themselves.
3591 return Safe_Aggregate (N);
3592 end In_Place_Assign_OK;
3598 function Must_Slide (N : Node_Id; Typ : Entity_Id) return Boolean
3600 Obj_Type : constant Entity_Id :=
3601 Etype (Defining_Identifier (Parent (N)));
3603 L1, L2, H1, H2 : Node_Id;
3606 -- No sliding if the type of the object is not established yet, if
3607 -- it is an unconstrained type whose actual subtype comes from the
3608 -- aggregate, or if the two types are identical.
3610 if not Is_Array_Type (Obj_Type) then
3613 elsif not Is_Constrained (Obj_Type) then
3616 elsif Typ = Obj_Type then
3620 -- Sliding can only occur along the first dimension
3622 Get_Index_Bounds (First_Index (Typ), L1, H1);
3623 Get_Index_Bounds (First_Index (Obj_Type), L2, H2);
3625 if not Is_Static_Expression (L1)
3626 or else not Is_Static_Expression (L2)
3627 or else not Is_Static_Expression (H1)
3628 or else not Is_Static_Expression (H2)
3632 return Expr_Value (L1) /= Expr_Value (L2)
3633 or else Expr_Value (H1) /= Expr_Value (H2);
3642 procedure Others_Check (Sub_Aggr : Node_Id; Dim : Pos) is
3643 Aggr_Lo : constant Node_Id := Aggr_Low (Dim);
3644 Aggr_Hi : constant Node_Id := Aggr_High (Dim);
3645 -- The bounds of the aggregate for this dimension.
3647 Ind_Typ : constant Entity_Id := Aggr_Index_Typ (Dim);
3648 -- The index type for this dimension.
3650 Need_To_Check : Boolean := False;
3652 Choices_Lo : Node_Id := Empty;
3653 Choices_Hi : Node_Id := Empty;
3654 -- The lowest and highest discrete choices for a named sub-aggregate
3656 Nb_Choices : Int := -1;
3657 -- The number of discrete non-others choices in this sub-aggregate
3659 Nb_Elements : Uint := Uint_0;
3660 -- The number of elements in a positional aggregate
3662 Cond : Node_Id := Empty;
3669 -- Check if we have an others choice. If we do make sure that this
3670 -- sub-aggregate contains at least one element in addition to the
3673 if Range_Checks_Suppressed (Ind_Typ) then
3674 Need_To_Check := False;
3676 elsif Present (Expressions (Sub_Aggr))
3677 and then Present (Component_Associations (Sub_Aggr))
3679 Need_To_Check := True;
3681 elsif Present (Component_Associations (Sub_Aggr)) then
3682 Assoc := Last (Component_Associations (Sub_Aggr));
3684 if Nkind (First (Choices (Assoc))) /= N_Others_Choice then
3685 Need_To_Check := False;
3688 -- Count the number of discrete choices. Start with -1
3689 -- because the others choice does not count.
3692 Assoc := First (Component_Associations (Sub_Aggr));
3693 while Present (Assoc) loop
3694 Choice := First (Choices (Assoc));
3695 while Present (Choice) loop
3696 Nb_Choices := Nb_Choices + 1;
3703 -- If there is only an others choice nothing to do
3705 Need_To_Check := (Nb_Choices > 0);
3709 Need_To_Check := False;
3712 -- If we are dealing with a positional sub-aggregate with an
3713 -- others choice then compute the number or positional elements.
3715 if Need_To_Check and then Present (Expressions (Sub_Aggr)) then
3716 Expr := First (Expressions (Sub_Aggr));
3717 Nb_Elements := Uint_0;
3718 while Present (Expr) loop
3719 Nb_Elements := Nb_Elements + 1;
3723 -- If the aggregate contains discrete choices and an others choice
3724 -- compute the smallest and largest discrete choice values.
3726 elsif Need_To_Check then
3727 Compute_Choices_Lo_And_Choices_Hi : declare
3729 Table : Case_Table_Type (1 .. Nb_Choices);
3730 -- Used to sort all the different choice values
3737 Assoc := First (Component_Associations (Sub_Aggr));
3738 while Present (Assoc) loop
3739 Choice := First (Choices (Assoc));
3740 while Present (Choice) loop
3741 if Nkind (Choice) = N_Others_Choice then
3745 Get_Index_Bounds (Choice, Low, High);
3746 Table (J).Choice_Lo := Low;
3747 Table (J).Choice_Hi := High;
3756 -- Sort the discrete choices
3758 Sort_Case_Table (Table);
3760 Choices_Lo := Table (1).Choice_Lo;
3761 Choices_Hi := Table (Nb_Choices).Choice_Hi;
3762 end Compute_Choices_Lo_And_Choices_Hi;
3765 -- If no others choice in this sub-aggregate, or the aggregate
3766 -- comprises only an others choice, nothing to do.
3768 if not Need_To_Check then
3771 -- If we are dealing with an aggregate containing an others
3772 -- choice and positional components, we generate the following test:
3774 -- if Ind_Typ'Pos (Aggr_Lo) + (Nb_Elements - 1) >
3775 -- Ind_Typ'Pos (Aggr_Hi)
3777 -- raise Constraint_Error;
3780 elsif Nb_Elements > Uint_0 then
3786 Make_Attribute_Reference (Loc,
3787 Prefix => New_Reference_To (Ind_Typ, Loc),
3788 Attribute_Name => Name_Pos,
3791 (Duplicate_Subexpr_Move_Checks (Aggr_Lo))),
3792 Right_Opnd => Make_Integer_Literal (Loc, Nb_Elements - 1)),
3795 Make_Attribute_Reference (Loc,
3796 Prefix => New_Reference_To (Ind_Typ, Loc),
3797 Attribute_Name => Name_Pos,
3798 Expressions => New_List (
3799 Duplicate_Subexpr_Move_Checks (Aggr_Hi))));
3801 -- If we are dealing with an aggregate containing an others
3802 -- choice and discrete choices we generate the following test:
3804 -- [constraint_error when
3805 -- Choices_Lo < Aggr_Lo or else Choices_Hi > Aggr_Hi];
3813 Duplicate_Subexpr_Move_Checks (Choices_Lo),
3815 Duplicate_Subexpr_Move_Checks (Aggr_Lo)),
3820 Duplicate_Subexpr (Choices_Hi),
3822 Duplicate_Subexpr (Aggr_Hi)));
3825 if Present (Cond) then
3827 Make_Raise_Constraint_Error (Loc,
3829 Reason => CE_Length_Check_Failed));
3832 -- Now look inside the sub-aggregate to see if there is more work
3834 if Dim < Aggr_Dimension then
3836 -- Process positional components
3838 if Present (Expressions (Sub_Aggr)) then
3839 Expr := First (Expressions (Sub_Aggr));
3840 while Present (Expr) loop
3841 Others_Check (Expr, Dim + 1);
3846 -- Process component associations
3848 if Present (Component_Associations (Sub_Aggr)) then
3849 Assoc := First (Component_Associations (Sub_Aggr));
3850 while Present (Assoc) loop
3851 Expr := Expression (Assoc);
3852 Others_Check (Expr, Dim + 1);
3859 -- Remaining Expand_Array_Aggregate variables
3862 -- Holds the temporary aggregate value
3865 -- Holds the declaration of Tmp
3867 Aggr_Code : List_Id;
3868 Parent_Node : Node_Id;
3869 Parent_Kind : Node_Kind;
3871 -- Start of processing for Expand_Array_Aggregate
3874 -- Do not touch the special aggregates of attributes used for Asm calls
3876 if Is_RTE (Ctyp, RE_Asm_Input_Operand)
3877 or else Is_RTE (Ctyp, RE_Asm_Output_Operand)
3882 -- If the semantic analyzer has determined that aggregate N will raise
3883 -- Constraint_Error at run-time, then the aggregate node has been
3884 -- replaced with an N_Raise_Constraint_Error node and we should
3887 pragma Assert (not Raises_Constraint_Error (N));
3891 -- Check that the index range defined by aggregate bounds is
3892 -- compatible with corresponding index subtype.
3894 Index_Compatibility_Check : declare
3895 Aggr_Index_Range : Node_Id := First_Index (Typ);
3896 -- The current aggregate index range
3898 Index_Constraint : Node_Id := First_Index (Etype (Typ));
3899 -- The corresponding index constraint against which we have to
3900 -- check the above aggregate index range.
3903 Compute_Others_Present (N, 1);
3905 for J in 1 .. Aggr_Dimension loop
3906 -- There is no need to emit a check if an others choice is
3907 -- present for this array aggregate dimension since in this
3908 -- case one of N's sub-aggregates has taken its bounds from the
3909 -- context and these bounds must have been checked already. In
3910 -- addition all sub-aggregates corresponding to the same
3911 -- dimension must all have the same bounds (checked in (c) below).
3913 if not Range_Checks_Suppressed (Etype (Index_Constraint))
3914 and then not Others_Present (J)
3916 -- We don't use Checks.Apply_Range_Check here because it
3917 -- emits a spurious check. Namely it checks that the range
3918 -- defined by the aggregate bounds is non empty. But we know
3919 -- this already if we get here.
3921 Check_Bounds (Aggr_Index_Range, Index_Constraint);
3924 -- Save the low and high bounds of the aggregate index as well
3925 -- as the index type for later use in checks (b) and (c) below.
3927 Aggr_Low (J) := Low_Bound (Aggr_Index_Range);
3928 Aggr_High (J) := High_Bound (Aggr_Index_Range);
3930 Aggr_Index_Typ (J) := Etype (Index_Constraint);
3932 Next_Index (Aggr_Index_Range);
3933 Next_Index (Index_Constraint);
3935 end Index_Compatibility_Check;
3939 -- If an others choice is present check that no aggregate
3940 -- index is outside the bounds of the index constraint.
3942 Others_Check (N, 1);
3946 -- For multidimensional arrays make sure that all subaggregates
3947 -- corresponding to the same dimension have the same bounds.
3949 if Aggr_Dimension > 1 then
3950 Check_Same_Aggr_Bounds (N, 1);
3955 -- Here we test for is packed array aggregate that we can handle
3956 -- at compile time. If so, return with transformation done. Note
3957 -- that we do this even if the aggregate is nested, because once
3958 -- we have done this processing, there is no more nested aggregate!
3960 if Packed_Array_Aggregate_Handled (N) then
3964 -- At this point we try to convert to positional form
3966 Convert_To_Positional (N);
3968 -- if the result is no longer an aggregate (e.g. it may be a string
3969 -- literal, or a temporary which has the needed value), then we are
3970 -- done, since there is no longer a nested aggregate.
3972 if Nkind (N) /= N_Aggregate then
3975 -- We are also done if the result is an analyzed aggregate
3976 -- This case could use more comments ???
3979 and then N /= Original_Node (N)
3984 -- Now see if back end processing is possible
3986 if Backend_Processing_Possible (N) then
3988 -- If the aggregate is static but the constraints are not, build
3989 -- a static subtype for the aggregate, so that Gigi can place it
3990 -- in static memory. Perform an unchecked_conversion to the non-
3991 -- static type imposed by the context.
3994 Itype : constant Entity_Id := Etype (N);
3996 Needs_Type : Boolean := False;
3999 Index := First_Index (Itype);
4001 while Present (Index) loop
4002 if not Is_Static_Subtype (Etype (Index)) then
4011 Build_Constrained_Type (Positional => True);
4012 Rewrite (N, Unchecked_Convert_To (Itype, N));
4022 -- Delay expansion for nested aggregates it will be taken care of
4023 -- when the parent aggregate is expanded
4025 Parent_Node := Parent (N);
4026 Parent_Kind := Nkind (Parent_Node);
4028 if Parent_Kind = N_Qualified_Expression then
4029 Parent_Node := Parent (Parent_Node);
4030 Parent_Kind := Nkind (Parent_Node);
4033 if Parent_Kind = N_Aggregate
4034 or else Parent_Kind = N_Extension_Aggregate
4035 or else Parent_Kind = N_Component_Association
4036 or else (Parent_Kind = N_Object_Declaration
4037 and then Controlled_Type (Typ))
4038 or else (Parent_Kind = N_Assignment_Statement
4039 and then Inside_Init_Proc)
4041 Set_Expansion_Delayed (N);
4047 -- Look if in place aggregate expansion is possible
4049 -- For object declarations we build the aggregate in place, unless
4050 -- the array is bit-packed or the component is controlled.
4052 -- For assignments we do the assignment in place if all the component
4053 -- associations have compile-time known values. For other cases we
4054 -- create a temporary. The analysis for safety of on-line assignment
4055 -- is delicate, i.e. we don't know how to do it fully yet ???
4057 -- For allocators we assign to the designated object in place if the
4058 -- aggregate meets the same conditions as other in-place assignments.
4059 -- In this case the aggregate may not come from source but was created
4060 -- for default initialization, e.g. with Initialize_Scalars.
4062 if Requires_Transient_Scope (Typ) then
4063 Establish_Transient_Scope
4064 (N, Sec_Stack => Has_Controlled_Component (Typ));
4067 if Has_Default_Init_Comps (N) then
4068 Maybe_In_Place_OK := False;
4070 elsif Is_Bit_Packed_Array (Typ)
4071 or else Has_Controlled_Component (Typ)
4073 Maybe_In_Place_OK := False;
4076 Maybe_In_Place_OK :=
4077 (Nkind (Parent (N)) = N_Assignment_Statement
4078 and then Comes_From_Source (N)
4079 and then In_Place_Assign_OK)
4082 (Nkind (Parent (Parent (N))) = N_Allocator
4083 and then In_Place_Assign_OK);
4086 if not Has_Default_Init_Comps (N)
4087 and then Comes_From_Source (Parent (N))
4088 and then Nkind (Parent (N)) = N_Object_Declaration
4089 and then not Must_Slide (N, Typ)
4090 and then N = Expression (Parent (N))
4091 and then not Is_Bit_Packed_Array (Typ)
4092 and then not Has_Controlled_Component (Typ)
4093 and then not Has_Address_Clause (Parent (N))
4095 Tmp := Defining_Identifier (Parent (N));
4096 Set_No_Initialization (Parent (N));
4097 Set_Expression (Parent (N), Empty);
4099 -- Set the type of the entity, for use in the analysis of the
4100 -- subsequent indexed assignments. If the nominal type is not
4101 -- constrained, build a subtype from the known bounds of the
4102 -- aggregate. If the declaration has a subtype mark, use it,
4103 -- otherwise use the itype of the aggregate.
4105 if not Is_Constrained (Typ) then
4106 Build_Constrained_Type (Positional => False);
4107 elsif Is_Entity_Name (Object_Definition (Parent (N)))
4108 and then Is_Constrained (Entity (Object_Definition (Parent (N))))
4110 Set_Etype (Tmp, Entity (Object_Definition (Parent (N))));
4112 Set_Size_Known_At_Compile_Time (Typ, False);
4113 Set_Etype (Tmp, Typ);
4116 elsif Maybe_In_Place_OK
4117 and then Nkind (Parent (N)) = N_Qualified_Expression
4118 and then Nkind (Parent (Parent (N))) = N_Allocator
4120 Set_Expansion_Delayed (N);
4123 -- In the remaining cases the aggregate is the RHS of an assignment.
4125 elsif Maybe_In_Place_OK
4126 and then Is_Entity_Name (Name (Parent (N)))
4128 Tmp := Entity (Name (Parent (N)));
4130 if Etype (Tmp) /= Etype (N) then
4131 Apply_Length_Check (N, Etype (Tmp));
4133 if Nkind (N) = N_Raise_Constraint_Error then
4135 -- Static error, nothing further to expand
4141 elsif Maybe_In_Place_OK
4142 and then Nkind (Name (Parent (N))) = N_Explicit_Dereference
4143 and then Is_Entity_Name (Prefix (Name (Parent (N))))
4145 Tmp := Name (Parent (N));
4147 if Etype (Tmp) /= Etype (N) then
4148 Apply_Length_Check (N, Etype (Tmp));
4151 elsif Maybe_In_Place_OK
4152 and then Nkind (Name (Parent (N))) = N_Slice
4153 and then Safe_Slice_Assignment (N)
4155 -- Safe_Slice_Assignment rewrites assignment as a loop
4161 -- In place aggregate expansion is not possible
4164 Maybe_In_Place_OK := False;
4165 Tmp := Make_Defining_Identifier (Loc, New_Internal_Name ('A'));
4167 Make_Object_Declaration
4169 Defining_Identifier => Tmp,
4170 Object_Definition => New_Occurrence_Of (Typ, Loc));
4171 Set_No_Initialization (Tmp_Decl, True);
4173 -- If we are within a loop, the temporary will be pushed on the
4174 -- stack at each iteration. If the aggregate is the expression for
4175 -- an allocator, it will be immediately copied to the heap and can
4176 -- be reclaimed at once. We create a transient scope around the
4177 -- aggregate for this purpose.
4179 if Ekind (Current_Scope) = E_Loop
4180 and then Nkind (Parent (Parent (N))) = N_Allocator
4182 Establish_Transient_Scope (N, False);
4185 Insert_Action (N, Tmp_Decl);
4188 -- Construct and insert the aggregate code. We can safely suppress
4189 -- index checks because this code is guaranteed not to raise CE
4190 -- on index checks. However we should *not* suppress all checks.
4196 if Nkind (Tmp) = N_Defining_Identifier then
4197 Target := New_Reference_To (Tmp, Loc);
4201 if Has_Default_Init_Comps (N) then
4203 -- Ada 2005 (AI-287): This case has not been analyzed???
4205 raise Program_Error;
4208 -- Name in assignment is explicit dereference
4210 Target := New_Copy (Tmp);
4214 Build_Array_Aggr_Code (N,
4216 Index => First_Index (Typ),
4218 Scalar_Comp => Is_Scalar_Type (Ctyp));
4221 if Comes_From_Source (Tmp) then
4222 Insert_Actions_After (Parent (N), Aggr_Code);
4225 Insert_Actions (N, Aggr_Code);
4228 -- If the aggregate has been assigned in place, remove the original
4231 if Nkind (Parent (N)) = N_Assignment_Statement
4232 and then Maybe_In_Place_OK
4234 Rewrite (Parent (N), Make_Null_Statement (Loc));
4236 elsif Nkind (Parent (N)) /= N_Object_Declaration
4237 or else Tmp /= Defining_Identifier (Parent (N))
4239 Rewrite (N, New_Occurrence_Of (Tmp, Loc));
4240 Analyze_And_Resolve (N, Typ);
4242 end Expand_Array_Aggregate;
4244 ------------------------
4245 -- Expand_N_Aggregate --
4246 ------------------------
4248 procedure Expand_N_Aggregate (N : Node_Id) is
4250 if Is_Record_Type (Etype (N)) then
4251 Expand_Record_Aggregate (N);
4253 Expand_Array_Aggregate (N);
4257 when RE_Not_Available =>
4259 end Expand_N_Aggregate;
4261 ----------------------------------
4262 -- Expand_N_Extension_Aggregate --
4263 ----------------------------------
4265 -- If the ancestor part is an expression, add a component association for
4266 -- the parent field. If the type of the ancestor part is not the direct
4267 -- parent of the expected type, build recursively the needed ancestors.
4268 -- If the ancestor part is a subtype_mark, replace aggregate with a decla-
4269 -- ration for a temporary of the expected type, followed by individual
4270 -- assignments to the given components.
4272 procedure Expand_N_Extension_Aggregate (N : Node_Id) is
4273 Loc : constant Source_Ptr := Sloc (N);
4274 A : constant Node_Id := Ancestor_Part (N);
4275 Typ : constant Entity_Id := Etype (N);
4278 -- If the ancestor is a subtype mark, an init proc must be called
4279 -- on the resulting object which thus has to be materialized in
4282 if Is_Entity_Name (A) and then Is_Type (Entity (A)) then
4283 Convert_To_Assignments (N, Typ);
4285 -- The extension aggregate is transformed into a record aggregate
4286 -- of the following form (c1 and c2 are inherited components)
4288 -- (Exp with c3 => a, c4 => b)
4289 -- ==> (c1 => Exp.c1, c2 => Exp.c2, c1 => a, c2 => b)
4294 -- No tag is needed in the case of Java_VM
4297 Expand_Record_Aggregate (N,
4300 Expand_Record_Aggregate (N,
4301 Orig_Tag => New_Occurrence_Of (Access_Disp_Table (Typ), Loc),
4307 when RE_Not_Available =>
4309 end Expand_N_Extension_Aggregate;
4311 -----------------------------
4312 -- Expand_Record_Aggregate --
4313 -----------------------------
4315 procedure Expand_Record_Aggregate
4317 Orig_Tag : Node_Id := Empty;
4318 Parent_Expr : Node_Id := Empty)
4320 Loc : constant Source_Ptr := Sloc (N);
4321 Comps : constant List_Id := Component_Associations (N);
4322 Typ : constant Entity_Id := Etype (N);
4323 Base_Typ : constant Entity_Id := Base_Type (Typ);
4325 function Has_Delayed_Nested_Aggregate_Or_Tagged_Comps return Boolean;
4326 -- Checks the presence of a nested aggregate which needs Late_Expansion
4327 -- or the presence of tagged components which may need tag adjustment.
4329 --------------------------------------------------
4330 -- Has_Delayed_Nested_Aggregate_Or_Tagged_Comps --
4331 --------------------------------------------------
4333 function Has_Delayed_Nested_Aggregate_Or_Tagged_Comps return Boolean is
4343 while Present (C) loop
4344 if Nkind (Expression (C)) = N_Qualified_Expression then
4345 Expr_Q := Expression (Expression (C));
4347 Expr_Q := Expression (C);
4350 -- Return true if the aggregate has any associations for
4351 -- tagged components that may require tag adjustment.
4352 -- These are cases where the source expression may have
4353 -- a tag that could differ from the component tag (e.g.,
4354 -- can occur for type conversions and formal parameters).
4355 -- (Tag adjustment is not needed if Java_VM because object
4356 -- tags are implicit in the JVM.)
4358 if Is_Tagged_Type (Etype (Expr_Q))
4359 and then (Nkind (Expr_Q) = N_Type_Conversion
4360 or else (Is_Entity_Name (Expr_Q)
4361 and then Ekind (Entity (Expr_Q)) in Formal_Kind))
4362 and then not Java_VM
4367 if Is_Delayed_Aggregate (Expr_Q) then
4375 end Has_Delayed_Nested_Aggregate_Or_Tagged_Comps;
4377 -- Remaining Expand_Record_Aggregate variables
4379 Tag_Value : Node_Id;
4383 -- Start of processing for Expand_Record_Aggregate
4386 -- If the aggregate is to be assigned to an atomic variable, we
4387 -- have to prevent a piecemeal assignment even if the aggregate
4388 -- is to be expanded. We create a temporary for the aggregate, and
4389 -- assign the temporary instead, so that the back end can generate
4390 -- an atomic move for it.
4393 and then (Nkind (Parent (N)) = N_Object_Declaration
4394 or else Nkind (Parent (N)) = N_Assignment_Statement)
4395 and then Comes_From_Source (Parent (N))
4397 Expand_Atomic_Aggregate (N, Typ);
4401 -- Gigi doesn't handle properly temporaries of variable size
4402 -- so we generate it in the front-end
4404 if not Size_Known_At_Compile_Time (Typ) then
4405 Convert_To_Assignments (N, Typ);
4407 -- Temporaries for controlled aggregates need to be attached to a
4408 -- final chain in order to be properly finalized, so it has to
4409 -- be created in the front-end
4411 elsif Is_Controlled (Typ)
4412 or else Has_Controlled_Component (Base_Type (Typ))
4414 Convert_To_Assignments (N, Typ);
4416 -- Ada 2005 (AI-287): In case of default initialized components we
4417 -- convert the aggregate into assignments.
4419 elsif Has_Default_Init_Comps (N) then
4420 Convert_To_Assignments (N, Typ);
4422 elsif Has_Delayed_Nested_Aggregate_Or_Tagged_Comps then
4423 Convert_To_Assignments (N, Typ);
4425 -- If an ancestor is private, some components are not inherited and
4426 -- we cannot expand into a record aggregate
4428 elsif Has_Private_Ancestor (Typ) then
4429 Convert_To_Assignments (N, Typ);
4431 -- ??? The following was done to compile fxacc00.ads in the ACVCs. Gigi
4432 -- is not able to handle the aggregate for Late_Request.
4434 elsif Is_Tagged_Type (Typ) and then Has_Discriminants (Typ) then
4435 Convert_To_Assignments (N, Typ);
4437 -- If some components are mutable, the size of the aggregate component
4438 -- may be disctinct from the default size of the type component, so
4439 -- we need to expand to insure that the back-end copies the proper
4440 -- size of the data.
4442 elsif Has_Mutable_Components (Typ) then
4443 Convert_To_Assignments (N, Typ);
4445 -- If the type involved has any non-bit aligned components, then
4446 -- we are not sure that the back end can handle this case correctly.
4448 elsif Type_May_Have_Bit_Aligned_Components (Typ) then
4449 Convert_To_Assignments (N, Typ);
4451 -- In all other cases we generate a proper aggregate that
4452 -- can be handled by gigi.
4455 -- If no discriminants, nothing special to do
4457 if not Has_Discriminants (Typ) then
4460 -- Case of discriminants present
4462 elsif Is_Derived_Type (Typ) then
4464 -- For untagged types, non-stored discriminants are replaced
4465 -- with stored discriminants, which are the ones that gigi uses
4466 -- to describe the type and its components.
4468 Generate_Aggregate_For_Derived_Type : declare
4469 Constraints : constant List_Id := New_List;
4470 First_Comp : Node_Id;
4471 Discriminant : Entity_Id;
4473 Num_Disc : Int := 0;
4474 Num_Gird : Int := 0;
4476 procedure Prepend_Stored_Values (T : Entity_Id);
4477 -- Scan the list of stored discriminants of the type, and
4478 -- add their values to the aggregate being built.
4480 ---------------------------
4481 -- Prepend_Stored_Values --
4482 ---------------------------
4484 procedure Prepend_Stored_Values (T : Entity_Id) is
4486 Discriminant := First_Stored_Discriminant (T);
4488 while Present (Discriminant) loop
4490 Make_Component_Association (Loc,
4492 New_List (New_Occurrence_Of (Discriminant, Loc)),
4496 Get_Discriminant_Value (
4499 Discriminant_Constraint (Typ))));
4501 if No (First_Comp) then
4502 Prepend_To (Component_Associations (N), New_Comp);
4504 Insert_After (First_Comp, New_Comp);
4507 First_Comp := New_Comp;
4508 Next_Stored_Discriminant (Discriminant);
4510 end Prepend_Stored_Values;
4512 -- Start of processing for Generate_Aggregate_For_Derived_Type
4515 -- Remove the associations for the discriminant of
4516 -- the derived type.
4518 First_Comp := First (Component_Associations (N));
4520 while Present (First_Comp) loop
4524 if Ekind (Entity (First (Choices (Comp)))) =
4528 Num_Disc := Num_Disc + 1;
4532 -- Insert stored discriminant associations in the correct
4533 -- order. If there are more stored discriminants than new
4534 -- discriminants, there is at least one new discriminant
4535 -- that constrains more than one of the stored discriminants.
4536 -- In this case we need to construct a proper subtype of
4537 -- the parent type, in order to supply values to all the
4538 -- components. Otherwise there is one-one correspondence
4539 -- between the constraints and the stored discriminants.
4541 First_Comp := Empty;
4543 Discriminant := First_Stored_Discriminant (Base_Type (Typ));
4545 while Present (Discriminant) loop
4546 Num_Gird := Num_Gird + 1;
4547 Next_Stored_Discriminant (Discriminant);
4550 -- Case of more stored discriminants than new discriminants
4552 if Num_Gird > Num_Disc then
4554 -- Create a proper subtype of the parent type, which is
4555 -- the proper implementation type for the aggregate, and
4556 -- convert it to the intended target type.
4558 Discriminant := First_Stored_Discriminant (Base_Type (Typ));
4560 while Present (Discriminant) loop
4563 Get_Discriminant_Value (
4566 Discriminant_Constraint (Typ)));
4567 Append (New_Comp, Constraints);
4568 Next_Stored_Discriminant (Discriminant);
4572 Make_Subtype_Declaration (Loc,
4573 Defining_Identifier =>
4574 Make_Defining_Identifier (Loc,
4575 New_Internal_Name ('T')),
4576 Subtype_Indication =>
4577 Make_Subtype_Indication (Loc,
4579 New_Occurrence_Of (Etype (Base_Type (Typ)), Loc),
4581 Make_Index_Or_Discriminant_Constraint
4582 (Loc, Constraints)));
4584 Insert_Action (N, Decl);
4585 Prepend_Stored_Values (Base_Type (Typ));
4587 Set_Etype (N, Defining_Identifier (Decl));
4590 Rewrite (N, Unchecked_Convert_To (Typ, N));
4593 -- Case where we do not have fewer new discriminants than
4594 -- stored discriminants, so in this case we can simply
4595 -- use the stored discriminants of the subtype.
4598 Prepend_Stored_Values (Typ);
4600 end Generate_Aggregate_For_Derived_Type;
4603 if Is_Tagged_Type (Typ) then
4605 -- The tagged case, _parent and _tag component must be created.
4607 -- Reset null_present unconditionally. tagged records always have
4608 -- at least one field (the tag or the parent)
4610 Set_Null_Record_Present (N, False);
4612 -- When the current aggregate comes from the expansion of an
4613 -- extension aggregate, the parent expr is replaced by an
4614 -- aggregate formed by selected components of this expr
4616 if Present (Parent_Expr)
4617 and then Is_Empty_List (Comps)
4619 Comp := First_Entity (Typ);
4620 while Present (Comp) loop
4622 -- Skip all entities that aren't discriminants or components
4624 if Ekind (Comp) /= E_Discriminant
4625 and then Ekind (Comp) /= E_Component
4629 -- Skip all expander-generated components
4632 not Comes_From_Source (Original_Record_Component (Comp))
4638 Make_Selected_Component (Loc,
4640 Unchecked_Convert_To (Typ,
4641 Duplicate_Subexpr (Parent_Expr, True)),
4643 Selector_Name => New_Occurrence_Of (Comp, Loc));
4646 Make_Component_Association (Loc,
4648 New_List (New_Occurrence_Of (Comp, Loc)),
4652 Analyze_And_Resolve (New_Comp, Etype (Comp));
4659 -- Compute the value for the Tag now, if the type is a root it
4660 -- will be included in the aggregate right away, otherwise it will
4661 -- be propagated to the parent aggregate
4663 if Present (Orig_Tag) then
4664 Tag_Value := Orig_Tag;
4668 Tag_Value := New_Occurrence_Of (Access_Disp_Table (Typ), Loc);
4671 -- For a derived type, an aggregate for the parent is formed with
4672 -- all the inherited components.
4674 if Is_Derived_Type (Typ) then
4677 First_Comp : Node_Id;
4678 Parent_Comps : List_Id;
4679 Parent_Aggr : Node_Id;
4680 Parent_Name : Node_Id;
4683 -- Remove the inherited component association from the
4684 -- aggregate and store them in the parent aggregate
4686 First_Comp := First (Component_Associations (N));
4687 Parent_Comps := New_List;
4689 while Present (First_Comp)
4690 and then Scope (Original_Record_Component (
4691 Entity (First (Choices (First_Comp))))) /= Base_Typ
4696 Append (Comp, Parent_Comps);
4699 Parent_Aggr := Make_Aggregate (Loc,
4700 Component_Associations => Parent_Comps);
4701 Set_Etype (Parent_Aggr, Etype (Base_Type (Typ)));
4703 -- Find the _parent component
4705 Comp := First_Component (Typ);
4706 while Chars (Comp) /= Name_uParent loop
4707 Comp := Next_Component (Comp);
4710 Parent_Name := New_Occurrence_Of (Comp, Loc);
4712 -- Insert the parent aggregate
4714 Prepend_To (Component_Associations (N),
4715 Make_Component_Association (Loc,
4716 Choices => New_List (Parent_Name),
4717 Expression => Parent_Aggr));
4719 -- Expand recursively the parent propagating the right Tag
4721 Expand_Record_Aggregate (
4722 Parent_Aggr, Tag_Value, Parent_Expr);
4725 -- For a root type, the tag component is added (unless compiling
4726 -- for the Java VM, where tags are implicit).
4728 elsif not Java_VM then
4730 Tag_Name : constant Node_Id :=
4731 New_Occurrence_Of (Tag_Component (Typ), Loc);
4732 Typ_Tag : constant Entity_Id := RTE (RE_Tag);
4733 Conv_Node : constant Node_Id :=
4734 Unchecked_Convert_To (Typ_Tag, Tag_Value);
4737 Set_Etype (Conv_Node, Typ_Tag);
4738 Prepend_To (Component_Associations (N),
4739 Make_Component_Association (Loc,
4740 Choices => New_List (Tag_Name),
4741 Expression => Conv_Node));
4746 end Expand_Record_Aggregate;
4748 ----------------------------
4749 -- Has_Default_Init_Comps --
4750 ----------------------------
4752 function Has_Default_Init_Comps (N : Node_Id) return Boolean is
4753 Comps : constant List_Id := Component_Associations (N);
4757 pragma Assert (Nkind (N) = N_Aggregate
4758 or else Nkind (N) = N_Extension_Aggregate);
4764 -- Check if any direct component has default initialized components
4767 while Present (C) loop
4768 if Box_Present (C) then
4775 -- Recursive call in case of aggregate expression
4778 while Present (C) loop
4779 Expr := Expression (C);
4782 and then (Nkind (Expr) = N_Aggregate
4783 or else Nkind (Expr) = N_Extension_Aggregate)
4784 and then Has_Default_Init_Comps (Expr)
4793 end Has_Default_Init_Comps;
4795 --------------------------
4796 -- Is_Delayed_Aggregate --
4797 --------------------------
4799 function Is_Delayed_Aggregate (N : Node_Id) return Boolean is
4800 Node : Node_Id := N;
4801 Kind : Node_Kind := Nkind (Node);
4804 if Kind = N_Qualified_Expression then
4805 Node := Expression (Node);
4806 Kind := Nkind (Node);
4809 if Kind /= N_Aggregate and then Kind /= N_Extension_Aggregate then
4812 return Expansion_Delayed (Node);
4814 end Is_Delayed_Aggregate;
4816 --------------------
4817 -- Late_Expansion --
4818 --------------------
4820 function Late_Expansion
4824 Flist : Node_Id := Empty;
4825 Obj : Entity_Id := Empty) return List_Id
4828 if Is_Record_Type (Etype (N)) then
4829 return Build_Record_Aggr_Code (N, Typ, Target, Flist, Obj);
4831 else pragma Assert (Is_Array_Type (Etype (N)));
4833 Build_Array_Aggr_Code
4835 Ctype => Component_Type (Etype (N)),
4836 Index => First_Index (Typ),
4838 Scalar_Comp => Is_Scalar_Type (Component_Type (Typ)),
4844 ----------------------------------
4845 -- Make_OK_Assignment_Statement --
4846 ----------------------------------
4848 function Make_OK_Assignment_Statement
4851 Expression : Node_Id) return Node_Id
4854 Set_Assignment_OK (Name);
4855 return Make_Assignment_Statement (Sloc, Name, Expression);
4856 end Make_OK_Assignment_Statement;
4858 -----------------------
4859 -- Number_Of_Choices --
4860 -----------------------
4862 function Number_Of_Choices (N : Node_Id) return Nat is
4866 Nb_Choices : Nat := 0;
4869 if Present (Expressions (N)) then
4873 Assoc := First (Component_Associations (N));
4874 while Present (Assoc) loop
4876 Choice := First (Choices (Assoc));
4877 while Present (Choice) loop
4879 if Nkind (Choice) /= N_Others_Choice then
4880 Nb_Choices := Nb_Choices + 1;
4890 end Number_Of_Choices;
4892 ------------------------------------
4893 -- Packed_Array_Aggregate_Handled --
4894 ------------------------------------
4896 -- The current version of this procedure will handle at compile time
4897 -- any array aggregate that meets these conditions:
4899 -- One dimensional, bit packed
4900 -- Underlying packed type is modular type
4901 -- Bounds are within 32-bit Int range
4902 -- All bounds and values are static
4904 function Packed_Array_Aggregate_Handled (N : Node_Id) return Boolean is
4905 Loc : constant Source_Ptr := Sloc (N);
4906 Typ : constant Entity_Id := Etype (N);
4907 Ctyp : constant Entity_Id := Component_Type (Typ);
4909 Not_Handled : exception;
4910 -- Exception raised if this aggregate cannot be handled
4913 -- For now, handle only one dimensional bit packed arrays
4915 if not Is_Bit_Packed_Array (Typ)
4916 or else Number_Dimensions (Typ) > 1
4917 or else not Is_Modular_Integer_Type (Packed_Array_Type (Typ))
4923 Csiz : constant Nat := UI_To_Int (Component_Size (Typ));
4927 -- Bounds of index type
4931 -- Values of bounds if compile time known
4933 function Get_Component_Val (N : Node_Id) return Uint;
4934 -- Given a expression value N of the component type Ctyp, returns
4935 -- A value of Csiz (component size) bits representing this value.
4936 -- If the value is non-static or any other reason exists why the
4937 -- value cannot be returned, then Not_Handled is raised.
4939 -----------------------
4940 -- Get_Component_Val --
4941 -----------------------
4943 function Get_Component_Val (N : Node_Id) return Uint is
4947 -- We have to analyze the expression here before doing any further
4948 -- processing here. The analysis of such expressions is deferred
4949 -- till expansion to prevent some problems of premature analysis.
4951 Analyze_And_Resolve (N, Ctyp);
4953 -- Must have a compile time value. String literals have to
4954 -- be converted into temporaries as well, because they cannot
4955 -- easily be converted into their bit representation.
4957 if not Compile_Time_Known_Value (N)
4958 or else Nkind (N) = N_String_Literal
4963 Val := Expr_Rep_Value (N);
4965 -- Adjust for bias, and strip proper number of bits
4967 if Has_Biased_Representation (Ctyp) then
4968 Val := Val - Expr_Value (Type_Low_Bound (Ctyp));
4971 return Val mod Uint_2 ** Csiz;
4972 end Get_Component_Val;
4974 -- Here we know we have a one dimensional bit packed array
4977 Get_Index_Bounds (First_Index (Typ), Lo, Hi);
4979 -- Cannot do anything if bounds are dynamic
4981 if not Compile_Time_Known_Value (Lo)
4983 not Compile_Time_Known_Value (Hi)
4988 -- Or are silly out of range of int bounds
4990 Lob := Expr_Value (Lo);
4991 Hib := Expr_Value (Hi);
4993 if not UI_Is_In_Int_Range (Lob)
4995 not UI_Is_In_Int_Range (Hib)
5000 -- At this stage we have a suitable aggregate for handling
5001 -- at compile time (the only remaining checks, are that the
5002 -- values of expressions in the aggregate are compile time
5003 -- known (check performed by Get_Component_Val), and that
5004 -- any subtypes or ranges are statically known.
5006 -- If the aggregate is not fully positional at this stage,
5007 -- then convert it to positional form. Either this will fail,
5008 -- in which case we can do nothing, or it will succeed, in
5009 -- which case we have succeeded in handling the aggregate,
5010 -- or it will stay an aggregate, in which case we have failed
5011 -- to handle this case.
5013 if Present (Component_Associations (N)) then
5014 Convert_To_Positional
5015 (N, Max_Others_Replicate => 64, Handle_Bit_Packed => True);
5016 return Nkind (N) /= N_Aggregate;
5019 -- Otherwise we are all positional, so convert to proper value
5022 Lov : constant Int := UI_To_Int (Lob);
5023 Hiv : constant Int := UI_To_Int (Hib);
5025 Len : constant Nat := Int'Max (0, Hiv - Lov + 1);
5026 -- The length of the array (number of elements)
5028 Aggregate_Val : Uint;
5029 -- Value of aggregate. The value is set in the low order
5030 -- bits of this value. For the little-endian case, the
5031 -- values are stored from low-order to high-order and
5032 -- for the big-endian case the values are stored from
5033 -- high-order to low-order. Note that gigi will take care
5034 -- of the conversions to left justify the value in the big
5035 -- endian case (because of left justified modular type
5036 -- processing), so we do not have to worry about that here.
5039 -- Integer literal for resulting constructed value
5042 -- Shift count from low order for next value
5045 -- Shift increment for loop
5048 -- Next expression from positional parameters of aggregate
5051 -- For little endian, we fill up the low order bits of the
5052 -- target value. For big endian we fill up the high order
5053 -- bits of the target value (which is a left justified
5056 if Bytes_Big_Endian xor Debug_Flag_8 then
5057 Shift := Csiz * (Len - 1);
5064 -- Loop to set the values
5067 Aggregate_Val := Uint_0;
5069 Expr := First (Expressions (N));
5070 Aggregate_Val := Get_Component_Val (Expr) * Uint_2 ** Shift;
5072 for J in 2 .. Len loop
5073 Shift := Shift + Incr;
5076 Aggregate_Val + Get_Component_Val (Expr) * Uint_2 ** Shift;
5080 -- Now we can rewrite with the proper value
5083 Make_Integer_Literal (Loc,
5084 Intval => Aggregate_Val);
5085 Set_Print_In_Hex (Lit);
5087 -- Construct the expression using this literal. Note that it is
5088 -- important to qualify the literal with its proper modular type
5089 -- since universal integer does not have the required range and
5090 -- also this is a left justified modular type, which is important
5091 -- in the big-endian case.
5094 Unchecked_Convert_To (Typ,
5095 Make_Qualified_Expression (Loc,
5097 New_Occurrence_Of (Packed_Array_Type (Typ), Loc),
5098 Expression => Lit)));
5100 Analyze_And_Resolve (N, Typ);
5108 end Packed_Array_Aggregate_Handled;
5110 ----------------------------
5111 -- Has_Mutable_Components --
5112 ----------------------------
5114 function Has_Mutable_Components (Typ : Entity_Id) return Boolean is
5118 Comp := First_Component (Typ);
5120 while Present (Comp) loop
5121 if Is_Record_Type (Etype (Comp))
5122 and then Has_Discriminants (Etype (Comp))
5123 and then not Is_Constrained (Etype (Comp))
5128 Next_Component (Comp);
5132 end Has_Mutable_Components;
5134 ------------------------------
5135 -- Initialize_Discriminants --
5136 ------------------------------
5138 procedure Initialize_Discriminants (N : Node_Id; Typ : Entity_Id) is
5139 Loc : constant Source_Ptr := Sloc (N);
5140 Bas : constant Entity_Id := Base_Type (Typ);
5141 Par : constant Entity_Id := Etype (Bas);
5142 Decl : constant Node_Id := Parent (Par);
5146 if Is_Tagged_Type (Bas)
5147 and then Is_Derived_Type (Bas)
5148 and then Has_Discriminants (Par)
5149 and then Has_Discriminants (Bas)
5150 and then Number_Discriminants (Bas) /= Number_Discriminants (Par)
5151 and then Nkind (Decl) = N_Full_Type_Declaration
5152 and then Nkind (Type_Definition (Decl)) = N_Record_Definition
5154 (Variant_Part (Component_List (Type_Definition (Decl))))
5155 and then Nkind (N) /= N_Extension_Aggregate
5158 -- Call init proc to set discriminants.
5159 -- There should eventually be a special procedure for this ???
5161 Ref := New_Reference_To (Defining_Identifier (N), Loc);
5162 Insert_Actions_After (N,
5163 Build_Initialization_Call (Sloc (N), Ref, Typ));
5165 end Initialize_Discriminants;
5167 ---------------------------
5168 -- Safe_Slice_Assignment --
5169 ---------------------------
5171 function Safe_Slice_Assignment (N : Node_Id) return Boolean is
5172 Loc : constant Source_Ptr := Sloc (Parent (N));
5173 Pref : constant Node_Id := Prefix (Name (Parent (N)));
5174 Range_Node : constant Node_Id := Discrete_Range (Name (Parent (N)));
5182 -- Generate: for J in Range loop Pref (J) := Expr; end loop;
5184 if Comes_From_Source (N)
5185 and then No (Expressions (N))
5186 and then Nkind (First (Choices (First (Component_Associations (N)))))
5190 Expression (First (Component_Associations (N)));
5191 L_J := Make_Defining_Identifier (Loc, New_Internal_Name ('J'));
5194 Make_Iteration_Scheme (Loc,
5195 Loop_Parameter_Specification =>
5196 Make_Loop_Parameter_Specification
5198 Defining_Identifier => L_J,
5199 Discrete_Subtype_Definition => Relocate_Node (Range_Node)));
5202 Make_Assignment_Statement (Loc,
5204 Make_Indexed_Component (Loc,
5205 Prefix => Relocate_Node (Pref),
5206 Expressions => New_List (New_Occurrence_Of (L_J, Loc))),
5207 Expression => Relocate_Node (Expr));
5209 -- Construct the final loop
5212 Make_Implicit_Loop_Statement
5213 (Node => Parent (N),
5214 Identifier => Empty,
5215 Iteration_Scheme => L_Iter,
5216 Statements => New_List (L_Body));
5218 -- Set type of aggregate to be type of lhs in assignment,
5219 -- to suppress redundant length checks.
5221 Set_Etype (N, Etype (Name (Parent (N))));
5223 Rewrite (Parent (N), Stat);
5224 Analyze (Parent (N));
5230 end Safe_Slice_Assignment;
5232 ---------------------
5233 -- Sort_Case_Table --
5234 ---------------------
5236 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type) is
5237 L : constant Int := Case_Table'First;
5238 U : constant Int := Case_Table'Last;
5247 T := Case_Table (K + 1);
5251 and then Expr_Value (Case_Table (J - 1).Choice_Lo) >
5252 Expr_Value (T.Choice_Lo)
5254 Case_Table (J) := Case_Table (J - 1);
5258 Case_Table (J) := T;
5261 end Sort_Case_Table;