1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2015, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Atree
; use Atree
;
27 with Checks
; use Checks
;
28 with Debug
; use Debug
;
29 with Einfo
; use Einfo
;
30 with Elists
; use Elists
;
31 with Errout
; use Errout
;
32 with Expander
; use Expander
;
33 with Exp_Util
; use Exp_Util
;
34 with Exp_Ch3
; use Exp_Ch3
;
35 with Exp_Ch6
; use Exp_Ch6
;
36 with Exp_Ch7
; use Exp_Ch7
;
37 with Exp_Ch9
; use Exp_Ch9
;
38 with Exp_Disp
; use Exp_Disp
;
39 with Exp_Tss
; use Exp_Tss
;
40 with Fname
; use Fname
;
41 with Freeze
; use Freeze
;
42 with Itypes
; use Itypes
;
44 with Namet
; use Namet
;
45 with Nmake
; use Nmake
;
46 with Nlists
; use Nlists
;
48 with Restrict
; use Restrict
;
49 with Rident
; use Rident
;
50 with Rtsfind
; use Rtsfind
;
51 with Ttypes
; use Ttypes
;
53 with Sem_Aggr
; use Sem_Aggr
;
54 with Sem_Aux
; use Sem_Aux
;
55 with Sem_Ch3
; use Sem_Ch3
;
56 with Sem_Eval
; use Sem_Eval
;
57 with Sem_Res
; use Sem_Res
;
58 with Sem_Util
; use Sem_Util
;
59 with Sinfo
; use Sinfo
;
60 with Snames
; use Snames
;
61 with Stand
; use Stand
;
62 with Stringt
; use Stringt
;
63 with Targparm
; use Targparm
;
64 with Tbuild
; use Tbuild
;
65 with Uintp
; use Uintp
;
67 package body Exp_Aggr
is
69 type Case_Bounds
is record
72 Choice_Node
: Node_Id
;
75 type Case_Table_Type
is array (Nat
range <>) of Case_Bounds
;
76 -- Table type used by Check_Case_Choices procedure
78 procedure Collect_Initialization_Statements
81 Node_After
: Node_Id
);
82 -- If Obj is not frozen, collect actions inserted after N until, but not
83 -- including, Node_After, for initialization of Obj, and move them to an
84 -- expression with actions, which becomes the Initialization_Statements for
87 function Has_Default_Init_Comps
(N
: Node_Id
) return Boolean;
88 -- N is an aggregate (record or array). Checks the presence of default
89 -- initialization (<>) in any component (Ada 2005: AI-287).
91 function Is_Static_Dispatch_Table_Aggregate
(N
: Node_Id
) return Boolean;
92 -- Returns true if N is an aggregate used to initialize the components
93 -- of a statically allocated dispatch table.
96 (Obj_Type
: Entity_Id
;
97 Typ
: Entity_Id
) return Boolean;
98 -- A static array aggregate in an object declaration can in most cases be
99 -- expanded in place. The one exception is when the aggregate is given
100 -- with component associations that specify different bounds from those of
101 -- the type definition in the object declaration. In this pathological
102 -- case the aggregate must slide, and we must introduce an intermediate
103 -- temporary to hold it.
105 -- The same holds in an assignment to one-dimensional array of arrays,
106 -- when a component may be given with bounds that differ from those of the
109 procedure Sort_Case_Table
(Case_Table
: in out Case_Table_Type
);
110 -- Sort the Case Table using the Lower Bound of each Choice as the key.
111 -- A simple insertion sort is used since the number of choices in a case
112 -- statement of variant part will usually be small and probably in near
115 ------------------------------------------------------
116 -- Local subprograms for Record Aggregate Expansion --
117 ------------------------------------------------------
119 function Build_Record_Aggr_Code
122 Lhs
: Node_Id
) return List_Id
;
123 -- N is an N_Aggregate or an N_Extension_Aggregate. Typ is the type of the
124 -- aggregate. Target is an expression containing the location on which the
125 -- component by component assignments will take place. Returns the list of
126 -- assignments plus all other adjustments needed for tagged and controlled
129 procedure Convert_To_Assignments
(N
: Node_Id
; Typ
: Entity_Id
);
130 -- N is an N_Aggregate or an N_Extension_Aggregate. Typ is the type of the
131 -- aggregate (which can only be a record type, this procedure is only used
132 -- for record types). Transform the given aggregate into a sequence of
133 -- assignments performed component by component.
135 procedure Expand_Record_Aggregate
137 Orig_Tag
: Node_Id
:= Empty
;
138 Parent_Expr
: Node_Id
:= Empty
);
139 -- This is the top level procedure for record aggregate expansion.
140 -- Expansion for record aggregates needs expand aggregates for tagged
141 -- record types. Specifically Expand_Record_Aggregate adds the Tag
142 -- field in front of the Component_Association list that was created
143 -- during resolution by Resolve_Record_Aggregate.
145 -- N is the record aggregate node.
146 -- Orig_Tag is the value of the Tag that has to be provided for this
147 -- specific aggregate. It carries the tag corresponding to the type
148 -- of the outermost aggregate during the recursive expansion
149 -- Parent_Expr is the ancestor part of the original extension
152 function Has_Mutable_Components
(Typ
: Entity_Id
) return Boolean;
153 -- Return true if one of the components is of a discriminated type with
154 -- defaults. An aggregate for a type with mutable components must be
155 -- expanded into individual assignments.
157 procedure Initialize_Discriminants
(N
: Node_Id
; Typ
: Entity_Id
);
158 -- If the type of the aggregate is a type extension with renamed discrimi-
159 -- nants, we must initialize the hidden discriminants of the parent.
160 -- Otherwise, the target object must not be initialized. The discriminants
161 -- are initialized by calling the initialization procedure for the type.
162 -- This is incorrect if the initialization of other components has any
163 -- side effects. We restrict this call to the case where the parent type
164 -- has a variant part, because this is the only case where the hidden
165 -- discriminants are accessed, namely when calling discriminant checking
166 -- functions of the parent type, and when applying a stream attribute to
167 -- an object of the derived type.
169 -----------------------------------------------------
170 -- Local Subprograms for Array Aggregate Expansion --
171 -----------------------------------------------------
173 function Aggr_Size_OK
(N
: Node_Id
; Typ
: Entity_Id
) return Boolean;
174 -- Very large static aggregates present problems to the back-end, and are
175 -- transformed into assignments and loops. This function verifies that the
176 -- total number of components of an aggregate is acceptable for rewriting
177 -- into a purely positional static form. Aggr_Size_OK must be called before
180 -- This function also detects and warns about one-component aggregates that
181 -- appear in a non-static context. Even if the component value is static,
182 -- such an aggregate must be expanded into an assignment.
184 function Backend_Processing_Possible
(N
: Node_Id
) return Boolean;
185 -- This function checks if array aggregate N can be processed directly
186 -- by the backend. If this is the case, True is returned.
188 function Build_Array_Aggr_Code
193 Scalar_Comp
: Boolean;
194 Indexes
: List_Id
:= No_List
) return List_Id
;
195 -- This recursive routine returns a list of statements containing the
196 -- loops and assignments that are needed for the expansion of the array
199 -- N is the (sub-)aggregate node to be expanded into code. This node has
200 -- been fully analyzed, and its Etype is properly set.
202 -- Index is the index node corresponding to the array sub-aggregate N
204 -- Into is the target expression into which we are copying the aggregate.
205 -- Note that this node may not have been analyzed yet, and so the Etype
206 -- field may not be set.
208 -- Scalar_Comp is True if the component type of the aggregate is scalar
210 -- Indexes is the current list of expressions used to index the object we
213 procedure Convert_Array_Aggr_In_Allocator
217 -- If the aggregate appears within an allocator and can be expanded in
218 -- place, this routine generates the individual assignments to components
219 -- of the designated object. This is an optimization over the general
220 -- case, where a temporary is first created on the stack and then used to
221 -- construct the allocated object on the heap.
223 procedure Convert_To_Positional
225 Max_Others_Replicate
: Nat
:= 5;
226 Handle_Bit_Packed
: Boolean := False);
227 -- If possible, convert named notation to positional notation. This
228 -- conversion is possible only in some static cases. If the conversion is
229 -- possible, then N is rewritten with the analyzed converted aggregate.
230 -- The parameter Max_Others_Replicate controls the maximum number of
231 -- values corresponding to an others choice that will be converted to
232 -- positional notation (the default of 5 is the normal limit, and reflects
233 -- the fact that normally the loop is better than a lot of separate
234 -- assignments). Note that this limit gets overridden in any case if
235 -- either of the restrictions No_Elaboration_Code or No_Implicit_Loops is
236 -- set. The parameter Handle_Bit_Packed is usually set False (since we do
237 -- not expect the back end to handle bit packed arrays, so the normal case
238 -- of conversion is pointless), but in the special case of a call from
239 -- Packed_Array_Aggregate_Handled, we set this parameter to True, since
240 -- these are cases we handle in there.
242 -- It would seem useful to have a higher default for Max_Others_Replicate,
243 -- but aggregates in the compiler make this impossible: the compiler
244 -- bootstrap fails if Max_Others_Replicate is greater than 25. This
247 procedure Expand_Array_Aggregate
(N
: Node_Id
);
248 -- This is the top-level routine to perform array aggregate expansion.
249 -- N is the N_Aggregate node to be expanded.
251 function Is_Two_Dim_Packed_Array
(Typ
: Entity_Id
) return Boolean;
252 -- For two-dimensional packed aggregates with constant bounds and constant
253 -- components, it is preferable to pack the inner aggregates because the
254 -- whole matrix can then be presented to the back-end as a one-dimensional
255 -- list of literals. This is much more efficient than expanding into single
256 -- component assignments. This function determines if the type Typ is for
257 -- an array that is suitable for this optimization: it returns True if Typ
258 -- is a two dimensional bit packed array with component size 1, 2, or 4.
260 function Late_Expansion
263 Target
: Node_Id
) return List_Id
;
264 -- This routine implements top-down expansion of nested aggregates. In
265 -- doing so, it avoids the generation of temporaries at each level. N is
266 -- a nested record or array aggregate with the Expansion_Delayed flag.
267 -- Typ is the expected type of the aggregate. Target is a (duplicatable)
268 -- expression that will hold the result of the aggregate expansion.
270 function Make_OK_Assignment_Statement
273 Expression
: Node_Id
) return Node_Id
;
274 -- This is like Make_Assignment_Statement, except that Assignment_OK
275 -- is set in the left operand. All assignments built by this unit use
276 -- this routine. This is needed to deal with assignments to initialized
277 -- constants that are done in place.
279 function Number_Of_Choices
(N
: Node_Id
) return Nat
;
280 -- Returns the number of discrete choices (not including the others choice
281 -- if present) contained in (sub-)aggregate N.
283 function Packed_Array_Aggregate_Handled
(N
: Node_Id
) return Boolean;
284 -- Given an array aggregate, this function handles the case of a packed
285 -- array aggregate with all constant values, where the aggregate can be
286 -- evaluated at compile time. If this is possible, then N is rewritten
287 -- to be its proper compile time value with all the components properly
288 -- assembled. The expression is analyzed and resolved and True is returned.
289 -- If this transformation is not possible, N is unchanged and False is
292 function Two_Dim_Packed_Array_Handled
(N
: Node_Id
) return Boolean;
293 -- If the type of the aggregate is a two-dimensional bit_packed array
294 -- it may be transformed into an array of bytes with constant values,
295 -- and presented to the back-end as a static value. The function returns
296 -- false if this transformation cannot be performed. THis is similar to,
297 -- and reuses part of the machinery in Packed_Array_Aggregate_Handled.
303 function Aggr_Size_OK
(N
: Node_Id
; Typ
: Entity_Id
) return Boolean is
312 -- Determines the maximum size of an array aggregate produced by
313 -- converting named to positional notation (e.g. from others clauses).
314 -- This avoids running away with attempts to convert huge aggregates,
315 -- which hit memory limits in the backend.
317 function Component_Count
(T
: Entity_Id
) return Int
;
318 -- The limit is applied to the total number of components that the
319 -- aggregate will have, which is the number of static expressions
320 -- that will appear in the flattened array. This requires a recursive
321 -- computation of the number of scalar components of the structure.
323 ---------------------
324 -- Component_Count --
325 ---------------------
327 function Component_Count
(T
: Entity_Id
) return Int
is
332 if Is_Scalar_Type
(T
) then
335 elsif Is_Record_Type
(T
) then
336 Comp
:= First_Component
(T
);
337 while Present
(Comp
) loop
338 Res
:= Res
+ Component_Count
(Etype
(Comp
));
339 Next_Component
(Comp
);
344 elsif Is_Array_Type
(T
) then
346 Lo
: constant Node_Id
:=
347 Type_Low_Bound
(Etype
(First_Index
(T
)));
348 Hi
: constant Node_Id
:=
349 Type_High_Bound
(Etype
(First_Index
(T
)));
351 Siz
: constant Int
:= Component_Count
(Component_Type
(T
));
354 if not Compile_Time_Known_Value
(Lo
)
355 or else not Compile_Time_Known_Value
(Hi
)
360 Siz
* UI_To_Int
(Expr_Value
(Hi
) - Expr_Value
(Lo
) + 1);
365 -- Can only be a null for an access type
371 -- Start of processing for Aggr_Size_OK
374 -- The normal aggregate limit is 50000, but we increase this limit to
375 -- 2**24 (about 16 million) if Restrictions (No_Elaboration_Code) or
376 -- Restrictions (No_Implicit_Loops) is specified, since in either case
377 -- we are at risk of declaring the program illegal because of this
378 -- limit. We also increase the limit when Static_Elaboration_Desired,
379 -- given that this means that objects are intended to be placed in data
382 -- We also increase the limit if the aggregate is for a packed two-
383 -- dimensional array, because if components are static it is much more
384 -- efficient to construct a one-dimensional equivalent array with static
387 -- Conversely, we decrease the maximum size if none of the above
388 -- requirements apply, and if the aggregate has a single component
389 -- association, which will be more efficient if implemented with a loop.
391 -- Finally, we use a small limit in CodePeer mode where we favor loops
392 -- instead of thousands of single assignments (from large aggregates).
394 Max_Aggr_Size
:= 50000;
396 if CodePeer_Mode
then
397 Max_Aggr_Size
:= 100;
399 elsif Restriction_Active
(No_Elaboration_Code
)
400 or else Restriction_Active
(No_Implicit_Loops
)
401 or else Is_Two_Dim_Packed_Array
(Typ
)
402 or else (Ekind
(Current_Scope
) = E_Package
403 and then Static_Elaboration_Desired
(Current_Scope
))
405 Max_Aggr_Size
:= 2 ** 24;
407 elsif No
(Expressions
(N
))
408 and then No
(Next
(First
(Component_Associations
(N
))))
410 Max_Aggr_Size
:= 5000;
413 Siz
:= Component_Count
(Component_Type
(Typ
));
415 Indx
:= First_Index
(Typ
);
416 while Present
(Indx
) loop
417 Lo
:= Type_Low_Bound
(Etype
(Indx
));
418 Hi
:= Type_High_Bound
(Etype
(Indx
));
420 -- Bounds need to be known at compile time
422 if not Compile_Time_Known_Value
(Lo
)
423 or else not Compile_Time_Known_Value
(Hi
)
428 Lov
:= Expr_Value
(Lo
);
429 Hiv
:= Expr_Value
(Hi
);
431 -- A flat array is always safe
437 -- One-component aggregates are suspicious, and if the context type
438 -- is an object declaration with non-static bounds it will trip gcc;
439 -- such an aggregate must be expanded into a single assignment.
441 if Hiv
= Lov
and then Nkind
(Parent
(N
)) = N_Object_Declaration
then
443 Index_Type
: constant Entity_Id
:=
445 (First_Index
(Etype
(Defining_Identifier
(Parent
(N
)))));
449 if not Compile_Time_Known_Value
(Type_Low_Bound
(Index_Type
))
450 or else not Compile_Time_Known_Value
451 (Type_High_Bound
(Index_Type
))
453 if Present
(Component_Associations
(N
)) then
455 First
(Choices
(First
(Component_Associations
(N
))));
457 if Is_Entity_Name
(Indx
)
458 and then not Is_Type
(Entity
(Indx
))
461 ("single component aggregate in "
462 & "non-static context??", Indx
);
463 Error_Msg_N
("\maybe subtype name was meant??", Indx
);
473 Rng
: constant Uint
:= Hiv
- Lov
+ 1;
476 -- Check if size is too large
478 if not UI_Is_In_Int_Range
(Rng
) then
482 Siz
:= Siz
* UI_To_Int
(Rng
);
486 or else Siz
> Max_Aggr_Size
491 -- Bounds must be in integer range, for later array construction
493 if not UI_Is_In_Int_Range
(Lov
)
495 not UI_Is_In_Int_Range
(Hiv
)
506 ---------------------------------
507 -- Backend_Processing_Possible --
508 ---------------------------------
510 -- Backend processing by Gigi/gcc is possible only if all the following
511 -- conditions are met:
513 -- 1. N is fully positional
515 -- 2. N is not a bit-packed array aggregate;
517 -- 3. The size of N's array type must be known at compile time. Note
518 -- that this implies that the component size is also known
520 -- 4. The array type of N does not follow the Fortran layout convention
521 -- or if it does it must be 1 dimensional.
523 -- 5. The array component type may not be tagged (which could necessitate
524 -- reassignment of proper tags).
526 -- 6. The array component type must not have unaligned bit components
528 -- 7. None of the components of the aggregate may be bit unaligned
531 -- 8. There cannot be delayed components, since we do not know enough
532 -- at this stage to know if back end processing is possible.
534 -- 9. There cannot be any discriminated record components, since the
535 -- back end cannot handle this complex case.
537 -- 10. No controlled actions need to be generated for components
539 -- 11. For a VM back end, the array should have no aliased components
541 function Backend_Processing_Possible
(N
: Node_Id
) return Boolean is
542 Typ
: constant Entity_Id
:= Etype
(N
);
543 -- Typ is the correct constrained array subtype of the aggregate
545 function Component_Check
(N
: Node_Id
; Index
: Node_Id
) return Boolean;
546 -- This routine checks components of aggregate N, enforcing checks
547 -- 1, 7, 8, and 9. In the multi-dimensional case, these checks are
548 -- performed on subaggregates. The Index value is the current index
549 -- being checked in the multi-dimensional case.
551 ---------------------
552 -- Component_Check --
553 ---------------------
555 function Component_Check
(N
: Node_Id
; Index
: Node_Id
) return Boolean is
559 -- Checks 1: (no component associations)
561 if Present
(Component_Associations
(N
)) then
565 -- Checks on components
567 -- Recurse to check subaggregates, which may appear in qualified
568 -- expressions. If delayed, the front-end will have to expand.
569 -- If the component is a discriminated record, treat as non-static,
570 -- as the back-end cannot handle this properly.
572 Expr
:= First
(Expressions
(N
));
573 while Present
(Expr
) loop
575 -- Checks 8: (no delayed components)
577 if Is_Delayed_Aggregate
(Expr
) then
581 -- Checks 9: (no discriminated records)
583 if Present
(Etype
(Expr
))
584 and then Is_Record_Type
(Etype
(Expr
))
585 and then Has_Discriminants
(Etype
(Expr
))
590 -- Checks 7. Component must not be bit aligned component
592 if Possible_Bit_Aligned_Component
(Expr
) then
596 -- Recursion to following indexes for multiple dimension case
598 if Present
(Next_Index
(Index
))
599 and then not Component_Check
(Expr
, Next_Index
(Index
))
604 -- All checks for that component finished, on to next
612 -- Start of processing for Backend_Processing_Possible
615 -- Checks 2 (array not bit packed) and 10 (no controlled actions)
617 if Is_Bit_Packed_Array
(Typ
) or else Needs_Finalization
(Typ
) then
621 -- If component is limited, aggregate must be expanded because each
622 -- component assignment must be built in place.
624 if Is_Limited_View
(Component_Type
(Typ
)) then
628 -- Checks 4 (array must not be multi-dimensional Fortran case)
630 if Convention
(Typ
) = Convention_Fortran
631 and then Number_Dimensions
(Typ
) > 1
636 -- Checks 3 (size of array must be known at compile time)
638 if not Size_Known_At_Compile_Time
(Typ
) then
642 -- Checks on components
644 if not Component_Check
(N
, First_Index
(Typ
)) then
648 -- Checks 5 (if the component type is tagged, then we may need to do
649 -- tag adjustments. Perhaps this should be refined to check for any
650 -- component associations that actually need tag adjustment, similar
651 -- to the test in Component_Not_OK_For_Backend for record aggregates
652 -- with tagged components, but not clear whether it's worthwhile ???;
653 -- in the case of the JVM, object tags are handled implicitly)
655 if Is_Tagged_Type
(Component_Type
(Typ
))
656 and then Tagged_Type_Expansion
661 -- Checks 6 (component type must not have bit aligned components)
663 if Type_May_Have_Bit_Aligned_Components
(Component_Type
(Typ
)) then
667 -- Checks 11: Array aggregates with aliased components are currently
668 -- not well supported by the VM backend; disable temporarily this
669 -- backend processing until it is definitely supported.
671 if VM_Target
/= No_VM
672 and then Has_Aliased_Components
(Base_Type
(Typ
))
677 -- Backend processing is possible
679 Set_Size_Known_At_Compile_Time
(Etype
(N
), True);
681 end Backend_Processing_Possible
;
683 ---------------------------
684 -- Build_Array_Aggr_Code --
685 ---------------------------
687 -- The code that we generate from a one dimensional aggregate is
689 -- 1. If the sub-aggregate contains discrete choices we
691 -- (a) Sort the discrete choices
693 -- (b) Otherwise for each discrete choice that specifies a range we
694 -- emit a loop. If a range specifies a maximum of three values, or
695 -- we are dealing with an expression we emit a sequence of
696 -- assignments instead of a loop.
698 -- (c) Generate the remaining loops to cover the others choice if any
700 -- 2. If the aggregate contains positional elements we
702 -- (a) translate the positional elements in a series of assignments
704 -- (b) Generate a final loop to cover the others choice if any.
705 -- Note that this final loop has to be a while loop since the case
707 -- L : Integer := Integer'Last;
708 -- H : Integer := Integer'Last;
709 -- A : array (L .. H) := (1, others =>0);
711 -- cannot be handled by a for loop. Thus for the following
713 -- array (L .. H) := (.. positional elements.., others =>E);
715 -- we always generate something like:
717 -- J : Index_Type := Index_Of_Last_Positional_Element;
719 -- J := Index_Base'Succ (J)
723 function Build_Array_Aggr_Code
728 Scalar_Comp
: Boolean;
729 Indexes
: List_Id
:= No_List
) return List_Id
731 Loc
: constant Source_Ptr
:= Sloc
(N
);
732 Index_Base
: constant Entity_Id
:= Base_Type
(Etype
(Index
));
733 Index_Base_L
: constant Node_Id
:= Type_Low_Bound
(Index_Base
);
734 Index_Base_H
: constant Node_Id
:= Type_High_Bound
(Index_Base
);
736 function Add
(Val
: Int
; To
: Node_Id
) return Node_Id
;
737 -- Returns an expression where Val is added to expression To, unless
738 -- To+Val is provably out of To's base type range. To must be an
739 -- already analyzed expression.
741 function Empty_Range
(L
, H
: Node_Id
) return Boolean;
742 -- Returns True if the range defined by L .. H is certainly empty
744 function Equal
(L
, H
: Node_Id
) return Boolean;
745 -- Returns True if L = H for sure
747 function Index_Base_Name
return Node_Id
;
748 -- Returns a new reference to the index type name
750 function Gen_Assign
(Ind
: Node_Id
; Expr
: Node_Id
) return List_Id
;
751 -- Ind must be a side-effect free expression. If the input aggregate
752 -- N to Build_Loop contains no sub-aggregates, then this function
753 -- returns the assignment statement:
755 -- Into (Indexes, Ind) := Expr;
757 -- Otherwise we call Build_Code recursively
759 -- Ada 2005 (AI-287): In case of default initialized component, Expr
760 -- is empty and we generate a call to the corresponding IP subprogram.
762 function Gen_Loop
(L
, H
: Node_Id
; Expr
: Node_Id
) return List_Id
;
763 -- Nodes L and H must be side-effect free expressions.
764 -- If the input aggregate N to Build_Loop contains no sub-aggregates,
765 -- This routine returns the for loop statement
767 -- for J in Index_Base'(L) .. Index_Base'(H) loop
768 -- Into (Indexes, J) := Expr;
771 -- Otherwise we call Build_Code recursively.
772 -- As an optimization if the loop covers 3 or less scalar elements we
773 -- generate a sequence of assignments.
775 function Gen_While
(L
, H
: Node_Id
; Expr
: Node_Id
) return List_Id
;
776 -- Nodes L and H must be side-effect free expressions.
777 -- If the input aggregate N to Build_Loop contains no sub-aggregates,
778 -- This routine returns the while loop statement
780 -- J : Index_Base := L;
782 -- J := Index_Base'Succ (J);
783 -- Into (Indexes, J) := Expr;
786 -- Otherwise we call Build_Code recursively
788 function Get_Assoc_Expr
(Assoc
: Node_Id
) return Node_Id
;
789 -- For an association with a box, use value given by aspect
790 -- Default_Component_Value of array type if specified, else use
791 -- value given by aspect Default_Value for component type itself
792 -- if specified, else return Empty.
794 function Local_Compile_Time_Known_Value
(E
: Node_Id
) return Boolean;
795 function Local_Expr_Value
(E
: Node_Id
) return Uint
;
796 -- These two Local routines are used to replace the corresponding ones
797 -- in sem_eval because while processing the bounds of an aggregate with
798 -- discrete choices whose index type is an enumeration, we build static
799 -- expressions not recognized by Compile_Time_Known_Value as such since
800 -- they have not yet been analyzed and resolved. All the expressions in
801 -- question are things like Index_Base_Name'Val (Const) which we can
802 -- easily recognize as being constant.
808 function Add
(Val
: Int
; To
: Node_Id
) return Node_Id
is
813 U_Val
: constant Uint
:= UI_From_Int
(Val
);
816 -- Note: do not try to optimize the case of Val = 0, because
817 -- we need to build a new node with the proper Sloc value anyway.
819 -- First test if we can do constant folding
821 if Local_Compile_Time_Known_Value
(To
) then
822 U_To
:= Local_Expr_Value
(To
) + Val
;
824 -- Determine if our constant is outside the range of the index.
825 -- If so return an Empty node. This empty node will be caught
826 -- by Empty_Range below.
828 if Compile_Time_Known_Value
(Index_Base_L
)
829 and then U_To
< Expr_Value
(Index_Base_L
)
833 elsif Compile_Time_Known_Value
(Index_Base_H
)
834 and then U_To
> Expr_Value
(Index_Base_H
)
839 Expr_Pos
:= Make_Integer_Literal
(Loc
, U_To
);
840 Set_Is_Static_Expression
(Expr_Pos
);
842 if not Is_Enumeration_Type
(Index_Base
) then
845 -- If we are dealing with enumeration return
846 -- Index_Base'Val (Expr_Pos)
850 Make_Attribute_Reference
852 Prefix
=> Index_Base_Name
,
853 Attribute_Name
=> Name_Val
,
854 Expressions
=> New_List
(Expr_Pos
));
860 -- If we are here no constant folding possible
862 if not Is_Enumeration_Type
(Index_Base
) then
865 Left_Opnd
=> Duplicate_Subexpr
(To
),
866 Right_Opnd
=> Make_Integer_Literal
(Loc
, U_Val
));
868 -- If we are dealing with enumeration return
869 -- Index_Base'Val (Index_Base'Pos (To) + Val)
873 Make_Attribute_Reference
875 Prefix
=> Index_Base_Name
,
876 Attribute_Name
=> Name_Pos
,
877 Expressions
=> New_List
(Duplicate_Subexpr
(To
)));
882 Right_Opnd
=> Make_Integer_Literal
(Loc
, U_Val
));
885 Make_Attribute_Reference
887 Prefix
=> Index_Base_Name
,
888 Attribute_Name
=> Name_Val
,
889 Expressions
=> New_List
(Expr_Pos
));
899 function Empty_Range
(L
, H
: Node_Id
) return Boolean is
900 Is_Empty
: Boolean := False;
905 -- First check if L or H were already detected as overflowing the
906 -- index base range type by function Add above. If this is so Add
907 -- returns the empty node.
909 if No
(L
) or else No
(H
) then
916 -- L > H range is empty
922 -- B_L > H range must be empty
928 -- L > B_H range must be empty
932 High
:= Index_Base_H
;
935 if Local_Compile_Time_Known_Value
(Low
)
937 Local_Compile_Time_Known_Value
(High
)
940 UI_Gt
(Local_Expr_Value
(Low
), Local_Expr_Value
(High
));
953 function Equal
(L
, H
: Node_Id
) return Boolean is
958 elsif Local_Compile_Time_Known_Value
(L
)
960 Local_Compile_Time_Known_Value
(H
)
962 return UI_Eq
(Local_Expr_Value
(L
), Local_Expr_Value
(H
));
972 function Gen_Assign
(Ind
: Node_Id
; Expr
: Node_Id
) return List_Id
is
973 L
: constant List_Id
:= New_List
;
976 New_Indexes
: List_Id
;
977 Indexed_Comp
: Node_Id
;
979 Comp_Type
: Entity_Id
:= Empty
;
981 function Add_Loop_Actions
(Lis
: List_Id
) return List_Id
;
982 -- Collect insert_actions generated in the construction of a
983 -- loop, and prepend them to the sequence of assignments to
984 -- complete the eventual body of the loop.
986 ----------------------
987 -- Add_Loop_Actions --
988 ----------------------
990 function Add_Loop_Actions
(Lis
: List_Id
) return List_Id
is
994 -- Ada 2005 (AI-287): Do nothing else in case of default
995 -- initialized component.
1000 elsif Nkind
(Parent
(Expr
)) = N_Component_Association
1001 and then Present
(Loop_Actions
(Parent
(Expr
)))
1003 Append_List
(Lis
, Loop_Actions
(Parent
(Expr
)));
1004 Res
:= Loop_Actions
(Parent
(Expr
));
1005 Set_Loop_Actions
(Parent
(Expr
), No_List
);
1011 end Add_Loop_Actions
;
1013 -- Start of processing for Gen_Assign
1016 if No
(Indexes
) then
1017 New_Indexes
:= New_List
;
1019 New_Indexes
:= New_Copy_List_Tree
(Indexes
);
1022 Append_To
(New_Indexes
, Ind
);
1024 if Present
(Next_Index
(Index
)) then
1027 Build_Array_Aggr_Code
1030 Index
=> Next_Index
(Index
),
1032 Scalar_Comp
=> Scalar_Comp
,
1033 Indexes
=> New_Indexes
));
1036 -- If we get here then we are at a bottom-level (sub-)aggregate
1040 (Make_Indexed_Component
(Loc
,
1041 Prefix
=> New_Copy_Tree
(Into
),
1042 Expressions
=> New_Indexes
));
1044 Set_Assignment_OK
(Indexed_Comp
);
1046 -- Ada 2005 (AI-287): In case of default initialized component, Expr
1047 -- is not present (and therefore we also initialize Expr_Q to empty).
1051 elsif Nkind
(Expr
) = N_Qualified_Expression
then
1052 Expr_Q
:= Expression
(Expr
);
1057 if Present
(Etype
(N
)) and then Etype
(N
) /= Any_Composite
then
1058 Comp_Type
:= Component_Type
(Etype
(N
));
1059 pragma Assert
(Comp_Type
= Ctype
); -- AI-287
1061 elsif Present
(Next
(First
(New_Indexes
))) then
1063 -- Ada 2005 (AI-287): Do nothing in case of default initialized
1064 -- component because we have received the component type in
1065 -- the formal parameter Ctype.
1067 -- ??? Some assert pragmas have been added to check if this new
1068 -- formal can be used to replace this code in all cases.
1070 if Present
(Expr
) then
1072 -- This is a multidimensional array. Recover the component type
1073 -- from the outermost aggregate, because subaggregates do not
1074 -- have an assigned type.
1081 while Present
(P
) loop
1082 if Nkind
(P
) = N_Aggregate
1083 and then Present
(Etype
(P
))
1085 Comp_Type
:= Component_Type
(Etype
(P
));
1093 pragma Assert
(Comp_Type
= Ctype
); -- AI-287
1098 -- Ada 2005 (AI-287): We only analyze the expression in case of non-
1099 -- default initialized components (otherwise Expr_Q is not present).
1102 and then Nkind_In
(Expr_Q
, N_Aggregate
, N_Extension_Aggregate
)
1104 -- At this stage the Expression may not have been analyzed yet
1105 -- because the array aggregate code has not been updated to use
1106 -- the Expansion_Delayed flag and avoid analysis altogether to
1107 -- solve the same problem (see Resolve_Aggr_Expr). So let us do
1108 -- the analysis of non-array aggregates now in order to get the
1109 -- value of Expansion_Delayed flag for the inner aggregate ???
1111 if Present
(Comp_Type
) and then not Is_Array_Type
(Comp_Type
) then
1112 Analyze_And_Resolve
(Expr_Q
, Comp_Type
);
1115 if Is_Delayed_Aggregate
(Expr_Q
) then
1117 -- This is either a subaggregate of a multidimensional array,
1118 -- or a component of an array type whose component type is
1119 -- also an array. In the latter case, the expression may have
1120 -- component associations that provide different bounds from
1121 -- those of the component type, and sliding must occur. Instead
1122 -- of decomposing the current aggregate assignment, force the
1123 -- re-analysis of the assignment, so that a temporary will be
1124 -- generated in the usual fashion, and sliding will take place.
1126 if Nkind
(Parent
(N
)) = N_Assignment_Statement
1127 and then Is_Array_Type
(Comp_Type
)
1128 and then Present
(Component_Associations
(Expr_Q
))
1129 and then Must_Slide
(Comp_Type
, Etype
(Expr_Q
))
1131 Set_Expansion_Delayed
(Expr_Q
, False);
1132 Set_Analyzed
(Expr_Q
, False);
1137 Late_Expansion
(Expr_Q
, Etype
(Expr_Q
), Indexed_Comp
));
1142 -- Ada 2005 (AI-287): In case of default initialized component, call
1143 -- the initialization subprogram associated with the component type.
1144 -- If the component type is an access type, add an explicit null
1145 -- assignment, because for the back-end there is an initialization
1146 -- present for the whole aggregate, and no default initialization
1149 -- In addition, if the component type is controlled, we must call
1150 -- its Initialize procedure explicitly, because there is no explicit
1151 -- object creation that will invoke it otherwise.
1154 if Present
(Base_Init_Proc
(Base_Type
(Ctype
)))
1155 or else Has_Task
(Base_Type
(Ctype
))
1158 Build_Initialization_Call
(Loc
,
1159 Id_Ref
=> Indexed_Comp
,
1161 With_Default_Init
=> True));
1163 elsif Is_Access_Type
(Ctype
) then
1165 Make_Assignment_Statement
(Loc
,
1166 Name
=> Indexed_Comp
,
1167 Expression
=> Make_Null
(Loc
)));
1170 if Needs_Finalization
(Ctype
) then
1173 (Obj_Ref
=> New_Copy_Tree
(Indexed_Comp
),
1179 Make_OK_Assignment_Statement
(Loc
,
1180 Name
=> Indexed_Comp
,
1181 Expression
=> New_Copy_Tree
(Expr
));
1183 -- The target of the assignment may not have been initialized,
1184 -- so it is not possible to call Finalize as expected in normal
1185 -- controlled assignments. We must also avoid using the primitive
1186 -- _assign (which depends on a valid target, and may for example
1187 -- perform discriminant checks on it).
1189 -- Both Finalize and usage of _assign are disabled by setting
1190 -- No_Ctrl_Actions on the assignment. The rest of the controlled
1191 -- actions are done manually with the proper finalization list
1192 -- coming from the context.
1194 Set_No_Ctrl_Actions
(A
);
1196 -- If this is an aggregate for an array of arrays, each
1197 -- sub-aggregate will be expanded as well, and even with
1198 -- No_Ctrl_Actions the assignments of inner components will
1199 -- require attachment in their assignments to temporaries. These
1200 -- temporaries must be finalized for each subaggregate, to prevent
1201 -- multiple attachments of the same temporary location to same
1202 -- finalization chain (and consequently circular lists). To ensure
1203 -- that finalization takes place for each subaggregate we wrap the
1204 -- assignment in a block.
1206 if Present
(Comp_Type
)
1207 and then Needs_Finalization
(Comp_Type
)
1208 and then Is_Array_Type
(Comp_Type
)
1209 and then Present
(Expr
)
1212 Make_Block_Statement
(Loc
,
1213 Handled_Statement_Sequence
=>
1214 Make_Handled_Sequence_Of_Statements
(Loc
,
1215 Statements
=> New_List
(A
)));
1220 -- Adjust the tag if tagged (because of possible view
1221 -- conversions), unless compiling for a VM where tags
1224 if Present
(Comp_Type
)
1225 and then Is_Tagged_Type
(Comp_Type
)
1226 and then Tagged_Type_Expansion
1229 Full_Typ
: constant Entity_Id
:= Underlying_Type
(Comp_Type
);
1233 Make_OK_Assignment_Statement
(Loc
,
1235 Make_Selected_Component
(Loc
,
1236 Prefix
=> New_Copy_Tree
(Indexed_Comp
),
1239 (First_Tag_Component
(Full_Typ
), Loc
)),
1242 Unchecked_Convert_To
(RTE
(RE_Tag
),
1244 (Node
(First_Elmt
(Access_Disp_Table
(Full_Typ
))),
1251 -- Adjust and attach the component to the proper final list, which
1252 -- can be the controller of the outer record object or the final
1253 -- list associated with the scope.
1255 -- If the component is itself an array of controlled types, whose
1256 -- value is given by a sub-aggregate, then the attach calls have
1257 -- been generated when individual subcomponent are assigned, and
1258 -- must not be done again to prevent malformed finalization chains
1259 -- (see comments above, concerning the creation of a block to hold
1260 -- inner finalization actions).
1262 if Present
(Comp_Type
)
1263 and then Needs_Finalization
(Comp_Type
)
1264 and then not Is_Limited_Type
(Comp_Type
)
1266 (Is_Array_Type
(Comp_Type
)
1267 and then Is_Controlled
(Component_Type
(Comp_Type
))
1268 and then Nkind
(Expr
) = N_Aggregate
)
1272 (Obj_Ref
=> New_Copy_Tree
(Indexed_Comp
),
1277 return Add_Loop_Actions
(L
);
1284 function Gen_Loop
(L
, H
: Node_Id
; Expr
: Node_Id
) return List_Id
is
1294 -- Index_Base'(L) .. Index_Base'(H)
1296 L_Iteration_Scheme
: Node_Id
;
1297 -- L_J in Index_Base'(L) .. Index_Base'(H)
1300 -- The statements to execute in the loop
1302 S
: constant List_Id
:= New_List
;
1303 -- List of statements
1306 -- Copy of expression tree, used for checking purposes
1309 -- If loop bounds define an empty range return the null statement
1311 if Empty_Range
(L
, H
) then
1312 Append_To
(S
, Make_Null_Statement
(Loc
));
1314 -- Ada 2005 (AI-287): Nothing else need to be done in case of
1315 -- default initialized component.
1321 -- The expression must be type-checked even though no component
1322 -- of the aggregate will have this value. This is done only for
1323 -- actual components of the array, not for subaggregates. Do
1324 -- the check on a copy, because the expression may be shared
1325 -- among several choices, some of which might be non-null.
1327 if Present
(Etype
(N
))
1328 and then Is_Array_Type
(Etype
(N
))
1329 and then No
(Next_Index
(Index
))
1331 Expander_Mode_Save_And_Set
(False);
1332 Tcopy
:= New_Copy_Tree
(Expr
);
1333 Set_Parent
(Tcopy
, N
);
1334 Analyze_And_Resolve
(Tcopy
, Component_Type
(Etype
(N
)));
1335 Expander_Mode_Restore
;
1341 -- If loop bounds are the same then generate an assignment
1343 elsif Equal
(L
, H
) then
1344 return Gen_Assign
(New_Copy_Tree
(L
), Expr
);
1346 -- If H - L <= 2 then generate a sequence of assignments when we are
1347 -- processing the bottom most aggregate and it contains scalar
1350 elsif No
(Next_Index
(Index
))
1351 and then Scalar_Comp
1352 and then Local_Compile_Time_Known_Value
(L
)
1353 and then Local_Compile_Time_Known_Value
(H
)
1354 and then Local_Expr_Value
(H
) - Local_Expr_Value
(L
) <= 2
1357 Append_List_To
(S
, Gen_Assign
(New_Copy_Tree
(L
), Expr
));
1358 Append_List_To
(S
, Gen_Assign
(Add
(1, To
=> L
), Expr
));
1360 if Local_Expr_Value
(H
) - Local_Expr_Value
(L
) = 2 then
1361 Append_List_To
(S
, Gen_Assign
(Add
(2, To
=> L
), Expr
));
1367 -- Otherwise construct the loop, starting with the loop index L_J
1369 L_J
:= Make_Temporary
(Loc
, 'J', L
);
1371 -- Construct "L .. H" in Index_Base. We use a qualified expression
1372 -- for the bound to convert to the index base, but we don't need
1373 -- to do that if we already have the base type at hand.
1375 if Etype
(L
) = Index_Base
then
1379 Make_Qualified_Expression
(Loc
,
1380 Subtype_Mark
=> Index_Base_Name
,
1384 if Etype
(H
) = Index_Base
then
1388 Make_Qualified_Expression
(Loc
,
1389 Subtype_Mark
=> Index_Base_Name
,
1398 -- Construct "for L_J in Index_Base range L .. H"
1400 L_Iteration_Scheme
:=
1401 Make_Iteration_Scheme
1403 Loop_Parameter_Specification
=>
1404 Make_Loop_Parameter_Specification
1406 Defining_Identifier
=> L_J
,
1407 Discrete_Subtype_Definition
=> L_Range
));
1409 -- Construct the statements to execute in the loop body
1411 L_Body
:= Gen_Assign
(New_Occurrence_Of
(L_J
, Loc
), Expr
);
1413 -- Construct the final loop
1416 Make_Implicit_Loop_Statement
1418 Identifier
=> Empty
,
1419 Iteration_Scheme
=> L_Iteration_Scheme
,
1420 Statements
=> L_Body
));
1422 -- A small optimization: if the aggregate is initialized with a box
1423 -- and the component type has no initialization procedure, remove the
1424 -- useless empty loop.
1426 if Nkind
(First
(S
)) = N_Loop_Statement
1427 and then Is_Empty_List
(Statements
(First
(S
)))
1429 return New_List
(Make_Null_Statement
(Loc
));
1439 -- The code built is
1441 -- W_J : Index_Base := L;
1442 -- while W_J < H loop
1443 -- W_J := Index_Base'Succ (W);
1447 function Gen_While
(L
, H
: Node_Id
; Expr
: Node_Id
) return List_Id
is
1451 -- W_J : Base_Type := L;
1453 W_Iteration_Scheme
: Node_Id
;
1456 W_Index_Succ
: Node_Id
;
1457 -- Index_Base'Succ (J)
1459 W_Increment
: Node_Id
;
1460 -- W_J := Index_Base'Succ (W)
1462 W_Body
: constant List_Id
:= New_List
;
1463 -- The statements to execute in the loop
1465 S
: constant List_Id
:= New_List
;
1466 -- list of statement
1469 -- If loop bounds define an empty range or are equal return null
1471 if Empty_Range
(L
, H
) or else Equal
(L
, H
) then
1472 Append_To
(S
, Make_Null_Statement
(Loc
));
1476 -- Build the decl of W_J
1478 W_J
:= Make_Temporary
(Loc
, 'J', L
);
1480 Make_Object_Declaration
1482 Defining_Identifier
=> W_J
,
1483 Object_Definition
=> Index_Base_Name
,
1486 -- Theoretically we should do a New_Copy_Tree (L) here, but we know
1487 -- that in this particular case L is a fresh Expr generated by
1488 -- Add which we are the only ones to use.
1490 Append_To
(S
, W_Decl
);
1492 -- Construct " while W_J < H"
1494 W_Iteration_Scheme
:=
1495 Make_Iteration_Scheme
1497 Condition
=> Make_Op_Lt
1499 Left_Opnd
=> New_Occurrence_Of
(W_J
, Loc
),
1500 Right_Opnd
=> New_Copy_Tree
(H
)));
1502 -- Construct the statements to execute in the loop body
1505 Make_Attribute_Reference
1507 Prefix
=> Index_Base_Name
,
1508 Attribute_Name
=> Name_Succ
,
1509 Expressions
=> New_List
(New_Occurrence_Of
(W_J
, Loc
)));
1512 Make_OK_Assignment_Statement
1514 Name
=> New_Occurrence_Of
(W_J
, Loc
),
1515 Expression
=> W_Index_Succ
);
1517 Append_To
(W_Body
, W_Increment
);
1518 Append_List_To
(W_Body
,
1519 Gen_Assign
(New_Occurrence_Of
(W_J
, Loc
), Expr
));
1521 -- Construct the final loop
1524 Make_Implicit_Loop_Statement
1526 Identifier
=> Empty
,
1527 Iteration_Scheme
=> W_Iteration_Scheme
,
1528 Statements
=> W_Body
));
1533 --------------------
1534 -- Get_Assoc_Expr --
1535 --------------------
1537 function Get_Assoc_Expr
(Assoc
: Node_Id
) return Node_Id
is
1538 Typ
: constant Entity_Id
:= Base_Type
(Etype
(N
));
1541 if Box_Present
(Assoc
) then
1542 if Is_Scalar_Type
(Ctype
) then
1543 if Present
(Default_Aspect_Component_Value
(Typ
)) then
1544 return Default_Aspect_Component_Value
(Typ
);
1545 elsif Present
(Default_Aspect_Value
(Ctype
)) then
1546 return Default_Aspect_Value
(Ctype
);
1556 return Expression
(Assoc
);
1560 ---------------------
1561 -- Index_Base_Name --
1562 ---------------------
1564 function Index_Base_Name
return Node_Id
is
1566 return New_Occurrence_Of
(Index_Base
, Sloc
(N
));
1567 end Index_Base_Name
;
1569 ------------------------------------
1570 -- Local_Compile_Time_Known_Value --
1571 ------------------------------------
1573 function Local_Compile_Time_Known_Value
(E
: Node_Id
) return Boolean is
1575 return Compile_Time_Known_Value
(E
)
1577 (Nkind
(E
) = N_Attribute_Reference
1578 and then Attribute_Name
(E
) = Name_Val
1579 and then Compile_Time_Known_Value
(First
(Expressions
(E
))));
1580 end Local_Compile_Time_Known_Value
;
1582 ----------------------
1583 -- Local_Expr_Value --
1584 ----------------------
1586 function Local_Expr_Value
(E
: Node_Id
) return Uint
is
1588 if Compile_Time_Known_Value
(E
) then
1589 return Expr_Value
(E
);
1591 return Expr_Value
(First
(Expressions
(E
)));
1593 end Local_Expr_Value
;
1595 -- Build_Array_Aggr_Code Variables
1602 Others_Assoc
: Node_Id
:= Empty
;
1604 Aggr_L
: constant Node_Id
:= Low_Bound
(Aggregate_Bounds
(N
));
1605 Aggr_H
: constant Node_Id
:= High_Bound
(Aggregate_Bounds
(N
));
1606 -- The aggregate bounds of this specific sub-aggregate. Note that if
1607 -- the code generated by Build_Array_Aggr_Code is executed then these
1608 -- bounds are OK. Otherwise a Constraint_Error would have been raised.
1610 Aggr_Low
: constant Node_Id
:= Duplicate_Subexpr_No_Checks
(Aggr_L
);
1611 Aggr_High
: constant Node_Id
:= Duplicate_Subexpr_No_Checks
(Aggr_H
);
1612 -- After Duplicate_Subexpr these are side-effect free
1617 Nb_Choices
: Nat
:= 0;
1618 Table
: Case_Table_Type
(1 .. Number_Of_Choices
(N
));
1619 -- Used to sort all the different choice values
1622 -- Number of elements in the positional aggregate
1624 New_Code
: constant List_Id
:= New_List
;
1626 -- Start of processing for Build_Array_Aggr_Code
1629 -- First before we start, a special case. if we have a bit packed
1630 -- array represented as a modular type, then clear the value to
1631 -- zero first, to ensure that unused bits are properly cleared.
1636 and then Is_Bit_Packed_Array
(Typ
)
1637 and then Is_Modular_Integer_Type
(Packed_Array_Impl_Type
(Typ
))
1639 Append_To
(New_Code
,
1640 Make_Assignment_Statement
(Loc
,
1641 Name
=> New_Copy_Tree
(Into
),
1643 Unchecked_Convert_To
(Typ
,
1644 Make_Integer_Literal
(Loc
, Uint_0
))));
1647 -- If the component type contains tasks, we need to build a Master
1648 -- entity in the current scope, because it will be needed if build-
1649 -- in-place functions are called in the expanded code.
1651 if Nkind
(Parent
(N
)) = N_Object_Declaration
and then Has_Task
(Typ
) then
1652 Build_Master_Entity
(Defining_Identifier
(Parent
(N
)));
1655 -- STEP 1: Process component associations
1657 -- For those associations that may generate a loop, initialize
1658 -- Loop_Actions to collect inserted actions that may be crated.
1660 -- Skip this if no component associations
1662 if No
(Expressions
(N
)) then
1664 -- STEP 1 (a): Sort the discrete choices
1666 Assoc
:= First
(Component_Associations
(N
));
1667 while Present
(Assoc
) loop
1668 Choice
:= First
(Choices
(Assoc
));
1669 while Present
(Choice
) loop
1670 if Nkind
(Choice
) = N_Others_Choice
then
1671 Set_Loop_Actions
(Assoc
, New_List
);
1672 Others_Assoc
:= Assoc
;
1676 Get_Index_Bounds
(Choice
, Low
, High
);
1679 Set_Loop_Actions
(Assoc
, New_List
);
1682 Nb_Choices
:= Nb_Choices
+ 1;
1684 Table
(Nb_Choices
) :=
1687 Choice_Node
=> Get_Assoc_Expr
(Assoc
));
1695 -- If there is more than one set of choices these must be static
1696 -- and we can therefore sort them. Remember that Nb_Choices does not
1697 -- account for an others choice.
1699 if Nb_Choices
> 1 then
1700 Sort_Case_Table
(Table
);
1703 -- STEP 1 (b): take care of the whole set of discrete choices
1705 for J
in 1 .. Nb_Choices
loop
1706 Low
:= Table
(J
).Choice_Lo
;
1707 High
:= Table
(J
).Choice_Hi
;
1708 Expr
:= Table
(J
).Choice_Node
;
1709 Append_List
(Gen_Loop
(Low
, High
, Expr
), To
=> New_Code
);
1712 -- STEP 1 (c): generate the remaining loops to cover others choice
1713 -- We don't need to generate loops over empty gaps, but if there is
1714 -- a single empty range we must analyze the expression for semantics
1716 if Present
(Others_Assoc
) then
1718 First
: Boolean := True;
1721 for J
in 0 .. Nb_Choices
loop
1725 Low
:= Add
(1, To
=> Table
(J
).Choice_Hi
);
1728 if J
= Nb_Choices
then
1731 High
:= Add
(-1, To
=> Table
(J
+ 1).Choice_Lo
);
1734 -- If this is an expansion within an init proc, make
1735 -- sure that discriminant references are replaced by
1736 -- the corresponding discriminal.
1738 if Inside_Init_Proc
then
1739 if Is_Entity_Name
(Low
)
1740 and then Ekind
(Entity
(Low
)) = E_Discriminant
1742 Set_Entity
(Low
, Discriminal
(Entity
(Low
)));
1745 if Is_Entity_Name
(High
)
1746 and then Ekind
(Entity
(High
)) = E_Discriminant
1748 Set_Entity
(High
, Discriminal
(Entity
(High
)));
1753 or else not Empty_Range
(Low
, High
)
1757 (Gen_Loop
(Low
, High
,
1758 Get_Assoc_Expr
(Others_Assoc
)), To
=> New_Code
);
1764 -- STEP 2: Process positional components
1767 -- STEP 2 (a): Generate the assignments for each positional element
1768 -- Note that here we have to use Aggr_L rather than Aggr_Low because
1769 -- Aggr_L is analyzed and Add wants an analyzed expression.
1771 Expr
:= First
(Expressions
(N
));
1773 while Present
(Expr
) loop
1774 Nb_Elements
:= Nb_Elements
+ 1;
1775 Append_List
(Gen_Assign
(Add
(Nb_Elements
, To
=> Aggr_L
), Expr
),
1780 -- STEP 2 (b): Generate final loop if an others choice is present
1781 -- Here Nb_Elements gives the offset of the last positional element.
1783 if Present
(Component_Associations
(N
)) then
1784 Assoc
:= Last
(Component_Associations
(N
));
1786 -- Ada 2005 (AI-287)
1788 Append_List
(Gen_While
(Add
(Nb_Elements
, To
=> Aggr_L
),
1790 Get_Assoc_Expr
(Assoc
)), -- AI-287
1796 end Build_Array_Aggr_Code
;
1798 ----------------------------
1799 -- Build_Record_Aggr_Code --
1800 ----------------------------
1802 function Build_Record_Aggr_Code
1805 Lhs
: Node_Id
) return List_Id
1807 Loc
: constant Source_Ptr
:= Sloc
(N
);
1808 L
: constant List_Id
:= New_List
;
1809 N_Typ
: constant Entity_Id
:= Etype
(N
);
1815 Comp_Type
: Entity_Id
;
1816 Selector
: Entity_Id
;
1817 Comp_Expr
: Node_Id
;
1820 -- If this is an internal aggregate, the External_Final_List is an
1821 -- expression for the controller record of the enclosing type.
1823 -- If the current aggregate has several controlled components, this
1824 -- expression will appear in several calls to attach to the finali-
1825 -- zation list, and it must not be shared.
1827 Ancestor_Is_Expression
: Boolean := False;
1828 Ancestor_Is_Subtype_Mark
: Boolean := False;
1830 Init_Typ
: Entity_Id
:= Empty
;
1832 Finalization_Done
: Boolean := False;
1833 -- True if Generate_Finalization_Actions has already been called; calls
1834 -- after the first do nothing.
1836 function Ancestor_Discriminant_Value
(Disc
: Entity_Id
) return Node_Id
;
1837 -- Returns the value that the given discriminant of an ancestor type
1838 -- should receive (in the absence of a conflict with the value provided
1839 -- by an ancestor part of an extension aggregate).
1841 procedure Check_Ancestor_Discriminants
(Anc_Typ
: Entity_Id
);
1842 -- Check that each of the discriminant values defined by the ancestor
1843 -- part of an extension aggregate match the corresponding values
1844 -- provided by either an association of the aggregate or by the
1845 -- constraint imposed by a parent type (RM95-4.3.2(8)).
1847 function Compatible_Int_Bounds
1848 (Agg_Bounds
: Node_Id
;
1849 Typ_Bounds
: Node_Id
) return Boolean;
1850 -- Return true if Agg_Bounds are equal or within Typ_Bounds. It is
1851 -- assumed that both bounds are integer ranges.
1853 procedure Generate_Finalization_Actions
;
1854 -- Deal with the various controlled type data structure initializations
1855 -- (but only if it hasn't been done already).
1857 function Get_Constraint_Association
(T
: Entity_Id
) return Node_Id
;
1858 -- Returns the first discriminant association in the constraint
1859 -- associated with T, if any, otherwise returns Empty.
1861 procedure Init_Hidden_Discriminants
(Typ
: Entity_Id
; List
: List_Id
);
1862 -- If Typ is derived, and constrains discriminants of the parent type,
1863 -- these discriminants are not components of the aggregate, and must be
1864 -- initialized. The assignments are appended to List. The same is done
1865 -- if Typ derives fron an already constrained subtype of a discriminated
1868 function Get_Explicit_Discriminant_Value
(D
: Entity_Id
) return Node_Id
;
1869 -- If the ancestor part is an unconstrained type and further ancestors
1870 -- do not provide discriminants for it, check aggregate components for
1871 -- values of the discriminants.
1873 function Is_Int_Range_Bounds
(Bounds
: Node_Id
) return Boolean;
1874 -- Check whether Bounds is a range node and its lower and higher bounds
1875 -- are integers literals.
1877 ---------------------------------
1878 -- Ancestor_Discriminant_Value --
1879 ---------------------------------
1881 function Ancestor_Discriminant_Value
(Disc
: Entity_Id
) return Node_Id
is
1883 Assoc_Elmt
: Elmt_Id
;
1884 Aggr_Comp
: Entity_Id
;
1885 Corresp_Disc
: Entity_Id
;
1886 Current_Typ
: Entity_Id
:= Base_Type
(Typ
);
1887 Parent_Typ
: Entity_Id
;
1888 Parent_Disc
: Entity_Id
;
1889 Save_Assoc
: Node_Id
:= Empty
;
1892 -- First check any discriminant associations to see if any of them
1893 -- provide a value for the discriminant.
1895 if Present
(Discriminant_Specifications
(Parent
(Current_Typ
))) then
1896 Assoc
:= First
(Component_Associations
(N
));
1897 while Present
(Assoc
) loop
1898 Aggr_Comp
:= Entity
(First
(Choices
(Assoc
)));
1900 if Ekind
(Aggr_Comp
) = E_Discriminant
then
1901 Save_Assoc
:= Expression
(Assoc
);
1903 Corresp_Disc
:= Corresponding_Discriminant
(Aggr_Comp
);
1904 while Present
(Corresp_Disc
) loop
1906 -- If found a corresponding discriminant then return the
1907 -- value given in the aggregate. (Note: this is not
1908 -- correct in the presence of side effects. ???)
1910 if Disc
= Corresp_Disc
then
1911 return Duplicate_Subexpr
(Expression
(Assoc
));
1915 Corresponding_Discriminant
(Corresp_Disc
);
1923 -- No match found in aggregate, so chain up parent types to find
1924 -- a constraint that defines the value of the discriminant.
1926 Parent_Typ
:= Etype
(Current_Typ
);
1927 while Current_Typ
/= Parent_Typ
loop
1928 if Has_Discriminants
(Parent_Typ
)
1929 and then not Has_Unknown_Discriminants
(Parent_Typ
)
1931 Parent_Disc
:= First_Discriminant
(Parent_Typ
);
1933 -- We either get the association from the subtype indication
1934 -- of the type definition itself, or from the discriminant
1935 -- constraint associated with the type entity (which is
1936 -- preferable, but it's not always present ???)
1938 if Is_Empty_Elmt_List
(
1939 Discriminant_Constraint
(Current_Typ
))
1941 Assoc
:= Get_Constraint_Association
(Current_Typ
);
1942 Assoc_Elmt
:= No_Elmt
;
1945 First_Elmt
(Discriminant_Constraint
(Current_Typ
));
1946 Assoc
:= Node
(Assoc_Elmt
);
1949 -- Traverse the discriminants of the parent type looking
1950 -- for one that corresponds.
1952 while Present
(Parent_Disc
) and then Present
(Assoc
) loop
1953 Corresp_Disc
:= Parent_Disc
;
1954 while Present
(Corresp_Disc
)
1955 and then Disc
/= Corresp_Disc
1958 Corresponding_Discriminant
(Corresp_Disc
);
1961 if Disc
= Corresp_Disc
then
1962 if Nkind
(Assoc
) = N_Discriminant_Association
then
1963 Assoc
:= Expression
(Assoc
);
1966 -- If the located association directly denotes
1967 -- a discriminant, then use the value of a saved
1968 -- association of the aggregate. This is an approach
1969 -- used to handle certain cases involving multiple
1970 -- discriminants mapped to a single discriminant of
1971 -- a descendant. It's not clear how to locate the
1972 -- appropriate discriminant value for such cases. ???
1974 if Is_Entity_Name
(Assoc
)
1975 and then Ekind
(Entity
(Assoc
)) = E_Discriminant
1977 Assoc
:= Save_Assoc
;
1980 return Duplicate_Subexpr
(Assoc
);
1983 Next_Discriminant
(Parent_Disc
);
1985 if No
(Assoc_Elmt
) then
1988 Next_Elmt
(Assoc_Elmt
);
1989 if Present
(Assoc_Elmt
) then
1990 Assoc
:= Node
(Assoc_Elmt
);
1998 Current_Typ
:= Parent_Typ
;
1999 Parent_Typ
:= Etype
(Current_Typ
);
2002 -- In some cases there's no ancestor value to locate (such as
2003 -- when an ancestor part given by an expression defines the
2004 -- discriminant value).
2007 end Ancestor_Discriminant_Value
;
2009 ----------------------------------
2010 -- Check_Ancestor_Discriminants --
2011 ----------------------------------
2013 procedure Check_Ancestor_Discriminants
(Anc_Typ
: Entity_Id
) is
2015 Disc_Value
: Node_Id
;
2019 Discr
:= First_Discriminant
(Base_Type
(Anc_Typ
));
2020 while Present
(Discr
) loop
2021 Disc_Value
:= Ancestor_Discriminant_Value
(Discr
);
2023 if Present
(Disc_Value
) then
2024 Cond
:= Make_Op_Ne
(Loc
,
2026 Make_Selected_Component
(Loc
,
2027 Prefix
=> New_Copy_Tree
(Target
),
2028 Selector_Name
=> New_Occurrence_Of
(Discr
, Loc
)),
2029 Right_Opnd
=> Disc_Value
);
2032 Make_Raise_Constraint_Error
(Loc
,
2034 Reason
=> CE_Discriminant_Check_Failed
));
2037 Next_Discriminant
(Discr
);
2039 end Check_Ancestor_Discriminants
;
2041 ---------------------------
2042 -- Compatible_Int_Bounds --
2043 ---------------------------
2045 function Compatible_Int_Bounds
2046 (Agg_Bounds
: Node_Id
;
2047 Typ_Bounds
: Node_Id
) return Boolean
2049 Agg_Lo
: constant Uint
:= Intval
(Low_Bound
(Agg_Bounds
));
2050 Agg_Hi
: constant Uint
:= Intval
(High_Bound
(Agg_Bounds
));
2051 Typ_Lo
: constant Uint
:= Intval
(Low_Bound
(Typ_Bounds
));
2052 Typ_Hi
: constant Uint
:= Intval
(High_Bound
(Typ_Bounds
));
2054 return Typ_Lo
<= Agg_Lo
and then Agg_Hi
<= Typ_Hi
;
2055 end Compatible_Int_Bounds
;
2057 --------------------------------
2058 -- Get_Constraint_Association --
2059 --------------------------------
2061 function Get_Constraint_Association
(T
: Entity_Id
) return Node_Id
is
2068 -- If type is private, get constraint from full view. This was
2069 -- previously done in an instance context, but is needed whenever
2070 -- the ancestor part has a discriminant, possibly inherited through
2071 -- multiple derivations.
2073 if Is_Private_Type
(Typ
) and then Present
(Full_View
(Typ
)) then
2074 Typ
:= Full_View
(Typ
);
2077 Indic
:= Subtype_Indication
(Type_Definition
(Parent
(Typ
)));
2079 -- Verify that the subtype indication carries a constraint
2081 if Nkind
(Indic
) = N_Subtype_Indication
2082 and then Present
(Constraint
(Indic
))
2084 return First
(Constraints
(Constraint
(Indic
)));
2088 end Get_Constraint_Association
;
2090 -------------------------------------
2091 -- Get_Explicit_Discriminant_Value --
2092 -------------------------------------
2094 function Get_Explicit_Discriminant_Value
2095 (D
: Entity_Id
) return Node_Id
2102 -- The aggregate has been normalized and all associations have a
2105 Assoc
:= First
(Component_Associations
(N
));
2106 while Present
(Assoc
) loop
2107 Choice
:= First
(Choices
(Assoc
));
2109 if Chars
(Choice
) = Chars
(D
) then
2110 Val
:= Expression
(Assoc
);
2119 end Get_Explicit_Discriminant_Value
;
2121 -------------------------------
2122 -- Init_Hidden_Discriminants --
2123 -------------------------------
2125 procedure Init_Hidden_Discriminants
(Typ
: Entity_Id
; List
: List_Id
) is
2127 Parent_Type
: Entity_Id
;
2129 Discr_Val
: Elmt_Id
;
2130 In_Aggr_Type
: Boolean;
2133 -- The constraints on the hidden discriminants, if present, are kept
2134 -- in the Stored_Constraint list of the type itself, or in that of
2135 -- the base type. If not in the constraints of the aggregate itself,
2136 -- we examine ancestors to find discriminants that are not renamed
2137 -- by other discriminants but constrained explicitly.
2139 In_Aggr_Type
:= True;
2141 Btype
:= Base_Type
(Typ
);
2142 while Is_Derived_Type
(Btype
)
2144 (Present
(Stored_Constraint
(Btype
))
2146 (In_Aggr_Type
and then Present
(Stored_Constraint
(Typ
))))
2148 Parent_Type
:= Etype
(Btype
);
2150 if not Has_Discriminants
(Parent_Type
) then
2154 Disc
:= First_Discriminant
(Parent_Type
);
2156 -- We know that one of the stored-constraint lists is present
2158 if Present
(Stored_Constraint
(Btype
)) then
2159 Discr_Val
:= First_Elmt
(Stored_Constraint
(Btype
));
2161 -- For private extension, stored constraint may be on full view
2163 elsif Is_Private_Type
(Btype
)
2164 and then Present
(Full_View
(Btype
))
2165 and then Present
(Stored_Constraint
(Full_View
(Btype
)))
2167 Discr_Val
:= First_Elmt
(Stored_Constraint
(Full_View
(Btype
)));
2170 Discr_Val
:= First_Elmt
(Stored_Constraint
(Typ
));
2173 while Present
(Discr_Val
) and then Present
(Disc
) loop
2175 -- Only those discriminants of the parent that are not
2176 -- renamed by discriminants of the derived type need to
2177 -- be added explicitly.
2179 if not Is_Entity_Name
(Node
(Discr_Val
))
2180 or else Ekind
(Entity
(Node
(Discr_Val
))) /= E_Discriminant
2183 Make_Selected_Component
(Loc
,
2184 Prefix
=> New_Copy_Tree
(Target
),
2185 Selector_Name
=> New_Occurrence_Of
(Disc
, Loc
));
2188 Make_OK_Assignment_Statement
(Loc
,
2190 Expression
=> New_Copy_Tree
(Node
(Discr_Val
)));
2192 Set_No_Ctrl_Actions
(Instr
);
2193 Append_To
(List
, Instr
);
2196 Next_Discriminant
(Disc
);
2197 Next_Elmt
(Discr_Val
);
2200 In_Aggr_Type
:= False;
2201 Btype
:= Base_Type
(Parent_Type
);
2203 end Init_Hidden_Discriminants
;
2205 -------------------------
2206 -- Is_Int_Range_Bounds --
2207 -------------------------
2209 function Is_Int_Range_Bounds
(Bounds
: Node_Id
) return Boolean is
2211 return Nkind
(Bounds
) = N_Range
2212 and then Nkind
(Low_Bound
(Bounds
)) = N_Integer_Literal
2213 and then Nkind
(High_Bound
(Bounds
)) = N_Integer_Literal
;
2214 end Is_Int_Range_Bounds
;
2216 -----------------------------------
2217 -- Generate_Finalization_Actions --
2218 -----------------------------------
2220 procedure Generate_Finalization_Actions
is
2222 -- Do the work only the first time this is called
2224 if Finalization_Done
then
2228 Finalization_Done
:= True;
2230 -- Determine the external finalization list. It is either the
2231 -- finalization list of the outer-scope or the one coming from an
2232 -- outer aggregate. When the target is not a temporary, the proper
2233 -- scope is the scope of the target rather than the potentially
2234 -- transient current scope.
2236 if Is_Controlled
(Typ
) and then Ancestor_Is_Subtype_Mark
then
2237 Ref
:= Convert_To
(Init_Typ
, New_Copy_Tree
(Target
));
2238 Set_Assignment_OK
(Ref
);
2241 Make_Procedure_Call_Statement
(Loc
,
2244 (Find_Prim_Op
(Init_Typ
, Name_Initialize
), Loc
),
2245 Parameter_Associations
=> New_List
(New_Copy_Tree
(Ref
))));
2247 end Generate_Finalization_Actions
;
2249 function Rewrite_Discriminant
(Expr
: Node_Id
) return Traverse_Result
;
2250 -- If default expression of a component mentions a discriminant of the
2251 -- type, it must be rewritten as the discriminant of the target object.
2253 function Replace_Type
(Expr
: Node_Id
) return Traverse_Result
;
2254 -- If the aggregate contains a self-reference, traverse each expression
2255 -- to replace a possible self-reference with a reference to the proper
2256 -- component of the target of the assignment.
2258 --------------------------
2259 -- Rewrite_Discriminant --
2260 --------------------------
2262 function Rewrite_Discriminant
(Expr
: Node_Id
) return Traverse_Result
is
2264 if Is_Entity_Name
(Expr
)
2265 and then Present
(Entity
(Expr
))
2266 and then Ekind
(Entity
(Expr
)) = E_In_Parameter
2267 and then Present
(Discriminal_Link
(Entity
(Expr
)))
2268 and then Scope
(Discriminal_Link
(Entity
(Expr
))) =
2269 Base_Type
(Etype
(N
))
2272 Make_Selected_Component
(Loc
,
2273 Prefix
=> New_Copy_Tree
(Lhs
),
2274 Selector_Name
=> Make_Identifier
(Loc
, Chars
(Expr
))));
2278 end Rewrite_Discriminant
;
2284 function Replace_Type
(Expr
: Node_Id
) return Traverse_Result
is
2286 -- Note regarding the Root_Type test below: Aggregate components for
2287 -- self-referential types include attribute references to the current
2288 -- instance, of the form: Typ'access, etc.. These references are
2289 -- rewritten as references to the target of the aggregate: the
2290 -- left-hand side of an assignment, the entity in a declaration,
2291 -- or a temporary. Without this test, we would improperly extended
2292 -- this rewriting to attribute references whose prefix was not the
2293 -- type of the aggregate.
2295 if Nkind
(Expr
) = N_Attribute_Reference
2296 and then Is_Entity_Name
(Prefix
(Expr
))
2297 and then Is_Type
(Entity
(Prefix
(Expr
)))
2298 and then Root_Type
(Etype
(N
)) = Root_Type
(Entity
(Prefix
(Expr
)))
2300 if Is_Entity_Name
(Lhs
) then
2301 Rewrite
(Prefix
(Expr
),
2302 New_Occurrence_Of
(Entity
(Lhs
), Loc
));
2304 elsif Nkind
(Lhs
) = N_Selected_Component
then
2306 Make_Attribute_Reference
(Loc
,
2307 Attribute_Name
=> Name_Unrestricted_Access
,
2308 Prefix
=> New_Copy_Tree
(Lhs
)));
2309 Set_Analyzed
(Parent
(Expr
), False);
2313 Make_Attribute_Reference
(Loc
,
2314 Attribute_Name
=> Name_Unrestricted_Access
,
2315 Prefix
=> New_Copy_Tree
(Lhs
)));
2316 Set_Analyzed
(Parent
(Expr
), False);
2323 procedure Replace_Self_Reference
is
2324 new Traverse_Proc
(Replace_Type
);
2326 procedure Replace_Discriminants
is
2327 new Traverse_Proc
(Rewrite_Discriminant
);
2329 -- Start of processing for Build_Record_Aggr_Code
2332 if Has_Self_Reference
(N
) then
2333 Replace_Self_Reference
(N
);
2336 -- If the target of the aggregate is class-wide, we must convert it
2337 -- to the actual type of the aggregate, so that the proper components
2338 -- are visible. We know already that the types are compatible.
2340 if Present
(Etype
(Lhs
))
2341 and then Is_Class_Wide_Type
(Etype
(Lhs
))
2343 Target
:= Unchecked_Convert_To
(Typ
, Lhs
);
2348 -- Deal with the ancestor part of extension aggregates or with the
2349 -- discriminants of the root type.
2351 if Nkind
(N
) = N_Extension_Aggregate
then
2353 Ancestor
: constant Node_Id
:= Ancestor_Part
(N
);
2357 -- If the ancestor part is a subtype mark "T", we generate
2359 -- init-proc (T (tmp)); if T is constrained and
2360 -- init-proc (S (tmp)); where S applies an appropriate
2361 -- constraint if T is unconstrained
2363 if Is_Entity_Name
(Ancestor
)
2364 and then Is_Type
(Entity
(Ancestor
))
2366 Ancestor_Is_Subtype_Mark
:= True;
2368 if Is_Constrained
(Entity
(Ancestor
)) then
2369 Init_Typ
:= Entity
(Ancestor
);
2371 -- For an ancestor part given by an unconstrained type mark,
2372 -- create a subtype constrained by appropriate corresponding
2373 -- discriminant values coming from either associations of the
2374 -- aggregate or a constraint on a parent type. The subtype will
2375 -- be used to generate the correct default value for the
2378 elsif Has_Discriminants
(Entity
(Ancestor
)) then
2380 Anc_Typ
: constant Entity_Id
:= Entity
(Ancestor
);
2381 Anc_Constr
: constant List_Id
:= New_List
;
2382 Discrim
: Entity_Id
;
2383 Disc_Value
: Node_Id
;
2384 New_Indic
: Node_Id
;
2385 Subt_Decl
: Node_Id
;
2388 Discrim
:= First_Discriminant
(Anc_Typ
);
2389 while Present
(Discrim
) loop
2390 Disc_Value
:= Ancestor_Discriminant_Value
(Discrim
);
2392 -- If no usable discriminant in ancestors, check
2393 -- whether aggregate has an explicit value for it.
2395 if No
(Disc_Value
) then
2397 Get_Explicit_Discriminant_Value
(Discrim
);
2400 Append_To
(Anc_Constr
, Disc_Value
);
2401 Next_Discriminant
(Discrim
);
2405 Make_Subtype_Indication
(Loc
,
2406 Subtype_Mark
=> New_Occurrence_Of
(Anc_Typ
, Loc
),
2408 Make_Index_Or_Discriminant_Constraint
(Loc
,
2409 Constraints
=> Anc_Constr
));
2411 Init_Typ
:= Create_Itype
(Ekind
(Anc_Typ
), N
);
2414 Make_Subtype_Declaration
(Loc
,
2415 Defining_Identifier
=> Init_Typ
,
2416 Subtype_Indication
=> New_Indic
);
2418 -- Itypes must be analyzed with checks off Declaration
2419 -- must have a parent for proper handling of subsidiary
2422 Set_Parent
(Subt_Decl
, N
);
2423 Analyze
(Subt_Decl
, Suppress
=> All_Checks
);
2427 Ref
:= Convert_To
(Init_Typ
, New_Copy_Tree
(Target
));
2428 Set_Assignment_OK
(Ref
);
2430 if not Is_Interface
(Init_Typ
) then
2432 Build_Initialization_Call
(Loc
,
2435 In_Init_Proc
=> Within_Init_Proc
,
2436 With_Default_Init
=> Has_Default_Init_Comps
(N
)
2438 Has_Task
(Base_Type
(Init_Typ
))));
2440 if Is_Constrained
(Entity
(Ancestor
))
2441 and then Has_Discriminants
(Entity
(Ancestor
))
2443 Check_Ancestor_Discriminants
(Entity
(Ancestor
));
2447 -- Handle calls to C++ constructors
2449 elsif Is_CPP_Constructor_Call
(Ancestor
) then
2450 Init_Typ
:= Etype
(Ancestor
);
2451 Ref
:= Convert_To
(Init_Typ
, New_Copy_Tree
(Target
));
2452 Set_Assignment_OK
(Ref
);
2455 Build_Initialization_Call
(Loc
,
2458 In_Init_Proc
=> Within_Init_Proc
,
2459 With_Default_Init
=> Has_Default_Init_Comps
(N
),
2460 Constructor_Ref
=> Ancestor
));
2462 -- Ada 2005 (AI-287): If the ancestor part is an aggregate of
2463 -- limited type, a recursive call expands the ancestor. Note that
2464 -- in the limited case, the ancestor part must be either a
2465 -- function call (possibly qualified, or wrapped in an unchecked
2466 -- conversion) or aggregate (definitely qualified).
2468 -- The ancestor part can also be a function call (that may be
2469 -- transformed into an explicit dereference) or a qualification
2472 elsif Is_Limited_Type
(Etype
(Ancestor
))
2473 and then Nkind_In
(Unqualify
(Ancestor
), N_Aggregate
,
2474 N_Extension_Aggregate
)
2476 Ancestor_Is_Expression
:= True;
2478 -- Set up finalization data for enclosing record, because
2479 -- controlled subcomponents of the ancestor part will be
2482 Generate_Finalization_Actions
;
2485 Build_Record_Aggr_Code
2486 (N
=> Unqualify
(Ancestor
),
2487 Typ
=> Etype
(Unqualify
(Ancestor
)),
2490 -- If the ancestor part is an expression "E", we generate
2494 -- In Ada 2005, this includes the case of a (possibly qualified)
2495 -- limited function call. The assignment will turn into a
2496 -- build-in-place function call (for further details, see
2497 -- Make_Build_In_Place_Call_In_Assignment).
2500 Ancestor_Is_Expression
:= True;
2501 Init_Typ
:= Etype
(Ancestor
);
2503 -- If the ancestor part is an aggregate, force its full
2504 -- expansion, which was delayed.
2506 if Nkind_In
(Unqualify
(Ancestor
), N_Aggregate
,
2507 N_Extension_Aggregate
)
2509 Set_Analyzed
(Ancestor
, False);
2510 Set_Analyzed
(Expression
(Ancestor
), False);
2513 Ref
:= Convert_To
(Init_Typ
, New_Copy_Tree
(Target
));
2514 Set_Assignment_OK
(Ref
);
2516 -- Make the assignment without usual controlled actions, since
2517 -- we only want to Adjust afterwards, but not to Finalize
2518 -- beforehand. Add manual Adjust when necessary.
2520 Assign
:= New_List
(
2521 Make_OK_Assignment_Statement
(Loc
,
2523 Expression
=> Ancestor
));
2524 Set_No_Ctrl_Actions
(First
(Assign
));
2526 -- Assign the tag now to make sure that the dispatching call in
2527 -- the subsequent deep_adjust works properly (unless VM_Target,
2528 -- where tags are implicit).
2530 if Tagged_Type_Expansion
then
2532 Make_OK_Assignment_Statement
(Loc
,
2534 Make_Selected_Component
(Loc
,
2535 Prefix
=> New_Copy_Tree
(Target
),
2538 (First_Tag_Component
(Base_Type
(Typ
)), Loc
)),
2541 Unchecked_Convert_To
(RTE
(RE_Tag
),
2544 (Access_Disp_Table
(Base_Type
(Typ
)))),
2547 Set_Assignment_OK
(Name
(Instr
));
2548 Append_To
(Assign
, Instr
);
2550 -- Ada 2005 (AI-251): If tagged type has progenitors we must
2551 -- also initialize tags of the secondary dispatch tables.
2553 if Has_Interfaces
(Base_Type
(Typ
)) then
2555 (Typ
=> Base_Type
(Typ
),
2557 Stmts_List
=> Assign
);
2561 -- Call Adjust manually
2563 if Needs_Finalization
(Etype
(Ancestor
))
2564 and then not Is_Limited_Type
(Etype
(Ancestor
))
2568 (Obj_Ref
=> New_Copy_Tree
(Ref
),
2569 Typ
=> Etype
(Ancestor
)));
2573 Make_Unsuppress_Block
(Loc
, Name_Discriminant_Check
, Assign
));
2575 if Has_Discriminants
(Init_Typ
) then
2576 Check_Ancestor_Discriminants
(Init_Typ
);
2581 -- Generate assignments of hidden discriminants. If the base type is
2582 -- an unchecked union, the discriminants are unknown to the back-end
2583 -- and absent from a value of the type, so assignments for them are
2586 if Has_Discriminants
(Typ
)
2587 and then not Is_Unchecked_Union
(Base_Type
(Typ
))
2589 Init_Hidden_Discriminants
(Typ
, L
);
2592 -- Normal case (not an extension aggregate)
2595 -- Generate the discriminant expressions, component by component.
2596 -- If the base type is an unchecked union, the discriminants are
2597 -- unknown to the back-end and absent from a value of the type, so
2598 -- assignments for them are not emitted.
2600 if Has_Discriminants
(Typ
)
2601 and then not Is_Unchecked_Union
(Base_Type
(Typ
))
2603 Init_Hidden_Discriminants
(Typ
, L
);
2605 -- Generate discriminant init values for the visible discriminants
2608 Discriminant
: Entity_Id
;
2609 Discriminant_Value
: Node_Id
;
2612 Discriminant
:= First_Stored_Discriminant
(Typ
);
2613 while Present
(Discriminant
) loop
2615 Make_Selected_Component
(Loc
,
2616 Prefix
=> New_Copy_Tree
(Target
),
2617 Selector_Name
=> New_Occurrence_Of
(Discriminant
, Loc
));
2619 Discriminant_Value
:=
2620 Get_Discriminant_Value
(
2623 Discriminant_Constraint
(N_Typ
));
2626 Make_OK_Assignment_Statement
(Loc
,
2628 Expression
=> New_Copy_Tree
(Discriminant_Value
));
2630 Set_No_Ctrl_Actions
(Instr
);
2631 Append_To
(L
, Instr
);
2633 Next_Stored_Discriminant
(Discriminant
);
2639 -- For CPP types we generate an implicit call to the C++ default
2640 -- constructor to ensure the proper initialization of the _Tag
2643 if Is_CPP_Class
(Root_Type
(Typ
)) and then CPP_Num_Prims
(Typ
) > 0 then
2644 Invoke_Constructor
: declare
2645 CPP_Parent
: constant Entity_Id
:= Enclosing_CPP_Parent
(Typ
);
2647 procedure Invoke_IC_Proc
(T
: Entity_Id
);
2648 -- Recursive routine used to climb to parents. Required because
2649 -- parents must be initialized before descendants to ensure
2650 -- propagation of inherited C++ slots.
2652 --------------------
2653 -- Invoke_IC_Proc --
2654 --------------------
2656 procedure Invoke_IC_Proc
(T
: Entity_Id
) is
2658 -- Avoid generating extra calls. Initialization required
2659 -- only for types defined from the level of derivation of
2660 -- type of the constructor and the type of the aggregate.
2662 if T
= CPP_Parent
then
2666 Invoke_IC_Proc
(Etype
(T
));
2668 -- Generate call to the IC routine
2670 if Present
(CPP_Init_Proc
(T
)) then
2672 Make_Procedure_Call_Statement
(Loc
,
2673 New_Occurrence_Of
(CPP_Init_Proc
(T
), Loc
)));
2677 -- Start of processing for Invoke_Constructor
2680 -- Implicit invocation of the C++ constructor
2682 if Nkind
(N
) = N_Aggregate
then
2684 Make_Procedure_Call_Statement
(Loc
,
2686 New_Occurrence_Of
(Base_Init_Proc
(CPP_Parent
), Loc
),
2687 Parameter_Associations
=> New_List
(
2688 Unchecked_Convert_To
(CPP_Parent
,
2689 New_Copy_Tree
(Lhs
)))));
2692 Invoke_IC_Proc
(Typ
);
2693 end Invoke_Constructor
;
2696 -- Generate the assignments, component by component
2698 -- tmp.comp1 := Expr1_From_Aggr;
2699 -- tmp.comp2 := Expr2_From_Aggr;
2702 Comp
:= First
(Component_Associations
(N
));
2703 while Present
(Comp
) loop
2704 Selector
:= Entity
(First
(Choices
(Comp
)));
2708 if Is_CPP_Constructor_Call
(Expression
(Comp
)) then
2710 Build_Initialization_Call
(Loc
,
2712 Make_Selected_Component
(Loc
,
2713 Prefix
=> New_Copy_Tree
(Target
),
2714 Selector_Name
=> New_Occurrence_Of
(Selector
, Loc
)),
2715 Typ
=> Etype
(Selector
),
2717 With_Default_Init
=> True,
2718 Constructor_Ref
=> Expression
(Comp
)));
2720 -- Ada 2005 (AI-287): For each default-initialized component generate
2721 -- a call to the corresponding IP subprogram if available.
2723 elsif Box_Present
(Comp
)
2724 and then Has_Non_Null_Base_Init_Proc
(Etype
(Selector
))
2726 if Ekind
(Selector
) /= E_Discriminant
then
2727 Generate_Finalization_Actions
;
2730 -- Ada 2005 (AI-287): If the component type has tasks then
2731 -- generate the activation chain and master entities (except
2732 -- in case of an allocator because in that case these entities
2733 -- are generated by Build_Task_Allocate_Block_With_Init_Stmts).
2736 Ctype
: constant Entity_Id
:= Etype
(Selector
);
2737 Inside_Allocator
: Boolean := False;
2738 P
: Node_Id
:= Parent
(N
);
2741 if Is_Task_Type
(Ctype
) or else Has_Task
(Ctype
) then
2742 while Present
(P
) loop
2743 if Nkind
(P
) = N_Allocator
then
2744 Inside_Allocator
:= True;
2751 if not Inside_Init_Proc
and not Inside_Allocator
then
2752 Build_Activation_Chain_Entity
(N
);
2758 Build_Initialization_Call
(Loc
,
2759 Id_Ref
=> Make_Selected_Component
(Loc
,
2760 Prefix
=> New_Copy_Tree
(Target
),
2762 New_Occurrence_Of
(Selector
, Loc
)),
2763 Typ
=> Etype
(Selector
),
2765 With_Default_Init
=> True));
2767 -- Prepare for component assignment
2769 elsif Ekind
(Selector
) /= E_Discriminant
2770 or else Nkind
(N
) = N_Extension_Aggregate
2772 -- All the discriminants have now been assigned
2774 -- This is now a good moment to initialize and attach all the
2775 -- controllers. Their position may depend on the discriminants.
2777 if Ekind
(Selector
) /= E_Discriminant
then
2778 Generate_Finalization_Actions
;
2781 Comp_Type
:= Underlying_Type
(Etype
(Selector
));
2783 Make_Selected_Component
(Loc
,
2784 Prefix
=> New_Copy_Tree
(Target
),
2785 Selector_Name
=> New_Occurrence_Of
(Selector
, Loc
));
2787 if Nkind
(Expression
(Comp
)) = N_Qualified_Expression
then
2788 Expr_Q
:= Expression
(Expression
(Comp
));
2790 Expr_Q
:= Expression
(Comp
);
2793 -- Now either create the assignment or generate the code for the
2794 -- inner aggregate top-down.
2796 if Is_Delayed_Aggregate
(Expr_Q
) then
2798 -- We have the following case of aggregate nesting inside
2799 -- an object declaration:
2801 -- type Arr_Typ is array (Integer range <>) of ...;
2803 -- type Rec_Typ (...) is record
2804 -- Obj_Arr_Typ : Arr_Typ (A .. B);
2807 -- Obj_Rec_Typ : Rec_Typ := (...,
2808 -- Obj_Arr_Typ => (X => (...), Y => (...)));
2810 -- The length of the ranges of the aggregate and Obj_Add_Typ
2811 -- are equal (B - A = Y - X), but they do not coincide (X /=
2812 -- A and B /= Y). This case requires array sliding which is
2813 -- performed in the following manner:
2815 -- subtype Arr_Sub is Arr_Typ (X .. Y);
2817 -- Temp (X) := (...);
2819 -- Temp (Y) := (...);
2820 -- Obj_Rec_Typ.Obj_Arr_Typ := Temp;
2822 if Ekind
(Comp_Type
) = E_Array_Subtype
2823 and then Is_Int_Range_Bounds
(Aggregate_Bounds
(Expr_Q
))
2824 and then Is_Int_Range_Bounds
(First_Index
(Comp_Type
))
2826 Compatible_Int_Bounds
2827 (Agg_Bounds
=> Aggregate_Bounds
(Expr_Q
),
2828 Typ_Bounds
=> First_Index
(Comp_Type
))
2830 -- Create the array subtype with bounds equal to those of
2831 -- the corresponding aggregate.
2834 SubE
: constant Entity_Id
:= Make_Temporary
(Loc
, 'T');
2836 SubD
: constant Node_Id
:=
2837 Make_Subtype_Declaration
(Loc
,
2838 Defining_Identifier
=> SubE
,
2839 Subtype_Indication
=>
2840 Make_Subtype_Indication
(Loc
,
2842 New_Occurrence_Of
(Etype
(Comp_Type
), Loc
),
2844 Make_Index_Or_Discriminant_Constraint
2846 Constraints
=> New_List
(
2848 (Aggregate_Bounds
(Expr_Q
))))));
2850 -- Create a temporary array of the above subtype which
2851 -- will be used to capture the aggregate assignments.
2853 TmpE
: constant Entity_Id
:= Make_Temporary
(Loc
, 'A', N
);
2855 TmpD
: constant Node_Id
:=
2856 Make_Object_Declaration
(Loc
,
2857 Defining_Identifier
=> TmpE
,
2858 Object_Definition
=> New_Occurrence_Of
(SubE
, Loc
));
2861 Set_No_Initialization
(TmpD
);
2862 Append_To
(L
, SubD
);
2863 Append_To
(L
, TmpD
);
2865 -- Expand aggregate into assignments to the temp array
2868 Late_Expansion
(Expr_Q
, Comp_Type
,
2869 New_Occurrence_Of
(TmpE
, Loc
)));
2874 Make_Assignment_Statement
(Loc
,
2875 Name
=> New_Copy_Tree
(Comp_Expr
),
2876 Expression
=> New_Occurrence_Of
(TmpE
, Loc
)));
2879 -- Normal case (sliding not required)
2883 Late_Expansion
(Expr_Q
, Comp_Type
, Comp_Expr
));
2886 -- Expr_Q is not delayed aggregate
2889 if Has_Discriminants
(Typ
) then
2890 Replace_Discriminants
(Expr_Q
);
2892 -- If the component is an array type that depends on
2893 -- discriminants, and the expression is a single Others
2894 -- clause, create an explicit subtype for it because the
2895 -- backend has troubles recovering the actual bounds.
2897 if Nkind
(Expr_Q
) = N_Aggregate
2898 and then Is_Array_Type
(Comp_Type
)
2899 and then Present
(Component_Associations
(Expr_Q
))
2902 Assoc
: constant Node_Id
:=
2903 First
(Component_Associations
(Expr_Q
));
2907 if Nkind
(First
(Choices
(Assoc
))) = N_Others_Choice
2910 Build_Actual_Subtype_Of_Component
2911 (Comp_Type
, Comp_Expr
);
2913 -- If the component type does not in fact depend on
2914 -- discriminants, the subtype declaration is empty.
2916 if Present
(Decl
) then
2917 Append_To
(L
, Decl
);
2918 Set_Etype
(Comp_Expr
, Defining_Entity
(Decl
));
2926 Make_OK_Assignment_Statement
(Loc
,
2928 Expression
=> Expr_Q
);
2930 Set_No_Ctrl_Actions
(Instr
);
2931 Append_To
(L
, Instr
);
2933 -- Adjust the tag if tagged (because of possible view
2934 -- conversions), unless compiling for a VM where tags are
2937 -- tmp.comp._tag := comp_typ'tag;
2939 if Is_Tagged_Type
(Comp_Type
)
2940 and then Tagged_Type_Expansion
2943 Make_OK_Assignment_Statement
(Loc
,
2945 Make_Selected_Component
(Loc
,
2946 Prefix
=> New_Copy_Tree
(Comp_Expr
),
2949 (First_Tag_Component
(Comp_Type
), Loc
)),
2952 Unchecked_Convert_To
(RTE
(RE_Tag
),
2954 (Node
(First_Elmt
(Access_Disp_Table
(Comp_Type
))),
2957 Append_To
(L
, Instr
);
2961 -- Adjust (tmp.comp);
2963 if Needs_Finalization
(Comp_Type
)
2964 and then not Is_Limited_Type
(Comp_Type
)
2968 (Obj_Ref
=> New_Copy_Tree
(Comp_Expr
),
2973 -- comment would be good here ???
2975 elsif Ekind
(Selector
) = E_Discriminant
2976 and then Nkind
(N
) /= N_Extension_Aggregate
2977 and then Nkind
(Parent
(N
)) = N_Component_Association
2978 and then Is_Constrained
(Typ
)
2980 -- We must check that the discriminant value imposed by the
2981 -- context is the same as the value given in the subaggregate,
2982 -- because after the expansion into assignments there is no
2983 -- record on which to perform a regular discriminant check.
2990 D_Val
:= First_Elmt
(Discriminant_Constraint
(Typ
));
2991 Disc
:= First_Discriminant
(Typ
);
2992 while Chars
(Disc
) /= Chars
(Selector
) loop
2993 Next_Discriminant
(Disc
);
2997 pragma Assert
(Present
(D_Val
));
2999 -- This check cannot performed for components that are
3000 -- constrained by a current instance, because this is not a
3001 -- value that can be compared with the actual constraint.
3003 if Nkind
(Node
(D_Val
)) /= N_Attribute_Reference
3004 or else not Is_Entity_Name
(Prefix
(Node
(D_Val
)))
3005 or else not Is_Type
(Entity
(Prefix
(Node
(D_Val
))))
3008 Make_Raise_Constraint_Error
(Loc
,
3011 Left_Opnd
=> New_Copy_Tree
(Node
(D_Val
)),
3012 Right_Opnd
=> Expression
(Comp
)),
3013 Reason
=> CE_Discriminant_Check_Failed
));
3016 -- Find self-reference in previous discriminant assignment,
3017 -- and replace with proper expression.
3024 while Present
(Ass
) loop
3025 if Nkind
(Ass
) = N_Assignment_Statement
3026 and then Nkind
(Name
(Ass
)) = N_Selected_Component
3027 and then Chars
(Selector_Name
(Name
(Ass
))) =
3031 (Ass
, New_Copy_Tree
(Expression
(Comp
)));
3044 -- If the type is tagged, the tag needs to be initialized (unless we
3045 -- are in VM-mode where tags are implicit). It is done late in the
3046 -- initialization process because in some cases, we call the init
3047 -- proc of an ancestor which will not leave out the right tag.
3049 if Ancestor_Is_Expression
then
3052 -- For CPP types we generated a call to the C++ default constructor
3053 -- before the components have been initialized to ensure the proper
3054 -- initialization of the _Tag component (see above).
3056 elsif Is_CPP_Class
(Typ
) then
3059 elsif Is_Tagged_Type
(Typ
) and then Tagged_Type_Expansion
then
3061 Make_OK_Assignment_Statement
(Loc
,
3063 Make_Selected_Component
(Loc
,
3064 Prefix
=> New_Copy_Tree
(Target
),
3067 (First_Tag_Component
(Base_Type
(Typ
)), Loc
)),
3070 Unchecked_Convert_To
(RTE
(RE_Tag
),
3072 (Node
(First_Elmt
(Access_Disp_Table
(Base_Type
(Typ
)))),
3075 Append_To
(L
, Instr
);
3077 -- Ada 2005 (AI-251): If the tagged type has been derived from an
3078 -- abstract interfaces we must also initialize the tags of the
3079 -- secondary dispatch tables.
3081 if Has_Interfaces
(Base_Type
(Typ
)) then
3083 (Typ
=> Base_Type
(Typ
),
3089 -- If the controllers have not been initialized yet (by lack of non-
3090 -- discriminant components), let's do it now.
3092 Generate_Finalization_Actions
;
3095 end Build_Record_Aggr_Code
;
3097 ---------------------------------------
3098 -- Collect_Initialization_Statements --
3099 ---------------------------------------
3101 procedure Collect_Initialization_Statements
3104 Node_After
: Node_Id
)
3106 Loc
: constant Source_Ptr
:= Sloc
(N
);
3107 Init_Actions
: constant List_Id
:= New_List
;
3108 Init_Node
: Node_Id
;
3109 Comp_Stmt
: Node_Id
;
3112 -- Nothing to do if Obj is already frozen, as in this case we known we
3113 -- won't need to move the initialization statements about later on.
3115 if Is_Frozen
(Obj
) then
3120 while Next
(Init_Node
) /= Node_After
loop
3121 Append_To
(Init_Actions
, Remove_Next
(Init_Node
));
3124 if not Is_Empty_List
(Init_Actions
) then
3125 Comp_Stmt
:= Make_Compound_Statement
(Loc
, Actions
=> Init_Actions
);
3126 Insert_Action_After
(Init_Node
, Comp_Stmt
);
3127 Set_Initialization_Statements
(Obj
, Comp_Stmt
);
3129 end Collect_Initialization_Statements
;
3131 -------------------------------
3132 -- Convert_Aggr_In_Allocator --
3133 -------------------------------
3135 procedure Convert_Aggr_In_Allocator
3140 Loc
: constant Source_Ptr
:= Sloc
(Aggr
);
3141 Typ
: constant Entity_Id
:= Etype
(Aggr
);
3142 Temp
: constant Entity_Id
:= Defining_Identifier
(Decl
);
3144 Occ
: constant Node_Id
:=
3145 Unchecked_Convert_To
(Typ
,
3146 Make_Explicit_Dereference
(Loc
, New_Occurrence_Of
(Temp
, Loc
)));
3149 if Is_Array_Type
(Typ
) then
3150 Convert_Array_Aggr_In_Allocator
(Decl
, Aggr
, Occ
);
3152 elsif Has_Default_Init_Comps
(Aggr
) then
3154 L
: constant List_Id
:= New_List
;
3155 Init_Stmts
: List_Id
;
3158 Init_Stmts
:= Late_Expansion
(Aggr
, Typ
, Occ
);
3160 if Has_Task
(Typ
) then
3161 Build_Task_Allocate_Block_With_Init_Stmts
(L
, Aggr
, Init_Stmts
);
3162 Insert_Actions
(Alloc
, L
);
3164 Insert_Actions
(Alloc
, Init_Stmts
);
3169 Insert_Actions
(Alloc
, Late_Expansion
(Aggr
, Typ
, Occ
));
3171 end Convert_Aggr_In_Allocator
;
3173 --------------------------------
3174 -- Convert_Aggr_In_Assignment --
3175 --------------------------------
3177 procedure Convert_Aggr_In_Assignment
(N
: Node_Id
) is
3178 Aggr
: Node_Id
:= Expression
(N
);
3179 Typ
: constant Entity_Id
:= Etype
(Aggr
);
3180 Occ
: constant Node_Id
:= New_Copy_Tree
(Name
(N
));
3183 if Nkind
(Aggr
) = N_Qualified_Expression
then
3184 Aggr
:= Expression
(Aggr
);
3187 Insert_Actions_After
(N
, Late_Expansion
(Aggr
, Typ
, Occ
));
3188 end Convert_Aggr_In_Assignment
;
3190 ---------------------------------
3191 -- Convert_Aggr_In_Object_Decl --
3192 ---------------------------------
3194 procedure Convert_Aggr_In_Object_Decl
(N
: Node_Id
) is
3195 Obj
: constant Entity_Id
:= Defining_Identifier
(N
);
3196 Aggr
: Node_Id
:= Expression
(N
);
3197 Loc
: constant Source_Ptr
:= Sloc
(Aggr
);
3198 Typ
: constant Entity_Id
:= Etype
(Aggr
);
3199 Occ
: constant Node_Id
:= New_Occurrence_Of
(Obj
, Loc
);
3201 function Discriminants_Ok
return Boolean;
3202 -- If the object type is constrained, the discriminants in the
3203 -- aggregate must be checked against the discriminants of the subtype.
3204 -- This cannot be done using Apply_Discriminant_Checks because after
3205 -- expansion there is no aggregate left to check.
3207 ----------------------
3208 -- Discriminants_Ok --
3209 ----------------------
3211 function Discriminants_Ok
return Boolean is
3212 Cond
: Node_Id
:= Empty
;
3221 D
:= First_Discriminant
(Typ
);
3222 Disc1
:= First_Elmt
(Discriminant_Constraint
(Typ
));
3223 Disc2
:= First_Elmt
(Discriminant_Constraint
(Etype
(Obj
)));
3224 while Present
(Disc1
) and then Present
(Disc2
) loop
3225 Val1
:= Node
(Disc1
);
3226 Val2
:= Node
(Disc2
);
3228 if not Is_OK_Static_Expression
(Val1
)
3229 or else not Is_OK_Static_Expression
(Val2
)
3231 Check
:= Make_Op_Ne
(Loc
,
3232 Left_Opnd
=> Duplicate_Subexpr
(Val1
),
3233 Right_Opnd
=> Duplicate_Subexpr
(Val2
));
3239 Cond
:= Make_Or_Else
(Loc
,
3241 Right_Opnd
=> Check
);
3244 elsif Expr_Value
(Val1
) /= Expr_Value
(Val2
) then
3245 Apply_Compile_Time_Constraint_Error
(Aggr
,
3246 Msg
=> "incorrect value for discriminant&??",
3247 Reason
=> CE_Discriminant_Check_Failed
,
3252 Next_Discriminant
(D
);
3257 -- If any discriminant constraint is non-static, emit a check
3259 if Present
(Cond
) then
3261 Make_Raise_Constraint_Error
(Loc
,
3263 Reason
=> CE_Discriminant_Check_Failed
));
3267 end Discriminants_Ok
;
3269 -- Start of processing for Convert_Aggr_In_Object_Decl
3272 Set_Assignment_OK
(Occ
);
3274 if Nkind
(Aggr
) = N_Qualified_Expression
then
3275 Aggr
:= Expression
(Aggr
);
3278 if Has_Discriminants
(Typ
)
3279 and then Typ
/= Etype
(Obj
)
3280 and then Is_Constrained
(Etype
(Obj
))
3281 and then not Discriminants_Ok
3286 -- If the context is an extended return statement, it has its own
3287 -- finalization machinery (i.e. works like a transient scope) and
3288 -- we do not want to create an additional one, because objects on
3289 -- the finalization list of the return must be moved to the caller's
3290 -- finalization list to complete the return.
3292 -- However, if the aggregate is limited, it is built in place, and the
3293 -- controlled components are not assigned to intermediate temporaries
3294 -- so there is no need for a transient scope in this case either.
3296 if Requires_Transient_Scope
(Typ
)
3297 and then Ekind
(Current_Scope
) /= E_Return_Statement
3298 and then not Is_Limited_Type
(Typ
)
3300 Establish_Transient_Scope
3303 Is_Controlled
(Typ
) or else Has_Controlled_Component
(Typ
));
3307 Node_After
: constant Node_Id
:= Next
(N
);
3309 Insert_Actions_After
(N
, Late_Expansion
(Aggr
, Typ
, Occ
));
3310 Collect_Initialization_Statements
(Obj
, N
, Node_After
);
3312 Set_No_Initialization
(N
);
3313 Initialize_Discriminants
(N
, Typ
);
3314 end Convert_Aggr_In_Object_Decl
;
3316 -------------------------------------
3317 -- Convert_Array_Aggr_In_Allocator --
3318 -------------------------------------
3320 procedure Convert_Array_Aggr_In_Allocator
3325 Aggr_Code
: List_Id
;
3326 Typ
: constant Entity_Id
:= Etype
(Aggr
);
3327 Ctyp
: constant Entity_Id
:= Component_Type
(Typ
);
3330 -- The target is an explicit dereference of the allocated object.
3331 -- Generate component assignments to it, as for an aggregate that
3332 -- appears on the right-hand side of an assignment statement.
3335 Build_Array_Aggr_Code
(Aggr
,
3337 Index
=> First_Index
(Typ
),
3339 Scalar_Comp
=> Is_Scalar_Type
(Ctyp
));
3341 Insert_Actions_After
(Decl
, Aggr_Code
);
3342 end Convert_Array_Aggr_In_Allocator
;
3344 ----------------------------
3345 -- Convert_To_Assignments --
3346 ----------------------------
3348 procedure Convert_To_Assignments
(N
: Node_Id
; Typ
: Entity_Id
) is
3349 Loc
: constant Source_Ptr
:= Sloc
(N
);
3353 Aggr_Code
: List_Id
;
3355 Target_Expr
: Node_Id
;
3356 Parent_Kind
: Node_Kind
;
3357 Unc_Decl
: Boolean := False;
3358 Parent_Node
: Node_Id
;
3361 pragma Assert
(not Is_Static_Dispatch_Table_Aggregate
(N
));
3362 pragma Assert
(Is_Record_Type
(Typ
));
3364 Parent_Node
:= Parent
(N
);
3365 Parent_Kind
:= Nkind
(Parent_Node
);
3367 if Parent_Kind
= N_Qualified_Expression
then
3369 -- Check if we are in a unconstrained declaration because in this
3370 -- case the current delayed expansion mechanism doesn't work when
3371 -- the declared object size depend on the initializing expr.
3374 Parent_Node
:= Parent
(Parent_Node
);
3375 Parent_Kind
:= Nkind
(Parent_Node
);
3377 if Parent_Kind
= N_Object_Declaration
then
3379 not Is_Entity_Name
(Object_Definition
(Parent_Node
))
3380 or else Has_Discriminants
3381 (Entity
(Object_Definition
(Parent_Node
)))
3382 or else Is_Class_Wide_Type
3383 (Entity
(Object_Definition
(Parent_Node
)));
3388 -- Just set the Delay flag in the cases where the transformation will be
3389 -- done top down from above.
3393 -- Internal aggregate (transformed when expanding the parent)
3395 or else Parent_Kind
= N_Aggregate
3396 or else Parent_Kind
= N_Extension_Aggregate
3397 or else Parent_Kind
= N_Component_Association
3399 -- Allocator (see Convert_Aggr_In_Allocator)
3401 or else Parent_Kind
= N_Allocator
3403 -- Object declaration (see Convert_Aggr_In_Object_Decl)
3405 or else (Parent_Kind
= N_Object_Declaration
and then not Unc_Decl
)
3407 -- Safe assignment (see Convert_Aggr_Assignments). So far only the
3408 -- assignments in init procs are taken into account.
3410 or else (Parent_Kind
= N_Assignment_Statement
3411 and then Inside_Init_Proc
)
3413 -- (Ada 2005) An inherently limited type in a return statement, which
3414 -- will be handled in a build-in-place fashion, and may be rewritten
3415 -- as an extended return and have its own finalization machinery.
3416 -- In the case of a simple return, the aggregate needs to be delayed
3417 -- until the scope for the return statement has been created, so
3418 -- that any finalization chain will be associated with that scope.
3419 -- For extended returns, we delay expansion to avoid the creation
3420 -- of an unwanted transient scope that could result in premature
3421 -- finalization of the return object (which is built in place
3422 -- within the caller's scope).
3425 (Is_Limited_View
(Typ
)
3427 (Nkind
(Parent
(Parent_Node
)) = N_Extended_Return_Statement
3428 or else Nkind
(Parent_Node
) = N_Simple_Return_Statement
))
3430 Set_Expansion_Delayed
(N
);
3434 -- Otherwise, if a transient scope is required, create it now. If we
3435 -- are within an initialization procedure do not create such, because
3436 -- the target of the assignment must not be declared within a local
3437 -- block, and because cleanup will take place on return from the
3438 -- initialization procedure.
3439 -- Should the condition be more restrictive ???
3441 if Requires_Transient_Scope
(Typ
) and then not Inside_Init_Proc
then
3442 Establish_Transient_Scope
(N
, Sec_Stack
=> Needs_Finalization
(Typ
));
3445 -- If the aggregate is non-limited, create a temporary. If it is limited
3446 -- and context is an assignment, this is a subaggregate for an enclosing
3447 -- aggregate being expanded. It must be built in place, so use target of
3448 -- the current assignment.
3450 if Is_Limited_Type
(Typ
)
3451 and then Nkind
(Parent
(N
)) = N_Assignment_Statement
3453 Target_Expr
:= New_Copy_Tree
(Name
(Parent
(N
)));
3454 Insert_Actions
(Parent
(N
),
3455 Build_Record_Aggr_Code
(N
, Typ
, Target_Expr
));
3456 Rewrite
(Parent
(N
), Make_Null_Statement
(Loc
));
3459 Temp
:= Make_Temporary
(Loc
, 'A', N
);
3461 -- If the type inherits unknown discriminants, use the view with
3462 -- known discriminants if available.
3464 if Has_Unknown_Discriminants
(Typ
)
3465 and then Present
(Underlying_Record_View
(Typ
))
3467 T
:= Underlying_Record_View
(Typ
);
3473 Make_Object_Declaration
(Loc
,
3474 Defining_Identifier
=> Temp
,
3475 Object_Definition
=> New_Occurrence_Of
(T
, Loc
));
3477 Set_No_Initialization
(Instr
);
3478 Insert_Action
(N
, Instr
);
3479 Initialize_Discriminants
(Instr
, T
);
3481 Target_Expr
:= New_Occurrence_Of
(Temp
, Loc
);
3482 Aggr_Code
:= Build_Record_Aggr_Code
(N
, T
, Target_Expr
);
3484 -- Save the last assignment statement associated with the aggregate
3485 -- when building a controlled object. This reference is utilized by
3486 -- the finalization machinery when marking an object as successfully
3489 if Needs_Finalization
(T
) then
3490 Set_Last_Aggregate_Assignment
(Temp
, Last
(Aggr_Code
));
3493 Insert_Actions
(N
, Aggr_Code
);
3494 Rewrite
(N
, New_Occurrence_Of
(Temp
, Loc
));
3495 Analyze_And_Resolve
(N
, T
);
3497 end Convert_To_Assignments
;
3499 ---------------------------
3500 -- Convert_To_Positional --
3501 ---------------------------
3503 procedure Convert_To_Positional
3505 Max_Others_Replicate
: Nat
:= 5;
3506 Handle_Bit_Packed
: Boolean := False)
3508 Typ
: constant Entity_Id
:= Etype
(N
);
3510 Static_Components
: Boolean := True;
3512 procedure Check_Static_Components
;
3513 -- Check whether all components of the aggregate are compile-time known
3514 -- values, and can be passed as is to the back-end without further
3520 Ixb
: Node_Id
) return Boolean;
3521 -- Convert the aggregate into a purely positional form if possible. On
3522 -- entry the bounds of all dimensions are known to be static, and the
3523 -- total number of components is safe enough to expand.
3525 function Is_Flat
(N
: Node_Id
; Dims
: Int
) return Boolean;
3526 -- Return True iff the array N is flat (which is not trivial in the case
3527 -- of multidimensional aggregates).
3529 -----------------------------
3530 -- Check_Static_Components --
3531 -----------------------------
3533 -- Could use some comments in this body ???
3535 procedure Check_Static_Components
is
3539 Static_Components
:= True;
3541 if Nkind
(N
) = N_String_Literal
then
3544 elsif Present
(Expressions
(N
)) then
3545 Expr
:= First
(Expressions
(N
));
3546 while Present
(Expr
) loop
3547 if Nkind
(Expr
) /= N_Aggregate
3548 or else not Compile_Time_Known_Aggregate
(Expr
)
3549 or else Expansion_Delayed
(Expr
)
3551 Static_Components
:= False;
3559 if Nkind
(N
) = N_Aggregate
3560 and then Present
(Component_Associations
(N
))
3562 Expr
:= First
(Component_Associations
(N
));
3563 while Present
(Expr
) loop
3564 if Nkind_In
(Expression
(Expr
), N_Integer_Literal
,
3569 elsif Is_Entity_Name
(Expression
(Expr
))
3570 and then Present
(Entity
(Expression
(Expr
)))
3571 and then Ekind
(Entity
(Expression
(Expr
))) =
3572 E_Enumeration_Literal
3576 elsif Nkind
(Expression
(Expr
)) /= N_Aggregate
3577 or else not Compile_Time_Known_Aggregate
(Expression
(Expr
))
3578 or else Expansion_Delayed
(Expression
(Expr
))
3580 Static_Components
:= False;
3587 end Check_Static_Components
;
3596 Ixb
: Node_Id
) return Boolean
3598 Loc
: constant Source_Ptr
:= Sloc
(N
);
3599 Blo
: constant Node_Id
:= Type_Low_Bound
(Etype
(Ixb
));
3600 Lo
: constant Node_Id
:= Type_Low_Bound
(Etype
(Ix
));
3601 Hi
: constant Node_Id
:= Type_High_Bound
(Etype
(Ix
));
3605 Others_Present
: Boolean := False;
3608 if Nkind
(Original_Node
(N
)) = N_String_Literal
then
3612 if not Compile_Time_Known_Value
(Lo
)
3613 or else not Compile_Time_Known_Value
(Hi
)
3618 Lov
:= Expr_Value
(Lo
);
3619 Hiv
:= Expr_Value
(Hi
);
3621 -- Check if there is an others choice
3623 if Present
(Component_Associations
(N
)) then
3629 Assoc
:= First
(Component_Associations
(N
));
3630 while Present
(Assoc
) loop
3632 -- If this is a box association, flattening is in general
3633 -- not possible because at this point we cannot tell if the
3634 -- default is static or even exists.
3636 if Box_Present
(Assoc
) then
3640 Choice
:= First
(Choices
(Assoc
));
3642 while Present
(Choice
) loop
3643 if Nkind
(Choice
) = N_Others_Choice
then
3644 Others_Present
:= True;
3655 -- If the low bound is not known at compile time and others is not
3656 -- present we can proceed since the bounds can be obtained from the
3659 -- Note: This case is required in VM platforms since their backends
3660 -- normalize array indexes in the range 0 .. N-1. Hence, if we do
3661 -- not flat an array whose bounds cannot be obtained from the type
3662 -- of the index the backend has no way to properly generate the code.
3663 -- See ACATS c460010 for an example.
3666 or else (not Compile_Time_Known_Value
(Blo
) and then Others_Present
)
3671 -- Determine if set of alternatives is suitable for conversion and
3672 -- build an array containing the values in sequence.
3675 Vals
: array (UI_To_Int
(Lov
) .. UI_To_Int
(Hiv
))
3676 of Node_Id
:= (others => Empty
);
3677 -- The values in the aggregate sorted appropriately
3680 -- Same data as Vals in list form
3683 -- Used to validate Max_Others_Replicate limit
3686 Num
: Int
:= UI_To_Int
(Lov
);
3692 if Present
(Expressions
(N
)) then
3693 Elmt
:= First
(Expressions
(N
));
3694 while Present
(Elmt
) loop
3695 if Nkind
(Elmt
) = N_Aggregate
3696 and then Present
(Next_Index
(Ix
))
3698 not Flatten
(Elmt
, Next_Index
(Ix
), Next_Index
(Ixb
))
3703 Vals
(Num
) := Relocate_Node
(Elmt
);
3710 if No
(Component_Associations
(N
)) then
3714 Elmt
:= First
(Component_Associations
(N
));
3716 if Nkind
(Expression
(Elmt
)) = N_Aggregate
then
3717 if Present
(Next_Index
(Ix
))
3720 (Expression
(Elmt
), Next_Index
(Ix
), Next_Index
(Ixb
))
3726 Component_Loop
: while Present
(Elmt
) loop
3727 Choice
:= First
(Choices
(Elmt
));
3728 Choice_Loop
: while Present
(Choice
) loop
3730 -- If we have an others choice, fill in the missing elements
3731 -- subject to the limit established by Max_Others_Replicate.
3733 if Nkind
(Choice
) = N_Others_Choice
then
3736 for J
in Vals
'Range loop
3737 if No
(Vals
(J
)) then
3738 Vals
(J
) := New_Copy_Tree
(Expression
(Elmt
));
3739 Rep_Count
:= Rep_Count
+ 1;
3741 -- Check for maximum others replication. Note that
3742 -- we skip this test if either of the restrictions
3743 -- No_Elaboration_Code or No_Implicit_Loops is
3744 -- active, if this is a preelaborable unit or
3745 -- a predefined unit, or if the unit must be
3746 -- placed in data memory. This also ensures that
3747 -- predefined units get the same level of constant
3748 -- folding in Ada 95 and Ada 2005, where their
3749 -- categorization has changed.
3752 P
: constant Entity_Id
:=
3753 Cunit_Entity
(Current_Sem_Unit
);
3756 -- Check if duplication OK and if so continue
3759 if Restriction_Active
(No_Elaboration_Code
)
3760 or else Restriction_Active
(No_Implicit_Loops
)
3762 (Ekind
(Current_Scope
) = E_Package
3763 and then Static_Elaboration_Desired
3765 or else Is_Preelaborated
(P
)
3766 or else (Ekind
(P
) = E_Package_Body
3768 Is_Preelaborated
(Spec_Entity
(P
)))
3770 Is_Predefined_File_Name
3771 (Unit_File_Name
(Get_Source_Unit
(P
)))
3775 -- If duplication not OK, then we return False
3776 -- if the replication count is too high
3778 elsif Rep_Count
> Max_Others_Replicate
then
3781 -- Continue on if duplication not OK, but the
3782 -- replication count is not excessive.
3791 exit Component_Loop
;
3793 -- Case of a subtype mark, identifier or expanded name
3795 elsif Is_Entity_Name
(Choice
)
3796 and then Is_Type
(Entity
(Choice
))
3798 Lo
:= Type_Low_Bound
(Etype
(Choice
));
3799 Hi
:= Type_High_Bound
(Etype
(Choice
));
3801 -- Case of subtype indication
3803 elsif Nkind
(Choice
) = N_Subtype_Indication
then
3804 Lo
:= Low_Bound
(Range_Expression
(Constraint
(Choice
)));
3805 Hi
:= High_Bound
(Range_Expression
(Constraint
(Choice
)));
3809 elsif Nkind
(Choice
) = N_Range
then
3810 Lo
:= Low_Bound
(Choice
);
3811 Hi
:= High_Bound
(Choice
);
3813 -- Normal subexpression case
3815 else pragma Assert
(Nkind
(Choice
) in N_Subexpr
);
3816 if not Compile_Time_Known_Value
(Choice
) then
3820 Choice_Index
:= UI_To_Int
(Expr_Value
(Choice
));
3822 if Choice_Index
in Vals
'Range then
3823 Vals
(Choice_Index
) :=
3824 New_Copy_Tree
(Expression
(Elmt
));
3827 -- Choice is statically out-of-range, will be
3828 -- rewritten to raise Constraint_Error.
3836 -- Range cases merge with Lo,Hi set
3838 if not Compile_Time_Known_Value
(Lo
)
3840 not Compile_Time_Known_Value
(Hi
)
3845 for J
in UI_To_Int
(Expr_Value
(Lo
)) ..
3846 UI_To_Int
(Expr_Value
(Hi
))
3848 Vals
(J
) := New_Copy_Tree
(Expression
(Elmt
));
3854 end loop Choice_Loop
;
3857 end loop Component_Loop
;
3859 -- If we get here the conversion is possible
3862 for J
in Vals
'Range loop
3863 Append
(Vals
(J
), Vlist
);
3866 Rewrite
(N
, Make_Aggregate
(Loc
, Expressions
=> Vlist
));
3867 Set_Aggregate_Bounds
(N
, Aggregate_Bounds
(Original_Node
(N
)));
3876 function Is_Flat
(N
: Node_Id
; Dims
: Int
) return Boolean is
3883 elsif Nkind
(N
) = N_Aggregate
then
3884 if Present
(Component_Associations
(N
)) then
3888 Elmt
:= First
(Expressions
(N
));
3889 while Present
(Elmt
) loop
3890 if not Is_Flat
(Elmt
, Dims
- 1) then
3904 -- Start of processing for Convert_To_Positional
3907 -- Ada 2005 (AI-287): Do not convert in case of default initialized
3908 -- components because in this case will need to call the corresponding
3911 if Has_Default_Init_Comps
(N
) then
3915 if Is_Flat
(N
, Number_Dimensions
(Typ
)) then
3919 if Is_Bit_Packed_Array
(Typ
) and then not Handle_Bit_Packed
then
3923 -- Do not convert to positional if controlled components are involved
3924 -- since these require special processing
3926 if Has_Controlled_Component
(Typ
) then
3930 Check_Static_Components
;
3932 -- If the size is known, or all the components are static, try to
3933 -- build a fully positional aggregate.
3935 -- The size of the type may not be known for an aggregate with
3936 -- discriminated array components, but if the components are static
3937 -- it is still possible to verify statically that the length is
3938 -- compatible with the upper bound of the type, and therefore it is
3939 -- worth flattening such aggregates as well.
3941 -- For now the back-end expands these aggregates into individual
3942 -- assignments to the target anyway, but it is conceivable that
3943 -- it will eventually be able to treat such aggregates statically???
3945 if Aggr_Size_OK
(N
, Typ
)
3946 and then Flatten
(N
, First_Index
(Typ
), First_Index
(Base_Type
(Typ
)))
3948 if Static_Components
then
3949 Set_Compile_Time_Known_Aggregate
(N
);
3950 Set_Expansion_Delayed
(N
, False);
3953 Analyze_And_Resolve
(N
, Typ
);
3956 -- Is Static_Eaboration_Desired has been specified, diagnose aggregates
3957 -- that will still require initialization code.
3959 if (Ekind
(Current_Scope
) = E_Package
3960 and then Static_Elaboration_Desired
(Current_Scope
))
3961 and then Nkind
(Parent
(N
)) = N_Object_Declaration
3967 if Nkind
(N
) = N_Aggregate
and then Present
(Expressions
(N
)) then
3968 Expr
:= First
(Expressions
(N
));
3969 while Present
(Expr
) loop
3970 if Nkind_In
(Expr
, N_Integer_Literal
, N_Real_Literal
)
3972 (Is_Entity_Name
(Expr
)
3973 and then Ekind
(Entity
(Expr
)) = E_Enumeration_Literal
)
3979 ("non-static object requires elaboration code??", N
);
3986 if Present
(Component_Associations
(N
)) then
3987 Error_Msg_N
("object requires elaboration code??", N
);
3992 end Convert_To_Positional
;
3994 ----------------------------
3995 -- Expand_Array_Aggregate --
3996 ----------------------------
3998 -- Array aggregate expansion proceeds as follows:
4000 -- 1. If requested we generate code to perform all the array aggregate
4001 -- bound checks, specifically
4003 -- (a) Check that the index range defined by aggregate bounds is
4004 -- compatible with corresponding index subtype.
4006 -- (b) If an others choice is present check that no aggregate
4007 -- index is outside the bounds of the index constraint.
4009 -- (c) For multidimensional arrays make sure that all subaggregates
4010 -- corresponding to the same dimension have the same bounds.
4012 -- 2. Check for packed array aggregate which can be converted to a
4013 -- constant so that the aggregate disappears completely.
4015 -- 3. Check case of nested aggregate. Generally nested aggregates are
4016 -- handled during the processing of the parent aggregate.
4018 -- 4. Check if the aggregate can be statically processed. If this is the
4019 -- case pass it as is to Gigi. Note that a necessary condition for
4020 -- static processing is that the aggregate be fully positional.
4022 -- 5. If in place aggregate expansion is possible (i.e. no need to create
4023 -- a temporary) then mark the aggregate as such and return. Otherwise
4024 -- create a new temporary and generate the appropriate initialization
4027 procedure Expand_Array_Aggregate
(N
: Node_Id
) is
4028 Loc
: constant Source_Ptr
:= Sloc
(N
);
4030 Typ
: constant Entity_Id
:= Etype
(N
);
4031 Ctyp
: constant Entity_Id
:= Component_Type
(Typ
);
4032 -- Typ is the correct constrained array subtype of the aggregate
4033 -- Ctyp is the corresponding component type.
4035 Aggr_Dimension
: constant Pos
:= Number_Dimensions
(Typ
);
4036 -- Number of aggregate index dimensions
4038 Aggr_Low
: array (1 .. Aggr_Dimension
) of Node_Id
;
4039 Aggr_High
: array (1 .. Aggr_Dimension
) of Node_Id
;
4040 -- Low and High bounds of the constraint for each aggregate index
4042 Aggr_Index_Typ
: array (1 .. Aggr_Dimension
) of Entity_Id
;
4043 -- The type of each index
4045 In_Place_Assign_OK_For_Declaration
: Boolean := False;
4046 -- True if we are to generate an in place assignment for a declaration
4048 Maybe_In_Place_OK
: Boolean;
4049 -- If the type is neither controlled nor packed and the aggregate
4050 -- is the expression in an assignment, assignment in place may be
4051 -- possible, provided other conditions are met on the LHS.
4053 Others_Present
: array (1 .. Aggr_Dimension
) of Boolean :=
4055 -- If Others_Present (J) is True, then there is an others choice
4056 -- in one of the sub-aggregates of N at dimension J.
4058 function Aggr_Assignment_OK_For_Backend
(N
: Node_Id
) return Boolean;
4059 -- Returns true if an aggregate assignment can be done by the back end
4061 procedure Build_Constrained_Type
(Positional
: Boolean);
4062 -- If the subtype is not static or unconstrained, build a constrained
4063 -- type using the computable sizes of the aggregate and its sub-
4066 procedure Check_Bounds
(Aggr_Bounds
: Node_Id
; Index_Bounds
: Node_Id
);
4067 -- Checks that the bounds of Aggr_Bounds are within the bounds defined
4070 procedure Check_Same_Aggr_Bounds
(Sub_Aggr
: Node_Id
; Dim
: Pos
);
4071 -- Checks that in a multi-dimensional array aggregate all subaggregates
4072 -- corresponding to the same dimension have the same bounds.
4073 -- Sub_Aggr is an array sub-aggregate. Dim is the dimension
4074 -- corresponding to the sub-aggregate.
4076 procedure Compute_Others_Present
(Sub_Aggr
: Node_Id
; Dim
: Pos
);
4077 -- Computes the values of array Others_Present. Sub_Aggr is the
4078 -- array sub-aggregate we start the computation from. Dim is the
4079 -- dimension corresponding to the sub-aggregate.
4081 function In_Place_Assign_OK
return Boolean;
4082 -- Simple predicate to determine whether an aggregate assignment can
4083 -- be done in place, because none of the new values can depend on the
4084 -- components of the target of the assignment.
4086 procedure Others_Check
(Sub_Aggr
: Node_Id
; Dim
: Pos
);
4087 -- Checks that if an others choice is present in any sub-aggregate no
4088 -- aggregate index is outside the bounds of the index constraint.
4089 -- Sub_Aggr is an array sub-aggregate. Dim is the dimension
4090 -- corresponding to the sub-aggregate.
4092 function Safe_Left_Hand_Side
(N
: Node_Id
) return Boolean;
4093 -- In addition to Maybe_In_Place_OK, in order for an aggregate to be
4094 -- built directly into the target of the assignment it must be free
4097 ------------------------------------
4098 -- Aggr_Assignment_OK_For_Backend --
4099 ------------------------------------
4101 -- Backend processing by Gigi/gcc is possible only if all the following
4102 -- conditions are met:
4104 -- 1. N consists of a single OTHERS choice, possibly recursively
4106 -- 2. The array type is not packed
4108 -- 3. The array type has no atomic components
4110 -- 4. The array type has no null ranges (the purpose of this is to
4111 -- avoid a bogus warning for an out-of-range value).
4113 -- 5. The component type is discrete
4115 -- 6. The component size is Storage_Unit or the value is of the form
4116 -- M * (1 + A**1 + A**2 + .. A**(K-1)) where A = 2**(Storage_Unit)
4117 -- and M in 1 .. A-1. This can also be viewed as K occurrences of
4118 -- the 8-bit value M, concatenated together.
4120 -- The ultimate goal is to generate a call to a fast memset routine
4121 -- specifically optimized for the target.
4123 function Aggr_Assignment_OK_For_Backend
(N
: Node_Id
) return Boolean is
4126 Expr
: Node_Id
:= N
;
4134 -- Recurse as far as possible to find the innermost component type
4137 while Is_Array_Type
(Ctyp
) loop
4138 if Nkind
(Expr
) /= N_Aggregate
4139 or else not Is_Others_Aggregate
(Expr
)
4144 if Present
(Packed_Array_Impl_Type
(Ctyp
)) then
4148 if Has_Atomic_Components
(Ctyp
) then
4152 Index
:= First_Index
(Ctyp
);
4153 while Present
(Index
) loop
4154 Get_Index_Bounds
(Index
, Low
, High
);
4156 if Is_Null_Range
(Low
, High
) then
4163 Expr
:= Expression
(First
(Component_Associations
(Expr
)));
4165 for J
in 1 .. Number_Dimensions
(Ctyp
) - 1 loop
4166 if Nkind
(Expr
) /= N_Aggregate
4167 or else not Is_Others_Aggregate
(Expr
)
4172 Expr
:= Expression
(First
(Component_Associations
(Expr
)));
4175 Ctyp
:= Component_Type
(Ctyp
);
4177 if Is_Atomic_Or_VFA
(Ctyp
) then
4182 if not Is_Discrete_Type
(Ctyp
) then
4186 -- The expression needs to be analyzed if True is returned
4188 Analyze_And_Resolve
(Expr
, Ctyp
);
4190 -- The back end uses the Esize as the precision of the type
4192 Nunits
:= UI_To_Int
(Esize
(Ctyp
)) / System_Storage_Unit
;
4198 if not Compile_Time_Known_Value
(Expr
) then
4202 Value
:= Expr_Value
(Expr
);
4204 if Has_Biased_Representation
(Ctyp
) then
4205 Value
:= Value
- Expr_Value
(Type_Low_Bound
(Ctyp
));
4208 -- Values 0 and -1 immediately satisfy the last check
4210 if Value
= Uint_0
or else Value
= Uint_Minus_1
then
4214 -- We need to work with an unsigned value
4217 Value
:= Value
+ 2**(System_Storage_Unit
* Nunits
);
4220 Remainder
:= Value
rem 2**System_Storage_Unit
;
4222 for J
in 1 .. Nunits
- 1 loop
4223 Value
:= Value
/ 2**System_Storage_Unit
;
4225 if Value
rem 2**System_Storage_Unit
/= Remainder
then
4231 end Aggr_Assignment_OK_For_Backend
;
4233 ----------------------------
4234 -- Build_Constrained_Type --
4235 ----------------------------
4237 procedure Build_Constrained_Type
(Positional
: Boolean) is
4238 Loc
: constant Source_Ptr
:= Sloc
(N
);
4239 Agg_Type
: constant Entity_Id
:= Make_Temporary
(Loc
, 'A');
4242 Typ
: constant Entity_Id
:= Etype
(N
);
4243 Indexes
: constant List_Id
:= New_List
;
4248 -- If the aggregate is purely positional, all its subaggregates
4249 -- have the same size. We collect the dimensions from the first
4250 -- subaggregate at each level.
4255 for D
in 1 .. Number_Dimensions
(Typ
) loop
4256 Sub_Agg
:= First
(Expressions
(Sub_Agg
));
4260 while Present
(Comp
) loop
4267 Low_Bound
=> Make_Integer_Literal
(Loc
, 1),
4268 High_Bound
=> Make_Integer_Literal
(Loc
, Num
)));
4272 -- We know the aggregate type is unconstrained and the aggregate
4273 -- is not processable by the back end, therefore not necessarily
4274 -- positional. Retrieve each dimension bounds (computed earlier).
4276 for D
in 1 .. Number_Dimensions
(Typ
) loop
4279 Low_Bound
=> Aggr_Low
(D
),
4280 High_Bound
=> Aggr_High
(D
)));
4285 Make_Full_Type_Declaration
(Loc
,
4286 Defining_Identifier
=> Agg_Type
,
4288 Make_Constrained_Array_Definition
(Loc
,
4289 Discrete_Subtype_Definitions
=> Indexes
,
4290 Component_Definition
=>
4291 Make_Component_Definition
(Loc
,
4292 Aliased_Present
=> False,
4293 Subtype_Indication
=>
4294 New_Occurrence_Of
(Component_Type
(Typ
), Loc
))));
4296 Insert_Action
(N
, Decl
);
4298 Set_Etype
(N
, Agg_Type
);
4299 Set_Is_Itype
(Agg_Type
);
4300 Freeze_Itype
(Agg_Type
, N
);
4301 end Build_Constrained_Type
;
4307 procedure Check_Bounds
(Aggr_Bounds
: Node_Id
; Index_Bounds
: Node_Id
) is
4314 Cond
: Node_Id
:= Empty
;
4317 Get_Index_Bounds
(Aggr_Bounds
, Aggr_Lo
, Aggr_Hi
);
4318 Get_Index_Bounds
(Index_Bounds
, Ind_Lo
, Ind_Hi
);
4320 -- Generate the following test:
4322 -- [constraint_error when
4323 -- Aggr_Lo <= Aggr_Hi and then
4324 -- (Aggr_Lo < Ind_Lo or else Aggr_Hi > Ind_Hi)]
4326 -- As an optimization try to see if some tests are trivially vacuous
4327 -- because we are comparing an expression against itself.
4329 if Aggr_Lo
= Ind_Lo
and then Aggr_Hi
= Ind_Hi
then
4332 elsif Aggr_Hi
= Ind_Hi
then
4335 Left_Opnd
=> Duplicate_Subexpr_Move_Checks
(Aggr_Lo
),
4336 Right_Opnd
=> Duplicate_Subexpr_Move_Checks
(Ind_Lo
));
4338 elsif Aggr_Lo
= Ind_Lo
then
4341 Left_Opnd
=> Duplicate_Subexpr_Move_Checks
(Aggr_Hi
),
4342 Right_Opnd
=> Duplicate_Subexpr_Move_Checks
(Ind_Hi
));
4349 Left_Opnd
=> Duplicate_Subexpr_Move_Checks
(Aggr_Lo
),
4350 Right_Opnd
=> Duplicate_Subexpr_Move_Checks
(Ind_Lo
)),
4354 Left_Opnd
=> Duplicate_Subexpr
(Aggr_Hi
),
4355 Right_Opnd
=> Duplicate_Subexpr
(Ind_Hi
)));
4358 if Present
(Cond
) then
4363 Left_Opnd
=> Duplicate_Subexpr_Move_Checks
(Aggr_Lo
),
4364 Right_Opnd
=> Duplicate_Subexpr_Move_Checks
(Aggr_Hi
)),
4366 Right_Opnd
=> Cond
);
4368 Set_Analyzed
(Left_Opnd
(Left_Opnd
(Cond
)), False);
4369 Set_Analyzed
(Right_Opnd
(Left_Opnd
(Cond
)), False);
4371 Make_Raise_Constraint_Error
(Loc
,
4373 Reason
=> CE_Range_Check_Failed
));
4377 ----------------------------
4378 -- Check_Same_Aggr_Bounds --
4379 ----------------------------
4381 procedure Check_Same_Aggr_Bounds
(Sub_Aggr
: Node_Id
; Dim
: Pos
) is
4382 Sub_Lo
: constant Node_Id
:= Low_Bound
(Aggregate_Bounds
(Sub_Aggr
));
4383 Sub_Hi
: constant Node_Id
:= High_Bound
(Aggregate_Bounds
(Sub_Aggr
));
4384 -- The bounds of this specific sub-aggregate
4386 Aggr_Lo
: constant Node_Id
:= Aggr_Low
(Dim
);
4387 Aggr_Hi
: constant Node_Id
:= Aggr_High
(Dim
);
4388 -- The bounds of the aggregate for this dimension
4390 Ind_Typ
: constant Entity_Id
:= Aggr_Index_Typ
(Dim
);
4391 -- The index type for this dimension.xxx
4393 Cond
: Node_Id
:= Empty
;
4398 -- If index checks are on generate the test
4400 -- [constraint_error when
4401 -- Aggr_Lo /= Sub_Lo or else Aggr_Hi /= Sub_Hi]
4403 -- As an optimization try to see if some tests are trivially vacuos
4404 -- because we are comparing an expression against itself. Also for
4405 -- the first dimension the test is trivially vacuous because there
4406 -- is just one aggregate for dimension 1.
4408 if Index_Checks_Suppressed
(Ind_Typ
) then
4411 elsif Dim
= 1 or else (Aggr_Lo
= Sub_Lo
and then Aggr_Hi
= Sub_Hi
)
4415 elsif Aggr_Hi
= Sub_Hi
then
4418 Left_Opnd
=> Duplicate_Subexpr_Move_Checks
(Aggr_Lo
),
4419 Right_Opnd
=> Duplicate_Subexpr_Move_Checks
(Sub_Lo
));
4421 elsif Aggr_Lo
= Sub_Lo
then
4424 Left_Opnd
=> Duplicate_Subexpr_Move_Checks
(Aggr_Hi
),
4425 Right_Opnd
=> Duplicate_Subexpr_Move_Checks
(Sub_Hi
));
4432 Left_Opnd
=> Duplicate_Subexpr_Move_Checks
(Aggr_Lo
),
4433 Right_Opnd
=> Duplicate_Subexpr_Move_Checks
(Sub_Lo
)),
4437 Left_Opnd
=> Duplicate_Subexpr
(Aggr_Hi
),
4438 Right_Opnd
=> Duplicate_Subexpr
(Sub_Hi
)));
4441 if Present
(Cond
) then
4443 Make_Raise_Constraint_Error
(Loc
,
4445 Reason
=> CE_Length_Check_Failed
));
4448 -- Now look inside the sub-aggregate to see if there is more work
4450 if Dim
< Aggr_Dimension
then
4452 -- Process positional components
4454 if Present
(Expressions
(Sub_Aggr
)) then
4455 Expr
:= First
(Expressions
(Sub_Aggr
));
4456 while Present
(Expr
) loop
4457 Check_Same_Aggr_Bounds
(Expr
, Dim
+ 1);
4462 -- Process component associations
4464 if Present
(Component_Associations
(Sub_Aggr
)) then
4465 Assoc
:= First
(Component_Associations
(Sub_Aggr
));
4466 while Present
(Assoc
) loop
4467 Expr
:= Expression
(Assoc
);
4468 Check_Same_Aggr_Bounds
(Expr
, Dim
+ 1);
4473 end Check_Same_Aggr_Bounds
;
4475 ----------------------------
4476 -- Compute_Others_Present --
4477 ----------------------------
4479 procedure Compute_Others_Present
(Sub_Aggr
: Node_Id
; Dim
: Pos
) is
4484 if Present
(Component_Associations
(Sub_Aggr
)) then
4485 Assoc
:= Last
(Component_Associations
(Sub_Aggr
));
4487 if Nkind
(First
(Choices
(Assoc
))) = N_Others_Choice
then
4488 Others_Present
(Dim
) := True;
4492 -- Now look inside the sub-aggregate to see if there is more work
4494 if Dim
< Aggr_Dimension
then
4496 -- Process positional components
4498 if Present
(Expressions
(Sub_Aggr
)) then
4499 Expr
:= First
(Expressions
(Sub_Aggr
));
4500 while Present
(Expr
) loop
4501 Compute_Others_Present
(Expr
, Dim
+ 1);
4506 -- Process component associations
4508 if Present
(Component_Associations
(Sub_Aggr
)) then
4509 Assoc
:= First
(Component_Associations
(Sub_Aggr
));
4510 while Present
(Assoc
) loop
4511 Expr
:= Expression
(Assoc
);
4512 Compute_Others_Present
(Expr
, Dim
+ 1);
4517 end Compute_Others_Present
;
4519 ------------------------
4520 -- In_Place_Assign_OK --
4521 ------------------------
4523 function In_Place_Assign_OK
return Boolean is
4531 function Safe_Aggregate
(Aggr
: Node_Id
) return Boolean;
4532 -- Check recursively that each component of a (sub)aggregate does
4533 -- not depend on the variable being assigned to.
4535 function Safe_Component
(Expr
: Node_Id
) return Boolean;
4536 -- Verify that an expression cannot depend on the variable being
4537 -- assigned to. Room for improvement here (but less than before).
4539 --------------------
4540 -- Safe_Aggregate --
4541 --------------------
4543 function Safe_Aggregate
(Aggr
: Node_Id
) return Boolean is
4547 if Present
(Expressions
(Aggr
)) then
4548 Expr
:= First
(Expressions
(Aggr
));
4549 while Present
(Expr
) loop
4550 if Nkind
(Expr
) = N_Aggregate
then
4551 if not Safe_Aggregate
(Expr
) then
4555 elsif not Safe_Component
(Expr
) then
4563 if Present
(Component_Associations
(Aggr
)) then
4564 Expr
:= First
(Component_Associations
(Aggr
));
4565 while Present
(Expr
) loop
4566 if Nkind
(Expression
(Expr
)) = N_Aggregate
then
4567 if not Safe_Aggregate
(Expression
(Expr
)) then
4571 -- If association has a box, no way to determine yet
4572 -- whether default can be assigned in place.
4574 elsif Box_Present
(Expr
) then
4577 elsif not Safe_Component
(Expression
(Expr
)) then
4588 --------------------
4589 -- Safe_Component --
4590 --------------------
4592 function Safe_Component
(Expr
: Node_Id
) return Boolean is
4593 Comp
: Node_Id
:= Expr
;
4595 function Check_Component
(Comp
: Node_Id
) return Boolean;
4596 -- Do the recursive traversal, after copy
4598 ---------------------
4599 -- Check_Component --
4600 ---------------------
4602 function Check_Component
(Comp
: Node_Id
) return Boolean is
4604 if Is_Overloaded
(Comp
) then
4608 return Compile_Time_Known_Value
(Comp
)
4610 or else (Is_Entity_Name
(Comp
)
4611 and then Present
(Entity
(Comp
))
4612 and then No
(Renamed_Object
(Entity
(Comp
))))
4614 or else (Nkind
(Comp
) = N_Attribute_Reference
4615 and then Check_Component
(Prefix
(Comp
)))
4617 or else (Nkind
(Comp
) in N_Binary_Op
4618 and then Check_Component
(Left_Opnd
(Comp
))
4619 and then Check_Component
(Right_Opnd
(Comp
)))
4621 or else (Nkind
(Comp
) in N_Unary_Op
4622 and then Check_Component
(Right_Opnd
(Comp
)))
4624 or else (Nkind
(Comp
) = N_Selected_Component
4625 and then Check_Component
(Prefix
(Comp
)))
4627 or else (Nkind
(Comp
) = N_Unchecked_Type_Conversion
4628 and then Check_Component
(Expression
(Comp
)));
4629 end Check_Component
;
4631 -- Start of processing for Safe_Component
4634 -- If the component appears in an association that may correspond
4635 -- to more than one element, it is not analyzed before expansion
4636 -- into assignments, to avoid side effects. We analyze, but do not
4637 -- resolve the copy, to obtain sufficient entity information for
4638 -- the checks that follow. If component is overloaded we assume
4639 -- an unsafe function call.
4641 if not Analyzed
(Comp
) then
4642 if Is_Overloaded
(Expr
) then
4645 elsif Nkind
(Expr
) = N_Aggregate
4646 and then not Is_Others_Aggregate
(Expr
)
4650 elsif Nkind
(Expr
) = N_Allocator
then
4652 -- For now, too complex to analyze
4657 Comp
:= New_Copy_Tree
(Expr
);
4658 Set_Parent
(Comp
, Parent
(Expr
));
4662 if Nkind
(Comp
) = N_Aggregate
then
4663 return Safe_Aggregate
(Comp
);
4665 return Check_Component
(Comp
);
4669 -- Start of processing for In_Place_Assign_OK
4672 if Present
(Component_Associations
(N
)) then
4674 -- On assignment, sliding can take place, so we cannot do the
4675 -- assignment in place unless the bounds of the aggregate are
4676 -- statically equal to those of the target.
4678 -- If the aggregate is given by an others choice, the bounds are
4679 -- derived from the left-hand side, and the assignment is safe if
4680 -- the expression is.
4682 if Is_Others_Aggregate
(N
) then
4685 (Expression
(First
(Component_Associations
(N
))));
4688 Aggr_In
:= First_Index
(Etype
(N
));
4690 if Nkind
(Parent
(N
)) = N_Assignment_Statement
then
4691 Obj_In
:= First_Index
(Etype
(Name
(Parent
(N
))));
4694 -- Context is an allocator. Check bounds of aggregate against
4695 -- given type in qualified expression.
4697 pragma Assert
(Nkind
(Parent
(Parent
(N
))) = N_Allocator
);
4699 First_Index
(Etype
(Entity
(Subtype_Mark
(Parent
(N
)))));
4702 while Present
(Aggr_In
) loop
4703 Get_Index_Bounds
(Aggr_In
, Aggr_Lo
, Aggr_Hi
);
4704 Get_Index_Bounds
(Obj_In
, Obj_Lo
, Obj_Hi
);
4706 if not Compile_Time_Known_Value
(Aggr_Lo
)
4707 or else not Compile_Time_Known_Value
(Aggr_Hi
)
4708 or else not Compile_Time_Known_Value
(Obj_Lo
)
4709 or else not Compile_Time_Known_Value
(Obj_Hi
)
4710 or else Expr_Value
(Aggr_Lo
) /= Expr_Value
(Obj_Lo
)
4711 or else Expr_Value
(Aggr_Hi
) /= Expr_Value
(Obj_Hi
)
4716 Next_Index
(Aggr_In
);
4717 Next_Index
(Obj_In
);
4721 -- Now check the component values themselves
4723 return Safe_Aggregate
(N
);
4724 end In_Place_Assign_OK
;
4730 procedure Others_Check
(Sub_Aggr
: Node_Id
; Dim
: Pos
) is
4731 Aggr_Lo
: constant Node_Id
:= Aggr_Low
(Dim
);
4732 Aggr_Hi
: constant Node_Id
:= Aggr_High
(Dim
);
4733 -- The bounds of the aggregate for this dimension
4735 Ind_Typ
: constant Entity_Id
:= Aggr_Index_Typ
(Dim
);
4736 -- The index type for this dimension
4738 Need_To_Check
: Boolean := False;
4740 Choices_Lo
: Node_Id
:= Empty
;
4741 Choices_Hi
: Node_Id
:= Empty
;
4742 -- The lowest and highest discrete choices for a named sub-aggregate
4744 Nb_Choices
: Int
:= -1;
4745 -- The number of discrete non-others choices in this sub-aggregate
4747 Nb_Elements
: Uint
:= Uint_0
;
4748 -- The number of elements in a positional aggregate
4750 Cond
: Node_Id
:= Empty
;
4757 -- Check if we have an others choice. If we do make sure that this
4758 -- sub-aggregate contains at least one element in addition to the
4761 if Range_Checks_Suppressed
(Ind_Typ
) then
4762 Need_To_Check
:= False;
4764 elsif Present
(Expressions
(Sub_Aggr
))
4765 and then Present
(Component_Associations
(Sub_Aggr
))
4767 Need_To_Check
:= True;
4769 elsif Present
(Component_Associations
(Sub_Aggr
)) then
4770 Assoc
:= Last
(Component_Associations
(Sub_Aggr
));
4772 if Nkind
(First
(Choices
(Assoc
))) /= N_Others_Choice
then
4773 Need_To_Check
:= False;
4776 -- Count the number of discrete choices. Start with -1 because
4777 -- the others choice does not count.
4779 -- Is there some reason we do not use List_Length here ???
4782 Assoc
:= First
(Component_Associations
(Sub_Aggr
));
4783 while Present
(Assoc
) loop
4784 Choice
:= First
(Choices
(Assoc
));
4785 while Present
(Choice
) loop
4786 Nb_Choices
:= Nb_Choices
+ 1;
4793 -- If there is only an others choice nothing to do
4795 Need_To_Check
:= (Nb_Choices
> 0);
4799 Need_To_Check
:= False;
4802 -- If we are dealing with a positional sub-aggregate with an others
4803 -- choice then compute the number or positional elements.
4805 if Need_To_Check
and then Present
(Expressions
(Sub_Aggr
)) then
4806 Expr
:= First
(Expressions
(Sub_Aggr
));
4807 Nb_Elements
:= Uint_0
;
4808 while Present
(Expr
) loop
4809 Nb_Elements
:= Nb_Elements
+ 1;
4813 -- If the aggregate contains discrete choices and an others choice
4814 -- compute the smallest and largest discrete choice values.
4816 elsif Need_To_Check
then
4817 Compute_Choices_Lo_And_Choices_Hi
: declare
4819 Table
: Case_Table_Type
(1 .. Nb_Choices
);
4820 -- Used to sort all the different choice values
4827 Assoc
:= First
(Component_Associations
(Sub_Aggr
));
4828 while Present
(Assoc
) loop
4829 Choice
:= First
(Choices
(Assoc
));
4830 while Present
(Choice
) loop
4831 if Nkind
(Choice
) = N_Others_Choice
then
4835 Get_Index_Bounds
(Choice
, Low
, High
);
4836 Table
(J
).Choice_Lo
:= Low
;
4837 Table
(J
).Choice_Hi
:= High
;
4846 -- Sort the discrete choices
4848 Sort_Case_Table
(Table
);
4850 Choices_Lo
:= Table
(1).Choice_Lo
;
4851 Choices_Hi
:= Table
(Nb_Choices
).Choice_Hi
;
4852 end Compute_Choices_Lo_And_Choices_Hi
;
4855 -- If no others choice in this sub-aggregate, or the aggregate
4856 -- comprises only an others choice, nothing to do.
4858 if not Need_To_Check
then
4861 -- If we are dealing with an aggregate containing an others choice
4862 -- and positional components, we generate the following test:
4864 -- if Ind_Typ'Pos (Aggr_Lo) + (Nb_Elements - 1) >
4865 -- Ind_Typ'Pos (Aggr_Hi)
4867 -- raise Constraint_Error;
4870 elsif Nb_Elements
> Uint_0
then
4876 Make_Attribute_Reference
(Loc
,
4877 Prefix
=> New_Occurrence_Of
(Ind_Typ
, Loc
),
4878 Attribute_Name
=> Name_Pos
,
4881 (Duplicate_Subexpr_Move_Checks
(Aggr_Lo
))),
4882 Right_Opnd
=> Make_Integer_Literal
(Loc
, Nb_Elements
- 1)),
4885 Make_Attribute_Reference
(Loc
,
4886 Prefix
=> New_Occurrence_Of
(Ind_Typ
, Loc
),
4887 Attribute_Name
=> Name_Pos
,
4888 Expressions
=> New_List
(
4889 Duplicate_Subexpr_Move_Checks
(Aggr_Hi
))));
4891 -- If we are dealing with an aggregate containing an others choice
4892 -- and discrete choices we generate the following test:
4894 -- [constraint_error when
4895 -- Choices_Lo < Aggr_Lo or else Choices_Hi > Aggr_Hi];
4902 Left_Opnd
=> Duplicate_Subexpr_Move_Checks
(Choices_Lo
),
4903 Right_Opnd
=> Duplicate_Subexpr_Move_Checks
(Aggr_Lo
)),
4907 Left_Opnd
=> Duplicate_Subexpr
(Choices_Hi
),
4908 Right_Opnd
=> Duplicate_Subexpr
(Aggr_Hi
)));
4911 if Present
(Cond
) then
4913 Make_Raise_Constraint_Error
(Loc
,
4915 Reason
=> CE_Length_Check_Failed
));
4916 -- Questionable reason code, shouldn't that be a
4917 -- CE_Range_Check_Failed ???
4920 -- Now look inside the sub-aggregate to see if there is more work
4922 if Dim
< Aggr_Dimension
then
4924 -- Process positional components
4926 if Present
(Expressions
(Sub_Aggr
)) then
4927 Expr
:= First
(Expressions
(Sub_Aggr
));
4928 while Present
(Expr
) loop
4929 Others_Check
(Expr
, Dim
+ 1);
4934 -- Process component associations
4936 if Present
(Component_Associations
(Sub_Aggr
)) then
4937 Assoc
:= First
(Component_Associations
(Sub_Aggr
));
4938 while Present
(Assoc
) loop
4939 Expr
:= Expression
(Assoc
);
4940 Others_Check
(Expr
, Dim
+ 1);
4947 -------------------------
4948 -- Safe_Left_Hand_Side --
4949 -------------------------
4951 function Safe_Left_Hand_Side
(N
: Node_Id
) return Boolean is
4952 function Is_Safe_Index
(Indx
: Node_Id
) return Boolean;
4953 -- If the left-hand side includes an indexed component, check that
4954 -- the indexes are free of side-effect.
4960 function Is_Safe_Index
(Indx
: Node_Id
) return Boolean is
4962 if Is_Entity_Name
(Indx
) then
4965 elsif Nkind
(Indx
) = N_Integer_Literal
then
4968 elsif Nkind
(Indx
) = N_Function_Call
4969 and then Is_Entity_Name
(Name
(Indx
))
4970 and then Has_Pragma_Pure_Function
(Entity
(Name
(Indx
)))
4974 elsif Nkind
(Indx
) = N_Type_Conversion
4975 and then Is_Safe_Index
(Expression
(Indx
))
4984 -- Start of processing for Safe_Left_Hand_Side
4987 if Is_Entity_Name
(N
) then
4990 elsif Nkind_In
(N
, N_Explicit_Dereference
, N_Selected_Component
)
4991 and then Safe_Left_Hand_Side
(Prefix
(N
))
4995 elsif Nkind
(N
) = N_Indexed_Component
4996 and then Safe_Left_Hand_Side
(Prefix
(N
))
4997 and then Is_Safe_Index
(First
(Expressions
(N
)))
5001 elsif Nkind
(N
) = N_Unchecked_Type_Conversion
then
5002 return Safe_Left_Hand_Side
(Expression
(N
));
5007 end Safe_Left_Hand_Side
;
5012 -- Holds the temporary aggregate value
5015 -- Holds the declaration of Tmp
5017 Aggr_Code
: List_Id
;
5018 Parent_Node
: Node_Id
;
5019 Parent_Kind
: Node_Kind
;
5021 -- Start of processing for Expand_Array_Aggregate
5024 -- Do not touch the special aggregates of attributes used for Asm calls
5026 if Is_RTE
(Ctyp
, RE_Asm_Input_Operand
)
5027 or else Is_RTE
(Ctyp
, RE_Asm_Output_Operand
)
5031 -- Do not expand an aggregate for an array type which contains tasks if
5032 -- the aggregate is associated with an unexpanded return statement of a
5033 -- build-in-place function. The aggregate is expanded when the related
5034 -- return statement (rewritten into an extended return) is processed.
5035 -- This delay ensures that any temporaries and initialization code
5036 -- generated for the aggregate appear in the proper return block and
5037 -- use the correct _chain and _master.
5039 elsif Has_Task
(Base_Type
(Etype
(N
)))
5040 and then Nkind
(Parent
(N
)) = N_Simple_Return_Statement
5041 and then Is_Build_In_Place_Function
5042 (Return_Applies_To
(Return_Statement_Entity
(Parent
(N
))))
5046 -- Do not attempt expansion if error already detected. We may reach this
5047 -- point in spite of previous errors when compiling with -gnatq, to
5048 -- force all possible errors (this is the usual ACATS mode).
5050 elsif Error_Posted
(N
) then
5054 -- If the semantic analyzer has determined that aggregate N will raise
5055 -- Constraint_Error at run time, then the aggregate node has been
5056 -- replaced with an N_Raise_Constraint_Error node and we should
5059 pragma Assert
(not Raises_Constraint_Error
(N
));
5063 -- Check that the index range defined by aggregate bounds is
5064 -- compatible with corresponding index subtype.
5066 Index_Compatibility_Check
: declare
5067 Aggr_Index_Range
: Node_Id
:= First_Index
(Typ
);
5068 -- The current aggregate index range
5070 Index_Constraint
: Node_Id
:= First_Index
(Etype
(Typ
));
5071 -- The corresponding index constraint against which we have to
5072 -- check the above aggregate index range.
5075 Compute_Others_Present
(N
, 1);
5077 for J
in 1 .. Aggr_Dimension
loop
5078 -- There is no need to emit a check if an others choice is present
5079 -- for this array aggregate dimension since in this case one of
5080 -- N's sub-aggregates has taken its bounds from the context and
5081 -- these bounds must have been checked already. In addition all
5082 -- sub-aggregates corresponding to the same dimension must all
5083 -- have the same bounds (checked in (c) below).
5085 if not Range_Checks_Suppressed
(Etype
(Index_Constraint
))
5086 and then not Others_Present
(J
)
5088 -- We don't use Checks.Apply_Range_Check here because it emits
5089 -- a spurious check. Namely it checks that the range defined by
5090 -- the aggregate bounds is non empty. But we know this already
5093 Check_Bounds
(Aggr_Index_Range
, Index_Constraint
);
5096 -- Save the low and high bounds of the aggregate index as well as
5097 -- the index type for later use in checks (b) and (c) below.
5099 Aggr_Low
(J
) := Low_Bound
(Aggr_Index_Range
);
5100 Aggr_High
(J
) := High_Bound
(Aggr_Index_Range
);
5102 Aggr_Index_Typ
(J
) := Etype
(Index_Constraint
);
5104 Next_Index
(Aggr_Index_Range
);
5105 Next_Index
(Index_Constraint
);
5107 end Index_Compatibility_Check
;
5111 -- If an others choice is present check that no aggregate index is
5112 -- outside the bounds of the index constraint.
5114 Others_Check
(N
, 1);
5118 -- For multidimensional arrays make sure that all subaggregates
5119 -- corresponding to the same dimension have the same bounds.
5121 if Aggr_Dimension
> 1 then
5122 Check_Same_Aggr_Bounds
(N
, 1);
5127 -- If we have a default component value, or simple initialization is
5128 -- required for the component type, then we replace <> in component
5129 -- associations by the required default value.
5132 Default_Val
: Node_Id
;
5136 if (Present
(Default_Aspect_Component_Value
(Typ
))
5137 or else Needs_Simple_Initialization
(Ctyp
))
5138 and then Present
(Component_Associations
(N
))
5140 Assoc
:= First
(Component_Associations
(N
));
5141 while Present
(Assoc
) loop
5142 if Nkind
(Assoc
) = N_Component_Association
5143 and then Box_Present
(Assoc
)
5145 Set_Box_Present
(Assoc
, False);
5147 if Present
(Default_Aspect_Component_Value
(Typ
)) then
5148 Default_Val
:= Default_Aspect_Component_Value
(Typ
);
5150 Default_Val
:= Get_Simple_Init_Val
(Ctyp
, N
);
5153 Set_Expression
(Assoc
, New_Copy_Tree
(Default_Val
));
5154 Analyze_And_Resolve
(Expression
(Assoc
), Ctyp
);
5164 -- Here we test for is packed array aggregate that we can handle at
5165 -- compile time. If so, return with transformation done. Note that we do
5166 -- this even if the aggregate is nested, because once we have done this
5167 -- processing, there is no more nested aggregate.
5169 if Packed_Array_Aggregate_Handled
(N
) then
5173 -- At this point we try to convert to positional form
5175 if Ekind
(Current_Scope
) = E_Package
5176 and then Static_Elaboration_Desired
(Current_Scope
)
5178 Convert_To_Positional
(N
, Max_Others_Replicate
=> 100);
5180 Convert_To_Positional
(N
);
5183 -- if the result is no longer an aggregate (e.g. it may be a string
5184 -- literal, or a temporary which has the needed value), then we are
5185 -- done, since there is no longer a nested aggregate.
5187 if Nkind
(N
) /= N_Aggregate
then
5190 -- We are also done if the result is an analyzed aggregate, indicating
5191 -- that Convert_To_Positional succeeded and reanalyzed the rewritten
5194 elsif Analyzed
(N
) and then N
/= Original_Node
(N
) then
5198 -- If all aggregate components are compile-time known and the aggregate
5199 -- has been flattened, nothing left to do. The same occurs if the
5200 -- aggregate is used to initialize the components of a statically
5201 -- allocated dispatch table.
5203 if Compile_Time_Known_Aggregate
(N
)
5204 or else Is_Static_Dispatch_Table_Aggregate
(N
)
5206 Set_Expansion_Delayed
(N
, False);
5210 -- Now see if back end processing is possible
5212 if Backend_Processing_Possible
(N
) then
5214 -- If the aggregate is static but the constraints are not, build
5215 -- a static subtype for the aggregate, so that Gigi can place it
5216 -- in static memory. Perform an unchecked_conversion to the non-
5217 -- static type imposed by the context.
5220 Itype
: constant Entity_Id
:= Etype
(N
);
5222 Needs_Type
: Boolean := False;
5225 Index
:= First_Index
(Itype
);
5226 while Present
(Index
) loop
5227 if not Is_OK_Static_Subtype
(Etype
(Index
)) then
5236 Build_Constrained_Type
(Positional
=> True);
5237 Rewrite
(N
, Unchecked_Convert_To
(Itype
, N
));
5247 -- Delay expansion for nested aggregates: it will be taken care of
5248 -- when the parent aggregate is expanded.
5250 Parent_Node
:= Parent
(N
);
5251 Parent_Kind
:= Nkind
(Parent_Node
);
5253 if Parent_Kind
= N_Qualified_Expression
then
5254 Parent_Node
:= Parent
(Parent_Node
);
5255 Parent_Kind
:= Nkind
(Parent_Node
);
5258 if Parent_Kind
= N_Aggregate
5259 or else Parent_Kind
= N_Extension_Aggregate
5260 or else Parent_Kind
= N_Component_Association
5261 or else (Parent_Kind
= N_Object_Declaration
5262 and then Needs_Finalization
(Typ
))
5263 or else (Parent_Kind
= N_Assignment_Statement
5264 and then Inside_Init_Proc
)
5266 if Static_Array_Aggregate
(N
)
5267 or else Compile_Time_Known_Aggregate
(N
)
5269 Set_Expansion_Delayed
(N
, False);
5272 Set_Expansion_Delayed
(N
);
5279 -- Look if in place aggregate expansion is possible
5281 -- For object declarations we build the aggregate in place, unless
5282 -- the array is bit-packed or the component is controlled.
5284 -- For assignments we do the assignment in place if all the component
5285 -- associations have compile-time known values. For other cases we
5286 -- create a temporary. The analysis for safety of on-line assignment
5287 -- is delicate, i.e. we don't know how to do it fully yet ???
5289 -- For allocators we assign to the designated object in place if the
5290 -- aggregate meets the same conditions as other in-place assignments.
5291 -- In this case the aggregate may not come from source but was created
5292 -- for default initialization, e.g. with Initialize_Scalars.
5294 if Requires_Transient_Scope
(Typ
) then
5295 Establish_Transient_Scope
5296 (N
, Sec_Stack
=> Has_Controlled_Component
(Typ
));
5299 if Has_Default_Init_Comps
(N
) then
5300 Maybe_In_Place_OK
:= False;
5302 elsif Is_Bit_Packed_Array
(Typ
)
5303 or else Has_Controlled_Component
(Typ
)
5305 Maybe_In_Place_OK
:= False;
5308 Maybe_In_Place_OK
:=
5309 (Nkind
(Parent
(N
)) = N_Assignment_Statement
5310 and then In_Place_Assign_OK
)
5313 (Nkind
(Parent
(Parent
(N
))) = N_Allocator
5314 and then In_Place_Assign_OK
);
5317 -- If this is an array of tasks, it will be expanded into build-in-place
5318 -- assignments. Build an activation chain for the tasks now.
5320 if Has_Task
(Etype
(N
)) then
5321 Build_Activation_Chain_Entity
(N
);
5324 -- Perform in-place expansion of aggregate in an object declaration.
5325 -- Note: actions generated for the aggregate will be captured in an
5326 -- expression-with-actions statement so that they can be transferred
5327 -- to freeze actions later if there is an address clause for the
5328 -- object. (Note: we don't use a block statement because this would
5329 -- cause generated freeze nodes to be elaborated in the wrong scope).
5331 -- Should document these individual tests ???
5333 if not Has_Default_Init_Comps
(N
)
5334 and then Comes_From_Source
(Parent_Node
)
5335 and then Parent_Kind
= N_Object_Declaration
5337 Must_Slide
(Etype
(Defining_Identifier
(Parent_Node
)), Typ
)
5338 and then N
= Expression
(Parent_Node
)
5339 and then not Is_Bit_Packed_Array
(Typ
)
5340 and then not Has_Controlled_Component
(Typ
)
5342 In_Place_Assign_OK_For_Declaration
:= True;
5343 Tmp
:= Defining_Identifier
(Parent
(N
));
5344 Set_No_Initialization
(Parent
(N
));
5345 Set_Expression
(Parent
(N
), Empty
);
5347 -- Set kind and type of the entity, for use in the analysis
5348 -- of the subsequent assignments. If the nominal type is not
5349 -- constrained, build a subtype from the known bounds of the
5350 -- aggregate. If the declaration has a subtype mark, use it,
5351 -- otherwise use the itype of the aggregate.
5353 Set_Ekind
(Tmp
, E_Variable
);
5355 if not Is_Constrained
(Typ
) then
5356 Build_Constrained_Type
(Positional
=> False);
5358 elsif Is_Entity_Name
(Object_Definition
(Parent
(N
)))
5359 and then Is_Constrained
(Entity
(Object_Definition
(Parent
(N
))))
5361 Set_Etype
(Tmp
, Entity
(Object_Definition
(Parent
(N
))));
5364 Set_Size_Known_At_Compile_Time
(Typ
, False);
5365 Set_Etype
(Tmp
, Typ
);
5368 elsif Maybe_In_Place_OK
5369 and then Nkind
(Parent
(N
)) = N_Qualified_Expression
5370 and then Nkind
(Parent
(Parent
(N
))) = N_Allocator
5372 Set_Expansion_Delayed
(N
);
5375 -- In the remaining cases the aggregate is the RHS of an assignment
5377 elsif Maybe_In_Place_OK
5378 and then Safe_Left_Hand_Side
(Name
(Parent
(N
)))
5380 Tmp
:= Name
(Parent
(N
));
5382 if Etype
(Tmp
) /= Etype
(N
) then
5383 Apply_Length_Check
(N
, Etype
(Tmp
));
5385 if Nkind
(N
) = N_Raise_Constraint_Error
then
5387 -- Static error, nothing further to expand
5393 -- If a slice assignment has an aggregate with a single others_choice,
5394 -- the assignment can be done in place even if bounds are not static,
5395 -- by converting it into a loop over the discrete range of the slice.
5397 elsif Maybe_In_Place_OK
5398 and then Nkind
(Name
(Parent
(N
))) = N_Slice
5399 and then Is_Others_Aggregate
(N
)
5401 Tmp
:= Name
(Parent
(N
));
5403 -- Set type of aggregate to be type of lhs in assignment, in order
5404 -- to suppress redundant length checks.
5406 Set_Etype
(N
, Etype
(Tmp
));
5410 -- In place aggregate expansion is not possible
5413 Maybe_In_Place_OK
:= False;
5414 Tmp
:= Make_Temporary
(Loc
, 'A', N
);
5416 Make_Object_Declaration
(Loc
,
5417 Defining_Identifier
=> Tmp
,
5418 Object_Definition
=> New_Occurrence_Of
(Typ
, Loc
));
5419 Set_No_Initialization
(Tmp_Decl
, True);
5421 -- If we are within a loop, the temporary will be pushed on the
5422 -- stack at each iteration. If the aggregate is the expression for an
5423 -- allocator, it will be immediately copied to the heap and can
5424 -- be reclaimed at once. We create a transient scope around the
5425 -- aggregate for this purpose.
5427 if Ekind
(Current_Scope
) = E_Loop
5428 and then Nkind
(Parent
(Parent
(N
))) = N_Allocator
5430 Establish_Transient_Scope
(N
, False);
5433 Insert_Action
(N
, Tmp_Decl
);
5436 -- Construct and insert the aggregate code. We can safely suppress index
5437 -- checks because this code is guaranteed not to raise CE on index
5438 -- checks. However we should *not* suppress all checks.
5444 if Nkind
(Tmp
) = N_Defining_Identifier
then
5445 Target
:= New_Occurrence_Of
(Tmp
, Loc
);
5448 if Has_Default_Init_Comps
(N
) then
5450 -- Ada 2005 (AI-287): This case has not been analyzed???
5452 raise Program_Error
;
5455 -- Name in assignment is explicit dereference
5457 Target
:= New_Copy
(Tmp
);
5460 -- If we are to generate an in place assignment for a declaration or
5461 -- an assignment statement, and the assignment can be done directly
5462 -- by the back end, then do not expand further.
5464 -- ??? We can also do that if in place expansion is not possible but
5465 -- then we could go into an infinite recursion.
5467 if (In_Place_Assign_OK_For_Declaration
or else Maybe_In_Place_OK
)
5468 and then VM_Target
= No_VM
5469 and then not AAMP_On_Target
5470 and then not Generate_SCIL
5471 and then not Possible_Bit_Aligned_Component
(Target
)
5472 and then not Is_Possibly_Unaligned_Slice
(Target
)
5473 and then Aggr_Assignment_OK_For_Backend
(N
)
5475 if Maybe_In_Place_OK
then
5481 Make_Assignment_Statement
(Loc
,
5483 Expression
=> New_Copy
(N
)));
5487 Build_Array_Aggr_Code
(N
,
5489 Index
=> First_Index
(Typ
),
5491 Scalar_Comp
=> Is_Scalar_Type
(Ctyp
));
5494 -- Save the last assignment statement associated with the aggregate
5495 -- when building a controlled object. This reference is utilized by
5496 -- the finalization machinery when marking an object as successfully
5499 if Needs_Finalization
(Typ
)
5500 and then Is_Entity_Name
(Target
)
5501 and then Present
(Entity
(Target
))
5502 and then Ekind_In
(Entity
(Target
), E_Constant
, E_Variable
)
5504 Set_Last_Aggregate_Assignment
(Entity
(Target
), Last
(Aggr_Code
));
5508 -- If the aggregate is the expression in a declaration, the expanded
5509 -- code must be inserted after it. The defining entity might not come
5510 -- from source if this is part of an inlined body, but the declaration
5513 if Comes_From_Source
(Tmp
)
5515 (Nkind
(Parent
(N
)) = N_Object_Declaration
5516 and then Comes_From_Source
(Parent
(N
))
5517 and then Tmp
= Defining_Entity
(Parent
(N
)))
5520 Node_After
: constant Node_Id
:= Next
(Parent_Node
);
5523 Insert_Actions_After
(Parent_Node
, Aggr_Code
);
5525 if Parent_Kind
= N_Object_Declaration
then
5526 Collect_Initialization_Statements
5527 (Obj
=> Tmp
, N
=> Parent_Node
, Node_After
=> Node_After
);
5532 Insert_Actions
(N
, Aggr_Code
);
5535 -- If the aggregate has been assigned in place, remove the original
5538 if Nkind
(Parent
(N
)) = N_Assignment_Statement
5539 and then Maybe_In_Place_OK
5541 Rewrite
(Parent
(N
), Make_Null_Statement
(Loc
));
5543 elsif Nkind
(Parent
(N
)) /= N_Object_Declaration
5544 or else Tmp
/= Defining_Identifier
(Parent
(N
))
5546 Rewrite
(N
, New_Occurrence_Of
(Tmp
, Loc
));
5547 Analyze_And_Resolve
(N
, Typ
);
5549 end Expand_Array_Aggregate
;
5551 ------------------------
5552 -- Expand_N_Aggregate --
5553 ------------------------
5555 procedure Expand_N_Aggregate
(N
: Node_Id
) is
5557 -- Record aggregate case
5559 if Is_Record_Type
(Etype
(N
)) then
5560 Expand_Record_Aggregate
(N
);
5562 -- Array aggregate case
5565 -- A special case, if we have a string subtype with bounds 1 .. N,
5566 -- where N is known at compile time, and the aggregate is of the
5567 -- form (others => 'x'), with a single choice and no expressions,
5568 -- and N is less than 80 (an arbitrary limit for now), then replace
5569 -- the aggregate by the equivalent string literal (but do not mark
5570 -- it as static since it is not).
5572 -- Note: this entire circuit is redundant with respect to code in
5573 -- Expand_Array_Aggregate that collapses others choices to positional
5574 -- form, but there are two problems with that circuit:
5576 -- a) It is limited to very small cases due to ill-understood
5577 -- interactions with bootstrapping. That limit is removed by
5578 -- use of the No_Implicit_Loops restriction.
5580 -- b) It incorrectly ends up with the resulting expressions being
5581 -- considered static when they are not. For example, the
5582 -- following test should fail:
5584 -- pragma Restrictions (No_Implicit_Loops);
5585 -- package NonSOthers4 is
5586 -- B : constant String (1 .. 6) := (others => 'A');
5587 -- DH : constant String (1 .. 8) := B & "BB";
5589 -- pragma Export (C, X, Link_Name => DH);
5592 -- But it succeeds (DH looks static to pragma Export)
5594 -- To be sorted out ???
5596 if Present
(Component_Associations
(N
)) then
5598 CA
: constant Node_Id
:= First
(Component_Associations
(N
));
5599 MX
: constant := 80;
5602 if Nkind
(First
(Choices
(CA
))) = N_Others_Choice
5603 and then Nkind
(Expression
(CA
)) = N_Character_Literal
5604 and then No
(Expressions
(N
))
5607 T
: constant Entity_Id
:= Etype
(N
);
5608 X
: constant Node_Id
:= First_Index
(T
);
5609 EC
: constant Node_Id
:= Expression
(CA
);
5610 CV
: constant Uint
:= Char_Literal_Value
(EC
);
5611 CC
: constant Int
:= UI_To_Int
(CV
);
5614 if Nkind
(X
) = N_Range
5615 and then Compile_Time_Known_Value
(Low_Bound
(X
))
5616 and then Expr_Value
(Low_Bound
(X
)) = 1
5617 and then Compile_Time_Known_Value
(High_Bound
(X
))
5620 Hi
: constant Uint
:= Expr_Value
(High_Bound
(X
));
5626 for J
in 1 .. UI_To_Int
(Hi
) loop
5627 Store_String_Char
(Char_Code
(CC
));
5631 Make_String_Literal
(Sloc
(N
),
5632 Strval
=> End_String
));
5634 if CC
>= Int
(2 ** 16) then
5635 Set_Has_Wide_Wide_Character
(N
);
5636 elsif CC
>= Int
(2 ** 8) then
5637 Set_Has_Wide_Character
(N
);
5640 Analyze_And_Resolve
(N
, T
);
5641 Set_Is_Static_Expression
(N
, False);
5651 -- Not that special case, so normal expansion of array aggregate
5653 Expand_Array_Aggregate
(N
);
5657 when RE_Not_Available
=>
5659 end Expand_N_Aggregate
;
5661 ----------------------------------
5662 -- Expand_N_Extension_Aggregate --
5663 ----------------------------------
5665 -- If the ancestor part is an expression, add a component association for
5666 -- the parent field. If the type of the ancestor part is not the direct
5667 -- parent of the expected type, build recursively the needed ancestors.
5668 -- If the ancestor part is a subtype_mark, replace aggregate with a decla-
5669 -- ration for a temporary of the expected type, followed by individual
5670 -- assignments to the given components.
5672 procedure Expand_N_Extension_Aggregate
(N
: Node_Id
) is
5673 Loc
: constant Source_Ptr
:= Sloc
(N
);
5674 A
: constant Node_Id
:= Ancestor_Part
(N
);
5675 Typ
: constant Entity_Id
:= Etype
(N
);
5678 -- If the ancestor is a subtype mark, an init proc must be called
5679 -- on the resulting object which thus has to be materialized in
5682 if Is_Entity_Name
(A
) and then Is_Type
(Entity
(A
)) then
5683 Convert_To_Assignments
(N
, Typ
);
5685 -- The extension aggregate is transformed into a record aggregate
5686 -- of the following form (c1 and c2 are inherited components)
5688 -- (Exp with c3 => a, c4 => b)
5689 -- ==> (c1 => Exp.c1, c2 => Exp.c2, c3 => a, c4 => b)
5694 if Tagged_Type_Expansion
then
5695 Expand_Record_Aggregate
(N
,
5698 (Node
(First_Elmt
(Access_Disp_Table
(Typ
))), Loc
),
5701 -- No tag is needed in the case of a VM
5704 Expand_Record_Aggregate
(N
, Parent_Expr
=> A
);
5709 when RE_Not_Available
=>
5711 end Expand_N_Extension_Aggregate
;
5713 -----------------------------
5714 -- Expand_Record_Aggregate --
5715 -----------------------------
5717 procedure Expand_Record_Aggregate
5719 Orig_Tag
: Node_Id
:= Empty
;
5720 Parent_Expr
: Node_Id
:= Empty
)
5722 Loc
: constant Source_Ptr
:= Sloc
(N
);
5723 Comps
: constant List_Id
:= Component_Associations
(N
);
5724 Typ
: constant Entity_Id
:= Etype
(N
);
5725 Base_Typ
: constant Entity_Id
:= Base_Type
(Typ
);
5727 Static_Components
: Boolean := True;
5728 -- Flag to indicate whether all components are compile-time known,
5729 -- and the aggregate can be constructed statically and handled by
5732 function Compile_Time_Known_Composite_Value
(N
: Node_Id
) return Boolean;
5733 -- Returns true if N is an expression of composite type which can be
5734 -- fully evaluated at compile time without raising constraint error.
5735 -- Such expressions can be passed as is to Gigi without any expansion.
5737 -- This returns true for N_Aggregate with Compile_Time_Known_Aggregate
5738 -- set and constants whose expression is such an aggregate, recursively.
5740 function Component_Not_OK_For_Backend
return Boolean;
5741 -- Check for presence of a component which makes it impossible for the
5742 -- backend to process the aggregate, thus requiring the use of a series
5743 -- of assignment statements. Cases checked for are a nested aggregate
5744 -- needing Late_Expansion, the presence of a tagged component which may
5745 -- need tag adjustment, and a bit unaligned component reference.
5747 -- We also force expansion into assignments if a component is of a
5748 -- mutable type (including a private type with discriminants) because
5749 -- in that case the size of the component to be copied may be smaller
5750 -- than the side of the target, and there is no simple way for gigi
5751 -- to compute the size of the object to be copied.
5753 -- NOTE: This is part of the ongoing work to define precisely the
5754 -- interface between front-end and back-end handling of aggregates.
5755 -- In general it is desirable to pass aggregates as they are to gigi,
5756 -- in order to minimize elaboration code. This is one case where the
5757 -- semantics of Ada complicate the analysis and lead to anomalies in
5758 -- the gcc back-end if the aggregate is not expanded into assignments.
5760 function Has_Visible_Private_Ancestor
(Id
: E
) return Boolean;
5761 -- If any ancestor of the current type is private, the aggregate
5762 -- cannot be built in place. We cannot rely on Has_Private_Ancestor,
5763 -- because it will not be set when type and its parent are in the
5764 -- same scope, and the parent component needs expansion.
5766 function Top_Level_Aggregate
(N
: Node_Id
) return Node_Id
;
5767 -- For nested aggregates return the ultimate enclosing aggregate; for
5768 -- non-nested aggregates return N.
5770 ----------------------------------------
5771 -- Compile_Time_Known_Composite_Value --
5772 ----------------------------------------
5774 function Compile_Time_Known_Composite_Value
5775 (N
: Node_Id
) return Boolean
5778 -- If we have an entity name, then see if it is the name of a
5779 -- constant and if so, test the corresponding constant value.
5781 if Is_Entity_Name
(N
) then
5783 E
: constant Entity_Id
:= Entity
(N
);
5786 if Ekind
(E
) /= E_Constant
then
5789 V
:= Constant_Value
(E
);
5791 and then Compile_Time_Known_Composite_Value
(V
);
5795 -- We have a value, see if it is compile time known
5798 if Nkind
(N
) = N_Aggregate
then
5799 return Compile_Time_Known_Aggregate
(N
);
5802 -- All other types of values are not known at compile time
5807 end Compile_Time_Known_Composite_Value
;
5809 ----------------------------------
5810 -- Component_Not_OK_For_Backend --
5811 ----------------------------------
5813 function Component_Not_OK_For_Backend
return Boolean is
5823 while Present
(C
) loop
5825 -- If the component has box initialization, expansion is needed
5826 -- and component is not ready for backend.
5828 if Box_Present
(C
) then
5832 if Nkind
(Expression
(C
)) = N_Qualified_Expression
then
5833 Expr_Q
:= Expression
(Expression
(C
));
5835 Expr_Q
:= Expression
(C
);
5838 -- Return true if the aggregate has any associations for tagged
5839 -- components that may require tag adjustment.
5841 -- These are cases where the source expression may have a tag that
5842 -- could differ from the component tag (e.g., can occur for type
5843 -- conversions and formal parameters). (Tag adjustment not needed
5844 -- if VM_Target because object tags are implicit in the machine.)
5846 if Is_Tagged_Type
(Etype
(Expr_Q
))
5847 and then (Nkind
(Expr_Q
) = N_Type_Conversion
5848 or else (Is_Entity_Name
(Expr_Q
)
5850 Ekind
(Entity
(Expr_Q
)) in Formal_Kind
))
5851 and then Tagged_Type_Expansion
5853 Static_Components
:= False;
5856 elsif Is_Delayed_Aggregate
(Expr_Q
) then
5857 Static_Components
:= False;
5860 elsif Possible_Bit_Aligned_Component
(Expr_Q
) then
5861 Static_Components
:= False;
5865 if Is_Elementary_Type
(Etype
(Expr_Q
)) then
5866 if not Compile_Time_Known_Value
(Expr_Q
) then
5867 Static_Components
:= False;
5870 elsif not Compile_Time_Known_Composite_Value
(Expr_Q
) then
5871 Static_Components
:= False;
5873 if Is_Private_Type
(Etype
(Expr_Q
))
5874 and then Has_Discriminants
(Etype
(Expr_Q
))
5884 end Component_Not_OK_For_Backend
;
5886 -----------------------------------
5887 -- Has_Visible_Private_Ancestor --
5888 -----------------------------------
5890 function Has_Visible_Private_Ancestor
(Id
: E
) return Boolean is
5891 R
: constant Entity_Id
:= Root_Type
(Id
);
5892 T1
: Entity_Id
:= Id
;
5896 if Is_Private_Type
(T1
) then
5906 end Has_Visible_Private_Ancestor
;
5908 -------------------------
5909 -- Top_Level_Aggregate --
5910 -------------------------
5912 function Top_Level_Aggregate
(N
: Node_Id
) return Node_Id
is
5917 while Present
(Parent
(Aggr
))
5918 and then Nkind_In
(Parent
(Aggr
), N_Component_Association
,
5921 Aggr
:= Parent
(Aggr
);
5925 end Top_Level_Aggregate
;
5929 Top_Level_Aggr
: constant Node_Id
:= Top_Level_Aggregate
(N
);
5930 Tag_Value
: Node_Id
;
5934 -- Start of processing for Expand_Record_Aggregate
5937 -- If the aggregate is to be assigned to an atomic/VFA variable, we have
5938 -- to prevent a piecemeal assignment even if the aggregate is to be
5939 -- expanded. We create a temporary for the aggregate, and assign the
5940 -- temporary instead, so that the back end can generate an atomic move
5943 if Is_Atomic_Or_VFA
(Typ
)
5944 and then Comes_From_Source
(Parent
(N
))
5945 and then Is_Atomic_VFA_Aggregate
(N
, Typ
)
5949 -- No special management required for aggregates used to initialize
5950 -- statically allocated dispatch tables
5952 elsif Is_Static_Dispatch_Table_Aggregate
(N
) then
5956 -- Ada 2005 (AI-318-2): We need to convert to assignments if components
5957 -- are build-in-place function calls. The assignments will each turn
5958 -- into a build-in-place function call. If components are all static,
5959 -- we can pass the aggregate to the backend regardless of limitedness.
5961 -- Extension aggregates, aggregates in extended return statements, and
5962 -- aggregates for C++ imported types must be expanded.
5964 if Ada_Version
>= Ada_2005
and then Is_Limited_View
(Typ
) then
5965 if not Nkind_In
(Parent
(N
), N_Object_Declaration
,
5966 N_Component_Association
)
5968 Convert_To_Assignments
(N
, Typ
);
5970 elsif Nkind
(N
) = N_Extension_Aggregate
5971 or else Convention
(Typ
) = Convention_CPP
5973 Convert_To_Assignments
(N
, Typ
);
5975 elsif not Size_Known_At_Compile_Time
(Typ
)
5976 or else Component_Not_OK_For_Backend
5977 or else not Static_Components
5979 Convert_To_Assignments
(N
, Typ
);
5982 Set_Compile_Time_Known_Aggregate
(N
);
5983 Set_Expansion_Delayed
(N
, False);
5986 -- Gigi doesn't properly handle temporaries of variable size so we
5987 -- generate it in the front-end
5989 elsif not Size_Known_At_Compile_Time
(Typ
)
5990 and then Tagged_Type_Expansion
5992 Convert_To_Assignments
(N
, Typ
);
5994 -- An aggregate used to initialize a controlled object must be turned
5995 -- into component assignments as the components themselves may require
5996 -- finalization actions such as adjustment.
5998 elsif Needs_Finalization
(Typ
) then
5999 Convert_To_Assignments
(N
, Typ
);
6001 -- Ada 2005 (AI-287): In case of default initialized components we
6002 -- convert the aggregate into assignments.
6004 elsif Has_Default_Init_Comps
(N
) then
6005 Convert_To_Assignments
(N
, Typ
);
6009 elsif Component_Not_OK_For_Backend
then
6010 Convert_To_Assignments
(N
, Typ
);
6012 -- If an ancestor is private, some components are not inherited and we
6013 -- cannot expand into a record aggregate.
6015 elsif Has_Visible_Private_Ancestor
(Typ
) then
6016 Convert_To_Assignments
(N
, Typ
);
6018 -- ??? The following was done to compile fxacc00.ads in the ACVCs. Gigi
6019 -- is not able to handle the aggregate for Late_Request.
6021 elsif Is_Tagged_Type
(Typ
) and then Has_Discriminants
(Typ
) then
6022 Convert_To_Assignments
(N
, Typ
);
6024 -- If the tagged types covers interface types we need to initialize all
6025 -- hidden components containing pointers to secondary dispatch tables.
6027 elsif Is_Tagged_Type
(Typ
) and then Has_Interfaces
(Typ
) then
6028 Convert_To_Assignments
(N
, Typ
);
6030 -- If some components are mutable, the size of the aggregate component
6031 -- may be distinct from the default size of the type component, so
6032 -- we need to expand to insure that the back-end copies the proper
6033 -- size of the data. However, if the aggregate is the initial value of
6034 -- a constant, the target is immutable and might be built statically
6035 -- if components are appropriate.
6037 elsif Has_Mutable_Components
(Typ
)
6039 (Nkind
(Parent
(Top_Level_Aggr
)) /= N_Object_Declaration
6040 or else not Constant_Present
(Parent
(Top_Level_Aggr
))
6041 or else not Static_Components
)
6043 Convert_To_Assignments
(N
, Typ
);
6045 -- If the type involved has bit aligned components, then we are not sure
6046 -- that the back end can handle this case correctly.
6048 elsif Type_May_Have_Bit_Aligned_Components
(Typ
) then
6049 Convert_To_Assignments
(N
, Typ
);
6051 -- In all other cases, build a proper aggregate to be handled by gigi
6054 if Nkind
(N
) = N_Aggregate
then
6056 -- If the aggregate is static and can be handled by the back-end,
6057 -- nothing left to do.
6059 if Static_Components
then
6060 Set_Compile_Time_Known_Aggregate
(N
);
6061 Set_Expansion_Delayed
(N
, False);
6065 -- If no discriminants, nothing special to do
6067 if not Has_Discriminants
(Typ
) then
6070 -- Case of discriminants present
6072 elsif Is_Derived_Type
(Typ
) then
6074 -- For untagged types, non-stored discriminants are replaced
6075 -- with stored discriminants, which are the ones that gigi uses
6076 -- to describe the type and its components.
6078 Generate_Aggregate_For_Derived_Type
: declare
6079 Constraints
: constant List_Id
:= New_List
;
6080 First_Comp
: Node_Id
;
6081 Discriminant
: Entity_Id
;
6083 Num_Disc
: Int
:= 0;
6084 Num_Gird
: Int
:= 0;
6086 procedure Prepend_Stored_Values
(T
: Entity_Id
);
6087 -- Scan the list of stored discriminants of the type, and add
6088 -- their values to the aggregate being built.
6090 ---------------------------
6091 -- Prepend_Stored_Values --
6092 ---------------------------
6094 procedure Prepend_Stored_Values
(T
: Entity_Id
) is
6096 Discriminant
:= First_Stored_Discriminant
(T
);
6097 while Present
(Discriminant
) loop
6099 Make_Component_Association
(Loc
,
6101 New_List
(New_Occurrence_Of
(Discriminant
, Loc
)),
6105 (Get_Discriminant_Value
6108 Discriminant_Constraint
(Typ
))));
6110 if No
(First_Comp
) then
6111 Prepend_To
(Component_Associations
(N
), New_Comp
);
6113 Insert_After
(First_Comp
, New_Comp
);
6116 First_Comp
:= New_Comp
;
6117 Next_Stored_Discriminant
(Discriminant
);
6119 end Prepend_Stored_Values
;
6121 -- Start of processing for Generate_Aggregate_For_Derived_Type
6124 -- Remove the associations for the discriminant of derived type
6126 First_Comp
:= First
(Component_Associations
(N
));
6127 while Present
(First_Comp
) loop
6131 if Ekind
(Entity
(First
(Choices
(Comp
)))) = E_Discriminant
6134 Num_Disc
:= Num_Disc
+ 1;
6138 -- Insert stored discriminant associations in the correct
6139 -- order. If there are more stored discriminants than new
6140 -- discriminants, there is at least one new discriminant that
6141 -- constrains more than one of the stored discriminants. In
6142 -- this case we need to construct a proper subtype of the
6143 -- parent type, in order to supply values to all the
6144 -- components. Otherwise there is one-one correspondence
6145 -- between the constraints and the stored discriminants.
6147 First_Comp
:= Empty
;
6149 Discriminant
:= First_Stored_Discriminant
(Base_Type
(Typ
));
6150 while Present
(Discriminant
) loop
6151 Num_Gird
:= Num_Gird
+ 1;
6152 Next_Stored_Discriminant
(Discriminant
);
6155 -- Case of more stored discriminants than new discriminants
6157 if Num_Gird
> Num_Disc
then
6159 -- Create a proper subtype of the parent type, which is the
6160 -- proper implementation type for the aggregate, and convert
6161 -- it to the intended target type.
6163 Discriminant
:= First_Stored_Discriminant
(Base_Type
(Typ
));
6164 while Present
(Discriminant
) loop
6167 (Get_Discriminant_Value
6170 Discriminant_Constraint
(Typ
)));
6171 Append
(New_Comp
, Constraints
);
6172 Next_Stored_Discriminant
(Discriminant
);
6176 Make_Subtype_Declaration
(Loc
,
6177 Defining_Identifier
=> Make_Temporary
(Loc
, 'T'),
6178 Subtype_Indication
=>
6179 Make_Subtype_Indication
(Loc
,
6181 New_Occurrence_Of
(Etype
(Base_Type
(Typ
)), Loc
),
6183 Make_Index_Or_Discriminant_Constraint
6184 (Loc
, Constraints
)));
6186 Insert_Action
(N
, Decl
);
6187 Prepend_Stored_Values
(Base_Type
(Typ
));
6189 Set_Etype
(N
, Defining_Identifier
(Decl
));
6192 Rewrite
(N
, Unchecked_Convert_To
(Typ
, N
));
6195 -- Case where we do not have fewer new discriminants than
6196 -- stored discriminants, so in this case we can simply use the
6197 -- stored discriminants of the subtype.
6200 Prepend_Stored_Values
(Typ
);
6202 end Generate_Aggregate_For_Derived_Type
;
6205 if Is_Tagged_Type
(Typ
) then
6207 -- In the tagged case, _parent and _tag component must be created
6209 -- Reset Null_Present unconditionally. Tagged records always have
6210 -- at least one field (the tag or the parent).
6212 Set_Null_Record_Present
(N
, False);
6214 -- When the current aggregate comes from the expansion of an
6215 -- extension aggregate, the parent expr is replaced by an
6216 -- aggregate formed by selected components of this expr.
6218 if Present
(Parent_Expr
) and then Is_Empty_List
(Comps
) then
6219 Comp
:= First_Component_Or_Discriminant
(Typ
);
6220 while Present
(Comp
) loop
6222 -- Skip all expander-generated components
6224 if not Comes_From_Source
(Original_Record_Component
(Comp
))
6230 Make_Selected_Component
(Loc
,
6232 Unchecked_Convert_To
(Typ
,
6233 Duplicate_Subexpr
(Parent_Expr
, True)),
6234 Selector_Name
=> New_Occurrence_Of
(Comp
, Loc
));
6237 Make_Component_Association
(Loc
,
6239 New_List
(New_Occurrence_Of
(Comp
, Loc
)),
6240 Expression
=> New_Comp
));
6242 Analyze_And_Resolve
(New_Comp
, Etype
(Comp
));
6245 Next_Component_Or_Discriminant
(Comp
);
6249 -- Compute the value for the Tag now, if the type is a root it
6250 -- will be included in the aggregate right away, otherwise it will
6251 -- be propagated to the parent aggregate.
6253 if Present
(Orig_Tag
) then
6254 Tag_Value
:= Orig_Tag
;
6255 elsif not Tagged_Type_Expansion
then
6260 (Node
(First_Elmt
(Access_Disp_Table
(Typ
))), Loc
);
6263 -- For a derived type, an aggregate for the parent is formed with
6264 -- all the inherited components.
6266 if Is_Derived_Type
(Typ
) then
6269 First_Comp
: Node_Id
;
6270 Parent_Comps
: List_Id
;
6271 Parent_Aggr
: Node_Id
;
6272 Parent_Name
: Node_Id
;
6275 -- Remove the inherited component association from the
6276 -- aggregate and store them in the parent aggregate
6278 First_Comp
:= First
(Component_Associations
(N
));
6279 Parent_Comps
:= New_List
;
6280 while Present
(First_Comp
)
6282 Scope
(Original_Record_Component
6283 (Entity
(First
(Choices
(First_Comp
))))) /=
6289 Append
(Comp
, Parent_Comps
);
6293 Make_Aggregate
(Loc
,
6294 Component_Associations
=> Parent_Comps
);
6295 Set_Etype
(Parent_Aggr
, Etype
(Base_Type
(Typ
)));
6297 -- Find the _parent component
6299 Comp
:= First_Component
(Typ
);
6300 while Chars
(Comp
) /= Name_uParent
loop
6301 Comp
:= Next_Component
(Comp
);
6304 Parent_Name
:= New_Occurrence_Of
(Comp
, Loc
);
6306 -- Insert the parent aggregate
6308 Prepend_To
(Component_Associations
(N
),
6309 Make_Component_Association
(Loc
,
6310 Choices
=> New_List
(Parent_Name
),
6311 Expression
=> Parent_Aggr
));
6313 -- Expand recursively the parent propagating the right Tag
6315 Expand_Record_Aggregate
6316 (Parent_Aggr
, Tag_Value
, Parent_Expr
);
6318 -- The ancestor part may be a nested aggregate that has
6319 -- delayed expansion: recheck now.
6321 if Component_Not_OK_For_Backend
then
6322 Convert_To_Assignments
(N
, Typ
);
6326 -- For a root type, the tag component is added (unless compiling
6327 -- for the VMs, where tags are implicit).
6329 elsif Tagged_Type_Expansion
then
6331 Tag_Name
: constant Node_Id
:=
6332 New_Occurrence_Of
(First_Tag_Component
(Typ
), Loc
);
6333 Typ_Tag
: constant Entity_Id
:= RTE
(RE_Tag
);
6334 Conv_Node
: constant Node_Id
:=
6335 Unchecked_Convert_To
(Typ_Tag
, Tag_Value
);
6338 Set_Etype
(Conv_Node
, Typ_Tag
);
6339 Prepend_To
(Component_Associations
(N
),
6340 Make_Component_Association
(Loc
,
6341 Choices
=> New_List
(Tag_Name
),
6342 Expression
=> Conv_Node
));
6348 end Expand_Record_Aggregate
;
6350 ----------------------------
6351 -- Has_Default_Init_Comps --
6352 ----------------------------
6354 function Has_Default_Init_Comps
(N
: Node_Id
) return Boolean is
6355 Comps
: constant List_Id
:= Component_Associations
(N
);
6360 pragma Assert
(Nkind_In
(N
, N_Aggregate
, N_Extension_Aggregate
));
6366 if Has_Self_Reference
(N
) then
6370 -- Check if any direct component has default initialized components
6373 while Present
(C
) loop
6374 if Box_Present
(C
) then
6381 -- Recursive call in case of aggregate expression
6384 while Present
(C
) loop
6385 Expr
:= Expression
(C
);
6388 and then Nkind_In
(Expr
, N_Aggregate
, N_Extension_Aggregate
)
6389 and then Has_Default_Init_Comps
(Expr
)
6398 end Has_Default_Init_Comps
;
6400 --------------------------
6401 -- Is_Delayed_Aggregate --
6402 --------------------------
6404 function Is_Delayed_Aggregate
(N
: Node_Id
) return Boolean is
6405 Node
: Node_Id
:= N
;
6406 Kind
: Node_Kind
:= Nkind
(Node
);
6409 if Kind
= N_Qualified_Expression
then
6410 Node
:= Expression
(Node
);
6411 Kind
:= Nkind
(Node
);
6414 if not Nkind_In
(Kind
, N_Aggregate
, N_Extension_Aggregate
) then
6417 return Expansion_Delayed
(Node
);
6419 end Is_Delayed_Aggregate
;
6421 ----------------------------------------
6422 -- Is_Static_Dispatch_Table_Aggregate --
6423 ----------------------------------------
6425 function Is_Static_Dispatch_Table_Aggregate
(N
: Node_Id
) return Boolean is
6426 Typ
: constant Entity_Id
:= Base_Type
(Etype
(N
));
6429 return Static_Dispatch_Tables
6430 and then Tagged_Type_Expansion
6431 and then RTU_Loaded
(Ada_Tags
)
6433 -- Avoid circularity when rebuilding the compiler
6435 and then Cunit_Entity
(Get_Source_Unit
(N
)) /= RTU_Entity
(Ada_Tags
)
6436 and then (Typ
= RTE
(RE_Dispatch_Table_Wrapper
)
6438 Typ
= RTE
(RE_Address_Array
)
6440 Typ
= RTE
(RE_Type_Specific_Data
)
6442 Typ
= RTE
(RE_Tag_Table
)
6444 (RTE_Available
(RE_Interface_Data
)
6445 and then Typ
= RTE
(RE_Interface_Data
))
6447 (RTE_Available
(RE_Interfaces_Array
)
6448 and then Typ
= RTE
(RE_Interfaces_Array
))
6450 (RTE_Available
(RE_Interface_Data_Element
)
6451 and then Typ
= RTE
(RE_Interface_Data_Element
)));
6452 end Is_Static_Dispatch_Table_Aggregate
;
6454 -----------------------------
6455 -- Is_Two_Dim_Packed_Array --
6456 -----------------------------
6458 function Is_Two_Dim_Packed_Array
(Typ
: Entity_Id
) return Boolean is
6459 C
: constant Int
:= UI_To_Int
(Component_Size
(Typ
));
6461 return Number_Dimensions
(Typ
) = 2
6462 and then Is_Bit_Packed_Array
(Typ
)
6463 and then (C
= 1 or else C
= 2 or else C
= 4);
6464 end Is_Two_Dim_Packed_Array
;
6466 --------------------
6467 -- Late_Expansion --
6468 --------------------
6470 function Late_Expansion
6473 Target
: Node_Id
) return List_Id
6475 Aggr_Code
: List_Id
;
6478 if Is_Record_Type
(Etype
(N
)) then
6479 Aggr_Code
:= Build_Record_Aggr_Code
(N
, Typ
, Target
);
6481 else pragma Assert
(Is_Array_Type
(Etype
(N
)));
6483 Build_Array_Aggr_Code
6485 Ctype
=> Component_Type
(Etype
(N
)),
6486 Index
=> First_Index
(Typ
),
6488 Scalar_Comp
=> Is_Scalar_Type
(Component_Type
(Typ
)),
6489 Indexes
=> No_List
);
6492 -- Save the last assignment statement associated with the aggregate
6493 -- when building a controlled object. This reference is utilized by
6494 -- the finalization machinery when marking an object as successfully
6497 if Needs_Finalization
(Typ
)
6498 and then Is_Entity_Name
(Target
)
6499 and then Present
(Entity
(Target
))
6500 and then Ekind_In
(Entity
(Target
), E_Constant
, E_Variable
)
6502 Set_Last_Aggregate_Assignment
(Entity
(Target
), Last
(Aggr_Code
));
6508 ----------------------------------
6509 -- Make_OK_Assignment_Statement --
6510 ----------------------------------
6512 function Make_OK_Assignment_Statement
6515 Expression
: Node_Id
) return Node_Id
6518 Set_Assignment_OK
(Name
);
6519 return Make_Assignment_Statement
(Sloc
, Name
, Expression
);
6520 end Make_OK_Assignment_Statement
;
6522 -----------------------
6523 -- Number_Of_Choices --
6524 -----------------------
6526 function Number_Of_Choices
(N
: Node_Id
) return Nat
is
6530 Nb_Choices
: Nat
:= 0;
6533 if Present
(Expressions
(N
)) then
6537 Assoc
:= First
(Component_Associations
(N
));
6538 while Present
(Assoc
) loop
6539 Choice
:= First
(Choices
(Assoc
));
6540 while Present
(Choice
) loop
6541 if Nkind
(Choice
) /= N_Others_Choice
then
6542 Nb_Choices
:= Nb_Choices
+ 1;
6552 end Number_Of_Choices
;
6554 ------------------------------------
6555 -- Packed_Array_Aggregate_Handled --
6556 ------------------------------------
6558 -- The current version of this procedure will handle at compile time
6559 -- any array aggregate that meets these conditions:
6561 -- One and two dimensional, bit packed
6562 -- Underlying packed type is modular type
6563 -- Bounds are within 32-bit Int range
6564 -- All bounds and values are static
6566 -- Note: for now, in the 2-D case, we only handle component sizes of
6567 -- 1, 2, 4 (cases where an integral number of elements occupies a byte).
6569 function Packed_Array_Aggregate_Handled
(N
: Node_Id
) return Boolean is
6570 Loc
: constant Source_Ptr
:= Sloc
(N
);
6571 Typ
: constant Entity_Id
:= Etype
(N
);
6572 Ctyp
: constant Entity_Id
:= Component_Type
(Typ
);
6574 Not_Handled
: exception;
6575 -- Exception raised if this aggregate cannot be handled
6578 -- Handle one- or two dimensional bit packed array
6580 if not Is_Bit_Packed_Array
(Typ
)
6581 or else Number_Dimensions
(Typ
) > 2
6586 -- If two-dimensional, check whether it can be folded, and transformed
6587 -- into a one-dimensional aggregate for the Packed_Array_Impl_Type of
6588 -- the original type.
6590 if Number_Dimensions
(Typ
) = 2 then
6591 return Two_Dim_Packed_Array_Handled
(N
);
6594 if not Is_Modular_Integer_Type
(Packed_Array_Impl_Type
(Typ
)) then
6598 if not Is_Scalar_Type
(Component_Type
(Typ
))
6599 and then Has_Non_Standard_Rep
(Component_Type
(Typ
))
6605 Csiz
: constant Nat
:= UI_To_Int
(Component_Size
(Typ
));
6609 -- Bounds of index type
6613 -- Values of bounds if compile time known
6615 function Get_Component_Val
(N
: Node_Id
) return Uint
;
6616 -- Given a expression value N of the component type Ctyp, returns a
6617 -- value of Csiz (component size) bits representing this value. If
6618 -- the value is non-static or any other reason exists why the value
6619 -- cannot be returned, then Not_Handled is raised.
6621 -----------------------
6622 -- Get_Component_Val --
6623 -----------------------
6625 function Get_Component_Val
(N
: Node_Id
) return Uint
is
6629 -- We have to analyze the expression here before doing any further
6630 -- processing here. The analysis of such expressions is deferred
6631 -- till expansion to prevent some problems of premature analysis.
6633 Analyze_And_Resolve
(N
, Ctyp
);
6635 -- Must have a compile time value. String literals have to be
6636 -- converted into temporaries as well, because they cannot easily
6637 -- be converted into their bit representation.
6639 if not Compile_Time_Known_Value
(N
)
6640 or else Nkind
(N
) = N_String_Literal
6645 Val
:= Expr_Rep_Value
(N
);
6647 -- Adjust for bias, and strip proper number of bits
6649 if Has_Biased_Representation
(Ctyp
) then
6650 Val
:= Val
- Expr_Value
(Type_Low_Bound
(Ctyp
));
6653 return Val
mod Uint_2
** Csiz
;
6654 end Get_Component_Val
;
6656 -- Here we know we have a one dimensional bit packed array
6659 Get_Index_Bounds
(First_Index
(Typ
), Lo
, Hi
);
6661 -- Cannot do anything if bounds are dynamic
6663 if not Compile_Time_Known_Value
(Lo
)
6665 not Compile_Time_Known_Value
(Hi
)
6670 -- Or are silly out of range of int bounds
6672 Lob
:= Expr_Value
(Lo
);
6673 Hib
:= Expr_Value
(Hi
);
6675 if not UI_Is_In_Int_Range
(Lob
)
6677 not UI_Is_In_Int_Range
(Hib
)
6682 -- At this stage we have a suitable aggregate for handling at compile
6683 -- time. The only remaining checks are that the values of expressions
6684 -- in the aggregate are compile-time known (checks are performed by
6685 -- Get_Component_Val), and that any subtypes or ranges are statically
6688 -- If the aggregate is not fully positional at this stage, then
6689 -- convert it to positional form. Either this will fail, in which
6690 -- case we can do nothing, or it will succeed, in which case we have
6691 -- succeeded in handling the aggregate and transforming it into a
6692 -- modular value, or it will stay an aggregate, in which case we
6693 -- have failed to create a packed value for it.
6695 if Present
(Component_Associations
(N
)) then
6696 Convert_To_Positional
6697 (N
, Max_Others_Replicate
=> 64, Handle_Bit_Packed
=> True);
6698 return Nkind
(N
) /= N_Aggregate
;
6701 -- Otherwise we are all positional, so convert to proper value
6704 Lov
: constant Int
:= UI_To_Int
(Lob
);
6705 Hiv
: constant Int
:= UI_To_Int
(Hib
);
6707 Len
: constant Nat
:= Int
'Max (0, Hiv
- Lov
+ 1);
6708 -- The length of the array (number of elements)
6710 Aggregate_Val
: Uint
;
6711 -- Value of aggregate. The value is set in the low order bits of
6712 -- this value. For the little-endian case, the values are stored
6713 -- from low-order to high-order and for the big-endian case the
6714 -- values are stored from high-order to low-order. Note that gigi
6715 -- will take care of the conversions to left justify the value in
6716 -- the big endian case (because of left justified modular type
6717 -- processing), so we do not have to worry about that here.
6720 -- Integer literal for resulting constructed value
6723 -- Shift count from low order for next value
6726 -- Shift increment for loop
6729 -- Next expression from positional parameters of aggregate
6731 Left_Justified
: Boolean;
6732 -- Set True if we are filling the high order bits of the target
6733 -- value (i.e. the value is left justified).
6736 -- For little endian, we fill up the low order bits of the target
6737 -- value. For big endian we fill up the high order bits of the
6738 -- target value (which is a left justified modular value).
6740 Left_Justified
:= Bytes_Big_Endian
;
6742 -- Switch justification if using -gnatd8
6744 if Debug_Flag_8
then
6745 Left_Justified
:= not Left_Justified
;
6748 -- Switch justfification if reverse storage order
6750 if Reverse_Storage_Order
(Base_Type
(Typ
)) then
6751 Left_Justified
:= not Left_Justified
;
6754 if Left_Justified
then
6755 Shift
:= Csiz
* (Len
- 1);
6762 -- Loop to set the values
6765 Aggregate_Val
:= Uint_0
;
6767 Expr
:= First
(Expressions
(N
));
6768 Aggregate_Val
:= Get_Component_Val
(Expr
) * Uint_2
** Shift
;
6770 for J
in 2 .. Len
loop
6771 Shift
:= Shift
+ Incr
;
6774 Aggregate_Val
+ Get_Component_Val
(Expr
) * Uint_2
** Shift
;
6778 -- Now we can rewrite with the proper value
6780 Lit
:= Make_Integer_Literal
(Loc
, Intval
=> Aggregate_Val
);
6781 Set_Print_In_Hex
(Lit
);
6783 -- Construct the expression using this literal. Note that it is
6784 -- important to qualify the literal with its proper modular type
6785 -- since universal integer does not have the required range and
6786 -- also this is a left justified modular type, which is important
6787 -- in the big-endian case.
6790 Unchecked_Convert_To
(Typ
,
6791 Make_Qualified_Expression
(Loc
,
6793 New_Occurrence_Of
(Packed_Array_Impl_Type
(Typ
), Loc
),
6794 Expression
=> Lit
)));
6796 Analyze_And_Resolve
(N
, Typ
);
6804 end Packed_Array_Aggregate_Handled
;
6806 ----------------------------
6807 -- Has_Mutable_Components --
6808 ----------------------------
6810 function Has_Mutable_Components
(Typ
: Entity_Id
) return Boolean is
6814 Comp
:= First_Component
(Typ
);
6815 while Present
(Comp
) loop
6816 if Is_Record_Type
(Etype
(Comp
))
6817 and then Has_Discriminants
(Etype
(Comp
))
6818 and then not Is_Constrained
(Etype
(Comp
))
6823 Next_Component
(Comp
);
6827 end Has_Mutable_Components
;
6829 ------------------------------
6830 -- Initialize_Discriminants --
6831 ------------------------------
6833 procedure Initialize_Discriminants
(N
: Node_Id
; Typ
: Entity_Id
) is
6834 Loc
: constant Source_Ptr
:= Sloc
(N
);
6835 Bas
: constant Entity_Id
:= Base_Type
(Typ
);
6836 Par
: constant Entity_Id
:= Etype
(Bas
);
6837 Decl
: constant Node_Id
:= Parent
(Par
);
6841 if Is_Tagged_Type
(Bas
)
6842 and then Is_Derived_Type
(Bas
)
6843 and then Has_Discriminants
(Par
)
6844 and then Has_Discriminants
(Bas
)
6845 and then Number_Discriminants
(Bas
) /= Number_Discriminants
(Par
)
6846 and then Nkind
(Decl
) = N_Full_Type_Declaration
6847 and then Nkind
(Type_Definition
(Decl
)) = N_Record_Definition
6849 Present
(Variant_Part
(Component_List
(Type_Definition
(Decl
))))
6850 and then Nkind
(N
) /= N_Extension_Aggregate
6853 -- Call init proc to set discriminants.
6854 -- There should eventually be a special procedure for this ???
6856 Ref
:= New_Occurrence_Of
(Defining_Identifier
(N
), Loc
);
6857 Insert_Actions_After
(N
,
6858 Build_Initialization_Call
(Sloc
(N
), Ref
, Typ
));
6860 end Initialize_Discriminants
;
6867 (Obj_Type
: Entity_Id
;
6868 Typ
: Entity_Id
) return Boolean
6870 L1
, L2
, H1
, H2
: Node_Id
;
6873 -- No sliding if the type of the object is not established yet, if it is
6874 -- an unconstrained type whose actual subtype comes from the aggregate,
6875 -- or if the two types are identical.
6877 if not Is_Array_Type
(Obj_Type
) then
6880 elsif not Is_Constrained
(Obj_Type
) then
6883 elsif Typ
= Obj_Type
then
6887 -- Sliding can only occur along the first dimension
6889 Get_Index_Bounds
(First_Index
(Typ
), L1
, H1
);
6890 Get_Index_Bounds
(First_Index
(Obj_Type
), L2
, H2
);
6892 if not Is_OK_Static_Expression
(L1
) or else
6893 not Is_OK_Static_Expression
(L2
) or else
6894 not Is_OK_Static_Expression
(H1
) or else
6895 not Is_OK_Static_Expression
(H2
)
6899 return Expr_Value
(L1
) /= Expr_Value
(L2
)
6901 Expr_Value
(H1
) /= Expr_Value
(H2
);
6906 ----------------------------------
6907 -- Two_Dim_Packed_Array_Handled --
6908 ----------------------------------
6910 function Two_Dim_Packed_Array_Handled
(N
: Node_Id
) return Boolean is
6911 Loc
: constant Source_Ptr
:= Sloc
(N
);
6912 Typ
: constant Entity_Id
:= Etype
(N
);
6913 Ctyp
: constant Entity_Id
:= Component_Type
(Typ
);
6914 Comp_Size
: constant Int
:= UI_To_Int
(Component_Size
(Typ
));
6915 Packed_Array
: constant Entity_Id
:=
6916 Packed_Array_Impl_Type
(Base_Type
(Typ
));
6919 -- Expression in original aggregate
6922 -- One-dimensional subaggregate
6926 -- For now, only deal with cases where an integral number of elements
6927 -- fit in a single byte. This includes the most common boolean case.
6929 if not (Comp_Size
= 1 or else
6930 Comp_Size
= 2 or else
6936 Convert_To_Positional
6937 (N
, Max_Others_Replicate
=> 64, Handle_Bit_Packed
=> True);
6939 -- Verify that all components are static
6941 if Nkind
(N
) = N_Aggregate
6942 and then Compile_Time_Known_Aggregate
(N
)
6946 -- The aggregate may have been re-analyzed and converted already
6948 elsif Nkind
(N
) /= N_Aggregate
then
6951 -- If component associations remain, the aggregate is not static
6953 elsif Present
(Component_Associations
(N
)) then
6957 One_Dim
:= First
(Expressions
(N
));
6958 while Present
(One_Dim
) loop
6959 if Present
(Component_Associations
(One_Dim
)) then
6963 One_Comp
:= First
(Expressions
(One_Dim
));
6964 while Present
(One_Comp
) loop
6965 if not Is_OK_Static_Expression
(One_Comp
) then
6976 -- Two-dimensional aggregate is now fully positional so pack one
6977 -- dimension to create a static one-dimensional array, and rewrite
6978 -- as an unchecked conversion to the original type.
6981 Byte_Size
: constant Int
:= UI_To_Int
(Component_Size
(Packed_Array
));
6982 -- The packed array type is a byte array
6985 -- Number of components accumulated in current byte
6988 -- Assembled list of packed values for equivalent aggregate
6991 -- integer value of component
6994 -- Step size for packing
6997 -- Endian-dependent start position for packing
7000 -- Current insertion position
7003 -- Component of packed array being assembled.
7010 -- Account for endianness. See corresponding comment in
7011 -- Packed_Array_Aggregate_Handled concerning the following.
7015 xor Reverse_Storage_Order
(Base_Type
(Typ
))
7017 Init_Shift
:= Byte_Size
- Comp_Size
;
7024 -- Iterate over each subaggregate
7026 Shift
:= Init_Shift
;
7027 One_Dim
:= First
(Expressions
(N
));
7028 while Present
(One_Dim
) loop
7029 One_Comp
:= First
(Expressions
(One_Dim
));
7030 while Present
(One_Comp
) loop
7031 if Packed_Num
= Byte_Size
/ Comp_Size
then
7033 -- Byte is complete, add to list of expressions
7035 Append
(Make_Integer_Literal
(Sloc
(One_Dim
), Val
), Comps
);
7037 Shift
:= Init_Shift
;
7041 Comp_Val
:= Expr_Rep_Value
(One_Comp
);
7043 -- Adjust for bias, and strip proper number of bits
7045 if Has_Biased_Representation
(Ctyp
) then
7046 Comp_Val
:= Comp_Val
- Expr_Value
(Type_Low_Bound
(Ctyp
));
7049 Comp_Val
:= Comp_Val
mod Uint_2
** Comp_Size
;
7050 Val
:= UI_To_Int
(Val
+ Comp_Val
* Uint_2
** Shift
);
7051 Shift
:= Shift
+ Incr
;
7052 One_Comp
:= Next
(One_Comp
);
7053 Packed_Num
:= Packed_Num
+ 1;
7057 One_Dim
:= Next
(One_Dim
);
7060 if Packed_Num
> 0 then
7062 -- Add final incomplete byte if present
7064 Append
(Make_Integer_Literal
(Sloc
(One_Dim
), Val
), Comps
);
7068 Unchecked_Convert_To
(Typ
,
7069 Make_Qualified_Expression
(Loc
,
7070 Subtype_Mark
=> New_Occurrence_Of
(Packed_Array
, Loc
),
7071 Expression
=> Make_Aggregate
(Loc
, Expressions
=> Comps
))));
7072 Analyze_And_Resolve
(N
);
7075 end Two_Dim_Packed_Array_Handled
;
7077 ---------------------
7078 -- Sort_Case_Table --
7079 ---------------------
7081 procedure Sort_Case_Table
(Case_Table
: in out Case_Table_Type
) is
7082 L
: constant Int
:= Case_Table
'First;
7083 U
: constant Int
:= Case_Table
'Last;
7091 T
:= Case_Table
(K
+ 1);
7095 and then Expr_Value
(Case_Table
(J
- 1).Choice_Lo
) >
7096 Expr_Value
(T
.Choice_Lo
)
7098 Case_Table
(J
) := Case_Table
(J
- 1);
7102 Case_Table
(J
) := T
;
7105 end Sort_Case_Table
;
7107 ----------------------------
7108 -- Static_Array_Aggregate --
7109 ----------------------------
7111 function Static_Array_Aggregate
(N
: Node_Id
) return Boolean is
7112 Bounds
: constant Node_Id
:= Aggregate_Bounds
(N
);
7114 Typ
: constant Entity_Id
:= Etype
(N
);
7115 Comp_Type
: constant Entity_Id
:= Component_Type
(Typ
);
7122 if Is_Tagged_Type
(Typ
)
7123 or else Is_Controlled
(Typ
)
7124 or else Is_Packed
(Typ
)
7130 and then Nkind
(Bounds
) = N_Range
7131 and then Nkind
(Low_Bound
(Bounds
)) = N_Integer_Literal
7132 and then Nkind
(High_Bound
(Bounds
)) = N_Integer_Literal
7134 Lo
:= Low_Bound
(Bounds
);
7135 Hi
:= High_Bound
(Bounds
);
7137 if No
(Component_Associations
(N
)) then
7139 -- Verify that all components are static integers
7141 Expr
:= First
(Expressions
(N
));
7142 while Present
(Expr
) loop
7143 if Nkind
(Expr
) /= N_Integer_Literal
then
7153 -- We allow only a single named association, either a static
7154 -- range or an others_clause, with a static expression.
7156 Expr
:= First
(Component_Associations
(N
));
7158 if Present
(Expressions
(N
)) then
7161 elsif Present
(Next
(Expr
)) then
7164 elsif Present
(Next
(First
(Choices
(Expr
)))) then
7168 -- The aggregate is static if all components are literals,
7169 -- or else all its components are static aggregates for the
7170 -- component type. We also limit the size of a static aggregate
7171 -- to prevent runaway static expressions.
7173 if Is_Array_Type
(Comp_Type
)
7174 or else Is_Record_Type
(Comp_Type
)
7176 if Nkind
(Expression
(Expr
)) /= N_Aggregate
7178 not Compile_Time_Known_Aggregate
(Expression
(Expr
))
7183 elsif Nkind
(Expression
(Expr
)) /= N_Integer_Literal
then
7187 if not Aggr_Size_OK
(N
, Typ
) then
7191 -- Create a positional aggregate with the right number of
7192 -- copies of the expression.
7194 Agg
:= Make_Aggregate
(Sloc
(N
), New_List
, No_List
);
7196 for I
in UI_To_Int
(Intval
(Lo
)) .. UI_To_Int
(Intval
(Hi
))
7198 Append_To
(Expressions
(Agg
), New_Copy
(Expression
(Expr
)));
7200 -- The copied expression must be analyzed and resolved.
7201 -- Besides setting the type, this ensures that static
7202 -- expressions are appropriately marked as such.
7205 (Last
(Expressions
(Agg
)), Component_Type
(Typ
));
7208 Set_Aggregate_Bounds
(Agg
, Bounds
);
7209 Set_Etype
(Agg
, Typ
);
7212 Set_Compile_Time_Known_Aggregate
(N
);
7221 end Static_Array_Aggregate
;