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 procedure Initialize_Discriminants
(N
: Node_Id
; Typ
: Entity_Id
);
221 -- If the type of the aggregate is a type extension with renamed discrimi-
222 -- nants, we must initialize the hidden discriminants of the parent.
223 -- Otherwise, the target object must not be initialized. The discriminants
224 -- are initialized by calling the initialization procedure for the type.
225 -- This is incorrect if the initialization of other components has any
226 -- side effects. We restrict this call to the case where the parent type
227 -- has a variant part, because this is the only case where the hidden
228 -- discriminants are accessed, namely when calling discriminant checking
229 -- functions of the parent type, and when applying a stream attribute to
230 -- an object of the derived type.
232 -----------------------------------------------------
233 -- Local Subprograms for Array Aggregate Expansion --
234 -----------------------------------------------------
236 function Aggr_Size_OK
(N
: Node_Id
; Typ
: Entity_Id
) return Boolean;
237 -- Very large static aggregates present problems to the back-end, and are
238 -- transformed into assignments and loops. This function verifies that the
239 -- total number of components of an aggregate is acceptable for rewriting
240 -- into a purely positional static form. Aggr_Size_OK must be called before
243 -- This function also detects and warns about one-component aggregates that
244 -- appear in a nonstatic context. Even if the component value is static,
245 -- such an aggregate must be expanded into an assignment.
247 function Backend_Processing_Possible
(N
: Node_Id
) return Boolean;
248 -- This function checks if array aggregate N can be processed directly
249 -- by the backend. If this is the case, True is returned.
251 function Build_Array_Aggr_Code
256 Scalar_Comp
: Boolean;
257 Indexes
: List_Id
:= No_List
) return List_Id
;
258 -- This recursive routine returns a list of statements containing the
259 -- loops and assignments that are needed for the expansion of the array
262 -- N is the (sub-)aggregate node to be expanded into code. This node has
263 -- been fully analyzed, and its Etype is properly set.
265 -- Index is the index node corresponding to the array subaggregate N
267 -- Into is the target expression into which we are copying the aggregate.
268 -- Note that this node may not have been analyzed yet, and so the Etype
269 -- field may not be set.
271 -- Scalar_Comp is True if the component type of the aggregate is scalar
273 -- Indexes is the current list of expressions used to index the object we
276 procedure Convert_Array_Aggr_In_Allocator
280 -- If the aggregate appears within an allocator and can be expanded in
281 -- place, this routine generates the individual assignments to components
282 -- of the designated object. This is an optimization over the general
283 -- case, where a temporary is first created on the stack and then used to
284 -- construct the allocated object on the heap.
286 procedure Convert_To_Positional
288 Max_Others_Replicate
: Nat
:= 32;
289 Handle_Bit_Packed
: Boolean := False);
290 -- If possible, convert named notation to positional notation. This
291 -- conversion is possible only in some static cases. If the conversion is
292 -- possible, then N is rewritten with the analyzed converted aggregate.
293 -- The parameter Max_Others_Replicate controls the maximum number of
294 -- values corresponding to an others choice that will be converted to
295 -- positional notation (the default of 32 is the normal limit, and reflects
296 -- the fact that normally the loop is better than a lot of separate
297 -- assignments). Note that this limit gets overridden in any case if
298 -- either of the restrictions No_Elaboration_Code or No_Implicit_Loops is
299 -- set. The parameter Handle_Bit_Packed is usually set False (since we do
300 -- not expect the back end to handle bit packed arrays, so the normal case
301 -- of conversion is pointless), but in the special case of a call from
302 -- Packed_Array_Aggregate_Handled, we set this parameter to True, since
303 -- these are cases we handle in there.
305 procedure Expand_Array_Aggregate
(N
: Node_Id
);
306 -- This is the top-level routine to perform array aggregate expansion.
307 -- N is the N_Aggregate node to be expanded.
309 function Is_Two_Dim_Packed_Array
(Typ
: Entity_Id
) return Boolean;
310 -- For two-dimensional packed aggregates with constant bounds and constant
311 -- components, it is preferable to pack the inner aggregates because the
312 -- whole matrix can then be presented to the back-end as a one-dimensional
313 -- list of literals. This is much more efficient than expanding into single
314 -- component assignments. This function determines if the type Typ is for
315 -- an array that is suitable for this optimization: it returns True if Typ
316 -- is a two dimensional bit packed array with component size 1, 2, or 4.
318 function Packed_Array_Aggregate_Handled
(N
: Node_Id
) return Boolean;
319 -- Given an array aggregate, this function handles the case of a packed
320 -- array aggregate with all constant values, where the aggregate can be
321 -- evaluated at compile time. If this is possible, then N is rewritten
322 -- to be its proper compile time value with all the components properly
323 -- assembled. The expression is analyzed and resolved and True is returned.
324 -- If this transformation is not possible, N is unchanged and False is
327 function Two_Dim_Packed_Array_Handled
(N
: Node_Id
) return Boolean;
328 -- If the type of the aggregate is a two-dimensional bit_packed array
329 -- it may be transformed into an array of bytes with constant values,
330 -- and presented to the back-end as a static value. The function returns
331 -- false if this transformation cannot be performed. THis is similar to,
332 -- and reuses part of the machinery in Packed_Array_Aggregate_Handled.
338 function Aggr_Size_OK
(N
: Node_Id
; Typ
: Entity_Id
) return Boolean is
347 -- Determines the maximum size of an array aggregate produced by
348 -- converting named to positional notation (e.g. from others clauses).
349 -- This avoids running away with attempts to convert huge aggregates,
350 -- which hit memory limits in the backend.
352 function Component_Count
(T
: Entity_Id
) return Nat
;
353 -- The limit is applied to the total number of subcomponents that the
354 -- aggregate will have, which is the number of static expressions
355 -- that will appear in the flattened array. This requires a recursive
356 -- computation of the number of scalar components of the structure.
358 ---------------------
359 -- Component_Count --
360 ---------------------
362 function Component_Count
(T
: Entity_Id
) return Nat
is
367 if Is_Scalar_Type
(T
) then
370 elsif Is_Record_Type
(T
) then
371 Comp
:= First_Component
(T
);
372 while Present
(Comp
) loop
373 Res
:= Res
+ Component_Count
(Etype
(Comp
));
374 Next_Component
(Comp
);
379 elsif Is_Array_Type
(T
) then
381 Lo
: constant Node_Id
:=
382 Type_Low_Bound
(Etype
(First_Index
(T
)));
383 Hi
: constant Node_Id
:=
384 Type_High_Bound
(Etype
(First_Index
(T
)));
386 Siz
: constant Nat
:= Component_Count
(Component_Type
(T
));
389 -- Check for superflat arrays, i.e. arrays with such bounds
390 -- as 4 .. 2, to insure that this function never returns a
391 -- meaningless negative value.
393 if not Compile_Time_Known_Value
(Lo
)
394 or else not Compile_Time_Known_Value
(Hi
)
395 or else Expr_Value
(Hi
) < Expr_Value
(Lo
)
400 -- If the number of components is greater than Int'Last,
401 -- then return Int'Last, so caller will return False (Aggr
402 -- size is not OK). Otherwise, UI_To_Int will crash.
405 UI
: constant Uint
:=
406 Expr_Value
(Hi
) - Expr_Value
(Lo
) + 1;
408 if UI_Is_In_Int_Range
(UI
) then
409 return Siz
* UI_To_Int
(UI
);
418 -- Can only be a null for an access type
424 -- Start of processing for Aggr_Size_OK
427 -- The normal aggregate limit is 500000, but we increase this limit to
428 -- 2**24 (about 16 million) if Restrictions (No_Elaboration_Code) or
429 -- Restrictions (No_Implicit_Loops) is specified, since in either case
430 -- we are at risk of declaring the program illegal because of this
431 -- limit. We also increase the limit when Static_Elaboration_Desired,
432 -- given that this means that objects are intended to be placed in data
435 -- We also increase the limit if the aggregate is for a packed two-
436 -- dimensional array, because if components are static it is much more
437 -- efficient to construct a one-dimensional equivalent array with static
440 -- Conversely, we decrease the maximum size if none of the above
441 -- requirements apply, and if the aggregate has a single component
442 -- association, which will be more efficient if implemented with a loop.
444 -- Finally, we use a small limit in CodePeer mode where we favor loops
445 -- instead of thousands of single assignments (from large aggregates).
447 Max_Aggr_Size
:= 500000;
449 if CodePeer_Mode
then
450 Max_Aggr_Size
:= 100;
452 elsif Restriction_Active
(No_Elaboration_Code
)
453 or else Restriction_Active
(No_Implicit_Loops
)
454 or else Is_Two_Dim_Packed_Array
(Typ
)
455 or else (Ekind
(Current_Scope
) = E_Package
456 and then Static_Elaboration_Desired
(Current_Scope
))
458 Max_Aggr_Size
:= 2 ** 24;
460 elsif No
(Expressions
(N
))
461 and then No
(Next
(First
(Component_Associations
(N
))))
463 Max_Aggr_Size
:= 5000;
466 Siz
:= Component_Count
(Component_Type
(Typ
));
468 Indx
:= First_Index
(Typ
);
469 while Present
(Indx
) loop
470 Lo
:= Type_Low_Bound
(Etype
(Indx
));
471 Hi
:= Type_High_Bound
(Etype
(Indx
));
473 -- Bounds need to be known at compile time
475 if not Compile_Time_Known_Value
(Lo
)
476 or else not Compile_Time_Known_Value
(Hi
)
481 Lov
:= Expr_Value
(Lo
);
482 Hiv
:= Expr_Value
(Hi
);
484 -- A flat array is always safe
490 -- One-component aggregates are suspicious, and if the context type
491 -- is an object declaration with nonstatic bounds it will trip gcc;
492 -- such an aggregate must be expanded into a single assignment.
494 if Hiv
= Lov
and then Nkind
(Parent
(N
)) = N_Object_Declaration
then
496 Index_Type
: constant Entity_Id
:=
498 (First_Index
(Etype
(Defining_Identifier
(Parent
(N
)))));
502 if not Compile_Time_Known_Value
(Type_Low_Bound
(Index_Type
))
503 or else not Compile_Time_Known_Value
504 (Type_High_Bound
(Index_Type
))
506 if Present
(Component_Associations
(N
)) then
509 (Choice_List
(First
(Component_Associations
(N
))));
511 if Is_Entity_Name
(Indx
)
512 and then not Is_Type
(Entity
(Indx
))
515 ("single component aggregate in "
516 & "non-static context??", Indx
);
517 Error_Msg_N
("\maybe subtype name was meant??", Indx
);
527 Rng
: constant Uint
:= Hiv
- Lov
+ 1;
530 -- Check if size is too large
532 if not UI_Is_In_Int_Range
(Rng
) then
536 Siz
:= Siz
* UI_To_Int
(Rng
);
540 or else Siz
> Max_Aggr_Size
545 -- Bounds must be in integer range, for later array construction
547 if not UI_Is_In_Int_Range
(Lov
)
549 not UI_Is_In_Int_Range
(Hiv
)
560 ---------------------------------
561 -- Backend_Processing_Possible --
562 ---------------------------------
564 -- Backend processing by Gigi/gcc is possible only if all the following
565 -- conditions are met:
567 -- 1. N is fully positional
569 -- 2. N is not a bit-packed array aggregate;
571 -- 3. The size of N's array type must be known at compile time. Note
572 -- that this implies that the component size is also known
574 -- 4. The array type of N does not follow the Fortran layout convention
575 -- or if it does it must be 1 dimensional.
577 -- 5. The array component type may not be tagged (which could necessitate
578 -- reassignment of proper tags).
580 -- 6. The array component type must not have unaligned bit components
582 -- 7. None of the components of the aggregate may be bit unaligned
585 -- 8. There cannot be delayed components, since we do not know enough
586 -- at this stage to know if back end processing is possible.
588 -- 9. There cannot be any discriminated record components, since the
589 -- back end cannot handle this complex case.
591 -- 10. No controlled actions need to be generated for components
593 -- 11. When generating C code, N must be part of a N_Object_Declaration
595 -- 12. When generating C code, N must not include function calls
597 function Backend_Processing_Possible
(N
: Node_Id
) return Boolean is
598 Typ
: constant Entity_Id
:= Etype
(N
);
599 -- Typ is the correct constrained array subtype of the aggregate
601 function Component_Check
(N
: Node_Id
; Index
: Node_Id
) return Boolean;
602 -- This routine checks components of aggregate N, enforcing checks
603 -- 1, 7, 8, 9, 11, and 12. In the multidimensional case, these checks
604 -- are performed on subaggregates. The Index value is the current index
605 -- being checked in the multidimensional case.
607 ---------------------
608 -- Component_Check --
609 ---------------------
611 function Component_Check
(N
: Node_Id
; Index
: Node_Id
) return Boolean is
612 function Ultimate_Original_Expression
(N
: Node_Id
) return Node_Id
;
613 -- Given a type conversion or an unchecked type conversion N, return
614 -- its innermost original expression.
616 ----------------------------------
617 -- Ultimate_Original_Expression --
618 ----------------------------------
620 function Ultimate_Original_Expression
(N
: Node_Id
) return Node_Id
is
621 Expr
: Node_Id
:= Original_Node
(N
);
624 while Nkind_In
(Expr
, N_Type_Conversion
,
625 N_Unchecked_Type_Conversion
)
627 Expr
:= Original_Node
(Expression
(Expr
));
631 end Ultimate_Original_Expression
;
637 -- Start of processing for Component_Check
640 -- Checks 1: (no component associations)
642 if Present
(Component_Associations
(N
)) then
646 -- Checks 11: The C code generator cannot handle aggregates that are
647 -- not part of an object declaration.
649 if Modify_Tree_For_C
then
651 Par
: Node_Id
:= Parent
(N
);
654 -- Skip enclosing nested aggregates and their qualified
657 while Nkind
(Par
) = N_Aggregate
658 or else Nkind
(Par
) = N_Qualified_Expression
663 if Nkind
(Par
) /= N_Object_Declaration
then
669 -- Checks on components
671 -- Recurse to check subaggregates, which may appear in qualified
672 -- expressions. If delayed, the front-end will have to expand.
673 -- If the component is a discriminated record, treat as nonstatic,
674 -- as the back-end cannot handle this properly.
676 Expr
:= First
(Expressions
(N
));
677 while Present
(Expr
) loop
679 -- Checks 8: (no delayed components)
681 if Is_Delayed_Aggregate
(Expr
) then
685 -- Checks 9: (no discriminated records)
687 if Present
(Etype
(Expr
))
688 and then Is_Record_Type
(Etype
(Expr
))
689 and then Has_Discriminants
(Etype
(Expr
))
694 -- Checks 7. Component must not be bit aligned component
696 if Possible_Bit_Aligned_Component
(Expr
) then
700 -- Checks 12: (no function call)
704 Nkind
(Ultimate_Original_Expression
(Expr
)) = N_Function_Call
709 -- Recursion to following indexes for multiple dimension case
711 if Present
(Next_Index
(Index
))
712 and then not Component_Check
(Expr
, Next_Index
(Index
))
717 -- All checks for that component finished, on to next
725 -- Start of processing for Backend_Processing_Possible
728 -- Checks 2 (array not bit packed) and 10 (no controlled actions)
730 if Is_Bit_Packed_Array
(Typ
) or else Needs_Finalization
(Typ
) then
734 -- If component is limited, aggregate must be expanded because each
735 -- component assignment must be built in place.
737 if Is_Limited_View
(Component_Type
(Typ
)) then
741 -- Checks 4 (array must not be multidimensional Fortran case)
743 if Convention
(Typ
) = Convention_Fortran
744 and then Number_Dimensions
(Typ
) > 1
749 -- Checks 3 (size of array must be known at compile time)
751 if not Size_Known_At_Compile_Time
(Typ
) then
755 -- Checks on components
757 if not Component_Check
(N
, First_Index
(Typ
)) then
761 -- Checks 5 (if the component type is tagged, then we may need to do
762 -- tag adjustments. Perhaps this should be refined to check for any
763 -- component associations that actually need tag adjustment, similar
764 -- to the test in Component_OK_For_Backend for record aggregates with
765 -- tagged components, but not clear whether it's worthwhile ???; in the
766 -- case of virtual machines (no Tagged_Type_Expansion), object tags are
767 -- handled implicitly).
769 if Is_Tagged_Type
(Component_Type
(Typ
))
770 and then Tagged_Type_Expansion
775 -- Checks 6 (component type must not have bit aligned components)
777 if Type_May_Have_Bit_Aligned_Components
(Component_Type
(Typ
)) then
781 -- Backend processing is possible
783 Set_Size_Known_At_Compile_Time
(Etype
(N
), True);
785 end Backend_Processing_Possible
;
787 ---------------------------
788 -- Build_Array_Aggr_Code --
789 ---------------------------
791 -- The code that we generate from a one dimensional aggregate is
793 -- 1. If the subaggregate contains discrete choices we
795 -- (a) Sort the discrete choices
797 -- (b) Otherwise for each discrete choice that specifies a range we
798 -- emit a loop. If a range specifies a maximum of three values, or
799 -- we are dealing with an expression we emit a sequence of
800 -- assignments instead of a loop.
802 -- (c) Generate the remaining loops to cover the others choice if any
804 -- 2. If the aggregate contains positional elements we
806 -- (a) translate the positional elements in a series of assignments
808 -- (b) Generate a final loop to cover the others choice if any.
809 -- Note that this final loop has to be a while loop since the case
811 -- L : Integer := Integer'Last;
812 -- H : Integer := Integer'Last;
813 -- A : array (L .. H) := (1, others =>0);
815 -- cannot be handled by a for loop. Thus for the following
817 -- array (L .. H) := (.. positional elements.., others =>E);
819 -- we always generate something like:
821 -- J : Index_Type := Index_Of_Last_Positional_Element;
823 -- J := Index_Base'Succ (J)
827 function Build_Array_Aggr_Code
832 Scalar_Comp
: Boolean;
833 Indexes
: List_Id
:= No_List
) return List_Id
835 Loc
: constant Source_Ptr
:= Sloc
(N
);
836 Index_Base
: constant Entity_Id
:= Base_Type
(Etype
(Index
));
837 Index_Base_L
: constant Node_Id
:= Type_Low_Bound
(Index_Base
);
838 Index_Base_H
: constant Node_Id
:= Type_High_Bound
(Index_Base
);
840 function Add
(Val
: Int
; To
: Node_Id
) return Node_Id
;
841 -- Returns an expression where Val is added to expression To, unless
842 -- To+Val is provably out of To's base type range. To must be an
843 -- already analyzed expression.
845 function Empty_Range
(L
, H
: Node_Id
) return Boolean;
846 -- Returns True if the range defined by L .. H is certainly empty
848 function Equal
(L
, H
: Node_Id
) return Boolean;
849 -- Returns True if L = H for sure
851 function Index_Base_Name
return Node_Id
;
852 -- Returns a new reference to the index type name
857 In_Loop
: Boolean := False) return List_Id
;
858 -- Ind must be a side-effect-free expression. If the input aggregate N
859 -- to Build_Loop contains no subaggregates, then this function returns
860 -- the assignment statement:
862 -- Into (Indexes, Ind) := Expr;
864 -- Otherwise we call Build_Code recursively. Flag In_Loop should be set
865 -- when the assignment appears within a generated loop.
867 -- Ada 2005 (AI-287): In case of default initialized component, Expr
868 -- is empty and we generate a call to the corresponding IP subprogram.
870 function Gen_Loop
(L
, H
: Node_Id
; Expr
: Node_Id
) return List_Id
;
871 -- Nodes L and H must be side-effect-free expressions. If the input
872 -- aggregate N to Build_Loop contains no subaggregates, this routine
873 -- returns the for loop statement:
875 -- for J in Index_Base'(L) .. Index_Base'(H) loop
876 -- Into (Indexes, J) := Expr;
879 -- Otherwise we call Build_Code recursively. As an optimization if the
880 -- loop covers 3 or fewer scalar elements we generate a sequence of
882 -- If the component association that generates the loop comes from an
883 -- Iterated_Component_Association, the loop parameter has the name of
884 -- the corresponding parameter in the original construct.
886 function Gen_While
(L
, H
: Node_Id
; Expr
: Node_Id
) return List_Id
;
887 -- Nodes L and H must be side-effect-free expressions. If the input
888 -- aggregate N to Build_Loop contains no subaggregates, this routine
889 -- returns the while loop statement:
891 -- J : Index_Base := L;
893 -- J := Index_Base'Succ (J);
894 -- Into (Indexes, J) := Expr;
897 -- Otherwise we call Build_Code recursively
899 function Get_Assoc_Expr
(Assoc
: Node_Id
) return Node_Id
;
900 -- For an association with a box, use value given by aspect
901 -- Default_Component_Value of array type if specified, else use
902 -- value given by aspect Default_Value for component type itself
903 -- if specified, else return Empty.
905 function Local_Compile_Time_Known_Value
(E
: Node_Id
) return Boolean;
906 function Local_Expr_Value
(E
: Node_Id
) return Uint
;
907 -- These two Local routines are used to replace the corresponding ones
908 -- in sem_eval because while processing the bounds of an aggregate with
909 -- discrete choices whose index type is an enumeration, we build static
910 -- expressions not recognized by Compile_Time_Known_Value as such since
911 -- they have not yet been analyzed and resolved. All the expressions in
912 -- question are things like Index_Base_Name'Val (Const) which we can
913 -- easily recognize as being constant.
919 function Add
(Val
: Int
; To
: Node_Id
) return Node_Id
is
924 U_Val
: constant Uint
:= UI_From_Int
(Val
);
927 -- Note: do not try to optimize the case of Val = 0, because
928 -- we need to build a new node with the proper Sloc value anyway.
930 -- First test if we can do constant folding
932 if Local_Compile_Time_Known_Value
(To
) then
933 U_To
:= Local_Expr_Value
(To
) + Val
;
935 -- Determine if our constant is outside the range of the index.
936 -- If so return an Empty node. This empty node will be caught
937 -- by Empty_Range below.
939 if Compile_Time_Known_Value
(Index_Base_L
)
940 and then U_To
< Expr_Value
(Index_Base_L
)
944 elsif Compile_Time_Known_Value
(Index_Base_H
)
945 and then U_To
> Expr_Value
(Index_Base_H
)
950 Expr_Pos
:= Make_Integer_Literal
(Loc
, U_To
);
951 Set_Is_Static_Expression
(Expr_Pos
);
953 if not Is_Enumeration_Type
(Index_Base
) then
956 -- If we are dealing with enumeration return
957 -- Index_Base'Val (Expr_Pos)
961 Make_Attribute_Reference
963 Prefix
=> Index_Base_Name
,
964 Attribute_Name
=> Name_Val
,
965 Expressions
=> New_List
(Expr_Pos
));
971 -- If we are here no constant folding possible
973 if not Is_Enumeration_Type
(Index_Base
) then
976 Left_Opnd
=> Duplicate_Subexpr
(To
),
977 Right_Opnd
=> Make_Integer_Literal
(Loc
, U_Val
));
979 -- If we are dealing with enumeration return
980 -- Index_Base'Val (Index_Base'Pos (To) + Val)
984 Make_Attribute_Reference
986 Prefix
=> Index_Base_Name
,
987 Attribute_Name
=> Name_Pos
,
988 Expressions
=> New_List
(Duplicate_Subexpr
(To
)));
993 Right_Opnd
=> Make_Integer_Literal
(Loc
, U_Val
));
996 Make_Attribute_Reference
998 Prefix
=> Index_Base_Name
,
999 Attribute_Name
=> Name_Val
,
1000 Expressions
=> New_List
(Expr_Pos
));
1010 function Empty_Range
(L
, H
: Node_Id
) return Boolean is
1011 Is_Empty
: Boolean := False;
1016 -- First check if L or H were already detected as overflowing the
1017 -- index base range type by function Add above. If this is so Add
1018 -- returns the empty node.
1020 if No
(L
) or else No
(H
) then
1024 for J
in 1 .. 3 loop
1027 -- L > H range is empty
1033 -- B_L > H range must be empty
1036 Low
:= Index_Base_L
;
1039 -- L > B_H range must be empty
1043 High
:= Index_Base_H
;
1046 if Local_Compile_Time_Known_Value
(Low
)
1048 Local_Compile_Time_Known_Value
(High
)
1051 UI_Gt
(Local_Expr_Value
(Low
), Local_Expr_Value
(High
));
1064 function Equal
(L
, H
: Node_Id
) return Boolean is
1069 elsif Local_Compile_Time_Known_Value
(L
)
1071 Local_Compile_Time_Known_Value
(H
)
1073 return UI_Eq
(Local_Expr_Value
(L
), Local_Expr_Value
(H
));
1086 In_Loop
: Boolean := False) return List_Id
1088 function Add_Loop_Actions
(Lis
: List_Id
) return List_Id
;
1089 -- Collect insert_actions generated in the construction of a loop,
1090 -- and prepend them to the sequence of assignments to complete the
1091 -- eventual body of the loop.
1093 procedure Initialize_Array_Component
1094 (Arr_Comp
: Node_Id
;
1096 Init_Expr
: Node_Id
;
1098 -- Perform the initialization of array component Arr_Comp with
1099 -- expected type Comp_Typ. Init_Expr denotes the initialization
1100 -- expression of the array component. All generated code is added
1103 procedure Initialize_Ctrl_Array_Component
1104 (Arr_Comp
: Node_Id
;
1105 Comp_Typ
: Entity_Id
;
1106 Init_Expr
: Node_Id
;
1108 -- Perform the initialization of array component Arr_Comp when its
1109 -- expected type Comp_Typ needs finalization actions. Init_Expr is
1110 -- the initialization expression of the array component. All hook-
1111 -- related declarations are inserted prior to aggregate N. Remaining
1112 -- code is added to list Stmts.
1114 ----------------------
1115 -- Add_Loop_Actions --
1116 ----------------------
1118 function Add_Loop_Actions
(Lis
: List_Id
) return List_Id
is
1122 -- Ada 2005 (AI-287): Do nothing else in case of default
1123 -- initialized component.
1128 elsif Nkind
(Parent
(Expr
)) = N_Component_Association
1129 and then Present
(Loop_Actions
(Parent
(Expr
)))
1131 Append_List
(Lis
, Loop_Actions
(Parent
(Expr
)));
1132 Res
:= Loop_Actions
(Parent
(Expr
));
1133 Set_Loop_Actions
(Parent
(Expr
), No_List
);
1139 end Add_Loop_Actions
;
1141 --------------------------------
1142 -- Initialize_Array_Component --
1143 --------------------------------
1145 procedure Initialize_Array_Component
1146 (Arr_Comp
: Node_Id
;
1148 Init_Expr
: Node_Id
;
1151 Exceptions_OK
: constant Boolean :=
1152 not Restriction_Active
1153 (No_Exception_Propagation
);
1155 Finalization_OK
: constant Boolean :=
1157 and then Needs_Finalization
(Comp_Typ
);
1159 Full_Typ
: constant Entity_Id
:= Underlying_Type
(Comp_Typ
);
1161 Blk_Stmts
: List_Id
;
1162 Init_Stmt
: Node_Id
;
1165 -- Protect the initialization statements from aborts. Generate:
1169 if Finalization_OK
and Abort_Allowed
then
1170 if Exceptions_OK
then
1171 Blk_Stmts
:= New_List
;
1176 Append_To
(Blk_Stmts
, Build_Runtime_Call
(Loc
, RE_Abort_Defer
));
1178 -- Otherwise aborts are not allowed. All generated code is added
1179 -- directly to the input list.
1185 -- Initialize the array element. Generate:
1187 -- Arr_Comp := Init_Expr;
1189 -- Note that the initialization expression is replicated because
1190 -- it has to be reevaluated within a generated loop.
1193 Make_OK_Assignment_Statement
(Loc
,
1194 Name
=> New_Copy_Tree
(Arr_Comp
),
1195 Expression
=> New_Copy_Tree
(Init_Expr
));
1196 Set_No_Ctrl_Actions
(Init_Stmt
);
1198 -- If this is an aggregate for an array of arrays, each
1199 -- subaggregate will be expanded as well, and even with
1200 -- No_Ctrl_Actions the assignments of inner components will
1201 -- require attachment in their assignments to temporaries. These
1202 -- temporaries must be finalized for each subaggregate. Generate:
1205 -- Arr_Comp := Init_Expr;
1208 if Finalization_OK
and then Is_Array_Type
(Comp_Typ
) then
1210 Make_Block_Statement
(Loc
,
1211 Handled_Statement_Sequence
=>
1212 Make_Handled_Sequence_Of_Statements
(Loc
,
1213 Statements
=> New_List
(Init_Stmt
)));
1216 Append_To
(Blk_Stmts
, Init_Stmt
);
1218 -- Adjust the tag due to a possible view conversion. Generate:
1220 -- Arr_Comp._tag := Full_TypP;
1222 if Tagged_Type_Expansion
1223 and then Present
(Comp_Typ
)
1224 and then Is_Tagged_Type
(Comp_Typ
)
1226 Append_To
(Blk_Stmts
,
1227 Make_OK_Assignment_Statement
(Loc
,
1229 Make_Selected_Component
(Loc
,
1230 Prefix
=> New_Copy_Tree
(Arr_Comp
),
1233 (First_Tag_Component
(Full_Typ
), Loc
)),
1236 Unchecked_Convert_To
(RTE
(RE_Tag
),
1238 (Node
(First_Elmt
(Access_Disp_Table
(Full_Typ
))),
1242 -- Adjust the array component. Controlled subaggregates are not
1243 -- considered because each of their individual elements will
1244 -- receive an adjustment of its own. Generate:
1246 -- [Deep_]Adjust (Arr_Comp);
1249 and then not Is_Limited_Type
(Comp_Typ
)
1250 and then not Is_Build_In_Place_Function_Call
(Init_Expr
)
1252 (Is_Array_Type
(Comp_Typ
)
1253 and then Is_Controlled
(Component_Type
(Comp_Typ
))
1254 and then Nkind
(Expr
) = N_Aggregate
)
1258 (Obj_Ref
=> New_Copy_Tree
(Arr_Comp
),
1261 -- Guard against a missing [Deep_]Adjust when the component
1262 -- type was not frozen properly.
1264 if Present
(Adj_Call
) then
1265 Append_To
(Blk_Stmts
, Adj_Call
);
1269 -- Complete the protection of the initialization statements
1271 if Finalization_OK
and Abort_Allowed
then
1273 -- Wrap the initialization statements in a block to catch a
1274 -- potential exception. Generate:
1278 -- Arr_Comp := Init_Expr;
1279 -- Arr_Comp._tag := Full_TypP;
1280 -- [Deep_]Adjust (Arr_Comp);
1282 -- Abort_Undefer_Direct;
1285 if Exceptions_OK
then
1287 Build_Abort_Undefer_Block
(Loc
,
1291 -- Otherwise exceptions are not propagated. Generate:
1294 -- Arr_Comp := Init_Expr;
1295 -- Arr_Comp._tag := Full_TypP;
1296 -- [Deep_]Adjust (Arr_Comp);
1300 Append_To
(Blk_Stmts
,
1301 Build_Runtime_Call
(Loc
, RE_Abort_Undefer
));
1304 end Initialize_Array_Component
;
1306 -------------------------------------
1307 -- Initialize_Ctrl_Array_Component --
1308 -------------------------------------
1310 procedure Initialize_Ctrl_Array_Component
1311 (Arr_Comp
: Node_Id
;
1312 Comp_Typ
: Entity_Id
;
1313 Init_Expr
: Node_Id
;
1317 Act_Stmts
: List_Id
;
1320 Hook_Clear
: Node_Id
;
1322 In_Place_Expansion
: Boolean;
1323 -- Flag set when a nonlimited controlled function call requires
1324 -- in-place expansion.
1327 -- Duplicate the initialization expression in case the context is
1328 -- a multi choice list or an "others" choice which plugs various
1329 -- holes in the aggregate. As a result the expression is no longer
1330 -- shared between the various components and is reevaluated for
1331 -- each such component.
1333 Expr
:= New_Copy_Tree
(Init_Expr
);
1334 Set_Parent
(Expr
, Parent
(Init_Expr
));
1336 -- Perform a preliminary analysis and resolution to determine what
1337 -- the initialization expression denotes. An unanalyzed function
1338 -- call may appear as an identifier or an indexed component.
1340 if Nkind_In
(Expr
, N_Function_Call
,
1342 N_Indexed_Component
)
1343 and then not Analyzed
(Expr
)
1345 Preanalyze_And_Resolve
(Expr
, Comp_Typ
);
1348 In_Place_Expansion
:=
1349 Nkind
(Expr
) = N_Function_Call
1350 and then not Is_Build_In_Place_Result_Type
(Comp_Typ
);
1352 -- The initialization expression is a controlled function call.
1353 -- Perform in-place removal of side effects to avoid creating a
1354 -- transient scope, which leads to premature finalization.
1356 -- This in-place expansion is not performed for limited transient
1357 -- objects because the initialization is already done in-place.
1359 if In_Place_Expansion
then
1361 -- Suppress the removal of side effects by general analysis
1362 -- because this behavior is emulated here. This avoids the
1363 -- generation of a transient scope, which leads to out-of-order
1364 -- adjustment and finalization.
1366 Set_No_Side_Effect_Removal
(Expr
);
1368 -- When the transient component initialization is related to a
1369 -- range or an "others", keep all generated statements within
1370 -- the enclosing loop. This way the controlled function call
1371 -- will be evaluated at each iteration, and its result will be
1372 -- finalized at the end of each iteration.
1378 -- Otherwise this is a single component initialization. Hook-
1379 -- related statements are inserted prior to the aggregate.
1383 Act_Stmts
:= No_List
;
1386 -- Install all hook-related declarations and prepare the clean
1389 Process_Transient_Component
1391 Comp_Typ
=> Comp_Typ
,
1393 Fin_Call
=> Fin_Call
,
1394 Hook_Clear
=> Hook_Clear
,
1396 Stmts
=> Act_Stmts
);
1399 -- Use the noncontrolled component initialization circuitry to
1400 -- assign the result of the function call to the array element.
1401 -- This also performs subaggregate wrapping, tag adjustment, and
1402 -- [deep] adjustment of the array element.
1404 Initialize_Array_Component
1405 (Arr_Comp
=> Arr_Comp
,
1406 Comp_Typ
=> Comp_Typ
,
1410 -- At this point the array element is fully initialized. Complete
1411 -- the processing of the controlled array component by finalizing
1412 -- the transient function result.
1414 if In_Place_Expansion
then
1415 Process_Transient_Component_Completion
1418 Fin_Call
=> Fin_Call
,
1419 Hook_Clear
=> Hook_Clear
,
1422 end Initialize_Ctrl_Array_Component
;
1426 Stmts
: constant List_Id
:= New_List
;
1428 Comp_Typ
: Entity_Id
:= Empty
;
1430 Indexed_Comp
: Node_Id
;
1431 Init_Call
: Node_Id
;
1432 New_Indexes
: List_Id
;
1434 -- Start of processing for Gen_Assign
1437 if No
(Indexes
) then
1438 New_Indexes
:= New_List
;
1440 New_Indexes
:= New_Copy_List_Tree
(Indexes
);
1443 Append_To
(New_Indexes
, Ind
);
1445 if Present
(Next_Index
(Index
)) then
1448 Build_Array_Aggr_Code
1451 Index
=> Next_Index
(Index
),
1453 Scalar_Comp
=> Scalar_Comp
,
1454 Indexes
=> New_Indexes
));
1457 -- If we get here then we are at a bottom-level (sub-)aggregate
1461 (Make_Indexed_Component
(Loc
,
1462 Prefix
=> New_Copy_Tree
(Into
),
1463 Expressions
=> New_Indexes
));
1465 Set_Assignment_OK
(Indexed_Comp
);
1467 -- Ada 2005 (AI-287): In case of default initialized component, Expr
1468 -- is not present (and therefore we also initialize Expr_Q to empty).
1472 elsif Nkind
(Expr
) = N_Qualified_Expression
then
1473 Expr_Q
:= Expression
(Expr
);
1478 if Present
(Etype
(N
)) and then Etype
(N
) /= Any_Composite
then
1479 Comp_Typ
:= Component_Type
(Etype
(N
));
1480 pragma Assert
(Comp_Typ
= Ctype
); -- AI-287
1482 elsif Present
(Next
(First
(New_Indexes
))) then
1484 -- Ada 2005 (AI-287): Do nothing in case of default initialized
1485 -- component because we have received the component type in
1486 -- the formal parameter Ctype.
1488 -- ??? Some assert pragmas have been added to check if this new
1489 -- formal can be used to replace this code in all cases.
1491 if Present
(Expr
) then
1493 -- This is a multidimensional array. Recover the component type
1494 -- from the outermost aggregate, because subaggregates do not
1495 -- have an assigned type.
1502 while Present
(P
) loop
1503 if Nkind
(P
) = N_Aggregate
1504 and then Present
(Etype
(P
))
1506 Comp_Typ
:= Component_Type
(Etype
(P
));
1514 pragma Assert
(Comp_Typ
= Ctype
); -- AI-287
1519 -- Ada 2005 (AI-287): We only analyze the expression in case of non-
1520 -- default initialized components (otherwise Expr_Q is not present).
1523 and then Nkind_In
(Expr_Q
, N_Aggregate
, N_Extension_Aggregate
)
1525 -- At this stage the Expression may not have been analyzed yet
1526 -- because the array aggregate code has not been updated to use
1527 -- the Expansion_Delayed flag and avoid analysis altogether to
1528 -- solve the same problem (see Resolve_Aggr_Expr). So let us do
1529 -- the analysis of non-array aggregates now in order to get the
1530 -- value of Expansion_Delayed flag for the inner aggregate ???
1532 -- In the case of an iterated component association, the analysis
1533 -- of the generated loop will analyze the expression in the
1534 -- proper context, in which the loop parameter is visible.
1536 if Present
(Comp_Typ
) and then not Is_Array_Type
(Comp_Typ
) then
1537 if Nkind
(Parent
(Expr_Q
)) = N_Iterated_Component_Association
1538 or else Nkind
(Parent
(Parent
((Expr_Q
)))) =
1539 N_Iterated_Component_Association
1543 Analyze_And_Resolve
(Expr_Q
, Comp_Typ
);
1547 if Is_Delayed_Aggregate
(Expr_Q
) then
1549 -- This is either a subaggregate of a multidimensional array,
1550 -- or a component of an array type whose component type is
1551 -- also an array. In the latter case, the expression may have
1552 -- component associations that provide different bounds from
1553 -- those of the component type, and sliding must occur. Instead
1554 -- of decomposing the current aggregate assignment, force the
1555 -- reanalysis of the assignment, so that a temporary will be
1556 -- generated in the usual fashion, and sliding will take place.
1558 if Nkind
(Parent
(N
)) = N_Assignment_Statement
1559 and then Is_Array_Type
(Comp_Typ
)
1560 and then Present
(Component_Associations
(Expr_Q
))
1561 and then Must_Slide
(Comp_Typ
, Etype
(Expr_Q
))
1563 Set_Expansion_Delayed
(Expr_Q
, False);
1564 Set_Analyzed
(Expr_Q
, False);
1569 Late_Expansion
(Expr_Q
, Etype
(Expr_Q
), Indexed_Comp
));
1574 if Present
(Expr
) then
1576 -- Handle an initialization expression of a controlled type in
1577 -- case it denotes a function call. In general such a scenario
1578 -- will produce a transient scope, but this will lead to wrong
1579 -- order of initialization, adjustment, and finalization in the
1580 -- context of aggregates.
1582 -- Target (1) := Ctrl_Func_Call;
1585 -- Trans_Obj : ... := Ctrl_Func_Call; -- object
1586 -- Target (1) := Trans_Obj;
1587 -- Finalize (Trans_Obj);
1589 -- Target (1)._tag := ...;
1590 -- Adjust (Target (1));
1592 -- In the example above, the call to Finalize occurs too early
1593 -- and as a result it may leave the array component in a bad
1594 -- state. Finalization of the transient object should really
1595 -- happen after adjustment.
1597 -- To avoid this scenario, perform in-place side-effect removal
1598 -- of the function call. This eliminates the transient property
1599 -- of the function result and ensures correct order of actions.
1601 -- Res : ... := Ctrl_Func_Call;
1602 -- Target (1) := Res;
1603 -- Target (1)._tag := ...;
1604 -- Adjust (Target (1));
1607 if Present
(Comp_Typ
)
1608 and then Needs_Finalization
(Comp_Typ
)
1609 and then Nkind
(Expr
) /= N_Aggregate
1611 Initialize_Ctrl_Array_Component
1612 (Arr_Comp
=> Indexed_Comp
,
1613 Comp_Typ
=> Comp_Typ
,
1617 -- Otherwise perform simple component initialization
1620 Initialize_Array_Component
1621 (Arr_Comp
=> Indexed_Comp
,
1622 Comp_Typ
=> Comp_Typ
,
1627 -- Ada 2005 (AI-287): In case of default initialized component, call
1628 -- the initialization subprogram associated with the component type.
1629 -- If the component type is an access type, add an explicit null
1630 -- assignment, because for the back-end there is an initialization
1631 -- present for the whole aggregate, and no default initialization
1634 -- In addition, if the component type is controlled, we must call
1635 -- its Initialize procedure explicitly, because there is no explicit
1636 -- object creation that will invoke it otherwise.
1639 if Present
(Base_Init_Proc
(Base_Type
(Ctype
)))
1640 or else Has_Task
(Base_Type
(Ctype
))
1642 Append_List_To
(Stmts
,
1643 Build_Initialization_Call
(Loc
,
1644 Id_Ref
=> Indexed_Comp
,
1646 With_Default_Init
=> True));
1648 -- If the component type has invariants, add an invariant
1649 -- check after the component is default-initialized. It will
1650 -- be analyzed and resolved before the code for initialization
1651 -- of other components.
1653 if Has_Invariants
(Ctype
) then
1654 Set_Etype
(Indexed_Comp
, Ctype
);
1655 Append_To
(Stmts
, Make_Invariant_Call
(Indexed_Comp
));
1658 elsif Is_Access_Type
(Ctype
) then
1660 Make_Assignment_Statement
(Loc
,
1661 Name
=> New_Copy_Tree
(Indexed_Comp
),
1662 Expression
=> Make_Null
(Loc
)));
1665 if Needs_Finalization
(Ctype
) then
1668 (Obj_Ref
=> New_Copy_Tree
(Indexed_Comp
),
1671 -- Guard against a missing [Deep_]Initialize when the component
1672 -- type was not properly frozen.
1674 if Present
(Init_Call
) then
1675 Append_To
(Stmts
, Init_Call
);
1680 return Add_Loop_Actions
(Stmts
);
1687 function Gen_Loop
(L
, H
: Node_Id
; Expr
: Node_Id
) return List_Id
is
1688 Is_Iterated_Component
: constant Boolean :=
1689 Nkind
(Parent
(Expr
)) = N_Iterated_Component_Association
;
1700 -- Index_Base'(L) .. Index_Base'(H)
1702 L_Iteration_Scheme
: Node_Id
;
1703 -- L_J in Index_Base'(L) .. Index_Base'(H)
1706 -- The statements to execute in the loop
1708 S
: constant List_Id
:= New_List
;
1709 -- List of statements
1712 -- Copy of expression tree, used for checking purposes
1715 -- If loop bounds define an empty range return the null statement
1717 if Empty_Range
(L
, H
) then
1718 Append_To
(S
, Make_Null_Statement
(Loc
));
1720 -- Ada 2005 (AI-287): Nothing else need to be done in case of
1721 -- default initialized component.
1727 -- The expression must be type-checked even though no component
1728 -- of the aggregate will have this value. This is done only for
1729 -- actual components of the array, not for subaggregates. Do
1730 -- the check on a copy, because the expression may be shared
1731 -- among several choices, some of which might be non-null.
1733 if Present
(Etype
(N
))
1734 and then Is_Array_Type
(Etype
(N
))
1735 and then No
(Next_Index
(Index
))
1737 Expander_Mode_Save_And_Set
(False);
1738 Tcopy
:= New_Copy_Tree
(Expr
);
1739 Set_Parent
(Tcopy
, N
);
1740 Analyze_And_Resolve
(Tcopy
, Component_Type
(Etype
(N
)));
1741 Expander_Mode_Restore
;
1747 -- If loop bounds are the same then generate an assignment, unless
1748 -- the parent construct is an Iterated_Component_Association.
1750 elsif Equal
(L
, H
) and then not Is_Iterated_Component
then
1751 return Gen_Assign
(New_Copy_Tree
(L
), Expr
);
1753 -- If H - L <= 2 then generate a sequence of assignments when we are
1754 -- processing the bottom most aggregate and it contains scalar
1757 elsif No
(Next_Index
(Index
))
1758 and then Scalar_Comp
1759 and then Local_Compile_Time_Known_Value
(L
)
1760 and then Local_Compile_Time_Known_Value
(H
)
1761 and then Local_Expr_Value
(H
) - Local_Expr_Value
(L
) <= 2
1762 and then not Is_Iterated_Component
1764 Append_List_To
(S
, Gen_Assign
(New_Copy_Tree
(L
), Expr
));
1765 Append_List_To
(S
, Gen_Assign
(Add
(1, To
=> L
), Expr
));
1767 if Local_Expr_Value
(H
) - Local_Expr_Value
(L
) = 2 then
1768 Append_List_To
(S
, Gen_Assign
(Add
(2, To
=> L
), Expr
));
1774 -- Otherwise construct the loop, starting with the loop index L_J
1776 if Is_Iterated_Component
then
1778 Make_Defining_Identifier
(Loc
,
1779 Chars
=> (Chars
(Defining_Identifier
(Parent
(Expr
)))));
1782 L_J
:= Make_Temporary
(Loc
, 'J', L
);
1785 -- Construct "L .. H" in Index_Base. We use a qualified expression
1786 -- for the bound to convert to the index base, but we don't need
1787 -- to do that if we already have the base type at hand.
1789 if Etype
(L
) = Index_Base
then
1793 Make_Qualified_Expression
(Loc
,
1794 Subtype_Mark
=> Index_Base_Name
,
1795 Expression
=> New_Copy_Tree
(L
));
1798 if Etype
(H
) = Index_Base
then
1802 Make_Qualified_Expression
(Loc
,
1803 Subtype_Mark
=> Index_Base_Name
,
1804 Expression
=> New_Copy_Tree
(H
));
1812 -- Construct "for L_J in Index_Base range L .. H"
1814 L_Iteration_Scheme
:=
1815 Make_Iteration_Scheme
1817 Loop_Parameter_Specification
=>
1818 Make_Loop_Parameter_Specification
1820 Defining_Identifier
=> L_J
,
1821 Discrete_Subtype_Definition
=> L_Range
));
1823 -- Construct the statements to execute in the loop body
1826 Gen_Assign
(New_Occurrence_Of
(L_J
, Loc
), Expr
, In_Loop
=> True);
1828 -- Construct the final loop
1831 Make_Implicit_Loop_Statement
1833 Identifier
=> Empty
,
1834 Iteration_Scheme
=> L_Iteration_Scheme
,
1835 Statements
=> L_Body
));
1837 -- A small optimization: if the aggregate is initialized with a box
1838 -- and the component type has no initialization procedure, remove the
1839 -- useless empty loop.
1841 if Nkind
(First
(S
)) = N_Loop_Statement
1842 and then Is_Empty_List
(Statements
(First
(S
)))
1844 return New_List
(Make_Null_Statement
(Loc
));
1854 -- The code built is
1856 -- W_J : Index_Base := L;
1857 -- while W_J < H loop
1858 -- W_J := Index_Base'Succ (W);
1862 function Gen_While
(L
, H
: Node_Id
; Expr
: Node_Id
) return List_Id
is
1866 -- W_J : Base_Type := L;
1868 W_Iteration_Scheme
: Node_Id
;
1871 W_Index_Succ
: Node_Id
;
1872 -- Index_Base'Succ (J)
1874 W_Increment
: Node_Id
;
1875 -- W_J := Index_Base'Succ (W)
1877 W_Body
: constant List_Id
:= New_List
;
1878 -- The statements to execute in the loop
1880 S
: constant List_Id
:= New_List
;
1881 -- list of statement
1884 -- If loop bounds define an empty range or are equal return null
1886 if Empty_Range
(L
, H
) or else Equal
(L
, H
) then
1887 Append_To
(S
, Make_Null_Statement
(Loc
));
1891 -- Build the decl of W_J
1893 W_J
:= Make_Temporary
(Loc
, 'J', L
);
1895 Make_Object_Declaration
1897 Defining_Identifier
=> W_J
,
1898 Object_Definition
=> Index_Base_Name
,
1901 -- Theoretically we should do a New_Copy_Tree (L) here, but we know
1902 -- that in this particular case L is a fresh Expr generated by
1903 -- Add which we are the only ones to use.
1905 Append_To
(S
, W_Decl
);
1907 -- Construct " while W_J < H"
1909 W_Iteration_Scheme
:=
1910 Make_Iteration_Scheme
1912 Condition
=> Make_Op_Lt
1914 Left_Opnd
=> New_Occurrence_Of
(W_J
, Loc
),
1915 Right_Opnd
=> New_Copy_Tree
(H
)));
1917 -- Construct the statements to execute in the loop body
1920 Make_Attribute_Reference
1922 Prefix
=> Index_Base_Name
,
1923 Attribute_Name
=> Name_Succ
,
1924 Expressions
=> New_List
(New_Occurrence_Of
(W_J
, Loc
)));
1927 Make_OK_Assignment_Statement
1929 Name
=> New_Occurrence_Of
(W_J
, Loc
),
1930 Expression
=> W_Index_Succ
);
1932 Append_To
(W_Body
, W_Increment
);
1934 Append_List_To
(W_Body
,
1935 Gen_Assign
(New_Occurrence_Of
(W_J
, Loc
), Expr
, In_Loop
=> True));
1937 -- Construct the final loop
1940 Make_Implicit_Loop_Statement
1942 Identifier
=> Empty
,
1943 Iteration_Scheme
=> W_Iteration_Scheme
,
1944 Statements
=> W_Body
));
1949 --------------------
1950 -- Get_Assoc_Expr --
1951 --------------------
1953 function Get_Assoc_Expr
(Assoc
: Node_Id
) return Node_Id
is
1954 Typ
: constant Entity_Id
:= Base_Type
(Etype
(N
));
1957 if Box_Present
(Assoc
) then
1958 if Is_Scalar_Type
(Ctype
) then
1959 if Present
(Default_Aspect_Component_Value
(Typ
)) then
1960 return Default_Aspect_Component_Value
(Typ
);
1961 elsif Present
(Default_Aspect_Value
(Ctype
)) then
1962 return Default_Aspect_Value
(Ctype
);
1972 return Expression
(Assoc
);
1976 ---------------------
1977 -- Index_Base_Name --
1978 ---------------------
1980 function Index_Base_Name
return Node_Id
is
1982 return New_Occurrence_Of
(Index_Base
, Sloc
(N
));
1983 end Index_Base_Name
;
1985 ------------------------------------
1986 -- Local_Compile_Time_Known_Value --
1987 ------------------------------------
1989 function Local_Compile_Time_Known_Value
(E
: Node_Id
) return Boolean is
1991 return Compile_Time_Known_Value
(E
)
1993 (Nkind
(E
) = N_Attribute_Reference
1994 and then Attribute_Name
(E
) = Name_Val
1995 and then Compile_Time_Known_Value
(First
(Expressions
(E
))));
1996 end Local_Compile_Time_Known_Value
;
1998 ----------------------
1999 -- Local_Expr_Value --
2000 ----------------------
2002 function Local_Expr_Value
(E
: Node_Id
) return Uint
is
2004 if Compile_Time_Known_Value
(E
) then
2005 return Expr_Value
(E
);
2007 return Expr_Value
(First
(Expressions
(E
)));
2009 end Local_Expr_Value
;
2013 New_Code
: constant List_Id
:= New_List
;
2015 Aggr_L
: constant Node_Id
:= Low_Bound
(Aggregate_Bounds
(N
));
2016 Aggr_H
: constant Node_Id
:= High_Bound
(Aggregate_Bounds
(N
));
2017 -- The aggregate bounds of this specific subaggregate. Note that if the
2018 -- code generated by Build_Array_Aggr_Code is executed then these bounds
2019 -- are OK. Otherwise a Constraint_Error would have been raised.
2021 Aggr_Low
: constant Node_Id
:= Duplicate_Subexpr_No_Checks
(Aggr_L
);
2022 Aggr_High
: constant Node_Id
:= Duplicate_Subexpr_No_Checks
(Aggr_H
);
2023 -- After Duplicate_Subexpr these are side-effect free
2032 Nb_Choices
: Nat
:= 0;
2033 Table
: Case_Table_Type
(1 .. Number_Of_Choices
(N
));
2034 -- Used to sort all the different choice values
2037 -- Number of elements in the positional aggregate
2039 Others_Assoc
: Node_Id
:= Empty
;
2041 -- Start of processing for Build_Array_Aggr_Code
2044 -- First before we start, a special case. if we have a bit packed
2045 -- array represented as a modular type, then clear the value to
2046 -- zero first, to ensure that unused bits are properly cleared.
2051 and then Is_Bit_Packed_Array
(Typ
)
2052 and then Is_Modular_Integer_Type
(Packed_Array_Impl_Type
(Typ
))
2054 Append_To
(New_Code
,
2055 Make_Assignment_Statement
(Loc
,
2056 Name
=> New_Copy_Tree
(Into
),
2058 Unchecked_Convert_To
(Typ
,
2059 Make_Integer_Literal
(Loc
, Uint_0
))));
2062 -- If the component type contains tasks, we need to build a Master
2063 -- entity in the current scope, because it will be needed if build-
2064 -- in-place functions are called in the expanded code.
2066 if Nkind
(Parent
(N
)) = N_Object_Declaration
and then Has_Task
(Typ
) then
2067 Build_Master_Entity
(Defining_Identifier
(Parent
(N
)));
2070 -- STEP 1: Process component associations
2072 -- For those associations that may generate a loop, initialize
2073 -- Loop_Actions to collect inserted actions that may be crated.
2075 -- Skip this if no component associations
2077 if No
(Expressions
(N
)) then
2079 -- STEP 1 (a): Sort the discrete choices
2081 Assoc
:= First
(Component_Associations
(N
));
2082 while Present
(Assoc
) loop
2083 Choice
:= First
(Choice_List
(Assoc
));
2084 while Present
(Choice
) loop
2085 if Nkind
(Choice
) = N_Others_Choice
then
2086 Set_Loop_Actions
(Assoc
, New_List
);
2087 Others_Assoc
:= Assoc
;
2091 Get_Index_Bounds
(Choice
, Low
, High
);
2094 Set_Loop_Actions
(Assoc
, New_List
);
2097 Nb_Choices
:= Nb_Choices
+ 1;
2099 Table
(Nb_Choices
) :=
2102 Choice_Node
=> Get_Assoc_Expr
(Assoc
));
2110 -- If there is more than one set of choices these must be static
2111 -- and we can therefore sort them. Remember that Nb_Choices does not
2112 -- account for an others choice.
2114 if Nb_Choices
> 1 then
2115 Sort_Case_Table
(Table
);
2118 -- STEP 1 (b): take care of the whole set of discrete choices
2120 for J
in 1 .. Nb_Choices
loop
2121 Low
:= Table
(J
).Choice_Lo
;
2122 High
:= Table
(J
).Choice_Hi
;
2123 Expr
:= Table
(J
).Choice_Node
;
2124 Append_List
(Gen_Loop
(Low
, High
, Expr
), To
=> New_Code
);
2127 -- STEP 1 (c): generate the remaining loops to cover others choice
2128 -- We don't need to generate loops over empty gaps, but if there is
2129 -- a single empty range we must analyze the expression for semantics
2131 if Present
(Others_Assoc
) then
2133 First
: Boolean := True;
2136 for J
in 0 .. Nb_Choices
loop
2140 Low
:= Add
(1, To
=> Table
(J
).Choice_Hi
);
2143 if J
= Nb_Choices
then
2146 High
:= Add
(-1, To
=> Table
(J
+ 1).Choice_Lo
);
2149 -- If this is an expansion within an init proc, make
2150 -- sure that discriminant references are replaced by
2151 -- the corresponding discriminal.
2153 if Inside_Init_Proc
then
2154 if Is_Entity_Name
(Low
)
2155 and then Ekind
(Entity
(Low
)) = E_Discriminant
2157 Set_Entity
(Low
, Discriminal
(Entity
(Low
)));
2160 if Is_Entity_Name
(High
)
2161 and then Ekind
(Entity
(High
)) = E_Discriminant
2163 Set_Entity
(High
, Discriminal
(Entity
(High
)));
2168 or else not Empty_Range
(Low
, High
)
2172 (Gen_Loop
(Low
, High
,
2173 Get_Assoc_Expr
(Others_Assoc
)), To
=> New_Code
);
2179 -- STEP 2: Process positional components
2182 -- STEP 2 (a): Generate the assignments for each positional element
2183 -- Note that here we have to use Aggr_L rather than Aggr_Low because
2184 -- Aggr_L is analyzed and Add wants an analyzed expression.
2186 Expr
:= First
(Expressions
(N
));
2188 while Present
(Expr
) loop
2189 Nb_Elements
:= Nb_Elements
+ 1;
2190 Append_List
(Gen_Assign
(Add
(Nb_Elements
, To
=> Aggr_L
), Expr
),
2195 -- STEP 2 (b): Generate final loop if an others choice is present
2196 -- Here Nb_Elements gives the offset of the last positional element.
2198 if Present
(Component_Associations
(N
)) then
2199 Assoc
:= Last
(Component_Associations
(N
));
2201 -- Ada 2005 (AI-287)
2203 Append_List
(Gen_While
(Add
(Nb_Elements
, To
=> Aggr_L
),
2205 Get_Assoc_Expr
(Assoc
)), -- AI-287
2211 end Build_Array_Aggr_Code
;
2213 ----------------------------
2214 -- Build_Record_Aggr_Code --
2215 ----------------------------
2217 function Build_Record_Aggr_Code
2220 Lhs
: Node_Id
) return List_Id
2222 Loc
: constant Source_Ptr
:= Sloc
(N
);
2223 L
: constant List_Id
:= New_List
;
2224 N_Typ
: constant Entity_Id
:= Etype
(N
);
2230 Comp_Type
: Entity_Id
;
2231 Selector
: Entity_Id
;
2232 Comp_Expr
: Node_Id
;
2235 -- If this is an internal aggregate, the External_Final_List is an
2236 -- expression for the controller record of the enclosing type.
2238 -- If the current aggregate has several controlled components, this
2239 -- expression will appear in several calls to attach to the finali-
2240 -- zation list, and it must not be shared.
2242 Ancestor_Is_Expression
: Boolean := False;
2243 Ancestor_Is_Subtype_Mark
: Boolean := False;
2245 Init_Typ
: Entity_Id
:= Empty
;
2247 Finalization_Done
: Boolean := False;
2248 -- True if Generate_Finalization_Actions has already been called; calls
2249 -- after the first do nothing.
2251 function Ancestor_Discriminant_Value
(Disc
: Entity_Id
) return Node_Id
;
2252 -- Returns the value that the given discriminant of an ancestor type
2253 -- should receive (in the absence of a conflict with the value provided
2254 -- by an ancestor part of an extension aggregate).
2256 procedure Check_Ancestor_Discriminants
(Anc_Typ
: Entity_Id
);
2257 -- Check that each of the discriminant values defined by the ancestor
2258 -- part of an extension aggregate match the corresponding values
2259 -- provided by either an association of the aggregate or by the
2260 -- constraint imposed by a parent type (RM95-4.3.2(8)).
2262 function Compatible_Int_Bounds
2263 (Agg_Bounds
: Node_Id
;
2264 Typ_Bounds
: Node_Id
) return Boolean;
2265 -- Return true if Agg_Bounds are equal or within Typ_Bounds. It is
2266 -- assumed that both bounds are integer ranges.
2268 procedure Generate_Finalization_Actions
;
2269 -- Deal with the various controlled type data structure initializations
2270 -- (but only if it hasn't been done already).
2272 function Get_Constraint_Association
(T
: Entity_Id
) return Node_Id
;
2273 -- Returns the first discriminant association in the constraint
2274 -- associated with T, if any, otherwise returns Empty.
2276 function Get_Explicit_Discriminant_Value
(D
: Entity_Id
) return Node_Id
;
2277 -- If the ancestor part is an unconstrained type and further ancestors
2278 -- do not provide discriminants for it, check aggregate components for
2279 -- values of the discriminants.
2281 procedure Init_Hidden_Discriminants
(Typ
: Entity_Id
; List
: List_Id
);
2282 -- If Typ is derived, and constrains discriminants of the parent type,
2283 -- these discriminants are not components of the aggregate, and must be
2284 -- initialized. The assignments are appended to List. The same is done
2285 -- if Typ derives fron an already constrained subtype of a discriminated
2288 procedure Init_Stored_Discriminants
;
2289 -- If the type is derived and has inherited discriminants, generate
2290 -- explicit assignments for each, using the store constraint of the
2291 -- type. Note that both visible and stored discriminants must be
2292 -- initialized in case the derived type has some renamed and some
2293 -- constrained discriminants.
2295 procedure Init_Visible_Discriminants
;
2296 -- If type has discriminants, retrieve their values from aggregate,
2297 -- and generate explicit assignments for each. This does not include
2298 -- discriminants inherited from ancestor, which are handled above.
2299 -- The type of the aggregate is a subtype created ealier using the
2300 -- given values of the discriminant components of the aggregate.
2302 procedure Initialize_Ctrl_Record_Component
2303 (Rec_Comp
: Node_Id
;
2304 Comp_Typ
: Entity_Id
;
2305 Init_Expr
: Node_Id
;
2307 -- Perform the initialization of controlled record component Rec_Comp.
2308 -- Comp_Typ is the component type. Init_Expr is the initialization
2309 -- expression for the record component. Hook-related declarations are
2310 -- inserted prior to aggregate N using Insert_Action. All remaining
2311 -- generated code is added to list Stmts.
2313 procedure Initialize_Record_Component
2314 (Rec_Comp
: Node_Id
;
2315 Comp_Typ
: Entity_Id
;
2316 Init_Expr
: Node_Id
;
2318 -- Perform the initialization of record component Rec_Comp. Comp_Typ
2319 -- is the component type. Init_Expr is the initialization expression
2320 -- of the record component. All generated code is added to list Stmts.
2322 function Is_Int_Range_Bounds
(Bounds
: Node_Id
) return Boolean;
2323 -- Check whether Bounds is a range node and its lower and higher bounds
2324 -- are integers literals.
2326 function Replace_Type
(Expr
: Node_Id
) return Traverse_Result
;
2327 -- If the aggregate contains a self-reference, traverse each expression
2328 -- to replace a possible self-reference with a reference to the proper
2329 -- component of the target of the assignment.
2331 function Rewrite_Discriminant
(Expr
: Node_Id
) return Traverse_Result
;
2332 -- If default expression of a component mentions a discriminant of the
2333 -- type, it must be rewritten as the discriminant of the target object.
2335 ---------------------------------
2336 -- Ancestor_Discriminant_Value --
2337 ---------------------------------
2339 function Ancestor_Discriminant_Value
(Disc
: Entity_Id
) return Node_Id
is
2341 Assoc_Elmt
: Elmt_Id
;
2342 Aggr_Comp
: Entity_Id
;
2343 Corresp_Disc
: Entity_Id
;
2344 Current_Typ
: Entity_Id
:= Base_Type
(Typ
);
2345 Parent_Typ
: Entity_Id
;
2346 Parent_Disc
: Entity_Id
;
2347 Save_Assoc
: Node_Id
:= Empty
;
2350 -- First check any discriminant associations to see if any of them
2351 -- provide a value for the discriminant.
2353 if Present
(Discriminant_Specifications
(Parent
(Current_Typ
))) then
2354 Assoc
:= First
(Component_Associations
(N
));
2355 while Present
(Assoc
) loop
2356 Aggr_Comp
:= Entity
(First
(Choices
(Assoc
)));
2358 if Ekind
(Aggr_Comp
) = E_Discriminant
then
2359 Save_Assoc
:= Expression
(Assoc
);
2361 Corresp_Disc
:= Corresponding_Discriminant
(Aggr_Comp
);
2362 while Present
(Corresp_Disc
) loop
2364 -- If found a corresponding discriminant then return the
2365 -- value given in the aggregate. (Note: this is not
2366 -- correct in the presence of side effects. ???)
2368 if Disc
= Corresp_Disc
then
2369 return Duplicate_Subexpr
(Expression
(Assoc
));
2372 Corresp_Disc
:= Corresponding_Discriminant
(Corresp_Disc
);
2380 -- No match found in aggregate, so chain up parent types to find
2381 -- a constraint that defines the value of the discriminant.
2383 Parent_Typ
:= Etype
(Current_Typ
);
2384 while Current_Typ
/= Parent_Typ
loop
2385 if Has_Discriminants
(Parent_Typ
)
2386 and then not Has_Unknown_Discriminants
(Parent_Typ
)
2388 Parent_Disc
:= First_Discriminant
(Parent_Typ
);
2390 -- We either get the association from the subtype indication
2391 -- of the type definition itself, or from the discriminant
2392 -- constraint associated with the type entity (which is
2393 -- preferable, but it's not always present ???)
2395 if Is_Empty_Elmt_List
(Discriminant_Constraint
(Current_Typ
))
2397 Assoc
:= Get_Constraint_Association
(Current_Typ
);
2398 Assoc_Elmt
:= No_Elmt
;
2401 First_Elmt
(Discriminant_Constraint
(Current_Typ
));
2402 Assoc
:= Node
(Assoc_Elmt
);
2405 -- Traverse the discriminants of the parent type looking
2406 -- for one that corresponds.
2408 while Present
(Parent_Disc
) and then Present
(Assoc
) loop
2409 Corresp_Disc
:= Parent_Disc
;
2410 while Present
(Corresp_Disc
)
2411 and then Disc
/= Corresp_Disc
2413 Corresp_Disc
:= Corresponding_Discriminant
(Corresp_Disc
);
2416 if Disc
= Corresp_Disc
then
2417 if Nkind
(Assoc
) = N_Discriminant_Association
then
2418 Assoc
:= Expression
(Assoc
);
2421 -- If the located association directly denotes
2422 -- a discriminant, then use the value of a saved
2423 -- association of the aggregate. This is an approach
2424 -- used to handle certain cases involving multiple
2425 -- discriminants mapped to a single discriminant of
2426 -- a descendant. It's not clear how to locate the
2427 -- appropriate discriminant value for such cases. ???
2429 if Is_Entity_Name
(Assoc
)
2430 and then Ekind
(Entity
(Assoc
)) = E_Discriminant
2432 Assoc
:= Save_Assoc
;
2435 return Duplicate_Subexpr
(Assoc
);
2438 Next_Discriminant
(Parent_Disc
);
2440 if No
(Assoc_Elmt
) then
2444 Next_Elmt
(Assoc_Elmt
);
2446 if Present
(Assoc_Elmt
) then
2447 Assoc
:= Node
(Assoc_Elmt
);
2455 Current_Typ
:= Parent_Typ
;
2456 Parent_Typ
:= Etype
(Current_Typ
);
2459 -- In some cases there's no ancestor value to locate (such as
2460 -- when an ancestor part given by an expression defines the
2461 -- discriminant value).
2464 end Ancestor_Discriminant_Value
;
2466 ----------------------------------
2467 -- Check_Ancestor_Discriminants --
2468 ----------------------------------
2470 procedure Check_Ancestor_Discriminants
(Anc_Typ
: Entity_Id
) is
2472 Disc_Value
: Node_Id
;
2476 Discr
:= First_Discriminant
(Base_Type
(Anc_Typ
));
2477 while Present
(Discr
) loop
2478 Disc_Value
:= Ancestor_Discriminant_Value
(Discr
);
2480 if Present
(Disc_Value
) then
2481 Cond
:= Make_Op_Ne
(Loc
,
2483 Make_Selected_Component
(Loc
,
2484 Prefix
=> New_Copy_Tree
(Target
),
2485 Selector_Name
=> New_Occurrence_Of
(Discr
, Loc
)),
2486 Right_Opnd
=> Disc_Value
);
2489 Make_Raise_Constraint_Error
(Loc
,
2491 Reason
=> CE_Discriminant_Check_Failed
));
2494 Next_Discriminant
(Discr
);
2496 end Check_Ancestor_Discriminants
;
2498 ---------------------------
2499 -- Compatible_Int_Bounds --
2500 ---------------------------
2502 function Compatible_Int_Bounds
2503 (Agg_Bounds
: Node_Id
;
2504 Typ_Bounds
: Node_Id
) return Boolean
2506 Agg_Lo
: constant Uint
:= Intval
(Low_Bound
(Agg_Bounds
));
2507 Agg_Hi
: constant Uint
:= Intval
(High_Bound
(Agg_Bounds
));
2508 Typ_Lo
: constant Uint
:= Intval
(Low_Bound
(Typ_Bounds
));
2509 Typ_Hi
: constant Uint
:= Intval
(High_Bound
(Typ_Bounds
));
2511 return Typ_Lo
<= Agg_Lo
and then Agg_Hi
<= Typ_Hi
;
2512 end Compatible_Int_Bounds
;
2514 -----------------------------------
2515 -- Generate_Finalization_Actions --
2516 -----------------------------------
2518 procedure Generate_Finalization_Actions
is
2520 -- Do the work only the first time this is called
2522 if Finalization_Done
then
2526 Finalization_Done
:= True;
2528 -- Determine the external finalization list. It is either the
2529 -- finalization list of the outer scope or the one coming from an
2530 -- outer aggregate. When the target is not a temporary, the proper
2531 -- scope is the scope of the target rather than the potentially
2532 -- transient current scope.
2534 if Is_Controlled
(Typ
) and then Ancestor_Is_Subtype_Mark
then
2535 Ref
:= Convert_To
(Init_Typ
, New_Copy_Tree
(Target
));
2536 Set_Assignment_OK
(Ref
);
2539 Make_Procedure_Call_Statement
(Loc
,
2542 (Find_Prim_Op
(Init_Typ
, Name_Initialize
), Loc
),
2543 Parameter_Associations
=> New_List
(New_Copy_Tree
(Ref
))));
2545 end Generate_Finalization_Actions
;
2547 --------------------------------
2548 -- Get_Constraint_Association --
2549 --------------------------------
2551 function Get_Constraint_Association
(T
: Entity_Id
) return Node_Id
is
2558 -- If type is private, get constraint from full view. This was
2559 -- previously done in an instance context, but is needed whenever
2560 -- the ancestor part has a discriminant, possibly inherited through
2561 -- multiple derivations.
2563 if Is_Private_Type
(Typ
) and then Present
(Full_View
(Typ
)) then
2564 Typ
:= Full_View
(Typ
);
2567 Indic
:= Subtype_Indication
(Type_Definition
(Parent
(Typ
)));
2569 -- Verify that the subtype indication carries a constraint
2571 if Nkind
(Indic
) = N_Subtype_Indication
2572 and then Present
(Constraint
(Indic
))
2574 return First
(Constraints
(Constraint
(Indic
)));
2578 end Get_Constraint_Association
;
2580 -------------------------------------
2581 -- Get_Explicit_Discriminant_Value --
2582 -------------------------------------
2584 function Get_Explicit_Discriminant_Value
2585 (D
: Entity_Id
) return Node_Id
2592 -- The aggregate has been normalized and all associations have a
2595 Assoc
:= First
(Component_Associations
(N
));
2596 while Present
(Assoc
) loop
2597 Choice
:= First
(Choices
(Assoc
));
2599 if Chars
(Choice
) = Chars
(D
) then
2600 Val
:= Expression
(Assoc
);
2609 end Get_Explicit_Discriminant_Value
;
2611 -------------------------------
2612 -- Init_Hidden_Discriminants --
2613 -------------------------------
2615 procedure Init_Hidden_Discriminants
(Typ
: Entity_Id
; List
: List_Id
) is
2616 function Is_Completely_Hidden_Discriminant
2617 (Discr
: Entity_Id
) return Boolean;
2618 -- Determine whether Discr is a completely hidden discriminant of
2621 ---------------------------------------
2622 -- Is_Completely_Hidden_Discriminant --
2623 ---------------------------------------
2625 function Is_Completely_Hidden_Discriminant
2626 (Discr
: Entity_Id
) return Boolean
2631 -- Use First/Next_Entity as First/Next_Discriminant do not yield
2632 -- completely hidden discriminants.
2634 Item
:= First_Entity
(Typ
);
2635 while Present
(Item
) loop
2636 if Ekind
(Item
) = E_Discriminant
2637 and then Is_Completely_Hidden
(Item
)
2638 and then Chars
(Original_Record_Component
(Item
)) =
2648 end Is_Completely_Hidden_Discriminant
;
2652 Base_Typ
: Entity_Id
;
2654 Discr_Constr
: Elmt_Id
;
2655 Discr_Init
: Node_Id
;
2656 Discr_Val
: Node_Id
;
2657 In_Aggr_Type
: Boolean;
2658 Par_Typ
: Entity_Id
;
2660 -- Start of processing for Init_Hidden_Discriminants
2663 -- The constraints on the hidden discriminants, if present, are kept
2664 -- in the Stored_Constraint list of the type itself, or in that of
2665 -- the base type. If not in the constraints of the aggregate itself,
2666 -- we examine ancestors to find discriminants that are not renamed
2667 -- by other discriminants but constrained explicitly.
2669 In_Aggr_Type
:= True;
2671 Base_Typ
:= Base_Type
(Typ
);
2672 while Is_Derived_Type
(Base_Typ
)
2674 (Present
(Stored_Constraint
(Base_Typ
))
2676 (In_Aggr_Type
and then Present
(Stored_Constraint
(Typ
))))
2678 Par_Typ
:= Etype
(Base_Typ
);
2680 if not Has_Discriminants
(Par_Typ
) then
2684 Discr
:= First_Discriminant
(Par_Typ
);
2686 -- We know that one of the stored-constraint lists is present
2688 if Present
(Stored_Constraint
(Base_Typ
)) then
2689 Discr_Constr
:= First_Elmt
(Stored_Constraint
(Base_Typ
));
2691 -- For private extension, stored constraint may be on full view
2693 elsif Is_Private_Type
(Base_Typ
)
2694 and then Present
(Full_View
(Base_Typ
))
2695 and then Present
(Stored_Constraint
(Full_View
(Base_Typ
)))
2698 First_Elmt
(Stored_Constraint
(Full_View
(Base_Typ
)));
2701 Discr_Constr
:= First_Elmt
(Stored_Constraint
(Typ
));
2704 while Present
(Discr
) and then Present
(Discr_Constr
) loop
2705 Discr_Val
:= Node
(Discr_Constr
);
2707 -- The parent discriminant is renamed in the derived type,
2708 -- nothing to initialize.
2710 -- type Deriv_Typ (Discr : ...)
2711 -- is new Parent_Typ (Discr => Discr);
2713 if Is_Entity_Name
(Discr_Val
)
2714 and then Ekind
(Entity
(Discr_Val
)) = E_Discriminant
2718 -- When the parent discriminant is constrained at the type
2719 -- extension level, it does not appear in the derived type.
2721 -- type Deriv_Typ (Discr : ...)
2722 -- is new Parent_Typ (Discr => Discr,
2723 -- Hidden_Discr => Expression);
2725 elsif Is_Completely_Hidden_Discriminant
(Discr
) then
2728 -- Otherwise initialize the discriminant
2732 Make_OK_Assignment_Statement
(Loc
,
2734 Make_Selected_Component
(Loc
,
2735 Prefix
=> New_Copy_Tree
(Target
),
2736 Selector_Name
=> New_Occurrence_Of
(Discr
, Loc
)),
2737 Expression
=> New_Copy_Tree
(Discr_Val
));
2739 Append_To
(List
, Discr_Init
);
2742 Next_Elmt
(Discr_Constr
);
2743 Next_Discriminant
(Discr
);
2746 In_Aggr_Type
:= False;
2747 Base_Typ
:= Base_Type
(Par_Typ
);
2749 end Init_Hidden_Discriminants
;
2751 --------------------------------
2752 -- Init_Visible_Discriminants --
2753 --------------------------------
2755 procedure Init_Visible_Discriminants
is
2756 Discriminant
: Entity_Id
;
2757 Discriminant_Value
: Node_Id
;
2760 Discriminant
:= First_Discriminant
(Typ
);
2761 while Present
(Discriminant
) loop
2763 Make_Selected_Component
(Loc
,
2764 Prefix
=> New_Copy_Tree
(Target
),
2765 Selector_Name
=> New_Occurrence_Of
(Discriminant
, Loc
));
2767 Discriminant_Value
:=
2768 Get_Discriminant_Value
2769 (Discriminant
, Typ
, Discriminant_Constraint
(N_Typ
));
2772 Make_OK_Assignment_Statement
(Loc
,
2774 Expression
=> New_Copy_Tree
(Discriminant_Value
));
2776 Append_To
(L
, Instr
);
2778 Next_Discriminant
(Discriminant
);
2780 end Init_Visible_Discriminants
;
2782 -------------------------------
2783 -- Init_Stored_Discriminants --
2784 -------------------------------
2786 procedure Init_Stored_Discriminants
is
2787 Discriminant
: Entity_Id
;
2788 Discriminant_Value
: Node_Id
;
2791 Discriminant
:= First_Stored_Discriminant
(Typ
);
2792 while Present
(Discriminant
) loop
2794 Make_Selected_Component
(Loc
,
2795 Prefix
=> New_Copy_Tree
(Target
),
2796 Selector_Name
=> New_Occurrence_Of
(Discriminant
, Loc
));
2798 Discriminant_Value
:=
2799 Get_Discriminant_Value
2800 (Discriminant
, N_Typ
, Discriminant_Constraint
(N_Typ
));
2803 Make_OK_Assignment_Statement
(Loc
,
2805 Expression
=> New_Copy_Tree
(Discriminant_Value
));
2807 Append_To
(L
, Instr
);
2809 Next_Stored_Discriminant
(Discriminant
);
2811 end Init_Stored_Discriminants
;
2813 --------------------------------------
2814 -- Initialize_Ctrl_Record_Component --
2815 --------------------------------------
2817 procedure Initialize_Ctrl_Record_Component
2818 (Rec_Comp
: Node_Id
;
2819 Comp_Typ
: Entity_Id
;
2820 Init_Expr
: Node_Id
;
2824 Hook_Clear
: Node_Id
;
2826 In_Place_Expansion
: Boolean;
2827 -- Flag set when a nonlimited controlled function call requires
2828 -- in-place expansion.
2831 -- Perform a preliminary analysis and resolution to determine what
2832 -- the initialization expression denotes. Unanalyzed function calls
2833 -- may appear as identifiers or indexed components.
2835 if Nkind_In
(Init_Expr
, N_Function_Call
,
2837 N_Indexed_Component
)
2838 and then not Analyzed
(Init_Expr
)
2840 Preanalyze_And_Resolve
(Init_Expr
, Comp_Typ
);
2843 In_Place_Expansion
:=
2844 Nkind
(Init_Expr
) = N_Function_Call
2845 and then not Is_Build_In_Place_Result_Type
(Comp_Typ
);
2847 -- The initialization expression is a controlled function call.
2848 -- Perform in-place removal of side effects to avoid creating a
2851 -- This in-place expansion is not performed for limited transient
2852 -- objects because the initialization is already done in place.
2854 if In_Place_Expansion
then
2856 -- Suppress the removal of side effects by general analysis
2857 -- because this behavior is emulated here. This avoids the
2858 -- generation of a transient scope, which leads to out-of-order
2859 -- adjustment and finalization.
2861 Set_No_Side_Effect_Removal
(Init_Expr
);
2863 -- Install all hook-related declarations and prepare the clean up
2864 -- statements. The generated code follows the initialization order
2865 -- of individual components and discriminants, rather than being
2866 -- inserted prior to the aggregate. This ensures that a transient
2867 -- component which mentions a discriminant has proper visibility
2868 -- of the discriminant.
2870 Process_Transient_Component
2872 Comp_Typ
=> Comp_Typ
,
2873 Init_Expr
=> Init_Expr
,
2874 Fin_Call
=> Fin_Call
,
2875 Hook_Clear
=> Hook_Clear
,
2879 -- Use the noncontrolled component initialization circuitry to
2880 -- assign the result of the function call to the record component.
2881 -- This also performs tag adjustment and [deep] adjustment of the
2882 -- record component.
2884 Initialize_Record_Component
2885 (Rec_Comp
=> Rec_Comp
,
2886 Comp_Typ
=> Comp_Typ
,
2887 Init_Expr
=> Init_Expr
,
2890 -- At this point the record component is fully initialized. Complete
2891 -- the processing of the controlled record component by finalizing
2892 -- the transient function result.
2894 if In_Place_Expansion
then
2895 Process_Transient_Component_Completion
2898 Fin_Call
=> Fin_Call
,
2899 Hook_Clear
=> Hook_Clear
,
2902 end Initialize_Ctrl_Record_Component
;
2904 ---------------------------------
2905 -- Initialize_Record_Component --
2906 ---------------------------------
2908 procedure Initialize_Record_Component
2909 (Rec_Comp
: Node_Id
;
2910 Comp_Typ
: Entity_Id
;
2911 Init_Expr
: Node_Id
;
2914 Exceptions_OK
: constant Boolean :=
2915 not Restriction_Active
(No_Exception_Propagation
);
2917 Finalization_OK
: constant Boolean := Needs_Finalization
(Comp_Typ
);
2919 Full_Typ
: constant Entity_Id
:= Underlying_Type
(Comp_Typ
);
2921 Blk_Stmts
: List_Id
;
2922 Init_Stmt
: Node_Id
;
2925 -- Protect the initialization statements from aborts. Generate:
2929 if Finalization_OK
and Abort_Allowed
then
2930 if Exceptions_OK
then
2931 Blk_Stmts
:= New_List
;
2936 Append_To
(Blk_Stmts
, Build_Runtime_Call
(Loc
, RE_Abort_Defer
));
2938 -- Otherwise aborts are not allowed. All generated code is added
2939 -- directly to the input list.
2945 -- Initialize the record component. Generate:
2947 -- Rec_Comp := Init_Expr;
2949 -- Note that the initialization expression is NOT replicated because
2950 -- only a single component may be initialized by it.
2953 Make_OK_Assignment_Statement
(Loc
,
2954 Name
=> New_Copy_Tree
(Rec_Comp
),
2955 Expression
=> Init_Expr
);
2956 Set_No_Ctrl_Actions
(Init_Stmt
);
2958 Append_To
(Blk_Stmts
, Init_Stmt
);
2960 -- Adjust the tag due to a possible view conversion. Generate:
2962 -- Rec_Comp._tag := Full_TypeP;
2964 if Tagged_Type_Expansion
and then Is_Tagged_Type
(Comp_Typ
) then
2965 Append_To
(Blk_Stmts
,
2966 Make_OK_Assignment_Statement
(Loc
,
2968 Make_Selected_Component
(Loc
,
2969 Prefix
=> New_Copy_Tree
(Rec_Comp
),
2972 (First_Tag_Component
(Full_Typ
), Loc
)),
2975 Unchecked_Convert_To
(RTE
(RE_Tag
),
2977 (Node
(First_Elmt
(Access_Disp_Table
(Full_Typ
))),
2981 -- Adjust the component. Generate:
2983 -- [Deep_]Adjust (Rec_Comp);
2986 and then not Is_Limited_Type
(Comp_Typ
)
2987 and then not Is_Build_In_Place_Function_Call
(Init_Expr
)
2991 (Obj_Ref
=> New_Copy_Tree
(Rec_Comp
),
2994 -- Guard against a missing [Deep_]Adjust when the component type
2995 -- was not properly frozen.
2997 if Present
(Adj_Call
) then
2998 Append_To
(Blk_Stmts
, Adj_Call
);
3002 -- Complete the protection of the initialization statements
3004 if Finalization_OK
and Abort_Allowed
then
3006 -- Wrap the initialization statements in a block to catch a
3007 -- potential exception. Generate:
3011 -- Rec_Comp := Init_Expr;
3012 -- Rec_Comp._tag := Full_TypP;
3013 -- [Deep_]Adjust (Rec_Comp);
3015 -- Abort_Undefer_Direct;
3018 if Exceptions_OK
then
3020 Build_Abort_Undefer_Block
(Loc
,
3024 -- Otherwise exceptions are not propagated. Generate:
3027 -- Rec_Comp := Init_Expr;
3028 -- Rec_Comp._tag := Full_TypP;
3029 -- [Deep_]Adjust (Rec_Comp);
3033 Append_To
(Blk_Stmts
,
3034 Build_Runtime_Call
(Loc
, RE_Abort_Undefer
));
3037 end Initialize_Record_Component
;
3039 -------------------------
3040 -- Is_Int_Range_Bounds --
3041 -------------------------
3043 function Is_Int_Range_Bounds
(Bounds
: Node_Id
) return Boolean is
3045 return Nkind
(Bounds
) = N_Range
3046 and then Nkind
(Low_Bound
(Bounds
)) = N_Integer_Literal
3047 and then Nkind
(High_Bound
(Bounds
)) = N_Integer_Literal
;
3048 end Is_Int_Range_Bounds
;
3054 function Replace_Type
(Expr
: Node_Id
) return Traverse_Result
is
3056 -- Note regarding the Root_Type test below: Aggregate components for
3057 -- self-referential types include attribute references to the current
3058 -- instance, of the form: Typ'access, etc.. These references are
3059 -- rewritten as references to the target of the aggregate: the
3060 -- left-hand side of an assignment, the entity in a declaration,
3061 -- or a temporary. Without this test, we would improperly extended
3062 -- this rewriting to attribute references whose prefix was not the
3063 -- type of the aggregate.
3065 if Nkind
(Expr
) = N_Attribute_Reference
3066 and then Is_Entity_Name
(Prefix
(Expr
))
3067 and then Is_Type
(Entity
(Prefix
(Expr
)))
3068 and then Root_Type
(Etype
(N
)) = Root_Type
(Entity
(Prefix
(Expr
)))
3070 if Is_Entity_Name
(Lhs
) then
3071 Rewrite
(Prefix
(Expr
), New_Occurrence_Of
(Entity
(Lhs
), Loc
));
3075 Make_Attribute_Reference
(Loc
,
3076 Attribute_Name
=> Name_Unrestricted_Access
,
3077 Prefix
=> New_Copy_Tree
(Lhs
)));
3078 Set_Analyzed
(Parent
(Expr
), False);
3085 --------------------------
3086 -- Rewrite_Discriminant --
3087 --------------------------
3089 function Rewrite_Discriminant
(Expr
: Node_Id
) return Traverse_Result
is
3091 if Is_Entity_Name
(Expr
)
3092 and then Present
(Entity
(Expr
))
3093 and then Ekind
(Entity
(Expr
)) = E_In_Parameter
3094 and then Present
(Discriminal_Link
(Entity
(Expr
)))
3095 and then Scope
(Discriminal_Link
(Entity
(Expr
))) =
3096 Base_Type
(Etype
(N
))
3099 Make_Selected_Component
(Loc
,
3100 Prefix
=> New_Copy_Tree
(Lhs
),
3101 Selector_Name
=> Make_Identifier
(Loc
, Chars
(Expr
))));
3105 end Rewrite_Discriminant
;
3107 procedure Replace_Discriminants
is
3108 new Traverse_Proc
(Rewrite_Discriminant
);
3110 procedure Replace_Self_Reference
is
3111 new Traverse_Proc
(Replace_Type
);
3113 -- Start of processing for Build_Record_Aggr_Code
3116 if Has_Self_Reference
(N
) then
3117 Replace_Self_Reference
(N
);
3120 -- If the target of the aggregate is class-wide, we must convert it
3121 -- to the actual type of the aggregate, so that the proper components
3122 -- are visible. We know already that the types are compatible.
3124 if Present
(Etype
(Lhs
))
3125 and then Is_Class_Wide_Type
(Etype
(Lhs
))
3127 Target
:= Unchecked_Convert_To
(Typ
, Lhs
);
3132 -- Deal with the ancestor part of extension aggregates or with the
3133 -- discriminants of the root type.
3135 if Nkind
(N
) = N_Extension_Aggregate
then
3137 Ancestor
: constant Node_Id
:= Ancestor_Part
(N
);
3142 -- If the ancestor part is a subtype mark "T", we generate
3144 -- init-proc (T (tmp)); if T is constrained and
3145 -- init-proc (S (tmp)); where S applies an appropriate
3146 -- constraint if T is unconstrained
3148 if Is_Entity_Name
(Ancestor
)
3149 and then Is_Type
(Entity
(Ancestor
))
3151 Ancestor_Is_Subtype_Mark
:= True;
3153 if Is_Constrained
(Entity
(Ancestor
)) then
3154 Init_Typ
:= Entity
(Ancestor
);
3156 -- For an ancestor part given by an unconstrained type mark,
3157 -- create a subtype constrained by appropriate corresponding
3158 -- discriminant values coming from either associations of the
3159 -- aggregate or a constraint on a parent type. The subtype will
3160 -- be used to generate the correct default value for the
3163 elsif Has_Discriminants
(Entity
(Ancestor
)) then
3165 Anc_Typ
: constant Entity_Id
:= Entity
(Ancestor
);
3166 Anc_Constr
: constant List_Id
:= New_List
;
3167 Discrim
: Entity_Id
;
3168 Disc_Value
: Node_Id
;
3169 New_Indic
: Node_Id
;
3170 Subt_Decl
: Node_Id
;
3173 Discrim
:= First_Discriminant
(Anc_Typ
);
3174 while Present
(Discrim
) loop
3175 Disc_Value
:= Ancestor_Discriminant_Value
(Discrim
);
3177 -- If no usable discriminant in ancestors, check
3178 -- whether aggregate has an explicit value for it.
3180 if No
(Disc_Value
) then
3182 Get_Explicit_Discriminant_Value
(Discrim
);
3185 Append_To
(Anc_Constr
, Disc_Value
);
3186 Next_Discriminant
(Discrim
);
3190 Make_Subtype_Indication
(Loc
,
3191 Subtype_Mark
=> New_Occurrence_Of
(Anc_Typ
, Loc
),
3193 Make_Index_Or_Discriminant_Constraint
(Loc
,
3194 Constraints
=> Anc_Constr
));
3196 Init_Typ
:= Create_Itype
(Ekind
(Anc_Typ
), N
);
3199 Make_Subtype_Declaration
(Loc
,
3200 Defining_Identifier
=> Init_Typ
,
3201 Subtype_Indication
=> New_Indic
);
3203 -- Itypes must be analyzed with checks off Declaration
3204 -- must have a parent for proper handling of subsidiary
3207 Set_Parent
(Subt_Decl
, N
);
3208 Analyze
(Subt_Decl
, Suppress
=> All_Checks
);
3212 Ref
:= Convert_To
(Init_Typ
, New_Copy_Tree
(Target
));
3213 Set_Assignment_OK
(Ref
);
3215 if not Is_Interface
(Init_Typ
) then
3217 Build_Initialization_Call
(Loc
,
3220 In_Init_Proc
=> Within_Init_Proc
,
3221 With_Default_Init
=> Has_Default_Init_Comps
(N
)
3223 Has_Task
(Base_Type
(Init_Typ
))));
3225 if Is_Constrained
(Entity
(Ancestor
))
3226 and then Has_Discriminants
(Entity
(Ancestor
))
3228 Check_Ancestor_Discriminants
(Entity
(Ancestor
));
3232 -- Handle calls to C++ constructors
3234 elsif Is_CPP_Constructor_Call
(Ancestor
) then
3235 Init_Typ
:= Etype
(Ancestor
);
3236 Ref
:= Convert_To
(Init_Typ
, New_Copy_Tree
(Target
));
3237 Set_Assignment_OK
(Ref
);
3240 Build_Initialization_Call
(Loc
,
3243 In_Init_Proc
=> Within_Init_Proc
,
3244 With_Default_Init
=> Has_Default_Init_Comps
(N
),
3245 Constructor_Ref
=> Ancestor
));
3247 -- Ada 2005 (AI-287): If the ancestor part is an aggregate of
3248 -- limited type, a recursive call expands the ancestor. Note that
3249 -- in the limited case, the ancestor part must be either a
3250 -- function call (possibly qualified) or aggregate (definitely
3253 elsif Is_Limited_Type
(Etype
(Ancestor
))
3254 and then Nkind_In
(Unqualify
(Ancestor
), N_Aggregate
,
3255 N_Extension_Aggregate
)
3257 Ancestor_Is_Expression
:= True;
3259 -- Set up finalization data for enclosing record, because
3260 -- controlled subcomponents of the ancestor part will be
3263 Generate_Finalization_Actions
;
3266 Build_Record_Aggr_Code
3267 (N
=> Unqualify
(Ancestor
),
3268 Typ
=> Etype
(Unqualify
(Ancestor
)),
3271 -- If the ancestor part is an expression "E", we generate
3275 -- In Ada 2005, this includes the case of a (possibly qualified)
3276 -- limited function call. The assignment will turn into a
3277 -- build-in-place function call (for further details, see
3278 -- Make_Build_In_Place_Call_In_Assignment).
3281 Ancestor_Is_Expression
:= True;
3282 Init_Typ
:= Etype
(Ancestor
);
3284 -- If the ancestor part is an aggregate, force its full
3285 -- expansion, which was delayed.
3287 if Nkind_In
(Unqualify
(Ancestor
), N_Aggregate
,
3288 N_Extension_Aggregate
)
3290 Set_Analyzed
(Ancestor
, False);
3291 Set_Analyzed
(Expression
(Ancestor
), False);
3294 Ref
:= Convert_To
(Init_Typ
, New_Copy_Tree
(Target
));
3295 Set_Assignment_OK
(Ref
);
3297 -- Make the assignment without usual controlled actions, since
3298 -- we only want to Adjust afterwards, but not to Finalize
3299 -- beforehand. Add manual Adjust when necessary.
3301 Assign
:= New_List
(
3302 Make_OK_Assignment_Statement
(Loc
,
3304 Expression
=> Ancestor
));
3305 Set_No_Ctrl_Actions
(First
(Assign
));
3307 -- Assign the tag now to make sure that the dispatching call in
3308 -- the subsequent deep_adjust works properly (unless
3309 -- Tagged_Type_Expansion where tags are implicit).
3311 if Tagged_Type_Expansion
then
3313 Make_OK_Assignment_Statement
(Loc
,
3315 Make_Selected_Component
(Loc
,
3316 Prefix
=> New_Copy_Tree
(Target
),
3319 (First_Tag_Component
(Base_Type
(Typ
)), Loc
)),
3322 Unchecked_Convert_To
(RTE
(RE_Tag
),
3325 (Access_Disp_Table
(Base_Type
(Typ
)))),
3328 Set_Assignment_OK
(Name
(Instr
));
3329 Append_To
(Assign
, Instr
);
3331 -- Ada 2005 (AI-251): If tagged type has progenitors we must
3332 -- also initialize tags of the secondary dispatch tables.
3334 if Has_Interfaces
(Base_Type
(Typ
)) then
3336 (Typ
=> Base_Type
(Typ
),
3338 Stmts_List
=> Assign
,
3339 Init_Tags_List
=> Assign
);
3343 -- Call Adjust manually
3345 if Needs_Finalization
(Etype
(Ancestor
))
3346 and then not Is_Limited_Type
(Etype
(Ancestor
))
3347 and then not Is_Build_In_Place_Function_Call
(Ancestor
)
3351 (Obj_Ref
=> New_Copy_Tree
(Ref
),
3352 Typ
=> Etype
(Ancestor
));
3354 -- Guard against a missing [Deep_]Adjust when the ancestor
3355 -- type was not properly frozen.
3357 if Present
(Adj_Call
) then
3358 Append_To
(Assign
, Adj_Call
);
3363 Make_Unsuppress_Block
(Loc
, Name_Discriminant_Check
, Assign
));
3365 if Has_Discriminants
(Init_Typ
) then
3366 Check_Ancestor_Discriminants
(Init_Typ
);
3370 pragma Assert
(Nkind
(N
) = N_Extension_Aggregate
);
3372 (not (Ancestor_Is_Expression
and Ancestor_Is_Subtype_Mark
));
3375 -- Generate assignments of hidden discriminants. If the base type is
3376 -- an unchecked union, the discriminants are unknown to the back-end
3377 -- and absent from a value of the type, so assignments for them are
3380 if Has_Discriminants
(Typ
)
3381 and then not Is_Unchecked_Union
(Base_Type
(Typ
))
3383 Init_Hidden_Discriminants
(Typ
, L
);
3386 -- Normal case (not an extension aggregate)
3389 -- Generate the discriminant expressions, component by component.
3390 -- If the base type is an unchecked union, the discriminants are
3391 -- unknown to the back-end and absent from a value of the type, so
3392 -- assignments for them are not emitted.
3394 if Has_Discriminants
(Typ
)
3395 and then not Is_Unchecked_Union
(Base_Type
(Typ
))
3397 Init_Hidden_Discriminants
(Typ
, L
);
3399 -- Generate discriminant init values for the visible discriminants
3401 Init_Visible_Discriminants
;
3403 if Is_Derived_Type
(N_Typ
) then
3404 Init_Stored_Discriminants
;
3409 -- For CPP types we generate an implicit call to the C++ default
3410 -- constructor to ensure the proper initialization of the _Tag
3413 if Is_CPP_Class
(Root_Type
(Typ
)) and then CPP_Num_Prims
(Typ
) > 0 then
3414 Invoke_Constructor
: declare
3415 CPP_Parent
: constant Entity_Id
:= Enclosing_CPP_Parent
(Typ
);
3417 procedure Invoke_IC_Proc
(T
: Entity_Id
);
3418 -- Recursive routine used to climb to parents. Required because
3419 -- parents must be initialized before descendants to ensure
3420 -- propagation of inherited C++ slots.
3422 --------------------
3423 -- Invoke_IC_Proc --
3424 --------------------
3426 procedure Invoke_IC_Proc
(T
: Entity_Id
) is
3428 -- Avoid generating extra calls. Initialization required
3429 -- only for types defined from the level of derivation of
3430 -- type of the constructor and the type of the aggregate.
3432 if T
= CPP_Parent
then
3436 Invoke_IC_Proc
(Etype
(T
));
3438 -- Generate call to the IC routine
3440 if Present
(CPP_Init_Proc
(T
)) then
3442 Make_Procedure_Call_Statement
(Loc
,
3443 Name
=> New_Occurrence_Of
(CPP_Init_Proc
(T
), Loc
)));
3447 -- Start of processing for Invoke_Constructor
3450 -- Implicit invocation of the C++ constructor
3452 if Nkind
(N
) = N_Aggregate
then
3454 Make_Procedure_Call_Statement
(Loc
,
3456 New_Occurrence_Of
(Base_Init_Proc
(CPP_Parent
), Loc
),
3457 Parameter_Associations
=> New_List
(
3458 Unchecked_Convert_To
(CPP_Parent
,
3459 New_Copy_Tree
(Lhs
)))));
3462 Invoke_IC_Proc
(Typ
);
3463 end Invoke_Constructor
;
3466 -- Generate the assignments, component by component
3468 -- tmp.comp1 := Expr1_From_Aggr;
3469 -- tmp.comp2 := Expr2_From_Aggr;
3472 Comp
:= First
(Component_Associations
(N
));
3473 while Present
(Comp
) loop
3474 Selector
:= Entity
(First
(Choices
(Comp
)));
3478 if Is_CPP_Constructor_Call
(Expression
(Comp
)) then
3480 Build_Initialization_Call
(Loc
,
3482 Make_Selected_Component
(Loc
,
3483 Prefix
=> New_Copy_Tree
(Target
),
3484 Selector_Name
=> New_Occurrence_Of
(Selector
, Loc
)),
3485 Typ
=> Etype
(Selector
),
3487 With_Default_Init
=> True,
3488 Constructor_Ref
=> Expression
(Comp
)));
3490 -- Ada 2005 (AI-287): For each default-initialized component generate
3491 -- a call to the corresponding IP subprogram if available.
3493 elsif Box_Present
(Comp
)
3494 and then Has_Non_Null_Base_Init_Proc
(Etype
(Selector
))
3496 if Ekind
(Selector
) /= E_Discriminant
then
3497 Generate_Finalization_Actions
;
3500 -- Ada 2005 (AI-287): If the component type has tasks then
3501 -- generate the activation chain and master entities (except
3502 -- in case of an allocator because in that case these entities
3503 -- are generated by Build_Task_Allocate_Block_With_Init_Stmts).
3506 Ctype
: constant Entity_Id
:= Etype
(Selector
);
3507 Inside_Allocator
: Boolean := False;
3508 P
: Node_Id
:= Parent
(N
);
3511 if Is_Task_Type
(Ctype
) or else Has_Task
(Ctype
) then
3512 while Present
(P
) loop
3513 if Nkind
(P
) = N_Allocator
then
3514 Inside_Allocator
:= True;
3521 if not Inside_Init_Proc
and not Inside_Allocator
then
3522 Build_Activation_Chain_Entity
(N
);
3528 Build_Initialization_Call
(Loc
,
3529 Id_Ref
=> Make_Selected_Component
(Loc
,
3530 Prefix
=> New_Copy_Tree
(Target
),
3532 New_Occurrence_Of
(Selector
, Loc
)),
3533 Typ
=> Etype
(Selector
),
3535 With_Default_Init
=> True));
3537 -- Prepare for component assignment
3539 elsif Ekind
(Selector
) /= E_Discriminant
3540 or else Nkind
(N
) = N_Extension_Aggregate
3542 -- All the discriminants have now been assigned
3544 -- This is now a good moment to initialize and attach all the
3545 -- controllers. Their position may depend on the discriminants.
3547 if Ekind
(Selector
) /= E_Discriminant
then
3548 Generate_Finalization_Actions
;
3551 Comp_Type
:= Underlying_Type
(Etype
(Selector
));
3553 Make_Selected_Component
(Loc
,
3554 Prefix
=> New_Copy_Tree
(Target
),
3555 Selector_Name
=> New_Occurrence_Of
(Selector
, Loc
));
3557 if Nkind
(Expression
(Comp
)) = N_Qualified_Expression
then
3558 Expr_Q
:= Expression
(Expression
(Comp
));
3560 Expr_Q
:= Expression
(Comp
);
3563 -- Now either create the assignment or generate the code for the
3564 -- inner aggregate top-down.
3566 if Is_Delayed_Aggregate
(Expr_Q
) then
3568 -- We have the following case of aggregate nesting inside
3569 -- an object declaration:
3571 -- type Arr_Typ is array (Integer range <>) of ...;
3573 -- type Rec_Typ (...) is record
3574 -- Obj_Arr_Typ : Arr_Typ (A .. B);
3577 -- Obj_Rec_Typ : Rec_Typ := (...,
3578 -- Obj_Arr_Typ => (X => (...), Y => (...)));
3580 -- The length of the ranges of the aggregate and Obj_Add_Typ
3581 -- are equal (B - A = Y - X), but they do not coincide (X /=
3582 -- A and B /= Y). This case requires array sliding which is
3583 -- performed in the following manner:
3585 -- subtype Arr_Sub is Arr_Typ (X .. Y);
3587 -- Temp (X) := (...);
3589 -- Temp (Y) := (...);
3590 -- Obj_Rec_Typ.Obj_Arr_Typ := Temp;
3592 if Ekind
(Comp_Type
) = E_Array_Subtype
3593 and then Is_Int_Range_Bounds
(Aggregate_Bounds
(Expr_Q
))
3594 and then Is_Int_Range_Bounds
(First_Index
(Comp_Type
))
3596 Compatible_Int_Bounds
3597 (Agg_Bounds
=> Aggregate_Bounds
(Expr_Q
),
3598 Typ_Bounds
=> First_Index
(Comp_Type
))
3600 -- Create the array subtype with bounds equal to those of
3601 -- the corresponding aggregate.
3604 SubE
: constant Entity_Id
:= Make_Temporary
(Loc
, 'T');
3606 SubD
: constant Node_Id
:=
3607 Make_Subtype_Declaration
(Loc
,
3608 Defining_Identifier
=> SubE
,
3609 Subtype_Indication
=>
3610 Make_Subtype_Indication
(Loc
,
3612 New_Occurrence_Of
(Etype
(Comp_Type
), Loc
),
3614 Make_Index_Or_Discriminant_Constraint
3616 Constraints
=> New_List
(
3618 (Aggregate_Bounds
(Expr_Q
))))));
3620 -- Create a temporary array of the above subtype which
3621 -- will be used to capture the aggregate assignments.
3623 TmpE
: constant Entity_Id
:= Make_Temporary
(Loc
, 'A', N
);
3625 TmpD
: constant Node_Id
:=
3626 Make_Object_Declaration
(Loc
,
3627 Defining_Identifier
=> TmpE
,
3628 Object_Definition
=> New_Occurrence_Of
(SubE
, Loc
));
3631 Set_No_Initialization
(TmpD
);
3632 Append_To
(L
, SubD
);
3633 Append_To
(L
, TmpD
);
3635 -- Expand aggregate into assignments to the temp array
3638 Late_Expansion
(Expr_Q
, Comp_Type
,
3639 New_Occurrence_Of
(TmpE
, Loc
)));
3644 Make_Assignment_Statement
(Loc
,
3645 Name
=> New_Copy_Tree
(Comp_Expr
),
3646 Expression
=> New_Occurrence_Of
(TmpE
, Loc
)));
3649 -- Normal case (sliding not required)
3653 Late_Expansion
(Expr_Q
, Comp_Type
, Comp_Expr
));
3656 -- Expr_Q is not delayed aggregate
3659 if Has_Discriminants
(Typ
) then
3660 Replace_Discriminants
(Expr_Q
);
3662 -- If the component is an array type that depends on
3663 -- discriminants, and the expression is a single Others
3664 -- clause, create an explicit subtype for it because the
3665 -- backend has troubles recovering the actual bounds.
3667 if Nkind
(Expr_Q
) = N_Aggregate
3668 and then Is_Array_Type
(Comp_Type
)
3669 and then Present
(Component_Associations
(Expr_Q
))
3672 Assoc
: constant Node_Id
:=
3673 First
(Component_Associations
(Expr_Q
));
3677 if Nkind
(First
(Choices
(Assoc
))) = N_Others_Choice
3680 Build_Actual_Subtype_Of_Component
3681 (Comp_Type
, Comp_Expr
);
3683 -- If the component type does not in fact depend on
3684 -- discriminants, the subtype declaration is empty.
3686 if Present
(Decl
) then
3687 Append_To
(L
, Decl
);
3688 Set_Etype
(Comp_Expr
, Defining_Entity
(Decl
));
3695 if Modify_Tree_For_C
3696 and then Nkind
(Expr_Q
) = N_Aggregate
3697 and then Is_Array_Type
(Etype
(Expr_Q
))
3698 and then Present
(First_Index
(Etype
(Expr_Q
)))
3701 Expr_Q_Type
: constant Node_Id
:= Etype
(Expr_Q
);
3704 Build_Array_Aggr_Code
3706 Ctype
=> Component_Type
(Expr_Q_Type
),
3707 Index
=> First_Index
(Expr_Q_Type
),
3710 Is_Scalar_Type
(Component_Type
(Expr_Q_Type
))));
3714 -- Handle an initialization expression of a controlled type
3715 -- in case it denotes a function call. In general such a
3716 -- scenario will produce a transient scope, but this will
3717 -- lead to wrong order of initialization, adjustment, and
3718 -- finalization in the context of aggregates.
3720 -- Target.Comp := Ctrl_Func_Call;
3723 -- Trans_Obj : ... := Ctrl_Func_Call; -- object
3724 -- Target.Comp := Trans_Obj;
3725 -- Finalize (Trans_Obj);
3727 -- Target.Comp._tag := ...;
3728 -- Adjust (Target.Comp);
3730 -- In the example above, the call to Finalize occurs too
3731 -- early and as a result it may leave the record component
3732 -- in a bad state. Finalization of the transient object
3733 -- should really happen after adjustment.
3735 -- To avoid this scenario, perform in-place side-effect
3736 -- removal of the function call. This eliminates the
3737 -- transient property of the function result and ensures
3738 -- correct order of actions.
3740 -- Res : ... := Ctrl_Func_Call;
3741 -- Target.Comp := Res;
3742 -- Target.Comp._tag := ...;
3743 -- Adjust (Target.Comp);
3746 if Needs_Finalization
(Comp_Type
)
3747 and then Nkind
(Expr_Q
) /= N_Aggregate
3749 Initialize_Ctrl_Record_Component
3750 (Rec_Comp
=> Comp_Expr
,
3751 Comp_Typ
=> Etype
(Selector
),
3752 Init_Expr
=> Expr_Q
,
3755 -- Otherwise perform single component initialization
3758 Initialize_Record_Component
3759 (Rec_Comp
=> Comp_Expr
,
3760 Comp_Typ
=> Etype
(Selector
),
3761 Init_Expr
=> Expr_Q
,
3767 -- comment would be good here ???
3769 elsif Ekind
(Selector
) = E_Discriminant
3770 and then Nkind
(N
) /= N_Extension_Aggregate
3771 and then Nkind
(Parent
(N
)) = N_Component_Association
3772 and then Is_Constrained
(Typ
)
3774 -- We must check that the discriminant value imposed by the
3775 -- context is the same as the value given in the subaggregate,
3776 -- because after the expansion into assignments there is no
3777 -- record on which to perform a regular discriminant check.
3784 D_Val
:= First_Elmt
(Discriminant_Constraint
(Typ
));
3785 Disc
:= First_Discriminant
(Typ
);
3786 while Chars
(Disc
) /= Chars
(Selector
) loop
3787 Next_Discriminant
(Disc
);
3791 pragma Assert
(Present
(D_Val
));
3793 -- This check cannot performed for components that are
3794 -- constrained by a current instance, because this is not a
3795 -- value that can be compared with the actual constraint.
3797 if Nkind
(Node
(D_Val
)) /= N_Attribute_Reference
3798 or else not Is_Entity_Name
(Prefix
(Node
(D_Val
)))
3799 or else not Is_Type
(Entity
(Prefix
(Node
(D_Val
))))
3802 Make_Raise_Constraint_Error
(Loc
,
3805 Left_Opnd
=> New_Copy_Tree
(Node
(D_Val
)),
3806 Right_Opnd
=> Expression
(Comp
)),
3807 Reason
=> CE_Discriminant_Check_Failed
));
3810 -- Find self-reference in previous discriminant assignment,
3811 -- and replace with proper expression.
3818 while Present
(Ass
) loop
3819 if Nkind
(Ass
) = N_Assignment_Statement
3820 and then Nkind
(Name
(Ass
)) = N_Selected_Component
3821 and then Chars
(Selector_Name
(Name
(Ass
))) =
3825 (Ass
, New_Copy_Tree
(Expression
(Comp
)));
3838 -- If the type is tagged, the tag needs to be initialized (unless we
3839 -- are in VM-mode where tags are implicit). It is done late in the
3840 -- initialization process because in some cases, we call the init
3841 -- proc of an ancestor which will not leave out the right tag.
3843 if Ancestor_Is_Expression
then
3846 -- For CPP types we generated a call to the C++ default constructor
3847 -- before the components have been initialized to ensure the proper
3848 -- initialization of the _Tag component (see above).
3850 elsif Is_CPP_Class
(Typ
) then
3853 elsif Is_Tagged_Type
(Typ
) and then Tagged_Type_Expansion
then
3855 Make_OK_Assignment_Statement
(Loc
,
3857 Make_Selected_Component
(Loc
,
3858 Prefix
=> New_Copy_Tree
(Target
),
3861 (First_Tag_Component
(Base_Type
(Typ
)), Loc
)),
3864 Unchecked_Convert_To
(RTE
(RE_Tag
),
3866 (Node
(First_Elmt
(Access_Disp_Table
(Base_Type
(Typ
)))),
3869 Append_To
(L
, Instr
);
3871 -- Ada 2005 (AI-251): If the tagged type has been derived from an
3872 -- abstract interfaces we must also initialize the tags of the
3873 -- secondary dispatch tables.
3875 if Has_Interfaces
(Base_Type
(Typ
)) then
3877 (Typ
=> Base_Type
(Typ
),
3880 Init_Tags_List
=> L
);
3884 -- If the controllers have not been initialized yet (by lack of non-
3885 -- discriminant components), let's do it now.
3887 Generate_Finalization_Actions
;
3890 end Build_Record_Aggr_Code
;
3892 ---------------------------------------
3893 -- Collect_Initialization_Statements --
3894 ---------------------------------------
3896 procedure Collect_Initialization_Statements
3899 Node_After
: Node_Id
)
3901 Loc
: constant Source_Ptr
:= Sloc
(N
);
3902 Init_Actions
: constant List_Id
:= New_List
;
3903 Init_Node
: Node_Id
;
3904 Comp_Stmt
: Node_Id
;
3907 -- Nothing to do if Obj is already frozen, as in this case we known we
3908 -- won't need to move the initialization statements about later on.
3910 if Is_Frozen
(Obj
) then
3915 while Next
(Init_Node
) /= Node_After
loop
3916 Append_To
(Init_Actions
, Remove_Next
(Init_Node
));
3919 if not Is_Empty_List
(Init_Actions
) then
3920 Comp_Stmt
:= Make_Compound_Statement
(Loc
, Actions
=> Init_Actions
);
3921 Insert_Action_After
(Init_Node
, Comp_Stmt
);
3922 Set_Initialization_Statements
(Obj
, Comp_Stmt
);
3924 end Collect_Initialization_Statements
;
3926 -------------------------------
3927 -- Convert_Aggr_In_Allocator --
3928 -------------------------------
3930 procedure Convert_Aggr_In_Allocator
3935 Loc
: constant Source_Ptr
:= Sloc
(Aggr
);
3936 Typ
: constant Entity_Id
:= Etype
(Aggr
);
3937 Temp
: constant Entity_Id
:= Defining_Identifier
(Decl
);
3939 Occ
: constant Node_Id
:=
3940 Unchecked_Convert_To
(Typ
,
3941 Make_Explicit_Dereference
(Loc
, New_Occurrence_Of
(Temp
, Loc
)));
3944 if Is_Array_Type
(Typ
) then
3945 Convert_Array_Aggr_In_Allocator
(Decl
, Aggr
, Occ
);
3947 elsif Has_Default_Init_Comps
(Aggr
) then
3949 L
: constant List_Id
:= New_List
;
3950 Init_Stmts
: List_Id
;
3953 Init_Stmts
:= Late_Expansion
(Aggr
, Typ
, Occ
);
3955 if Has_Task
(Typ
) then
3956 Build_Task_Allocate_Block_With_Init_Stmts
(L
, Aggr
, Init_Stmts
);
3957 Insert_Actions
(Alloc
, L
);
3959 Insert_Actions
(Alloc
, Init_Stmts
);
3964 Insert_Actions
(Alloc
, Late_Expansion
(Aggr
, Typ
, Occ
));
3966 end Convert_Aggr_In_Allocator
;
3968 --------------------------------
3969 -- Convert_Aggr_In_Assignment --
3970 --------------------------------
3972 procedure Convert_Aggr_In_Assignment
(N
: Node_Id
) is
3973 Aggr
: Node_Id
:= Expression
(N
);
3974 Typ
: constant Entity_Id
:= Etype
(Aggr
);
3975 Occ
: constant Node_Id
:= New_Copy_Tree
(Name
(N
));
3978 if Nkind
(Aggr
) = N_Qualified_Expression
then
3979 Aggr
:= Expression
(Aggr
);
3982 Insert_Actions_After
(N
, Late_Expansion
(Aggr
, Typ
, Occ
));
3983 end Convert_Aggr_In_Assignment
;
3985 ---------------------------------
3986 -- Convert_Aggr_In_Object_Decl --
3987 ---------------------------------
3989 procedure Convert_Aggr_In_Object_Decl
(N
: Node_Id
) is
3990 Obj
: constant Entity_Id
:= Defining_Identifier
(N
);
3991 Aggr
: Node_Id
:= Expression
(N
);
3992 Loc
: constant Source_Ptr
:= Sloc
(Aggr
);
3993 Typ
: constant Entity_Id
:= Etype
(Aggr
);
3994 Occ
: constant Node_Id
:= New_Occurrence_Of
(Obj
, Loc
);
3996 function Discriminants_Ok
return Boolean;
3997 -- If the object type is constrained, the discriminants in the
3998 -- aggregate must be checked against the discriminants of the subtype.
3999 -- This cannot be done using Apply_Discriminant_Checks because after
4000 -- expansion there is no aggregate left to check.
4002 ----------------------
4003 -- Discriminants_Ok --
4004 ----------------------
4006 function Discriminants_Ok
return Boolean is
4007 Cond
: Node_Id
:= Empty
;
4016 D
:= First_Discriminant
(Typ
);
4017 Disc1
:= First_Elmt
(Discriminant_Constraint
(Typ
));
4018 Disc2
:= First_Elmt
(Discriminant_Constraint
(Etype
(Obj
)));
4019 while Present
(Disc1
) and then Present
(Disc2
) loop
4020 Val1
:= Node
(Disc1
);
4021 Val2
:= Node
(Disc2
);
4023 if not Is_OK_Static_Expression
(Val1
)
4024 or else not Is_OK_Static_Expression
(Val2
)
4026 Check
:= Make_Op_Ne
(Loc
,
4027 Left_Opnd
=> Duplicate_Subexpr
(Val1
),
4028 Right_Opnd
=> Duplicate_Subexpr
(Val2
));
4034 Cond
:= Make_Or_Else
(Loc
,
4036 Right_Opnd
=> Check
);
4039 elsif Expr_Value
(Val1
) /= Expr_Value
(Val2
) then
4040 Apply_Compile_Time_Constraint_Error
(Aggr
,
4041 Msg
=> "incorrect value for discriminant&??",
4042 Reason
=> CE_Discriminant_Check_Failed
,
4047 Next_Discriminant
(D
);
4052 -- If any discriminant constraint is nonstatic, emit a check
4054 if Present
(Cond
) then
4056 Make_Raise_Constraint_Error
(Loc
,
4058 Reason
=> CE_Discriminant_Check_Failed
));
4062 end Discriminants_Ok
;
4064 -- Start of processing for Convert_Aggr_In_Object_Decl
4067 Set_Assignment_OK
(Occ
);
4069 if Nkind
(Aggr
) = N_Qualified_Expression
then
4070 Aggr
:= Expression
(Aggr
);
4073 if Has_Discriminants
(Typ
)
4074 and then Typ
/= Etype
(Obj
)
4075 and then Is_Constrained
(Etype
(Obj
))
4076 and then not Discriminants_Ok
4081 -- If the context is an extended return statement, it has its own
4082 -- finalization machinery (i.e. works like a transient scope) and
4083 -- we do not want to create an additional one, because objects on
4084 -- the finalization list of the return must be moved to the caller's
4085 -- finalization list to complete the return.
4087 -- However, if the aggregate is limited, it is built in place, and the
4088 -- controlled components are not assigned to intermediate temporaries
4089 -- so there is no need for a transient scope in this case either.
4091 if Requires_Transient_Scope
(Typ
)
4092 and then Ekind
(Current_Scope
) /= E_Return_Statement
4093 and then not Is_Limited_Type
(Typ
)
4095 Establish_Transient_Scope
(Aggr
, Manage_Sec_Stack
=> False);
4099 Node_After
: constant Node_Id
:= Next
(N
);
4101 Insert_Actions_After
(N
, Late_Expansion
(Aggr
, Typ
, Occ
));
4102 Collect_Initialization_Statements
(Obj
, N
, Node_After
);
4105 Set_No_Initialization
(N
);
4106 Initialize_Discriminants
(N
, Typ
);
4107 end Convert_Aggr_In_Object_Decl
;
4109 -------------------------------------
4110 -- Convert_Array_Aggr_In_Allocator --
4111 -------------------------------------
4113 procedure Convert_Array_Aggr_In_Allocator
4118 Aggr_Code
: List_Id
;
4119 Typ
: constant Entity_Id
:= Etype
(Aggr
);
4120 Ctyp
: constant Entity_Id
:= Component_Type
(Typ
);
4123 -- The target is an explicit dereference of the allocated object.
4124 -- Generate component assignments to it, as for an aggregate that
4125 -- appears on the right-hand side of an assignment statement.
4128 Build_Array_Aggr_Code
(Aggr
,
4130 Index
=> First_Index
(Typ
),
4132 Scalar_Comp
=> Is_Scalar_Type
(Ctyp
));
4134 Insert_Actions_After
(Decl
, Aggr_Code
);
4135 end Convert_Array_Aggr_In_Allocator
;
4137 ----------------------------
4138 -- Convert_To_Assignments --
4139 ----------------------------
4141 procedure Convert_To_Assignments
(N
: Node_Id
; Typ
: Entity_Id
) is
4142 Loc
: constant Source_Ptr
:= Sloc
(N
);
4146 Aggr_Code
: List_Id
;
4148 Target_Expr
: Node_Id
;
4149 Parent_Kind
: Node_Kind
;
4150 Unc_Decl
: Boolean := False;
4151 Parent_Node
: Node_Id
;
4154 pragma Assert
(Nkind_In
(N
, N_Aggregate
, N_Extension_Aggregate
));
4155 pragma Assert
(not Is_Static_Dispatch_Table_Aggregate
(N
));
4156 pragma Assert
(Is_Record_Type
(Typ
));
4158 Parent_Node
:= Parent
(N
);
4159 Parent_Kind
:= Nkind
(Parent_Node
);
4161 if Parent_Kind
= N_Qualified_Expression
then
4162 -- Check if we are in an unconstrained declaration because in this
4163 -- case the current delayed expansion mechanism doesn't work when
4164 -- the declared object size depends on the initializing expr.
4166 Parent_Node
:= Parent
(Parent_Node
);
4167 Parent_Kind
:= Nkind
(Parent_Node
);
4169 if Parent_Kind
= N_Object_Declaration
then
4171 not Is_Entity_Name
(Object_Definition
(Parent_Node
))
4172 or else (Nkind
(N
) = N_Aggregate
4175 (Entity
(Object_Definition
(Parent_Node
))))
4176 or else Is_Class_Wide_Type
4177 (Entity
(Object_Definition
(Parent_Node
)));
4181 -- Just set the Delay flag in the cases where the transformation will be
4182 -- done top down from above.
4186 -- Internal aggregate (transformed when expanding the parent)
4188 or else Parent_Kind
= N_Aggregate
4189 or else Parent_Kind
= N_Extension_Aggregate
4190 or else Parent_Kind
= N_Component_Association
4192 -- Allocator (see Convert_Aggr_In_Allocator)
4194 or else Parent_Kind
= N_Allocator
4196 -- Object declaration (see Convert_Aggr_In_Object_Decl)
4198 or else (Parent_Kind
= N_Object_Declaration
and then not Unc_Decl
)
4200 -- Safe assignment (see Convert_Aggr_Assignments). So far only the
4201 -- assignments in init procs are taken into account.
4203 or else (Parent_Kind
= N_Assignment_Statement
4204 and then Inside_Init_Proc
)
4206 -- (Ada 2005) An inherently limited type in a return statement, which
4207 -- will be handled in a build-in-place fashion, and may be rewritten
4208 -- as an extended return and have its own finalization machinery.
4209 -- In the case of a simple return, the aggregate needs to be delayed
4210 -- until the scope for the return statement has been created, so
4211 -- that any finalization chain will be associated with that scope.
4212 -- For extended returns, we delay expansion to avoid the creation
4213 -- of an unwanted transient scope that could result in premature
4214 -- finalization of the return object (which is built in place
4215 -- within the caller's scope).
4217 or else Is_Build_In_Place_Aggregate_Return
(N
)
4219 Set_Expansion_Delayed
(N
);
4223 -- Otherwise, if a transient scope is required, create it now. If we
4224 -- are within an initialization procedure do not create such, because
4225 -- the target of the assignment must not be declared within a local
4226 -- block, and because cleanup will take place on return from the
4227 -- initialization procedure.
4229 -- Should the condition be more restrictive ???
4231 if Requires_Transient_Scope
(Typ
) and then not Inside_Init_Proc
then
4232 Establish_Transient_Scope
(N
, Manage_Sec_Stack
=> False);
4235 -- If the aggregate is nonlimited, create a temporary. If it is limited
4236 -- and context is an assignment, this is a subaggregate for an enclosing
4237 -- aggregate being expanded. It must be built in place, so use target of
4238 -- the current assignment.
4240 if Is_Limited_Type
(Typ
)
4241 and then Nkind
(Parent
(N
)) = N_Assignment_Statement
4243 Target_Expr
:= New_Copy_Tree
(Name
(Parent
(N
)));
4244 Insert_Actions
(Parent
(N
),
4245 Build_Record_Aggr_Code
(N
, Typ
, Target_Expr
));
4246 Rewrite
(Parent
(N
), Make_Null_Statement
(Loc
));
4248 -- Generating C, do not declare a temporary to initialize an aggregate
4249 -- assigned to Out or In_Out parameters whose type has no discriminants.
4250 -- This avoids stack overflow errors at run time.
4252 elsif Modify_Tree_For_C
4253 and then Nkind
(Parent
(N
)) = N_Assignment_Statement
4254 and then Nkind
(Name
(Parent
(N
))) = N_Identifier
4255 and then Ekind_In
(Entity
(Name
(Parent
(N
))), E_Out_Parameter
,
4257 and then not Has_Discriminants
(Etype
(Entity
(Name
(Parent
(N
)))))
4259 Target_Expr
:= New_Copy_Tree
(Name
(Parent
(N
)));
4260 Insert_Actions
(Parent
(N
),
4261 Build_Record_Aggr_Code
(N
, Typ
, Target_Expr
));
4262 Rewrite
(Parent
(N
), Make_Null_Statement
(Loc
));
4265 Temp
:= Make_Temporary
(Loc
, 'A', N
);
4267 -- If the type inherits unknown discriminants, use the view with
4268 -- known discriminants if available.
4270 if Has_Unknown_Discriminants
(Typ
)
4271 and then Present
(Underlying_Record_View
(Typ
))
4273 T
:= Underlying_Record_View
(Typ
);
4279 Make_Object_Declaration
(Loc
,
4280 Defining_Identifier
=> Temp
,
4281 Object_Definition
=> New_Occurrence_Of
(T
, Loc
));
4283 Set_No_Initialization
(Instr
);
4284 Insert_Action
(N
, Instr
);
4285 Initialize_Discriminants
(Instr
, T
);
4287 Target_Expr
:= New_Occurrence_Of
(Temp
, Loc
);
4288 Aggr_Code
:= Build_Record_Aggr_Code
(N
, T
, Target_Expr
);
4290 -- Save the last assignment statement associated with the aggregate
4291 -- when building a controlled object. This reference is utilized by
4292 -- the finalization machinery when marking an object as successfully
4295 if Needs_Finalization
(T
) then
4296 Set_Last_Aggregate_Assignment
(Temp
, Last
(Aggr_Code
));
4299 Insert_Actions
(N
, Aggr_Code
);
4300 Rewrite
(N
, New_Occurrence_Of
(Temp
, Loc
));
4301 Analyze_And_Resolve
(N
, T
);
4303 end Convert_To_Assignments
;
4305 ---------------------------
4306 -- Convert_To_Positional --
4307 ---------------------------
4309 procedure Convert_To_Positional
4311 Max_Others_Replicate
: Nat
:= 32;
4312 Handle_Bit_Packed
: Boolean := False)
4314 Typ
: constant Entity_Id
:= Etype
(N
);
4316 Static_Components
: Boolean := True;
4318 procedure Check_Static_Components
;
4319 -- Check whether all components of the aggregate are compile-time known
4320 -- values, and can be passed as is to the back-end without further
4326 Ixb
: Node_Id
) return Boolean;
4327 -- Convert the aggregate into a purely positional form if possible. On
4328 -- entry the bounds of all dimensions are known to be static, and the
4329 -- total number of components is safe enough to expand.
4331 function Is_Flat
(N
: Node_Id
; Dims
: Int
) return Boolean;
4332 -- Return True iff the array N is flat (which is not trivial in the case
4333 -- of multidimensional aggregates).
4335 function Is_Static_Element
(N
: Node_Id
) return Boolean;
4336 -- Return True if N, an element of a component association list, i.e.
4337 -- N_Component_Association or N_Iterated_Component_Association, has a
4338 -- compile-time known value and can be passed as is to the back-end
4339 -- without further expansion.
4340 -- An Iterated_Component_Association is treated as nonstatic in most
4341 -- cases for now, so there are possibilities for optimization.
4343 -----------------------------
4344 -- Check_Static_Components --
4345 -----------------------------
4347 -- Could use some comments in this body ???
4349 procedure Check_Static_Components
is
4354 Static_Components
:= True;
4356 if Nkind
(N
) = N_String_Literal
then
4359 elsif Present
(Expressions
(N
)) then
4360 Expr
:= First
(Expressions
(N
));
4361 while Present
(Expr
) loop
4362 if Nkind
(Expr
) /= N_Aggregate
4363 or else not Compile_Time_Known_Aggregate
(Expr
)
4364 or else Expansion_Delayed
(Expr
)
4366 Static_Components
:= False;
4374 if Nkind
(N
) = N_Aggregate
4375 and then Present
(Component_Associations
(N
))
4377 Assoc
:= First
(Component_Associations
(N
));
4378 while Present
(Assoc
) loop
4379 if not Is_Static_Element
(Assoc
) then
4380 Static_Components
:= False;
4387 end Check_Static_Components
;
4396 Ixb
: Node_Id
) return Boolean
4398 Loc
: constant Source_Ptr
:= Sloc
(N
);
4399 Blo
: constant Node_Id
:= Type_Low_Bound
(Etype
(Ixb
));
4400 Lo
: constant Node_Id
:= Type_Low_Bound
(Etype
(Ix
));
4401 Hi
: constant Node_Id
:= Type_High_Bound
(Etype
(Ix
));
4405 Others_Present
: Boolean := False;
4408 if Nkind
(Original_Node
(N
)) = N_String_Literal
then
4412 if not Compile_Time_Known_Value
(Lo
)
4413 or else not Compile_Time_Known_Value
(Hi
)
4418 Lov
:= Expr_Value
(Lo
);
4419 Hiv
:= Expr_Value
(Hi
);
4421 -- Check if there is an others choice
4423 if Present
(Component_Associations
(N
)) then
4429 Assoc
:= First
(Component_Associations
(N
));
4430 while Present
(Assoc
) loop
4432 -- If this is a box association, flattening is in general
4433 -- not possible because at this point we cannot tell if the
4434 -- default is static or even exists.
4436 if Box_Present
(Assoc
) then
4439 elsif Nkind
(Assoc
) = N_Iterated_Component_Association
then
4443 Choice
:= First
(Choice_List
(Assoc
));
4445 while Present
(Choice
) loop
4446 if Nkind
(Choice
) = N_Others_Choice
then
4447 Others_Present
:= True;
4458 -- If the low bound is not known at compile time and others is not
4459 -- present we can proceed since the bounds can be obtained from the
4463 or else (not Compile_Time_Known_Value
(Blo
) and then Others_Present
)
4468 -- Determine if set of alternatives is suitable for conversion and
4469 -- build an array containing the values in sequence.
4472 Vals
: array (UI_To_Int
(Lov
) .. UI_To_Int
(Hiv
))
4473 of Node_Id
:= (others => Empty
);
4474 -- The values in the aggregate sorted appropriately
4477 -- Same data as Vals in list form
4480 -- Used to validate Max_Others_Replicate limit
4483 Num
: Int
:= UI_To_Int
(Lov
);
4489 if Present
(Expressions
(N
)) then
4490 Elmt
:= First
(Expressions
(N
));
4491 while Present
(Elmt
) loop
4492 if Nkind
(Elmt
) = N_Aggregate
4493 and then Present
(Next_Index
(Ix
))
4495 not Flatten
(Elmt
, Next_Index
(Ix
), Next_Index
(Ixb
))
4500 -- Duplicate expression for each index it covers
4502 Vals
(Num
) := New_Copy_Tree
(Elmt
);
4509 if No
(Component_Associations
(N
)) then
4513 Elmt
:= First
(Component_Associations
(N
));
4515 if Nkind
(Expression
(Elmt
)) = N_Aggregate
then
4516 if Present
(Next_Index
(Ix
))
4519 (Expression
(Elmt
), Next_Index
(Ix
), Next_Index
(Ixb
))
4525 Component_Loop
: while Present
(Elmt
) loop
4526 Choice
:= First
(Choice_List
(Elmt
));
4527 Choice_Loop
: while Present
(Choice
) loop
4529 -- If we have an others choice, fill in the missing elements
4530 -- subject to the limit established by Max_Others_Replicate.
4532 if Nkind
(Choice
) = N_Others_Choice
then
4535 -- If the expression involves a construct that generates
4536 -- a loop, we must generate individual assignments and
4537 -- no flattening is possible.
4539 if Nkind
(Expression
(Elmt
)) = N_Quantified_Expression
4544 for J
in Vals
'Range loop
4545 if No
(Vals
(J
)) then
4546 Vals
(J
) := New_Copy_Tree
(Expression
(Elmt
));
4547 Rep_Count
:= Rep_Count
+ 1;
4549 -- Check for maximum others replication. Note that
4550 -- we skip this test if either of the restrictions
4551 -- No_Elaboration_Code or No_Implicit_Loops is
4552 -- active, if this is a preelaborable unit or
4553 -- a predefined unit, or if the unit must be
4554 -- placed in data memory. This also ensures that
4555 -- predefined units get the same level of constant
4556 -- folding in Ada 95 and Ada 2005, where their
4557 -- categorization has changed.
4560 P
: constant Entity_Id
:=
4561 Cunit_Entity
(Current_Sem_Unit
);
4564 -- Check if duplication is always OK and, if so,
4565 -- continue processing.
4567 if Restriction_Active
(No_Elaboration_Code
)
4568 or else Restriction_Active
(No_Implicit_Loops
)
4570 (Ekind
(Current_Scope
) = E_Package
4571 and then Static_Elaboration_Desired
4573 or else Is_Preelaborated
(P
)
4574 or else (Ekind
(P
) = E_Package_Body
4576 Is_Preelaborated
(Spec_Entity
(P
)))
4578 Is_Predefined_Unit
(Get_Source_Unit
(P
))
4582 -- If duplication is not always OK, continue
4583 -- only if either the element is static or is
4584 -- an aggregate which can itself be flattened,
4585 -- and the replication count is not too high.
4587 elsif (Is_Static_Element
(Elmt
)
4589 (Nkind
(Expression
(Elmt
)) = N_Aggregate
4590 and then Present
(Next_Index
(Ix
))))
4591 and then Rep_Count
<= Max_Others_Replicate
4595 -- Return False in all the other cases
4605 and then Warn_On_Redundant_Constructs
4607 Error_Msg_N
("there are no others?r?", Elmt
);
4610 exit Component_Loop
;
4612 -- Case of a subtype mark, identifier or expanded name
4614 elsif Is_Entity_Name
(Choice
)
4615 and then Is_Type
(Entity
(Choice
))
4617 Lo
:= Type_Low_Bound
(Etype
(Choice
));
4618 Hi
:= Type_High_Bound
(Etype
(Choice
));
4620 -- Case of subtype indication
4622 elsif Nkind
(Choice
) = N_Subtype_Indication
then
4623 Lo
:= Low_Bound
(Range_Expression
(Constraint
(Choice
)));
4624 Hi
:= High_Bound
(Range_Expression
(Constraint
(Choice
)));
4628 elsif Nkind
(Choice
) = N_Range
then
4629 Lo
:= Low_Bound
(Choice
);
4630 Hi
:= High_Bound
(Choice
);
4632 -- Normal subexpression case
4634 else pragma Assert
(Nkind
(Choice
) in N_Subexpr
);
4635 if not Compile_Time_Known_Value
(Choice
) then
4639 Choice_Index
:= UI_To_Int
(Expr_Value
(Choice
));
4641 if Choice_Index
in Vals
'Range then
4642 Vals
(Choice_Index
) :=
4643 New_Copy_Tree
(Expression
(Elmt
));
4646 -- Choice is statically out-of-range, will be
4647 -- rewritten to raise Constraint_Error.
4655 -- Range cases merge with Lo,Hi set
4657 if not Compile_Time_Known_Value
(Lo
)
4659 not Compile_Time_Known_Value
(Hi
)
4664 for J
in UI_To_Int
(Expr_Value
(Lo
)) ..
4665 UI_To_Int
(Expr_Value
(Hi
))
4667 Vals
(J
) := New_Copy_Tree
(Expression
(Elmt
));
4673 end loop Choice_Loop
;
4676 end loop Component_Loop
;
4678 -- If we get here the conversion is possible
4681 for J
in Vals
'Range loop
4682 Append
(Vals
(J
), Vlist
);
4685 Rewrite
(N
, Make_Aggregate
(Loc
, Expressions
=> Vlist
));
4686 Set_Aggregate_Bounds
(N
, Aggregate_Bounds
(Original_Node
(N
)));
4695 function Is_Flat
(N
: Node_Id
; Dims
: Int
) return Boolean is
4702 elsif Nkind
(N
) = N_Aggregate
then
4703 if Present
(Component_Associations
(N
)) then
4707 Elmt
:= First
(Expressions
(N
));
4708 while Present
(Elmt
) loop
4709 if not Is_Flat
(Elmt
, Dims
- 1) then
4723 -------------------------
4724 -- Is_Static_Element --
4725 -------------------------
4727 function Is_Static_Element
(N
: Node_Id
) return Boolean is
4728 Expr
: constant Node_Id
:= Expression
(N
);
4731 if Nkind_In
(Expr
, N_Integer_Literal
, N_Real_Literal
) then
4734 elsif Is_Entity_Name
(Expr
)
4735 and then Present
(Entity
(Expr
))
4736 and then Ekind
(Entity
(Expr
)) = E_Enumeration_Literal
4740 elsif Nkind
(N
) = N_Iterated_Component_Association
then
4743 elsif Nkind
(Expr
) = N_Aggregate
4744 and then Compile_Time_Known_Aggregate
(Expr
)
4745 and then not Expansion_Delayed
(Expr
)
4752 end Is_Static_Element
;
4754 -- Start of processing for Convert_To_Positional
4757 -- Only convert to positional when generating C in case of an
4758 -- object declaration, this is the only case where aggregates are
4761 if Modify_Tree_For_C
and then not Is_CCG_Supported_Aggregate
(N
) then
4765 -- Ada 2005 (AI-287): Do not convert in case of default initialized
4766 -- components because in this case will need to call the corresponding
4769 if Has_Default_Init_Comps
(N
) then
4773 -- A subaggregate may have been flattened but is not known to be
4774 -- Compile_Time_Known. Set that flag in cases that cannot require
4775 -- elaboration code, so that the aggregate can be used as the
4776 -- initial value of a thread-local variable.
4778 if Is_Flat
(N
, Number_Dimensions
(Typ
)) then
4779 if Static_Array_Aggregate
(N
) then
4780 Set_Compile_Time_Known_Aggregate
(N
);
4786 if Is_Bit_Packed_Array
(Typ
) and then not Handle_Bit_Packed
then
4790 -- Do not convert to positional if controlled components are involved
4791 -- since these require special processing
4793 if Has_Controlled_Component
(Typ
) then
4797 Check_Static_Components
;
4799 -- If the size is known, or all the components are static, try to
4800 -- build a fully positional aggregate.
4802 -- The size of the type may not be known for an aggregate with
4803 -- discriminated array components, but if the components are static
4804 -- it is still possible to verify statically that the length is
4805 -- compatible with the upper bound of the type, and therefore it is
4806 -- worth flattening such aggregates as well.
4808 -- For now the back-end expands these aggregates into individual
4809 -- assignments to the target anyway, but it is conceivable that
4810 -- it will eventually be able to treat such aggregates statically???
4812 if Aggr_Size_OK
(N
, Typ
)
4813 and then Flatten
(N
, First_Index
(Typ
), First_Index
(Base_Type
(Typ
)))
4815 if Static_Components
then
4816 Set_Compile_Time_Known_Aggregate
(N
);
4817 Set_Expansion_Delayed
(N
, False);
4820 Analyze_And_Resolve
(N
, Typ
);
4823 -- If Static_Elaboration_Desired has been specified, diagnose aggregates
4824 -- that will still require initialization code.
4826 if (Ekind
(Current_Scope
) = E_Package
4827 and then Static_Elaboration_Desired
(Current_Scope
))
4828 and then Nkind
(Parent
(N
)) = N_Object_Declaration
4834 if Nkind
(N
) = N_Aggregate
and then Present
(Expressions
(N
)) then
4835 Expr
:= First
(Expressions
(N
));
4836 while Present
(Expr
) loop
4837 if Nkind_In
(Expr
, N_Integer_Literal
, N_Real_Literal
)
4839 (Is_Entity_Name
(Expr
)
4840 and then Ekind
(Entity
(Expr
)) = E_Enumeration_Literal
)
4846 ("non-static object requires elaboration code??", N
);
4853 if Present
(Component_Associations
(N
)) then
4854 Error_Msg_N
("object requires elaboration code??", N
);
4859 end Convert_To_Positional
;
4861 ----------------------------
4862 -- Expand_Array_Aggregate --
4863 ----------------------------
4865 -- Array aggregate expansion proceeds as follows:
4867 -- 1. If requested we generate code to perform all the array aggregate
4868 -- bound checks, specifically
4870 -- (a) Check that the index range defined by aggregate bounds is
4871 -- compatible with corresponding index subtype.
4873 -- (b) If an others choice is present check that no aggregate
4874 -- index is outside the bounds of the index constraint.
4876 -- (c) For multidimensional arrays make sure that all subaggregates
4877 -- corresponding to the same dimension have the same bounds.
4879 -- 2. Check for packed array aggregate which can be converted to a
4880 -- constant so that the aggregate disappears completely.
4882 -- 3. Check case of nested aggregate. Generally nested aggregates are
4883 -- handled during the processing of the parent aggregate.
4885 -- 4. Check if the aggregate can be statically processed. If this is the
4886 -- case pass it as is to Gigi. Note that a necessary condition for
4887 -- static processing is that the aggregate be fully positional.
4889 -- 5. If in place aggregate expansion is possible (i.e. no need to create
4890 -- a temporary) then mark the aggregate as such and return. Otherwise
4891 -- create a new temporary and generate the appropriate initialization
4894 procedure Expand_Array_Aggregate
(N
: Node_Id
) is
4895 Loc
: constant Source_Ptr
:= Sloc
(N
);
4897 Typ
: constant Entity_Id
:= Etype
(N
);
4898 Ctyp
: constant Entity_Id
:= Component_Type
(Typ
);
4899 -- Typ is the correct constrained array subtype of the aggregate
4900 -- Ctyp is the corresponding component type.
4902 Aggr_Dimension
: constant Pos
:= Number_Dimensions
(Typ
);
4903 -- Number of aggregate index dimensions
4905 Aggr_Low
: array (1 .. Aggr_Dimension
) of Node_Id
;
4906 Aggr_High
: array (1 .. Aggr_Dimension
) of Node_Id
;
4907 -- Low and High bounds of the constraint for each aggregate index
4909 Aggr_Index_Typ
: array (1 .. Aggr_Dimension
) of Entity_Id
;
4910 -- The type of each index
4912 In_Place_Assign_OK_For_Declaration
: Boolean := False;
4913 -- True if we are to generate an in place assignment for a declaration
4915 Maybe_In_Place_OK
: Boolean;
4916 -- If the type is neither controlled nor packed and the aggregate
4917 -- is the expression in an assignment, assignment in place may be
4918 -- possible, provided other conditions are met on the LHS.
4920 Others_Present
: array (1 .. Aggr_Dimension
) of Boolean :=
4922 -- If Others_Present (J) is True, then there is an others choice in one
4923 -- of the subaggregates of N at dimension J.
4925 function Aggr_Assignment_OK_For_Backend
(N
: Node_Id
) return Boolean;
4926 -- Returns true if an aggregate assignment can be done by the back end
4928 procedure Build_Constrained_Type
(Positional
: Boolean);
4929 -- If the subtype is not static or unconstrained, build a constrained
4930 -- type using the computable sizes of the aggregate and its sub-
4933 procedure Check_Bounds
(Aggr_Bounds
: Node_Id
; Index_Bounds
: Node_Id
);
4934 -- Checks that the bounds of Aggr_Bounds are within the bounds defined
4937 procedure Check_Same_Aggr_Bounds
(Sub_Aggr
: Node_Id
; Dim
: Pos
);
4938 -- Checks that in a multidimensional array aggregate all subaggregates
4939 -- corresponding to the same dimension have the same bounds. Sub_Aggr is
4940 -- an array subaggregate. Dim is the dimension corresponding to the
4943 procedure Compute_Others_Present
(Sub_Aggr
: Node_Id
; Dim
: Pos
);
4944 -- Computes the values of array Others_Present. Sub_Aggr is the array
4945 -- subaggregate we start the computation from. Dim is the dimension
4946 -- corresponding to the subaggregate.
4948 function In_Place_Assign_OK
return Boolean;
4949 -- Simple predicate to determine whether an aggregate assignment can
4950 -- be done in place, because none of the new values can depend on the
4951 -- components of the target of the assignment.
4953 procedure Others_Check
(Sub_Aggr
: Node_Id
; Dim
: Pos
);
4954 -- Checks that if an others choice is present in any subaggregate, no
4955 -- aggregate index is outside the bounds of the index constraint.
4956 -- Sub_Aggr is an array subaggregate. Dim is the dimension corresponding
4957 -- to the subaggregate.
4959 function Safe_Left_Hand_Side
(N
: Node_Id
) return Boolean;
4960 -- In addition to Maybe_In_Place_OK, in order for an aggregate to be
4961 -- built directly into the target of the assignment it must be free
4964 ------------------------------------
4965 -- Aggr_Assignment_OK_For_Backend --
4966 ------------------------------------
4968 -- Backend processing by Gigi/gcc is possible only if all the following
4969 -- conditions are met:
4971 -- 1. N consists of a single OTHERS choice, possibly recursively
4973 -- 2. The array type has no null ranges (the purpose of this is to
4974 -- avoid a bogus warning for an out-of-range value).
4976 -- 3. The array type has no atomic components
4978 -- 4. The component type is elementary
4980 -- 5. The component size is a multiple of Storage_Unit
4982 -- 6. The component size is Storage_Unit or the value is of the form
4983 -- M * (1 + A**1 + A**2 + .. A**(K-1)) where A = 2**(Storage_Unit)
4984 -- and M in 1 .. A-1. This can also be viewed as K occurrences of
4985 -- the 8-bit value M, concatenated together.
4987 -- The ultimate goal is to generate a call to a fast memset routine
4988 -- specifically optimized for the target.
4990 function Aggr_Assignment_OK_For_Backend
(N
: Node_Id
) return Boolean is
5002 -- Recurse as far as possible to find the innermost component type
5006 while Is_Array_Type
(Ctyp
) loop
5007 if Nkind
(Expr
) /= N_Aggregate
5008 or else not Is_Others_Aggregate
(Expr
)
5013 Index
:= First_Index
(Ctyp
);
5014 while Present
(Index
) loop
5015 Get_Index_Bounds
(Index
, Low
, High
);
5017 if Is_Null_Range
(Low
, High
) then
5024 Expr
:= Expression
(First
(Component_Associations
(Expr
)));
5026 for J
in 1 .. Number_Dimensions
(Ctyp
) - 1 loop
5027 if Nkind
(Expr
) /= N_Aggregate
5028 or else not Is_Others_Aggregate
(Expr
)
5033 Expr
:= Expression
(First
(Component_Associations
(Expr
)));
5036 if Has_Atomic_Components
(Ctyp
) then
5040 Csiz
:= Component_Size
(Ctyp
);
5041 Ctyp
:= Component_Type
(Ctyp
);
5043 if Is_Atomic_Or_VFA
(Ctyp
) then
5048 -- An Iterated_Component_Association involves a loop (in most cases)
5049 -- and is never static.
5051 if Nkind
(Parent
(Expr
)) = N_Iterated_Component_Association
then
5055 -- Access types need to be dealt with specially
5057 if Is_Access_Type
(Ctyp
) then
5059 -- Component_Size is not set by Layout_Type if the component
5060 -- type is an access type ???
5062 Csiz
:= Esize
(Ctyp
);
5064 -- Fat pointers are rejected as they are not really elementary
5067 if Csiz
/= System_Address_Size
then
5071 -- The supported expressions are NULL and constants, others are
5072 -- rejected upfront to avoid being analyzed below, which can be
5073 -- problematic for some of them, for example allocators.
5075 if Nkind
(Expr
) /= N_Null
and then not Is_Entity_Name
(Expr
) then
5079 -- Scalar types are OK if their size is a multiple of Storage_Unit
5081 elsif Is_Scalar_Type
(Ctyp
) then
5082 if Csiz
mod System_Storage_Unit
/= 0 then
5086 -- Composite types are rejected
5092 -- The expression needs to be analyzed if True is returned
5094 Analyze_And_Resolve
(Expr
, Ctyp
);
5096 -- Strip away any conversions from the expression as they simply
5097 -- qualify the real expression.
5099 while Nkind_In
(Expr
, N_Unchecked_Type_Conversion
,
5102 Expr
:= Expression
(Expr
);
5105 Nunits
:= UI_To_Int
(Csiz
) / System_Storage_Unit
;
5111 if not Compile_Time_Known_Value
(Expr
) then
5115 -- The only supported value for floating point is 0.0
5117 if Is_Floating_Point_Type
(Ctyp
) then
5118 return Expr_Value_R
(Expr
) = Ureal_0
;
5121 -- For other types, we can look into the value as an integer
5123 Value
:= Expr_Value
(Expr
);
5125 if Has_Biased_Representation
(Ctyp
) then
5126 Value
:= Value
- Expr_Value
(Type_Low_Bound
(Ctyp
));
5129 -- Values 0 and -1 immediately satisfy the last check
5131 if Value
= Uint_0
or else Value
= Uint_Minus_1
then
5135 -- We need to work with an unsigned value
5138 Value
:= Value
+ 2**(System_Storage_Unit
* Nunits
);
5141 Remainder
:= Value
rem 2**System_Storage_Unit
;
5143 for J
in 1 .. Nunits
- 1 loop
5144 Value
:= Value
/ 2**System_Storage_Unit
;
5146 if Value
rem 2**System_Storage_Unit
/= Remainder
then
5152 end Aggr_Assignment_OK_For_Backend
;
5154 ----------------------------
5155 -- Build_Constrained_Type --
5156 ----------------------------
5158 procedure Build_Constrained_Type
(Positional
: Boolean) is
5159 Loc
: constant Source_Ptr
:= Sloc
(N
);
5160 Agg_Type
: constant Entity_Id
:= Make_Temporary
(Loc
, 'A');
5163 Typ
: constant Entity_Id
:= Etype
(N
);
5164 Indexes
: constant List_Id
:= New_List
;
5169 -- If the aggregate is purely positional, all its subaggregates
5170 -- have the same size. We collect the dimensions from the first
5171 -- subaggregate at each level.
5176 for D
in 1 .. Number_Dimensions
(Typ
) loop
5177 Sub_Agg
:= First
(Expressions
(Sub_Agg
));
5181 while Present
(Comp
) loop
5188 Low_Bound
=> Make_Integer_Literal
(Loc
, 1),
5189 High_Bound
=> Make_Integer_Literal
(Loc
, Num
)));
5193 -- We know the aggregate type is unconstrained and the aggregate
5194 -- is not processable by the back end, therefore not necessarily
5195 -- positional. Retrieve each dimension bounds (computed earlier).
5197 for D
in 1 .. Number_Dimensions
(Typ
) loop
5200 Low_Bound
=> Aggr_Low
(D
),
5201 High_Bound
=> Aggr_High
(D
)));
5206 Make_Full_Type_Declaration
(Loc
,
5207 Defining_Identifier
=> Agg_Type
,
5209 Make_Constrained_Array_Definition
(Loc
,
5210 Discrete_Subtype_Definitions
=> Indexes
,
5211 Component_Definition
=>
5212 Make_Component_Definition
(Loc
,
5213 Aliased_Present
=> False,
5214 Subtype_Indication
=>
5215 New_Occurrence_Of
(Component_Type
(Typ
), Loc
))));
5217 Insert_Action
(N
, Decl
);
5219 Set_Etype
(N
, Agg_Type
);
5220 Set_Is_Itype
(Agg_Type
);
5221 Freeze_Itype
(Agg_Type
, N
);
5222 end Build_Constrained_Type
;
5228 procedure Check_Bounds
(Aggr_Bounds
: Node_Id
; Index_Bounds
: Node_Id
) is
5235 Cond
: Node_Id
:= Empty
;
5238 Get_Index_Bounds
(Aggr_Bounds
, Aggr_Lo
, Aggr_Hi
);
5239 Get_Index_Bounds
(Index_Bounds
, Ind_Lo
, Ind_Hi
);
5241 -- Generate the following test:
5243 -- [constraint_error when
5244 -- Aggr_Lo <= Aggr_Hi and then
5245 -- (Aggr_Lo < Ind_Lo or else Aggr_Hi > Ind_Hi)]
5247 -- As an optimization try to see if some tests are trivially vacuous
5248 -- because we are comparing an expression against itself.
5250 if Aggr_Lo
= Ind_Lo
and then Aggr_Hi
= Ind_Hi
then
5253 elsif Aggr_Hi
= Ind_Hi
then
5256 Left_Opnd
=> Duplicate_Subexpr_Move_Checks
(Aggr_Lo
),
5257 Right_Opnd
=> Duplicate_Subexpr_Move_Checks
(Ind_Lo
));
5259 elsif Aggr_Lo
= Ind_Lo
then
5262 Left_Opnd
=> Duplicate_Subexpr_Move_Checks
(Aggr_Hi
),
5263 Right_Opnd
=> Duplicate_Subexpr_Move_Checks
(Ind_Hi
));
5270 Left_Opnd
=> Duplicate_Subexpr_Move_Checks
(Aggr_Lo
),
5271 Right_Opnd
=> Duplicate_Subexpr_Move_Checks
(Ind_Lo
)),
5275 Left_Opnd
=> Duplicate_Subexpr
(Aggr_Hi
),
5276 Right_Opnd
=> Duplicate_Subexpr
(Ind_Hi
)));
5279 if Present
(Cond
) then
5284 Left_Opnd
=> Duplicate_Subexpr_Move_Checks
(Aggr_Lo
),
5285 Right_Opnd
=> Duplicate_Subexpr_Move_Checks
(Aggr_Hi
)),
5287 Right_Opnd
=> Cond
);
5289 Set_Analyzed
(Left_Opnd
(Left_Opnd
(Cond
)), False);
5290 Set_Analyzed
(Right_Opnd
(Left_Opnd
(Cond
)), False);
5292 Make_Raise_Constraint_Error
(Loc
,
5294 Reason
=> CE_Range_Check_Failed
));
5298 ----------------------------
5299 -- Check_Same_Aggr_Bounds --
5300 ----------------------------
5302 procedure Check_Same_Aggr_Bounds
(Sub_Aggr
: Node_Id
; Dim
: Pos
) is
5303 Sub_Lo
: constant Node_Id
:= Low_Bound
(Aggregate_Bounds
(Sub_Aggr
));
5304 Sub_Hi
: constant Node_Id
:= High_Bound
(Aggregate_Bounds
(Sub_Aggr
));
5305 -- The bounds of this specific subaggregate
5307 Aggr_Lo
: constant Node_Id
:= Aggr_Low
(Dim
);
5308 Aggr_Hi
: constant Node_Id
:= Aggr_High
(Dim
);
5309 -- The bounds of the aggregate for this dimension
5311 Ind_Typ
: constant Entity_Id
:= Aggr_Index_Typ
(Dim
);
5312 -- The index type for this dimension.xxx
5314 Cond
: Node_Id
:= Empty
;
5319 -- If index checks are on generate the test
5321 -- [constraint_error when
5322 -- Aggr_Lo /= Sub_Lo or else Aggr_Hi /= Sub_Hi]
5324 -- As an optimization try to see if some tests are trivially vacuos
5325 -- because we are comparing an expression against itself. Also for
5326 -- the first dimension the test is trivially vacuous because there
5327 -- is just one aggregate for dimension 1.
5329 if Index_Checks_Suppressed
(Ind_Typ
) then
5332 elsif Dim
= 1 or else (Aggr_Lo
= Sub_Lo
and then Aggr_Hi
= Sub_Hi
)
5336 elsif Aggr_Hi
= Sub_Hi
then
5339 Left_Opnd
=> Duplicate_Subexpr_Move_Checks
(Aggr_Lo
),
5340 Right_Opnd
=> Duplicate_Subexpr_Move_Checks
(Sub_Lo
));
5342 elsif Aggr_Lo
= Sub_Lo
then
5345 Left_Opnd
=> Duplicate_Subexpr_Move_Checks
(Aggr_Hi
),
5346 Right_Opnd
=> Duplicate_Subexpr_Move_Checks
(Sub_Hi
));
5353 Left_Opnd
=> Duplicate_Subexpr_Move_Checks
(Aggr_Lo
),
5354 Right_Opnd
=> Duplicate_Subexpr_Move_Checks
(Sub_Lo
)),
5358 Left_Opnd
=> Duplicate_Subexpr
(Aggr_Hi
),
5359 Right_Opnd
=> Duplicate_Subexpr
(Sub_Hi
)));
5362 if Present
(Cond
) then
5364 Make_Raise_Constraint_Error
(Loc
,
5366 Reason
=> CE_Length_Check_Failed
));
5369 -- Now look inside the subaggregate to see if there is more work
5371 if Dim
< Aggr_Dimension
then
5373 -- Process positional components
5375 if Present
(Expressions
(Sub_Aggr
)) then
5376 Expr
:= First
(Expressions
(Sub_Aggr
));
5377 while Present
(Expr
) loop
5378 Check_Same_Aggr_Bounds
(Expr
, Dim
+ 1);
5383 -- Process component associations
5385 if Present
(Component_Associations
(Sub_Aggr
)) then
5386 Assoc
:= First
(Component_Associations
(Sub_Aggr
));
5387 while Present
(Assoc
) loop
5388 Expr
:= Expression
(Assoc
);
5389 Check_Same_Aggr_Bounds
(Expr
, Dim
+ 1);
5394 end Check_Same_Aggr_Bounds
;
5396 ----------------------------
5397 -- Compute_Others_Present --
5398 ----------------------------
5400 procedure Compute_Others_Present
(Sub_Aggr
: Node_Id
; Dim
: Pos
) is
5405 if Present
(Component_Associations
(Sub_Aggr
)) then
5406 Assoc
:= Last
(Component_Associations
(Sub_Aggr
));
5408 if Nkind
(First
(Choice_List
(Assoc
))) = N_Others_Choice
then
5409 Others_Present
(Dim
) := True;
5413 -- Now look inside the subaggregate to see if there is more work
5415 if Dim
< Aggr_Dimension
then
5417 -- Process positional components
5419 if Present
(Expressions
(Sub_Aggr
)) then
5420 Expr
:= First
(Expressions
(Sub_Aggr
));
5421 while Present
(Expr
) loop
5422 Compute_Others_Present
(Expr
, Dim
+ 1);
5427 -- Process component associations
5429 if Present
(Component_Associations
(Sub_Aggr
)) then
5430 Assoc
:= First
(Component_Associations
(Sub_Aggr
));
5431 while Present
(Assoc
) loop
5432 Expr
:= Expression
(Assoc
);
5433 Compute_Others_Present
(Expr
, Dim
+ 1);
5438 end Compute_Others_Present
;
5440 ------------------------
5441 -- In_Place_Assign_OK --
5442 ------------------------
5444 function In_Place_Assign_OK
return Boolean is
5452 function Safe_Aggregate
(Aggr
: Node_Id
) return Boolean;
5453 -- Check recursively that each component of a (sub)aggregate does not
5454 -- depend on the variable being assigned to.
5456 function Safe_Component
(Expr
: Node_Id
) return Boolean;
5457 -- Verify that an expression cannot depend on the variable being
5458 -- assigned to. Room for improvement here (but less than before).
5460 --------------------
5461 -- Safe_Aggregate --
5462 --------------------
5464 function Safe_Aggregate
(Aggr
: Node_Id
) return Boolean is
5468 if Nkind
(Parent
(Aggr
)) = N_Iterated_Component_Association
then
5472 if Present
(Expressions
(Aggr
)) then
5473 Expr
:= First
(Expressions
(Aggr
));
5474 while Present
(Expr
) loop
5475 if Nkind
(Expr
) = N_Aggregate
then
5476 if not Safe_Aggregate
(Expr
) then
5480 elsif not Safe_Component
(Expr
) then
5488 if Present
(Component_Associations
(Aggr
)) then
5489 Expr
:= First
(Component_Associations
(Aggr
));
5490 while Present
(Expr
) loop
5491 if Nkind
(Expression
(Expr
)) = N_Aggregate
then
5492 if not Safe_Aggregate
(Expression
(Expr
)) then
5496 -- If association has a box, no way to determine yet
5497 -- whether default can be assigned in place.
5499 elsif Box_Present
(Expr
) then
5502 elsif not Safe_Component
(Expression
(Expr
)) then
5513 --------------------
5514 -- Safe_Component --
5515 --------------------
5517 function Safe_Component
(Expr
: Node_Id
) return Boolean is
5518 Comp
: Node_Id
:= Expr
;
5520 function Check_Component
(Comp
: Node_Id
) return Boolean;
5521 -- Do the recursive traversal, after copy
5523 ---------------------
5524 -- Check_Component --
5525 ---------------------
5527 function Check_Component
(Comp
: Node_Id
) return Boolean is
5529 if Is_Overloaded
(Comp
) then
5533 return Compile_Time_Known_Value
(Comp
)
5535 or else (Is_Entity_Name
(Comp
)
5536 and then Present
(Entity
(Comp
))
5537 and then No
(Renamed_Object
(Entity
(Comp
))))
5539 or else (Nkind
(Comp
) = N_Attribute_Reference
5540 and then Check_Component
(Prefix
(Comp
)))
5542 or else (Nkind
(Comp
) in N_Binary_Op
5543 and then Check_Component
(Left_Opnd
(Comp
))
5544 and then Check_Component
(Right_Opnd
(Comp
)))
5546 or else (Nkind
(Comp
) in N_Unary_Op
5547 and then Check_Component
(Right_Opnd
(Comp
)))
5549 or else (Nkind
(Comp
) = N_Selected_Component
5550 and then Check_Component
(Prefix
(Comp
)))
5552 or else (Nkind
(Comp
) = N_Unchecked_Type_Conversion
5553 and then Check_Component
(Expression
(Comp
)));
5554 end Check_Component
;
5556 -- Start of processing for Safe_Component
5559 -- If the component appears in an association that may correspond
5560 -- to more than one element, it is not analyzed before expansion
5561 -- into assignments, to avoid side effects. We analyze, but do not
5562 -- resolve the copy, to obtain sufficient entity information for
5563 -- the checks that follow. If component is overloaded we assume
5564 -- an unsafe function call.
5566 if not Analyzed
(Comp
) then
5567 if Is_Overloaded
(Expr
) then
5570 elsif Nkind
(Expr
) = N_Aggregate
5571 and then not Is_Others_Aggregate
(Expr
)
5575 elsif Nkind
(Expr
) = N_Allocator
then
5577 -- For now, too complex to analyze
5581 elsif Nkind
(Parent
(Expr
)) =
5582 N_Iterated_Component_Association
5584 -- Ditto for iterated component associations, which in
5585 -- general require an enclosing loop and involve nonstatic
5591 Comp
:= New_Copy_Tree
(Expr
);
5592 Set_Parent
(Comp
, Parent
(Expr
));
5596 if Nkind
(Comp
) = N_Aggregate
then
5597 return Safe_Aggregate
(Comp
);
5599 return Check_Component
(Comp
);
5603 -- Start of processing for In_Place_Assign_OK
5606 if Present
(Component_Associations
(N
)) then
5608 -- On assignment, sliding can take place, so we cannot do the
5609 -- assignment in place unless the bounds of the aggregate are
5610 -- statically equal to those of the target.
5612 -- If the aggregate is given by an others choice, the bounds are
5613 -- derived from the left-hand side, and the assignment is safe if
5614 -- the expression is.
5616 if Is_Others_Aggregate
(N
) then
5619 (Expression
(First
(Component_Associations
(N
))));
5622 Aggr_In
:= First_Index
(Etype
(N
));
5624 if Nkind
(Parent
(N
)) = N_Assignment_Statement
then
5625 Obj_In
:= First_Index
(Etype
(Name
(Parent
(N
))));
5628 -- Context is an allocator. Check bounds of aggregate against
5629 -- given type in qualified expression.
5631 pragma Assert
(Nkind
(Parent
(Parent
(N
))) = N_Allocator
);
5633 First_Index
(Etype
(Entity
(Subtype_Mark
(Parent
(N
)))));
5636 while Present
(Aggr_In
) loop
5637 Get_Index_Bounds
(Aggr_In
, Aggr_Lo
, Aggr_Hi
);
5638 Get_Index_Bounds
(Obj_In
, Obj_Lo
, Obj_Hi
);
5640 if not Compile_Time_Known_Value
(Aggr_Lo
)
5641 or else not Compile_Time_Known_Value
(Obj_Lo
)
5642 or else not Compile_Time_Known_Value
(Obj_Hi
)
5643 or else Expr_Value
(Aggr_Lo
) /= Expr_Value
(Obj_Lo
)
5647 -- For an assignment statement we require static matching of
5648 -- bounds. Ditto for an allocator whose qualified expression
5649 -- is a constrained type. If the expression in the allocator
5650 -- is an unconstrained array, we accept an upper bound that
5651 -- is not static, to allow for nonstatic expressions of the
5652 -- base type. Clearly there are further possibilities (with
5653 -- diminishing returns) for safely building arrays in place
5656 elsif Nkind
(Parent
(N
)) = N_Assignment_Statement
5657 or else Is_Constrained
(Etype
(Parent
(N
)))
5659 if not Compile_Time_Known_Value
(Aggr_Hi
)
5660 or else Expr_Value
(Aggr_Hi
) /= Expr_Value
(Obj_Hi
)
5666 Next_Index
(Aggr_In
);
5667 Next_Index
(Obj_In
);
5671 -- Now check the component values themselves
5673 return Safe_Aggregate
(N
);
5674 end In_Place_Assign_OK
;
5680 procedure Others_Check
(Sub_Aggr
: Node_Id
; Dim
: Pos
) is
5681 Aggr_Lo
: constant Node_Id
:= Aggr_Low
(Dim
);
5682 Aggr_Hi
: constant Node_Id
:= Aggr_High
(Dim
);
5683 -- The bounds of the aggregate for this dimension
5685 Ind_Typ
: constant Entity_Id
:= Aggr_Index_Typ
(Dim
);
5686 -- The index type for this dimension
5688 Need_To_Check
: Boolean := False;
5690 Choices_Lo
: Node_Id
:= Empty
;
5691 Choices_Hi
: Node_Id
:= Empty
;
5692 -- The lowest and highest discrete choices for a named subaggregate
5694 Nb_Choices
: Int
:= -1;
5695 -- The number of discrete non-others choices in this subaggregate
5697 Nb_Elements
: Uint
:= Uint_0
;
5698 -- The number of elements in a positional aggregate
5700 Cond
: Node_Id
:= Empty
;
5707 -- Check if we have an others choice. If we do make sure that this
5708 -- subaggregate contains at least one element in addition to the
5711 if Range_Checks_Suppressed
(Ind_Typ
) then
5712 Need_To_Check
:= False;
5714 elsif Present
(Expressions
(Sub_Aggr
))
5715 and then Present
(Component_Associations
(Sub_Aggr
))
5717 Need_To_Check
:= True;
5719 elsif Present
(Component_Associations
(Sub_Aggr
)) then
5720 Assoc
:= Last
(Component_Associations
(Sub_Aggr
));
5722 if Nkind
(First
(Choice_List
(Assoc
))) /= N_Others_Choice
then
5723 Need_To_Check
:= False;
5726 -- Count the number of discrete choices. Start with -1 because
5727 -- the others choice does not count.
5729 -- Is there some reason we do not use List_Length here ???
5732 Assoc
:= First
(Component_Associations
(Sub_Aggr
));
5733 while Present
(Assoc
) loop
5734 Choice
:= First
(Choice_List
(Assoc
));
5735 while Present
(Choice
) loop
5736 Nb_Choices
:= Nb_Choices
+ 1;
5743 -- If there is only an others choice nothing to do
5745 Need_To_Check
:= (Nb_Choices
> 0);
5749 Need_To_Check
:= False;
5752 -- If we are dealing with a positional subaggregate with an others
5753 -- choice then compute the number or positional elements.
5755 if Need_To_Check
and then Present
(Expressions
(Sub_Aggr
)) then
5756 Expr
:= First
(Expressions
(Sub_Aggr
));
5757 Nb_Elements
:= Uint_0
;
5758 while Present
(Expr
) loop
5759 Nb_Elements
:= Nb_Elements
+ 1;
5763 -- If the aggregate contains discrete choices and an others choice
5764 -- compute the smallest and largest discrete choice values.
5766 elsif Need_To_Check
then
5767 Compute_Choices_Lo_And_Choices_Hi
: declare
5769 Table
: Case_Table_Type
(1 .. Nb_Choices
);
5770 -- Used to sort all the different choice values
5777 Assoc
:= First
(Component_Associations
(Sub_Aggr
));
5778 while Present
(Assoc
) loop
5779 Choice
:= First
(Choice_List
(Assoc
));
5780 while Present
(Choice
) loop
5781 if Nkind
(Choice
) = N_Others_Choice
then
5785 Get_Index_Bounds
(Choice
, Low
, High
);
5786 Table
(J
).Choice_Lo
:= Low
;
5787 Table
(J
).Choice_Hi
:= High
;
5796 -- Sort the discrete choices
5798 Sort_Case_Table
(Table
);
5800 Choices_Lo
:= Table
(1).Choice_Lo
;
5801 Choices_Hi
:= Table
(Nb_Choices
).Choice_Hi
;
5802 end Compute_Choices_Lo_And_Choices_Hi
;
5805 -- If no others choice in this subaggregate, or the aggregate
5806 -- comprises only an others choice, nothing to do.
5808 if not Need_To_Check
then
5811 -- If we are dealing with an aggregate containing an others choice
5812 -- and positional components, we generate the following test:
5814 -- if Ind_Typ'Pos (Aggr_Lo) + (Nb_Elements - 1) >
5815 -- Ind_Typ'Pos (Aggr_Hi)
5817 -- raise Constraint_Error;
5820 elsif Nb_Elements
> Uint_0
then
5826 Make_Attribute_Reference
(Loc
,
5827 Prefix
=> New_Occurrence_Of
(Ind_Typ
, Loc
),
5828 Attribute_Name
=> Name_Pos
,
5831 (Duplicate_Subexpr_Move_Checks
(Aggr_Lo
))),
5832 Right_Opnd
=> Make_Integer_Literal
(Loc
, Nb_Elements
- 1)),
5835 Make_Attribute_Reference
(Loc
,
5836 Prefix
=> New_Occurrence_Of
(Ind_Typ
, Loc
),
5837 Attribute_Name
=> Name_Pos
,
5838 Expressions
=> New_List
(
5839 Duplicate_Subexpr_Move_Checks
(Aggr_Hi
))));
5841 -- If we are dealing with an aggregate containing an others choice
5842 -- and discrete choices we generate the following test:
5844 -- [constraint_error when
5845 -- Choices_Lo < Aggr_Lo or else Choices_Hi > Aggr_Hi];
5852 Left_Opnd
=> Duplicate_Subexpr_Move_Checks
(Choices_Lo
),
5853 Right_Opnd
=> Duplicate_Subexpr_Move_Checks
(Aggr_Lo
)),
5857 Left_Opnd
=> Duplicate_Subexpr
(Choices_Hi
),
5858 Right_Opnd
=> Duplicate_Subexpr
(Aggr_Hi
)));
5861 if Present
(Cond
) then
5863 Make_Raise_Constraint_Error
(Loc
,
5865 Reason
=> CE_Length_Check_Failed
));
5866 -- Questionable reason code, shouldn't that be a
5867 -- CE_Range_Check_Failed ???
5870 -- Now look inside the subaggregate to see if there is more work
5872 if Dim
< Aggr_Dimension
then
5874 -- Process positional components
5876 if Present
(Expressions
(Sub_Aggr
)) then
5877 Expr
:= First
(Expressions
(Sub_Aggr
));
5878 while Present
(Expr
) loop
5879 Others_Check
(Expr
, Dim
+ 1);
5884 -- Process component associations
5886 if Present
(Component_Associations
(Sub_Aggr
)) then
5887 Assoc
:= First
(Component_Associations
(Sub_Aggr
));
5888 while Present
(Assoc
) loop
5889 Expr
:= Expression
(Assoc
);
5890 Others_Check
(Expr
, Dim
+ 1);
5897 -------------------------
5898 -- Safe_Left_Hand_Side --
5899 -------------------------
5901 function Safe_Left_Hand_Side
(N
: Node_Id
) return Boolean is
5902 function Is_Safe_Index
(Indx
: Node_Id
) return Boolean;
5903 -- If the left-hand side includes an indexed component, check that
5904 -- the indexes are free of side effects.
5910 function Is_Safe_Index
(Indx
: Node_Id
) return Boolean is
5912 if Is_Entity_Name
(Indx
) then
5915 elsif Nkind
(Indx
) = N_Integer_Literal
then
5918 elsif Nkind
(Indx
) = N_Function_Call
5919 and then Is_Entity_Name
(Name
(Indx
))
5920 and then Has_Pragma_Pure_Function
(Entity
(Name
(Indx
)))
5924 elsif Nkind
(Indx
) = N_Type_Conversion
5925 and then Is_Safe_Index
(Expression
(Indx
))
5934 -- Start of processing for Safe_Left_Hand_Side
5937 if Is_Entity_Name
(N
) then
5940 elsif Nkind_In
(N
, N_Explicit_Dereference
, N_Selected_Component
)
5941 and then Safe_Left_Hand_Side
(Prefix
(N
))
5945 elsif Nkind
(N
) = N_Indexed_Component
5946 and then Safe_Left_Hand_Side
(Prefix
(N
))
5947 and then Is_Safe_Index
(First
(Expressions
(N
)))
5951 elsif Nkind
(N
) = N_Unchecked_Type_Conversion
then
5952 return Safe_Left_Hand_Side
(Expression
(N
));
5957 end Safe_Left_Hand_Side
;
5962 -- Holds the temporary aggregate value
5965 -- Holds the declaration of Tmp
5967 Aggr_Code
: List_Id
;
5968 Parent_Node
: Node_Id
;
5969 Parent_Kind
: Node_Kind
;
5971 -- Start of processing for Expand_Array_Aggregate
5974 -- Do not touch the special aggregates of attributes used for Asm calls
5976 if Is_RTE
(Ctyp
, RE_Asm_Input_Operand
)
5977 or else Is_RTE
(Ctyp
, RE_Asm_Output_Operand
)
5981 -- Do not expand an aggregate for an array type which contains tasks if
5982 -- the aggregate is associated with an unexpanded return statement of a
5983 -- build-in-place function. The aggregate is expanded when the related
5984 -- return statement (rewritten into an extended return) is processed.
5985 -- This delay ensures that any temporaries and initialization code
5986 -- generated for the aggregate appear in the proper return block and
5987 -- use the correct _chain and _master.
5989 elsif Has_Task
(Base_Type
(Etype
(N
)))
5990 and then Nkind
(Parent
(N
)) = N_Simple_Return_Statement
5991 and then Is_Build_In_Place_Function
5992 (Return_Applies_To
(Return_Statement_Entity
(Parent
(N
))))
5996 -- Do not attempt expansion if error already detected. We may reach this
5997 -- point in spite of previous errors when compiling with -gnatq, to
5998 -- force all possible errors (this is the usual ACATS mode).
6000 elsif Error_Posted
(N
) then
6004 -- If the semantic analyzer has determined that aggregate N will raise
6005 -- Constraint_Error at run time, then the aggregate node has been
6006 -- replaced with an N_Raise_Constraint_Error node and we should
6009 pragma Assert
(not Raises_Constraint_Error
(N
));
6013 -- Check that the index range defined by aggregate bounds is
6014 -- compatible with corresponding index subtype.
6016 Index_Compatibility_Check
: declare
6017 Aggr_Index_Range
: Node_Id
:= First_Index
(Typ
);
6018 -- The current aggregate index range
6020 Index_Constraint
: Node_Id
:= First_Index
(Etype
(Typ
));
6021 -- The corresponding index constraint against which we have to
6022 -- check the above aggregate index range.
6025 Compute_Others_Present
(N
, 1);
6027 for J
in 1 .. Aggr_Dimension
loop
6028 -- There is no need to emit a check if an others choice is present
6029 -- for this array aggregate dimension since in this case one of
6030 -- N's subaggregates has taken its bounds from the context and
6031 -- these bounds must have been checked already. In addition all
6032 -- subaggregates corresponding to the same dimension must all have
6033 -- the same bounds (checked in (c) below).
6035 if not Range_Checks_Suppressed
(Etype
(Index_Constraint
))
6036 and then not Others_Present
(J
)
6038 -- We don't use Checks.Apply_Range_Check here because it emits
6039 -- a spurious check. Namely it checks that the range defined by
6040 -- the aggregate bounds is nonempty. But we know this already
6043 Check_Bounds
(Aggr_Index_Range
, Index_Constraint
);
6046 -- Save the low and high bounds of the aggregate index as well as
6047 -- the index type for later use in checks (b) and (c) below.
6049 Aggr_Low
(J
) := Low_Bound
(Aggr_Index_Range
);
6050 Aggr_High
(J
) := High_Bound
(Aggr_Index_Range
);
6052 Aggr_Index_Typ
(J
) := Etype
(Index_Constraint
);
6054 Next_Index
(Aggr_Index_Range
);
6055 Next_Index
(Index_Constraint
);
6057 end Index_Compatibility_Check
;
6061 -- If an others choice is present check that no aggregate index is
6062 -- outside the bounds of the index constraint.
6064 Others_Check
(N
, 1);
6068 -- For multidimensional arrays make sure that all subaggregates
6069 -- corresponding to the same dimension have the same bounds.
6071 if Aggr_Dimension
> 1 then
6072 Check_Same_Aggr_Bounds
(N
, 1);
6077 -- If we have a default component value, or simple initialization is
6078 -- required for the component type, then we replace <> in component
6079 -- associations by the required default value.
6082 Default_Val
: Node_Id
;
6086 if (Present
(Default_Aspect_Component_Value
(Typ
))
6087 or else Needs_Simple_Initialization
(Ctyp
))
6088 and then Present
(Component_Associations
(N
))
6090 Assoc
:= First
(Component_Associations
(N
));
6091 while Present
(Assoc
) loop
6092 if Nkind
(Assoc
) = N_Component_Association
6093 and then Box_Present
(Assoc
)
6095 Set_Box_Present
(Assoc
, False);
6097 if Present
(Default_Aspect_Component_Value
(Typ
)) then
6098 Default_Val
:= Default_Aspect_Component_Value
(Typ
);
6100 Default_Val
:= Get_Simple_Init_Val
(Ctyp
, N
);
6103 Set_Expression
(Assoc
, New_Copy_Tree
(Default_Val
));
6104 Analyze_And_Resolve
(Expression
(Assoc
), Ctyp
);
6114 -- Here we test for is packed array aggregate that we can handle at
6115 -- compile time. If so, return with transformation done. Note that we do
6116 -- this even if the aggregate is nested, because once we have done this
6117 -- processing, there is no more nested aggregate.
6119 if Packed_Array_Aggregate_Handled
(N
) then
6123 -- At this point we try to convert to positional form
6125 if Ekind
(Current_Scope
) = E_Package
6126 and then Static_Elaboration_Desired
(Current_Scope
)
6128 Convert_To_Positional
(N
, Max_Others_Replicate
=> 100);
6130 Convert_To_Positional
(N
);
6133 -- if the result is no longer an aggregate (e.g. it may be a string
6134 -- literal, or a temporary which has the needed value), then we are
6135 -- done, since there is no longer a nested aggregate.
6137 if Nkind
(N
) /= N_Aggregate
then
6140 -- We are also done if the result is an analyzed aggregate, indicating
6141 -- that Convert_To_Positional succeeded and reanalyzed the rewritten
6144 elsif Analyzed
(N
) and then Is_Rewrite_Substitution
(N
) then
6148 -- If all aggregate components are compile-time known and the aggregate
6149 -- has been flattened, nothing left to do. The same occurs if the
6150 -- aggregate is used to initialize the components of a statically
6151 -- allocated dispatch table.
6153 if Compile_Time_Known_Aggregate
(N
)
6154 or else Is_Static_Dispatch_Table_Aggregate
(N
)
6156 Set_Expansion_Delayed
(N
, False);
6160 -- Now see if back end processing is possible
6162 if Backend_Processing_Possible
(N
) then
6164 -- If the aggregate is static but the constraints are not, build
6165 -- a static subtype for the aggregate, so that Gigi can place it
6166 -- in static memory. Perform an unchecked_conversion to the non-
6167 -- static type imposed by the context.
6170 Itype
: constant Entity_Id
:= Etype
(N
);
6172 Needs_Type
: Boolean := False;
6175 Index
:= First_Index
(Itype
);
6176 while Present
(Index
) loop
6177 if not Is_OK_Static_Subtype
(Etype
(Index
)) then
6186 Build_Constrained_Type
(Positional
=> True);
6187 Rewrite
(N
, Unchecked_Convert_To
(Itype
, N
));
6197 -- Delay expansion for nested aggregates: it will be taken care of when
6198 -- the parent aggregate is expanded.
6200 Parent_Node
:= Parent
(N
);
6201 Parent_Kind
:= Nkind
(Parent_Node
);
6203 if Parent_Kind
= N_Qualified_Expression
then
6204 Parent_Node
:= Parent
(Parent_Node
);
6205 Parent_Kind
:= Nkind
(Parent_Node
);
6208 if Parent_Kind
= N_Aggregate
6209 or else Parent_Kind
= N_Extension_Aggregate
6210 or else Parent_Kind
= N_Component_Association
6211 or else (Parent_Kind
= N_Object_Declaration
6212 and then Needs_Finalization
(Typ
))
6213 or else (Parent_Kind
= N_Assignment_Statement
6214 and then Inside_Init_Proc
)
6216 Set_Expansion_Delayed
(N
, not Static_Array_Aggregate
(N
));
6222 -- Look if in place aggregate expansion is possible
6224 -- For object declarations we build the aggregate in place, unless
6225 -- the array is bit-packed.
6227 -- For assignments we do the assignment in place if all the component
6228 -- associations have compile-time known values, or are default-
6229 -- initialized limited components, e.g. tasks. For other cases we
6230 -- create a temporary. The analysis for safety of on-line assignment
6231 -- is delicate, i.e. we don't know how to do it fully yet ???
6233 -- For allocators we assign to the designated object in place if the
6234 -- aggregate meets the same conditions as other in-place assignments.
6235 -- In this case the aggregate may not come from source but was created
6236 -- for default initialization, e.g. with Initialize_Scalars.
6238 if Requires_Transient_Scope
(Typ
) then
6239 Establish_Transient_Scope
(N
, Manage_Sec_Stack
=> False);
6242 -- An array of limited components is built in place
6244 if Is_Limited_Type
(Typ
) then
6245 Maybe_In_Place_OK
:= True;
6247 elsif Has_Default_Init_Comps
(N
) then
6248 Maybe_In_Place_OK
:= False;
6250 elsif Is_Bit_Packed_Array
(Typ
)
6251 or else Has_Controlled_Component
(Typ
)
6253 Maybe_In_Place_OK
:= False;
6256 Maybe_In_Place_OK
:=
6257 (Nkind
(Parent
(N
)) = N_Assignment_Statement
6258 and then In_Place_Assign_OK
)
6261 (Nkind
(Parent
(Parent
(N
))) = N_Allocator
6262 and then In_Place_Assign_OK
);
6265 -- If this is an array of tasks, it will be expanded into build-in-place
6266 -- assignments. Build an activation chain for the tasks now.
6268 if Has_Task
(Etype
(N
)) then
6269 Build_Activation_Chain_Entity
(N
);
6272 -- Perform in-place expansion of aggregate in an object declaration.
6273 -- Note: actions generated for the aggregate will be captured in an
6274 -- expression-with-actions statement so that they can be transferred
6275 -- to freeze actions later if there is an address clause for the
6276 -- object. (Note: we don't use a block statement because this would
6277 -- cause generated freeze nodes to be elaborated in the wrong scope).
6279 -- Do not perform in-place expansion for SPARK 05 because aggregates are
6280 -- expected to appear in qualified form. In-place expansion eliminates
6281 -- the qualification and eventually violates this SPARK 05 restiction.
6283 -- Arrays of limited components must be built in place. The code
6284 -- previously excluded controlled components but this is an old
6285 -- oversight: the rules in 7.6 (17) are clear.
6287 if (not Has_Default_Init_Comps
(N
)
6288 or else Is_Limited_Type
(Etype
(N
)))
6289 and then Comes_From_Source
(Parent_Node
)
6290 and then Parent_Kind
= N_Object_Declaration
6291 and then Present
(Expression
(Parent_Node
))
6293 Must_Slide
(Etype
(Defining_Identifier
(Parent_Node
)), Typ
)
6294 and then not Is_Bit_Packed_Array
(Typ
)
6295 and then not Restriction_Check_Required
(SPARK_05
)
6297 In_Place_Assign_OK_For_Declaration
:= True;
6298 Tmp
:= Defining_Identifier
(Parent_Node
);
6299 Set_No_Initialization
(Parent_Node
);
6300 Set_Expression
(Parent_Node
, Empty
);
6302 -- Set kind and type of the entity, for use in the analysis
6303 -- of the subsequent assignments. If the nominal type is not
6304 -- constrained, build a subtype from the known bounds of the
6305 -- aggregate. If the declaration has a subtype mark, use it,
6306 -- otherwise use the itype of the aggregate.
6308 Set_Ekind
(Tmp
, E_Variable
);
6310 if not Is_Constrained
(Typ
) then
6311 Build_Constrained_Type
(Positional
=> False);
6313 elsif Is_Entity_Name
(Object_Definition
(Parent_Node
))
6314 and then Is_Constrained
(Entity
(Object_Definition
(Parent_Node
)))
6316 Set_Etype
(Tmp
, Entity
(Object_Definition
(Parent_Node
)));
6319 Set_Size_Known_At_Compile_Time
(Typ
, False);
6320 Set_Etype
(Tmp
, Typ
);
6323 elsif Maybe_In_Place_OK
6324 and then Nkind
(Parent
(N
)) = N_Qualified_Expression
6325 and then Nkind
(Parent
(Parent
(N
))) = N_Allocator
6327 Set_Expansion_Delayed
(N
);
6330 -- Limited arrays in return statements are expanded when
6331 -- enclosing construct is expanded.
6333 elsif Maybe_In_Place_OK
6334 and then Nkind
(Parent
(N
)) = N_Simple_Return_Statement
6336 Set_Expansion_Delayed
(N
);
6339 -- In the remaining cases the aggregate is the RHS of an assignment
6341 elsif Maybe_In_Place_OK
6342 and then Safe_Left_Hand_Side
(Name
(Parent
(N
)))
6344 Tmp
:= Name
(Parent
(N
));
6346 if Etype
(Tmp
) /= Etype
(N
) then
6347 Apply_Length_Check
(N
, Etype
(Tmp
));
6349 if Nkind
(N
) = N_Raise_Constraint_Error
then
6351 -- Static error, nothing further to expand
6357 -- If a slice assignment has an aggregate with a single others_choice,
6358 -- the assignment can be done in place even if bounds are not static,
6359 -- by converting it into a loop over the discrete range of the slice.
6361 elsif Maybe_In_Place_OK
6362 and then Nkind
(Name
(Parent
(N
))) = N_Slice
6363 and then Is_Others_Aggregate
(N
)
6365 Tmp
:= Name
(Parent
(N
));
6367 -- Set type of aggregate to be type of lhs in assignment, in order
6368 -- to suppress redundant length checks.
6370 Set_Etype
(N
, Etype
(Tmp
));
6374 -- In place aggregate expansion is not possible
6377 Maybe_In_Place_OK
:= False;
6378 Tmp
:= Make_Temporary
(Loc
, 'A', N
);
6380 Make_Object_Declaration
(Loc
,
6381 Defining_Identifier
=> Tmp
,
6382 Object_Definition
=> New_Occurrence_Of
(Typ
, Loc
));
6383 Set_No_Initialization
(Tmp_Decl
, True);
6384 Set_Warnings_Off
(Tmp
);
6386 -- If we are within a loop, the temporary will be pushed on the
6387 -- stack at each iteration. If the aggregate is the expression
6388 -- for an allocator, it will be immediately copied to the heap
6389 -- and can be reclaimed at once. We create a transient scope
6390 -- around the aggregate for this purpose.
6392 if Ekind
(Current_Scope
) = E_Loop
6393 and then Nkind
(Parent
(Parent
(N
))) = N_Allocator
6395 Establish_Transient_Scope
(N
, Manage_Sec_Stack
=> False);
6398 Insert_Action
(N
, Tmp_Decl
);
6401 -- Construct and insert the aggregate code. We can safely suppress index
6402 -- checks because this code is guaranteed not to raise CE on index
6403 -- checks. However we should *not* suppress all checks.
6409 if Nkind
(Tmp
) = N_Defining_Identifier
then
6410 Target
:= New_Occurrence_Of
(Tmp
, Loc
);
6413 if Has_Default_Init_Comps
(N
)
6414 and then not Maybe_In_Place_OK
6416 -- Ada 2005 (AI-287): This case has not been analyzed???
6418 raise Program_Error
;
6421 -- Name in assignment is explicit dereference
6423 Target
:= New_Copy
(Tmp
);
6426 -- If we are to generate an in place assignment for a declaration or
6427 -- an assignment statement, and the assignment can be done directly
6428 -- by the back end, then do not expand further.
6430 -- ??? We can also do that if in place expansion is not possible but
6431 -- then we could go into an infinite recursion.
6433 if (In_Place_Assign_OK_For_Declaration
or else Maybe_In_Place_OK
)
6434 and then not CodePeer_Mode
6435 and then not Modify_Tree_For_C
6436 and then not Possible_Bit_Aligned_Component
(Target
)
6437 and then not Is_Possibly_Unaligned_Slice
(Target
)
6438 and then Aggr_Assignment_OK_For_Backend
(N
)
6440 if Maybe_In_Place_OK
then
6446 Make_Assignment_Statement
(Loc
,
6448 Expression
=> New_Copy_Tree
(N
)));
6452 Build_Array_Aggr_Code
(N
,
6454 Index
=> First_Index
(Typ
),
6456 Scalar_Comp
=> Is_Scalar_Type
(Ctyp
));
6459 -- Save the last assignment statement associated with the aggregate
6460 -- when building a controlled object. This reference is utilized by
6461 -- the finalization machinery when marking an object as successfully
6464 if Needs_Finalization
(Typ
)
6465 and then Is_Entity_Name
(Target
)
6466 and then Present
(Entity
(Target
))
6467 and then Ekind_In
(Entity
(Target
), E_Constant
, E_Variable
)
6469 Set_Last_Aggregate_Assignment
(Entity
(Target
), Last
(Aggr_Code
));
6473 -- If the aggregate is the expression in a declaration, the expanded
6474 -- code must be inserted after it. The defining entity might not come
6475 -- from source if this is part of an inlined body, but the declaration
6478 if Comes_From_Source
(Tmp
)
6480 (Nkind
(Parent
(N
)) = N_Object_Declaration
6481 and then Comes_From_Source
(Parent
(N
))
6482 and then Tmp
= Defining_Entity
(Parent
(N
)))
6485 Node_After
: constant Node_Id
:= Next
(Parent_Node
);
6488 Insert_Actions_After
(Parent_Node
, Aggr_Code
);
6490 if Parent_Kind
= N_Object_Declaration
then
6491 Collect_Initialization_Statements
6492 (Obj
=> Tmp
, N
=> Parent_Node
, Node_After
=> Node_After
);
6497 Insert_Actions
(N
, Aggr_Code
);
6500 -- If the aggregate has been assigned in place, remove the original
6503 if Nkind
(Parent
(N
)) = N_Assignment_Statement
6504 and then Maybe_In_Place_OK
6506 Rewrite
(Parent
(N
), Make_Null_Statement
(Loc
));
6508 elsif Nkind
(Parent
(N
)) /= N_Object_Declaration
6509 or else Tmp
/= Defining_Identifier
(Parent
(N
))
6511 Rewrite
(N
, New_Occurrence_Of
(Tmp
, Loc
));
6512 Analyze_And_Resolve
(N
, Typ
);
6514 end Expand_Array_Aggregate
;
6516 ------------------------
6517 -- Expand_N_Aggregate --
6518 ------------------------
6520 procedure Expand_N_Aggregate
(N
: Node_Id
) is
6522 -- Record aggregate case
6524 if Is_Record_Type
(Etype
(N
)) then
6525 Expand_Record_Aggregate
(N
);
6527 -- Array aggregate case
6530 -- A special case, if we have a string subtype with bounds 1 .. N,
6531 -- where N is known at compile time, and the aggregate is of the
6532 -- form (others => 'x'), with a single choice and no expressions,
6533 -- and N is less than 80 (an arbitrary limit for now), then replace
6534 -- the aggregate by the equivalent string literal (but do not mark
6535 -- it as static since it is not).
6537 -- Note: this entire circuit is redundant with respect to code in
6538 -- Expand_Array_Aggregate that collapses others choices to positional
6539 -- form, but there are two problems with that circuit:
6541 -- a) It is limited to very small cases due to ill-understood
6542 -- interactions with bootstrapping. That limit is removed by
6543 -- use of the No_Implicit_Loops restriction.
6545 -- b) It incorrectly ends up with the resulting expressions being
6546 -- considered static when they are not. For example, the
6547 -- following test should fail:
6549 -- pragma Restrictions (No_Implicit_Loops);
6550 -- package NonSOthers4 is
6551 -- B : constant String (1 .. 6) := (others => 'A');
6552 -- DH : constant String (1 .. 8) := B & "BB";
6554 -- pragma Export (C, X, Link_Name => DH);
6557 -- But it succeeds (DH looks static to pragma Export)
6559 -- To be sorted out ???
6561 if Present
(Component_Associations
(N
)) then
6563 CA
: constant Node_Id
:= First
(Component_Associations
(N
));
6564 MX
: constant := 80;
6567 if Nkind
(First
(Choice_List
(CA
))) = N_Others_Choice
6568 and then Nkind
(Expression
(CA
)) = N_Character_Literal
6569 and then No
(Expressions
(N
))
6572 T
: constant Entity_Id
:= Etype
(N
);
6573 X
: constant Node_Id
:= First_Index
(T
);
6574 EC
: constant Node_Id
:= Expression
(CA
);
6575 CV
: constant Uint
:= Char_Literal_Value
(EC
);
6576 CC
: constant Int
:= UI_To_Int
(CV
);
6579 if Nkind
(X
) = N_Range
6580 and then Compile_Time_Known_Value
(Low_Bound
(X
))
6581 and then Expr_Value
(Low_Bound
(X
)) = 1
6582 and then Compile_Time_Known_Value
(High_Bound
(X
))
6585 Hi
: constant Uint
:= Expr_Value
(High_Bound
(X
));
6591 for J
in 1 .. UI_To_Int
(Hi
) loop
6592 Store_String_Char
(Char_Code
(CC
));
6596 Make_String_Literal
(Sloc
(N
),
6597 Strval
=> End_String
));
6599 if CC
>= Int
(2 ** 16) then
6600 Set_Has_Wide_Wide_Character
(N
);
6601 elsif CC
>= Int
(2 ** 8) then
6602 Set_Has_Wide_Character
(N
);
6605 Analyze_And_Resolve
(N
, T
);
6606 Set_Is_Static_Expression
(N
, False);
6616 -- Not that special case, so normal expansion of array aggregate
6618 Expand_Array_Aggregate
(N
);
6622 when RE_Not_Available
=>
6624 end Expand_N_Aggregate
;
6626 ------------------------------
6627 -- Expand_N_Delta_Aggregate --
6628 ------------------------------
6630 procedure Expand_N_Delta_Aggregate
(N
: Node_Id
) is
6631 Loc
: constant Source_Ptr
:= Sloc
(N
);
6632 Typ
: constant Entity_Id
:= Etype
(N
);
6637 Make_Object_Declaration
(Loc
,
6638 Defining_Identifier
=> Make_Temporary
(Loc
, 'T'),
6639 Object_Definition
=> New_Occurrence_Of
(Typ
, Loc
),
6640 Expression
=> New_Copy_Tree
(Expression
(N
)));
6642 if Is_Array_Type
(Etype
(N
)) then
6643 Expand_Delta_Array_Aggregate
(N
, New_List
(Decl
));
6645 Expand_Delta_Record_Aggregate
(N
, New_List
(Decl
));
6647 end Expand_N_Delta_Aggregate
;
6649 ----------------------------------
6650 -- Expand_Delta_Array_Aggregate --
6651 ----------------------------------
6653 procedure Expand_Delta_Array_Aggregate
(N
: Node_Id
; Deltas
: List_Id
) is
6654 Loc
: constant Source_Ptr
:= Sloc
(N
);
6655 Temp
: constant Entity_Id
:= Defining_Identifier
(First
(Deltas
));
6658 function Generate_Loop
(C
: Node_Id
) return Node_Id
;
6659 -- Generate a loop containing individual component assignments for
6660 -- choices that are ranges, subtype indications, subtype names, and
6661 -- iterated component associations.
6667 function Generate_Loop
(C
: Node_Id
) return Node_Id
is
6668 Sl
: constant Source_Ptr
:= Sloc
(C
);
6672 if Nkind
(Parent
(C
)) = N_Iterated_Component_Association
then
6674 Make_Defining_Identifier
(Loc
,
6675 Chars
=> (Chars
(Defining_Identifier
(Parent
(C
)))));
6677 Ix
:= Make_Temporary
(Sl
, 'I');
6681 Make_Loop_Statement
(Loc
,
6683 Make_Iteration_Scheme
(Sl
,
6684 Loop_Parameter_Specification
=>
6685 Make_Loop_Parameter_Specification
(Sl
,
6686 Defining_Identifier
=> Ix
,
6687 Discrete_Subtype_Definition
=> New_Copy_Tree
(C
))),
6689 Statements
=> New_List
(
6690 Make_Assignment_Statement
(Sl
,
6692 Make_Indexed_Component
(Sl
,
6693 Prefix
=> New_Occurrence_Of
(Temp
, Sl
),
6694 Expressions
=> New_List
(New_Occurrence_Of
(Ix
, Sl
))),
6695 Expression
=> New_Copy_Tree
(Expression
(Assoc
)))),
6696 End_Label
=> Empty
);
6703 -- Start of processing for Expand_Delta_Array_Aggregate
6706 Assoc
:= First
(Component_Associations
(N
));
6707 while Present
(Assoc
) loop
6708 Choice
:= First
(Choice_List
(Assoc
));
6709 if Nkind
(Assoc
) = N_Iterated_Component_Association
then
6710 while Present
(Choice
) loop
6711 Append_To
(Deltas
, Generate_Loop
(Choice
));
6716 while Present
(Choice
) loop
6718 -- Choice can be given by a range, a subtype indication, a
6719 -- subtype name, a scalar value, or an entity.
6721 if Nkind
(Choice
) = N_Range
6722 or else (Is_Entity_Name
(Choice
)
6723 and then Is_Type
(Entity
(Choice
)))
6725 Append_To
(Deltas
, Generate_Loop
(Choice
));
6727 elsif Nkind
(Choice
) = N_Subtype_Indication
then
6729 Generate_Loop
(Range_Expression
(Constraint
(Choice
))));
6733 Make_Assignment_Statement
(Sloc
(Choice
),
6735 Make_Indexed_Component
(Sloc
(Choice
),
6736 Prefix
=> New_Occurrence_Of
(Temp
, Loc
),
6737 Expressions
=> New_List
(New_Copy_Tree
(Choice
))),
6738 Expression
=> New_Copy_Tree
(Expression
(Assoc
))));
6748 Insert_Actions
(N
, Deltas
);
6749 Rewrite
(N
, New_Occurrence_Of
(Temp
, Loc
));
6750 end Expand_Delta_Array_Aggregate
;
6752 -----------------------------------
6753 -- Expand_Delta_Record_Aggregate --
6754 -----------------------------------
6756 procedure Expand_Delta_Record_Aggregate
(N
: Node_Id
; Deltas
: List_Id
) is
6757 Loc
: constant Source_Ptr
:= Sloc
(N
);
6758 Temp
: constant Entity_Id
:= Defining_Identifier
(First
(Deltas
));
6763 Assoc
:= First
(Component_Associations
(N
));
6765 while Present
(Assoc
) loop
6766 Choice
:= First
(Choice_List
(Assoc
));
6767 while Present
(Choice
) loop
6769 Make_Assignment_Statement
(Sloc
(Choice
),
6771 Make_Selected_Component
(Sloc
(Choice
),
6772 Prefix
=> New_Occurrence_Of
(Temp
, Loc
),
6773 Selector_Name
=> Make_Identifier
(Loc
, Chars
(Choice
))),
6774 Expression
=> New_Copy_Tree
(Expression
(Assoc
))));
6781 Insert_Actions
(N
, Deltas
);
6782 Rewrite
(N
, New_Occurrence_Of
(Temp
, Loc
));
6783 end Expand_Delta_Record_Aggregate
;
6785 ----------------------------------
6786 -- Expand_N_Extension_Aggregate --
6787 ----------------------------------
6789 -- If the ancestor part is an expression, add a component association for
6790 -- the parent field. If the type of the ancestor part is not the direct
6791 -- parent of the expected type, build recursively the needed ancestors.
6792 -- If the ancestor part is a subtype_mark, replace aggregate with a
6793 -- declaration for a temporary of the expected type, followed by
6794 -- individual assignments to the given components.
6796 procedure Expand_N_Extension_Aggregate
(N
: Node_Id
) is
6797 A
: constant Node_Id
:= Ancestor_Part
(N
);
6798 Loc
: constant Source_Ptr
:= Sloc
(N
);
6799 Typ
: constant Entity_Id
:= Etype
(N
);
6802 -- If the ancestor is a subtype mark, an init proc must be called
6803 -- on the resulting object which thus has to be materialized in
6806 if Is_Entity_Name
(A
) and then Is_Type
(Entity
(A
)) then
6807 Convert_To_Assignments
(N
, Typ
);
6809 -- The extension aggregate is transformed into a record aggregate
6810 -- of the following form (c1 and c2 are inherited components)
6812 -- (Exp with c3 => a, c4 => b)
6813 -- ==> (c1 => Exp.c1, c2 => Exp.c2, c3 => a, c4 => b)
6818 if Tagged_Type_Expansion
then
6819 Expand_Record_Aggregate
(N
,
6822 (Node
(First_Elmt
(Access_Disp_Table
(Typ
))), Loc
),
6825 -- No tag is needed in the case of a VM
6828 Expand_Record_Aggregate
(N
, Parent_Expr
=> A
);
6833 when RE_Not_Available
=>
6835 end Expand_N_Extension_Aggregate
;
6837 -----------------------------
6838 -- Expand_Record_Aggregate --
6839 -----------------------------
6841 procedure Expand_Record_Aggregate
6843 Orig_Tag
: Node_Id
:= Empty
;
6844 Parent_Expr
: Node_Id
:= Empty
)
6846 Loc
: constant Source_Ptr
:= Sloc
(N
);
6847 Comps
: constant List_Id
:= Component_Associations
(N
);
6848 Typ
: constant Entity_Id
:= Etype
(N
);
6849 Base_Typ
: constant Entity_Id
:= Base_Type
(Typ
);
6851 Static_Components
: Boolean := True;
6852 -- Flag to indicate whether all components are compile-time known,
6853 -- and the aggregate can be constructed statically and handled by
6854 -- the back-end. Set to False by Component_OK_For_Backend.
6856 procedure Build_Back_End_Aggregate
;
6857 -- Build a proper aggregate to be handled by the back-end
6859 function Compile_Time_Known_Composite_Value
(N
: Node_Id
) return Boolean;
6860 -- Returns true if N is an expression of composite type which can be
6861 -- fully evaluated at compile time without raising constraint error.
6862 -- Such expressions can be passed as is to Gigi without any expansion.
6864 -- This returns true for N_Aggregate with Compile_Time_Known_Aggregate
6865 -- set and constants whose expression is such an aggregate, recursively.
6867 function Component_OK_For_Backend
return Boolean;
6868 -- Check for presence of a component which makes it impossible for the
6869 -- backend to process the aggregate, thus requiring the use of a series
6870 -- of assignment statements. Cases checked for are a nested aggregate
6871 -- needing Late_Expansion, the presence of a tagged component which may
6872 -- need tag adjustment, and a bit unaligned component reference.
6874 -- We also force expansion into assignments if a component is of a
6875 -- mutable type (including a private type with discriminants) because
6876 -- in that case the size of the component to be copied may be smaller
6877 -- than the side of the target, and there is no simple way for gigi
6878 -- to compute the size of the object to be copied.
6880 -- NOTE: This is part of the ongoing work to define precisely the
6881 -- interface between front-end and back-end handling of aggregates.
6882 -- In general it is desirable to pass aggregates as they are to gigi,
6883 -- in order to minimize elaboration code. This is one case where the
6884 -- semantics of Ada complicate the analysis and lead to anomalies in
6885 -- the gcc back-end if the aggregate is not expanded into assignments.
6887 -- NOTE: This sets the global Static_Components to False in most, but
6888 -- not all, cases when it returns False.
6890 function Has_Per_Object_Constraint
(L
: List_Id
) return Boolean;
6891 -- Return True if any element of L has Has_Per_Object_Constraint set.
6892 -- L should be the Choices component of an N_Component_Association.
6894 function Has_Visible_Private_Ancestor
(Id
: E
) return Boolean;
6895 -- If any ancestor of the current type is private, the aggregate
6896 -- cannot be built in place. We cannot rely on Has_Private_Ancestor,
6897 -- because it will not be set when type and its parent are in the
6898 -- same scope, and the parent component needs expansion.
6900 function Top_Level_Aggregate
(N
: Node_Id
) return Node_Id
;
6901 -- For nested aggregates return the ultimate enclosing aggregate; for
6902 -- non-nested aggregates return N.
6904 ------------------------------
6905 -- Build_Back_End_Aggregate --
6906 ------------------------------
6908 procedure Build_Back_End_Aggregate
is
6911 Tag_Value
: Node_Id
;
6914 if Nkind
(N
) = N_Aggregate
then
6916 -- If the aggregate is static and can be handled by the back-end,
6917 -- nothing left to do.
6919 if Static_Components
then
6920 Set_Compile_Time_Known_Aggregate
(N
);
6921 Set_Expansion_Delayed
(N
, False);
6925 -- If no discriminants, nothing special to do
6927 if not Has_Discriminants
(Typ
) then
6930 -- Case of discriminants present
6932 elsif Is_Derived_Type
(Typ
) then
6934 -- For untagged types, non-stored discriminants are replaced with
6935 -- stored discriminants, which are the ones that gigi uses to
6936 -- describe the type and its components.
6938 Generate_Aggregate_For_Derived_Type
: declare
6939 procedure Prepend_Stored_Values
(T
: Entity_Id
);
6940 -- Scan the list of stored discriminants of the type, and add
6941 -- their values to the aggregate being built.
6943 ---------------------------
6944 -- Prepend_Stored_Values --
6945 ---------------------------
6947 procedure Prepend_Stored_Values
(T
: Entity_Id
) is
6949 First_Comp
: Node_Id
:= Empty
;
6952 Discr
:= First_Stored_Discriminant
(T
);
6953 while Present
(Discr
) loop
6955 Make_Component_Association
(Loc
,
6956 Choices
=> New_List
(
6957 New_Occurrence_Of
(Discr
, Loc
)),
6960 (Get_Discriminant_Value
6963 Discriminant_Constraint
(Typ
))));
6965 if No
(First_Comp
) then
6966 Prepend_To
(Component_Associations
(N
), New_Comp
);
6968 Insert_After
(First_Comp
, New_Comp
);
6971 First_Comp
:= New_Comp
;
6972 Next_Stored_Discriminant
(Discr
);
6974 end Prepend_Stored_Values
;
6978 Constraints
: constant List_Id
:= New_List
;
6982 Num_Disc
: Nat
:= 0;
6983 Num_Gird
: Nat
:= 0;
6985 -- Start of processing for Generate_Aggregate_For_Derived_Type
6988 -- Remove the associations for the discriminant of derived type
6991 First_Comp
: Node_Id
;
6994 First_Comp
:= First
(Component_Associations
(N
));
6995 while Present
(First_Comp
) loop
6999 if Ekind
(Entity
(First
(Choices
(Comp
)))) =
7003 Num_Disc
:= Num_Disc
+ 1;
7008 -- Insert stored discriminant associations in the correct
7009 -- order. If there are more stored discriminants than new
7010 -- discriminants, there is at least one new discriminant that
7011 -- constrains more than one of the stored discriminants. In
7012 -- this case we need to construct a proper subtype of the
7013 -- parent type, in order to supply values to all the
7014 -- components. Otherwise there is one-one correspondence
7015 -- between the constraints and the stored discriminants.
7017 Discr
:= First_Stored_Discriminant
(Base_Type
(Typ
));
7018 while Present
(Discr
) loop
7019 Num_Gird
:= Num_Gird
+ 1;
7020 Next_Stored_Discriminant
(Discr
);
7023 -- Case of more stored discriminants than new discriminants
7025 if Num_Gird
> Num_Disc
then
7027 -- Create a proper subtype of the parent type, which is the
7028 -- proper implementation type for the aggregate, and convert
7029 -- it to the intended target type.
7031 Discr
:= First_Stored_Discriminant
(Base_Type
(Typ
));
7032 while Present
(Discr
) loop
7035 (Get_Discriminant_Value
7038 Discriminant_Constraint
(Typ
)));
7040 Append
(New_Comp
, Constraints
);
7041 Next_Stored_Discriminant
(Discr
);
7045 Make_Subtype_Declaration
(Loc
,
7046 Defining_Identifier
=> Make_Temporary
(Loc
, 'T'),
7047 Subtype_Indication
=>
7048 Make_Subtype_Indication
(Loc
,
7050 New_Occurrence_Of
(Etype
(Base_Type
(Typ
)), Loc
),
7052 Make_Index_Or_Discriminant_Constraint
7053 (Loc
, Constraints
)));
7055 Insert_Action
(N
, Decl
);
7056 Prepend_Stored_Values
(Base_Type
(Typ
));
7058 Set_Etype
(N
, Defining_Identifier
(Decl
));
7061 Rewrite
(N
, Unchecked_Convert_To
(Typ
, N
));
7064 -- Case where we do not have fewer new discriminants than
7065 -- stored discriminants, so in this case we can simply use the
7066 -- stored discriminants of the subtype.
7069 Prepend_Stored_Values
(Typ
);
7071 end Generate_Aggregate_For_Derived_Type
;
7074 if Is_Tagged_Type
(Typ
) then
7076 -- In the tagged case, _parent and _tag component must be created
7078 -- Reset Null_Present unconditionally. Tagged records always have
7079 -- at least one field (the tag or the parent).
7081 Set_Null_Record_Present
(N
, False);
7083 -- When the current aggregate comes from the expansion of an
7084 -- extension aggregate, the parent expr is replaced by an
7085 -- aggregate formed by selected components of this expr.
7087 if Present
(Parent_Expr
) and then Is_Empty_List
(Comps
) then
7088 Comp
:= First_Component_Or_Discriminant
(Typ
);
7089 while Present
(Comp
) loop
7091 -- Skip all expander-generated components
7093 if not Comes_From_Source
(Original_Record_Component
(Comp
))
7099 Make_Selected_Component
(Loc
,
7101 Unchecked_Convert_To
(Typ
,
7102 Duplicate_Subexpr
(Parent_Expr
, True)),
7103 Selector_Name
=> New_Occurrence_Of
(Comp
, Loc
));
7106 Make_Component_Association
(Loc
,
7107 Choices
=> New_List
(
7108 New_Occurrence_Of
(Comp
, Loc
)),
7109 Expression
=> New_Comp
));
7111 Analyze_And_Resolve
(New_Comp
, Etype
(Comp
));
7114 Next_Component_Or_Discriminant
(Comp
);
7118 -- Compute the value for the Tag now, if the type is a root it
7119 -- will be included in the aggregate right away, otherwise it will
7120 -- be propagated to the parent aggregate.
7122 if Present
(Orig_Tag
) then
7123 Tag_Value
:= Orig_Tag
;
7125 elsif not Tagged_Type_Expansion
then
7131 (Node
(First_Elmt
(Access_Disp_Table
(Typ
))), Loc
);
7134 -- For a derived type, an aggregate for the parent is formed with
7135 -- all the inherited components.
7137 if Is_Derived_Type
(Typ
) then
7139 First_Comp
: Node_Id
;
7140 Parent_Comps
: List_Id
;
7141 Parent_Aggr
: Node_Id
;
7142 Parent_Name
: Node_Id
;
7145 -- Remove the inherited component association from the
7146 -- aggregate and store them in the parent aggregate
7148 First_Comp
:= First
(Component_Associations
(N
));
7149 Parent_Comps
:= New_List
;
7150 while Present
(First_Comp
)
7152 Scope
(Original_Record_Component
7153 (Entity
(First
(Choices
(First_Comp
))))) /=
7159 Append
(Comp
, Parent_Comps
);
7163 Make_Aggregate
(Loc
,
7164 Component_Associations
=> Parent_Comps
);
7165 Set_Etype
(Parent_Aggr
, Etype
(Base_Type
(Typ
)));
7167 -- Find the _parent component
7169 Comp
:= First_Component
(Typ
);
7170 while Chars
(Comp
) /= Name_uParent
loop
7171 Comp
:= Next_Component
(Comp
);
7174 Parent_Name
:= New_Occurrence_Of
(Comp
, Loc
);
7176 -- Insert the parent aggregate
7178 Prepend_To
(Component_Associations
(N
),
7179 Make_Component_Association
(Loc
,
7180 Choices
=> New_List
(Parent_Name
),
7181 Expression
=> Parent_Aggr
));
7183 -- Expand recursively the parent propagating the right Tag
7185 Expand_Record_Aggregate
7186 (Parent_Aggr
, Tag_Value
, Parent_Expr
);
7188 -- The ancestor part may be a nested aggregate that has
7189 -- delayed expansion: recheck now.
7191 if not Component_OK_For_Backend
then
7192 Convert_To_Assignments
(N
, Typ
);
7196 -- For a root type, the tag component is added (unless compiling
7197 -- for the VMs, where tags are implicit).
7199 elsif Tagged_Type_Expansion
then
7201 Tag_Name
: constant Node_Id
:=
7203 (First_Tag_Component
(Typ
), Loc
);
7204 Typ_Tag
: constant Entity_Id
:= RTE
(RE_Tag
);
7205 Conv_Node
: constant Node_Id
:=
7206 Unchecked_Convert_To
(Typ_Tag
, Tag_Value
);
7209 Set_Etype
(Conv_Node
, Typ_Tag
);
7210 Prepend_To
(Component_Associations
(N
),
7211 Make_Component_Association
(Loc
,
7212 Choices
=> New_List
(Tag_Name
),
7213 Expression
=> Conv_Node
));
7217 end Build_Back_End_Aggregate
;
7219 ----------------------------------------
7220 -- Compile_Time_Known_Composite_Value --
7221 ----------------------------------------
7223 function Compile_Time_Known_Composite_Value
7224 (N
: Node_Id
) return Boolean
7227 -- If we have an entity name, then see if it is the name of a
7228 -- constant and if so, test the corresponding constant value.
7230 if Is_Entity_Name
(N
) then
7232 E
: constant Entity_Id
:= Entity
(N
);
7235 if Ekind
(E
) /= E_Constant
then
7238 V
:= Constant_Value
(E
);
7240 and then Compile_Time_Known_Composite_Value
(V
);
7244 -- We have a value, see if it is compile time known
7247 if Nkind
(N
) = N_Aggregate
then
7248 return Compile_Time_Known_Aggregate
(N
);
7251 -- All other types of values are not known at compile time
7256 end Compile_Time_Known_Composite_Value
;
7258 ------------------------------
7259 -- Component_OK_For_Backend --
7260 ------------------------------
7262 function Component_OK_For_Backend
return Boolean is
7272 while Present
(C
) loop
7274 -- If the component has box initialization, expansion is needed
7275 -- and component is not ready for backend.
7277 if Box_Present
(C
) then
7281 if Nkind
(Expression
(C
)) = N_Qualified_Expression
then
7282 Expr_Q
:= Expression
(Expression
(C
));
7284 Expr_Q
:= Expression
(C
);
7287 -- Return False for array components whose bounds raise
7288 -- constraint error.
7291 Comp
: constant Entity_Id
:= First
(Choices
(C
));
7295 if Present
(Etype
(Comp
))
7296 and then Is_Array_Type
(Etype
(Comp
))
7298 Indx
:= First_Index
(Etype
(Comp
));
7299 while Present
(Indx
) loop
7300 if Nkind
(Type_Low_Bound
(Etype
(Indx
))) =
7301 N_Raise_Constraint_Error
7302 or else Nkind
(Type_High_Bound
(Etype
(Indx
))) =
7303 N_Raise_Constraint_Error
7308 Indx
:= Next_Index
(Indx
);
7313 -- Return False if the aggregate has any associations for tagged
7314 -- components that may require tag adjustment.
7316 -- These are cases where the source expression may have a tag that
7317 -- could differ from the component tag (e.g., can occur for type
7318 -- conversions and formal parameters). (Tag adjustment not needed
7319 -- if Tagged_Type_Expansion because object tags are implicit in
7322 if Is_Tagged_Type
(Etype
(Expr_Q
))
7324 (Nkind
(Expr_Q
) = N_Type_Conversion
7326 (Is_Entity_Name
(Expr_Q
)
7327 and then Is_Formal
(Entity
(Expr_Q
))))
7328 and then Tagged_Type_Expansion
7330 Static_Components
:= False;
7333 elsif Is_Delayed_Aggregate
(Expr_Q
) then
7334 Static_Components
:= False;
7337 elsif Nkind
(Expr_Q
) = N_Quantified_Expression
then
7338 Static_Components
:= False;
7341 elsif Possible_Bit_Aligned_Component
(Expr_Q
) then
7342 Static_Components
:= False;
7345 elsif Modify_Tree_For_C
7346 and then Nkind
(C
) = N_Component_Association
7347 and then Has_Per_Object_Constraint
(Choices
(C
))
7349 Static_Components
:= False;
7352 elsif Modify_Tree_For_C
7353 and then Nkind
(Expr_Q
) = N_Identifier
7354 and then Is_Array_Type
(Etype
(Expr_Q
))
7356 Static_Components
:= False;
7359 elsif Modify_Tree_For_C
7360 and then Nkind
(Expr_Q
) = N_Type_Conversion
7361 and then Is_Array_Type
(Etype
(Expr_Q
))
7363 Static_Components
:= False;
7367 if Is_Elementary_Type
(Etype
(Expr_Q
)) then
7368 if not Compile_Time_Known_Value
(Expr_Q
) then
7369 Static_Components
:= False;
7372 elsif not Compile_Time_Known_Composite_Value
(Expr_Q
) then
7373 Static_Components
:= False;
7375 if Is_Private_Type
(Etype
(Expr_Q
))
7376 and then Has_Discriminants
(Etype
(Expr_Q
))
7386 end Component_OK_For_Backend
;
7388 -------------------------------
7389 -- Has_Per_Object_Constraint --
7390 -------------------------------
7392 function Has_Per_Object_Constraint
(L
: List_Id
) return Boolean is
7393 N
: Node_Id
:= First
(L
);
7395 while Present
(N
) loop
7396 if Is_Entity_Name
(N
)
7397 and then Present
(Entity
(N
))
7398 and then Has_Per_Object_Constraint
(Entity
(N
))
7407 end Has_Per_Object_Constraint
;
7409 -----------------------------------
7410 -- Has_Visible_Private_Ancestor --
7411 -----------------------------------
7413 function Has_Visible_Private_Ancestor
(Id
: E
) return Boolean is
7414 R
: constant Entity_Id
:= Root_Type
(Id
);
7415 T1
: Entity_Id
:= Id
;
7419 if Is_Private_Type
(T1
) then
7429 end Has_Visible_Private_Ancestor
;
7431 -------------------------
7432 -- Top_Level_Aggregate --
7433 -------------------------
7435 function Top_Level_Aggregate
(N
: Node_Id
) return Node_Id
is
7440 while Present
(Parent
(Aggr
))
7441 and then Nkind_In
(Parent
(Aggr
), N_Aggregate
,
7442 N_Component_Association
)
7444 Aggr
:= Parent
(Aggr
);
7448 end Top_Level_Aggregate
;
7452 Top_Level_Aggr
: constant Node_Id
:= Top_Level_Aggregate
(N
);
7454 -- Start of processing for Expand_Record_Aggregate
7457 -- If the aggregate is to be assigned to an atomic/VFA variable, we have
7458 -- to prevent a piecemeal assignment even if the aggregate is to be
7459 -- expanded. We create a temporary for the aggregate, and assign the
7460 -- temporary instead, so that the back end can generate an atomic move
7463 if Is_Atomic_VFA_Aggregate
(N
) then
7466 -- No special management required for aggregates used to initialize
7467 -- statically allocated dispatch tables
7469 elsif Is_Static_Dispatch_Table_Aggregate
(N
) then
7473 -- Ada 2005 (AI-318-2): We need to convert to assignments if components
7474 -- are build-in-place function calls. The assignments will each turn
7475 -- into a build-in-place function call. If components are all static,
7476 -- we can pass the aggregate to the back end regardless of limitedness.
7478 -- Extension aggregates, aggregates in extended return statements, and
7479 -- aggregates for C++ imported types must be expanded.
7481 if Ada_Version
>= Ada_2005
and then Is_Limited_View
(Typ
) then
7482 if not Nkind_In
(Parent
(N
), N_Component_Association
,
7483 N_Object_Declaration
)
7485 Convert_To_Assignments
(N
, Typ
);
7487 elsif Nkind
(N
) = N_Extension_Aggregate
7488 or else Convention
(Typ
) = Convention_CPP
7490 Convert_To_Assignments
(N
, Typ
);
7492 elsif not Size_Known_At_Compile_Time
(Typ
)
7493 or else not Component_OK_For_Backend
7494 or else not Static_Components
7496 Convert_To_Assignments
(N
, Typ
);
7498 -- In all other cases, build a proper aggregate to be handled by
7502 Build_Back_End_Aggregate
;
7505 -- Gigi doesn't properly handle temporaries of variable size so we
7506 -- generate it in the front-end
7508 elsif not Size_Known_At_Compile_Time
(Typ
)
7509 and then Tagged_Type_Expansion
7511 Convert_To_Assignments
(N
, Typ
);
7513 -- An aggregate used to initialize a controlled object must be turned
7514 -- into component assignments as the components themselves may require
7515 -- finalization actions such as adjustment.
7517 elsif Needs_Finalization
(Typ
) then
7518 Convert_To_Assignments
(N
, Typ
);
7520 -- Ada 2005 (AI-287): In case of default initialized components we
7521 -- convert the aggregate into assignments.
7523 elsif Has_Default_Init_Comps
(N
) then
7524 Convert_To_Assignments
(N
, Typ
);
7528 elsif not Component_OK_For_Backend
then
7529 Convert_To_Assignments
(N
, Typ
);
7531 -- If an ancestor is private, some components are not inherited and we
7532 -- cannot expand into a record aggregate.
7534 elsif Has_Visible_Private_Ancestor
(Typ
) then
7535 Convert_To_Assignments
(N
, Typ
);
7537 -- ??? The following was done to compile fxacc00.ads in the ACVCs. Gigi
7538 -- is not able to handle the aggregate for Late_Request.
7540 elsif Is_Tagged_Type
(Typ
) and then Has_Discriminants
(Typ
) then
7541 Convert_To_Assignments
(N
, Typ
);
7543 -- If the tagged types covers interface types we need to initialize all
7544 -- hidden components containing pointers to secondary dispatch tables.
7546 elsif Is_Tagged_Type
(Typ
) and then Has_Interfaces
(Typ
) then
7547 Convert_To_Assignments
(N
, Typ
);
7549 -- If some components are mutable, the size of the aggregate component
7550 -- may be distinct from the default size of the type component, so
7551 -- we need to expand to insure that the back-end copies the proper
7552 -- size of the data. However, if the aggregate is the initial value of
7553 -- a constant, the target is immutable and might be built statically
7554 -- if components are appropriate.
7556 elsif Has_Mutable_Components
(Typ
)
7558 (Nkind
(Parent
(Top_Level_Aggr
)) /= N_Object_Declaration
7559 or else not Constant_Present
(Parent
(Top_Level_Aggr
))
7560 or else not Static_Components
)
7562 Convert_To_Assignments
(N
, Typ
);
7564 -- If the type involved has bit aligned components, then we are not sure
7565 -- that the back end can handle this case correctly.
7567 elsif Type_May_Have_Bit_Aligned_Components
(Typ
) then
7568 Convert_To_Assignments
(N
, Typ
);
7570 -- When generating C, only generate an aggregate when declaring objects
7571 -- since C does not support aggregates in e.g. assignment statements.
7573 elsif Modify_Tree_For_C
and then not Is_CCG_Supported_Aggregate
(N
) then
7574 Convert_To_Assignments
(N
, Typ
);
7576 -- In all other cases, build a proper aggregate to be handled by gigi
7579 Build_Back_End_Aggregate
;
7581 end Expand_Record_Aggregate
;
7583 ----------------------------
7584 -- Has_Default_Init_Comps --
7585 ----------------------------
7587 function Has_Default_Init_Comps
(N
: Node_Id
) return Boolean is
7588 Comps
: constant List_Id
:= Component_Associations
(N
);
7593 pragma Assert
(Nkind_In
(N
, N_Aggregate
, N_Extension_Aggregate
));
7599 if Has_Self_Reference
(N
) then
7603 -- Check if any direct component has default initialized components
7606 while Present
(C
) loop
7607 if Box_Present
(C
) then
7614 -- Recursive call in case of aggregate expression
7617 while Present
(C
) loop
7618 Expr
:= Expression
(C
);
7621 and then Nkind_In
(Expr
, N_Aggregate
, N_Extension_Aggregate
)
7622 and then Has_Default_Init_Comps
(Expr
)
7631 end Has_Default_Init_Comps
;
7633 ----------------------------------------
7634 -- Is_Build_In_Place_Aggregate_Return --
7635 ----------------------------------------
7637 function Is_Build_In_Place_Aggregate_Return
(N
: Node_Id
) return Boolean is
7638 P
: Node_Id
:= Parent
(N
);
7641 while Nkind
(P
) = N_Qualified_Expression
loop
7645 if Nkind
(P
) = N_Simple_Return_Statement
then
7648 elsif Nkind
(Parent
(P
)) = N_Extended_Return_Statement
then
7656 Is_Build_In_Place_Function
7657 (Return_Applies_To
(Return_Statement_Entity
(P
)));
7658 end Is_Build_In_Place_Aggregate_Return
;
7660 --------------------------
7661 -- Is_Delayed_Aggregate --
7662 --------------------------
7664 function Is_Delayed_Aggregate
(N
: Node_Id
) return Boolean is
7665 Node
: Node_Id
:= N
;
7666 Kind
: Node_Kind
:= Nkind
(Node
);
7669 if Kind
= N_Qualified_Expression
then
7670 Node
:= Expression
(Node
);
7671 Kind
:= Nkind
(Node
);
7674 if not Nkind_In
(Kind
, N_Aggregate
, N_Extension_Aggregate
) then
7677 return Expansion_Delayed
(Node
);
7679 end Is_Delayed_Aggregate
;
7681 --------------------------------
7682 -- Is_CCG_Supported_Aggregate --
7683 --------------------------------
7685 function Is_CCG_Supported_Aggregate
7686 (N
: Node_Id
) return Boolean
7688 In_Obj_Decl
: Boolean := False;
7689 P
: Node_Id
:= Parent
(N
);
7692 while Present
(P
) loop
7693 if Nkind
(P
) = N_Object_Declaration
then
7694 In_Obj_Decl
:= True;
7700 -- Cases where aggregates are supported by the CCG backend
7703 if Nkind
(Parent
(N
)) = N_Object_Declaration
then
7706 elsif Nkind
(Parent
(N
)) = N_Qualified_Expression
7707 and then Nkind_In
(Parent
(Parent
(N
)), N_Allocator
,
7708 N_Object_Declaration
)
7715 end Is_CCG_Supported_Aggregate
;
7717 ----------------------------------------
7718 -- Is_Static_Dispatch_Table_Aggregate --
7719 ----------------------------------------
7721 function Is_Static_Dispatch_Table_Aggregate
(N
: Node_Id
) return Boolean is
7722 Typ
: constant Entity_Id
:= Base_Type
(Etype
(N
));
7725 return Building_Static_Dispatch_Tables
7726 and then Tagged_Type_Expansion
7727 and then RTU_Loaded
(Ada_Tags
)
7729 -- Avoid circularity when rebuilding the compiler
7731 and then Cunit_Entity
(Get_Source_Unit
(N
)) /= RTU_Entity
(Ada_Tags
)
7732 and then (Typ
= RTE
(RE_Dispatch_Table_Wrapper
)
7734 Typ
= RTE
(RE_Address_Array
)
7736 Typ
= RTE
(RE_Type_Specific_Data
)
7738 Typ
= RTE
(RE_Tag_Table
)
7740 (RTE_Available
(RE_Interface_Data
)
7741 and then Typ
= RTE
(RE_Interface_Data
))
7743 (RTE_Available
(RE_Interfaces_Array
)
7744 and then Typ
= RTE
(RE_Interfaces_Array
))
7746 (RTE_Available
(RE_Interface_Data_Element
)
7747 and then Typ
= RTE
(RE_Interface_Data_Element
)));
7748 end Is_Static_Dispatch_Table_Aggregate
;
7750 -----------------------------
7751 -- Is_Two_Dim_Packed_Array --
7752 -----------------------------
7754 function Is_Two_Dim_Packed_Array
(Typ
: Entity_Id
) return Boolean is
7755 C
: constant Int
:= UI_To_Int
(Component_Size
(Typ
));
7757 return Number_Dimensions
(Typ
) = 2
7758 and then Is_Bit_Packed_Array
(Typ
)
7759 and then (C
= 1 or else C
= 2 or else C
= 4);
7760 end Is_Two_Dim_Packed_Array
;
7762 --------------------
7763 -- Late_Expansion --
7764 --------------------
7766 function Late_Expansion
7769 Target
: Node_Id
) return List_Id
7771 Aggr_Code
: List_Id
;
7774 if Is_Array_Type
(Etype
(N
)) then
7776 Build_Array_Aggr_Code
7778 Ctype
=> Component_Type
(Etype
(N
)),
7779 Index
=> First_Index
(Typ
),
7781 Scalar_Comp
=> Is_Scalar_Type
(Component_Type
(Typ
)),
7782 Indexes
=> No_List
);
7784 -- Directly or indirectly (e.g. access protected procedure) a record
7787 Aggr_Code
:= Build_Record_Aggr_Code
(N
, Typ
, Target
);
7790 -- Save the last assignment statement associated with the aggregate
7791 -- when building a controlled object. This reference is utilized by
7792 -- the finalization machinery when marking an object as successfully
7795 if Needs_Finalization
(Typ
)
7796 and then Is_Entity_Name
(Target
)
7797 and then Present
(Entity
(Target
))
7798 and then Ekind_In
(Entity
(Target
), E_Constant
, E_Variable
)
7800 Set_Last_Aggregate_Assignment
(Entity
(Target
), Last
(Aggr_Code
));
7806 ----------------------------------
7807 -- Make_OK_Assignment_Statement --
7808 ----------------------------------
7810 function Make_OK_Assignment_Statement
7813 Expression
: Node_Id
) return Node_Id
7816 Set_Assignment_OK
(Name
);
7817 return Make_Assignment_Statement
(Sloc
, Name
, Expression
);
7818 end Make_OK_Assignment_Statement
;
7820 -----------------------
7821 -- Number_Of_Choices --
7822 -----------------------
7824 function Number_Of_Choices
(N
: Node_Id
) return Nat
is
7828 Nb_Choices
: Nat
:= 0;
7831 if Present
(Expressions
(N
)) then
7835 Assoc
:= First
(Component_Associations
(N
));
7836 while Present
(Assoc
) loop
7837 Choice
:= First
(Choice_List
(Assoc
));
7838 while Present
(Choice
) loop
7839 if Nkind
(Choice
) /= N_Others_Choice
then
7840 Nb_Choices
:= Nb_Choices
+ 1;
7850 end Number_Of_Choices
;
7852 ------------------------------------
7853 -- Packed_Array_Aggregate_Handled --
7854 ------------------------------------
7856 -- The current version of this procedure will handle at compile time
7857 -- any array aggregate that meets these conditions:
7859 -- One and two dimensional, bit packed
7860 -- Underlying packed type is modular type
7861 -- Bounds are within 32-bit Int range
7862 -- All bounds and values are static
7864 -- Note: for now, in the 2-D case, we only handle component sizes of
7865 -- 1, 2, 4 (cases where an integral number of elements occupies a byte).
7867 function Packed_Array_Aggregate_Handled
(N
: Node_Id
) return Boolean is
7868 Loc
: constant Source_Ptr
:= Sloc
(N
);
7869 Typ
: constant Entity_Id
:= Etype
(N
);
7870 Ctyp
: constant Entity_Id
:= Component_Type
(Typ
);
7872 Not_Handled
: exception;
7873 -- Exception raised if this aggregate cannot be handled
7876 -- Handle one- or two dimensional bit packed array
7878 if not Is_Bit_Packed_Array
(Typ
)
7879 or else Number_Dimensions
(Typ
) > 2
7884 -- If two-dimensional, check whether it can be folded, and transformed
7885 -- into a one-dimensional aggregate for the Packed_Array_Impl_Type of
7886 -- the original type.
7888 if Number_Dimensions
(Typ
) = 2 then
7889 return Two_Dim_Packed_Array_Handled
(N
);
7892 if not Is_Modular_Integer_Type
(Packed_Array_Impl_Type
(Typ
)) then
7896 if not Is_Scalar_Type
(Ctyp
) then
7901 Csiz
: constant Nat
:= UI_To_Int
(Component_Size
(Typ
));
7905 -- Bounds of index type
7909 -- Values of bounds if compile time known
7911 function Get_Component_Val
(N
: Node_Id
) return Uint
;
7912 -- Given a expression value N of the component type Ctyp, returns a
7913 -- value of Csiz (component size) bits representing this value. If
7914 -- the value is nonstatic or any other reason exists why the value
7915 -- cannot be returned, then Not_Handled is raised.
7917 -----------------------
7918 -- Get_Component_Val --
7919 -----------------------
7921 function Get_Component_Val
(N
: Node_Id
) return Uint
is
7925 -- We have to analyze the expression here before doing any further
7926 -- processing here. The analysis of such expressions is deferred
7927 -- till expansion to prevent some problems of premature analysis.
7929 Analyze_And_Resolve
(N
, Ctyp
);
7931 -- Must have a compile time value. String literals have to be
7932 -- converted into temporaries as well, because they cannot easily
7933 -- be converted into their bit representation.
7935 if not Compile_Time_Known_Value
(N
)
7936 or else Nkind
(N
) = N_String_Literal
7941 Val
:= Expr_Rep_Value
(N
);
7943 -- Adjust for bias, and strip proper number of bits
7945 if Has_Biased_Representation
(Ctyp
) then
7946 Val
:= Val
- Expr_Value
(Type_Low_Bound
(Ctyp
));
7949 return Val
mod Uint_2
** Csiz
;
7950 end Get_Component_Val
;
7952 -- Here we know we have a one dimensional bit packed array
7955 Get_Index_Bounds
(First_Index
(Typ
), Lo
, Hi
);
7957 -- Cannot do anything if bounds are dynamic
7959 if not Compile_Time_Known_Value
(Lo
)
7961 not Compile_Time_Known_Value
(Hi
)
7966 -- Or are silly out of range of int bounds
7968 Lob
:= Expr_Value
(Lo
);
7969 Hib
:= Expr_Value
(Hi
);
7971 if not UI_Is_In_Int_Range
(Lob
)
7973 not UI_Is_In_Int_Range
(Hib
)
7978 -- At this stage we have a suitable aggregate for handling at compile
7979 -- time. The only remaining checks are that the values of expressions
7980 -- in the aggregate are compile-time known (checks are performed by
7981 -- Get_Component_Val), and that any subtypes or ranges are statically
7984 -- If the aggregate is not fully positional at this stage, then
7985 -- convert it to positional form. Either this will fail, in which
7986 -- case we can do nothing, or it will succeed, in which case we have
7987 -- succeeded in handling the aggregate and transforming it into a
7988 -- modular value, or it will stay an aggregate, in which case we
7989 -- have failed to create a packed value for it.
7991 if Present
(Component_Associations
(N
)) then
7992 Convert_To_Positional
7993 (N
, Max_Others_Replicate
=> 64, Handle_Bit_Packed
=> True);
7994 return Nkind
(N
) /= N_Aggregate
;
7997 -- Otherwise we are all positional, so convert to proper value
8000 Lov
: constant Int
:= UI_To_Int
(Lob
);
8001 Hiv
: constant Int
:= UI_To_Int
(Hib
);
8003 Len
: constant Nat
:= Int
'Max (0, Hiv
- Lov
+ 1);
8004 -- The length of the array (number of elements)
8006 Aggregate_Val
: Uint
;
8007 -- Value of aggregate. The value is set in the low order bits of
8008 -- this value. For the little-endian case, the values are stored
8009 -- from low-order to high-order and for the big-endian case the
8010 -- values are stored from high-order to low-order. Note that gigi
8011 -- will take care of the conversions to left justify the value in
8012 -- the big endian case (because of left justified modular type
8013 -- processing), so we do not have to worry about that here.
8016 -- Integer literal for resulting constructed value
8019 -- Shift count from low order for next value
8022 -- Shift increment for loop
8025 -- Next expression from positional parameters of aggregate
8027 Left_Justified
: Boolean;
8028 -- Set True if we are filling the high order bits of the target
8029 -- value (i.e. the value is left justified).
8032 -- For little endian, we fill up the low order bits of the target
8033 -- value. For big endian we fill up the high order bits of the
8034 -- target value (which is a left justified modular value).
8036 Left_Justified
:= Bytes_Big_Endian
;
8038 -- Switch justification if using -gnatd8
8040 if Debug_Flag_8
then
8041 Left_Justified
:= not Left_Justified
;
8044 -- Switch justfification if reverse storage order
8046 if Reverse_Storage_Order
(Base_Type
(Typ
)) then
8047 Left_Justified
:= not Left_Justified
;
8050 if Left_Justified
then
8051 Shift
:= Csiz
* (Len
- 1);
8058 -- Loop to set the values
8061 Aggregate_Val
:= Uint_0
;
8063 Expr
:= First
(Expressions
(N
));
8064 Aggregate_Val
:= Get_Component_Val
(Expr
) * Uint_2
** Shift
;
8066 for J
in 2 .. Len
loop
8067 Shift
:= Shift
+ Incr
;
8070 Aggregate_Val
+ Get_Component_Val
(Expr
) * Uint_2
** Shift
;
8074 -- Now we can rewrite with the proper value
8076 Lit
:= Make_Integer_Literal
(Loc
, Intval
=> Aggregate_Val
);
8077 Set_Print_In_Hex
(Lit
);
8079 -- Construct the expression using this literal. Note that it is
8080 -- important to qualify the literal with its proper modular type
8081 -- since universal integer does not have the required range and
8082 -- also this is a left justified modular type, which is important
8083 -- in the big-endian case.
8086 Unchecked_Convert_To
(Typ
,
8087 Make_Qualified_Expression
(Loc
,
8089 New_Occurrence_Of
(Packed_Array_Impl_Type
(Typ
), Loc
),
8090 Expression
=> Lit
)));
8092 Analyze_And_Resolve
(N
, Typ
);
8100 end Packed_Array_Aggregate_Handled
;
8102 ----------------------------
8103 -- Has_Mutable_Components --
8104 ----------------------------
8106 function Has_Mutable_Components
(Typ
: Entity_Id
) return Boolean is
8110 Comp
:= First_Component
(Typ
);
8111 while Present
(Comp
) loop
8112 if Is_Record_Type
(Etype
(Comp
))
8113 and then Has_Discriminants
(Etype
(Comp
))
8114 and then not Is_Constrained
(Etype
(Comp
))
8119 Next_Component
(Comp
);
8123 end Has_Mutable_Components
;
8125 ------------------------------
8126 -- Initialize_Discriminants --
8127 ------------------------------
8129 procedure Initialize_Discriminants
(N
: Node_Id
; Typ
: Entity_Id
) is
8130 Loc
: constant Source_Ptr
:= Sloc
(N
);
8131 Bas
: constant Entity_Id
:= Base_Type
(Typ
);
8132 Par
: constant Entity_Id
:= Etype
(Bas
);
8133 Decl
: constant Node_Id
:= Parent
(Par
);
8137 if Is_Tagged_Type
(Bas
)
8138 and then Is_Derived_Type
(Bas
)
8139 and then Has_Discriminants
(Par
)
8140 and then Has_Discriminants
(Bas
)
8141 and then Number_Discriminants
(Bas
) /= Number_Discriminants
(Par
)
8142 and then Nkind
(Decl
) = N_Full_Type_Declaration
8143 and then Nkind
(Type_Definition
(Decl
)) = N_Record_Definition
8145 Present
(Variant_Part
(Component_List
(Type_Definition
(Decl
))))
8146 and then Nkind
(N
) /= N_Extension_Aggregate
8149 -- Call init proc to set discriminants.
8150 -- There should eventually be a special procedure for this ???
8152 Ref
:= New_Occurrence_Of
(Defining_Identifier
(N
), Loc
);
8153 Insert_Actions_After
(N
,
8154 Build_Initialization_Call
(Sloc
(N
), Ref
, Typ
));
8156 end Initialize_Discriminants
;
8163 (Obj_Type
: Entity_Id
;
8164 Typ
: Entity_Id
) return Boolean
8166 L1
, L2
, H1
, H2
: Node_Id
;
8169 -- No sliding if the type of the object is not established yet, if it is
8170 -- an unconstrained type whose actual subtype comes from the aggregate,
8171 -- or if the two types are identical.
8173 if not Is_Array_Type
(Obj_Type
) then
8176 elsif not Is_Constrained
(Obj_Type
) then
8179 elsif Typ
= Obj_Type
then
8183 -- Sliding can only occur along the first dimension
8185 Get_Index_Bounds
(First_Index
(Typ
), L1
, H1
);
8186 Get_Index_Bounds
(First_Index
(Obj_Type
), L2
, H2
);
8188 if not Is_OK_Static_Expression
(L1
) or else
8189 not Is_OK_Static_Expression
(L2
) or else
8190 not Is_OK_Static_Expression
(H1
) or else
8191 not Is_OK_Static_Expression
(H2
)
8195 return Expr_Value
(L1
) /= Expr_Value
(L2
)
8197 Expr_Value
(H1
) /= Expr_Value
(H2
);
8202 ---------------------------------
8203 -- Process_Transient_Component --
8204 ---------------------------------
8206 procedure Process_Transient_Component
8208 Comp_Typ
: Entity_Id
;
8209 Init_Expr
: Node_Id
;
8210 Fin_Call
: out Node_Id
;
8211 Hook_Clear
: out Node_Id
;
8212 Aggr
: Node_Id
:= Empty
;
8213 Stmts
: List_Id
:= No_List
)
8215 procedure Add_Item
(Item
: Node_Id
);
8216 -- Insert arbitrary node Item into the tree depending on the values of
8223 procedure Add_Item
(Item
: Node_Id
) is
8225 if Present
(Aggr
) then
8226 Insert_Action
(Aggr
, Item
);
8228 pragma Assert
(Present
(Stmts
));
8229 Append_To
(Stmts
, Item
);
8235 Hook_Assign
: Node_Id
;
8236 Hook_Decl
: Node_Id
;
8240 Res_Typ
: Entity_Id
;
8242 -- Start of processing for Process_Transient_Component
8245 -- Add the access type, which provides a reference to the function
8246 -- result. Generate:
8248 -- type Res_Typ is access all Comp_Typ;
8250 Res_Typ
:= Make_Temporary
(Loc
, 'A');
8251 Set_Ekind
(Res_Typ
, E_General_Access_Type
);
8252 Set_Directly_Designated_Type
(Res_Typ
, Comp_Typ
);
8255 (Make_Full_Type_Declaration
(Loc
,
8256 Defining_Identifier
=> Res_Typ
,
8258 Make_Access_To_Object_Definition
(Loc
,
8259 All_Present
=> True,
8260 Subtype_Indication
=> New_Occurrence_Of
(Comp_Typ
, Loc
))));
8262 -- Add the temporary which captures the result of the function call.
8265 -- Res : constant Res_Typ := Init_Expr'Reference;
8267 -- Note that this temporary is effectively a transient object because
8268 -- its lifetime is bounded by the current array or record component.
8270 Res_Id
:= Make_Temporary
(Loc
, 'R');
8271 Set_Ekind
(Res_Id
, E_Constant
);
8272 Set_Etype
(Res_Id
, Res_Typ
);
8274 -- Mark the transient object as successfully processed to avoid double
8277 Set_Is_Finalized_Transient
(Res_Id
);
8279 -- Signal the general finalization machinery that this transient object
8280 -- should not be considered for finalization actions because its cleanup
8281 -- will be performed by Process_Transient_Component_Completion.
8283 Set_Is_Ignored_Transient
(Res_Id
);
8286 Make_Object_Declaration
(Loc
,
8287 Defining_Identifier
=> Res_Id
,
8288 Constant_Present
=> True,
8289 Object_Definition
=> New_Occurrence_Of
(Res_Typ
, Loc
),
8291 Make_Reference
(Loc
, New_Copy_Tree
(Init_Expr
)));
8293 Add_Item
(Res_Decl
);
8295 -- Construct all pieces necessary to hook and finalize the transient
8298 Build_Transient_Object_Statements
8299 (Obj_Decl
=> Res_Decl
,
8300 Fin_Call
=> Fin_Call
,
8301 Hook_Assign
=> Hook_Assign
,
8302 Hook_Clear
=> Hook_Clear
,
8303 Hook_Decl
=> Hook_Decl
,
8304 Ptr_Decl
=> Ptr_Decl
);
8306 -- Add the access type which provides a reference to the transient
8307 -- result. Generate:
8309 -- type Ptr_Typ is access all Comp_Typ;
8311 Add_Item
(Ptr_Decl
);
8313 -- Add the temporary which acts as a hook to the transient result.
8316 -- Hook : Ptr_Typ := null;
8318 Add_Item
(Hook_Decl
);
8320 -- Attach the transient result to the hook. Generate:
8322 -- Hook := Ptr_Typ (Res);
8324 Add_Item
(Hook_Assign
);
8326 -- The original initialization expression now references the value of
8327 -- the temporary function result. Generate:
8332 Make_Explicit_Dereference
(Loc
,
8333 Prefix
=> New_Occurrence_Of
(Res_Id
, Loc
)));
8334 end Process_Transient_Component
;
8336 --------------------------------------------
8337 -- Process_Transient_Component_Completion --
8338 --------------------------------------------
8340 procedure Process_Transient_Component_Completion
8344 Hook_Clear
: Node_Id
;
8347 Exceptions_OK
: constant Boolean :=
8348 not Restriction_Active
(No_Exception_Propagation
);
8351 pragma Assert
(Present
(Hook_Clear
));
8353 -- Generate the following code if exception propagation is allowed:
8356 -- Abort : constant Boolean := Triggered_By_Abort;
8358 -- Abort : constant Boolean := False; -- no abort
8360 -- E : Exception_Occurrence;
8361 -- Raised : Boolean := False;
8368 -- [Deep_]Finalize (Res.all);
8372 -- if not Raised then
8374 -- Save_Occurrence (E,
8375 -- Get_Curent_Excep.all.all);
8381 -- if Raised and then not Abort then
8382 -- Raise_From_Controlled_Operation (E);
8386 if Exceptions_OK
then
8387 Abort_And_Exception
: declare
8388 Blk_Decls
: constant List_Id
:= New_List
;
8389 Blk_Stmts
: constant List_Id
:= New_List
;
8390 Fin_Stmts
: constant List_Id
:= New_List
;
8392 Fin_Data
: Finalization_Exception_Data
;
8395 -- Create the declarations of the two flags and the exception
8398 Build_Object_Declarations
(Fin_Data
, Blk_Decls
, Loc
);
8403 if Abort_Allowed
then
8404 Append_To
(Blk_Stmts
,
8405 Build_Runtime_Call
(Loc
, RE_Abort_Defer
));
8408 -- Wrap the hook clear and the finalization call in order to trap
8409 -- a potential exception.
8411 Append_To
(Fin_Stmts
, Hook_Clear
);
8413 if Present
(Fin_Call
) then
8414 Append_To
(Fin_Stmts
, Fin_Call
);
8417 Append_To
(Blk_Stmts
,
8418 Make_Block_Statement
(Loc
,
8419 Handled_Statement_Sequence
=>
8420 Make_Handled_Sequence_Of_Statements
(Loc
,
8421 Statements
=> Fin_Stmts
,
8422 Exception_Handlers
=> New_List
(
8423 Build_Exception_Handler
(Fin_Data
)))));
8428 if Abort_Allowed
then
8429 Append_To
(Blk_Stmts
,
8430 Build_Runtime_Call
(Loc
, RE_Abort_Undefer
));
8433 -- Reraise the potential exception with a proper "upgrade" to
8434 -- Program_Error if needed.
8436 Append_To
(Blk_Stmts
, Build_Raise_Statement
(Fin_Data
));
8438 -- Wrap everything in a block
8441 Make_Block_Statement
(Loc
,
8442 Declarations
=> Blk_Decls
,
8443 Handled_Statement_Sequence
=>
8444 Make_Handled_Sequence_Of_Statements
(Loc
,
8445 Statements
=> Blk_Stmts
)));
8446 end Abort_And_Exception
;
8448 -- Generate the following code if exception propagation is not allowed
8449 -- and aborts are allowed:
8454 -- [Deep_]Finalize (Res.all);
8456 -- Abort_Undefer_Direct;
8459 elsif Abort_Allowed
then
8460 Abort_Only
: declare
8461 Blk_Stmts
: constant List_Id
:= New_List
;
8464 Append_To
(Blk_Stmts
, Build_Runtime_Call
(Loc
, RE_Abort_Defer
));
8465 Append_To
(Blk_Stmts
, Hook_Clear
);
8467 if Present
(Fin_Call
) then
8468 Append_To
(Blk_Stmts
, Fin_Call
);
8472 Build_Abort_Undefer_Block
(Loc
,
8477 -- Otherwise generate:
8480 -- [Deep_]Finalize (Res.all);
8483 Append_To
(Stmts
, Hook_Clear
);
8485 if Present
(Fin_Call
) then
8486 Append_To
(Stmts
, Fin_Call
);
8489 end Process_Transient_Component_Completion
;
8491 ---------------------
8492 -- Sort_Case_Table --
8493 ---------------------
8495 procedure Sort_Case_Table
(Case_Table
: in out Case_Table_Type
) is
8496 L
: constant Int
:= Case_Table
'First;
8497 U
: constant Int
:= Case_Table
'Last;
8505 T
:= Case_Table
(K
+ 1);
8509 and then Expr_Value
(Case_Table
(J
- 1).Choice_Lo
) >
8510 Expr_Value
(T
.Choice_Lo
)
8512 Case_Table
(J
) := Case_Table
(J
- 1);
8516 Case_Table
(J
) := T
;
8519 end Sort_Case_Table
;
8521 ----------------------------
8522 -- Static_Array_Aggregate --
8523 ----------------------------
8525 function Static_Array_Aggregate
(N
: Node_Id
) return Boolean is
8526 function Is_Static_Component
(Nod
: Node_Id
) return Boolean;
8527 -- Return True if Nod has a compile-time known value and can be passed
8528 -- as is to the back-end without further expansion.
8530 ---------------------------
8531 -- Is_Static_Component --
8532 ---------------------------
8534 function Is_Static_Component
(Nod
: Node_Id
) return Boolean is
8536 if Nkind_In
(Nod
, N_Integer_Literal
, N_Real_Literal
) then
8539 elsif Is_Entity_Name
(Nod
)
8540 and then Present
(Entity
(Nod
))
8541 and then Ekind
(Entity
(Nod
)) = E_Enumeration_Literal
8545 elsif Nkind
(Nod
) = N_Aggregate
8546 and then Compile_Time_Known_Aggregate
(Nod
)
8553 end Is_Static_Component
;
8557 Bounds
: constant Node_Id
:= Aggregate_Bounds
(N
);
8558 Typ
: constant Entity_Id
:= Etype
(N
);
8565 -- Start of processing for Static_Array_Aggregate
8568 if Is_Packed
(Typ
) or else Has_Discriminants
(Component_Type
(Typ
)) then
8573 and then Nkind
(Bounds
) = N_Range
8574 and then Nkind
(Low_Bound
(Bounds
)) = N_Integer_Literal
8575 and then Nkind
(High_Bound
(Bounds
)) = N_Integer_Literal
8577 Lo
:= Low_Bound
(Bounds
);
8578 Hi
:= High_Bound
(Bounds
);
8580 if No
(Component_Associations
(N
)) then
8582 -- Verify that all components are static
8584 Expr
:= First
(Expressions
(N
));
8585 while Present
(Expr
) loop
8586 if not Is_Static_Component
(Expr
) then
8596 -- We allow only a single named association, either a static
8597 -- range or an others_clause, with a static expression.
8599 Expr
:= First
(Component_Associations
(N
));
8601 if Present
(Expressions
(N
)) then
8604 elsif Present
(Next
(Expr
)) then
8607 elsif Present
(Next
(First
(Choice_List
(Expr
)))) then
8611 -- The aggregate is static if all components are literals,
8612 -- or else all its components are static aggregates for the
8613 -- component type. We also limit the size of a static aggregate
8614 -- to prevent runaway static expressions.
8616 if not Is_Static_Component
(Expression
(Expr
)) then
8620 if not Aggr_Size_OK
(N
, Typ
) then
8624 -- Create a positional aggregate with the right number of
8625 -- copies of the expression.
8627 Agg
:= Make_Aggregate
(Sloc
(N
), New_List
, No_List
);
8629 for I
in UI_To_Int
(Intval
(Lo
)) .. UI_To_Int
(Intval
(Hi
))
8631 Append_To
(Expressions
(Agg
), New_Copy
(Expression
(Expr
)));
8633 -- The copied expression must be analyzed and resolved.
8634 -- Besides setting the type, this ensures that static
8635 -- expressions are appropriately marked as such.
8638 (Last
(Expressions
(Agg
)), Component_Type
(Typ
));
8641 Set_Aggregate_Bounds
(Agg
, Bounds
);
8642 Set_Etype
(Agg
, Typ
);
8645 Set_Compile_Time_Known_Aggregate
(N
);
8654 end Static_Array_Aggregate
;
8656 ----------------------------------
8657 -- Two_Dim_Packed_Array_Handled --
8658 ----------------------------------
8660 function Two_Dim_Packed_Array_Handled
(N
: Node_Id
) return Boolean is
8661 Loc
: constant Source_Ptr
:= Sloc
(N
);
8662 Typ
: constant Entity_Id
:= Etype
(N
);
8663 Ctyp
: constant Entity_Id
:= Component_Type
(Typ
);
8664 Comp_Size
: constant Int
:= UI_To_Int
(Component_Size
(Typ
));
8665 Packed_Array
: constant Entity_Id
:=
8666 Packed_Array_Impl_Type
(Base_Type
(Typ
));
8669 -- Expression in original aggregate
8672 -- One-dimensional subaggregate
8676 -- For now, only deal with cases where an integral number of elements
8677 -- fit in a single byte. This includes the most common boolean case.
8679 if not (Comp_Size
= 1 or else
8680 Comp_Size
= 2 or else
8686 Convert_To_Positional
8687 (N
, Max_Others_Replicate
=> 64, Handle_Bit_Packed
=> True);
8689 -- Verify that all components are static
8691 if Nkind
(N
) = N_Aggregate
8692 and then Compile_Time_Known_Aggregate
(N
)
8696 -- The aggregate may have been reanalyzed and converted already
8698 elsif Nkind
(N
) /= N_Aggregate
then
8701 -- If component associations remain, the aggregate is not static
8703 elsif Present
(Component_Associations
(N
)) then
8707 One_Dim
:= First
(Expressions
(N
));
8708 while Present
(One_Dim
) loop
8709 if Present
(Component_Associations
(One_Dim
)) then
8713 One_Comp
:= First
(Expressions
(One_Dim
));
8714 while Present
(One_Comp
) loop
8715 if not Is_OK_Static_Expression
(One_Comp
) then
8726 -- Two-dimensional aggregate is now fully positional so pack one
8727 -- dimension to create a static one-dimensional array, and rewrite
8728 -- as an unchecked conversion to the original type.
8731 Byte_Size
: constant Int
:= UI_To_Int
(Component_Size
(Packed_Array
));
8732 -- The packed array type is a byte array
8735 -- Number of components accumulated in current byte
8738 -- Assembled list of packed values for equivalent aggregate
8741 -- Integer value of component
8744 -- Step size for packing
8747 -- Endian-dependent start position for packing
8750 -- Current insertion position
8753 -- Component of packed array being assembled
8760 -- Account for endianness. See corresponding comment in
8761 -- Packed_Array_Aggregate_Handled concerning the following.
8765 xor Reverse_Storage_Order
(Base_Type
(Typ
))
8767 Init_Shift
:= Byte_Size
- Comp_Size
;
8774 -- Iterate over each subaggregate
8776 Shift
:= Init_Shift
;
8777 One_Dim
:= First
(Expressions
(N
));
8778 while Present
(One_Dim
) loop
8779 One_Comp
:= First
(Expressions
(One_Dim
));
8780 while Present
(One_Comp
) loop
8781 if Packed_Num
= Byte_Size
/ Comp_Size
then
8783 -- Byte is complete, add to list of expressions
8785 Append
(Make_Integer_Literal
(Sloc
(One_Dim
), Val
), Comps
);
8787 Shift
:= Init_Shift
;
8791 Comp_Val
:= Expr_Rep_Value
(One_Comp
);
8793 -- Adjust for bias, and strip proper number of bits
8795 if Has_Biased_Representation
(Ctyp
) then
8796 Comp_Val
:= Comp_Val
- Expr_Value
(Type_Low_Bound
(Ctyp
));
8799 Comp_Val
:= Comp_Val
mod Uint_2
** Comp_Size
;
8800 Val
:= UI_To_Int
(Val
+ Comp_Val
* Uint_2
** Shift
);
8801 Shift
:= Shift
+ Incr
;
8802 One_Comp
:= Next
(One_Comp
);
8803 Packed_Num
:= Packed_Num
+ 1;
8807 One_Dim
:= Next
(One_Dim
);
8810 if Packed_Num
> 0 then
8812 -- Add final incomplete byte if present
8814 Append
(Make_Integer_Literal
(Sloc
(One_Dim
), Val
), Comps
);
8818 Unchecked_Convert_To
(Typ
,
8819 Make_Qualified_Expression
(Loc
,
8820 Subtype_Mark
=> New_Occurrence_Of
(Packed_Array
, Loc
),
8821 Expression
=> Make_Aggregate
(Loc
, Expressions
=> Comps
))));
8822 Analyze_And_Resolve
(N
);
8825 end Two_Dim_Packed_Array_Handled
;