1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2019, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Atree
; use Atree
;
27 with Checks
; use Checks
;
28 with Debug
; use Debug
;
29 with Einfo
; use Einfo
;
30 with Elists
; use Elists
;
31 with Errout
; use Errout
;
32 with Expander
; use Expander
;
33 with Exp_Util
; use Exp_Util
;
34 with Exp_Ch3
; use Exp_Ch3
;
35 with Exp_Ch6
; use Exp_Ch6
;
36 with Exp_Ch7
; use Exp_Ch7
;
37 with Exp_Ch9
; use Exp_Ch9
;
38 with Exp_Disp
; use Exp_Disp
;
39 with Exp_Tss
; use Exp_Tss
;
40 with Freeze
; use Freeze
;
41 with Itypes
; use Itypes
;
43 with Namet
; use Namet
;
44 with Nmake
; use Nmake
;
45 with Nlists
; use Nlists
;
47 with Restrict
; use Restrict
;
48 with Rident
; use Rident
;
49 with Rtsfind
; use Rtsfind
;
50 with Ttypes
; use Ttypes
;
52 with Sem_Aggr
; use Sem_Aggr
;
53 with Sem_Aux
; use Sem_Aux
;
54 with Sem_Ch3
; use Sem_Ch3
;
55 with Sem_Eval
; use Sem_Eval
;
56 with Sem_Res
; use Sem_Res
;
57 with Sem_Util
; use Sem_Util
;
58 with Sinfo
; use Sinfo
;
59 with Snames
; use Snames
;
60 with Stand
; use Stand
;
61 with Stringt
; use Stringt
;
62 with Tbuild
; use Tbuild
;
63 with Uintp
; use Uintp
;
64 with Urealp
; use Urealp
;
66 package body Exp_Aggr
is
68 type Case_Bounds
is record
71 Choice_Node
: Node_Id
;
74 type Case_Table_Type
is array (Nat
range <>) of Case_Bounds
;
75 -- Table type used by Check_Case_Choices procedure
77 procedure Collect_Initialization_Statements
80 Node_After
: Node_Id
);
81 -- If Obj is not frozen, collect actions inserted after N until, but not
82 -- including, Node_After, for initialization of Obj, and move them to an
83 -- expression with actions, which becomes the Initialization_Statements for
86 procedure Expand_Delta_Array_Aggregate
(N
: Node_Id
; Deltas
: List_Id
);
87 procedure Expand_Delta_Record_Aggregate
(N
: Node_Id
; Deltas
: List_Id
);
89 function Has_Default_Init_Comps
(N
: Node_Id
) return Boolean;
90 -- N is an aggregate (record or array). Checks the presence of default
91 -- initialization (<>) in any component (Ada 2005: AI-287).
93 function Is_CCG_Supported_Aggregate
(N
: Node_Id
) return Boolean;
94 -- Return True if aggregate N is located in a context supported by the
95 -- CCG backend; False otherwise.
97 function Is_Static_Dispatch_Table_Aggregate
(N
: Node_Id
) return Boolean;
98 -- Returns true if N is an aggregate used to initialize the components
99 -- of a statically allocated dispatch table.
101 function Late_Expansion
104 Target
: Node_Id
) return List_Id
;
105 -- This routine implements top-down expansion of nested aggregates. In
106 -- doing so, it avoids the generation of temporaries at each level. N is
107 -- a nested record or array aggregate with the Expansion_Delayed flag.
108 -- Typ is the expected type of the aggregate. Target is a (duplicatable)
109 -- expression that will hold the result of the aggregate expansion.
111 function Make_OK_Assignment_Statement
114 Expression
: Node_Id
) return Node_Id
;
115 -- This is like Make_Assignment_Statement, except that Assignment_OK
116 -- is set in the left operand. All assignments built by this unit use
117 -- this routine. This is needed to deal with assignments to initialized
118 -- constants that are done in place.
121 (Obj_Type
: Entity_Id
;
122 Typ
: Entity_Id
) return Boolean;
123 -- A static array aggregate in an object declaration can in most cases be
124 -- expanded in place. The one exception is when the aggregate is given
125 -- with component associations that specify different bounds from those of
126 -- the type definition in the object declaration. In this pathological
127 -- case the aggregate must slide, and we must introduce an intermediate
128 -- temporary to hold it.
130 -- The same holds in an assignment to one-dimensional array of arrays,
131 -- when a component may be given with bounds that differ from those of the
134 function Number_Of_Choices
(N
: Node_Id
) return Nat
;
135 -- Returns the number of discrete choices (not including the others choice
136 -- if present) contained in (sub-)aggregate N.
138 procedure Process_Transient_Component
140 Comp_Typ
: Entity_Id
;
142 Fin_Call
: out Node_Id
;
143 Hook_Clear
: out Node_Id
;
144 Aggr
: Node_Id
:= Empty
;
145 Stmts
: List_Id
:= No_List
);
146 -- Subsidiary to the expansion of array and record aggregates. Generate
147 -- part of the necessary code to finalize a transient component. Comp_Typ
148 -- is the component type. Init_Expr is the initialization expression of the
149 -- component which is always a function call. Fin_Call is the finalization
150 -- call used to clean up the transient function result. Hook_Clear is the
151 -- hook reset statement. Aggr and Stmts both control the placement of the
152 -- generated code. Aggr is the related aggregate. If present, all code is
153 -- inserted prior to Aggr using Insert_Action. Stmts is the initialization
154 -- statements of the component. If present, all code is added to Stmts.
156 procedure Process_Transient_Component_Completion
160 Hook_Clear
: Node_Id
;
162 -- Subsidiary to the expansion of array and record aggregates. Generate
163 -- part of the necessary code to finalize a transient component. Aggr is
164 -- the related aggregate. Fin_Clear is the finalization call used to clean
165 -- up the transient component. Hook_Clear is the hook reset statment. Stmts
166 -- is the initialization statement list for the component. All generated
167 -- code is added to Stmts.
169 procedure Sort_Case_Table
(Case_Table
: in out Case_Table_Type
);
170 -- Sort the Case Table using the Lower Bound of each Choice as the key.
171 -- A simple insertion sort is used since the number of choices in a case
172 -- statement of variant part will usually be small and probably in near
175 ------------------------------------------------------
176 -- Local subprograms for Record Aggregate Expansion --
177 ------------------------------------------------------
179 function Is_Build_In_Place_Aggregate_Return
(N
: Node_Id
) return Boolean;
180 -- True if N is an aggregate (possibly qualified or converted) that is
181 -- being returned from a build-in-place function.
183 function Build_Record_Aggr_Code
186 Lhs
: Node_Id
) return List_Id
;
187 -- N is an N_Aggregate or an N_Extension_Aggregate. Typ is the type of the
188 -- aggregate. Target is an expression containing the location on which the
189 -- component by component assignments will take place. Returns the list of
190 -- assignments plus all other adjustments needed for tagged and controlled
193 procedure Convert_To_Assignments
(N
: Node_Id
; Typ
: Entity_Id
);
194 -- Transform a record aggregate into a sequence of assignments performed
195 -- component by component. N is an N_Aggregate or N_Extension_Aggregate.
196 -- Typ is the type of the record aggregate.
198 procedure Expand_Record_Aggregate
200 Orig_Tag
: Node_Id
:= Empty
;
201 Parent_Expr
: Node_Id
:= Empty
);
202 -- This is the top level procedure for record aggregate expansion.
203 -- Expansion for record aggregates needs expand aggregates for tagged
204 -- record types. Specifically Expand_Record_Aggregate adds the Tag
205 -- field in front of the Component_Association list that was created
206 -- during resolution by Resolve_Record_Aggregate.
208 -- N is the record aggregate node.
209 -- Orig_Tag is the value of the Tag that has to be provided for this
210 -- specific aggregate. It carries the tag corresponding to the type
211 -- of the outermost aggregate during the recursive expansion
212 -- Parent_Expr is the ancestor part of the original extension
215 function Has_Mutable_Components
(Typ
: Entity_Id
) return Boolean;
216 -- Return true if one of the components is of a discriminated type with
217 -- defaults. An aggregate for a type with mutable components must be
218 -- expanded into individual assignments.
220 function In_Place_Assign_OK
(N
: Node_Id
) return Boolean;
221 -- Predicate to determine whether an aggregate assignment can be done in
222 -- place, because none of the new values can depend on the components of
223 -- the target of the assignment.
225 procedure Initialize_Discriminants
(N
: Node_Id
; Typ
: Entity_Id
);
226 -- If the type of the aggregate is a type extension with renamed discrimi-
227 -- nants, we must initialize the hidden discriminants of the parent.
228 -- Otherwise, the target object must not be initialized. The discriminants
229 -- are initialized by calling the initialization procedure for the type.
230 -- This is incorrect if the initialization of other components has any
231 -- side effects. We restrict this call to the case where the parent type
232 -- has a variant part, because this is the only case where the hidden
233 -- discriminants are accessed, namely when calling discriminant checking
234 -- functions of the parent type, and when applying a stream attribute to
235 -- an object of the derived type.
237 -----------------------------------------------------
238 -- Local Subprograms for Array Aggregate Expansion --
239 -----------------------------------------------------
241 function Aggr_Size_OK
(N
: Node_Id
; Typ
: Entity_Id
) return Boolean;
242 -- Very large static aggregates present problems to the back-end, and are
243 -- transformed into assignments and loops. This function verifies that the
244 -- total number of components of an aggregate is acceptable for rewriting
245 -- into a purely positional static form. Aggr_Size_OK must be called before
248 -- This function also detects and warns about one-component aggregates that
249 -- appear in a nonstatic context. Even if the component value is static,
250 -- such an aggregate must be expanded into an assignment.
252 function Backend_Processing_Possible
(N
: Node_Id
) return Boolean;
253 -- This function checks if array aggregate N can be processed directly
254 -- by the backend. If this is the case, True is returned.
256 function Build_Array_Aggr_Code
261 Scalar_Comp
: Boolean;
262 Indexes
: List_Id
:= No_List
) return List_Id
;
263 -- This recursive routine returns a list of statements containing the
264 -- loops and assignments that are needed for the expansion of the array
267 -- N is the (sub-)aggregate node to be expanded into code. This node has
268 -- been fully analyzed, and its Etype is properly set.
270 -- Index is the index node corresponding to the array subaggregate N
272 -- Into is the target expression into which we are copying the aggregate.
273 -- Note that this node may not have been analyzed yet, and so the Etype
274 -- field may not be set.
276 -- Scalar_Comp is True if the component type of the aggregate is scalar
278 -- Indexes is the current list of expressions used to index the object we
281 procedure Convert_Array_Aggr_In_Allocator
285 -- If the aggregate appears within an allocator and can be expanded in
286 -- place, this routine generates the individual assignments to components
287 -- of the designated object. This is an optimization over the general
288 -- case, where a temporary is first created on the stack and then used to
289 -- construct the allocated object on the heap.
291 procedure Convert_To_Positional
293 Max_Others_Replicate
: Nat
:= 32;
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 Max_Others_Replicate controls the maximum number of
299 -- values corresponding to an others choice that will be converted to
300 -- positional notation (the default of 32 is the normal limit, and reflects
301 -- the fact that normally the loop is better than a lot of separate
302 -- assignments). Note that this limit gets overridden in any case if
303 -- either of the restrictions No_Elaboration_Code or No_Implicit_Loops is
304 -- set. The parameter Handle_Bit_Packed is usually set False (since we do
305 -- not expect the back end to handle bit packed arrays, so the normal case
306 -- of conversion is pointless), but in the special case of a call from
307 -- Packed_Array_Aggregate_Handled, we set this parameter to True, since
308 -- these are cases we handle in there.
310 procedure Expand_Array_Aggregate
(N
: Node_Id
);
311 -- This is the top-level routine to perform array aggregate expansion.
312 -- N is the N_Aggregate node to be expanded.
314 function Is_Two_Dim_Packed_Array
(Typ
: Entity_Id
) return Boolean;
315 -- For two-dimensional packed aggregates with constant bounds and constant
316 -- components, it is preferable to pack the inner aggregates because the
317 -- whole matrix can then be presented to the back-end as a one-dimensional
318 -- list of literals. This is much more efficient than expanding into single
319 -- component assignments. This function determines if the type Typ is for
320 -- an array that is suitable for this optimization: it returns True if Typ
321 -- is a two dimensional bit packed array with component size 1, 2, or 4.
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.
343 function Aggr_Size_OK
(N
: Node_Id
; Typ
: Entity_Id
) return Boolean is
352 -- Determines the maximum size of an array aggregate produced by
353 -- converting named to positional notation (e.g. from others clauses).
354 -- This avoids running away with attempts to convert huge aggregates,
355 -- which hit memory limits in the backend.
357 function Component_Count
(T
: Entity_Id
) return Nat
;
358 -- The limit is applied to the total number of subcomponents that the
359 -- aggregate will have, which is the number of static expressions
360 -- that will appear in the flattened array. This requires a recursive
361 -- computation of the number of scalar components of the structure.
363 ---------------------
364 -- Component_Count --
365 ---------------------
367 function Component_Count
(T
: Entity_Id
) return Nat
is
372 if Is_Scalar_Type
(T
) then
375 elsif Is_Record_Type
(T
) then
376 Comp
:= First_Component
(T
);
377 while Present
(Comp
) loop
378 Res
:= Res
+ Component_Count
(Etype
(Comp
));
379 Next_Component
(Comp
);
384 elsif Is_Array_Type
(T
) then
386 Lo
: constant Node_Id
:=
387 Type_Low_Bound
(Etype
(First_Index
(T
)));
388 Hi
: constant Node_Id
:=
389 Type_High_Bound
(Etype
(First_Index
(T
)));
391 Siz
: constant Nat
:= Component_Count
(Component_Type
(T
));
394 -- Check for superflat arrays, i.e. arrays with such bounds
395 -- as 4 .. 2, to insure that this function never returns a
396 -- meaningless negative value.
398 if not Compile_Time_Known_Value
(Lo
)
399 or else not Compile_Time_Known_Value
(Hi
)
400 or else Expr_Value
(Hi
) < Expr_Value
(Lo
)
405 -- If the number of components is greater than Int'Last,
406 -- then return Int'Last, so caller will return False (Aggr
407 -- size is not OK). Otherwise, UI_To_Int will crash.
410 UI
: constant Uint
:=
411 Expr_Value
(Hi
) - Expr_Value
(Lo
) + 1;
413 if UI_Is_In_Int_Range
(UI
) then
414 return Siz
* UI_To_Int
(UI
);
423 -- Can only be a null for an access type
429 -- Start of processing for Aggr_Size_OK
432 -- The normal aggregate limit is 500000, but we increase this limit to
433 -- 2**24 (about 16 million) if Restrictions (No_Elaboration_Code) or
434 -- Restrictions (No_Implicit_Loops) is specified, since in either case
435 -- we are at risk of declaring the program illegal because of this
436 -- limit. We also increase the limit when Static_Elaboration_Desired,
437 -- given that this means that objects are intended to be placed in data
440 -- We also increase the limit if the aggregate is for a packed two-
441 -- dimensional array, because if components are static it is much more
442 -- efficient to construct a one-dimensional equivalent array with static
445 -- Conversely, we decrease the maximum size if none of the above
446 -- requirements apply, and if the aggregate has a single component
447 -- association, which will be more efficient if implemented with a loop.
449 -- Finally, we use a small limit in CodePeer mode where we favor loops
450 -- instead of thousands of single assignments (from large aggregates).
452 Max_Aggr_Size
:= 500000;
454 if CodePeer_Mode
then
455 Max_Aggr_Size
:= 100;
457 elsif Restriction_Active
(No_Elaboration_Code
)
458 or else Restriction_Active
(No_Implicit_Loops
)
459 or else Is_Two_Dim_Packed_Array
(Typ
)
460 or else (Ekind
(Current_Scope
) = E_Package
461 and then Static_Elaboration_Desired
(Current_Scope
))
463 Max_Aggr_Size
:= 2 ** 24;
465 elsif No
(Expressions
(N
))
466 and then No
(Next
(First
(Component_Associations
(N
))))
468 Max_Aggr_Size
:= 5000;
471 Size
:= UI_From_Int
(Component_Count
(Component_Type
(Typ
)));
473 Indx
:= First_Index
(Typ
);
474 while Present
(Indx
) loop
475 Lo
:= Type_Low_Bound
(Etype
(Indx
));
476 Hi
:= Type_High_Bound
(Etype
(Indx
));
478 -- Bounds need to be known at compile time
480 if not Compile_Time_Known_Value
(Lo
)
481 or else not Compile_Time_Known_Value
(Hi
)
486 Lov
:= Expr_Value
(Lo
);
487 Hiv
:= Expr_Value
(Hi
);
489 -- A flat array is always safe
495 -- One-component aggregates are suspicious, and if the context type
496 -- is an object declaration with nonstatic bounds it will trip gcc;
497 -- such an aggregate must be expanded into a single assignment.
499 if Hiv
= Lov
and then Nkind
(Parent
(N
)) = N_Object_Declaration
then
501 Index_Type
: constant Entity_Id
:=
503 (First_Index
(Etype
(Defining_Identifier
(Parent
(N
)))));
507 if not Compile_Time_Known_Value
(Type_Low_Bound
(Index_Type
))
508 or else not Compile_Time_Known_Value
509 (Type_High_Bound
(Index_Type
))
511 if Present
(Component_Associations
(N
)) then
514 (Choice_List
(First
(Component_Associations
(N
))));
516 if Is_Entity_Name
(Indx
)
517 and then not Is_Type
(Entity
(Indx
))
520 ("single component aggregate in "
521 & "non-static context??", Indx
);
522 Error_Msg_N
("\maybe subtype name was meant??", Indx
);
532 Rng
: constant Uint
:= Hiv
- Lov
+ 1;
535 -- Check if size is too large
537 if not UI_Is_In_Int_Range
(Rng
) then
541 -- Compute the size using universal arithmetic to avoid the
542 -- possibility of overflow on very large aggregates.
547 or else Size
> Max_Aggr_Size
553 -- Bounds must be in integer range, for later array construction
555 if not UI_Is_In_Int_Range
(Lov
)
557 not UI_Is_In_Int_Range
(Hiv
)
568 ---------------------------------
569 -- Backend_Processing_Possible --
570 ---------------------------------
572 -- Backend processing by Gigi/gcc is possible only if all the following
573 -- conditions are met:
575 -- 1. N is fully positional
577 -- 2. N is not a bit-packed array aggregate;
579 -- 3. The size of N's array type must be known at compile time. Note
580 -- that this implies that the component size is also known
582 -- 4. The array type of N does not follow the Fortran layout convention
583 -- or if it does it must be 1 dimensional.
585 -- 5. The array component type may not be tagged (which could necessitate
586 -- reassignment of proper tags).
588 -- 6. The array component type must not have unaligned bit components
590 -- 7. None of the components of the aggregate may be bit unaligned
593 -- 8. There cannot be delayed components, since we do not know enough
594 -- at this stage to know if back end processing is possible.
596 -- 9. There cannot be any discriminated record components, since the
597 -- back end cannot handle this complex case.
599 -- 10. No controlled actions need to be generated for components
601 -- 11. When generating C code, N must be part of a N_Object_Declaration
603 -- 12. When generating C code, N must not include function calls
605 function Backend_Processing_Possible
(N
: Node_Id
) return Boolean is
606 Typ
: constant Entity_Id
:= Etype
(N
);
607 -- Typ is the correct constrained array subtype of the aggregate
609 function Component_Check
(N
: Node_Id
; Index
: Node_Id
) return Boolean;
610 -- This routine checks components of aggregate N, enforcing checks
611 -- 1, 7, 8, 9, 11, and 12. In the multidimensional case, these checks
612 -- are performed on subaggregates. The Index value is the current index
613 -- being checked in the multidimensional case.
615 ---------------------
616 -- Component_Check --
617 ---------------------
619 function Component_Check
(N
: Node_Id
; Index
: Node_Id
) return Boolean is
620 function Ultimate_Original_Expression
(N
: Node_Id
) return Node_Id
;
621 -- Given a type conversion or an unchecked type conversion N, return
622 -- its innermost original expression.
624 ----------------------------------
625 -- Ultimate_Original_Expression --
626 ----------------------------------
628 function Ultimate_Original_Expression
(N
: Node_Id
) return Node_Id
is
629 Expr
: Node_Id
:= Original_Node
(N
);
632 while Nkind_In
(Expr
, N_Type_Conversion
,
633 N_Unchecked_Type_Conversion
)
635 Expr
:= Original_Node
(Expression
(Expr
));
639 end Ultimate_Original_Expression
;
645 -- Start of processing for Component_Check
648 -- Checks 1: (no component associations)
650 if Present
(Component_Associations
(N
)) then
654 -- Checks 11: The C code generator cannot handle aggregates that are
655 -- not part of an object declaration.
657 if Modify_Tree_For_C
and then not Is_CCG_Supported_Aggregate
(N
) then
661 -- Checks on components
663 -- Recurse to check subaggregates, which may appear in qualified
664 -- expressions. If delayed, the front-end will have to expand.
665 -- If the component is a discriminated record, treat as nonstatic,
666 -- as the back-end cannot handle this properly.
668 Expr
:= First
(Expressions
(N
));
669 while Present
(Expr
) loop
671 -- Checks 8: (no delayed components)
673 if Is_Delayed_Aggregate
(Expr
) then
677 -- Checks 9: (no discriminated records)
679 if Present
(Etype
(Expr
))
680 and then Is_Record_Type
(Etype
(Expr
))
681 and then Has_Discriminants
(Etype
(Expr
))
686 -- Checks 7. Component must not be bit aligned component
688 if Possible_Bit_Aligned_Component
(Expr
) then
692 -- Checks 12: (no function call)
696 Nkind
(Ultimate_Original_Expression
(Expr
)) = N_Function_Call
701 -- Recursion to following indexes for multiple dimension case
703 if Present
(Next_Index
(Index
))
704 and then not Component_Check
(Expr
, Next_Index
(Index
))
709 -- All checks for that component finished, on to next
717 -- Start of processing for Backend_Processing_Possible
720 -- Checks 2 (array not bit packed) and 10 (no controlled actions)
722 if Is_Bit_Packed_Array
(Typ
) or else Needs_Finalization
(Typ
) then
726 -- If component is limited, aggregate must be expanded because each
727 -- component assignment must be built in place.
729 if Is_Limited_View
(Component_Type
(Typ
)) then
733 -- Checks 4 (array must not be multidimensional Fortran case)
735 if Convention
(Typ
) = Convention_Fortran
736 and then Number_Dimensions
(Typ
) > 1
741 -- Checks 3 (size of array must be known at compile time)
743 if not Size_Known_At_Compile_Time
(Typ
) then
747 -- Checks on components
749 if not Component_Check
(N
, First_Index
(Typ
)) then
753 -- Checks 5 (if the component type is tagged, then we may need to do
754 -- tag adjustments. Perhaps this should be refined to check for any
755 -- component associations that actually need tag adjustment, similar
756 -- to the test in Component_OK_For_Backend for record aggregates with
757 -- tagged components, but not clear whether it's worthwhile ???; in the
758 -- case of virtual machines (no Tagged_Type_Expansion), object tags are
759 -- handled implicitly).
761 if Is_Tagged_Type
(Component_Type
(Typ
))
762 and then Tagged_Type_Expansion
767 -- Checks 6 (component type must not have bit aligned components)
769 if Type_May_Have_Bit_Aligned_Components
(Component_Type
(Typ
)) then
773 -- Backend processing is possible
775 Set_Size_Known_At_Compile_Time
(Etype
(N
), True);
777 end Backend_Processing_Possible
;
779 ---------------------------
780 -- Build_Array_Aggr_Code --
781 ---------------------------
783 -- The code that we generate from a one dimensional aggregate is
785 -- 1. If the subaggregate contains discrete choices we
787 -- (a) Sort the discrete choices
789 -- (b) Otherwise for each discrete choice that specifies a range we
790 -- emit a loop. If a range specifies a maximum of three values, or
791 -- we are dealing with an expression we emit a sequence of
792 -- assignments instead of a loop.
794 -- (c) Generate the remaining loops to cover the others choice if any
796 -- 2. If the aggregate contains positional elements we
798 -- (a) translate the positional elements in a series of assignments
800 -- (b) Generate a final loop to cover the others choice if any.
801 -- Note that this final loop has to be a while loop since the case
803 -- L : Integer := Integer'Last;
804 -- H : Integer := Integer'Last;
805 -- A : array (L .. H) := (1, others =>0);
807 -- cannot be handled by a for loop. Thus for the following
809 -- array (L .. H) := (.. positional elements.., others =>E);
811 -- we always generate something like:
813 -- J : Index_Type := Index_Of_Last_Positional_Element;
815 -- J := Index_Base'Succ (J)
819 function Build_Array_Aggr_Code
824 Scalar_Comp
: Boolean;
825 Indexes
: List_Id
:= No_List
) return List_Id
827 Loc
: constant Source_Ptr
:= Sloc
(N
);
828 Index_Base
: constant Entity_Id
:= Base_Type
(Etype
(Index
));
829 Index_Base_L
: constant Node_Id
:= Type_Low_Bound
(Index_Base
);
830 Index_Base_H
: constant Node_Id
:= Type_High_Bound
(Index_Base
);
832 function Add
(Val
: Int
; To
: Node_Id
) return Node_Id
;
833 -- Returns an expression where Val is added to expression To, unless
834 -- To+Val is provably out of To's base type range. To must be an
835 -- already analyzed expression.
837 function Empty_Range
(L
, H
: Node_Id
) return Boolean;
838 -- Returns True if the range defined by L .. H is certainly empty
840 function Equal
(L
, H
: Node_Id
) return Boolean;
841 -- Returns True if L = H for sure
843 function Index_Base_Name
return Node_Id
;
844 -- Returns a new reference to the index type name
849 In_Loop
: Boolean := False) return List_Id
;
850 -- Ind must be a side-effect-free expression. If the input aggregate N
851 -- to Build_Loop contains no subaggregates, then this function returns
852 -- the assignment statement:
854 -- Into (Indexes, Ind) := Expr;
856 -- Otherwise we call Build_Code recursively. Flag In_Loop should be set
857 -- when the assignment appears within a generated loop.
859 -- Ada 2005 (AI-287): In case of default initialized component, Expr
860 -- is empty and we generate a call to the corresponding IP subprogram.
862 function Gen_Loop
(L
, H
: Node_Id
; Expr
: Node_Id
) return List_Id
;
863 -- Nodes L and H must be side-effect-free expressions. If the input
864 -- aggregate N to Build_Loop contains no subaggregates, this routine
865 -- returns the for loop statement:
867 -- for J in Index_Base'(L) .. Index_Base'(H) loop
868 -- Into (Indexes, J) := Expr;
871 -- Otherwise we call Build_Code recursively. As an optimization if the
872 -- loop covers 3 or fewer scalar elements we generate a sequence of
874 -- If the component association that generates the loop comes from an
875 -- Iterated_Component_Association, the loop parameter has the name of
876 -- the corresponding parameter in the original construct.
878 function Gen_While
(L
, H
: Node_Id
; Expr
: Node_Id
) return List_Id
;
879 -- Nodes L and H must be side-effect-free expressions. If the input
880 -- aggregate N to Build_Loop contains no subaggregates, this routine
881 -- returns the while loop statement:
883 -- J : Index_Base := L;
885 -- J := Index_Base'Succ (J);
886 -- Into (Indexes, J) := Expr;
889 -- Otherwise we call Build_Code recursively
891 function Get_Assoc_Expr
(Assoc
: Node_Id
) return Node_Id
;
892 -- For an association with a box, use value given by aspect
893 -- Default_Component_Value of array type if specified, else use
894 -- value given by aspect Default_Value for component type itself
895 -- if specified, else return Empty.
897 function Local_Compile_Time_Known_Value
(E
: Node_Id
) return Boolean;
898 function Local_Expr_Value
(E
: Node_Id
) return Uint
;
899 -- These two Local routines are used to replace the corresponding ones
900 -- in sem_eval because while processing the bounds of an aggregate with
901 -- discrete choices whose index type is an enumeration, we build static
902 -- expressions not recognized by Compile_Time_Known_Value as such since
903 -- they have not yet been analyzed and resolved. All the expressions in
904 -- question are things like Index_Base_Name'Val (Const) which we can
905 -- easily recognize as being constant.
911 function Add
(Val
: Int
; To
: Node_Id
) return Node_Id
is
916 U_Val
: constant Uint
:= UI_From_Int
(Val
);
919 -- Note: do not try to optimize the case of Val = 0, because
920 -- we need to build a new node with the proper Sloc value anyway.
922 -- First test if we can do constant folding
924 if Local_Compile_Time_Known_Value
(To
) then
925 U_To
:= Local_Expr_Value
(To
) + Val
;
927 -- Determine if our constant is outside the range of the index.
928 -- If so return an Empty node. This empty node will be caught
929 -- by Empty_Range below.
931 if Compile_Time_Known_Value
(Index_Base_L
)
932 and then U_To
< Expr_Value
(Index_Base_L
)
936 elsif Compile_Time_Known_Value
(Index_Base_H
)
937 and then U_To
> Expr_Value
(Index_Base_H
)
942 Expr_Pos
:= Make_Integer_Literal
(Loc
, U_To
);
943 Set_Is_Static_Expression
(Expr_Pos
);
945 if not Is_Enumeration_Type
(Index_Base
) then
948 -- If we are dealing with enumeration return
949 -- Index_Base'Val (Expr_Pos)
953 Make_Attribute_Reference
955 Prefix
=> Index_Base_Name
,
956 Attribute_Name
=> Name_Val
,
957 Expressions
=> New_List
(Expr_Pos
));
963 -- If we are here no constant folding possible
965 if not Is_Enumeration_Type
(Index_Base
) then
968 Left_Opnd
=> Duplicate_Subexpr
(To
),
969 Right_Opnd
=> Make_Integer_Literal
(Loc
, U_Val
));
971 -- If we are dealing with enumeration return
972 -- Index_Base'Val (Index_Base'Pos (To) + Val)
976 Make_Attribute_Reference
978 Prefix
=> Index_Base_Name
,
979 Attribute_Name
=> Name_Pos
,
980 Expressions
=> New_List
(Duplicate_Subexpr
(To
)));
985 Right_Opnd
=> Make_Integer_Literal
(Loc
, U_Val
));
988 Make_Attribute_Reference
990 Prefix
=> Index_Base_Name
,
991 Attribute_Name
=> Name_Val
,
992 Expressions
=> New_List
(Expr_Pos
));
1002 function Empty_Range
(L
, H
: Node_Id
) return Boolean is
1003 Is_Empty
: Boolean := False;
1008 -- First check if L or H were already detected as overflowing the
1009 -- index base range type by function Add above. If this is so Add
1010 -- returns the empty node.
1012 if No
(L
) or else No
(H
) then
1016 for J
in 1 .. 3 loop
1019 -- L > H range is empty
1025 -- B_L > H range must be empty
1028 Low
:= Index_Base_L
;
1031 -- L > B_H range must be empty
1035 High
:= Index_Base_H
;
1038 if Local_Compile_Time_Known_Value
(Low
)
1040 Local_Compile_Time_Known_Value
(High
)
1043 UI_Gt
(Local_Expr_Value
(Low
), Local_Expr_Value
(High
));
1056 function Equal
(L
, H
: Node_Id
) return Boolean is
1061 elsif Local_Compile_Time_Known_Value
(L
)
1063 Local_Compile_Time_Known_Value
(H
)
1065 return UI_Eq
(Local_Expr_Value
(L
), Local_Expr_Value
(H
));
1078 In_Loop
: Boolean := False) return List_Id
1080 function Add_Loop_Actions
(Lis
: List_Id
) return List_Id
;
1081 -- Collect insert_actions generated in the construction of a loop,
1082 -- and prepend them to the sequence of assignments to complete the
1083 -- eventual body of the loop.
1085 procedure Initialize_Array_Component
1086 (Arr_Comp
: Node_Id
;
1088 Init_Expr
: Node_Id
;
1090 -- Perform the initialization of array component Arr_Comp with
1091 -- expected type Comp_Typ. Init_Expr denotes the initialization
1092 -- expression of the array component. All generated code is added
1095 procedure Initialize_Ctrl_Array_Component
1096 (Arr_Comp
: Node_Id
;
1097 Comp_Typ
: Entity_Id
;
1098 Init_Expr
: Node_Id
;
1100 -- Perform the initialization of array component Arr_Comp when its
1101 -- expected type Comp_Typ needs finalization actions. Init_Expr is
1102 -- the initialization expression of the array component. All hook-
1103 -- related declarations are inserted prior to aggregate N. Remaining
1104 -- code is added to list Stmts.
1106 ----------------------
1107 -- Add_Loop_Actions --
1108 ----------------------
1110 function Add_Loop_Actions
(Lis
: List_Id
) return List_Id
is
1114 -- Ada 2005 (AI-287): Do nothing else in case of default
1115 -- initialized component.
1120 elsif Nkind
(Parent
(Expr
)) = N_Component_Association
1121 and then Present
(Loop_Actions
(Parent
(Expr
)))
1123 Append_List
(Lis
, Loop_Actions
(Parent
(Expr
)));
1124 Res
:= Loop_Actions
(Parent
(Expr
));
1125 Set_Loop_Actions
(Parent
(Expr
), No_List
);
1131 end Add_Loop_Actions
;
1133 --------------------------------
1134 -- Initialize_Array_Component --
1135 --------------------------------
1137 procedure Initialize_Array_Component
1138 (Arr_Comp
: Node_Id
;
1140 Init_Expr
: Node_Id
;
1143 Exceptions_OK
: constant Boolean :=
1144 not Restriction_Active
1145 (No_Exception_Propagation
);
1147 Finalization_OK
: constant Boolean :=
1149 and then Needs_Finalization
(Comp_Typ
);
1151 Full_Typ
: constant Entity_Id
:= Underlying_Type
(Comp_Typ
);
1153 Blk_Stmts
: List_Id
;
1154 Init_Stmt
: Node_Id
;
1157 -- Protect the initialization statements from aborts. Generate:
1161 if Finalization_OK
and Abort_Allowed
then
1162 if Exceptions_OK
then
1163 Blk_Stmts
:= New_List
;
1168 Append_To
(Blk_Stmts
, Build_Runtime_Call
(Loc
, RE_Abort_Defer
));
1170 -- Otherwise aborts are not allowed. All generated code is added
1171 -- directly to the input list.
1177 -- Initialize the array element. Generate:
1179 -- Arr_Comp := Init_Expr;
1181 -- Note that the initialization expression is replicated because
1182 -- it has to be reevaluated within a generated loop.
1185 Make_OK_Assignment_Statement
(Loc
,
1186 Name
=> New_Copy_Tree
(Arr_Comp
),
1187 Expression
=> New_Copy_Tree
(Init_Expr
));
1188 Set_No_Ctrl_Actions
(Init_Stmt
);
1190 -- If this is an aggregate for an array of arrays, each
1191 -- subaggregate will be expanded as well, and even with
1192 -- No_Ctrl_Actions the assignments of inner components will
1193 -- require attachment in their assignments to temporaries. These
1194 -- temporaries must be finalized for each subaggregate. Generate:
1197 -- Arr_Comp := Init_Expr;
1200 if Finalization_OK
and then Is_Array_Type
(Comp_Typ
) then
1202 Make_Block_Statement
(Loc
,
1203 Handled_Statement_Sequence
=>
1204 Make_Handled_Sequence_Of_Statements
(Loc
,
1205 Statements
=> New_List
(Init_Stmt
)));
1208 Append_To
(Blk_Stmts
, Init_Stmt
);
1210 -- Adjust the tag due to a possible view conversion. Generate:
1212 -- Arr_Comp._tag := Full_TypP;
1214 if Tagged_Type_Expansion
1215 and then Present
(Comp_Typ
)
1216 and then Is_Tagged_Type
(Comp_Typ
)
1218 Append_To
(Blk_Stmts
,
1219 Make_OK_Assignment_Statement
(Loc
,
1221 Make_Selected_Component
(Loc
,
1222 Prefix
=> New_Copy_Tree
(Arr_Comp
),
1225 (First_Tag_Component
(Full_Typ
), Loc
)),
1228 Unchecked_Convert_To
(RTE
(RE_Tag
),
1230 (Node
(First_Elmt
(Access_Disp_Table
(Full_Typ
))),
1234 -- Adjust the array component. Controlled subaggregates are not
1235 -- considered because each of their individual elements will
1236 -- receive an adjustment of its own. Generate:
1238 -- [Deep_]Adjust (Arr_Comp);
1241 and then not Is_Limited_Type
(Comp_Typ
)
1242 and then not Is_Build_In_Place_Function_Call
(Init_Expr
)
1244 (Is_Array_Type
(Comp_Typ
)
1245 and then Is_Controlled
(Component_Type
(Comp_Typ
))
1246 and then Nkind
(Expr
) = N_Aggregate
)
1250 (Obj_Ref
=> New_Copy_Tree
(Arr_Comp
),
1253 -- Guard against a missing [Deep_]Adjust when the component
1254 -- type was not frozen properly.
1256 if Present
(Adj_Call
) then
1257 Append_To
(Blk_Stmts
, Adj_Call
);
1261 -- Complete the protection of the initialization statements
1263 if Finalization_OK
and Abort_Allowed
then
1265 -- Wrap the initialization statements in a block to catch a
1266 -- potential exception. Generate:
1270 -- Arr_Comp := Init_Expr;
1271 -- Arr_Comp._tag := Full_TypP;
1272 -- [Deep_]Adjust (Arr_Comp);
1274 -- Abort_Undefer_Direct;
1277 if Exceptions_OK
then
1279 Build_Abort_Undefer_Block
(Loc
,
1283 -- Otherwise exceptions are not propagated. Generate:
1286 -- Arr_Comp := Init_Expr;
1287 -- Arr_Comp._tag := Full_TypP;
1288 -- [Deep_]Adjust (Arr_Comp);
1292 Append_To
(Blk_Stmts
,
1293 Build_Runtime_Call
(Loc
, RE_Abort_Undefer
));
1296 end Initialize_Array_Component
;
1298 -------------------------------------
1299 -- Initialize_Ctrl_Array_Component --
1300 -------------------------------------
1302 procedure Initialize_Ctrl_Array_Component
1303 (Arr_Comp
: Node_Id
;
1304 Comp_Typ
: Entity_Id
;
1305 Init_Expr
: Node_Id
;
1309 Act_Stmts
: List_Id
;
1312 Hook_Clear
: Node_Id
;
1314 In_Place_Expansion
: Boolean;
1315 -- Flag set when a nonlimited controlled function call requires
1316 -- in-place expansion.
1319 -- Duplicate the initialization expression in case the context is
1320 -- a multi choice list or an "others" choice which plugs various
1321 -- holes in the aggregate. As a result the expression is no longer
1322 -- shared between the various components and is reevaluated for
1323 -- each such component.
1325 Expr
:= New_Copy_Tree
(Init_Expr
);
1326 Set_Parent
(Expr
, Parent
(Init_Expr
));
1328 -- Perform a preliminary analysis and resolution to determine what
1329 -- the initialization expression denotes. An unanalyzed function
1330 -- call may appear as an identifier or an indexed component.
1332 if Nkind_In
(Expr
, N_Function_Call
,
1334 N_Indexed_Component
)
1335 and then not Analyzed
(Expr
)
1337 Preanalyze_And_Resolve
(Expr
, Comp_Typ
);
1340 In_Place_Expansion
:=
1341 Nkind
(Expr
) = N_Function_Call
1342 and then not Is_Build_In_Place_Result_Type
(Comp_Typ
);
1344 -- The initialization expression is a controlled function call.
1345 -- Perform in-place removal of side effects to avoid creating a
1346 -- transient scope, which leads to premature finalization.
1348 -- This in-place expansion is not performed for limited transient
1349 -- objects, because the initialization is already done in place.
1351 if In_Place_Expansion
then
1353 -- Suppress the removal of side effects by general analysis,
1354 -- because this behavior is emulated here. This avoids the
1355 -- generation of a transient scope, which leads to out-of-order
1356 -- adjustment and finalization.
1358 Set_No_Side_Effect_Removal
(Expr
);
1360 -- When the transient component initialization is related to a
1361 -- range or an "others", keep all generated statements within
1362 -- the enclosing loop. This way the controlled function call
1363 -- will be evaluated at each iteration, and its result will be
1364 -- finalized at the end of each iteration.
1370 -- Otherwise this is a single component initialization. Hook-
1371 -- related statements are inserted prior to the aggregate.
1375 Act_Stmts
:= No_List
;
1378 -- Install all hook-related declarations and prepare the clean
1381 Process_Transient_Component
1383 Comp_Typ
=> Comp_Typ
,
1385 Fin_Call
=> Fin_Call
,
1386 Hook_Clear
=> Hook_Clear
,
1388 Stmts
=> Act_Stmts
);
1391 -- Use the noncontrolled component initialization circuitry to
1392 -- assign the result of the function call to the array element.
1393 -- This also performs subaggregate wrapping, tag adjustment, and
1394 -- [deep] adjustment of the array element.
1396 Initialize_Array_Component
1397 (Arr_Comp
=> Arr_Comp
,
1398 Comp_Typ
=> Comp_Typ
,
1402 -- At this point the array element is fully initialized. Complete
1403 -- the processing of the controlled array component by finalizing
1404 -- the transient function result.
1406 if In_Place_Expansion
then
1407 Process_Transient_Component_Completion
1410 Fin_Call
=> Fin_Call
,
1411 Hook_Clear
=> Hook_Clear
,
1414 end Initialize_Ctrl_Array_Component
;
1418 Stmts
: constant List_Id
:= New_List
;
1420 Comp_Typ
: Entity_Id
:= Empty
;
1422 Indexed_Comp
: Node_Id
;
1423 Init_Call
: Node_Id
;
1424 New_Indexes
: List_Id
;
1426 -- Start of processing for Gen_Assign
1429 if No
(Indexes
) then
1430 New_Indexes
:= New_List
;
1432 New_Indexes
:= New_Copy_List_Tree
(Indexes
);
1435 Append_To
(New_Indexes
, Ind
);
1437 if Present
(Next_Index
(Index
)) then
1440 Build_Array_Aggr_Code
1443 Index
=> Next_Index
(Index
),
1445 Scalar_Comp
=> Scalar_Comp
,
1446 Indexes
=> New_Indexes
));
1449 -- If we get here then we are at a bottom-level (sub-)aggregate
1453 (Make_Indexed_Component
(Loc
,
1454 Prefix
=> New_Copy_Tree
(Into
),
1455 Expressions
=> New_Indexes
));
1457 Set_Assignment_OK
(Indexed_Comp
);
1459 -- Ada 2005 (AI-287): In case of default initialized component, Expr
1460 -- is not present (and therefore we also initialize Expr_Q to empty).
1464 elsif Nkind
(Expr
) = N_Qualified_Expression
then
1465 Expr_Q
:= Expression
(Expr
);
1470 if Present
(Etype
(N
)) and then Etype
(N
) /= Any_Composite
then
1471 Comp_Typ
:= Component_Type
(Etype
(N
));
1472 pragma Assert
(Comp_Typ
= Ctype
); -- AI-287
1474 elsif Present
(Next
(First
(New_Indexes
))) then
1476 -- Ada 2005 (AI-287): Do nothing in case of default initialized
1477 -- component because we have received the component type in
1478 -- the formal parameter Ctype.
1480 -- ??? Some assert pragmas have been added to check if this new
1481 -- formal can be used to replace this code in all cases.
1483 if Present
(Expr
) then
1485 -- This is a multidimensional array. Recover the component type
1486 -- from the outermost aggregate, because subaggregates do not
1487 -- have an assigned type.
1494 while Present
(P
) loop
1495 if Nkind
(P
) = N_Aggregate
1496 and then Present
(Etype
(P
))
1498 Comp_Typ
:= Component_Type
(Etype
(P
));
1506 pragma Assert
(Comp_Typ
= Ctype
); -- AI-287
1511 -- Ada 2005 (AI-287): We only analyze the expression in case of non-
1512 -- default initialized components (otherwise Expr_Q is not present).
1515 and then Nkind_In
(Expr_Q
, N_Aggregate
, N_Extension_Aggregate
)
1517 -- At this stage the Expression may not have been analyzed yet
1518 -- because the array aggregate code has not been updated to use
1519 -- the Expansion_Delayed flag and avoid analysis altogether to
1520 -- solve the same problem (see Resolve_Aggr_Expr). So let us do
1521 -- the analysis of non-array aggregates now in order to get the
1522 -- value of Expansion_Delayed flag for the inner aggregate ???
1524 -- In the case of an iterated component association, the analysis
1525 -- of the generated loop will analyze the expression in the
1526 -- proper context, in which the loop parameter is visible.
1528 if Present
(Comp_Typ
) and then not Is_Array_Type
(Comp_Typ
) then
1529 if Nkind
(Parent
(Expr_Q
)) = N_Iterated_Component_Association
1530 or else Nkind
(Parent
(Parent
((Expr_Q
)))) =
1531 N_Iterated_Component_Association
1535 Analyze_And_Resolve
(Expr_Q
, Comp_Typ
);
1539 if Is_Delayed_Aggregate
(Expr_Q
) then
1541 -- This is either a subaggregate of a multidimensional array,
1542 -- or a component of an array type whose component type is
1543 -- also an array. In the latter case, the expression may have
1544 -- component associations that provide different bounds from
1545 -- those of the component type, and sliding must occur. Instead
1546 -- of decomposing the current aggregate assignment, force the
1547 -- reanalysis of the assignment, so that a temporary will be
1548 -- generated in the usual fashion, and sliding will take place.
1550 if Nkind
(Parent
(N
)) = N_Assignment_Statement
1551 and then Is_Array_Type
(Comp_Typ
)
1552 and then Present
(Component_Associations
(Expr_Q
))
1553 and then Must_Slide
(Comp_Typ
, Etype
(Expr_Q
))
1555 Set_Expansion_Delayed
(Expr_Q
, False);
1556 Set_Analyzed
(Expr_Q
, False);
1561 Late_Expansion
(Expr_Q
, Etype
(Expr_Q
), Indexed_Comp
));
1566 if Present
(Expr
) then
1568 -- Handle an initialization expression of a controlled type in
1569 -- case it denotes a function call. In general such a scenario
1570 -- will produce a transient scope, but this will lead to wrong
1571 -- order of initialization, adjustment, and finalization in the
1572 -- context of aggregates.
1574 -- Target (1) := Ctrl_Func_Call;
1577 -- Trans_Obj : ... := Ctrl_Func_Call; -- object
1578 -- Target (1) := Trans_Obj;
1579 -- Finalize (Trans_Obj);
1581 -- Target (1)._tag := ...;
1582 -- Adjust (Target (1));
1584 -- In the example above, the call to Finalize occurs too early
1585 -- and as a result it may leave the array component in a bad
1586 -- state. Finalization of the transient object should really
1587 -- happen after adjustment.
1589 -- To avoid this scenario, perform in-place side-effect removal
1590 -- of the function call. This eliminates the transient property
1591 -- of the function result and ensures correct order of actions.
1593 -- Res : ... := Ctrl_Func_Call;
1594 -- Target (1) := Res;
1595 -- Target (1)._tag := ...;
1596 -- Adjust (Target (1));
1599 if Present
(Comp_Typ
)
1600 and then Needs_Finalization
(Comp_Typ
)
1601 and then Nkind
(Expr
) /= N_Aggregate
1603 Initialize_Ctrl_Array_Component
1604 (Arr_Comp
=> Indexed_Comp
,
1605 Comp_Typ
=> Comp_Typ
,
1609 -- Otherwise perform simple component initialization
1612 Initialize_Array_Component
1613 (Arr_Comp
=> Indexed_Comp
,
1614 Comp_Typ
=> Comp_Typ
,
1619 -- Ada 2005 (AI-287): In case of default initialized component, call
1620 -- the initialization subprogram associated with the component type.
1621 -- If the component type is an access type, add an explicit null
1622 -- assignment, because for the back-end there is an initialization
1623 -- present for the whole aggregate, and no default initialization
1626 -- In addition, if the component type is controlled, we must call
1627 -- its Initialize procedure explicitly, because there is no explicit
1628 -- object creation that will invoke it otherwise.
1631 if Present
(Base_Init_Proc
(Base_Type
(Ctype
)))
1632 or else Has_Task
(Base_Type
(Ctype
))
1634 Append_List_To
(Stmts
,
1635 Build_Initialization_Call
(Loc
,
1636 Id_Ref
=> Indexed_Comp
,
1638 With_Default_Init
=> True));
1640 -- If the component type has invariants, add an invariant
1641 -- check after the component is default-initialized. It will
1642 -- be analyzed and resolved before the code for initialization
1643 -- of other components.
1645 if Has_Invariants
(Ctype
) then
1646 Set_Etype
(Indexed_Comp
, Ctype
);
1647 Append_To
(Stmts
, Make_Invariant_Call
(Indexed_Comp
));
1650 elsif Is_Access_Type
(Ctype
) then
1652 Make_Assignment_Statement
(Loc
,
1653 Name
=> New_Copy_Tree
(Indexed_Comp
),
1654 Expression
=> Make_Null
(Loc
)));
1657 if Needs_Finalization
(Ctype
) then
1660 (Obj_Ref
=> New_Copy_Tree
(Indexed_Comp
),
1663 -- Guard against a missing [Deep_]Initialize when the component
1664 -- type was not properly frozen.
1666 if Present
(Init_Call
) then
1667 Append_To
(Stmts
, Init_Call
);
1672 return Add_Loop_Actions
(Stmts
);
1679 function Gen_Loop
(L
, H
: Node_Id
; Expr
: Node_Id
) return List_Id
is
1680 Is_Iterated_Component
: constant Boolean :=
1681 Nkind
(Parent
(Expr
)) = N_Iterated_Component_Association
;
1692 -- Index_Base'(L) .. Index_Base'(H)
1694 L_Iteration_Scheme
: Node_Id
;
1695 -- L_J in Index_Base'(L) .. Index_Base'(H)
1698 -- The statements to execute in the loop
1700 S
: constant List_Id
:= New_List
;
1701 -- List of statements
1704 -- Copy of expression tree, used for checking purposes
1707 -- If loop bounds define an empty range return the null statement
1709 if Empty_Range
(L
, H
) then
1710 Append_To
(S
, Make_Null_Statement
(Loc
));
1712 -- Ada 2005 (AI-287): Nothing else need to be done in case of
1713 -- default initialized component.
1719 -- The expression must be type-checked even though no component
1720 -- of the aggregate will have this value. This is done only for
1721 -- actual components of the array, not for subaggregates. Do
1722 -- the check on a copy, because the expression may be shared
1723 -- among several choices, some of which might be non-null.
1725 if Present
(Etype
(N
))
1726 and then Is_Array_Type
(Etype
(N
))
1727 and then No
(Next_Index
(Index
))
1729 Expander_Mode_Save_And_Set
(False);
1730 Tcopy
:= New_Copy_Tree
(Expr
);
1731 Set_Parent
(Tcopy
, N
);
1732 Analyze_And_Resolve
(Tcopy
, Component_Type
(Etype
(N
)));
1733 Expander_Mode_Restore
;
1739 -- If loop bounds are the same then generate an assignment, unless
1740 -- the parent construct is an Iterated_Component_Association.
1742 elsif Equal
(L
, H
) and then not Is_Iterated_Component
then
1743 return Gen_Assign
(New_Copy_Tree
(L
), Expr
);
1745 -- If H - L <= 2 then generate a sequence of assignments when we are
1746 -- processing the bottom most aggregate and it contains scalar
1749 elsif No
(Next_Index
(Index
))
1750 and then Scalar_Comp
1751 and then Local_Compile_Time_Known_Value
(L
)
1752 and then Local_Compile_Time_Known_Value
(H
)
1753 and then Local_Expr_Value
(H
) - Local_Expr_Value
(L
) <= 2
1754 and then not Is_Iterated_Component
1756 Append_List_To
(S
, Gen_Assign
(New_Copy_Tree
(L
), Expr
));
1757 Append_List_To
(S
, Gen_Assign
(Add
(1, To
=> L
), Expr
));
1759 if Local_Expr_Value
(H
) - Local_Expr_Value
(L
) = 2 then
1760 Append_List_To
(S
, Gen_Assign
(Add
(2, To
=> L
), Expr
));
1766 -- Otherwise construct the loop, starting with the loop index L_J
1768 if Is_Iterated_Component
then
1770 Make_Defining_Identifier
(Loc
,
1771 Chars
=> (Chars
(Defining_Identifier
(Parent
(Expr
)))));
1774 L_J
:= Make_Temporary
(Loc
, 'J', L
);
1777 -- Construct "L .. H" in Index_Base. We use a qualified expression
1778 -- for the bound to convert to the index base, but we don't need
1779 -- to do that if we already have the base type at hand.
1781 if Etype
(L
) = Index_Base
then
1785 Make_Qualified_Expression
(Loc
,
1786 Subtype_Mark
=> Index_Base_Name
,
1787 Expression
=> New_Copy_Tree
(L
));
1790 if Etype
(H
) = Index_Base
then
1794 Make_Qualified_Expression
(Loc
,
1795 Subtype_Mark
=> Index_Base_Name
,
1796 Expression
=> New_Copy_Tree
(H
));
1804 -- Construct "for L_J in Index_Base range L .. H"
1806 L_Iteration_Scheme
:=
1807 Make_Iteration_Scheme
1809 Loop_Parameter_Specification
=>
1810 Make_Loop_Parameter_Specification
1812 Defining_Identifier
=> L_J
,
1813 Discrete_Subtype_Definition
=> L_Range
));
1815 -- Construct the statements to execute in the loop body
1818 Gen_Assign
(New_Occurrence_Of
(L_J
, Loc
), Expr
, In_Loop
=> True);
1820 -- Construct the final loop
1823 Make_Implicit_Loop_Statement
1825 Identifier
=> Empty
,
1826 Iteration_Scheme
=> L_Iteration_Scheme
,
1827 Statements
=> L_Body
));
1829 -- A small optimization: if the aggregate is initialized with a box
1830 -- and the component type has no initialization procedure, remove the
1831 -- useless empty loop.
1833 if Nkind
(First
(S
)) = N_Loop_Statement
1834 and then Is_Empty_List
(Statements
(First
(S
)))
1836 return New_List
(Make_Null_Statement
(Loc
));
1846 -- The code built is
1848 -- W_J : Index_Base := L;
1849 -- while W_J < H loop
1850 -- W_J := Index_Base'Succ (W);
1854 function Gen_While
(L
, H
: Node_Id
; Expr
: Node_Id
) return List_Id
is
1858 -- W_J : Base_Type := L;
1860 W_Iteration_Scheme
: Node_Id
;
1863 W_Index_Succ
: Node_Id
;
1864 -- Index_Base'Succ (J)
1866 W_Increment
: Node_Id
;
1867 -- W_J := Index_Base'Succ (W)
1869 W_Body
: constant List_Id
:= New_List
;
1870 -- The statements to execute in the loop
1872 S
: constant List_Id
:= New_List
;
1873 -- list of statement
1876 -- If loop bounds define an empty range or are equal return null
1878 if Empty_Range
(L
, H
) or else Equal
(L
, H
) then
1879 Append_To
(S
, Make_Null_Statement
(Loc
));
1883 -- Build the decl of W_J
1885 W_J
:= Make_Temporary
(Loc
, 'J', L
);
1887 Make_Object_Declaration
1889 Defining_Identifier
=> W_J
,
1890 Object_Definition
=> Index_Base_Name
,
1893 -- Theoretically we should do a New_Copy_Tree (L) here, but we know
1894 -- that in this particular case L is a fresh Expr generated by
1895 -- Add which we are the only ones to use.
1897 Append_To
(S
, W_Decl
);
1899 -- Construct " while W_J < H"
1901 W_Iteration_Scheme
:=
1902 Make_Iteration_Scheme
1904 Condition
=> Make_Op_Lt
1906 Left_Opnd
=> New_Occurrence_Of
(W_J
, Loc
),
1907 Right_Opnd
=> New_Copy_Tree
(H
)));
1909 -- Construct the statements to execute in the loop body
1912 Make_Attribute_Reference
1914 Prefix
=> Index_Base_Name
,
1915 Attribute_Name
=> Name_Succ
,
1916 Expressions
=> New_List
(New_Occurrence_Of
(W_J
, Loc
)));
1919 Make_OK_Assignment_Statement
1921 Name
=> New_Occurrence_Of
(W_J
, Loc
),
1922 Expression
=> W_Index_Succ
);
1924 Append_To
(W_Body
, W_Increment
);
1926 Append_List_To
(W_Body
,
1927 Gen_Assign
(New_Occurrence_Of
(W_J
, Loc
), Expr
, In_Loop
=> True));
1929 -- Construct the final loop
1932 Make_Implicit_Loop_Statement
1934 Identifier
=> Empty
,
1935 Iteration_Scheme
=> W_Iteration_Scheme
,
1936 Statements
=> W_Body
));
1941 --------------------
1942 -- Get_Assoc_Expr --
1943 --------------------
1945 function Get_Assoc_Expr
(Assoc
: Node_Id
) return Node_Id
is
1946 Typ
: constant Entity_Id
:= Base_Type
(Etype
(N
));
1949 if Box_Present
(Assoc
) then
1950 if Is_Scalar_Type
(Ctype
) then
1951 if Present
(Default_Aspect_Component_Value
(Typ
)) then
1952 return Default_Aspect_Component_Value
(Typ
);
1953 elsif Present
(Default_Aspect_Value
(Ctype
)) then
1954 return Default_Aspect_Value
(Ctype
);
1964 return Expression
(Assoc
);
1968 ---------------------
1969 -- Index_Base_Name --
1970 ---------------------
1972 function Index_Base_Name
return Node_Id
is
1974 return New_Occurrence_Of
(Index_Base
, Sloc
(N
));
1975 end Index_Base_Name
;
1977 ------------------------------------
1978 -- Local_Compile_Time_Known_Value --
1979 ------------------------------------
1981 function Local_Compile_Time_Known_Value
(E
: Node_Id
) return Boolean is
1983 return Compile_Time_Known_Value
(E
)
1985 (Nkind
(E
) = N_Attribute_Reference
1986 and then Attribute_Name
(E
) = Name_Val
1987 and then Compile_Time_Known_Value
(First
(Expressions
(E
))));
1988 end Local_Compile_Time_Known_Value
;
1990 ----------------------
1991 -- Local_Expr_Value --
1992 ----------------------
1994 function Local_Expr_Value
(E
: Node_Id
) return Uint
is
1996 if Compile_Time_Known_Value
(E
) then
1997 return Expr_Value
(E
);
1999 return Expr_Value
(First
(Expressions
(E
)));
2001 end Local_Expr_Value
;
2005 New_Code
: constant List_Id
:= New_List
;
2007 Aggr_L
: constant Node_Id
:= Low_Bound
(Aggregate_Bounds
(N
));
2008 Aggr_H
: constant Node_Id
:= High_Bound
(Aggregate_Bounds
(N
));
2009 -- The aggregate bounds of this specific subaggregate. Note that if the
2010 -- code generated by Build_Array_Aggr_Code is executed then these bounds
2011 -- are OK. Otherwise a Constraint_Error would have been raised.
2013 Aggr_Low
: constant Node_Id
:= Duplicate_Subexpr_No_Checks
(Aggr_L
);
2014 Aggr_High
: constant Node_Id
:= Duplicate_Subexpr_No_Checks
(Aggr_H
);
2015 -- After Duplicate_Subexpr these are side-effect free
2024 Nb_Choices
: Nat
:= 0;
2025 Table
: Case_Table_Type
(1 .. Number_Of_Choices
(N
));
2026 -- Used to sort all the different choice values
2029 -- Number of elements in the positional aggregate
2031 Others_Assoc
: Node_Id
:= Empty
;
2033 -- Start of processing for Build_Array_Aggr_Code
2036 -- First before we start, a special case. if we have a bit packed
2037 -- array represented as a modular type, then clear the value to
2038 -- zero first, to ensure that unused bits are properly cleared.
2043 and then Is_Bit_Packed_Array
(Typ
)
2044 and then Is_Modular_Integer_Type
(Packed_Array_Impl_Type
(Typ
))
2046 Append_To
(New_Code
,
2047 Make_Assignment_Statement
(Loc
,
2048 Name
=> New_Copy_Tree
(Into
),
2050 Unchecked_Convert_To
(Typ
,
2051 Make_Integer_Literal
(Loc
, Uint_0
))));
2054 -- If the component type contains tasks, we need to build a Master
2055 -- entity in the current scope, because it will be needed if build-
2056 -- in-place functions are called in the expanded code.
2058 if Nkind
(Parent
(N
)) = N_Object_Declaration
and then Has_Task
(Typ
) then
2059 Build_Master_Entity
(Defining_Identifier
(Parent
(N
)));
2062 -- STEP 1: Process component associations
2064 -- For those associations that may generate a loop, initialize
2065 -- Loop_Actions to collect inserted actions that may be crated.
2067 -- Skip this if no component associations
2069 if No
(Expressions
(N
)) then
2071 -- STEP 1 (a): Sort the discrete choices
2073 Assoc
:= First
(Component_Associations
(N
));
2074 while Present
(Assoc
) loop
2075 Choice
:= First
(Choice_List
(Assoc
));
2076 while Present
(Choice
) loop
2077 if Nkind
(Choice
) = N_Others_Choice
then
2078 Others_Assoc
:= Assoc
;
2082 Get_Index_Bounds
(Choice
, Low
, High
);
2085 Set_Loop_Actions
(Assoc
, New_List
);
2088 Nb_Choices
:= Nb_Choices
+ 1;
2090 Table
(Nb_Choices
) :=
2093 Choice_Node
=> Get_Assoc_Expr
(Assoc
));
2101 -- If there is more than one set of choices these must be static
2102 -- and we can therefore sort them. Remember that Nb_Choices does not
2103 -- account for an others choice.
2105 if Nb_Choices
> 1 then
2106 Sort_Case_Table
(Table
);
2109 -- STEP 1 (b): take care of the whole set of discrete choices
2111 for J
in 1 .. Nb_Choices
loop
2112 Low
:= Table
(J
).Choice_Lo
;
2113 High
:= Table
(J
).Choice_Hi
;
2114 Expr
:= Table
(J
).Choice_Node
;
2115 Append_List
(Gen_Loop
(Low
, High
, Expr
), To
=> New_Code
);
2118 -- STEP 1 (c): generate the remaining loops to cover others choice
2119 -- We don't need to generate loops over empty gaps, but if there is
2120 -- a single empty range we must analyze the expression for semantics
2122 if Present
(Others_Assoc
) then
2124 First
: Boolean := True;
2128 for J
in 0 .. Nb_Choices
loop
2132 Low
:= Add
(1, To
=> Table
(J
).Choice_Hi
);
2135 if J
= Nb_Choices
then
2138 High
:= Add
(-1, To
=> Table
(J
+ 1).Choice_Lo
);
2141 -- If this is an expansion within an init proc, make
2142 -- sure that discriminant references are replaced by
2143 -- the corresponding discriminal.
2145 if Inside_Init_Proc
then
2146 if Is_Entity_Name
(Low
)
2147 and then Ekind
(Entity
(Low
)) = E_Discriminant
2149 Set_Entity
(Low
, Discriminal
(Entity
(Low
)));
2152 if Is_Entity_Name
(High
)
2153 and then Ekind
(Entity
(High
)) = E_Discriminant
2155 Set_Entity
(High
, Discriminal
(Entity
(High
)));
2160 or else not Empty_Range
(Low
, High
)
2164 -- Duplicate the expression in case we will be generating
2165 -- several loops. As a result the expression is no longer
2166 -- shared between the loops and is reevaluated for each
2169 Expr
:= Get_Assoc_Expr
(Others_Assoc
);
2170 Dup_Expr
:= New_Copy_Tree
(Expr
);
2171 Set_Parent
(Dup_Expr
, Parent
(Expr
));
2173 Set_Loop_Actions
(Others_Assoc
, New_List
);
2175 (Gen_Loop
(Low
, High
, Dup_Expr
), To
=> New_Code
);
2181 -- STEP 2: Process positional components
2184 -- STEP 2 (a): Generate the assignments for each positional element
2185 -- Note that here we have to use Aggr_L rather than Aggr_Low because
2186 -- Aggr_L is analyzed and Add wants an analyzed expression.
2188 Expr
:= First
(Expressions
(N
));
2190 while Present
(Expr
) loop
2191 Nb_Elements
:= Nb_Elements
+ 1;
2192 Append_List
(Gen_Assign
(Add
(Nb_Elements
, To
=> Aggr_L
), Expr
),
2197 -- STEP 2 (b): Generate final loop if an others choice is present
2198 -- Here Nb_Elements gives the offset of the last positional element.
2200 if Present
(Component_Associations
(N
)) then
2201 Assoc
:= Last
(Component_Associations
(N
));
2203 -- Ada 2005 (AI-287)
2205 Append_List
(Gen_While
(Add
(Nb_Elements
, To
=> Aggr_L
),
2207 Get_Assoc_Expr
(Assoc
)), -- AI-287
2213 end Build_Array_Aggr_Code
;
2215 ----------------------------
2216 -- Build_Record_Aggr_Code --
2217 ----------------------------
2219 function Build_Record_Aggr_Code
2222 Lhs
: Node_Id
) return List_Id
2224 Loc
: constant Source_Ptr
:= Sloc
(N
);
2225 L
: constant List_Id
:= New_List
;
2226 N_Typ
: constant Entity_Id
:= Etype
(N
);
2232 Comp_Type
: Entity_Id
;
2233 Selector
: Entity_Id
;
2234 Comp_Expr
: Node_Id
;
2237 -- If this is an internal aggregate, the External_Final_List is an
2238 -- expression for the controller record of the enclosing type.
2240 -- If the current aggregate has several controlled components, this
2241 -- expression will appear in several calls to attach to the finali-
2242 -- zation list, and it must not be shared.
2244 Ancestor_Is_Expression
: Boolean := False;
2245 Ancestor_Is_Subtype_Mark
: Boolean := False;
2247 Init_Typ
: Entity_Id
:= Empty
;
2249 Finalization_Done
: Boolean := False;
2250 -- True if Generate_Finalization_Actions has already been called; calls
2251 -- after the first do nothing.
2253 function Ancestor_Discriminant_Value
(Disc
: Entity_Id
) return Node_Id
;
2254 -- Returns the value that the given discriminant of an ancestor type
2255 -- should receive (in the absence of a conflict with the value provided
2256 -- by an ancestor part of an extension aggregate).
2258 procedure Check_Ancestor_Discriminants
(Anc_Typ
: Entity_Id
);
2259 -- Check that each of the discriminant values defined by the ancestor
2260 -- part of an extension aggregate match the corresponding values
2261 -- provided by either an association of the aggregate or by the
2262 -- constraint imposed by a parent type (RM95-4.3.2(8)).
2264 function Compatible_Int_Bounds
2265 (Agg_Bounds
: Node_Id
;
2266 Typ_Bounds
: Node_Id
) return Boolean;
2267 -- Return true if Agg_Bounds are equal or within Typ_Bounds. It is
2268 -- assumed that both bounds are integer ranges.
2270 procedure Generate_Finalization_Actions
;
2271 -- Deal with the various controlled type data structure initializations
2272 -- (but only if it hasn't been done already).
2274 function Get_Constraint_Association
(T
: Entity_Id
) return Node_Id
;
2275 -- Returns the first discriminant association in the constraint
2276 -- associated with T, if any, otherwise returns Empty.
2278 function Get_Explicit_Discriminant_Value
(D
: Entity_Id
) return Node_Id
;
2279 -- If the ancestor part is an unconstrained type and further ancestors
2280 -- do not provide discriminants for it, check aggregate components for
2281 -- values of the discriminants.
2283 procedure Init_Hidden_Discriminants
(Typ
: Entity_Id
; List
: List_Id
);
2284 -- If Typ is derived, and constrains discriminants of the parent type,
2285 -- these discriminants are not components of the aggregate, and must be
2286 -- initialized. The assignments are appended to List. The same is done
2287 -- if Typ derives fron an already constrained subtype of a discriminated
2290 procedure Init_Stored_Discriminants
;
2291 -- If the type is derived and has inherited discriminants, generate
2292 -- explicit assignments for each, using the store constraint of the
2293 -- type. Note that both visible and stored discriminants must be
2294 -- initialized in case the derived type has some renamed and some
2295 -- constrained discriminants.
2297 procedure Init_Visible_Discriminants
;
2298 -- If type has discriminants, retrieve their values from aggregate,
2299 -- and generate explicit assignments for each. This does not include
2300 -- discriminants inherited from ancestor, which are handled above.
2301 -- The type of the aggregate is a subtype created ealier using the
2302 -- given values of the discriminant components of the aggregate.
2304 procedure Initialize_Ctrl_Record_Component
2305 (Rec_Comp
: Node_Id
;
2306 Comp_Typ
: Entity_Id
;
2307 Init_Expr
: Node_Id
;
2309 -- Perform the initialization of controlled record component Rec_Comp.
2310 -- Comp_Typ is the component type. Init_Expr is the initialization
2311 -- expression for the record component. Hook-related declarations are
2312 -- inserted prior to aggregate N using Insert_Action. All remaining
2313 -- generated code is added to list Stmts.
2315 procedure Initialize_Record_Component
2316 (Rec_Comp
: Node_Id
;
2317 Comp_Typ
: Entity_Id
;
2318 Init_Expr
: Node_Id
;
2320 -- Perform the initialization of record component Rec_Comp. Comp_Typ
2321 -- is the component type. Init_Expr is the initialization expression
2322 -- of the record component. All generated code is added to list Stmts.
2324 function Is_Int_Range_Bounds
(Bounds
: Node_Id
) return Boolean;
2325 -- Check whether Bounds is a range node and its lower and higher bounds
2326 -- are integers literals.
2328 function Replace_Type
(Expr
: Node_Id
) return Traverse_Result
;
2329 -- If the aggregate contains a self-reference, traverse each expression
2330 -- to replace a possible self-reference with a reference to the proper
2331 -- component of the target of the assignment.
2333 function Rewrite_Discriminant
(Expr
: Node_Id
) return Traverse_Result
;
2334 -- If default expression of a component mentions a discriminant of the
2335 -- type, it must be rewritten as the discriminant of the target object.
2337 ---------------------------------
2338 -- Ancestor_Discriminant_Value --
2339 ---------------------------------
2341 function Ancestor_Discriminant_Value
(Disc
: Entity_Id
) return Node_Id
is
2343 Assoc_Elmt
: Elmt_Id
;
2344 Aggr_Comp
: Entity_Id
;
2345 Corresp_Disc
: Entity_Id
;
2346 Current_Typ
: Entity_Id
:= Base_Type
(Typ
);
2347 Parent_Typ
: Entity_Id
;
2348 Parent_Disc
: Entity_Id
;
2349 Save_Assoc
: Node_Id
:= Empty
;
2352 -- First check any discriminant associations to see if any of them
2353 -- provide a value for the discriminant.
2355 if Present
(Discriminant_Specifications
(Parent
(Current_Typ
))) then
2356 Assoc
:= First
(Component_Associations
(N
));
2357 while Present
(Assoc
) loop
2358 Aggr_Comp
:= Entity
(First
(Choices
(Assoc
)));
2360 if Ekind
(Aggr_Comp
) = E_Discriminant
then
2361 Save_Assoc
:= Expression
(Assoc
);
2363 Corresp_Disc
:= Corresponding_Discriminant
(Aggr_Comp
);
2364 while Present
(Corresp_Disc
) loop
2366 -- If found a corresponding discriminant then return the
2367 -- value given in the aggregate. (Note: this is not
2368 -- correct in the presence of side effects. ???)
2370 if Disc
= Corresp_Disc
then
2371 return Duplicate_Subexpr
(Expression
(Assoc
));
2374 Corresp_Disc
:= Corresponding_Discriminant
(Corresp_Disc
);
2382 -- No match found in aggregate, so chain up parent types to find
2383 -- a constraint that defines the value of the discriminant.
2385 Parent_Typ
:= Etype
(Current_Typ
);
2386 while Current_Typ
/= Parent_Typ
loop
2387 if Has_Discriminants
(Parent_Typ
)
2388 and then not Has_Unknown_Discriminants
(Parent_Typ
)
2390 Parent_Disc
:= First_Discriminant
(Parent_Typ
);
2392 -- We either get the association from the subtype indication
2393 -- of the type definition itself, or from the discriminant
2394 -- constraint associated with the type entity (which is
2395 -- preferable, but it's not always present ???)
2397 if Is_Empty_Elmt_List
(Discriminant_Constraint
(Current_Typ
))
2399 Assoc
:= Get_Constraint_Association
(Current_Typ
);
2400 Assoc_Elmt
:= No_Elmt
;
2403 First_Elmt
(Discriminant_Constraint
(Current_Typ
));
2404 Assoc
:= Node
(Assoc_Elmt
);
2407 -- Traverse the discriminants of the parent type looking
2408 -- for one that corresponds.
2410 while Present
(Parent_Disc
) and then Present
(Assoc
) loop
2411 Corresp_Disc
:= Parent_Disc
;
2412 while Present
(Corresp_Disc
)
2413 and then Disc
/= Corresp_Disc
2415 Corresp_Disc
:= Corresponding_Discriminant
(Corresp_Disc
);
2418 if Disc
= Corresp_Disc
then
2419 if Nkind
(Assoc
) = N_Discriminant_Association
then
2420 Assoc
:= Expression
(Assoc
);
2423 -- If the located association directly denotes
2424 -- a discriminant, then use the value of a saved
2425 -- association of the aggregate. This is an approach
2426 -- used to handle certain cases involving multiple
2427 -- discriminants mapped to a single discriminant of
2428 -- a descendant. It's not clear how to locate the
2429 -- appropriate discriminant value for such cases. ???
2431 if Is_Entity_Name
(Assoc
)
2432 and then Ekind
(Entity
(Assoc
)) = E_Discriminant
2434 Assoc
:= Save_Assoc
;
2437 return Duplicate_Subexpr
(Assoc
);
2440 Next_Discriminant
(Parent_Disc
);
2442 if No
(Assoc_Elmt
) then
2446 Next_Elmt
(Assoc_Elmt
);
2448 if Present
(Assoc_Elmt
) then
2449 Assoc
:= Node
(Assoc_Elmt
);
2457 Current_Typ
:= Parent_Typ
;
2458 Parent_Typ
:= Etype
(Current_Typ
);
2461 -- In some cases there's no ancestor value to locate (such as
2462 -- when an ancestor part given by an expression defines the
2463 -- discriminant value).
2466 end Ancestor_Discriminant_Value
;
2468 ----------------------------------
2469 -- Check_Ancestor_Discriminants --
2470 ----------------------------------
2472 procedure Check_Ancestor_Discriminants
(Anc_Typ
: Entity_Id
) is
2474 Disc_Value
: Node_Id
;
2478 Discr
:= First_Discriminant
(Base_Type
(Anc_Typ
));
2479 while Present
(Discr
) loop
2480 Disc_Value
:= Ancestor_Discriminant_Value
(Discr
);
2482 if Present
(Disc_Value
) then
2483 Cond
:= Make_Op_Ne
(Loc
,
2485 Make_Selected_Component
(Loc
,
2486 Prefix
=> New_Copy_Tree
(Target
),
2487 Selector_Name
=> New_Occurrence_Of
(Discr
, Loc
)),
2488 Right_Opnd
=> Disc_Value
);
2491 Make_Raise_Constraint_Error
(Loc
,
2493 Reason
=> CE_Discriminant_Check_Failed
));
2496 Next_Discriminant
(Discr
);
2498 end Check_Ancestor_Discriminants
;
2500 ---------------------------
2501 -- Compatible_Int_Bounds --
2502 ---------------------------
2504 function Compatible_Int_Bounds
2505 (Agg_Bounds
: Node_Id
;
2506 Typ_Bounds
: Node_Id
) return Boolean
2508 Agg_Lo
: constant Uint
:= Intval
(Low_Bound
(Agg_Bounds
));
2509 Agg_Hi
: constant Uint
:= Intval
(High_Bound
(Agg_Bounds
));
2510 Typ_Lo
: constant Uint
:= Intval
(Low_Bound
(Typ_Bounds
));
2511 Typ_Hi
: constant Uint
:= Intval
(High_Bound
(Typ_Bounds
));
2513 return Typ_Lo
<= Agg_Lo
and then Agg_Hi
<= Typ_Hi
;
2514 end Compatible_Int_Bounds
;
2516 -----------------------------------
2517 -- Generate_Finalization_Actions --
2518 -----------------------------------
2520 procedure Generate_Finalization_Actions
is
2522 -- Do the work only the first time this is called
2524 if Finalization_Done
then
2528 Finalization_Done
:= True;
2530 -- Determine the external finalization list. It is either the
2531 -- finalization list of the outer scope or the one coming from an
2532 -- outer aggregate. When the target is not a temporary, the proper
2533 -- scope is the scope of the target rather than the potentially
2534 -- transient current scope.
2536 if Is_Controlled
(Typ
) and then Ancestor_Is_Subtype_Mark
then
2537 Ref
:= Convert_To
(Init_Typ
, New_Copy_Tree
(Target
));
2538 Set_Assignment_OK
(Ref
);
2541 Make_Procedure_Call_Statement
(Loc
,
2544 (Find_Prim_Op
(Init_Typ
, Name_Initialize
), Loc
),
2545 Parameter_Associations
=> New_List
(New_Copy_Tree
(Ref
))));
2547 end Generate_Finalization_Actions
;
2549 --------------------------------
2550 -- Get_Constraint_Association --
2551 --------------------------------
2553 function Get_Constraint_Association
(T
: Entity_Id
) return Node_Id
is
2560 -- If type is private, get constraint from full view. This was
2561 -- previously done in an instance context, but is needed whenever
2562 -- the ancestor part has a discriminant, possibly inherited through
2563 -- multiple derivations.
2565 if Is_Private_Type
(Typ
) and then Present
(Full_View
(Typ
)) then
2566 Typ
:= Full_View
(Typ
);
2569 Indic
:= Subtype_Indication
(Type_Definition
(Parent
(Typ
)));
2571 -- Verify that the subtype indication carries a constraint
2573 if Nkind
(Indic
) = N_Subtype_Indication
2574 and then Present
(Constraint
(Indic
))
2576 return First
(Constraints
(Constraint
(Indic
)));
2580 end Get_Constraint_Association
;
2582 -------------------------------------
2583 -- Get_Explicit_Discriminant_Value --
2584 -------------------------------------
2586 function Get_Explicit_Discriminant_Value
2587 (D
: Entity_Id
) return Node_Id
2594 -- The aggregate has been normalized and all associations have a
2597 Assoc
:= First
(Component_Associations
(N
));
2598 while Present
(Assoc
) loop
2599 Choice
:= First
(Choices
(Assoc
));
2601 if Chars
(Choice
) = Chars
(D
) then
2602 Val
:= Expression
(Assoc
);
2611 end Get_Explicit_Discriminant_Value
;
2613 -------------------------------
2614 -- Init_Hidden_Discriminants --
2615 -------------------------------
2617 procedure Init_Hidden_Discriminants
(Typ
: Entity_Id
; List
: List_Id
) is
2618 function Is_Completely_Hidden_Discriminant
2619 (Discr
: Entity_Id
) return Boolean;
2620 -- Determine whether Discr is a completely hidden discriminant of
2623 ---------------------------------------
2624 -- Is_Completely_Hidden_Discriminant --
2625 ---------------------------------------
2627 function Is_Completely_Hidden_Discriminant
2628 (Discr
: Entity_Id
) return Boolean
2633 -- Use First/Next_Entity as First/Next_Discriminant do not yield
2634 -- completely hidden discriminants.
2636 Item
:= First_Entity
(Typ
);
2637 while Present
(Item
) loop
2638 if Ekind
(Item
) = E_Discriminant
2639 and then Is_Completely_Hidden
(Item
)
2640 and then Chars
(Original_Record_Component
(Item
)) =
2650 end Is_Completely_Hidden_Discriminant
;
2654 Base_Typ
: Entity_Id
;
2656 Discr_Constr
: Elmt_Id
;
2657 Discr_Init
: Node_Id
;
2658 Discr_Val
: Node_Id
;
2659 In_Aggr_Type
: Boolean;
2660 Par_Typ
: Entity_Id
;
2662 -- Start of processing for Init_Hidden_Discriminants
2665 -- The constraints on the hidden discriminants, if present, are kept
2666 -- in the Stored_Constraint list of the type itself, or in that of
2667 -- the base type. If not in the constraints of the aggregate itself,
2668 -- we examine ancestors to find discriminants that are not renamed
2669 -- by other discriminants but constrained explicitly.
2671 In_Aggr_Type
:= True;
2673 Base_Typ
:= Base_Type
(Typ
);
2674 while Is_Derived_Type
(Base_Typ
)
2676 (Present
(Stored_Constraint
(Base_Typ
))
2678 (In_Aggr_Type
and then Present
(Stored_Constraint
(Typ
))))
2680 Par_Typ
:= Etype
(Base_Typ
);
2682 if not Has_Discriminants
(Par_Typ
) then
2686 Discr
:= First_Discriminant
(Par_Typ
);
2688 -- We know that one of the stored-constraint lists is present
2690 if Present
(Stored_Constraint
(Base_Typ
)) then
2691 Discr_Constr
:= First_Elmt
(Stored_Constraint
(Base_Typ
));
2693 -- For private extension, stored constraint may be on full view
2695 elsif Is_Private_Type
(Base_Typ
)
2696 and then Present
(Full_View
(Base_Typ
))
2697 and then Present
(Stored_Constraint
(Full_View
(Base_Typ
)))
2700 First_Elmt
(Stored_Constraint
(Full_View
(Base_Typ
)));
2702 -- Otherwise, no discriminant to process
2705 Discr_Constr
:= No_Elmt
;
2708 while Present
(Discr
) and then Present
(Discr_Constr
) loop
2709 Discr_Val
:= Node
(Discr_Constr
);
2711 -- The parent discriminant is renamed in the derived type,
2712 -- nothing to initialize.
2714 -- type Deriv_Typ (Discr : ...)
2715 -- is new Parent_Typ (Discr => Discr);
2717 if Is_Entity_Name
(Discr_Val
)
2718 and then Ekind
(Entity
(Discr_Val
)) = E_Discriminant
2722 -- When the parent discriminant is constrained at the type
2723 -- extension level, it does not appear in the derived type.
2725 -- type Deriv_Typ (Discr : ...)
2726 -- is new Parent_Typ (Discr => Discr,
2727 -- Hidden_Discr => Expression);
2729 elsif Is_Completely_Hidden_Discriminant
(Discr
) then
2732 -- Otherwise initialize the discriminant
2736 Make_OK_Assignment_Statement
(Loc
,
2738 Make_Selected_Component
(Loc
,
2739 Prefix
=> New_Copy_Tree
(Target
),
2740 Selector_Name
=> New_Occurrence_Of
(Discr
, Loc
)),
2741 Expression
=> New_Copy_Tree
(Discr_Val
));
2743 Append_To
(List
, Discr_Init
);
2746 Next_Elmt
(Discr_Constr
);
2747 Next_Discriminant
(Discr
);
2750 In_Aggr_Type
:= False;
2751 Base_Typ
:= Base_Type
(Par_Typ
);
2753 end Init_Hidden_Discriminants
;
2755 --------------------------------
2756 -- Init_Visible_Discriminants --
2757 --------------------------------
2759 procedure Init_Visible_Discriminants
is
2760 Discriminant
: Entity_Id
;
2761 Discriminant_Value
: Node_Id
;
2764 Discriminant
:= First_Discriminant
(Typ
);
2765 while Present
(Discriminant
) loop
2767 Make_Selected_Component
(Loc
,
2768 Prefix
=> New_Copy_Tree
(Target
),
2769 Selector_Name
=> New_Occurrence_Of
(Discriminant
, Loc
));
2771 Discriminant_Value
:=
2772 Get_Discriminant_Value
2773 (Discriminant
, Typ
, Discriminant_Constraint
(N_Typ
));
2776 Make_OK_Assignment_Statement
(Loc
,
2778 Expression
=> New_Copy_Tree
(Discriminant_Value
));
2780 Append_To
(L
, Instr
);
2782 Next_Discriminant
(Discriminant
);
2784 end Init_Visible_Discriminants
;
2786 -------------------------------
2787 -- Init_Stored_Discriminants --
2788 -------------------------------
2790 procedure Init_Stored_Discriminants
is
2791 Discriminant
: Entity_Id
;
2792 Discriminant_Value
: Node_Id
;
2795 Discriminant
:= First_Stored_Discriminant
(Typ
);
2796 while Present
(Discriminant
) loop
2798 Make_Selected_Component
(Loc
,
2799 Prefix
=> New_Copy_Tree
(Target
),
2800 Selector_Name
=> New_Occurrence_Of
(Discriminant
, Loc
));
2802 Discriminant_Value
:=
2803 Get_Discriminant_Value
2804 (Discriminant
, N_Typ
, Discriminant_Constraint
(N_Typ
));
2807 Make_OK_Assignment_Statement
(Loc
,
2809 Expression
=> New_Copy_Tree
(Discriminant_Value
));
2811 Append_To
(L
, Instr
);
2813 Next_Stored_Discriminant
(Discriminant
);
2815 end Init_Stored_Discriminants
;
2817 --------------------------------------
2818 -- Initialize_Ctrl_Record_Component --
2819 --------------------------------------
2821 procedure Initialize_Ctrl_Record_Component
2822 (Rec_Comp
: Node_Id
;
2823 Comp_Typ
: Entity_Id
;
2824 Init_Expr
: Node_Id
;
2828 Hook_Clear
: Node_Id
;
2830 In_Place_Expansion
: Boolean;
2831 -- Flag set when a nonlimited controlled function call requires
2832 -- in-place expansion.
2835 -- Perform a preliminary analysis and resolution to determine what
2836 -- the initialization expression denotes. Unanalyzed function calls
2837 -- may appear as identifiers or indexed components.
2839 if Nkind_In
(Init_Expr
, N_Function_Call
,
2841 N_Indexed_Component
)
2842 and then not Analyzed
(Init_Expr
)
2844 Preanalyze_And_Resolve
(Init_Expr
, Comp_Typ
);
2847 In_Place_Expansion
:=
2848 Nkind
(Init_Expr
) = N_Function_Call
2849 and then not Is_Build_In_Place_Result_Type
(Comp_Typ
);
2851 -- The initialization expression is a controlled function call.
2852 -- Perform in-place removal of side effects to avoid creating a
2855 -- This in-place expansion is not performed for limited transient
2856 -- objects because the initialization is already done in place.
2858 if In_Place_Expansion
then
2860 -- Suppress the removal of side effects by general analysis
2861 -- because this behavior is emulated here. This avoids the
2862 -- generation of a transient scope, which leads to out-of-order
2863 -- adjustment and finalization.
2865 Set_No_Side_Effect_Removal
(Init_Expr
);
2867 -- Install all hook-related declarations and prepare the clean up
2868 -- statements. The generated code follows the initialization order
2869 -- of individual components and discriminants, rather than being
2870 -- inserted prior to the aggregate. This ensures that a transient
2871 -- component which mentions a discriminant has proper visibility
2872 -- of the discriminant.
2874 Process_Transient_Component
2876 Comp_Typ
=> Comp_Typ
,
2877 Init_Expr
=> Init_Expr
,
2878 Fin_Call
=> Fin_Call
,
2879 Hook_Clear
=> Hook_Clear
,
2883 -- Use the noncontrolled component initialization circuitry to
2884 -- assign the result of the function call to the record component.
2885 -- This also performs tag adjustment and [deep] adjustment of the
2886 -- record component.
2888 Initialize_Record_Component
2889 (Rec_Comp
=> Rec_Comp
,
2890 Comp_Typ
=> Comp_Typ
,
2891 Init_Expr
=> Init_Expr
,
2894 -- At this point the record component is fully initialized. Complete
2895 -- the processing of the controlled record component by finalizing
2896 -- the transient function result.
2898 if In_Place_Expansion
then
2899 Process_Transient_Component_Completion
2902 Fin_Call
=> Fin_Call
,
2903 Hook_Clear
=> Hook_Clear
,
2906 end Initialize_Ctrl_Record_Component
;
2908 ---------------------------------
2909 -- Initialize_Record_Component --
2910 ---------------------------------
2912 procedure Initialize_Record_Component
2913 (Rec_Comp
: Node_Id
;
2914 Comp_Typ
: Entity_Id
;
2915 Init_Expr
: Node_Id
;
2918 Exceptions_OK
: constant Boolean :=
2919 not Restriction_Active
(No_Exception_Propagation
);
2921 Finalization_OK
: constant Boolean := Needs_Finalization
(Comp_Typ
);
2923 Full_Typ
: constant Entity_Id
:= Underlying_Type
(Comp_Typ
);
2925 Blk_Stmts
: List_Id
;
2926 Init_Stmt
: Node_Id
;
2929 -- Protect the initialization statements from aborts. Generate:
2933 if Finalization_OK
and Abort_Allowed
then
2934 if Exceptions_OK
then
2935 Blk_Stmts
:= New_List
;
2940 Append_To
(Blk_Stmts
, Build_Runtime_Call
(Loc
, RE_Abort_Defer
));
2942 -- Otherwise aborts are not allowed. All generated code is added
2943 -- directly to the input list.
2949 -- Initialize the record component. Generate:
2951 -- Rec_Comp := Init_Expr;
2953 -- Note that the initialization expression is NOT replicated because
2954 -- only a single component may be initialized by it.
2957 Make_OK_Assignment_Statement
(Loc
,
2958 Name
=> New_Copy_Tree
(Rec_Comp
),
2959 Expression
=> Init_Expr
);
2960 Set_No_Ctrl_Actions
(Init_Stmt
);
2962 Append_To
(Blk_Stmts
, Init_Stmt
);
2964 -- Adjust the tag due to a possible view conversion. Generate:
2966 -- Rec_Comp._tag := Full_TypeP;
2968 if Tagged_Type_Expansion
and then Is_Tagged_Type
(Comp_Typ
) then
2969 Append_To
(Blk_Stmts
,
2970 Make_OK_Assignment_Statement
(Loc
,
2972 Make_Selected_Component
(Loc
,
2973 Prefix
=> New_Copy_Tree
(Rec_Comp
),
2976 (First_Tag_Component
(Full_Typ
), Loc
)),
2979 Unchecked_Convert_To
(RTE
(RE_Tag
),
2981 (Node
(First_Elmt
(Access_Disp_Table
(Full_Typ
))),
2985 -- Adjust the component. Generate:
2987 -- [Deep_]Adjust (Rec_Comp);
2990 and then not Is_Limited_Type
(Comp_Typ
)
2991 and then not Is_Build_In_Place_Function_Call
(Init_Expr
)
2995 (Obj_Ref
=> New_Copy_Tree
(Rec_Comp
),
2998 -- Guard against a missing [Deep_]Adjust when the component type
2999 -- was not properly frozen.
3001 if Present
(Adj_Call
) then
3002 Append_To
(Blk_Stmts
, Adj_Call
);
3006 -- Complete the protection of the initialization statements
3008 if Finalization_OK
and Abort_Allowed
then
3010 -- Wrap the initialization statements in a block to catch a
3011 -- potential exception. Generate:
3015 -- Rec_Comp := Init_Expr;
3016 -- Rec_Comp._tag := Full_TypP;
3017 -- [Deep_]Adjust (Rec_Comp);
3019 -- Abort_Undefer_Direct;
3022 if Exceptions_OK
then
3024 Build_Abort_Undefer_Block
(Loc
,
3028 -- Otherwise exceptions are not propagated. Generate:
3031 -- Rec_Comp := Init_Expr;
3032 -- Rec_Comp._tag := Full_TypP;
3033 -- [Deep_]Adjust (Rec_Comp);
3037 Append_To
(Blk_Stmts
,
3038 Build_Runtime_Call
(Loc
, RE_Abort_Undefer
));
3041 end Initialize_Record_Component
;
3043 -------------------------
3044 -- Is_Int_Range_Bounds --
3045 -------------------------
3047 function Is_Int_Range_Bounds
(Bounds
: Node_Id
) return Boolean is
3049 return Nkind
(Bounds
) = N_Range
3050 and then Nkind
(Low_Bound
(Bounds
)) = N_Integer_Literal
3051 and then Nkind
(High_Bound
(Bounds
)) = N_Integer_Literal
;
3052 end Is_Int_Range_Bounds
;
3058 function Replace_Type
(Expr
: Node_Id
) return Traverse_Result
is
3060 -- Note regarding the Root_Type test below: Aggregate components for
3061 -- self-referential types include attribute references to the current
3062 -- instance, of the form: Typ'access, etc.. These references are
3063 -- rewritten as references to the target of the aggregate: the
3064 -- left-hand side of an assignment, the entity in a declaration,
3065 -- or a temporary. Without this test, we would improperly extended
3066 -- this rewriting to attribute references whose prefix was not the
3067 -- type of the aggregate.
3069 if Nkind
(Expr
) = N_Attribute_Reference
3070 and then Is_Entity_Name
(Prefix
(Expr
))
3071 and then Is_Type
(Entity
(Prefix
(Expr
)))
3072 and then Root_Type
(Etype
(N
)) = Root_Type
(Entity
(Prefix
(Expr
)))
3074 if Is_Entity_Name
(Lhs
) then
3075 Rewrite
(Prefix
(Expr
), New_Occurrence_Of
(Entity
(Lhs
), Loc
));
3079 Make_Attribute_Reference
(Loc
,
3080 Attribute_Name
=> Name_Unrestricted_Access
,
3081 Prefix
=> New_Copy_Tree
(Lhs
)));
3082 Set_Analyzed
(Parent
(Expr
), False);
3089 --------------------------
3090 -- Rewrite_Discriminant --
3091 --------------------------
3093 function Rewrite_Discriminant
(Expr
: Node_Id
) return Traverse_Result
is
3095 if Is_Entity_Name
(Expr
)
3096 and then Present
(Entity
(Expr
))
3097 and then Ekind
(Entity
(Expr
)) = E_In_Parameter
3098 and then Present
(Discriminal_Link
(Entity
(Expr
)))
3099 and then Scope
(Discriminal_Link
(Entity
(Expr
))) =
3100 Base_Type
(Etype
(N
))
3103 Make_Selected_Component
(Loc
,
3104 Prefix
=> New_Copy_Tree
(Lhs
),
3105 Selector_Name
=> Make_Identifier
(Loc
, Chars
(Expr
))));
3107 -- The generated code will be reanalyzed, but if the reference
3108 -- to the discriminant appears within an already analyzed
3109 -- expression (e.g. a conditional) we must set its proper entity
3110 -- now. Context is an initialization procedure.
3116 end Rewrite_Discriminant
;
3118 procedure Replace_Discriminants
is
3119 new Traverse_Proc
(Rewrite_Discriminant
);
3121 procedure Replace_Self_Reference
is
3122 new Traverse_Proc
(Replace_Type
);
3124 -- Start of processing for Build_Record_Aggr_Code
3127 if Has_Self_Reference
(N
) then
3128 Replace_Self_Reference
(N
);
3131 -- If the target of the aggregate is class-wide, we must convert it
3132 -- to the actual type of the aggregate, so that the proper components
3133 -- are visible. We know already that the types are compatible.
3135 if Present
(Etype
(Lhs
))
3136 and then Is_Class_Wide_Type
(Etype
(Lhs
))
3138 Target
:= Unchecked_Convert_To
(Typ
, Lhs
);
3143 -- Deal with the ancestor part of extension aggregates or with the
3144 -- discriminants of the root type.
3146 if Nkind
(N
) = N_Extension_Aggregate
then
3148 Ancestor
: constant Node_Id
:= Ancestor_Part
(N
);
3153 -- If the ancestor part is a subtype mark "T", we generate
3155 -- init-proc (T (tmp)); if T is constrained and
3156 -- init-proc (S (tmp)); where S applies an appropriate
3157 -- constraint if T is unconstrained
3159 if Is_Entity_Name
(Ancestor
)
3160 and then Is_Type
(Entity
(Ancestor
))
3162 Ancestor_Is_Subtype_Mark
:= True;
3164 if Is_Constrained
(Entity
(Ancestor
)) then
3165 Init_Typ
:= Entity
(Ancestor
);
3167 -- For an ancestor part given by an unconstrained type mark,
3168 -- create a subtype constrained by appropriate corresponding
3169 -- discriminant values coming from either associations of the
3170 -- aggregate or a constraint on a parent type. The subtype will
3171 -- be used to generate the correct default value for the
3174 elsif Has_Discriminants
(Entity
(Ancestor
)) then
3176 Anc_Typ
: constant Entity_Id
:= Entity
(Ancestor
);
3177 Anc_Constr
: constant List_Id
:= New_List
;
3178 Discrim
: Entity_Id
;
3179 Disc_Value
: Node_Id
;
3180 New_Indic
: Node_Id
;
3181 Subt_Decl
: Node_Id
;
3184 Discrim
:= First_Discriminant
(Anc_Typ
);
3185 while Present
(Discrim
) loop
3186 Disc_Value
:= Ancestor_Discriminant_Value
(Discrim
);
3188 -- If no usable discriminant in ancestors, check
3189 -- whether aggregate has an explicit value for it.
3191 if No
(Disc_Value
) then
3193 Get_Explicit_Discriminant_Value
(Discrim
);
3196 Append_To
(Anc_Constr
, Disc_Value
);
3197 Next_Discriminant
(Discrim
);
3201 Make_Subtype_Indication
(Loc
,
3202 Subtype_Mark
=> New_Occurrence_Of
(Anc_Typ
, Loc
),
3204 Make_Index_Or_Discriminant_Constraint
(Loc
,
3205 Constraints
=> Anc_Constr
));
3207 Init_Typ
:= Create_Itype
(Ekind
(Anc_Typ
), N
);
3210 Make_Subtype_Declaration
(Loc
,
3211 Defining_Identifier
=> Init_Typ
,
3212 Subtype_Indication
=> New_Indic
);
3214 -- Itypes must be analyzed with checks off Declaration
3215 -- must have a parent for proper handling of subsidiary
3218 Set_Parent
(Subt_Decl
, N
);
3219 Analyze
(Subt_Decl
, Suppress
=> All_Checks
);
3223 Ref
:= Convert_To
(Init_Typ
, New_Copy_Tree
(Target
));
3224 Set_Assignment_OK
(Ref
);
3226 if not Is_Interface
(Init_Typ
) then
3228 Build_Initialization_Call
(Loc
,
3231 In_Init_Proc
=> Within_Init_Proc
,
3232 With_Default_Init
=> Has_Default_Init_Comps
(N
)
3234 Has_Task
(Base_Type
(Init_Typ
))));
3236 if Is_Constrained
(Entity
(Ancestor
))
3237 and then Has_Discriminants
(Entity
(Ancestor
))
3239 Check_Ancestor_Discriminants
(Entity
(Ancestor
));
3243 -- Handle calls to C++ constructors
3245 elsif Is_CPP_Constructor_Call
(Ancestor
) then
3246 Init_Typ
:= Etype
(Ancestor
);
3247 Ref
:= Convert_To
(Init_Typ
, New_Copy_Tree
(Target
));
3248 Set_Assignment_OK
(Ref
);
3251 Build_Initialization_Call
(Loc
,
3254 In_Init_Proc
=> Within_Init_Proc
,
3255 With_Default_Init
=> Has_Default_Init_Comps
(N
),
3256 Constructor_Ref
=> Ancestor
));
3258 -- Ada 2005 (AI-287): If the ancestor part is an aggregate of
3259 -- limited type, a recursive call expands the ancestor. Note that
3260 -- in the limited case, the ancestor part must be either a
3261 -- function call (possibly qualified) or aggregate (definitely
3264 elsif Is_Limited_Type
(Etype
(Ancestor
))
3265 and then Nkind_In
(Unqualify
(Ancestor
), N_Aggregate
,
3266 N_Extension_Aggregate
)
3268 Ancestor_Is_Expression
:= True;
3270 -- Set up finalization data for enclosing record, because
3271 -- controlled subcomponents of the ancestor part will be
3274 Generate_Finalization_Actions
;
3277 Build_Record_Aggr_Code
3278 (N
=> Unqualify
(Ancestor
),
3279 Typ
=> Etype
(Unqualify
(Ancestor
)),
3282 -- If the ancestor part is an expression "E", we generate
3286 -- In Ada 2005, this includes the case of a (possibly qualified)
3287 -- limited function call. The assignment will turn into a
3288 -- build-in-place function call (for further details, see
3289 -- Make_Build_In_Place_Call_In_Assignment).
3292 Ancestor_Is_Expression
:= True;
3293 Init_Typ
:= Etype
(Ancestor
);
3295 -- If the ancestor part is an aggregate, force its full
3296 -- expansion, which was delayed.
3298 if Nkind_In
(Unqualify
(Ancestor
), N_Aggregate
,
3299 N_Extension_Aggregate
)
3301 Set_Analyzed
(Ancestor
, False);
3302 Set_Analyzed
(Expression
(Ancestor
), False);
3305 Ref
:= Convert_To
(Init_Typ
, New_Copy_Tree
(Target
));
3306 Set_Assignment_OK
(Ref
);
3308 -- Make the assignment without usual controlled actions, since
3309 -- we only want to Adjust afterwards, but not to Finalize
3310 -- beforehand. Add manual Adjust when necessary.
3312 Assign
:= New_List
(
3313 Make_OK_Assignment_Statement
(Loc
,
3315 Expression
=> Ancestor
));
3316 Set_No_Ctrl_Actions
(First
(Assign
));
3318 -- Assign the tag now to make sure that the dispatching call in
3319 -- the subsequent deep_adjust works properly (unless
3320 -- Tagged_Type_Expansion where tags are implicit).
3322 if Tagged_Type_Expansion
then
3324 Make_OK_Assignment_Statement
(Loc
,
3326 Make_Selected_Component
(Loc
,
3327 Prefix
=> New_Copy_Tree
(Target
),
3330 (First_Tag_Component
(Base_Type
(Typ
)), Loc
)),
3333 Unchecked_Convert_To
(RTE
(RE_Tag
),
3336 (Access_Disp_Table
(Base_Type
(Typ
)))),
3339 Set_Assignment_OK
(Name
(Instr
));
3340 Append_To
(Assign
, Instr
);
3342 -- Ada 2005 (AI-251): If tagged type has progenitors we must
3343 -- also initialize tags of the secondary dispatch tables.
3345 if Has_Interfaces
(Base_Type
(Typ
)) then
3347 (Typ
=> Base_Type
(Typ
),
3349 Stmts_List
=> Assign
,
3350 Init_Tags_List
=> Assign
);
3354 -- Call Adjust manually
3356 if Needs_Finalization
(Etype
(Ancestor
))
3357 and then not Is_Limited_Type
(Etype
(Ancestor
))
3358 and then not Is_Build_In_Place_Function_Call
(Ancestor
)
3362 (Obj_Ref
=> New_Copy_Tree
(Ref
),
3363 Typ
=> Etype
(Ancestor
));
3365 -- Guard against a missing [Deep_]Adjust when the ancestor
3366 -- type was not properly frozen.
3368 if Present
(Adj_Call
) then
3369 Append_To
(Assign
, Adj_Call
);
3374 Make_Unsuppress_Block
(Loc
, Name_Discriminant_Check
, Assign
));
3376 if Has_Discriminants
(Init_Typ
) then
3377 Check_Ancestor_Discriminants
(Init_Typ
);
3381 pragma Assert
(Nkind
(N
) = N_Extension_Aggregate
);
3383 (not (Ancestor_Is_Expression
and Ancestor_Is_Subtype_Mark
));
3386 -- Generate assignments of hidden discriminants. If the base type is
3387 -- an unchecked union, the discriminants are unknown to the back-end
3388 -- and absent from a value of the type, so assignments for them are
3391 if Has_Discriminants
(Typ
)
3392 and then not Is_Unchecked_Union
(Base_Type
(Typ
))
3394 Init_Hidden_Discriminants
(Typ
, L
);
3397 -- Normal case (not an extension aggregate)
3400 -- Generate the discriminant expressions, component by component.
3401 -- If the base type is an unchecked union, the discriminants are
3402 -- unknown to the back-end and absent from a value of the type, so
3403 -- assignments for them are not emitted.
3405 if Has_Discriminants
(Typ
)
3406 and then not Is_Unchecked_Union
(Base_Type
(Typ
))
3408 Init_Hidden_Discriminants
(Typ
, L
);
3410 -- Generate discriminant init values for the visible discriminants
3412 Init_Visible_Discriminants
;
3414 if Is_Derived_Type
(N_Typ
) then
3415 Init_Stored_Discriminants
;
3420 -- For CPP types we generate an implicit call to the C++ default
3421 -- constructor to ensure the proper initialization of the _Tag
3424 if Is_CPP_Class
(Root_Type
(Typ
)) and then CPP_Num_Prims
(Typ
) > 0 then
3425 Invoke_Constructor
: declare
3426 CPP_Parent
: constant Entity_Id
:= Enclosing_CPP_Parent
(Typ
);
3428 procedure Invoke_IC_Proc
(T
: Entity_Id
);
3429 -- Recursive routine used to climb to parents. Required because
3430 -- parents must be initialized before descendants to ensure
3431 -- propagation of inherited C++ slots.
3433 --------------------
3434 -- Invoke_IC_Proc --
3435 --------------------
3437 procedure Invoke_IC_Proc
(T
: Entity_Id
) is
3439 -- Avoid generating extra calls. Initialization required
3440 -- only for types defined from the level of derivation of
3441 -- type of the constructor and the type of the aggregate.
3443 if T
= CPP_Parent
then
3447 Invoke_IC_Proc
(Etype
(T
));
3449 -- Generate call to the IC routine
3451 if Present
(CPP_Init_Proc
(T
)) then
3453 Make_Procedure_Call_Statement
(Loc
,
3454 Name
=> New_Occurrence_Of
(CPP_Init_Proc
(T
), Loc
)));
3458 -- Start of processing for Invoke_Constructor
3461 -- Implicit invocation of the C++ constructor
3463 if Nkind
(N
) = N_Aggregate
then
3465 Make_Procedure_Call_Statement
(Loc
,
3467 New_Occurrence_Of
(Base_Init_Proc
(CPP_Parent
), Loc
),
3468 Parameter_Associations
=> New_List
(
3469 Unchecked_Convert_To
(CPP_Parent
,
3470 New_Copy_Tree
(Lhs
)))));
3473 Invoke_IC_Proc
(Typ
);
3474 end Invoke_Constructor
;
3477 -- Generate the assignments, component by component
3479 -- tmp.comp1 := Expr1_From_Aggr;
3480 -- tmp.comp2 := Expr2_From_Aggr;
3483 Comp
:= First
(Component_Associations
(N
));
3484 while Present
(Comp
) loop
3485 Selector
:= Entity
(First
(Choices
(Comp
)));
3489 if Is_CPP_Constructor_Call
(Expression
(Comp
)) then
3491 Build_Initialization_Call
(Loc
,
3493 Make_Selected_Component
(Loc
,
3494 Prefix
=> New_Copy_Tree
(Target
),
3495 Selector_Name
=> New_Occurrence_Of
(Selector
, Loc
)),
3496 Typ
=> Etype
(Selector
),
3498 With_Default_Init
=> True,
3499 Constructor_Ref
=> Expression
(Comp
)));
3501 -- Ada 2005 (AI-287): For each default-initialized component generate
3502 -- a call to the corresponding IP subprogram if available.
3504 elsif Box_Present
(Comp
)
3505 and then Has_Non_Null_Base_Init_Proc
(Etype
(Selector
))
3507 if Ekind
(Selector
) /= E_Discriminant
then
3508 Generate_Finalization_Actions
;
3511 -- Ada 2005 (AI-287): If the component type has tasks then
3512 -- generate the activation chain and master entities (except
3513 -- in case of an allocator because in that case these entities
3514 -- are generated by Build_Task_Allocate_Block_With_Init_Stmts).
3517 Ctype
: constant Entity_Id
:= Etype
(Selector
);
3518 Inside_Allocator
: Boolean := False;
3519 P
: Node_Id
:= Parent
(N
);
3522 if Is_Task_Type
(Ctype
) or else Has_Task
(Ctype
) then
3523 while Present
(P
) loop
3524 if Nkind
(P
) = N_Allocator
then
3525 Inside_Allocator
:= True;
3532 if not Inside_Init_Proc
and not Inside_Allocator
then
3533 Build_Activation_Chain_Entity
(N
);
3539 Build_Initialization_Call
(Loc
,
3540 Id_Ref
=> Make_Selected_Component
(Loc
,
3541 Prefix
=> New_Copy_Tree
(Target
),
3543 New_Occurrence_Of
(Selector
, Loc
)),
3544 Typ
=> Etype
(Selector
),
3546 With_Default_Init
=> True));
3548 -- Prepare for component assignment
3550 elsif Ekind
(Selector
) /= E_Discriminant
3551 or else Nkind
(N
) = N_Extension_Aggregate
3553 -- All the discriminants have now been assigned
3555 -- This is now a good moment to initialize and attach all the
3556 -- controllers. Their position may depend on the discriminants.
3558 if Ekind
(Selector
) /= E_Discriminant
then
3559 Generate_Finalization_Actions
;
3562 Comp_Type
:= Underlying_Type
(Etype
(Selector
));
3564 Make_Selected_Component
(Loc
,
3565 Prefix
=> New_Copy_Tree
(Target
),
3566 Selector_Name
=> New_Occurrence_Of
(Selector
, Loc
));
3568 if Nkind
(Expression
(Comp
)) = N_Qualified_Expression
then
3569 Expr_Q
:= Expression
(Expression
(Comp
));
3571 Expr_Q
:= Expression
(Comp
);
3574 -- Now either create the assignment or generate the code for the
3575 -- inner aggregate top-down.
3577 if Is_Delayed_Aggregate
(Expr_Q
) then
3579 -- We have the following case of aggregate nesting inside
3580 -- an object declaration:
3582 -- type Arr_Typ is array (Integer range <>) of ...;
3584 -- type Rec_Typ (...) is record
3585 -- Obj_Arr_Typ : Arr_Typ (A .. B);
3588 -- Obj_Rec_Typ : Rec_Typ := (...,
3589 -- Obj_Arr_Typ => (X => (...), Y => (...)));
3591 -- The length of the ranges of the aggregate and Obj_Add_Typ
3592 -- are equal (B - A = Y - X), but they do not coincide (X /=
3593 -- A and B /= Y). This case requires array sliding which is
3594 -- performed in the following manner:
3596 -- subtype Arr_Sub is Arr_Typ (X .. Y);
3598 -- Temp (X) := (...);
3600 -- Temp (Y) := (...);
3601 -- Obj_Rec_Typ.Obj_Arr_Typ := Temp;
3603 if Ekind
(Comp_Type
) = E_Array_Subtype
3604 and then Is_Int_Range_Bounds
(Aggregate_Bounds
(Expr_Q
))
3605 and then Is_Int_Range_Bounds
(First_Index
(Comp_Type
))
3607 Compatible_Int_Bounds
3608 (Agg_Bounds
=> Aggregate_Bounds
(Expr_Q
),
3609 Typ_Bounds
=> First_Index
(Comp_Type
))
3611 -- Create the array subtype with bounds equal to those of
3612 -- the corresponding aggregate.
3615 SubE
: constant Entity_Id
:= Make_Temporary
(Loc
, 'T');
3617 SubD
: constant Node_Id
:=
3618 Make_Subtype_Declaration
(Loc
,
3619 Defining_Identifier
=> SubE
,
3620 Subtype_Indication
=>
3621 Make_Subtype_Indication
(Loc
,
3623 New_Occurrence_Of
(Etype
(Comp_Type
), Loc
),
3625 Make_Index_Or_Discriminant_Constraint
3627 Constraints
=> New_List
(
3629 (Aggregate_Bounds
(Expr_Q
))))));
3631 -- Create a temporary array of the above subtype which
3632 -- will be used to capture the aggregate assignments.
3634 TmpE
: constant Entity_Id
:= Make_Temporary
(Loc
, 'A', N
);
3636 TmpD
: constant Node_Id
:=
3637 Make_Object_Declaration
(Loc
,
3638 Defining_Identifier
=> TmpE
,
3639 Object_Definition
=> New_Occurrence_Of
(SubE
, Loc
));
3642 Set_No_Initialization
(TmpD
);
3643 Append_To
(L
, SubD
);
3644 Append_To
(L
, TmpD
);
3646 -- Expand aggregate into assignments to the temp array
3649 Late_Expansion
(Expr_Q
, Comp_Type
,
3650 New_Occurrence_Of
(TmpE
, Loc
)));
3655 Make_Assignment_Statement
(Loc
,
3656 Name
=> New_Copy_Tree
(Comp_Expr
),
3657 Expression
=> New_Occurrence_Of
(TmpE
, Loc
)));
3660 -- Normal case (sliding not required)
3664 Late_Expansion
(Expr_Q
, Comp_Type
, Comp_Expr
));
3667 -- Expr_Q is not delayed aggregate
3670 if Has_Discriminants
(Typ
) then
3671 Replace_Discriminants
(Expr_Q
);
3673 -- If the component is an array type that depends on
3674 -- discriminants, and the expression is a single Others
3675 -- clause, create an explicit subtype for it because the
3676 -- backend has troubles recovering the actual bounds.
3678 if Nkind
(Expr_Q
) = N_Aggregate
3679 and then Is_Array_Type
(Comp_Type
)
3680 and then Present
(Component_Associations
(Expr_Q
))
3683 Assoc
: constant Node_Id
:=
3684 First
(Component_Associations
(Expr_Q
));
3688 if Nkind
(First
(Choices
(Assoc
))) = N_Others_Choice
3691 Build_Actual_Subtype_Of_Component
3692 (Comp_Type
, Comp_Expr
);
3694 -- If the component type does not in fact depend on
3695 -- discriminants, the subtype declaration is empty.
3697 if Present
(Decl
) then
3698 Append_To
(L
, Decl
);
3699 Set_Etype
(Comp_Expr
, Defining_Entity
(Decl
));
3706 if Modify_Tree_For_C
3707 and then Nkind
(Expr_Q
) = N_Aggregate
3708 and then Is_Array_Type
(Etype
(Expr_Q
))
3709 and then Present
(First_Index
(Etype
(Expr_Q
)))
3712 Expr_Q_Type
: constant Node_Id
:= Etype
(Expr_Q
);
3715 Build_Array_Aggr_Code
3717 Ctype
=> Component_Type
(Expr_Q_Type
),
3718 Index
=> First_Index
(Expr_Q_Type
),
3721 Is_Scalar_Type
(Component_Type
(Expr_Q_Type
))));
3725 -- Handle an initialization expression of a controlled type
3726 -- in case it denotes a function call. In general such a
3727 -- scenario will produce a transient scope, but this will
3728 -- lead to wrong order of initialization, adjustment, and
3729 -- finalization in the context of aggregates.
3731 -- Target.Comp := Ctrl_Func_Call;
3734 -- Trans_Obj : ... := Ctrl_Func_Call; -- object
3735 -- Target.Comp := Trans_Obj;
3736 -- Finalize (Trans_Obj);
3738 -- Target.Comp._tag := ...;
3739 -- Adjust (Target.Comp);
3741 -- In the example above, the call to Finalize occurs too
3742 -- early and as a result it may leave the record component
3743 -- in a bad state. Finalization of the transient object
3744 -- should really happen after adjustment.
3746 -- To avoid this scenario, perform in-place side-effect
3747 -- removal of the function call. This eliminates the
3748 -- transient property of the function result and ensures
3749 -- correct order of actions.
3751 -- Res : ... := Ctrl_Func_Call;
3752 -- Target.Comp := Res;
3753 -- Target.Comp._tag := ...;
3754 -- Adjust (Target.Comp);
3757 if Needs_Finalization
(Comp_Type
)
3758 and then Nkind
(Expr_Q
) /= N_Aggregate
3760 Initialize_Ctrl_Record_Component
3761 (Rec_Comp
=> Comp_Expr
,
3762 Comp_Typ
=> Etype
(Selector
),
3763 Init_Expr
=> Expr_Q
,
3766 -- Otherwise perform single component initialization
3769 Initialize_Record_Component
3770 (Rec_Comp
=> Comp_Expr
,
3771 Comp_Typ
=> Etype
(Selector
),
3772 Init_Expr
=> Expr_Q
,
3778 -- comment would be good here ???
3780 elsif Ekind
(Selector
) = E_Discriminant
3781 and then Nkind
(N
) /= N_Extension_Aggregate
3782 and then Nkind
(Parent
(N
)) = N_Component_Association
3783 and then Is_Constrained
(Typ
)
3785 -- We must check that the discriminant value imposed by the
3786 -- context is the same as the value given in the subaggregate,
3787 -- because after the expansion into assignments there is no
3788 -- record on which to perform a regular discriminant check.
3795 D_Val
:= First_Elmt
(Discriminant_Constraint
(Typ
));
3796 Disc
:= First_Discriminant
(Typ
);
3797 while Chars
(Disc
) /= Chars
(Selector
) loop
3798 Next_Discriminant
(Disc
);
3802 pragma Assert
(Present
(D_Val
));
3804 -- This check cannot performed for components that are
3805 -- constrained by a current instance, because this is not a
3806 -- value that can be compared with the actual constraint.
3808 if Nkind
(Node
(D_Val
)) /= N_Attribute_Reference
3809 or else not Is_Entity_Name
(Prefix
(Node
(D_Val
)))
3810 or else not Is_Type
(Entity
(Prefix
(Node
(D_Val
))))
3813 Make_Raise_Constraint_Error
(Loc
,
3816 Left_Opnd
=> New_Copy_Tree
(Node
(D_Val
)),
3817 Right_Opnd
=> Expression
(Comp
)),
3818 Reason
=> CE_Discriminant_Check_Failed
));
3821 -- Find self-reference in previous discriminant assignment,
3822 -- and replace with proper expression.
3829 while Present
(Ass
) loop
3830 if Nkind
(Ass
) = N_Assignment_Statement
3831 and then Nkind
(Name
(Ass
)) = N_Selected_Component
3832 and then Chars
(Selector_Name
(Name
(Ass
))) =
3836 (Ass
, New_Copy_Tree
(Expression
(Comp
)));
3849 -- If the type is tagged, the tag needs to be initialized (unless we
3850 -- are in VM-mode where tags are implicit). It is done late in the
3851 -- initialization process because in some cases, we call the init
3852 -- proc of an ancestor which will not leave out the right tag.
3854 if Ancestor_Is_Expression
then
3857 -- For CPP types we generated a call to the C++ default constructor
3858 -- before the components have been initialized to ensure the proper
3859 -- initialization of the _Tag component (see above).
3861 elsif Is_CPP_Class
(Typ
) then
3864 elsif Is_Tagged_Type
(Typ
) and then Tagged_Type_Expansion
then
3866 Make_OK_Assignment_Statement
(Loc
,
3868 Make_Selected_Component
(Loc
,
3869 Prefix
=> New_Copy_Tree
(Target
),
3872 (First_Tag_Component
(Base_Type
(Typ
)), Loc
)),
3875 Unchecked_Convert_To
(RTE
(RE_Tag
),
3877 (Node
(First_Elmt
(Access_Disp_Table
(Base_Type
(Typ
)))),
3880 Append_To
(L
, Instr
);
3882 -- Ada 2005 (AI-251): If the tagged type has been derived from an
3883 -- abstract interfaces we must also initialize the tags of the
3884 -- secondary dispatch tables.
3886 if Has_Interfaces
(Base_Type
(Typ
)) then
3888 (Typ
=> Base_Type
(Typ
),
3891 Init_Tags_List
=> L
);
3895 -- If the controllers have not been initialized yet (by lack of non-
3896 -- discriminant components), let's do it now.
3898 Generate_Finalization_Actions
;
3901 end Build_Record_Aggr_Code
;
3903 ---------------------------------------
3904 -- Collect_Initialization_Statements --
3905 ---------------------------------------
3907 procedure Collect_Initialization_Statements
3910 Node_After
: Node_Id
)
3912 Loc
: constant Source_Ptr
:= Sloc
(N
);
3913 Init_Actions
: constant List_Id
:= New_List
;
3914 Init_Node
: Node_Id
;
3915 Comp_Stmt
: Node_Id
;
3918 -- Nothing to do if Obj is already frozen, as in this case we known we
3919 -- won't need to move the initialization statements about later on.
3921 if Is_Frozen
(Obj
) then
3926 while Next
(Init_Node
) /= Node_After
loop
3927 Append_To
(Init_Actions
, Remove_Next
(Init_Node
));
3930 if not Is_Empty_List
(Init_Actions
) then
3931 Comp_Stmt
:= Make_Compound_Statement
(Loc
, Actions
=> Init_Actions
);
3932 Insert_Action_After
(Init_Node
, Comp_Stmt
);
3933 Set_Initialization_Statements
(Obj
, Comp_Stmt
);
3935 end Collect_Initialization_Statements
;
3937 -------------------------------
3938 -- Convert_Aggr_In_Allocator --
3939 -------------------------------
3941 procedure Convert_Aggr_In_Allocator
3946 Loc
: constant Source_Ptr
:= Sloc
(Aggr
);
3947 Typ
: constant Entity_Id
:= Etype
(Aggr
);
3948 Temp
: constant Entity_Id
:= Defining_Identifier
(Decl
);
3950 Occ
: constant Node_Id
:=
3951 Unchecked_Convert_To
(Typ
,
3952 Make_Explicit_Dereference
(Loc
, New_Occurrence_Of
(Temp
, Loc
)));
3955 if Is_Array_Type
(Typ
) then
3956 Convert_Array_Aggr_In_Allocator
(Decl
, Aggr
, Occ
);
3958 elsif Has_Default_Init_Comps
(Aggr
) then
3960 L
: constant List_Id
:= New_List
;
3961 Init_Stmts
: List_Id
;
3964 Init_Stmts
:= Late_Expansion
(Aggr
, Typ
, Occ
);
3966 if Has_Task
(Typ
) then
3967 Build_Task_Allocate_Block_With_Init_Stmts
(L
, Aggr
, Init_Stmts
);
3968 Insert_Actions
(Alloc
, L
);
3970 Insert_Actions
(Alloc
, Init_Stmts
);
3975 Insert_Actions
(Alloc
, Late_Expansion
(Aggr
, Typ
, Occ
));
3977 end Convert_Aggr_In_Allocator
;
3979 --------------------------------
3980 -- Convert_Aggr_In_Assignment --
3981 --------------------------------
3983 procedure Convert_Aggr_In_Assignment
(N
: Node_Id
) is
3984 Aggr
: Node_Id
:= Expression
(N
);
3985 Typ
: constant Entity_Id
:= Etype
(Aggr
);
3986 Occ
: constant Node_Id
:= New_Copy_Tree
(Name
(N
));
3989 if Nkind
(Aggr
) = N_Qualified_Expression
then
3990 Aggr
:= Expression
(Aggr
);
3993 Insert_Actions_After
(N
, Late_Expansion
(Aggr
, Typ
, Occ
));
3994 end Convert_Aggr_In_Assignment
;
3996 ---------------------------------
3997 -- Convert_Aggr_In_Object_Decl --
3998 ---------------------------------
4000 procedure Convert_Aggr_In_Object_Decl
(N
: Node_Id
) is
4001 Obj
: constant Entity_Id
:= Defining_Identifier
(N
);
4002 Aggr
: Node_Id
:= Expression
(N
);
4003 Loc
: constant Source_Ptr
:= Sloc
(Aggr
);
4004 Typ
: constant Entity_Id
:= Etype
(Aggr
);
4005 Occ
: constant Node_Id
:= New_Occurrence_Of
(Obj
, Loc
);
4007 function Discriminants_Ok
return Boolean;
4008 -- If the object type is constrained, the discriminants in the
4009 -- aggregate must be checked against the discriminants of the subtype.
4010 -- This cannot be done using Apply_Discriminant_Checks because after
4011 -- expansion there is no aggregate left to check.
4013 ----------------------
4014 -- Discriminants_Ok --
4015 ----------------------
4017 function Discriminants_Ok
return Boolean is
4018 Cond
: Node_Id
:= Empty
;
4027 D
:= First_Discriminant
(Typ
);
4028 Disc1
:= First_Elmt
(Discriminant_Constraint
(Typ
));
4029 Disc2
:= First_Elmt
(Discriminant_Constraint
(Etype
(Obj
)));
4030 while Present
(Disc1
) and then Present
(Disc2
) loop
4031 Val1
:= Node
(Disc1
);
4032 Val2
:= Node
(Disc2
);
4034 if not Is_OK_Static_Expression
(Val1
)
4035 or else not Is_OK_Static_Expression
(Val2
)
4037 Check
:= Make_Op_Ne
(Loc
,
4038 Left_Opnd
=> Duplicate_Subexpr
(Val1
),
4039 Right_Opnd
=> Duplicate_Subexpr
(Val2
));
4045 Cond
:= Make_Or_Else
(Loc
,
4047 Right_Opnd
=> Check
);
4050 elsif Expr_Value
(Val1
) /= Expr_Value
(Val2
) then
4051 Apply_Compile_Time_Constraint_Error
(Aggr
,
4052 Msg
=> "incorrect value for discriminant&??",
4053 Reason
=> CE_Discriminant_Check_Failed
,
4058 Next_Discriminant
(D
);
4063 -- If any discriminant constraint is nonstatic, emit a check
4065 if Present
(Cond
) then
4067 Make_Raise_Constraint_Error
(Loc
,
4069 Reason
=> CE_Discriminant_Check_Failed
));
4073 end Discriminants_Ok
;
4075 -- Start of processing for Convert_Aggr_In_Object_Decl
4078 Set_Assignment_OK
(Occ
);
4080 if Nkind
(Aggr
) = N_Qualified_Expression
then
4081 Aggr
:= Expression
(Aggr
);
4084 if Has_Discriminants
(Typ
)
4085 and then Typ
/= Etype
(Obj
)
4086 and then Is_Constrained
(Etype
(Obj
))
4087 and then not Discriminants_Ok
4092 -- If the context is an extended return statement, it has its own
4093 -- finalization machinery (i.e. works like a transient scope) and
4094 -- we do not want to create an additional one, because objects on
4095 -- the finalization list of the return must be moved to the caller's
4096 -- finalization list to complete the return.
4098 -- However, if the aggregate is limited, it is built in place, and the
4099 -- controlled components are not assigned to intermediate temporaries
4100 -- so there is no need for a transient scope in this case either.
4102 if Requires_Transient_Scope
(Typ
)
4103 and then Ekind
(Current_Scope
) /= E_Return_Statement
4104 and then not Is_Limited_Type
(Typ
)
4106 Establish_Transient_Scope
(Aggr
, Manage_Sec_Stack
=> False);
4110 Node_After
: constant Node_Id
:= Next
(N
);
4112 Insert_Actions_After
(N
, Late_Expansion
(Aggr
, Typ
, Occ
));
4113 Collect_Initialization_Statements
(Obj
, N
, Node_After
);
4116 Set_No_Initialization
(N
);
4117 Initialize_Discriminants
(N
, Typ
);
4118 end Convert_Aggr_In_Object_Decl
;
4120 -------------------------------------
4121 -- Convert_Array_Aggr_In_Allocator --
4122 -------------------------------------
4124 procedure Convert_Array_Aggr_In_Allocator
4129 Aggr_Code
: List_Id
;
4130 Typ
: constant Entity_Id
:= Etype
(Aggr
);
4131 Ctyp
: constant Entity_Id
:= Component_Type
(Typ
);
4134 -- The target is an explicit dereference of the allocated object.
4135 -- Generate component assignments to it, as for an aggregate that
4136 -- appears on the right-hand side of an assignment statement.
4139 Build_Array_Aggr_Code
(Aggr
,
4141 Index
=> First_Index
(Typ
),
4143 Scalar_Comp
=> Is_Scalar_Type
(Ctyp
));
4145 Insert_Actions_After
(Decl
, Aggr_Code
);
4146 end Convert_Array_Aggr_In_Allocator
;
4148 ------------------------
4149 -- In_Place_Assign_OK --
4150 ------------------------
4152 function In_Place_Assign_OK
(N
: Node_Id
) return Boolean is
4153 Is_Array
: constant Boolean := Is_Array_Type
(Etype
(N
));
4162 function Safe_Aggregate
(Aggr
: Node_Id
) return Boolean;
4163 -- Check recursively that each component of a (sub)aggregate does not
4164 -- depend on the variable being assigned to.
4166 function Safe_Component
(Expr
: Node_Id
) return Boolean;
4167 -- Verify that an expression cannot depend on the variable being
4168 -- assigned to. Room for improvement here (but less than before).
4170 --------------------
4171 -- Safe_Aggregate --
4172 --------------------
4174 function Safe_Aggregate
(Aggr
: Node_Id
) return Boolean is
4178 if Nkind
(Parent
(Aggr
)) = N_Iterated_Component_Association
then
4182 if Present
(Expressions
(Aggr
)) then
4183 Expr
:= First
(Expressions
(Aggr
));
4184 while Present
(Expr
) loop
4185 if Nkind
(Expr
) = N_Aggregate
then
4186 if not Safe_Aggregate
(Expr
) then
4190 elsif not Safe_Component
(Expr
) then
4198 if Present
(Component_Associations
(Aggr
)) then
4199 Expr
:= First
(Component_Associations
(Aggr
));
4200 while Present
(Expr
) loop
4201 if Nkind
(Expression
(Expr
)) = N_Aggregate
then
4202 if not Safe_Aggregate
(Expression
(Expr
)) then
4206 -- If association has a box, no way to determine yet whether
4207 -- default can be assigned in place.
4209 elsif Box_Present
(Expr
) then
4212 elsif not Safe_Component
(Expression
(Expr
)) then
4223 --------------------
4224 -- Safe_Component --
4225 --------------------
4227 function Safe_Component
(Expr
: Node_Id
) return Boolean is
4228 Comp
: Node_Id
:= Expr
;
4230 function Check_Component
(Comp
: Node_Id
) return Boolean;
4231 -- Do the recursive traversal, after copy
4233 ---------------------
4234 -- Check_Component --
4235 ---------------------
4237 function Check_Component
(Comp
: Node_Id
) return Boolean is
4239 if Is_Overloaded
(Comp
) then
4243 return Compile_Time_Known_Value
(Comp
)
4245 or else (Is_Entity_Name
(Comp
)
4246 and then Present
(Entity
(Comp
))
4247 and then Ekind
(Entity
(Comp
)) not in Type_Kind
4248 and then No
(Renamed_Object
(Entity
(Comp
))))
4250 or else (Nkind
(Comp
) = N_Attribute_Reference
4251 and then Check_Component
(Prefix
(Comp
)))
4253 or else (Nkind
(Comp
) in N_Binary_Op
4254 and then Check_Component
(Left_Opnd
(Comp
))
4255 and then Check_Component
(Right_Opnd
(Comp
)))
4257 or else (Nkind
(Comp
) in N_Unary_Op
4258 and then Check_Component
(Right_Opnd
(Comp
)))
4260 or else (Nkind
(Comp
) = N_Selected_Component
4262 and then Check_Component
(Prefix
(Comp
)))
4264 or else (Nkind_In
(Comp
, N_Type_Conversion
,
4265 N_Unchecked_Type_Conversion
)
4266 and then Check_Component
(Expression
(Comp
)));
4267 end Check_Component
;
4269 -- Start of processing for Safe_Component
4272 -- If the component appears in an association that may correspond
4273 -- to more than one element, it is not analyzed before expansion
4274 -- into assignments, to avoid side effects. We analyze, but do not
4275 -- resolve the copy, to obtain sufficient entity information for
4276 -- the checks that follow. If component is overloaded we assume
4277 -- an unsafe function call.
4279 if not Analyzed
(Comp
) then
4280 if Is_Overloaded
(Expr
) then
4283 elsif Nkind
(Expr
) = N_Aggregate
4284 and then not Is_Others_Aggregate
(Expr
)
4288 elsif Nkind
(Expr
) = N_Allocator
then
4290 -- For now, too complex to analyze
4294 elsif Nkind
(Parent
(Expr
)) = N_Iterated_Component_Association
then
4296 -- Ditto for iterated component associations, which in general
4297 -- require an enclosing loop and involve nonstatic expressions.
4302 Comp
:= New_Copy_Tree
(Expr
);
4303 Set_Parent
(Comp
, Parent
(Expr
));
4307 if Nkind
(Comp
) = N_Aggregate
then
4308 return Safe_Aggregate
(Comp
);
4310 return Check_Component
(Comp
);
4314 -- Start of processing for In_Place_Assign_OK
4317 -- By-copy semantic cannot be guaranteed for controlled objects or
4318 -- objects with discriminants.
4320 if Needs_Finalization
(Etype
(N
))
4321 or else Has_Discriminants
(Etype
(N
))
4325 elsif Is_Array
and then Present
(Component_Associations
(N
)) then
4327 -- On assignment, sliding can take place, so we cannot do the
4328 -- assignment in place unless the bounds of the aggregate are
4329 -- statically equal to those of the target.
4331 -- If the aggregate is given by an others choice, the bounds are
4332 -- derived from the left-hand side, and the assignment is safe if
4333 -- the expression is.
4335 if Is_Others_Aggregate
(N
) then
4338 (Expression
(First
(Component_Associations
(N
))));
4341 Aggr_In
:= First_Index
(Etype
(N
));
4343 if Nkind
(Parent
(N
)) = N_Assignment_Statement
then
4344 Obj_In
:= First_Index
(Etype
(Name
(Parent
(N
))));
4347 -- Context is an allocator. Check bounds of aggregate against
4348 -- given type in qualified expression.
4350 pragma Assert
(Nkind
(Parent
(Parent
(N
))) = N_Allocator
);
4351 Obj_In
:= First_Index
(Etype
(Entity
(Subtype_Mark
(Parent
(N
)))));
4354 while Present
(Aggr_In
) loop
4355 Get_Index_Bounds
(Aggr_In
, Aggr_Lo
, Aggr_Hi
);
4356 Get_Index_Bounds
(Obj_In
, Obj_Lo
, Obj_Hi
);
4358 if not Compile_Time_Known_Value
(Aggr_Lo
)
4359 or else not Compile_Time_Known_Value
(Obj_Lo
)
4360 or else not Compile_Time_Known_Value
(Obj_Hi
)
4361 or else Expr_Value
(Aggr_Lo
) /= Expr_Value
(Obj_Lo
)
4365 -- For an assignment statement we require static matching of
4366 -- bounds. Ditto for an allocator whose qualified expression
4367 -- is a constrained type. If the expression in the allocator
4368 -- is an unconstrained array, we accept an upper bound that
4369 -- is not static, to allow for nonstatic expressions of the
4370 -- base type. Clearly there are further possibilities (with
4371 -- diminishing returns) for safely building arrays in place
4374 elsif Nkind
(Parent
(N
)) = N_Assignment_Statement
4375 or else Is_Constrained
(Etype
(Parent
(N
)))
4377 if not Compile_Time_Known_Value
(Aggr_Hi
)
4378 or else Expr_Value
(Aggr_Hi
) /= Expr_Value
(Obj_Hi
)
4384 Next_Index
(Aggr_In
);
4385 Next_Index
(Obj_In
);
4389 -- Now check the component values themselves
4391 return Safe_Aggregate
(N
);
4392 end In_Place_Assign_OK
;
4394 ----------------------------
4395 -- Convert_To_Assignments --
4396 ----------------------------
4398 procedure Convert_To_Assignments
(N
: Node_Id
; Typ
: Entity_Id
) is
4399 Loc
: constant Source_Ptr
:= Sloc
(N
);
4403 Aggr_Code
: List_Id
;
4405 Target_Expr
: Node_Id
;
4406 Parent_Kind
: Node_Kind
;
4407 Unc_Decl
: Boolean := False;
4408 Parent_Node
: Node_Id
;
4411 pragma Assert
(Nkind_In
(N
, N_Aggregate
, N_Extension_Aggregate
));
4412 pragma Assert
(not Is_Static_Dispatch_Table_Aggregate
(N
));
4413 pragma Assert
(Is_Record_Type
(Typ
));
4415 Parent_Node
:= Parent
(N
);
4416 Parent_Kind
:= Nkind
(Parent_Node
);
4418 if Parent_Kind
= N_Qualified_Expression
then
4419 -- Check if we are in an unconstrained declaration because in this
4420 -- case the current delayed expansion mechanism doesn't work when
4421 -- the declared object size depends on the initializing expr.
4423 Parent_Node
:= Parent
(Parent_Node
);
4424 Parent_Kind
:= Nkind
(Parent_Node
);
4426 if Parent_Kind
= N_Object_Declaration
then
4428 not Is_Entity_Name
(Object_Definition
(Parent_Node
))
4429 or else (Nkind
(N
) = N_Aggregate
4432 (Entity
(Object_Definition
(Parent_Node
))))
4433 or else Is_Class_Wide_Type
4434 (Entity
(Object_Definition
(Parent_Node
)));
4438 -- Just set the Delay flag in the cases where the transformation will be
4439 -- done top down from above.
4443 -- Internal aggregate (transformed when expanding the parent)
4445 or else Parent_Kind
= N_Aggregate
4446 or else Parent_Kind
= N_Extension_Aggregate
4447 or else Parent_Kind
= N_Component_Association
4449 -- Allocator (see Convert_Aggr_In_Allocator)
4451 or else Parent_Kind
= N_Allocator
4453 -- Object declaration (see Convert_Aggr_In_Object_Decl)
4455 or else (Parent_Kind
= N_Object_Declaration
and then not Unc_Decl
)
4457 -- Safe assignment (see Convert_Aggr_Assignments). So far only the
4458 -- assignments in init procs are taken into account.
4460 or else (Parent_Kind
= N_Assignment_Statement
4461 and then Inside_Init_Proc
)
4463 -- (Ada 2005) An inherently limited type in a return statement, which
4464 -- will be handled in a build-in-place fashion, and may be rewritten
4465 -- as an extended return and have its own finalization machinery.
4466 -- In the case of a simple return, the aggregate needs to be delayed
4467 -- until the scope for the return statement has been created, so
4468 -- that any finalization chain will be associated with that scope.
4469 -- For extended returns, we delay expansion to avoid the creation
4470 -- of an unwanted transient scope that could result in premature
4471 -- finalization of the return object (which is built in place
4472 -- within the caller's scope).
4474 or else Is_Build_In_Place_Aggregate_Return
(N
)
4476 Set_Expansion_Delayed
(N
);
4480 -- Otherwise, if a transient scope is required, create it now. If we
4481 -- are within an initialization procedure do not create such, because
4482 -- the target of the assignment must not be declared within a local
4483 -- block, and because cleanup will take place on return from the
4484 -- initialization procedure.
4486 -- Should the condition be more restrictive ???
4488 if Requires_Transient_Scope
(Typ
) and then not Inside_Init_Proc
then
4489 Establish_Transient_Scope
(N
, Manage_Sec_Stack
=> False);
4492 -- If the aggregate is nonlimited, create a temporary, since aggregates
4493 -- have "by copy" semantics. If it is limited and context is an
4494 -- assignment, this is a subaggregate for an enclosing aggregate being
4495 -- expanded. It must be built in place, so use target of the current
4498 if Is_Limited_Type
(Typ
)
4499 and then Nkind
(Parent
(N
)) = N_Assignment_Statement
4501 Target_Expr
:= New_Copy_Tree
(Name
(Parent
(N
)));
4502 Insert_Actions
(Parent
(N
),
4503 Build_Record_Aggr_Code
(N
, Typ
, Target_Expr
));
4504 Rewrite
(Parent
(N
), Make_Null_Statement
(Loc
));
4506 -- Do not declare a temporary to initialize an aggregate assigned to an
4507 -- identifier when in-place assignment is possible, preserving the
4508 -- by-copy semantic of aggregates. This avoids large stack usage and
4509 -- generates more efficient code.
4511 elsif Nkind
(Parent
(N
)) = N_Assignment_Statement
4512 and then Nkind
(Name
(Parent
(N
))) = N_Identifier
4513 and then In_Place_Assign_OK
(N
)
4515 Target_Expr
:= New_Copy_Tree
(Name
(Parent
(N
)));
4516 Insert_Actions
(Parent
(N
),
4517 Build_Record_Aggr_Code
(N
, Typ
, Target_Expr
));
4518 Rewrite
(Parent
(N
), Make_Null_Statement
(Loc
));
4521 Temp
:= Make_Temporary
(Loc
, 'A', N
);
4523 -- If the type inherits unknown discriminants, use the view with
4524 -- known discriminants if available.
4526 if Has_Unknown_Discriminants
(Typ
)
4527 and then Present
(Underlying_Record_View
(Typ
))
4529 T
:= Underlying_Record_View
(Typ
);
4535 Make_Object_Declaration
(Loc
,
4536 Defining_Identifier
=> Temp
,
4537 Object_Definition
=> New_Occurrence_Of
(T
, Loc
));
4539 Set_No_Initialization
(Instr
);
4540 Insert_Action
(N
, Instr
);
4541 Initialize_Discriminants
(Instr
, T
);
4543 Target_Expr
:= New_Occurrence_Of
(Temp
, Loc
);
4544 Aggr_Code
:= Build_Record_Aggr_Code
(N
, T
, Target_Expr
);
4546 -- Save the last assignment statement associated with the aggregate
4547 -- when building a controlled object. This reference is utilized by
4548 -- the finalization machinery when marking an object as successfully
4551 if Needs_Finalization
(T
) then
4552 Set_Last_Aggregate_Assignment
(Temp
, Last
(Aggr_Code
));
4555 Insert_Actions
(N
, Aggr_Code
);
4556 Rewrite
(N
, New_Occurrence_Of
(Temp
, Loc
));
4557 Analyze_And_Resolve
(N
, T
);
4559 end Convert_To_Assignments
;
4561 ---------------------------
4562 -- Convert_To_Positional --
4563 ---------------------------
4565 procedure Convert_To_Positional
4567 Max_Others_Replicate
: Nat
:= 32;
4568 Handle_Bit_Packed
: Boolean := False)
4570 Typ
: constant Entity_Id
:= Etype
(N
);
4572 Static_Components
: Boolean := True;
4574 procedure Check_Static_Components
;
4575 -- Check whether all components of the aggregate are compile-time known
4576 -- values, and can be passed as is to the back-end without further
4582 Ixb
: Node_Id
) return Boolean;
4583 -- Convert the aggregate into a purely positional form if possible. On
4584 -- entry the bounds of all dimensions are known to be static, and the
4585 -- total number of components is safe enough to expand.
4587 function Is_Flat
(N
: Node_Id
; Dims
: Int
) return Boolean;
4588 -- Return True iff the array N is flat (which is not trivial in the case
4589 -- of multidimensional aggregates).
4591 function Is_Static_Element
(N
: Node_Id
) return Boolean;
4592 -- Return True if N, an element of a component association list, i.e.
4593 -- N_Component_Association or N_Iterated_Component_Association, has a
4594 -- compile-time known value and can be passed as is to the back-end
4595 -- without further expansion.
4596 -- An Iterated_Component_Association is treated as nonstatic in most
4597 -- cases for now, so there are possibilities for optimization.
4599 -----------------------------
4600 -- Check_Static_Components --
4601 -----------------------------
4603 -- Could use some comments in this body ???
4605 procedure Check_Static_Components
is
4610 Static_Components
:= True;
4612 if Nkind
(N
) = N_String_Literal
then
4615 elsif Present
(Expressions
(N
)) then
4616 Expr
:= First
(Expressions
(N
));
4617 while Present
(Expr
) loop
4618 if Nkind
(Expr
) /= N_Aggregate
4619 or else not Compile_Time_Known_Aggregate
(Expr
)
4620 or else Expansion_Delayed
(Expr
)
4622 Static_Components
:= False;
4630 if Nkind
(N
) = N_Aggregate
4631 and then Present
(Component_Associations
(N
))
4633 Assoc
:= First
(Component_Associations
(N
));
4634 while Present
(Assoc
) loop
4635 if not Is_Static_Element
(Assoc
) then
4636 Static_Components
:= False;
4643 end Check_Static_Components
;
4652 Ixb
: Node_Id
) return Boolean
4654 Loc
: constant Source_Ptr
:= Sloc
(N
);
4655 Blo
: constant Node_Id
:= Type_Low_Bound
(Etype
(Ixb
));
4656 Lo
: constant Node_Id
:= Type_Low_Bound
(Etype
(Ix
));
4657 Hi
: constant Node_Id
:= Type_High_Bound
(Etype
(Ix
));
4661 Others_Present
: Boolean := False;
4664 if Nkind
(Original_Node
(N
)) = N_String_Literal
then
4668 if not Compile_Time_Known_Value
(Lo
)
4669 or else not Compile_Time_Known_Value
(Hi
)
4674 Lov
:= Expr_Value
(Lo
);
4675 Hiv
:= Expr_Value
(Hi
);
4677 -- Check if there is an others choice
4679 if Present
(Component_Associations
(N
)) then
4685 Assoc
:= First
(Component_Associations
(N
));
4686 while Present
(Assoc
) loop
4688 -- If this is a box association, flattening is in general
4689 -- not possible because at this point we cannot tell if the
4690 -- default is static or even exists.
4692 if Box_Present
(Assoc
) then
4695 elsif Nkind
(Assoc
) = N_Iterated_Component_Association
then
4699 Choice
:= First
(Choice_List
(Assoc
));
4701 while Present
(Choice
) loop
4702 if Nkind
(Choice
) = N_Others_Choice
then
4703 Others_Present
:= True;
4714 -- If the low bound is not known at compile time and others is not
4715 -- present we can proceed since the bounds can be obtained from the
4719 or else (not Compile_Time_Known_Value
(Blo
) and then Others_Present
)
4724 -- Determine if set of alternatives is suitable for conversion and
4725 -- build an array containing the values in sequence.
4728 Vals
: array (UI_To_Int
(Lov
) .. UI_To_Int
(Hiv
))
4729 of Node_Id
:= (others => Empty
);
4730 -- The values in the aggregate sorted appropriately
4733 -- Same data as Vals in list form
4736 -- Used to validate Max_Others_Replicate limit
4739 Num
: Int
:= UI_To_Int
(Lov
);
4745 if Present
(Expressions
(N
)) then
4746 Elmt
:= First
(Expressions
(N
));
4747 while Present
(Elmt
) loop
4748 if Nkind
(Elmt
) = N_Aggregate
4749 and then Present
(Next_Index
(Ix
))
4751 not Flatten
(Elmt
, Next_Index
(Ix
), Next_Index
(Ixb
))
4756 -- Duplicate expression for each index it covers
4758 Vals
(Num
) := New_Copy_Tree
(Elmt
);
4765 if No
(Component_Associations
(N
)) then
4769 Elmt
:= First
(Component_Associations
(N
));
4771 if Nkind
(Expression
(Elmt
)) = N_Aggregate
then
4772 if Present
(Next_Index
(Ix
))
4775 (Expression
(Elmt
), Next_Index
(Ix
), Next_Index
(Ixb
))
4781 Component_Loop
: while Present
(Elmt
) loop
4782 Choice
:= First
(Choice_List
(Elmt
));
4783 Choice_Loop
: while Present
(Choice
) loop
4785 -- If we have an others choice, fill in the missing elements
4786 -- subject to the limit established by Max_Others_Replicate.
4788 if Nkind
(Choice
) = N_Others_Choice
then
4791 -- If the expression involves a construct that generates
4792 -- a loop, we must generate individual assignments and
4793 -- no flattening is possible.
4795 if Nkind
(Expression
(Elmt
)) = N_Quantified_Expression
4800 for J
in Vals
'Range loop
4801 if No
(Vals
(J
)) then
4802 Vals
(J
) := New_Copy_Tree
(Expression
(Elmt
));
4803 Rep_Count
:= Rep_Count
+ 1;
4805 -- Check for maximum others replication. Note that
4806 -- we skip this test if either of the restrictions
4807 -- No_Elaboration_Code or No_Implicit_Loops is
4808 -- active, if this is a preelaborable unit or
4809 -- a predefined unit, or if the unit must be
4810 -- placed in data memory. This also ensures that
4811 -- predefined units get the same level of constant
4812 -- folding in Ada 95 and Ada 2005, where their
4813 -- categorization has changed.
4816 P
: constant Entity_Id
:=
4817 Cunit_Entity
(Current_Sem_Unit
);
4820 -- Check if duplication is always OK and, if so,
4821 -- continue processing.
4823 if Restriction_Active
(No_Elaboration_Code
)
4824 or else Restriction_Active
(No_Implicit_Loops
)
4826 (Ekind
(Current_Scope
) = E_Package
4827 and then Static_Elaboration_Desired
4829 or else Is_Preelaborated
(P
)
4830 or else (Ekind
(P
) = E_Package_Body
4832 Is_Preelaborated
(Spec_Entity
(P
)))
4834 Is_Predefined_Unit
(Get_Source_Unit
(P
))
4838 -- If duplication is not always OK, continue
4839 -- only if either the element is static or is
4840 -- an aggregate which can itself be flattened,
4841 -- and the replication count is not too high.
4843 elsif (Is_Static_Element
(Elmt
)
4845 (Nkind
(Expression
(Elmt
)) = N_Aggregate
4846 and then Present
(Next_Index
(Ix
))))
4847 and then Rep_Count
<= Max_Others_Replicate
4851 -- Return False in all the other cases
4861 and then Warn_On_Redundant_Constructs
4863 Error_Msg_N
("there are no others?r?", Elmt
);
4866 exit Component_Loop
;
4868 -- Case of a subtype mark, identifier or expanded name
4870 elsif Is_Entity_Name
(Choice
)
4871 and then Is_Type
(Entity
(Choice
))
4873 Lo
:= Type_Low_Bound
(Etype
(Choice
));
4874 Hi
:= Type_High_Bound
(Etype
(Choice
));
4876 -- Case of subtype indication
4878 elsif Nkind
(Choice
) = N_Subtype_Indication
then
4879 Lo
:= Low_Bound
(Range_Expression
(Constraint
(Choice
)));
4880 Hi
:= High_Bound
(Range_Expression
(Constraint
(Choice
)));
4884 elsif Nkind
(Choice
) = N_Range
then
4885 Lo
:= Low_Bound
(Choice
);
4886 Hi
:= High_Bound
(Choice
);
4888 -- Normal subexpression case
4890 else pragma Assert
(Nkind
(Choice
) in N_Subexpr
);
4891 if not Compile_Time_Known_Value
(Choice
) then
4895 Choice_Index
:= UI_To_Int
(Expr_Value
(Choice
));
4897 if Choice_Index
in Vals
'Range then
4898 Vals
(Choice_Index
) :=
4899 New_Copy_Tree
(Expression
(Elmt
));
4902 -- Choice is statically out-of-range, will be
4903 -- rewritten to raise Constraint_Error.
4911 -- Range cases merge with Lo,Hi set
4913 if not Compile_Time_Known_Value
(Lo
)
4915 not Compile_Time_Known_Value
(Hi
)
4920 for J
in UI_To_Int
(Expr_Value
(Lo
)) ..
4921 UI_To_Int
(Expr_Value
(Hi
))
4923 Vals
(J
) := New_Copy_Tree
(Expression
(Elmt
));
4929 end loop Choice_Loop
;
4932 end loop Component_Loop
;
4934 -- If we get here the conversion is possible
4937 for J
in Vals
'Range loop
4938 Append
(Vals
(J
), Vlist
);
4941 Rewrite
(N
, Make_Aggregate
(Loc
, Expressions
=> Vlist
));
4942 Set_Aggregate_Bounds
(N
, Aggregate_Bounds
(Original_Node
(N
)));
4951 function Is_Flat
(N
: Node_Id
; Dims
: Int
) return Boolean is
4958 elsif Nkind
(N
) = N_Aggregate
then
4959 if Present
(Component_Associations
(N
)) then
4963 Elmt
:= First
(Expressions
(N
));
4964 while Present
(Elmt
) loop
4965 if not Is_Flat
(Elmt
, Dims
- 1) then
4979 -------------------------
4980 -- Is_Static_Element --
4981 -------------------------
4983 function Is_Static_Element
(N
: Node_Id
) return Boolean is
4984 Expr
: constant Node_Id
:= Expression
(N
);
4987 if Nkind_In
(Expr
, N_Integer_Literal
, N_Real_Literal
) then
4990 elsif Is_Entity_Name
(Expr
)
4991 and then Present
(Entity
(Expr
))
4992 and then Ekind
(Entity
(Expr
)) = E_Enumeration_Literal
4996 elsif Nkind
(N
) = N_Iterated_Component_Association
then
4999 elsif Nkind
(Expr
) = N_Aggregate
5000 and then Compile_Time_Known_Aggregate
(Expr
)
5001 and then not Expansion_Delayed
(Expr
)
5008 end Is_Static_Element
;
5010 -- Start of processing for Convert_To_Positional
5013 -- Only convert to positional when generating C in case of an
5014 -- object declaration, this is the only case where aggregates are
5017 if Modify_Tree_For_C
and then not Is_CCG_Supported_Aggregate
(N
) then
5021 -- Ada 2005 (AI-287): Do not convert in case of default initialized
5022 -- components because in this case will need to call the corresponding
5025 if Has_Default_Init_Comps
(N
) then
5029 -- A subaggregate may have been flattened but is not known to be
5030 -- Compile_Time_Known. Set that flag in cases that cannot require
5031 -- elaboration code, so that the aggregate can be used as the
5032 -- initial value of a thread-local variable.
5034 if Is_Flat
(N
, Number_Dimensions
(Typ
)) then
5035 if Static_Array_Aggregate
(N
) then
5036 Set_Compile_Time_Known_Aggregate
(N
);
5042 if Is_Bit_Packed_Array
(Typ
) and then not Handle_Bit_Packed
then
5046 -- Do not convert to positional if controlled components are involved
5047 -- since these require special processing
5049 if Has_Controlled_Component
(Typ
) then
5053 Check_Static_Components
;
5055 -- If the size is known, or all the components are static, try to
5056 -- build a fully positional aggregate.
5058 -- The size of the type may not be known for an aggregate with
5059 -- discriminated array components, but if the components are static
5060 -- it is still possible to verify statically that the length is
5061 -- compatible with the upper bound of the type, and therefore it is
5062 -- worth flattening such aggregates as well.
5064 -- For now the back-end expands these aggregates into individual
5065 -- assignments to the target anyway, but it is conceivable that
5066 -- it will eventually be able to treat such aggregates statically???
5068 if Aggr_Size_OK
(N
, Typ
)
5069 and then Flatten
(N
, First_Index
(Typ
), First_Index
(Base_Type
(Typ
)))
5071 if Static_Components
then
5072 Set_Compile_Time_Known_Aggregate
(N
);
5073 Set_Expansion_Delayed
(N
, False);
5076 Analyze_And_Resolve
(N
, Typ
);
5079 -- If Static_Elaboration_Desired has been specified, diagnose aggregates
5080 -- that will still require initialization code.
5082 if (Ekind
(Current_Scope
) = E_Package
5083 and then Static_Elaboration_Desired
(Current_Scope
))
5084 and then Nkind
(Parent
(N
)) = N_Object_Declaration
5090 if Nkind
(N
) = N_Aggregate
and then Present
(Expressions
(N
)) then
5091 Expr
:= First
(Expressions
(N
));
5092 while Present
(Expr
) loop
5093 if Nkind_In
(Expr
, N_Integer_Literal
, N_Real_Literal
)
5095 (Is_Entity_Name
(Expr
)
5096 and then Ekind
(Entity
(Expr
)) = E_Enumeration_Literal
)
5102 ("non-static object requires elaboration code??", N
);
5109 if Present
(Component_Associations
(N
)) then
5110 Error_Msg_N
("object requires elaboration code??", N
);
5115 end Convert_To_Positional
;
5117 ----------------------------
5118 -- Expand_Array_Aggregate --
5119 ----------------------------
5121 -- Array aggregate expansion proceeds as follows:
5123 -- 1. If requested we generate code to perform all the array aggregate
5124 -- bound checks, specifically
5126 -- (a) Check that the index range defined by aggregate bounds is
5127 -- compatible with corresponding index subtype.
5129 -- (b) If an others choice is present check that no aggregate
5130 -- index is outside the bounds of the index constraint.
5132 -- (c) For multidimensional arrays make sure that all subaggregates
5133 -- corresponding to the same dimension have the same bounds.
5135 -- 2. Check for packed array aggregate which can be converted to a
5136 -- constant so that the aggregate disappears completely.
5138 -- 3. Check case of nested aggregate. Generally nested aggregates are
5139 -- handled during the processing of the parent aggregate.
5141 -- 4. Check if the aggregate can be statically processed. If this is the
5142 -- case pass it as is to Gigi. Note that a necessary condition for
5143 -- static processing is that the aggregate be fully positional.
5145 -- 5. If in-place aggregate expansion is possible (i.e. no need to create
5146 -- a temporary) then mark the aggregate as such and return. Otherwise
5147 -- create a new temporary and generate the appropriate initialization
5150 procedure Expand_Array_Aggregate
(N
: Node_Id
) is
5151 Loc
: constant Source_Ptr
:= Sloc
(N
);
5153 Typ
: constant Entity_Id
:= Etype
(N
);
5154 Ctyp
: constant Entity_Id
:= Component_Type
(Typ
);
5155 -- Typ is the correct constrained array subtype of the aggregate
5156 -- Ctyp is the corresponding component type.
5158 Aggr_Dimension
: constant Pos
:= Number_Dimensions
(Typ
);
5159 -- Number of aggregate index dimensions
5161 Aggr_Low
: array (1 .. Aggr_Dimension
) of Node_Id
;
5162 Aggr_High
: array (1 .. Aggr_Dimension
) of Node_Id
;
5163 -- Low and High bounds of the constraint for each aggregate index
5165 Aggr_Index_Typ
: array (1 .. Aggr_Dimension
) of Entity_Id
;
5166 -- The type of each index
5168 In_Place_Assign_OK_For_Declaration
: Boolean := False;
5169 -- True if we are to generate an in-place assignment for a declaration
5171 Maybe_In_Place_OK
: Boolean;
5172 -- If the type is neither controlled nor packed and the aggregate
5173 -- is the expression in an assignment, assignment in place may be
5174 -- possible, provided other conditions are met on the LHS.
5176 Others_Present
: array (1 .. Aggr_Dimension
) of Boolean :=
5178 -- If Others_Present (J) is True, then there is an others choice in one
5179 -- of the subaggregates of N at dimension J.
5181 function Aggr_Assignment_OK_For_Backend
(N
: Node_Id
) return Boolean;
5182 -- Returns true if an aggregate assignment can be done by the back end
5184 procedure Build_Constrained_Type
(Positional
: Boolean);
5185 -- If the subtype is not static or unconstrained, build a constrained
5186 -- type using the computable sizes of the aggregate and its sub-
5189 procedure Check_Bounds
(Aggr_Bounds
: Node_Id
; Index_Bounds
: Node_Id
);
5190 -- Checks that the bounds of Aggr_Bounds are within the bounds defined
5193 procedure Check_Same_Aggr_Bounds
(Sub_Aggr
: Node_Id
; Dim
: Pos
);
5194 -- Checks that in a multidimensional array aggregate all subaggregates
5195 -- corresponding to the same dimension have the same bounds. Sub_Aggr is
5196 -- an array subaggregate. Dim is the dimension corresponding to the
5199 procedure Compute_Others_Present
(Sub_Aggr
: Node_Id
; Dim
: Pos
);
5200 -- Computes the values of array Others_Present. Sub_Aggr is the array
5201 -- subaggregate we start the computation from. Dim is the dimension
5202 -- corresponding to the subaggregate.
5204 procedure Others_Check
(Sub_Aggr
: Node_Id
; Dim
: Pos
);
5205 -- Checks that if an others choice is present in any subaggregate, no
5206 -- aggregate index is outside the bounds of the index constraint.
5207 -- Sub_Aggr is an array subaggregate. Dim is the dimension corresponding
5208 -- to the subaggregate.
5210 function Safe_Left_Hand_Side
(N
: Node_Id
) return Boolean;
5211 -- In addition to Maybe_In_Place_OK, in order for an aggregate to be
5212 -- built directly into the target of the assignment it must be free
5215 ------------------------------------
5216 -- Aggr_Assignment_OK_For_Backend --
5217 ------------------------------------
5219 -- Backend processing by Gigi/gcc is possible only if all the following
5220 -- conditions are met:
5222 -- 1. N consists of a single OTHERS choice, possibly recursively
5224 -- 2. The array type has no null ranges (the purpose of this is to
5225 -- avoid a bogus warning for an out-of-range value).
5227 -- 3. The array type has no atomic components
5229 -- 4. The component type is elementary
5231 -- 5. The component size is a multiple of Storage_Unit
5233 -- 6. The component size is Storage_Unit or the value is of the form
5234 -- M * (1 + A**1 + A**2 + .. A**(K-1)) where A = 2**(Storage_Unit)
5235 -- and M in 1 .. A-1. This can also be viewed as K occurrences of
5236 -- the 8-bit value M, concatenated together.
5238 -- The ultimate goal is to generate a call to a fast memset routine
5239 -- specifically optimized for the target.
5241 function Aggr_Assignment_OK_For_Backend
(N
: Node_Id
) return Boolean is
5253 -- Back end doesn't know about <>
5255 if Has_Default_Init_Comps
(N
) then
5259 -- Recurse as far as possible to find the innermost component type
5263 while Is_Array_Type
(Ctyp
) loop
5264 if Nkind
(Expr
) /= N_Aggregate
5265 or else not Is_Others_Aggregate
(Expr
)
5270 Index
:= First_Index
(Ctyp
);
5271 while Present
(Index
) loop
5272 Get_Index_Bounds
(Index
, Low
, High
);
5274 if Is_Null_Range
(Low
, High
) then
5281 Expr
:= Expression
(First
(Component_Associations
(Expr
)));
5283 for J
in 1 .. Number_Dimensions
(Ctyp
) - 1 loop
5284 if Nkind
(Expr
) /= N_Aggregate
5285 or else not Is_Others_Aggregate
(Expr
)
5290 Expr
:= Expression
(First
(Component_Associations
(Expr
)));
5293 if Has_Atomic_Components
(Ctyp
) then
5297 Csiz
:= Component_Size
(Ctyp
);
5298 Ctyp
:= Component_Type
(Ctyp
);
5300 if Is_Atomic_Or_VFA
(Ctyp
) then
5305 -- An Iterated_Component_Association involves a loop (in most cases)
5306 -- and is never static.
5308 if Nkind
(Parent
(Expr
)) = N_Iterated_Component_Association
then
5312 -- Access types need to be dealt with specially
5314 if Is_Access_Type
(Ctyp
) then
5316 -- Component_Size is not set by Layout_Type if the component
5317 -- type is an access type ???
5319 Csiz
:= Esize
(Ctyp
);
5321 -- Fat pointers are rejected as they are not really elementary
5324 if Csiz
/= System_Address_Size
then
5328 -- The supported expressions are NULL and constants, others are
5329 -- rejected upfront to avoid being analyzed below, which can be
5330 -- problematic for some of them, for example allocators.
5332 if Nkind
(Expr
) /= N_Null
and then not Is_Entity_Name
(Expr
) then
5336 -- Scalar types are OK if their size is a multiple of Storage_Unit
5338 elsif Is_Scalar_Type
(Ctyp
) then
5339 if Csiz
mod System_Storage_Unit
/= 0 then
5343 -- Composite types are rejected
5349 -- If the expression has side effects (e.g. contains calls with
5350 -- potential side effects) reject as well. We only preanalyze the
5351 -- expression to prevent the removal of intended side effects.
5353 Preanalyze_And_Resolve
(Expr
, Ctyp
);
5355 if not Side_Effect_Free
(Expr
) then
5359 -- The expression needs to be analyzed if True is returned
5361 Analyze_And_Resolve
(Expr
, Ctyp
);
5363 -- Strip away any conversions from the expression as they simply
5364 -- qualify the real expression.
5366 while Nkind_In
(Expr
, N_Unchecked_Type_Conversion
,
5369 Expr
:= Expression
(Expr
);
5372 Nunits
:= UI_To_Int
(Csiz
) / System_Storage_Unit
;
5378 if not Compile_Time_Known_Value
(Expr
) then
5382 -- The only supported value for floating point is 0.0
5384 if Is_Floating_Point_Type
(Ctyp
) then
5385 return Expr_Value_R
(Expr
) = Ureal_0
;
5388 -- For other types, we can look into the value as an integer
5390 Value
:= Expr_Value
(Expr
);
5392 if Has_Biased_Representation
(Ctyp
) then
5393 Value
:= Value
- Expr_Value
(Type_Low_Bound
(Ctyp
));
5396 -- Values 0 and -1 immediately satisfy the last check
5398 if Value
= Uint_0
or else Value
= Uint_Minus_1
then
5402 -- We need to work with an unsigned value
5405 Value
:= Value
+ 2**(System_Storage_Unit
* Nunits
);
5408 Remainder
:= Value
rem 2**System_Storage_Unit
;
5410 for J
in 1 .. Nunits
- 1 loop
5411 Value
:= Value
/ 2**System_Storage_Unit
;
5413 if Value
rem 2**System_Storage_Unit
/= Remainder
then
5419 end Aggr_Assignment_OK_For_Backend
;
5421 ----------------------------
5422 -- Build_Constrained_Type --
5423 ----------------------------
5425 procedure Build_Constrained_Type
(Positional
: Boolean) is
5426 Loc
: constant Source_Ptr
:= Sloc
(N
);
5427 Agg_Type
: constant Entity_Id
:= Make_Temporary
(Loc
, 'A');
5430 Typ
: constant Entity_Id
:= Etype
(N
);
5431 Indexes
: constant List_Id
:= New_List
;
5436 -- If the aggregate is purely positional, all its subaggregates
5437 -- have the same size. We collect the dimensions from the first
5438 -- subaggregate at each level.
5443 for D
in 1 .. Number_Dimensions
(Typ
) loop
5444 Sub_Agg
:= First
(Expressions
(Sub_Agg
));
5448 while Present
(Comp
) loop
5455 Low_Bound
=> Make_Integer_Literal
(Loc
, 1),
5456 High_Bound
=> Make_Integer_Literal
(Loc
, Num
)));
5460 -- We know the aggregate type is unconstrained and the aggregate
5461 -- is not processable by the back end, therefore not necessarily
5462 -- positional. Retrieve each dimension bounds (computed earlier).
5464 for D
in 1 .. Number_Dimensions
(Typ
) loop
5467 Low_Bound
=> Aggr_Low
(D
),
5468 High_Bound
=> Aggr_High
(D
)));
5473 Make_Full_Type_Declaration
(Loc
,
5474 Defining_Identifier
=> Agg_Type
,
5476 Make_Constrained_Array_Definition
(Loc
,
5477 Discrete_Subtype_Definitions
=> Indexes
,
5478 Component_Definition
=>
5479 Make_Component_Definition
(Loc
,
5480 Aliased_Present
=> False,
5481 Subtype_Indication
=>
5482 New_Occurrence_Of
(Component_Type
(Typ
), Loc
))));
5484 Insert_Action
(N
, Decl
);
5486 Set_Etype
(N
, Agg_Type
);
5487 Set_Is_Itype
(Agg_Type
);
5488 Freeze_Itype
(Agg_Type
, N
);
5489 end Build_Constrained_Type
;
5495 procedure Check_Bounds
(Aggr_Bounds
: Node_Id
; Index_Bounds
: Node_Id
) is
5502 Cond
: Node_Id
:= Empty
;
5505 Get_Index_Bounds
(Aggr_Bounds
, Aggr_Lo
, Aggr_Hi
);
5506 Get_Index_Bounds
(Index_Bounds
, Ind_Lo
, Ind_Hi
);
5508 -- Generate the following test:
5510 -- [constraint_error when
5511 -- Aggr_Lo <= Aggr_Hi and then
5512 -- (Aggr_Lo < Ind_Lo or else Aggr_Hi > Ind_Hi)]
5514 -- As an optimization try to see if some tests are trivially vacuous
5515 -- because we are comparing an expression against itself.
5517 if Aggr_Lo
= Ind_Lo
and then Aggr_Hi
= Ind_Hi
then
5520 elsif Aggr_Hi
= Ind_Hi
then
5523 Left_Opnd
=> Duplicate_Subexpr_Move_Checks
(Aggr_Lo
),
5524 Right_Opnd
=> Duplicate_Subexpr_Move_Checks
(Ind_Lo
));
5526 elsif Aggr_Lo
= Ind_Lo
then
5529 Left_Opnd
=> Duplicate_Subexpr_Move_Checks
(Aggr_Hi
),
5530 Right_Opnd
=> Duplicate_Subexpr_Move_Checks
(Ind_Hi
));
5537 Left_Opnd
=> Duplicate_Subexpr_Move_Checks
(Aggr_Lo
),
5538 Right_Opnd
=> Duplicate_Subexpr_Move_Checks
(Ind_Lo
)),
5542 Left_Opnd
=> Duplicate_Subexpr
(Aggr_Hi
),
5543 Right_Opnd
=> Duplicate_Subexpr
(Ind_Hi
)));
5546 if Present
(Cond
) then
5551 Left_Opnd
=> Duplicate_Subexpr_Move_Checks
(Aggr_Lo
),
5552 Right_Opnd
=> Duplicate_Subexpr_Move_Checks
(Aggr_Hi
)),
5554 Right_Opnd
=> Cond
);
5556 Set_Analyzed
(Left_Opnd
(Left_Opnd
(Cond
)), False);
5557 Set_Analyzed
(Right_Opnd
(Left_Opnd
(Cond
)), False);
5559 Make_Raise_Constraint_Error
(Loc
,
5561 Reason
=> CE_Range_Check_Failed
));
5565 ----------------------------
5566 -- Check_Same_Aggr_Bounds --
5567 ----------------------------
5569 procedure Check_Same_Aggr_Bounds
(Sub_Aggr
: Node_Id
; Dim
: Pos
) is
5570 Sub_Lo
: constant Node_Id
:= Low_Bound
(Aggregate_Bounds
(Sub_Aggr
));
5571 Sub_Hi
: constant Node_Id
:= High_Bound
(Aggregate_Bounds
(Sub_Aggr
));
5572 -- The bounds of this specific subaggregate
5574 Aggr_Lo
: constant Node_Id
:= Aggr_Low
(Dim
);
5575 Aggr_Hi
: constant Node_Id
:= Aggr_High
(Dim
);
5576 -- The bounds of the aggregate for this dimension
5578 Ind_Typ
: constant Entity_Id
:= Aggr_Index_Typ
(Dim
);
5579 -- The index type for this dimension.xxx
5581 Cond
: Node_Id
:= Empty
;
5586 -- If index checks are on generate the test
5588 -- [constraint_error when
5589 -- Aggr_Lo /= Sub_Lo or else Aggr_Hi /= Sub_Hi]
5591 -- As an optimization try to see if some tests are trivially vacuos
5592 -- because we are comparing an expression against itself. Also for
5593 -- the first dimension the test is trivially vacuous because there
5594 -- is just one aggregate for dimension 1.
5596 if Index_Checks_Suppressed
(Ind_Typ
) then
5599 elsif Dim
= 1 or else (Aggr_Lo
= Sub_Lo
and then Aggr_Hi
= Sub_Hi
)
5603 elsif Aggr_Hi
= Sub_Hi
then
5606 Left_Opnd
=> Duplicate_Subexpr_Move_Checks
(Aggr_Lo
),
5607 Right_Opnd
=> Duplicate_Subexpr_Move_Checks
(Sub_Lo
));
5609 elsif Aggr_Lo
= Sub_Lo
then
5612 Left_Opnd
=> Duplicate_Subexpr_Move_Checks
(Aggr_Hi
),
5613 Right_Opnd
=> Duplicate_Subexpr_Move_Checks
(Sub_Hi
));
5620 Left_Opnd
=> Duplicate_Subexpr_Move_Checks
(Aggr_Lo
),
5621 Right_Opnd
=> Duplicate_Subexpr_Move_Checks
(Sub_Lo
)),
5625 Left_Opnd
=> Duplicate_Subexpr
(Aggr_Hi
),
5626 Right_Opnd
=> Duplicate_Subexpr
(Sub_Hi
)));
5629 if Present
(Cond
) then
5631 Make_Raise_Constraint_Error
(Loc
,
5633 Reason
=> CE_Length_Check_Failed
));
5636 -- Now look inside the subaggregate to see if there is more work
5638 if Dim
< Aggr_Dimension
then
5640 -- Process positional components
5642 if Present
(Expressions
(Sub_Aggr
)) then
5643 Expr
:= First
(Expressions
(Sub_Aggr
));
5644 while Present
(Expr
) loop
5645 Check_Same_Aggr_Bounds
(Expr
, Dim
+ 1);
5650 -- Process component associations
5652 if Present
(Component_Associations
(Sub_Aggr
)) then
5653 Assoc
:= First
(Component_Associations
(Sub_Aggr
));
5654 while Present
(Assoc
) loop
5655 Expr
:= Expression
(Assoc
);
5656 Check_Same_Aggr_Bounds
(Expr
, Dim
+ 1);
5661 end Check_Same_Aggr_Bounds
;
5663 ----------------------------
5664 -- Compute_Others_Present --
5665 ----------------------------
5667 procedure Compute_Others_Present
(Sub_Aggr
: Node_Id
; Dim
: Pos
) is
5672 if Present
(Component_Associations
(Sub_Aggr
)) then
5673 Assoc
:= Last
(Component_Associations
(Sub_Aggr
));
5675 if Nkind
(First
(Choice_List
(Assoc
))) = N_Others_Choice
then
5676 Others_Present
(Dim
) := True;
5680 -- Now look inside the subaggregate to see if there is more work
5682 if Dim
< Aggr_Dimension
then
5684 -- Process positional components
5686 if Present
(Expressions
(Sub_Aggr
)) then
5687 Expr
:= First
(Expressions
(Sub_Aggr
));
5688 while Present
(Expr
) loop
5689 Compute_Others_Present
(Expr
, Dim
+ 1);
5694 -- Process component associations
5696 if Present
(Component_Associations
(Sub_Aggr
)) then
5697 Assoc
:= First
(Component_Associations
(Sub_Aggr
));
5698 while Present
(Assoc
) loop
5699 Expr
:= Expression
(Assoc
);
5700 Compute_Others_Present
(Expr
, Dim
+ 1);
5705 end Compute_Others_Present
;
5711 procedure Others_Check
(Sub_Aggr
: Node_Id
; Dim
: Pos
) is
5712 Aggr_Lo
: constant Node_Id
:= Aggr_Low
(Dim
);
5713 Aggr_Hi
: constant Node_Id
:= Aggr_High
(Dim
);
5714 -- The bounds of the aggregate for this dimension
5716 Ind_Typ
: constant Entity_Id
:= Aggr_Index_Typ
(Dim
);
5717 -- The index type for this dimension
5719 Need_To_Check
: Boolean := False;
5721 Choices_Lo
: Node_Id
:= Empty
;
5722 Choices_Hi
: Node_Id
:= Empty
;
5723 -- The lowest and highest discrete choices for a named subaggregate
5725 Nb_Choices
: Int
:= -1;
5726 -- The number of discrete non-others choices in this subaggregate
5728 Nb_Elements
: Uint
:= Uint_0
;
5729 -- The number of elements in a positional aggregate
5731 Cond
: Node_Id
:= Empty
;
5738 -- Check if we have an others choice. If we do make sure that this
5739 -- subaggregate contains at least one element in addition to the
5742 if Range_Checks_Suppressed
(Ind_Typ
) then
5743 Need_To_Check
:= False;
5745 elsif Present
(Expressions
(Sub_Aggr
))
5746 and then Present
(Component_Associations
(Sub_Aggr
))
5748 Need_To_Check
:= True;
5750 elsif Present
(Component_Associations
(Sub_Aggr
)) then
5751 Assoc
:= Last
(Component_Associations
(Sub_Aggr
));
5753 if Nkind
(First
(Choice_List
(Assoc
))) /= N_Others_Choice
then
5754 Need_To_Check
:= False;
5757 -- Count the number of discrete choices. Start with -1 because
5758 -- the others choice does not count.
5760 -- Is there some reason we do not use List_Length here ???
5763 Assoc
:= First
(Component_Associations
(Sub_Aggr
));
5764 while Present
(Assoc
) loop
5765 Choice
:= First
(Choice_List
(Assoc
));
5766 while Present
(Choice
) loop
5767 Nb_Choices
:= Nb_Choices
+ 1;
5774 -- If there is only an others choice nothing to do
5776 Need_To_Check
:= (Nb_Choices
> 0);
5780 Need_To_Check
:= False;
5783 -- If we are dealing with a positional subaggregate with an others
5784 -- choice then compute the number or positional elements.
5786 if Need_To_Check
and then Present
(Expressions
(Sub_Aggr
)) then
5787 Expr
:= First
(Expressions
(Sub_Aggr
));
5788 Nb_Elements
:= Uint_0
;
5789 while Present
(Expr
) loop
5790 Nb_Elements
:= Nb_Elements
+ 1;
5794 -- If the aggregate contains discrete choices and an others choice
5795 -- compute the smallest and largest discrete choice values.
5797 elsif Need_To_Check
then
5798 Compute_Choices_Lo_And_Choices_Hi
: declare
5800 Table
: Case_Table_Type
(1 .. Nb_Choices
);
5801 -- Used to sort all the different choice values
5808 Assoc
:= First
(Component_Associations
(Sub_Aggr
));
5809 while Present
(Assoc
) loop
5810 Choice
:= First
(Choice_List
(Assoc
));
5811 while Present
(Choice
) loop
5812 if Nkind
(Choice
) = N_Others_Choice
then
5816 Get_Index_Bounds
(Choice
, Low
, High
);
5817 Table
(J
).Choice_Lo
:= Low
;
5818 Table
(J
).Choice_Hi
:= High
;
5827 -- Sort the discrete choices
5829 Sort_Case_Table
(Table
);
5831 Choices_Lo
:= Table
(1).Choice_Lo
;
5832 Choices_Hi
:= Table
(Nb_Choices
).Choice_Hi
;
5833 end Compute_Choices_Lo_And_Choices_Hi
;
5836 -- If no others choice in this subaggregate, or the aggregate
5837 -- comprises only an others choice, nothing to do.
5839 if not Need_To_Check
then
5842 -- If we are dealing with an aggregate containing an others choice
5843 -- and positional components, we generate the following test:
5845 -- if Ind_Typ'Pos (Aggr_Lo) + (Nb_Elements - 1) >
5846 -- Ind_Typ'Pos (Aggr_Hi)
5848 -- raise Constraint_Error;
5851 elsif Nb_Elements
> Uint_0
then
5857 Make_Attribute_Reference
(Loc
,
5858 Prefix
=> New_Occurrence_Of
(Ind_Typ
, Loc
),
5859 Attribute_Name
=> Name_Pos
,
5862 (Duplicate_Subexpr_Move_Checks
(Aggr_Lo
))),
5863 Right_Opnd
=> Make_Integer_Literal
(Loc
, Nb_Elements
- 1)),
5866 Make_Attribute_Reference
(Loc
,
5867 Prefix
=> New_Occurrence_Of
(Ind_Typ
, Loc
),
5868 Attribute_Name
=> Name_Pos
,
5869 Expressions
=> New_List
(
5870 Duplicate_Subexpr_Move_Checks
(Aggr_Hi
))));
5872 -- If we are dealing with an aggregate containing an others choice
5873 -- and discrete choices we generate the following test:
5875 -- [constraint_error when
5876 -- Choices_Lo < Aggr_Lo or else Choices_Hi > Aggr_Hi];
5883 Left_Opnd
=> Duplicate_Subexpr_Move_Checks
(Choices_Lo
),
5884 Right_Opnd
=> Duplicate_Subexpr_Move_Checks
(Aggr_Lo
)),
5888 Left_Opnd
=> Duplicate_Subexpr
(Choices_Hi
),
5889 Right_Opnd
=> Duplicate_Subexpr
(Aggr_Hi
)));
5892 if Present
(Cond
) then
5894 Make_Raise_Constraint_Error
(Loc
,
5896 Reason
=> CE_Length_Check_Failed
));
5897 -- Questionable reason code, shouldn't that be a
5898 -- CE_Range_Check_Failed ???
5901 -- Now look inside the subaggregate to see if there is more work
5903 if Dim
< Aggr_Dimension
then
5905 -- Process positional components
5907 if Present
(Expressions
(Sub_Aggr
)) then
5908 Expr
:= First
(Expressions
(Sub_Aggr
));
5909 while Present
(Expr
) loop
5910 Others_Check
(Expr
, Dim
+ 1);
5915 -- Process component associations
5917 if Present
(Component_Associations
(Sub_Aggr
)) then
5918 Assoc
:= First
(Component_Associations
(Sub_Aggr
));
5919 while Present
(Assoc
) loop
5920 Expr
:= Expression
(Assoc
);
5921 Others_Check
(Expr
, Dim
+ 1);
5928 -------------------------
5929 -- Safe_Left_Hand_Side --
5930 -------------------------
5932 function Safe_Left_Hand_Side
(N
: Node_Id
) return Boolean is
5933 function Is_Safe_Index
(Indx
: Node_Id
) return Boolean;
5934 -- If the left-hand side includes an indexed component, check that
5935 -- the indexes are free of side effects.
5941 function Is_Safe_Index
(Indx
: Node_Id
) return Boolean is
5943 if Is_Entity_Name
(Indx
) then
5946 elsif Nkind
(Indx
) = N_Integer_Literal
then
5949 elsif Nkind
(Indx
) = N_Function_Call
5950 and then Is_Entity_Name
(Name
(Indx
))
5951 and then Has_Pragma_Pure_Function
(Entity
(Name
(Indx
)))
5955 elsif Nkind
(Indx
) = N_Type_Conversion
5956 and then Is_Safe_Index
(Expression
(Indx
))
5965 -- Start of processing for Safe_Left_Hand_Side
5968 if Is_Entity_Name
(N
) then
5971 elsif Nkind_In
(N
, N_Explicit_Dereference
, N_Selected_Component
)
5972 and then Safe_Left_Hand_Side
(Prefix
(N
))
5976 elsif Nkind
(N
) = N_Indexed_Component
5977 and then Safe_Left_Hand_Side
(Prefix
(N
))
5978 and then Is_Safe_Index
(First
(Expressions
(N
)))
5982 elsif Nkind
(N
) = N_Unchecked_Type_Conversion
then
5983 return Safe_Left_Hand_Side
(Expression
(N
));
5988 end Safe_Left_Hand_Side
;
5993 -- Holds the temporary aggregate value
5996 -- Holds the declaration of Tmp
5998 Aggr_Code
: List_Id
;
5999 Parent_Node
: Node_Id
;
6000 Parent_Kind
: Node_Kind
;
6002 -- Start of processing for Expand_Array_Aggregate
6005 -- Do not touch the special aggregates of attributes used for Asm calls
6007 if Is_RTE
(Ctyp
, RE_Asm_Input_Operand
)
6008 or else Is_RTE
(Ctyp
, RE_Asm_Output_Operand
)
6012 -- Do not expand an aggregate for an array type which contains tasks if
6013 -- the aggregate is associated with an unexpanded return statement of a
6014 -- build-in-place function. The aggregate is expanded when the related
6015 -- return statement (rewritten into an extended return) is processed.
6016 -- This delay ensures that any temporaries and initialization code
6017 -- generated for the aggregate appear in the proper return block and
6018 -- use the correct _chain and _master.
6020 elsif Has_Task
(Base_Type
(Etype
(N
)))
6021 and then Nkind
(Parent
(N
)) = N_Simple_Return_Statement
6022 and then Is_Build_In_Place_Function
6023 (Return_Applies_To
(Return_Statement_Entity
(Parent
(N
))))
6027 -- Do not attempt expansion if error already detected. We may reach this
6028 -- point in spite of previous errors when compiling with -gnatq, to
6029 -- force all possible errors (this is the usual ACATS mode).
6031 elsif Error_Posted
(N
) then
6035 -- If the semantic analyzer has determined that aggregate N will raise
6036 -- Constraint_Error at run time, then the aggregate node has been
6037 -- replaced with an N_Raise_Constraint_Error node and we should
6040 pragma Assert
(not Raises_Constraint_Error
(N
));
6044 -- Check that the index range defined by aggregate bounds is
6045 -- compatible with corresponding index subtype.
6047 Index_Compatibility_Check
: declare
6048 Aggr_Index_Range
: Node_Id
:= First_Index
(Typ
);
6049 -- The current aggregate index range
6051 Index_Constraint
: Node_Id
:= First_Index
(Etype
(Typ
));
6052 -- The corresponding index constraint against which we have to
6053 -- check the above aggregate index range.
6056 Compute_Others_Present
(N
, 1);
6058 for J
in 1 .. Aggr_Dimension
loop
6059 -- There is no need to emit a check if an others choice is present
6060 -- for this array aggregate dimension since in this case one of
6061 -- N's subaggregates has taken its bounds from the context and
6062 -- these bounds must have been checked already. In addition all
6063 -- subaggregates corresponding to the same dimension must all have
6064 -- the same bounds (checked in (c) below).
6066 if not Range_Checks_Suppressed
(Etype
(Index_Constraint
))
6067 and then not Others_Present
(J
)
6069 -- We don't use Checks.Apply_Range_Check here because it emits
6070 -- a spurious check. Namely it checks that the range defined by
6071 -- the aggregate bounds is nonempty. But we know this already
6074 Check_Bounds
(Aggr_Index_Range
, Index_Constraint
);
6077 -- Save the low and high bounds of the aggregate index as well as
6078 -- the index type for later use in checks (b) and (c) below.
6080 Aggr_Low
(J
) := Low_Bound
(Aggr_Index_Range
);
6081 Aggr_High
(J
) := High_Bound
(Aggr_Index_Range
);
6083 Aggr_Index_Typ
(J
) := Etype
(Index_Constraint
);
6085 Next_Index
(Aggr_Index_Range
);
6086 Next_Index
(Index_Constraint
);
6088 end Index_Compatibility_Check
;
6092 -- If an others choice is present check that no aggregate index is
6093 -- outside the bounds of the index constraint.
6095 Others_Check
(N
, 1);
6099 -- For multidimensional arrays make sure that all subaggregates
6100 -- corresponding to the same dimension have the same bounds.
6102 if Aggr_Dimension
> 1 then
6103 Check_Same_Aggr_Bounds
(N
, 1);
6108 -- If we have a default component value, or simple initialization is
6109 -- required for the component type, then we replace <> in component
6110 -- associations by the required default value.
6113 Default_Val
: Node_Id
;
6117 if (Present
(Default_Aspect_Component_Value
(Typ
))
6118 or else Needs_Simple_Initialization
(Ctyp
))
6119 and then Present
(Component_Associations
(N
))
6121 Assoc
:= First
(Component_Associations
(N
));
6122 while Present
(Assoc
) loop
6123 if Nkind
(Assoc
) = N_Component_Association
6124 and then Box_Present
(Assoc
)
6126 Set_Box_Present
(Assoc
, False);
6128 if Present
(Default_Aspect_Component_Value
(Typ
)) then
6129 Default_Val
:= Default_Aspect_Component_Value
(Typ
);
6131 Default_Val
:= Get_Simple_Init_Val
(Ctyp
, N
);
6134 Set_Expression
(Assoc
, New_Copy_Tree
(Default_Val
));
6135 Analyze_And_Resolve
(Expression
(Assoc
), Ctyp
);
6145 -- Here we test for is packed array aggregate that we can handle at
6146 -- compile time. If so, return with transformation done. Note that we do
6147 -- this even if the aggregate is nested, because once we have done this
6148 -- processing, there is no more nested aggregate.
6150 if Packed_Array_Aggregate_Handled
(N
) then
6154 -- At this point we try to convert to positional form
6156 if Ekind
(Current_Scope
) = E_Package
6157 and then Static_Elaboration_Desired
(Current_Scope
)
6159 Convert_To_Positional
(N
, Max_Others_Replicate
=> 100);
6161 Convert_To_Positional
(N
);
6164 -- if the result is no longer an aggregate (e.g. it may be a string
6165 -- literal, or a temporary which has the needed value), then we are
6166 -- done, since there is no longer a nested aggregate.
6168 if Nkind
(N
) /= N_Aggregate
then
6171 -- We are also done if the result is an analyzed aggregate, indicating
6172 -- that Convert_To_Positional succeeded and reanalyzed the rewritten
6175 elsif Analyzed
(N
) and then Is_Rewrite_Substitution
(N
) then
6179 -- If all aggregate components are compile-time known and the aggregate
6180 -- has been flattened, nothing left to do. The same occurs if the
6181 -- aggregate is used to initialize the components of a statically
6182 -- allocated dispatch table.
6184 if Compile_Time_Known_Aggregate
(N
)
6185 or else Is_Static_Dispatch_Table_Aggregate
(N
)
6187 Set_Expansion_Delayed
(N
, False);
6191 -- Now see if back end processing is possible
6193 if Backend_Processing_Possible
(N
) then
6195 -- If the aggregate is static but the constraints are not, build
6196 -- a static subtype for the aggregate, so that Gigi can place it
6197 -- in static memory. Perform an unchecked_conversion to the non-
6198 -- static type imposed by the context.
6201 Itype
: constant Entity_Id
:= Etype
(N
);
6203 Needs_Type
: Boolean := False;
6206 Index
:= First_Index
(Itype
);
6207 while Present
(Index
) loop
6208 if not Is_OK_Static_Subtype
(Etype
(Index
)) then
6217 Build_Constrained_Type
(Positional
=> True);
6218 Rewrite
(N
, Unchecked_Convert_To
(Itype
, N
));
6228 -- Delay expansion for nested aggregates: it will be taken care of when
6229 -- the parent aggregate is expanded.
6231 Parent_Node
:= Parent
(N
);
6232 Parent_Kind
:= Nkind
(Parent_Node
);
6234 if Parent_Kind
= N_Qualified_Expression
then
6235 Parent_Node
:= Parent
(Parent_Node
);
6236 Parent_Kind
:= Nkind
(Parent_Node
);
6239 if Parent_Kind
= N_Aggregate
6240 or else Parent_Kind
= N_Extension_Aggregate
6241 or else Parent_Kind
= N_Component_Association
6242 or else (Parent_Kind
= N_Object_Declaration
6243 and then Needs_Finalization
(Typ
))
6244 or else (Parent_Kind
= N_Assignment_Statement
6245 and then Inside_Init_Proc
)
6247 Set_Expansion_Delayed
(N
, not Static_Array_Aggregate
(N
));
6253 -- Check whether in-place aggregate expansion is possible
6255 -- For object declarations we build the aggregate in place, unless
6256 -- the array is bit-packed.
6258 -- For assignments we do the assignment in place if all the component
6259 -- associations have compile-time known values, or are default-
6260 -- initialized limited components, e.g. tasks. For other cases we
6261 -- create a temporary. The analysis for safety of on-line assignment
6262 -- is delicate, i.e. we don't know how to do it fully yet ???
6264 -- For allocators we assign to the designated object in place if the
6265 -- aggregate meets the same conditions as other in-place assignments.
6266 -- In this case the aggregate may not come from source but was created
6267 -- for default initialization, e.g. with Initialize_Scalars.
6269 if Requires_Transient_Scope
(Typ
) then
6270 Establish_Transient_Scope
(N
, Manage_Sec_Stack
=> False);
6273 -- An array of limited components is built in place
6275 if Is_Limited_Type
(Typ
) then
6276 Maybe_In_Place_OK
:= True;
6278 elsif Has_Default_Init_Comps
(N
) then
6279 Maybe_In_Place_OK
:= False;
6281 elsif Is_Bit_Packed_Array
(Typ
)
6282 or else Has_Controlled_Component
(Typ
)
6284 Maybe_In_Place_OK
:= False;
6287 Maybe_In_Place_OK
:=
6288 (Nkind
(Parent
(N
)) = N_Assignment_Statement
6289 and then In_Place_Assign_OK
(N
))
6292 (Nkind
(Parent
(Parent
(N
))) = N_Allocator
6293 and then In_Place_Assign_OK
(N
));
6296 -- If this is an array of tasks, it will be expanded into build-in-place
6297 -- assignments. Build an activation chain for the tasks now.
6299 if Has_Task
(Etype
(N
)) then
6300 Build_Activation_Chain_Entity
(N
);
6303 -- Perform in-place expansion of aggregate in an object declaration.
6304 -- Note: actions generated for the aggregate will be captured in an
6305 -- expression-with-actions statement so that they can be transferred
6306 -- to freeze actions later if there is an address clause for the
6307 -- object. (Note: we don't use a block statement because this would
6308 -- cause generated freeze nodes to be elaborated in the wrong scope).
6310 -- Do not perform in-place expansion for SPARK 05 because aggregates are
6311 -- expected to appear in qualified form. In-place expansion eliminates
6312 -- the qualification and eventually violates this SPARK 05 restiction.
6314 -- Arrays of limited components must be built in place. The code
6315 -- previously excluded controlled components but this is an old
6316 -- oversight: the rules in 7.6 (17) are clear.
6318 if Comes_From_Source
(Parent_Node
)
6319 and then Parent_Kind
= N_Object_Declaration
6320 and then Present
(Expression
(Parent_Node
))
6322 Must_Slide
(Etype
(Defining_Identifier
(Parent_Node
)), Typ
)
6323 and then not Is_Bit_Packed_Array
(Typ
)
6324 and then not Restriction_Check_Required
(SPARK_05
)
6326 In_Place_Assign_OK_For_Declaration
:= True;
6327 Tmp
:= Defining_Identifier
(Parent_Node
);
6328 Set_No_Initialization
(Parent_Node
);
6329 Set_Expression
(Parent_Node
, Empty
);
6331 -- Set kind and type of the entity, for use in the analysis
6332 -- of the subsequent assignments. If the nominal type is not
6333 -- constrained, build a subtype from the known bounds of the
6334 -- aggregate. If the declaration has a subtype mark, use it,
6335 -- otherwise use the itype of the aggregate.
6337 Set_Ekind
(Tmp
, E_Variable
);
6339 if not Is_Constrained
(Typ
) then
6340 Build_Constrained_Type
(Positional
=> False);
6342 elsif Is_Entity_Name
(Object_Definition
(Parent_Node
))
6343 and then Is_Constrained
(Entity
(Object_Definition
(Parent_Node
)))
6345 Set_Etype
(Tmp
, Entity
(Object_Definition
(Parent_Node
)));
6348 Set_Size_Known_At_Compile_Time
(Typ
, False);
6349 Set_Etype
(Tmp
, Typ
);
6352 elsif Maybe_In_Place_OK
6353 and then Nkind
(Parent
(N
)) = N_Qualified_Expression
6354 and then Nkind
(Parent
(Parent
(N
))) = N_Allocator
6356 Set_Expansion_Delayed
(N
);
6359 -- Limited arrays in return statements are expanded when
6360 -- enclosing construct is expanded.
6362 elsif Maybe_In_Place_OK
6363 and then Nkind
(Parent
(N
)) = N_Simple_Return_Statement
6365 Set_Expansion_Delayed
(N
);
6368 -- In the remaining cases the aggregate is the RHS of an assignment
6370 elsif Maybe_In_Place_OK
6371 and then Safe_Left_Hand_Side
(Name
(Parent
(N
)))
6373 Tmp
:= Name
(Parent
(N
));
6375 if Etype
(Tmp
) /= Etype
(N
) then
6376 Apply_Length_Check
(N
, Etype
(Tmp
));
6378 if Nkind
(N
) = N_Raise_Constraint_Error
then
6380 -- Static error, nothing further to expand
6386 -- If a slice assignment has an aggregate with a single others_choice,
6387 -- the assignment can be done in place even if bounds are not static,
6388 -- by converting it into a loop over the discrete range of the slice.
6390 elsif Maybe_In_Place_OK
6391 and then Nkind
(Name
(Parent
(N
))) = N_Slice
6392 and then Is_Others_Aggregate
(N
)
6394 Tmp
:= Name
(Parent
(N
));
6396 -- Set type of aggregate to be type of lhs in assignment, in order
6397 -- to suppress redundant length checks.
6399 Set_Etype
(N
, Etype
(Tmp
));
6403 -- In-place aggregate expansion is not possible
6406 Maybe_In_Place_OK
:= False;
6407 Tmp
:= Make_Temporary
(Loc
, 'A', N
);
6409 Make_Object_Declaration
(Loc
,
6410 Defining_Identifier
=> Tmp
,
6411 Object_Definition
=> New_Occurrence_Of
(Typ
, Loc
));
6412 Set_No_Initialization
(Tmp_Decl
, True);
6413 Set_Warnings_Off
(Tmp
);
6415 -- If we are within a loop, the temporary will be pushed on the
6416 -- stack at each iteration. If the aggregate is the expression
6417 -- for an allocator, it will be immediately copied to the heap
6418 -- and can be reclaimed at once. We create a transient scope
6419 -- around the aggregate for this purpose.
6421 if Ekind
(Current_Scope
) = E_Loop
6422 and then Nkind
(Parent
(Parent
(N
))) = N_Allocator
6424 Establish_Transient_Scope
(N
, Manage_Sec_Stack
=> False);
6427 Insert_Action
(N
, Tmp_Decl
);
6430 -- Construct and insert the aggregate code. We can safely suppress index
6431 -- checks because this code is guaranteed not to raise CE on index
6432 -- checks. However we should *not* suppress all checks.
6438 if Nkind
(Tmp
) = N_Defining_Identifier
then
6439 Target
:= New_Occurrence_Of
(Tmp
, Loc
);
6442 if Has_Default_Init_Comps
(N
)
6443 and then not Maybe_In_Place_OK
6445 -- Ada 2005 (AI-287): This case has not been analyzed???
6447 raise Program_Error
;
6450 -- Name in assignment is explicit dereference
6452 Target
:= New_Copy
(Tmp
);
6455 -- If we are to generate an in-place assignment for a declaration or
6456 -- an assignment statement, and the assignment can be done directly
6457 -- by the back end, then do not expand further.
6459 -- ??? We can also do that if in-place expansion is not possible but
6460 -- then we could go into an infinite recursion.
6462 if (In_Place_Assign_OK_For_Declaration
or else Maybe_In_Place_OK
)
6463 and then not CodePeer_Mode
6464 and then not Modify_Tree_For_C
6465 and then not Possible_Bit_Aligned_Component
(Target
)
6466 and then not Is_Possibly_Unaligned_Slice
(Target
)
6467 and then Aggr_Assignment_OK_For_Backend
(N
)
6469 if Maybe_In_Place_OK
then
6475 Make_Assignment_Statement
(Loc
,
6477 Expression
=> New_Copy_Tree
(N
)));
6481 Build_Array_Aggr_Code
(N
,
6483 Index
=> First_Index
(Typ
),
6485 Scalar_Comp
=> Is_Scalar_Type
(Ctyp
));
6488 -- Save the last assignment statement associated with the aggregate
6489 -- when building a controlled object. This reference is utilized by
6490 -- the finalization machinery when marking an object as successfully
6493 if Needs_Finalization
(Typ
)
6494 and then Is_Entity_Name
(Target
)
6495 and then Present
(Entity
(Target
))
6496 and then Ekind_In
(Entity
(Target
), E_Constant
, E_Variable
)
6498 Set_Last_Aggregate_Assignment
(Entity
(Target
), Last
(Aggr_Code
));
6502 -- If the aggregate is the expression in a declaration, the expanded
6503 -- code must be inserted after it. The defining entity might not come
6504 -- from source if this is part of an inlined body, but the declaration
6507 if Comes_From_Source
(Tmp
)
6509 (Nkind
(Parent
(N
)) = N_Object_Declaration
6510 and then Comes_From_Source
(Parent
(N
))
6511 and then Tmp
= Defining_Entity
(Parent
(N
)))
6514 Node_After
: constant Node_Id
:= Next
(Parent_Node
);
6517 Insert_Actions_After
(Parent_Node
, Aggr_Code
);
6519 if Parent_Kind
= N_Object_Declaration
then
6520 Collect_Initialization_Statements
6521 (Obj
=> Tmp
, N
=> Parent_Node
, Node_After
=> Node_After
);
6526 Insert_Actions
(N
, Aggr_Code
);
6529 -- If the aggregate has been assigned in place, remove the original
6532 if Nkind
(Parent
(N
)) = N_Assignment_Statement
6533 and then Maybe_In_Place_OK
6535 Rewrite
(Parent
(N
), Make_Null_Statement
(Loc
));
6537 elsif Nkind
(Parent
(N
)) /= N_Object_Declaration
6538 or else Tmp
/= Defining_Identifier
(Parent
(N
))
6540 Rewrite
(N
, New_Occurrence_Of
(Tmp
, Loc
));
6541 Analyze_And_Resolve
(N
, Typ
);
6543 end Expand_Array_Aggregate
;
6545 ------------------------
6546 -- Expand_N_Aggregate --
6547 ------------------------
6549 procedure Expand_N_Aggregate
(N
: Node_Id
) is
6551 -- Record aggregate case
6553 if Is_Record_Type
(Etype
(N
)) then
6554 Expand_Record_Aggregate
(N
);
6556 -- Array aggregate case
6559 -- A special case, if we have a string subtype with bounds 1 .. N,
6560 -- where N is known at compile time, and the aggregate is of the
6561 -- form (others => 'x'), with a single choice and no expressions,
6562 -- and N is less than 80 (an arbitrary limit for now), then replace
6563 -- the aggregate by the equivalent string literal (but do not mark
6564 -- it as static since it is not).
6566 -- Note: this entire circuit is redundant with respect to code in
6567 -- Expand_Array_Aggregate that collapses others choices to positional
6568 -- form, but there are two problems with that circuit:
6570 -- a) It is limited to very small cases due to ill-understood
6571 -- interactions with bootstrapping. That limit is removed by
6572 -- use of the No_Implicit_Loops restriction.
6574 -- b) It incorrectly ends up with the resulting expressions being
6575 -- considered static when they are not. For example, the
6576 -- following test should fail:
6578 -- pragma Restrictions (No_Implicit_Loops);
6579 -- package NonSOthers4 is
6580 -- B : constant String (1 .. 6) := (others => 'A');
6581 -- DH : constant String (1 .. 8) := B & "BB";
6583 -- pragma Export (C, X, Link_Name => DH);
6586 -- But it succeeds (DH looks static to pragma Export)
6588 -- To be sorted out ???
6590 if Present
(Component_Associations
(N
)) then
6592 CA
: constant Node_Id
:= First
(Component_Associations
(N
));
6593 MX
: constant := 80;
6596 if Nkind
(First
(Choice_List
(CA
))) = N_Others_Choice
6597 and then Nkind
(Expression
(CA
)) = N_Character_Literal
6598 and then No
(Expressions
(N
))
6601 T
: constant Entity_Id
:= Etype
(N
);
6602 X
: constant Node_Id
:= First_Index
(T
);
6603 EC
: constant Node_Id
:= Expression
(CA
);
6604 CV
: constant Uint
:= Char_Literal_Value
(EC
);
6605 CC
: constant Int
:= UI_To_Int
(CV
);
6608 if Nkind
(X
) = N_Range
6609 and then Compile_Time_Known_Value
(Low_Bound
(X
))
6610 and then Expr_Value
(Low_Bound
(X
)) = 1
6611 and then Compile_Time_Known_Value
(High_Bound
(X
))
6614 Hi
: constant Uint
:= Expr_Value
(High_Bound
(X
));
6620 for J
in 1 .. UI_To_Int
(Hi
) loop
6621 Store_String_Char
(Char_Code
(CC
));
6625 Make_String_Literal
(Sloc
(N
),
6626 Strval
=> End_String
));
6628 if CC
>= Int
(2 ** 16) then
6629 Set_Has_Wide_Wide_Character
(N
);
6630 elsif CC
>= Int
(2 ** 8) then
6631 Set_Has_Wide_Character
(N
);
6634 Analyze_And_Resolve
(N
, T
);
6635 Set_Is_Static_Expression
(N
, False);
6645 -- Not that special case, so normal expansion of array aggregate
6647 Expand_Array_Aggregate
(N
);
6651 when RE_Not_Available
=>
6653 end Expand_N_Aggregate
;
6655 ------------------------------
6656 -- Expand_N_Delta_Aggregate --
6657 ------------------------------
6659 procedure Expand_N_Delta_Aggregate
(N
: Node_Id
) is
6660 Loc
: constant Source_Ptr
:= Sloc
(N
);
6661 Typ
: constant Entity_Id
:= Etype
(N
);
6666 Make_Object_Declaration
(Loc
,
6667 Defining_Identifier
=> Make_Temporary
(Loc
, 'T'),
6668 Object_Definition
=> New_Occurrence_Of
(Typ
, Loc
),
6669 Expression
=> New_Copy_Tree
(Expression
(N
)));
6671 if Is_Array_Type
(Etype
(N
)) then
6672 Expand_Delta_Array_Aggregate
(N
, New_List
(Decl
));
6674 Expand_Delta_Record_Aggregate
(N
, New_List
(Decl
));
6676 end Expand_N_Delta_Aggregate
;
6678 ----------------------------------
6679 -- Expand_Delta_Array_Aggregate --
6680 ----------------------------------
6682 procedure Expand_Delta_Array_Aggregate
(N
: Node_Id
; Deltas
: List_Id
) is
6683 Loc
: constant Source_Ptr
:= Sloc
(N
);
6684 Temp
: constant Entity_Id
:= Defining_Identifier
(First
(Deltas
));
6687 function Generate_Loop
(C
: Node_Id
) return Node_Id
;
6688 -- Generate a loop containing individual component assignments for
6689 -- choices that are ranges, subtype indications, subtype names, and
6690 -- iterated component associations.
6696 function Generate_Loop
(C
: Node_Id
) return Node_Id
is
6697 Sl
: constant Source_Ptr
:= Sloc
(C
);
6701 if Nkind
(Parent
(C
)) = N_Iterated_Component_Association
then
6703 Make_Defining_Identifier
(Loc
,
6704 Chars
=> (Chars
(Defining_Identifier
(Parent
(C
)))));
6706 Ix
:= Make_Temporary
(Sl
, 'I');
6710 Make_Loop_Statement
(Loc
,
6712 Make_Iteration_Scheme
(Sl
,
6713 Loop_Parameter_Specification
=>
6714 Make_Loop_Parameter_Specification
(Sl
,
6715 Defining_Identifier
=> Ix
,
6716 Discrete_Subtype_Definition
=> New_Copy_Tree
(C
))),
6718 Statements
=> New_List
(
6719 Make_Assignment_Statement
(Sl
,
6721 Make_Indexed_Component
(Sl
,
6722 Prefix
=> New_Occurrence_Of
(Temp
, Sl
),
6723 Expressions
=> New_List
(New_Occurrence_Of
(Ix
, Sl
))),
6724 Expression
=> New_Copy_Tree
(Expression
(Assoc
)))),
6725 End_Label
=> Empty
);
6732 -- Start of processing for Expand_Delta_Array_Aggregate
6735 Assoc
:= First
(Component_Associations
(N
));
6736 while Present
(Assoc
) loop
6737 Choice
:= First
(Choice_List
(Assoc
));
6738 if Nkind
(Assoc
) = N_Iterated_Component_Association
then
6739 while Present
(Choice
) loop
6740 Append_To
(Deltas
, Generate_Loop
(Choice
));
6745 while Present
(Choice
) loop
6747 -- Choice can be given by a range, a subtype indication, a
6748 -- subtype name, a scalar value, or an entity.
6750 if Nkind
(Choice
) = N_Range
6751 or else (Is_Entity_Name
(Choice
)
6752 and then Is_Type
(Entity
(Choice
)))
6754 Append_To
(Deltas
, Generate_Loop
(Choice
));
6756 elsif Nkind
(Choice
) = N_Subtype_Indication
then
6758 Generate_Loop
(Range_Expression
(Constraint
(Choice
))));
6762 Make_Assignment_Statement
(Sloc
(Choice
),
6764 Make_Indexed_Component
(Sloc
(Choice
),
6765 Prefix
=> New_Occurrence_Of
(Temp
, Loc
),
6766 Expressions
=> New_List
(New_Copy_Tree
(Choice
))),
6767 Expression
=> New_Copy_Tree
(Expression
(Assoc
))));
6777 Insert_Actions
(N
, Deltas
);
6778 Rewrite
(N
, New_Occurrence_Of
(Temp
, Loc
));
6779 end Expand_Delta_Array_Aggregate
;
6781 -----------------------------------
6782 -- Expand_Delta_Record_Aggregate --
6783 -----------------------------------
6785 procedure Expand_Delta_Record_Aggregate
(N
: Node_Id
; Deltas
: List_Id
) is
6786 Loc
: constant Source_Ptr
:= Sloc
(N
);
6787 Temp
: constant Entity_Id
:= Defining_Identifier
(First
(Deltas
));
6792 Assoc
:= First
(Component_Associations
(N
));
6794 while Present
(Assoc
) loop
6795 Choice
:= First
(Choice_List
(Assoc
));
6796 while Present
(Choice
) loop
6798 Make_Assignment_Statement
(Sloc
(Choice
),
6800 Make_Selected_Component
(Sloc
(Choice
),
6801 Prefix
=> New_Occurrence_Of
(Temp
, Loc
),
6802 Selector_Name
=> Make_Identifier
(Loc
, Chars
(Choice
))),
6803 Expression
=> New_Copy_Tree
(Expression
(Assoc
))));
6810 Insert_Actions
(N
, Deltas
);
6811 Rewrite
(N
, New_Occurrence_Of
(Temp
, Loc
));
6812 end Expand_Delta_Record_Aggregate
;
6814 ----------------------------------
6815 -- Expand_N_Extension_Aggregate --
6816 ----------------------------------
6818 -- If the ancestor part is an expression, add a component association for
6819 -- the parent field. If the type of the ancestor part is not the direct
6820 -- parent of the expected type, build recursively the needed ancestors.
6821 -- If the ancestor part is a subtype_mark, replace aggregate with a
6822 -- declaration for a temporary of the expected type, followed by
6823 -- individual assignments to the given components.
6825 procedure Expand_N_Extension_Aggregate
(N
: Node_Id
) is
6826 A
: constant Node_Id
:= Ancestor_Part
(N
);
6827 Loc
: constant Source_Ptr
:= Sloc
(N
);
6828 Typ
: constant Entity_Id
:= Etype
(N
);
6831 -- If the ancestor is a subtype mark, an init proc must be called
6832 -- on the resulting object which thus has to be materialized in
6835 if Is_Entity_Name
(A
) and then Is_Type
(Entity
(A
)) then
6836 Convert_To_Assignments
(N
, Typ
);
6838 -- The extension aggregate is transformed into a record aggregate
6839 -- of the following form (c1 and c2 are inherited components)
6841 -- (Exp with c3 => a, c4 => b)
6842 -- ==> (c1 => Exp.c1, c2 => Exp.c2, c3 => a, c4 => b)
6847 if Tagged_Type_Expansion
then
6848 Expand_Record_Aggregate
(N
,
6851 (Node
(First_Elmt
(Access_Disp_Table
(Typ
))), Loc
),
6854 -- No tag is needed in the case of a VM
6857 Expand_Record_Aggregate
(N
, Parent_Expr
=> A
);
6862 when RE_Not_Available
=>
6864 end Expand_N_Extension_Aggregate
;
6866 -----------------------------
6867 -- Expand_Record_Aggregate --
6868 -----------------------------
6870 procedure Expand_Record_Aggregate
6872 Orig_Tag
: Node_Id
:= Empty
;
6873 Parent_Expr
: Node_Id
:= Empty
)
6875 Loc
: constant Source_Ptr
:= Sloc
(N
);
6876 Comps
: constant List_Id
:= Component_Associations
(N
);
6877 Typ
: constant Entity_Id
:= Etype
(N
);
6878 Base_Typ
: constant Entity_Id
:= Base_Type
(Typ
);
6880 Static_Components
: Boolean := True;
6881 -- Flag to indicate whether all components are compile-time known,
6882 -- and the aggregate can be constructed statically and handled by
6883 -- the back-end. Set to False by Component_OK_For_Backend.
6885 procedure Build_Back_End_Aggregate
;
6886 -- Build a proper aggregate to be handled by the back-end
6888 function Compile_Time_Known_Composite_Value
(N
: Node_Id
) return Boolean;
6889 -- Returns true if N is an expression of composite type which can be
6890 -- fully evaluated at compile time without raising constraint error.
6891 -- Such expressions can be passed as is to Gigi without any expansion.
6893 -- This returns true for N_Aggregate with Compile_Time_Known_Aggregate
6894 -- set and constants whose expression is such an aggregate, recursively.
6896 function Component_OK_For_Backend
return Boolean;
6897 -- Check for presence of a component which makes it impossible for the
6898 -- backend to process the aggregate, thus requiring the use of a series
6899 -- of assignment statements. Cases checked for are a nested aggregate
6900 -- needing Late_Expansion, the presence of a tagged component which may
6901 -- need tag adjustment, and a bit unaligned component reference.
6903 -- We also force expansion into assignments if a component is of a
6904 -- mutable type (including a private type with discriminants) because
6905 -- in that case the size of the component to be copied may be smaller
6906 -- than the side of the target, and there is no simple way for gigi
6907 -- to compute the size of the object to be copied.
6909 -- NOTE: This is part of the ongoing work to define precisely the
6910 -- interface between front-end and back-end handling of aggregates.
6911 -- In general it is desirable to pass aggregates as they are to gigi,
6912 -- in order to minimize elaboration code. This is one case where the
6913 -- semantics of Ada complicate the analysis and lead to anomalies in
6914 -- the gcc back-end if the aggregate is not expanded into assignments.
6916 -- NOTE: This sets the global Static_Components to False in most, but
6917 -- not all, cases when it returns False.
6919 function Has_Per_Object_Constraint
(L
: List_Id
) return Boolean;
6920 -- Return True if any element of L has Has_Per_Object_Constraint set.
6921 -- L should be the Choices component of an N_Component_Association.
6923 function Has_Visible_Private_Ancestor
(Id
: E
) return Boolean;
6924 -- If any ancestor of the current type is private, the aggregate
6925 -- cannot be built in place. We cannot rely on Has_Private_Ancestor,
6926 -- because it will not be set when type and its parent are in the
6927 -- same scope, and the parent component needs expansion.
6929 function Top_Level_Aggregate
(N
: Node_Id
) return Node_Id
;
6930 -- For nested aggregates return the ultimate enclosing aggregate; for
6931 -- non-nested aggregates return N.
6933 ------------------------------
6934 -- Build_Back_End_Aggregate --
6935 ------------------------------
6937 procedure Build_Back_End_Aggregate
is
6940 Tag_Value
: Node_Id
;
6943 if Nkind
(N
) = N_Aggregate
then
6945 -- If the aggregate is static and can be handled by the back-end,
6946 -- nothing left to do.
6948 if Static_Components
then
6949 Set_Compile_Time_Known_Aggregate
(N
);
6950 Set_Expansion_Delayed
(N
, False);
6954 -- If no discriminants, nothing special to do
6956 if not Has_Discriminants
(Typ
) then
6959 -- Case of discriminants present
6961 elsif Is_Derived_Type
(Typ
) then
6963 -- For untagged types, non-stored discriminants are replaced with
6964 -- stored discriminants, which are the ones that gigi uses to
6965 -- describe the type and its components.
6967 Generate_Aggregate_For_Derived_Type
: declare
6968 procedure Prepend_Stored_Values
(T
: Entity_Id
);
6969 -- Scan the list of stored discriminants of the type, and add
6970 -- their values to the aggregate being built.
6972 ---------------------------
6973 -- Prepend_Stored_Values --
6974 ---------------------------
6976 procedure Prepend_Stored_Values
(T
: Entity_Id
) is
6978 First_Comp
: Node_Id
:= Empty
;
6981 Discr
:= First_Stored_Discriminant
(T
);
6982 while Present
(Discr
) loop
6984 Make_Component_Association
(Loc
,
6985 Choices
=> New_List
(
6986 New_Occurrence_Of
(Discr
, Loc
)),
6989 (Get_Discriminant_Value
6992 Discriminant_Constraint
(Typ
))));
6994 if No
(First_Comp
) then
6995 Prepend_To
(Component_Associations
(N
), New_Comp
);
6997 Insert_After
(First_Comp
, New_Comp
);
7000 First_Comp
:= New_Comp
;
7001 Next_Stored_Discriminant
(Discr
);
7003 end Prepend_Stored_Values
;
7007 Constraints
: constant List_Id
:= New_List
;
7011 Num_Disc
: Nat
:= 0;
7012 Num_Gird
: Nat
:= 0;
7014 -- Start of processing for Generate_Aggregate_For_Derived_Type
7017 -- Remove the associations for the discriminant of derived type
7020 First_Comp
: Node_Id
;
7023 First_Comp
:= First
(Component_Associations
(N
));
7024 while Present
(First_Comp
) loop
7028 if Ekind
(Entity
(First
(Choices
(Comp
)))) =
7032 Num_Disc
:= Num_Disc
+ 1;
7037 -- Insert stored discriminant associations in the correct
7038 -- order. If there are more stored discriminants than new
7039 -- discriminants, there is at least one new discriminant that
7040 -- constrains more than one of the stored discriminants. In
7041 -- this case we need to construct a proper subtype of the
7042 -- parent type, in order to supply values to all the
7043 -- components. Otherwise there is one-one correspondence
7044 -- between the constraints and the stored discriminants.
7046 Discr
:= First_Stored_Discriminant
(Base_Type
(Typ
));
7047 while Present
(Discr
) loop
7048 Num_Gird
:= Num_Gird
+ 1;
7049 Next_Stored_Discriminant
(Discr
);
7052 -- Case of more stored discriminants than new discriminants
7054 if Num_Gird
> Num_Disc
then
7056 -- Create a proper subtype of the parent type, which is the
7057 -- proper implementation type for the aggregate, and convert
7058 -- it to the intended target type.
7060 Discr
:= First_Stored_Discriminant
(Base_Type
(Typ
));
7061 while Present
(Discr
) loop
7064 (Get_Discriminant_Value
7067 Discriminant_Constraint
(Typ
)));
7069 Append
(New_Comp
, Constraints
);
7070 Next_Stored_Discriminant
(Discr
);
7074 Make_Subtype_Declaration
(Loc
,
7075 Defining_Identifier
=> Make_Temporary
(Loc
, 'T'),
7076 Subtype_Indication
=>
7077 Make_Subtype_Indication
(Loc
,
7079 New_Occurrence_Of
(Etype
(Base_Type
(Typ
)), Loc
),
7081 Make_Index_Or_Discriminant_Constraint
7082 (Loc
, Constraints
)));
7084 Insert_Action
(N
, Decl
);
7085 Prepend_Stored_Values
(Base_Type
(Typ
));
7087 Set_Etype
(N
, Defining_Identifier
(Decl
));
7090 Rewrite
(N
, Unchecked_Convert_To
(Typ
, N
));
7093 -- Case where we do not have fewer new discriminants than
7094 -- stored discriminants, so in this case we can simply use the
7095 -- stored discriminants of the subtype.
7098 Prepend_Stored_Values
(Typ
);
7100 end Generate_Aggregate_For_Derived_Type
;
7103 if Is_Tagged_Type
(Typ
) then
7105 -- In the tagged case, _parent and _tag component must be created
7107 -- Reset Null_Present unconditionally. Tagged records always have
7108 -- at least one field (the tag or the parent).
7110 Set_Null_Record_Present
(N
, False);
7112 -- When the current aggregate comes from the expansion of an
7113 -- extension aggregate, the parent expr is replaced by an
7114 -- aggregate formed by selected components of this expr.
7116 if Present
(Parent_Expr
) and then Is_Empty_List
(Comps
) then
7117 Comp
:= First_Component_Or_Discriminant
(Typ
);
7118 while Present
(Comp
) loop
7120 -- Skip all expander-generated components
7122 if not Comes_From_Source
(Original_Record_Component
(Comp
))
7128 Make_Selected_Component
(Loc
,
7130 Unchecked_Convert_To
(Typ
,
7131 Duplicate_Subexpr
(Parent_Expr
, True)),
7132 Selector_Name
=> New_Occurrence_Of
(Comp
, Loc
));
7135 Make_Component_Association
(Loc
,
7136 Choices
=> New_List
(
7137 New_Occurrence_Of
(Comp
, Loc
)),
7138 Expression
=> New_Comp
));
7140 Analyze_And_Resolve
(New_Comp
, Etype
(Comp
));
7143 Next_Component_Or_Discriminant
(Comp
);
7147 -- Compute the value for the Tag now, if the type is a root it
7148 -- will be included in the aggregate right away, otherwise it will
7149 -- be propagated to the parent aggregate.
7151 if Present
(Orig_Tag
) then
7152 Tag_Value
:= Orig_Tag
;
7154 elsif not Tagged_Type_Expansion
then
7160 (Node
(First_Elmt
(Access_Disp_Table
(Typ
))), Loc
);
7163 -- For a derived type, an aggregate for the parent is formed with
7164 -- all the inherited components.
7166 if Is_Derived_Type
(Typ
) then
7168 First_Comp
: Node_Id
;
7169 Parent_Comps
: List_Id
;
7170 Parent_Aggr
: Node_Id
;
7171 Parent_Name
: Node_Id
;
7174 -- Remove the inherited component association from the
7175 -- aggregate and store them in the parent aggregate
7177 First_Comp
:= First
(Component_Associations
(N
));
7178 Parent_Comps
:= New_List
;
7179 while Present
(First_Comp
)
7181 Scope
(Original_Record_Component
7182 (Entity
(First
(Choices
(First_Comp
))))) /=
7188 Append
(Comp
, Parent_Comps
);
7192 Make_Aggregate
(Loc
,
7193 Component_Associations
=> Parent_Comps
);
7194 Set_Etype
(Parent_Aggr
, Etype
(Base_Type
(Typ
)));
7196 -- Find the _parent component
7198 Comp
:= First_Component
(Typ
);
7199 while Chars
(Comp
) /= Name_uParent
loop
7200 Comp
:= Next_Component
(Comp
);
7203 Parent_Name
:= New_Occurrence_Of
(Comp
, Loc
);
7205 -- Insert the parent aggregate
7207 Prepend_To
(Component_Associations
(N
),
7208 Make_Component_Association
(Loc
,
7209 Choices
=> New_List
(Parent_Name
),
7210 Expression
=> Parent_Aggr
));
7212 -- Expand recursively the parent propagating the right Tag
7214 Expand_Record_Aggregate
7215 (Parent_Aggr
, Tag_Value
, Parent_Expr
);
7217 -- The ancestor part may be a nested aggregate that has
7218 -- delayed expansion: recheck now.
7220 if not Component_OK_For_Backend
then
7221 Convert_To_Assignments
(N
, Typ
);
7225 -- For a root type, the tag component is added (unless compiling
7226 -- for the VMs, where tags are implicit).
7228 elsif Tagged_Type_Expansion
then
7230 Tag_Name
: constant Node_Id
:=
7232 (First_Tag_Component
(Typ
), Loc
);
7233 Typ_Tag
: constant Entity_Id
:= RTE
(RE_Tag
);
7234 Conv_Node
: constant Node_Id
:=
7235 Unchecked_Convert_To
(Typ_Tag
, Tag_Value
);
7238 Set_Etype
(Conv_Node
, Typ_Tag
);
7239 Prepend_To
(Component_Associations
(N
),
7240 Make_Component_Association
(Loc
,
7241 Choices
=> New_List
(Tag_Name
),
7242 Expression
=> Conv_Node
));
7246 end Build_Back_End_Aggregate
;
7248 ----------------------------------------
7249 -- Compile_Time_Known_Composite_Value --
7250 ----------------------------------------
7252 function Compile_Time_Known_Composite_Value
7253 (N
: Node_Id
) return Boolean
7256 -- If we have an entity name, then see if it is the name of a
7257 -- constant and if so, test the corresponding constant value.
7259 if Is_Entity_Name
(N
) then
7261 E
: constant Entity_Id
:= Entity
(N
);
7264 if Ekind
(E
) /= E_Constant
then
7267 V
:= Constant_Value
(E
);
7269 and then Compile_Time_Known_Composite_Value
(V
);
7273 -- We have a value, see if it is compile time known
7276 if Nkind
(N
) = N_Aggregate
then
7277 return Compile_Time_Known_Aggregate
(N
);
7280 -- All other types of values are not known at compile time
7285 end Compile_Time_Known_Composite_Value
;
7287 ------------------------------
7288 -- Component_OK_For_Backend --
7289 ------------------------------
7291 function Component_OK_For_Backend
return Boolean is
7301 while Present
(C
) loop
7303 -- If the component has box initialization, expansion is needed
7304 -- and component is not ready for backend.
7306 if Box_Present
(C
) then
7310 if Nkind
(Expression
(C
)) = N_Qualified_Expression
then
7311 Expr_Q
:= Expression
(Expression
(C
));
7313 Expr_Q
:= Expression
(C
);
7316 -- Return False for array components whose bounds raise
7317 -- constraint error.
7320 Comp
: constant Entity_Id
:= First
(Choices
(C
));
7324 if Present
(Etype
(Comp
))
7325 and then Is_Array_Type
(Etype
(Comp
))
7327 Indx
:= First_Index
(Etype
(Comp
));
7328 while Present
(Indx
) loop
7329 if Nkind
(Type_Low_Bound
(Etype
(Indx
))) =
7330 N_Raise_Constraint_Error
7331 or else Nkind
(Type_High_Bound
(Etype
(Indx
))) =
7332 N_Raise_Constraint_Error
7337 Indx
:= Next_Index
(Indx
);
7342 -- Return False if the aggregate has any associations for tagged
7343 -- components that may require tag adjustment.
7345 -- These are cases where the source expression may have a tag that
7346 -- could differ from the component tag (e.g., can occur for type
7347 -- conversions and formal parameters). (Tag adjustment not needed
7348 -- if Tagged_Type_Expansion because object tags are implicit in
7351 if Is_Tagged_Type
(Etype
(Expr_Q
))
7353 (Nkind
(Expr_Q
) = N_Type_Conversion
7355 (Is_Entity_Name
(Expr_Q
)
7356 and then Is_Formal
(Entity
(Expr_Q
))))
7357 and then Tagged_Type_Expansion
7359 Static_Components
:= False;
7362 elsif Is_Delayed_Aggregate
(Expr_Q
) then
7363 Static_Components
:= False;
7366 elsif Nkind
(Expr_Q
) = N_Quantified_Expression
then
7367 Static_Components
:= False;
7370 elsif Possible_Bit_Aligned_Component
(Expr_Q
) then
7371 Static_Components
:= False;
7374 elsif Modify_Tree_For_C
7375 and then Nkind
(C
) = N_Component_Association
7376 and then Has_Per_Object_Constraint
(Choices
(C
))
7378 Static_Components
:= False;
7381 elsif Modify_Tree_For_C
7382 and then Nkind
(Expr_Q
) = N_Identifier
7383 and then Is_Array_Type
(Etype
(Expr_Q
))
7385 Static_Components
:= False;
7388 elsif Modify_Tree_For_C
7389 and then Nkind
(Expr_Q
) = N_Type_Conversion
7390 and then Is_Array_Type
(Etype
(Expr_Q
))
7392 Static_Components
:= False;
7396 if Is_Elementary_Type
(Etype
(Expr_Q
)) then
7397 if not Compile_Time_Known_Value
(Expr_Q
) then
7398 Static_Components
:= False;
7401 elsif not Compile_Time_Known_Composite_Value
(Expr_Q
) then
7402 Static_Components
:= False;
7404 if Is_Private_Type
(Etype
(Expr_Q
))
7405 and then Has_Discriminants
(Etype
(Expr_Q
))
7415 end Component_OK_For_Backend
;
7417 -------------------------------
7418 -- Has_Per_Object_Constraint --
7419 -------------------------------
7421 function Has_Per_Object_Constraint
(L
: List_Id
) return Boolean is
7422 N
: Node_Id
:= First
(L
);
7424 while Present
(N
) loop
7425 if Is_Entity_Name
(N
)
7426 and then Present
(Entity
(N
))
7427 and then Has_Per_Object_Constraint
(Entity
(N
))
7436 end Has_Per_Object_Constraint
;
7438 -----------------------------------
7439 -- Has_Visible_Private_Ancestor --
7440 -----------------------------------
7442 function Has_Visible_Private_Ancestor
(Id
: E
) return Boolean is
7443 R
: constant Entity_Id
:= Root_Type
(Id
);
7444 T1
: Entity_Id
:= Id
;
7448 if Is_Private_Type
(T1
) then
7458 end Has_Visible_Private_Ancestor
;
7460 -------------------------
7461 -- Top_Level_Aggregate --
7462 -------------------------
7464 function Top_Level_Aggregate
(N
: Node_Id
) return Node_Id
is
7469 while Present
(Parent
(Aggr
))
7470 and then Nkind_In
(Parent
(Aggr
), N_Aggregate
,
7471 N_Component_Association
)
7473 Aggr
:= Parent
(Aggr
);
7477 end Top_Level_Aggregate
;
7481 Top_Level_Aggr
: constant Node_Id
:= Top_Level_Aggregate
(N
);
7483 -- Start of processing for Expand_Record_Aggregate
7486 -- If the aggregate is to be assigned to an atomic/VFA variable, we have
7487 -- to prevent a piecemeal assignment even if the aggregate is to be
7488 -- expanded. We create a temporary for the aggregate, and assign the
7489 -- temporary instead, so that the back end can generate an atomic move
7492 if Is_Atomic_VFA_Aggregate
(N
) then
7495 -- No special management required for aggregates used to initialize
7496 -- statically allocated dispatch tables
7498 elsif Is_Static_Dispatch_Table_Aggregate
(N
) then
7502 -- If the pragma Aggregate_Individually_Assign is set, always convert to
7505 if Aggregate_Individually_Assign
then
7506 Convert_To_Assignments
(N
, Typ
);
7508 -- Ada 2005 (AI-318-2): We need to convert to assignments if components
7509 -- are build-in-place function calls. The assignments will each turn
7510 -- into a build-in-place function call. If components are all static,
7511 -- we can pass the aggregate to the back end regardless of limitedness.
7513 -- Extension aggregates, aggregates in extended return statements, and
7514 -- aggregates for C++ imported types must be expanded.
7516 elsif Ada_Version
>= Ada_2005
and then Is_Limited_View
(Typ
) then
7517 if not Nkind_In
(Parent
(N
), N_Component_Association
,
7518 N_Object_Declaration
)
7520 Convert_To_Assignments
(N
, Typ
);
7522 elsif Nkind
(N
) = N_Extension_Aggregate
7523 or else Convention
(Typ
) = Convention_CPP
7525 Convert_To_Assignments
(N
, Typ
);
7527 elsif not Size_Known_At_Compile_Time
(Typ
)
7528 or else not Component_OK_For_Backend
7529 or else not Static_Components
7531 Convert_To_Assignments
(N
, Typ
);
7533 -- In all other cases, build a proper aggregate to be handled by
7537 Build_Back_End_Aggregate
;
7540 -- Gigi doesn't properly handle temporaries of variable size so we
7541 -- generate it in the front-end
7543 elsif not Size_Known_At_Compile_Time
(Typ
)
7544 and then Tagged_Type_Expansion
7546 Convert_To_Assignments
(N
, Typ
);
7548 -- An aggregate used to initialize a controlled object must be turned
7549 -- into component assignments as the components themselves may require
7550 -- finalization actions such as adjustment.
7552 elsif Needs_Finalization
(Typ
) then
7553 Convert_To_Assignments
(N
, Typ
);
7555 -- Ada 2005 (AI-287): In case of default initialized components we
7556 -- convert the aggregate into assignments.
7558 elsif Has_Default_Init_Comps
(N
) then
7559 Convert_To_Assignments
(N
, Typ
);
7563 elsif not Component_OK_For_Backend
then
7564 Convert_To_Assignments
(N
, Typ
);
7566 -- If an ancestor is private, some components are not inherited and we
7567 -- cannot expand into a record aggregate.
7569 elsif Has_Visible_Private_Ancestor
(Typ
) then
7570 Convert_To_Assignments
(N
, Typ
);
7572 -- ??? The following was done to compile fxacc00.ads in the ACVCs. Gigi
7573 -- is not able to handle the aggregate for Late_Request.
7575 elsif Is_Tagged_Type
(Typ
) and then Has_Discriminants
(Typ
) then
7576 Convert_To_Assignments
(N
, Typ
);
7578 -- If the tagged types covers interface types we need to initialize all
7579 -- hidden components containing pointers to secondary dispatch tables.
7581 elsif Is_Tagged_Type
(Typ
) and then Has_Interfaces
(Typ
) then
7582 Convert_To_Assignments
(N
, Typ
);
7584 -- If some components are mutable, the size of the aggregate component
7585 -- may be distinct from the default size of the type component, so
7586 -- we need to expand to insure that the back-end copies the proper
7587 -- size of the data. However, if the aggregate is the initial value of
7588 -- a constant, the target is immutable and might be built statically
7589 -- if components are appropriate.
7591 elsif Has_Mutable_Components
(Typ
)
7593 (Nkind
(Parent
(Top_Level_Aggr
)) /= N_Object_Declaration
7594 or else not Constant_Present
(Parent
(Top_Level_Aggr
))
7595 or else not Static_Components
)
7597 Convert_To_Assignments
(N
, Typ
);
7599 -- If the type involved has bit aligned components, then we are not sure
7600 -- that the back end can handle this case correctly.
7602 elsif Type_May_Have_Bit_Aligned_Components
(Typ
) then
7603 Convert_To_Assignments
(N
, Typ
);
7605 -- When generating C, only generate an aggregate when declaring objects
7606 -- since C does not support aggregates in e.g. assignment statements.
7608 elsif Modify_Tree_For_C
and then not Is_CCG_Supported_Aggregate
(N
) then
7609 Convert_To_Assignments
(N
, Typ
);
7611 -- In all other cases, build a proper aggregate to be handled by gigi
7614 Build_Back_End_Aggregate
;
7616 end Expand_Record_Aggregate
;
7618 ----------------------------
7619 -- Has_Default_Init_Comps --
7620 ----------------------------
7622 function Has_Default_Init_Comps
(N
: Node_Id
) return Boolean is
7623 Comps
: constant List_Id
:= Component_Associations
(N
);
7628 pragma Assert
(Nkind_In
(N
, N_Aggregate
, N_Extension_Aggregate
));
7634 if Has_Self_Reference
(N
) then
7638 -- Check if any direct component has default initialized components
7641 while Present
(C
) loop
7642 if Box_Present
(C
) then
7649 -- Recursive call in case of aggregate expression
7652 while Present
(C
) loop
7653 Expr
:= Expression
(C
);
7656 and then Nkind_In
(Expr
, N_Aggregate
, N_Extension_Aggregate
)
7657 and then Has_Default_Init_Comps
(Expr
)
7666 end Has_Default_Init_Comps
;
7668 ----------------------------------------
7669 -- Is_Build_In_Place_Aggregate_Return --
7670 ----------------------------------------
7672 function Is_Build_In_Place_Aggregate_Return
(N
: Node_Id
) return Boolean is
7673 P
: Node_Id
:= Parent
(N
);
7676 while Nkind
(P
) = N_Qualified_Expression
loop
7680 if Nkind
(P
) = N_Simple_Return_Statement
then
7683 elsif Nkind
(Parent
(P
)) = N_Extended_Return_Statement
then
7691 Is_Build_In_Place_Function
7692 (Return_Applies_To
(Return_Statement_Entity
(P
)));
7693 end Is_Build_In_Place_Aggregate_Return
;
7695 --------------------------
7696 -- Is_Delayed_Aggregate --
7697 --------------------------
7699 function Is_Delayed_Aggregate
(N
: Node_Id
) return Boolean is
7700 Node
: Node_Id
:= N
;
7701 Kind
: Node_Kind
:= Nkind
(Node
);
7704 if Kind
= N_Qualified_Expression
then
7705 Node
:= Expression
(Node
);
7706 Kind
:= Nkind
(Node
);
7709 if not Nkind_In
(Kind
, N_Aggregate
, N_Extension_Aggregate
) then
7712 return Expansion_Delayed
(Node
);
7714 end Is_Delayed_Aggregate
;
7716 --------------------------------
7717 -- Is_CCG_Supported_Aggregate --
7718 --------------------------------
7720 function Is_CCG_Supported_Aggregate
7721 (N
: Node_Id
) return Boolean
7723 P
: Node_Id
:= Parent
(N
);
7726 -- Aggregates are not supported for nonstandard rep clauses, since they
7727 -- may lead to extra padding fields in CCG.
7729 if Ekind
(Etype
(N
)) in Record_Kind
7730 and then Has_Non_Standard_Rep
(Etype
(N
))
7735 while Present
(P
) and then Nkind
(P
) = N_Aggregate
loop
7739 -- Check cases where aggregates are supported by the CCG backend
7741 if Nkind
(P
) = N_Object_Declaration
then
7743 P_Typ
: constant Entity_Id
:= Etype
(Defining_Identifier
(P
));
7746 if Is_Record_Type
(P_Typ
) then
7749 return Compile_Time_Known_Bounds
(P_Typ
);
7753 elsif Nkind
(P
) = N_Qualified_Expression
then
7754 if Nkind
(Parent
(P
)) = N_Object_Declaration
then
7756 P_Typ
: constant Entity_Id
:=
7757 Etype
(Defining_Identifier
(Parent
(P
)));
7759 if Is_Record_Type
(P_Typ
) then
7762 return Compile_Time_Known_Bounds
(P_Typ
);
7766 elsif Nkind
(Parent
(P
)) = N_Allocator
then
7772 end Is_CCG_Supported_Aggregate
;
7774 ----------------------------------------
7775 -- Is_Static_Dispatch_Table_Aggregate --
7776 ----------------------------------------
7778 function Is_Static_Dispatch_Table_Aggregate
(N
: Node_Id
) return Boolean is
7779 Typ
: constant Entity_Id
:= Base_Type
(Etype
(N
));
7782 return Building_Static_Dispatch_Tables
7783 and then Tagged_Type_Expansion
7784 and then RTU_Loaded
(Ada_Tags
)
7786 -- Avoid circularity when rebuilding the compiler
7788 and then Cunit_Entity
(Get_Source_Unit
(N
)) /= RTU_Entity
(Ada_Tags
)
7789 and then (Typ
= RTE
(RE_Dispatch_Table_Wrapper
)
7791 Typ
= RTE
(RE_Address_Array
)
7793 Typ
= RTE
(RE_Type_Specific_Data
)
7795 Typ
= RTE
(RE_Tag_Table
)
7797 (RTE_Available
(RE_Interface_Data
)
7798 and then Typ
= RTE
(RE_Interface_Data
))
7800 (RTE_Available
(RE_Interfaces_Array
)
7801 and then Typ
= RTE
(RE_Interfaces_Array
))
7803 (RTE_Available
(RE_Interface_Data_Element
)
7804 and then Typ
= RTE
(RE_Interface_Data_Element
)));
7805 end Is_Static_Dispatch_Table_Aggregate
;
7807 -----------------------------
7808 -- Is_Two_Dim_Packed_Array --
7809 -----------------------------
7811 function Is_Two_Dim_Packed_Array
(Typ
: Entity_Id
) return Boolean is
7812 C
: constant Int
:= UI_To_Int
(Component_Size
(Typ
));
7814 return Number_Dimensions
(Typ
) = 2
7815 and then Is_Bit_Packed_Array
(Typ
)
7816 and then (C
= 1 or else C
= 2 or else C
= 4);
7817 end Is_Two_Dim_Packed_Array
;
7819 --------------------
7820 -- Late_Expansion --
7821 --------------------
7823 function Late_Expansion
7826 Target
: Node_Id
) return List_Id
7828 Aggr_Code
: List_Id
;
7831 if Is_Array_Type
(Etype
(N
)) then
7833 Build_Array_Aggr_Code
7835 Ctype
=> Component_Type
(Etype
(N
)),
7836 Index
=> First_Index
(Typ
),
7838 Scalar_Comp
=> Is_Scalar_Type
(Component_Type
(Typ
)),
7839 Indexes
=> No_List
);
7841 -- Directly or indirectly (e.g. access protected procedure) a record
7844 Aggr_Code
:= Build_Record_Aggr_Code
(N
, Typ
, Target
);
7847 -- Save the last assignment statement associated with the aggregate
7848 -- when building a controlled object. This reference is utilized by
7849 -- the finalization machinery when marking an object as successfully
7852 if Needs_Finalization
(Typ
)
7853 and then Is_Entity_Name
(Target
)
7854 and then Present
(Entity
(Target
))
7855 and then Ekind_In
(Entity
(Target
), E_Constant
, E_Variable
)
7857 Set_Last_Aggregate_Assignment
(Entity
(Target
), Last
(Aggr_Code
));
7863 ----------------------------------
7864 -- Make_OK_Assignment_Statement --
7865 ----------------------------------
7867 function Make_OK_Assignment_Statement
7870 Expression
: Node_Id
) return Node_Id
7873 Set_Assignment_OK
(Name
);
7874 return Make_Assignment_Statement
(Sloc
, Name
, Expression
);
7875 end Make_OK_Assignment_Statement
;
7877 -----------------------
7878 -- Number_Of_Choices --
7879 -----------------------
7881 function Number_Of_Choices
(N
: Node_Id
) return Nat
is
7885 Nb_Choices
: Nat
:= 0;
7888 if Present
(Expressions
(N
)) then
7892 Assoc
:= First
(Component_Associations
(N
));
7893 while Present
(Assoc
) loop
7894 Choice
:= First
(Choice_List
(Assoc
));
7895 while Present
(Choice
) loop
7896 if Nkind
(Choice
) /= N_Others_Choice
then
7897 Nb_Choices
:= Nb_Choices
+ 1;
7907 end Number_Of_Choices
;
7909 ------------------------------------
7910 -- Packed_Array_Aggregate_Handled --
7911 ------------------------------------
7913 -- The current version of this procedure will handle at compile time
7914 -- any array aggregate that meets these conditions:
7916 -- One and two dimensional, bit packed
7917 -- Underlying packed type is modular type
7918 -- Bounds are within 32-bit Int range
7919 -- All bounds and values are static
7921 -- Note: for now, in the 2-D case, we only handle component sizes of
7922 -- 1, 2, 4 (cases where an integral number of elements occupies a byte).
7924 function Packed_Array_Aggregate_Handled
(N
: Node_Id
) return Boolean is
7925 Loc
: constant Source_Ptr
:= Sloc
(N
);
7926 Typ
: constant Entity_Id
:= Etype
(N
);
7927 Ctyp
: constant Entity_Id
:= Component_Type
(Typ
);
7929 Not_Handled
: exception;
7930 -- Exception raised if this aggregate cannot be handled
7933 -- Handle one- or two dimensional bit packed array
7935 if not Is_Bit_Packed_Array
(Typ
)
7936 or else Number_Dimensions
(Typ
) > 2
7941 -- If two-dimensional, check whether it can be folded, and transformed
7942 -- into a one-dimensional aggregate for the Packed_Array_Impl_Type of
7943 -- the original type.
7945 if Number_Dimensions
(Typ
) = 2 then
7946 return Two_Dim_Packed_Array_Handled
(N
);
7949 if not Is_Modular_Integer_Type
(Packed_Array_Impl_Type
(Typ
)) then
7953 if not Is_Scalar_Type
(Ctyp
) then
7958 Csiz
: constant Nat
:= UI_To_Int
(Component_Size
(Typ
));
7962 -- Bounds of index type
7966 -- Values of bounds if compile time known
7968 function Get_Component_Val
(N
: Node_Id
) return Uint
;
7969 -- Given a expression value N of the component type Ctyp, returns a
7970 -- value of Csiz (component size) bits representing this value. If
7971 -- the value is nonstatic or any other reason exists why the value
7972 -- cannot be returned, then Not_Handled is raised.
7974 -----------------------
7975 -- Get_Component_Val --
7976 -----------------------
7978 function Get_Component_Val
(N
: Node_Id
) return Uint
is
7982 -- We have to analyze the expression here before doing any further
7983 -- processing here. The analysis of such expressions is deferred
7984 -- till expansion to prevent some problems of premature analysis.
7986 Analyze_And_Resolve
(N
, Ctyp
);
7988 -- Must have a compile time value. String literals have to be
7989 -- converted into temporaries as well, because they cannot easily
7990 -- be converted into their bit representation.
7992 if not Compile_Time_Known_Value
(N
)
7993 or else Nkind
(N
) = N_String_Literal
7998 Val
:= Expr_Rep_Value
(N
);
8000 -- Adjust for bias, and strip proper number of bits
8002 if Has_Biased_Representation
(Ctyp
) then
8003 Val
:= Val
- Expr_Value
(Type_Low_Bound
(Ctyp
));
8006 return Val
mod Uint_2
** Csiz
;
8007 end Get_Component_Val
;
8009 -- Here we know we have a one dimensional bit packed array
8012 Get_Index_Bounds
(First_Index
(Typ
), Lo
, Hi
);
8014 -- Cannot do anything if bounds are dynamic
8016 if not Compile_Time_Known_Value
(Lo
)
8018 not Compile_Time_Known_Value
(Hi
)
8023 -- Or are silly out of range of int bounds
8025 Lob
:= Expr_Value
(Lo
);
8026 Hib
:= Expr_Value
(Hi
);
8028 if not UI_Is_In_Int_Range
(Lob
)
8030 not UI_Is_In_Int_Range
(Hib
)
8035 -- At this stage we have a suitable aggregate for handling at compile
8036 -- time. The only remaining checks are that the values of expressions
8037 -- in the aggregate are compile-time known (checks are performed by
8038 -- Get_Component_Val), and that any subtypes or ranges are statically
8041 -- If the aggregate is not fully positional at this stage, then
8042 -- convert it to positional form. Either this will fail, in which
8043 -- case we can do nothing, or it will succeed, in which case we have
8044 -- succeeded in handling the aggregate and transforming it into a
8045 -- modular value, or it will stay an aggregate, in which case we
8046 -- have failed to create a packed value for it.
8048 if Present
(Component_Associations
(N
)) then
8049 Convert_To_Positional
8050 (N
, Max_Others_Replicate
=> 64, Handle_Bit_Packed
=> True);
8051 return Nkind
(N
) /= N_Aggregate
;
8054 -- Otherwise we are all positional, so convert to proper value
8057 Lov
: constant Int
:= UI_To_Int
(Lob
);
8058 Hiv
: constant Int
:= UI_To_Int
(Hib
);
8060 Len
: constant Nat
:= Int
'Max (0, Hiv
- Lov
+ 1);
8061 -- The length of the array (number of elements)
8063 Aggregate_Val
: Uint
;
8064 -- Value of aggregate. The value is set in the low order bits of
8065 -- this value. For the little-endian case, the values are stored
8066 -- from low-order to high-order and for the big-endian case the
8067 -- values are stored from high-order to low-order. Note that gigi
8068 -- will take care of the conversions to left justify the value in
8069 -- the big endian case (because of left justified modular type
8070 -- processing), so we do not have to worry about that here.
8073 -- Integer literal for resulting constructed value
8076 -- Shift count from low order for next value
8079 -- Shift increment for loop
8082 -- Next expression from positional parameters of aggregate
8084 Left_Justified
: Boolean;
8085 -- Set True if we are filling the high order bits of the target
8086 -- value (i.e. the value is left justified).
8089 -- For little endian, we fill up the low order bits of the target
8090 -- value. For big endian we fill up the high order bits of the
8091 -- target value (which is a left justified modular value).
8093 Left_Justified
:= Bytes_Big_Endian
;
8095 -- Switch justification if using -gnatd8
8097 if Debug_Flag_8
then
8098 Left_Justified
:= not Left_Justified
;
8101 -- Switch justfification if reverse storage order
8103 if Reverse_Storage_Order
(Base_Type
(Typ
)) then
8104 Left_Justified
:= not Left_Justified
;
8107 if Left_Justified
then
8108 Shift
:= Csiz
* (Len
- 1);
8115 -- Loop to set the values
8118 Aggregate_Val
:= Uint_0
;
8120 Expr
:= First
(Expressions
(N
));
8121 Aggregate_Val
:= Get_Component_Val
(Expr
) * Uint_2
** Shift
;
8123 for J
in 2 .. Len
loop
8124 Shift
:= Shift
+ Incr
;
8127 Aggregate_Val
+ Get_Component_Val
(Expr
) * Uint_2
** Shift
;
8131 -- Now we can rewrite with the proper value
8133 Lit
:= Make_Integer_Literal
(Loc
, Intval
=> Aggregate_Val
);
8134 Set_Print_In_Hex
(Lit
);
8136 -- Construct the expression using this literal. Note that it is
8137 -- important to qualify the literal with its proper modular type
8138 -- since universal integer does not have the required range and
8139 -- also this is a left justified modular type, which is important
8140 -- in the big-endian case.
8143 Unchecked_Convert_To
(Typ
,
8144 Make_Qualified_Expression
(Loc
,
8146 New_Occurrence_Of
(Packed_Array_Impl_Type
(Typ
), Loc
),
8147 Expression
=> Lit
)));
8149 Analyze_And_Resolve
(N
, Typ
);
8157 end Packed_Array_Aggregate_Handled
;
8159 ----------------------------
8160 -- Has_Mutable_Components --
8161 ----------------------------
8163 function Has_Mutable_Components
(Typ
: Entity_Id
) return Boolean is
8168 Comp
:= First_Component
(Typ
);
8169 while Present
(Comp
) loop
8170 Ctyp
:= Underlying_Type
(Etype
(Comp
));
8171 if Is_Record_Type
(Ctyp
)
8172 and then Has_Discriminants
(Ctyp
)
8173 and then not Is_Constrained
(Ctyp
)
8178 Next_Component
(Comp
);
8182 end Has_Mutable_Components
;
8184 ------------------------------
8185 -- Initialize_Discriminants --
8186 ------------------------------
8188 procedure Initialize_Discriminants
(N
: Node_Id
; Typ
: Entity_Id
) is
8189 Loc
: constant Source_Ptr
:= Sloc
(N
);
8190 Bas
: constant Entity_Id
:= Base_Type
(Typ
);
8191 Par
: constant Entity_Id
:= Etype
(Bas
);
8192 Decl
: constant Node_Id
:= Parent
(Par
);
8196 if Is_Tagged_Type
(Bas
)
8197 and then Is_Derived_Type
(Bas
)
8198 and then Has_Discriminants
(Par
)
8199 and then Has_Discriminants
(Bas
)
8200 and then Number_Discriminants
(Bas
) /= Number_Discriminants
(Par
)
8201 and then Nkind
(Decl
) = N_Full_Type_Declaration
8202 and then Nkind
(Type_Definition
(Decl
)) = N_Record_Definition
8204 Present
(Variant_Part
(Component_List
(Type_Definition
(Decl
))))
8205 and then Nkind
(N
) /= N_Extension_Aggregate
8208 -- Call init proc to set discriminants.
8209 -- There should eventually be a special procedure for this ???
8211 Ref
:= New_Occurrence_Of
(Defining_Identifier
(N
), Loc
);
8212 Insert_Actions_After
(N
,
8213 Build_Initialization_Call
(Sloc
(N
), Ref
, Typ
));
8215 end Initialize_Discriminants
;
8222 (Obj_Type
: Entity_Id
;
8223 Typ
: Entity_Id
) return Boolean
8225 L1
, L2
, H1
, H2
: Node_Id
;
8228 -- No sliding if the type of the object is not established yet, if it is
8229 -- an unconstrained type whose actual subtype comes from the aggregate,
8230 -- or if the two types are identical.
8232 if not Is_Array_Type
(Obj_Type
) then
8235 elsif not Is_Constrained
(Obj_Type
) then
8238 elsif Typ
= Obj_Type
then
8242 -- Sliding can only occur along the first dimension
8244 Get_Index_Bounds
(First_Index
(Typ
), L1
, H1
);
8245 Get_Index_Bounds
(First_Index
(Obj_Type
), L2
, H2
);
8247 if not Is_OK_Static_Expression
(L1
) or else
8248 not Is_OK_Static_Expression
(L2
) or else
8249 not Is_OK_Static_Expression
(H1
) or else
8250 not Is_OK_Static_Expression
(H2
)
8254 return Expr_Value
(L1
) /= Expr_Value
(L2
)
8256 Expr_Value
(H1
) /= Expr_Value
(H2
);
8261 ---------------------------------
8262 -- Process_Transient_Component --
8263 ---------------------------------
8265 procedure Process_Transient_Component
8267 Comp_Typ
: Entity_Id
;
8268 Init_Expr
: Node_Id
;
8269 Fin_Call
: out Node_Id
;
8270 Hook_Clear
: out Node_Id
;
8271 Aggr
: Node_Id
:= Empty
;
8272 Stmts
: List_Id
:= No_List
)
8274 procedure Add_Item
(Item
: Node_Id
);
8275 -- Insert arbitrary node Item into the tree depending on the values of
8282 procedure Add_Item
(Item
: Node_Id
) is
8284 if Present
(Aggr
) then
8285 Insert_Action
(Aggr
, Item
);
8287 pragma Assert
(Present
(Stmts
));
8288 Append_To
(Stmts
, Item
);
8294 Hook_Assign
: Node_Id
;
8295 Hook_Decl
: Node_Id
;
8299 Res_Typ
: Entity_Id
;
8301 -- Start of processing for Process_Transient_Component
8304 -- Add the access type, which provides a reference to the function
8305 -- result. Generate:
8307 -- type Res_Typ is access all Comp_Typ;
8309 Res_Typ
:= Make_Temporary
(Loc
, 'A');
8310 Set_Ekind
(Res_Typ
, E_General_Access_Type
);
8311 Set_Directly_Designated_Type
(Res_Typ
, Comp_Typ
);
8314 (Make_Full_Type_Declaration
(Loc
,
8315 Defining_Identifier
=> Res_Typ
,
8317 Make_Access_To_Object_Definition
(Loc
,
8318 All_Present
=> True,
8319 Subtype_Indication
=> New_Occurrence_Of
(Comp_Typ
, Loc
))));
8321 -- Add the temporary which captures the result of the function call.
8324 -- Res : constant Res_Typ := Init_Expr'Reference;
8326 -- Note that this temporary is effectively a transient object because
8327 -- its lifetime is bounded by the current array or record component.
8329 Res_Id
:= Make_Temporary
(Loc
, 'R');
8330 Set_Ekind
(Res_Id
, E_Constant
);
8331 Set_Etype
(Res_Id
, Res_Typ
);
8333 -- Mark the transient object as successfully processed to avoid double
8336 Set_Is_Finalized_Transient
(Res_Id
);
8338 -- Signal the general finalization machinery that this transient object
8339 -- should not be considered for finalization actions because its cleanup
8340 -- will be performed by Process_Transient_Component_Completion.
8342 Set_Is_Ignored_Transient
(Res_Id
);
8345 Make_Object_Declaration
(Loc
,
8346 Defining_Identifier
=> Res_Id
,
8347 Constant_Present
=> True,
8348 Object_Definition
=> New_Occurrence_Of
(Res_Typ
, Loc
),
8350 Make_Reference
(Loc
, New_Copy_Tree
(Init_Expr
)));
8352 Add_Item
(Res_Decl
);
8354 -- Construct all pieces necessary to hook and finalize the transient
8357 Build_Transient_Object_Statements
8358 (Obj_Decl
=> Res_Decl
,
8359 Fin_Call
=> Fin_Call
,
8360 Hook_Assign
=> Hook_Assign
,
8361 Hook_Clear
=> Hook_Clear
,
8362 Hook_Decl
=> Hook_Decl
,
8363 Ptr_Decl
=> Ptr_Decl
);
8365 -- Add the access type which provides a reference to the transient
8366 -- result. Generate:
8368 -- type Ptr_Typ is access all Comp_Typ;
8370 Add_Item
(Ptr_Decl
);
8372 -- Add the temporary which acts as a hook to the transient result.
8375 -- Hook : Ptr_Typ := null;
8377 Add_Item
(Hook_Decl
);
8379 -- Attach the transient result to the hook. Generate:
8381 -- Hook := Ptr_Typ (Res);
8383 Add_Item
(Hook_Assign
);
8385 -- The original initialization expression now references the value of
8386 -- the temporary function result. Generate:
8391 Make_Explicit_Dereference
(Loc
,
8392 Prefix
=> New_Occurrence_Of
(Res_Id
, Loc
)));
8393 end Process_Transient_Component
;
8395 --------------------------------------------
8396 -- Process_Transient_Component_Completion --
8397 --------------------------------------------
8399 procedure Process_Transient_Component_Completion
8403 Hook_Clear
: Node_Id
;
8406 Exceptions_OK
: constant Boolean :=
8407 not Restriction_Active
(No_Exception_Propagation
);
8410 pragma Assert
(Present
(Hook_Clear
));
8412 -- Generate the following code if exception propagation is allowed:
8415 -- Abort : constant Boolean := Triggered_By_Abort;
8417 -- Abort : constant Boolean := False; -- no abort
8419 -- E : Exception_Occurrence;
8420 -- Raised : Boolean := False;
8427 -- [Deep_]Finalize (Res.all);
8431 -- if not Raised then
8433 -- Save_Occurrence (E,
8434 -- Get_Curent_Excep.all.all);
8440 -- if Raised and then not Abort then
8441 -- Raise_From_Controlled_Operation (E);
8445 if Exceptions_OK
then
8446 Abort_And_Exception
: declare
8447 Blk_Decls
: constant List_Id
:= New_List
;
8448 Blk_Stmts
: constant List_Id
:= New_List
;
8449 Fin_Stmts
: constant List_Id
:= New_List
;
8451 Fin_Data
: Finalization_Exception_Data
;
8454 -- Create the declarations of the two flags and the exception
8457 Build_Object_Declarations
(Fin_Data
, Blk_Decls
, Loc
);
8462 if Abort_Allowed
then
8463 Append_To
(Blk_Stmts
,
8464 Build_Runtime_Call
(Loc
, RE_Abort_Defer
));
8467 -- Wrap the hook clear and the finalization call in order to trap
8468 -- a potential exception.
8470 Append_To
(Fin_Stmts
, Hook_Clear
);
8472 if Present
(Fin_Call
) then
8473 Append_To
(Fin_Stmts
, Fin_Call
);
8476 Append_To
(Blk_Stmts
,
8477 Make_Block_Statement
(Loc
,
8478 Handled_Statement_Sequence
=>
8479 Make_Handled_Sequence_Of_Statements
(Loc
,
8480 Statements
=> Fin_Stmts
,
8481 Exception_Handlers
=> New_List
(
8482 Build_Exception_Handler
(Fin_Data
)))));
8487 if Abort_Allowed
then
8488 Append_To
(Blk_Stmts
,
8489 Build_Runtime_Call
(Loc
, RE_Abort_Undefer
));
8492 -- Reraise the potential exception with a proper "upgrade" to
8493 -- Program_Error if needed.
8495 Append_To
(Blk_Stmts
, Build_Raise_Statement
(Fin_Data
));
8497 -- Wrap everything in a block
8500 Make_Block_Statement
(Loc
,
8501 Declarations
=> Blk_Decls
,
8502 Handled_Statement_Sequence
=>
8503 Make_Handled_Sequence_Of_Statements
(Loc
,
8504 Statements
=> Blk_Stmts
)));
8505 end Abort_And_Exception
;
8507 -- Generate the following code if exception propagation is not allowed
8508 -- and aborts are allowed:
8513 -- [Deep_]Finalize (Res.all);
8515 -- Abort_Undefer_Direct;
8518 elsif Abort_Allowed
then
8519 Abort_Only
: declare
8520 Blk_Stmts
: constant List_Id
:= New_List
;
8523 Append_To
(Blk_Stmts
, Build_Runtime_Call
(Loc
, RE_Abort_Defer
));
8524 Append_To
(Blk_Stmts
, Hook_Clear
);
8526 if Present
(Fin_Call
) then
8527 Append_To
(Blk_Stmts
, Fin_Call
);
8531 Build_Abort_Undefer_Block
(Loc
,
8536 -- Otherwise generate:
8539 -- [Deep_]Finalize (Res.all);
8542 Append_To
(Stmts
, Hook_Clear
);
8544 if Present
(Fin_Call
) then
8545 Append_To
(Stmts
, Fin_Call
);
8548 end Process_Transient_Component_Completion
;
8550 ---------------------
8551 -- Sort_Case_Table --
8552 ---------------------
8554 procedure Sort_Case_Table
(Case_Table
: in out Case_Table_Type
) is
8555 L
: constant Int
:= Case_Table
'First;
8556 U
: constant Int
:= Case_Table
'Last;
8564 T
:= Case_Table
(K
+ 1);
8568 and then Expr_Value
(Case_Table
(J
- 1).Choice_Lo
) >
8569 Expr_Value
(T
.Choice_Lo
)
8571 Case_Table
(J
) := Case_Table
(J
- 1);
8575 Case_Table
(J
) := T
;
8578 end Sort_Case_Table
;
8580 ----------------------------
8581 -- Static_Array_Aggregate --
8582 ----------------------------
8584 function Static_Array_Aggregate
(N
: Node_Id
) return Boolean is
8585 function Is_Static_Component
(Nod
: Node_Id
) return Boolean;
8586 -- Return True if Nod has a compile-time known value and can be passed
8587 -- as is to the back-end without further expansion.
8589 ---------------------------
8590 -- Is_Static_Component --
8591 ---------------------------
8593 function Is_Static_Component
(Nod
: Node_Id
) return Boolean is
8595 if Nkind_In
(Nod
, N_Integer_Literal
, N_Real_Literal
) then
8598 elsif Is_Entity_Name
(Nod
)
8599 and then Present
(Entity
(Nod
))
8600 and then Ekind
(Entity
(Nod
)) = E_Enumeration_Literal
8604 elsif Nkind
(Nod
) = N_Aggregate
8605 and then Compile_Time_Known_Aggregate
(Nod
)
8612 end Is_Static_Component
;
8616 Bounds
: constant Node_Id
:= Aggregate_Bounds
(N
);
8617 Typ
: constant Entity_Id
:= Etype
(N
);
8624 -- Start of processing for Static_Array_Aggregate
8627 if Is_Packed
(Typ
) or else Has_Discriminants
(Component_Type
(Typ
)) then
8632 and then Nkind
(Bounds
) = N_Range
8633 and then Nkind
(Low_Bound
(Bounds
)) = N_Integer_Literal
8634 and then Nkind
(High_Bound
(Bounds
)) = N_Integer_Literal
8636 Lo
:= Low_Bound
(Bounds
);
8637 Hi
:= High_Bound
(Bounds
);
8639 if No
(Component_Associations
(N
)) then
8641 -- Verify that all components are static
8643 Expr
:= First
(Expressions
(N
));
8644 while Present
(Expr
) loop
8645 if not Is_Static_Component
(Expr
) then
8655 -- We allow only a single named association, either a static
8656 -- range or an others_clause, with a static expression.
8658 Expr
:= First
(Component_Associations
(N
));
8660 if Present
(Expressions
(N
)) then
8663 elsif Present
(Next
(Expr
)) then
8666 elsif Present
(Next
(First
(Choice_List
(Expr
)))) then
8670 -- The aggregate is static if all components are literals,
8671 -- or else all its components are static aggregates for the
8672 -- component type. We also limit the size of a static aggregate
8673 -- to prevent runaway static expressions.
8675 if not Is_Static_Component
(Expression
(Expr
)) then
8679 if not Aggr_Size_OK
(N
, Typ
) then
8683 -- Create a positional aggregate with the right number of
8684 -- copies of the expression.
8686 Agg
:= Make_Aggregate
(Sloc
(N
), New_List
, No_List
);
8688 for I
in UI_To_Int
(Intval
(Lo
)) .. UI_To_Int
(Intval
(Hi
))
8690 Append_To
(Expressions
(Agg
), New_Copy
(Expression
(Expr
)));
8692 -- The copied expression must be analyzed and resolved.
8693 -- Besides setting the type, this ensures that static
8694 -- expressions are appropriately marked as such.
8697 (Last
(Expressions
(Agg
)), Component_Type
(Typ
));
8700 Set_Aggregate_Bounds
(Agg
, Bounds
);
8701 Set_Etype
(Agg
, Typ
);
8704 Set_Compile_Time_Known_Aggregate
(N
);
8713 end Static_Array_Aggregate
;
8715 ----------------------------------
8716 -- Two_Dim_Packed_Array_Handled --
8717 ----------------------------------
8719 function Two_Dim_Packed_Array_Handled
(N
: Node_Id
) return Boolean is
8720 Loc
: constant Source_Ptr
:= Sloc
(N
);
8721 Typ
: constant Entity_Id
:= Etype
(N
);
8722 Ctyp
: constant Entity_Id
:= Component_Type
(Typ
);
8723 Comp_Size
: constant Int
:= UI_To_Int
(Component_Size
(Typ
));
8724 Packed_Array
: constant Entity_Id
:=
8725 Packed_Array_Impl_Type
(Base_Type
(Typ
));
8728 -- Expression in original aggregate
8731 -- One-dimensional subaggregate
8735 -- For now, only deal with cases where an integral number of elements
8736 -- fit in a single byte. This includes the most common boolean case.
8738 if not (Comp_Size
= 1 or else
8739 Comp_Size
= 2 or else
8745 Convert_To_Positional
8746 (N
, Max_Others_Replicate
=> 64, Handle_Bit_Packed
=> True);
8748 -- Verify that all components are static
8750 if Nkind
(N
) = N_Aggregate
8751 and then Compile_Time_Known_Aggregate
(N
)
8755 -- The aggregate may have been reanalyzed and converted already
8757 elsif Nkind
(N
) /= N_Aggregate
then
8760 -- If component associations remain, the aggregate is not static
8762 elsif Present
(Component_Associations
(N
)) then
8766 One_Dim
:= First
(Expressions
(N
));
8767 while Present
(One_Dim
) loop
8768 if Present
(Component_Associations
(One_Dim
)) then
8772 One_Comp
:= First
(Expressions
(One_Dim
));
8773 while Present
(One_Comp
) loop
8774 if not Is_OK_Static_Expression
(One_Comp
) then
8785 -- Two-dimensional aggregate is now fully positional so pack one
8786 -- dimension to create a static one-dimensional array, and rewrite
8787 -- as an unchecked conversion to the original type.
8790 Byte_Size
: constant Int
:= UI_To_Int
(Component_Size
(Packed_Array
));
8791 -- The packed array type is a byte array
8794 -- Number of components accumulated in current byte
8797 -- Assembled list of packed values for equivalent aggregate
8800 -- Integer value of component
8803 -- Step size for packing
8806 -- Endian-dependent start position for packing
8809 -- Current insertion position
8812 -- Component of packed array being assembled
8819 -- Account for endianness. See corresponding comment in
8820 -- Packed_Array_Aggregate_Handled concerning the following.
8824 xor Reverse_Storage_Order
(Base_Type
(Typ
))
8826 Init_Shift
:= Byte_Size
- Comp_Size
;
8833 -- Iterate over each subaggregate
8835 Shift
:= Init_Shift
;
8836 One_Dim
:= First
(Expressions
(N
));
8837 while Present
(One_Dim
) loop
8838 One_Comp
:= First
(Expressions
(One_Dim
));
8839 while Present
(One_Comp
) loop
8840 if Packed_Num
= Byte_Size
/ Comp_Size
then
8842 -- Byte is complete, add to list of expressions
8844 Append
(Make_Integer_Literal
(Sloc
(One_Dim
), Val
), Comps
);
8846 Shift
:= Init_Shift
;
8850 Comp_Val
:= Expr_Rep_Value
(One_Comp
);
8852 -- Adjust for bias, and strip proper number of bits
8854 if Has_Biased_Representation
(Ctyp
) then
8855 Comp_Val
:= Comp_Val
- Expr_Value
(Type_Low_Bound
(Ctyp
));
8858 Comp_Val
:= Comp_Val
mod Uint_2
** Comp_Size
;
8859 Val
:= UI_To_Int
(Val
+ Comp_Val
* Uint_2
** Shift
);
8860 Shift
:= Shift
+ Incr
;
8861 One_Comp
:= Next
(One_Comp
);
8862 Packed_Num
:= Packed_Num
+ 1;
8866 One_Dim
:= Next
(One_Dim
);
8869 if Packed_Num
> 0 then
8871 -- Add final incomplete byte if present
8873 Append
(Make_Integer_Literal
(Sloc
(One_Dim
), Val
), Comps
);
8877 Unchecked_Convert_To
(Typ
,
8878 Make_Qualified_Expression
(Loc
,
8879 Subtype_Mark
=> New_Occurrence_Of
(Packed_Array
, Loc
),
8880 Expression
=> Make_Aggregate
(Loc
, Expressions
=> Comps
))));
8881 Analyze_And_Resolve
(N
);
8884 end Two_Dim_Packed_Array_Handled
;