1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2023, 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 Aspects
; use Aspects
;
27 with Atree
; use Atree
;
28 with Checks
; use Checks
;
29 with Debug
; use Debug
;
30 with Einfo
; use Einfo
;
31 with Einfo
.Entities
; use Einfo
.Entities
;
32 with Einfo
.Utils
; use Einfo
.Utils
;
33 with Elists
; use Elists
;
34 with Errout
; use Errout
;
35 with Expander
; use Expander
;
36 with Exp_Util
; use Exp_Util
;
37 with Exp_Ch3
; use Exp_Ch3
;
38 with Exp_Ch6
; use Exp_Ch6
;
39 with Exp_Ch7
; use Exp_Ch7
;
40 with Exp_Ch9
; use Exp_Ch9
;
41 with Exp_Disp
; use Exp_Disp
;
42 with Exp_Tss
; use Exp_Tss
;
43 with Freeze
; use Freeze
;
44 with Itypes
; use Itypes
;
46 with Namet
; use Namet
;
47 with Nmake
; use Nmake
;
48 with Nlists
; use Nlists
;
50 with Restrict
; use Restrict
;
51 with Rident
; use Rident
;
52 with Rtsfind
; use Rtsfind
;
53 with Ttypes
; use Ttypes
;
55 with Sem_Aggr
; use Sem_Aggr
;
56 with Sem_Aux
; use Sem_Aux
;
57 with Sem_Case
; use Sem_Case
;
58 with Sem_Ch3
; use Sem_Ch3
;
59 with Sem_Ch8
; use Sem_Ch8
;
60 with Sem_Ch13
; use Sem_Ch13
;
61 with Sem_Eval
; use Sem_Eval
;
62 with Sem_Mech
; use Sem_Mech
;
63 with Sem_Res
; use Sem_Res
;
64 with Sem_Type
; use Sem_Type
;
65 with Sem_Util
; use Sem_Util
;
66 use Sem_Util
.Storage_Model_Support
;
67 with Sinfo
; use Sinfo
;
68 with Sinfo
.Nodes
; use Sinfo
.Nodes
;
69 with Sinfo
.Utils
; use Sinfo
.Utils
;
70 with Snames
; use Snames
;
71 with Stand
; use Stand
;
72 with Stringt
; use Stringt
;
73 with Tbuild
; use Tbuild
;
74 with Uintp
; use Uintp
;
75 with Urealp
; use Urealp
;
76 with Warnsw
; use Warnsw
;
78 package body Exp_Aggr
is
80 function Build_Assignment_With_Temporary
83 Source
: Node_Id
) return List_Id
;
84 -- Returns a list of actions to assign Source to Target of type Typ using
85 -- an extra temporary, which can potentially be large.
87 type Case_Bounds
is record
90 Choice_Node
: Node_Id
;
93 type Case_Table_Type
is array (Nat
range <>) of Case_Bounds
;
94 -- Table type used by Check_Case_Choices procedure
96 procedure Expand_Delta_Array_Aggregate
(N
: Node_Id
; Deltas
: List_Id
);
97 procedure Expand_Delta_Record_Aggregate
(N
: Node_Id
; Deltas
: List_Id
);
98 procedure Expand_Container_Aggregate
(N
: Node_Id
);
100 function Get_Base_Object
(N
: Node_Id
) return Entity_Id
;
101 -- Return the base object, i.e. the outermost prefix object, that N refers
102 -- to statically, or Empty if it cannot be determined. The assumption is
103 -- that all dereferences are explicit in the tree rooted at N.
105 function Has_Default_Init_Comps
(N
: Node_Id
) return Boolean;
106 -- N is an aggregate (record or array). Checks the presence of default
107 -- initialization (<>) in any component (Ada 2005: AI-287).
109 procedure Initialize_Component
115 -- Perform the initialization of component Comp with expected type
116 -- Comp_Typ of aggregate N. Init_Expr denotes the initialization
117 -- expression of the component. All generated code is added to Stmts.
119 function Is_CCG_Supported_Aggregate
(N
: Node_Id
) return Boolean;
120 -- Return True if aggregate N is located in a context supported by the
121 -- CCG backend; False otherwise.
123 function Is_Static_Dispatch_Table_Aggregate
(N
: Node_Id
) return Boolean;
124 -- Returns true if N is an aggregate used to initialize the components
125 -- of a statically allocated dispatch table.
127 function Late_Expansion
130 Target
: Node_Id
) return List_Id
;
131 -- This routine implements top-down expansion of nested aggregates. In
132 -- doing so, it avoids the generation of temporaries at each level. N is
133 -- a nested record or array aggregate with the Expansion_Delayed flag.
134 -- Typ is the expected type of the aggregate. Target is a (duplicatable)
135 -- expression that will hold the result of the aggregate expansion.
137 function Make_OK_Assignment_Statement
140 Expression
: Node_Id
) return Node_Id
;
141 -- This is like Make_Assignment_Statement, except that Assignment_OK
142 -- is set in the left operand. All assignments built by this unit use
143 -- this routine. This is needed to deal with assignments to initialized
144 -- constants that are done in place.
148 Obj_Type
: Entity_Id
;
149 Typ
: Entity_Id
) return Boolean;
150 -- A static array aggregate in an object declaration can in most cases be
151 -- expanded in place. The one exception is when the aggregate is given
152 -- with component associations that specify different bounds from those of
153 -- the type definition in the object declaration. In this pathological
154 -- case the aggregate must slide, and we must introduce an intermediate
155 -- temporary to hold it.
157 -- The same holds in an assignment to one-dimensional array of arrays,
158 -- when a component may be given with bounds that differ from those of the
161 function Number_Of_Choices
(N
: Node_Id
) return Nat
;
162 -- Returns the number of discrete choices (not including the others choice
163 -- if present) contained in (sub-)aggregate N.
165 procedure Sort_Case_Table
(Case_Table
: in out Case_Table_Type
);
166 -- Sort the Case Table using the Lower Bound of each Choice as the key.
167 -- A simple insertion sort is used since the number of choices in a case
168 -- statement of variant part will usually be small and probably in near
171 ------------------------------------------------------
172 -- Local subprograms for Record Aggregate Expansion --
173 ------------------------------------------------------
175 function Is_Build_In_Place_Aggregate_Return
(N
: Node_Id
) return Boolean;
176 -- True if N is an aggregate (possibly qualified or converted) that is
177 -- being returned from a build-in-place function.
179 function Build_Record_Aggr_Code
182 Lhs
: Node_Id
) return List_Id
;
183 -- N is an N_Aggregate or an N_Extension_Aggregate. Typ is the type of the
184 -- aggregate. Target is an expression containing the location on which the
185 -- component by component assignments will take place. Returns the list of
186 -- assignments plus all other adjustments needed for tagged and controlled
189 procedure Convert_To_Assignments
(N
: Node_Id
; Typ
: Entity_Id
);
190 -- Transform a record aggregate into a sequence of assignments performed
191 -- component by component. N is an N_Aggregate or N_Extension_Aggregate.
192 -- Typ is the type of the record aggregate.
194 procedure Expand_Record_Aggregate
196 Orig_Tag
: Node_Id
:= Empty
;
197 Parent_Expr
: Node_Id
:= Empty
);
198 -- This is the top level procedure for record aggregate expansion.
199 -- Expansion for record aggregates needs expand aggregates for tagged
200 -- record types. Specifically Expand_Record_Aggregate adds the Tag
201 -- field in front of the Component_Association list that was created
202 -- during resolution by Resolve_Record_Aggregate.
204 -- N is the record aggregate node.
205 -- Orig_Tag is the value of the Tag that has to be provided for this
206 -- specific aggregate. It carries the tag corresponding to the type
207 -- of the outermost aggregate during the recursive expansion
208 -- Parent_Expr is the ancestor part of the original extension
211 function Has_Mutable_Components
(Typ
: Entity_Id
) return Boolean;
212 -- Return true if one of the components is of a discriminated type with
213 -- defaults. An aggregate for a type with mutable components must be
214 -- expanded into individual assignments.
216 function In_Place_Assign_OK
218 Target_Object
: Entity_Id
:= Empty
) return Boolean;
219 -- Predicate to determine whether an aggregate assignment can be done in
220 -- place, because none of the new values can depend on the components of
221 -- the target of the assignment.
223 procedure Initialize_Discriminants
(N
: Node_Id
; Typ
: Entity_Id
);
224 -- If the type of the aggregate is a type extension with renamed discrimi-
225 -- nants, we must initialize the hidden discriminants of the parent.
226 -- Otherwise, the target object must not be initialized. The discriminants
227 -- are initialized by calling the initialization procedure for the type.
228 -- This is incorrect if the initialization of other components has any
229 -- side effects. We restrict this call to the case where the parent type
230 -- has a variant part, because this is the only case where the hidden
231 -- discriminants are accessed, namely when calling discriminant checking
232 -- functions of the parent type, and when applying a stream attribute to
233 -- an object of the derived type.
235 -----------------------------------------------------
236 -- Local Subprograms for Array Aggregate Expansion --
237 -----------------------------------------------------
239 function Aggr_Assignment_OK_For_Backend
(N
: Node_Id
) return Boolean;
240 -- Returns true if an aggregate assignment can be done by the back end
242 function Aggr_Size_OK
(N
: Node_Id
) return Boolean;
243 -- Very large static aggregates present problems to the back-end, and are
244 -- transformed into assignments and loops. This function verifies that the
245 -- total number of components of an aggregate is acceptable for rewriting
246 -- into a purely positional static form. Aggr_Size_OK must be called before
249 -- This function also detects and warns about one-component aggregates that
250 -- appear in a nonstatic context. Even if the component value is static,
251 -- such an aggregate must be expanded into an assignment.
253 function Backend_Processing_Possible
(N
: Node_Id
) return Boolean;
254 -- This function checks if array aggregate N can be processed directly
255 -- by the backend. If this is the case, True is returned.
257 function Build_Array_Aggr_Code
262 Scalar_Comp
: Boolean;
263 Indexes
: List_Id
:= No_List
) return List_Id
;
264 -- This recursive routine returns a list of statements containing the
265 -- loops and assignments that are needed for the expansion of the array
268 -- N is the (sub-)aggregate node to be expanded into code. This node has
269 -- been fully analyzed, and its Etype is properly set.
271 -- Index is the index node corresponding to the array subaggregate N
273 -- Into is the target expression into which we are copying the aggregate.
274 -- Note that this node may not have been analyzed yet, and so the Etype
275 -- field may not be set.
277 -- Scalar_Comp is True if the component type of the aggregate is scalar
279 -- Indexes is the current list of expressions used to index the object we
282 procedure Convert_Array_Aggr_In_Allocator
286 -- If the aggregate appears within an allocator and can be expanded in
287 -- place, this routine generates the individual assignments to components
288 -- of the designated object. This is an optimization over the general
289 -- case, where a temporary is first created on the stack and then used to
290 -- construct the allocated object on the heap.
292 procedure Convert_To_Positional
294 Handle_Bit_Packed
: Boolean := False);
295 -- If possible, convert named notation to positional notation. This
296 -- conversion is possible only in some static cases. If the conversion is
297 -- possible, then N is rewritten with the analyzed converted aggregate.
298 -- The parameter Handle_Bit_Packed is usually set False (since we do
299 -- not expect the back end to handle bit packed arrays, so the normal case
300 -- of conversion is pointless), but in the special case of a call from
301 -- Packed_Array_Aggregate_Handled, we set this parameter to True, since
302 -- these are cases we handle in there.
304 procedure Expand_Array_Aggregate
(N
: Node_Id
);
305 -- This is the top-level routine to perform array aggregate expansion.
306 -- N is the N_Aggregate node to be expanded.
308 function Is_Two_Dim_Packed_Array
(Typ
: Entity_Id
) return Boolean;
309 -- For 2D packed array aggregates with constant bounds and constant scalar
310 -- components, it is preferable to pack the inner aggregates because the
311 -- whole matrix can then be presented to the back-end as a one-dimensional
312 -- list of literals. This is much more efficient than expanding into single
313 -- component assignments. This function determines if the type Typ is for
314 -- an array that is suitable for this optimization: it returns True if Typ
315 -- is a two dimensional bit packed array with component size 1, 2, or 4.
317 function Max_Aggregate_Size
319 Default_Size
: Nat
:= 5000) return Nat
;
320 -- Return the max size for a static aggregate N. Return Default_Size if no
321 -- other special criteria trigger.
323 function Packed_Array_Aggregate_Handled
(N
: Node_Id
) return Boolean;
324 -- Given an array aggregate, this function handles the case of a packed
325 -- array aggregate with all constant values, where the aggregate can be
326 -- evaluated at compile time. If this is possible, then N is rewritten
327 -- to be its proper compile time value with all the components properly
328 -- assembled. The expression is analyzed and resolved and True is returned.
329 -- If this transformation is not possible, N is unchanged and False is
332 function Two_Dim_Packed_Array_Handled
(N
: Node_Id
) return Boolean;
333 -- If the type of the aggregate is a two-dimensional bit_packed array
334 -- it may be transformed into an array of bytes with constant values,
335 -- and presented to the back-end as a static value. The function returns
336 -- false if this transformation cannot be performed. THis is similar to,
337 -- and reuses part of the machinery in Packed_Array_Aggregate_Handled.
339 ------------------------------------
340 -- Aggr_Assignment_OK_For_Backend --
341 ------------------------------------
343 -- Back-end processing by Gigi/gcc is possible only if all the following
344 -- conditions are met:
346 -- 1. N consists of a single OTHERS choice, possibly recursively, or
347 -- of a single choice, possibly recursively, if it is surrounded by
348 -- a qualified expression whose subtype mark is unconstrained.
350 -- 2. The array type has no null ranges (the purpose of this is to
351 -- avoid a bogus warning for an out-of-range value).
353 -- 3. The array type has no atomic components
355 -- 4. The component type is elementary
357 -- 5. The component size is a multiple of Storage_Unit
359 -- 6. The component size is Storage_Unit or the value is of the form
360 -- M * (1 + A**1 + A**2 + .. A**(K-1)) where A = 2**(Storage_Unit)
361 -- and M in 0 .. A-1. This can also be viewed as K occurrences of
362 -- the Storage_Unit value M, concatenated together.
364 -- The ultimate goal is to generate a call to a fast memset routine
365 -- specifically optimized for the target.
367 function Aggr_Assignment_OK_For_Backend
(N
: Node_Id
) return Boolean is
369 function Is_OK_Aggregate
(Aggr
: Node_Id
) return Boolean;
370 -- Return true if Aggr is suitable for back-end assignment
372 ---------------------
373 -- Is_OK_Aggregate --
374 ---------------------
376 function Is_OK_Aggregate
(Aggr
: Node_Id
) return Boolean is
377 Assoc
: constant List_Id
:= Component_Associations
(Aggr
);
380 -- An "others" aggregate is most likely OK, but see below
382 if Is_Others_Aggregate
(Aggr
) then
385 -- An aggregate with a single choice requires a qualified expression
386 -- whose subtype mark is an unconstrained type because we need it to
387 -- have the semantics of an "others" aggregate.
389 elsif Nkind
(Parent
(N
)) = N_Qualified_Expression
390 and then not Is_Constrained
(Entity
(Subtype_Mark
(Parent
(N
))))
391 and then Is_Single_Aggregate
(Aggr
)
395 -- The other cases are not OK
401 -- In any case we do not support an iterated association
403 return Nkind
(First
(Assoc
)) /= N_Iterated_Component_Association
;
406 Bounds
: Range_Nodes
;
407 Csiz
: Uint
:= No_Uint
;
415 -- Start of processing for Aggr_Assignment_OK_For_Backend
418 -- Back end doesn't know about <>
420 if Has_Default_Init_Comps
(N
) then
424 -- Recurse as far as possible to find the innermost component type
428 while Is_Array_Type
(Ctyp
) loop
429 if Nkind
(Expr
) /= N_Aggregate
430 or else not Is_OK_Aggregate
(Expr
)
435 Index
:= First_Index
(Ctyp
);
436 while Present
(Index
) loop
437 Bounds
:= Get_Index_Bounds
(Index
);
439 if Is_Null_Range
(Bounds
.First
, Bounds
.Last
) then
446 Expr
:= Expression
(First
(Component_Associations
(Expr
)));
448 for J
in 1 .. Number_Dimensions
(Ctyp
) - 1 loop
449 if Nkind
(Expr
) /= N_Aggregate
450 or else not Is_OK_Aggregate
(Expr
)
455 Expr
:= Expression
(First
(Component_Associations
(Expr
)));
458 if Has_Atomic_Components
(Ctyp
) then
462 Csiz
:= Component_Size
(Ctyp
);
463 Ctyp
:= Component_Type
(Ctyp
);
465 if Is_Full_Access
(Ctyp
) then
470 -- Access types need to be dealt with specially
472 if Is_Access_Type
(Ctyp
) then
474 -- Component_Size is not set by Layout_Type if the component
475 -- type is an access type ???
477 Csiz
:= Esize
(Ctyp
);
479 -- Fat pointers are rejected as they are not really elementary
482 if No
(Csiz
) or else Csiz
/= System_Address_Size
then
486 -- The supported expressions are NULL and constants, others are
487 -- rejected upfront to avoid being analyzed below, which can be
488 -- problematic for some of them, for example allocators.
490 if Nkind
(Expr
) /= N_Null
and then not Is_Entity_Name
(Expr
) then
494 -- Scalar types are OK if their size is a multiple of Storage_Unit
496 elsif Is_Scalar_Type
(Ctyp
) and then Present
(Csiz
) then
498 if Csiz
mod System_Storage_Unit
/= 0 then
502 -- Composite types are rejected
508 -- If the expression has side effects (e.g. contains calls with
509 -- potential side effects) reject as well. We only preanalyze the
510 -- expression to prevent the removal of intended side effects.
512 Preanalyze_And_Resolve
(Expr
, Ctyp
);
514 if not Side_Effect_Free
(Expr
) then
518 -- The expression needs to be analyzed if True is returned
520 Analyze_And_Resolve
(Expr
, Ctyp
);
522 -- Strip away any conversions from the expression as they simply
523 -- qualify the real expression.
525 while Nkind
(Expr
) in N_Unchecked_Type_Conversion | N_Type_Conversion
527 Expr
:= Expression
(Expr
);
530 Nunits
:= UI_To_Int
(Csiz
) / System_Storage_Unit
;
536 if not Compile_Time_Known_Value
(Expr
) then
540 -- The only supported value for floating point is 0.0
542 if Is_Floating_Point_Type
(Ctyp
) then
543 return Expr_Value_R
(Expr
) = Ureal_0
;
546 -- For other types, we can look into the value as an integer, which
547 -- means the representation value for enumeration literals.
549 Value
:= Expr_Rep_Value
(Expr
);
551 if Has_Biased_Representation
(Ctyp
) then
552 Value
:= Value
- Expr_Value
(Type_Low_Bound
(Ctyp
));
555 -- Values 0 and -1 immediately satisfy the last check
557 if Value
= Uint_0
or else Value
= Uint_Minus_1
then
561 -- We need to work with an unsigned value
564 Value
:= Value
+ 2**(System_Storage_Unit
* Nunits
);
567 Remainder
:= Value
rem 2**System_Storage_Unit
;
569 for J
in 1 .. Nunits
- 1 loop
570 Value
:= Value
/ 2**System_Storage_Unit
;
572 if Value
rem 2**System_Storage_Unit
/= Remainder
then
578 end Aggr_Assignment_OK_For_Backend
;
584 function Aggr_Size_OK
(N
: Node_Id
) return Boolean is
585 Typ
: constant Entity_Id
:= Etype
(N
);
594 -- Determines the maximum size of an array aggregate produced by
595 -- converting named to positional notation (e.g. from others clauses).
596 -- This avoids running away with attempts to convert huge aggregates,
597 -- which hit memory limits in the backend.
599 function Component_Count
(T
: Entity_Id
) return Nat
;
600 -- The limit is applied to the total number of subcomponents that the
601 -- aggregate will have, which is the number of static expressions
602 -- that will appear in the flattened array. This requires a recursive
603 -- computation of the number of scalar components of the structure.
605 ---------------------
606 -- Component_Count --
607 ---------------------
609 function Component_Count
(T
: Entity_Id
) return Nat
is
614 if Is_Scalar_Type
(T
) then
617 elsif Is_Record_Type
(T
) then
618 Comp
:= First_Component
(T
);
619 while Present
(Comp
) loop
620 Res
:= Res
+ Component_Count
(Etype
(Comp
));
621 Next_Component
(Comp
);
626 elsif Is_Array_Type
(T
) then
628 Lo
: constant Node_Id
:=
629 Type_Low_Bound
(Etype
(First_Index
(T
)));
630 Hi
: constant Node_Id
:=
631 Type_High_Bound
(Etype
(First_Index
(T
)));
633 Siz
: constant Nat
:= Component_Count
(Component_Type
(T
));
636 -- Check for superflat arrays, i.e. arrays with such bounds
637 -- as 4 .. 2, to insure that this function never returns a
638 -- meaningless negative value.
640 if not Compile_Time_Known_Value
(Lo
)
641 or else not Compile_Time_Known_Value
(Hi
)
642 or else Expr_Value
(Hi
) < Expr_Value
(Lo
)
647 -- If the number of components is greater than Int'Last,
648 -- then return Int'Last, so caller will return False (Aggr
649 -- size is not OK). Otherwise, UI_To_Int will crash.
652 UI
: constant Uint
:=
653 (Expr_Value
(Hi
) - Expr_Value
(Lo
) + 1) * Siz
;
655 if UI_Is_In_Int_Range
(UI
) then
656 return UI_To_Int
(UI
);
665 -- Can only be a null for an access type
671 -- Start of processing for Aggr_Size_OK
674 -- We bump the maximum size unless the aggregate has a single component
675 -- association, which will be more efficient if implemented with a loop.
676 -- The -gnatd_g switch disables this bumping.
678 if (No
(Expressions
(N
))
679 and then No
(Next
(First
(Component_Associations
(N
)))))
680 or else Debug_Flag_Underscore_G
682 Max_Aggr_Size
:= Max_Aggregate_Size
(N
);
684 Max_Aggr_Size
:= Max_Aggregate_Size
(N
, 500_000
);
687 Size
:= UI_From_Int
(Component_Count
(Component_Type
(Typ
)));
689 Indx
:= First_Index
(Typ
);
690 while Present
(Indx
) loop
691 Lo
:= Type_Low_Bound
(Etype
(Indx
));
692 Hi
:= Type_High_Bound
(Etype
(Indx
));
694 -- Bounds need to be known at compile time
696 if not Compile_Time_Known_Value
(Lo
)
697 or else not Compile_Time_Known_Value
(Hi
)
702 Lov
:= Expr_Value
(Lo
);
703 Hiv
:= Expr_Value
(Hi
);
705 -- A flat array is always safe
711 -- One-component aggregates are suspicious, and if the context type
712 -- is an object declaration with nonstatic bounds it will trip gcc;
713 -- such an aggregate must be expanded into a single assignment.
715 if Hiv
= Lov
and then Nkind
(Parent
(N
)) = N_Object_Declaration
then
717 Index_Type
: constant Entity_Id
:=
719 (First_Index
(Etype
(Defining_Identifier
(Parent
(N
)))));
723 if not Compile_Time_Known_Value
(Type_Low_Bound
(Index_Type
))
724 or else not Compile_Time_Known_Value
725 (Type_High_Bound
(Index_Type
))
727 if Present
(Component_Associations
(N
)) then
730 (Choice_List
(First
(Component_Associations
(N
))));
732 if Is_Entity_Name
(Indx
)
733 and then not Is_Type
(Entity
(Indx
))
736 ("single component aggregate in "
737 & "non-static context??", Indx
);
738 Error_Msg_N
("\maybe subtype name was meant??", Indx
);
748 Rng
: constant Uint
:= Hiv
- Lov
+ 1;
751 -- Check if size is too large
753 if not UI_Is_In_Int_Range
(Rng
) then
757 -- Compute the size using universal arithmetic to avoid the
758 -- possibility of overflow on very large aggregates.
763 or else Size
> Max_Aggr_Size
769 -- Bounds must be in integer range, for later array construction
771 if not UI_Is_In_Int_Range
(Lov
)
773 not UI_Is_In_Int_Range
(Hiv
)
784 ---------------------------------
785 -- Backend_Processing_Possible --
786 ---------------------------------
788 -- Backend processing by Gigi/gcc is possible only if all the following
789 -- conditions are met:
791 -- 1. N is fully positional
793 -- 2. N is not a bit-packed array aggregate;
795 -- 3. The size of N's array type must be known at compile time. Note
796 -- that this implies that the component size is also known
798 -- 4. The array type of N does not follow the Fortran layout convention
799 -- or if it does it must be 1 dimensional.
801 -- 5. The array component type may not be tagged (which could necessitate
802 -- reassignment of proper tags).
804 -- 6. The array component type must not have unaligned bit components
806 -- 7. None of the components of the aggregate may be bit unaligned
809 -- 8. There cannot be delayed components, since we do not know enough
810 -- at this stage to know if back end processing is possible.
812 -- 9. There cannot be any discriminated record components, since the
813 -- back end cannot handle this complex case.
815 -- 10. No controlled actions need to be generated for components
817 -- 11. When generating C code, N must be part of a N_Object_Declaration
819 -- 12. When generating C code, N must not include function calls
821 function Backend_Processing_Possible
(N
: Node_Id
) return Boolean is
822 Typ
: constant Entity_Id
:= Etype
(N
);
823 -- Typ is the correct constrained array subtype of the aggregate
825 function Component_Check
(N
: Node_Id
; Index
: Node_Id
) return Boolean;
826 -- This routine checks components of aggregate N, enforcing checks
827 -- 1, 7, 8, 9, 11, and 12. In the multidimensional case, these checks
828 -- are performed on subaggregates. The Index value is the current index
829 -- being checked in the multidimensional case.
831 ---------------------
832 -- Component_Check --
833 ---------------------
835 function Component_Check
(N
: Node_Id
; Index
: Node_Id
) return Boolean is
836 function Ultimate_Original_Expression
(N
: Node_Id
) return Node_Id
;
837 -- Given a type conversion or an unchecked type conversion N, return
838 -- its innermost original expression.
840 ----------------------------------
841 -- Ultimate_Original_Expression --
842 ----------------------------------
844 function Ultimate_Original_Expression
(N
: Node_Id
) return Node_Id
is
845 Expr
: Node_Id
:= Original_Node
(N
);
848 while Nkind
(Expr
) in
849 N_Type_Conversion | N_Unchecked_Type_Conversion
851 Expr
:= Original_Node
(Expression
(Expr
));
855 end Ultimate_Original_Expression
;
861 -- Start of processing for Component_Check
864 -- Checks 1: (no component associations)
866 if Present
(Component_Associations
(N
)) then
870 -- Checks 11: The C code generator cannot handle aggregates that are
871 -- not part of an object declaration.
873 if Modify_Tree_For_C
and then not Is_CCG_Supported_Aggregate
(N
) then
877 -- Checks on components
879 -- Recurse to check subaggregates, which may appear in qualified
880 -- expressions. If delayed, the front-end will have to expand.
881 -- If the component is a discriminated record, treat as nonstatic,
882 -- as the back-end cannot handle this properly.
884 Expr
:= First
(Expressions
(N
));
885 while Present
(Expr
) loop
887 -- Checks 8: (no delayed components)
889 if Is_Delayed_Aggregate
(Expr
) then
893 -- Checks 9: (no discriminated records)
895 if Present
(Etype
(Expr
))
896 and then Is_Record_Type
(Etype
(Expr
))
897 and then Has_Discriminants
(Etype
(Expr
))
902 -- Checks 7. Component must not be bit aligned component
904 if Possible_Bit_Aligned_Component
(Expr
) then
908 -- Checks 12: (no function call)
912 Nkind
(Ultimate_Original_Expression
(Expr
)) = N_Function_Call
917 -- Recursion to following indexes for multiple dimension case
919 if Present
(Next_Index
(Index
))
920 and then not Component_Check
(Expr
, Next_Index
(Index
))
925 -- All checks for that component finished, on to next
933 -- Start of processing for Backend_Processing_Possible
936 -- Checks 2 (array not bit packed) and 10 (no controlled actions)
938 if Is_Bit_Packed_Array
(Typ
) or else Needs_Finalization
(Typ
) then
942 -- If component is limited, aggregate must be expanded because each
943 -- component assignment must be built in place.
945 if Is_Limited_View
(Component_Type
(Typ
)) then
949 -- Checks 4 (array must not be multidimensional Fortran case)
951 if Convention
(Typ
) = Convention_Fortran
952 and then Number_Dimensions
(Typ
) > 1
957 -- Checks 3 (size of array must be known at compile time)
959 if not Size_Known_At_Compile_Time
(Typ
) then
963 -- Checks on components
965 if not Component_Check
(N
, First_Index
(Typ
)) then
969 -- Checks 5 (if the component type is tagged, then we may need to do
970 -- tag adjustments. Perhaps this should be refined to check for any
971 -- component associations that actually need tag adjustment, similar
972 -- to the test in Component_OK_For_Backend for record aggregates with
973 -- tagged components, but not clear whether it's worthwhile ???; in the
974 -- case of virtual machines (no Tagged_Type_Expansion), object tags are
975 -- handled implicitly).
977 if Is_Tagged_Type
(Component_Type
(Typ
))
978 and then Tagged_Type_Expansion
983 -- Checks 6 (component type must not have bit aligned components)
985 if Type_May_Have_Bit_Aligned_Components
(Component_Type
(Typ
)) then
989 -- Backend processing is possible
992 end Backend_Processing_Possible
;
994 ---------------------------
995 -- Build_Array_Aggr_Code --
996 ---------------------------
998 -- The code that we generate from a one dimensional aggregate is
1000 -- 1. If the subaggregate contains discrete choices we
1002 -- (a) Sort the discrete choices
1004 -- (b) Otherwise for each discrete choice that specifies a range we
1005 -- emit a loop. If a range specifies a maximum of three values, or
1006 -- we are dealing with an expression we emit a sequence of
1007 -- assignments instead of a loop.
1009 -- (c) Generate the remaining loops to cover the others choice if any
1011 -- 2. If the aggregate contains positional elements we
1013 -- (a) Translate the positional elements in a series of assignments
1015 -- (b) Generate a final loop to cover the others choice if any.
1016 -- Note that this final loop has to be a while loop since the case
1018 -- L : Integer := Integer'Last;
1019 -- H : Integer := Integer'Last;
1020 -- A : array (L .. H) := (1, others =>0);
1022 -- cannot be handled by a for loop. Thus for the following
1024 -- array (L .. H) := (.. positional elements.., others => E);
1026 -- we always generate something like:
1028 -- J : Index_Type := Index_Of_Last_Positional_Element;
1030 -- J := Index_Base'Succ (J)
1034 function Build_Array_Aggr_Code
1039 Scalar_Comp
: Boolean;
1040 Indexes
: List_Id
:= No_List
) return List_Id
1042 Loc
: constant Source_Ptr
:= Sloc
(N
);
1043 Typ
: constant Entity_Id
:= Etype
(N
);
1044 Index_Base
: constant Entity_Id
:= Base_Type
(Etype
(Index
));
1045 Index_Base_L
: constant Node_Id
:= Type_Low_Bound
(Index_Base
);
1046 Index_Base_H
: constant Node_Id
:= Type_High_Bound
(Index_Base
);
1048 function Add
(Val
: Int
; To
: Node_Id
) return Node_Id
;
1049 -- Returns an expression where Val is added to expression To, unless
1050 -- To+Val is provably out of To's base type range. To must be an
1051 -- already analyzed expression.
1053 function Empty_Range
(L
, H
: Node_Id
) return Boolean;
1054 -- Returns True if the range defined by L .. H is certainly empty
1056 function Equal
(L
, H
: Node_Id
) return Boolean;
1057 -- Returns True if L = H for sure
1059 function Index_Base_Name
return Node_Id
;
1060 -- Returns a new reference to the index type name
1064 Expr
: Node_Id
) return List_Id
;
1065 -- Ind must be a side-effect-free expression. If the input aggregate N
1066 -- to Build_Loop contains no subaggregates, then this function returns
1067 -- the assignment statement:
1069 -- Into (Indexes, Ind) := Expr;
1071 -- Otherwise we call Build_Code recursively.
1073 -- Ada 2005 (AI-287): In case of default initialized component, Expr
1074 -- is empty and we generate a call to the corresponding IP subprogram.
1076 function Gen_Loop
(L
, H
: Node_Id
; Expr
: Node_Id
) return List_Id
;
1077 -- Nodes L and H must be side-effect-free expressions. If the input
1078 -- aggregate N to Build_Loop contains no subaggregates, this routine
1079 -- returns the for loop statement:
1081 -- for J in Index_Base'(L) .. Index_Base'(H) loop
1082 -- Into (Indexes, J) := Expr;
1085 -- Otherwise we call Build_Code recursively. As an optimization if the
1086 -- loop covers 3 or fewer scalar elements we generate a sequence of
1088 -- If the component association that generates the loop comes from an
1089 -- Iterated_Component_Association, the loop parameter has the name of
1090 -- the corresponding parameter in the original construct.
1092 function Gen_While
(L
, H
: Node_Id
; Expr
: Node_Id
) return List_Id
;
1093 -- Nodes L and H must be side-effect-free expressions. If the input
1094 -- aggregate N to Build_Loop contains no subaggregates, this routine
1095 -- returns the while loop statement:
1097 -- J : Index_Base := L;
1099 -- J := Index_Base'Succ (J);
1100 -- Into (Indexes, J) := Expr;
1103 -- Otherwise we call Build_Code recursively
1105 function Get_Assoc_Expr
(Assoc
: Node_Id
) return Node_Id
;
1106 -- For an association with a box, use value given by aspect
1107 -- Default_Component_Value of array type if specified, else use
1108 -- value given by aspect Default_Value for component type itself
1109 -- if specified, else return Empty.
1111 function Local_Compile_Time_Known_Value
(E
: Node_Id
) return Boolean;
1112 function Local_Expr_Value
(E
: Node_Id
) return Uint
;
1113 -- These two Local routines are used to replace the corresponding ones
1114 -- in sem_eval because while processing the bounds of an aggregate with
1115 -- discrete choices whose index type is an enumeration, we build static
1116 -- expressions not recognized by Compile_Time_Known_Value as such since
1117 -- they have not yet been analyzed and resolved. All the expressions in
1118 -- question are things like Index_Base_Name'Val (Const) which we can
1119 -- easily recognize as being constant.
1125 function Add
(Val
: Int
; To
: Node_Id
) return Node_Id
is
1130 U_Val
: constant Uint
:= UI_From_Int
(Val
);
1133 -- Note: do not try to optimize the case of Val = 0, because
1134 -- we need to build a new node with the proper Sloc value anyway.
1136 -- First test if we can do constant folding
1138 if Local_Compile_Time_Known_Value
(To
) then
1139 U_To
:= Local_Expr_Value
(To
) + Val
;
1141 -- Determine if our constant is outside the range of the index.
1142 -- If so return an Empty node. This empty node will be caught
1143 -- by Empty_Range below.
1145 if Compile_Time_Known_Value
(Index_Base_L
)
1146 and then U_To
< Expr_Value
(Index_Base_L
)
1150 elsif Compile_Time_Known_Value
(Index_Base_H
)
1151 and then U_To
> Expr_Value
(Index_Base_H
)
1156 Expr_Pos
:= Make_Integer_Literal
(Loc
, U_To
);
1157 Set_Is_Static_Expression
(Expr_Pos
);
1159 if not Is_Enumeration_Type
(Index_Base
) then
1162 -- If we are dealing with enumeration return
1163 -- Index_Base'Val (Expr_Pos)
1167 Make_Attribute_Reference
1169 Prefix
=> Index_Base_Name
,
1170 Attribute_Name
=> Name_Val
,
1171 Expressions
=> New_List
(Expr_Pos
));
1177 -- If we are here no constant folding possible
1179 if not Is_Enumeration_Type
(Index_Base
) then
1182 Left_Opnd
=> Duplicate_Subexpr
(To
),
1183 Right_Opnd
=> Make_Integer_Literal
(Loc
, U_Val
));
1185 -- If we are dealing with enumeration return
1186 -- Index_Base'Val (Index_Base'Pos (To) + Val)
1190 Make_Attribute_Reference
1192 Prefix
=> Index_Base_Name
,
1193 Attribute_Name
=> Name_Pos
,
1194 Expressions
=> New_List
(Duplicate_Subexpr
(To
)));
1198 Left_Opnd
=> To_Pos
,
1199 Right_Opnd
=> Make_Integer_Literal
(Loc
, U_Val
));
1202 Make_Attribute_Reference
1204 Prefix
=> Index_Base_Name
,
1205 Attribute_Name
=> Name_Val
,
1206 Expressions
=> New_List
(Expr_Pos
));
1216 function Empty_Range
(L
, H
: Node_Id
) return Boolean is
1217 Is_Empty
: Boolean := False;
1222 -- First check if L or H were already detected as overflowing the
1223 -- index base range type by function Add above. If this is so Add
1224 -- returns the empty node.
1226 if No
(L
) or else No
(H
) then
1230 for J
in 1 .. 3 loop
1233 -- L > H range is empty
1239 -- B_L > H range must be empty
1242 Low
:= Index_Base_L
;
1245 -- L > B_H range must be empty
1249 High
:= Index_Base_H
;
1252 if Local_Compile_Time_Known_Value
(Low
)
1254 Local_Compile_Time_Known_Value
(High
)
1257 UI_Gt
(Local_Expr_Value
(Low
), Local_Expr_Value
(High
));
1270 function Equal
(L
, H
: Node_Id
) return Boolean is
1275 elsif Local_Compile_Time_Known_Value
(L
)
1277 Local_Compile_Time_Known_Value
(H
)
1279 return UI_Eq
(Local_Expr_Value
(L
), Local_Expr_Value
(H
));
1291 Expr
: Node_Id
) return List_Id
1293 function Add_Loop_Actions
(Lis
: List_Id
) return List_Id
;
1294 -- Collect insert_actions generated in the construction of a loop,
1295 -- and prepend them to the sequence of assignments to complete the
1296 -- eventual body of the loop.
1298 ----------------------
1299 -- Add_Loop_Actions --
1300 ----------------------
1302 function Add_Loop_Actions
(Lis
: List_Id
) return List_Id
is
1306 -- Ada 2005 (AI-287): Do nothing else in case of default
1307 -- initialized component.
1312 elsif Nkind
(Parent
(Expr
)) = N_Component_Association
1313 and then Present
(Loop_Actions
(Parent
(Expr
)))
1315 Append_List
(Lis
, Loop_Actions
(Parent
(Expr
)));
1316 Res
:= Loop_Actions
(Parent
(Expr
));
1317 Set_Loop_Actions
(Parent
(Expr
), No_List
);
1323 end Add_Loop_Actions
;
1327 Stmts
: constant List_Id
:= New_List
;
1329 Comp_Typ
: Entity_Id
:= Empty
;
1331 Indexed_Comp
: Node_Id
;
1332 Init_Call
: Node_Id
;
1333 New_Indexes
: List_Id
;
1335 -- Start of processing for Gen_Assign
1338 if No
(Indexes
) then
1339 New_Indexes
:= New_List
;
1341 New_Indexes
:= New_Copy_List_Tree
(Indexes
);
1344 Append_To
(New_Indexes
, Ind
);
1346 if Present
(Next_Index
(Index
)) then
1349 Build_Array_Aggr_Code
1352 Index
=> Next_Index
(Index
),
1354 Scalar_Comp
=> Scalar_Comp
,
1355 Indexes
=> New_Indexes
));
1358 -- If we get here then we are at a bottom-level (sub-)aggregate
1362 (Make_Indexed_Component
(Loc
,
1363 Prefix
=> New_Copy_Tree
(Into
),
1364 Expressions
=> New_Indexes
));
1366 Set_Assignment_OK
(Indexed_Comp
);
1368 -- Ada 2005 (AI-287): In case of default initialized component, Expr
1369 -- is not present (and therefore we also initialize Expr_Q to empty).
1371 Expr_Q
:= Unqualify
(Expr
);
1373 if Present
(Etype
(N
)) and then Etype
(N
) /= Any_Composite
then
1374 Comp_Typ
:= Component_Type
(Etype
(N
));
1375 pragma Assert
(Comp_Typ
= Ctype
); -- AI-287
1377 elsif Present
(Next
(First
(New_Indexes
))) then
1379 -- Ada 2005 (AI-287): Do nothing in case of default initialized
1380 -- component because we have received the component type in
1381 -- the formal parameter Ctype.
1383 -- ??? Some assert pragmas have been added to check if this new
1384 -- formal can be used to replace this code in all cases.
1386 if Present
(Expr
) then
1388 -- This is a multidimensional array. Recover the component type
1389 -- from the outermost aggregate, because subaggregates do not
1390 -- have an assigned type.
1397 while Present
(P
) loop
1398 if Nkind
(P
) = N_Aggregate
1399 and then Present
(Etype
(P
))
1401 Comp_Typ
:= Component_Type
(Etype
(P
));
1409 pragma Assert
(Comp_Typ
= Ctype
); -- AI-287
1414 -- Ada 2005 (AI-287): We only analyze the expression in case of non-
1415 -- default initialized components (otherwise Expr_Q is not present).
1418 and then Nkind
(Expr_Q
) in N_Aggregate | N_Extension_Aggregate
1420 -- At this stage the Expression may not have been analyzed yet
1421 -- because the array aggregate code has not been updated to use
1422 -- the Expansion_Delayed flag and avoid analysis altogether to
1423 -- solve the same problem (see Resolve_Aggr_Expr). So let us do
1424 -- the analysis of non-array aggregates now in order to get the
1425 -- value of Expansion_Delayed flag for the inner aggregate ???
1427 -- In the case of an iterated component association, the analysis
1428 -- of the generated loop will analyze the expression in the
1429 -- proper context, in which the loop parameter is visible.
1431 if Present
(Comp_Typ
) and then not Is_Array_Type
(Comp_Typ
) then
1432 if Nkind
(Parent
(Expr_Q
)) = N_Iterated_Component_Association
1433 or else Nkind
(Parent
(Parent
((Expr_Q
)))) =
1434 N_Iterated_Component_Association
1438 Analyze_And_Resolve
(Expr_Q
, Comp_Typ
);
1442 if Is_Delayed_Aggregate
(Expr_Q
) then
1444 -- This is either a subaggregate of a multidimensional array,
1445 -- or a component of an array type whose component type is
1446 -- also an array. In the latter case, the expression may have
1447 -- component associations that provide different bounds from
1448 -- those of the component type, and sliding must occur. Instead
1449 -- of decomposing the current aggregate assignment, force the
1450 -- reanalysis of the assignment, so that a temporary will be
1451 -- generated in the usual fashion, and sliding will take place.
1453 if Nkind
(Parent
(N
)) = N_Assignment_Statement
1454 and then Is_Array_Type
(Comp_Typ
)
1455 and then Present
(Component_Associations
(Expr_Q
))
1456 and then Must_Slide
(N
, Comp_Typ
, Etype
(Expr_Q
))
1458 Set_Expansion_Delayed
(Expr_Q
, False);
1459 Set_Analyzed
(Expr_Q
, False);
1464 Late_Expansion
(Expr_Q
, Etype
(Expr_Q
), Indexed_Comp
));
1469 if Present
(Expr
) then
1470 Initialize_Component
1472 Comp
=> Indexed_Comp
,
1473 Comp_Typ
=> Comp_Typ
,
1477 -- Ada 2005 (AI-287): In case of default initialized component, call
1478 -- the initialization subprogram associated with the component type.
1479 -- If the component type is an access type, add an explicit null
1480 -- assignment, because for the back-end there is an initialization
1481 -- present for the whole aggregate, and no default initialization
1484 -- In addition, if the component type is controlled, we must call
1485 -- its Initialize procedure explicitly, because there is no explicit
1486 -- object creation that will invoke it otherwise.
1489 if Present
(Base_Init_Proc
(Base_Type
(Ctype
)))
1490 or else Has_Task
(Base_Type
(Ctype
))
1492 Append_List_To
(Stmts
,
1493 Build_Initialization_Call
(Loc
,
1494 Id_Ref
=> Indexed_Comp
,
1496 With_Default_Init
=> True));
1498 -- If the component type has invariants, add an invariant
1499 -- check after the component is default-initialized. It will
1500 -- be analyzed and resolved before the code for initialization
1501 -- of other components.
1503 if Has_Invariants
(Ctype
) then
1504 Set_Etype
(Indexed_Comp
, Ctype
);
1505 Append_To
(Stmts
, Make_Invariant_Call
(Indexed_Comp
));
1509 if Needs_Finalization
(Ctype
) then
1512 (Obj_Ref
=> New_Copy_Tree
(Indexed_Comp
),
1515 -- Guard against a missing [Deep_]Initialize when the component
1516 -- type was not properly frozen.
1518 if Present
(Init_Call
) then
1519 Append_To
(Stmts
, Init_Call
);
1523 -- If Default_Initial_Condition applies to the component type,
1524 -- add a DIC check after the component is default-initialized,
1525 -- as well as after an Initialize procedure is called, in the
1526 -- case of components of a controlled type. It will be analyzed
1527 -- and resolved before the code for initialization of other
1530 -- Theoretically this might also be needed for cases where Expr
1531 -- is not empty, but a default init still applies, such as for
1532 -- Default_Value cases, in which case we won't get here. ???
1534 if Has_DIC
(Ctype
) and then Present
(DIC_Procedure
(Ctype
)) then
1536 Build_DIC_Call
(Loc
, New_Copy_Tree
(Indexed_Comp
), Ctype
));
1540 return Add_Loop_Actions
(Stmts
);
1547 function Gen_Loop
(L
, H
: Node_Id
; Expr
: Node_Id
) return List_Id
is
1548 Is_Iterated_Component
: constant Boolean :=
1549 Parent_Kind
(Expr
) = N_Iterated_Component_Association
;
1562 -- Index_Base'(L) .. Index_Base'(H)
1564 L_Iteration_Scheme
: Node_Id
;
1565 -- L_J in Index_Base'(L) .. Index_Base'(H)
1568 -- The statements to execute in the loop
1570 S
: constant List_Id
:= New_List
;
1571 -- List of statements
1574 -- Copy of expression tree, used for checking purposes
1577 -- If loop bounds define an empty range return the null statement
1579 if Empty_Range
(L
, H
) then
1580 Append_To
(S
, Make_Null_Statement
(Loc
));
1582 -- Ada 2005 (AI-287): Nothing else need to be done in case of
1583 -- default initialized component.
1589 -- The expression must be type-checked even though no component
1590 -- of the aggregate will have this value. This is done only for
1591 -- actual components of the array, not for subaggregates. Do
1592 -- the check on a copy, because the expression may be shared
1593 -- among several choices, some of which might be non-null.
1595 if Present
(Etype
(N
))
1596 and then Is_Array_Type
(Etype
(N
))
1597 and then No
(Next_Index
(Index
))
1599 Expander_Mode_Save_And_Set
(False);
1600 Tcopy
:= New_Copy_Tree
(Expr
);
1601 Set_Parent
(Tcopy
, N
);
1603 -- For iterated_component_association analyze and resolve
1604 -- the expression with name of the index parameter visible.
1605 -- To manipulate scopes, we use entity of the implicit loop.
1607 if Is_Iterated_Component
then
1609 Index_Parameter
: constant Entity_Id
:=
1610 Defining_Identifier
(Parent
(Expr
));
1612 Push_Scope
(Scope
(Index_Parameter
));
1613 Enter_Name
(Index_Parameter
);
1615 (Tcopy
, Component_Type
(Etype
(N
)));
1619 -- For ordinary component association, just analyze and
1620 -- resolve the expression.
1623 Analyze_And_Resolve
(Tcopy
, Component_Type
(Etype
(N
)));
1626 Expander_Mode_Restore
;
1632 -- If loop bounds are the same then generate an assignment, unless
1633 -- the parent construct is an Iterated_Component_Association.
1635 elsif Equal
(L
, H
) and then not Is_Iterated_Component
then
1636 return Gen_Assign
(New_Copy_Tree
(L
), Expr
);
1638 -- If H - L <= 2 then generate a sequence of assignments when we are
1639 -- processing the bottom most aggregate and it contains scalar
1642 elsif No
(Next_Index
(Index
))
1643 and then Scalar_Comp
1644 and then Local_Compile_Time_Known_Value
(L
)
1645 and then Local_Compile_Time_Known_Value
(H
)
1646 and then Local_Expr_Value
(H
) - Local_Expr_Value
(L
) <= 2
1647 and then not Is_Iterated_Component
1649 Append_List_To
(S
, Gen_Assign
(New_Copy_Tree
(L
), Expr
));
1650 Append_List_To
(S
, Gen_Assign
(Add
(1, To
=> L
), Expr
));
1652 if Local_Expr_Value
(H
) - Local_Expr_Value
(L
) = 2 then
1653 Append_List_To
(S
, Gen_Assign
(Add
(2, To
=> L
), Expr
));
1659 -- Otherwise construct the loop, starting with the loop index L_J
1661 if Is_Iterated_Component
then
1663 -- Create a new scope for the loop variable so that the
1664 -- following Gen_Assign (that ends up calling
1665 -- Preanalyze_And_Resolve) can correctly find it.
1667 Ent
:= New_Internal_Entity
(E_Loop
,
1668 Current_Scope
, Loc
, 'L');
1669 Set_Etype
(Ent
, Standard_Void_Type
);
1670 Set_Parent
(Ent
, Parent
(Parent
(Expr
)));
1674 Make_Defining_Identifier
(Loc
,
1675 Chars
=> (Chars
(Defining_Identifier
(Parent
(Expr
)))));
1679 -- The Etype will be set by a later Analyze call.
1680 Set_Etype
(L_J
, Any_Type
);
1682 Mutate_Ekind
(L_J
, E_Variable
);
1683 Set_Is_Not_Self_Hidden
(L_J
);
1684 Set_Scope
(L_J
, Ent
);
1686 L_J
:= Make_Temporary
(Loc
, 'J', L
);
1689 -- Construct "L .. H" in Index_Base. We use a qualified expression
1690 -- for the bound to convert to the index base, but we don't need
1691 -- to do that if we already have the base type at hand.
1693 if Etype
(L
) = Index_Base
then
1694 L_L
:= New_Copy_Tree
(L
);
1697 Make_Qualified_Expression
(Loc
,
1698 Subtype_Mark
=> Index_Base_Name
,
1699 Expression
=> New_Copy_Tree
(L
));
1702 if Etype
(H
) = Index_Base
then
1703 L_H
:= New_Copy_Tree
(H
);
1706 Make_Qualified_Expression
(Loc
,
1707 Subtype_Mark
=> Index_Base_Name
,
1708 Expression
=> New_Copy_Tree
(H
));
1716 -- Construct "for L_J in Index_Base range L .. H"
1718 L_Iteration_Scheme
:=
1719 Make_Iteration_Scheme
(Loc
,
1720 Loop_Parameter_Specification
=>
1721 Make_Loop_Parameter_Specification
(Loc
,
1722 Defining_Identifier
=> L_J
,
1723 Discrete_Subtype_Definition
=> L_Range
));
1725 -- Construct the statements to execute in the loop body
1727 L_Body
:= Gen_Assign
(New_Occurrence_Of
(L_J
, Loc
), Expr
);
1729 -- Construct the final loop
1732 Make_Implicit_Loop_Statement
1734 Identifier
=> Empty
,
1735 Iteration_Scheme
=> L_Iteration_Scheme
,
1736 Statements
=> L_Body
));
1738 if Is_Iterated_Component
then
1742 -- A small optimization: if the aggregate is initialized with a box
1743 -- and the component type has no initialization procedure, remove the
1744 -- useless empty loop.
1746 if Nkind
(First
(S
)) = N_Loop_Statement
1747 and then Is_Empty_List
(Statements
(First
(S
)))
1749 return New_List
(Make_Null_Statement
(Loc
));
1759 -- The code built is
1761 -- W_J : Index_Base := L;
1762 -- while W_J < H loop
1763 -- W_J := Index_Base'Succ (W);
1767 function Gen_While
(L
, H
: Node_Id
; Expr
: Node_Id
) return List_Id
is
1771 -- W_J : Base_Type := L;
1773 W_Iteration_Scheme
: Node_Id
;
1776 W_Index_Succ
: Node_Id
;
1777 -- Index_Base'Succ (J)
1779 W_Increment
: Node_Id
;
1780 -- W_J := Index_Base'Succ (W)
1782 W_Body
: constant List_Id
:= New_List
;
1783 -- The statements to execute in the loop
1785 S
: constant List_Id
:= New_List
;
1786 -- list of statement
1789 -- If loop bounds define an empty range or are equal return null
1791 if Empty_Range
(L
, H
) or else Equal
(L
, H
) then
1792 Append_To
(S
, Make_Null_Statement
(Loc
));
1796 -- Build the decl of W_J
1798 W_J
:= Make_Temporary
(Loc
, 'J', L
);
1800 Make_Object_Declaration
1802 Defining_Identifier
=> W_J
,
1803 Object_Definition
=> Index_Base_Name
,
1806 -- Theoretically we should do a New_Copy_Tree (L) here, but we know
1807 -- that in this particular case L is a fresh Expr generated by
1808 -- Add which we are the only ones to use.
1810 Append_To
(S
, W_Decl
);
1812 -- Construct " while W_J < H"
1814 W_Iteration_Scheme
:=
1815 Make_Iteration_Scheme
1817 Condition
=> Make_Op_Lt
1819 Left_Opnd
=> New_Occurrence_Of
(W_J
, Loc
),
1820 Right_Opnd
=> New_Copy_Tree
(H
)));
1822 -- Construct the statements to execute in the loop body
1825 Make_Attribute_Reference
1827 Prefix
=> Index_Base_Name
,
1828 Attribute_Name
=> Name_Succ
,
1829 Expressions
=> New_List
(New_Occurrence_Of
(W_J
, Loc
)));
1832 Make_OK_Assignment_Statement
1834 Name
=> New_Occurrence_Of
(W_J
, Loc
),
1835 Expression
=> W_Index_Succ
);
1837 Append_To
(W_Body
, W_Increment
);
1839 Append_List_To
(W_Body
,
1840 Gen_Assign
(New_Occurrence_Of
(W_J
, Loc
), Expr
));
1842 -- Construct the final loop
1845 Make_Implicit_Loop_Statement
1847 Identifier
=> Empty
,
1848 Iteration_Scheme
=> W_Iteration_Scheme
,
1849 Statements
=> W_Body
));
1854 --------------------
1855 -- Get_Assoc_Expr --
1856 --------------------
1858 -- Duplicate the expression in case we will be generating several loops.
1859 -- As a result the expression is no longer shared between the loops and
1860 -- is reevaluated for each such loop.
1862 function Get_Assoc_Expr
(Assoc
: Node_Id
) return Node_Id
is
1863 Typ
: constant Entity_Id
:= Base_Type
(Etype
(N
));
1866 if Box_Present
(Assoc
) then
1867 if Present
(Default_Aspect_Component_Value
(Typ
)) then
1868 return New_Copy_Tree
(Default_Aspect_Component_Value
(Typ
));
1869 elsif Needs_Simple_Initialization
(Ctype
) then
1870 return New_Copy_Tree
(Get_Simple_Init_Val
(Ctype
, N
));
1876 -- The expression will be passed to Gen_Loop, which immediately
1877 -- calls Parent_Kind on it, so we set Parent when it matters.
1880 Expr
: constant Node_Id
:= New_Copy_Tree
(Expression
(Assoc
))
1882 Copy_Parent
(To
=> Expr
, From
=> Expression
(Assoc
));
1887 ---------------------
1888 -- Index_Base_Name --
1889 ---------------------
1891 function Index_Base_Name
return Node_Id
is
1893 return New_Occurrence_Of
(Index_Base
, Sloc
(N
));
1894 end Index_Base_Name
;
1896 ------------------------------------
1897 -- Local_Compile_Time_Known_Value --
1898 ------------------------------------
1900 function Local_Compile_Time_Known_Value
(E
: Node_Id
) return Boolean is
1902 return Compile_Time_Known_Value
(E
)
1904 (Nkind
(E
) = N_Attribute_Reference
1905 and then Attribute_Name
(E
) = Name_Val
1906 and then Compile_Time_Known_Value
(First
(Expressions
(E
))));
1907 end Local_Compile_Time_Known_Value
;
1909 ----------------------
1910 -- Local_Expr_Value --
1911 ----------------------
1913 function Local_Expr_Value
(E
: Node_Id
) return Uint
is
1915 if Compile_Time_Known_Value
(E
) then
1916 return Expr_Value
(E
);
1918 return Expr_Value
(First
(Expressions
(E
)));
1920 end Local_Expr_Value
;
1924 New_Code
: constant List_Id
:= New_List
;
1926 Aggr_Bounds
: constant Range_Nodes
:=
1927 Get_Index_Bounds
(Aggregate_Bounds
(N
));
1928 Aggr_L
: Node_Id
renames Aggr_Bounds
.First
;
1929 Aggr_H
: Node_Id
renames Aggr_Bounds
.Last
;
1930 -- The aggregate bounds of this specific subaggregate. Note that if the
1931 -- code generated by Build_Array_Aggr_Code is executed then these bounds
1932 -- are OK. Otherwise a Constraint_Error would have been raised.
1934 Aggr_Low
: constant Node_Id
:= Duplicate_Subexpr_No_Checks
(Aggr_L
);
1935 Aggr_High
: constant Node_Id
:= Duplicate_Subexpr_No_Checks
(Aggr_H
);
1936 -- After Duplicate_Subexpr these are side-effect free
1942 Bounds
: Range_Nodes
;
1943 Low
: Node_Id
renames Bounds
.First
;
1944 High
: Node_Id
renames Bounds
.Last
;
1946 Nb_Choices
: Nat
:= 0;
1947 Table
: Case_Table_Type
(1 .. Number_Of_Choices
(N
));
1948 -- Used to sort all the different choice values
1951 -- Number of elements in the positional aggregate
1953 Others_Assoc
: Node_Id
:= Empty
;
1955 -- Start of processing for Build_Array_Aggr_Code
1958 -- First before we start, a special case. If we have a bit packed
1959 -- array represented as a modular type, then clear the value to
1960 -- zero first, to ensure that unused bits are properly cleared.
1963 and then Is_Bit_Packed_Array
(Typ
)
1964 and then Is_Modular_Integer_Type
(Packed_Array_Impl_Type
(Typ
))
1967 Zero
: constant Node_Id
:= Make_Integer_Literal
(Loc
, Uint_0
);
1969 Analyze_And_Resolve
(Zero
, Packed_Array_Impl_Type
(Typ
));
1970 Append_To
(New_Code
,
1971 Make_Assignment_Statement
(Loc
,
1972 Name
=> New_Copy_Tree
(Into
),
1973 Expression
=> Unchecked_Convert_To
(Typ
, Zero
)));
1977 -- If the component type contains tasks, we need to build a Master
1978 -- entity in the current scope, because it will be needed if build-
1979 -- in-place functions are called in the expanded code.
1981 if Nkind
(Parent
(N
)) = N_Object_Declaration
and then Has_Task
(Typ
) then
1982 Build_Master_Entity
(Defining_Identifier
(Parent
(N
)));
1985 -- STEP 1: Process component associations
1987 -- For those associations that may generate a loop, initialize
1988 -- Loop_Actions to collect inserted actions that may be crated.
1990 -- Skip this if no component associations
1992 if No
(Expressions
(N
)) then
1994 -- STEP 1 (a): Sort the discrete choices
1996 Assoc
:= First
(Component_Associations
(N
));
1997 while Present
(Assoc
) loop
1998 Choice
:= First
(Choice_List
(Assoc
));
1999 while Present
(Choice
) loop
2000 if Nkind
(Choice
) = N_Others_Choice
then
2001 Others_Assoc
:= Assoc
;
2005 Bounds
:= Get_Index_Bounds
(Choice
);
2008 Set_Loop_Actions
(Assoc
, New_List
);
2011 Nb_Choices
:= Nb_Choices
+ 1;
2013 Table
(Nb_Choices
) :=
2016 Choice_Node
=> Get_Assoc_Expr
(Assoc
));
2024 -- If there is more than one set of choices these must be static
2025 -- and we can therefore sort them. Remember that Nb_Choices does not
2026 -- account for an others choice.
2028 if Nb_Choices
> 1 then
2029 Sort_Case_Table
(Table
);
2032 -- STEP 1 (b): take care of the whole set of discrete choices
2034 for J
in 1 .. Nb_Choices
loop
2035 Low
:= Table
(J
).Choice_Lo
;
2036 High
:= Table
(J
).Choice_Hi
;
2037 Expr
:= Table
(J
).Choice_Node
;
2038 Append_List
(Gen_Loop
(Low
, High
, Expr
), To
=> New_Code
);
2041 -- STEP 1 (c): generate the remaining loops to cover others choice
2042 -- We don't need to generate loops over empty gaps, but if there is
2043 -- a single empty range we must analyze the expression for semantics
2045 if Present
(Others_Assoc
) then
2047 First
: Boolean := True;
2050 for J
in 0 .. Nb_Choices
loop
2054 Low
:= Add
(1, To
=> Table
(J
).Choice_Hi
);
2057 if J
= Nb_Choices
then
2060 High
:= Add
(-1, To
=> Table
(J
+ 1).Choice_Lo
);
2063 -- If this is an expansion within an init proc, make
2064 -- sure that discriminant references are replaced by
2065 -- the corresponding discriminal.
2067 if Inside_Init_Proc
then
2068 if Is_Entity_Name
(Low
)
2069 and then Ekind
(Entity
(Low
)) = E_Discriminant
2071 Set_Entity
(Low
, Discriminal
(Entity
(Low
)));
2074 if Is_Entity_Name
(High
)
2075 and then Ekind
(Entity
(High
)) = E_Discriminant
2077 Set_Entity
(High
, Discriminal
(Entity
(High
)));
2081 if First
or else not Empty_Range
(Low
, High
) then
2083 Set_Loop_Actions
(Others_Assoc
, New_List
);
2084 Expr
:= Get_Assoc_Expr
(Others_Assoc
);
2085 Append_List
(Gen_Loop
(Low
, High
, Expr
), To
=> New_Code
);
2091 -- STEP 2: Process positional components
2094 -- STEP 2 (a): Generate the assignments for each positional element
2095 -- Note that here we have to use Aggr_L rather than Aggr_Low because
2096 -- Aggr_L is analyzed and Add wants an analyzed expression.
2098 Expr
:= First
(Expressions
(N
));
2100 while Present
(Expr
) loop
2101 Nb_Elements
:= Nb_Elements
+ 1;
2102 Append_List
(Gen_Assign
(Add
(Nb_Elements
, To
=> Aggr_L
), Expr
),
2107 -- STEP 2 (b): Generate final loop if an others choice is present.
2108 -- Here Nb_Elements gives the offset of the last positional element.
2110 if Present
(Component_Associations
(N
)) then
2111 Assoc
:= Last
(Component_Associations
(N
));
2113 if Nkind
(Assoc
) = N_Iterated_Component_Association
then
2114 -- Ada 2022: generate a loop to have a proper scope for
2115 -- the identifier that typically appears in the expression.
2116 -- The lower bound of the loop is the position after all
2117 -- previous positional components.
2119 Append_List
(Gen_Loop
(Add
(Nb_Elements
+ 1, To
=> Aggr_L
),
2121 Expression
(Assoc
)),
2124 -- Ada 2005 (AI-287)
2126 Append_List
(Gen_While
(Add
(Nb_Elements
, To
=> Aggr_L
),
2128 Get_Assoc_Expr
(Assoc
)),
2135 end Build_Array_Aggr_Code
;
2137 -------------------------------------
2138 -- Build_Assignment_With_Temporary --
2139 -------------------------------------
2141 function Build_Assignment_With_Temporary
2144 Source
: Node_Id
) return List_Id
2146 Loc
: constant Source_Ptr
:= Sloc
(Source
);
2148 Aggr_Code
: List_Id
;
2152 Aggr_Code
:= New_List
;
2154 Tmp
:= Build_Temporary_On_Secondary_Stack
(Loc
, Typ
, Aggr_Code
);
2156 Append_To
(Aggr_Code
,
2157 Make_OK_Assignment_Statement
(Loc
,
2159 Make_Explicit_Dereference
(Loc
,
2160 Prefix
=> New_Occurrence_Of
(Tmp
, Loc
)),
2161 Expression
=> Source
));
2163 Append_To
(Aggr_Code
,
2164 Make_OK_Assignment_Statement
(Loc
,
2167 Make_Explicit_Dereference
(Loc
,
2168 Prefix
=> New_Occurrence_Of
(Tmp
, Loc
))));
2171 end Build_Assignment_With_Temporary
;
2173 ----------------------------
2174 -- Build_Record_Aggr_Code --
2175 ----------------------------
2177 function Build_Record_Aggr_Code
2180 Lhs
: Node_Id
) return List_Id
2182 Loc
: constant Source_Ptr
:= Sloc
(N
);
2183 L
: constant List_Id
:= New_List
;
2184 N_Typ
: constant Entity_Id
:= Etype
(N
);
2190 Comp_Type
: Entity_Id
;
2191 Selector
: Entity_Id
;
2192 Comp_Expr
: Node_Id
;
2195 Ancestor_Is_Subtype_Mark
: Boolean := False;
2197 Init_Typ
: Entity_Id
:= Empty
;
2199 Finalization_Done
: Boolean := False;
2200 -- True if Generate_Finalization_Actions has already been called; calls
2201 -- after the first do nothing.
2203 function Ancestor_Discriminant_Value
(Disc
: Entity_Id
) return Node_Id
;
2204 -- Returns the value that the given discriminant of an ancestor type
2205 -- should receive (in the absence of a conflict with the value provided
2206 -- by an ancestor part of an extension aggregate).
2208 procedure Check_Ancestor_Discriminants
(Anc_Typ
: Entity_Id
);
2209 -- Check that each of the discriminant values defined by the ancestor
2210 -- part of an extension aggregate match the corresponding values
2211 -- provided by either an association of the aggregate or by the
2212 -- constraint imposed by a parent type (RM95-4.3.2(8)).
2214 function Compatible_Int_Bounds
2215 (Agg_Bounds
: Node_Id
;
2216 Typ_Bounds
: Node_Id
) return Boolean;
2217 -- Return true if Agg_Bounds are equal or within Typ_Bounds. It is
2218 -- assumed that both bounds are integer ranges.
2220 procedure Generate_Finalization_Actions
;
2221 -- Deal with the various controlled type data structure initializations
2222 -- (but only if it hasn't been done already).
2224 function Get_Constraint_Association
(T
: Entity_Id
) return Node_Id
;
2225 -- Returns the first discriminant association in the constraint
2226 -- associated with T, if any, otherwise returns Empty.
2228 function Get_Explicit_Discriminant_Value
(D
: Entity_Id
) return Node_Id
;
2229 -- If the ancestor part is an unconstrained type and further ancestors
2230 -- do not provide discriminants for it, check aggregate components for
2231 -- values of the discriminants.
2233 procedure Init_Hidden_Discriminants
(Typ
: Entity_Id
; List
: List_Id
);
2234 -- If Typ is derived, and constrains discriminants of the parent type,
2235 -- these discriminants are not components of the aggregate, and must be
2236 -- initialized. The assignments are appended to List. The same is done
2237 -- if Typ derives from an already constrained subtype of a discriminated
2240 procedure Init_Stored_Discriminants
;
2241 -- If the type is derived and has inherited discriminants, generate
2242 -- explicit assignments for each, using the store constraint of the
2243 -- type. Note that both visible and stored discriminants must be
2244 -- initialized in case the derived type has some renamed and some
2245 -- constrained discriminants.
2247 procedure Init_Visible_Discriminants
;
2248 -- If type has discriminants, retrieve their values from aggregate,
2249 -- and generate explicit assignments for each. This does not include
2250 -- discriminants inherited from ancestor, which are handled above.
2251 -- The type of the aggregate is a subtype created ealier using the
2252 -- given values of the discriminant components of the aggregate.
2254 function Is_Int_Range_Bounds
(Bounds
: Node_Id
) return Boolean;
2255 -- Check whether Bounds is a range node and its lower and higher bounds
2256 -- are integers literals.
2258 function Replace_Type
(Expr
: Node_Id
) return Traverse_Result
;
2259 -- If the aggregate contains a self-reference, traverse each expression
2260 -- to replace a possible self-reference with a reference to the proper
2261 -- component of the target of the assignment.
2263 function Rewrite_Discriminant
(Expr
: Node_Id
) return Traverse_Result
;
2264 -- If default expression of a component mentions a discriminant of the
2265 -- type, it must be rewritten as the discriminant of the target object.
2267 ---------------------------------
2268 -- Ancestor_Discriminant_Value --
2269 ---------------------------------
2271 function Ancestor_Discriminant_Value
(Disc
: Entity_Id
) return Node_Id
is
2273 Assoc_Elmt
: Elmt_Id
;
2274 Aggr_Comp
: Entity_Id
;
2275 Corresp_Disc
: Entity_Id
;
2276 Current_Typ
: Entity_Id
:= Base_Type
(Typ
);
2277 Parent_Typ
: Entity_Id
;
2278 Parent_Disc
: Entity_Id
;
2279 Save_Assoc
: Node_Id
:= Empty
;
2282 -- First check any discriminant associations to see if any of them
2283 -- provide a value for the discriminant.
2285 if Present
(Discriminant_Specifications
(Parent
(Current_Typ
))) then
2286 Assoc
:= First
(Component_Associations
(N
));
2287 while Present
(Assoc
) loop
2288 Aggr_Comp
:= Entity
(First
(Choices
(Assoc
)));
2290 if Ekind
(Aggr_Comp
) = E_Discriminant
then
2291 Save_Assoc
:= Expression
(Assoc
);
2293 Corresp_Disc
:= Corresponding_Discriminant
(Aggr_Comp
);
2294 while Present
(Corresp_Disc
) loop
2296 -- If found a corresponding discriminant then return the
2297 -- value given in the aggregate. (Note: this is not
2298 -- correct in the presence of side effects. ???)
2300 if Disc
= Corresp_Disc
then
2301 return Duplicate_Subexpr
(Expression
(Assoc
));
2304 Corresp_Disc
:= Corresponding_Discriminant
(Corresp_Disc
);
2312 -- No match found in aggregate, so chain up parent types to find
2313 -- a constraint that defines the value of the discriminant.
2315 Parent_Typ
:= Etype
(Current_Typ
);
2316 while Current_Typ
/= Parent_Typ
loop
2317 if Has_Discriminants
(Parent_Typ
)
2318 and then not Has_Unknown_Discriminants
(Parent_Typ
)
2320 Parent_Disc
:= First_Discriminant
(Parent_Typ
);
2322 -- We either get the association from the subtype indication
2323 -- of the type definition itself, or from the discriminant
2324 -- constraint associated with the type entity (which is
2325 -- preferable, but it's not always present ???)
2327 if Is_Empty_Elmt_List
(Discriminant_Constraint
(Current_Typ
))
2329 Assoc
:= Get_Constraint_Association
(Current_Typ
);
2330 Assoc_Elmt
:= No_Elmt
;
2333 First_Elmt
(Discriminant_Constraint
(Current_Typ
));
2334 Assoc
:= Node
(Assoc_Elmt
);
2337 -- Traverse the discriminants of the parent type looking
2338 -- for one that corresponds.
2340 while Present
(Parent_Disc
) and then Present
(Assoc
) loop
2341 Corresp_Disc
:= Parent_Disc
;
2342 while Present
(Corresp_Disc
)
2343 and then Disc
/= Corresp_Disc
2345 Corresp_Disc
:= Corresponding_Discriminant
(Corresp_Disc
);
2348 if Disc
= Corresp_Disc
then
2349 if Nkind
(Assoc
) = N_Discriminant_Association
then
2350 Assoc
:= Expression
(Assoc
);
2353 -- If the located association directly denotes
2354 -- a discriminant, then use the value of a saved
2355 -- association of the aggregate. This is an approach
2356 -- used to handle certain cases involving multiple
2357 -- discriminants mapped to a single discriminant of
2358 -- a descendant. It's not clear how to locate the
2359 -- appropriate discriminant value for such cases. ???
2361 if Is_Entity_Name
(Assoc
)
2362 and then Ekind
(Entity
(Assoc
)) = E_Discriminant
2364 Assoc
:= Save_Assoc
;
2367 return Duplicate_Subexpr
(Assoc
);
2370 Next_Discriminant
(Parent_Disc
);
2372 if No
(Assoc_Elmt
) then
2376 Next_Elmt
(Assoc_Elmt
);
2378 if Present
(Assoc_Elmt
) then
2379 Assoc
:= Node
(Assoc_Elmt
);
2387 Current_Typ
:= Parent_Typ
;
2388 Parent_Typ
:= Etype
(Current_Typ
);
2391 -- In some cases there's no ancestor value to locate (such as
2392 -- when an ancestor part given by an expression defines the
2393 -- discriminant value).
2396 end Ancestor_Discriminant_Value
;
2398 ----------------------------------
2399 -- Check_Ancestor_Discriminants --
2400 ----------------------------------
2402 procedure Check_Ancestor_Discriminants
(Anc_Typ
: Entity_Id
) is
2404 Disc_Value
: Node_Id
;
2408 Discr
:= First_Discriminant
(Base_Type
(Anc_Typ
));
2409 while Present
(Discr
) loop
2410 Disc_Value
:= Ancestor_Discriminant_Value
(Discr
);
2412 if Present
(Disc_Value
) then
2413 Cond
:= Make_Op_Ne
(Loc
,
2415 Make_Selected_Component
(Loc
,
2416 Prefix
=> New_Copy_Tree
(Target
),
2417 Selector_Name
=> New_Occurrence_Of
(Discr
, Loc
)),
2418 Right_Opnd
=> Disc_Value
);
2421 Make_Raise_Constraint_Error
(Loc
,
2423 Reason
=> CE_Discriminant_Check_Failed
));
2426 Next_Discriminant
(Discr
);
2428 end Check_Ancestor_Discriminants
;
2430 ---------------------------
2431 -- Compatible_Int_Bounds --
2432 ---------------------------
2434 function Compatible_Int_Bounds
2435 (Agg_Bounds
: Node_Id
;
2436 Typ_Bounds
: Node_Id
) return Boolean
2438 Agg_Lo
: constant Uint
:= Intval
(Low_Bound
(Agg_Bounds
));
2439 Agg_Hi
: constant Uint
:= Intval
(High_Bound
(Agg_Bounds
));
2440 Typ_Lo
: constant Uint
:= Intval
(Low_Bound
(Typ_Bounds
));
2441 Typ_Hi
: constant Uint
:= Intval
(High_Bound
(Typ_Bounds
));
2443 return Typ_Lo
<= Agg_Lo
and then Agg_Hi
<= Typ_Hi
;
2444 end Compatible_Int_Bounds
;
2446 -----------------------------------
2447 -- Generate_Finalization_Actions --
2448 -----------------------------------
2450 procedure Generate_Finalization_Actions
is
2452 -- Do the work only the first time this is called
2454 if Finalization_Done
then
2458 Finalization_Done
:= True;
2460 -- Determine the external finalization list. It is either the
2461 -- finalization list of the outer scope or the one coming from an
2462 -- outer aggregate. When the target is not a temporary, the proper
2463 -- scope is the scope of the target rather than the potentially
2464 -- transient current scope.
2466 if Is_Controlled
(Typ
) and then Ancestor_Is_Subtype_Mark
then
2467 Ref
:= Convert_To
(Init_Typ
, New_Copy_Tree
(Target
));
2468 Set_Assignment_OK
(Ref
);
2471 Make_Procedure_Call_Statement
(Loc
,
2474 (Find_Prim_Op
(Init_Typ
, Name_Initialize
), Loc
),
2475 Parameter_Associations
=> New_List
(New_Copy_Tree
(Ref
))));
2477 end Generate_Finalization_Actions
;
2479 --------------------------------
2480 -- Get_Constraint_Association --
2481 --------------------------------
2483 function Get_Constraint_Association
(T
: Entity_Id
) return Node_Id
is
2490 -- If type is private, get constraint from full view. This was
2491 -- previously done in an instance context, but is needed whenever
2492 -- the ancestor part has a discriminant, possibly inherited through
2493 -- multiple derivations.
2495 if Is_Private_Type
(Typ
) and then Present
(Full_View
(Typ
)) then
2496 Typ
:= Full_View
(Typ
);
2499 Indic
:= Subtype_Indication
(Type_Definition
(Parent
(Typ
)));
2501 -- Verify that the subtype indication carries a constraint
2503 if Nkind
(Indic
) = N_Subtype_Indication
2504 and then Present
(Constraint
(Indic
))
2506 return First
(Constraints
(Constraint
(Indic
)));
2510 end Get_Constraint_Association
;
2512 -------------------------------------
2513 -- Get_Explicit_Discriminant_Value --
2514 -------------------------------------
2516 function Get_Explicit_Discriminant_Value
2517 (D
: Entity_Id
) return Node_Id
2524 -- The aggregate has been normalized and all associations have a
2527 Assoc
:= First
(Component_Associations
(N
));
2528 while Present
(Assoc
) loop
2529 Choice
:= First
(Choices
(Assoc
));
2531 if Chars
(Choice
) = Chars
(D
) then
2532 Val
:= Expression
(Assoc
);
2541 end Get_Explicit_Discriminant_Value
;
2543 -------------------------------
2544 -- Init_Hidden_Discriminants --
2545 -------------------------------
2547 procedure Init_Hidden_Discriminants
(Typ
: Entity_Id
; List
: List_Id
) is
2548 function Is_Completely_Hidden_Discriminant
2549 (Discr
: Entity_Id
) return Boolean;
2550 -- Determine whether Discr is a completely hidden discriminant of
2553 ---------------------------------------
2554 -- Is_Completely_Hidden_Discriminant --
2555 ---------------------------------------
2557 function Is_Completely_Hidden_Discriminant
2558 (Discr
: Entity_Id
) return Boolean
2563 -- Use First/Next_Entity as First/Next_Discriminant do not yield
2564 -- completely hidden discriminants.
2566 Item
:= First_Entity
(Typ
);
2567 while Present
(Item
) loop
2568 if Ekind
(Item
) = E_Discriminant
2569 and then Is_Completely_Hidden
(Item
)
2570 and then Chars
(Original_Record_Component
(Item
)) =
2580 end Is_Completely_Hidden_Discriminant
;
2584 Base_Typ
: Entity_Id
;
2586 Discr_Constr
: Elmt_Id
;
2587 Discr_Init
: Node_Id
;
2588 Discr_Val
: Node_Id
;
2589 In_Aggr_Type
: Boolean;
2590 Par_Typ
: Entity_Id
;
2592 -- Start of processing for Init_Hidden_Discriminants
2595 -- The constraints on the hidden discriminants, if present, are kept
2596 -- in the Stored_Constraint list of the type itself, or in that of
2597 -- the base type. If not in the constraints of the aggregate itself,
2598 -- we examine ancestors to find discriminants that are not renamed
2599 -- by other discriminants but constrained explicitly.
2601 In_Aggr_Type
:= True;
2603 Base_Typ
:= Base_Type
(Typ
);
2604 while Is_Derived_Type
(Base_Typ
)
2606 (Present
(Stored_Constraint
(Base_Typ
))
2608 (In_Aggr_Type
and then Present
(Stored_Constraint
(Typ
))))
2610 Par_Typ
:= Etype
(Base_Typ
);
2612 if not Has_Discriminants
(Par_Typ
) then
2616 Discr
:= First_Discriminant
(Par_Typ
);
2618 -- We know that one of the stored-constraint lists is present
2620 if Present
(Stored_Constraint
(Base_Typ
)) then
2621 Discr_Constr
:= First_Elmt
(Stored_Constraint
(Base_Typ
));
2623 -- For private extension, stored constraint may be on full view
2625 elsif Is_Private_Type
(Base_Typ
)
2626 and then Present
(Full_View
(Base_Typ
))
2627 and then Present
(Stored_Constraint
(Full_View
(Base_Typ
)))
2630 First_Elmt
(Stored_Constraint
(Full_View
(Base_Typ
)));
2632 -- Otherwise, no discriminant to process
2635 Discr_Constr
:= No_Elmt
;
2638 while Present
(Discr
) and then Present
(Discr_Constr
) loop
2639 Discr_Val
:= Node
(Discr_Constr
);
2641 -- The parent discriminant is renamed in the derived type,
2642 -- nothing to initialize.
2644 -- type Deriv_Typ (Discr : ...)
2645 -- is new Parent_Typ (Discr => Discr);
2647 if Is_Entity_Name
(Discr_Val
)
2648 and then Ekind
(Entity
(Discr_Val
)) = E_Discriminant
2652 -- When the parent discriminant is constrained at the type
2653 -- extension level, it does not appear in the derived type.
2655 -- type Deriv_Typ (Discr : ...)
2656 -- is new Parent_Typ (Discr => Discr,
2657 -- Hidden_Discr => Expression);
2659 elsif Is_Completely_Hidden_Discriminant
(Discr
) then
2662 -- Otherwise initialize the discriminant
2666 Make_OK_Assignment_Statement
(Loc
,
2668 Make_Selected_Component
(Loc
,
2669 Prefix
=> New_Copy_Tree
(Target
),
2670 Selector_Name
=> New_Occurrence_Of
(Discr
, Loc
)),
2671 Expression
=> New_Copy_Tree
(Discr_Val
));
2673 Append_To
(List
, Discr_Init
);
2676 Next_Elmt
(Discr_Constr
);
2677 Next_Discriminant
(Discr
);
2680 In_Aggr_Type
:= False;
2681 Base_Typ
:= Base_Type
(Par_Typ
);
2683 end Init_Hidden_Discriminants
;
2685 --------------------------------
2686 -- Init_Visible_Discriminants --
2687 --------------------------------
2689 procedure Init_Visible_Discriminants
is
2690 Discriminant
: Entity_Id
;
2691 Discriminant_Value
: Node_Id
;
2694 Discriminant
:= First_Discriminant
(Typ
);
2695 while Present
(Discriminant
) loop
2697 Make_Selected_Component
(Loc
,
2698 Prefix
=> New_Copy_Tree
(Target
),
2699 Selector_Name
=> New_Occurrence_Of
(Discriminant
, Loc
));
2701 Discriminant_Value
:=
2702 Get_Discriminant_Value
2703 (Discriminant
, Typ
, Discriminant_Constraint
(N_Typ
));
2706 Make_OK_Assignment_Statement
(Loc
,
2708 Expression
=> New_Copy_Tree
(Discriminant_Value
));
2710 Append_To
(L
, Instr
);
2712 Next_Discriminant
(Discriminant
);
2714 end Init_Visible_Discriminants
;
2716 -------------------------------
2717 -- Init_Stored_Discriminants --
2718 -------------------------------
2720 procedure Init_Stored_Discriminants
is
2721 Discriminant
: Entity_Id
;
2722 Discriminant_Value
: Node_Id
;
2725 Discriminant
:= First_Stored_Discriminant
(Typ
);
2726 while Present
(Discriminant
) loop
2728 Make_Selected_Component
(Loc
,
2729 Prefix
=> New_Copy_Tree
(Target
),
2730 Selector_Name
=> New_Occurrence_Of
(Discriminant
, Loc
));
2732 Discriminant_Value
:=
2733 Get_Discriminant_Value
2734 (Discriminant
, N_Typ
, Discriminant_Constraint
(N_Typ
));
2737 Make_OK_Assignment_Statement
(Loc
,
2739 Expression
=> New_Copy_Tree
(Discriminant_Value
));
2741 Append_To
(L
, Instr
);
2743 Next_Stored_Discriminant
(Discriminant
);
2745 end Init_Stored_Discriminants
;
2747 -------------------------
2748 -- Is_Int_Range_Bounds --
2749 -------------------------
2751 function Is_Int_Range_Bounds
(Bounds
: Node_Id
) return Boolean is
2753 return Nkind
(Bounds
) = N_Range
2754 and then Nkind
(Low_Bound
(Bounds
)) = N_Integer_Literal
2755 and then Nkind
(High_Bound
(Bounds
)) = N_Integer_Literal
;
2756 end Is_Int_Range_Bounds
;
2762 function Replace_Type
(Expr
: Node_Id
) return Traverse_Result
is
2764 -- Note about the Is_Ancestor test below: aggregate components for
2765 -- self-referential types include attribute references to the current
2766 -- instance, of the form: Typ'access, etc. These references are
2767 -- rewritten as references to the target of the aggregate: the
2768 -- left-hand side of an assignment, the entity in a declaration,
2769 -- or a temporary. Without this test, we would improperly extend
2770 -- this rewriting to attribute references whose prefix is not the
2771 -- type of the aggregate.
2773 if Nkind
(Expr
) = N_Attribute_Reference
2774 and then Is_Entity_Name
(Prefix
(Expr
))
2775 and then Is_Type
(Entity
(Prefix
(Expr
)))
2778 (Entity
(Prefix
(Expr
)), Etype
(N
), Use_Full_View
=> True)
2780 if Is_Entity_Name
(Lhs
) then
2781 Rewrite
(Prefix
(Expr
), New_Occurrence_Of
(Entity
(Lhs
), Loc
));
2785 Make_Attribute_Reference
(Loc
,
2786 Attribute_Name
=> Name_Unrestricted_Access
,
2787 Prefix
=> New_Copy_Tree
(Lhs
)));
2788 Set_Analyzed
(Parent
(Expr
), False);
2795 --------------------------
2796 -- Rewrite_Discriminant --
2797 --------------------------
2799 function Rewrite_Discriminant
(Expr
: Node_Id
) return Traverse_Result
is
2801 if Is_Entity_Name
(Expr
)
2802 and then Present
(Entity
(Expr
))
2803 and then Ekind
(Entity
(Expr
)) = E_In_Parameter
2804 and then Present
(Discriminal_Link
(Entity
(Expr
)))
2805 and then Scope
(Discriminal_Link
(Entity
(Expr
))) =
2806 Base_Type
(Etype
(N
))
2809 Make_Selected_Component
(Loc
,
2810 Prefix
=> New_Copy_Tree
(Lhs
),
2811 Selector_Name
=> Make_Identifier
(Loc
, Chars
(Expr
))));
2813 -- The generated code will be reanalyzed, but if the reference
2814 -- to the discriminant appears within an already analyzed
2815 -- expression (e.g. a conditional) we must set its proper entity
2816 -- now. Context is an initialization procedure.
2822 end Rewrite_Discriminant
;
2824 procedure Replace_Discriminants
is
2825 new Traverse_Proc
(Rewrite_Discriminant
);
2827 procedure Replace_Self_Reference
is
2828 new Traverse_Proc
(Replace_Type
);
2830 -- Start of processing for Build_Record_Aggr_Code
2833 if Has_Self_Reference
(N
) then
2834 Replace_Self_Reference
(N
);
2837 -- If the target of the aggregate is class-wide, we must convert it
2838 -- to the actual type of the aggregate, so that the proper components
2839 -- are visible. We know already that the types are compatible.
2841 if Present
(Etype
(Lhs
)) and then Is_Class_Wide_Type
(Etype
(Lhs
)) then
2842 Target
:= Unchecked_Convert_To
(Typ
, Lhs
);
2847 -- Deal with the ancestor part of extension aggregates or with the
2848 -- discriminants of the root type.
2850 if Nkind
(N
) = N_Extension_Aggregate
then
2852 Ancestor
: constant Node_Id
:= Ancestor_Part
(N
);
2853 Ancestor_Q
: constant Node_Id
:= Unqualify
(Ancestor
);
2858 -- If the ancestor part is a subtype mark T, we generate
2860 -- init-proc (T (tmp)); if T is constrained and
2861 -- init-proc (S (tmp)); where S applies an appropriate
2862 -- constraint if T is unconstrained
2864 if Is_Entity_Name
(Ancestor
)
2865 and then Is_Type
(Entity
(Ancestor
))
2867 Ancestor_Is_Subtype_Mark
:= True;
2869 if Is_Constrained
(Entity
(Ancestor
)) then
2870 Init_Typ
:= Entity
(Ancestor
);
2872 -- For an ancestor part given by an unconstrained type mark,
2873 -- create a subtype constrained by appropriate corresponding
2874 -- discriminant values coming from either associations of the
2875 -- aggregate or a constraint on a parent type. The subtype will
2876 -- be used to generate the correct default value for the
2879 elsif Has_Discriminants
(Entity
(Ancestor
)) then
2881 Anc_Typ
: constant Entity_Id
:= Entity
(Ancestor
);
2882 Anc_Constr
: constant List_Id
:= New_List
;
2883 Discrim
: Entity_Id
;
2884 Disc_Value
: Node_Id
;
2885 New_Indic
: Node_Id
;
2886 Subt_Decl
: Node_Id
;
2889 Discrim
:= First_Discriminant
(Anc_Typ
);
2890 while Present
(Discrim
) loop
2891 Disc_Value
:= Ancestor_Discriminant_Value
(Discrim
);
2893 -- If no usable discriminant in ancestors, check
2894 -- whether aggregate has an explicit value for it.
2896 if No
(Disc_Value
) then
2898 Get_Explicit_Discriminant_Value
(Discrim
);
2901 Append_To
(Anc_Constr
, Disc_Value
);
2902 Next_Discriminant
(Discrim
);
2906 Make_Subtype_Indication
(Loc
,
2907 Subtype_Mark
=> New_Occurrence_Of
(Anc_Typ
, Loc
),
2909 Make_Index_Or_Discriminant_Constraint
(Loc
,
2910 Constraints
=> Anc_Constr
));
2912 Init_Typ
:= Create_Itype
(Ekind
(Anc_Typ
), N
);
2915 Make_Subtype_Declaration
(Loc
,
2916 Defining_Identifier
=> Init_Typ
,
2917 Subtype_Indication
=> New_Indic
);
2919 -- Itypes must be analyzed with checks off Declaration
2920 -- must have a parent for proper handling of subsidiary
2923 Set_Parent
(Subt_Decl
, N
);
2924 Analyze
(Subt_Decl
, Suppress
=> All_Checks
);
2928 Ref
:= Convert_To
(Init_Typ
, New_Copy_Tree
(Target
));
2929 Set_Assignment_OK
(Ref
);
2931 if not Is_Interface
(Init_Typ
) then
2933 Build_Initialization_Call
(Loc
,
2936 In_Init_Proc
=> Within_Init_Proc
,
2937 With_Default_Init
=> Has_Default_Init_Comps
(N
)
2939 Has_Task
(Base_Type
(Init_Typ
))));
2941 if Is_Constrained
(Entity
(Ancestor
))
2942 and then Has_Discriminants
(Entity
(Ancestor
))
2944 Check_Ancestor_Discriminants
(Entity
(Ancestor
));
2947 -- If ancestor type has Default_Initialization_Condition,
2948 -- add a DIC check after the ancestor object is initialized
2951 if Has_DIC
(Entity
(Ancestor
))
2952 and then Present
(DIC_Procedure
(Entity
(Ancestor
)))
2956 (Loc
, New_Copy_Tree
(Ref
), Entity
(Ancestor
)));
2960 -- Handle calls to C++ constructors
2962 elsif Is_CPP_Constructor_Call
(Ancestor
) then
2963 Init_Typ
:= Etype
(Ancestor
);
2964 Ref
:= Convert_To
(Init_Typ
, New_Copy_Tree
(Target
));
2965 Set_Assignment_OK
(Ref
);
2968 Build_Initialization_Call
(Loc
,
2971 In_Init_Proc
=> Within_Init_Proc
,
2972 With_Default_Init
=> Has_Default_Init_Comps
(N
),
2973 Constructor_Ref
=> Ancestor
));
2975 -- Ada 2005 (AI-287): If the ancestor part is an aggregate of
2976 -- limited type, a recursive call expands the ancestor. Note that
2977 -- in the limited case, the ancestor part must be either a
2978 -- function call (possibly qualified) or aggregate (definitely
2981 elsif Is_Limited_Type
(Etype
(Ancestor
))
2982 and then Nkind
(Ancestor_Q
) in N_Aggregate
2983 | N_Extension_Aggregate
2986 Build_Record_Aggr_Code
2988 Typ
=> Etype
(Ancestor_Q
),
2991 -- If the ancestor part is an expression E of type T, we generate
2995 -- In Ada 2005, this includes the case of a (possibly qualified)
2996 -- limited function call. The assignment will later be turned into
2997 -- a build-in-place function call (for further details, see
2998 -- Make_Build_In_Place_Call_In_Assignment).
3001 Init_Typ
:= Etype
(Ancestor
);
3003 -- If the ancestor part is an aggregate, force its full
3004 -- expansion, which was delayed.
3006 if Nkind
(Ancestor_Q
) in N_Aggregate | N_Extension_Aggregate
3008 Set_Analyzed
(Ancestor
, False);
3009 Set_Analyzed
(Expression
(Ancestor
), False);
3012 Ref
:= Convert_To
(Init_Typ
, New_Copy_Tree
(Target
));
3014 Assign
:= New_List
(
3015 Make_OK_Assignment_Statement
(Loc
,
3017 Expression
=> Ancestor
));
3019 -- Arrange for the component to be adjusted if need be (the
3020 -- call will be generated by Make_Tag_Ctrl_Assignment).
3022 if Needs_Finalization
(Init_Typ
)
3023 and then not Is_Limited_View
(Init_Typ
)
3025 Set_No_Finalize_Actions
(First
(Assign
));
3027 Set_No_Ctrl_Actions
(First
(Assign
));
3031 Make_Suppress_Block
(Loc
, Name_Discriminant_Check
, Assign
));
3033 if Has_Discriminants
(Init_Typ
) then
3034 Check_Ancestor_Discriminants
(Init_Typ
);
3039 -- Generate assignments of hidden discriminants. If the base type is
3040 -- an unchecked union, the discriminants are unknown to the back-end
3041 -- and absent from a value of the type, so assignments for them are
3044 if Has_Discriminants
(Typ
)
3045 and then not Is_Unchecked_Union
(Base_Type
(Typ
))
3047 Init_Hidden_Discriminants
(Typ
, L
);
3050 -- Normal case (not an extension aggregate)
3053 -- Generate the discriminant expressions, component by component.
3054 -- If the base type is an unchecked union, the discriminants are
3055 -- unknown to the back-end and absent from a value of the type, so
3056 -- assignments for them are not emitted.
3058 if Has_Discriminants
(Typ
)
3059 and then not Is_Unchecked_Union
(Base_Type
(Typ
))
3061 Init_Hidden_Discriminants
(Typ
, L
);
3063 -- Generate discriminant init values for the visible discriminants
3065 Init_Visible_Discriminants
;
3067 if Is_Derived_Type
(N_Typ
) then
3068 Init_Stored_Discriminants
;
3073 -- For CPP types we generate an implicit call to the C++ default
3074 -- constructor to ensure the proper initialization of the _Tag
3077 if Is_CPP_Class
(Root_Type
(Typ
)) and then CPP_Num_Prims
(Typ
) > 0 then
3078 Invoke_Constructor
: declare
3079 CPP_Parent
: constant Entity_Id
:= Enclosing_CPP_Parent
(Typ
);
3081 procedure Invoke_IC_Proc
(T
: Entity_Id
);
3082 -- Recursive routine used to climb to parents. Required because
3083 -- parents must be initialized before descendants to ensure
3084 -- propagation of inherited C++ slots.
3086 --------------------
3087 -- Invoke_IC_Proc --
3088 --------------------
3090 procedure Invoke_IC_Proc
(T
: Entity_Id
) is
3092 -- Avoid generating extra calls. Initialization required
3093 -- only for types defined from the level of derivation of
3094 -- type of the constructor and the type of the aggregate.
3096 if T
= CPP_Parent
then
3100 Invoke_IC_Proc
(Etype
(T
));
3102 -- Generate call to the IC routine
3104 if Present
(CPP_Init_Proc
(T
)) then
3106 Make_Procedure_Call_Statement
(Loc
,
3107 Name
=> New_Occurrence_Of
(CPP_Init_Proc
(T
), Loc
)));
3111 -- Start of processing for Invoke_Constructor
3114 -- Implicit invocation of the C++ constructor
3116 if Nkind
(N
) = N_Aggregate
then
3118 Make_Procedure_Call_Statement
(Loc
,
3120 New_Occurrence_Of
(Base_Init_Proc
(CPP_Parent
), Loc
),
3121 Parameter_Associations
=> New_List
(
3122 Unchecked_Convert_To
(CPP_Parent
,
3123 New_Copy_Tree
(Lhs
)))));
3126 Invoke_IC_Proc
(Typ
);
3127 end Invoke_Constructor
;
3130 -- Generate the assignments, component by component
3132 -- tmp.comp1 := Expr1_From_Aggr;
3133 -- tmp.comp2 := Expr2_From_Aggr;
3136 Comp
:= First
(Component_Associations
(N
));
3137 while Present
(Comp
) loop
3138 Selector
:= Entity
(First
(Choices
(Comp
)));
3139 pragma Assert
(Present
(Selector
));
3143 if Is_CPP_Constructor_Call
(Expression
(Comp
)) then
3145 Build_Initialization_Call
(Loc
,
3147 Make_Selected_Component
(Loc
,
3148 Prefix
=> New_Copy_Tree
(Target
),
3149 Selector_Name
=> New_Occurrence_Of
(Selector
, Loc
)),
3150 Typ
=> Etype
(Selector
),
3152 With_Default_Init
=> True,
3153 Constructor_Ref
=> Expression
(Comp
)));
3155 elsif Box_Present
(Comp
)
3156 and then Needs_Simple_Initialization
(Etype
(Selector
))
3159 Make_Selected_Component
(Loc
,
3160 Prefix
=> New_Copy_Tree
(Target
),
3161 Selector_Name
=> New_Occurrence_Of
(Selector
, Loc
));
3163 Initialize_Component
3166 Comp_Typ
=> Etype
(Selector
),
3167 Init_Expr
=> Get_Simple_Init_Val
3168 (Typ
=> Etype
(Selector
),
3171 (if Known_Esize
(Selector
)
3172 then Esize
(Selector
)
3176 -- Ada 2005 (AI-287): For each default-initialized component generate
3177 -- a call to the corresponding IP subprogram if available.
3179 elsif Box_Present
(Comp
)
3180 and then Has_Non_Null_Base_Init_Proc
(Etype
(Selector
))
3182 if Ekind
(Selector
) /= E_Discriminant
then
3183 Generate_Finalization_Actions
;
3186 -- Ada 2005 (AI-287): If the component type has tasks then
3187 -- generate the activation chain and master entities (except
3188 -- in case of an allocator because in that case these entities
3189 -- are generated by Build_Task_Allocate_Block_With_Init_Stmts).
3192 Ctype
: constant Entity_Id
:= Etype
(Selector
);
3193 Inside_Allocator
: Boolean := False;
3194 P
: Node_Id
:= Parent
(N
);
3197 if Is_Task_Type
(Ctype
) or else Has_Task
(Ctype
) then
3198 while Present
(P
) loop
3199 if Nkind
(P
) = N_Allocator
then
3200 Inside_Allocator
:= True;
3207 if not Inside_Init_Proc
and not Inside_Allocator
then
3208 Build_Activation_Chain_Entity
(N
);
3214 Build_Initialization_Call
(Loc
,
3215 Id_Ref
=> Make_Selected_Component
(Loc
,
3216 Prefix
=> New_Copy_Tree
(Target
),
3218 New_Occurrence_Of
(Selector
, Loc
)),
3219 Typ
=> Etype
(Selector
),
3221 With_Default_Init
=> True));
3223 -- Prepare for component assignment
3225 elsif Ekind
(Selector
) /= E_Discriminant
3226 or else Nkind
(N
) = N_Extension_Aggregate
3228 -- All the discriminants have now been assigned
3230 -- This is now a good moment to initialize and attach all the
3231 -- controllers. Their position may depend on the discriminants.
3233 if Ekind
(Selector
) /= E_Discriminant
then
3234 Generate_Finalization_Actions
;
3237 Comp_Type
:= Underlying_Type
(Etype
(Selector
));
3239 Make_Selected_Component
(Loc
,
3240 Prefix
=> New_Copy_Tree
(Target
),
3241 Selector_Name
=> New_Occurrence_Of
(Selector
, Loc
));
3243 Expr_Q
:= Unqualify
(Expression
(Comp
));
3245 -- Now either create the assignment or generate the code for the
3246 -- inner aggregate top-down.
3248 if Is_Delayed_Aggregate
(Expr_Q
) then
3250 -- We have the following case of aggregate nesting inside
3251 -- an object declaration:
3253 -- type Arr_Typ is array (Integer range <>) of ...;
3255 -- type Rec_Typ (...) is record
3256 -- Obj_Arr_Typ : Arr_Typ (A .. B);
3259 -- Obj_Rec_Typ : Rec_Typ := (...,
3260 -- Obj_Arr_Typ => (X => (...), Y => (...)));
3262 -- The length of the ranges of the aggregate and Obj_Add_Typ
3263 -- are equal (B - A = Y - X), but they do not coincide (X /=
3264 -- A and B /= Y). This case requires array sliding which is
3265 -- performed in the following manner:
3267 -- subtype Arr_Sub is Arr_Typ (X .. Y);
3269 -- Temp (X) := (...);
3271 -- Temp (Y) := (...);
3272 -- Obj_Rec_Typ.Obj_Arr_Typ := Temp;
3274 if Ekind
(Comp_Type
) = E_Array_Subtype
3275 and then Is_Int_Range_Bounds
(Aggregate_Bounds
(Expr_Q
))
3276 and then Is_Int_Range_Bounds
(First_Index
(Comp_Type
))
3278 Compatible_Int_Bounds
3279 (Agg_Bounds
=> Aggregate_Bounds
(Expr_Q
),
3280 Typ_Bounds
=> First_Index
(Comp_Type
))
3282 -- Create the array subtype with bounds equal to those of
3283 -- the corresponding aggregate.
3286 SubE
: constant Entity_Id
:= Make_Temporary
(Loc
, 'T');
3288 SubD
: constant Node_Id
:=
3289 Make_Subtype_Declaration
(Loc
,
3290 Defining_Identifier
=> SubE
,
3291 Subtype_Indication
=>
3292 Make_Subtype_Indication
(Loc
,
3294 New_Occurrence_Of
(Etype
(Comp_Type
), Loc
),
3296 Make_Index_Or_Discriminant_Constraint
3298 Constraints
=> New_List
(
3300 (Aggregate_Bounds
(Expr_Q
))))));
3302 -- Create a temporary array of the above subtype which
3303 -- will be used to capture the aggregate assignments.
3305 TmpE
: constant Entity_Id
:= Make_Temporary
(Loc
, 'A', N
);
3307 TmpD
: constant Node_Id
:=
3308 Make_Object_Declaration
(Loc
,
3309 Defining_Identifier
=> TmpE
,
3310 Object_Definition
=> New_Occurrence_Of
(SubE
, Loc
));
3313 Set_No_Initialization
(TmpD
);
3314 Append_To
(L
, SubD
);
3315 Append_To
(L
, TmpD
);
3317 -- Expand aggregate into assignments to the temp array
3320 Late_Expansion
(Expr_Q
, Comp_Type
,
3321 New_Occurrence_Of
(TmpE
, Loc
)));
3326 Make_Assignment_Statement
(Loc
,
3327 Name
=> New_Copy_Tree
(Comp_Expr
),
3328 Expression
=> New_Occurrence_Of
(TmpE
, Loc
)));
3331 -- Normal case (sliding not required)
3335 Late_Expansion
(Expr_Q
, Comp_Type
, Comp_Expr
));
3338 -- Expr_Q is not delayed aggregate
3341 if Has_Discriminants
(Typ
) then
3342 Replace_Discriminants
(Expr_Q
);
3344 -- If the component is an array type that depends on
3345 -- discriminants, and the expression is a single Others
3346 -- clause, create an explicit subtype for it because the
3347 -- backend has troubles recovering the actual bounds.
3349 if Nkind
(Expr_Q
) = N_Aggregate
3350 and then Is_Array_Type
(Comp_Type
)
3351 and then Present
(Component_Associations
(Expr_Q
))
3354 Assoc
: constant Node_Id
:=
3355 First
(Component_Associations
(Expr_Q
));
3361 Nkind
(First
(Choices
(Assoc
))) = N_Others_Choice
3364 Build_Actual_Subtype_Of_Component
3365 (Comp_Type
, Comp_Expr
);
3367 -- If the component type does not in fact depend on
3368 -- discriminants, the subtype declaration is empty.
3370 if Present
(Decl
) then
3371 Append_To
(L
, Decl
);
3372 Set_Etype
(Comp_Expr
, Defining_Entity
(Decl
));
3379 if Modify_Tree_For_C
3380 and then Nkind
(Expr_Q
) = N_Aggregate
3381 and then Is_Array_Type
(Etype
(Expr_Q
))
3382 and then Present
(First_Index
(Etype
(Expr_Q
)))
3385 Expr_Q_Type
: constant Entity_Id
:= Etype
(Expr_Q
);
3388 Build_Array_Aggr_Code
3390 Ctype
=> Component_Type
(Expr_Q_Type
),
3391 Index
=> First_Index
(Expr_Q_Type
),
3394 Is_Scalar_Type
(Component_Type
(Expr_Q_Type
))));
3398 Initialize_Component
3401 Comp_Typ
=> Etype
(Selector
),
3402 Init_Expr
=> Expr_Q
,
3407 -- comment would be good here ???
3409 elsif Ekind
(Selector
) = E_Discriminant
3410 and then Nkind
(N
) /= N_Extension_Aggregate
3411 and then Nkind
(Parent
(N
)) = N_Component_Association
3412 and then Is_Constrained
(Typ
)
3414 -- We must check that the discriminant value imposed by the
3415 -- context is the same as the value given in the subaggregate,
3416 -- because after the expansion into assignments there is no
3417 -- record on which to perform a regular discriminant check.
3424 D_Val
:= First_Elmt
(Discriminant_Constraint
(Typ
));
3425 Disc
:= First_Discriminant
(Typ
);
3426 while Chars
(Disc
) /= Chars
(Selector
) loop
3427 Next_Discriminant
(Disc
);
3431 pragma Assert
(Present
(D_Val
));
3433 -- This check cannot performed for components that are
3434 -- constrained by a current instance, because this is not a
3435 -- value that can be compared with the actual constraint.
3437 if Nkind
(Node
(D_Val
)) /= N_Attribute_Reference
3438 or else not Is_Entity_Name
(Prefix
(Node
(D_Val
)))
3439 or else not Is_Type
(Entity
(Prefix
(Node
(D_Val
))))
3442 Make_Raise_Constraint_Error
(Loc
,
3445 Left_Opnd
=> New_Copy_Tree
(Node
(D_Val
)),
3446 Right_Opnd
=> Expression
(Comp
)),
3447 Reason
=> CE_Discriminant_Check_Failed
));
3450 -- Find self-reference in previous discriminant assignment,
3451 -- and replace with proper expression.
3458 while Present
(Ass
) loop
3459 if Nkind
(Ass
) = N_Assignment_Statement
3460 and then Nkind
(Name
(Ass
)) = N_Selected_Component
3461 and then Chars
(Selector_Name
(Name
(Ass
))) =
3465 (Ass
, New_Copy_Tree
(Expression
(Comp
)));
3475 -- If the component association was specified with a box and the
3476 -- component type has a Default_Initial_Condition, then generate
3477 -- a call to the DIC procedure.
3479 if Has_DIC
(Etype
(Selector
))
3480 and then Was_Default_Init_Box_Association
(Comp
)
3481 and then Present
(DIC_Procedure
(Etype
(Selector
)))
3484 Build_DIC_Call
(Loc
,
3485 Make_Selected_Component
(Loc
,
3486 Prefix
=> New_Copy_Tree
(Target
),
3487 Selector_Name
=> New_Occurrence_Of
(Selector
, Loc
)),
3494 -- For CPP types we generated a call to the C++ default constructor
3495 -- before the components have been initialized to ensure the proper
3496 -- initialization of the _Tag component (see above).
3498 if Is_CPP_Class
(Typ
) then
3501 -- If the type is tagged, the tag needs to be initialized (unless we
3502 -- are in VM-mode where tags are implicit). It is done late in the
3503 -- initialization process because in some cases, we call the init
3504 -- proc of an ancestor which will not leave out the right tag.
3506 elsif Is_Tagged_Type
(Typ
) and then Tagged_Type_Expansion
then
3508 Make_Tag_Assignment_From_Type
3509 (Loc
, New_Copy_Tree
(Target
), Base_Type
(Typ
));
3511 Append_To
(L
, Instr
);
3513 -- Ada 2005 (AI-251): If the tagged type has been derived from an
3514 -- abstract interfaces we must also initialize the tags of the
3515 -- secondary dispatch tables.
3517 if Has_Interfaces
(Base_Type
(Typ
)) then
3519 (Typ
=> Base_Type
(Typ
),
3522 Init_Tags_List
=> L
);
3526 -- If the controllers have not been initialized yet (by lack of non-
3527 -- discriminant components), let's do it now.
3529 Generate_Finalization_Actions
;
3532 end Build_Record_Aggr_Code
;
3534 -------------------------------
3535 -- Convert_Aggr_In_Allocator --
3536 -------------------------------
3538 procedure Convert_Aggr_In_Allocator
3543 Loc
: constant Source_Ptr
:= Sloc
(Aggr
);
3544 Typ
: constant Entity_Id
:= Etype
(Aggr
);
3545 Temp
: constant Entity_Id
:= Defining_Identifier
(Decl
);
3547 Occ
: constant Node_Id
:=
3548 Unchecked_Convert_To
(Typ
,
3549 Make_Explicit_Dereference
(Loc
, New_Occurrence_Of
(Temp
, Loc
)));
3552 if Is_Array_Type
(Typ
) then
3553 Convert_Array_Aggr_In_Allocator
(Decl
, Aggr
, Occ
);
3555 elsif Has_Default_Init_Comps
(Aggr
) then
3557 L
: constant List_Id
:= New_List
;
3558 Init_Stmts
: List_Id
;
3561 Init_Stmts
:= Late_Expansion
(Aggr
, Typ
, Occ
);
3563 if Has_Task
(Typ
) then
3564 Build_Task_Allocate_Block_With_Init_Stmts
(L
, Aggr
, Init_Stmts
);
3565 Insert_Actions
(Alloc
, L
);
3567 Insert_Actions
(Alloc
, Init_Stmts
);
3572 Insert_Actions
(Alloc
, Late_Expansion
(Aggr
, Typ
, Occ
));
3574 end Convert_Aggr_In_Allocator
;
3576 --------------------------------
3577 -- Convert_Aggr_In_Assignment --
3578 --------------------------------
3580 procedure Convert_Aggr_In_Assignment
(N
: Node_Id
) is
3581 Aggr
: constant Node_Id
:= Unqualify
(Expression
(N
));
3582 Typ
: constant Entity_Id
:= Etype
(Aggr
);
3583 Occ
: constant Node_Id
:= New_Copy_Tree
(Name
(N
));
3586 Insert_Actions_After
(N
, Late_Expansion
(Aggr
, Typ
, Occ
));
3587 end Convert_Aggr_In_Assignment
;
3589 ---------------------------------
3590 -- Convert_Aggr_In_Object_Decl --
3591 ---------------------------------
3593 procedure Convert_Aggr_In_Object_Decl
(N
: Node_Id
) is
3594 Obj
: constant Entity_Id
:= Defining_Identifier
(N
);
3595 Aggr
: constant Node_Id
:= Unqualify
(Expression
(N
));
3596 Loc
: constant Source_Ptr
:= Sloc
(Aggr
);
3597 Typ
: constant Entity_Id
:= Etype
(Aggr
);
3598 Occ
: constant Node_Id
:= New_Occurrence_Of
(Obj
, Loc
);
3600 Has_Transient_Scope
: Boolean := False;
3602 function Discriminants_Ok
return Boolean;
3603 -- If the object type is constrained, the discriminants in the
3604 -- aggregate must be checked against the discriminants of the subtype.
3605 -- This cannot be done using Apply_Discriminant_Checks because after
3606 -- expansion there is no aggregate left to check.
3608 ----------------------
3609 -- Discriminants_Ok --
3610 ----------------------
3612 function Discriminants_Ok
return Boolean is
3613 Cond
: Node_Id
:= Empty
;
3622 D
:= First_Discriminant
(Typ
);
3623 Disc1
:= First_Elmt
(Discriminant_Constraint
(Typ
));
3624 Disc2
:= First_Elmt
(Discriminant_Constraint
(Etype
(Obj
)));
3625 while Present
(Disc1
) and then Present
(Disc2
) loop
3626 Val1
:= Node
(Disc1
);
3627 Val2
:= Node
(Disc2
);
3629 if not Is_OK_Static_Expression
(Val1
)
3630 or else not Is_OK_Static_Expression
(Val2
)
3632 Check
:= Make_Op_Ne
(Loc
,
3633 Left_Opnd
=> Duplicate_Subexpr
(Val1
),
3634 Right_Opnd
=> Duplicate_Subexpr
(Val2
));
3640 Cond
:= Make_Or_Else
(Loc
,
3642 Right_Opnd
=> Check
);
3645 elsif Expr_Value
(Val1
) /= Expr_Value
(Val2
) then
3646 Apply_Compile_Time_Constraint_Error
(Aggr
,
3647 Msg
=> "incorrect value for discriminant&??",
3648 Reason
=> CE_Discriminant_Check_Failed
,
3653 Next_Discriminant
(D
);
3658 -- If any discriminant constraint is nonstatic, emit a check
3660 if Present
(Cond
) then
3662 Make_Raise_Constraint_Error
(Loc
,
3664 Reason
=> CE_Discriminant_Check_Failed
));
3668 end Discriminants_Ok
;
3670 -- Start of processing for Convert_Aggr_In_Object_Decl
3673 Set_Assignment_OK
(Occ
);
3675 if Has_Discriminants
(Typ
)
3676 and then Typ
/= Etype
(Obj
)
3677 and then Is_Constrained
(Etype
(Obj
))
3678 and then not Discriminants_Ok
3683 -- If the context is an extended return statement, it has its own
3684 -- finalization machinery (i.e. works like a transient scope) and
3685 -- we do not want to create an additional one, because objects on
3686 -- the finalization list of the return must be moved to the caller's
3687 -- finalization list to complete the return.
3689 -- Similarly if the aggregate is limited, it is built in place, and the
3690 -- controlled components are not assigned to intermediate temporaries
3691 -- so there is no need for a transient scope in this case either.
3693 if Requires_Transient_Scope
(Typ
)
3694 and then Ekind
(Current_Scope
) /= E_Return_Statement
3695 and then not Is_Limited_Type
(Typ
)
3697 Establish_Transient_Scope
(Aggr
, Manage_Sec_Stack
=> False);
3698 Has_Transient_Scope
:= True;
3702 Stmts
: constant List_Id
:= Late_Expansion
(Aggr
, Typ
, Occ
);
3707 -- If Obj is already frozen or if N is wrapped in a transient scope,
3708 -- Stmts do not need to be saved in Initialization_Statements since
3709 -- there is no freezing issue.
3711 if Is_Frozen
(Obj
) or else Has_Transient_Scope
then
3712 Insert_Actions_After
(N
, Stmts
);
3714 Stmt
:= Make_Compound_Statement
(Sloc
(N
), Actions
=> Stmts
);
3715 Insert_Action_After
(N
, Stmt
);
3717 -- Insert_Action_After may freeze Obj in which case we should
3718 -- remove the compound statement just created and simply insert
3721 if Is_Frozen
(Obj
) then
3723 Insert_Actions_After
(N
, Stmts
);
3725 Set_Initialization_Statements
(Obj
, Stmt
);
3729 -- If Typ has controlled components and a call to a Slice_Assign
3730 -- procedure is part of the initialization statements, then we
3731 -- need to initialize the array component since Slice_Assign will
3732 -- need to adjust it.
3734 if Has_Controlled_Component
(Typ
) then
3735 Stmt
:= First
(Stmts
);
3737 while Present
(Stmt
) loop
3738 if Nkind
(Stmt
) = N_Procedure_Call_Statement
3739 and then Is_TSS
(Entity
(Name
(Stmt
)), TSS_Slice_Assign
)
3741 Param
:= First
(Parameter_Associations
(Stmt
));
3744 Build_Initialization_Call
3745 (Sloc
(N
), New_Copy_Tree
(Param
), Etype
(Param
)));
3753 Set_No_Initialization
(N
);
3755 -- After expansion the expression can be removed from the declaration
3756 -- except if the object is class-wide, in which case the aggregate
3757 -- provides the actual type.
3759 if not Is_Class_Wide_Type
(Etype
(Obj
)) then
3760 Set_Expression
(N
, Empty
);
3763 Initialize_Discriminants
(N
, Typ
);
3764 end Convert_Aggr_In_Object_Decl
;
3766 -------------------------------------
3767 -- Convert_Array_Aggr_In_Allocator --
3768 -------------------------------------
3770 procedure Convert_Array_Aggr_In_Allocator
3775 Typ
: constant Entity_Id
:= Etype
(Aggr
);
3776 Ctyp
: constant Entity_Id
:= Component_Type
(Typ
);
3777 Aggr_Code
: List_Id
;
3781 -- The target is an explicit dereference of the allocated object
3783 -- If the assignment can be done directly by the back end, then
3784 -- reset Set_Expansion_Delayed and do not expand further.
3786 if not CodePeer_Mode
3787 and then not Modify_Tree_For_C
3788 and then Aggr_Assignment_OK_For_Backend
(Aggr
)
3790 New_Aggr
:= New_Copy_Tree
(Aggr
);
3791 Set_Expansion_Delayed
(New_Aggr
, False);
3793 -- In the case of Target's type using the Designated_Storage_Model
3794 -- aspect with a Copy_To procedure, insert a temporary and have the
3795 -- back end handle the assignment to it. Copy the result to the
3798 if Has_Designated_Storage_Model_Aspect
3799 (Etype
(Prefix
(Expression
(Target
))))
3800 and then Present
(Storage_Model_Copy_To
3801 (Storage_Model_Object
3802 (Etype
(Prefix
(Expression
(Target
))))))
3805 Build_Assignment_With_Temporary
(Target
, Typ
, New_Aggr
);
3810 Make_OK_Assignment_Statement
(Sloc
(New_Aggr
),
3812 Expression
=> New_Aggr
));
3815 -- Or else, generate component assignments to it, as for an aggregate
3816 -- that appears on the right-hand side of an assignment statement.
3819 Build_Array_Aggr_Code
(Aggr
,
3821 Index
=> First_Index
(Typ
),
3823 Scalar_Comp
=> Is_Scalar_Type
(Ctyp
));
3826 Insert_Actions_After
(Decl
, Aggr_Code
);
3827 end Convert_Array_Aggr_In_Allocator
;
3829 ------------------------
3830 -- In_Place_Assign_OK --
3831 ------------------------
3833 function In_Place_Assign_OK
3835 Target_Object
: Entity_Id
:= Empty
) return Boolean
3837 Is_Array
: constant Boolean := Is_Array_Type
(Etype
(N
));
3840 Aggr_Bounds
: Range_Nodes
;
3842 Obj_Bounds
: Range_Nodes
;
3843 Parent_Kind
: Node_Kind
;
3844 Parent_Node
: Node_Id
;
3846 function Safe_Aggregate
(Aggr
: Node_Id
) return Boolean;
3847 -- Check recursively that each component of a (sub)aggregate does not
3848 -- depend on the variable being assigned to.
3850 function Safe_Component
(Expr
: Node_Id
) return Boolean;
3851 -- Verify that an expression cannot depend on the target being assigned
3852 -- to. Return true for compile-time known values, stand-alone objects,
3853 -- parameters passed by copy, calls to functions that return by copy,
3854 -- selected components thereof only if the aggregate's type is an array,
3855 -- indexed components and slices thereof only if the aggregate's type is
3856 -- a record, and simple expressions involving only these as operands.
3857 -- This is OK whatever the target because, for a component to overlap
3858 -- with the target, it must be either a direct reference to a component
3859 -- of the target, in which case there must be a matching selection or
3860 -- indexation or slicing, or an indirect reference to such a component,
3861 -- which is excluded by the above condition. Additionally, if the target
3862 -- is statically known, return true for arbitrarily nested selections,
3863 -- indexations or slicings, provided that their ultimate prefix is not
3864 -- the target itself.
3866 --------------------
3867 -- Safe_Aggregate --
3868 --------------------
3870 function Safe_Aggregate
(Aggr
: Node_Id
) return Boolean is
3874 if Nkind
(Parent
(Aggr
)) = N_Iterated_Component_Association
then
3878 if Present
(Expressions
(Aggr
)) then
3879 Expr
:= First
(Expressions
(Aggr
));
3880 while Present
(Expr
) loop
3881 if Nkind
(Expr
) = N_Aggregate
then
3882 if not Safe_Aggregate
(Expr
) then
3886 elsif not Safe_Component
(Expr
) then
3894 if Present
(Component_Associations
(Aggr
)) then
3895 Expr
:= First
(Component_Associations
(Aggr
));
3896 while Present
(Expr
) loop
3897 if Nkind
(Expression
(Expr
)) = N_Aggregate
then
3898 if not Safe_Aggregate
(Expression
(Expr
)) then
3902 -- If association has a box, no way to determine yet whether
3903 -- default can be assigned in place.
3905 elsif Box_Present
(Expr
) then
3908 elsif not Safe_Component
(Expression
(Expr
)) then
3919 --------------------
3920 -- Safe_Component --
3921 --------------------
3923 function Safe_Component
(Expr
: Node_Id
) return Boolean is
3924 Comp
: Node_Id
:= Expr
;
3926 function Check_Component
(C
: Node_Id
; T_OK
: Boolean) return Boolean;
3927 -- Do the recursive traversal, after copy. If T_OK is True, return
3928 -- True for a stand-alone object only if the target is statically
3929 -- known and distinct from the object. At the top level, we start
3930 -- with T_OK set to False and set it to True at a deeper level only
3931 -- if we cannot disambiguate the component here without statically
3932 -- knowing the target. Note that this is not optimal, we should do
3933 -- something along the lines of Denotes_Same_Prefix for that.
3935 ---------------------
3936 -- Check_Component --
3937 ---------------------
3939 function Check_Component
(C
: Node_Id
; T_OK
: Boolean) return Boolean
3942 function SDO
(E
: Entity_Id
) return Uint
;
3943 -- Return the Scope Depth Of the enclosing dynamic scope of E
3949 function SDO
(E
: Entity_Id
) return Uint
is
3951 return Scope_Depth
(Enclosing_Dynamic_Scope
(E
));
3954 -- Start of processing for Check_Component
3957 if Is_Overloaded
(C
) then
3960 elsif Compile_Time_Known_Value
(C
) then
3965 when N_Attribute_Reference
=>
3966 return Check_Component
(Prefix
(C
), T_OK
);
3968 when N_Function_Call
=>
3969 if Nkind
(Name
(C
)) = N_Explicit_Dereference
then
3970 return not Returns_By_Ref
(Etype
(Name
(C
)));
3972 return not Returns_By_Ref
(Entity
(Name
(C
)));
3975 when N_Indexed_Component | N_Slice
=>
3976 -- In a target record, these operations cannot determine
3977 -- alone a component so we can recurse whatever the target.
3978 return Check_Component
(Prefix
(C
), T_OK
or else Is_Array
);
3980 when N_Selected_Component
=>
3981 -- In a target array, this operation cannot determine alone
3982 -- a component so we can recurse whatever the target.
3984 Check_Component
(Prefix
(C
), T_OK
or else not Is_Array
);
3986 when N_Type_Conversion | N_Unchecked_Type_Conversion
=>
3987 return Check_Component
(Expression
(C
), T_OK
);
3990 return Check_Component
(Left_Opnd
(C
), T_OK
)
3991 and then Check_Component
(Right_Opnd
(C
), T_OK
);
3994 return Check_Component
(Right_Opnd
(C
), T_OK
);
3997 if Is_Entity_Name
(C
) and then Is_Object
(Entity
(C
)) then
3998 -- Case of a formal parameter component. It's either
3999 -- trivial if passed by copy or very annoying if not,
4000 -- because in the latter case it's almost equivalent
4001 -- to a dereference, so the path-based disambiguation
4002 -- logic is totally off and we always need the target.
4004 if Is_Formal
(Entity
(C
)) then
4006 -- If it is passed by copy, then this is safe
4008 if Mechanism
(Entity
(C
)) = By_Copy
then
4011 -- Otherwise, this is safe if the target is present
4012 -- and is at least as deeply nested as the component.
4015 return Present
(Target_Object
)
4016 and then not Is_Formal
(Target_Object
)
4017 and then SDO
(Target_Object
) >= SDO
(Entity
(C
));
4020 -- For a renamed object, recurse
4022 elsif Present
(Renamed_Object
(Entity
(C
))) then
4024 Check_Component
(Renamed_Object
(Entity
(C
)), T_OK
);
4026 -- If this is safe whatever the target, we are done
4031 -- If there is no target or the component is the target,
4032 -- this is not safe.
4034 elsif No
(Target_Object
)
4035 or else Entity
(C
) = Target_Object
4039 -- Case of a formal parameter target. This is safe if it
4040 -- is at most as deeply nested as the component.
4042 elsif Is_Formal
(Target_Object
) then
4043 return SDO
(Target_Object
) <= SDO
(Entity
(C
));
4045 -- For distinct stand-alone objects, this is safe
4051 -- For anything else than an object, this is not safe
4057 end Check_Component
;
4059 -- Start of processing for Safe_Component
4062 -- If the component appears in an association that may correspond
4063 -- to more than one element, it is not analyzed before expansion
4064 -- into assignments, to avoid side effects. We analyze, but do not
4065 -- resolve the copy, to obtain sufficient entity information for
4066 -- the checks that follow. If component is overloaded we assume
4067 -- an unsafe function call.
4069 if not Analyzed
(Comp
) then
4070 if Is_Overloaded
(Expr
) then
4073 elsif Nkind
(Expr
) = N_Allocator
then
4075 -- For now, too complex to analyze
4079 elsif Nkind
(Parent
(Expr
)) = N_Iterated_Component_Association
then
4081 -- Ditto for iterated component associations, which in general
4082 -- require an enclosing loop and involve nonstatic expressions.
4087 Comp
:= New_Copy_Tree
(Expr
);
4088 Set_Parent
(Comp
, Parent
(Expr
));
4092 if Nkind
(Comp
) = N_Aggregate
then
4093 return Safe_Aggregate
(Comp
);
4095 return Check_Component
(Comp
, False);
4099 -- Start of processing for In_Place_Assign_OK
4102 -- By-copy semantic cannot be guaranteed for controlled objects
4104 if Needs_Finalization
(Etype
(N
)) then
4108 Parent_Node
:= Parent
(N
);
4109 Parent_Kind
:= Nkind
(Parent_Node
);
4111 if Parent_Kind
= N_Qualified_Expression
then
4112 Parent_Node
:= Parent
(Parent_Node
);
4113 Parent_Kind
:= Nkind
(Parent_Node
);
4116 -- On assignment, sliding can take place, so we cannot do the
4117 -- assignment in place unless the bounds of the aggregate are
4118 -- statically equal to those of the target.
4120 -- If the aggregate is given by an others choice, the bounds are
4121 -- derived from the left-hand side, and the assignment is safe if
4122 -- the expression is.
4125 and then Present
(Component_Associations
(N
))
4126 and then not Is_Others_Aggregate
(N
)
4128 Aggr_In
:= First_Index
(Etype
(N
));
4130 -- Context is an assignment
4132 if Parent_Kind
= N_Assignment_Statement
then
4133 Obj_In
:= First_Index
(Etype
(Name
(Parent_Node
)));
4135 -- Context is an allocator. Check the bounds of the aggregate against
4136 -- those of the designated type, except in the case where the type is
4137 -- unconstrained (and then we can directly return true, see below).
4139 else pragma Assert
(Parent_Kind
= N_Allocator
);
4141 Desig_Typ
: constant Entity_Id
:=
4142 Designated_Type
(Etype
(Parent_Node
));
4144 if not Is_Constrained
(Desig_Typ
) then
4148 Obj_In
:= First_Index
(Desig_Typ
);
4152 while Present
(Aggr_In
) loop
4153 Aggr_Bounds
:= Get_Index_Bounds
(Aggr_In
);
4154 Obj_Bounds
:= Get_Index_Bounds
(Obj_In
);
4156 -- We require static bounds for the target and a static matching
4157 -- of low bound for the aggregate.
4159 if not Compile_Time_Known_Value
(Obj_Bounds
.First
)
4160 or else not Compile_Time_Known_Value
(Obj_Bounds
.Last
)
4161 or else not Compile_Time_Known_Value
(Aggr_Bounds
.First
)
4162 or else Expr_Value
(Aggr_Bounds
.First
) /=
4163 Expr_Value
(Obj_Bounds
.First
)
4167 -- For an assignment statement we require static matching of
4168 -- bounds. Ditto for an allocator whose qualified expression
4169 -- is a constrained type. If the expression in the allocator
4170 -- is an unconstrained array, we accept an upper bound that
4171 -- is not static, to allow for nonstatic expressions of the
4172 -- base type. Clearly there are further possibilities (with
4173 -- diminishing returns) for safely building arrays in place
4176 elsif Parent_Kind
= N_Assignment_Statement
4177 or else Is_Constrained
(Etype
(Parent_Node
))
4179 if not Compile_Time_Known_Value
(Aggr_Bounds
.Last
)
4180 or else Expr_Value
(Aggr_Bounds
.Last
) /=
4181 Expr_Value
(Obj_Bounds
.Last
)
4187 Next_Index
(Aggr_In
);
4188 Next_Index
(Obj_In
);
4192 -- Now check the component values themselves, except for an allocator
4193 -- for which the target is newly allocated memory.
4195 if Parent_Kind
= N_Allocator
then
4198 return Safe_Aggregate
(N
);
4200 end In_Place_Assign_OK
;
4202 ----------------------------
4203 -- Convert_To_Assignments --
4204 ----------------------------
4206 procedure Convert_To_Assignments
(N
: Node_Id
; Typ
: Entity_Id
) is
4207 Loc
: constant Source_Ptr
:= Sloc
(N
);
4211 Aggr_Code
: List_Id
;
4213 Target_Expr
: Node_Id
;
4214 Parent_Kind
: Node_Kind
;
4215 Unc_Decl
: Boolean := False;
4216 Parent_Node
: Node_Id
;
4219 pragma Assert
(Nkind
(N
) in N_Aggregate | N_Extension_Aggregate
);
4220 pragma Assert
(not Is_Static_Dispatch_Table_Aggregate
(N
));
4221 pragma Assert
(Is_Record_Type
(Typ
));
4223 Parent_Node
:= Parent
(N
);
4224 Parent_Kind
:= Nkind
(Parent_Node
);
4226 if Parent_Kind
= N_Qualified_Expression
then
4227 -- Check if we are in an unconstrained declaration because in this
4228 -- case the current delayed expansion mechanism doesn't work when
4229 -- the declared object size depends on the initializing expr.
4231 Parent_Node
:= Parent
(Parent_Node
);
4232 Parent_Kind
:= Nkind
(Parent_Node
);
4234 if Parent_Kind
= N_Object_Declaration
then
4236 not Is_Entity_Name
(Object_Definition
(Parent_Node
))
4237 or else (Nkind
(N
) = N_Aggregate
4240 (Entity
(Object_Definition
(Parent_Node
))))
4241 or else Is_Class_Wide_Type
4242 (Entity
(Object_Definition
(Parent_Node
)));
4246 -- Just set the Delay flag in the cases where the transformation will be
4247 -- done top down from above.
4250 -- Internal aggregates (transformed when expanding the parent),
4251 -- excluding container aggregates as these are transformed into
4252 -- subprogram calls later.
4255 N_Component_Association | N_Aggregate | N_Extension_Aggregate
4256 and then not Is_Container_Aggregate
(Parent_Node
))
4258 -- Allocator (see Convert_Aggr_In_Allocator)
4260 or else Parent_Kind
= N_Allocator
4262 -- Object declaration (see Convert_Aggr_In_Object_Decl)
4264 or else (Parent_Kind
= N_Object_Declaration
and then not Unc_Decl
)
4266 -- Safe assignment (see Convert_Aggr_Assignments). So far only the
4267 -- assignments in init procs are taken into account.
4269 or else (Parent_Kind
= N_Assignment_Statement
4270 and then Inside_Init_Proc
)
4272 -- (Ada 2005) An inherently limited type in a return statement, which
4273 -- will be handled in a build-in-place fashion, and may be rewritten
4274 -- as an extended return and have its own finalization machinery.
4275 -- In the case of a simple return, the aggregate needs to be delayed
4276 -- until the scope for the return statement has been created, so
4277 -- that any finalization chain will be associated with that scope.
4278 -- For extended returns, we delay expansion to avoid the creation
4279 -- of an unwanted transient scope that could result in premature
4280 -- finalization of the return object (which is built in place
4281 -- within the caller's scope).
4283 or else Is_Build_In_Place_Aggregate_Return
(N
)
4285 Set_Expansion_Delayed
(N
);
4289 -- Otherwise, if a transient scope is required, create it now. If we
4290 -- are within an initialization procedure do not create such, because
4291 -- the target of the assignment must not be declared within a local
4292 -- block, and because cleanup will take place on return from the
4293 -- initialization procedure.
4295 -- Should the condition be more restrictive ???
4297 if Requires_Transient_Scope
(Typ
) and then not Inside_Init_Proc
then
4298 Establish_Transient_Scope
(N
, Manage_Sec_Stack
=> False);
4301 -- If the aggregate is nonlimited, create a temporary, since aggregates
4302 -- have "by copy" semantics. If it is limited and context is an
4303 -- assignment, this is a subaggregate for an enclosing aggregate being
4304 -- expanded. It must be built in place, so use target of the current
4307 if Is_Limited_Type
(Typ
)
4308 and then Parent_Kind
= N_Assignment_Statement
4310 Target_Expr
:= New_Copy_Tree
(Name
(Parent_Node
));
4311 Insert_Actions
(Parent_Node
,
4312 Build_Record_Aggr_Code
(N
, Typ
, Target_Expr
));
4313 Rewrite
(Parent_Node
, Make_Null_Statement
(Loc
));
4315 -- Do not declare a temporary to initialize an aggregate assigned to
4316 -- a target when in-place assignment is possible, i.e. preserving the
4317 -- by-copy semantic of aggregates. This avoids large stack usage and
4318 -- generates more efficient code.
4320 elsif Parent_Kind
= N_Assignment_Statement
4321 and then In_Place_Assign_OK
(N
, Get_Base_Object
(Name
(Parent_Node
)))
4324 Lhs
: constant Node_Id
:= Name
(Parent_Node
);
4326 -- Apply discriminant check if required
4328 if Has_Discriminants
(Etype
(N
)) then
4329 Apply_Discriminant_Check
(N
, Etype
(Lhs
), Lhs
);
4332 -- The check just above may have replaced the aggregate with a CE
4334 if Nkind
(N
) in N_Aggregate | N_Extension_Aggregate
then
4335 Target_Expr
:= New_Copy_Tree
(Lhs
);
4336 Insert_Actions
(Parent_Node
,
4337 Build_Record_Aggr_Code
(N
, Typ
, Target_Expr
));
4338 Rewrite
(Parent_Node
, Make_Null_Statement
(Loc
));
4343 Temp
:= Make_Temporary
(Loc
, 'A', N
);
4345 -- If the type inherits unknown discriminants, use the view with
4346 -- known discriminants if available.
4348 if Has_Unknown_Discriminants
(Typ
)
4349 and then Present
(Underlying_Record_View
(Typ
))
4351 T
:= Underlying_Record_View
(Typ
);
4357 Make_Object_Declaration
(Loc
,
4358 Defining_Identifier
=> Temp
,
4359 Object_Definition
=> New_Occurrence_Of
(T
, Loc
));
4361 Set_No_Initialization
(Instr
);
4362 Insert_Action
(N
, Instr
);
4363 Initialize_Discriminants
(Instr
, T
);
4365 Target_Expr
:= New_Occurrence_Of
(Temp
, Loc
);
4366 Aggr_Code
:= Build_Record_Aggr_Code
(N
, T
, Target_Expr
);
4368 -- Save the last assignment statement associated with the aggregate
4369 -- when building a controlled object. This reference is utilized by
4370 -- the finalization machinery when marking an object as successfully
4373 if Needs_Finalization
(T
) then
4374 Set_Last_Aggregate_Assignment
(Temp
, Last
(Aggr_Code
));
4377 Insert_Actions
(N
, Aggr_Code
);
4378 Rewrite
(N
, New_Occurrence_Of
(Temp
, Loc
));
4379 Analyze_And_Resolve
(N
, T
);
4381 end Convert_To_Assignments
;
4383 ---------------------------
4384 -- Convert_To_Positional --
4385 ---------------------------
4387 procedure Convert_To_Positional
4389 Handle_Bit_Packed
: Boolean := False)
4391 Typ
: constant Entity_Id
:= Etype
(N
);
4392 Dims
: constant Nat
:= Number_Dimensions
(Typ
);
4393 Max_Others_Replicate
: constant Nat
:= Max_Aggregate_Size
(N
);
4395 Static_Components
: Boolean := True;
4397 procedure Check_Static_Components
;
4398 -- Check whether all components of the aggregate are compile-time known
4399 -- values, and can be passed as is to the back-end without further
4406 Ixb
: Node_Id
) return Boolean;
4407 -- Convert the aggregate into a purely positional form if possible after
4408 -- checking that the bounds of all dimensions are known to be static.
4410 function Is_Flat
(N
: Node_Id
; Dims
: Nat
) return Boolean;
4411 -- Return True if the aggregate N is flat (which is not trivial in the
4412 -- case of multidimensional aggregates).
4414 function Is_Static_Element
(N
: Node_Id
; Dims
: Nat
) return Boolean;
4415 -- Return True if N, an element of a component association list, i.e.
4416 -- N_Component_Association or N_Iterated_Component_Association, has a
4417 -- compile-time known value and can be passed as is to the back-end
4418 -- without further expansion.
4419 -- An Iterated_Component_Association is treated as nonstatic in most
4420 -- cases for now, so there are possibilities for optimization.
4422 -----------------------------
4423 -- Check_Static_Components --
4424 -----------------------------
4426 -- Could use some comments in this body ???
4428 procedure Check_Static_Components
is
4433 Static_Components
:= True;
4435 if Nkind
(N
) = N_String_Literal
then
4438 elsif Present
(Expressions
(N
)) then
4439 Expr
:= First
(Expressions
(N
));
4440 while Present
(Expr
) loop
4441 if Nkind
(Expr
) /= N_Aggregate
4442 or else not Compile_Time_Known_Aggregate
(Expr
)
4443 or else Expansion_Delayed
(Expr
)
4445 Static_Components
:= False;
4453 if Nkind
(N
) = N_Aggregate
4454 and then Present
(Component_Associations
(N
))
4456 Assoc
:= First
(Component_Associations
(N
));
4457 while Present
(Assoc
) loop
4458 if not Is_Static_Element
(Assoc
, Dims
) then
4459 Static_Components
:= False;
4466 end Check_Static_Components
;
4476 Ixb
: Node_Id
) return Boolean
4478 Loc
: constant Source_Ptr
:= Sloc
(N
);
4479 Blo
: constant Node_Id
:= Type_Low_Bound
(Etype
(Ixb
));
4480 Lo
: constant Node_Id
:= Type_Low_Bound
(Etype
(Ix
));
4481 Hi
: constant Node_Id
:= Type_High_Bound
(Etype
(Ix
));
4483 function Cannot_Flatten_Next_Aggr
(Expr
: Node_Id
) return Boolean;
4484 -- Return true if Expr is an aggregate for the next dimension that
4485 -- cannot be recursively flattened.
4487 ------------------------------
4488 -- Cannot_Flatten_Next_Aggr --
4489 ------------------------------
4491 function Cannot_Flatten_Next_Aggr
(Expr
: Node_Id
) return Boolean is
4493 return Nkind
(Expr
) = N_Aggregate
4494 and then Present
(Next_Index
(Ix
))
4496 Flatten
(Expr
, Dims
- 1, Next_Index
(Ix
), Next_Index
(Ixb
));
4497 end Cannot_Flatten_Next_Aggr
;
4503 Others_Present
: Boolean;
4505 -- Start of processing for Flatten
4508 if Nkind
(Original_Node
(N
)) = N_String_Literal
then
4512 if not Compile_Time_Known_Value
(Lo
)
4513 or else not Compile_Time_Known_Value
(Hi
)
4518 Lov
:= Expr_Value
(Lo
);
4519 Hiv
:= Expr_Value
(Hi
);
4521 -- Check if there is an others choice
4523 Others_Present
:= False;
4525 if Present
(Component_Associations
(N
)) then
4526 if Is_Empty_List
(Component_Associations
(N
)) then
4527 -- an expanded null array aggregate
4536 Assoc
:= First
(Component_Associations
(N
));
4537 while Present
(Assoc
) loop
4539 -- If this is a box association, flattening is in general
4540 -- not possible because at this point we cannot tell if the
4541 -- default is static or even exists.
4543 if Box_Present
(Assoc
) then
4546 elsif Nkind
(Assoc
) = N_Iterated_Component_Association
then
4550 Choice
:= First
(Choice_List
(Assoc
));
4552 while Present
(Choice
) loop
4553 if Nkind
(Choice
) = N_Others_Choice
then
4554 Others_Present
:= True;
4565 -- If the low bound is not known at compile time and others is not
4566 -- present we can proceed since the bounds can be obtained from the
4570 or else (not Compile_Time_Known_Value
(Blo
) and then Others_Present
)
4575 -- Determine if set of alternatives is suitable for conversion and
4576 -- build an array containing the values in sequence.
4579 Vals
: array (UI_To_Int
(Lov
) .. UI_To_Int
(Hiv
))
4580 of Node_Id
:= (others => Empty
);
4581 -- The values in the aggregate sorted appropriately
4584 -- Same data as Vals in list form
4587 -- Used to validate Max_Others_Replicate limit
4591 Num
: Int
:= UI_To_Int
(Lov
);
4597 if Present
(Expressions
(N
)) then
4598 Elmt
:= First
(Expressions
(N
));
4599 while Present
(Elmt
) loop
4600 -- In the case of a multidimensional array, check that the
4601 -- aggregate can be recursively flattened.
4603 if Cannot_Flatten_Next_Aggr
(Elmt
) then
4607 -- Duplicate expression for each index it covers
4609 Vals
(Num
) := New_Copy_Tree
(Elmt
);
4616 if No
(Component_Associations
(N
)) then
4620 Elmt
:= First
(Component_Associations
(N
));
4622 Component_Loop
: while Present
(Elmt
) loop
4623 Expr
:= Expression
(Elmt
);
4625 -- In the case of a multidimensional array, check that the
4626 -- aggregate can be recursively flattened.
4628 if Cannot_Flatten_Next_Aggr
(Expr
) then
4632 Choice
:= First
(Choice_List
(Elmt
));
4633 Choice_Loop
: while Present
(Choice
) loop
4635 -- If we have an others choice, fill in the missing elements
4636 -- subject to the limit established by Max_Others_Replicate.
4638 if Nkind
(Choice
) = N_Others_Choice
then
4641 -- If the expression involves a construct that generates
4642 -- a loop, we must generate individual assignments and
4643 -- no flattening is possible.
4645 if Nkind
(Expr
) = N_Quantified_Expression
then
4649 for J
in Vals
'Range loop
4650 if No
(Vals
(J
)) then
4651 Vals
(J
) := New_Copy_Tree
(Expr
);
4652 Rep_Count
:= Rep_Count
+ 1;
4654 -- Check for maximum others replication. Note that
4655 -- we skip this test if either of the restrictions
4656 -- No_Implicit_Loops or No_Elaboration_Code is
4657 -- active, if this is a preelaborable unit or
4658 -- a predefined unit, or if the unit must be
4659 -- placed in data memory. This also ensures that
4660 -- predefined units get the same level of constant
4661 -- folding in Ada 95 and Ada 2005, where their
4662 -- categorization has changed.
4665 P
: constant Entity_Id
:=
4666 Cunit_Entity
(Current_Sem_Unit
);
4669 -- Check if duplication is always OK and, if so,
4670 -- continue processing.
4672 if Restriction_Active
(No_Implicit_Loops
) then
4675 -- If duplication is not always OK, continue
4676 -- only if either the element is static or is
4677 -- an aggregate (we already know it is OK).
4679 elsif not Is_Static_Element
(Elmt
, Dims
)
4680 and then Nkind
(Expr
) /= N_Aggregate
4684 -- Check if duplication is OK for elaboration
4685 -- purposes and, if so, continue processing.
4687 elsif Restriction_Active
(No_Elaboration_Code
)
4689 (Ekind
(Current_Scope
) = E_Package
4691 Static_Elaboration_Desired
(Current_Scope
))
4692 or else Is_Preelaborated
(P
)
4693 or else (Ekind
(P
) = E_Package_Body
4695 Is_Preelaborated
(Spec_Entity
(P
)))
4697 Is_Predefined_Unit
(Get_Source_Unit
(P
))
4701 -- Otherwise, check that the replication count
4704 elsif Rep_Count
> Max_Others_Replicate
then
4712 and then Warn_On_Redundant_Constructs
4714 Error_Msg_N
("there are no others?r?", Elmt
);
4717 exit Component_Loop
;
4719 -- Case of a subtype mark, identifier or expanded name
4721 elsif Is_Entity_Name
(Choice
)
4722 and then Is_Type
(Entity
(Choice
))
4724 Lo
:= Type_Low_Bound
(Etype
(Choice
));
4725 Hi
:= Type_High_Bound
(Etype
(Choice
));
4727 -- Case of subtype indication
4729 elsif Nkind
(Choice
) = N_Subtype_Indication
then
4730 Lo
:= Low_Bound
(Range_Expression
(Constraint
(Choice
)));
4731 Hi
:= High_Bound
(Range_Expression
(Constraint
(Choice
)));
4735 elsif Nkind
(Choice
) = N_Range
then
4736 Lo
:= Low_Bound
(Choice
);
4737 Hi
:= High_Bound
(Choice
);
4739 -- Normal subexpression case
4741 else pragma Assert
(Nkind
(Choice
) in N_Subexpr
);
4742 if not Compile_Time_Known_Value
(Choice
) then
4746 Choice_Index
:= UI_To_Int
(Expr_Value
(Choice
));
4748 if Choice_Index
in Vals
'Range then
4749 Vals
(Choice_Index
) := New_Copy_Tree
(Expr
);
4752 -- Choice is statically out-of-range, will be
4753 -- rewritten to raise Constraint_Error.
4761 -- Range cases merge with Lo,Hi set
4763 if not Compile_Time_Known_Value
(Lo
)
4765 not Compile_Time_Known_Value
(Hi
)
4770 for J
in UI_To_Int
(Expr_Value
(Lo
)) ..
4771 UI_To_Int
(Expr_Value
(Hi
))
4773 Vals
(J
) := New_Copy_Tree
(Expr
);
4779 end loop Choice_Loop
;
4782 end loop Component_Loop
;
4784 -- If we get here the conversion is possible
4787 for J
in Vals
'Range loop
4788 Append
(Vals
(J
), Vlist
);
4791 Rewrite
(N
, Make_Aggregate
(Loc
, Expressions
=> Vlist
));
4792 Set_Aggregate_Bounds
(N
, Aggregate_Bounds
(Original_Node
(N
)));
4801 function Is_Flat
(N
: Node_Id
; Dims
: Nat
) return Boolean is
4808 elsif Nkind
(N
) = N_Aggregate
then
4809 if Present
(Component_Associations
(N
)) then
4813 Elmt
:= First
(Expressions
(N
));
4814 while Present
(Elmt
) loop
4815 if not Is_Flat
(Elmt
, Dims
- 1) then
4829 -------------------------
4830 -- Is_Static_Element --
4831 -------------------------
4833 function Is_Static_Element
(N
: Node_Id
; Dims
: Nat
) return Boolean is
4834 Expr
: constant Node_Id
:= Expression
(N
);
4837 -- In most cases the interesting expressions are unambiguously static
4839 if Compile_Time_Known_Value
(Expr
) then
4842 elsif Nkind
(N
) = N_Iterated_Component_Association
then
4845 elsif Nkind
(Expr
) = N_Aggregate
4846 and then Compile_Time_Known_Aggregate
(Expr
)
4847 and then not Expansion_Delayed
(Expr
)
4851 -- However, one may write static expressions that are syntactically
4852 -- ambiguous, so preanalyze the expression before checking it again,
4853 -- but only at the innermost level for a multidimensional array.
4856 Preanalyze_And_Resolve
(Expr
, Component_Type
(Typ
));
4857 return Compile_Time_Known_Value
(Expr
);
4862 end Is_Static_Element
;
4864 -- Start of processing for Convert_To_Positional
4867 -- Only convert to positional when generating C in case of an
4868 -- object declaration, this is the only case where aggregates are
4871 if Modify_Tree_For_C
and then not Is_CCG_Supported_Aggregate
(N
) then
4875 -- Ada 2005 (AI-287): Do not convert in case of default initialized
4876 -- components because in this case will need to call the corresponding
4879 if Has_Default_Init_Comps
(N
) then
4883 -- A subaggregate may have been flattened but is not known to be
4884 -- Compile_Time_Known. Set that flag in cases that cannot require
4885 -- elaboration code, so that the aggregate can be used as the
4886 -- initial value of a thread-local variable.
4888 if Is_Flat
(N
, Dims
) then
4889 if Static_Array_Aggregate
(N
) then
4890 Set_Compile_Time_Known_Aggregate
(N
);
4896 if Is_Bit_Packed_Array
(Typ
) and then not Handle_Bit_Packed
then
4900 -- Do not convert to positional if controlled components are involved
4901 -- since these require special processing
4903 if Has_Controlled_Component
(Typ
) then
4907 Check_Static_Components
;
4909 -- If the size is known, or all the components are static, try to
4910 -- build a fully positional aggregate.
4912 -- The size of the type may not be known for an aggregate with
4913 -- discriminated array components, but if the components are static
4914 -- it is still possible to verify statically that the length is
4915 -- compatible with the upper bound of the type, and therefore it is
4916 -- worth flattening such aggregates as well.
4920 Flatten
(N
, Dims
, First_Index
(Typ
), First_Index
(Base_Type
(Typ
)))
4922 if Static_Components
then
4923 Set_Compile_Time_Known_Aggregate
(N
);
4924 Set_Expansion_Delayed
(N
, False);
4927 Analyze_And_Resolve
(N
, Typ
);
4930 -- If Static_Elaboration_Desired has been specified, diagnose aggregates
4931 -- that will still require initialization code.
4933 if (Ekind
(Current_Scope
) = E_Package
4934 and then Static_Elaboration_Desired
(Current_Scope
))
4935 and then Nkind
(Parent
(N
)) = N_Object_Declaration
4941 if Nkind
(N
) = N_Aggregate
and then Present
(Expressions
(N
)) then
4942 Expr
:= First
(Expressions
(N
));
4943 while Present
(Expr
) loop
4944 if not Compile_Time_Known_Value
(Expr
) then
4946 ("non-static object requires elaboration code??", N
);
4953 if Present
(Component_Associations
(N
)) then
4954 Error_Msg_N
("object requires elaboration code??", N
);
4959 end Convert_To_Positional
;
4961 ----------------------------
4962 -- Expand_Array_Aggregate --
4963 ----------------------------
4965 -- Array aggregate expansion proceeds as follows:
4967 -- 1. If requested we generate code to perform all the array aggregate
4968 -- bound checks, specifically
4970 -- (a) Check that the index range defined by aggregate bounds is
4971 -- compatible with corresponding index subtype.
4973 -- (b) If an others choice is present check that no aggregate
4974 -- index is outside the bounds of the index constraint.
4976 -- (c) For multidimensional arrays make sure that all subaggregates
4977 -- corresponding to the same dimension have the same bounds.
4979 -- 2. Check for packed array aggregate which can be converted to a
4980 -- constant so that the aggregate disappears completely.
4982 -- 3. Check case of nested aggregate. Generally nested aggregates are
4983 -- handled during the processing of the parent aggregate.
4985 -- 4. Check if the aggregate can be statically processed. If this is the
4986 -- case pass it as is to Gigi. Note that a necessary condition for
4987 -- static processing is that the aggregate be fully positional.
4989 -- 5. If in-place aggregate expansion is possible (i.e. no need to create
4990 -- a temporary) then mark the aggregate as such and return. Otherwise
4991 -- create a new temporary and generate the appropriate initialization
4994 procedure Expand_Array_Aggregate
(N
: Node_Id
) is
4995 Loc
: constant Source_Ptr
:= Sloc
(N
);
4997 Typ
: constant Entity_Id
:= Etype
(N
);
4998 Ctyp
: constant Entity_Id
:= Component_Type
(Typ
);
4999 -- Typ is the correct constrained array subtype of the aggregate
5000 -- Ctyp is the corresponding component type.
5002 Aggr_Dimension
: constant Pos
:= Number_Dimensions
(Typ
);
5003 -- Number of aggregate index dimensions
5005 Aggr_Low
: array (1 .. Aggr_Dimension
) of Node_Id
;
5006 Aggr_High
: array (1 .. Aggr_Dimension
) of Node_Id
;
5007 -- Low and High bounds of the constraint for each aggregate index
5009 Aggr_Index_Typ
: array (1 .. Aggr_Dimension
) of Entity_Id
;
5010 -- The type of each index
5012 In_Place_Assign_OK_For_Declaration
: Boolean := False;
5013 -- True if we are to generate an in-place assignment for a declaration
5015 Maybe_In_Place_OK
: Boolean;
5016 -- If the type is neither controlled nor packed and the aggregate
5017 -- is the expression in an assignment, assignment in place may be
5018 -- possible, provided other conditions are met on the LHS.
5020 Others_Present
: array (1 .. Aggr_Dimension
) of Boolean :=
5022 -- If Others_Present (J) is True, then there is an others choice in one
5023 -- of the subaggregates of N at dimension J.
5025 procedure Build_Constrained_Type
(Positional
: Boolean);
5026 -- If the subtype is not static or unconstrained, build a constrained
5027 -- type using the computable sizes of the aggregate and its sub-
5030 procedure Check_Bounds
(Aggr_Bounds_Node
, Index_Bounds_Node
: Node_Id
);
5031 -- Checks that the bounds of Aggr_Bounds are within the bounds defined
5032 -- by Index_Bounds. For null array aggregate (Ada 2022) check that the
5033 -- aggregate bounds define a null range.
5035 procedure Check_Same_Aggr_Bounds
(Sub_Aggr
: Node_Id
; Dim
: Pos
);
5036 -- Checks that in a multidimensional array aggregate all subaggregates
5037 -- corresponding to the same dimension have the same bounds. Sub_Aggr is
5038 -- an array subaggregate. Dim is the dimension corresponding to the
5041 procedure Compute_Others_Present
(Sub_Aggr
: Node_Id
; Dim
: Pos
);
5042 -- Computes the values of array Others_Present. Sub_Aggr is the array
5043 -- subaggregate we start the computation from. Dim is the dimension
5044 -- corresponding to the subaggregate.
5046 procedure Others_Check
(Sub_Aggr
: Node_Id
; Dim
: Pos
);
5047 -- Checks that if an others choice is present in any subaggregate, no
5048 -- aggregate index is outside the bounds of the index constraint.
5049 -- Sub_Aggr is an array subaggregate. Dim is the dimension corresponding
5050 -- to the subaggregate.
5052 function Safe_Left_Hand_Side
(N
: Node_Id
) return Boolean;
5053 -- In addition to Maybe_In_Place_OK, in order for an aggregate to be
5054 -- built directly into the target of the assignment it must be free
5055 -- of side effects. N is the LHS of an assignment.
5057 procedure Two_Pass_Aggregate_Expansion
(N
: Node_Id
);
5058 -- If the aggregate consists only of iterated associations then the
5059 -- aggregate is constructed in two steps:
5060 -- a) Build an expression to compute the number of elements
5061 -- generated by each iterator, and use the expression to allocate
5062 -- the destination aggregate.
5063 -- b) Generate the loops corresponding to each iterator to insert
5064 -- the elements in their proper positions.
5066 ----------------------------
5067 -- Build_Constrained_Type --
5068 ----------------------------
5070 procedure Build_Constrained_Type
(Positional
: Boolean) is
5071 Agg_Type
: constant Entity_Id
:= Make_Temporary
(Loc
, 'A');
5073 Indexes
: constant List_Id
:= New_List
;
5078 -- If the aggregate is purely positional, all its subaggregates
5079 -- have the same size. We collect the dimensions from the first
5080 -- subaggregate at each level.
5085 for D
in 1 .. Aggr_Dimension
loop
5086 Num
:= List_Length
(Expressions
(Sub_Agg
));
5090 Low_Bound
=> Make_Integer_Literal
(Loc
, Uint_1
),
5091 High_Bound
=> Make_Integer_Literal
(Loc
, Num
)));
5093 Sub_Agg
:= First
(Expressions
(Sub_Agg
));
5097 -- We know the aggregate type is unconstrained and the aggregate
5098 -- is not processable by the back end, therefore not necessarily
5099 -- positional. Retrieve each dimension bounds (computed earlier).
5101 for D
in 1 .. Aggr_Dimension
loop
5104 Low_Bound
=> Aggr_Low
(D
),
5105 High_Bound
=> Aggr_High
(D
)));
5110 Make_Full_Type_Declaration
(Loc
,
5111 Defining_Identifier
=> Agg_Type
,
5113 Make_Constrained_Array_Definition
(Loc
,
5114 Discrete_Subtype_Definitions
=> Indexes
,
5115 Component_Definition
=>
5116 Make_Component_Definition
(Loc
,
5117 Subtype_Indication
=>
5118 New_Occurrence_Of
(Component_Type
(Typ
), Loc
))));
5120 Insert_Action
(N
, Decl
);
5122 Set_Etype
(N
, Agg_Type
);
5123 Set_Is_Itype
(Agg_Type
);
5124 Freeze_Itype
(Agg_Type
, N
);
5125 end Build_Constrained_Type
;
5131 procedure Check_Bounds
(Aggr_Bounds_Node
, Index_Bounds_Node
: Node_Id
) is
5132 Aggr_Bounds
: constant Range_Nodes
:=
5133 Get_Index_Bounds
(Aggr_Bounds_Node
);
5134 Ind_Bounds
: constant Range_Nodes
:=
5135 Get_Index_Bounds
(Index_Bounds_Node
);
5140 -- For a null array aggregate check that high bound (i.e., low
5141 -- bound predecessor) exists. Fail if low bound is low bound of
5142 -- base subtype (in all cases, including modular).
5144 if Is_Null_Aggregate
(N
) then
5146 Make_Raise_Constraint_Error
(Loc
,
5149 New_Copy_Tree
(Aggr_Bounds
.First
),
5151 (Type_Low_Bound
(Base_Type
(Etype
(Ind_Bounds
.First
))))),
5152 Reason
=> CE_Range_Check_Failed
));
5156 -- Generate the following test:
5158 -- [constraint_error when
5159 -- Aggr_Bounds.First <= Aggr_Bounds.Last and then
5160 -- (Aggr_Bounds.First < Ind_Bounds.First
5161 -- or else Aggr_Bounds.Last > Ind_Bounds.Last)]
5163 -- As an optimization try to see if some tests are trivially vacuous
5164 -- because we are comparing an expression against itself.
5166 if Aggr_Bounds
.First
= Ind_Bounds
.First
5167 and then Aggr_Bounds
.Last
= Ind_Bounds
.Last
5171 elsif Aggr_Bounds
.Last
= Ind_Bounds
.Last
then
5175 Duplicate_Subexpr_Move_Checks
(Aggr_Bounds
.First
),
5177 Duplicate_Subexpr_Move_Checks
(Ind_Bounds
.First
));
5179 elsif Aggr_Bounds
.First
= Ind_Bounds
.First
then
5182 Left_Opnd
=> Duplicate_Subexpr_Move_Checks
(Aggr_Bounds
.Last
),
5183 Right_Opnd
=> Duplicate_Subexpr_Move_Checks
(Ind_Bounds
.Last
));
5191 Duplicate_Subexpr_Move_Checks
(Aggr_Bounds
.First
),
5193 Duplicate_Subexpr_Move_Checks
(Ind_Bounds
.First
)),
5197 Left_Opnd
=> Duplicate_Subexpr
(Aggr_Bounds
.Last
),
5198 Right_Opnd
=> Duplicate_Subexpr
(Ind_Bounds
.Last
)));
5201 if Present
(Cond
) then
5207 Duplicate_Subexpr_Move_Checks
(Aggr_Bounds
.First
),
5209 Duplicate_Subexpr_Move_Checks
(Aggr_Bounds
.Last
)),
5211 Right_Opnd
=> Cond
);
5213 Set_Analyzed
(Left_Opnd
(Left_Opnd
(Cond
)), False);
5214 Set_Analyzed
(Right_Opnd
(Left_Opnd
(Cond
)), False);
5216 Make_Raise_Constraint_Error
(Loc
,
5218 Reason
=> CE_Range_Check_Failed
));
5222 ----------------------------
5223 -- Check_Same_Aggr_Bounds --
5224 ----------------------------
5226 procedure Check_Same_Aggr_Bounds
(Sub_Aggr
: Node_Id
; Dim
: Pos
) is
5227 Sub_Bounds
: constant Range_Nodes
:=
5228 Get_Index_Bounds
(Aggregate_Bounds
(Sub_Aggr
));
5229 Sub_Lo
: Node_Id
renames Sub_Bounds
.First
;
5230 Sub_Hi
: Node_Id
renames Sub_Bounds
.Last
;
5231 -- The bounds of this specific subaggregate
5233 Aggr_Lo
: constant Node_Id
:= Aggr_Low
(Dim
);
5234 Aggr_Hi
: constant Node_Id
:= Aggr_High
(Dim
);
5235 -- The bounds of the aggregate for this dimension
5237 Ind_Typ
: constant Entity_Id
:= Aggr_Index_Typ
(Dim
);
5238 -- The index type for this dimension.xxx
5245 -- If index checks are on generate the test
5247 -- [constraint_error when
5248 -- Aggr_Lo /= Sub_Lo or else Aggr_Hi /= Sub_Hi]
5250 -- As an optimization try to see if some tests are trivially vacuos
5251 -- because we are comparing an expression against itself. Also for
5252 -- the first dimension the test is trivially vacuous because there
5253 -- is just one aggregate for dimension 1.
5255 if Index_Checks_Suppressed
(Ind_Typ
) then
5258 elsif Dim
= 1 or else (Aggr_Lo
= Sub_Lo
and then Aggr_Hi
= Sub_Hi
)
5262 elsif Aggr_Hi
= Sub_Hi
then
5265 Left_Opnd
=> Duplicate_Subexpr_Move_Checks
(Aggr_Lo
),
5266 Right_Opnd
=> Duplicate_Subexpr_Move_Checks
(Sub_Lo
));
5268 elsif Aggr_Lo
= Sub_Lo
then
5271 Left_Opnd
=> Duplicate_Subexpr_Move_Checks
(Aggr_Hi
),
5272 Right_Opnd
=> Duplicate_Subexpr_Move_Checks
(Sub_Hi
));
5279 Left_Opnd
=> Duplicate_Subexpr_Move_Checks
(Aggr_Lo
),
5280 Right_Opnd
=> Duplicate_Subexpr_Move_Checks
(Sub_Lo
)),
5284 Left_Opnd
=> Duplicate_Subexpr
(Aggr_Hi
),
5285 Right_Opnd
=> Duplicate_Subexpr
(Sub_Hi
)));
5288 if Present
(Cond
) then
5290 Make_Raise_Constraint_Error
(Loc
,
5292 Reason
=> CE_Length_Check_Failed
));
5295 -- Now look inside the subaggregate to see if there is more work
5297 if Dim
< Aggr_Dimension
then
5299 -- Process positional components
5301 if Present
(Expressions
(Sub_Aggr
)) then
5302 Expr
:= First
(Expressions
(Sub_Aggr
));
5303 while Present
(Expr
) loop
5304 Check_Same_Aggr_Bounds
(Expr
, Dim
+ 1);
5309 -- Process component associations
5311 if Present
(Component_Associations
(Sub_Aggr
)) then
5312 Assoc
:= First
(Component_Associations
(Sub_Aggr
));
5313 while Present
(Assoc
) loop
5314 Expr
:= Expression
(Assoc
);
5315 Check_Same_Aggr_Bounds
(Expr
, Dim
+ 1);
5320 end Check_Same_Aggr_Bounds
;
5322 ----------------------------
5323 -- Compute_Others_Present --
5324 ----------------------------
5326 procedure Compute_Others_Present
(Sub_Aggr
: Node_Id
; Dim
: Pos
) is
5331 if Present
(Component_Associations
(Sub_Aggr
)) then
5332 Assoc
:= Last
(Component_Associations
(Sub_Aggr
));
5335 and then Nkind
(First
(Choice_List
(Assoc
))) = N_Others_Choice
5337 Others_Present
(Dim
) := True;
5339 -- An others_clause may be superfluous if previous components
5340 -- cover the full given range of a constrained array. In such
5341 -- a case an others_clause does not contribute any additional
5342 -- components and has not been analyzed. We analyze it now to
5343 -- detect type errors in the expression, even though no code
5344 -- will be generated for it.
5346 if Dim
= Aggr_Dimension
5347 and then Nkind
(Assoc
) /= N_Iterated_Component_Association
5348 and then not Analyzed
(Expression
(Assoc
))
5349 and then not Box_Present
(Assoc
)
5351 Preanalyze_And_Resolve
(Expression
(Assoc
), Ctyp
);
5356 -- Now look inside the subaggregate to see if there is more work
5358 if Dim
< Aggr_Dimension
then
5360 -- Process positional components
5362 if Present
(Expressions
(Sub_Aggr
)) then
5363 Expr
:= First
(Expressions
(Sub_Aggr
));
5364 while Present
(Expr
) loop
5365 Compute_Others_Present
(Expr
, Dim
+ 1);
5370 -- Process component associations
5372 if Present
(Component_Associations
(Sub_Aggr
)) then
5373 Assoc
:= First
(Component_Associations
(Sub_Aggr
));
5374 while Present
(Assoc
) loop
5375 Expr
:= Expression
(Assoc
);
5376 Compute_Others_Present
(Expr
, Dim
+ 1);
5381 end Compute_Others_Present
;
5387 procedure Others_Check
(Sub_Aggr
: Node_Id
; Dim
: Pos
) is
5388 Aggr_Lo
: constant Node_Id
:= Aggr_Low
(Dim
);
5389 Aggr_Hi
: constant Node_Id
:= Aggr_High
(Dim
);
5390 -- The bounds of the aggregate for this dimension
5392 Ind_Typ
: constant Entity_Id
:= Aggr_Index_Typ
(Dim
);
5393 -- The index type for this dimension
5395 Need_To_Check
: Boolean := False;
5397 Choices_Lo
: Node_Id
:= Empty
;
5398 Choices_Hi
: Node_Id
:= Empty
;
5399 -- The lowest and highest discrete choices for a named subaggregate
5401 Nb_Choices
: Int
:= -1;
5402 -- The number of discrete non-others choices in this subaggregate
5404 Nb_Elements
: Uint
:= Uint_0
;
5405 -- The number of elements in a positional aggregate
5407 Cond
: Node_Id
:= Empty
;
5414 -- Check if we have an others choice. If we do make sure that this
5415 -- subaggregate contains at least one element in addition to the
5418 if Range_Checks_Suppressed
(Ind_Typ
) then
5419 Need_To_Check
:= False;
5421 elsif Present
(Expressions
(Sub_Aggr
))
5422 and then Present
(Component_Associations
(Sub_Aggr
))
5425 not (Is_Empty_List
(Expressions
(Sub_Aggr
))
5426 and then Is_Empty_List
5427 (Component_Associations
(Sub_Aggr
)));
5429 elsif Present
(Component_Associations
(Sub_Aggr
)) then
5430 Assoc
:= Last
(Component_Associations
(Sub_Aggr
));
5432 if Nkind
(First
(Choice_List
(Assoc
))) /= N_Others_Choice
then
5433 Need_To_Check
:= False;
5436 -- Count the number of discrete choices. Start with -1 because
5437 -- the others choice does not count.
5439 -- Is there some reason we do not use List_Length here ???
5442 Assoc
:= First
(Component_Associations
(Sub_Aggr
));
5443 while Present
(Assoc
) loop
5444 Choice
:= First
(Choice_List
(Assoc
));
5445 while Present
(Choice
) loop
5446 Nb_Choices
:= Nb_Choices
+ 1;
5453 -- If there is only an others choice nothing to do
5455 Need_To_Check
:= (Nb_Choices
> 0);
5459 Need_To_Check
:= False;
5462 -- If we are dealing with a positional subaggregate with an others
5463 -- choice then compute the number or positional elements.
5465 if Need_To_Check
and then Present
(Expressions
(Sub_Aggr
)) then
5466 Expr
:= First
(Expressions
(Sub_Aggr
));
5467 Nb_Elements
:= Uint_0
;
5468 while Present
(Expr
) loop
5469 Nb_Elements
:= Nb_Elements
+ 1;
5473 -- If the aggregate contains discrete choices and an others choice
5474 -- compute the smallest and largest discrete choice values.
5476 elsif Need_To_Check
then
5477 Compute_Choices_Lo_And_Choices_Hi
: declare
5479 Table
: Case_Table_Type
(1 .. Nb_Choices
);
5480 -- Used to sort all the different choice values
5485 Assoc
:= First
(Component_Associations
(Sub_Aggr
));
5486 while Present
(Assoc
) loop
5487 Choice
:= First
(Choice_List
(Assoc
));
5488 while Present
(Choice
) loop
5489 if Nkind
(Choice
) = N_Others_Choice
then
5494 Bounds
: constant Range_Nodes
:=
5495 Get_Index_Bounds
(Choice
);
5497 Table
(J
).Choice_Lo
:= Bounds
.First
;
5498 Table
(J
).Choice_Hi
:= Bounds
.Last
;
5508 -- Sort the discrete choices
5510 Sort_Case_Table
(Table
);
5512 Choices_Lo
:= Table
(1).Choice_Lo
;
5513 Choices_Hi
:= Table
(Nb_Choices
).Choice_Hi
;
5514 end Compute_Choices_Lo_And_Choices_Hi
;
5517 -- If no others choice in this subaggregate, or the aggregate
5518 -- comprises only an others choice, nothing to do.
5520 if not Need_To_Check
then
5523 -- If we are dealing with an aggregate containing an others choice
5524 -- and positional components, we generate the following test:
5526 -- if Ind_Typ'Pos (Aggr_Lo) + (Nb_Elements - 1) >
5527 -- Ind_Typ'Pos (Aggr_Hi)
5529 -- raise Constraint_Error;
5532 -- in the general case, but the following simpler test:
5534 -- [constraint_error when
5535 -- Aggr_Lo + (Nb_Elements - 1) > Aggr_Hi];
5537 -- instead if the index type is a signed integer.
5539 elsif Nb_Elements
> Uint_0
then
5540 if Nb_Elements
= Uint_1
then
5543 Left_Opnd
=> Duplicate_Subexpr_Move_Checks
(Aggr_Lo
),
5544 Right_Opnd
=> Duplicate_Subexpr_Move_Checks
(Aggr_Hi
));
5546 elsif Is_Signed_Integer_Type
(Ind_Typ
) then
5551 Left_Opnd
=> Duplicate_Subexpr_Move_Checks
(Aggr_Lo
),
5553 Make_Integer_Literal
(Loc
, Nb_Elements
- 1)),
5554 Right_Opnd
=> Duplicate_Subexpr_Move_Checks
(Aggr_Hi
));
5562 Make_Attribute_Reference
(Loc
,
5563 Prefix
=> New_Occurrence_Of
(Ind_Typ
, Loc
),
5564 Attribute_Name
=> Name_Pos
,
5567 (Duplicate_Subexpr_Move_Checks
(Aggr_Lo
))),
5568 Right_Opnd
=> Make_Integer_Literal
(Loc
, Nb_Elements
- 1)),
5571 Make_Attribute_Reference
(Loc
,
5572 Prefix
=> New_Occurrence_Of
(Ind_Typ
, Loc
),
5573 Attribute_Name
=> Name_Pos
,
5574 Expressions
=> New_List
(
5575 Duplicate_Subexpr_Move_Checks
(Aggr_Hi
))));
5578 -- If we are dealing with an aggregate containing an others choice
5579 -- and discrete choices we generate the following test:
5581 -- [constraint_error when
5582 -- Choices_Lo < Aggr_Lo or else Choices_Hi > Aggr_Hi];
5589 Left_Opnd
=> Duplicate_Subexpr_Move_Checks
(Choices_Lo
),
5590 Right_Opnd
=> Duplicate_Subexpr_Move_Checks
(Aggr_Lo
)),
5594 Left_Opnd
=> Duplicate_Subexpr
(Choices_Hi
),
5595 Right_Opnd
=> Duplicate_Subexpr
(Aggr_Hi
)));
5598 if Present
(Cond
) then
5600 Make_Raise_Constraint_Error
(Loc
,
5602 Reason
=> CE_Length_Check_Failed
));
5603 -- Questionable reason code, shouldn't that be a
5604 -- CE_Range_Check_Failed ???
5607 -- Now look inside the subaggregate to see if there is more work
5609 if Dim
< Aggr_Dimension
then
5611 -- Process positional components
5613 if Present
(Expressions
(Sub_Aggr
)) then
5614 Expr
:= First
(Expressions
(Sub_Aggr
));
5615 while Present
(Expr
) loop
5616 Others_Check
(Expr
, Dim
+ 1);
5621 -- Process component associations
5623 if Present
(Component_Associations
(Sub_Aggr
)) then
5624 Assoc
:= First
(Component_Associations
(Sub_Aggr
));
5625 while Present
(Assoc
) loop
5626 Expr
:= Expression
(Assoc
);
5627 Others_Check
(Expr
, Dim
+ 1);
5634 -------------------------
5635 -- Safe_Left_Hand_Side --
5636 -------------------------
5638 function Safe_Left_Hand_Side
(N
: Node_Id
) return Boolean is
5639 function Is_Safe_Index
(Indx
: Node_Id
) return Boolean;
5640 -- If the left-hand side includes an indexed component, check that
5641 -- the indexes are free of side effects.
5647 function Is_Safe_Index
(Indx
: Node_Id
) return Boolean is
5649 if Is_Entity_Name
(Indx
) then
5652 elsif Nkind
(Indx
) = N_Integer_Literal
then
5655 elsif Nkind
(Indx
) = N_Function_Call
5656 and then Is_Entity_Name
(Name
(Indx
))
5657 and then Has_Pragma_Pure_Function
(Entity
(Name
(Indx
)))
5661 elsif Nkind
(Indx
) = N_Type_Conversion
5662 and then Is_Safe_Index
(Expression
(Indx
))
5671 -- Start of processing for Safe_Left_Hand_Side
5674 if Is_Entity_Name
(N
) then
5677 elsif Nkind
(N
) in N_Explicit_Dereference | N_Selected_Component
5678 and then Safe_Left_Hand_Side
(Prefix
(N
))
5682 elsif Nkind
(N
) = N_Indexed_Component
5683 and then Safe_Left_Hand_Side
(Prefix
(N
))
5684 and then Is_Safe_Index
(First
(Expressions
(N
)))
5688 elsif Nkind
(N
) = N_Unchecked_Type_Conversion
then
5689 return Safe_Left_Hand_Side
(Expression
(N
));
5694 end Safe_Left_Hand_Side
;
5696 ----------------------------------
5697 -- Two_Pass_Aggregate_Expansion --
5698 ----------------------------------
5700 procedure Two_Pass_Aggregate_Expansion
(N
: Node_Id
) is
5701 Loc
: constant Source_Ptr
:= Sloc
(N
);
5702 Comp_Type
: constant Entity_Id
:= Etype
(N
);
5703 Index_Id
: constant Entity_Id
:= Make_Temporary
(Loc
, 'I', N
);
5704 Index_Type
: constant Entity_Id
:= Etype
(First_Index
(Etype
(N
)));
5705 Size_Id
: constant Entity_Id
:= Make_Temporary
(Loc
, 'I', N
);
5706 TmpE
: constant Entity_Id
:= Make_Temporary
(Loc
, 'A', N
);
5708 Assoc
: Node_Id
:= First
(Component_Associations
(N
));
5714 Size_Expr_Code
: List_Id
;
5715 Insertion_Code
: List_Id
:= New_List
;
5718 Size_Expr_Code
:= New_List
(
5719 Make_Object_Declaration
(Loc
,
5720 Defining_Identifier
=> Size_Id
,
5721 Object_Definition
=> New_Occurrence_Of
(Standard_Integer
, Loc
),
5722 Expression
=> Make_Integer_Literal
(Loc
, 0)));
5724 -- First pass: execute the iterators to count the number of elements
5725 -- that will be generated.
5727 while Present
(Assoc
) loop
5728 Iter
:= Iterator_Specification
(Assoc
);
5729 Incr
:= Make_Assignment_Statement
(Loc
,
5730 Name
=> New_Occurrence_Of
(Size_Id
, Loc
),
5733 Left_Opnd
=> New_Occurrence_Of
(Size_Id
, Loc
),
5734 Right_Opnd
=> Make_Integer_Literal
(Loc
, 1)));
5736 One_Loop
:= Make_Implicit_Loop_Statement
(N
,
5738 Make_Iteration_Scheme
(Loc
,
5739 Iterator_Specification
=> New_Copy_Tree
(Iter
)),
5740 Statements
=> New_List
(Incr
));
5742 Append
(One_Loop
, Size_Expr_Code
);
5746 Insert_Actions
(N
, Size_Expr_Code
);
5748 -- Build a constrained subtype with the calculated length
5749 -- and declare the proper bounded aggregate object.
5750 -- The index type is some discrete type, so the bounds of the
5751 -- constructed array are computed as T'Val (T'Pos (ineger bound));
5754 Pos_Lo
: constant Node_Id
:=
5755 Make_Attribute_Reference
(Loc
,
5756 Prefix
=> New_Occurrence_Of
(Index_Type
, Loc
),
5757 Attribute_Name
=> Name_Pos
,
5758 Expressions
=> New_List
(
5759 Make_Attribute_Reference
(Loc
,
5760 Prefix
=> New_Occurrence_Of
(Index_Type
, Loc
),
5761 Attribute_Name
=> Name_First
)));
5763 Aggr_Lo
: constant Node_Id
:=
5764 Make_Attribute_Reference
(Loc
,
5765 Prefix
=> New_Occurrence_Of
(Index_Type
, Loc
),
5766 Attribute_Name
=> Name_Val
,
5767 Expressions
=> New_List
(New_Copy_Tree
(Pos_Lo
)));
5769 -- Hi = Index_type'Pos (Lo + Size -1).
5771 Pos_Hi
: constant Node_Id
:=
5773 Left_Opnd
=> New_Copy_Tree
(Pos_Lo
),
5775 Make_Op_Subtract
(Loc
,
5776 Left_Opnd
=> New_Occurrence_Of
(Size_Id
, Loc
),
5777 Right_Opnd
=> Make_Integer_Literal
(Loc
, 1)));
5779 -- Corresponding index value
5781 Aggr_Hi
: constant Node_Id
:=
5782 Make_Attribute_Reference
(Loc
,
5783 Prefix
=> New_Occurrence_Of
(Index_Type
, Loc
),
5784 Attribute_Name
=> Name_Val
,
5785 Expressions
=> New_List
(New_Copy_Tree
(Pos_Hi
)));
5787 SubE
: constant Entity_Id
:= Make_Temporary
(Loc
, 'T');
5788 SubD
: constant Node_Id
:=
5789 Make_Subtype_Declaration
(Loc
,
5790 Defining_Identifier
=> SubE
,
5791 Subtype_Indication
=>
5792 Make_Subtype_Indication
(Loc
,
5794 New_Occurrence_Of
(Etype
(Comp_Type
), Loc
),
5796 Make_Index_Or_Discriminant_Constraint
5799 New_List
(Make_Range
(Loc
, Aggr_Lo
, Aggr_Hi
)))));
5801 -- Create a temporary array of the above subtype which
5802 -- will be used to capture the aggregate assignments.
5804 TmpD
: constant Node_Id
:=
5805 Make_Object_Declaration
(Loc
,
5806 Defining_Identifier
=> TmpE
,
5807 Object_Definition
=> New_Occurrence_Of
(SubE
, Loc
));
5809 Insert_Actions
(N
, New_List
(SubD
, TmpD
));
5812 -- Second pass: use the iterators to generate the elements of the
5813 -- aggregate. Insertion index starts at Index_Type'First. We
5814 -- assume that the second evaluation of each iterator generates
5815 -- the same number of elements as the first pass, and consider
5816 -- that the execution is erroneous (even if the RM does not state
5817 -- this explicitly) if the number of elements generated differs
5818 -- between first and second pass.
5820 Assoc
:= First
(Component_Associations
(N
));
5822 -- Initialize insertion position to first array component.
5824 Insertion_Code
:= New_List
(
5825 Make_Object_Declaration
(Loc
,
5826 Defining_Identifier
=> Index_Id
,
5827 Object_Definition
=>
5828 New_Occurrence_Of
(Index_Type
, Loc
),
5830 Make_Attribute_Reference
(Loc
,
5831 Prefix
=> New_Occurrence_Of
(Index_Type
, Loc
),
5832 Attribute_Name
=> Name_First
)));
5834 while Present
(Assoc
) loop
5835 Iter
:= Iterator_Specification
(Assoc
);
5836 New_Comp
:= Make_Assignment_Statement
(Loc
,
5838 Make_Indexed_Component
(Loc
,
5839 Prefix
=> New_Occurrence_Of
(TmpE
, Loc
),
5841 New_List
(New_Occurrence_Of
(Index_Id
, Loc
))),
5842 Expression
=> Copy_Separate_Tree
(Expression
(Assoc
)));
5844 -- Advance index position for insertion.
5846 Incr
:= Make_Assignment_Statement
(Loc
,
5847 Name
=> New_Occurrence_Of
(Index_Id
, Loc
),
5849 Make_Attribute_Reference
(Loc
,
5851 New_Occurrence_Of
(Index_Type
, Loc
),
5852 Attribute_Name
=> Name_Succ
,
5854 New_List
(New_Occurrence_Of
(Index_Id
, Loc
))));
5856 -- Add guard to skip last increment when upper bound is reached.
5858 Incr
:= Make_If_Statement
(Loc
,
5861 Left_Opnd
=> New_Occurrence_Of
(Index_Id
, Loc
),
5863 Make_Attribute_Reference
(Loc
,
5864 Prefix
=> New_Occurrence_Of
(Index_Type
, Loc
),
5865 Attribute_Name
=> Name_Last
)),
5866 Then_Statements
=> New_List
(Incr
));
5868 One_Loop
:= Make_Implicit_Loop_Statement
(N
,
5870 Make_Iteration_Scheme
(Loc
,
5871 Iterator_Specification
=> Copy_Separate_Tree
(Iter
)),
5872 Statements
=> New_List
(New_Comp
, Incr
));
5874 Append
(One_Loop
, Insertion_Code
);
5878 Insert_Actions
(N
, Insertion_Code
);
5880 -- Depending on context this may not work for build-in-place
5883 Rewrite
(N
, New_Occurrence_Of
(TmpE
, Loc
));
5885 end Two_Pass_Aggregate_Expansion
;
5890 -- Holds the temporary aggregate value
5893 -- Holds the declaration of Tmp
5895 Aggr_Code
: List_Id
;
5896 Parent_Node
: Node_Id
;
5897 Parent_Kind
: Node_Kind
;
5899 -- Start of processing for Expand_Array_Aggregate
5902 -- Do not touch the special aggregates of attributes used for Asm calls
5904 if Is_RTE
(Ctyp
, RE_Asm_Input_Operand
)
5905 or else Is_RTE
(Ctyp
, RE_Asm_Output_Operand
)
5909 elsif Present
(Component_Associations
(N
))
5910 and then Nkind
(First
(Component_Associations
(N
))) =
5911 N_Iterated_Component_Association
5913 Present
(Iterator_Specification
(First
(Component_Associations
(N
))))
5915 Two_Pass_Aggregate_Expansion
(N
);
5918 -- Do not attempt expansion if error already detected. We may reach this
5919 -- point in spite of previous errors when compiling with -gnatq, to
5920 -- force all possible errors (this is the usual ACATS mode).
5922 elsif Error_Posted
(N
) then
5926 -- If the semantic analyzer has determined that aggregate N will raise
5927 -- Constraint_Error at run time, then the aggregate node has been
5928 -- replaced with an N_Raise_Constraint_Error node and we should
5931 pragma Assert
(not Raises_Constraint_Error
(N
));
5935 -- Check that the index range defined by aggregate bounds is
5936 -- compatible with corresponding index subtype.
5938 Index_Compatibility_Check
: declare
5939 Aggr_Index_Range
: Node_Id
:= First_Index
(Typ
);
5940 -- The current aggregate index range
5942 Index_Constraint
: Node_Id
:= First_Index
(Etype
(Typ
));
5943 -- The corresponding index constraint against which we have to
5944 -- check the above aggregate index range.
5947 Compute_Others_Present
(N
, 1);
5949 for J
in 1 .. Aggr_Dimension
loop
5950 -- There is no need to emit a check if an others choice is present
5951 -- for this array aggregate dimension since in this case one of
5952 -- N's subaggregates has taken its bounds from the context and
5953 -- these bounds must have been checked already. In addition all
5954 -- subaggregates corresponding to the same dimension must all have
5955 -- the same bounds (checked in (c) below).
5957 if not Range_Checks_Suppressed
(Etype
(Index_Constraint
))
5958 and then not Others_Present
(J
)
5960 -- We don't use Checks.Apply_Range_Check here because it emits
5961 -- a spurious check. Namely it checks that the range defined by
5962 -- the aggregate bounds is nonempty. But we know this already
5965 Check_Bounds
(Aggr_Index_Range
, Index_Constraint
);
5968 -- Save the low and high bounds of the aggregate index as well as
5969 -- the index type for later use in checks (b) and (c) below.
5972 (Aggr_Index_Range
, L
=> Aggr_Low
(J
), H
=> Aggr_High
(J
));
5974 Aggr_Index_Typ
(J
) := Etype
(Index_Constraint
);
5976 Next_Index
(Aggr_Index_Range
);
5977 Next_Index
(Index_Constraint
);
5979 end Index_Compatibility_Check
;
5983 -- If an others choice is present check that no aggregate index is
5984 -- outside the bounds of the index constraint.
5986 Others_Check
(N
, 1);
5990 -- For multidimensional arrays make sure that all subaggregates
5991 -- corresponding to the same dimension have the same bounds.
5993 if Aggr_Dimension
> 1 then
5994 Check_Same_Aggr_Bounds
(N
, 1);
5999 -- If we have a default component value, or simple initialization is
6000 -- required for the component type, then we replace <> in component
6001 -- associations by the required default value.
6004 Default_Val
: Node_Id
;
6008 if (Present
(Default_Aspect_Component_Value
(Typ
))
6009 or else Needs_Simple_Initialization
(Ctyp
))
6010 and then Present
(Component_Associations
(N
))
6012 Assoc
:= First
(Component_Associations
(N
));
6013 while Present
(Assoc
) loop
6014 if Nkind
(Assoc
) = N_Component_Association
6015 and then Box_Present
(Assoc
)
6017 Set_Box_Present
(Assoc
, False);
6019 if Present
(Default_Aspect_Component_Value
(Typ
)) then
6020 Default_Val
:= Default_Aspect_Component_Value
(Typ
);
6022 Default_Val
:= Get_Simple_Init_Val
(Ctyp
, N
);
6025 Set_Expression
(Assoc
, New_Copy_Tree
(Default_Val
));
6026 Analyze_And_Resolve
(Expression
(Assoc
), Ctyp
);
6036 -- Here we test for is packed array aggregate that we can handle at
6037 -- compile time. If so, return with transformation done. Note that we do
6038 -- this even if the aggregate is nested, because once we have done this
6039 -- processing, there is no more nested aggregate.
6041 if Packed_Array_Aggregate_Handled
(N
) then
6045 -- At this point we try to convert to positional form
6047 Convert_To_Positional
(N
);
6049 -- If the result is no longer an aggregate (e.g. it may be a string
6050 -- literal, or a temporary which has the needed value), then we are
6051 -- done, since there is no longer a nested aggregate.
6053 if Nkind
(N
) /= N_Aggregate
then
6056 -- We are also done if the result is an analyzed aggregate, indicating
6057 -- that Convert_To_Positional succeeded and reanalyzed the rewritten
6060 elsif Analyzed
(N
) and then Is_Rewrite_Substitution
(N
) then
6064 -- If all aggregate components are compile-time known and the aggregate
6065 -- has been flattened, nothing left to do. The same occurs if the
6066 -- aggregate is used to initialize the components of a statically
6067 -- allocated dispatch table.
6069 if Compile_Time_Known_Aggregate
(N
)
6070 or else Is_Static_Dispatch_Table_Aggregate
(N
)
6072 Set_Expansion_Delayed
(N
, False);
6076 -- Now see if back end processing is possible
6078 if Backend_Processing_Possible
(N
) then
6080 -- If the aggregate is static but the constraints are not, build
6081 -- a static subtype for the aggregate, so that Gigi can place it
6082 -- in static memory. Perform an unchecked_conversion to the non-
6083 -- static type imposed by the context.
6086 Itype
: constant Entity_Id
:= Etype
(N
);
6088 Needs_Type
: Boolean := False;
6091 Index
:= First_Index
(Itype
);
6092 while Present
(Index
) loop
6093 if not Is_OK_Static_Subtype
(Etype
(Index
)) then
6102 Build_Constrained_Type
(Positional
=> True);
6103 Rewrite
(N
, Unchecked_Convert_To
(Itype
, N
));
6113 -- Delay expansion for nested aggregates: it will be taken care of when
6114 -- the parent aggregate is expanded, excluding container aggregates as
6115 -- these are transformed into subprogram calls later.
6117 Parent_Node
:= Parent
(N
);
6118 Parent_Kind
:= Nkind
(Parent_Node
);
6120 if Parent_Kind
= N_Qualified_Expression
then
6121 Parent_Node
:= Parent
(Parent_Node
);
6122 Parent_Kind
:= Nkind
(Parent_Node
);
6125 if ((Parent_Kind
= N_Component_Association
6126 or else Parent_Kind
= N_Aggregate
6127 or else Parent_Kind
= N_Extension_Aggregate
)
6128 and then not Is_Container_Aggregate
(Parent_Node
))
6129 or else (Parent_Kind
= N_Object_Declaration
6130 and then (Needs_Finalization
(Typ
)
6131 or else Is_Special_Return_Object
6132 (Defining_Identifier
(Parent_Node
))))
6133 or else (Parent_Kind
= N_Assignment_Statement
6134 and then Inside_Init_Proc
)
6136 Set_Expansion_Delayed
(N
, not Static_Array_Aggregate
(N
));
6142 -- Check whether in-place aggregate expansion is possible
6144 -- For object declarations we build the aggregate in place, unless
6145 -- the array is bit-packed.
6147 -- For assignments we do the assignment in place if all the component
6148 -- associations have compile-time known values, or are default-
6149 -- initialized limited components, e.g. tasks. For other cases we
6150 -- create a temporary. A full analysis for safety of in-place assignment
6153 -- For allocators we assign to the designated object in place if the
6154 -- aggregate meets the same conditions as other in-place assignments.
6155 -- In this case the aggregate may not come from source but was created
6156 -- for default initialization, e.g. with Initialize_Scalars.
6158 if Requires_Transient_Scope
(Typ
) then
6159 Establish_Transient_Scope
(N
, Manage_Sec_Stack
=> False);
6162 -- An array of limited components is built in place
6164 if Is_Limited_Type
(Typ
) then
6165 Maybe_In_Place_OK
:= True;
6167 elsif Has_Default_Init_Comps
(N
) then
6168 Maybe_In_Place_OK
:= False;
6170 elsif Is_Bit_Packed_Array
(Typ
)
6171 or else Has_Controlled_Component
(Typ
)
6173 Maybe_In_Place_OK
:= False;
6175 elsif Parent_Kind
= N_Assignment_Statement
then
6176 Maybe_In_Place_OK
:=
6177 In_Place_Assign_OK
(N
, Get_Base_Object
(Name
(Parent_Node
)));
6179 elsif Parent_Kind
= N_Allocator
then
6180 Maybe_In_Place_OK
:= In_Place_Assign_OK
(N
);
6183 Maybe_In_Place_OK
:= False;
6186 -- If this is an array of tasks, it will be expanded into build-in-place
6187 -- assignments. Build an activation chain for the tasks now.
6189 if Has_Task
(Typ
) then
6190 Build_Activation_Chain_Entity
(N
);
6193 -- Perform in-place expansion of aggregate in an object declaration.
6194 -- Note: actions generated for the aggregate will be captured in an
6195 -- expression-with-actions statement so that they can be transferred
6196 -- to freeze actions later if there is an address clause for the
6197 -- object. (Note: we don't use a block statement because this would
6198 -- cause generated freeze nodes to be elaborated in the wrong scope).
6200 -- Arrays of limited components must be built in place. The code
6201 -- previously excluded controlled components but this is an old
6202 -- oversight: the rules in 7.6 (17) are clear.
6204 if Comes_From_Source
(Parent_Node
)
6205 and then Parent_Kind
= N_Object_Declaration
6206 and then Present
(Expression
(Parent_Node
))
6208 Must_Slide
(N
, Etype
(Defining_Identifier
(Parent_Node
)), Typ
)
6209 and then not Is_Bit_Packed_Array
(Typ
)
6211 In_Place_Assign_OK_For_Declaration
:= True;
6212 Tmp
:= Defining_Identifier
(Parent_Node
);
6213 Set_No_Initialization
(Parent_Node
);
6214 Set_Expression
(Parent_Node
, Empty
);
6216 -- Set kind and type of the entity, for use in the analysis
6217 -- of the subsequent assignments. If the nominal type is not
6218 -- constrained, build a subtype from the known bounds of the
6219 -- aggregate. If the declaration has a subtype mark, use it,
6220 -- otherwise use the itype of the aggregate.
6222 Mutate_Ekind
(Tmp
, E_Variable
);
6224 if not Is_Constrained
(Typ
) then
6225 Build_Constrained_Type
(Positional
=> False);
6227 elsif Is_Entity_Name
(Object_Definition
(Parent_Node
))
6228 and then Is_Constrained
(Entity
(Object_Definition
(Parent_Node
)))
6230 Set_Etype
(Tmp
, Entity
(Object_Definition
(Parent_Node
)));
6233 Set_Size_Known_At_Compile_Time
(Typ
, False);
6234 Set_Etype
(Tmp
, Typ
);
6237 elsif Maybe_In_Place_OK
and then Parent_Kind
= N_Allocator
then
6238 Set_Expansion_Delayed
(N
);
6241 -- Limited arrays in return statements are expanded when
6242 -- enclosing construct is expanded.
6244 elsif Maybe_In_Place_OK
6245 and then Parent_Kind
= N_Simple_Return_Statement
6247 Set_Expansion_Delayed
(N
);
6250 -- In the remaining cases the aggregate appears in the RHS of an
6251 -- assignment, which may be part of the expansion of an object
6252 -- declaration. If the aggregate is an actual in a call, itself
6253 -- possibly in a RHS, building it in the target is not possible.
6255 elsif Maybe_In_Place_OK
6256 and then Nkind
(Parent_Node
) not in N_Subprogram_Call
6257 and then Safe_Left_Hand_Side
(Name
(Parent_Node
))
6259 Tmp
:= Name
(Parent_Node
);
6261 if Etype
(Tmp
) /= Etype
(N
) then
6262 Apply_Length_Check
(N
, Etype
(Tmp
));
6264 if Nkind
(N
) = N_Raise_Constraint_Error
then
6266 -- Static error, nothing further to expand
6272 -- If a slice assignment has an aggregate with a single others_choice,
6273 -- the assignment can be done in place even if bounds are not static,
6274 -- by converting it into a loop over the discrete range of the slice.
6276 elsif Maybe_In_Place_OK
6277 and then Nkind
(Name
(Parent_Node
)) = N_Slice
6278 and then Is_Others_Aggregate
(N
)
6280 Tmp
:= Name
(Parent_Node
);
6282 -- Set type of aggregate to be type of lhs in assignment, in order
6283 -- to suppress redundant length checks.
6285 Set_Etype
(N
, Etype
(Tmp
));
6289 -- In-place aggregate expansion is not possible
6292 Maybe_In_Place_OK
:= False;
6293 Tmp
:= Make_Temporary
(Loc
, 'A', N
);
6295 Make_Object_Declaration
(Loc
,
6296 Defining_Identifier
=> Tmp
,
6297 Object_Definition
=> New_Occurrence_Of
(Typ
, Loc
));
6298 Set_No_Initialization
(Tmp_Decl
, True);
6300 -- If we are within a loop, the temporary will be pushed on the
6301 -- stack at each iteration. If the aggregate is the expression
6302 -- for an allocator, it will be immediately copied to the heap
6303 -- and can be reclaimed at once. We create a transient scope
6304 -- around the aggregate for this purpose.
6306 if Ekind
(Current_Scope
) = E_Loop
6307 and then Parent_Kind
= N_Allocator
6309 Establish_Transient_Scope
(N
, Manage_Sec_Stack
=> False);
6311 -- If the parent is an assignment for which no controlled actions
6312 -- should take place, prevent the temporary from being finalized.
6314 elsif Parent_Kind
= N_Assignment_Statement
6315 and then No_Ctrl_Actions
(Parent_Node
)
6317 Mutate_Ekind
(Tmp
, E_Variable
);
6318 Set_Is_Ignored_Transient
(Tmp
);
6321 Insert_Action
(N
, Tmp_Decl
);
6324 -- Construct and insert the aggregate code. We can safely suppress index
6325 -- checks because this code is guaranteed not to raise CE on index
6326 -- checks. However we should *not* suppress all checks.
6332 if Nkind
(Tmp
) = N_Defining_Identifier
then
6333 Target
:= New_Occurrence_Of
(Tmp
, Loc
);
6336 if Has_Default_Init_Comps
(N
)
6337 and then not Maybe_In_Place_OK
6339 -- Ada 2005 (AI-287): This case has not been analyzed???
6341 raise Program_Error
;
6344 -- Name in assignment is explicit dereference
6346 Target
:= New_Copy
(Tmp
);
6349 -- If we are to generate an in-place assignment for a declaration or
6350 -- an assignment statement, and the assignment can be done directly
6351 -- by the back end, then do not expand further.
6353 -- ??? We can also do that if in-place expansion is not possible but
6354 -- then we could go into an infinite recursion.
6356 if (In_Place_Assign_OK_For_Declaration
or else Maybe_In_Place_OK
)
6357 and then not CodePeer_Mode
6358 and then not Modify_Tree_For_C
6359 and then not Possible_Bit_Aligned_Component
(Target
)
6360 and then not Is_Possibly_Unaligned_Slice
(Target
)
6361 and then Aggr_Assignment_OK_For_Backend
(N
)
6364 -- In the case of an assignment using an access with the
6365 -- Designated_Storage_Model aspect with a Copy_To procedure,
6366 -- insert a temporary and have the back end handle the assignment
6367 -- to it. Copy the result to the original target.
6369 if Parent_Kind
= N_Assignment_Statement
6370 and then Nkind
(Name
(Parent_Node
)) = N_Explicit_Dereference
6371 and then Has_Designated_Storage_Model_Aspect
6372 (Etype
(Prefix
(Name
(Parent_Node
))))
6373 and then Present
(Storage_Model_Copy_To
6374 (Storage_Model_Object
6375 (Etype
(Prefix
(Name
(Parent_Node
))))))
6377 Aggr_Code
:= Build_Assignment_With_Temporary
6378 (Target
, Typ
, New_Copy_Tree
(N
));
6381 if Maybe_In_Place_OK
then
6385 Aggr_Code
:= New_List
(
6386 Make_Assignment_Statement
(Loc
,
6388 Expression
=> New_Copy_Tree
(N
)));
6393 Build_Array_Aggr_Code
(N
,
6395 Index
=> First_Index
(Typ
),
6397 Scalar_Comp
=> Is_Scalar_Type
(Ctyp
));
6400 -- Save the last assignment statement associated with the aggregate
6401 -- when building a controlled object. This reference is utilized by
6402 -- the finalization machinery when marking an object as successfully
6405 if Needs_Finalization
(Typ
)
6406 and then Is_Entity_Name
(Target
)
6407 and then Present
(Entity
(Target
))
6408 and then Ekind
(Entity
(Target
)) in E_Constant | E_Variable
6410 Set_Last_Aggregate_Assignment
(Entity
(Target
), Last
(Aggr_Code
));
6414 -- If the aggregate is the expression in a declaration, the expanded
6415 -- code must be inserted after it. The defining entity might not come
6416 -- from source if this is part of an inlined body, but the declaration
6418 -- The test below looks very specialized and kludgy???
6420 if Comes_From_Source
(Tmp
)
6422 (Nkind
(Parent
(N
)) = N_Object_Declaration
6423 and then Comes_From_Source
(Parent
(N
))
6424 and then Tmp
= Defining_Entity
(Parent
(N
)))
6426 if Parent_Kind
/= N_Object_Declaration
or else Is_Frozen
(Tmp
) then
6427 Insert_Actions_After
(Parent_Node
, Aggr_Code
);
6430 Comp_Stmt
: constant Node_Id
:=
6431 Make_Compound_Statement
6432 (Sloc
(Parent_Node
), Actions
=> Aggr_Code
);
6434 Insert_Action_After
(Parent_Node
, Comp_Stmt
);
6435 Set_Initialization_Statements
(Tmp
, Comp_Stmt
);
6439 Insert_Actions
(N
, Aggr_Code
);
6442 -- If the aggregate has been assigned in place, remove the original
6445 if Parent_Kind
= N_Assignment_Statement
and then Maybe_In_Place_OK
then
6446 Rewrite
(Parent_Node
, Make_Null_Statement
(Loc
));
6448 -- Or else, if a temporary was created, replace the aggregate with it
6450 elsif Parent_Kind
/= N_Object_Declaration
6451 or else Tmp
/= Defining_Identifier
(Parent_Node
)
6453 Rewrite
(N
, New_Occurrence_Of
(Tmp
, Loc
));
6454 Analyze_And_Resolve
(N
, Typ
);
6456 end Expand_Array_Aggregate
;
6458 ------------------------
6459 -- Expand_N_Aggregate --
6460 ------------------------
6462 procedure Expand_N_Aggregate
(N
: Node_Id
) is
6463 T
: constant Entity_Id
:= Etype
(N
);
6465 -- Record aggregate case
6467 if Is_Record_Type
(T
)
6468 and then not Is_Private_Type
(T
)
6469 and then not Is_Homogeneous_Aggregate
(N
)
6471 Expand_Record_Aggregate
(N
);
6473 elsif Has_Aspect
(T
, Aspect_Aggregate
) then
6474 Expand_Container_Aggregate
(N
);
6476 -- Array aggregate case
6479 -- A special case, if we have a string subtype with bounds 1 .. N,
6480 -- where N is known at compile time, and the aggregate is of the
6481 -- form (others => 'x'), with a single choice and no expressions,
6482 -- and N is less than 80 (an arbitrary limit for now), then replace
6483 -- the aggregate by the equivalent string literal (but do not mark
6484 -- it as static since it is not).
6486 -- Note: this entire circuit is redundant with respect to code in
6487 -- Expand_Array_Aggregate that collapses others choices to positional
6488 -- form, but there are two problems with that circuit:
6490 -- a) It is limited to very small cases due to ill-understood
6491 -- interactions with bootstrapping. That limit is removed by
6492 -- use of the No_Implicit_Loops restriction.
6494 -- b) It incorrectly ends up with the resulting expressions being
6495 -- considered static when they are not. For example, the
6496 -- following test should fail:
6498 -- pragma Restrictions (No_Implicit_Loops);
6499 -- package NonSOthers4 is
6500 -- B : constant String (1 .. 6) := (others => 'A');
6501 -- DH : constant String (1 .. 8) := B & "BB";
6503 -- pragma Export (C, X, Link_Name => DH);
6506 -- But it succeeds (DH looks static to pragma Export)
6508 -- To be sorted out ???
6510 if Present
(Component_Associations
(N
)) then
6512 CA
: constant Node_Id
:= First
(Component_Associations
(N
));
6513 MX
: constant := 80;
6517 and then Nkind
(First
(Choice_List
(CA
))) = N_Others_Choice
6518 and then Nkind
(Expression
(CA
)) = N_Character_Literal
6519 and then No
(Expressions
(N
))
6522 X
: constant Node_Id
:= First_Index
(T
);
6523 EC
: constant Node_Id
:= Expression
(CA
);
6524 CV
: constant Uint
:= Char_Literal_Value
(EC
);
6525 CC
: constant Char_Code
:= UI_To_CC
(CV
);
6528 if Nkind
(X
) = N_Range
6529 and then Compile_Time_Known_Value
(Low_Bound
(X
))
6530 and then Expr_Value
(Low_Bound
(X
)) = 1
6531 and then Compile_Time_Known_Value
(High_Bound
(X
))
6534 Hi
: constant Uint
:= Expr_Value
(High_Bound
(X
));
6540 for J
in 1 .. UI_To_Int
(Hi
) loop
6541 Store_String_Char
(CC
);
6545 Make_String_Literal
(Sloc
(N
),
6546 Strval
=> End_String
));
6548 if In_Character_Range
(CC
) then
6550 elsif In_Wide_Character_Range
(CC
) then
6551 Set_Has_Wide_Character
(N
);
6553 Set_Has_Wide_Wide_Character
(N
);
6556 Analyze_And_Resolve
(N
, T
);
6557 Set_Is_Static_Expression
(N
, False);
6567 -- Not that special case, so normal expansion of array aggregate
6569 Expand_Array_Aggregate
(N
);
6573 when RE_Not_Available
=>
6575 end Expand_N_Aggregate
;
6577 --------------------------------
6578 -- Expand_Container_Aggregate --
6579 --------------------------------
6581 procedure Expand_Container_Aggregate
(N
: Node_Id
) is
6582 Loc
: constant Source_Ptr
:= Sloc
(N
);
6583 Typ
: constant Entity_Id
:= Etype
(N
);
6584 Asp
: constant Node_Id
:= Find_Value_Of_Aspect
(Typ
, Aspect_Aggregate
);
6586 Empty_Subp
: Node_Id
:= Empty
;
6587 Add_Named_Subp
: Node_Id
:= Empty
;
6588 Add_Unnamed_Subp
: Node_Id
:= Empty
;
6589 New_Indexed_Subp
: Node_Id
:= Empty
;
6590 Assign_Indexed_Subp
: Node_Id
:= Empty
;
6592 Aggr_Code
: constant List_Id
:= New_List
;
6593 Temp
: constant Entity_Id
:= Make_Temporary
(Loc
, 'C', N
);
6598 Init_Stat
: Node_Id
;
6601 -- The following are used when the size of the aggregate is not
6602 -- static and requires a dynamic evaluation.
6604 Siz_Exp
: Node_Id
:= Empty
;
6605 Count_Type
: Entity_Id
;
6607 function Aggregate_Size
return Int
;
6608 -- Compute number of entries in aggregate, including choices
6609 -- that cover a range or subtype, as well as iterated constructs.
6610 -- Return -1 if the size is not known statically, in which case
6611 -- allocate a default size for the aggregate, or build an expression
6612 -- to estimate the size dynamically.
6614 function Build_Siz_Exp
(Comp
: Node_Id
) return Int
;
6615 -- When the aggregate contains a single Iterated_Component_Association
6616 -- or Element_Association with non-static bounds, build an expression
6617 -- to be used as the allocated size of the container. This may be an
6618 -- overestimate if a filter is present, but is a safe approximation.
6619 -- If bounds are dynamic the aggregate is created in two passes, and
6620 -- the first generates a loop for the sole purpose of computing the
6621 -- number of elements that will be generated on the second pass.
6623 procedure Expand_Iterated_Component
(Comp
: Node_Id
);
6624 -- Handle iterated_component_association and iterated_Element
6625 -- association by generating a loop over the specified range,
6626 -- given either by a loop parameter specification or an iterator
6629 --------------------
6630 -- Aggregate_Size --
6631 --------------------
6633 function Aggregate_Size
return Int
is
6639 procedure Add_Range_Size
;
6640 -- Compute number of components specified by a component association
6641 -- given by a range or subtype name.
6643 --------------------
6644 -- Add_Range_Size --
6645 --------------------
6647 procedure Add_Range_Size
is
6649 -- The bounds of the discrete range are integers or enumeration
6652 if Nkind
(Lo
) = N_Integer_Literal
then
6653 Siz
:= Siz
+ UI_To_Int
(Intval
(Hi
))
6654 - UI_To_Int
(Intval
(Lo
)) + 1;
6656 Siz
:= Siz
+ UI_To_Int
(Enumeration_Pos
(Hi
))
6657 - UI_To_Int
(Enumeration_Pos
(Lo
)) + 1;
6662 -- Aggregate is either all positional or all named
6664 Siz
:= List_Length
(Expressions
(N
));
6666 if Present
(Component_Associations
(N
)) then
6667 Comp
:= First
(Component_Associations
(N
));
6668 -- If there is a single component association it can be
6669 -- an iterated component with dynamic bounds or an element
6670 -- iterator over an iterable object. If it is an array
6671 -- we can use the attribute Length to get its size;
6672 -- for a predefined container the function Length plays
6673 -- the same role. There is no available mechanism for
6674 -- user-defined containers. For now we treat all of these
6677 if List_Length
(Component_Associations
(N
)) = 1
6678 and then Nkind
(Comp
) in N_Iterated_Component_Association |
6679 N_Iterated_Element_Association
6681 return Build_Siz_Exp
(Comp
);
6684 -- Otherwise all associations must specify static sizes.
6686 while Present
(Comp
) loop
6687 Choice
:= First
(Choice_List
(Comp
));
6689 while Present
(Choice
) loop
6692 if Nkind
(Choice
) = N_Range
then
6693 Lo
:= Low_Bound
(Choice
);
6694 Hi
:= High_Bound
(Choice
);
6697 elsif Is_Entity_Name
(Choice
)
6698 and then Is_Type
(Entity
(Choice
))
6700 Lo
:= Type_Low_Bound
(Entity
(Choice
));
6701 Hi
:= Type_High_Bound
(Entity
(Choice
));
6707 New_Copy_Tree
(Hi
)));
6710 -- Single choice (syntax excludes a subtype
6729 function Build_Siz_Exp
(Comp
: Node_Id
) return Int
is
6732 if Nkind
(Comp
) = N_Range
then
6733 Lo
:= Low_Bound
(Comp
);
6734 Hi
:= High_Bound
(Comp
);
6738 -- Compute static size when possible.
6740 if Is_Static_Expression
(Lo
)
6741 and then Is_Static_Expression
(Hi
)
6743 if Nkind
(Lo
) = N_Integer_Literal
then
6744 Siz
:= UI_To_Int
(Intval
(Hi
)) - UI_To_Int
(Intval
(Lo
)) + 1;
6746 Siz
:= UI_To_Int
(Enumeration_Pos
(Hi
))
6747 - UI_To_Int
(Enumeration_Pos
(Lo
)) + 1;
6753 Make_Op_Add
(Sloc
(Comp
),
6755 Make_Op_Subtract
(Sloc
(Comp
),
6756 Left_Opnd
=> New_Copy_Tree
(Hi
),
6757 Right_Opnd
=> New_Copy_Tree
(Lo
)),
6759 Make_Integer_Literal
(Loc
, 1));
6763 elsif Nkind
(Comp
) = N_Iterated_Component_Association
then
6764 return Build_Siz_Exp
(First
(Discrete_Choices
(Comp
)));
6766 elsif Nkind
(Comp
) = N_Iterated_Element_Association
then
6769 -- ??? Need to create code for a loop and add to generated code,
6770 -- as is done for array aggregates with iterated element
6771 -- associations, instead of using Append operations.
6778 -------------------------------
6779 -- Expand_Iterated_Component --
6780 -------------------------------
6782 procedure Expand_Iterated_Component
(Comp
: Node_Id
) is
6783 Expr
: constant Node_Id
:= Expression
(Comp
);
6785 Key_Expr
: Node_Id
:= Empty
;
6786 Loop_Id
: Entity_Id
;
6788 L_Iteration_Scheme
: Node_Id
;
6789 Loop_Stat
: Node_Id
;
6794 if Nkind
(Comp
) = N_Iterated_Element_Association
then
6795 Key_Expr
:= Key_Expression
(Comp
);
6797 -- We create a new entity as loop identifier in all cases,
6798 -- as is done for generated loops elsewhere, as the loop
6799 -- structure has been previously analyzed.
6801 if Present
(Iterator_Specification
(Comp
)) then
6803 -- Either an Iterator_Specification or a Loop_Parameter_
6804 -- Specification is present.
6806 L_Iteration_Scheme
:=
6807 Make_Iteration_Scheme
(Loc
,
6808 Iterator_Specification
=> Iterator_Specification
(Comp
));
6810 Make_Defining_Identifier
(Loc
,
6811 Chars
=> Chars
(Defining_Identifier
6812 (Iterator_Specification
(Comp
))));
6813 Set_Defining_Identifier
6814 (Iterator_Specification
(L_Iteration_Scheme
), Loop_Id
);
6817 L_Iteration_Scheme
:=
6818 Make_Iteration_Scheme
(Loc
,
6819 Loop_Parameter_Specification
=>
6820 Loop_Parameter_Specification
(Comp
));
6822 Make_Defining_Identifier
(Loc
,
6823 Chars
=> Chars
(Defining_Identifier
6824 (Loop_Parameter_Specification
(Comp
))));
6825 Set_Defining_Identifier
6826 (Loop_Parameter_Specification
6827 (L_Iteration_Scheme
), Loop_Id
);
6831 -- Iterated_Component_Association.
6833 if Present
(Iterator_Specification
(Comp
)) then
6835 Make_Defining_Identifier
(Loc
,
6836 Chars
=> Chars
(Defining_Identifier
6837 (Iterator_Specification
(Comp
))));
6838 L_Iteration_Scheme
:=
6839 Make_Iteration_Scheme
(Loc
,
6840 Iterator_Specification
=> Iterator_Specification
(Comp
));
6843 -- Loop_Parameter_Specification is parsed with a choice list.
6844 -- where the range is the first (and only) choice.
6847 Make_Defining_Identifier
(Loc
,
6848 Chars
=> Chars
(Defining_Identifier
(Comp
)));
6849 L_Range
:= Relocate_Node
(First
(Discrete_Choices
(Comp
)));
6851 L_Iteration_Scheme
:=
6852 Make_Iteration_Scheme
(Loc
,
6853 Loop_Parameter_Specification
=>
6854 Make_Loop_Parameter_Specification
(Loc
,
6855 Defining_Identifier
=> Loop_Id
,
6856 Discrete_Subtype_Definition
=> L_Range
));
6860 -- Build insertion statement. For a positional aggregate, only the
6861 -- expression is needed. For a named aggregate, the loop variable,
6862 -- whose type is that of the key, is an additional parameter for
6863 -- the insertion operation.
6864 -- If a Key_Expression is present, it serves as the additional
6865 -- parameter. Otherwise the key is given by the loop parameter
6868 if Present
(Add_Unnamed_Subp
)
6869 and then No
(Add_Named_Subp
)
6872 (Make_Procedure_Call_Statement
(Loc
,
6873 Name
=> New_Occurrence_Of
(Entity
(Add_Unnamed_Subp
), Loc
),
6874 Parameter_Associations
=>
6875 New_List
(New_Occurrence_Of
(Temp
, Loc
),
6876 New_Copy_Tree
(Expr
))));
6878 -- Named or indexed aggregate, for which a key is present,
6879 -- possibly with a specified key_expression.
6881 if Present
(Key_Expr
) then
6882 Params
:= New_List
(New_Occurrence_Of
(Temp
, Loc
),
6883 New_Copy_Tree
(Key_Expr
),
6884 New_Copy_Tree
(Expr
));
6886 Params
:= New_List
(New_Occurrence_Of
(Temp
, Loc
),
6887 New_Occurrence_Of
(Loop_Id
, Loc
),
6888 New_Copy_Tree
(Expr
));
6892 (Make_Procedure_Call_Statement
(Loc
,
6893 Name
=> New_Occurrence_Of
(Entity
(Add_Named_Subp
), Loc
),
6894 Parameter_Associations
=> Params
));
6897 Loop_Stat
:= Make_Implicit_Loop_Statement
6899 Identifier
=> Empty
,
6900 Iteration_Scheme
=> L_Iteration_Scheme
,
6901 Statements
=> Stats
);
6902 Append
(Loop_Stat
, Aggr_Code
);
6904 end Expand_Iterated_Component
;
6906 -- Start of processing for Expand_Container_Aggregate
6909 Parse_Aspect_Aggregate
(Asp
,
6910 Empty_Subp
, Add_Named_Subp
, Add_Unnamed_Subp
,
6911 New_Indexed_Subp
, Assign_Indexed_Subp
);
6913 -- The constructor for bounded containers is a function with
6914 -- a parameter that sets the size of the container. If the
6915 -- size cannot be determined statically we use a default value
6916 -- or a dynamic expression.
6918 Siz
:= Aggregate_Size
;
6920 if Ekind
(Entity
(Empty_Subp
)) = E_Function
6921 and then Present
(First_Formal
(Entity
(Empty_Subp
)))
6923 Default
:= Default_Value
(First_Formal
(Entity
(Empty_Subp
)));
6925 -- If aggregate size is not static, we can use default value
6926 -- of formal parameter for allocation. We assume that this
6927 -- (implementation-dependent) value is static, even though
6928 -- the AI does not require it.
6930 -- Create declaration for size: a constant literal in the simple
6931 -- case, an expression if iterated component associations may be
6932 -- involved, the default otherwise.
6934 Count_Type
:= Etype
(First_Formal
(Entity
(Empty_Subp
)));
6936 if No
(Siz_Exp
) then
6937 Siz
:= UI_To_Int
(Intval
(Default
));
6938 Siz_Exp
:= Make_Integer_Literal
(Loc
, Siz
);
6941 Siz_Exp
:= Make_Type_Conversion
(Loc
,
6943 New_Occurrence_Of
(Count_Type
, Loc
),
6944 Expression
=> Siz_Exp
);
6948 Siz_Exp
:= Make_Integer_Literal
(Loc
, Siz
);
6951 Siz_Decl
:= Make_Object_Declaration
(Loc
,
6952 Defining_Identifier
=> Make_Temporary
(Loc
, 'S', N
),
6953 Object_Definition
=>
6954 New_Occurrence_Of
(Count_Type
, Loc
),
6955 Expression
=> Siz_Exp
);
6956 Append
(Siz_Decl
, Aggr_Code
);
6958 if Nkind
(Siz_Exp
) = N_Integer_Literal
then
6960 Make_Object_Declaration
(Loc
,
6961 Defining_Identifier
=> Temp
,
6962 Object_Definition
=> New_Occurrence_Of
(Typ
, Loc
),
6963 Expression
=> Make_Function_Call
(Loc
,
6964 Name
=> New_Occurrence_Of
(Entity
(Empty_Subp
), Loc
),
6965 Parameter_Associations
=>
6968 (Defining_Identifier
(Siz_Decl
), Loc
))));
6972 Make_Object_Declaration
(Loc
,
6973 Defining_Identifier
=> Temp
,
6974 Object_Definition
=> New_Occurrence_Of
(Typ
, Loc
),
6975 Expression
=> Make_Function_Call
(Loc
,
6977 New_Occurrence_Of
(Entity
(New_Indexed_Subp
), Loc
),
6978 Parameter_Associations
=>
6980 Make_Integer_Literal
(Loc
, 1),
6982 (Defining_Identifier
(Siz_Decl
), Loc
))));
6985 Append
(Init_Stat
, Aggr_Code
);
6987 -- Size is dynamic: Create declaration for object, and intitialize
6988 -- with a call to the null container, or an assignment to it.
6992 Make_Object_Declaration
(Loc
,
6993 Defining_Identifier
=> Temp
,
6994 Object_Definition
=> New_Occurrence_Of
(Typ
, Loc
));
6996 Insert_Action
(N
, Decl
);
6998 -- The Empty entity is either a parameterless function, or
7001 if Ekind
(Entity
(Empty_Subp
)) = E_Function
then
7002 Init_Stat
:= Make_Assignment_Statement
(Loc
,
7003 Name
=> New_Occurrence_Of
(Temp
, Loc
),
7004 Expression
=> Make_Function_Call
(Loc
,
7005 Name
=> New_Occurrence_Of
(Entity
(Empty_Subp
), Loc
)));
7008 Init_Stat
:= Make_Assignment_Statement
(Loc
,
7009 Name
=> New_Occurrence_Of
(Temp
, Loc
),
7010 Expression
=> New_Occurrence_Of
(Entity
(Empty_Subp
), Loc
));
7013 Append
(Init_Stat
, Aggr_Code
);
7016 ---------------------------
7017 -- Positional aggregate --
7018 ---------------------------
7020 -- If the aggregate is positional the aspect must include
7021 -- an Add_Unnamed subprogram.
7023 if Present
(Add_Unnamed_Subp
) then
7024 if Present
(Expressions
(N
)) then
7026 Insert
: constant Entity_Id
:= Entity
(Add_Unnamed_Subp
);
7031 Comp
:= First
(Expressions
(N
));
7032 while Present
(Comp
) loop
7033 Stat
:= Make_Procedure_Call_Statement
(Loc
,
7034 Name
=> New_Occurrence_Of
(Insert
, Loc
),
7035 Parameter_Associations
=>
7036 New_List
(New_Occurrence_Of
(Temp
, Loc
),
7037 New_Copy_Tree
(Comp
)));
7038 Append
(Stat
, Aggr_Code
);
7044 -- Indexed aggregates are handled below. Unnamed aggregates
7045 -- such as sets may include iterated component associations.
7047 if No
(New_Indexed_Subp
) then
7048 Comp
:= First
(Component_Associations
(N
));
7049 while Present
(Comp
) loop
7050 if Nkind
(Comp
) = N_Iterated_Component_Association
then
7051 Expand_Iterated_Component
(Comp
);
7057 ---------------------
7058 -- Named_Aggregate --
7059 ---------------------
7061 elsif Present
(Add_Named_Subp
) then
7063 Insert
: constant Entity_Id
:= Entity
(Add_Named_Subp
);
7067 Comp
:= First
(Component_Associations
(N
));
7069 -- Each component association may contain several choices;
7070 -- generate an insertion statement for each.
7072 while Present
(Comp
) loop
7073 if Nkind
(Comp
) in N_Iterated_Component_Association
7074 | N_Iterated_Element_Association
7076 Expand_Iterated_Component
(Comp
);
7078 Key
:= First
(Choices
(Comp
));
7080 while Present
(Key
) loop
7081 Stat
:= Make_Procedure_Call_Statement
(Loc
,
7082 Name
=> New_Occurrence_Of
(Insert
, Loc
),
7083 Parameter_Associations
=>
7084 New_List
(New_Occurrence_Of
(Temp
, Loc
),
7085 New_Copy_Tree
(Key
),
7086 New_Copy_Tree
(Expression
(Comp
))));
7087 Append
(Stat
, Aggr_Code
);
7098 -----------------------
7099 -- Indexed_Aggregate --
7100 -----------------------
7102 -- For an indexed aggregate there must be an Assigned_Indexeed
7103 -- subprogram. Note that unlike array aggregates, a container
7104 -- aggregate must be fully positional or fully indexed. In the
7105 -- first case the expansion has already taken place.
7106 -- TBA: the keys for an indexed aggregate must provide a dense
7107 -- range with no repetitions.
7109 if Present
(Assign_Indexed_Subp
)
7110 and then Present
(Component_Associations
(N
))
7113 Insert
: constant Entity_Id
:= Entity
(Assign_Indexed_Subp
);
7114 Index_Type
: constant Entity_Id
:=
7115 Etype
(Next_Formal
(First_Formal
(Insert
)));
7117 function Expand_Range_Component
7119 Expr
: Node_Id
) return Node_Id
;
7120 -- Transform a component assoication with a range into an
7121 -- explicit loop. If the choice is a subtype name, it is
7122 -- rewritten as a range with the corresponding bounds, which
7123 -- are known to be static.
7131 -----------------------------
7132 -- Expand_Raange_Component --
7133 -----------------------------
7135 function Expand_Range_Component
7137 Expr
: Node_Id
) return Node_Id
7139 Loop_Id
: constant Entity_Id
:=
7140 Make_Temporary
(Loc
, 'T');
7142 L_Iteration_Scheme
: Node_Id
;
7146 L_Iteration_Scheme
:=
7147 Make_Iteration_Scheme
(Loc
,
7148 Loop_Parameter_Specification
=>
7149 Make_Loop_Parameter_Specification
(Loc
,
7150 Defining_Identifier
=> Loop_Id
,
7151 Discrete_Subtype_Definition
=> New_Copy_Tree
(Rng
)));
7154 (Make_Procedure_Call_Statement
(Loc
,
7156 New_Occurrence_Of
(Entity
(Assign_Indexed_Subp
), Loc
),
7157 Parameter_Associations
=>
7158 New_List
(New_Occurrence_Of
(Temp
, Loc
),
7159 New_Occurrence_Of
(Loop_Id
, Loc
),
7160 New_Copy_Tree
(Expr
))));
7162 return Make_Implicit_Loop_Statement
7164 Identifier
=> Empty
,
7165 Iteration_Scheme
=> L_Iteration_Scheme
,
7166 Statements
=> Stats
);
7167 end Expand_Range_Component
;
7172 -- Modify the call to the constructor to allocate the
7173 -- required size for the aggregwte : call the provided
7174 -- constructor rather than the Empty aggregate.
7176 Index
:= Make_Op_Add
(Loc
,
7177 Left_Opnd
=> New_Copy_Tree
(Type_Low_Bound
(Index_Type
)),
7178 Right_Opnd
=> Make_Integer_Literal
(Loc
, Siz
- 1));
7180 Set_Expression
(Init_Stat
,
7181 Make_Function_Call
(Loc
,
7183 New_Occurrence_Of
(Entity
(New_Indexed_Subp
), Loc
),
7184 Parameter_Associations
=>
7186 New_Copy_Tree
(Type_Low_Bound
(Index_Type
)),
7190 if Present
(Expressions
(N
)) then
7191 Comp
:= First
(Expressions
(N
));
7193 while Present
(Comp
) loop
7195 -- Compute index position for successive components
7196 -- in the list of expressions, and use the indexed
7197 -- assignment procedure for each.
7199 Index
:= Make_Op_Add
(Loc
,
7200 Left_Opnd
=> Type_Low_Bound
(Index_Type
),
7201 Right_Opnd
=> Make_Integer_Literal
(Loc
, Pos
));
7203 Stat
:= Make_Procedure_Call_Statement
(Loc
,
7204 Name
=> New_Occurrence_Of
(Insert
, Loc
),
7205 Parameter_Associations
=>
7206 New_List
(New_Occurrence_Of
(Temp
, Loc
),
7208 New_Copy_Tree
(Comp
)));
7212 Append
(Stat
, Aggr_Code
);
7217 if Present
(Component_Associations
(N
)) then
7218 Comp
:= First
(Component_Associations
(N
));
7220 -- The choice may be a static value, or a range with
7223 while Present
(Comp
) loop
7224 if Nkind
(Comp
) = N_Component_Association
then
7225 Key
:= First
(Choices
(Comp
));
7226 while Present
(Key
) loop
7228 -- If the expression is a box, the corresponding
7229 -- component (s) is left uninitialized.
7231 if Box_Present
(Comp
) then
7234 elsif Nkind
(Key
) = N_Range
then
7236 -- Create loop for tne specified range,
7237 -- with copies of the expression.
7240 Expand_Range_Component
(Key
, Expression
(Comp
));
7243 Stat
:= Make_Procedure_Call_Statement
(Loc
,
7244 Name
=> New_Occurrence_Of
7245 (Entity
(Assign_Indexed_Subp
), Loc
),
7246 Parameter_Associations
=>
7247 New_List
(New_Occurrence_Of
(Temp
, Loc
),
7248 New_Copy_Tree
(Key
),
7249 New_Copy_Tree
(Expression
(Comp
))));
7252 Append
(Stat
, Aggr_Code
);
7259 -- Iterated component association. Discard
7260 -- positional insertion procedure.
7262 if not Present
(Iterator_Specification
(Comp
)) then
7263 Add_Named_Subp
:= Assign_Indexed_Subp
;
7264 Add_Unnamed_Subp
:= Empty
;
7267 Expand_Iterated_Component
(Comp
);
7276 Insert_Actions
(N
, Aggr_Code
);
7277 Rewrite
(N
, New_Occurrence_Of
(Temp
, Loc
));
7278 Analyze_And_Resolve
(N
, Typ
);
7279 end Expand_Container_Aggregate
;
7281 ------------------------------
7282 -- Expand_N_Delta_Aggregate --
7283 ------------------------------
7285 procedure Expand_N_Delta_Aggregate
(N
: Node_Id
) is
7286 Loc
: constant Source_Ptr
:= Sloc
(N
);
7287 Typ
: constant Entity_Id
:= Etype
(Expression
(N
));
7292 Make_Object_Declaration
(Loc
,
7293 Defining_Identifier
=> Make_Temporary
(Loc
, 'T'),
7294 Object_Definition
=> New_Occurrence_Of
(Typ
, Loc
),
7295 Expression
=> New_Copy_Tree
(Expression
(N
)));
7297 if Is_Array_Type
(Etype
(N
)) then
7298 Expand_Delta_Array_Aggregate
(N
, New_List
(Decl
));
7300 Expand_Delta_Record_Aggregate
(N
, New_List
(Decl
));
7302 end Expand_N_Delta_Aggregate
;
7304 ----------------------------------
7305 -- Expand_Delta_Array_Aggregate --
7306 ----------------------------------
7308 procedure Expand_Delta_Array_Aggregate
(N
: Node_Id
; Deltas
: List_Id
) is
7309 Loc
: constant Source_Ptr
:= Sloc
(N
);
7310 Temp
: constant Entity_Id
:= Defining_Identifier
(First
(Deltas
));
7313 function Generate_Loop
(C
: Node_Id
) return Node_Id
;
7314 -- Generate a loop containing individual component assignments for
7315 -- choices that are ranges, subtype indications, subtype names, and
7316 -- iterated component associations.
7322 function Generate_Loop
(C
: Node_Id
) return Node_Id
is
7323 Sl
: constant Source_Ptr
:= Sloc
(C
);
7327 if Nkind
(Parent
(C
)) = N_Iterated_Component_Association
then
7329 Make_Defining_Identifier
(Loc
,
7330 Chars
=> (Chars
(Defining_Identifier
(Parent
(C
)))));
7332 Ix
:= Make_Temporary
(Sl
, 'I');
7336 Make_Implicit_Loop_Statement
(C
,
7338 Make_Iteration_Scheme
(Sl
,
7339 Loop_Parameter_Specification
=>
7340 Make_Loop_Parameter_Specification
(Sl
,
7341 Defining_Identifier
=> Ix
,
7342 Discrete_Subtype_Definition
=> New_Copy_Tree
(C
))),
7344 Statements
=> New_List
(
7345 Make_Assignment_Statement
(Sl
,
7347 Make_Indexed_Component
(Sl
,
7348 Prefix
=> New_Occurrence_Of
(Temp
, Sl
),
7349 Expressions
=> New_List
(New_Occurrence_Of
(Ix
, Sl
))),
7350 Expression
=> New_Copy_Tree
(Expression
(Assoc
)))),
7351 End_Label
=> Empty
);
7358 -- Start of processing for Expand_Delta_Array_Aggregate
7361 Assoc
:= First
(Component_Associations
(N
));
7362 while Present
(Assoc
) loop
7363 Choice
:= First
(Choice_List
(Assoc
));
7364 if Nkind
(Assoc
) = N_Iterated_Component_Association
then
7365 while Present
(Choice
) loop
7366 Append_To
(Deltas
, Generate_Loop
(Choice
));
7371 while Present
(Choice
) loop
7373 -- Choice can be given by a range, a subtype indication, a
7374 -- subtype name, a scalar value, or an entity.
7376 if Nkind
(Choice
) = N_Range
7377 or else (Is_Entity_Name
(Choice
)
7378 and then Is_Type
(Entity
(Choice
)))
7380 Append_To
(Deltas
, Generate_Loop
(Choice
));
7382 elsif Nkind
(Choice
) = N_Subtype_Indication
then
7384 Generate_Loop
(Range_Expression
(Constraint
(Choice
))));
7388 Make_Assignment_Statement
(Sloc
(Choice
),
7390 Make_Indexed_Component
(Sloc
(Choice
),
7391 Prefix
=> New_Occurrence_Of
(Temp
, Loc
),
7392 Expressions
=> New_List
(New_Copy_Tree
(Choice
))),
7393 Expression
=> New_Copy_Tree
(Expression
(Assoc
))));
7403 Insert_Actions
(N
, Deltas
);
7404 Rewrite
(N
, New_Occurrence_Of
(Temp
, Loc
));
7405 end Expand_Delta_Array_Aggregate
;
7407 -----------------------------------
7408 -- Expand_Delta_Record_Aggregate --
7409 -----------------------------------
7411 procedure Expand_Delta_Record_Aggregate
(N
: Node_Id
; Deltas
: List_Id
) is
7412 Loc
: constant Source_Ptr
:= Sloc
(N
);
7413 Temp
: constant Entity_Id
:= Defining_Identifier
(First
(Deltas
));
7418 Assoc
:= First
(Component_Associations
(N
));
7420 while Present
(Assoc
) loop
7421 Choice
:= First
(Choice_List
(Assoc
));
7422 while Present
(Choice
) loop
7424 Make_Assignment_Statement
(Sloc
(Choice
),
7426 Make_Selected_Component
(Sloc
(Choice
),
7427 Prefix
=> New_Occurrence_Of
(Temp
, Loc
),
7428 Selector_Name
=> Make_Identifier
(Loc
, Chars
(Choice
))),
7429 Expression
=> New_Copy_Tree
(Expression
(Assoc
))));
7436 Insert_Actions
(N
, Deltas
);
7437 Rewrite
(N
, New_Occurrence_Of
(Temp
, Loc
));
7438 end Expand_Delta_Record_Aggregate
;
7440 ----------------------------------
7441 -- Expand_N_Extension_Aggregate --
7442 ----------------------------------
7444 -- If the ancestor part is an expression, add a component association for
7445 -- the parent field. If the type of the ancestor part is not the direct
7446 -- parent of the expected type, build recursively the needed ancestors.
7447 -- If the ancestor part is a subtype_mark, replace aggregate with a
7448 -- declaration for a temporary of the expected type, followed by
7449 -- individual assignments to the given components.
7451 procedure Expand_N_Extension_Aggregate
(N
: Node_Id
) is
7452 A
: constant Node_Id
:= Ancestor_Part
(N
);
7453 Loc
: constant Source_Ptr
:= Sloc
(N
);
7454 Typ
: constant Entity_Id
:= Etype
(N
);
7457 -- If the ancestor is a subtype mark, an init proc must be called
7458 -- on the resulting object which thus has to be materialized in
7461 if Is_Entity_Name
(A
) and then Is_Type
(Entity
(A
)) then
7462 Convert_To_Assignments
(N
, Typ
);
7464 -- The extension aggregate is transformed into a record aggregate
7465 -- of the following form (c1 and c2 are inherited components)
7467 -- (Exp with c3 => a, c4 => b)
7468 -- ==> (c1 => Exp.c1, c2 => Exp.c2, c3 => a, c4 => b)
7473 if Tagged_Type_Expansion
then
7474 Expand_Record_Aggregate
(N
,
7477 (Node
(First_Elmt
(Access_Disp_Table
(Typ
))), Loc
),
7480 -- No tag is needed in the case of a VM
7483 Expand_Record_Aggregate
(N
, Parent_Expr
=> A
);
7488 when RE_Not_Available
=>
7490 end Expand_N_Extension_Aggregate
;
7492 -----------------------------
7493 -- Expand_Record_Aggregate --
7494 -----------------------------
7496 procedure Expand_Record_Aggregate
7498 Orig_Tag
: Node_Id
:= Empty
;
7499 Parent_Expr
: Node_Id
:= Empty
)
7501 Loc
: constant Source_Ptr
:= Sloc
(N
);
7502 Comps
: constant List_Id
:= Component_Associations
(N
);
7503 Typ
: constant Entity_Id
:= Etype
(N
);
7504 Base_Typ
: constant Entity_Id
:= Base_Type
(Typ
);
7506 Static_Components
: Boolean := True;
7507 -- Flag to indicate whether all components are compile-time known,
7508 -- and the aggregate can be constructed statically and handled by
7509 -- the back-end. Set to False by Component_OK_For_Backend.
7511 procedure Build_Back_End_Aggregate
;
7512 -- Build a proper aggregate to be handled by the back-end
7514 function Compile_Time_Known_Composite_Value
(N
: Node_Id
) return Boolean;
7515 -- Returns true if N is an expression of composite type which can be
7516 -- fully evaluated at compile time without raising constraint error.
7517 -- Such expressions can be passed as is to Gigi without any expansion.
7519 -- This returns true for N_Aggregate with Compile_Time_Known_Aggregate
7520 -- set and constants whose expression is such an aggregate, recursively.
7522 function Component_OK_For_Backend
return Boolean;
7523 -- Check for presence of a component which makes it impossible for the
7524 -- backend to process the aggregate, thus requiring the use of a series
7525 -- of assignment statements. Cases checked for are a nested aggregate
7526 -- needing Late_Expansion, the presence of a tagged component which may
7527 -- need tag adjustment, and a bit unaligned component reference.
7529 -- We also force expansion into assignments if a component is of a
7530 -- mutable type (including a private type with discriminants) because
7531 -- in that case the size of the component to be copied may be smaller
7532 -- than the side of the target, and there is no simple way for gigi
7533 -- to compute the size of the object to be copied.
7535 -- NOTE: This is part of the ongoing work to define precisely the
7536 -- interface between front-end and back-end handling of aggregates.
7537 -- In general it is desirable to pass aggregates as they are to gigi,
7538 -- in order to minimize elaboration code. This is one case where the
7539 -- semantics of Ada complicate the analysis and lead to anomalies in
7540 -- the gcc back-end if the aggregate is not expanded into assignments.
7542 -- NOTE: This sets the global Static_Components to False in most, but
7543 -- not all, cases when it returns False.
7545 function Has_Per_Object_Constraint
(L
: List_Id
) return Boolean;
7546 -- Return True if any element of L has Has_Per_Object_Constraint set.
7547 -- L should be the Choices component of an N_Component_Association.
7549 function Has_Visible_Private_Ancestor
(Id
: E
) return Boolean;
7550 -- If any ancestor of the current type is private, the aggregate
7551 -- cannot be built in place. We cannot rely on Has_Private_Ancestor,
7552 -- because it will not be set when type and its parent are in the
7553 -- same scope, and the parent component needs expansion.
7555 function Top_Level_Aggregate
(N
: Node_Id
) return Node_Id
;
7556 -- For nested aggregates return the ultimate enclosing aggregate; for
7557 -- non-nested aggregates return N.
7559 ------------------------------
7560 -- Build_Back_End_Aggregate --
7561 ------------------------------
7563 procedure Build_Back_End_Aggregate
is
7566 Tag_Value
: Node_Id
;
7569 if Nkind
(N
) = N_Aggregate
then
7571 -- If the aggregate is static and can be handled by the back-end,
7572 -- nothing left to do.
7574 if Static_Components
then
7575 Set_Compile_Time_Known_Aggregate
(N
);
7576 Set_Expansion_Delayed
(N
, False);
7580 -- If no discriminants, nothing special to do
7582 if not Has_Discriminants
(Typ
) then
7585 -- Case of discriminants present
7587 elsif Is_Derived_Type
(Typ
) then
7589 -- For untagged types, non-stored discriminants are replaced with
7590 -- stored discriminants, which are the ones that gigi uses to
7591 -- describe the type and its components.
7593 Generate_Aggregate_For_Derived_Type
: declare
7594 procedure Prepend_Stored_Values
(T
: Entity_Id
);
7595 -- Scan the list of stored discriminants of the type, and add
7596 -- their values to the aggregate being built.
7598 ---------------------------
7599 -- Prepend_Stored_Values --
7600 ---------------------------
7602 procedure Prepend_Stored_Values
(T
: Entity_Id
) is
7604 First_Comp
: Node_Id
:= Empty
;
7607 Discr
:= First_Stored_Discriminant
(T
);
7608 while Present
(Discr
) loop
7610 Make_Component_Association
(Loc
,
7611 Choices
=> New_List
(
7612 New_Occurrence_Of
(Discr
, Loc
)),
7615 (Get_Discriminant_Value
7618 Discriminant_Constraint
(Typ
))));
7620 if No
(First_Comp
) then
7621 Prepend_To
(Component_Associations
(N
), New_Comp
);
7623 Insert_After
(First_Comp
, New_Comp
);
7626 First_Comp
:= New_Comp
;
7627 Next_Stored_Discriminant
(Discr
);
7629 end Prepend_Stored_Values
;
7633 Constraints
: constant List_Id
:= New_List
;
7637 Num_Disc
: Nat
:= 0;
7638 Num_Stor
: Nat
:= 0;
7640 -- Start of processing for Generate_Aggregate_For_Derived_Type
7643 -- Remove the associations for the discriminant of derived type
7646 First_Comp
: Node_Id
;
7649 First_Comp
:= First
(Component_Associations
(N
));
7650 while Present
(First_Comp
) loop
7654 if Ekind
(Entity
(First
(Choices
(Comp
)))) =
7658 Num_Disc
:= Num_Disc
+ 1;
7663 -- Insert stored discriminant associations in the correct
7664 -- order. If there are more stored discriminants than new
7665 -- discriminants, there is at least one new discriminant that
7666 -- constrains more than one of the stored discriminants. In
7667 -- this case we need to construct a proper subtype of the
7668 -- parent type, in order to supply values to all the
7669 -- components. Otherwise there is one-one correspondence
7670 -- between the constraints and the stored discriminants.
7672 Discr
:= First_Stored_Discriminant
(Base_Type
(Typ
));
7673 while Present
(Discr
) loop
7674 Num_Stor
:= Num_Stor
+ 1;
7675 Next_Stored_Discriminant
(Discr
);
7678 -- Case of more stored discriminants than new discriminants
7680 if Num_Stor
> Num_Disc
then
7682 -- Create a proper subtype of the parent type, which is the
7683 -- proper implementation type for the aggregate, and convert
7684 -- it to the intended target type.
7686 Discr
:= First_Stored_Discriminant
(Base_Type
(Typ
));
7687 while Present
(Discr
) loop
7690 (Get_Discriminant_Value
7693 Discriminant_Constraint
(Typ
)));
7695 Append
(New_Comp
, Constraints
);
7696 Next_Stored_Discriminant
(Discr
);
7700 Make_Subtype_Declaration
(Loc
,
7701 Defining_Identifier
=> Make_Temporary
(Loc
, 'T'),
7702 Subtype_Indication
=>
7703 Make_Subtype_Indication
(Loc
,
7705 New_Occurrence_Of
(Etype
(Base_Type
(Typ
)), Loc
),
7707 Make_Index_Or_Discriminant_Constraint
7708 (Loc
, Constraints
)));
7710 Insert_Action
(N
, Decl
);
7711 Prepend_Stored_Values
(Base_Type
(Typ
));
7713 Set_Etype
(N
, Defining_Identifier
(Decl
));
7716 Rewrite
(N
, Unchecked_Convert_To
(Typ
, N
));
7719 -- Case where we do not have fewer new discriminants than
7720 -- stored discriminants, so in this case we can simply use the
7721 -- stored discriminants of the subtype.
7724 Prepend_Stored_Values
(Typ
);
7726 end Generate_Aggregate_For_Derived_Type
;
7729 if Is_Tagged_Type
(Typ
) then
7731 -- In the tagged case, _parent and _tag component must be created
7733 -- Reset Null_Present unconditionally. Tagged records always have
7734 -- at least one field (the tag or the parent).
7736 Set_Null_Record_Present
(N
, False);
7738 -- When the current aggregate comes from the expansion of an
7739 -- extension aggregate, the parent expr is replaced by an
7740 -- aggregate formed by selected components of this expr.
7742 if Present
(Parent_Expr
) and then Is_Empty_List
(Comps
) then
7743 Comp
:= First_Component_Or_Discriminant
(Typ
);
7744 while Present
(Comp
) loop
7746 -- Skip all expander-generated components
7748 if not Comes_From_Source
(Original_Record_Component
(Comp
))
7754 Make_Selected_Component
(Loc
,
7756 Unchecked_Convert_To
(Typ
,
7757 Duplicate_Subexpr
(Parent_Expr
, True)),
7758 Selector_Name
=> New_Occurrence_Of
(Comp
, Loc
));
7761 Make_Component_Association
(Loc
,
7762 Choices
=> New_List
(
7763 New_Occurrence_Of
(Comp
, Loc
)),
7764 Expression
=> New_Comp
));
7766 Analyze_And_Resolve
(New_Comp
, Etype
(Comp
));
7769 Next_Component_Or_Discriminant
(Comp
);
7773 -- Compute the value for the Tag now, if the type is a root it
7774 -- will be included in the aggregate right away, otherwise it will
7775 -- be propagated to the parent aggregate.
7777 if Present
(Orig_Tag
) then
7778 Tag_Value
:= Orig_Tag
;
7780 elsif not Tagged_Type_Expansion
then
7786 (Node
(First_Elmt
(Access_Disp_Table
(Typ
))), Loc
);
7789 -- For a derived type, an aggregate for the parent is formed with
7790 -- all the inherited components.
7792 if Is_Derived_Type
(Typ
) then
7794 First_Comp
: Node_Id
;
7795 Parent_Comps
: List_Id
;
7796 Parent_Aggr
: Node_Id
;
7797 Parent_Name
: Node_Id
;
7800 First_Comp
:= First
(Component_Associations
(N
));
7801 Parent_Comps
:= New_List
;
7803 -- First skip the discriminants
7805 while Present
(First_Comp
)
7806 and then Ekind
(Entity
(First
(Choices
(First_Comp
))))
7812 -- Then remove the inherited component association from the
7813 -- aggregate and store them in the parent aggregate
7815 while Present
(First_Comp
)
7817 Scope
(Original_Record_Component
7818 (Entity
(First
(Choices
(First_Comp
))))) /=
7824 Append
(Comp
, Parent_Comps
);
7828 Make_Aggregate
(Loc
,
7829 Component_Associations
=> Parent_Comps
);
7830 Set_Etype
(Parent_Aggr
, Etype
(Base_Type
(Typ
)));
7832 -- Find the _parent component
7834 Comp
:= First_Component
(Typ
);
7835 while Chars
(Comp
) /= Name_uParent
loop
7836 Next_Component
(Comp
);
7839 Parent_Name
:= New_Occurrence_Of
(Comp
, Loc
);
7841 -- Insert the parent aggregate
7843 Prepend_To
(Component_Associations
(N
),
7844 Make_Component_Association
(Loc
,
7845 Choices
=> New_List
(Parent_Name
),
7846 Expression
=> Parent_Aggr
));
7848 -- Expand recursively the parent propagating the right Tag
7850 Expand_Record_Aggregate
7851 (Parent_Aggr
, Tag_Value
, Parent_Expr
);
7853 -- The ancestor part may be a nested aggregate that has
7854 -- delayed expansion: recheck now.
7856 if not Component_OK_For_Backend
then
7857 Convert_To_Assignments
(N
, Typ
);
7861 -- For a root type, the tag component is added (unless compiling
7862 -- for the VMs, where tags are implicit).
7864 elsif Tagged_Type_Expansion
then
7866 Tag_Name
: constant Node_Id
:=
7868 (First_Tag_Component
(Typ
), Loc
);
7869 Typ_Tag
: constant Entity_Id
:= RTE
(RE_Tag
);
7870 Conv_Node
: constant Node_Id
:=
7871 Unchecked_Convert_To
(Typ_Tag
, Tag_Value
);
7874 Set_Etype
(Conv_Node
, Typ_Tag
);
7875 Prepend_To
(Component_Associations
(N
),
7876 Make_Component_Association
(Loc
,
7877 Choices
=> New_List
(Tag_Name
),
7878 Expression
=> Conv_Node
));
7882 end Build_Back_End_Aggregate
;
7884 ----------------------------------------
7885 -- Compile_Time_Known_Composite_Value --
7886 ----------------------------------------
7888 function Compile_Time_Known_Composite_Value
7889 (N
: Node_Id
) return Boolean
7892 -- If we have an entity name, then see if it is the name of a
7893 -- constant and if so, test the corresponding constant value.
7895 if Is_Entity_Name
(N
) then
7897 E
: constant Entity_Id
:= Entity
(N
);
7900 if Ekind
(E
) /= E_Constant
then
7903 V
:= Constant_Value
(E
);
7905 and then Compile_Time_Known_Composite_Value
(V
);
7909 -- We have a value, see if it is compile time known
7912 if Nkind
(N
) = N_Aggregate
then
7913 return Compile_Time_Known_Aggregate
(N
);
7916 -- All other types of values are not known at compile time
7921 end Compile_Time_Known_Composite_Value
;
7923 ------------------------------
7924 -- Component_OK_For_Backend --
7925 ------------------------------
7927 function Component_OK_For_Backend
return Boolean is
7933 while Present
(C
) loop
7935 -- If the component has box initialization, expansion is needed
7936 -- and component is not ready for backend.
7938 if Box_Present
(C
) then
7942 Expr_Q
:= Unqualify
(Expression
(C
));
7944 -- Return False for array components whose bounds raise
7945 -- constraint error.
7948 Comp
: constant Entity_Id
:= First
(Choices
(C
));
7952 if Present
(Etype
(Comp
))
7953 and then Is_Array_Type
(Etype
(Comp
))
7955 Indx
:= First_Index
(Etype
(Comp
));
7956 while Present
(Indx
) loop
7957 if Nkind
(Type_Low_Bound
(Etype
(Indx
))) =
7958 N_Raise_Constraint_Error
7959 or else Nkind
(Type_High_Bound
(Etype
(Indx
))) =
7960 N_Raise_Constraint_Error
7970 -- Return False if the aggregate has any associations for tagged
7971 -- components that may require tag adjustment.
7973 -- These are cases where the source expression may have a tag that
7974 -- could differ from the component tag (e.g., can occur for type
7975 -- conversions and formal parameters). (Tag adjustment not needed
7976 -- if Tagged_Type_Expansion because object tags are implicit in
7979 if Is_Tagged_Type
(Etype
(Expr_Q
))
7981 (Nkind
(Expr_Q
) = N_Type_Conversion
7983 (Is_Entity_Name
(Expr_Q
)
7984 and then Is_Formal
(Entity
(Expr_Q
))))
7985 and then Tagged_Type_Expansion
7987 Static_Components
:= False;
7990 elsif Is_Delayed_Aggregate
(Expr_Q
) then
7991 Static_Components
:= False;
7994 elsif Nkind
(Expr_Q
) = N_Quantified_Expression
then
7995 Static_Components
:= False;
7998 elsif Possible_Bit_Aligned_Component
(Expr_Q
) then
7999 Static_Components
:= False;
8002 elsif Modify_Tree_For_C
8003 and then Nkind
(C
) = N_Component_Association
8004 and then Has_Per_Object_Constraint
(Choices
(C
))
8006 Static_Components
:= False;
8009 elsif Modify_Tree_For_C
8010 and then Nkind
(Expr_Q
) = N_Identifier
8011 and then Is_Array_Type
(Etype
(Expr_Q
))
8013 Static_Components
:= False;
8016 elsif Modify_Tree_For_C
8017 and then Nkind
(Expr_Q
) = N_Type_Conversion
8018 and then Is_Array_Type
(Etype
(Expr_Q
))
8020 Static_Components
:= False;
8024 if Is_Elementary_Type
(Etype
(Expr_Q
)) then
8025 if not Compile_Time_Known_Value
(Expr_Q
) then
8026 Static_Components
:= False;
8029 elsif not Compile_Time_Known_Composite_Value
(Expr_Q
) then
8030 Static_Components
:= False;
8032 if Is_Private_Type
(Etype
(Expr_Q
))
8033 and then Has_Discriminants
(Etype
(Expr_Q
))
8043 end Component_OK_For_Backend
;
8045 -------------------------------
8046 -- Has_Per_Object_Constraint --
8047 -------------------------------
8049 function Has_Per_Object_Constraint
(L
: List_Id
) return Boolean is
8050 N
: Node_Id
:= First
(L
);
8052 while Present
(N
) loop
8053 if Is_Entity_Name
(N
)
8054 and then Present
(Entity
(N
))
8055 and then Has_Per_Object_Constraint
(Entity
(N
))
8064 end Has_Per_Object_Constraint
;
8066 -----------------------------------
8067 -- Has_Visible_Private_Ancestor --
8068 -----------------------------------
8070 function Has_Visible_Private_Ancestor
(Id
: E
) return Boolean is
8071 R
: constant Entity_Id
:= Root_Type
(Id
);
8072 T1
: Entity_Id
:= Id
;
8076 if Is_Private_Type
(T1
) then
8086 end Has_Visible_Private_Ancestor
;
8088 -------------------------
8089 -- Top_Level_Aggregate --
8090 -------------------------
8092 function Top_Level_Aggregate
(N
: Node_Id
) return Node_Id
is
8097 while Present
(Parent
(Aggr
))
8098 and then Nkind
(Parent
(Aggr
)) in
8099 N_Aggregate | N_Component_Association
8101 Aggr
:= Parent
(Aggr
);
8105 end Top_Level_Aggregate
;
8109 Top_Level_Aggr
: constant Node_Id
:= Top_Level_Aggregate
(N
);
8111 -- Start of processing for Expand_Record_Aggregate
8114 -- No special management required for aggregates used to initialize
8115 -- statically allocated dispatch tables
8117 if Is_Static_Dispatch_Table_Aggregate
(N
) then
8120 -- Case pattern aggregates need to remain as aggregates
8122 elsif Is_Case_Choice_Pattern
(N
) then
8126 -- If the pragma Aggregate_Individually_Assign is set, always convert to
8129 if Aggregate_Individually_Assign
then
8130 Convert_To_Assignments
(N
, Typ
);
8132 -- Ada 2005 (AI-318-2): We need to convert to assignments if components
8133 -- are build-in-place function calls. The assignments will each turn
8134 -- into a build-in-place function call. If components are all static,
8135 -- we can pass the aggregate to the back end regardless of limitedness.
8137 -- Extension aggregates, aggregates in extended return statements, and
8138 -- aggregates for C++ imported types must be expanded.
8140 elsif Ada_Version
>= Ada_2005
and then Is_Limited_View
(Typ
) then
8141 if Nkind
(Parent
(N
)) not in
8142 N_Component_Association | N_Object_Declaration
8144 Convert_To_Assignments
(N
, Typ
);
8146 elsif Nkind
(N
) = N_Extension_Aggregate
8147 or else Convention
(Typ
) = Convention_CPP
8149 Convert_To_Assignments
(N
, Typ
);
8151 elsif not Size_Known_At_Compile_Time
(Typ
)
8152 or else not Component_OK_For_Backend
8153 or else not Static_Components
8155 Convert_To_Assignments
(N
, Typ
);
8157 -- In all other cases, build a proper aggregate to be handled by
8161 Build_Back_End_Aggregate
;
8164 -- Gigi doesn't properly handle temporaries of variable size so we
8165 -- generate it in the front-end
8167 elsif not Size_Known_At_Compile_Time
(Typ
)
8168 and then Tagged_Type_Expansion
8170 Convert_To_Assignments
(N
, Typ
);
8172 -- An aggregate used to initialize a controlled object must be turned
8173 -- into component assignments as the components themselves may require
8174 -- finalization actions such as adjustment.
8176 elsif Needs_Finalization
(Typ
) then
8177 Convert_To_Assignments
(N
, Typ
);
8179 -- Ada 2005 (AI-287): In case of default initialized components we
8180 -- convert the aggregate into assignments.
8182 elsif Has_Default_Init_Comps
(N
) then
8183 Convert_To_Assignments
(N
, Typ
);
8187 elsif not Component_OK_For_Backend
then
8188 Convert_To_Assignments
(N
, Typ
);
8190 -- If an ancestor is private, some components are not inherited and we
8191 -- cannot expand into a record aggregate.
8193 elsif Has_Visible_Private_Ancestor
(Typ
) then
8194 Convert_To_Assignments
(N
, Typ
);
8196 -- ??? The following was done to compile fxacc00.ads in the ACVCs. Gigi
8197 -- is not able to handle the aggregate for Late_Request.
8199 elsif Is_Tagged_Type
(Typ
) and then Has_Discriminants
(Typ
) then
8200 Convert_To_Assignments
(N
, Typ
);
8202 -- If the tagged types covers interface types we need to initialize all
8203 -- hidden components containing pointers to secondary dispatch tables.
8205 elsif Is_Tagged_Type
(Typ
) and then Has_Interfaces
(Typ
) then
8206 Convert_To_Assignments
(N
, Typ
);
8208 -- If some components are mutable, the size of the aggregate component
8209 -- may be distinct from the default size of the type component, so
8210 -- we need to expand to insure that the back-end copies the proper
8211 -- size of the data. However, if the aggregate is the initial value of
8212 -- a constant, the target is immutable and might be built statically
8213 -- if components are appropriate.
8215 elsif Has_Mutable_Components
(Typ
)
8217 (Nkind
(Parent
(Top_Level_Aggr
)) /= N_Object_Declaration
8218 or else not Constant_Present
(Parent
(Top_Level_Aggr
))
8219 or else not Static_Components
)
8221 Convert_To_Assignments
(N
, Typ
);
8223 -- If the type involved has bit aligned components, then we are not sure
8224 -- that the back end can handle this case correctly.
8226 elsif Type_May_Have_Bit_Aligned_Components
(Typ
) then
8227 Convert_To_Assignments
(N
, Typ
);
8229 -- When generating C, only generate an aggregate when declaring objects
8230 -- since C does not support aggregates in e.g. assignment statements.
8232 elsif Modify_Tree_For_C
and then not Is_CCG_Supported_Aggregate
(N
) then
8233 Convert_To_Assignments
(N
, Typ
);
8235 -- In all other cases, build a proper aggregate to be handled by gigi
8238 Build_Back_End_Aggregate
;
8240 end Expand_Record_Aggregate
;
8242 ---------------------
8243 -- Get_Base_Object --
8244 ---------------------
8246 function Get_Base_Object
(N
: Node_Id
) return Entity_Id
is
8250 R
:= Get_Referenced_Object
(N
);
8252 while Nkind
(R
) in N_Indexed_Component | N_Selected_Component | N_Slice
8254 R
:= Get_Referenced_Object
(Prefix
(R
));
8257 if Is_Entity_Name
(R
) and then Is_Object
(Entity
(R
)) then
8262 end Get_Base_Object
;
8264 ----------------------------
8265 -- Has_Default_Init_Comps --
8266 ----------------------------
8268 function Has_Default_Init_Comps
(N
: Node_Id
) return Boolean is
8271 -- Component association and expression, respectively
8274 pragma Assert
(Nkind
(N
) in N_Aggregate | N_Extension_Aggregate
);
8276 if Has_Self_Reference
(N
) then
8280 Assoc
:= First
(Component_Associations
(N
));
8281 while Present
(Assoc
) loop
8282 -- Each component association has either a box or an expression
8284 pragma Assert
(Box_Present
(Assoc
) xor Present
(Expression
(Assoc
)));
8286 -- Check if any direct component has default initialized components
8288 if Box_Present
(Assoc
) then
8291 -- Recursive call in case of aggregate expression
8294 Expr
:= Expression
(Assoc
);
8296 if Nkind
(Expr
) in N_Aggregate | N_Extension_Aggregate
8297 and then Has_Default_Init_Comps
(Expr
)
8307 end Has_Default_Init_Comps
;
8309 --------------------------
8310 -- Initialize_Component --
8311 --------------------------
8313 procedure Initialize_Component
8317 Init_Expr
: Node_Id
;
8320 Exceptions_OK
: constant Boolean :=
8321 not Restriction_Active
(No_Exception_Propagation
);
8322 Finalization_OK
: constant Boolean :=
8324 and then Needs_Finalization
(Comp_Typ
);
8325 Loc
: constant Source_Ptr
:= Sloc
(N
);
8327 Blk_Stmts
: List_Id
;
8328 Init_Stmt
: Node_Id
;
8331 pragma Assert
(Nkind
(Init_Expr
) in N_Subexpr
);
8333 -- Protect the initialization statements from aborts. Generate:
8337 if Finalization_OK
and Abort_Allowed
then
8338 if Exceptions_OK
then
8339 Blk_Stmts
:= New_List
;
8344 Append_To
(Blk_Stmts
, Build_Runtime_Call
(Loc
, RE_Abort_Defer
));
8346 -- Otherwise aborts are not allowed. All generated code is added
8347 -- directly to the input list.
8353 -- Initialize the component. Generate:
8355 -- Comp := Init_Expr;
8357 -- Note that the initialization expression is not duplicated because
8358 -- either only a single component may be initialized by it (record)
8359 -- or it has already been duplicated if need be (array).
8362 Make_OK_Assignment_Statement
(Loc
,
8363 Name
=> New_Copy_Tree
(Comp
),
8364 Expression
=> Relocate_Node
(Init_Expr
));
8366 Append_To
(Blk_Stmts
, Init_Stmt
);
8368 -- Arrange for the component to be adjusted if need be (the call will be
8369 -- generated by Make_Tag_Ctrl_Assignment). But, in the case of an array
8370 -- aggregate, controlled subaggregates are not considered because each
8371 -- of their individual elements will receive an adjustment of its own.
8374 and then not Is_Limited_View
(Comp_Typ
)
8376 (Is_Array_Type
(Etype
(N
))
8377 and then Is_Array_Type
(Comp_Typ
)
8378 and then Needs_Finalization
(Component_Type
(Comp_Typ
))
8379 and then Nkind
(Unqualify
(Init_Expr
)) = N_Aggregate
)
8381 Set_No_Finalize_Actions
(Init_Stmt
);
8383 -- Or else, only adjust the tag due to a possible view conversion
8386 Set_No_Ctrl_Actions
(Init_Stmt
);
8388 if Tagged_Type_Expansion
and then Is_Tagged_Type
(Comp_Typ
) then
8389 Append_To
(Blk_Stmts
,
8390 Make_Tag_Assignment_From_Type
8391 (Loc
, New_Copy_Tree
(Comp
), Underlying_Type
(Comp_Typ
)));
8395 -- Complete the protection of the initialization statements
8397 if Finalization_OK
and Abort_Allowed
then
8399 -- Wrap the initialization statements in a block to catch a
8400 -- potential exception. Generate:
8404 -- Comp := Init_Expr;
8405 -- Comp._tag := Full_TypP;
8406 -- [Deep_]Adjust (Comp);
8408 -- Abort_Undefer_Direct;
8411 if Exceptions_OK
then
8413 Build_Abort_Undefer_Block
(Loc
,
8417 -- Otherwise exceptions are not propagated. Generate:
8420 -- Comp := Init_Expr;
8421 -- Comp._tag := Full_TypP;
8422 -- [Deep_]Adjust (Comp);
8426 Append_To
(Blk_Stmts
,
8427 Build_Runtime_Call
(Loc
, RE_Abort_Undefer
));
8430 end Initialize_Component
;
8432 ----------------------------------------
8433 -- Is_Build_In_Place_Aggregate_Return --
8434 ----------------------------------------
8436 function Is_Build_In_Place_Aggregate_Return
(N
: Node_Id
) return Boolean is
8437 P
: Node_Id
:= Parent
(N
);
8440 while Nkind
(P
) = N_Qualified_Expression
loop
8444 if Nkind
(P
) = N_Simple_Return_Statement
then
8447 elsif Nkind
(Parent
(P
)) = N_Extended_Return_Statement
then
8455 Is_Build_In_Place_Function
8456 (Return_Applies_To
(Return_Statement_Entity
(P
)));
8457 end Is_Build_In_Place_Aggregate_Return
;
8459 --------------------------
8460 -- Is_Delayed_Aggregate --
8461 --------------------------
8463 function Is_Delayed_Aggregate
(N
: Node_Id
) return Boolean is
8464 Unqual_N
: constant Node_Id
:= Unqualify
(N
);
8467 return Nkind
(Unqual_N
) in N_Aggregate | N_Extension_Aggregate
8468 and then Expansion_Delayed
(Unqual_N
);
8469 end Is_Delayed_Aggregate
;
8471 --------------------------------
8472 -- Is_CCG_Supported_Aggregate --
8473 --------------------------------
8475 function Is_CCG_Supported_Aggregate
8476 (N
: Node_Id
) return Boolean
8478 P
: Node_Id
:= Parent
(N
);
8481 -- Aggregates are not supported for nonstandard rep clauses, since they
8482 -- may lead to extra padding fields in CCG.
8484 if Is_Record_Type
(Etype
(N
))
8485 and then Has_Non_Standard_Rep
(Etype
(N
))
8490 while Present
(P
) and then Nkind
(P
) = N_Aggregate
loop
8494 -- Check cases where aggregates are supported by the CCG backend
8496 if Nkind
(P
) = N_Object_Declaration
then
8498 P_Typ
: constant Entity_Id
:= Etype
(Defining_Identifier
(P
));
8501 if Is_Record_Type
(P_Typ
) then
8504 return Compile_Time_Known_Bounds
(P_Typ
);
8508 elsif Nkind
(P
) = N_Qualified_Expression
then
8509 if Nkind
(Parent
(P
)) = N_Object_Declaration
then
8511 P_Typ
: constant Entity_Id
:=
8512 Etype
(Defining_Identifier
(Parent
(P
)));
8514 if Is_Record_Type
(P_Typ
) then
8517 return Compile_Time_Known_Bounds
(P_Typ
);
8521 elsif Nkind
(Parent
(P
)) = N_Allocator
then
8527 end Is_CCG_Supported_Aggregate
;
8529 ----------------------------------------
8530 -- Is_Static_Dispatch_Table_Aggregate --
8531 ----------------------------------------
8533 function Is_Static_Dispatch_Table_Aggregate
(N
: Node_Id
) return Boolean is
8534 Typ
: constant Entity_Id
:= Base_Type
(Etype
(N
));
8537 return Building_Static_Dispatch_Tables
8538 and then Tagged_Type_Expansion
8540 -- Avoid circularity when rebuilding the compiler
8542 and then not Is_RTU
(Cunit_Entity
(Get_Source_Unit
(N
)), Ada_Tags
)
8543 and then (Is_RTE
(Typ
, RE_Dispatch_Table_Wrapper
)
8545 Is_RTE
(Typ
, RE_Address_Array
)
8547 Is_RTE
(Typ
, RE_Type_Specific_Data
)
8549 Is_RTE
(Typ
, RE_Tag_Table
)
8551 Is_RTE
(Typ
, RE_Object_Specific_Data
)
8553 Is_RTE
(Typ
, RE_Interface_Data
)
8555 Is_RTE
(Typ
, RE_Interfaces_Array
)
8557 Is_RTE
(Typ
, RE_Interface_Data_Element
));
8558 end Is_Static_Dispatch_Table_Aggregate
;
8560 -----------------------------
8561 -- Is_Two_Dim_Packed_Array --
8562 -----------------------------
8564 function Is_Two_Dim_Packed_Array
(Typ
: Entity_Id
) return Boolean is
8565 C
: constant Uint
:= Component_Size
(Typ
);
8568 return Number_Dimensions
(Typ
) = 2
8569 and then Is_Bit_Packed_Array
(Typ
)
8570 and then Is_Scalar_Type
(Component_Type
(Typ
))
8571 and then C
in Uint_1 | Uint_2 | Uint_4
; -- False if No_Uint
8572 end Is_Two_Dim_Packed_Array
;
8574 --------------------
8575 -- Late_Expansion --
8576 --------------------
8578 function Late_Expansion
8581 Target
: Node_Id
) return List_Id
8583 Aggr_Code
: List_Id
;
8587 if Is_Array_Type
(Typ
) then
8588 -- If the assignment can be done directly by the back end, then
8589 -- reset Set_Expansion_Delayed and do not expand further.
8591 if not CodePeer_Mode
8592 and then not Modify_Tree_For_C
8593 and then not Possible_Bit_Aligned_Component
(Target
)
8594 and then not Is_Possibly_Unaligned_Slice
(Target
)
8595 and then Aggr_Assignment_OK_For_Backend
(N
)
8597 New_Aggr
:= New_Copy_Tree
(N
);
8598 Set_Expansion_Delayed
(New_Aggr
, False);
8602 Make_OK_Assignment_Statement
(Sloc
(New_Aggr
),
8604 Expression
=> New_Aggr
));
8606 -- Or else, generate component assignments to it
8610 Build_Array_Aggr_Code
8612 Ctype
=> Component_Type
(Typ
),
8613 Index
=> First_Index
(Typ
),
8615 Scalar_Comp
=> Is_Scalar_Type
(Component_Type
(Typ
)),
8616 Indexes
=> No_List
);
8619 -- Directly or indirectly (e.g. access protected procedure) a record
8622 Aggr_Code
:= Build_Record_Aggr_Code
(N
, Typ
, Target
);
8625 -- Save the last assignment statement associated with the aggregate
8626 -- when building a controlled object. This reference is utilized by
8627 -- the finalization machinery when marking an object as successfully
8630 if Needs_Finalization
(Typ
)
8631 and then Is_Entity_Name
(Target
)
8632 and then Present
(Entity
(Target
))
8633 and then Ekind
(Entity
(Target
)) in E_Constant | E_Variable
8635 Set_Last_Aggregate_Assignment
(Entity
(Target
), Last
(Aggr_Code
));
8641 ----------------------------------
8642 -- Make_OK_Assignment_Statement --
8643 ----------------------------------
8645 function Make_OK_Assignment_Statement
8648 Expression
: Node_Id
) return Node_Id
8651 Set_Assignment_OK
(Name
);
8652 return Make_Assignment_Statement
(Sloc
, Name
, Expression
);
8653 end Make_OK_Assignment_Statement
;
8655 ------------------------
8656 -- Max_Aggregate_Size --
8657 ------------------------
8659 function Max_Aggregate_Size
8661 Default_Size
: Nat
:= 5000) return Nat
8663 function Use_Small_Size
(N
: Node_Id
) return Boolean;
8664 -- True if we should return a very small size, which means large
8665 -- aggregates will be implemented as a loop when possible (potentially
8666 -- transformed to memset calls).
8668 function Aggr_Context
(N
: Node_Id
) return Node_Id
;
8669 -- Return the context in which the aggregate appears, not counting
8670 -- qualified expressions and similar.
8676 function Aggr_Context
(N
: Node_Id
) return Node_Id
is
8677 Result
: Node_Id
:= Parent
(N
);
8679 if Nkind
(Result
) in N_Qualified_Expression
8681 | N_Unchecked_Type_Conversion
8684 | N_Component_Association
8687 Result
:= Aggr_Context
(Result
);
8693 --------------------
8694 -- Use_Small_Size --
8695 --------------------
8697 function Use_Small_Size
(N
: Node_Id
) return Boolean is
8698 C
: constant Node_Id
:= Aggr_Context
(N
);
8699 -- The decision depends on the context in which the aggregate occurs,
8700 -- and for variable declarations, whether we are nested inside a
8704 -- True for assignment statements and similar
8706 when N_Assignment_Statement
8707 | N_Simple_Return_Statement
8709 | N_Attribute_Reference
8713 -- True for nested variable declarations. False for library level
8714 -- variables, and for constants (whether or not nested).
8716 when N_Object_Declaration
=>
8717 return not Constant_Present
(C
)
8718 and then Is_Subprogram
(Current_Scope
);
8720 -- False for all other contexts
8729 Typ
: constant Entity_Id
:= Etype
(N
);
8731 -- Start of processing for Max_Aggregate_Size
8734 -- We use a small limit in CodePeer mode where we favor loops instead of
8735 -- thousands of single assignments (from large aggregates).
8737 -- We also increase the limit to 2**24 (about 16 million) if
8738 -- Restrictions (No_Elaboration_Code) or Restrictions
8739 -- (No_Implicit_Loops) is specified, since in either case we are at risk
8740 -- of declaring the program illegal because of this limit. We also
8741 -- increase the limit when Static_Elaboration_Desired, given that this
8742 -- means that objects are intended to be placed in data memory.
8744 -- Same if the aggregate is for a packed two-dimensional array, because
8745 -- if components are static it is much more efficient to construct a
8746 -- one-dimensional equivalent array with static components.
8748 if CodePeer_Mode
then
8750 elsif Restriction_Active
(No_Elaboration_Code
)
8751 or else Restriction_Active
(No_Implicit_Loops
)
8752 or else Is_Two_Dim_Packed_Array
(Typ
)
8753 or else (Ekind
(Current_Scope
) = E_Package
8754 and then Static_Elaboration_Desired
(Current_Scope
))
8757 elsif Use_Small_Size
(N
) then
8761 return Default_Size
;
8762 end Max_Aggregate_Size
;
8764 -----------------------
8765 -- Number_Of_Choices --
8766 -----------------------
8768 function Number_Of_Choices
(N
: Node_Id
) return Nat
is
8772 Nb_Choices
: Nat
:= 0;
8775 if Present
(Expressions
(N
)) then
8779 Assoc
:= First
(Component_Associations
(N
));
8780 while Present
(Assoc
) loop
8781 Choice
:= First
(Choice_List
(Assoc
));
8782 while Present
(Choice
) loop
8783 if Nkind
(Choice
) /= N_Others_Choice
then
8784 Nb_Choices
:= Nb_Choices
+ 1;
8794 end Number_Of_Choices
;
8796 ------------------------------------
8797 -- Packed_Array_Aggregate_Handled --
8798 ------------------------------------
8800 -- The current version of this procedure will handle at compile time
8801 -- any array aggregate that meets these conditions:
8803 -- One and two dimensional, bit packed
8804 -- Underlying packed type is modular type
8805 -- Bounds are within 32-bit Int range
8806 -- All bounds and values are static
8808 -- Note: for now, in the 2-D case, we only handle component sizes of
8809 -- 1, 2, 4 (cases where an integral number of elements occupies a byte).
8811 function Packed_Array_Aggregate_Handled
(N
: Node_Id
) return Boolean is
8812 Loc
: constant Source_Ptr
:= Sloc
(N
);
8813 Typ
: constant Entity_Id
:= Etype
(N
);
8814 Ctyp
: constant Entity_Id
:= Component_Type
(Typ
);
8816 Not_Handled
: exception;
8817 -- Exception raised if this aggregate cannot be handled
8820 -- Handle one- or two dimensional bit packed array
8822 if not Is_Bit_Packed_Array
(Typ
)
8823 or else Number_Dimensions
(Typ
) > 2
8828 -- If two-dimensional, check whether it can be folded, and transformed
8829 -- into a one-dimensional aggregate for the Packed_Array_Impl_Type of
8830 -- the original type.
8832 if Number_Dimensions
(Typ
) = 2 then
8833 return Two_Dim_Packed_Array_Handled
(N
);
8836 if not Is_Modular_Integer_Type
(Packed_Array_Impl_Type
(Typ
)) then
8840 if not Is_Scalar_Type
(Ctyp
) then
8845 Csiz
: constant Nat
:= UI_To_Int
(Component_Size
(Typ
));
8847 function Get_Component_Val
(N
: Node_Id
) return Uint
;
8848 -- Given a expression value N of the component type Ctyp, returns a
8849 -- value of Csiz (component size) bits representing this value. If
8850 -- the value is nonstatic or any other reason exists why the value
8851 -- cannot be returned, then Not_Handled is raised.
8853 -----------------------
8854 -- Get_Component_Val --
8855 -----------------------
8857 function Get_Component_Val
(N
: Node_Id
) return Uint
is
8861 -- We have to analyze the expression here before doing any further
8862 -- processing here. The analysis of such expressions is deferred
8863 -- till expansion to prevent some problems of premature analysis.
8865 Analyze_And_Resolve
(N
, Ctyp
);
8867 -- Must have a compile time value. String literals have to be
8868 -- converted into temporaries as well, because they cannot easily
8869 -- be converted into their bit representation.
8871 if not Compile_Time_Known_Value
(N
)
8872 or else Nkind
(N
) = N_String_Literal
8877 Val
:= Expr_Rep_Value
(N
);
8879 -- Adjust for bias, and strip proper number of bits
8881 if Has_Biased_Representation
(Ctyp
) then
8882 Val
:= Val
- Expr_Value
(Type_Low_Bound
(Ctyp
));
8885 return Val
mod Uint_2
** Csiz
;
8886 end Get_Component_Val
;
8888 Bounds
: constant Range_Nodes
:= Get_Index_Bounds
(First_Index
(Typ
));
8890 -- Here we know we have a one dimensional bit packed array
8893 -- Cannot do anything if bounds are dynamic
8895 if not (Compile_Time_Known_Value
(Bounds
.First
)
8897 Compile_Time_Known_Value
(Bounds
.Last
))
8903 Bounds_Vals
: Range_Values
;
8904 -- Compile-time known values of bounds
8906 -- Or are silly out of range of int bounds
8908 Bounds_Vals
.First
:= Expr_Value
(Bounds
.First
);
8909 Bounds_Vals
.Last
:= Expr_Value
(Bounds
.Last
);
8911 if not UI_Is_In_Int_Range
(Bounds_Vals
.First
)
8913 not UI_Is_In_Int_Range
(Bounds_Vals
.Last
)
8918 -- At this stage we have a suitable aggregate for handling at
8919 -- compile time. The only remaining checks are that the values of
8920 -- expressions in the aggregate are compile-time known (checks are
8921 -- performed by Get_Component_Val), and that any subtypes or
8922 -- ranges are statically known.
8924 -- If the aggregate is not fully positional at this stage, then
8925 -- convert it to positional form. Either this will fail, in which
8926 -- case we can do nothing, or it will succeed, in which case we
8927 -- have succeeded in handling the aggregate and transforming it
8928 -- into a modular value, or it will stay an aggregate, in which
8929 -- case we have failed to create a packed value for it.
8931 if Present
(Component_Associations
(N
)) then
8932 Convert_To_Positional
(N
, Handle_Bit_Packed
=> True);
8933 return Nkind
(N
) /= N_Aggregate
;
8936 -- Otherwise we are all positional, so convert to proper value
8939 Len
: constant Nat
:=
8940 Int
'Max (0, UI_To_Int
(Bounds_Vals
.Last
) -
8941 UI_To_Int
(Bounds_Vals
.First
) + 1);
8942 -- The length of the array (number of elements)
8944 Aggregate_Val
: Uint
;
8945 -- Value of aggregate. The value is set in the low order bits
8946 -- of this value. For the little-endian case, the values are
8947 -- stored from low-order to high-order and for the big-endian
8948 -- case the values are stored from high order to low order.
8949 -- Note that gigi will take care of the conversions to left
8950 -- justify the value in the big endian case (because of left
8951 -- justified modular type processing), so we do not have to
8952 -- worry about that here.
8955 -- Integer literal for resulting constructed value
8958 -- Shift count from low order for next value
8961 -- Shift increment for loop
8964 -- Next expression from positional parameters of aggregate
8966 Left_Justified
: Boolean;
8967 -- Set True if we are filling the high order bits of the target
8968 -- value (i.e. the value is left justified).
8971 -- For little endian, we fill up the low order bits of the
8972 -- target value. For big endian we fill up the high order bits
8973 -- of the target value (which is a left justified modular
8976 Left_Justified
:= Bytes_Big_Endian
;
8978 -- Switch justification if using -gnatd8
8980 if Debug_Flag_8
then
8981 Left_Justified
:= not Left_Justified
;
8984 -- Switch justfification if reverse storage order
8986 if Reverse_Storage_Order
(Base_Type
(Typ
)) then
8987 Left_Justified
:= not Left_Justified
;
8990 if Left_Justified
then
8991 Shift
:= Csiz
* (Len
- 1);
8998 -- Loop to set the values
9001 Aggregate_Val
:= Uint_0
;
9003 Expr
:= First
(Expressions
(N
));
9004 Aggregate_Val
:= Get_Component_Val
(Expr
) * Uint_2
** Shift
;
9006 for J
in 2 .. Len
loop
9007 Shift
:= Shift
+ Incr
;
9011 Get_Component_Val
(Expr
) * Uint_2
** Shift
;
9015 -- Now we can rewrite with the proper value
9017 Lit
:= Make_Integer_Literal
(Loc
, Intval
=> Aggregate_Val
);
9018 Set_Print_In_Hex
(Lit
);
9020 -- Construct the expression using this literal. Note that it
9021 -- is important to qualify the literal with its proper modular
9022 -- type since universal integer does not have the required
9023 -- range and also this is a left justified modular type,
9024 -- which is important in the big-endian case.
9027 Unchecked_Convert_To
(Typ
,
9028 Make_Qualified_Expression
(Loc
,
9030 New_Occurrence_Of
(Packed_Array_Impl_Type
(Typ
), Loc
),
9031 Expression
=> Lit
)));
9033 Analyze_And_Resolve
(N
, Typ
);
9042 end Packed_Array_Aggregate_Handled
;
9044 ----------------------------
9045 -- Has_Mutable_Components --
9046 ----------------------------
9048 function Has_Mutable_Components
(Typ
: Entity_Id
) return Boolean is
9053 Comp
:= First_Component
(Typ
);
9054 while Present
(Comp
) loop
9055 Ctyp
:= Underlying_Type
(Etype
(Comp
));
9056 if Is_Record_Type
(Ctyp
)
9057 and then Has_Discriminants
(Ctyp
)
9058 and then not Is_Constrained
(Ctyp
)
9063 Next_Component
(Comp
);
9067 end Has_Mutable_Components
;
9069 ------------------------------
9070 -- Initialize_Discriminants --
9071 ------------------------------
9073 procedure Initialize_Discriminants
(N
: Node_Id
; Typ
: Entity_Id
) is
9074 Loc
: constant Source_Ptr
:= Sloc
(N
);
9075 Bas
: constant Entity_Id
:= Base_Type
(Typ
);
9076 Par
: constant Entity_Id
:= Etype
(Bas
);
9077 Decl
: constant Node_Id
:= Parent
(Par
);
9081 if Is_Tagged_Type
(Bas
)
9082 and then Is_Derived_Type
(Bas
)
9083 and then Has_Discriminants
(Par
)
9084 and then Has_Discriminants
(Bas
)
9085 and then Number_Discriminants
(Bas
) /= Number_Discriminants
(Par
)
9086 and then Nkind
(Decl
) = N_Full_Type_Declaration
9087 and then Nkind
(Type_Definition
(Decl
)) = N_Record_Definition
9089 Present
(Variant_Part
(Component_List
(Type_Definition
(Decl
))))
9090 and then Nkind
(N
) /= N_Extension_Aggregate
9093 -- Call init proc to set discriminants.
9094 -- There should eventually be a special procedure for this ???
9096 Ref
:= New_Occurrence_Of
(Defining_Identifier
(N
), Loc
);
9097 Insert_Actions_After
(N
,
9098 Build_Initialization_Call
(Sloc
(N
), Ref
, Typ
));
9100 end Initialize_Discriminants
;
9108 Obj_Type
: Entity_Id
;
9109 Typ
: Entity_Id
) return Boolean
9112 -- No sliding if the type of the object is not established yet, if it is
9113 -- an unconstrained type whose actual subtype comes from the aggregate,
9114 -- or if the two types are identical. If the aggregate contains only
9115 -- an Others_Clause it gets its type from the context and no sliding
9116 -- is involved either.
9118 if not Is_Array_Type
(Obj_Type
) then
9121 elsif not Is_Constrained
(Obj_Type
) then
9124 elsif Typ
= Obj_Type
then
9127 elsif Is_Others_Aggregate
(Aggr
) then
9131 -- Sliding can only occur along the first dimension
9132 -- If any the bounds of non-static sliding is required
9133 -- to force potential range checks.
9136 Bounds1
: constant Range_Nodes
:=
9137 Get_Index_Bounds
(First_Index
(Typ
));
9138 Bounds2
: constant Range_Nodes
:=
9139 Get_Index_Bounds
(First_Index
(Obj_Type
));
9142 if not Is_OK_Static_Expression
(Bounds1
.First
) or else
9143 not Is_OK_Static_Expression
(Bounds2
.First
) or else
9144 not Is_OK_Static_Expression
(Bounds1
.Last
) or else
9145 not Is_OK_Static_Expression
(Bounds2
.Last
)
9150 return Expr_Value
(Bounds1
.First
) /= Expr_Value
(Bounds2
.First
)
9152 Expr_Value
(Bounds1
.Last
) /= Expr_Value
(Bounds2
.Last
);
9158 ---------------------
9159 -- Sort_Case_Table --
9160 ---------------------
9162 procedure Sort_Case_Table
(Case_Table
: in out Case_Table_Type
) is
9163 L
: constant Int
:= Case_Table
'First;
9164 U
: constant Int
:= Case_Table
'Last;
9172 T
:= Case_Table
(K
+ 1);
9176 and then Expr_Value
(Case_Table
(J
- 1).Choice_Lo
) >
9177 Expr_Value
(T
.Choice_Lo
)
9179 Case_Table
(J
) := Case_Table
(J
- 1);
9183 Case_Table
(J
) := T
;
9186 end Sort_Case_Table
;
9188 ----------------------------
9189 -- Static_Array_Aggregate --
9190 ----------------------------
9192 function Static_Array_Aggregate
(N
: Node_Id
) return Boolean is
9193 function Is_Static_Component
(Nod
: Node_Id
) return Boolean;
9194 -- Return True if Nod has a compile-time known value and can be passed
9195 -- as is to the back-end without further expansion.
9197 ---------------------------
9198 -- Is_Static_Component --
9199 ---------------------------
9201 function Is_Static_Component
(Nod
: Node_Id
) return Boolean is
9203 if Nkind
(Nod
) in N_Integer_Literal | N_Real_Literal
then
9206 elsif Is_Entity_Name
(Nod
)
9207 and then Present
(Entity
(Nod
))
9208 and then Ekind
(Entity
(Nod
)) = E_Enumeration_Literal
9212 elsif Nkind
(Nod
) = N_Aggregate
9213 and then Compile_Time_Known_Aggregate
(Nod
)
9220 end Is_Static_Component
;
9224 Bounds
: constant Node_Id
:= Aggregate_Bounds
(N
);
9225 Typ
: constant Entity_Id
:= Etype
(N
);
9232 -- Start of processing for Static_Array_Aggregate
9235 if Is_Packed
(Typ
) or else Has_Discriminants
(Component_Type
(Typ
)) then
9240 and then Nkind
(Bounds
) = N_Range
9241 and then Nkind
(Low_Bound
(Bounds
)) = N_Integer_Literal
9242 and then Nkind
(High_Bound
(Bounds
)) = N_Integer_Literal
9244 Lo
:= Low_Bound
(Bounds
);
9245 Hi
:= High_Bound
(Bounds
);
9247 if No
(Component_Associations
(N
)) then
9249 -- Verify that all components are static
9251 Expr
:= First
(Expressions
(N
));
9252 while Present
(Expr
) loop
9253 if not Is_Static_Component
(Expr
) then
9263 -- We allow only a single named association, either a static
9264 -- range or an others_clause, with a static expression.
9266 Expr
:= First
(Component_Associations
(N
));
9268 if Present
(Expressions
(N
)) then
9271 elsif Present
(Next
(Expr
)) then
9274 elsif Present
(Next
(First
(Choice_List
(Expr
)))) then
9278 -- The aggregate is static if all components are literals,
9279 -- or else all its components are static aggregates for the
9280 -- component type. We also limit the size of a static aggregate
9281 -- to prevent runaway static expressions.
9283 if not Is_Static_Component
(Expression
(Expr
)) then
9287 if not Aggr_Size_OK
(N
) then
9291 -- Create a positional aggregate with the right number of
9292 -- copies of the expression.
9294 Agg
:= Make_Aggregate
(Sloc
(N
), New_List
, No_List
);
9296 for I
in UI_To_Int
(Intval
(Lo
)) .. UI_To_Int
(Intval
(Hi
))
9298 Append_To
(Expressions
(Agg
), New_Copy
(Expression
(Expr
)));
9300 -- The copied expression must be analyzed and resolved.
9301 -- Besides setting the type, this ensures that static
9302 -- expressions are appropriately marked as such.
9305 (Last
(Expressions
(Agg
)), Component_Type
(Typ
));
9308 Set_Aggregate_Bounds
(Agg
, Bounds
);
9309 Set_Etype
(Agg
, Typ
);
9312 Set_Compile_Time_Known_Aggregate
(N
);
9321 end Static_Array_Aggregate
;
9323 ----------------------------------
9324 -- Two_Dim_Packed_Array_Handled --
9325 ----------------------------------
9327 function Two_Dim_Packed_Array_Handled
(N
: Node_Id
) return Boolean is
9328 Loc
: constant Source_Ptr
:= Sloc
(N
);
9329 Typ
: constant Entity_Id
:= Etype
(N
);
9330 Ctyp
: constant Entity_Id
:= Component_Type
(Typ
);
9331 Comp_Size
: constant Int
:= UI_To_Int
(Component_Size
(Typ
));
9332 Packed_Array
: constant Entity_Id
:=
9333 Packed_Array_Impl_Type
(Base_Type
(Typ
));
9336 -- Expression in original aggregate
9339 -- One-dimensional subaggregate
9343 -- For now, only deal with cases where an integral number of elements
9344 -- fit in a single byte. This includes the most common boolean case.
9346 if not (Comp_Size
= 1 or else
9347 Comp_Size
= 2 or else
9353 Convert_To_Positional
(N
, Handle_Bit_Packed
=> True);
9355 -- Verify that all components are static
9357 if Nkind
(N
) = N_Aggregate
9358 and then Compile_Time_Known_Aggregate
(N
)
9362 -- The aggregate may have been reanalyzed and converted already
9364 elsif Nkind
(N
) /= N_Aggregate
then
9367 -- If component associations remain, the aggregate is not static
9369 elsif Present
(Component_Associations
(N
)) then
9373 One_Dim
:= First
(Expressions
(N
));
9374 while Present
(One_Dim
) loop
9375 if Present
(Component_Associations
(One_Dim
)) then
9379 One_Comp
:= First
(Expressions
(One_Dim
));
9380 while Present
(One_Comp
) loop
9381 if not Is_OK_Static_Expression
(One_Comp
) then
9392 -- Two-dimensional aggregate is now fully positional so pack one
9393 -- dimension to create a static one-dimensional array, and rewrite
9394 -- as an unchecked conversion to the original type.
9397 Byte_Size
: constant Int
:= UI_To_Int
(Component_Size
(Packed_Array
));
9398 -- The packed array type is a byte array
9401 -- Number of components accumulated in current byte
9404 -- Assembled list of packed values for equivalent aggregate
9407 -- Integer value of component
9410 -- Step size for packing
9413 -- Endian-dependent start position for packing
9416 -- Current insertion position
9419 -- Component of packed array being assembled
9426 -- Account for endianness. See corresponding comment in
9427 -- Packed_Array_Aggregate_Handled concerning the following.
9431 xor Reverse_Storage_Order
(Base_Type
(Typ
))
9433 Init_Shift
:= Byte_Size
- Comp_Size
;
9440 -- Iterate over each subaggregate
9442 Shift
:= Init_Shift
;
9443 One_Dim
:= First
(Expressions
(N
));
9444 while Present
(One_Dim
) loop
9445 One_Comp
:= First
(Expressions
(One_Dim
));
9446 while Present
(One_Comp
) loop
9447 if Packed_Num
= Byte_Size
/ Comp_Size
then
9449 -- Byte is complete, add to list of expressions
9451 Append
(Make_Integer_Literal
(Sloc
(One_Dim
), Val
), Comps
);
9453 Shift
:= Init_Shift
;
9457 Comp_Val
:= Expr_Rep_Value
(One_Comp
);
9459 -- Adjust for bias, and strip proper number of bits
9461 if Has_Biased_Representation
(Ctyp
) then
9462 Comp_Val
:= Comp_Val
- Expr_Value
(Type_Low_Bound
(Ctyp
));
9465 Comp_Val
:= Comp_Val
mod Uint_2
** Comp_Size
;
9466 Val
:= UI_To_Int
(Val
+ Comp_Val
* Uint_2
** Shift
);
9467 Shift
:= Shift
+ Incr
;
9469 Packed_Num
:= Packed_Num
+ 1;
9476 if Packed_Num
> 0 then
9478 -- Add final incomplete byte if present
9480 Append
(Make_Integer_Literal
(Sloc
(One_Dim
), Val
), Comps
);
9484 Unchecked_Convert_To
(Typ
,
9485 Make_Qualified_Expression
(Loc
,
9486 Subtype_Mark
=> New_Occurrence_Of
(Packed_Array
, Loc
),
9487 Expression
=> Make_Aggregate
(Loc
, Expressions
=> Comps
))));
9488 Analyze_And_Resolve
(N
);
9491 end Two_Dim_Packed_Array_Handled
;